// Declare inserts a named object of the given kind in scope. func (p *gcParser) declare(scope *ast.Scope, kind ast.ObjKind, name string) *ast.Object { // a type may have been declared before - if it exists // already in the respective package scope, return that // type if kind == ast.Typ { if obj := scope.Lookup(name); obj != nil { assert(obj.Kind == ast.Typ) return obj } } // any other object must be a newly declared object - // create it and insert it into the package scope obj := ast.NewObj(kind, name) if scope.Insert(obj) != nil { p.errorf("already declared: %v %s", kind, obj.Name) } // a new type object is a named type and may be referred // to before the underlying type is known - set it up if kind == ast.Typ { obj.Type = &Name{Obj: obj} } return obj }
func Lookup(scope *ast.Scope, name string) *ast.Object { for scope != nil { if obj := scope.Lookup(name); obj != nil { return obj } scope = scope.Outer } return nil }
func (p *parser) findIdentInScope(scope *ast.Scope) *ast.Ident { pos := p.pos name := "_" var obj *ast.Object if p.tok == token.IDENT { name = string(p.lit) obj = scope.Lookup(name) p.next() } else { p.expect(token.IDENT) // use expect() error handling } if obj == nil { // TODO(gri) At the moment we always arrive here because // we don't track the lookup scope (and sometimes // we can't). Just create a useable ident for now. obj = ast.NewObj(ast.Err, pos, name) } return &ast.Ident{pos, obj} }
// Declare inserts a named object of the given kind in scope. func (p *gcParser) declare(scope *ast.Scope, kind ast.ObjKind, name string) *ast.Object { // the object may have been imported before - if it exists // already in the respective package scope, return that object if obj := scope.Lookup(name); obj != nil { assert(obj.Kind == kind) return obj } // otherwise create a new object and insert it into the package scope obj := ast.NewObj(kind, name) if scope.Insert(obj) != nil { p.errorf("already declared: %v %s", kind, obj.Name) } // if the new type object is a named type it may be referred // to before the underlying type is known - set it up if kind == ast.Typ { obj.Type = &NamedType{Obj: obj} } return obj }