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