forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodes.go
1710 lines (1520 loc) · 40 KB
/
nodes.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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gno
import (
"fmt"
"reflect"
rdebug "runtime/debug"
"strconv"
)
//----------------------------------------
// Primitives
type Word int
const (
// Special words
ILLEGAL Word = iota
// Names and basic type literals
// (these words stand for classes of literals)
NAME // main
INT // 12345
FLOAT // 123.45
IMAG // 123.45i
CHAR // 'a'
STRING // "abc"
// Operators and delimiters
ADD // +
SUB // -
MUL // *
QUO // /
REM // %
BAND // &
BOR // |
XOR // ^
SHL // <<
SHR // >>
BAND_NOT // &^
ADD_ASSIGN // +=
SUB_ASSIGN // -=
MUL_ASSIGN // *=
QUO_ASSIGN // /=
REM_ASSIGN // %=
BAND_ASSIGN // &=
BOR_ASSIGN // |=
XOR_ASSIGN // ^=
SHL_ASSIGN // <<=
SHR_ASSIGN // >>=
BAND_NOT_ASSIGN // &^=
LAND // &&
LOR // ||
ARROW // <-
INC // ++
DEC // --
EQL // ==
LSS // <
GTR // >
ASSIGN // =
NOT // !
NEQ // !=
LEQ // <=
GEQ // >=
DEFINE // :=
// Keywords
BREAK
CASE
CHAN
CONST
CONTINUE
DEFAULT
DEFER
ELSE
FALLTHROUGH
FOR
FUNC
GO
GOTO
IF
IMPORT
INTERFACE
MAP
PACKAGE
RANGE
RETURN
SELECT
STRUCT
SWITCH
TYPE
VAR
)
type Name string
//----------------------------------------
// Attributes
// All nodes have attributes for general analysis purposes.
// Exported Attribute fields like Loc and Label are persisted
// even after preprocessing. Temporary attributes (e.g. those
// for preprocessing) are stored in .data.
type Location struct {
PkgPath string
File string
Line int
}
type Attributes struct {
Loc Location
Label Name
data map[interface{}]interface{} // not persisted
}
func (a *Attributes) GetLocation() Location {
return a.Loc
}
func (a *Attributes) SetLocation(loc Location) {
a.Loc = loc
}
func (a *Attributes) GetAttribute(key interface{}) interface{} {
return a.data[key]
}
func (a *Attributes) SetAttribute(key interface{}, value interface{}) {
if a.data == nil {
a.data = make(map[interface{}]interface{})
}
a.data[key] = value
}
//----------------------------------------
// Node
type Node interface {
assertNode()
String() string
Copy() Node
GetLocation() Location
SetLocation(Location)
GetAttribute(key interface{}) interface{}
SetAttribute(key interface{}, value interface{})
}
// non-pointer receiver to help make immutable.
func (_ *NameExpr) assertNode() {}
func (_ *BasicLitExpr) assertNode() {}
func (_ *BinaryExpr) assertNode() {}
func (_ *CallExpr) assertNode() {}
func (_ *IndexExpr) assertNode() {}
func (_ *SelectorExpr) assertNode() {}
func (_ *SliceExpr) assertNode() {}
func (_ *StarExpr) assertNode() {}
func (_ *RefExpr) assertNode() {}
func (_ *TypeAssertExpr) assertNode() {}
func (_ *UnaryExpr) assertNode() {}
func (_ *CompositeLitExpr) assertNode() {}
func (_ *KeyValueExpr) assertNode() {}
func (_ *FuncLitExpr) assertNode() {}
func (_ *constExpr) assertNode() {}
func (_ *FieldTypeExpr) assertNode() {}
func (_ *ArrayTypeExpr) assertNode() {}
func (_ *SliceTypeExpr) assertNode() {}
func (_ *InterfaceTypeExpr) assertNode() {}
func (_ *ChanTypeExpr) assertNode() {}
func (_ *FuncTypeExpr) assertNode() {}
func (_ *MapTypeExpr) assertNode() {}
func (_ *StructTypeExpr) assertNode() {}
func (_ *constTypeExpr) assertNode() {}
func (_ *AssignStmt) assertNode() {}
func (_ *BlockStmt) assertNode() {}
func (_ *BranchStmt) assertNode() {}
func (_ *DeclStmt) assertNode() {}
func (_ *DeferStmt) assertNode() {}
func (_ *ExprStmt) assertNode() {}
func (_ *ForStmt) assertNode() {}
func (_ *GoStmt) assertNode() {}
func (_ *IfStmt) assertNode() {}
func (_ *IfCaseStmt) assertNode() {}
func (_ *IncDecStmt) assertNode() {}
func (_ *LabeledStmt) assertNode() {}
func (_ *RangeStmt) assertNode() {}
func (_ *ReturnStmt) assertNode() {}
func (_ *PanicStmt) assertNode() {}
func (_ *SelectStmt) assertNode() {}
func (_ *SelectCaseStmt) assertNode() {}
func (_ *SendStmt) assertNode() {}
func (_ *SwitchStmt) assertNode() {}
func (_ *SwitchClauseStmt) assertNode() {}
func (_ *EmptyStmt) assertNode() {}
func (_ *bodyStmt) assertNode() {}
func (_ *FuncDecl) assertNode() {}
func (_ *ImportDecl) assertNode() {}
func (_ *ValueDecl) assertNode() {}
func (_ *TypeDecl) assertNode() {}
func (_ *FileNode) assertNode() {}
func (_ *PackageNode) assertNode() {}
var _ Node = &NameExpr{}
var _ Node = &BasicLitExpr{}
var _ Node = &BinaryExpr{}
var _ Node = &CallExpr{}
var _ Node = &IndexExpr{}
var _ Node = &SelectorExpr{}
var _ Node = &SliceExpr{}
var _ Node = &StarExpr{}
var _ Node = &RefExpr{}
var _ Node = &TypeAssertExpr{}
var _ Node = &UnaryExpr{}
var _ Node = &CompositeLitExpr{}
var _ Node = &KeyValueExpr{}
var _ Node = &FuncLitExpr{}
var _ Node = &constExpr{}
var _ Node = &FieldTypeExpr{}
var _ Node = &ArrayTypeExpr{}
var _ Node = &SliceTypeExpr{}
var _ Node = &InterfaceTypeExpr{}
var _ Node = &ChanTypeExpr{}
var _ Node = &FuncTypeExpr{}
var _ Node = &MapTypeExpr{}
var _ Node = &StructTypeExpr{}
var _ Node = &constTypeExpr{}
var _ Node = &AssignStmt{}
var _ Node = &BlockStmt{}
var _ Node = &BranchStmt{}
var _ Node = &DeclStmt{}
var _ Node = &DeferStmt{}
var _ Node = &ExprStmt{}
var _ Node = &ForStmt{}
var _ Node = &GoStmt{}
var _ Node = &IfStmt{}
var _ Node = &IfCaseStmt{}
var _ Node = &IncDecStmt{}
var _ Node = &LabeledStmt{}
var _ Node = &RangeStmt{}
var _ Node = &ReturnStmt{}
var _ Node = &PanicStmt{}
var _ Node = &SelectStmt{}
var _ Node = &SelectCaseStmt{}
var _ Node = &SendStmt{}
var _ Node = &SwitchStmt{}
var _ Node = &SwitchClauseStmt{}
var _ Node = &EmptyStmt{}
var _ Node = &bodyStmt{}
var _ Node = &FuncDecl{}
var _ Node = &ImportDecl{}
var _ Node = &ValueDecl{}
var _ Node = &TypeDecl{}
var _ Node = &FileNode{}
var _ Node = &PackageNode{}
//----------------------------------------
// Expr
//
// expressions generally have no side effects on the caller's context,
// except for channel blocks, type assertions, and panics.
type Expr interface {
Node
assertExpr()
}
type Exprs []Expr
// non-pointer receiver to help make immutable.
func (*NameExpr) assertExpr() {}
func (*BasicLitExpr) assertExpr() {}
func (*BinaryExpr) assertExpr() {}
func (*CallExpr) assertExpr() {}
func (*IndexExpr) assertExpr() {}
func (*SelectorExpr) assertExpr() {}
func (*SliceExpr) assertExpr() {}
func (*StarExpr) assertExpr() {}
func (*RefExpr) assertExpr() {}
func (*TypeAssertExpr) assertExpr() {}
func (*UnaryExpr) assertExpr() {}
func (*CompositeLitExpr) assertExpr() {}
func (*KeyValueExpr) assertExpr() {}
func (*FuncLitExpr) assertExpr() {}
func (*constExpr) assertExpr() {}
var _ Expr = &NameExpr{}
var _ Expr = &BasicLitExpr{}
var _ Expr = &BinaryExpr{}
var _ Expr = &CallExpr{}
var _ Expr = &IndexExpr{}
var _ Expr = &SelectorExpr{}
var _ Expr = &SliceExpr{}
var _ Expr = &StarExpr{}
var _ Expr = &RefExpr{}
var _ Expr = &TypeAssertExpr{}
var _ Expr = &UnaryExpr{}
var _ Expr = &CompositeLitExpr{}
var _ Expr = &KeyValueExpr{}
var _ Expr = &FuncLitExpr{}
var _ Expr = &constExpr{}
type NameExpr struct {
Attributes
// TODO rename .Path's to .ValuePaths.
Path ValuePath // set by preprocessor.
Name
}
type NameExprs []NameExpr
type BasicLitExpr struct {
Attributes
// INT, FLOAT, IMAG, CHAR, or STRING
Kind Word
// literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo"
// or `\m\n\o`
Value string
}
type BinaryExpr struct { // (Left Op Right)
Attributes
Left Expr // left operand
Op Word // operator
Right Expr // right operand
}
type CallExpr struct { // Func(Args<Varg?...>)
Attributes
Func Expr // function expression
Args Exprs // function arguments, if any.
Varg bool // if true, final arg is variadic.
NumArgs int // len(Args) or len(Args[0].Results)
}
type IndexExpr struct { // X[Index]
Attributes
X Expr // expression
Index Expr // index expression
HasOK bool // if true, is form: `value, ok := <X>[<Key>]
}
type SelectorExpr struct { // X.Sel
Attributes
X Expr // expression
Path ValuePath // set by preprocessor.
Sel Name // field selector
}
type SliceExpr struct { // X[Low:High:Max]
Attributes
X Expr // expression
Low Expr // begin of slice range; or nil
High Expr // end of slice range; or nil
Max Expr // maximum capacity of slice; or nil; added in Go 1.2
}
// A StarExpr node represents an expression of the form
// "*" Expression. Semantically it could be a unary "*"
// expression, or a pointer type.
type StarExpr struct { // *X
Attributes
X Expr // operand
}
type RefExpr struct { // &X
Attributes
X Expr // operand
}
type TypeAssertExpr struct { // X.(Type)
Attributes
X Expr // expression.
Type Expr // asserted type, never nil.
HasOK bool // if true, is form: `_, ok := <X>.(<Type>)`.
}
// A UnaryExpr node represents a unary expression. Unary
// "*" expressions (dereferencing and pointer-types) are
// represented with StarExpr nodes. Unary & expressions
// (referencing) are represented with RefExpr nodes.
type UnaryExpr struct { // (Op X)
Attributes
X Expr // operand
Op Word // operator
}
// MyType{<key>:<value>} struct, array, slice, and map
// expressions.
type CompositeLitExpr struct {
Attributes
Type Expr // literal type; or nil
Elts KeyValueExprs // list of struct fields; if any
}
// Returns true if any elements are keyed.
// Panics if inconsistent.
func (clx *CompositeLitExpr) IsKeyed() bool {
if len(clx.Elts) == 0 {
return false
} else if clx.Elts[0].Key == nil {
for i := 1; i < len(clx.Elts); i++ {
if clx.Elts[i].Key != nil {
panic("mixed keyed and unkeyed elements")
}
}
return false
} else {
for i := 1; i < len(clx.Elts); i++ {
if clx.Elts[i].Key == nil {
panic("mixed keyed and unkeyed elements")
}
}
return true
}
}
// A KeyValueExpr represents a single key-value pair in
// struct, array, slice, and map expressions.
type KeyValueExpr struct {
Attributes
Key Expr // or nil
Value Expr // never nil
}
type KeyValueExprs []KeyValueExpr
// A FuncLitExpr node represents a function literal. Here one
// can reference statements from an expression, which
// completes the procedural circle.
type FuncLitExpr struct {
Attributes
StaticBlock
Type FuncTypeExpr // function type
Body // function body
}
// The preprocessor replaces const expressions
// with *constExpr nodes.
type constExpr struct {
Attributes
Source Expr // (preprocessed) source of this value.
TypedValue
}
//----------------------------------------
// Type(Expressions)
//
// In Go, Type expressions can be evaluated immediately
// without invoking the stack machine. Exprs in type
// expressions are const (as in array len expr or map key type
// expr) or refer to an exposed symbol (with any pointer
// indirections). this makes for more optimal performance.
//
// In Gno, type expressions are evaluated on the stack, with
// continuation opcodes, so the Gno VM could support types as
// first class objects.
type TypeExpr interface {
Expr
assertTypeExpr()
}
// non-pointer receiver to help make immutable.
func (_ *FieldTypeExpr) assertTypeExpr() {}
func (_ *ArrayTypeExpr) assertTypeExpr() {}
func (_ *SliceTypeExpr) assertTypeExpr() {}
func (_ *InterfaceTypeExpr) assertTypeExpr() {}
func (_ *ChanTypeExpr) assertTypeExpr() {}
func (_ *FuncTypeExpr) assertTypeExpr() {}
func (_ *MapTypeExpr) assertTypeExpr() {}
func (_ *StructTypeExpr) assertTypeExpr() {}
func (_ *constTypeExpr) assertTypeExpr() {}
func (_ *FieldTypeExpr) assertExpr() {}
func (_ *ArrayTypeExpr) assertExpr() {}
func (_ *SliceTypeExpr) assertExpr() {}
func (_ *InterfaceTypeExpr) assertExpr() {}
func (_ *ChanTypeExpr) assertExpr() {}
func (_ *FuncTypeExpr) assertExpr() {}
func (_ *MapTypeExpr) assertExpr() {}
func (_ *StructTypeExpr) assertExpr() {}
func (_ *constTypeExpr) assertExpr() {}
var _ TypeExpr = &FieldTypeExpr{}
var _ TypeExpr = &ArrayTypeExpr{}
var _ TypeExpr = &SliceTypeExpr{}
var _ TypeExpr = &InterfaceTypeExpr{}
var _ TypeExpr = &ChanTypeExpr{}
var _ TypeExpr = &FuncTypeExpr{}
var _ TypeExpr = &MapTypeExpr{}
var _ TypeExpr = &StructTypeExpr{}
var _ TypeExpr = &constTypeExpr{}
type FieldTypeExpr struct {
Attributes
Name
Type Expr
// Currently only BasicLitExpr allowed.
// NOTE: In Go, only struct fields can have tags.
Tag Expr
}
type FieldTypeExprs []FieldTypeExpr
func (ftxz FieldTypeExprs) IsNamed() bool {
named := false
for i, ftx := range ftxz {
if i == 0 {
named = ftx.Name != ""
} else {
if named && ftx.Name == "" {
panic("[]FieldTypeExpr has inconsistent namedness (starts named)")
} else if !named && ftx.Name != "" {
panic("[]FieldTypeExpr has inconsistent namedness (starts unnamed)")
}
}
}
return named
}
type ArrayTypeExpr struct {
Attributes
Len Expr // if nil, variadic array lit
Elt Expr // element type
}
type SliceTypeExpr struct {
Attributes
Elt Expr // element type
Vrd bool // variadic arg expression
}
type InterfaceTypeExpr struct {
Attributes
Methods FieldTypeExprs // list of methods
Generic Name // for uverse generics
}
type ChanDir int
const (
SEND ChanDir = 1 << iota
RECV
)
const (
BOTH = SEND | RECV
)
type ChanTypeExpr struct {
Attributes
Dir ChanDir // channel direction
Value Expr // value type
}
type FuncTypeExpr struct {
Attributes
Params FieldTypeExprs // (incoming) parameters, if any.
Results FieldTypeExprs // (outgoing) results, if any.
}
type MapTypeExpr struct {
Attributes
Key Expr // const
Value Expr // value type
}
type StructTypeExpr struct {
Attributes
Fields FieldTypeExprs // list of field declarations
}
// Like constExpr but for types.
type constTypeExpr struct {
Attributes
Source Expr
Type Type
}
//----------------------------------------
// Stmt
//
// statements generally have side effects on the calling context.
type Stmt interface {
Node
assertStmt()
}
type Body []Stmt
func (ss Body) GetBody() Body {
return ss
}
func (ss Body) GetLabeledStmt(label Name) (Stmt, int) {
for i, stmt := range ss {
if ls, ok := stmt.(*LabeledStmt); ok {
return ls.Stmt, i
}
}
return nil, -1
}
//----------------------------------------
// non-pointer receiver to help make immutable.
func (*AssignStmt) assertStmt() {}
func (*BlockStmt) assertStmt() {}
func (*BranchStmt) assertStmt() {}
func (*DeclStmt) assertStmt() {}
func (*DeferStmt) assertStmt() {}
func (*EmptyStmt) assertStmt() {} // useful for _ctif
func (*ExprStmt) assertStmt() {}
func (*ForStmt) assertStmt() {}
func (*GoStmt) assertStmt() {}
func (*IfStmt) assertStmt() {}
func (*IfCaseStmt) assertStmt() {}
func (*IncDecStmt) assertStmt() {}
func (*LabeledStmt) assertStmt() {}
func (*RangeStmt) assertStmt() {}
func (*ReturnStmt) assertStmt() {}
func (*PanicStmt) assertStmt() {}
func (*SelectStmt) assertStmt() {}
func (*SelectCaseStmt) assertStmt() {}
func (*SendStmt) assertStmt() {}
func (*SwitchStmt) assertStmt() {}
func (*SwitchClauseStmt) assertStmt() {}
func (*bodyStmt) assertStmt() {}
var _ Stmt = &AssignStmt{}
var _ Stmt = &BlockStmt{}
var _ Stmt = &BranchStmt{}
var _ Stmt = &DeclStmt{}
var _ Stmt = &DeferStmt{}
var _ Stmt = &EmptyStmt{}
var _ Stmt = &ExprStmt{}
var _ Stmt = &ForStmt{}
var _ Stmt = &GoStmt{}
var _ Stmt = &IfStmt{}
var _ Stmt = &IfCaseStmt{}
var _ Stmt = &IncDecStmt{}
var _ Stmt = &LabeledStmt{}
var _ Stmt = &RangeStmt{}
var _ Stmt = &ReturnStmt{}
var _ Stmt = &PanicStmt{}
var _ Stmt = &SelectStmt{}
var _ Stmt = &SelectCaseStmt{}
var _ Stmt = &SendStmt{}
var _ Stmt = &SwitchStmt{}
var _ Stmt = &SwitchClauseStmt{}
var _ Stmt = &bodyStmt{}
type AssignStmt struct {
Attributes
Lhs Exprs
Op Word // assignment word (DEFINE, ASSIGN)
Rhs Exprs
}
type BlockStmt struct {
Attributes
StaticBlock
Body
}
type BranchStmt struct {
Attributes
Op Word // keyword word (BREAK, CONTINUE, GOTO, FALLTHROUGH)
Label Name // label name; or empty
Depth uint8 // blocks to pop
BodyIndex int // index of statement of body
}
type DeclStmt struct {
Attributes
Body // (simple) ValueDecl or TypeDecl
}
type DeferStmt struct {
Attributes
Call CallExpr
}
// A compile artifact to use in place of nil.
// For example, _ctif() may return an empty statement.
type EmptyStmt struct {
Attributes
}
type ExprStmt struct {
Attributes
X Expr
}
type ForStmt struct {
Attributes
StaticBlock
Init Stmt // initialization (simple) statement; or nil
Cond Expr // condition; or nil
Post Stmt // post iteration (simple) statement; or nil
Body
}
type GoStmt struct {
Attributes
Call CallExpr
}
// NOTE: syntactically, code may choose to chain if-else statements
// with `} else if ... {` constructions, but this is not represented
// in the logical AST.
type IfStmt struct {
Attributes
StaticBlock
Init Stmt // initialization (simple) statement; or nil
Cond Expr // condition; or nil
Then IfCaseStmt // body statements
Else IfCaseStmt // else statements
}
type IfCaseStmt struct {
Attributes
StaticBlock
Body
}
type IncDecStmt struct {
Attributes
X Expr
Op Word // INC or DEC
}
type LabeledStmt struct {
Attributes
Label Name
Stmt Stmt
}
type RangeStmt struct {
Attributes
StaticBlock
X Expr // value to range over
Key, Value Expr // Key, Value may be nil
Op Word // ASSIGN or DEFINE
Body
IsMap bool // if X is map type
IsString bool // if X is string type
IsArrayPtr bool // if X is array-pointer type
}
type ReturnStmt struct {
Attributes
Results Exprs // result expressions; or nil
}
type PanicStmt struct {
Attributes
Exception Expr // panic expression; not nil
}
type SelectStmt struct {
Attributes
Cases []SelectCaseStmt
}
type SelectCaseStmt struct {
Attributes
StaticBlock
Comm Stmt // send or receive statement; nil means default case
Body
}
type SendStmt struct {
Attributes
Chan Expr
Value Expr
}
// type ReceiveStmt
// is just AssignStmt with a Receive unary expression.
type SwitchStmt struct {
Attributes
StaticBlock
Init Stmt // init (simple) stmt; or nil
X Expr // tag or _.(type) expr; or nil
IsTypeSwitch bool // true iff X is .(type) expr
Clauses []SwitchClauseStmt // case clauses
VarName Name // type-switched value; or ""
}
type SwitchClauseStmt struct {
Attributes
StaticBlock
Cases Exprs // list of expressions or types; nil means default case
Body
}
//----------------------------------------
// bodyStmt (persistent)
// NOTE: embedded in Block.
type bodyStmt struct {
Attributes
Body // for non-loop stmts
BodyLen int // for for-continue
NextBodyIndex int // init:-2, cond/elem:-1, body:0..., post:n
NumOps int // number of Ops, for goto
NumStmts int // number of Stmts, for goto
Cond Expr // for ForStmt
Post Stmt // for ForStmt
Active Stmt // for PopStmt()
Key Expr // for RangeStmt
Value Expr // for RangeStmt
Op Word // for RangeStmt
ListLen int // for RangeStmt only
ListIndex int // for RangeStmt only
NextItem *MapListItem // fpr RangeStmt w/ maps only
StrLen int // for RangeStmt w/ strings only
StrIndex int // for RangeStmt w/ strings only
NextRune rune // for RangeStmt w/ strings only
}
func (s *bodyStmt) PopActiveStmt() (as Stmt) {
as = s.Active
s.Active = nil
return
}
func (s *bodyStmt) String() string {
next := ""
if s.NextBodyIndex < 0 {
next = "(init)"
} else if s.NextBodyIndex == len(s.Body) {
next = "(end)"
} else {
next = s.Body[s.NextBodyIndex].String()
}
active := ""
if s.Active != nil {
if s.NextBodyIndex < 0 || s.NextBodyIndex == len(s.Body) {
// none
} else if s.Body[s.NextBodyIndex-1] == s.Active {
active = "*"
} else {
active = fmt.Sprintf(" unexpected active: %v", s.Active)
}
}
return fmt.Sprintf("bodyStmt[%d/%d/%d]=%s%s",
s.ListLen,
s.ListIndex,
s.NextBodyIndex,
next,
active)
}
//----------------------------------------
// Simple Statement
// NOTE: SimpleStmt is not used in nodes due to itable conversion costs.
//
// These are used in if, switch, and for statements for simple
// initialization. The only allowed types are EmptyStmt, ExprStmt,
// SendStmt, IncDecStmt, and AssignStmt.
type SimpleStmt interface {
Stmt
assertSimpleStmt()
}
// non-pointer receiver to help make immutable.
func (*EmptyStmt) assertSimpleStmt() {}
func (*ExprStmt) assertSimpleStmt() {}
func (*SendStmt) assertSimpleStmt() {}
func (*IncDecStmt) assertSimpleStmt() {}
func (*AssignStmt) assertSimpleStmt() {}
//----------------------------------------
// Decl
type Decl interface {
Node
GetDeclNames() []Name
assertDecl()
}
type Decls []Decl
// non-pointer receiver to help make immutable.
func (_ *FuncDecl) assertDecl() {}
func (_ *ImportDecl) assertDecl() {}
func (_ *ValueDecl) assertDecl() {}
func (_ *TypeDecl) assertDecl() {}
var _ Decl = &FuncDecl{}
var _ Decl = &ImportDecl{}
var _ Decl = &ValueDecl{}
var _ Decl = &TypeDecl{}
type FuncDecl struct {
Attributes
StaticBlock
NameExpr
IsMethod bool
Recv FieldTypeExpr // receiver (if method); or empty (if function)
Type FuncTypeExpr // function signature: parameters and results
Body // function body; or empty for external (non-Go) function
}
func (fd *FuncDecl) GetDeclNames() []Name {
return []Name{fd.NameExpr.Name}
}
type ImportDecl struct {
Attributes
NameExpr // local package name, or ".". required.
PkgPath string
}
func (id *ImportDecl) GetDeclNames() []Name {
if id.NameExpr.Name == "." {
return nil // ignore
} else {
return []Name{id.NameExpr.Name}
}
}
type ValueDecl struct {
Attributes
NameExprs
Type Expr // value type; or nil
Values Exprs // initial value; or nil (unless const).
Const bool
}
func (vd *ValueDecl) GetDeclNames() []Name {
ns := make([]Name, 0, len(vd.NameExprs))
for _, nx := range vd.NameExprs {
if nx.Name == "_" {
// ignore
} else {
ns = append(ns, nx.Name)
}
}
return ns
}
type TypeDecl struct {
Attributes
NameExpr
Type Expr // Name, SelectorExpr, StarExpr, or XxxTypes
IsAlias bool // type alias since Go 1.9
}
func (td *TypeDecl) GetDeclNames() []Name {
if td.NameExpr.Name == "_" {
return nil // ignore
} else {
return []Name{td.NameExpr.Name}
}
}
func HasDeclName(d Decl, n2 Name) bool {
ns := d.GetDeclNames()
for _, n := range ns {
if n == n2 {
return true
}
}
return false
}
//----------------------------------------
// SimpleDeclStmt
//
// These are elements of DeclStmt, and get pushed to m.Stmts.
type SimpleDeclStmt interface {
Decl
Stmt
assertSimpleDeclStmt()
}
// not used to avoid itable costs.
// type SimpleDeclStmts []SimpleDeclStmt
func (_ *ValueDecl) assertSimpleDeclStmt() {}
func (_ *TypeDecl) assertSimpleDeclStmt() {}
func (_ *ValueDecl) assertStmt() {}
func (_ *TypeDecl) assertStmt() {}
var _ SimpleDeclStmt = &ValueDecl{}