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
7package types2
8
9import (
10	"cmd/compile/internal/syntax"
11	"fmt"
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.
22type 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 syntax.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.
34func NewScope(parent *Scope, pos, end syntax.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.
45func (s *Scope) Parent() *Scope { return s.parent }
46
47// Len returns the number of scope elements.
48func (s *Scope) Len() int { return len(s.elems) }
49
50// Names returns the scope's element names in sorted order.
51func (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.
63func (s *Scope) NumChildren() int { return len(s.children) }
64
65// Child returns the i'th child scope for 0 <= i < NumChildren().
66func (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.
70func (s *Scope) Lookup(name string) Object {
71	obj := resolve(name, s.elems[name])
72	// Hijack Lookup for "any": with gotypesalias=1, we want the Universe to
73	// return an Alias for "any", and with gotypesalias=0 we want to return
74	// the legacy representation of aliases.
75	//
76	// This is rather tricky, but works out after auditing of the usage of
77	// s.elems. The only external API to access scope elements is Lookup.
78	//
79	// TODO: remove this once gotypesalias=0 is no longer supported.
80	if obj == universeAnyAlias && !aliasAny() {
81		return universeAnyNoAlias
82	}
83	return obj
84}
85
86// LookupParent follows the parent chain of scopes starting with s until
87// it finds a scope where Lookup(name) returns a non-nil object, and then
88// returns that scope and object. If a valid position pos is provided,
89// only objects that were declared at or before pos are considered.
90// If no such scope and object exists, the result is (nil, nil).
91//
92// Note that obj.Parent() may be different from the returned scope if the
93// object was inserted into the scope and already had a parent at that
94// time (see Insert). This can only happen for dot-imported objects
95// whose scope is the scope of the package that exported them.
96func (s *Scope) LookupParent(name string, pos syntax.Pos) (*Scope, Object) {
97	for ; s != nil; s = s.parent {
98		if obj := s.Lookup(name); obj != nil && (!pos.IsKnown() || cmpPos(obj.scopePos(), pos) <= 0) {
99			return s, obj
100		}
101	}
102	return nil, nil
103}
104
105// Insert attempts to insert an object obj into scope s.
106// If s already contains an alternative object alt with
107// the same name, Insert leaves s unchanged and returns alt.
108// Otherwise it inserts obj, sets the object's parent scope
109// if not already set, and returns nil.
110func (s *Scope) Insert(obj Object) Object {
111	name := obj.Name()
112	if alt := s.Lookup(name); alt != nil {
113		return alt
114	}
115	s.insert(name, obj)
116	if obj.Parent() == nil {
117		obj.setParent(s)
118	}
119	return nil
120}
121
122// InsertLazy is like Insert, but allows deferring construction of the
123// inserted object until it's accessed with Lookup. The Object
124// returned by resolve must have the same name as given to InsertLazy.
125// If s already contains an alternative object with the same name,
126// InsertLazy leaves s unchanged and returns false. Otherwise it
127// records the binding and returns true. The object's parent scope
128// will be set to s after resolve is called.
129func (s *Scope) InsertLazy(name string, resolve func() Object) bool {
130	if s.elems[name] != nil {
131		return false
132	}
133	s.insert(name, &lazyObject{parent: s, resolve: resolve})
134	return true
135}
136
137func (s *Scope) insert(name string, obj Object) {
138	if s.elems == nil {
139		s.elems = make(map[string]Object)
140	}
141	s.elems[name] = obj
142}
143
144// Squash merges s with its parent scope p by adding all
145// objects of s to p, adding all children of s to the
146// children of p, and removing s from p's children.
147// The function f is called for each object obj in s which
148// has an object alt in p. s should be discarded after
149// having been squashed.
150func (s *Scope) Squash(err func(obj, alt Object)) {
151	p := s.parent
152	assert(p != nil)
153	for name, obj := range s.elems {
154		obj = resolve(name, obj)
155		obj.setParent(nil)
156		if alt := p.Insert(obj); alt != nil {
157			err(obj, alt)
158		}
159	}
160
161	j := -1 // index of s in p.children
162	for i, ch := range p.children {
163		if ch == s {
164			j = i
165			break
166		}
167	}
168	assert(j >= 0)
169	k := len(p.children) - 1
170	p.children[j] = p.children[k]
171	p.children = p.children[:k]
172
173	p.children = append(p.children, s.children...)
174
175	s.children = nil
176	s.elems = nil
177}
178
179// Pos and End describe the scope's source code extent [pos, end).
180// The results are guaranteed to be valid only if the type-checked
181// AST has complete position information. The extent is undefined
182// for Universe and package scopes.
183func (s *Scope) Pos() syntax.Pos { return s.pos }
184func (s *Scope) End() syntax.Pos { return s.end }
185
186// Contains reports whether pos is within the scope's extent.
187// The result is guaranteed to be valid only if the type-checked
188// AST has complete position information.
189func (s *Scope) Contains(pos syntax.Pos) bool {
190	return cmpPos(s.pos, pos) <= 0 && cmpPos(pos, s.end) < 0
191}
192
193// Innermost returns the innermost (child) scope containing
194// pos. If pos is not within any scope, the result is nil.
195// The result is also nil for the Universe scope.
196// The result is guaranteed to be valid only if the type-checked
197// AST has complete position information.
198func (s *Scope) Innermost(pos syntax.Pos) *Scope {
199	// Package scopes do not have extents since they may be
200	// discontiguous, so iterate over the package's files.
201	if s.parent == Universe {
202		for _, s := range s.children {
203			if inner := s.Innermost(pos); inner != nil {
204				return inner
205			}
206		}
207	}
208
209	if s.Contains(pos) {
210		for _, s := range s.children {
211			if s.Contains(pos) {
212				return s.Innermost(pos)
213			}
214		}
215		return s
216	}
217	return nil
218}
219
220// WriteTo writes a string representation of the scope to w,
221// with the scope elements sorted by name.
222// The level of indentation is controlled by n >= 0, with
223// n == 0 for no indentation.
224// If recurse is set, it also writes nested (children) scopes.
225func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
226	const ind = ".  "
227	indn := strings.Repeat(ind, n)
228
229	fmt.Fprintf(w, "%s%s scope %p {\n", indn, s.comment, s)
230
231	indn1 := indn + ind
232	for _, name := range s.Names() {
233		fmt.Fprintf(w, "%s%s\n", indn1, s.Lookup(name))
234	}
235
236	if recurse {
237		for _, s := range s.children {
238			s.WriteTo(w, n+1, recurse)
239		}
240	}
241
242	fmt.Fprintf(w, "%s}\n", indn)
243}
244
245// String returns a string representation of the scope, for debugging.
246func (s *Scope) String() string {
247	var buf strings.Builder
248	s.WriteTo(&buf, 0, false)
249	return buf.String()
250}
251
252// A lazyObject represents an imported Object that has not been fully
253// resolved yet by its importer.
254type lazyObject struct {
255	parent  *Scope
256	resolve func() Object
257	obj     Object
258	once    sync.Once
259}
260
261// resolve returns the Object represented by obj, resolving lazy
262// objects as appropriate.
263func resolve(name string, obj Object) Object {
264	if lazy, ok := obj.(*lazyObject); ok {
265		lazy.once.Do(func() {
266			obj := lazy.resolve()
267
268			if _, ok := obj.(*lazyObject); ok {
269				panic("recursive lazy object")
270			}
271			if obj.Name() != name {
272				panic("lazy object has unexpected name")
273			}
274
275			if obj.Parent() == nil {
276				obj.setParent(lazy.parent)
277			}
278			lazy.obj = obj
279		})
280
281		obj = lazy.obj
282	}
283	return obj
284}
285
286// stub implementations so *lazyObject implements Object and we can
287// store them directly into Scope.elems.
288func (*lazyObject) Parent() *Scope                     { panic("unreachable") }
289func (*lazyObject) Pos() syntax.Pos                    { panic("unreachable") }
290func (*lazyObject) Pkg() *Package                      { panic("unreachable") }
291func (*lazyObject) Name() string                       { panic("unreachable") }
292func (*lazyObject) Type() Type                         { panic("unreachable") }
293func (*lazyObject) Exported() bool                     { panic("unreachable") }
294func (*lazyObject) Id() string                         { panic("unreachable") }
295func (*lazyObject) String() string                     { panic("unreachable") }
296func (*lazyObject) order() uint32                      { panic("unreachable") }
297func (*lazyObject) color() color                       { panic("unreachable") }
298func (*lazyObject) setType(Type)                       { panic("unreachable") }
299func (*lazyObject) setOrder(uint32)                    { panic("unreachable") }
300func (*lazyObject) setColor(color color)               { panic("unreachable") }
301func (*lazyObject) setParent(*Scope)                   { panic("unreachable") }
302func (*lazyObject) sameId(*Package, string, bool) bool { panic("unreachable") }
303func (*lazyObject) scopePos() syntax.Pos               { panic("unreachable") }
304func (*lazyObject) setScopePos(syntax.Pos)             { panic("unreachable") }
305