github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/go/callgraph/cha/cha.go (about) 1 // Copyright 2014 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 cha computes the call graph of a Go program using the Class 8 // Hierarchy Analysis (CHA) algorithm. 9 // 10 // CHA was first described in "Optimization of Object-Oriented Programs 11 // Using Static Class Hierarchy Analysis", Jeffrey Dean, David Grove, 12 // and Craig Chambers, ECOOP'95. 13 // 14 // CHA is related to RTA (see go/callgraph/rta); the difference is that 15 // CHA conservatively computes the entire "implements" relation between 16 // interfaces and concrete types ahead of time, whereas RTA uses dynamic 17 // programming to construct it on the fly as it encounters new functions 18 // reachable from main. CHA may thus include spurious call edges for 19 // types that haven't been instantiated yet, or types that are never 20 // instantiated. 21 // 22 // Since CHA conservatively assumes that all functions are address-taken 23 // and all concrete types are put into interfaces, it is sound to run on 24 // partial programs, such as libraries without a main or test function. 25 // 26 package cha // import "golang.org/x/tools/go/callgraph/cha" 27 28 import ( 29 "go/types" 30 31 "golang.org/x/tools/go/callgraph" 32 "golang.org/x/tools/go/ssa" 33 "golang.org/x/tools/go/ssa/ssautil" 34 "golang.org/x/tools/go/types/typeutil" 35 ) 36 37 // CallGraph computes the call graph of the specified program using the 38 // Class Hierarchy Analysis algorithm. 39 // 40 func CallGraph(prog *ssa.Program) *callgraph.Graph { 41 cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph 42 43 allFuncs := ssautil.AllFunctions(prog) 44 45 // funcsBySig contains all functions, keyed by signature. It is 46 // the effective set of address-taken functions used to resolve 47 // a dynamic call of a particular signature. 48 var funcsBySig typeutil.Map // value is []*ssa.Function 49 50 // methodsByName contains all methods, 51 // grouped by name for efficient lookup. 52 methodsByName := make(map[string][]*ssa.Function) 53 54 // methodsMemo records, for every abstract method call call I.f on 55 // interface type I, the set of concrete methods C.f of all 56 // types C that satisfy interface I. 57 methodsMemo := make(map[*types.Func][]*ssa.Function) 58 lookupMethods := func(m *types.Func) []*ssa.Function { 59 methods, ok := methodsMemo[m] 60 if !ok { 61 I := m.Type().(*types.Signature).Recv().Type().Underlying().(*types.Interface) 62 for _, f := range methodsByName[m.Name()] { 63 C := f.Signature.Recv().Type() // named or *named 64 if types.Implements(C, I) { 65 methods = append(methods, f) 66 } 67 } 68 methodsMemo[m] = methods 69 } 70 return methods 71 } 72 73 for f := range allFuncs { 74 if f.Signature.Recv() == nil { 75 // Package initializers can never be address-taken. 76 if f.Name() == "init" && f.Synthetic == "package initializer" { 77 continue 78 } 79 funcs, _ := funcsBySig.At(f.Signature).([]*ssa.Function) 80 funcs = append(funcs, f) 81 funcsBySig.Set(f.Signature, funcs) 82 } else { 83 methodsByName[f.Name()] = append(methodsByName[f.Name()], f) 84 } 85 } 86 87 addEdge := func(fnode *callgraph.Node, site ssa.CallInstruction, g *ssa.Function) { 88 gnode := cg.CreateNode(g) 89 callgraph.AddEdge(fnode, site, gnode) 90 } 91 92 addEdges := func(fnode *callgraph.Node, site ssa.CallInstruction, callees []*ssa.Function) { 93 // Because every call to a highly polymorphic and 94 // frequently used abstract method such as 95 // (io.Writer).Write is assumed to call every concrete 96 // Write method in the program, the call graph can 97 // contain a lot of duplication. 98 // 99 // TODO(adonovan): opt: consider factoring the callgraph 100 // API so that the Callers component of each edge is a 101 // slice of nodes, not a singleton. 102 for _, g := range callees { 103 addEdge(fnode, site, g) 104 } 105 } 106 107 for f := range allFuncs { 108 fnode := cg.CreateNode(f) 109 for _, b := range f.Blocks { 110 for _, instr := range b.Instrs { 111 if site, ok := instr.(ssa.CallInstruction); ok { 112 call := site.Common() 113 if call.IsInvoke() { 114 addEdges(fnode, site, lookupMethods(call.Method)) 115 } else if g := call.StaticCallee(); g != nil { 116 addEdge(fnode, site, g) 117 } else if _, ok := call.Value.(*ssa.Builtin); !ok { 118 callees, _ := funcsBySig.At(call.Signature()).([]*ssa.Function) 119 addEdges(fnode, site, callees) 120 } 121 } 122 } 123 } 124 } 125 126 return cg 127 }