/
types.go
1090 lines (982 loc) · 25.9 KB
/
types.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
// Types
//
// Supports operations on types, such as figuring out the type of a struct
// field or array access. Also figures out the result of a binary expression
// between two types, or a dereference (*) or address-of (&) operation.
//
// Type definitions are fully-defined (Type.final = true), all other types are
// references to them.
package main
import (
"bytes"
"fmt"
"go/ast"
"go/token"
)
var BuiltinTypes map[string]Type
func init() {
BuiltinTypes = make(map[string]Type, 0)
builtin := []string{
"uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64",
"float32", "float64", "complex64", "complex128", "uint", "int", "uintptr",
"rune", "byte", "bool", "nil",
}
for _, ident := range builtin {
BuiltinTypes[ident] = newFinalBaseType(&ast.Ident{Name: ident})
}
ifaceTyp := newStructType(&ast.InterfaceType{
Methods: &ast.FieldList{},
})
ifaceTyp.Complete(nil)
BuiltinTypes["true"] = BuiltinTypes["bool"]
BuiltinTypes["false"] = BuiltinTypes["bool"]
BuiltinTypes["iota"] = BuiltinTypes["int"]
BuiltinTypes["string"] = newCustomIndexedType(BuiltinTypes["byte"], BuiltinTypes["int"], true)
errorTyp := newStructType(nil)
errorTyp.AddMethod("Error", newCustomFuncType(func(args []Type) Type {
return BuiltinTypes["string"]
}).SetParameterAccess(true))
BuiltinTypes["error"] = errorTyp
// builtin functions
BuiltinTypes["len"] = newCustomFuncType(func(args []Type) Type {
return BuiltinTypes["int"]
}).SetParameterAccess(true)
BuiltinTypes["cap"] = BuiltinTypes["len"]
BuiltinTypes["new"] = newCustomFuncType(func(args []Type) Type {
return newPointerTypeFromT(args[0])
}).SetParameterAccess(true)
BuiltinTypes["make"] = newCustomFuncType(func(args []Type) Type {
return args[0]
}).SetParameterAccess(true)
BuiltinTypes["append"] = newCustomFuncType(func(args []Type) Type {
return args[0]
}).SetParameterAccess(false)
BuiltinTypes["copy"] = newCustomFuncType(func(args []Type) Type {
return BuiltinTypes["int"]
}).SetParameterAccess(false, true)
BuiltinTypes["close"] = newCustomFuncType(func(args []Type) Type {
return nil
}).SetParameterAccess(false)
BuiltinTypes["delete"] = newCustomFuncType(func(args []Type) Type {
return nil
}).SetParameterAccess(false, true)
BuiltinTypes["complex"] = newCustomFuncType(func(args []Type) Type {
width := args[0].Definition().(*ast.Ident).Name[len("float"):]
return BuiltinTypes["complex"+width]
}).SetParameterAccess(true)
BuiltinTypes["real"] = newCustomFuncType(func(args []Type) Type {
width := args[0].Definition().(*ast.Ident).Name[len("complex"):]
return BuiltinTypes["float"+width]
}).SetParameterAccess(true)
BuiltinTypes["imag"] = BuiltinTypes["real"]
BuiltinTypes["panic"] = newCustomFuncType(func(args []Type) Type {
return nil
}).SetParameterAccess(false) // the interface value passed could be changed
BuiltinTypes["recover"] = newCustomFuncType(func(args []Type) Type {
return ifaceTyp
}).SetParameterAccess(true)
BuiltinTypes["print"] = newCustomFuncType(func(args []Type) Type {
return nil
}).SetParameterAccess(true)
// builtin packages
// unsafe
//BuiltinTypes["unsafe"] =
// reflect
//BuiltinTypes["reflect"] =
}
type Type interface {
Complete(Resolver)
Definition() ast.Node
Fields() []string // return an ordered list of all fields in this type
Field(string) Type // return .field's type
AddMethod(string, *FuncType) // add a new method to this type
Method(string) *FuncType // return .method() type
Dereference() Type // the return type of a *dereference operation
IndexKey() Type // type for [key]
IndexValue() Type // the return type of an [index] or <-chan operation
Call([]Type) Type // the return types of calling this type
Math(Type, token.Token) Type // outcome of any math operation with another type
PassByValue() bool // is this type passed by value or reference?
String() string // a representation of this type
CType() string // type signature (int* for slice)
CDecl() string // type definition (typedef struct {} something)
}
type BaseType struct {
ast.Node // definition node
methods map[string]*FuncType // every type can have a method set
underlying Type // if this type is a direct shadow of an existing type
complete bool // ensure each type is only completed once
}
func newBaseType(node ast.Node) *BaseType {
return &BaseType{Node: node, methods: make(map[string]*FuncType)}
}
func newShadowType(typ Type) *BaseType {
t := newBaseType(nil)
t.underlying = typ
t.complete = true
return t
}
func (t *BaseType) completed() bool {
if !t.complete {
t.complete = true
return false
}
return true
}
func (t *BaseType) Complete(resolver Resolver) {
if t.completed() {
return
}
t.underlying = TypeOfDecl(t.Node, resolver)
return
}
// All methods to go to the underlying type
func (t *BaseType) Definition() ast.Node {
if t.underlying == nil {
return t.Node
}
return t.underlying.Definition()
}
func (t *BaseType) Fields() []string {
if t.underlying != nil {
return t.underlying.Fields()
}
return nil
}
func (t *BaseType) Field(name string) Type {
if t.underlying != nil {
return t.underlying.Field(name)
}
return nil
}
// Methods get added to the current type
func (t *BaseType) AddMethod(name string, f *FuncType) {
t.methods[name] = f
}
func (t *BaseType) Method(name string) *FuncType {
if method, ok := t.methods[name]; ok {
return method
}
if t.underlying != nil {
return t.underlying.Method(name)
}
return nil
}
func (t *BaseType) Dereference() Type {
if t.underlying == nil {
return nil
}
return t.underlying.Dereference()
}
func (t *BaseType) IndexKey() Type {
if t.underlying != nil {
return t.underlying.IndexKey()
}
return nil
}
func (t *BaseType) IndexValue() Type {
if t.underlying != nil {
return t.underlying.IndexValue()
}
return nil
}
func (t *BaseType) Call(args []Type) Type {
return t.underlying.Call(args)
}
// The declaration of the type, such as "typedef Pixel struct {}"
func (t *BaseType) CDecl() string {
return "UNKNOWN DECL"
}
// The name of the type, such as "*Pixel" or "int32[]"
func (t *BaseType) CType() string {
return "UNKNOWN TYPE"
}
func (t *BaseType) Math(other Type, op token.Token) Type {
if t.underlying != nil {
return BinaryOp(t.underlying, op, other)
}
return BinaryOp(t, op, other)
}
func (t *BaseType) PassByValue() bool {
if t.underlying == nil {
return true
}
return t.underlying.PassByValue()
}
func (typ *BaseType) String() string {
var buffer bytes.Buffer
if typ.underlying != nil {
buffer.WriteString("Shadow{")
buffer.WriteString(typ.underlying.String())
buffer.WriteString("}")
} else {
buffer.WriteString("BaseType{")
buffer.WriteString(typ.Node.(*ast.Ident).Name)
buffer.WriteString("} ")
}
buffer.WriteString(typ.MethodSet())
return buffer.String()
}
func (t *BaseType) MethodSet() string {
var buffer bytes.Buffer
if len(t.methods) > 0 {
buffer.WriteString(" methods{")
for k, method := range t.methods {
if method.pointerMethod {
buffer.WriteString("*")
}
buffer.WriteString(k + ",")
}
buffer.WriteString("}")
}
return buffer.String()
}
type FinalBaseType struct {
*BaseType
}
func newFinalBaseType(node ast.Node) *FinalBaseType {
t := &FinalBaseType{
BaseType: newBaseType(node),
}
t.completed()
return t
}
func (t *FinalBaseType) Complete(resolver Resolver) {
// do nothing
return
}
type ConstType struct {
*BaseType
value string
}
func newConstType(node ast.Node) *ConstType {
return &ConstType{
BaseType: newBaseType(node),
}
}
func (t *ConstType) Complete(resolver Resolver) {
if t.completed() {
return
}
switch t.Node.(*ast.BasicLit).Kind {
case token.FLOAT:
t.BaseType.underlying = resolver("float64")
case token.INT:
t.BaseType.underlying = resolver("int")
case token.STRING:
t.BaseType.underlying = resolver("string")
}
t.value = t.Node.(*ast.BasicLit).Value
return
}
func (t *ConstType) String() string {
return fmt.Sprintf("%s=%s", t.BaseType.String(), t.value)
}
// Structs AND Interfaces
type StructType struct {
*BaseType
fieldOrder []string
fields map[string]Type
embedded []Type // could be *StructType or *PointerType (or *BaseType shadow)
iface bool
}
func newStructType(node ast.Node) *StructType {
return &StructType{
BaseType: newBaseType(node),
fieldOrder: make([]string, 0),
fields: make(map[string]Type),
}
}
// fill in all struct fields, but not those carried from embedded structs
func (t *StructType) Complete(resolver Resolver) {
if t.completed() {
return
}
switch e := t.Node.(type) {
case *ast.StructType:
for _, field := range e.Fields.List {
fieldTyp := TypeOfDecl(field.Type, resolver)
// Embedded fields
// *Struct1
// Struct2
// abc.Struct3
if len(field.Names) == 0 {
var ig IdentifierGroup
AccessIdentBuild(&ig, field.Type, nil)
name := ig.group[len(ig.group)-1].id
t.addField(name, fieldTyp)
t.embedded = append(t.embedded, fieldTyp)
} else {
for _, name := range field.Names {
t.addField(name.Name, fieldTyp)
}
}
}
case *ast.InterfaceType:
t.iface = true
for _, method := range e.Methods.List {
switch m := method.Type.(type) {
case *ast.FuncType:
methodTyp := newMethodType(m, nil, t)
methodTyp.Complete(resolver)
methodTyp.pointerMethod = true // all interface methods take pointers
for _, name := range method.Names {
t.AddMethod(name.Name, methodTyp)
}
case *ast.Ident:
// embedded
t.embedded = append(t.embedded, resolver(m.Name))
}
}
}
}
func (t *StructType) addField(name string, typ Type) {
t.fields[name] = typ
t.fieldOrder = append(t.fieldOrder, name)
return
}
func (t *StructType) Fields() []string {
return t.fieldOrder
}
func (t *StructType) Field(name string) Type {
if builtin, ok := t.fields[name]; ok {
return builtin
}
// else search the embedded fields
for _, embedded := range t.embedded {
if field := embedded.Field(name); field != nil {
return field
}
}
return nil
}
func (t *StructType) Method(name string) *FuncType {
if builtin := t.BaseType.Method(name); builtin != nil {
return builtin
}
// else search the embedded methods
for _, embedded := range t.embedded {
if method := embedded.Method(name); method != nil {
return method
}
}
return nil
}
func (t *StructType) String() string {
var buffer bytes.Buffer
if !t.iface {
buffer.WriteString("struct {")
} else {
buffer.WriteString("interface {")
}
for i, field := range t.Fields() {
buffer.WriteString(fmt.Sprintf("%s", field))
if i != len(t.Fields())-1 {
buffer.WriteString(", ")
}
}
buffer.WriteString("}")
if len(t.embedded) > 0 {
buffer.WriteString("embedded {")
for i, e := range t.embedded {
buffer.WriteString(e.String())
if i != len(t.embedded)-1 {
buffer.WriteString(", ")
}
}
buffer.WriteString("}")
}
buffer.WriteString(t.MethodSet())
return buffer.String()
}
// an array, list, map or chan type
type IndexedType struct {
*BaseType
key Type
value Type
byValue bool // is this type passed by value (array) or reference (map, slice)
}
func newIndexedType(node ast.Node) *IndexedType {
return &IndexedType{
BaseType: newBaseType(node),
}
}
func newCustomIndexedType(value Type, key Type, byValue bool) *IndexedType {
t := newIndexedType(nil)
t.key = key
t.value = value
t.byValue = byValue
return t
}
// fill in key and value sections
func (typ *IndexedType) Complete(resolver Resolver) {
if typ.completed() {
return
}
switch t := typ.Node.(type) {
case *ast.ArrayType:
if t.Len != nil {
typ.byValue = true // array
} else {
typ.byValue = false // slice
}
typ.key = BuiltinTypes["int"]
typ.value = TypeOfDecl(t.Elt, resolver)
case *ast.MapType:
typ.byValue = false
typ.key = TypeOfDecl(t.Key, resolver)
typ.value = TypeOfDecl(t.Value, resolver)
case *ast.ChanType:
typ.byValue = false
typ.value = TypeOfDecl(t.Value, resolver)
}
return
}
func (t *IndexedType) IndexKey() Type {
return t.key
}
// IndexValue will return a MultiType for the val, ok := map[key] expressions
func (t *IndexedType) IndexValue() Type {
return newMultiType(t.value, BuiltinTypes["bool"])
}
func (t *IndexedType) PassByValue() bool {
return t.byValue
}
func (typ *IndexedType) String() string {
var buffer bytes.Buffer
switch t := typ.Node.(type) {
case *ast.ArrayType:
buffer.WriteString("[")
if t.Len != nil {
buffer.WriteString(typ.IndexKey().String())
}
buffer.WriteString("]")
case *ast.MapType:
buffer.WriteString("map[")
buffer.WriteString(typ.IndexKey().String())
buffer.WriteString("]")
case *ast.ChanType:
if t.Dir == ast.SEND {
buffer.WriteString("->")
} else if t.Dir == ast.RECV {
buffer.WriteString("<-")
}
buffer.WriteString("chan ")
default:
buffer.WriteString("custom[")
buffer.WriteString(typ.IndexKey().String())
buffer.WriteString("]")
}
buffer.WriteString(typ.value.String())
buffer.WriteString(typ.MethodSet())
return buffer.String()
}
// a pointer type
type PointerType struct {
*BaseType
inner Type
}
func newPointerType(node ast.Node) *PointerType {
return &PointerType{
BaseType: newBaseType(node),
}
}
func newPointerTypeFromT(typ Type) *PointerType {
return &PointerType{
BaseType: newBaseType(nil),
inner: typ,
}
}
func (t *PointerType) PassByValue() bool {
return false
}
// Resolve the inner type
func (t *PointerType) Complete(resolver Resolver) {
if t.completed() {
return
}
expr := t.Node.(*ast.StarExpr).X
t.inner = TypeOfDecl(expr, resolver)
if t.inner == nil {
panic(fmt.Sprintf("Couldn't resolve pointer value %+v", expr))
}
return
}
func (t *PointerType) Dereference() Type {
return t.inner
}
// Methods all go to the inner type
func (t *PointerType) AddMethod(name string, f *FuncType) {
t.inner.AddMethod(name, f)
}
func (t *PointerType) Method(name string) *FuncType {
// check both T* and T
return t.inner.Method(name)
}
func (t *PointerType) Fields() []string {
return t.inner.Fields()
}
func (t *PointerType) Field(name string) Type {
return t.inner.Field(name)
}
func (t *PointerType) String() string {
var buffer bytes.Buffer
buffer.WriteString("*")
buffer.WriteString(t.inner.String())
return buffer.String()
}
// a function
type FuncType struct {
*BaseType
params []Type
results []Type
ellipsis bool // is the last argument in this function an ellipsis?
method, pointerMethod bool
receiver Type // if method == true
name string // if we know the name of this function
body *ast.BlockStmt // the actual declaration, if possible; not there for builtin/C functions
typ *ast.FuncType
customCall func([]Type) Type // a custom function for builtin magic (make, new, unsafe.*, etc)
noWriteMask []bool // an optional mask for functions without bodies, true means the arg wasn't written to
notSafe bool // is this function not safe to parallelize?
}
// decl [optional], but used as the defining node in a BasicBlock, so it must
// be returned from BaseType.Definition(). If decl is nil, the function will be
// marked as not safe
func newFuncType(typ *ast.FuncType, decl *ast.FuncDecl) *FuncType {
t := &FuncType{
BaseType: newBaseType(decl),
typ: typ,
}
if decl != nil {
t.name = decl.Name.Name
t.body = decl.Body
} else {
t.notSafe = true
}
return t
}
func newMethodType(typ *ast.FuncType, decl *ast.FuncDecl, recv Type) *FuncType {
funcTyp := newFuncType(typ, decl)
funcTyp.method = true
funcTyp.pointerMethod = recv.Dereference() != nil
funcTyp.receiver = recv
return funcTyp
}
func newCustomFuncType(f func([]Type) Type) *FuncType {
t := newFuncType(nil, nil)
t.customCall = f
t.complete = true
t.notSafe = false // custom functions are safe
return t
}
// fill in params and results
func (t *FuncType) Complete(resolver Resolver) {
if t.completed() {
return
}
expr := t.typ
if expr.Params != nil {
for _, arg := range expr.Params.List {
argType := TypeOfDecl(arg.Type, resolver)
// no name args
i := len(arg.Names)
if i == 0 {
i = 1
}
for j := 0; j < i; j++ {
t.params = append(t.params, argType)
}
if _, ellipsis := arg.Type.(*ast.Ellipsis); ellipsis {
t.ellipsis = true
}
}
}
if expr.Results != nil {
for _, result := range expr.Results.List {
resultType := TypeOfDecl(result.Type, resolver)
i := len(result.Names)
if i == 0 {
i = 1
}
for j := 0; j < i; j++ {
t.results = append(t.results, resultType)
}
}
}
return
}
func (t *FuncType) Call(args []Type) Type {
if t.customCall != nil {
return t.customCall(args)
} else {
if *verbose {
fmt.Println("CALL", t.results)
}
if len(t.results) > 1 {
return newMultiType(t.results...)
} else {
return t.results[0]
}
}
return nil
}
func (t *FuncType) SetParameterAccess(mask ...bool) *FuncType {
t.noWriteMask = mask
return t // allow method chaining
}
// Return true if this parameter is pass-by-value, false otherwise
func (t *FuncType) GetParameterAccess(index int) bool {
if t.noWriteMask == nil {
if len(t.params) <= index {
return t.params[len(t.params)-1].PassByValue()
}
return t.params[index].PassByValue()
}
if len(t.noWriteMask) <= index {
// return the value of the last argument for "..."" functions
return t.noWriteMask[len(t.noWriteMask)-1]
}
return t.noWriteMask[index]
}
func (t *FuncType) PassByValue() bool {
return false
}
func (t *FuncType) String() string {
var buffer bytes.Buffer
buffer.WriteString("func ")
if t.method {
buffer.WriteString("(")
buffer.WriteString(t.receiver.String())
buffer.WriteString(") ")
}
if len(t.name) > 0 {
buffer.WriteString(t.name)
}
buffer.WriteString("(")
for i, param := range t.params {
//fmt.Printf("%+v\n", t.params)
buffer.WriteString(param.String())
if i < len(t.params)-1 {
buffer.WriteString(", ")
}
}
buffer.WriteString(")")
if len(t.results) > 0 {
buffer.WriteString(" (")
for i, result := range t.results {
buffer.WriteString(result.String())
if i < len(t.results)-1 {
buffer.WriteString(", ")
}
if t.ellipsis && i == len(t.results)-1 {
buffer.WriteString("...")
}
}
buffer.WriteString(")")
}
if t.body == nil {
buffer.WriteString(" <builtin>")
}
return buffer.String()
}
// Represent multiple return values in a single Type
type MultiType struct {
*BaseType
values []Type
}
func newMultiType(values ...Type) *MultiType {
t := &MultiType{
BaseType: newBaseType(values[0].Definition()),
values: values,
}
t.BaseType.underlying = values[0]
return t
}
func (t *MultiType) Expand() []Type {
return t.values
}
// A type that will be resolved in the future through .Complete()
type FutureType struct {
*BaseType
}
func newFutureType() *FutureType {
return &FutureType{
BaseType: newBaseType(nil),
}
}
func (t *FutureType) Finish(base Type) {
if base == nil {
panic("BaseType = nil")
}
t.BaseType.underlying = base
}
type PackageType struct {
*BaseType
resolver Resolver
path string
}
func newPackageType(node ast.Node) Type {
return &PackageType{
BaseType: newBaseType(node),
}
}
func (t *PackageType) Complete(resolver Resolver) {
if t.completed() {
return
}
t.resolver = resolver
t.path = t.Node.(*ast.ImportSpec).Path.Value
}
func (t *PackageType) Field(name string) Type {
// TODO: enforce exported fields only?
return t.resolver(name)
}
func (t *PackageType) Method(name string) *FuncType {
if funcTyp, ok := t.resolver(name).(*FuncType); ok {
return funcTyp
}
return nil
}
func (t *PackageType) String() string {
var buffer bytes.Buffer
buffer.WriteString("package ")
buffer.WriteString(t.path)
return buffer.String()
}
// The outcome of a binary operation
func BinaryOp(X Type, op token.Token, Y Type) Type {
switch op {
case token.LAND, token.LOR, token.NEQ, token.LEQ, token.GEQ, token.EQL,
token.LSS, token.GTR:
return BuiltinTypes["bool"]
default:
// Binary operations are always between two of the same types, unless
// shifting. Untyped constants are converted to the type of the other
// operand
_, xConst := X.(*ConstType)
_, yConst := Y.(*ConstType)
switch {
case !xConst && !yConst:
return X
case xConst && !yConst:
return X
case yConst && !xConst:
return Y
default:
// TODO: see http://golang.org/ref/spec#Constant_expressions
return X
}
}
return nil
}
// Takes an identifier, returns the node that defines it. This should search all
// scopes up to the package level.
type Resolver func(ident string) Type
// Create a resolver for types
func MakeResolver(block *BasicBlock, p *Package, c *Compiler) Resolver {
return func(ident string) Type {
for child := block; child != nil; child = child.parent {
defineData := child.Get(AccessPassType).(*AccessPassData)
if result, ok := defineData.defines[ident]; ok {
return result
}
}
packageScope := c.GetPassResult(DefinedTypesPassType, p).(*DefinedTypesData)
if identType, ok := packageScope.defined[ident]; ok {
return identType
}
for _, embedded := range packageScope.embedded {
if identType := embedded.Field(ident); identType != nil {
return identType
}
}
if identType, ok := BuiltinTypes[ident]; ok {
return identType
}
return nil
}
}
func TypeOf(expr ast.Node, resolver Resolver) Type {
if *verbose {
fmt.Printf("TypeOf (%T %+v)\n", expr, expr)
}
t := typeOf(expr, resolver, false, true)
if *verbose {
fmt.Printf("==> %s\n", t)
}
return t
}
// Used to find the types of arguments or definitions, they vary by how they
// handle *pointers
func TypeOfDecl(expr ast.Node, resolver Resolver) Type {
if *verbose {
fmt.Printf("TypeOfDecl (%T %+v)\n", expr, expr)
}
t := typeOf(expr, resolver, true, true)
if *verbose {
fmt.Printf("==> %s\n", t)
}
return t
}
// Create the new Type for this declaration, but don't Complete() it to avoid
// recursive loops.
func TypeDecl(expr ast.Node, resolver Resolver) Type {
if *verbose {
fmt.Printf("TypeDecl (%T %+v)\n", expr, expr)
}
t := typeOf(expr, resolver, true, false)
if *verbose {
fmt.Printf("==> %T\n", t)
}
return t
}
func typeOf(expr ast.Node, resolver Resolver, definition bool, complete bool) Type {
switch t := expr.(type) {
case *ast.CallExpr:
callType := TypeOf(t.Fun, resolver)
// is this a type conversion or a function call?
switch callType.(type) {
case *FuncType:
// gather arguments to pass to Call()
var args []Type
for _, argExpr := range t.Args {
args = append(args, TypeOf(argExpr, resolver))
}
return callType.Call(args)
default:
// int32(X)
// deal with (*int32) parentheses manually
var retTyp Type
switch inner := t.Fun.(type) {
case *ast.ParenExpr:
retTyp = TypeOfDecl(inner.X, resolver)
default:
retTyp = TypeOfDecl(t.Fun, resolver)
}
return retTyp
}
case *ast.StructType, *ast.InterfaceType:
structTyp := newStructType(t)
if complete {
structTyp.Complete(resolver)
}
return structTyp
case *ast.FuncDecl:
funcTyp := newFuncType(t.Type, t)
if complete {
funcTyp.Complete(resolver)
}
return funcTyp
case *ast.FuncLit:
// safety: we can't go into these, there be dragons here
funcTyp := newFuncType(t.Type, nil)
if complete {
funcTyp.Complete(resolver)
}
return funcTyp
case *ast.FuncType:
// for arguments/local variables that are functions
funcTyp := newFuncType(t, nil)
if complete {
funcTyp.Complete(resolver)
}
return funcTyp
case *ast.Ident:
return resolver(t.Name)
case *ast.BasicLit:
constTyp := newConstType(t)
if complete {
constTyp.Complete(resolver)
}
return constTyp
case *ast.IndexExpr:
indexer := TypeOf(t.X, resolver)
return indexer.IndexValue()