github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/compile/internal/types2/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 types2 8 9 import ( 10 "github.com/shogo82148/std/cmd/compile/internal/syntax" 11 "github.com/shogo82148/std/io" 12 ) 13 14 // A Scope maintains a set of objects and links to its containing 15 // (parent) and contained (children) scopes. Objects may be inserted 16 // and looked up by name. The zero value for Scope is a ready-to-use 17 // empty scope. 18 type Scope struct { 19 parent *Scope 20 children []*Scope 21 number int 22 elems map[string]Object 23 pos, end syntax.Pos 24 comment string 25 isFunc bool 26 } 27 28 // NewScope returns a new, empty scope contained in the given parent 29 // scope, if any. The comment is for debugging only. 30 func NewScope(parent *Scope, pos, end syntax.Pos, comment string) *Scope 31 32 // Parent returns the scope's containing (parent) scope. 33 func (s *Scope) Parent() *Scope 34 35 // Len returns the number of scope elements. 36 func (s *Scope) Len() int 37 38 // Names returns the scope's element names in sorted order. 39 func (s *Scope) Names() []string 40 41 // NumChildren returns the number of scopes nested in s. 42 func (s *Scope) NumChildren() int 43 44 // Child returns the i'th child scope for 0 <= i < NumChildren(). 45 func (s *Scope) Child(i int) *Scope 46 47 // Lookup returns the object in scope s with the given name if such an 48 // object exists; otherwise the result is nil. 49 func (s *Scope) Lookup(name string) Object 50 51 // LookupParent follows the parent chain of scopes starting with s until 52 // it finds a scope where Lookup(name) returns a non-nil object, and then 53 // returns that scope and object. If a valid position pos is provided, 54 // only objects that were declared at or before pos are considered. 55 // If no such scope and object exists, the result is (nil, nil). 56 // 57 // Note that obj.Parent() may be different from the returned scope if the 58 // object was inserted into the scope and already had a parent at that 59 // time (see Insert). This can only happen for dot-imported objects 60 // whose scope is the scope of the package that exported them. 61 func (s *Scope) LookupParent(name string, pos syntax.Pos) (*Scope, Object) 62 63 // Insert attempts to insert an object obj into scope s. 64 // If s already contains an alternative object alt with 65 // the same name, Insert leaves s unchanged and returns alt. 66 // Otherwise it inserts obj, sets the object's parent scope 67 // if not already set, and returns nil. 68 func (s *Scope) Insert(obj Object) Object 69 70 // InsertLazy is like Insert, but allows deferring construction of the 71 // inserted object until it's accessed with Lookup. The Object 72 // returned by resolve must have the same name as given to InsertLazy. 73 // If s already contains an alternative object with the same name, 74 // InsertLazy leaves s unchanged and returns false. Otherwise it 75 // records the binding and returns true. The object's parent scope 76 // will be set to s after resolve is called. 77 func (s *Scope) InsertLazy(name string, resolve func() Object) bool 78 79 // Squash merges s with its parent scope p by adding all 80 // objects of s to p, adding all children of s to the 81 // children of p, and removing s from p's children. 82 // The function f is called for each object obj in s which 83 // has an object alt in p. s should be discarded after 84 // having been squashed. 85 func (s *Scope) Squash(err func(obj, alt Object)) 86 87 // Pos and End describe the scope's source code extent [pos, end). 88 // The results are guaranteed to be valid only if the type-checked 89 // AST has complete position information. The extent is undefined 90 // for Universe and package scopes. 91 func (s *Scope) Pos() syntax.Pos 92 func (s *Scope) End() syntax.Pos 93 94 // Contains reports whether pos is within the scope's extent. 95 // The result is guaranteed to be valid only if the type-checked 96 // AST has complete position information. 97 func (s *Scope) Contains(pos syntax.Pos) bool 98 99 // Innermost returns the innermost (child) scope containing 100 // pos. If pos is not within any scope, the result is nil. 101 // The result is also nil for the Universe scope. 102 // The result is guaranteed to be valid only if the type-checked 103 // AST has complete position information. 104 func (s *Scope) Innermost(pos syntax.Pos) *Scope 105 106 // WriteTo writes a string representation of the scope to w, 107 // with the scope elements sorted by name. 108 // The level of indentation is controlled by n >= 0, with 109 // n == 0 for no indentation. 110 // If recurse is set, it also writes nested (children) scopes. 111 func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) 112 113 // String returns a string representation of the scope, for debugging. 114 func (s *Scope) String() string