github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/ssa/source.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ssa 6 7 // This file defines utilities for working with source positions 8 // or source-level named entities ("objects"). 9 10 // TODO(adonovan): test that {Value,Instruction}.Pos() positions match 11 // the originating syntax, as specified. 12 13 import ( 14 "go/ast" 15 "go/token" 16 "go/types" 17 18 "github.com/powerman/golang-tools/internal/typeparams" 19 ) 20 21 // EnclosingFunction returns the function that contains the syntax 22 // node denoted by path. 23 // 24 // Syntax associated with package-level variable specifications is 25 // enclosed by the package's init() function. 26 // 27 // Returns nil if not found; reasons might include: 28 // - the node is not enclosed by any function. 29 // - the node is within an anonymous function (FuncLit) and 30 // its SSA function has not been created yet 31 // (pkg.Build() has not yet been called). 32 // 33 func EnclosingFunction(pkg *Package, path []ast.Node) *Function { 34 // Start with package-level function... 35 fn := findEnclosingPackageLevelFunction(pkg, path) 36 if fn == nil { 37 return nil // not in any function 38 } 39 40 // ...then walk down the nested anonymous functions. 41 n := len(path) 42 outer: 43 for i := range path { 44 if lit, ok := path[n-1-i].(*ast.FuncLit); ok { 45 for _, anon := range fn.AnonFuncs { 46 if anon.Pos() == lit.Type.Func { 47 fn = anon 48 continue outer 49 } 50 } 51 // SSA function not found: 52 // - package not yet built, or maybe 53 // - builder skipped FuncLit in dead block 54 // (in principle; but currently the Builder 55 // generates even dead FuncLits). 56 return nil 57 } 58 } 59 return fn 60 } 61 62 // HasEnclosingFunction returns true if the AST node denoted by path 63 // is contained within the declaration of some function or 64 // package-level variable. 65 // 66 // Unlike EnclosingFunction, the behaviour of this function does not 67 // depend on whether SSA code for pkg has been built, so it can be 68 // used to quickly reject check inputs that will cause 69 // EnclosingFunction to fail, prior to SSA building. 70 // 71 func HasEnclosingFunction(pkg *Package, path []ast.Node) bool { 72 return findEnclosingPackageLevelFunction(pkg, path) != nil 73 } 74 75 // findEnclosingPackageLevelFunction returns the Function 76 // corresponding to the package-level function enclosing path. 77 // 78 func findEnclosingPackageLevelFunction(pkg *Package, path []ast.Node) *Function { 79 if n := len(path); n >= 2 { // [... {Gen,Func}Decl File] 80 switch decl := path[n-2].(type) { 81 case *ast.GenDecl: 82 if decl.Tok == token.VAR && n >= 3 { 83 // Package-level 'var' initializer. 84 return pkg.init 85 } 86 87 case *ast.FuncDecl: 88 if decl.Recv == nil && decl.Name.Name == "init" { 89 // Explicit init() function. 90 for _, b := range pkg.init.Blocks { 91 for _, instr := range b.Instrs { 92 if instr, ok := instr.(*Call); ok { 93 if callee, ok := instr.Call.Value.(*Function); ok && callee.Pkg == pkg && callee.Pos() == decl.Name.NamePos { 94 return callee 95 } 96 } 97 } 98 } 99 // Hack: return non-nil when SSA is not yet 100 // built so that HasEnclosingFunction works. 101 return pkg.init 102 } 103 // Declared function/method. 104 return findNamedFunc(pkg, decl.Name.NamePos) 105 } 106 } 107 return nil // not in any function 108 } 109 110 // findNamedFunc returns the named function whose FuncDecl.Ident is at 111 // position pos. 112 // 113 func findNamedFunc(pkg *Package, pos token.Pos) *Function { 114 // Look at all package members and method sets of named types. 115 // Not very efficient. 116 for _, mem := range pkg.Members { 117 switch mem := mem.(type) { 118 case *Function: 119 if mem.Pos() == pos { 120 return mem 121 } 122 case *Type: 123 mset := pkg.Prog.MethodSets.MethodSet(types.NewPointer(mem.Type())) 124 for i, n := 0, mset.Len(); i < n; i++ { 125 // Don't call Program.Method: avoid creating wrappers. 126 obj := mset.At(i).Obj().(*types.Func) 127 if obj.Pos() == pos { 128 return pkg.objects[obj].(*Function) 129 } 130 } 131 } 132 } 133 return nil 134 } 135 136 // ValueForExpr returns the SSA Value that corresponds to non-constant 137 // expression e. 138 // 139 // It returns nil if no value was found, e.g. 140 // - the expression is not lexically contained within f; 141 // - f was not built with debug information; or 142 // - e is a constant expression. (For efficiency, no debug 143 // information is stored for constants. Use 144 // go/types.Info.Types[e].Value instead.) 145 // - e is a reference to nil or a built-in function. 146 // - the value was optimised away. 147 // 148 // If e is an addressable expression used in an lvalue context, 149 // value is the address denoted by e, and isAddr is true. 150 // 151 // The types of e (or &e, if isAddr) and the result are equal 152 // (modulo "untyped" bools resulting from comparisons). 153 // 154 // (Tip: to find the ssa.Value given a source position, use 155 // astutil.PathEnclosingInterval to locate the ast.Node, then 156 // EnclosingFunction to locate the Function, then ValueForExpr to find 157 // the ssa.Value.) 158 // 159 func (f *Function) ValueForExpr(e ast.Expr) (value Value, isAddr bool) { 160 if f.debugInfo() { // (opt) 161 e = unparen(e) 162 for _, b := range f.Blocks { 163 for _, instr := range b.Instrs { 164 if ref, ok := instr.(*DebugRef); ok { 165 if ref.Expr == e { 166 return ref.X, ref.IsAddr 167 } 168 } 169 } 170 } 171 } 172 return 173 } 174 175 // --- Lookup functions for source-level named entities (types.Objects) --- 176 177 // Package returns the SSA Package corresponding to the specified 178 // type-checker package object. 179 // It returns nil if no such SSA package has been created. 180 // 181 func (prog *Program) Package(obj *types.Package) *Package { 182 return prog.packages[obj] 183 } 184 185 // packageLevelMember returns the package-level member corresponding to 186 // the specified named object, which may be a package-level const 187 // (*NamedConst), var (*Global) or func (*Function) of some package in 188 // prog. It returns nil if the object is not found. 189 // 190 func (prog *Program) packageLevelMember(obj types.Object) Member { 191 if pkg, ok := prog.packages[obj.Pkg()]; ok { 192 return pkg.objects[obj] 193 } 194 return nil 195 } 196 197 // originFunc returns the package-level generic function that is the 198 // origin of obj. If returns nil if the generic function is not found. 199 func (prog *Program) originFunc(obj *types.Func) *Function { 200 return prog.declaredFunc(typeparams.OriginMethod(obj)) 201 } 202 203 // FuncValue returns the concrete Function denoted by the source-level 204 // named function obj, or nil if obj denotes an interface method. 205 // 206 // TODO(adonovan): check the invariant that obj.Type() matches the 207 // result's Signature, both in the params/results and in the receiver. 208 // 209 func (prog *Program) FuncValue(obj *types.Func) *Function { 210 fn, _ := prog.packageLevelMember(obj).(*Function) 211 return fn 212 } 213 214 // ConstValue returns the SSA Value denoted by the source-level named 215 // constant obj. 216 // 217 func (prog *Program) ConstValue(obj *types.Const) *Const { 218 // TODO(adonovan): opt: share (don't reallocate) 219 // Consts for const objects and constant ast.Exprs. 220 221 // Universal constant? {true,false,nil} 222 if obj.Parent() == types.Universe { 223 return NewConst(obj.Val(), obj.Type()) 224 } 225 // Package-level named constant? 226 if v := prog.packageLevelMember(obj); v != nil { 227 return v.(*NamedConst).Value 228 } 229 return NewConst(obj.Val(), obj.Type()) 230 } 231 232 // VarValue returns the SSA Value that corresponds to a specific 233 // identifier denoting the source-level named variable obj. 234 // 235 // VarValue returns nil if a local variable was not found, perhaps 236 // because its package was not built, the debug information was not 237 // requested during SSA construction, or the value was optimized away. 238 // 239 // ref is the path to an ast.Ident (e.g. from PathEnclosingInterval), 240 // and that ident must resolve to obj. 241 // 242 // pkg is the package enclosing the reference. (A reference to a var 243 // always occurs within a function, so we need to know where to find it.) 244 // 245 // If the identifier is a field selector and its base expression is 246 // non-addressable, then VarValue returns the value of that field. 247 // For example: 248 // func f() struct {x int} 249 // f().x // VarValue(x) returns a *Field instruction of type int 250 // 251 // All other identifiers denote addressable locations (variables). 252 // For them, VarValue may return either the variable's address or its 253 // value, even when the expression is evaluated only for its value; the 254 // situation is reported by isAddr, the second component of the result. 255 // 256 // If !isAddr, the returned value is the one associated with the 257 // specific identifier. For example, 258 // var x int // VarValue(x) returns Const 0 here 259 // x = 1 // VarValue(x) returns Const 1 here 260 // 261 // It is not specified whether the value or the address is returned in 262 // any particular case, as it may depend upon optimizations performed 263 // during SSA code generation, such as registerization, constant 264 // folding, avoidance of materialization of subexpressions, etc. 265 // 266 func (prog *Program) VarValue(obj *types.Var, pkg *Package, ref []ast.Node) (value Value, isAddr bool) { 267 // All references to a var are local to some function, possibly init. 268 fn := EnclosingFunction(pkg, ref) 269 if fn == nil { 270 return // e.g. def of struct field; SSA not built? 271 } 272 273 id := ref[0].(*ast.Ident) 274 275 // Defining ident of a parameter? 276 if id.Pos() == obj.Pos() { 277 for _, param := range fn.Params { 278 if param.Object() == obj { 279 return param, false 280 } 281 } 282 } 283 284 // Other ident? 285 for _, b := range fn.Blocks { 286 for _, instr := range b.Instrs { 287 if dr, ok := instr.(*DebugRef); ok { 288 if dr.Pos() == id.Pos() { 289 return dr.X, dr.IsAddr 290 } 291 } 292 } 293 } 294 295 // Defining ident of package-level var? 296 if v := prog.packageLevelMember(obj); v != nil { 297 return v.(*Global), true 298 } 299 300 return // e.g. debug info not requested, or var optimized away 301 }