本文整理汇总了Golang中go/types.Object类的典型用法代码示例。如果您正苦于以下问题:Golang Object类的具体用法?Golang Object怎么用?Golang Object使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Object类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: tokenOf
func tokenOf(o types.Object) string {
switch o := o.(type) {
case *types.Func:
return "func"
case *types.Var:
return "var"
case *types.TypeName:
return "type"
case *types.Const:
return "const"
case *types.PkgName:
return "package"
case *types.Builtin:
return "builtin" // e.g. when describing package "unsafe"
case *types.Nil:
return "nil"
case *types.Label:
return "label"
case *types.Alias:
if o.Orig() == nil {
return "alias"
}
return tokenOf(o.Orig())
}
panic(o)
}
示例2: typesObjectString
func typesObjectString(obj types.Object) string {
var prefix string
switch obj.(type) {
case *types.Builtin:
prefix = "builtin"
case *types.Func:
prefix = "func"
case *types.Const:
prefix = "const"
case *types.PkgName:
prefix = "package"
case *types.Var:
prefix = "var"
case *types.Label:
prefix = "label"
case *types.Nil:
return "nil"
case *types.TypeName:
prefix = "type"
default:
panic(fmt.Sprintf("unexpected type: %T", obj))
}
return prefix + " " + obj.Name()
}
示例3: updateGetDefinitionsContext
func updateGetDefinitionsContext(ctx *getDefinitionsContext, def *Definition, ident *ast.Ident, obj types.Object) {
switch obj.(type) {
case *types.Var:
//Processing vars later to be sure that all info about structs already filled
ctx.vars = append(ctx.vars, newObjectWithIdent(obj, ident))
case *types.Func:
//Processing funcs later to be sure that all info about interfaces already filled
ctx.funcs = append(ctx.funcs, newObjectWithIdent(obj, ident))
case *types.TypeName:
//If the underlying type is struct, then filling
//positions of struct's fields (key) and struct name(value)
//to map. Then we can extract struct name for fields when
//will be analyze them.
t := obj.(*types.TypeName)
underlyingType := t.Type().Underlying()
switch underlyingType.(type) {
case *types.Struct:
s := underlyingType.(*types.Struct)
for i := 0; i < s.NumFields(); i++ {
field := s.Field(i)
ctx.structs[posToStr(ctx.fset, field.Pos())] = obj.Name()
}
}
}
//Check for interfaces
underlyingType := obj.Type().Underlying()
switch underlyingType.(type) {
case *types.Interface:
d := new(defWithInterface)
d.def = def
d.interfac = underlyingType.(*types.Interface)
ctx.interfaces = append(ctx.interfaces, d)
}
}
示例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 *renamer) checkSelections(from types.Object) {
for pkg, info := range r.packages {
if id := someUse(info, from); id != nil {
if !r.checkExport(id, pkg, from) {
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(), r.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(from, delta, syntax, obj)
return
}
} else if sel.Obj().Name() == r.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(from, -delta, syntax, sel.Obj())
return
}
}
}
}
}
示例5: 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
}
示例6: 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
}
示例7: appendObject
func (b *candidateCollector) appendObject(obj types.Object) {
// TODO(mdempsky): Change this to true.
const proposeBuiltins = false
if obj.Pkg() != b.localpkg {
if obj.Parent() == types.Universe {
if !proposeBuiltins {
return
}
} else if !obj.Exported() {
return
}
}
// TODO(mdempsky): Reconsider this functionality.
if b.filter != nil && !b.filter(obj) {
return
}
if b.filter != nil || strings.HasPrefix(obj.Name(), b.partial) {
b.exact = append(b.exact, obj)
} else if strings.HasPrefix(strings.ToLower(obj.Name()), strings.ToLower(b.partial)) {
b.badcase = append(b.badcase, obj)
}
}
示例8: 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 escaping {
// Walk up the chain of Captures.
x := v
for {
if c, ok := x.(*Capture); ok {
x = c.Outer
} else {
break
}
}
// By construction, all captures are ultimately Allocs in the
// naive SSA form. Parameters are pre-spilled to the stack.
x.(*Alloc).Heap = true
}
return v // function-local var (address)
}
// Definition must be in an enclosing function;
// plumb it through intervening closures.
if f.Enclosing == nil {
panic("no Value for type.Object " + obj.GetName())
}
v := &Capture{f.Enclosing.lookup(obj, true)} // escaping
f.objects[obj] = v
f.FreeVars = append(f.FreeVars, v)
return v
}
示例9: equalObj
// equalObj reports how x and y differ. They are assumed to belong to
// different universes so cannot be compared directly.
func equalObj(x, y types.Object) error {
if reflect.TypeOf(x) != reflect.TypeOf(y) {
return fmt.Errorf("%T vs %T", x, y)
}
xt := x.Type()
yt := y.Type()
switch x.(type) {
case *types.Var, *types.Func:
// ok
case *types.Const:
xval := x.(*types.Const).Val()
yval := y.(*types.Const).Val()
// Use string comparison for floating-point values since rounding is permitted.
if constant.Compare(xval, token.NEQ, yval) &&
!(xval.Kind() == constant.Float && xval.String() == yval.String()) {
return fmt.Errorf("unequal constants %s vs %s", xval, yval)
}
case *types.TypeName:
xt = xt.Underlying()
yt = yt.Underlying()
default:
return fmt.Errorf("unexpected %T", x)
}
return equalType(xt, yt)
}
示例10: obj
func (p *exporter) obj(obj types.Object) {
switch obj := obj.(type) {
case *types.Const:
p.tag(constTag)
p.pos(obj)
p.qualifiedName(obj)
p.typ(obj.Type())
p.value(obj.Val())
case *types.TypeName:
p.tag(typeTag)
p.typ(obj.Type())
case *types.Var:
p.tag(varTag)
p.pos(obj)
p.qualifiedName(obj)
p.typ(obj.Type())
case *types.Func:
p.tag(funcTag)
p.pos(obj)
p.qualifiedName(obj)
sig := obj.Type().(*types.Signature)
p.paramList(sig.Params(), sig.Variadic())
p.paramList(sig.Results(), false)
default:
log.Fatalf("gcimporter: unexpected object %v (%T)", obj, obj)
}
}
示例11: getFullName
//getFullName is returning unique name of obj.
func getFullName(obj types.Object, ctx *getDefinitionsContext, isType bool) string {
if obj == nil {
return ""
}
if isType {
return obj.Type().String()
}
result := ""
switch obj.(type) {
case *types.Func:
f := obj.(*types.Func)
r := strings.NewReplacer("(", "", "*", "", ")", "")
result = r.Replace(f.FullName())
default:
if obj.Pkg() != nil {
result += obj.Pkg().Path()
result += "."
}
if packageName, ok := ctx.structs[posToStr(ctx.fset, obj.Pos())]; ok {
result += packageName
result += "."
}
result += obj.Name()
}
return result
}
示例12: qualifiedName
func (p *exporter) qualifiedName(obj types.Object) {
if obj == nil {
p.string("")
return
}
p.string(obj.Name())
p.pkg(obj.Pkg(), false)
}
示例13: fileLine
func (p *exporter) fileLine(obj types.Object) (file string, line int) {
if p.fset != nil {
pos := p.fset.Position(obj.Pos())
file = pos.Filename
line = pos.Line
}
return
}
示例14: 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
}
示例15: 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())
}
}