本文整理汇总了Golang中golang.org/x/tools/go/types.Object类的典型用法代码示例。如果您正苦于以下问题:Golang Object类的具体用法?Golang Object怎么用?Golang Object使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Object类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
}
}
}
}
示例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
}
示例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
}
示例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)))
}
}
示例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)
}
示例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
}
示例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
}
示例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())
}
}
示例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
}
示例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
}
示例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)
}
}
}
}
}
示例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)
}
}
示例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...]
}
}
示例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(),
}
}
示例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."))
}