You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

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