本文整理汇总了Golang中golang.org/x/tools/go/types.Object.Name方法的典型用法代码示例。如果您正苦于以下问题:Golang Object.Name方法的具体用法?Golang Object.Name怎么用?Golang Object.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types.Object
的用法示例。
在下文中一共展示了Object.Name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: 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)))
}
}
示例4: formatMember
func formatMember(obj types.Object, maxname int) string {
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.Pkg(), obj.Type()), obj.Val().String())
case *types.Func:
fmt.Fprintf(&buf, " %s", types.TypeString(obj.Pkg(), obj.Type()))
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.Pkg(), obj.Type().Underlying()))
} else {
fmt.Fprintf(&buf, " %s", abbrev)
}
case *types.Var:
fmt.Fprintf(&buf, " %s", types.TypeString(obj.Pkg(), obj.Type()))
}
return buf.String()
}
示例5: 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())
}
}
示例6: 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
}
示例7: addSpilledParam
// addSpilledParam declares a parameter that is pre-spilled to the
// stack; the function body will load/store the spilled location.
// Subsequent lifting will eliminate spills where possible.
//
func (f *Function) addSpilledParam(obj types.Object) {
param := f.addParamObj(obj)
spill := &Alloc{Comment: obj.Name()}
spill.setType(types.NewPointer(obj.Type()))
spill.setPos(obj.Pos())
f.objects[obj] = spill
f.Locals = append(f.Locals, spill)
f.emit(spill)
f.emit(&Store{Addr: spill, Val: param})
}
示例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))
}
}
示例9: 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
}
示例10: 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
}
示例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: obj
func (p *exporter) obj(obj types.Object) {
if trace {
p.tracef("object %s {\n", obj.Name())
defer p.tracef("}\n")
}
switch obj := obj.(type) {
case *types.Const:
p.int(constTag)
p.string(obj.Name())
p.typ(obj.Type())
p.value(obj.Val())
case *types.TypeName:
p.int(typeTag)
// name is written by corresponding named type
p.typ(obj.Type().(*types.Named))
case *types.Var:
p.int(varTag)
p.string(obj.Name())
p.typ(obj.Type())
case *types.Func:
p.int(funcTag)
p.string(obj.Name())
p.typ(obj.Type())
default:
panic(fmt.Sprintf("unexpected object type %T", obj))
}
}
示例13: 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)
}
}
示例14: 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
}
示例15: 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(),
}
}