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


Golang Object.Pkg方法代码示例

本文整理汇总了Golang中golang.org/x/tools/go/types.Object.Pkg方法的典型用法代码示例。如果您正苦于以下问题:Golang Object.Pkg方法的具体用法?Golang Object.Pkg怎么用?Golang Object.Pkg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在golang.org/x/tools/go/types.Object的用法示例。


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

示例1: checkInPackageBlock

// checkInPackageBlock performs safety checks for renames of
// func/var/const/type objects in the package block.
func (e *Export) checkInPackageBlock(from types.Object, to string) {
	info := e.u.pkgInfo
	lexinfo := lexical.Structure(e.u.prog.Fset, from.Pkg(), &info.Info, info.Files)

	// We don't rename anything in the package block to init, as that might
	// conflict or otherwise break stuff
	if to == "init" {
		e.Conflicting = true
		return
	}

	// Check for conflicts between package block and all file blocks.
	for _, f := range info.Files {
		if _, b := lexinfo.Blocks[f].Lookup(to); b == lexinfo.Blocks[f] {
			e.Conflicting = true
			return
		}
	}

	if f, ok := from.(*types.Func); ok && recv(f) == nil {
		e.checkFunction(f, to)
		if e.Conflicting {
			return
		}
	}

	// Check for conflicts in lexical scope.
	// Do not need to check all imported packages:
	// Since it's unnecessarily exported, no one else is going to be sad if I unexport it!
	e.checkInLexicalScope(from, to)
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:33,代码来源:check.go

示例2: checkInLocalScope

func (r *Unexporter) checkInLocalScope(objsToUpdate map[types.Object]string, from types.Object, to string) {
	info := r.packages[from.Pkg()]

	// Is this object an implicit local var for a type switch?
	// Each case has its own var, whose position is the decl of y,
	// but Ident in that decl does not appear in the Uses map.
	//
	//   switch y := x.(type) {	 // Defs[Ident(y)] is undefined
	//   case int:    print(y)       // Implicits[CaseClause(int)]    = Var(y_int)
	//   case string: print(y)       // Implicits[CaseClause(string)] = Var(y_string)
	//   }
	//
	var isCaseVar bool
	for syntax, obj := range info.Implicits {
		if _, ok := syntax.(*ast.CaseClause); ok && obj.Pos() == from.Pos() {
			isCaseVar = true
			r.check(objsToUpdate, obj, to)
		}
	}

	r.checkInLexicalScope(objsToUpdate, from, to, info)

	// Finally, if this was a type switch, change the variable y.
	if isCaseVar {
		_, path, _ := r.iprog.PathEnclosingInterval(from.Pos(), from.Pos())
		path[0].(*ast.Ident).Name = to // path is [Ident AssignStmt TypeSwitchStmt...]
	}
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:28,代码来源:check.go

示例3: formatMember

func formatMember(obj types.Object, maxname int) string {
	qualifier := types.RelativeTo(obj.Pkg())
	var buf bytes.Buffer
	fmt.Fprintf(&buf, "%-5s %-*s", tokenOf(obj), maxname, obj.Name())
	switch obj := obj.(type) {
	case *types.Const:
		fmt.Fprintf(&buf, " %s = %s", types.TypeString(obj.Type(), qualifier), obj.Val().String())

	case *types.Func:
		fmt.Fprintf(&buf, " %s", types.TypeString(obj.Type(), qualifier))

	case *types.TypeName:
		// Abbreviate long aggregate type names.
		var abbrev string
		switch t := obj.Type().Underlying().(type) {
		case *types.Interface:
			if t.NumMethods() > 1 {
				abbrev = "interface{...}"
			}
		case *types.Struct:
			if t.NumFields() > 1 {
				abbrev = "struct{...}"
			}
		}
		if abbrev == "" {
			fmt.Fprintf(&buf, " %s", types.TypeString(obj.Type().Underlying(), qualifier))
		} else {
			fmt.Fprintf(&buf, " %s", abbrev)
		}

	case *types.Var:
		fmt.Fprintf(&buf, " %s", types.TypeString(obj.Type(), qualifier))
	}
	return buf.String()
}
开发者ID:Requilence,项目名称:integram,代码行数:35,代码来源:describe.go

示例4: checkSelections

// checkSelection checks that all uses and selections that resolve to
// the specified object would continue to do so after the renaming.
func (r *Unexporter) checkSelections(objsToUpdate map[types.Object]string, from types.Object, to string) {
	for pkg, info := range r.packages {
		if id := someUse(info, from); id != nil {
			if !r.checkExport(id, pkg, from, to) {
				return
			}
		}

		for syntax, sel := range info.Selections {
			// There may be extant selections of only the old
			// name or only the new name, so we must check both.
			// (If neither, the renaming is sound.)
			//
			// In both cases, we wish to compare the lengths
			// of the implicit field path (Selection.Index)
			// to see if the renaming would change it.
			//
			// If a selection that resolves to 'from', when renamed,
			// would yield a path of the same or shorter length,
			// this indicates ambiguity or a changed referent,
			// analogous to same- or sub-block lexical conflict.
			//
			// If a selection using the name 'to' would
			// yield a path of the same or shorter length,
			// this indicates ambiguity or shadowing,
			// analogous to same- or super-block lexical conflict.

			// TODO(adonovan): fix: derive from Types[syntax.X].Mode
			// TODO(adonovan): test with pointer, value, addressable value.
			isAddressable := true

			if sel.Obj() == from {
				if obj, indices, _ := types.LookupFieldOrMethod(sel.Recv(), isAddressable, from.Pkg(), to); obj != nil {
					// Renaming this existing selection of
					// 'from' may block access to an existing
					// type member named 'to'.
					delta := len(indices) - len(sel.Index())
					if delta > 0 {
						continue // no ambiguity
					}
					r.selectionConflict(objsToUpdate, from, to, delta, syntax, obj)
					return
				}

			} else if sel.Obj().Name() == to {
				if obj, indices, _ := types.LookupFieldOrMethod(sel.Recv(), isAddressable, from.Pkg(), from.Name()); obj == from {
					// Renaming 'from' may cause this existing
					// selection of the name 'to' to change
					// its meaning.
					delta := len(indices) - len(sel.Index())
					if delta > 0 {
						continue //  no ambiguity
					}
					r.selectionConflict(objsToUpdate, from, to, -delta, syntax, sel.Obj())
					return
				}
			}
		}
	}
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:62,代码来源:check.go

示例5: checkInPackageBlock

// checkInPackageBlock performs safety checks for renames of
// func/var/const/type objects in the package block.
func (r *renamer) checkInPackageBlock(from types.Object) {
	// Check that there are no references to the name from another
	// package if the renaming would make it unexported.
	if ast.IsExported(from.Name()) && !ast.IsExported(r.to) {
		for pkg, info := range r.packages {
			if pkg == from.Pkg() {
				continue
			}
			if id := someUse(info, from); id != nil &&
				!r.checkExport(id, pkg, from) {
				break
			}
		}
	}

	info := r.packages[from.Pkg()]

	// Check that in the package block, "init" is a function, and never referenced.
	if r.to == "init" {
		kind := objectKind(from)
		if kind == "func" {
			// Reject if intra-package references to it exist.
			for id, obj := range info.Uses {
				if obj == from {
					r.errorf(from.Pos(),
						"renaming this func %q to %q would make it a package initializer",
						from.Name(), r.to)
					r.errorf(id.Pos(), "\tbut references to it exist")
					break
				}
			}
		} else {
			r.errorf(from.Pos(), "you cannot have a %s at package level named %q",
				kind, r.to)
		}
	}

	// Check for conflicts between package block and all file blocks.
	for _, f := range info.Files {
		fileScope := info.Info.Scopes[f]
		b, prev := fileScope.LookupParent(r.to, token.NoPos)
		if b == fileScope {
			r.errorf(from.Pos(), "renaming this %s %q to %q would conflict",
				objectKind(from), from.Name(), r.to)
			r.errorf(prev.Pos(), "\twith this %s",
				objectKind(prev))
			return // since checkInPackageBlock would report redundant errors
		}
	}

	// Check for conflicts in lexical scope.
	if from.Exported() {
		for _, info := range r.packages {
			r.checkInLexicalScope(from, info)
		}
	} else {
		r.checkInLexicalScope(from, info)
	}
}
开发者ID:binaryedge,项目名称:40fy-client,代码行数:61,代码来源:check.go

示例6: simpleObjInfo

func simpleObjInfo(obj types.Object) string {
	pkg := obj.Pkg()
	s := simpleType(obj.String())
	if pkg != nil && pkg.Name() == "main" {
		return strings.Replace(s, simpleType(pkg.Path())+".", "", -1)
	}
	return s
}
开发者ID:donseba,项目名称:gotools,代码行数:8,代码来源:types.go

示例7: checkInPackageBlock

// checkInPackageBlock performs safety checks for renames of
// func/var/const/type objects in the package block.
func (r *Unexporter) checkInPackageBlock(objsToUpdate map[types.Object]string, from types.Object, to string) {
	// Check that there are no references to the name from another
	// package if the renaming would make it unexported.
	if ast.IsExported(from.Name()) && !ast.IsExported(to) {
		for pkg, info := range r.packages {
			if pkg == from.Pkg() {
				continue
			}
			if id := someUse(info, from); id != nil &&
				!r.checkExport(id, pkg, from, to) {
				break
			}
		}
	}

	info := r.packages[from.Pkg()]
	lexinfo := r.lexInfo(info)

	// Check that in the package block, "init" is a function, and never referenced.
	if to == "init" {
		kind := objectKind(from)
		if kind == "func" {
			// Reject if intra-package references to it exist.
			if refs := lexinfo.Refs[from]; len(refs) > 0 {
				r.warn(from,
					r.errorf(from.Pos(),
						"renaming this func %q to %q would make it a package initializer",
						from.Name(), to),
					r.errorf(refs[0].Id.Pos(), "\tbut references to it exist"))
			}
		} else {
			r.warn(from, r.errorf(from.Pos(), "you cannot have a %s at package level named %q",
				kind, to))
		}
	}

	// Check for conflicts between package block and all file blocks.
	for _, f := range info.Files {
		if prev, b := lexinfo.Blocks[f].Lookup(to); b == lexinfo.Blocks[f] {
			r.warn(from,
				r.errorf(from.Pos(), "renaming this %s %q to %q would conflict",
					objectKind(from), from.Name(), to),
				r.errorf(prev.Pos(), "\twith this %s",
					objectKind(prev)))
			return // since checkInPackageBlock would report redundant errors
		}
	}

	// Check for conflicts in lexical scope.
	if from.Exported() {
		for _, info := range r.packages {
			r.checkInLexicalScope(objsToUpdate, from, to, info)
		}
	} else {
		r.checkInLexicalScope(objsToUpdate, from, to, info)
	}
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:59,代码来源:check.go

示例8: addSymbol

func (sym *symtab) addSymbol(obj types.Object) {
	fn := types.ObjectString(obj, nil)
	n := obj.Name()
	pkg := obj.Pkg()
	id := n
	if pkg != nil {
		id = pkg.Name() + "_" + n
	}
	switch obj.(type) {
	case *types.Const:
		sym.syms[fn] = &symbol{
			gopkg:   pkg,
			goobj:   obj,
			kind:    skConst,
			id:      id,
			goname:  n,
			cgoname: "cgo_const_" + id,
			cpyname: "cpy_const_" + id,
		}
		sym.addType(obj, obj.Type())

	case *types.Var:
		sym.syms[fn] = &symbol{
			gopkg:   pkg,
			goobj:   obj,
			kind:    skVar,
			id:      id,
			goname:  n,
			cgoname: "cgo_var_" + id,
			cpyname: "cpy_var_" + id,
		}
		sym.addType(obj, obj.Type())

	case *types.Func:
		sym.syms[fn] = &symbol{
			gopkg:   pkg,
			goobj:   obj,
			kind:    skFunc,
			id:      id,
			goname:  n,
			cgoname: "cgo_func_" + id,
			cpyname: "cpy_func_" + id,
		}
		sig := obj.Type().Underlying().(*types.Signature)
		sym.processTuple(sig.Params())
		sym.processTuple(sig.Results())

	case *types.TypeName:
		sym.addType(obj, obj.Type())

	default:
		panic(fmt.Errorf("gopy: handled object [%#v]", obj))
	}
}
开发者ID:kellrott,项目名称:gopy,代码行数:54,代码来源:symtab.go

示例9: newObject

func newObject(obj types.Object, sel *types.Selection) (*Object, error) {
	// WARN: Dev only
	if sel != nil {
		return newSelector(sel)
	}
	o := &Object{
		Name: obj.Name(),
		pos:  obj.Pos(),
	}
	o.setPkg(obj.Pkg())
	switch typ := obj.(type) {
	case *types.PkgName:
		o.ObjType = Package
		if p := typ.Imported(); p != nil {
			o.setPkg(p)
		}
	case *types.Const:
		o.ObjType = Const
	case *types.TypeName:
		o.ObjType = TypeName
	case *types.Var:
		o.ObjType = Var
		o.IsField = typ.IsField()
		if t, ok := derefType(typ.Type()).(*types.Named); ok {
			o.ObjType = TypeName
			// WARN: This looks wrong
			o.IsField = false
			if obj := t.Obj(); obj != nil {
				o.Name = obj.Name()
				o.setPkg(obj.Pkg())
				o.pos = obj.Pos() // WARN
			}
		}
	case *types.Func:
		if sig := typ.Type().(*types.Signature); sig != nil {
			o.ObjType = Func
		} else {
			switch r := derefType(sig.Recv().Type()).(type) {
			case *types.Named:
				o.ObjType = Method
				o.setParent(r.Obj())
			case *types.Interface:
				o.ObjType = Interface
			default:
				// This should never happen
			}
		}
	default:
		// TODO: log type
		o.ObjType = Bad
	}
	return o, nil
}
开发者ID:charlievieth,项目名称:define,代码行数:53,代码来源:object.go

示例10: checkExport

func (r *renamer) checkExport(id *ast.Ident, pkg *types.Package, from types.Object) bool {
	// Reject cross-package references if r.to is unexported.
	// (Such references may be qualified identifiers or field/method
	// selections.)
	if !ast.IsExported(r.to) && pkg != from.Pkg() {
		r.errorf(from.Pos(),
			"renaming this %s %q to %q would make it unexported",
			objectKind(from), from.Name(), r.to)
		r.errorf(id.Pos(), "\tbreaking references from packages such as %q",
			pkg.Path())
		return false
	}
	return true
}
开发者ID:jappre,项目名称:mutation,代码行数:14,代码来源:check.go

示例11: getKey

func getKey(obj types.Object) object {
	if obj == nil {
		return object{}
	}

	pkg := obj.Pkg()
	pkgPath := ""
	if pkg != nil {
		pkgPath = pkg.Path()
	}

	return object{
		pkgPath: pkgPath,
		name:    obj.Name(),
	}
}
开发者ID:aganno2,项目名称:check,代码行数:16,代码来源:varcheck.go

示例12: newFuncFrom

func newFuncFrom(p *Package, parent string, obj types.Object, sig *types.Signature) (Func, error) {
	haserr := false
	res := sig.Results()
	var ret types.Type

	switch res.Len() {
	case 2:
		if !isErrorType(res.At(1).Type()) {
			return Func{}, fmt.Errorf(
				"bind: second result value must be of type error: %s",
				obj,
			)
		}
		haserr = true
		ret = res.At(0).Type()

	case 1:
		if isErrorType(res.At(0).Type()) {
			haserr = true
			ret = nil
		} else {
			ret = res.At(0).Type()
		}
	case 0:
		ret = nil
	default:
		return Func{}, fmt.Errorf("bind: too many results to return: %v", obj)
	}

	id := obj.Pkg().Name() + "_" + obj.Name()
	if parent != "" {
		id = obj.Pkg().Name() + "_" + parent + "_" + obj.Name()
	}

	return Func{
		pkg:  p,
		sig:  newSignatureFrom(p, sig),
		typ:  obj.Type(),
		name: obj.Name(),
		id:   id,
		doc:  p.getDoc(parent, obj),
		ret:  ret,
		err:  haserr,
	}, nil
}
开发者ID:kellrott,项目名称:gopy,代码行数:45,代码来源:package.go

示例13: makeDefInfo

func (g *Grapher) makeDefInfo(obj types.Object) (*DefKey, *defInfo, error) {
	switch obj := obj.(type) {
	case *types.Builtin:
		return &DefKey{"builtin", []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
	case *types.Nil:
		return &DefKey{"builtin", []string{"nil"}}, &defInfo{pkgscope: false, exported: true}, nil
	case *types.TypeName:
		if basic, ok := obj.Type().(*types.Basic); ok {
			return &DefKey{"builtin", []string{basic.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
		}
		if obj.Name() == "error" {
			return &DefKey{"builtin", []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
		}
	case *types.PkgName:
		return &DefKey{obj.Imported().Path(), []string{}}, &defInfo{pkgscope: false, exported: true}, nil
	case *types.Const:
		var pkg string
		if obj.Pkg() == nil {
			pkg = "builtin"
		} else {
			pkg = obj.Pkg().Path()
		}
		if obj.Val().Kind() == exact.Bool && pkg == "builtin" {
			return &DefKey{pkg, []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
		}
	}

	if obj.Pkg() == nil {
		// builtin
		return &DefKey{"builtin", []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
	}

	path := g.path(obj)

	// Handle the case where a dir has 2 main packages that are not
	// intended to be compiled together and have overlapping def
	// paths. Prefix the def path with the filename.
	if obj.Pkg().Name() == "main" {
		p := g.program.Fset.Position(obj.Pos())
		path = append([]string{filepath.Base(p.Filename)}, path...)
	}

	return &DefKey{obj.Pkg().Path(), path}, &defInfo{pkgscope: g.pkgscope[obj], exported: g.exported[obj]}, nil
}
开发者ID:ildarisaev,项目名称:srclib-go,代码行数:44,代码来源:graph.go

示例14: getEnclosingStruct

func getEnclosingStruct(object types.Object) types.Type {
	pkgScope := object.Pkg().Scope()
	s := pkgScope.Innermost(object.Pos())
	if s.Parent() == pkgScope {
		s = pkgScope
	}

	var obj types.Object
	for _, name := range s.Names() {
		o := s.Lookup(name)
		if o.Pos() <= object.Pos() && (obj == nil || o.Pos() > obj.Pos()) {
			obj = o
		}
	}

	if obj == nil {
		return nil
	}
	if obj == object {
		return obj.Type()
	}

	t := obj.Type().Underlying().(*types.Struct)
	for {
		var f types.Object
		for i := 0; i < t.NumFields(); i++ {
			field := t.Field(i)
			if field == object {
				return t
			}
			if field.Pos() <= object.Pos() && (f == nil || field.Pos() > f.Pos()) {
				f = field
			}
		}
		if fs, ok := f.Type().(*types.Struct); ok {
			t = fs
		} else {
			break
		}
	}

	return t
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:43,代码来源:util.go

示例15: objectName

func (c *funcContext) objectName(o types.Object) string {
	if isPkgLevel(o) {
		c.p.dependencies[o] = true

		if o.Pkg() != c.p.Pkg || (isVarOrConst(o) && o.Exported()) {
			return c.pkgVar(o.Pkg()) + "." + o.Name()
		}
	}

	name, ok := c.p.objectNames[o]
	if !ok {
		name = c.newVariableWithLevel(o.Name(), isPkgLevel(o))
		c.p.objectNames[o] = name
	}

	if v, ok := o.(*types.Var); ok && c.p.escapingVars[v] {
		return name + "[0]"
	}
	return name
}
开发者ID:mcanthony,项目名称:gopherjs,代码行数:20,代码来源:utils.go


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