Debian Codegraph @

go/ssa/ssautil/visit.go

raw symbols SCIP
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 ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/ // import "golang.org/x/tools/go/ssa/ssautil"
6
7 import (
8 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
9 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
10
11 "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/"
12
13 _ "unsafescip-go gomod github.com/golang/go/src go1.25.0 unsafe/" // for linkname hack
14 )
15
16 // This file defines utilities for visiting the SSA representation of
17 // a Program.
18 //
19 // TODO(adonovan): test coverage.
20
21 // AllFunctions finds and returns the set of functions potentially
22 // needed by program prog, as determined by a simple linker-style
23 // reachability algorithm starting from the members and method-sets of
24 // each package. The result may include anonymous functions and
25 // synthetic wrappers.
26 //
27 // Precondition: all packages are built.
28 //
29 // TODO(adonovan): this function is underspecified. It doesn't
30 // actually work like a linker, which computes reachability from main
31 // using something like go/callgraph/cha (without materializing the
32 // call graph). In fact, it treats all public functions and all
33 // methods of public non-parameterized types as roots, even though
34 // they may be unreachable--but only in packages created from syntax.
35 //
36 // I think we should deprecate AllFunctions function in favor of two
37 // clearly defined ones:
38 //
39 // 1. The first would efficiently compute CHA reachability from a set
40 // of main packages, making it suitable for a whole-program
41 // analysis context with InstantiateGenerics, in conjunction with
42 // Program.Build.
43 //
44 // 2. The second would return only the set of functions corresponding
45 // to source Func{Decl,Lit} syntax, like SrcFunctions in
46 // go/analysis/passes/buildssa; this is suitable for
47 // package-at-a-time (or handful of packages) context.
48 // ssa.Package could easily expose it as a field.
49 //
50 // We could add them unexported for now and use them via the linkname hack.
51 func AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Programscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#) map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]bool {
52 seen := make(map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]bool)
53
54 var function func(fn *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#)
55 function = func(fn *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) {
56 if !seen[fn] {
57 seen[fn] = true
58 var buf [10]*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value# // avoid alloc in common case
59 for _, b := range fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. {
60 for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. {
61 for _, op := range instr.Operandsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#Operands.(buf[:0]) {
62 if fn, ok := (*op).(*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#); ok {
63 function(fn)
64 }
65 }
66 }
67 }
68 }
69 }
70
71 // TODO(adonovan): opt: provide a way to share a builder
72 // across a sequence of MethodValue calls.
73
74 methodsOf := func(T typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#) {
75 if !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.IsInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/IsInterface().(T) {
76 mset := prog.MethodSetsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodSets..MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(T)
77 for sel := range mset.Methodsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Methods().() {
78 // Skip generic methods.
79 if sel.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Obj().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#).Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#Signature().().TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#TypeParams().() == nil {
80 function(prog.MethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodValue().(sel))
81 }
82 }
83 }
84 }
85
86 // Historically, Program.RuntimeTypes used to include the type
87 // of any exported member of a package loaded from syntax that
88 // has a non-parameterized type, plus all types
89 // reachable from that type using reflection, even though
90 // these runtime types may not be required for them.
91 //
92 // Rather than break existing programs that rely on
93 // AllFunctions visiting extra methods that are unreferenced
94 // by IR and unreachable via reflection, we moved the logic
95 // here, unprincipled though it is.
96 // (See doc comment for better ideas.)
97 //
98 // Nonetheless, after the move, we no longer visit every
99 // method of any type recursively reachable from T, only the
100 // methods of T and *T themselves, and we only apply this to
101 // named types T, and not to the type of every exported
102 // package member.
103 exportedTypeHack := func(t *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#) {
104 if isSyntacticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/isSyntactic().(t.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Package().()) &&
105 astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IsExportedscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IsExported().(t.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Name().()) &&
106 !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.IsInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/IsInterface().(t.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().()) {
107 // Consider only named types.
108 // (Ignore aliases and unsafe.Pointer.)
109 if named, ok := t.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#); ok {
110 // Skip generic types.
111 if named.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#TypeParams().() == nil {
112 methodsOf(named) // T
113 methodsOf(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(named)) // *T
114 }
115 }
116 }
117 }
118
119 for _, pkg := range prog.AllPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#AllPackages().() {
120 for _, mem := range pkg.Membersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Members. {
121 switch mem := mem.(type) {
122 case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#:
123 // Visit all package-level declared functions.
124 //
125 // (This may include generic functions, which is
126 // inconsistent with the treatment of methods:
127 // we skip both generic methods,
128 // and methods of generic types.)
129 function(mem)
130
131 case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#:
132 exportedTypeHack(mem)
133 }
134 }
135 }
136
137 // Visit all methods of types for which runtime types were
138 // materialized, as they are reachable through reflection.
139 for _, T := range prog.RuntimeTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#RuntimeTypes().() {
140 methodsOf(T)
141 }
142
143 return seen
144 }
145
146 // MainPackages returns the subset of the specified packages
147 // named "main" that define a main function.
148 // The result may include synthetic "testmain" packages.
149 func MainPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/MainPackages().(pkgs []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#) []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package# {
150 var mains []*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#
151 for _, pkg := range pkgs {
152 if pkg.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().() == "main" && pkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("main") != nil {
153 mains = append(mains, pkg)
154 }
155 }
156 return mains
157 }
158
159 // TODO(adonovan): propose a principled API for this. One possibility
160 // is a new field, Package.SrcFunctions []*Function, which would
161 // contain the list of SrcFunctions described in point 2 of the
162 // AllFunctions doc comment, or nil if the package is not from syntax.
163 // But perhaps overloading nil vs empty slice is too subtle.
164 //
165 //go:linkname isSyntactic golang.org/x/tools/go/ssa.isSyntactic
166 func isSyntacticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/isSyntactic().(pkg *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#) bool