Debian Codegraph @

btf/traversal.go

raw symbols SCIP
1 package btf
2
3 import (
4 "fmt"
5 "iter"
6 )
7
8 // Functions to traverse a cyclic graph of types. The below was very useful:
9 // https://eli.thegreenplace.net/2015/directed-graph-traversal-orderings-and-applications-to-data-flow-analysis/#post-order-and-reverse-post-order
10
11 // postorder yields all types reachable from root in post order.
12 func postorder(root Type, visited map[Type]struct{}) iter.Seq[Type] {
13 return func(yield func(Type) bool) {
14 visitInPostorder(root, visited, yield)
15 }
16 }
17
18 // visitInPostorder is a separate function to avoid arguments escaping
19 // to the heap. Don't change the setup without re-running the benchmarks.
20 func visitInPostorder(root Type, visited map[Type]struct{}, yield func(typ Type) bool) bool {
21 if _, ok := visited[root]; ok {
22 return true
23 }
24 if visited == nil {
25 visited = make(map[Type]struct{})
26 }
27 visited[root] = struct{}{}
28
29 for child := range children(root) {
30 if !visitInPostorder(*child, visited, yield) {
31 return false
32 }
33 }
34
35 return yield(root)
36 }
37
38 // children yields all direct descendants of typ.
39 func children(typ Type) iter.Seq[*Type] {
40 return func(yield func(*Type) bool) {
41 // Explicitly type switch on the most common types to allow the inliner to
42 // do its work. This avoids allocating intermediate slices from walk() on
43 // the heap.
44 var tags []string
45 switch v := typ.(type) {
46 case *Void, *Int, *Enum, *Fwd, *Float, *declTag:
47 // No children to traverse.
48 // declTags is declared as a leaf type since it's parsed into .Tags fields of other types
49 // during unmarshaling.
50 case *Pointer:
51 if !yield(&v.Target) {
52 return
53 }
54 case *Array:
55 if !yield(&v.Index) {
56 return
57 }
58 if !yield(&v.Type) {
59 return
60 }
61 case *Struct:
62 for i := range v.Members {
63 if !yield(&v.Members[i].Type) {
64 return
65 }
66 for _, t := range v.Members[i].Tags {
67 var tag Type = &declTag{v, t, i}
68 if !yield(&tag) {
69 return
70 }
71 }
72 }
73 tags = v.Tags
74 case *Union:
75 for i := range v.Members {
76 if !yield(&v.Members[i].Type) {
77 return
78 }
79 for _, t := range v.Members[i].Tags {
80 var tag Type = &declTag{v, t, i}
81 if !yield(&tag) {
82 return
83 }
84 }
85 }
86 tags = v.Tags
87 case *Typedef:
88 if !yield(&v.Type) {
89 return
90 }
91 tags = v.Tags
92 case *Volatile:
93 if !yield(&v.Type) {
94 return
95 }
96 case *Const:
97 if !yield(&v.Type) {
98 return
99 }
100 case *Restrict:
101 if !yield(&v.Type) {
102 return
103 }
104 case *Func:
105 if !yield(&v.Type) {
106 return
107 }
108 if fp, ok := v.Type.(*FuncProto); ok {
109 for i := range fp.Params {
110 if len(v.ParamTags) <= i {
111 continue
112 }
113 for _, t := range v.ParamTags[i] {
114 var tag Type = &declTag{v, t, i}
115 if !yield(&tag) {
116 return
117 }
118 }
119 }
120 }
121 tags = v.Tags
122 case *FuncProto:
123 if !yield(&v.Return) {
124 return
125 }
126 for i := range v.Params {
127 if !yield(&v.Params[i].Type) {
128 return
129 }
130 }
131 case *Var:
132 if !yield(&v.Type) {
133 return
134 }
135 tags = v.Tags
136 case *Datasec:
137 for i := range v.Vars {
138 if !yield(&v.Vars[i].Type) {
139 return
140 }
141 }
142 case *TypeTag:
143 if !yield(&v.Type) {
144 return
145 }
146 case *cycle:
147 // cycle has children, but we ignore them deliberately.
148 default:
149 panic(fmt.Sprintf("don't know how to walk Type %T", v))
150 }
151
152 for _, t := range tags {
153 var tag Type = &declTag{typ, t, -1}
154 if !yield(&tag) {
155 return
156 }
157 }
158 }
159 }