// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.// Source: ../../cmd/compile/internal/types2/scope.go// Copyright 2013 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// This file implements Scopes.package typesimport ()// A Scope maintains a set of objects and links to its containing// (parent) and contained (children) scopes. Objects may be inserted// and looked up by name. The zero value for Scope is a ready-to-use// empty scope.typeScopestruct { parent *Scope children []*Scope number int// parent.children[number-1] is this scope; 0 if there is no parent elems map[string]Object// lazily allocated pos, end token.Pos// scope extent; may be invalid comment string// for debugging only isFunc bool// set if this is a function scope (internal use only)}// NewScope returns a new, empty scope contained in the given parent// scope, if any. The comment is for debugging only.func ( *Scope, , token.Pos, string) *Scope { := &Scope{, nil, 0, nil, , , , false}// don't add children to Universe scope!if != nil && != Universe { .children = append(.children, ) .number = len(.children) }return}// Parent returns the scope's containing (parent) scope.func ( *Scope) () *Scope { return .parent }// Len returns the number of scope elements.func ( *Scope) () int { returnlen(.elems) }// Names returns the scope's element names in sorted order.func ( *Scope) () []string { := make([]string, len(.elems)) := 0for := range .elems { [] = ++ }slices.Sort()return}// NumChildren returns the number of scopes nested in s.func ( *Scope) () int { returnlen(.children) }// Child returns the i'th child scope for 0 <= i < NumChildren().func ( *Scope) ( int) *Scope { return .children[] }// Lookup returns the object in scope s with the given name if such an// object exists; otherwise the result is nil.func ( *Scope) ( string) Object { := resolve(, .elems[])// Hijack Lookup for "any": with gotypesalias=1, we want the Universe to // return an Alias for "any", and with gotypesalias=0 we want to return // the legacy representation of aliases. // // This is rather tricky, but works out after auditing of the usage of // s.elems. The only external API to access scope elements is Lookup. // // TODO: remove this once gotypesalias=0 is no longer supported.if == universeAnyAlias && !aliasAny() {returnuniverseAnyNoAlias }return}// Insert attempts to insert an object obj into scope s.// If s already contains an alternative object alt with// the same name, Insert leaves s unchanged and returns alt.// Otherwise it inserts obj, sets the object's parent scope// if not already set, and returns nil.func ( *Scope) ( Object) Object { := .Name()if := .Lookup(); != nil {return } .insert(, )// TODO(gri) Can we always set the parent to s (or is there // a need to keep the original parent or some race condition)? // If we can, than we may not need environment.lookupScope // which is only there so that we get the correct scope for // marking "used" dot-imported packages.if .Parent() == nil { .setParent() }returnnil}// InsertLazy is like Insert, but allows deferring construction of the// inserted object until it's accessed with Lookup. The Object// returned by resolve must have the same name as given to InsertLazy.// If s already contains an alternative object with the same name,// InsertLazy leaves s unchanged and returns false. Otherwise it// records the binding and returns true. The object's parent scope// will be set to s after resolve is called.func ( *Scope) ( string, func() Object) bool {if .elems[] != nil {returnfalse } .insert(, &lazyObject{parent: , resolve: })returntrue}func ( *Scope) ( string, Object) {if .elems == nil { .elems = make(map[string]Object) } .elems[] = }// WriteTo writes a string representation of the scope to w,// with the scope elements sorted by name.// The level of indentation is controlled by n >= 0, with// n == 0 for no indentation.// If recurse is set, it also writes nested (children) scopes.func ( *Scope) ( io.Writer, int, bool) {const = ". " := strings.Repeat(, )fmt.Fprintf(, "%s%s scope %p {\n", , .comment, ) := + for , := range .Names() {fmt.Fprintf(, "%s%s\n", , .Lookup()) }if {for , := range .children { .(, +1, ) } }fmt.Fprintf(, "%s}\n", )}// String returns a string representation of the scope, for debugging.func ( *Scope) () string {varstrings.Builder .WriteTo(&, 0, false)return .String()}// A lazyObject represents an imported Object that has not been fully// resolved yet by its importer.type lazyObject struct { parent *Scope resolve func() Object obj Object once sync.Once}// resolve returns the Object represented by obj, resolving lazy// objects as appropriate.func resolve( string, Object) Object {if , := .(*lazyObject); { .once.Do(func() { := .resolve()if , := .(*lazyObject); {panic("recursive lazy object") }if .Name() != {panic("lazy object has unexpected name") }if .Parent() == nil { .setParent(.parent) } .obj = }) = .obj }return}// stub implementations so *lazyObject implements Object and we can// store them directly into Scope.elems.func (*lazyObject) () *Scope { panic("unreachable") }func (*lazyObject) () token.Pos { panic("unreachable") }func (*lazyObject) () *Package { panic("unreachable") }func (*lazyObject) () string { panic("unreachable") }func (*lazyObject) () Type { panic("unreachable") }func (*lazyObject) () bool { panic("unreachable") }func (*lazyObject) () string { panic("unreachable") }func (*lazyObject) () string { panic("unreachable") }func (*lazyObject) () uint32 { panic("unreachable") }func (*lazyObject) () color { panic("unreachable") }func (*lazyObject) (Type) { panic("unreachable") }func (*lazyObject) (uint32) { panic("unreachable") }func (*lazyObject) ( color) { panic("unreachable") }func (*lazyObject) (*Scope) { panic("unreachable") }func (*lazyObject) (*Package, string, bool) bool { panic("unreachable") }func (*lazyObject) () token.Pos { panic("unreachable") }func (*lazyObject) (token.Pos) { panic("unreachable") }
The pages are generated with Goldsv0.7.3. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.