-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
1983 lines (1823 loc) · 57.6 KB
/
decode.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
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bapi
import (
"fmt"
"reflect"
"strings"
"github.com/golang/protobuf/proto"
"github.com/google/gapid/core/data/protoutil"
"github.com/google/gapid/core/text/parse/cst"
"github.com/google/gapid/gapil/ast"
"github.com/google/gapid/gapil/semantic"
)
type decoder struct {
content Content
inst decoderInstances
toSort []semantic.Owner
err error
}
type decoderInstances struct {
API []*semantic.API
Abort []*semantic.Abort
Annotation []*semantic.Annotation
ArrayAssign []*semantic.ArrayAssign
ArrayIndex []*semantic.ArrayIndex
ArrayInit []*semantic.ArrayInitializer
Assert []*semantic.Assert
Assign []*semantic.Assign
BinaryOp []*semantic.BinaryOp
BitTest []*semantic.BitTest
Block []*semantic.Block
Branch []*semantic.Branch
Call []*semantic.Call
Callable []*semantic.Callable
Case []*semantic.Case
Cast []*semantic.Cast
Choice []*semantic.Choice
Class []*semantic.Class
ClassInit []*semantic.ClassInitializer
Clone []*semantic.Clone
Copy []*semantic.Copy
Create []*semantic.Create
DeclareLocal []*semantic.DeclareLocal
Definition []*semantic.Definition
Enum []*semantic.Enum
EnumEntry []*semantic.EnumEntry
Fence []*semantic.Fence
Field []*semantic.Field
FieldInit []*semantic.FieldInitializer
Function []*semantic.Function
Global []*semantic.Global
Ignore []*semantic.Ignore
Iteration []*semantic.Iteration
Length []*semantic.Length
Local []*semantic.Local
Make []*semantic.Make
Map []*semantic.Map
MapAssign []*semantic.MapAssign
MapContains []*semantic.MapContains
MapIndex []*semantic.MapIndex
MapIteration []*semantic.MapIteration
MapRemove []*semantic.MapRemove
MapClear []*semantic.MapClear
Member []*semantic.Member
MessageValue []*semantic.MessageValue
Observed []*semantic.Observed
Parameter []*semantic.Parameter
Pointer []*semantic.Pointer
PointerRange []*semantic.PointerRange
Pseudonym []*semantic.Pseudonym
Read []*semantic.Read
Reference []*semantic.Reference
Return []*semantic.Return
Select []*semantic.Select
Signature []*semantic.Signature
Slice []*semantic.Slice
SliceAssign []*semantic.SliceAssign
SliceIndex []*semantic.SliceIndex
SliceRange []*semantic.SliceRange
Statement []semantic.Statement
StaticArray []*semantic.StaticArray
StringValue []semantic.StringValue
Switch []*semantic.Switch
UnaryOp []*semantic.UnaryOp
Unknown []*semantic.Unknown
Write []*semantic.Write
ASTAnnotation []*ast.Annotation
ASTAbort []*ast.Abort
ASTAPI []*ast.API
ASTAssign []*ast.Assign
ASTBinaryOp []*ast.BinaryOp
ASTBlock []*ast.Block
ASTBool []*ast.Bool
ASTBranch []*ast.Branch
ASTCall []*ast.Call
ASTCase []*ast.Case
ASTClass []*ast.Class
ASTClear []*ast.Clear
ASTDeclareLocal []*ast.DeclareLocal
ASTDefault []*ast.Default
ASTDefinition []*ast.Definition
ASTDelete []*ast.Delete
ASTEnum []*ast.Enum
ASTEnumEntry []*ast.EnumEntry
ASTFence []*ast.Fence
ASTField []*ast.Field
ASTFunction []*ast.Function
ASTGeneric []*ast.Generic
ASTGroup []*ast.Group
ASTIdentifier []*ast.Identifier
ASTImport []*ast.Import
ASTIndex []*ast.Index
ASTIndexedType []*ast.IndexedType
ASTIteration []*ast.Iteration
ASTMapIteration []*ast.MapIteration
ASTMember []*ast.Member
ASTNamedArg []*ast.NamedArg
ASTNull []*ast.Null
ASTNumber []*ast.Number
ASTParameter []*ast.Parameter
ASTPointerType []*ast.PointerType
ASTPreConst []*ast.PreConst
ASTPseudonym []*ast.Pseudonym
ASTReturn []*ast.Return
ASTString []*ast.String
ASTSwitch []*ast.Switch
ASTUnaryOp []*ast.UnaryOp
ASTUnknown []*ast.Unknown
CSTBranch []*cst.Branch
CSTLeaf []*cst.Leaf
CSTSource []*cst.Source
}
func (i *decoderInstances) build(p *Instances) {
toProtoName := func(s string) string {
s = strings.Replace(s, "AST", "Ast", -1)
s = strings.Replace(s, "API", "Api", -1)
s = strings.Replace(s, "CST", "Cst", -1)
return s
}
iV, pV := reflect.ValueOf(i).Elem(), reflect.ValueOf(p).Elem()
for i, c := 0, iV.NumField(); i < c; i++ {
name := toProtoName(iV.Type().Field(i).Name)
pf := pV.FieldByName(name)
if !pf.IsValid() {
panic(fmt.Errorf("Proto Instances did not have a field with name %v", name))
}
count := pf.Len()
iV.Field(i).Set(reflect.MakeSlice(iV.Field(i).Type(), count, count))
}
}
// Decode deserializes the APIs from data.
func Decode(data []byte) ([]*semantic.API, *semantic.Mappings, error) {
d := decoder{}
if err := proto.Unmarshal(data, &d.content); err != nil {
return nil, nil, err
}
d.inst.build(d.content.Instances)
apis := make([]*semantic.API, len(d.content.Apis))
for i, id := range d.content.Apis {
apis[i] = d.api(id)
if d.err != nil {
return nil, nil, d.err
}
}
mappings := d.mappings()
if d.err != nil {
return nil, nil, d.err
}
for _, s := range d.toSort {
s.SortMembers()
}
return apis, mappings, nil
}
func (d *decoder) mappings() *semantic.Mappings {
out := semantic.Mappings{}
for _, semToAST := range d.content.Mappings.SemToAst {
ast := d.astNode(semToAST.Ast)
sem := d.node(semToAST.Sem)
out.Add(ast, sem)
}
for _, astToCST := range d.content.Mappings.AstToCst {
ast := d.astNode(astToCST.Ast)
cst := d.cstNode(astToCST.Cst)
out.AST.Add(ast, cst)
}
return &out
}
func (d *decoder) abort(abortID uint64) (out *semantic.Abort) {
d.build(d.content.Instances.Abort, &d.inst.Abort, abortID, &out,
func(p *Abort, s *semantic.Abort) {
s.AST = d.astAbort(p.Ast)
s.Function = d.function(p.Function)
s.Statement = d.stat(p.Statement)
})
return
}
func (d *decoder) annotation(annotationID uint64) (out *semantic.Annotation) {
d.build(d.content.Instances.Annotation, &d.inst.Annotation, annotationID, &out,
func(p *Annotation, s *semantic.Annotation) {
s.AST = d.astAnnotation(p.Ast)
s.Named = semantic.Named(d.str(p.Name))
if len(p.Arguments) > 0 {
foreach(p.Arguments, d.expr, &out.Arguments)
}
})
return
}
func (d *decoder) annotations(p *Annotations) semantic.Annotations {
if p == nil || len(p.Annotations) == 0 {
return nil
}
out := semantic.Annotations{}
foreach(p.Annotations, d.annotation, &out)
return out
}
func (d *decoder) assert(assertID uint64) (out *semantic.Assert) {
d.build(d.content.Instances.Assert, &d.inst.Assert, assertID, &out,
func(p *Assert, s *semantic.Assert) {
s.AST = d.astCall(p.Ast)
s.Condition = d.expr(p.Condition)
s.Message = d.str(p.Message)
})
return
}
func (d *decoder) api(apiID uint64) (out *semantic.API) {
d.build(d.content.Instances.Api, &d.inst.API, apiID, &out,
func(p *API, s *semantic.API) {
s.Named = semantic.Named(d.str(p.Name))
foreach(p.Enums, d.enum, &s.Enums)
foreach(p.Definitions, d.definition, &s.Definitions)
foreach(p.Classes, d.class, &s.Classes)
foreach(p.Pseudonyms, d.pseudonym, &s.Pseudonyms)
foreach(p.Externs, d.function, &s.Externs)
foreach(p.Subroutines, d.function, &s.Subroutines)
foreach(p.Functions, d.function, &s.Functions)
foreach(p.Globals, d.global, &s.Globals)
foreach(p.StaticArrays, d.array, &s.StaticArrays)
foreach(p.Maps, d.map_, &s.Maps)
foreach(p.Pointers, d.pointer, &s.Pointers)
foreach(p.Slices, d.slice, &s.Slices)
foreach(p.References, d.reference, &s.References)
foreach(p.Signatures, d.signature, &s.Signatures)
s.Index = semantic.Uint8Value(p.Index)
d.toSort = append(d.toSort, s)
})
return
}
func (d *decoder) array(arrayID uint64) (out *semantic.StaticArray) {
d.build(d.content.Instances.StaticArray, &d.inst.StaticArray, arrayID, &out,
func(p *StaticArray, s *semantic.StaticArray) {
s.Named = semantic.Named(d.str(p.Name))
s.ValueType = d.ty(p.ValueType)
s.Size = uint32(p.Size)
s.SizeExpr = d.expr(p.SizeExpr)
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
})
return
}
func (d *decoder) arrayAssign(arrayAssignID uint64) (out *semantic.ArrayAssign) {
d.build(d.content.Instances.ArrayAssign, &d.inst.ArrayAssign, arrayAssignID, &out,
func(p *ArrayAssign, s *semantic.ArrayAssign) {
s.AST = d.astAssign(p.Ast)
s.To = d.expr(p.To).(*semantic.ArrayIndex)
s.Operator = d.str(p.Operator)
s.Value = d.expr(p.Value)
})
return
}
func (d *decoder) assign(assignID uint64) (out *semantic.Assign) {
d.build(d.content.Instances.Assign, &d.inst.Assign, assignID, &out,
func(p *Assign, s *semantic.Assign) {
s.AST = d.astAssign(p.Ast)
s.LHS = d.expr(p.Lhs)
s.Operator = d.str(p.Operator)
s.RHS = d.expr(p.Rhs)
})
return
}
func (d *decoder) arrayInit(arrayInitID uint64) (out *semantic.ArrayInitializer) {
d.build(d.content.Instances.ArrayInit, &d.inst.ArrayInit, arrayInitID, &out,
func(p *ArrayInitializer, s *semantic.ArrayInitializer) {
s.AST = d.astCall(p.Ast)
s.Array = d.ty(p.Array)
foreach(p.Values, d.expr, &s.Values)
})
return
}
func (d *decoder) arrayIndex(arrayIndexID uint64) (out *semantic.ArrayIndex) {
d.build(d.content.Instances.ArrayIndex, &d.inst.ArrayIndex, arrayIndexID, &out,
func(p *ArrayIndex, s *semantic.ArrayIndex) {
s.AST = d.astIndex(p.Ast)
s.Type = d.array(p.Type)
s.Array = d.expr(p.Array)
s.Index = d.expr(p.Index)
})
return
}
func (d *decoder) binaryOp(binaryOpID uint64) (out *semantic.BinaryOp) {
d.build(d.content.Instances.BinaryOp, &d.inst.BinaryOp, binaryOpID, &out,
func(p *BinaryOp, s *semantic.BinaryOp) {
s.AST = d.astBinaryOp(p.Ast)
s.Type = d.ty(p.Type)
s.LHS = d.expr(p.Lhs)
s.Operator = d.str(p.Operator)
s.RHS = d.expr(p.Rhs)
})
return
}
func (d *decoder) bitTest(bitTestID uint64) (out *semantic.BitTest) {
d.build(d.content.Instances.BitTest, &d.inst.BitTest, bitTestID, &out,
func(p *BitTest, s *semantic.BitTest) {
s.AST = d.astBinaryOp(p.Ast)
s.Bitfield = d.expr(p.Bitfield)
s.Bits = d.expr(p.Bits)
})
return
}
func (d *decoder) branch(branchID uint64) (out *semantic.Branch) {
d.build(d.content.Instances.Branch, &d.inst.Branch, branchID, &out,
func(p *Branch, s *semantic.Branch) {
s.AST = d.astBranch(p.Ast)
s.Condition = d.expr(p.Condition)
s.True = d.block(p.True)
s.False = d.block(p.False)
})
return
}
func (d *decoder) boolValue(boolValueID uint64) semantic.BoolValue {
return semantic.BoolValue(d.content.Instances.BoolValue[boolValueID-1].Value)
}
func (d *decoder) block(blockID uint64) (out *semantic.Block) {
d.build(d.content.Instances.Block, &d.inst.Block, blockID, &out,
func(p *Block, s *semantic.Block) {
s.AST = d.astBlock(p.Ast)
foreach(p.Statements, d.stat, &s.Statements)
})
return
}
func (d *decoder) builtin(p Builtin) *semantic.Builtin {
switch p {
case Builtin_AnyType:
return semantic.AnyType
case Builtin_BoolType:
return semantic.BoolType
case Builtin_CharType:
return semantic.CharType
case Builtin_Float32Type:
return semantic.Float32Type
case Builtin_Float64Type:
return semantic.Float64Type
case Builtin_IntType:
return semantic.IntType
case Builtin_Int8Type:
return semantic.Int8Type
case Builtin_Int16Type:
return semantic.Int16Type
case Builtin_Int32Type:
return semantic.Int32Type
case Builtin_Int64Type:
return semantic.Int64Type
case Builtin_MessageType:
return semantic.MessageType
case Builtin_SizeType:
return semantic.SizeType
case Builtin_StringType:
return semantic.StringType
case Builtin_UintType:
return semantic.UintType
case Builtin_Uint8Type:
return semantic.Uint8Type
case Builtin_Uint16Type:
return semantic.Uint16Type
case Builtin_Uint32Type:
return semantic.Uint32Type
case Builtin_Uint64Type:
return semantic.Uint64Type
case Builtin_VoidType:
return semantic.VoidType
default:
panic(fmt.Errorf("Unhandled builtin %v", p))
}
}
func (d *decoder) call(callID uint64) (out *semantic.Call) {
d.build(d.content.Instances.Call, &d.inst.Call, callID, &out,
func(p *Call, s *semantic.Call) {
s.AST = d.astCall(p.Ast)
s.Target = d.callable(p.Target)
s.Type = d.ty(p.Type)
foreach(p.Arguments, d.expr, &s.Arguments)
})
return
}
func (d *decoder) callable(callableID uint64) (out *semantic.Callable) {
d.build(d.content.Instances.Callable, &d.inst.Callable, callableID, &out,
func(p *Callable, s *semantic.Callable) {
s.Function = d.function(p.Function)
s.Object = d.expr(p.Object)
})
return
}
func (d *decoder) case_(caseID uint64) (out *semantic.Case) {
d.build(d.content.Instances.Case, &d.inst.Case, caseID, &out,
func(p *Case, s *semantic.Case) {
s.AST = d.astCase(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Block = d.block(p.Block)
foreach(p.Conditions, d.expr, &s.Conditions)
})
return
}
func (d *decoder) cast(castID uint64) (out *semantic.Cast) {
d.build(d.content.Instances.Cast, &d.inst.Cast, castID, &out,
func(p *Cast, s *semantic.Cast) {
s.AST = d.astCall(p.Ast)
s.Object = d.expr(p.Object)
s.Type = d.ty(p.Type)
})
return
}
func (d *decoder) choice(choiceID uint64) (out *semantic.Choice) {
d.build(d.content.Instances.Choice, &d.inst.Choice, choiceID, &out,
func(p *Choice, s *semantic.Choice) {
s.AST = d.astCase(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Expression = d.expr(p.Expression)
foreach(p.Conditions, d.expr, &s.Conditions)
})
return
}
func (d *decoder) class(classID uint64) (out *semantic.Class) {
d.build(d.content.Instances.Class, &d.inst.Class, classID, &out,
func(p *Class, s *semantic.Class) {
s.AST = d.astClass(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Named = semantic.Named(d.str(p.Name))
s.Docs = d.docs(p.Docs)
foreach(p.Fields, d.field, &s.Fields)
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
d.toSort = append(d.toSort, s)
})
return
}
func (d *decoder) classInit(classInitID uint64) (out *semantic.ClassInitializer) {
d.build(d.content.Instances.ClassInit, &d.inst.ClassInit, classInitID, &out,
func(p *ClassInitializer, s *semantic.ClassInitializer) {
s.AST = d.astCall(p.Ast)
s.Class = d.class(p.Class)
foreach(p.Fields, d.fieldInit, &s.Fields)
})
return
}
func (d *decoder) clone(cloneID uint64) (out *semantic.Clone) {
d.build(d.content.Instances.Clone, &d.inst.Clone, cloneID, &out,
func(p *Clone, s *semantic.Clone) {
s.AST = d.astCall(p.Ast)
s.Slice = d.expr(p.Slice)
s.Type = d.slice(p.Type)
})
return
}
func (d *decoder) copy(copyID uint64) (out *semantic.Copy) {
d.build(d.content.Instances.Copy, &d.inst.Copy, copyID, &out,
func(p *Copy, s *semantic.Copy) {
s.AST = d.astCall(p.Ast)
s.Src = d.expr(p.Src)
s.Dst = d.expr(p.Dst)
})
return
}
func (d *decoder) create(createID uint64) (out *semantic.Create) {
d.build(d.content.Instances.Create, &d.inst.Create, createID, &out,
func(p *Create, s *semantic.Create) {
s.AST = d.astCall(p.Ast)
s.Type = d.reference(p.Type)
s.Initializer = d.classInit(p.Initializer)
})
return
}
func (d *decoder) declareLocal(declareLocalID uint64) (out *semantic.DeclareLocal) {
d.build(d.content.Instances.DeclareLocal, &d.inst.DeclareLocal, declareLocalID, &out,
func(p *DeclareLocal, s *semantic.DeclareLocal) {
s.AST = d.astDeclareLocal(p.Ast)
s.Local = d.local(p.Local)
})
return
}
func (d *decoder) definition(definitionID uint64) (out *semantic.Definition) {
d.build(d.content.Instances.Definition, &d.inst.Definition, definitionID, &out,
func(p *Definition, s *semantic.Definition) {
s.AST = d.astDefinition(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Named = semantic.Named(d.str(p.Name))
s.Docs = d.docs(p.Docs)
s.Expression = d.expr(p.Expression)
})
return
}
func (d *decoder) docs(p *Documentation) semantic.Documentation {
s := semantic.Documentation{}
foreach(p.Strings, d.str, &s)
return s
}
func (d *decoder) enum(enumID uint64) (out *semantic.Enum) {
d.build(d.content.Instances.Enum, &d.inst.Enum, enumID, &out,
func(p *Enum, s *semantic.Enum) {
s.AST = d.astEnum(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Named = semantic.Named(d.str(p.Name))
s.Docs = d.docs(p.Docs)
s.IsBitfield = p.IsBitfield
s.NumberType = d.ty(p.NumberType)
foreach(p.Entries, d.enumEntry, &s.Entries)
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
d.toSort = append(d.toSort, s)
})
return
}
func (d *decoder) enumEntry(enumEntryID uint64) (out *semantic.EnumEntry) {
d.build(d.content.Instances.EnumEntry, &d.inst.EnumEntry, enumEntryID, &out,
func(p *EnumEntry, s *semantic.EnumEntry) {
s.AST = d.astEnumEntry(p.Ast)
s.Named = semantic.Named(d.str(p.Name))
s.Docs = d.docs(p.Docs)
s.Value = d.expr(p.Value)
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
})
return
}
func (d *decoder) fence(fenceID uint64) (out *semantic.Fence) {
d.build(d.content.Instances.Fence, &d.inst.Fence, fenceID, &out,
func(p *Fence, s *semantic.Fence) {
s.AST = d.astFence(p.Ast)
s.Statement = d.stat(p.Statement)
s.Explicit = p.Explicit
})
return
}
func (d *decoder) float32Value(float32ValueID uint64) semantic.Float32Value {
return semantic.Float32Value(d.content.Instances.Float32Value[float32ValueID-1].Value)
}
func (d *decoder) float64Value(float64ValueID uint64) semantic.Float64Value {
return semantic.Float64Value(d.content.Instances.Float64Value[float64ValueID-1].Value)
}
func (d *decoder) expr(exprID uint64) semantic.Expression {
if exprID == 0 {
return nil
}
exprIdx := exprID - 1
switch p := protoutil.OneOf(d.content.Instances.Expression[exprIdx]).(type) {
case *Expression_ArrayIndex:
return d.arrayIndex(p.ArrayIndex)
case *Expression_ArrayInitializer:
return d.arrayInit(p.ArrayInitializer)
case *Expression_BinaryOp:
return d.binaryOp(p.BinaryOp)
case *Expression_BitTest:
return d.bitTest(p.BitTest)
case *Expression_BoolValue:
return d.boolValue(p.BoolValue)
case *Expression_Call:
return d.call(p.Call)
case *Expression_Cast:
return d.cast(p.Cast)
case *Expression_ClassInit:
return d.classInit(p.ClassInit)
case *Expression_Clone:
return d.clone(p.Clone)
case *Expression_Create:
return d.create(p.Create)
case *Expression_Definition:
return d.definition(p.Definition)
case *Expression_EnumEntry:
return d.enumEntry(p.EnumEntry)
case *Expression_Field:
return d.field(p.Field)
case *Expression_Float32Value:
return d.float32Value(p.Float32Value)
case *Expression_Float64Value:
return d.float64Value(p.Float64Value)
case *Expression_Global:
return d.global(p.Global)
case *Expression_Ignore:
return d.ignore(p.Ignore)
case *Expression_Int8Value:
return d.int8Value(p.Int8Value)
case *Expression_Int16Value:
return d.int16Value(p.Int16Value)
case *Expression_Int32Value:
return d.int32Value(p.Int32Value)
case *Expression_Int64Value:
return d.int64Value(p.Int64Value)
case *Expression_Length:
return d.length(p.Length)
case *Expression_Local:
return d.local(p.Local)
case *Expression_Make:
return d.make(p.Make)
case *Expression_MapContains:
return d.mapContains(p.MapContains)
case *Expression_MapIndex:
return d.mapIndex(p.MapIndex)
case *Expression_Member:
return d.member(p.Member)
case *Expression_MessageValue:
return d.messageValue(p.MessageValue)
case *Expression_Null:
return d.null(p.Null)
case *Expression_Observed:
return d.observed(p.Observed)
case *Expression_Parameter:
return d.param(p.Parameter)
case *Expression_PointerRange:
return d.pointerRange(p.PointerRange)
case *Expression_Select:
return d.select_(p.Select)
case *Expression_SliceIndex:
return d.sliceIndex(p.SliceIndex)
case *Expression_SliceRange:
return d.sliceRange(p.SliceRange)
case *Expression_StringValue:
return d.stringValue(p.StringValue)
case *Expression_Uint8Value:
return d.uint8Value(p.Uint8Value)
case *Expression_Uint16Value:
return d.uint16Value(p.Uint16Value)
case *Expression_Uint32Value:
return d.uint32Value(p.Uint32Value)
case *Expression_Uint64Value:
return d.uint64Value(p.Uint64Value)
case *Expression_UnaryOp:
return d.unaryOp(p.UnaryOp)
case *Expression_Unknown:
return d.unknown(p.Unknown)
default:
panic(fmt.Errorf("Unhandled expression type %T", p))
}
}
func (d *decoder) field(fieldID uint64) (out *semantic.Field) {
d.build(d.content.Instances.Field, &d.inst.Field, fieldID, &out,
func(p *Field, s *semantic.Field) {
s.AST = d.astField(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Named = semantic.Named(d.str(p.Name))
s.Docs = d.docs(p.Docs)
s.Type = d.ty(p.Type)
s.Default = d.expr(p.Default)
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
})
return
}
func (d *decoder) fieldInit(fieldInitID uint64) (out *semantic.FieldInitializer) {
d.build(d.content.Instances.FieldInit, &d.inst.FieldInit, fieldInitID, &out,
func(p *FieldInitializer, s *semantic.FieldInitializer) {
s.AST = d.astNode(p.Ast)
s.Field = d.field(p.Field)
s.Value = d.expr(p.Value)
})
return
}
func (d *decoder) function(funcID uint64) (out *semantic.Function) {
d.build(d.content.Instances.Function, &d.inst.Function, funcID, &out,
func(p *Function, s *semantic.Function) {
s.AST = d.astFunction(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Named = semantic.Named(d.str(p.Name))
s.Docs = d.docs(p.Docs)
s.Return = d.param(p.Return)
s.This = d.param(p.This)
foreach(p.FullParameters, d.param, &s.FullParameters)
s.Block = d.block(p.Block)
s.Signature = d.signature(p.Signature)
s.Extern = p.Extern
s.Subroutine = p.Subroutine
s.Recursive = p.Recursive
if p.Order.Resolved {
s.Order |= semantic.Resolved
}
if p.Order.Pre {
s.Order |= semantic.Pre
}
if p.Order.Post {
s.Order |= semantic.Post
}
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
})
return
}
func (d *decoder) global(globalID uint64) (out *semantic.Global) {
d.build(d.content.Instances.Global, &d.inst.Global, globalID, &out,
func(p *Global, s *semantic.Global) {
s.AST = d.astField(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Named = semantic.Named(d.str(p.Name))
s.Type = d.ty(p.Type)
s.Default = d.expr(p.Default)
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
})
return
}
func (d *decoder) ignore(ignoreID uint64) (out *semantic.Ignore) {
d.build(d.content.Instances.Ignore, &d.inst.Ignore, ignoreID, &out,
func(p *Ignore, s *semantic.Ignore) {
s.AST = d.astNode(p.Ast)
})
return
}
func (d *decoder) int8Value(int8ValueID uint64) semantic.Int8Value {
return semantic.Int8Value(d.content.Instances.Int8Value[int8ValueID-1].Value)
}
func (d *decoder) int16Value(int16ValueID uint64) semantic.Int16Value {
return semantic.Int16Value(d.content.Instances.Int16Value[int16ValueID-1].Value)
}
func (d *decoder) int32Value(int32ValueID uint64) semantic.Int32Value {
return semantic.Int32Value(d.content.Instances.Int32Value[int32ValueID-1].Value)
}
func (d *decoder) int64Value(int64ValueID uint64) semantic.Int64Value {
return semantic.Int64Value(d.content.Instances.Int64Value[int64ValueID-1].Value)
}
func (d *decoder) iteration(iterationID uint64) (out *semantic.Iteration) {
d.build(d.content.Instances.Iteration, &d.inst.Iteration, iterationID, &out,
func(p *Iteration, s *semantic.Iteration) {
s.AST = d.astIteration(p.Ast)
s.Iterator = d.local(p.Iterator)
s.From = d.expr(p.From)
s.To = d.expr(p.To)
s.Block = d.block(p.Block)
})
return
}
func (d *decoder) length(lengthID uint64) (out *semantic.Length) {
d.build(d.content.Instances.Length, &d.inst.Length, lengthID, &out,
func(p *Length, s *semantic.Length) {
s.AST = d.astCall(p.Ast)
s.Object = d.expr(p.Object)
s.Type = d.ty(p.Type)
})
return
}
func (d *decoder) local(localID uint64) (out *semantic.Local) {
d.build(d.content.Instances.Local, &d.inst.Local, localID, &out,
func(p *Local, s *semantic.Local) {
if p.Declaration != 0 {
s.Declaration = d.stat(p.Declaration).(*semantic.DeclareLocal)
}
s.Type = d.ty(p.Type)
s.Named = semantic.Named(d.str(p.Name))
s.Value = d.expr(p.Value)
})
return
}
func (d *decoder) make(makeID uint64) (out *semantic.Make) {
d.build(d.content.Instances.Make, &d.inst.Make, makeID, &out,
func(p *Make, s *semantic.Make) {
s.AST = d.astCall(p.Ast)
s.Type = d.slice(p.Type)
s.Size = d.expr(p.Size)
})
return
}
func (d *decoder) map_(mapID uint64) (out *semantic.Map) {
d.build(d.content.Instances.Map, &d.inst.Map, mapID, &out,
func(p *Map, s *semantic.Map) {
s.Named = semantic.Named(d.str(p.Name))
s.KeyType = d.ty(p.KeyType)
s.ValueType = d.ty(p.ValueType)
s.Dense = p.Dense
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
d.toSort = append(d.toSort, s)
})
return
}
func (d *decoder) mapAssign(mapAssignID uint64) (out *semantic.MapAssign) {
d.build(d.content.Instances.MapAssign, &d.inst.MapAssign, mapAssignID, &out,
func(p *MapAssign, s *semantic.MapAssign) {
s.AST = d.astAssign(p.Ast)
s.Operator = d.str(p.Operator)
s.To = d.expr(p.To).(*semantic.MapIndex)
s.Value = d.expr(p.Value)
})
return
}
func (d *decoder) mapContains(mapContainsID uint64) (out *semantic.MapContains) {
d.build(d.content.Instances.MapContains, &d.inst.MapContains, mapContainsID, &out,
func(p *MapContains, s *semantic.MapContains) {
s.AST = d.astBinaryOp(p.Ast)
s.Type = d.map_(p.Type)
s.Map = d.expr(p.Map)
s.Key = d.expr(p.Key)
})
return
}
func (d *decoder) mapIndex(mapIndexID uint64) (out *semantic.MapIndex) {
d.build(d.content.Instances.MapIndex, &d.inst.MapIndex, mapIndexID, &out,
func(p *MapIndex, s *semantic.MapIndex) {
s.AST = d.astIndex(p.Ast)
s.Type = d.map_(p.Type)
s.Map = d.expr(p.Map)
s.Index = d.expr(p.Index)
})
return
}
func (d *decoder) mapIteration(mapIterationID uint64) (out *semantic.MapIteration) {
d.build(d.content.Instances.MapIteration, &d.inst.MapIteration, mapIterationID, &out,
func(p *MapIteration, s *semantic.MapIteration) {
s.AST = d.astMapIteration(p.Ast)
s.IndexIterator = d.local(p.IndexIterator)
s.KeyIterator = d.local(p.KeyIterator)
s.ValueIterator = d.local(p.ValueIterator)
s.Map = d.expr(p.Map)
s.Block = d.block(p.Block)
})
return
}
func (d *decoder) mapRemove(mapRemoveID uint64) (out *semantic.MapRemove) {
d.build(d.content.Instances.MapRemove, &d.inst.MapRemove, mapRemoveID, &out,
func(p *MapRemove, s *semantic.MapRemove) {
s.AST = d.astDelete(p.Ast)
s.Type = d.map_(p.Type)
s.Map = d.expr(p.Map)
s.Key = d.expr(p.Key)
})
return
}
func (d *decoder) mapClear(mapClearID uint64) (out *semantic.MapClear) {
d.build(d.content.Instances.MapClear, &d.inst.MapClear, mapClearID, &out,
func(p *MapClear, s *semantic.MapClear) {
s.AST = d.astClear(p.Ast)
s.Type = d.map_(p.Type)
s.Map = d.expr(p.Map)
})
return
}
func (d *decoder) member(memberID uint64) (out *semantic.Member) {
d.build(d.content.Instances.Member, &d.inst.Member, memberID, &out,
func(p *Member, s *semantic.Member) {
s.AST = d.astMember(p.Ast)
s.Field = d.field(p.Field)
s.Object = d.expr(p.Object)
})
return
}
func (d *decoder) messageValue(messageValueID uint64) (out *semantic.MessageValue) {
d.build(d.content.Instances.MessageValue, &d.inst.MessageValue, messageValueID, &out,
func(p *MessageValue, s *semantic.MessageValue) {
s.AST = d.astClass(p.Ast)
foreach(p.Arguments, d.fieldInit, &s.Arguments)
})
return
}
func (d *decoder) null(nullID uint64) semantic.Null {
p := d.content.Instances.Null[nullID-1]
return semantic.Null{
AST: d.astNull(p.Ast),
Type: d.ty(p.Type),
}
}
func (d *decoder) observed(observedID uint64) (out *semantic.Observed) {
d.build(d.content.Instances.Observed, &d.inst.Observed, observedID, &out,
func(p *Observed, s *semantic.Observed) {
s.Parameter = d.param(p.Parameter)
})
return
}
func (d *decoder) param(paramID uint64) (out *semantic.Parameter) {
d.build(d.content.Instances.Parameter, &d.inst.Parameter, paramID, &out,
func(p *Parameter, s *semantic.Parameter) {
s.AST = d.astParameter(p.Ast)
s.Annotations = d.annotations(p.Annotations)
s.Function = d.function(p.Function)
s.Named = semantic.Named(d.str(p.Name))
s.Docs = d.docs(p.Docs)
s.Type = d.ty(p.Type)
})
return
}
func (d *decoder) pointer(ptrID uint64) (out *semantic.Pointer) {
d.build(d.content.Instances.Pointer, &d.inst.Pointer, ptrID, &out,
func(p *Pointer, s *semantic.Pointer) {
s.Named = semantic.Named(d.str(p.Name))
s.To = d.ty(p.To)
s.Const = p.Const
s.Slice = d.slice(p.Slice)
if owner := d.node(p.Owner); owner != nil {
semantic.Add(owner.(semantic.Owner), s)
}
})
return
}
func (d *decoder) pointerRange(ptrRangeID uint64) (out *semantic.PointerRange) {
d.build(d.content.Instances.PointerRange, &d.inst.PointerRange, ptrRangeID, &out,
func(p *PointerRange, s *semantic.PointerRange) {
s.AST = d.astIndex(p.Ast)
s.Type = d.slice(p.Type)
s.Pointer = d.expr(p.Pointer)
s.Range = d.expr(p.Range).(*semantic.BinaryOp)