github.com/bir3/gocompiler@v0.9.2202/src/go/types/scope.go (about)

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