本文整理汇总了Golang中golang.org/x/tools/go/types.Object.Pos方法的典型用法代码示例。如果您正苦于以下问题:Golang Object.Pos方法的具体用法?Golang Object.Pos怎么用?Golang Object.Pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types.Object
的用法示例。
在下文中一共展示了Object.Pos方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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())
}
}
示例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: 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})
}
示例5: 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
}
示例6: 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)))
}
}
示例7: 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
}
示例8: 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
}
示例9: 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
}
示例10: 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...]
}
}
示例11: checkInPackageBlock
// checkInPackageBlock performs safety checks for renames of
// func/var/const/type objects in the package block.
func (r *renamer) checkInPackageBlock(from types.Object) {
// Check that there are no references to the name from another
// package if the renaming would make it unexported.
if ast.IsExported(from.Name()) && !ast.IsExported(r.to) {
for pkg, info := range r.packages {
if pkg == from.Pkg() {
continue
}
if id := someUse(info, from); id != nil &&
!r.checkExport(id, pkg, from) {
break
}
}
}
info := r.packages[from.Pkg()]
// Check that in the package block, "init" is a function, and never referenced.
if r.to == "init" {
kind := objectKind(from)
if kind == "func" {
// Reject if intra-package references to it exist.
for id, obj := range info.Uses {
if obj == from {
r.errorf(from.Pos(),
"renaming this func %q to %q would make it a package initializer",
from.Name(), r.to)
r.errorf(id.Pos(), "\tbut references to it exist")
break
}
}
} else {
r.errorf(from.Pos(), "you cannot have a %s at package level named %q",
kind, r.to)
}
}
// Check for conflicts between package block and all file blocks.
for _, f := range info.Files {
fileScope := info.Info.Scopes[f]
b, prev := fileScope.LookupParent(r.to, token.NoPos)
if b == fileScope {
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict",
objectKind(from), from.Name(), r.to)
r.errorf(prev.Pos(), "\twith this %s",
objectKind(prev))
return // since checkInPackageBlock would report redundant errors
}
}
// Check for conflicts in lexical scope.
if from.Exported() {
for _, info := range r.packages {
r.checkInLexicalScope(from, info)
}
} else {
r.checkInLexicalScope(from, info)
}
}
示例12: checkInPackageBlock
// checkInPackageBlock performs safety checks for renames of
// func/var/const/type objects in the package block.
func (r *Unexporter) checkInPackageBlock(objsToUpdate map[types.Object]string, from types.Object, to string) {
// Check that there are no references to the name from another
// package if the renaming would make it unexported.
if ast.IsExported(from.Name()) && !ast.IsExported(to) {
for pkg, info := range r.packages {
if pkg == from.Pkg() {
continue
}
if id := someUse(info, from); id != nil &&
!r.checkExport(id, pkg, from, to) {
break
}
}
}
info := r.packages[from.Pkg()]
lexinfo := r.lexInfo(info)
// Check that in the package block, "init" is a function, and never referenced.
if to == "init" {
kind := objectKind(from)
if kind == "func" {
// Reject if intra-package references to it exist.
if refs := lexinfo.Refs[from]; len(refs) > 0 {
r.warn(from,
r.errorf(from.Pos(),
"renaming this func %q to %q would make it a package initializer",
from.Name(), to),
r.errorf(refs[0].Id.Pos(), "\tbut references to it exist"))
}
} else {
r.warn(from, r.errorf(from.Pos(), "you cannot have a %s at package level named %q",
kind, to))
}
}
// Check for conflicts between package block and all file blocks.
for _, f := range info.Files {
if prev, b := lexinfo.Blocks[f].Lookup(to); b == lexinfo.Blocks[f] {
r.warn(from,
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict",
objectKind(from), from.Name(), to),
r.errorf(prev.Pos(), "\twith this %s",
objectKind(prev)))
return // since checkInPackageBlock would report redundant errors
}
}
// Check for conflicts in lexical scope.
if from.Exported() {
for _, info := range r.packages {
r.checkInLexicalScope(objsToUpdate, from, to, info)
}
} else {
r.checkInLexicalScope(objsToUpdate, from, to, info)
}
}
示例13: check
// check performs safety checks of the renaming of the 'from' object to r.to.
func (r *renamer) check(from types.Object) {
if r.objsToUpdate[from] {
return
}
r.objsToUpdate[from] = true
// NB: order of conditions is important.
if from_, ok := from.(*types.PkgName); ok {
r.checkInFileBlock(from_)
} else if from_, ok := from.(*types.Label); ok {
r.checkLabel(from_)
} else if isPackageLevel(from) {
r.checkInPackageBlock(from)
} else if v, ok := from.(*types.Var); ok && v.IsField() {
r.checkStructField(v)
} else if f, ok := from.(*types.Func); ok && recv(f) != nil {
r.checkMethod(f)
} else if isLocal(from) {
r.checkInLocalScope(from)
} else {
r.errorf(from.Pos(), "unexpected %s object %q (please report a bug)\n",
objectKind(from), from)
}
}
示例14: check
func (r *Unexporter) check(objsToUpdate map[types.Object]string, from types.Object, to string) {
if _, ok := objsToUpdate[from]; ok {
return
}
objsToUpdate[from] = to
// NB: order of conditions is important.
if from_, ok := from.(*types.PkgName); ok {
r.checkInFileBlock(objsToUpdate, from_, to)
} else if from_, ok := from.(*types.Label); ok {
r.checkLabel(from_, to)
} else if isPackageLevel(from) {
r.checkInPackageBlock(objsToUpdate, from, to)
} else if v, ok := from.(*types.Var); ok && v.IsField() {
r.checkStructField(objsToUpdate, v, to)
} else if f, ok := from.(*types.Func); ok && recv(f) != nil {
r.checkMethod(objsToUpdate, f, to)
} else if isLocal(from) {
r.checkInLocalScope(objsToUpdate, from, to)
} else {
r.errorf(from.Pos(), "unexpected %s object %q (please report a bug)\n",
objectKind(from), from)
}
}
示例15: selectionConflict
func (r *renamer) selectionConflict(from types.Object, delta int, syntax *ast.SelectorExpr, obj types.Object) {
r.errorf(from.Pos(), "renaming this %s %q to %q",
objectKind(from), from.Name(), r.to)
switch {
case delta < 0:
// analogous to sub-block conflict
r.errorf(syntax.Sel.Pos(),
"\twould change the referent of this selection")
r.errorf(obj.Pos(), "\tto this %s", objectKind(obj))
case delta == 0:
// analogous to same-block conflict
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.errorf(syntax.Sel.Pos(),
"\twould shadow this selection")
r.errorf(obj.Pos(), "\tto the %s declared here",
objectKind(obj))
}
}