@@ -38,10 +38,11 @@ func maxSizes(w io.Writer, topics *Topics) *maxSizeGen {
38
38
39
39
type maxSizeGen struct {
40
40
passes
41
- p printer
42
- state maxSizeState
43
- ctx * Context
44
- topics * Topics
41
+ p printer
42
+ state maxSizeState
43
+ ctx * Context
44
+ topics * Topics
45
+ panicked bool
45
46
}
46
47
47
48
func (s * maxSizeGen ) Method () Method { return MaxSize }
@@ -53,7 +54,7 @@ func (s *maxSizeGen) Apply(dirs []string) error {
53
54
// this lets us chain together addition
54
55
// operations where possible
55
56
func (s * maxSizeGen ) addConstant (sz string ) {
56
- if ! s .p .ok () {
57
+ if ! s .p .ok () || s . panicked {
57
58
return
58
59
}
59
60
@@ -82,6 +83,7 @@ func (s *maxSizeGen) Execute(p Elem) ([]string, error) {
82
83
if ! s .p .ok () {
83
84
return nil , s .p .err
84
85
}
86
+ s .panicked = false
85
87
p = s .applyall (p )
86
88
if p == nil {
87
89
return nil , nil
@@ -109,13 +111,17 @@ func (s *maxSizeGen) Execute(p Elem) ([]string, error) {
109
111
s .p .printf ("\n func %s (s int) {" , getMaxSizeMethod (p .TypeName ()))
110
112
s .state = assignM
111
113
next (s , p )
112
- s .p .nakedReturn ()
114
+ if s .panicked {
115
+ s .p .print ("\n }\n " )
116
+ } else {
117
+ s .p .nakedReturn ()
118
+ }
113
119
s .topics .Add (p .TypeName (), getMaxSizeMethod (p .TypeName ()))
114
120
return nil , s .p .err
115
121
}
116
122
117
123
func (s * maxSizeGen ) gStruct (st * Struct ) {
118
- if ! s .p .ok () {
124
+ if ! s .p .ok () || s . panicked {
119
125
return
120
126
}
121
127
@@ -134,7 +140,7 @@ func (s *maxSizeGen) gStruct(st *Struct) {
134
140
continue
135
141
}
136
142
137
- if ! s .p .ok () {
143
+ if ! s .p .ok () || s . panicked {
138
144
return
139
145
}
140
146
next (s , st .Fields [i ].FieldElem )
@@ -156,19 +162,23 @@ func (s *maxSizeGen) gStruct(st *Struct) {
156
162
}
157
163
158
164
func (s * maxSizeGen ) gPtr (p * Ptr ) {
165
+ if s .panicked {
166
+ return
167
+ }
159
168
s .state = addM // inner must use add
160
169
next (s , p .Value )
161
170
s .state = addM // closing block; reset to add
162
171
}
163
172
164
173
func (s * maxSizeGen ) gSlice (sl * Slice ) {
165
- if ! s .p .ok () {
174
+ if ! s .p .ok () || s . panicked {
166
175
return
167
176
}
168
177
s .state = addM
169
178
s .p .comment ("Calculating size of slice: " + sl .Varname ())
170
179
if (sl .AllocBound () == "" || sl .AllocBound () == "-" ) && (sl .MaxTotalBytes () == "" || sl .MaxTotalBytes () == "-" ) {
171
180
s .p .printf ("\n panic(\" Slice %s is unbounded\" )" , sl .Varname ())
181
+ s .panicked = true
172
182
s .state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
173
183
return
174
184
}
@@ -194,13 +204,14 @@ func (s *maxSizeGen) gSlice(sl *Slice) {
194
204
s .addConstant (fmt .Sprintf ("((%s) * (%s))" , topLevelAllocBound , str ))
195
205
} else {
196
206
s .p .printf ("\n panic(\" Unable to determine max size: %s\" )" , err )
207
+ s .panicked = true
197
208
}
198
209
s .state = addM
199
210
return
200
211
}
201
212
202
213
func (s * maxSizeGen ) gArray (a * Array ) {
203
- if ! s .p .ok () {
214
+ if ! s .p .ok () || s . panicked {
204
215
return
205
216
}
206
217
// If this is not the first line where we define s = ... then we need to reset the state
@@ -216,19 +227,23 @@ func (s *maxSizeGen) gArray(a *Array) {
216
227
s .addConstant (fmt .Sprintf ("((%s) * (%s))" , a .Size , str ))
217
228
} else {
218
229
s .p .printf ("\n panic(\" Unable to determine max size: %s\" )" , err )
219
-
230
+ s . panicked = true
220
231
}
221
232
s .state = addM
222
233
return
223
234
}
224
235
225
236
func (s * maxSizeGen ) gMap (m * Map ) {
237
+ if s .panicked {
238
+ return
239
+ }
226
240
vn := m .Varname ()
227
241
s .state = addM
228
242
s .addConstant (builtinSize (mapHeader ))
229
243
topLevelAllocBound := m .AllocBound ()
230
244
if topLevelAllocBound != "" && topLevelAllocBound == "-" {
231
245
s .p .printf ("\n panic(\" Map %s is unbounded\" )" , m .Varname ())
246
+ s .panicked = true
232
247
s .state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
233
248
return
234
249
}
@@ -241,21 +256,25 @@ func (s *maxSizeGen) gMap(m *Map) {
241
256
}
242
257
}
243
258
244
- s .p .comment ("Adding size of map keys for " + vn )
245
- s .p .printf ("\n s += %s" , topLevelAllocBound )
246
- s .state = multM
247
- next (s , m .Key )
259
+ if ! s .panicked {
260
+ s .p .comment ("Adding size of map keys for " + vn )
261
+ s .p .printf ("\n s += %s" , topLevelAllocBound )
262
+ s .state = multM
263
+ next (s , m .Key )
264
+ }
248
265
249
- s .p .comment ("Adding size of map values for " + vn )
250
- s .p .printf ("\n s += %s" , topLevelAllocBound )
251
- s .state = multM
252
- next (s , m .Value )
266
+ if ! s .panicked {
267
+ s .p .comment ("Adding size of map values for " + vn )
268
+ s .p .printf ("\n s += %s" , topLevelAllocBound )
269
+ s .state = multM
270
+ next (s , m .Value )
271
+ }
253
272
254
273
s .state = addM
255
274
}
256
275
257
276
func (s * maxSizeGen ) gBase (b * BaseElem ) {
258
- if ! s .p .ok () {
277
+ if ! s .p .ok () || s . panicked {
259
278
return
260
279
}
261
280
if b .MaxTotalBytes () != "" {
@@ -276,6 +295,7 @@ func (s *maxSizeGen) gBase(b *BaseElem) {
276
295
value , err := baseMaxSizeExpr (b .Value , vname , b .BaseName (), b .TypeName (), b .common .AllocBound ())
277
296
if err != nil {
278
297
s .p .printf ("\n panic(\" Unable to determine max size: %s\" )" , err )
298
+ s .panicked = true
279
299
s .state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
280
300
return
281
301
}
@@ -290,6 +310,7 @@ func (s *maxSizeGen) gBase(b *BaseElem) {
290
310
value , err := baseMaxSizeExpr (b .Value , vname , b .BaseName (), b .TypeName (), b .common .AllocBound ())
291
311
if err != nil {
292
312
s .p .printf ("\n panic(\" Unable to determine max size: %s\" )" , err )
313
+ s .panicked = true
293
314
s .state = addM // reset the add to prevent further + expressions from being added to the end the panic statement
294
315
return
295
316
}
0 commit comments