aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types2/scope.go
AgeCommit message (Collapse)Author
2021-07-09[dev.typeparams] cmd/compile/internal/types2: use scope numbers to identify ↵Robert Griesemer
local types Rather than using a local types' position information, use the type name's scope numbers to uniquely identify the type from others with the same name. We use scope numbers rather than indices (with number-1 == index) to preserve the invariant that the zero value for a scope is a ready to use empty scope. Using scope numbers turned out to be fairly simple after all and provides a reasonably stable identification which will make debugging simpler. A scope number series may be a bit longer than a unique ID for each type name but local types should be reasonably rare. Also did a minor cleanup in universe.go to ensure Named.orig is set up correctly (there's still an open TODO but with a work-around). Change-Id: I73935fa9bd960809fd5c95fe8b8a535c313cfc8f Reviewed-on: https://go-review.googlesource.com/c/go/+/333192 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-06-04[dev.typeparams] cmd/compile: lazy import resolution for types2Matthew Dempsky
This CL adds three new functions to the types2 API to support lazy import resolution: 1. A new Scope.InsertLazy method to allow recording that Objects exist in a particular Scope (in particular, package scopes) without having to yet fully construct those objects. Instead, types2 will call the provided `resolve` function if/when the object is actually needed. 2. Similarly, a new NewTypeNameLazy function to create TypeName objects without yet instantiating their underlying Named instance. 3. Finally, an InstantiateLazy method, that allows creating type instances without requiring any of the types to be expanded right away. Importantly, this requires providing a types2.Checker argument to handle recursive types correctly. The APIs as-is are a bit clumsy (esp. NewTypeNameLazy), but seem to work well for cmd/compile's needs. In particular, they simplify some of the complexities of handling recursive type definitions within the importer. Also, the current prototype is a bit fragile. It uses sync.Once to manage concurrent lazy resolution, which is frustrating to debug in the presence of reentrancy issues. It also means the importer needs to deal with concurrency as well. These aren't issues for types2 though as cmd/compile only walks the type-checked AST sequentially. Finally, it looks like some of the details of lazy type names are similar to the lazy "instance" stuff used for generics, so maybe there's opportunity for unifying them under a more general (but still internal) lazy type mechanism. I had originally intended for this CL to also update the types2 importer, but (1) it doesn't have access to the types2.Checker instance needed to call InstantiateLazy, and (2) it creates a new TypeName/TypeParam at each use rather than reusing them, which evidently works with types2.Instantiate but not types2.(*Checker).instantiate (i.e., InstantiateLazy). I spent a while trying to fix these issues, but kept running into more subtle issues. Instead, I've included my WIP "unified IR" CL as a followup CL that demonstrates these Lazy methods (see noder/reader2.go). Updates #46449. Change-Id: I4d1e8e649f6325a11790d25fd90c39fa07c8d41d Reviewed-on: https://go-review.googlesource.com/c/go/+/323569 Run-TryBot: Matthew Dempsky <mdempsky@google.com> Trust: Matthew Dempsky <mdempsky@google.com> Trust: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-04-07cmd/compile/internal/syntax, types2: move cmpPos to pos.CmpRobert Griesemer
Make position comparison generally available. Change-Id: I94b6f658fa19a15b30574dbb2181879115c131a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/307215 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-10-28[dev.typeparams] cmd/compile/internal/types2: review of scopes.goRobert Griesemer
This file has a few changes compared to the go/types version: 1) syntax.Pos is used instead of token.Pos. 2) The cmpPos helper function (defined elsewhere) is used to compare positions (syntax.Pos positions cannot be compared directly with <=). 3) A new method Scope.Squash was added (primary difference). f=scope.go; diff $f ../../../../go/types/$f 7c7 < package types2 --- > package types 11d10 < "cmd/compile/internal/syntax" 12a12 > "go/token" 26c26 < pos, end syntax.Pos // scope extent; may be invalid --- > pos, end token.Pos // scope extent; may be invalid 33c33 < func NewScope(parent *Scope, pos, end syntax.Pos, comment string) *Scope { --- > func NewScope(parent *Scope, pos, end token.Pos, comment string) *Scope { 82c82 < func (s *Scope) LookupParent(name string, pos syntax.Pos) (*Scope, Object) { --- > func (s *Scope) LookupParent(name string, pos token.Pos) (*Scope, Object) { 84c84 < if obj := s.elems[name]; obj != nil && (!pos.IsKnown() || cmpPos(obj.scopePos(), pos) <= 0) { --- > if obj := s.elems[name]; obj != nil && (!pos.IsValid() || obj.scopePos() <= pos) { 111,144d110 < // Squash merges s with its parent scope p by adding all < // objects of s to p, adding all children of s to the < // children of p, and removing s from p's children. < // The function f is called for each object obj in s which < // has an object alt in p. s should be discarded after < // having been squashed. < func (s *Scope) Squash(err func(obj, alt Object)) { < p := s.parent < assert(p != nil) < for _, obj := range s.elems { < obj.setParent(nil) < if alt := p.Insert(obj); alt != nil { < err(obj, alt) < } < } < < j := -1 // index of s in p.children < for i, ch := range p.children { < if ch == s { < j = i < break < } < } < assert(j >= 0) < k := len(p.children) - 1 < p.children[j] = p.children[k] < p.children = p.children[:k] < < p.children = append(p.children, s.children...) < < s.children = nil < s.elems = nil < } < 149,150c115,116 < func (s *Scope) Pos() syntax.Pos { return s.pos } < func (s *Scope) End() syntax.Pos { return s.end } --- > func (s *Scope) Pos() token.Pos { return s.pos } > func (s *Scope) End() token.Pos { return s.end } 155,156c121,122 < func (s *Scope) Contains(pos syntax.Pos) bool { < return cmpPos(s.pos, pos) <= 0 && cmpPos(pos, s.end) < 0 --- > func (s *Scope) Contains(pos token.Pos) bool { > return s.pos <= pos && pos < s.end 164c130 < func (s *Scope) Innermost(pos syntax.Pos) *Scope { --- > func (s *Scope) Innermost(pos token.Pos) *Scope { Change-Id: If6c459f45dae8980ffb3a902a46b1700e9b55dc7 Reviewed-on: https://go-review.googlesource.com/c/go/+/265700 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-10-21[dev.typeparams] cmd/compile/internal/importer, types2: initial check-in of ↵Robert Griesemer
types2 and importer This is a copy of the importer and types2 (unreviewed) prototype version excluding the testdata directory containing tests (see below). Each file is marked with the comment // UNREVIEWED on the first line. The plan is to check in this code wholesale (it runs and passes all tests) and then review the code file-by-file via subsequent CLs and remove the "// UNREVIEWED" comments as we review the files. Since most tests are unchanged from the original go/types, the next CL will commit those tests as they don't need to be reviewed again. (Eventually we may want to factor them out and share them from a single place, e.g. the test directory.) The existing file fmtmap_test.go was updated. Change-Id: I9bd0ad1a7e7188b501423483a44d18e623c0fe71 Reviewed-on: https://go-review.googlesource.com/c/go/+/263624 Trust: Robert Griesemer <gri@golang.org> Trust: Keith Randall <khr@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>