From 8ed5a161ce85ee6d1e5fdef097a57593cfe15185 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 9 Jan 2023 14:19:27 -0800 Subject: [PATCH] go/types: generate more files from types2 sources Change-Id: I5e00b4367d7288867b03702d0c76b8210c125668 Reviewed-on: https://go-review.googlesource.com/c/go/+/461083 Reviewed-by: Robert Findley Reviewed-by: Robert Griesemer TryBot-Result: Gopher Robot Auto-Submit: Robert Griesemer Run-TryBot: Robert Griesemer --- src/go/types/array.go | 6 ++- src/go/types/basic.go | 6 ++- src/go/types/context_test.go | 2 + src/go/types/generator.go | 86 ++++++++++++++++++++++++-------- src/go/types/instantiate_test.go | 7 +-- src/go/types/object.go | 51 +++++++++++++++++-- src/go/types/package.go | 4 +- src/go/types/predicates.go | 16 +++--- src/go/types/sizeof_test.go | 2 +- src/go/types/typeterm_test.go | 2 + src/go/types/universe.go | 2 + 11 files changed, 142 insertions(+), 42 deletions(-) diff --git a/src/go/types/array.go b/src/go/types/array.go index 5b28474bb3..759e2f2e67 100644 --- a/src/go/types/array.go +++ b/src/go/types/array.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. @@ -21,5 +23,5 @@ func (a *Array) Len() int64 { return a.len } // Elem returns element type of array a. func (a *Array) Elem() Type { return a.elem } -func (t *Array) Underlying() Type { return t } -func (t *Array) String() string { return TypeString(t, nil) } +func (a *Array) Underlying() Type { return a } +func (a *Array) String() string { return TypeString(a, nil) } diff --git a/src/go/types/basic.go b/src/go/types/basic.go index 215923f657..7d6decdb1a 100644 --- a/src/go/types/basic.go +++ b/src/go/types/basic.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. @@ -78,5 +80,5 @@ func (b *Basic) Info() BasicInfo { return b.info } // Name returns the name of basic type b. func (b *Basic) Name() string { return b.name } -func (t *Basic) Underlying() Type { return t } -func (t *Basic) String() string { return TypeString(t, nil) } +func (b *Basic) Underlying() Type { return b } +func (b *Basic) String() string { return TypeString(b, nil) } diff --git a/src/go/types/context_test.go b/src/go/types/context_test.go index b323651e29..1340bf2053 100644 --- a/src/go/types/context_test.go +++ b/src/go/types/context_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/generator.go b/src/go/types/generator.go index 7b5c7f60e2..2971b30511 100644 --- a/src/go/types/generator.go +++ b/src/go/types/generator.go @@ -64,32 +64,43 @@ func main() { 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, + "array.go": nil, + "basic.go": nil, + "chan.go": nil, + "context.go": nil, + "context_test.go": nil, + "gccgosizes.go": nil, + "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) }, + "lookup.go": nil, + "main_test.go": nil, + "map.go": nil, + "named.go": func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) }, + "object.go": func(f *ast.File) { fixTokenPos(f); renameIdent(f, "NewTypeNameLazy", "_NewTypeNameLazy") }, + "objset.go": nil, + "package.go": nil, + "pointer.go": nil, + "predicates.go": nil, + "selection.go": nil, + "sizes.go": func(f *ast.File) { renameIdent(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_test.go": nil, + "typeterm.go": nil, + "universe.go": fixGlobalTypVarDecl, + "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) { +// renameIdent renames an identifier. +// Note: This doesn't change the use of the identifier in comments. +func renameIdent(f *ast.File, from, to string) { ast.Inspect(f, func(n ast.Node) bool { switch n := n.(type) { case *ast.Ident: @@ -102,6 +113,22 @@ func rename(f *ast.File, from, to string) { }) } +// renameImportPath renames an import path. +func renameImportPath(f *ast.File, from, to string) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.ImportSpec: + if n.Path.Kind == token.STRING && n.Path.Value == from { + n.Path.Value = to + return false + } + } + return true + }) +} + +// fixTokenPos changes imports of "cmd/compile/internal/syntax" to "go/token" +// and uses of syntax.Pos to token.Pos. func fixTokenPos(f *ast.File) { ast.Inspect(f, func(n ast.Node) bool { switch n := n.(type) { @@ -120,6 +147,7 @@ func fixTokenPos(f *ast.File) { }) } +// fixTraceSel renames uses of x.Trace to x.trace, where x for any x with a Trace field. func fixTraceSel(f *ast.File) { ast.Inspect(f, func(n ast.Node) bool { switch n := n.(type) { @@ -132,3 +160,19 @@ func fixTraceSel(f *ast.File) { return true }) } + +// fixGlobalTypVarDecl changes the global Typ variable from an array to a slice +// (in types2 we use an array for efficiency, in go/types it's a slice and we +// cannot change that). +func fixGlobalTypVarDecl(f *ast.File) { + ast.Inspect(f, func(n ast.Node) bool { + switch n := n.(type) { + case *ast.ValueSpec: + if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 { + n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil + return false + } + } + return true + }) +} diff --git a/src/go/types/instantiate_test.go b/src/go/types/instantiate_test.go index 8b551187bd..54e725fc97 100644 --- a/src/go/types/instantiate_test.go +++ b/src/go/types/instantiate_test.go @@ -1,7 +1,8 @@ +// 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. - package types_test import ( @@ -134,15 +135,12 @@ func TestInstantiateEquality(t *testing.T) { func TestInstantiateNonEquality(t *testing.T) { const src = "package p; type T[P any] int" - pkg1 := mustTypecheck(".", src, nil) pkg2 := mustTypecheck(".", src, nil) - // We consider T1 and T2 to be distinct types, so their instances should not // be deduplicated by the context. T1 := pkg1.Scope().Lookup("T").Type().(*Named) T2 := pkg2.Scope().Lookup("T").Type().(*Named) - ctxt := NewContext() res1, err := Instantiate(ctxt, T1, []Type{Typ[Int]}, false) if err != nil { @@ -152,7 +150,6 @@ func TestInstantiateNonEquality(t *testing.T) { if err != nil { t.Fatal(err) } - if res1 == res2 { t.Errorf("instance from pkg1 (%s) is pointer-equivalent to instance from pkg2 (%s)", res1, res2) } diff --git a/src/go/types/object.go b/src/go/types/object.go index 3013552bf5..74b630af9e 100644 --- a/src/go/types/object.go +++ b/src/go/types/object.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. @@ -9,6 +11,8 @@ import ( "fmt" "go/constant" "go/token" + "unicode" + "unicode/utf8" ) // An Object describes a named language entity such as a package, @@ -57,10 +61,15 @@ type Object interface { setScopePos(pos token.Pos) } +func isExported(name string) bool { + ch, _ := utf8.DecodeRuneInString(name) + return unicode.IsUpper(ch) +} + // Id returns name if it is exported, otherwise it // returns the name qualified with the package path. func Id(pkg *Package, name string) string { - if token.IsExported(name) { + if isExported(name) { return name } // unexported names need the package path for differentiation @@ -140,7 +149,7 @@ func (obj *object) Type() Type { return obj.typ } // Exported reports whether the object is exported (starts with a capital letter). // It doesn't take into account whether the object is in a local (function) scope // or not. -func (obj *object) Exported() bool { return token.IsExported(obj.name) } +func (obj *object) Exported() bool { return isExported(obj.name) } // Id is a wrapper for Id(obj.Pkg(), obj.Name()). func (obj *object) Id() string { return Id(obj.pkg, obj.name) } @@ -178,6 +187,42 @@ func (obj *object) sameId(pkg *Package, name string) bool { return pkg.path == obj.pkg.path } +// less reports whether object a is ordered before object b. +// +// Objects are ordered nil before non-nil, exported before +// non-exported, then by name, and finally (for non-exported +// functions) by package path. +func (a *object) less(b *object) bool { + if a == b { + return false + } + + // Nil before non-nil. + if a == nil { + return true + } + if b == nil { + return false + } + + // Exported functions before non-exported. + ea := isExported(a.name) + eb := isExported(b.name) + if ea != eb { + return ea + } + + // Order by name and then (for non-exported names) by package. + if a.name != b.name { + return a.name < b.name + } + if !ea { + return a.pkg.path < b.pkg.path + } + + return false +} + // A PkgName represents an imported Go package. // PkgNames don't have a type. type PkgName struct { @@ -229,7 +274,7 @@ func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName { return &TypeName{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}} } -// _NewTypeNameLazy returns a new defined type like NewTypeName, but it +// NewTypeNameLazy returns a new defined type like NewTypeName, but it // lazily calls resolve to finish constructing the Named object. func _NewTypeNameLazy(pos token.Pos, pkg *Package, name string, load func(named *Named) (tparams []*TypeParam, underlying Type, methods []*Func)) *TypeName { obj := NewTypeName(pos, pkg, name, nil) diff --git a/src/go/types/package.go b/src/go/types/package.go index 201f603234..7a64aa9fe4 100644 --- a/src/go/types/package.go +++ b/src/go/types/package.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. @@ -13,8 +15,8 @@ type Package struct { path string name string scope *Scope - complete bool imports []*Package + complete bool fake bool // scope lookup errors are silently dropped if package is fake (internal use only) cgo bool // uses of this package will be rewritten into uses of declarations from _cgo_gotypes.go } diff --git a/src/go/types/predicates.go b/src/go/types/predicates.go index b26b5beb9c..c80d41d702 100644 --- a/src/go/types/predicates.go +++ b/src/go/types/predicates.go @@ -1,3 +1,5 @@ +// Code generated by "go run generator.go"; DO NOT EDIT. + // 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. @@ -34,13 +36,13 @@ func isBasic(t Type, info BasicInfo) bool { // allX is an optimized version of isX(coreType(t)) (which // is the same as underIs(t, isX)). -func allBoolean(typ Type) bool { return allBasic(typ, IsBoolean) } -func allInteger(typ Type) bool { return allBasic(typ, IsInteger) } -func allUnsigned(typ Type) bool { return allBasic(typ, IsUnsigned) } -func allNumeric(typ Type) bool { return allBasic(typ, IsNumeric) } -func allString(typ Type) bool { return allBasic(typ, IsString) } -func allOrdered(typ Type) bool { return allBasic(typ, IsOrdered) } -func allNumericOrString(typ Type) bool { return allBasic(typ, IsNumeric|IsString) } +func allBoolean(t Type) bool { return allBasic(t, IsBoolean) } +func allInteger(t Type) bool { return allBasic(t, IsInteger) } +func allUnsigned(t Type) bool { return allBasic(t, IsUnsigned) } +func allNumeric(t Type) bool { return allBasic(t, IsNumeric) } +func allString(t Type) bool { return allBasic(t, IsString) } +func allOrdered(t Type) bool { return allBasic(t, IsOrdered) } +func allNumericOrString(t Type) bool { return allBasic(t, IsNumeric|IsString) } // allBasic reports whether under(t) is a basic type with the specified info. // If t is a type parameter, the result is true if isBasic(t, info) is true diff --git a/src/go/types/sizeof_test.go b/src/go/types/sizeof_test.go index d4ce0a7fcd..f17a1781f5 100644 --- a/src/go/types/sizeof_test.go +++ b/src/go/types/sizeof_test.go @@ -46,7 +46,7 @@ func TestSizeof(t *testing.T) { // Misc {Scope{}, 44, 88}, - {Package{}, 40, 80}, + {Package{}, 36, 72}, {_TypeSet{}, 28, 56}, } for _, test := range tests { diff --git a/src/go/types/typeterm_test.go b/src/go/types/typeterm_test.go index 683b95ee56..bf67f3872d 100644 --- a/src/go/types/typeterm_test.go +++ b/src/go/types/typeterm_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/universe.go b/src/go/types/universe.go index 8860aadfca..6984cf849b 100644 --- a/src/go/types/universe.go +++ b/src/go/types/universe.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. -- 2.48.1