| 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 | "golang.org/x/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 | type callsite struct { |
| 43 | targets nodeid // pts(ยท) contains objects for dynamically called functions |
| 44 | instr ssa.CallInstruction // the call instruction; nil for synthetic/intrinsic |
| 45 | } |
| 46 | |
| 47 | func (c *callsite) String() string { |
| 48 | if c.instr != nil { |
| 49 | return c.instr.Common().Description() |
| 50 | } |
| 51 | return "synthetic function call" |
| 52 | } |
| 53 | |
| 54 | // pos returns the source position of this callsite, or token.NoPos if implicit. |
| 55 | func (c *callsite) pos() token.Pos { |
| 56 | if c.instr != nil { |
| 57 | return c.instr.Pos() |
| 58 | } |
| 59 | return token.NoPos |
| 60 | } |
| 61 |
Members