Debian Codegraph @

go/ssa/source.go

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