本文整理汇总了Golang中golang.org/x/tools/go/types.Object类的典型用法代码示例。如果您正苦于以下问题:Golang Object类的具体用法?Golang Object怎么用?Golang Object使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Object类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: 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
示例2: isStringer
func isStringer(obj types.Object) bool {
switch obj := obj.(type) {
case *types.Func:
if obj.Name() != "String" {
return false
}
sig, ok := obj.Type().(*types.Signature)
if !ok {
return false
}
if sig.Recv() == nil {
return false
}
if sig.Params().Len() != 0 {
return false
}
res := sig.Results()
if res.Len() != 1 {
return false
}
ret := res.At(0).Type()
if ret != types.Universe.Lookup("string").Type() {
return false
}
return true
default:
return false
}
return false
}
开发者ID:vkolev,项目名称:gopy,代码行数:31,代码来源:utils.go
示例3: lookup
// lookup returns the address of the named variable identified by obj
// that is local to function f or one of its enclosing functions.
// If escaping, the reference comes from a potentially escaping pointer
// expression and the referent must be heap-allocated.
//
func (f *Function) lookup(obj types.Object, escaping bool) Value {
if v, ok := f.objects[obj]; ok {
if alloc, ok := v.(*Alloc); ok && escaping {
alloc.Heap = true
}
return v // function-local var (address)
}
// Definition must be in an enclosing function;
// plumb it through intervening closures.
if f.parent == nil {
panic("no Value for type.Object " + obj.Name())
}
outer := f.parent.lookup(obj, true) // escaping
v := &FreeVar{
name: obj.Name(),
typ: outer.Type(),
pos: outer.Pos(),
outer: outer,
parent: f,
}
f.objects[obj] = v
f.FreeVars = append(f.FreeVars, v)
return v
}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:30,代码来源:func.go
示例4: selectionConflict
func (r *Unexporter) selectionConflict(objsToUpdate map[types.Object]string, from types.Object, to string, delta int, syntax *ast.SelectorExpr, obj types.Object) {
rename := r.errorf(from.Pos(), "renaming this %s %q to %q",
objectKind(from), from.Name(), to)
switch {
case delta < 0:
// analogous to sub-block conflict
r.warn(from, rename,
r.errorf(syntax.Sel.Pos(),
"\twould change the referent of this selection"),
r.errorf(obj.Pos(), "\tof this %s", objectKind(obj)))
case delta == 0:
// analogous to same-block conflict
r.warn(from, rename,
r.errorf(syntax.Sel.Pos(),
"\twould make this reference ambiguous"),
r.errorf(obj.Pos(), "\twith this %s", objectKind(obj)))
case delta > 0:
// analogous to super-block conflict
r.warn(from, rename,
r.errorf(syntax.Sel.Pos(),
"\twould shadow this selection"),
r.errorf(obj.Pos(), "\tof the %s declared here",
objectKind(obj)))
}
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:26,代码来源:check.go
示例5: 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
示例6: isLocal
// isLocal reports whether obj is local to some function.
// Precondition: not a struct field or interface method.
func isLocal(obj types.Object) bool {
// [... 5=stmt 4=func 3=file 2=pkg 1=universe]
var depth int
for scope := obj.Parent(); scope != nil; scope = scope.Parent() {
depth++
}
return depth >= 4
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:10,代码来源:util14.go
示例7: 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
示例8: decl
func (v *visitor) decl(obj types.Object) {
key := getKey(obj)
if _, ok := v.uses[key]; !ok {
v.uses[key] = 0
}
if _, ok := v.positions[key]; !ok {
v.positions[key] = v.prog.Fset.Position(obj.Pos())
}
}
开发者ID:aganno2,项目名称:check,代码行数:9,代码来源:varcheck.go
示例9: sameObj
// same reports whether x and y are identical, or both are PkgNames
// that import the same Package.
//
func sameObj(x, y types.Object) bool {
if x == y {
return true
}
if x, ok := x.(*types.PkgName); ok {
if y, ok := y.(*types.PkgName); ok {
return x.Imported() == y.Imported()
}
}
return false
}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:14,代码来源:referrers.go
示例10: defineObject
func (r *resolver) defineObject(b *Block, name string, obj types.Object) {
if obj.Name() == "_" {
return
}
i := len(b.bindings)
b.bindings = append(b.bindings, obj)
b.index[name] = i
if trace {
logf("def %s = %s in %s\n", name, types.ObjectString(obj, r.qualifier), b)
}
r.result.Defs[obj] = b
}
开发者ID:Christeefym,项目名称:lantern,代码行数:12,代码来源:lexical.go
示例11: checkInLexicalScope
func (e *Export) checkInLexicalScope(from types.Object, to string) {
info := e.u.pkgInfo
lexinfo := lexical.Structure(e.u.prog.Fset, info.Pkg, &info.Info, info.Files)
b := lexinfo.Defs[from] // the block defining the 'from' object
if b != nil {
to, toBlock := b.Lookup(to)
if toBlock == b {
e.Conflicting = true
return // same-block conflict
} else if toBlock != nil {
for _, ref := range lexinfo.Refs[to] {
if obj, _ := ref.Env.Lookup(from.Name()); obj == from {
e.Conflicting = true
return // super-block conflict
}
}
}
}
// Check for sub-block conflict.
// Is there an intervening definition of r.to between
// the block defining 'from' and some reference to it?
for _, ref := range lexinfo.Refs[from] {
_, fromBlock := ref.Env.Lookup(from.Name())
fromDepth := fromBlock.Depth()
to, toBlock := ref.Env.Lookup(to)
if to != nil {
// sub-block conflict
if toBlock.Depth() > fromDepth {
e.Conflicting = true
return
}
}
}
// Renaming a type that is used as an embedded field
// requires renaming the field too. e.g.
// type T int // if we rename this to U..
// var s struct {T}
// print(s.T) // ...this must change too
if _, ok := from.(*types.TypeName); ok {
for id, obj := range info.Uses {
if obj == from {
if field := info.Defs[id]; field != nil {
e.check(field, to)
}
}
}
}
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:52,代码来源:check.go
示例12: printObject
func printObject(u *unexporter.Unexporter, o types.Object) {
var objName string
if simpleNamesFlag {
objName = o.Name()
} else {
objName = o.String()
}
if showFilename {
pos := u.PositionForObject(o)
fmt.Printf("%s:%d:%d: %s\n", pos.Filename, pos.Line, pos.Column, objName)
} else {
fmt.Println(objName)
}
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:14,代码来源:main.go
示例13: 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
示例14: 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
示例15: objectKind
func objectKind(obj types.Object) string {
switch obj := obj.(type) {
case *types.PkgName:
return "imported package name"
case *types.TypeName:
return "type"
case *types.Var:
if obj.IsField() {
return "field"
}
case *types.Func:
if obj.Type().(*types.Signature).Recv() != nil {
return "method"
}
}
// label, func, var, const
return strings.ToLower(strings.TrimPrefix(reflect.TypeOf(obj).String(), "*types."))
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:18,代码来源:util14.go
示例16: getDoc
// getDoc returns the doc string associated with types.Object
func (p *Package) getDoc(o types.Object) string {
n := o.Name()
switch o.(type) {
case *types.Const:
for _, c := range p.doc.Consts {
for _, cn := range c.Names {
if n == cn {
return c.Doc
}
}
}
case *types.Var:
for _, v := range p.doc.Vars {
for _, vn := range v.Names {
if n == vn {
return v.Doc
}
}
}
case *types.Func:
for _, f := range p.doc.Funcs {
if n == f.Name {
return f.Doc
}
}
case *types.TypeName:
for _, t := range p.doc.Types {
if n == t.Name {
return t.Doc
}
}
default:
// TODO(sbinet)
panic(fmt.Errorf("not yet supported: %v (%T)", o, o))
}
return ""
}
开发者ID:justinfx,项目名称:gopy,代码行数:43,代码来源:bind.go
示例17: 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
示例18: ssaValueForIdent
// ssaValueForIdent returns the ssa.Value for the ast.Ident whose path
// to the root of the AST is path. isAddr reports whether the
// ssa.Value is the address denoted by the ast.Ident, not its value.
//
func ssaValueForIdent(prog *ssa.Program, qinfo *loader.PackageInfo, obj types.Object, path []ast.Node) (value ssa.Value, isAddr bool, err error) {
switch obj := obj.(type) {
case *types.Var:
pkg := prog.Package(qinfo.Pkg)
pkg.Build()
if v, addr := prog.VarValue(obj, pkg, path); v != nil {
return v, addr, nil
}
return nil, false, fmt.Errorf("can't locate SSA Value for var %s", obj.Name())
case *types.Func:
fn := prog.FuncValue(obj)
if fn == nil {
return nil, false, fmt.Errorf("%s is an interface method", obj)
}
// TODO(adonovan): there's no point running PTA on a *Func ident.
// Eliminate this feature.
return fn, false, nil
}
panic(obj)
}
开发者ID:Requilence,项目名称:integram,代码行数:25,代码来源:pointsto.go
示例19: selectionConflict
func (r *renamer) selectionConflict(from types.Object, delta int, syntax *ast.SelectorExpr, obj types.Object) {
r.errorf(from.Pos(), "renaming this %s %q to %q",
objectKind(from), from.Name(), r.to)
switch {
case delta < 0:
// analogous to sub-block conflict
r.errorf(syntax.Sel.Pos(),
"\twould change the referent of this selection")
r.errorf(obj.Pos(), "\tto this %s", objectKind(obj))
case delta == 0:
// analogous to same-block conflict
r.errorf(syntax.Sel.Pos(),
"\twould make this reference ambiguous")
r.errorf(obj.Pos(), "\twith this %s", objectKind(obj))
case delta > 0:
// analogous to super-block conflict
r.errorf(syntax.Sel.Pos(),
"\twould shadow this selection")
r.errorf(obj.Pos(), "\tto the %s declared here",
objectKind(obj))
}
}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:23,代码来源:check.go
示例20: addParamObj
func (f *Function) addParamObj(obj types.Object) *Parameter {
name := obj.Name()
if name == "" {
name = fmt.Sprintf("arg%d", len(f.Params))
}
param := f.addParam(name, obj.Type(), obj.Pos())
param.object = obj
return param
}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:9,代码来源:func.go
注:本文中的golang.org/x/tools/go/types.Object类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论