forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcribe.go
746 lines (732 loc) · 18.1 KB
/
transcribe.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
package gno
import (
"fmt"
"reflect"
)
type (
TransCtrl uint8
TransStage uint8
TransField uint8
)
const (
TRANS_CONTINUE TransCtrl = iota
TRANS_SKIP
TRANS_BREAK
TRANS_EXIT
)
const (
TRANS_ENTER TransStage = iota
TRANS_BLOCK
TRANS_LEAVE
)
const (
TRANS_ROOT TransField = iota
TRANS_BINARY_LEFT
TRANS_BINARY_RIGHT
TRANS_CALL_FUNC
TRANS_CALL_ARG
TRANS_INDEX_X
TRANS_INDEX_INDEX
TRANS_SELECTOR_X
TRANS_SLICE_X
TRANS_SLICE_LOW
TRANS_SLICE_HIGH
TRANS_SLICE_MAX
TRANS_STAR_X
TRANS_REF_X
TRANS_TYPEASSERT_X
TRANS_TYPEASSERT_TYPE
TRANS_UNARY_X
TRANS_COMPOSITE_TYPE
TRANS_COMPOSITE_KEY
TRANS_COMPOSITE_VALUE
TRANS_FUNCLIT_TYPE
TRANS_FUNCLIT_BODY
TRANS_FIELDTYPE_TYPE
TRANS_FIELDTYPE_TAG
TRANS_ARRAYTYPE_LEN
TRANS_ARRAYTYPE_ELT
TRANS_SLICETYPE_ELT
TRANS_INTERFACETYPE_METHOD
TRANS_CHANTYPE_VALUE
TRANS_FUNCTYPE_PARAM
TRANS_FUNCTYPE_RESULT
TRANS_MAPTYPE_KEY
TRANS_MAPTYPE_VALUE
TRANS_STRUCTTYPE_FIELD
TRANS_MAYBENATIVETYPE_TYPE
TRANS_ASSIGN_LHS
TRANS_ASSIGN_RHS
TRANS_BLOCK_BODY
TRANS_DECL_BODY
TRANS_DEFER_CALL
TRANS_EXPR_X
TRANS_FOR_INIT
TRANS_FOR_COND
TRANS_FOR_POST
TRANS_FOR_BODY
TRANS_GO_CALL
TRANS_IF_INIT
TRANS_IF_COND
TRANS_IF_BODY
TRANS_IF_ELSE
TRANS_IF_CASE_BODY
TRANS_INCDEC_X
TRANS_RANGE_X
TRANS_RANGE_KEY
TRANS_RANGE_VALUE
TRANS_RANGE_BODY
TRANS_RETURN_RESULT
TRANS_PANIC_EXCEPTION
TRANS_SELECT_CASE
TRANS_SELECTCASE_COMM
TRANS_SELECTCASE_BODY
TRANS_SEND_CHAN
TRANS_SEND_VALUE
TRANS_SWITCH_INIT
TRANS_SWITCH_X
TRANS_SWITCH_CASE
TRANS_SWITCHCASE_CASE
TRANS_SWITCHCASE_BODY
TRANS_FUNC_RECV
TRANS_FUNC_TYPE
TRANS_FUNC_BODY
TRANS_IMPORT_PATH
TRANS_CONST_TYPE
TRANS_CONST_VALUE
TRANS_VAR_TYPE
TRANS_VAR_VALUE
TRANS_TYPE_TYPE
TRANS_FILE_BODY
)
// return:
// - TRANS_CONTINUE to visit children recursively;
// - TRANS_SKIP to break out of the
// ENTER,CHILDS1,[BLOCK,CHILDS2]?,LEAVE sequence for that node,
// i.e. skipping (the rest of) it;
// - TRANS_BREAK to break out of looping in CHILDS1 or CHILDS2,
// - TRANS_EXIT to stop traversing altogether.
//
// Do not mutate ns.
// Must return a new node to replace the old one,
// or the node will be deleted (or set to nil).
// Read: transform the ftype/index of context ns which
// is n during stage.
type Transform func(ns []Node, ftype TransField, index int, n Node, stage TransStage) (Node, TransCtrl)
// n MUST be a pointer to a node struct.
// returns the transcribe code returned for n.
// returns new node nn to replace n.
func Transcribe(n Node, t Transform) (nn Node) {
if reflect.TypeOf(n).Kind() != reflect.Ptr {
panic("Transcribe() expects a non-pointer concrete Node struct")
}
var ns []Node = make([]Node, 0, 32)
var nc TransCtrl
nn = transcribe(t, ns, TRANS_ROOT, 0, n, &nc)
return
}
func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc *TransCtrl) (nn Node) {
// transcribe n on the way in.
var c TransCtrl
nn, c = t(ns, ftype, index, n, TRANS_ENTER)
if isStopOrSkip(nc, c) {
return
}
// push nn to node stack.
nns := append(ns, nn)
// visit any children of n.
switch cnn := nn.(type) {
case *NameExpr:
case *BasicLitExpr:
case *BinaryExpr:
cnn.Left = transcribe(t, nns, TRANS_BINARY_LEFT, 0, cnn.Left, &c).(Expr) // XXX wished this worked with nil.
if isStopOrSkip(nc, c) {
return
}
cnn.Right = transcribe(t, nns, TRANS_BINARY_RIGHT, 0, cnn.Right, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *CallExpr:
cnn.Func = transcribe(t, nns, TRANS_CALL_FUNC, 0, cnn.Func, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
for idx := range cnn.Args {
cnn.Args[idx] = transcribe(t, nns, TRANS_CALL_ARG, idx, cnn.Args[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *IndexExpr:
cnn.X = transcribe(t, nns, TRANS_INDEX_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
cnn.Index = transcribe(t, nns, TRANS_INDEX_INDEX, 0, cnn.Index, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *SelectorExpr:
cnn.X = transcribe(t, nns, TRANS_SELECTOR_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *SliceExpr:
cnn.X = transcribe(t, nns, TRANS_SLICE_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
if cnn.Low != nil {
cnn.Low = transcribe(t, nns, TRANS_SLICE_LOW, 0, cnn.Low, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
if cnn.High != nil {
cnn.High = transcribe(t, nns, TRANS_SLICE_HIGH, 0, cnn.High, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
if cnn.Max != nil {
cnn.Max = transcribe(t, nns, TRANS_SLICE_MAX, 0, cnn.Max, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
case *StarExpr:
cnn.X = transcribe(t, nns, TRANS_STAR_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *RefExpr:
cnn.X = transcribe(t, nns, TRANS_REF_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *TypeAssertExpr:
cnn.X = transcribe(t, nns, TRANS_TYPEASSERT_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
if cnn.Type != nil {
cnn.Type = transcribe(t, nns, TRANS_TYPEASSERT_TYPE, 0, cnn.Type, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
case *UnaryExpr:
cnn.X = transcribe(t, nns, TRANS_UNARY_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *CompositeLitExpr:
if cnn.Type != nil {
cnn.Type = transcribe(t, nns, TRANS_COMPOSITE_TYPE, 0, cnn.Type, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
for idx, kvx := range cnn.Elts {
k, v := kvx.Key, kvx.Value
if k != nil {
k = transcribe(t, nns, TRANS_COMPOSITE_KEY, idx, k, &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
v = transcribe(t, nns, TRANS_COMPOSITE_VALUE, idx, v, &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
cnn.Elts[idx] = KeyValueExpr{Key: k, Value: v}
}
case *FuncLitExpr:
cnn.Type = *transcribe(t, nns, TRANS_FUNCLIT_TYPE, 0, &cnn.Type, &c).(*FuncTypeExpr)
if isStopOrSkip(nc, c) {
return
}
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*FuncLitExpr)
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_FUNCLIT_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *FieldTypeExpr:
cnn.Type = transcribe(t, nns, TRANS_FIELDTYPE_TYPE, 0, cnn.Type, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
if cnn.Tag != nil {
cnn.Tag = transcribe(t, nns, TRANS_FIELDTYPE_TAG, 0, cnn.Tag, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
case *ArrayTypeExpr:
if cnn.Len != nil {
cnn.Len = transcribe(t, nns, TRANS_ARRAYTYPE_LEN, 0, cnn.Len, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
cnn.Elt = transcribe(t, nns, TRANS_ARRAYTYPE_ELT, 0, cnn.Elt, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *SliceTypeExpr:
cnn.Elt = transcribe(t, nns, TRANS_SLICETYPE_ELT, 0, cnn.Elt, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *InterfaceTypeExpr:
for idx := range cnn.Methods {
cnn.Methods[idx] = *transcribe(t, nns, TRANS_INTERFACETYPE_METHOD, idx, &cnn.Methods[idx], &c).(*FieldTypeExpr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *ChanTypeExpr:
cnn.Value = transcribe(t, nns, TRANS_CHANTYPE_VALUE, 0, cnn.Value, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *FuncTypeExpr:
for idx := range cnn.Params {
cnn.Params[idx] = *transcribe(t, nns, TRANS_FUNCTYPE_PARAM, idx, &cnn.Params[idx], &c).(*FieldTypeExpr)
if isStopOrSkip(nc, c) {
return
}
}
for idx := range cnn.Results {
cnn.Results[idx] = *transcribe(t, nns, TRANS_FUNCTYPE_RESULT, idx, &cnn.Results[idx], &c).(*FieldTypeExpr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *MapTypeExpr:
cnn.Key = transcribe(t, nns, TRANS_MAPTYPE_KEY, 0, cnn.Key, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
cnn.Value = transcribe(t, nns, TRANS_MAPTYPE_VALUE, 0, cnn.Value, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *StructTypeExpr:
for idx := range cnn.Fields {
cnn.Fields[idx] = *transcribe(t, nns, TRANS_STRUCTTYPE_FIELD, idx, &cnn.Fields[idx], &c).(*FieldTypeExpr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *MaybeNativeTypeExpr:
cnn.Type = transcribe(t, nns, TRANS_MAYBENATIVETYPE_TYPE, 0, cnn.Type, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *AssignStmt:
for idx := range cnn.Lhs {
cnn.Lhs[idx] = transcribe(t, nns, TRANS_ASSIGN_LHS, idx, cnn.Lhs[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
for idx := range cnn.Rhs {
cnn.Rhs[idx] = transcribe(t, nns, TRANS_ASSIGN_RHS, idx, cnn.Rhs[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *BlockStmt:
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*BlockStmt)
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_BLOCK_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *BranchStmt:
case *DeclStmt:
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_DECL_BODY, idx, cnn.Body[idx], &c).(SimpleDeclStmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *DeferStmt:
cnn.Call = *transcribe(t, nns, TRANS_DEFER_CALL, 0, &cnn.Call, &c).(*CallExpr)
if isStopOrSkip(nc, c) {
return
}
case *EmptyStmt:
case *ExprStmt:
cnn.X = transcribe(t, nns, TRANS_EXPR_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *ForStmt:
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*ForStmt)
}
if cnn.Init != nil {
cnn.Init = transcribe(t, nns, TRANS_FOR_INIT, 0, cnn.Init, &c).(SimpleStmt)
if isStopOrSkip(nc, c) {
return
}
}
if cnn.Cond != nil {
cnn.Cond = transcribe(t, nns, TRANS_FOR_COND, 0, cnn.Cond, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
if cnn.Post != nil {
cnn.Post = transcribe(t, nns, TRANS_FOR_POST, 0, cnn.Post, &c).(SimpleStmt)
if isStopOrSkip(nc, c) {
return
}
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_FOR_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *GoStmt:
cnn.Call = *transcribe(t, nns, TRANS_GO_CALL, 0, &cnn.Call, &c).(*CallExpr)
if isStopOrSkip(nc, c) {
return
}
case *IfStmt:
// NOTE: like switch stmts, both if statements AND
// contained cases visit with the TRANS_BLOCK stage, even
// though during runtime only one block is created.
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*IfStmt)
}
if cnn.Init != nil {
cnn.Init = transcribe(t, nns, TRANS_IF_INIT, 0, cnn.Init, &c).(SimpleStmt)
if isStopOrSkip(nc, c) {
return
}
}
cnn.Cond = transcribe(t, nns, TRANS_IF_COND, 0, cnn.Cond, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
cnn.Then = *transcribe(t, nns, TRANS_IF_BODY, 0, &cnn.Then, &c).(*IfCaseStmt)
if isStopOrSkip(nc, c) {
return
}
cnn.Else = *transcribe(t, nns, TRANS_IF_ELSE, 0, &cnn.Else, &c).(*IfCaseStmt)
if isStopOrSkip(nc, c) {
return
}
case *IfCaseStmt:
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*IfCaseStmt)
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_IF_CASE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *IncDecStmt:
cnn.X = transcribe(t, nns, TRANS_INCDEC_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *RangeStmt:
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*RangeStmt)
}
cnn.X = transcribe(t, nns, TRANS_RANGE_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
if cnn.Key != nil {
cnn.Key = transcribe(t, nns, TRANS_RANGE_KEY, 0, cnn.Key, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
if cnn.Value != nil {
cnn.Value = transcribe(t, nns, TRANS_RANGE_VALUE, 0, cnn.Value, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_RANGE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *ReturnStmt:
for idx := range cnn.Results {
cnn.Results[idx] = transcribe(t, nns, TRANS_RETURN_RESULT, idx, cnn.Results[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *PanicStmt:
cnn.Exception = transcribe(t, nns, TRANS_PANIC_EXCEPTION, 0, cnn.Exception, &c).(Expr)
case *SelectStmt:
for idx := range cnn.Cases {
cnn.Cases[idx] = *transcribe(t, nns, TRANS_SELECT_CASE, idx, &cnn.Cases[idx], &c).(*SelectCaseStmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *SelectCaseStmt:
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*SelectCaseStmt)
}
cnn.Comm = transcribe(t, nns, TRANS_SELECTCASE_COMM, 0, cnn.Comm, &c).(Stmt)
if isStopOrSkip(nc, c) {
return
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_SELECTCASE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *SendStmt:
cnn.Chan = transcribe(t, nns, TRANS_SEND_CHAN, 0, cnn.Chan, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
cnn.Value = transcribe(t, nns, TRANS_SEND_VALUE, 0, cnn.Value, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *SwitchStmt:
// NOTE: unlike the select case, and like if stmts, both
// switch statements AND contained cases visit with the
// TRANS_BLOCK stage, even though during runtime only one
// block is created.
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*SwitchStmt)
}
if cnn.Init != nil {
cnn.Init = transcribe(t, nns, TRANS_SWITCH_INIT, 0, cnn.Init, &c).(SimpleStmt)
if isStopOrSkip(nc, c) {
return
}
}
cnn.X = transcribe(t, nns, TRANS_SWITCH_X, 0, cnn.X, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
for idx := range cnn.Clauses {
cnn.Clauses[idx] = *transcribe(t, nns, TRANS_SWITCH_CASE, idx, &cnn.Clauses[idx], &c).(*SwitchClauseStmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *SwitchClauseStmt:
// NOTE: unlike the select case, both switch
// statements AND switch cases visit with the
// TRANS_BLOCK stage, even though during runtime
// only one block is created.
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*SwitchClauseStmt)
}
for idx := range cnn.Cases {
cnn.Cases[idx] = transcribe(t, nns, TRANS_SWITCHCASE_CASE, idx, cnn.Cases[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_SWITCHCASE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *FuncDecl:
if cnn.Recv.Type != nil {
cnn.Recv = *transcribe(t, nns, TRANS_FUNC_RECV, 0, &cnn.Recv, &c).(*FieldTypeExpr)
if isStopOrSkip(nc, c) {
return
}
}
cnn.Type = *transcribe(t, nns, TRANS_FUNC_TYPE, 0, &cnn.Type, &c).(*FuncTypeExpr)
if isStopOrSkip(nc, c) {
return
}
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*FuncDecl)
}
for idx := range cnn.Body {
cnn.Body[idx] = transcribe(t, nns, TRANS_FUNC_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *ImportDecl:
// nothing to do
case *ValueDecl:
if cnn.Type != nil {
cnn.Type = transcribe(t, nns, TRANS_VAR_TYPE, 0, cnn.Type, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
}
for idx := range cnn.Values {
cnn.Values[idx] = transcribe(t, nns, TRANS_VAR_VALUE, idx, cnn.Values[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *TypeDecl:
cnn.Type = transcribe(t, nns, TRANS_TYPE_TYPE, 0, cnn.Type, &c).(Expr)
if isStopOrSkip(nc, c) {
return
}
case *FileNode:
cnn2, c2 := t(ns, ftype, index, cnn, TRANS_BLOCK)
if isStopOrSkip(nc, c2) {
nn = cnn2
return
} else {
cnn = cnn2.(*FileNode)
}
for idx := range cnn.Decls {
cnn.Decls[idx] = transcribe(t, nns, TRANS_FILE_BODY, idx, cnn.Decls[idx], &c).(Decl)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
return
}
}
case *ConstExpr, *constTypeExpr: // leaf nodes
// These nodes get created by the preprocessor while
// leaving the type expression of a composite lit, before
// visiting the key value elements of the composite lit.
default:
if n == nil {
panic(fmt.Sprintf("node missing for %v", ftype))
} else {
panic(fmt.Sprintf("unexpected node type %#v", n))
}
}
// transcribe n on the way out.
nn, *nc = t(ns, ftype, index, nn, TRANS_LEAVE)
return
}
// returns true if transcribe() should stop or skip or exit (& if so then sets *c to TRANS_EXIT if exit, or TRANS_CONTINUE if break).
func isStopOrSkip(oldnc *TransCtrl, nc TransCtrl) (stop bool) {
if nc == TRANS_EXIT {
*oldnc = TRANS_EXIT
return true
} else if nc == TRANS_SKIP {
*oldnc = TRANS_CONTINUE
return true
} else if nc == TRANS_CONTINUE {
return false
} else {
panic("should not happen")
}
}
// returns true if transcribe() should break (a loop).
func isBreak(nc TransCtrl) (brek bool) {
if nc == TRANS_BREAK {
return true
} else {
return false
}
}