From: Robert Griesemer Date: Fri, 6 Jan 2023 00:56:42 +0000 (-0800) Subject: go/types: generate various source files from types2 files X-Git-Tag: go1.21rc1~1924 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f9cba430b228c81e5b5f1ec30e0f6183141f3c0c;p=gostls13.git go/types: generate various source files from types2 files Starting point for more code sharing. Change-Id: Ia6bc3ba54476a5202bfd5f89cef09bacb3f4f3b9 Reviewed-on: https://go-review.googlesource.com/c/go/+/460761 Reviewed-by: Robert Griesemer TryBot-Result: Gopher Robot Run-TryBot: Robert Griesemer Reviewed-by: Robert Findley Auto-Submit: Robert Griesemer --- diff --git a/src/go/types/chan.go b/src/go/types/chan.go index 1f7b72be30..565e9d2e9d 100644 --- a/src/go/types/chan.go +++ b/src/go/types/chan.go @@ -1,3 +1,5 @@ +// 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. @@ -31,5 +33,5 @@ func (c *Chan) Dir() ChanDir { return c.dir } // 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) } diff --git a/src/go/types/context.go b/src/go/types/context.go index 15756b062d..81b2f139eb 100644 --- a/src/go/types/context.go +++ b/src/go/types/context.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/gccgosizes.go b/src/go/types/gccgosizes.go index 9d077cc5a6..d98105c69a 100644 --- a/src/go/types/gccgosizes.go +++ b/src/go/types/gccgosizes.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/generate.go b/src/go/types/generate.go new file mode 100644 index 0000000000..6759f117b9 --- /dev/null +++ b/src/go/types/generate.go @@ -0,0 +1,8 @@ +// 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 diff --git a/src/go/types/generator.go b/src/go/types/generator.go new file mode 100644 index 0000000000..7b5c7f60e2 --- /dev/null +++ b/src/go/types/generator.go @@ -0,0 +1,134 @@ +// 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 + }) +} diff --git a/src/go/types/lookup.go b/src/go/types/lookup.go index 4eedcc23a1..7ed367ee86 100644 --- a/src/go/types/lookup.go +++ b/src/go/types/lookup.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/main_test.go b/src/go/types/main_test.go index 73d7d183f7..b0630947c1 100644 --- a/src/go/types/main_test.go +++ b/src/go/types/main_test.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/map.go b/src/go/types/map.go index 01e13b214e..9827e8c147 100644 --- a/src/go/types/map.go +++ b/src/go/types/map.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/named.go b/src/go/types/named.go index 04638abbdc..f55b55e08a 100644 --- a/src/go/types/named.go +++ b/src/go/types/named.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/objset.go b/src/go/types/objset.go index 55eb74addb..4e7aaac500 100644 --- a/src/go/types/objset.go +++ b/src/go/types/objset.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/pointer.go b/src/go/types/pointer.go index 6352ee57e2..091824c55e 100644 --- a/src/go/types/pointer.go +++ b/src/go/types/pointer.go @@ -1,3 +1,5 @@ +// 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. @@ -15,5 +17,5 @@ func NewPointer(elem Type) *Pointer { return &Pointer{base: elem} } // 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) } diff --git a/src/go/types/selection.go b/src/go/types/selection.go index 09c304d378..bc26f39b3f 100644 --- a/src/go/types/selection.go +++ b/src/go/types/selection.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/sizes.go b/src/go/types/sizes.go index cb5253b453..235718481c 100644 --- a/src/go/types/sizes.go +++ b/src/go/types/sizes.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/slice.go b/src/go/types/slice.go index debdd81586..0fb8476d24 100644 --- a/src/go/types/slice.go +++ b/src/go/types/slice.go @@ -1,3 +1,5 @@ +// 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. @@ -15,5 +17,5 @@ func NewSlice(elem Type) *Slice { return &Slice{elem: elem} } // 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) } diff --git a/src/go/types/subst.go b/src/go/types/subst.go index 5876b61edf..9f0eb975b0 100644 --- a/src/go/types/subst.go +++ b/src/go/types/subst.go @@ -1,3 +1,5 @@ +// 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. @@ -46,9 +48,9 @@ func (m substMap) lookup(tpar *TypeParam) Type { } // 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. @@ -144,7 +146,7 @@ func (subst *subster) typ(typ Type) Type { 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, @@ -203,13 +205,13 @@ func (subst *subster) typ(typ Type) Type { 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...) } } diff --git a/src/go/types/termlist.go b/src/go/types/termlist.go index 83a02eefac..46de92459b 100644 --- a/src/go/types/termlist.go +++ b/src/go/types/termlist.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/termlist_test.go b/src/go/types/termlist_test.go index 0ff687ebda..7499fc6b9e 100644 --- a/src/go/types/termlist_test.go +++ b/src/go/types/termlist_test.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/tuple.go b/src/go/types/tuple.go index e85c5aa81b..8ccf0289a3 100644 --- a/src/go/types/tuple.go +++ b/src/go/types/tuple.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/typelists.go b/src/go/types/typelists.go index 0f241356c3..864a34fa54 100644 --- a/src/go/types/typelists.go +++ b/src/go/types/typelists.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/typeparam.go b/src/go/types/typeparam.go index 40d96ac947..c573fa8633 100644 --- a/src/go/types/typeparam.go +++ b/src/go/types/typeparam.go @@ -1,12 +1,12 @@ +// 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 @@ -58,15 +58,15 @@ func (check *Checker) newTypeParam(obj *TypeName, constraint Type) *TypeParam { 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 diff --git a/src/go/types/typeterm.go b/src/go/types/typeterm.go index a7b8969627..d862add06e 100644 --- a/src/go/types/typeterm.go +++ b/src/go/types/typeterm.go @@ -1,3 +1,5 @@ +// 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. diff --git a/src/go/types/validtype.go b/src/go/types/validtype.go index d62c3983f0..5fd5f73cfa 100644 --- a/src/go/types/validtype.go +++ b/src/go/types/validtype.go @@ -1,3 +1,5 @@ +// 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.