github.com/bir3/gocompiler@v0.3.205/src/go/types/scope.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // This file implements Scopes. 6 7 package types 8 9 import ( 10 "fmt" 11 "github.com/bir3/gocompiler/src/go/token" 12 "io" 13 "sort" 14 "strings" 15 "sync" 16 ) 17 18 // A Scope maintains a set of objects and links to its containing 19 // (parent) and contained (children) scopes. Objects may be inserted 20 // and looked up by name. The zero value for Scope is a ready-to-use 21 // empty scope. 22 type Scope struct { 23 parent *Scope 24 children []*Scope 25 number int // parent.children[number-1] is this scope; 0 if there is no parent 26 elems map[string]Object // lazily allocated 27 pos, end token.Pos // scope extent; may be invalid 28 comment string // for debugging only 29 isFunc bool // set if this is a function scope (internal use only) 30 } 31 32 // NewScope returns a new, empty scope contained in the given parent 33 // scope, if any. The comment is for debugging only. 34 func NewScope(parent *Scope, pos, end token.Pos, comment string) *Scope { 35 s := &Scope{parent, nil, 0, nil, pos, end, comment, false} 36 // don't add children to Universe scope! 37 if parent != nil && parent != Universe { 38 parent.children = append(parent.children, s) 39 s.number = len(parent.children) 40 } 41 return s 42 } 43 44 // Parent returns the scope's containing (parent) scope. 45 func (s *Scope) Parent() *Scope { return s.parent } 46 47 // Len returns the number of scope elements. 48 func (s *Scope) Len() int { return len(s.elems) } 49 50 // Names returns the scope's element names in sorted order. 51 func (s *Scope) Names() []string { 52 names := make([]string, len(s.elems)) 53 i := 0 54 for name := range s.elems { 55 names[i] = name 56 i++ 57 } 58 sort.Strings(names) 59 return names 60 } 61 62 // NumChildren returns the number of scopes nested in s. 63 func (s *Scope) NumChildren() int { return len(s.children) } 64 65 // Child returns the i'th child scope for 0 <= i < NumChildren(). 66 func (s *Scope) Child(i int) *Scope { return s.children[i] } 67 68 // Lookup returns the object in scope s with the given name if such an 69 // object exists; otherwise the result is nil. 70 func (s *Scope) Lookup(name string) Object { 71 return resolve(name, s.elems[name]) 72 } 73 74 // LookupParent follows the parent chain of scopes starting with s until 75 // it finds a scope where Lookup(name) returns a non-nil object, and then 76 // returns that scope and object. If a valid position pos is provided, 77 // only objects that were declared at or before pos are considered. 78 // If no such scope and object exists, the result is (nil, nil). 79 // 80 // Note that obj.Parent() may be different from the returned scope if the 81 // object was inserted into the scope and already had a parent at that 82 // time (see Insert). This can only happen for dot-imported objects 83 // whose scope is the scope of the package that exported them. 84 func (s *Scope) LookupParent(name string, pos token.Pos) (*Scope, Object) { 85 for ; s != nil; s = s.parent { 86 if obj := s.Lookup(name); obj != nil && (!pos.IsValid() || obj.scopePos() <= pos) { 87 return s, obj 88 } 89 } 90 return nil, nil 91 } 92 93 // Insert attempts to insert an object obj into scope s. 94 // If s already contains an alternative object alt with 95 // the same name, Insert leaves s unchanged and returns alt. 96 // Otherwise it inserts obj, sets the object's parent scope 97 // if not already set, and returns nil. 98 func (s *Scope) Insert(obj Object) Object { 99 name := obj.Name() 100 if alt := s.Lookup(name); alt != nil { 101 return alt 102 } 103 s.insert(name, obj) 104 if obj.Parent() == nil { 105 obj.setParent(s) 106 } 107 return nil 108 } 109 110 // _InsertLazy is like Insert, but allows deferring construction of the 111 // inserted object until it's accessed with Lookup. The Object 112 // returned by resolve must have the same name as given to _InsertLazy. 113 // If s already contains an alternative object with the same name, 114 // _InsertLazy leaves s unchanged and returns false. Otherwise it 115 // records the binding and returns true. The object's parent scope 116 // will be set to s after resolve is called. 117 func (s *Scope) _InsertLazy(name string, resolve func() Object) bool { 118 if s.elems[name] != nil { 119 return false 120 } 121 s.insert(name, &lazyObject{parent: s, resolve: resolve}) 122 return true 123 } 124 125 func (s *Scope) insert(name string, obj Object) { 126 if s.elems == nil { 127 s.elems = make(map[string]Object) 128 } 129 s.elems[name] = obj 130 } 131 132 // squash merges s with its parent scope p by adding all 133 // objects of s to p, adding all children of s to the 134 // children of p, and removing s from p's children. 135 // The function f is called for each object obj in s which 136 // has an object alt in p. s should be discarded after 137 // having been squashed. 138 func (s *Scope) squash(err func(obj, alt Object)) { 139 p := s.parent 140 assert(p != nil) 141 for name, obj := range s.elems { 142 obj = resolve(name, obj) 143 obj.setParent(nil) 144 if alt := p.Insert(obj); alt != nil { 145 err(obj, alt) 146 } 147 } 148 149 j := -1 // index of s in p.children 150 for i, ch := range p.children { 151 if ch == s { 152 j = i 153 break 154 } 155 } 156 assert(j >= 0) 157 k := len(p.children) - 1 158 p.children[j] = p.children[k] 159 p.children = p.children[:k] 160 161 p.children = append(p.children, s.children...) 162 163 s.children = nil 164 s.elems = nil 165 } 166 167 // Pos and End describe the scope's source code extent [pos, end). 168 // The results are guaranteed to be valid only if the type-checked 169 // AST has complete position information. The extent is undefined 170 // for Universe and package scopes. 171 func (s *Scope) Pos() token.Pos { return s.pos } 172 func (s *Scope) End() token.Pos { return s.end } 173 174 // Contains reports whether pos is within the scope's extent. 175 // The result is guaranteed to be valid only if the type-checked 176 // AST has complete position information. 177 func (s *Scope) Contains(pos token.Pos) bool { 178 return s.pos <= pos && pos < s.end 179 } 180 181 // Innermost returns the innermost (child) scope containing 182 // pos. If pos is not within any scope, the result is nil. 183 // The result is also nil for the Universe scope. 184 // The result is guaranteed to be valid only if the type-checked 185 // AST has complete position information. 186 func (s *Scope) Innermost(pos token.Pos) *Scope { 187 // Package scopes do not have extents since they may be 188 // discontiguous, so iterate over the package's files. 189 if s.parent == Universe { 190 for _, s := range s.children { 191 if inner := s.Innermost(pos); inner != nil { 192 return inner 193 } 194 } 195 } 196 197 if s.Contains(pos) { 198 for _, s := range s.children { 199 if s.Contains(pos) { 200 return s.Innermost(pos) 201 } 202 } 203 return s 204 } 205 return nil 206 } 207 208 // WriteTo writes a string representation of the scope to w, 209 // with the scope elements sorted by name. 210 // The level of indentation is controlled by n >= 0, with 211 // n == 0 for no indentation. 212 // If recurse is set, it also writes nested (children) scopes. 213 func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) { 214 const ind = ". " 215 indn := strings.Repeat(ind, n) 216 217 fmt.Fprintf(w, "%s%s scope %p {\n", indn, s.comment, s) 218 219 indn1 := indn + ind 220 for _, name := range s.Names() { 221 fmt.Fprintf(w, "%s%s\n", indn1, s.Lookup(name)) 222 } 223 224 if recurse { 225 for _, s := range s.children { 226 s.WriteTo(w, n+1, recurse) 227 } 228 } 229 230 fmt.Fprintf(w, "%s}\n", indn) 231 } 232 233 // String returns a string representation of the scope, for debugging. 234 func (s *Scope) String() string { 235 var buf strings.Builder 236 s.WriteTo(&buf, 0, false) 237 return buf.String() 238 } 239 240 // A lazyObject represents an imported Object that has not been fully 241 // resolved yet by its importer. 242 type lazyObject struct { 243 parent *Scope 244 resolve func() Object 245 obj Object 246 once sync.Once 247 } 248 249 // resolve returns the Object represented by obj, resolving lazy 250 // objects as appropriate. 251 func resolve(name string, obj Object) Object { 252 if lazy, ok := obj.(*lazyObject); ok { 253 lazy.once.Do(func() { 254 obj := lazy.resolve() 255 256 if _, ok := obj.(*lazyObject); ok { 257 panic("recursive lazy object") 258 } 259 if obj.Name() != name { 260 panic("lazy object has unexpected name") 261 } 262 263 if obj.Parent() == nil { 264 obj.setParent(lazy.parent) 265 } 266 lazy.obj = obj 267 }) 268 269 obj = lazy.obj 270 } 271 return obj 272 } 273 274 // stub implementations so *lazyObject implements Object and we can 275 // store them directly into Scope.elems. 276 func (*lazyObject) Parent() *Scope { panic("unreachable") } 277 func (*lazyObject) Pos() token.Pos { panic("unreachable") } 278 func (*lazyObject) Pkg() *Package { panic("unreachable") } 279 func (*lazyObject) Name() string { panic("unreachable") } 280 func (*lazyObject) Type() Type { panic("unreachable") } 281 func (*lazyObject) Exported() bool { panic("unreachable") } 282 func (*lazyObject) Id() string { panic("unreachable") } 283 func (*lazyObject) String() string { panic("unreachable") } 284 func (*lazyObject) order() uint32 { panic("unreachable") } 285 func (*lazyObject) color() color { panic("unreachable") } 286 func (*lazyObject) setType(Type) { panic("unreachable") } 287 func (*lazyObject) setOrder(uint32) { panic("unreachable") } 288 func (*lazyObject) setColor(color color) { panic("unreachable") } 289 func (*lazyObject) setParent(*Scope) { panic("unreachable") } 290 func (*lazyObject) sameId(pkg *Package, name string) bool { panic("unreachable") } 291 func (*lazyObject) scopePos() token.Pos { panic("unreachable") } 292 func (*lazyObject) setScopePos(pos token.Pos) { panic("unreachable") }