-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastcast.go
590 lines (539 loc) · 18.5 KB
/
astcast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Code generated by astcast_generate.go; DO NOT EDIT
// Package astcast wraps type assertion operations in such way that you don't have
// to worry about nil pointer results anymore.
package astcast
import (
"go/ast"
)
// A set of sentinel nil-like values that are returned
// by all "casting" functions in case of failed type assertion.
var (
NilArrayType = &ast.ArrayType{}
NilBadExpr = &ast.BadExpr{}
NilBasicLit = &ast.BasicLit{}
NilBinaryExpr = &ast.BinaryExpr{}
NilCallExpr = &ast.CallExpr{}
NilChanType = &ast.ChanType{}
NilCompositeLit = &ast.CompositeLit{}
NilEllipsis = &ast.Ellipsis{}
NilFuncLit = &ast.FuncLit{}
NilFuncType = &ast.FuncType{}
NilIdent = &ast.Ident{}
NilIndexExpr = &ast.IndexExpr{}
NilInterfaceType = &ast.InterfaceType{}
NilKeyValueExpr = &ast.KeyValueExpr{}
NilMapType = &ast.MapType{}
NilParenExpr = &ast.ParenExpr{}
NilSelectorExpr = &ast.SelectorExpr{}
NilSliceExpr = &ast.SliceExpr{}
NilStarExpr = &ast.StarExpr{}
NilStructType = &ast.StructType{}
NilTypeAssertExpr = &ast.TypeAssertExpr{}
NilUnaryExpr = &ast.UnaryExpr{}
NilAssignStmt = &ast.AssignStmt{}
NilBadStmt = &ast.BadStmt{}
NilBlockStmt = &ast.BlockStmt{}
NilBranchStmt = &ast.BranchStmt{}
NilCaseClause = &ast.CaseClause{}
NilCommClause = &ast.CommClause{}
NilDeclStmt = &ast.DeclStmt{}
NilDeferStmt = &ast.DeferStmt{}
NilEmptyStmt = &ast.EmptyStmt{}
NilExprStmt = &ast.ExprStmt{}
NilForStmt = &ast.ForStmt{}
NilGoStmt = &ast.GoStmt{}
NilIfStmt = &ast.IfStmt{}
NilIncDecStmt = &ast.IncDecStmt{}
NilLabeledStmt = &ast.LabeledStmt{}
NilRangeStmt = &ast.RangeStmt{}
NilReturnStmt = &ast.ReturnStmt{}
NilSelectStmt = &ast.SelectStmt{}
NilSendStmt = &ast.SendStmt{}
NilSwitchStmt = &ast.SwitchStmt{}
NilTypeSwitchStmt = &ast.TypeSwitchStmt{}
NilComment = &ast.Comment{}
NilCommentGroup = &ast.CommentGroup{}
NilFieldList = &ast.FieldList{}
NilFile = &ast.File{}
NilPackage = &ast.Package{}
)
// ToArrayType returns x as a non-nil *ast.ArrayType.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilArrayType.
func ToArrayType(x ast.Node) *ast.ArrayType {
if x, ok := x.(*ast.ArrayType); ok {
return x
}
return NilArrayType
}
// ToBadExpr returns x as a non-nil *ast.BadExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilBadExpr.
func ToBadExpr(x ast.Node) *ast.BadExpr {
if x, ok := x.(*ast.BadExpr); ok {
return x
}
return NilBadExpr
}
// ToBasicLit returns x as a non-nil *ast.BasicLit.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilBasicLit.
func ToBasicLit(x ast.Node) *ast.BasicLit {
if x, ok := x.(*ast.BasicLit); ok {
return x
}
return NilBasicLit
}
// ToBinaryExpr returns x as a non-nil *ast.BinaryExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilBinaryExpr.
func ToBinaryExpr(x ast.Node) *ast.BinaryExpr {
if x, ok := x.(*ast.BinaryExpr); ok {
return x
}
return NilBinaryExpr
}
// ToCallExpr returns x as a non-nil *ast.CallExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilCallExpr.
func ToCallExpr(x ast.Node) *ast.CallExpr {
if x, ok := x.(*ast.CallExpr); ok {
return x
}
return NilCallExpr
}
// ToChanType returns x as a non-nil *ast.ChanType.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilChanType.
func ToChanType(x ast.Node) *ast.ChanType {
if x, ok := x.(*ast.ChanType); ok {
return x
}
return NilChanType
}
// ToCompositeLit returns x as a non-nil *ast.CompositeLit.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilCompositeLit.
func ToCompositeLit(x ast.Node) *ast.CompositeLit {
if x, ok := x.(*ast.CompositeLit); ok {
return x
}
return NilCompositeLit
}
// ToEllipsis returns x as a non-nil *ast.Ellipsis.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilEllipsis.
func ToEllipsis(x ast.Node) *ast.Ellipsis {
if x, ok := x.(*ast.Ellipsis); ok {
return x
}
return NilEllipsis
}
// ToFuncLit returns x as a non-nil *ast.FuncLit.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilFuncLit.
func ToFuncLit(x ast.Node) *ast.FuncLit {
if x, ok := x.(*ast.FuncLit); ok {
return x
}
return NilFuncLit
}
// ToFuncType returns x as a non-nil *ast.FuncType.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilFuncType.
func ToFuncType(x ast.Node) *ast.FuncType {
if x, ok := x.(*ast.FuncType); ok {
return x
}
return NilFuncType
}
// ToIdent returns x as a non-nil *ast.Ident.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilIdent.
func ToIdent(x ast.Node) *ast.Ident {
if x, ok := x.(*ast.Ident); ok {
return x
}
return NilIdent
}
// ToIndexExpr returns x as a non-nil *ast.IndexExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilIndexExpr.
func ToIndexExpr(x ast.Node) *ast.IndexExpr {
if x, ok := x.(*ast.IndexExpr); ok {
return x
}
return NilIndexExpr
}
// ToInterfaceType returns x as a non-nil *ast.InterfaceType.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilInterfaceType.
func ToInterfaceType(x ast.Node) *ast.InterfaceType {
if x, ok := x.(*ast.InterfaceType); ok {
return x
}
return NilInterfaceType
}
// ToKeyValueExpr returns x as a non-nil *ast.KeyValueExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilKeyValueExpr.
func ToKeyValueExpr(x ast.Node) *ast.KeyValueExpr {
if x, ok := x.(*ast.KeyValueExpr); ok {
return x
}
return NilKeyValueExpr
}
// ToMapType returns x as a non-nil *ast.MapType.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilMapType.
func ToMapType(x ast.Node) *ast.MapType {
if x, ok := x.(*ast.MapType); ok {
return x
}
return NilMapType
}
// ToParenExpr returns x as a non-nil *ast.ParenExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilParenExpr.
func ToParenExpr(x ast.Node) *ast.ParenExpr {
if x, ok := x.(*ast.ParenExpr); ok {
return x
}
return NilParenExpr
}
// ToSelectorExpr returns x as a non-nil *ast.SelectorExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilSelectorExpr.
func ToSelectorExpr(x ast.Node) *ast.SelectorExpr {
if x, ok := x.(*ast.SelectorExpr); ok {
return x
}
return NilSelectorExpr
}
// ToSliceExpr returns x as a non-nil *ast.SliceExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilSliceExpr.
func ToSliceExpr(x ast.Node) *ast.SliceExpr {
if x, ok := x.(*ast.SliceExpr); ok {
return x
}
return NilSliceExpr
}
// ToStarExpr returns x as a non-nil *ast.StarExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilStarExpr.
func ToStarExpr(x ast.Node) *ast.StarExpr {
if x, ok := x.(*ast.StarExpr); ok {
return x
}
return NilStarExpr
}
// ToStructType returns x as a non-nil *ast.StructType.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilStructType.
func ToStructType(x ast.Node) *ast.StructType {
if x, ok := x.(*ast.StructType); ok {
return x
}
return NilStructType
}
// ToTypeAssertExpr returns x as a non-nil *ast.TypeAssertExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilTypeAssertExpr.
func ToTypeAssertExpr(x ast.Node) *ast.TypeAssertExpr {
if x, ok := x.(*ast.TypeAssertExpr); ok {
return x
}
return NilTypeAssertExpr
}
// ToUnaryExpr returns x as a non-nil *ast.UnaryExpr.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilUnaryExpr.
func ToUnaryExpr(x ast.Node) *ast.UnaryExpr {
if x, ok := x.(*ast.UnaryExpr); ok {
return x
}
return NilUnaryExpr
}
// ToAssignStmt returns x as a non-nil *ast.AssignStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilAssignStmt.
func ToAssignStmt(x ast.Node) *ast.AssignStmt {
if x, ok := x.(*ast.AssignStmt); ok {
return x
}
return NilAssignStmt
}
// ToBadStmt returns x as a non-nil *ast.BadStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilBadStmt.
func ToBadStmt(x ast.Node) *ast.BadStmt {
if x, ok := x.(*ast.BadStmt); ok {
return x
}
return NilBadStmt
}
// ToBlockStmt returns x as a non-nil *ast.BlockStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilBlockStmt.
func ToBlockStmt(x ast.Node) *ast.BlockStmt {
if x, ok := x.(*ast.BlockStmt); ok {
return x
}
return NilBlockStmt
}
// ToBranchStmt returns x as a non-nil *ast.BranchStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilBranchStmt.
func ToBranchStmt(x ast.Node) *ast.BranchStmt {
if x, ok := x.(*ast.BranchStmt); ok {
return x
}
return NilBranchStmt
}
// ToCaseClause returns x as a non-nil *ast.CaseClause.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilCaseClause.
func ToCaseClause(x ast.Node) *ast.CaseClause {
if x, ok := x.(*ast.CaseClause); ok {
return x
}
return NilCaseClause
}
// ToCommClause returns x as a non-nil *ast.CommClause.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilCommClause.
func ToCommClause(x ast.Node) *ast.CommClause {
if x, ok := x.(*ast.CommClause); ok {
return x
}
return NilCommClause
}
// ToDeclStmt returns x as a non-nil *ast.DeclStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilDeclStmt.
func ToDeclStmt(x ast.Node) *ast.DeclStmt {
if x, ok := x.(*ast.DeclStmt); ok {
return x
}
return NilDeclStmt
}
// ToDeferStmt returns x as a non-nil *ast.DeferStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilDeferStmt.
func ToDeferStmt(x ast.Node) *ast.DeferStmt {
if x, ok := x.(*ast.DeferStmt); ok {
return x
}
return NilDeferStmt
}
// ToEmptyStmt returns x as a non-nil *ast.EmptyStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilEmptyStmt.
func ToEmptyStmt(x ast.Node) *ast.EmptyStmt {
if x, ok := x.(*ast.EmptyStmt); ok {
return x
}
return NilEmptyStmt
}
// ToExprStmt returns x as a non-nil *ast.ExprStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilExprStmt.
func ToExprStmt(x ast.Node) *ast.ExprStmt {
if x, ok := x.(*ast.ExprStmt); ok {
return x
}
return NilExprStmt
}
// ToForStmt returns x as a non-nil *ast.ForStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilForStmt.
func ToForStmt(x ast.Node) *ast.ForStmt {
if x, ok := x.(*ast.ForStmt); ok {
return x
}
return NilForStmt
}
// ToGoStmt returns x as a non-nil *ast.GoStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilGoStmt.
func ToGoStmt(x ast.Node) *ast.GoStmt {
if x, ok := x.(*ast.GoStmt); ok {
return x
}
return NilGoStmt
}
// ToIfStmt returns x as a non-nil *ast.IfStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilIfStmt.
func ToIfStmt(x ast.Node) *ast.IfStmt {
if x, ok := x.(*ast.IfStmt); ok {
return x
}
return NilIfStmt
}
// ToIncDecStmt returns x as a non-nil *ast.IncDecStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilIncDecStmt.
func ToIncDecStmt(x ast.Node) *ast.IncDecStmt {
if x, ok := x.(*ast.IncDecStmt); ok {
return x
}
return NilIncDecStmt
}
// ToLabeledStmt returns x as a non-nil *ast.LabeledStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilLabeledStmt.
func ToLabeledStmt(x ast.Node) *ast.LabeledStmt {
if x, ok := x.(*ast.LabeledStmt); ok {
return x
}
return NilLabeledStmt
}
// ToRangeStmt returns x as a non-nil *ast.RangeStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilRangeStmt.
func ToRangeStmt(x ast.Node) *ast.RangeStmt {
if x, ok := x.(*ast.RangeStmt); ok {
return x
}
return NilRangeStmt
}
// ToReturnStmt returns x as a non-nil *ast.ReturnStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilReturnStmt.
func ToReturnStmt(x ast.Node) *ast.ReturnStmt {
if x, ok := x.(*ast.ReturnStmt); ok {
return x
}
return NilReturnStmt
}
// ToSelectStmt returns x as a non-nil *ast.SelectStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilSelectStmt.
func ToSelectStmt(x ast.Node) *ast.SelectStmt {
if x, ok := x.(*ast.SelectStmt); ok {
return x
}
return NilSelectStmt
}
// ToSendStmt returns x as a non-nil *ast.SendStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilSendStmt.
func ToSendStmt(x ast.Node) *ast.SendStmt {
if x, ok := x.(*ast.SendStmt); ok {
return x
}
return NilSendStmt
}
// ToSwitchStmt returns x as a non-nil *ast.SwitchStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilSwitchStmt.
func ToSwitchStmt(x ast.Node) *ast.SwitchStmt {
if x, ok := x.(*ast.SwitchStmt); ok {
return x
}
return NilSwitchStmt
}
// ToTypeSwitchStmt returns x as a non-nil *ast.TypeSwitchStmt.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilTypeSwitchStmt.
func ToTypeSwitchStmt(x ast.Node) *ast.TypeSwitchStmt {
if x, ok := x.(*ast.TypeSwitchStmt); ok {
return x
}
return NilTypeSwitchStmt
}
// ToComment returns x as a non-nil *ast.Comment.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilComment.
func ToComment(x ast.Node) *ast.Comment {
if x, ok := x.(*ast.Comment); ok {
return x
}
return NilComment
}
// ToCommentGroup returns x as a non-nil *ast.CommentGroup.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilCommentGroup.
func ToCommentGroup(x ast.Node) *ast.CommentGroup {
if x, ok := x.(*ast.CommentGroup); ok {
return x
}
return NilCommentGroup
}
// ToFieldList returns x as a non-nil *ast.FieldList.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilFieldList.
func ToFieldList(x ast.Node) *ast.FieldList {
if x, ok := x.(*ast.FieldList); ok {
return x
}
return NilFieldList
}
// ToFile returns x as a non-nil *ast.File.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilFile.
func ToFile(x ast.Node) *ast.File {
if x, ok := x.(*ast.File); ok {
return x
}
return NilFile
}
// ToPackage returns x as a non-nil *ast.Package.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is NilPackage.
func ToPackage(x ast.Node) *ast.Package {
if x, ok := x.(*ast.Package); ok {
return x
}
return NilPackage
}