]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types: generate various source files from types2 files
authorRobert Griesemer <gri@golang.org>
Fri, 6 Jan 2023 00:56:42 +0000 (16:56 -0800)
committerGopher Robot <gobot@golang.org>
Tue, 17 Jan 2023 19:55:35 +0000 (19:55 +0000)
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>

22 files changed:
src/go/types/chan.go
src/go/types/context.go
src/go/types/gccgosizes.go
src/go/types/generate.go [new file with mode: 0644]
src/go/types/generator.go [new file with mode: 0644]
src/go/types/lookup.go
src/go/types/main_test.go
src/go/types/map.go
src/go/types/named.go
src/go/types/objset.go
src/go/types/pointer.go
src/go/types/selection.go
src/go/types/sizes.go
src/go/types/slice.go
src/go/types/subst.go
src/go/types/termlist.go
src/go/types/termlist_test.go
src/go/types/tuple.go
src/go/types/typelists.go
src/go/types/typeparam.go
src/go/types/typeterm.go
src/go/types/validtype.go

index 1f7b72be309c868c450151bdbf7100e70244f884..565e9d2e9d1f34caab56bba5e6e23a3d6189ece2 100644 (file)
@@ -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) }
index 15756b062d97664e9bdeae9ac5e930e7cefa9568..81b2f139ebe9f38ce27396d94937b7c6edcb3e49 100644 (file)
@@ -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.
index 9d077cc5a657b74da6216c860d2efe4df666c1ed..d98105c69a94399d0ee3b1de2c0280e0fc316dda 100644 (file)
@@ -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 (file)
index 0000000..6759f11
--- /dev/null
@@ -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 (file)
index 0000000..7b5c7f6
--- /dev/null
@@ -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
+       })
+}
index 4eedcc23a1957937a9ace528fbfb2f46445b862b..7ed367ee864bfdd354800b7aa940d96edda73747 100644 (file)
@@ -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.
index 73d7d183f71e78115d474d687db5a34aa20bbaba..b0630947c13d4b2985acf2475cf07b3cf9fcb265 100644 (file)
@@ -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.
index 01e13b214e966412debe21f1f64e2d35d65abc66..9827e8c1477920aa63c12875ef6d53115fdd06bb 100644 (file)
@@ -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.
index 04638abbdc483e1cb0dfcfed72654e99bf8c101f..f55b55e08a6749c8af8aa17053513bf95ae460e0 100644 (file)
@@ -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.
index 55eb74addbae5e69fa19d15f9bd7966e793631b3..4e7aaac500b3febf68b9d3f1f678286b4fde0a9f 100644 (file)
@@ -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.
index 6352ee57e2c832ca0e3eea4aa2106a9aeba42675..091824c55e061a665489894fec112cfb4cc770c5 100644 (file)
@@ -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) }
index 09c304d37856861d9ba7167bf0ef4fc535fd281d..bc26f39b3fc8aebbf0d9a75d7e4f8331704742de 100644 (file)
@@ -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.
index cb5253b453270c3f029274147eaa09a7b675ebde..235718481ca45c30f485cd1f4d5587a6a7190511 100644 (file)
@@ -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.
index debdd81586cce0fb39c4df553250f8b952c286bb..0fb8476d248c9179cd6d17beee2372908693a4d8 100644 (file)
@@ -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) }
index 5876b61edfadc9030bb38475cab852713bc47543..9f0eb975b01e6966fa5967838c54575d79479049 100644 (file)
@@ -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...)
                        }
                }
index 83a02eefac3944df86bbfb7c7da681bca2e94c76..46de92459bdc320d6a1fc56bd924519d4d0268eb 100644 (file)
@@ -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.
index 0ff687ebda69850d7c7a5e119f2da214420dc4e4..7499fc6b9ef52ca90e8f17b64e787f2e48066b86 100644 (file)
@@ -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.
index e85c5aa81bef566c2af58fbb0d1b144dce537147..8ccf0289a3f6634a879b475c9855d4ed37125bf1 100644 (file)
@@ -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.
index 0f241356c33f111bf3bef1f6126474f2ff23efbe..864a34fa54206cba1bb7eccc924fd334fea798fb 100644 (file)
@@ -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.
index 40d96ac9470ab7c6f3d47f97f1ecd9f641a16318..c573fa86335b3dbb3d4d557c4e224c49b416dd45 100644 (file)
@@ -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
index a7b896962721e4b8e1a7233b6cbdbf76c4baffea..d862add06e7370fb94785ecc5ed46816f9046e13 100644 (file)
@@ -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.
index d62c3983f0a525f987dc08a348edf37559d62383..5fd5f73cfa89837b996ad291898434e76c5b47a5 100644 (file)
@@ -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.