本文整理汇总了Golang中golang.org/x/tools/go/types.Object.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Object.String方法的具体用法?Golang Object.String怎么用?Golang Object.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types.Object
的用法示例。
在下文中一共展示了Object.String方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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 ssa.Value for " + obj.String())
}
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
}
示例2: 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
}
示例3: memberFromObject
// memberFromObject populates package pkg with a member for the
// typechecker object obj.
//
// For objects from Go source code, syntax is the associated syntax
// tree (for funcs and vars only); it will be used during the build
// phase.
//
func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
name := obj.Name()
switch obj := obj.(type) {
case *types.TypeName:
pkg.Members[name] = &Type{
object: obj,
pkg: pkg,
}
case *types.Const:
c := &NamedConst{
object: obj,
Value: NewConst(obj.Val(), obj.Type()),
pkg: pkg,
}
pkg.values[obj] = c.Value
pkg.Members[name] = c
case *types.Var:
g := &Global{
Pkg: pkg,
name: name,
object: obj,
typ: types.NewPointer(obj.Type()), // address
pos: obj.Pos(),
}
pkg.values[obj] = g
pkg.Members[name] = g
case *types.Func:
sig := obj.Type().(*types.Signature)
if sig.Recv() == nil && name == "init" {
pkg.ninit++
name = fmt.Sprintf("init#%d", pkg.ninit)
}
fn := &Function{
name: name,
object: obj,
Signature: sig,
syntax: syntax,
pos: obj.Pos(),
Pkg: pkg,
Prog: pkg.Prog,
}
if syntax == nil {
fn.Synthetic = "loaded from gc object file"
}
pkg.values[obj] = fn
if sig.Recv() == nil {
pkg.Members[name] = fn // package-level function
}
default: // (incl. *types.Package)
panic("unexpected Object type: " + obj.String())
}
}
示例4: 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)
}
}
示例5: IsSameObject
func IsSameObject(a, b types.Object) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
var apath string
var bpath string
if a.Pkg() != nil {
apath = a.Pkg().Path()
}
if b.Pkg() != nil {
bpath = b.Pkg().Path()
}
if apath != bpath {
return false
}
if a.Id() != b.Id() {
return false
}
return a.String() == b.String()
}
示例6: path
func (g *Grapher) path(obj types.Object) (path []string) {
if path, present := g.paths[obj]; present {
return path
}
var scope *types.Scope
pkgInfo, astPath, _ := g.program.PathEnclosingInterval(obj.Pos(), obj.Pos())
if astPath != nil {
for _, node := range astPath {
if s, hasScope := pkgInfo.Scopes[node]; hasScope {
scope = s
}
}
}
if scope == nil {
scope = obj.Parent()
}
if scope == nil {
// TODO(sqs): make this actually handle cases like the one described in
// https://github.com/sourcegraph/sourcegraph.com/issues/218
log.Printf("Warning: no scope for object %s at pos %s", obj.String(), g.program.Fset.Position(obj.Pos()))
return nil
}
prefix, hasPath := g.scopePaths[scope]
if !hasPath {
panic("no scope path for scope " + scope.String())
}
path = append([]string{}, prefix...)
p := g.program.Fset.Position(obj.Pos())
path = append(path, obj.Name()+uniqID(p))
return path
panic("no scope node for object " + obj.String())
}
示例7: LookupObjects
//.........这里部分代码省略.........
break
}
}
}
} else {
for k, v := range conf.Info.Defs {
if k != nil && v != nil && IsSameObject(v, cursorObj) {
cursorPos = k.Pos()
break
}
}
}
}
// if kind == ObjField || cursorIsInterfaceMethod {
// fieldTypeInfo = conf.Info
// }
}
// if kind == ObjField {
// fieldTypeObj = w.LookupStructFromField(fieldTypeInfo, cursorPkg, cursorObj, cursorPos)
// }
if typesFindDef {
fmt.Println(w.fset.Position(cursorPos))
}
if typesFindInfo {
if kind == ObjField && fieldTypeObj != nil {
typeName := fieldTypeObj.Name()
if fieldTypeObj.Pkg() != nil && fieldTypeObj.Pkg() != pkg {
typeName = fieldTypeObj.Pkg().Name() + "." + fieldTypeObj.Name()
}
fmt.Println(typeName, simpleObjInfo(cursorObj))
} else if kind == ObjBuiltin {
fmt.Println(builtinInfo(cursorObj.Name()))
} else if kind == ObjPkgName {
fmt.Println(cursorObj.String())
} else if cursorIsInterfaceMethod {
fmt.Println(strings.Replace(simpleObjInfo(cursorObj), "(interface)", cursorPkg.Name()+"."+cursorInterfaceTypeName, 1))
} else {
fmt.Println(simpleObjInfo(cursorObj))
}
}
if typesFindDoc && typesFindDef {
pos := w.fset.Position(cursorPos)
file := w.parsedFileCache[pos.Filename]
if file != nil {
line := pos.Line
var group *ast.CommentGroup
for _, v := range file.Comments {
lastLine := w.fset.Position(v.End()).Line
if lastLine == line || lastLine == line-1 {
group = v
} else if lastLine > line {
break
}
}
if group != nil {
fmt.Println(group.Text())
}
}
}
if !typesFindUse {
return
}
var usages []int
if kind == ObjPkgName {
示例8: LookupObjects
//.........这里部分代码省略.........
if cursorPkg != nil && cursorPkg != pkg &&
kind != ObjPkgName && w.isBinaryPkg(cursorPkg.Path()) {
conf := &PkgConfig{
IgnoreFuncBodies: true,
AllowBinary: true,
WithTestFiles: true,
Info: &types.Info{
Defs: make(map[*ast.Ident]types.Object),
},
}
pkg, _ := w.Import("", cursorPkg.Path(), conf)
if pkg != nil {
if cursorIsInterfaceMethod {
for _, obj := range conf.Info.Defs {
if obj == nil {
continue
}
if fn, ok := obj.(*types.Func); ok {
if fn.Name() == cursorObj.Name() {
if sig, ok := fn.Type().Underlying().(*types.Signature); ok {
if named, ok := sig.Recv().Type().(*types.Named); ok {
if named.Obj() != nil && named.Obj().Name() == cursorInterfaceTypeName {
cursorPos = obj.Pos()
break
}
}
}
}
}
}
} else {
for _, obj := range conf.Info.Defs {
if obj != nil && obj.String() == cursorObj.String() {
cursorPos = obj.Pos()
break
}
}
}
}
if kind == ObjField || cursorIsInterfaceMethod {
fieldTypeInfo = conf.Info
}
}
if kind == ObjField {
fieldTypeObj = w.LookupStructFromField(fieldTypeInfo, cursorPkg, cursorObj, cursorPos)
}
if typeFindDef {
fmt.Println(w.fset.Position(cursorPos))
}
if typeFindInfo {
if kind == ObjField && fieldTypeObj != nil {
typeName := fieldTypeObj.Name()
if fieldTypeObj.Pkg() != nil && fieldTypeObj.Pkg() != pkg {
typeName = fieldTypeObj.Pkg().Name() + "." + fieldTypeObj.Name()
}
fmt.Println(typeName, simpleType(cursorObj.String()))
} else if kind == ObjBuiltin {
fmt.Println(builtinInfo(cursorObj.Name()))
} else if kind == ObjPkgName {
fmt.Println(cursorObj.String())
} else if cursorIsInterfaceMethod {
fmt.Println(strings.Replace(simpleType(cursorObj.String()), "(interface)", cursorPkg.Name()+"."+cursorInterfaceTypeName, 1))
} else {
fmt.Println(simpleType(cursorObj.String()))
}