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 }
// 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 }
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))) } }
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() }
// 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()) } }
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 }
// 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}) }
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)) } }
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 }
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 }
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) } } } } }
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)) } }
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) } }
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 }
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(), } }
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)) } }
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 }
// getDoc returns the doc string associated with types.Object func (p *Package) getDoc(o types.Object) string { n := o.Name() switch o.(type) { case *types.Const: for _, c := range p.doc.Consts { for _, cn := range c.Names { if n == cn { return c.Doc } } } case *types.Var: for _, v := range p.doc.Vars { for _, vn := range v.Names { if n == vn { return v.Doc } } } case *types.Func: for _, f := range p.doc.Funcs { if n == f.Name { return f.Doc } } case *types.TypeName: for _, t := range p.doc.Types { if n == t.Name { return t.Doc } } default: // TODO(sbinet) panic(fmt.Errorf("not yet supported: %v (%T)", o, o)) } return "" }
func (c *funcContext) objectName(o types.Object) string { if isPkgLevel(o) { c.p.dependencies[o] = true if o.Pkg() != c.p.Pkg || (isVarOrConst(o) && o.Exported()) { return c.pkgVar(o.Pkg()) + "." + o.Name() } } name, ok := c.p.objectNames[o] if !ok { name = c.newVariableWithLevel(o.Name(), isPkgLevel(o)) c.p.objectNames[o] = name } if v, ok := o.(*types.Var); ok && c.p.escapingVars[v] { return name + "[0]" } return name }
// ssaValueForIdent returns the ssa.Value for the ast.Ident whose path // to the root of the AST is path. isAddr reports whether the // ssa.Value is the address denoted by the ast.Ident, not its value. // func ssaValueForIdent(prog *ssa.Program, qinfo *loader.PackageInfo, obj types.Object, path []ast.Node) (value ssa.Value, isAddr bool, err error) { switch obj := obj.(type) { case *types.Var: pkg := prog.Package(qinfo.Pkg) pkg.Build() if v, addr := prog.VarValue(obj, pkg, path); v != nil { return v, addr, nil } return nil, false, fmt.Errorf("can't locate SSA Value for var %s", obj.Name()) case *types.Func: fn := prog.FuncValue(obj) if fn == nil { return nil, false, fmt.Errorf("%s is an interface method", obj) } // TODO(adonovan): there's no point running PTA on a *Func ident. // Eliminate this feature. return fn, false, nil } panic(obj) }
// 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) } }
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)) } }
func checkNilFuncComparison(f *File, node ast.Node) { e := node.(*ast.BinaryExpr) // Only want == or != comparisons. if e.Op != token.EQL && e.Op != token.NEQ { return } // Only want comparisons with a nil identifier on one side. var e2 ast.Expr switch { case f.isNil(e.X): e2 = e.Y case f.isNil(e.Y): e2 = e.X default: return } // Only want identifiers or selector expressions. var obj types.Object switch v := e2.(type) { case *ast.Ident: obj = f.pkg.uses[v] case *ast.SelectorExpr: obj = f.pkg.uses[v.Sel] default: return } // Only want functions. if _, ok := obj.(*types.Func); !ok { return } f.Badf(e.Pos(), "comparison of function %v %v nil is always %v", obj.Name(), e.Op, e.Op == token.NEQ) }
// 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) } }
func (g *Grapher) path(obj types.Object) (path []string) { if path, present := g.paths[obj]; present { return path } var scope *types.Scope pkgInfo, astPath, _ := g.program.PathEnclosingInterval(obj.Pos(), obj.Pos()) if astPath != nil { for _, node := range astPath { if s, hasScope := pkgInfo.Scopes[node]; hasScope { scope = s } } } if scope == nil { scope = obj.Parent() } if scope == nil { // TODO(sqs): make this actually handle cases like the one described in // https://github.com/sourcegraph/sourcegraph.com/issues/218 log.Printf("Warning: no scope for object %s at pos %s", obj.String(), g.program.Fset.Position(obj.Pos())) return nil } prefix, hasPath := g.scopePaths[scope] if !hasPath { panic("no scope path for scope " + scope.String()) } path = append([]string{}, prefix...) p := g.program.Fset.Position(obj.Pos()) path = append(path, obj.Name()+uniqID(p)) return path panic("no scope node for object " + obj.String()) }
// checkSelection checks that all uses and selections that resolve to // the specified object would continue to do so after the renaming. func (e *Export) checkSelections(from types.Object, to string) { info := e.u.pkgInfo if id := someUse(info, from); id != nil { e.Conflicting = true return } for _, sel := range info.Selections { if sel.Obj() == from { if obj, indices, _ := types.LookupFieldOrMethod(sel.Recv(), true, 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 } e.Conflicting = true return } } else if sel.Obj().Name() == to { if obj, indices, _ := types.LookupFieldOrMethod(sel.Recv(), true, 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 } e.Conflicting = true return } } } }
func wholePath(obj types.Object, path string, prog *loader.Program) string { if v, ok := obj.(*types.Var); ok && v.IsField() { structName := getDeclareStructOrInterface(prog, v) return fmt.Sprintf("(\"%s\".%s).%s", path, structName, obj.Name()) } else if f, ok := obj.(*types.Func); ok { if r := recv(f); r != nil { return fmt.Sprintf("(\"%s\".%s).%s", r.Pkg().Path(), typeName(r.Type()), obj.Name()) } } return fmt.Sprintf("\"%s\".%s", path, obj.Name()) }
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 }
func isAccessibleFrom(obj types.Object, pkg *types.Package) bool { return ast.IsExported(obj.Name()) || obj.Pkg() == pkg }
func isPackageLevel(obj types.Object) bool { return obj.Pkg().Scope().Lookup(obj.Name()) == obj }