github.com/v2fly/tools@v0.100.0/go/pointer/callgraph.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 pointer
     6  
     7  // This file defines the internal (context-sensitive) call graph.
     8  
     9  import (
    10  	"fmt"
    11  	"go/token"
    12  
    13  	"github.com/v2fly/tools/go/ssa"
    14  )
    15  
    16  type cgnode struct {
    17  	fn         *ssa.Function
    18  	obj        nodeid      // start of this contour's object block
    19  	sites      []*callsite // ordered list of callsites within this function
    20  	callersite *callsite   // where called from, if known; nil for shared contours
    21  }
    22  
    23  // contour returns a description of this node's contour.
    24  func (n *cgnode) contour() string {
    25  	if n.callersite == nil {
    26  		return "shared contour"
    27  	}
    28  	if n.callersite.instr != nil {
    29  		return fmt.Sprintf("as called from %s", n.callersite.instr.Parent())
    30  	}
    31  	return fmt.Sprintf("as called from intrinsic (targets=n%d)", n.callersite.targets)
    32  }
    33  
    34  func (n *cgnode) String() string {
    35  	return fmt.Sprintf("cg%d:%s", n.obj, n.fn)
    36  }
    37  
    38  // A callsite represents a single call site within a cgnode;
    39  // it is implicitly context-sensitive.
    40  // callsites never represent calls to built-ins;
    41  // they are handled as intrinsics.
    42  //
    43  type callsite struct {
    44  	targets nodeid              // pts(ยท) contains objects for dynamically called functions
    45  	instr   ssa.CallInstruction // the call instruction; nil for synthetic/intrinsic
    46  }
    47  
    48  func (c *callsite) String() string {
    49  	if c.instr != nil {
    50  		return c.instr.Common().Description()
    51  	}
    52  	return "synthetic function call"
    53  }
    54  
    55  // pos returns the source position of this callsite, or token.NoPos if implicit.
    56  func (c *callsite) pos() token.Pos {
    57  	if c.instr != nil {
    58  		return c.instr.Pos()
    59  	}
    60  	return token.NoPos
    61  }