• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ast.Scope类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中go/ast.Scope的典型用法代码示例。如果您正苦于以下问题:Golang Scope类的具体用法?Golang Scope怎么用?Golang Scope使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Scope类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: declare

// 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
}
开发者ID:Quantumboost,项目名称:gcc,代码行数:27,代码来源:gcimporter.go


示例2: insertToScope

func insertToScope(scope *ast.Scope, obj *ast.Object) {
	if obj.Name == "_" {
		return
	}
	if obj.Kind == ast.Fun && obj.Name == "init" {
		return
	}
	scope.Insert(obj)
}
开发者ID:soniah,项目名称:gosloppy,代码行数:9,代码来源:scopewalker.go


示例3: Lookup

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
}
开发者ID:soniah,项目名称:gosloppy,代码行数:9,代码来源:scopewalker.go


示例4: declInScope

// declInScope declares an object of a given kind and name in scope and sets the object's Decl and N fields.
// It returns the newly allocated object. If an object with the same name already exists in scope, an error
// is reported and the object is not inserted.
// (Objects with _ name are always inserted into a scope without errors, but they cannot be found.)
func (tc *typechecker) declInScope(scope *ast.Scope, kind ast.Kind, name *ast.Ident, decl interface{}, n int) *ast.Object {
	obj := ast.NewObj(kind, name.Name)
	obj.Decl = decl
	obj.N = n
	name.Obj = obj
	if alt := scope.Insert(obj); alt != obj {
		tc.Errorf(name.Pos(), "%s already declared at %s", name.Name, objPos(alt))
	}
	return obj
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:14,代码来源:scope.go


示例5: declIdent

func (p *parser) declIdent(scope *ast.Scope, id *ast.Ident) {
	decl := scope.Declare(id.Obj)
	if p.checkDecl && decl != id.Obj {
		if decl.Kind == ast.Err {
			// declared object is a forward declaration - update it
			*decl = *id.Obj
			id.Obj = decl
			return
		}
		p.Error(id.Pos(), "'"+id.Name()+"' declared already at "+decl.Pos.String())
	}
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:12,代码来源:parser.go


示例6: declInScope

// declInScope declares an object of a given kind and name in scope and sets the object's Decl and N fields.
// It returns the newly allocated object. If an object with the same name already exists in scope, an error
// is reported and the object is not inserted.
func (tc *typechecker) declInScope(scope *ast.Scope, kind ast.ObjKind, name *ast.Ident, decl interface{}, n int) *ast.Object {
	obj := ast.NewObj(kind, name.Name)
	obj.Decl = decl
	//obj.N = n
	name.Obj = obj
	if name.Name != "_" {
		if alt := scope.Insert(obj); alt != nil {
			tc.Errorf(name.Pos(), "%s already declared at %s", name.Name, tc.fset.Position(alt.Pos()).String())
		}
	}
	return obj
}
开发者ID:WXB506,项目名称:golang,代码行数:15,代码来源:scope.go


示例7: declare

// declare declares an object of the given kind and name (ident) in scope;
// decl is the corresponding declaration in the AST. An error is reported
// if the object was declared before.
//
// TODO(gri) This is very similar to the declare function in go/parser; it
// is only used to associate methods with their respective receiver base types.
// In a future version, it might be simpler and cleaner to do all the resolution
// in the type-checking phase. It would simplify the parser, AST, and also
// reduce some amount of code duplication.
//
func (check *checker) declare(scope *ast.Scope, kind ast.ObjKind, ident *ast.Ident, decl ast.Decl) {
	assert(ident.Obj == nil) // identifier already declared or resolved
	obj := ast.NewObj(kind, ident.Name)
	obj.Decl = decl
	ident.Obj = obj
	if ident.Name != "_" {
		if alt := scope.Insert(obj); alt != nil {
			prevDecl := ""
			if pos := alt.Pos(); pos.IsValid() {
				prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", check.fset.Position(pos))
			}
			check.errorf(ident.Pos(), fmt.Sprintf("%s redeclared in this block%s", ident.Name, prevDecl))
		}
	}
}
开发者ID:timnau,项目名称:golang,代码行数:25,代码来源:check.go


示例8: parseSignature

// Signature = Parameters [ Result ] .
// Result    = Type | Parameters .
//
func (p *gcParser) parseSignature(scope *ast.Scope, isVariadic *bool) {
	p.parseParameters(scope, isVariadic)

	// optional result type
	switch p.tok {
	case scanner.Ident, scanner.String, '[', '*', '<':
		// single, unnamed result
		result := ast.NewObj(ast.Var, "_")
		result.Type = p.parseType()
		scope.Insert(result)
	case '(':
		// named or multiple result(s)
		p.parseParameters(scope, nil)
	}
}
开发者ID:radhermit,项目名称:gcc,代码行数:18,代码来源:gcimporter.go


示例9: findIdentInScope

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}
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:19,代码来源:parser.go


示例10: parseParameter

// Parameter = ( identifier | "?" ) [ "..." ] Type .
//
func (p *gcParser) parseParameter(scope *ast.Scope, isVariadic *bool) {
	name := p.parseName()
	if name == "" {
		name = "_" // cannot access unnamed identifiers
	}
	if isVariadic != nil {
		if *isVariadic {
			p.error("... not on final argument")
		}
		if p.tok == '.' {
			p.expectSpecial("...")
			*isVariadic = true
		}
	}
	ptyp := p.parseType()
	par := ast.NewObj(ast.Var, name)
	par.Type = ptyp
	scope.Insert(par)
}
开发者ID:radhermit,项目名称:gcc,代码行数:21,代码来源:gcimporter.go


示例11: declare

func (p *parser) declare(decl, data interface{}, scope *ast.Scope, kind ast.ObjKind, idents ...*ast.Ident) {
	for _, ident := range idents {
		assert(ident.Obj == nil, "identifier already declared or resolved")
		obj := ast.NewObj(kind, ident.Name)
		// remember the corresponding declaration for redeclaration
		// errors and global variable resolution/typechecking phase
		obj.Decl = decl
		obj.Data = data
		ident.Obj = obj
		if ident.Name != "_" {
			if alt := scope.Insert(obj); alt != nil && p.mode&DeclarationErrors != 0 {
				prevDecl := ""
				if pos := alt.Pos(); pos.IsValid() {
					prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", p.file.Position(pos))
				}
				p.error(ident.Pos(), fmt.Sprintf("%s redeclared in this block%s", ident.Name, prevDecl))
			}
		}
	}
}
开发者ID:h12w,项目名称:gombi,代码行数:20,代码来源:parser.go


示例12: declare

// 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
}
开发者ID:hfeeki,项目名称:go,代码行数:23,代码来源:gcimporter.go


示例13: parseField

// Field = Name Type [ ":" string_lit ] .
//
func (p *gcParser) parseField(scope *ast.Scope) {
	// TODO(gri) The code below is not correct for anonymous fields:
	//           The name is the type name; it should not be empty.
	name := p.parseName()
	ftyp := p.parseType()
	if name == "" {
		// anonymous field - ftyp must be T or *T and T must be a type name
		ftyp = Deref(ftyp)
		if ftyp, ok := ftyp.(*Name); ok {
			name = ftyp.Obj.Name
		} else {
			p.errorf("anonymous field expected")
		}
	}
	if p.tok == ':' {
		p.next()
		tag := p.expect(scanner.String)
		_ = tag // TODO(gri) store tag somewhere
	}
	fld := ast.NewObj(ast.Var, name)
	fld.Type = ftyp
	scope.Insert(fld)
}
开发者ID:radhermit,项目名称:gcc,代码行数:25,代码来源:gcimporter.go



注:本文中的go/ast.Scope类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang ast.SelectorExpr类代码示例发布时间:2022-05-28
下一篇:
Golang ast.ReturnStmt类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap