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 method := range mset.Methodsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Methods().() {
78 function(prog.MethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodValue().(method))
79 }
80 }
81 }
82
83 // Historically, Program.RuntimeTypes used to include the type
84 // of any exported member of a package loaded from syntax that
85 // has a non-parameterized type, plus all types
86 // reachable from that type using reflection, even though
87 // these runtime types may not be required for them.
88 //
89 // Rather than break existing programs that rely on
90 // AllFunctions visiting extra methods that are unreferenced
91 // by IR and unreachable via reflection, we moved the logic
92 // here, unprincipled though it is.
93 // (See doc comment for better ideas.)
94 //
95 // Nonetheless, after the move, we no longer visit every
96 // method of any type recursively reachable from T, only the
97 // methods of T and *T themselves, and we only apply this to
98 // named types T, and not to the type of every exported
99 // package member.
100 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#) {
101 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().()) &&
102 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().()) &&
103 !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().()) {
104 // Consider only named types.
105 // (Ignore aliases and unsafe.Pointer.)
106 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 {
107 if named.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#TypeParams().() == nil {
108 methodsOf(named) // T
109 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
110 }
111 }
112 }
113 }
114
115 for _, pkg := range prog.AllPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#AllPackages().() {
116 for _, mem := range pkg.Membersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Members. {
117 switch mem := mem.(type) {
118 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#:
119 // Visit all package-level declared functions.
120 function(mem)
121
122 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#:
123 exportedTypeHack(mem)
124 }
125 }
126 }
127
128 // Visit all methods of types for which runtime types were
129 // materialized, as they are reachable through reflection.
130 for _, T := range prog.RuntimeTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#RuntimeTypes().() {
131 methodsOf(T)
132 }
133
134 return seen
135 }
136
137 // MainPackages returns the subset of the specified packages
138 // named "main" that define a main function.
139 // The result may include synthetic "testmain" packages.
140 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# {
141 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#
142 for _, pkg := range pkgs {
143 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 {
144 mains = append(mains, pkg)
145 }
146 }
147 return mains
148 }
149
150 // TODO(adonovan): propose a principled API for this. One possibility
151 // is a new field, Package.SrcFunctions []*Function, which would
152 // contain the list of SrcFunctions described in point 2 of the
153 // AllFunctions doc comment, or nil if the package is not from syntax.
154 // But perhaps overloading nil vs empty slice is too subtle.
155 //
156 //go:linkname isSyntactic golang.org/x/tools/go/ssa.isSyntactic
157 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