当前位置: 首页>>代码示例>>Golang>>正文


Golang ast.NewObj函数代码示例

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


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

示例1: VisitFuncType

func (c *compiler) VisitFuncType(f *ast.FuncType) TypeValue {
	var fn_type types.Func

	if f.Params != nil && len(f.Params.List) > 0 {
		final_param_type := f.Params.List[len(f.Params.List)-1].Type
		if _, varargs := final_param_type.(*ast.Ellipsis); varargs {
			fn_type.IsVariadic = true
		}
		for i := 0; i < len(f.Params.List); i++ {
			namecount := len(f.Params.List[i].Names)
			typ := c.GetType(f.Params.List[i].Type)
			if namecount == 0 {
				arg := ast.NewObj(ast.Var, "_")
				arg.Type = typ
				fn_type.Params = append(fn_type.Params, arg)
			} else {
				args := make([]*ast.Object, namecount)
				for j := 0; j < namecount; j++ {
					ident := f.Params.List[i].Names[j]
					if ident != nil {
						args[j] = ident.Obj
					} else {
						args[j] = ast.NewObj(ast.Var, "_")
					}
					args[j].Type = typ
				}
				fn_type.Params = append(fn_type.Params, args...)
			}
		}
	}

	if f.Results != nil {
		for i := 0; i < len(f.Results.List); i++ {
			namecount := len(f.Results.List[i].Names)
			typ := c.GetType(f.Results.List[i].Type)
			if namecount > 0 {
				results := make([]*ast.Object, namecount)
				for j := 0; j < namecount; j++ {
					ident := f.Results.List[i].Names[j]
					if ident != nil {
						results[j] = ident.Obj
					} else {
						results[j] = ast.NewObj(ast.Var, "_")
					}
					results[j].Type = typ
				}
				fn_type.Results = append(fn_type.Results, results...)
			} else {
				result := ast.NewObj(ast.Var, "_")
				result.Type = typ
				fn_type.Results = append(fn_type.Results, result)
			}
		}
	}

	return TypeValue{&fn_type}
}
开发者ID:spate,项目名称:llgo,代码行数:57,代码来源:types.go

示例2: parseMethodOrEmbedSpec

// MethodOrEmbedSpec = Name [ Signature ] .
//
func (p *gcParser) parseMethodOrEmbedSpec() *ast.Object {
	name := p.parseName()
	if p.tok == '(' {
		obj := ast.NewObj(ast.Fun, name)
		obj.Type = p.parseSignature()
		return obj
	}
	// TODO lookup name and return that type
	return ast.NewObj(ast.Typ, "_")
}
开发者ID:kisielk,项目名称:llgo,代码行数:12,代码来源:gcimporter.go

示例3: findIdent

func (p *parser) findIdent() *ast.Ident {
	pos := p.pos
	name := "_"
	var obj *ast.Object
	if p.tok == token.IDENT {
		name = string(p.lit)
		obj = p.funcScope.Lookup(name)
		p.next()
	} else {
		p.expect(token.IDENT) // use expect() error handling
	}
	if obj == nil {
		// No declaration found: either we are outside any function
		// (p.funcScope == nil) or the identifier is not declared
		// in any function. Try the file and package scope.
		obj = p.fileScope.Lookup(name) // file scope is nested in package scope
		if obj == nil {
			// No declaration found anywhere: track as
			// unresolved identifier in the package scope.
			obj = ast.NewObj(ast.Err, pos, name)
			p.pkgScope.Declare(obj)
		}
	}
	return &ast.Ident{pos, obj}
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:25,代码来源:parser.go

示例4: declareShortVar

func (p *parser) declareShortVar(decl *ast.AssignStmt, list []ast.Expr) {
	// Go spec: A short variable declaration may redeclare variables
	// provided they were originally declared in the same block with
	// the same type, and at least one of the non-blank variables is new.
	n := 0 // number of new variables
	for _, x := range list {
		if ident, isIdent := x.(*ast.Ident); isIdent {
			assert(ident.Obj == nil, "identifier already declared or resolved")
			obj := ast.NewObj(ast.Var, ident.Name)
			// remember corresponding assignment for other tools
			obj.Decl = decl
			ident.Obj = obj
			if ident.Name != "_" {
				if alt := p.topScope.Insert(obj); alt != nil {
					ident.Obj = alt // redeclaration
				} else {
					n++ // new declaration
				}
			}
		} else {
			p.errorExpected(x.Pos(), "identifier on left side of :=")
		}
	}
	if n == 0 && p.mode&DeclarationErrors != 0 {
		p.error(list[0].Pos(), "no new variables on left side of :=")
	}
}
开发者ID:h12w,项目名称:gombi,代码行数:27,代码来源:parser.go

示例5: parseMethodDecl

// MethodDecl = "func" Receiver Name Signature .
// Receiver   = "(" ( identifier | "?" ) [ "*" ] ExportedName ")" [ FuncBody ].
//
func (p *gcParser) parseMethodDecl() {
	// "func" already consumed
	p.expect('(')
	recv, _ := p.parseParameter() // receiver
	p.expect(')')

	// unexported method names in imports are qualified with their package.
	fn := ast.NewObj(ast.Fun, p.parseName())
	fnType := p.parseSignature()
	fnType.Recv = recv
	fn.Type = fnType

	var recvType *Name
	if ptr, isptr := recv.Type.(*Pointer); isptr {
		recvType = ptr.Base.(*Name)
	} else {
		recvType = recv.Type.(*Name)
	}
	recvType.Methods = append(recvType.Methods, fn)
	recvType.Methods.Sort()

	if p.tok == '{' {
		p.parseFuncBody()
	}
}
开发者ID:kisielk,项目名称:llgo,代码行数:28,代码来源:gcimporter.go

示例6: 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

示例7: collectMethods

// collectMethods collects the method declarations from an AST File and
// returns a mapping from receiver types to their method FuncDecl's.
func (c *checker) collectMethods(file *ast.File) {
	for _, decl := range file.Decls {
		if funcdecl, ok := decl.(*ast.FuncDecl); ok && funcdecl.Recv != nil {
			recvField := funcdecl.Recv.List[0]
			var recv *ast.Ident
			switch typ := recvField.Type.(type) {
			case *ast.StarExpr:
				recv = typ.X.(*ast.Ident)
			case *ast.Ident:
				recv = typ
			case *ast.BadExpr:
				return
			}

			if recv.Obj == nil {
				// error reported elsewhere.
				return
			}

			if recv.Obj.Kind != ast.Typ {
				c.errorf(recv.Pos(), "%s is not a type", recv.Name)
				return
			}

			// The Obj field of the funcdecl wll be nil, so we'll have to
			// create a new one.
			funcdecl.Name.Obj = ast.NewObj(ast.Fun, funcdecl.Name.String())
			funcdecl.Name.Obj.Decl = funcdecl
			c.methods[recv.Obj] = append(c.methods[recv.Obj], funcdecl.Name.Obj)
		}
	}
}
开发者ID:kelsieflynn,项目名称:llgo,代码行数:34,代码来源:check.go

示例8: define

func define(kind ast.ObjKind, name string) *ast.Object {
	obj := ast.NewObj(kind, name)
	if scope.Insert(obj) != nil {
		panic("types internal error: double declaration")
	}
	return obj
}
开发者ID:kisielk,项目名称:llgo,代码行数:7,代码来源:universe.go

示例9: simpleImporter

func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, error) {
	pkg := imports[path]
	if pkg == nil {
		// Guess the package name without importing it. Start with the last
		// element of the path.
		name := path[strings.LastIndex(path, "/")+1:]

		// Trim commonly used prefixes and suffixes containing illegal name
		// runes.
		name = strings.TrimSuffix(name, ".go")
		name = strings.TrimSuffix(name, "-go")
		name = strings.TrimPrefix(name, "go.")
		name = strings.TrimPrefix(name, "go-")
		name = strings.TrimPrefix(name, "biogo.")

		// It's also common for the last element of the path to contain an
		// extra "go" prefix, but not always. TODO: examine unresolved ids to
		// detect when trimming the "go" prefix is appropriate.

		pkg = ast.NewObj(ast.Pkg, name)
		pkg.Data = ast.NewScope(nil)
		imports[path] = pkg
	}
	return pkg, nil
}
开发者ID:Linvas,项目名称:ant,代码行数:25,代码来源:autorouter.go

示例10: parsePkgId

// ImportPath = string_lit .
//
func (p *gcParser) parsePkgId() *ast.Object {
	id, err := strconv.Unquote(p.expect(scanner.String))
	if err != nil {
		p.error(err)
	}

	switch id {
	case "":
		// id == "" stands for the imported package id
		// (only known at time of package installation)
		id = p.id
	case "unsafe":
		// package unsafe is not in the imports map - handle explicitly
		return Unsafe
	}

	pkg := p.imports[id]
	if pkg == nil {
		scope = ast.NewScope(nil)
		pkg = ast.NewObj(ast.Pkg, "")
		pkg.Data = scope
		p.imports[id] = pkg
	}

	return pkg
}
开发者ID:WXB506,项目名称:golang,代码行数:28,代码来源:gcimporter.go

示例11: parseExportedName

// ExportedName = ImportPath "." dotIdentifier .
//
func (p *gcParser) parseExportedName(kind ast.ObjKind) *ast.Object {
	pkg := p.parsePkgId()
	p.expect('.')
	name := p.parseDotIdent()

	// a type may have been declared before - if it exists
	// already in the respective package scope, return that
	// type
	scope := pkg.Data.(*ast.Scope)
	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: %s", 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:WXB506,项目名称:golang,代码行数:33,代码来源:gcimporter.go

示例12: parseImportSpec

func parseImportSpec(p *parser, doc *ast.CommentGroup) ast.Spec {
	if p.trace {
		defer un(trace(p, "ImportSpec"))
	}

	var ident *ast.Ident
	if p.tok == token.PERIOD {
		ident = &ast.Ident{p.pos, ast.NewObj(ast.Pkg, p.pos, ".")}
		p.next()
	} else if p.tok == token.IDENT {
		ident = p.parseIdent(ast.Pkg)
		// TODO(gri) Make sure the ident is not already declared in the
		//           package scope. Also, cannot add the same name to
		//           the package scope later.
		p.declIdent(p.fileScope, ident)
	}

	var path *ast.BasicLit
	if p.tok == token.STRING {
		path = &ast.BasicLit{p.pos, p.tok, p.lit}
		p.next()
	} else {
		p.expect(token.STRING) // use expect() error handling
	}
	p.expectSemi()

	return &ast.ImportSpec{doc, ident, path, p.lineComment}
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:28,代码来源:parser.go

示例13: parseExport

// Export        = "PackageClause { Decl } "$$" .
// PackageClause = "package" identifier [ "safe" ] "\n" .
//
func (p *gcParser) parseExport() *ast.Object {
	p.expectKeyword("package")
	name := p.expect(scanner.Ident)
	if p.tok != '\n' {
		// A package is safe if it was compiled with the -u flag,
		// which disables the unsafe package.
		// TODO(gri) remember "safe" package
		p.expectKeyword("safe")
	}
	p.expect('\n')

	assert(p.imports[p.id] == nil)
	pkg := ast.NewObj(ast.Pkg, name)
	pkg.Data = ast.NewScope(nil)
	p.imports[p.id] = pkg

	for p.tok != '$' && p.tok != scanner.EOF {
		p.parseDecl()
	}

	if ch := p.scanner.Peek(); p.tok != '$' || ch != '$' {
		// don't call next()/expect() since reading past the
		// export data may cause scanner errors (e.g. NUL chars)
		p.errorf("expected '$$', got %s %c", scanner.TokenString(p.tok), ch)
	}

	if n := p.scanner.ErrorCount; n != 0 {
		p.errorf("expected no scanner errors, got %d", n)
	}

	return pkg
}
开发者ID:WXB506,项目名称:golang,代码行数:35,代码来源:gcimporter.go

示例14: init

func init() {
	scope = ast.NewScope(nil)
	Universe = scope

	Bool = defType("bool")
	defType("byte") // TODO(gri) should be an alias for uint8
	defType("complex64")
	Complex128 = defType("complex128")
	defType("float32")
	Float64 = defType("float64")
	defType("int8")
	defType("int16")
	defType("int32")
	defType("int64")
	String = defType("string")
	defType("uint8")
	defType("uint16")
	defType("uint32")
	defType("uint64")
	Int = defType("int")
	defType("uint")
	defType("uintptr")

	defConst("true")
	defConst("false")
	defConst("iota")
	defConst("nil")

	defFun("append")
	defFun("cap")
	defFun("close")
	defFun("complex")
	defFun("copy")
	defFun("delete")
	defFun("imag")
	defFun("len")
	defFun("make")
	defFun("new")
	defFun("panic")
	defFun("print")
	defFun("println")
	defFun("real")
	defFun("recover")

	scope = ast.NewScope(nil)
	Unsafe = ast.NewObj(ast.Pkg, "unsafe")
	Unsafe.Data = scope

	defType("Pointer")

	defFun("Alignof")
	defFun("New")
	defFun("NewArray")
	defFun("Offsetof")
	defFun("Reflect")
	defFun("Sizeof")
	defFun("Typeof")
	defFun("Unreflect")
}
开发者ID:Quantumboost,项目名称:gcc,代码行数:59,代码来源:universe.go

示例15: findField

// findField returns the object with the given name if visible in the type's scope.
// If no such object is found, an error is reported and a bad object is returned instead.
func (tc *typechecker) findField(typ *ast.Type, name *ast.Ident) (obj *ast.Object) {
	// TODO(gri) This is simplistic at the moment and ignores anonymous fields.
	obj = typ.Scope.Lookup(name.Name)
	if obj == nil {
		tc.Errorf(name.Pos(), "%s not declared", name.Name)
		obj = ast.NewObj(ast.Bad, name.Name)
	}
	return
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:11,代码来源:scope.go


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