Starting point for more code sharing.
Change-Id: Ia6bc3ba54476a5202bfd5f89cef09bacb3f4f3b9
Reviewed-on: https://go-review.googlesource.com/c/go/+/460761
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2011 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.
// Elem returns the element type of channel c.
func (c *Chan) Elem() Type { return c.elem }
-func (t *Chan) Underlying() Type { return t }
-func (t *Chan) String() string { return TypeString(t, nil) }
+func (c *Chan) Underlying() Type { return c }
+func (c *Chan) String() string { return TypeString(c, nil) }
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2021 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2019 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.
--- /dev/null
+// Copyright 2023 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.
+
+// This file exists only to drive go:generate.
+//go:generate go run generator.go
+
+package types
--- /dev/null
+// Copyright 2023 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.
+
+//go:build ignore
+
+// This file implements a custom generator to create various go/types
+// source files from the corresponding types2 files.
+
+package main
+
+import (
+ "bytes"
+ "go/ast"
+ "go/format"
+ "go/parser"
+ "go/token"
+ "log"
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+)
+
+const (
+ srcDir = "cmd/compile/internal/types2"
+ dstDir = "go/types"
+)
+
+var fset = token.NewFileSet()
+
+func main() {
+ for filename, action := range filemap {
+ // parse src
+ srcFilename := filepath.FromSlash(runtime.GOROOT() + "/src/" + srcDir + "/" + filename)
+ file, err := parser.ParseFile(fset, srcFilename, nil, parser.ParseComments)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // fix package name
+ file.Name.Name = strings.ReplaceAll(file.Name.Name, "types2", "types")
+
+ // rewrite AST as needed
+ if action != nil {
+ action(file)
+ }
+
+ // format AST
+ var buf bytes.Buffer
+ buf.WriteString("// Code generated by \"go run generator.go\"; DO NOT EDIT.\n\n")
+ if err := format.Node(&buf, fset, file); err != nil {
+ log.Fatal(err)
+ }
+
+ // write dst
+ dstFilename := filepath.FromSlash(runtime.GOROOT() + "/src/" + dstDir + "/" + filename)
+ if err := os.WriteFile(dstFilename, buf.Bytes(), 0o644); err != nil {
+ log.Fatal(err)
+ }
+ }
+}
+
+type action func(in *ast.File)
+
+var filemap = map[string]action{
+ "chan.go": nil,
+ "context.go": nil,
+ "gccgosizes.go": nil,
+ "lookup.go": nil,
+ "main_test.go": nil,
+ "map.go": nil,
+ "named.go": func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) },
+ "objset.go": nil,
+ "pointer.go": nil,
+ "selection.go": nil,
+ "sizes.go": func(f *ast.File) { rename(f, "IsSyncAtomicAlign64", "isSyncAtomicAlign64") },
+ "slice.go": nil,
+ "subst.go": func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) },
+ "termlist.go": nil,
+ "termlist_test.go": nil,
+ "tuple.go": nil,
+ "typelists.go": nil,
+ "typeparam.go": nil,
+ "typeterm.go": nil,
+ "validtype.go": nil,
+}
+
+// TODO(gri) We should be able to make these rewriters more configurable/composable.
+// For now this is a good starting point.
+
+func rename(f *ast.File, from, to string) {
+ ast.Inspect(f, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.Ident:
+ if n.Name == from {
+ n.Name = to
+ }
+ return false
+ }
+ return true
+ })
+}
+
+func fixTokenPos(f *ast.File) {
+ ast.Inspect(f, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.ImportSpec:
+ if n.Path.Kind == token.STRING && n.Path.Value == `"cmd/compile/internal/syntax"` {
+ n.Path.Value = `"go/token"`
+ return false
+ }
+ case *ast.SelectorExpr:
+ if x, _ := n.X.(*ast.Ident); x != nil && x.Name == "syntax" && n.Sel.Name == "Pos" {
+ x.Name = "token"
+ return false
+ }
+ }
+ return true
+ })
+}
+
+func fixTraceSel(f *ast.File) {
+ ast.Inspect(f, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.SelectorExpr:
+ if n.Sel.Name == "Trace" {
+ n.Sel.Name = "trace"
+ return false
+ }
+ }
+ return true
+ })
+}
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2013 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2022 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2011 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2011 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2013 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2011 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.
// Elem returns the element type for the given pointer p.
func (p *Pointer) Elem() Type { return p.base }
-func (t *Pointer) Underlying() Type { return t }
-func (t *Pointer) String() string { return TypeString(t, nil) }
+func (p *Pointer) Underlying() Type { return p }
+func (p *Pointer) String() string { return TypeString(p, nil) }
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2013 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2013 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2011 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.
// Elem returns the element type of slice s.
func (s *Slice) Elem() Type { return s.elem }
-func (t *Slice) Underlying() Type { return t }
-func (t *Slice) String() string { return TypeString(t, nil) }
+func (s *Slice) Underlying() Type { return s }
+func (s *Slice) String() string { return TypeString(s, nil) }
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2018 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.
}
// subst returns the type typ with its type parameters tpars replaced by the
-// corresponding type arguments targs, recursively. subst is pure in the sense
-// that it doesn't modify the incoming type. If a substitution took place, the
-// result type is different from the incoming type.
+// corresponding type arguments targs, recursively. subst doesn't modify the
+// incoming type. If a substitution took place, the result type is different
+// from the incoming type.
//
// If expanding is non-nil, it is the instance type currently being expanded.
// One of expanding or ctxt must be non-nil.
if params != t.params || results != t.results {
return &Signature{
rparams: t.rparams,
- // TODO(rFindley) why can't we nil out tparams here, rather than in instantiate?
+ // TODO(gri) why can't we nil out tparams here, rather than in instantiate?
tparams: t.tparams,
// instantiated signatures have a nil scope
recv: recv,
case *Named:
// dump is for debugging
- dump := func(string, ...any) {}
+ dump := func(string, ...interface{}) {}
if subst.check != nil && subst.check.conf.trace {
subst.check.indent++
defer func() {
subst.check.indent--
}()
- dump = func(format string, args ...any) {
+ dump = func(format string, args ...interface{}) {
subst.check.trace(subst.pos, format, args...)
}
}
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2021 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2021 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2011 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2021 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2011 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 types
-import (
- "sync/atomic"
-)
+import "sync/atomic"
// Note: This is a uint32 rather than a uint64 because the
// respective 64 bit atomic instructions are not available
return typ
}
+// Obj returns the type name for the type parameter t.
+func (t *TypeParam) Obj() *TypeName { return t.obj }
+
// Index returns the index of the type param within its param list, or -1 if
// the type parameter has not yet been bound to a type.
func (t *TypeParam) Index() int {
return t.index
}
-// Obj returns the type name for t.
-func (t *TypeParam) Obj() *TypeName { return t.obj }
-
// Constraint returns the type constraint specified for t.
func (t *TypeParam) Constraint() Type {
return t.bound
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2021 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.
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2022 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.