github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/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  	"bytes"
    11  	"fmt"
    12  	"io"
    13  	"sort"
    14  	"strings"
    15  )
    16  
    17  // TODO(gri) Provide scopes with a name or other mechanism so that
    18  //           objects can use that information for better printing.
    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  	comment  string            // for debugging only
    28  	elems    map[string]Object // lazily allocated
    29  }
    30  
    31  // NewScope returns a new, empty scope contained in the given parent
    32  // scope, if any.  The comment is for debugging only.
    33  func NewScope(parent *Scope, comment string) *Scope {
    34  	s := &Scope{parent: parent, comment: comment}
    35  	// don't add children to Universe scope!
    36  	if parent != nil && parent != Universe {
    37  		parent.children = append(parent.children, s)
    38  	}
    39  	return s
    40  }
    41  
    42  // Parent returns the scope's containing (parent) scope.
    43  func (s *Scope) Parent() *Scope { return s.parent }
    44  
    45  // Len() returns the number of scope elements.
    46  func (s *Scope) Len() int { return len(s.elems) }
    47  
    48  // Names returns the scope's element names in sorted order.
    49  func (s *Scope) Names() []string {
    50  	names := make([]string, len(s.elems))
    51  	i := 0
    52  	for name := range s.elems {
    53  		names[i] = name
    54  		i++
    55  	}
    56  	sort.Strings(names)
    57  	return names
    58  }
    59  
    60  // NumChildren() returns the number of scopes nested in s.
    61  func (s *Scope) NumChildren() int { return len(s.children) }
    62  
    63  // Child returns the i'th child scope for 0 <= i < NumChildren().
    64  func (s *Scope) Child(i int) *Scope { return s.children[i] }
    65  
    66  // Lookup returns the object in scope s with the given name if such an
    67  // object exists; otherwise the result is nil.
    68  func (s *Scope) Lookup(name string) Object {
    69  	return s.elems[name]
    70  }
    71  
    72  // LookupParent follows the parent chain of scopes starting with s until
    73  // it finds a scope where Lookup(name) returns a non-nil object, and then
    74  // returns that scope and object. If no such scope exists, the result is (nil, nil).
    75  //
    76  // Note that obj.Parent() may be different from the returned scope if the
    77  // object was inserted into the scope and already had a parent at that
    78  // time (see Insert, below). This can only happen for dot-imported objects
    79  // whose scope is the scope of the package that exported them.
    80  func (s *Scope) LookupParent(name string) (*Scope, Object) {
    81  	for ; s != nil; s = s.parent {
    82  		if obj := s.elems[name]; obj != nil {
    83  			return s, obj
    84  		}
    85  	}
    86  	return nil, nil
    87  }
    88  
    89  // Insert attempts to insert an object obj into scope s.
    90  // If s already contains an alternative object alt with
    91  // the same name, Insert leaves s unchanged and returns alt.
    92  // Otherwise it inserts obj, sets the object's parent scope
    93  // if not already set, and returns nil.
    94  func (s *Scope) Insert(obj Object) Object {
    95  	name := obj.Name()
    96  	if alt := s.elems[name]; alt != nil {
    97  		return alt
    98  	}
    99  	if s.elems == nil {
   100  		s.elems = make(map[string]Object)
   101  	}
   102  	s.elems[name] = obj
   103  	if obj.Parent() == nil {
   104  		obj.setParent(s)
   105  	}
   106  	return nil
   107  }
   108  
   109  // WriteTo writes a string representation of the scope to w,
   110  // with the scope elements sorted by name.
   111  // The level of indentation is controlled by n >= 0, with
   112  // n == 0 for no indentation.
   113  // If recurse is set, it also writes nested (children) scopes.
   114  func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
   115  	const ind = ".  "
   116  	indn := strings.Repeat(ind, n)
   117  
   118  	fmt.Fprintf(w, "%s%s scope %p {", indn, s.comment, s)
   119  	if len(s.elems) == 0 {
   120  		fmt.Fprintf(w, "}\n")
   121  		return
   122  	}
   123  
   124  	fmt.Fprintln(w)
   125  	indn1 := indn + ind
   126  	for _, name := range s.Names() {
   127  		fmt.Fprintf(w, "%s%s\n", indn1, s.elems[name])
   128  	}
   129  
   130  	if recurse {
   131  		for _, s := range s.children {
   132  			fmt.Fprintln(w)
   133  			s.WriteTo(w, n+1, recurse)
   134  		}
   135  	}
   136  
   137  	fmt.Fprintf(w, "%s}", indn)
   138  }
   139  
   140  // String returns a string representation of the scope, for debugging.
   141  func (s *Scope) String() string {
   142  	var buf bytes.Buffer
   143  	s.WriteTo(&buf, 0, false)
   144  	return buf.String()
   145  }