VarValue returns the SSA Value that corresponds to a specific identifier denoting the source-level named variable obj.

VarValue returns nil if a local variable was not found, perhaps because its package was not built, the debug information was not requested during SSA construction, or the value was optimized away.

ref is the path to an ast.Ident (e.g. from PathEnclosingInterval), and that ident must resolve to obj.

pkg is the package enclosing the reference. (A reference to a var always occurs within a function, so we need to know where to find it.)

If the identifier is a field selector and its base expression is non-addressable, then VarValue returns the value of that field. For example:

func f() struct {x int}
f().x  // VarValue(x) returns a *Field instruction of type int

All other identifiers denote addressable locations (variables). For them, VarValue may return either the variable's address or its value, even when the expression is evaluated only for its value; the situation is reported by isAddr, the second component of the result.

If !isAddr, the returned value is the one associated with the specific identifier. For example,

var x int    // VarValue(x) returns Const 0 here
x = 1        // VarValue(x) returns Const 1 here

It is not specified whether the value or the address is returned in any particular case, as it may depend upon optimizations performed during SSA code generation, such as registerization, constant folding, avoidance of materialization of subexpressions, etc.

VarValue is referenced in 10 repositories