本文整理汇总了Golang中golang.org/x/tools/go/types.Object.Type方法的典型用法代码示例。如果您正苦于以下问题:Golang Object.Type方法的具体用法?Golang Object.Type怎么用?Golang Object.Type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types.Object
的用法示例。
在下文中一共展示了Object.Type方法的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: 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
}
示例3: 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})
}
示例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: 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))
}
}
示例6: printObj
func (p *printer) printObj(obj types.Object) {
p.print(obj.Name())
typ, basic := obj.Type().Underlying().(*types.Basic)
if basic && typ.Info()&types.IsUntyped != 0 {
// don't write untyped types
} else {
p.print(" ")
p.writeType(p.pkg, obj.Type())
}
if obj, ok := obj.(*types.Const); ok {
floatFmt := basic && typ.Info()&(types.IsFloat|types.IsComplex) != 0
p.print(" = ")
p.print(valString(obj.Val(), floatFmt))
}
}
示例7: newFuncFrom
func newFuncFrom(p *Package, parent string, obj types.Object, sig *types.Signature) (Func, error) {
haserr := false
res := sig.Results()
var ret types.Type
switch res.Len() {
case 2:
if !isErrorType(res.At(1).Type()) {
return Func{}, fmt.Errorf(
"bind: second result value must be of type error: %s",
obj,
)
}
haserr = true
ret = res.At(0).Type()
case 1:
if isErrorType(res.At(0).Type()) {
haserr = true
ret = nil
} else {
ret = res.At(0).Type()
}
case 0:
ret = nil
default:
return Func{}, fmt.Errorf("bind: too many results to return: %v", obj)
}
id := obj.Pkg().Name() + "_" + obj.Name()
if parent != "" {
id = obj.Pkg().Name() + "_" + parent + "_" + obj.Name()
}
return Func{
pkg: p,
sig: newSignatureFrom(p, sig),
typ: obj.Type(),
name: obj.Name(),
id: id,
doc: p.getDoc(parent, obj),
ret: ret,
err: haserr,
}, nil
}
示例8: 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."))
}
示例9: makeDefInfo
func (g *Grapher) makeDefInfo(obj types.Object) (*DefKey, *defInfo, error) {
switch obj := obj.(type) {
case *types.Builtin:
return &DefKey{"builtin", []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
case *types.Nil:
return &DefKey{"builtin", []string{"nil"}}, &defInfo{pkgscope: false, exported: true}, nil
case *types.TypeName:
if basic, ok := obj.Type().(*types.Basic); ok {
return &DefKey{"builtin", []string{basic.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
}
if obj.Name() == "error" {
return &DefKey{"builtin", []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
}
case *types.PkgName:
return &DefKey{obj.Imported().Path(), []string{}}, &defInfo{pkgscope: false, exported: true}, nil
case *types.Const:
var pkg string
if obj.Pkg() == nil {
pkg = "builtin"
} else {
pkg = obj.Pkg().Path()
}
if obj.Val().Kind() == exact.Bool && pkg == "builtin" {
return &DefKey{pkg, []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
}
}
if obj.Pkg() == nil {
// builtin
return &DefKey{"builtin", []string{obj.Name()}}, &defInfo{pkgscope: false, exported: true}, nil
}
path := g.path(obj)
// Handle the case where a dir has 2 main packages that are not
// intended to be compiled together and have overlapping def
// paths. Prefix the def path with the filename.
if obj.Pkg().Name() == "main" {
p := g.program.Fset.Position(obj.Pos())
path = append([]string{filepath.Base(p.Filename)}, path...)
}
return &DefKey{obj.Pkg().Path(), path}, &defInfo{pkgscope: g.pkgscope[obj], exported: g.exported[obj]}, nil
}
示例10: 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))
}
}
示例11: isExported
func isExported(obj types.Object) bool {
// https://golang.org/ref/spec#Exported_identifiers
// An identifier is exported if both:
// the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
// the identifier is declared in the package block or it is a field name or method name.
// All other identifiers are not exported.
if !obj.Exported() {
// does not start with an upper case letter
return false
}
if v, ok := obj.(*types.Var); ok && v.IsField() {
// is a field name
return true
}
if sig, ok := obj.Type().(*types.Signature); ok && sig.Recv() != nil {
// is a method name
return true
}
// is declared in the package block
return obj.Parent() == obj.Pkg().Scope()
}
示例12: 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())
}
}
示例13: defKind
func defKind(obj types.Object) string {
switch obj := obj.(type) {
case *types.PkgName:
return definfo.Package
case *types.Const:
return definfo.Const
case *types.TypeName:
return definfo.Type
case *types.Var:
if obj.IsField() {
return definfo.Field
}
return definfo.Var
case *types.Func:
sig := obj.Type().(*types.Signature)
if sig.Recv() == nil {
return definfo.Func
} else {
return definfo.Method
}
default:
panic(fmt.Sprintf("unhandled obj type %T", obj))
}
}
示例14: getEnclosingStruct
func getEnclosingStruct(object types.Object) types.Type {
pkgScope := object.Pkg().Scope()
s := pkgScope.Innermost(object.Pos())
if s.Parent() == pkgScope {
s = pkgScope
}
var obj types.Object
for _, name := range s.Names() {
o := s.Lookup(name)
if o.Pos() <= object.Pos() && (obj == nil || o.Pos() > obj.Pos()) {
obj = o
}
}
if obj == nil {
return nil
}
if obj == object {
return obj.Type()
}
t := obj.Type().Underlying().(*types.Struct)
for {
var f types.Object
for i := 0; i < t.NumFields(); i++ {
field := t.Field(i)
if field == object {
return t
}
if field.Pos() <= object.Pos() && (f == nil || field.Pos() > f.Pos()) {
f = field
}
}
if fs, ok := f.Type().(*types.Struct); ok {
t = fs
} else {
break
}
}
return t
}
示例15: addNamedLocal
// addNamedLocal creates a local variable, adds it to function f and
// returns it. Its name and type are taken from obj. Subsequent
// calls to f.lookup(obj) will return the same local.
//
func (f *Function) addNamedLocal(obj types.Object) *Alloc {
l := f.addLocal(obj.Type(), obj.Pos())
l.Comment = obj.Name()
f.objects[obj] = l
return l
}