cmd/fix: add gotypes module

Adjusts for the move from golang.org/x/tools/go/types and .../go/exact
to go/types and go/constant in the main repository.

Change-Id: I0da7248c540939e3e9b09c915b0a296937f1be73
Reviewed-on: https://go-review.googlesource.com/12284
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/src/cmd/fix/gotypes.go b/src/cmd/fix/gotypes.go
new file mode 100644
index 0000000..8c7b466
--- /dev/null
+++ b/src/cmd/fix/gotypes.go
@@ -0,0 +1,75 @@
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+	"go/ast"
+	"strconv"
+)
+
+func init() {
+	register(gotypesFix)
+}
+
+var gotypesFix = fix{
+	"gotypes",
+	"2015-07-16",
+	gotypes,
+	`Change imports of golang.org/x/tools/go/{exact,types} to go/{constant,types}`,
+}
+
+func gotypes(f *ast.File) bool {
+	truth := fixGoTypes(f)
+	if fixGoExact(f) {
+		truth = true
+	}
+	return truth
+}
+
+func fixGoTypes(f *ast.File) bool {
+	return rewriteImport(f, "golang.org/x/tools/go/types", "go/types")
+}
+
+func fixGoExact(f *ast.File) bool {
+	// This one is harder because the import name changes.
+	// First find the import spec.
+	var importSpec *ast.ImportSpec
+	walk(f, func(n interface{}) {
+		if importSpec != nil {
+			return
+		}
+		spec, ok := n.(*ast.ImportSpec)
+		if !ok {
+			return
+		}
+		path, err := strconv.Unquote(spec.Path.Value)
+		if err != nil {
+			return
+		}
+		if path == "golang.org/x/tools/go/exact" {
+			importSpec = spec
+		}
+
+	})
+	if importSpec == nil {
+		return false
+	}
+
+	// We are about to rename exact.* to constant.*, but constant is a common
+	// name. See if it will conflict. This is a hack but it is effective.
+	exists := renameTop(f, "constant", "constant")
+	suffix := ""
+	if exists {
+		suffix = "_"
+	}
+	// Now we need to rename all the uses of the import. RewriteImport
+	// affects renameTop, but not vice versa, so do them in this order.
+	renameTop(f, "exact", "constant"+suffix)
+	rewriteImport(f, "golang.org/x/tools/go/exact", "go/constant")
+	// renameTop will also rewrite the imported package name. Fix that;
+	// we know it should be missing.
+	importSpec.Name = nil
+	return true
+}
diff --git a/src/cmd/fix/gotypes_test.go b/src/cmd/fix/gotypes_test.go
new file mode 100644
index 0000000..1ecb7a2
--- /dev/null
+++ b/src/cmd/fix/gotypes_test.go
@@ -0,0 +1,89 @@
+// Copyright 2012 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func init() {
+	addTestCases(gotypesTests, gotypes)
+}
+
+var gotypesTests = []testCase{
+	{
+		Name: "gotypes.0",
+		In: `package main
+
+import "golang.org/x/tools/go/types"
+import "golang.org/x/tools/go/exact"
+
+var _ = exact.Kind
+
+func f() {
+	_ = exact.MakeBool(true)
+}
+`,
+		Out: `package main
+
+import "go/types"
+import "go/constant"
+
+var _ = constant.Kind
+
+func f() {
+	_ = constant.MakeBool(true)
+}
+`,
+	},
+	{
+		Name: "gotypes.1",
+		In: `package main
+
+import "golang.org/x/tools/go/types"
+import foo "golang.org/x/tools/go/exact"
+
+var _ = foo.Kind
+
+func f() {
+	_ = foo.MakeBool(true)
+}
+`,
+		Out: `package main
+
+import "go/types"
+import "go/constant"
+
+var _ = foo.Kind
+
+func f() {
+	_ = foo.MakeBool(true)
+}
+`,
+	},
+	{
+		Name: "gotypes.0",
+		In: `package main
+
+import "golang.org/x/tools/go/types"
+import "golang.org/x/tools/go/exact"
+
+var _ = exact.Kind
+var constant = 23 // Use of new package name.
+
+func f() {
+	_ = exact.MakeBool(true)
+}
+`,
+		Out: `package main
+
+import "go/types"
+import "go/constant"
+
+var _ = constant_.Kind
+var constant = 23 // Use of new package name.
+
+func f() {
+	_ = constant_.MakeBool(true)
+}
+`,
+	},
+}