-
Notifications
You must be signed in to change notification settings - Fork 34
/
TypeVisitor.java
1221 lines (1159 loc) · 46.1 KB
/
TypeVisitor.java
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 com.redhat.ceylon.compiler.typechecker.analyzer;
import static com.redhat.ceylon.compiler.typechecker.analyzer.Util.checkTypeBelongsToContainingScope;
import static com.redhat.ceylon.compiler.typechecker.analyzer.Util.formatPath;
import static com.redhat.ceylon.compiler.typechecker.analyzer.Util.getBaseDeclaration;
import static com.redhat.ceylon.compiler.typechecker.analyzer.Util.getTypeArguments;
import static com.redhat.ceylon.compiler.typechecker.model.Util.getContainingClassOrInterface;
import static com.redhat.ceylon.compiler.typechecker.model.Util.producedType;
import static com.redhat.ceylon.compiler.typechecker.tree.Util.name;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.redhat.ceylon.compiler.typechecker.model.Class;
import com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface;
import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.compiler.typechecker.model.Functional;
import com.redhat.ceylon.compiler.typechecker.model.Import;
import com.redhat.ceylon.compiler.typechecker.model.ImportList;
import com.redhat.ceylon.compiler.typechecker.model.Interface;
import com.redhat.ceylon.compiler.typechecker.model.IntersectionType;
import com.redhat.ceylon.compiler.typechecker.model.Method;
import com.redhat.ceylon.compiler.typechecker.model.MethodOrValue;
import com.redhat.ceylon.compiler.typechecker.model.Module;
import com.redhat.ceylon.compiler.typechecker.model.ModuleImport;
import com.redhat.ceylon.compiler.typechecker.model.Package;
import com.redhat.ceylon.compiler.typechecker.model.Parameter;
import com.redhat.ceylon.compiler.typechecker.model.ProducedType;
import com.redhat.ceylon.compiler.typechecker.model.Scope;
import com.redhat.ceylon.compiler.typechecker.model.TypeAlias;
import com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration;
import com.redhat.ceylon.compiler.typechecker.model.TypeParameter;
import com.redhat.ceylon.compiler.typechecker.model.TypedDeclaration;
import com.redhat.ceylon.compiler.typechecker.model.UnionType;
import com.redhat.ceylon.compiler.typechecker.model.Unit;
import com.redhat.ceylon.compiler.typechecker.model.UnknownType;
import com.redhat.ceylon.compiler.typechecker.model.Value;
import com.redhat.ceylon.compiler.typechecker.model.ValueParameter;
import com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Tree.ClassSpecifier;
import com.redhat.ceylon.compiler.typechecker.tree.Tree.InvocationExpression;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;
/**
* Second phase of type analysis.
* Scan the compilation unit looking for literal type
* declarations and maps them to the associated model
* objects. Also builds up a list of imports for the
* compilation unit. Finally, assigns types to the
* associated model objects of declarations declared
* using an explicit type (this must be done in
* this phase, since shared declarations may be used
* out of order in expressions).
*
* @author Gavin King
*
*/
public class TypeVisitor extends Visitor {
private Unit unit;
@Override public void visit(Tree.CompilationUnit that) {
unit = that.getUnit();
super.visit(that);
}
@Override
public void visit(Tree.Import that) {
Package importedPackage = that instanceof Tree.ImportDynamic ?
unit.getPackage() : getPackage(that.getImportPath());
if (importedPackage!=null) {
that.getImportPath().setModel(importedPackage);
Tree.ImportMemberOrTypeList imtl = that.getImportMemberOrTypeList();
if (imtl!=null) {
ImportList il = imtl.getImportList();
il.setImportedScope(importedPackage);
Set<String> names = new HashSet<String>();
for (Tree.ImportMemberOrType member: imtl.getImportMemberOrTypes()) {
names.add(importMember(member, importedPackage, il, that));
}
if (imtl.getImportWildcard()==null) {
if (imtl.getImportMemberOrTypes().isEmpty()) {
imtl.addError("empty import list");
}
}
else {
importAllMembers(importedPackage, names, il);
}
}
}
}
private void importAllMembers(Package importedPackage, Set<String> ignoredMembers,
ImportList il) {
for (Declaration dec: importedPackage.getMembers()) {
if (dec.isShared() && !dec.isAnonymous() &&
!ignoredMembers.contains(dec.getName()) &&
!isNonimportable(importedPackage, dec.getName())) {
Import i = new Import();
i.setAlias(dec.getName());
i.setDeclaration(dec);
i.setWildcardImport(true);
addWildcardImport(il, dec, i);
}
}
}
private void addWildcardImport(ImportList il, Declaration dec, Import i) {
Import o = unit.getImport(dec.getName());
if (o==null) {
unit.getImports().add(i);
il.getImports().add(i);
}
else if (o.isWildcardImport()) {
unit.getImports().remove(o);
il.getImports().remove(o);
if (o.getDeclaration().equals(dec)) {
//this case only happens in the IDE,
//due to reuse of the Unit
unit.getImports().add(i);
il.getImports().add(i);
}
}
}
private Package getPackage(Tree.ImportPath path) {
if (path!=null && !path.getIdentifiers().isEmpty()) {
String nameToImport = formatPath(path.getIdentifiers());
Module module = unit.getPackage().getModule();
Package pkg = module.getPackage(nameToImport);
if (pkg != null) {
if (pkg.getModule().equals(module)) {
return pkg;
}
if (!pkg.isShared()) {
path.addError("imported package is not shared: " +
nameToImport);
}
//check that the package really does belong to
//an imported module, to work around bug where
//default package thinks it can see stuff in
//all modules in the same source dir
for (ModuleImport mi: module.getImports()) {
if (mi.getModule().equals(pkg.getModule())) {
return pkg;
}
}
}
path.addError("package not found in dependent modules: " + nameToImport);
}
return null;
}
// private boolean hasName(List<Tree.Identifier> importPath, Package mp) {
// if (mp.getName().size()==importPath.size()) {
// for (int i=0; i<mp.getName().size(); i++) {
// if (!mp.getName().get(i).equals(name(importPath.get(i)))) {
// return false;
// }
// }
// return true;
// }
// else {
// return false;
// }
// }
private String importMember(Tree.ImportMemberOrType member, Package importedPackage,
ImportList il, Tree.Import that) {
if (member.getIdentifier()==null) {
return null;
}
Import i = new Import();
member.setImportModel(i);
Tree.Alias alias = member.getAlias();
String name = name(member.getIdentifier());
if (alias==null) {
i.setAlias(name);
}
else {
i.setAlias(name(alias.getIdentifier()));
}
if (that instanceof Tree.ImportDynamic) {
Value v = new Value();
v.setType(new UnknownType(unit).getType());
v.setContainer(unit.getPackage());
v.setName(name);
i.setDeclaration(v);
i.setDynamicImport(true);
member.setDeclarationModel(v);
if (il.hasImport(v)) {
member.getIdentifier().addError("already imported: " +
name);
}
addImport(member, il, i);
//checkAliasCase(alias, v);
}
else {
if (isNonimportable(importedPackage, name)) {
member.getIdentifier().addError("root type may not be imported");
return name;
}
for (Declaration d: unit.getDeclarations()) {
String n = d.getName();
if (d.isToplevel() && n!=null &&
i.getAlias().equals(n)) {
if (alias==null) {
member.getIdentifier()
.addError("toplevel declaration with this name declared in this unit: " + n);
}
else {
alias.addError("toplevel declaration with this name declared in this unit: " + n);
}
}
}
Declaration dec = importedPackage.getMember(name, null, false);
if (dec==null) {
member.getIdentifier().addError("imported declaration not found: " +
name, 100);
unit.getUnresolvedReferences().add(member.getIdentifier());
}
else {
if (!dec.isShared() && !dec.getUnit().getPackage().equals(unit.getPackage())) {
member.getIdentifier().addError("imported declaration is not shared: " +
name, 400);
}
if (dec.isProtectedVisibility()) {
member.getIdentifier().addError("imported declaration is not visible: " +
name);
}
i.setDeclaration(dec);
member.setDeclarationModel(dec);
if (il.hasImport(dec)) {
member.getIdentifier().addError("already imported: " +
name);
}
addImport(member, il, i);
checkAliasCase(alias, dec);
}
importMembers(member, dec);
}
return name;
}
public void importMembers(Tree.ImportMemberOrType member, Declaration d) {
Tree.ImportMemberOrTypeList imtl = member.getImportMemberOrTypeList();
if (imtl!=null) {
if (imtl.getImportMemberOrTypes().isEmpty()) {
imtl.addError("empty import list");
}
if (d instanceof TypeDeclaration) {
ImportList til = imtl.getImportList();
til.setImportedScope((TypeDeclaration) d);
for (Tree.ImportMemberOrType submember: imtl.getImportMemberOrTypes()) {
importMember(submember, (TypeDeclaration) d, til);
}
}
else {
imtl.addError("member alias list must follow a type");
}
}
}
private void checkAliasCase(Tree.Alias alias, Declaration d) {
if (alias!=null) {
if (d instanceof TypeDeclaration &&
alias.getIdentifier().getToken().getType()!=CeylonLexer.UIDENTIFIER) {
alias.getIdentifier().addError("imported type should have uppercase alias: " +
d.getName());
}
if (d instanceof TypedDeclaration &&
alias.getIdentifier().getToken().getType()!=CeylonLexer.LIDENTIFIER) {
alias.getIdentifier().addError("imported member should have lowercase alias: " +
d.getName());
}
}
}
private void importMember(Tree.ImportMemberOrType member, TypeDeclaration d,
ImportList il) {
if (member.getIdentifier()==null) {
return;
}
Import i = new Import();
member.setImportModel(i);
Tree.Alias alias = member.getAlias();
String name = name(member.getIdentifier());
if (alias==null) {
i.setAlias(name);
}
else {
i.setAlias(name(alias.getIdentifier()));
}
Declaration m = d.getMember(name, null, false);
if (m==null) {
member.getIdentifier().addError("imported declaration not found: " +
name + " of " + d.getName(), 100);
unit.getUnresolvedReferences().add(member.getIdentifier());
}
else {
if (!m.isShared()) {
member.getIdentifier().addError("imported declaration is not shared: " +
name + " of " + d.getName(), 400);
}
if (m.isProtectedVisibility()) {
member.getIdentifier().addError("imported declaration is not visible: " +
name + " of " + d.getName());
}
if (!m.isStaticallyImportable()) {
i.setTypeDeclaration(d);
if (alias==null) {
member.addError("does not specify an alias");
}
}
i.setDeclaration(m);
member.setDeclarationModel(m);
if (il.hasImport(m)) {
member.getIdentifier().addError("already imported: " +
name + " of " + d.getName());
}
if (m.isStaticallyImportable()) {
addImport(member, il, i);
}
else {
addMemberImport(member, il, i);
}
checkAliasCase(alias, m);
}
importMembers(member, m);
//imtl.addError("member aliases may not have member aliases");
}
private void addMemberImport(Tree.ImportMemberOrType member, ImportList il,
Import i) {
if (il.getImport(i.getAlias())==null) {
unit.getImports().add(i);
il.getImports().add(i);
}
else {
member.addError("duplicate member import alias: " + i.getAlias());
}
}
private boolean isNonimportable(Package pkg, String name) {
return pkg.getQualifiedNameString().equals("java.lang") &&
("Object".equals(name) ||
"Throwable".equals(name) /*||
"Exception".equals(name)*/);
}
private void addImport(Tree.ImportMemberOrType member, ImportList il,
Import i) {
Import o = unit.getImport(i.getAlias());
if (o==null) {
unit.getImports().add(i);
il.getImports().add(i);
}
else if (o.isWildcardImport()) {
unit.getImports().remove(o);
il.getImports().remove(o);
unit.getImports().add(i);
il.getImports().add(i);
}
else {
member.addError("duplicate import alias: " + i.getAlias());
}
}
@Override
public void visit(Tree.UnionType that) {
super.visit(that);
UnionType ut = new UnionType(unit);
List<ProducedType> types = new ArrayList<ProducedType>();
for (Tree.StaticType st: that.getStaticTypes()) {
//addToUnion( types, st.getTypeModel() );
ProducedType t = st.getTypeModel();
if (t!=null) types.add(t);
}
ut.setCaseTypes(types);
that.setTypeModel(ut.getType());
//that.setTarget(pt);
}
@Override
public void visit(Tree.IntersectionType that) {
super.visit(that);
IntersectionType it = new IntersectionType(unit);
List<ProducedType> types = new ArrayList<ProducedType>();
for (Tree.StaticType st: that.getStaticTypes()) {
//addToIntersection(types, st.getTypeModel(), unit);
ProducedType t = st.getTypeModel();
if (t!=null) types.add(t);
}
it.setSatisfiedTypes(types);
that.setTypeModel(it.getType());
//that.setTarget(pt);
}
@Override
public void visit(Tree.SequenceType that) {
super.visit(that);
if (that.getElementType()!=null) {
ProducedType et = that.getElementType().getTypeModel();
if (et!=null) {
that.setTypeModel(unit.getSequentialType(et));
}
}
}
@Override
public void visit(Tree.IterableType that) {
super.visit(that);
Tree.Type elem = that.getElementType();
if (elem!=null) {
if (elem instanceof Tree.SequencedType) {
ProducedType et = ((Tree.SequencedType) elem).getType().getTypeModel();
if (et!=null) {
if (((Tree.SequencedType) elem).getAtLeastOne()) {
that.setTypeModel(unit.getNonemptyIterableType(et));
}
else {
that.setTypeModel(unit.getIterableType(et));
}
}
}
else {
that.addError("malformed iterable type");
}
}
}
@Override
public void visit(Tree.OptionalType that) {
super.visit(that);
ProducedType dt = that.getDefiniteType().getTypeModel();
if (dt!=null) {
that.setTypeModel(unit.getOptionalType(dt));
}
}
@Override
public void visit(Tree.EntryType that) {
super.visit(that);
ProducedType kt = that.getKeyType().getTypeModel();
ProducedType vt = that.getValueType()==null ?
new UnknownType(unit).getType() :
that.getValueType().getTypeModel();
that.setTypeModel(unit.getEntryType(kt, vt));
}
@Override
public void visit(Tree.FunctionType that) {
super.visit(that);
that.setTypeModel(producedType(unit.getCallableDeclaration(),
that.getReturnType().getTypeModel(),
getTupleType(that.getArgumentTypes())));
}
@Override
public void visit(Tree.TupleType that) {
super.visit(that);
that.setTypeModel(getTupleType(that.getElementTypes()));
}
private ProducedType getTupleType(List<Tree.Type> ets) {
List<ProducedType> args = new ArrayList<ProducedType>();
boolean sequenced = false;
boolean atleastone = false;
int firstDefaulted = -1;
for (int i=0; i<ets.size(); i++) {
Tree.Type st = ets.get(i);
ProducedType arg = st==null ? null : st.getTypeModel();
if (arg==null) {
arg = new UnknownType(unit).getType();
}
else if (st instanceof Tree.DefaultedType) {
if (firstDefaulted==-1) {
firstDefaulted = i;
}
}
else if (st instanceof Tree.SequencedType) {
if (i!=ets.size()-1) {
st.addError("variant element must occur last in a tuple type");
}
else {
sequenced = true;
atleastone = ((Tree.SequencedType) st).getAtLeastOne();
arg = ((Tree.SequencedType) st).getType().getTypeModel();
}
}
args.add(arg);
}
return unit.getTupleType(args, sequenced, atleastone, firstDefaulted);
}
//TODO: copy/pasted from ExpressionVisitor
private static TypeDeclaration getSupertypeDeclaration(Tree.BaseType that,
Tree.SupertypeQualifier sq) {
String typeName = name(sq.getIdentifier());
Declaration dec = that.getScope().getMemberOrParameter(that.getUnit(), typeName, null, false);
if (dec instanceof TypeDeclaration) {
/*ClassOrInterface ci = getContainingClassOrInterface(that.getScope());
if (ci.getType().getSupertype((TypeDeclaration) dec)==null) {
sq.addError("not a supertype of containing class or interface: " +
ci.getName() + " does not inherit " + dec.getName());
}*/
Declaration member = dec.getMember(name(that.getIdentifier()), null, false);
if (member instanceof TypeDeclaration) {
return (TypeDeclaration) member;
}
else{
return null;
}
}
else {
sq.addError("qualifying supertype does not exist: " + typeName);
return null;
}
}
@Override
public void visit(Tree.BaseType that) {
super.visit(that);
TypeDeclaration type;
Tree.SupertypeQualifier sq = that.getSupertypeQualifier();
if (sq==null) {
type = getBaseDeclaration(that);
}
else {
type = getSupertypeDeclaration(that, sq);
}
String name = name(that.getIdentifier());
if (type==null) {
that.addError("type declaration does not exist: " + name, 102);
unit.getUnresolvedReferences().add(that.getIdentifier());
}
else {
if (!type.isVisible(that.getScope())) {
that.addError("type is not visible: " + name, 400);
}
ProducedType outerType = that.getScope().getDeclaringType(type);
visitSimpleType(that, outerType, type);
}
}
public void visit(Tree.SuperType that) {
//if (inExtendsClause) { //can't appear anywhere else in the tree!
ClassOrInterface ci = getContainingClassOrInterface(that.getScope());
if (ci!=null) {
if (ci.isClassOrInterfaceMember()) {
ClassOrInterface s = (ClassOrInterface) ci.getContainer();
ProducedType t = s.getExtendedType();
//TODO: type arguments
that.setTypeModel(t);
}
else {
that.addError("super appears in extends for non-member class");
}
}
//}
}
public void visit(Tree.QualifiedType that) {
super.visit(that);
ProducedType pt = that.getOuterType().getTypeModel();
if (pt!=null) {
TypeDeclaration d = pt.getDeclaration();
String name = name(that.getIdentifier());
TypeDeclaration type = (TypeDeclaration) d.getMember(name, unit, null, false);
if (type==null) {
if (d.isMemberAmbiguous(name, unit, null, false)) {
that.addError("member type declaration is ambiguous: " +
name + " for type " + d.getName());
}
else {
that.addError("member type declaration does not exist: " +
name + " in type " + d.getName(), 100);
unit.getUnresolvedReferences().add(that.getIdentifier());
}
}
else {
if (!type.isVisible(that.getScope())) {
that.addError("member type is not visible: " +
name + " of type " + d.getName(), 400);
}
visitSimpleType(that, pt, type);
}
}
}
private void visitSimpleType(Tree.SimpleType that, ProducedType ot, TypeDeclaration dec) {
Tree.TypeArgumentList tal = that.getTypeArgumentList();
List<ProducedType> ta = getTypeArguments(tal,
dec.getTypeParameters());
if (tal!=null) tal.setTypeModels(ta);
//if (acceptsTypeArguments(dec, ta, tal, that)) {
ProducedType pt = dec.getProducedType(ot, ta);
// if (!dec.isAlias()) {
//TODO: remove this awful hack which means
// we can't define aliases for types
// with sequenced type parameters
dec = pt.getDeclaration();
// }
that.setTypeModel(pt);
that.setDeclarationModel(dec);
//}
}
/*private void visitExtendedType(Tree.QualifiedType that, ProducedType ot, TypeDeclaration dec) {
Tree.TypeArgumentList tal = that.getTypeArgumentList();
List<ProducedType> typeArguments = getTypeArguments(tal);
//if (acceptsTypeArguments(dec, typeArguments, tal, that)) {
ProducedType pt = dec.getProducedType(ot, typeArguments);
that.setTypeModel(pt);
//}
}*/
@Override
public void visit(Tree.VoidModifier that) {
Class vtd = unit.getAnythingDeclaration();
if (vtd!=null) {
that.setTypeModel(vtd.getType());
}
}
public void visit(Tree.SequencedType that) {
super.visit(that);
ProducedType type = that.getType().getTypeModel();
if (type!=null) {
ProducedType et = that.getAtLeastOne() ?
unit.getSequenceType(type) :
unit.getSequentialType(type);
that.setTypeModel(et);
}
}
public void visit(Tree.DefaultedType that) {
super.visit(that);
ProducedType type = that.getType().getTypeModel();
if (type!=null) {
that.setTypeModel(type);
}
}
@Override
public void visit(Tree.TypedDeclaration that) {
super.visit(that);
setType(that, that.getType(), that.getDeclarationModel());
}
@Override
public void visit(Tree.TypedArgument that) {
super.visit(that);
setType(that, that.getType(), that.getDeclarationModel());
}
/*@Override
public void visit(Tree.FunctionArgument that) {
super.visit(that);
setType(that, that.getType(), that.getDeclarationModel());
}*/
private void setType(Node that, Tree.Type type, TypedDeclaration td) {
if (type==null) {
that.addError("missing type of declaration: " + td.getName());
}
else if (!(type instanceof Tree.LocalModifier)) { //if the type declaration is missing, we do type inference later
ProducedType t = type.getTypeModel();
if (t!=null) {
td.setType(t);
}
}
}
private void defaultSuperclass(Tree.ExtendedType et, TypeDeclaration c) {
if (et==null) {
Class iotd = unit.getBasicDeclaration();
if (iotd!=null) {
c.setExtendedType(iotd.getType());
}
}
}
@Override
public void visit(Tree.ObjectDefinition that) {
defaultSuperclass(that.getExtendedType(),
that.getAnonymousClass());
super.visit(that);
}
@Override
public void visit(Tree.ObjectArgument that) {
defaultSuperclass(that.getExtendedType(),
that.getAnonymousClass());
super.visit(that);
}
@Override
public void visit(Tree.ClassDefinition that) {
Class c = that.getDeclarationModel();
Class vd = unit.getAnythingDeclaration();
if (c!=vd) {
defaultSuperclass(that.getExtendedType(), c);
}
super.visit(that);
}
@Override
public void visit(Tree.InterfaceDefinition that) {
Class od = unit.getObjectDeclaration();
if (od!=null) {
that.getDeclarationModel().setExtendedType(od.getType());
}
super.visit(that);
}
@Override
public void visit(Tree.TypeParameterDeclaration that) {
Class vd = unit.getAnythingDeclaration();
if (vd!=null) {
that.getDeclarationModel().setExtendedType(vd.getType());
}
super.visit(that);
if (that.getTypeSpecifier()!=null) {
Tree.StaticType type = that.getTypeSpecifier().getType();
if (type!=null) {
ProducedType t = type.getTypeModel();
if (t.containsTypeParameters()) {
type.addError("default type argument involves type parameters: " +
t.getProducedTypeName());
}
else {
that.getDeclarationModel().setDefaultTypeArgument(t);
}
}
}
}
@Override
public void visit(Tree.ClassDeclaration that) {
super.visit(that);
TypeDeclaration td = that.getDeclarationModel();
ClassSpecifier cs = that.getClassSpecifier();
if (cs==null) {
that.addError("missing class body or aliased class reference");
}
else {
Tree.SimpleType ct = cs.getType();
if (ct==null) {
that.addError("malformed aliased class");
}
else if (!(ct instanceof Tree.StaticType)) {
cs.addError("aliased type must be a class");
}
else if (ct instanceof Tree.QualifiedType) {
cs.addError("aliased class may not be a qualified type");
}
else {
ProducedType type = ct.getTypeModel();
if (type!=null) {
/*if (type.containsTypeAliases()) {
et.addError("aliased type involves type aliases: " +
type.getProducedTypeName());
}
else*/ if (type.getDeclaration() instanceof Class) {
that.getDeclarationModel().setExtendedType(type);
}
else {
ct.addError("not a class: " +
type.getDeclaration().getName(unit));
}
TypeDeclaration etd = ct.getDeclarationModel();
if (etd==td) {
//TODO: handle indirect circularities!
ct.addError("directly aliases itself: " + td.getName());
return;
}
InvocationExpression ie = cs.getInvocationExpression();
if (ie!=null) {
//TODO: it would probably be better to leave
// all this following stuff to
//ExpressionVisitor.visit(ExtendedTypeExpression)
Tree.Primary pr = ie.getPrimary();
if (pr instanceof Tree.ExtendedTypeExpression) {
pr.setTypeModel(type);
Tree.ExtendedTypeExpression ete = (Tree.ExtendedTypeExpression) pr;
ete.setDeclaration(etd);
ete.setTarget(type);
}
}
}
}
}
}
@Override
public void visit(Tree.InterfaceDeclaration that) {
super.visit(that);
if (that.getTypeSpecifier()==null) {
that.addError("missing interface body or aliased interface reference");
}
else {
Tree.StaticType et = that.getTypeSpecifier().getType();
if (et==null) {
that.addError("malformed aliased interface");
}
else if (!(et instanceof Tree.StaticType)) {
that.getTypeSpecifier()
.addError("aliased type must be an interface");
}
else {
ProducedType type = et.getTypeModel();
if (type!=null) {
/*if (type.containsTypeAliases()) {
et.addError("aliased type involves type aliases: " +
type.getProducedTypeName());
}
else*/ if (type.getDeclaration() instanceof Interface) {
that.getDeclarationModel().setExtendedType(type);
}
else {
et.addError("not an interface: " +
type.getDeclaration().getName(unit));
}
}
}
}
}
@Override
public void visit(Tree.TypeAliasDeclaration that) {
super.visit(that);
if (that.getTypeSpecifier()==null) {
that.addError("missing aliased type");
}
else {
Tree.StaticType et = that.getTypeSpecifier().getType();
if (et==null) {
that.addError("malformed aliased type");
}
else {
ProducedType type = et.getTypeModel();
if (type!=null) {
/*if (type.containsTypeAliases()) {
et.addError("aliased type involves type aliases: " +
type.getProducedTypeName());
}
else {*/
that.getDeclarationModel().setExtendedType(type);
//}
}
}
}
}
@Override
public void visit(Tree.MethodDeclaration that) {
super.visit(that);
Tree.SpecifierExpression sie = that.getSpecifierExpression();
if (sie==null
&& that.getType() instanceof Tree.FunctionModifier) {
that.getType().addError("method must specify an explicit return type or definition");
}
TypedDeclaration dec = that.getDeclarationModel();
if (dec!=null) {
Scope s = dec.getContainer();
if (s instanceof Functional) {
Parameter param = ((Functional) s).getParameter( dec.getName() );
if (param instanceof ValueParameter &&
((ValueParameter) param).isHidden()) {
ProducedType ft = dec.getProducedReference(null,
Collections.<ProducedType>emptyList()).getFullType();
param.setType(ft);
if (sie!=null) {
sie.addError("function is an initializer parameter and may not have an initial value: " +
dec.getName());
}
}
}
}
}
@Override
public void visit(Tree.AttributeDeclaration that) {
super.visit(that);
Tree.SpecifierOrInitializerExpression sie = that.getSpecifierOrInitializerExpression();
TypedDeclaration dec = that.getDeclarationModel();
if (dec!=null) {
Scope s = dec.getContainer();
if (s instanceof Functional) {
Parameter param = ((Functional) s).getParameter( dec.getName() );
if (param instanceof ValueParameter &&
((ValueParameter) param).isHidden()) {
param.setType(dec.getType());
param.setSequenced(that.getType() instanceof Tree.SequencedType);
if (sie!=null) {
sie.addError("attribute is an initializer parameter and may not have an initial value: " +
dec.getName());
}
}
}
}
}
@Override
public void visit(Tree.ExtendedType that) {
super.visit(that);
TypeDeclaration td = (TypeDeclaration) that.getScope();
if (td.isAlias()) {
that.addError("alias may not extend a type");
return;
}
InvocationExpression ie = that.getInvocationExpression();
Tree.SimpleType et = that.getType();
if (et==null) {
that.addError("malformed extended type");
}
else {
/*if (et instanceof Tree.QualifiedType) {
Tree.QualifiedType st = (Tree.QualifiedType) et;
ProducedType pt = st.getOuterType().getTypeModel();
if (pt!=null) {
TypeDeclaration superclass = (TypeDeclaration) getMemberDeclaration(pt.getDeclaration(), st.getIdentifier(), context);
if (superclass==null) {
that.addError("member type declaration not found: " +
st.getIdentifier().getText());
}
else {
visitExtendedType(st, pt, superclass);
}
}
}*/
if (!(et instanceof Tree.SimpleType)) {
et.addError("extended type must be a class"); //actually this case never occurs due to grammar
}
else {
ProducedType type = et.getTypeModel();
if (type!=null) {
TypeDeclaration etd = ((Tree.SimpleType) et).getDeclarationModel();
if (etd==td) {
//TODO: handle indirect circularities!
et.addError("directly extends itself: " + td.getName());
return;
}
if (et instanceof Tree.QualifiedType) {
if ( !(((Tree.QualifiedType) et).getOuterType() instanceof Tree.SuperType) ) {
checkTypeBelongsToContainingScope(type, td.getContainer(), et);
}
}
if (ie!=null) {
//TODO: it would probably be better to leave
// all this following stuff to
//ExpressionVisitor.visit(ExtendedTypeExpression)
Tree.Primary pr = ie.getPrimary();
if (pr instanceof Tree.ExtendedTypeExpression) {
pr.setTypeModel(type);
Tree.ExtendedTypeExpression ete = (Tree.ExtendedTypeExpression) pr;
ete.setDeclaration(etd);
ete.setTarget(type);
}
}
if (etd instanceof TypeParameter) {
et.addError("directly extends a type parameter: " +
type.getDeclaration().getName(unit));
}
else if (etd instanceof Interface) {
et.addError("extends an interface: " +
type.getDeclaration().getName(unit));
}
else if (etd instanceof TypeAlias) {
et.addError("extends a type alias: " +
type.getDeclaration().getName(unit));
}
else {
td.setExtendedType(type);
}
}
}
}
}
@Override
public void visit(Tree.SatisfiedTypes that) {
super.visit(that);
TypeDeclaration td = (TypeDeclaration) that.getScope();
if (td.isAlias()) {
that.addError("alias may not satisfy a type");
return;
}
List<ProducedType> list = new ArrayList<ProducedType>();
if ( that.getTypes().isEmpty() ) {
that.addError("missing types in satisfies");
}
boolean foundTypeParam = false;
boolean foundClass = false;