/
AnalyzerUtil.java
1282 lines (1214 loc) · 46.5 KB
/
AnalyzerUtil.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.tree.TreeUtil.formatPath;
import static com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.hasError;
import static com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.name;
import static com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.intersectionType;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.isBooleanFalse;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.isBooleanTrue;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.isConstructor;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.isForBackend;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.isNamed;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.isTypeUnknown;
import static com.redhat.ceylon.model.typechecker.model.ModelUtil.unionType;
import static com.redhat.ceylon.model.typechecker.model.SiteVariance.IN;
import static com.redhat.ceylon.model.typechecker.model.SiteVariance.OUT;
import static java.util.Collections.emptyList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.redhat.ceylon.common.Backend;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Tree.ClassBody;
import com.redhat.ceylon.compiler.typechecker.tree.Tree.TypeVariance;
import com.redhat.ceylon.model.cmr.JDKUtils;
import com.redhat.ceylon.model.typechecker.model.Class;
import com.redhat.ceylon.model.typechecker.model.Constructor;
import com.redhat.ceylon.model.typechecker.model.Declaration;
import com.redhat.ceylon.model.typechecker.model.Generic;
import com.redhat.ceylon.model.typechecker.model.Interface;
import com.redhat.ceylon.model.typechecker.model.Module;
import com.redhat.ceylon.model.typechecker.model.ModuleImport;
import com.redhat.ceylon.model.typechecker.model.Package;
import com.redhat.ceylon.model.typechecker.model.Parameter;
import com.redhat.ceylon.model.typechecker.model.ParameterList;
import com.redhat.ceylon.model.typechecker.model.Reference;
import com.redhat.ceylon.model.typechecker.model.Scope;
import com.redhat.ceylon.model.typechecker.model.SiteVariance;
import com.redhat.ceylon.model.typechecker.model.Type;
import com.redhat.ceylon.model.typechecker.model.TypeAlias;
import com.redhat.ceylon.model.typechecker.model.TypeDeclaration;
import com.redhat.ceylon.model.typechecker.model.TypeParameter;
import com.redhat.ceylon.model.typechecker.model.TypedDeclaration;
import com.redhat.ceylon.model.typechecker.model.Unit;
import com.redhat.ceylon.model.typechecker.model.Value;
/**
* Bucket for some helper methods used by various
* visitors.
*
* @author Gavin King
*
*/
public class AnalyzerUtil {
static final List<Type> NO_TYPE_ARGS = emptyList();
static TypedDeclaration getTypedMember(TypeDeclaration td,
String name, List<Type> signature, boolean ellipsis,
Unit unit) {
Declaration member =
td.getMember(name, unit, signature, ellipsis);
if (member instanceof TypedDeclaration) {
return (TypedDeclaration) member;
}
else {
return null;
}
}
static TypeDeclaration getTypeMember(TypeDeclaration td,
String name, List<Type> signature, boolean ellipsis,
Unit unit) {
Declaration member =
td.getMember(name, unit, signature, ellipsis);
if (member instanceof TypeDeclaration) {
return (TypeDeclaration) member;
}
else if (member instanceof TypedDeclaration) {
return anonymousType(name,
(TypedDeclaration) member);
}
else {
return null;
}
}
static TypedDeclaration getTypedDeclaration(Scope scope,
String name, List<Type> signature, boolean ellipsis,
Unit unit) {
Declaration result =
scope.getMemberOrParameter(unit,
name, signature, ellipsis);
if (result instanceof TypedDeclaration) {
return (TypedDeclaration) result;
}
else {
return null;
}
}
static TypeDeclaration getTypeDeclaration(Scope scope,
String name, List<Type> signature, boolean ellipsis,
Unit unit) {
Declaration result =
scope.getMemberOrParameter(unit,
name, signature, ellipsis);
if (result instanceof TypeDeclaration) {
return (TypeDeclaration) result;
}
else if (result instanceof TypedDeclaration) {
return anonymousType(name,
(TypedDeclaration) result);
}
else {
return null;
}
}
static TypedDeclaration getPackageTypedDeclaration(
String name, List<Type> signature, boolean ellipsis,
Unit unit) {
Declaration result =
unit.getPackage()
.getMember(name, signature, ellipsis);
if (result instanceof TypedDeclaration) {
return (TypedDeclaration) result;
}
else {
return null;
}
}
static TypeDeclaration getPackageTypeDeclaration(
String name, List<Type> signature, boolean ellipsis,
Unit unit) {
Declaration result =
unit.getPackage()
.getMember(name, signature, ellipsis);
if (result instanceof TypeDeclaration) {
return (TypeDeclaration) result;
}
else if (result instanceof TypedDeclaration) {
return anonymousType(name,
(TypedDeclaration) result);
}
else {
return null;
}
}
private static TypeDeclaration anonymousType(String name,
TypedDeclaration result) {
Type type = result.getType();
if (type!=null) {
TypeDeclaration typeDeclaration =
type.getDeclaration();
if ((typeDeclaration instanceof Class ||
typeDeclaration instanceof Constructor) &&
typeDeclaration.isAnonymous() &&
isNamed(name, typeDeclaration)) {
return typeDeclaration;
}
}
return null;
}
/**
* Get the type arguments specified explicitly in the
* code, or an empty list if no type arguments were
* explicitly specified. For missing arguments, use
* default type arguments.
*
* @param tas the type argument list
* @param qualifyingType the qualifying type
* @param typeParameters the list of type parameters
*
* @return a list of type arguments to the given type
* parameters
*/
static List<Type> getTypeArguments(
Tree.TypeArguments tas,
Type qualifyingType,
List<TypeParameter> typeParameters) {
if (tas instanceof Tree.TypeArgumentList) {
//accumulate substitutions in case we need
//them below for calculating default args
Map<TypeParameter,Type> typeArgs =
new HashMap<TypeParameter,Type>();
Map<TypeParameter,SiteVariance> vars =
new HashMap<TypeParameter,SiteVariance>();
if (qualifyingType!=null) {
typeArgs.putAll(qualifyingType.getTypeArguments());
vars.putAll(qualifyingType.getVarianceOverrides());
}
Tree.TypeArgumentList tal =
(Tree.TypeArgumentList) tas;
int size = typeParameters.size();
List<Type> typeArguments =
new ArrayList<Type>(size);
List<Tree.Type> types = tal.getTypes();
int count = types.size();
for (int i=0; i<count; i++) {
Tree.Type type = types.get(i);
Type t = type.getTypeModel();
if (t==null) {
typeArguments.add(null);
}
else {
typeArguments.add(t);
if (i<size) {
TypeParameter tp =
typeParameters.get(i);
if (tp.isTypeConstructor()) {
setTypeConstructor(type, tp);
}
typeArgs.put(tp, t);
if (type instanceof Tree.StaticType) {
Tree.StaticType st =
(Tree.StaticType) type;
TypeVariance tv =
st.getTypeVariance();
if (tv!=null) {
boolean contra =
tv.getText()
.equals("in");
vars.put(tp, contra?IN:OUT);
}
}
}
}
}
//for missing arguments, use the default args
for (int i=typeArguments.size(); i<size; i++) {
TypeParameter tp = typeParameters.get(i);
Type dta = tp.getDefaultTypeArgument();
if (dta==null) {
break;
}
else {
Type da = dta.substitute(typeArgs, vars);
typeArguments.add(da);
typeArgs.put(tp, da);
}
}
return typeArguments;
}
else {
return emptyList();
}
}
public static Tree.Statement getLastExecutableStatement(
Tree.ClassBody that) {
List<Tree.Statement> statements =
that.getStatements();
Unit unit = that.getUnit();
for (int i=statements.size()-1; i>=0; i--) {
Tree.Statement s = statements.get(i);
if (isExecutableStatement(unit, s) ||
s instanceof Tree.Constructor ||
s instanceof Tree.Enumerated) {
return s;
}
}
return null;
}
static Tree.Declaration getLastConstructor(Tree.ClassBody that) {
List<Tree.Statement> statements = that.getStatements();
for (int i=statements.size()-1; i>=0; i--) {
Tree.Statement s = statements.get(i);
if (s instanceof Tree.Constructor ||
s instanceof Tree.Enumerated) {
return (Tree.Declaration) s;
}
}
return null;
}
static boolean isExecutableStatement(Unit unit, Tree.Statement s) {
if (s instanceof Tree.SpecifierStatement) {
//shortcut refinement statements with => aren't really "executable"
Tree.SpecifierStatement ss =
(Tree.SpecifierStatement) s;
Tree.SpecifierExpression se =
ss.getSpecifierExpression();
return !(ss.getRefinement() &&
se instanceof Tree.LazySpecifierExpression);
}
else if (s instanceof Tree.ExecutableStatement) {
return true;
}
else {
if (s instanceof Tree.AttributeDeclaration) {
Tree.AttributeDeclaration ad =
(Tree.AttributeDeclaration) s;
Tree.SpecifierOrInitializerExpression sie =
ad.getSpecifierOrInitializerExpression();
return !(sie==null ||
sie instanceof Tree.LazySpecifierExpression);
}
else if (s instanceof Tree.ObjectDefinition) {
Tree.ObjectDefinition o =
(Tree.ObjectDefinition) s;
if (o.getExtendedType()!=null) {
Type et =
o.getExtendedType()
.getType()
.getTypeModel();
if (et!=null
&& !et.isObject()
&& !et.isBasic()) {
return true;
}
}
ClassBody body = o.getClassBody();
if (body!=null) {
if (getLastExecutableStatement(body)!=null) {
return true;
}
}
return false;
}
else {
return false;
}
}
}
static String typingMessage(Type type,
String problem, Type otherType,
Unit unit) {
String unknownTypeError =
type.getFirstUnknownTypeError(true);
String typeName = type.asString(unit);
String otherTypeName = otherType.asString(unit);
String expandedTypeName;
String expandedOtherTypeName;
if (otherTypeName.equals(typeName)) {
typeName = type.asQualifiedString();
otherTypeName = otherType.asQualifiedString();
expandedTypeName =
type.resolveAliases()
.asQualifiedString();
expandedOtherTypeName =
otherType.resolveAliases()
.asQualifiedString();
}
else {
expandedTypeName =
type.resolveAliases()
.asString(unit);
expandedOtherTypeName =
otherType.resolveAliases()
.asString(unit);
}
StringBuilder sb = new StringBuilder();
sb.append(": '")
.append(typeName)
.append("'");
if (!typeName.equals(expandedTypeName)) {
sb.append(" ('")
.append(expandedTypeName)
.append("')");
}
sb.append(problem);
sb.append("'")
.append(otherTypeName)
.append("'");
if (!otherTypeName.equals(expandedOtherTypeName)) {
sb.append(" ('")
.append(expandedOtherTypeName)
.append("')");
}
if (unknownTypeError!=null) {
sb.append(": ")
.append(unknownTypeError);
}
return sb.toString();
}
private static String message(Type type,
String problem, Unit unit) {
String typeName = type.asString(unit);
String expandedTypeName =
type.resolveAliases()
.asString(unit);
StringBuilder sb = new StringBuilder();
sb.append(": '")
.append(typeName)
.append("'");
if (!typeName.equals(expandedTypeName)) {
sb.append(" ('")
.append(expandedTypeName)
.append("')");
}
sb.append(problem);
return sb.toString();
}
static boolean checkCallable(Type type, Node node, String message) {
Unit unit = node.getUnit();
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
return false;
}
else if (!unit.isCallableType(type)) {
if (!hasError(node)) {
String extra =
message(type,
" is not a subtype of 'Callable'",
unit);
if (node instanceof Tree.StaticMemberOrTypeExpression) {
Tree.StaticMemberOrTypeExpression smte =
(Tree.StaticMemberOrTypeExpression) node;
Declaration d = smte.getDeclaration();
String name = d.getName();
if (d instanceof Interface) {
extra = ": '" + name + "' is an interface";
}
else if (d instanceof TypeAlias) {
extra = ": '" + name + "' is a type alias";
}
else if (d instanceof TypeParameter) {
extra = ": '" + name + "' is a type parameter";
}
else if (d instanceof Value) {
extra = ": value '" + name + "' has type '" +
type.asString(unit) +
"' which is not a subtype of 'Callable'";
}
}
node.addError(message + extra);
}
return false;
}
else {
return true;
}
}
static Type checkSupertype(Type type,
TypeDeclaration td, Node node, String message) {
return checkSupertype(type, false, td, node, message);
}
static Type checkSupertype(Type type, boolean unary,
TypeDeclaration td, Node node, String message) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
return null;
}
else {
Type supertype = type.getSupertype(td);
if (supertype==null) {
node.addError(message +
message(type,
" is not a subtype of '" +
td.getName() + "'",
node.getUnit()));
}
else if (!unary &&
!supertype.getVarianceOverrides()
.isEmpty()) {
node.addError(message +
message(type,
" does not have a principal instantiation for '" +
td.getName() + "'",
node.getUnit()));
}
return supertype;
}
}
static void checkAssignable(Type type,
Type supertype, Node node, String message) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
}
else if (isTypeUnknown(supertype)) {
addTypeUnknownError(node, supertype, message);
}
else if (!type.isSubtypeOf(supertype)) {
node.addError(message +
notAssignableMessage(type, supertype, node));
}
}
static void checkAssignableWithWarning(Type type,
Type supertype, Node node, String message) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
}
else if (isTypeUnknown(supertype)) {
addTypeUnknownError(node, supertype, message);
}
else if (!type.isSubtypeOf(supertype)) {
node.addUnsupportedError(message +
notAssignableMessage(type, supertype, node));
}
}
static void checkAssignableToOneOf(Type type,
Type supertype1, Type supertype2,
Node node, String message, int code) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
}
else if (isTypeUnknown(supertype1)) {
addTypeUnknownError(node, supertype1, message);
}
else if (isTypeUnknown(supertype2)) {
addTypeUnknownError(node, supertype2, message);
}
else if (!type.isSubtypeOf(supertype1)
&& !type.isSubtypeOf(supertype2)) {
node.addError(message +
notAssignableMessage(type, supertype1, node),
code);
}
}
static String notAssignableMessage(Type type,
Type supertype, Node node) {
return typingMessage(type,
" is not assignable to ",
supertype,
node.getUnit());
}
static void checkAssignable(Type type,
Type supertype, Node node,
String message, int code) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
}
else if (isTypeUnknown(supertype)) {
addTypeUnknownError(node, supertype, message);
}
else if (!type.isSubtypeOf(supertype)) {
node.addError(message +
notAssignableMessage(type, supertype, node),
code);
}
}
/*static void checkAssignable(Type type, Type supertype,
TypeDeclaration td, Node node, String message) {
if (isTypeUnknown(type) || isTypeUnknown(supertype)) {
addTypeUnknownError(node, message);
}
else if (!type.isSubtypeOf(supertype)) {
node.addError(message + message(type, " is not assignable to ", supertype, node.getUnit()));
}
}*/
static void checkIsExactly(Type type,
Type supertype, Node node, String message) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
}
else if (isTypeUnknown(supertype)) {
addTypeUnknownError(node, supertype, message);
}
else if (!type.isExactly(supertype)) {
node.addError(message +
notExactlyMessage(type, supertype, node));
}
}
static void checkIsExactly(Type type,
Type supertype, Node node, String message,
int code) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
}
else if (isTypeUnknown(supertype)) {
addTypeUnknownError(node, supertype, message);
}
else if (!type.isExactly(supertype)) {
node.addError(message +
notExactlyMessage(type, supertype, node),
code);
}
}
static void checkIsExactlyOneOf(Type type,
Type supertype1, Type supertype2,
Node node, String message) {
if (isTypeUnknown(type)) {
addTypeUnknownError(node, type, message);
}
else if (isTypeUnknown(supertype1)) {
addTypeUnknownError(node, supertype1, message);
}
else if (isTypeUnknown(supertype2)) {
addTypeUnknownError(node, supertype2, message);
}
else if (!type.isExactly(supertype1)
&& !type.isExactly(supertype2)) {
node.addError(message +
notExactlyMessage(type, supertype1, node));
}
}
static String notExactlyMessage(Type type,
Type supertype, Node node) {
return typingMessage(type,
" is not exactly ",
supertype,
node.getUnit());
}
private static void addTypeUnknownError(Node node,
Type type, String message) {
if (!hasError(node)) {
node.addError(message +
": type cannot be determined" +
getTypeUnknownError(type));
}
}
static String getTypeUnknownError(Type type) {
if (type == null) {
return "";
}
else {
String error = type.getFirstUnknownTypeError();
if (error != null) {
return ": " + error;
}
else {
return "";
}
}
}
static boolean inLanguageModule(Unit unit) {
return unit.getPackage()
.getQualifiedNameString()
.startsWith(Module.LANGUAGE_MODULE_NAME);
}
static String typeDescription(TypeDeclaration td, Unit unit) {
String name = td.getName();
if (td instanceof TypeParameter) {
Declaration container =
(Declaration) td.getContainer();
return "type parameter '" + name + "' of '" +
container.getName(unit) + "'";
}
else {
return "type '" + name + "'";
}
}
static String typeNamesAsIntersection(
List<Type> upperBounds, Unit unit) {
if (upperBounds.isEmpty()) {
return "Anything";
}
StringBuilder sb = new StringBuilder();
for (Type st: upperBounds) {
sb.append(st.asString(unit)).append(" & ");
}
if (sb.toString().endsWith(" & ")) {
sb.setLength(sb.length()-3);
}
return sb.toString();
}
public static boolean isAlwaysSatisfied(Tree.ConditionList cl) {
if (cl==null) return false;
for (Tree.Condition c: cl.getConditions()) {
if (c instanceof Tree.BooleanCondition) {
Tree.BooleanCondition bc =
(Tree.BooleanCondition) c;
Tree.Expression ex = bc.getExpression();
if (ex!=null) {
Tree.Term term =
unwrapExpressionUntilTerm(ex);
//TODO: take into account conjunctions/disjunctions
if (term instanceof Tree.BaseMemberExpression) {
Tree.BaseMemberExpression bme =
(Tree.BaseMemberExpression)
term;
Declaration d = bme.getDeclaration();
if (isBooleanTrue(d)) {
continue;
}
}
}
}
return false;
}
return true;
}
public static boolean isNeverSatisfied(Tree.ConditionList cl) {
if (cl==null) return false;
for (Tree.Condition c: cl.getConditions()) {
if (c instanceof Tree.BooleanCondition) {
Tree.BooleanCondition bc =
(Tree.BooleanCondition) c;
Tree.Expression ex =
bc.getExpression();
if (ex!=null) {
Tree.Term term =
unwrapExpressionUntilTerm(ex);
//TODO: take into account conjunctions/disjunctions
if (term instanceof Tree.BaseMemberExpression) {
Tree.BaseMemberExpression bme =
(Tree.BaseMemberExpression)
term;
Declaration d = bme.getDeclaration();
if (isBooleanFalse(d)) {
return true;
}
}
}
}
}
return false;
}
public static boolean isAtLeastOne(Tree.ForClause forClause) {
Tree.ForIterator fi = forClause.getForIterator();
if (fi!=null) {
Tree.SpecifierExpression se =
fi.getSpecifierExpression();
if (se!=null) {
Tree.Expression e = se.getExpression();
if (e!=null) {
Unit unit = forClause.getUnit();
Type at =
unit.getAnythingType();
Type neit =
unit.getNonemptyIterableType(at);
Type t = e.getTypeModel();
return t!=null && t.isSubtypeOf(neit);
}
}
}
return false;
}
static boolean declaredInPackage(Declaration dec, Unit unit) {
return dec.getUnit().getPackage().equals(unit.getPackage());
}
public static boolean isIndirectInvocation(Tree.InvocationExpression that) {
return isIndirectInvocation(that.getPrimary());
}
private static boolean isIndirectInvocation(Tree.Primary primary) {
Tree.Term term = unwrapExpressionUntilTerm(primary);
if (term instanceof Tree.MemberOrTypeExpression) {
Tree.MemberOrTypeExpression mte =
(Tree.MemberOrTypeExpression) term;
return isIndirectInvocation(mte);
}
else {
return true;
}
}
private static boolean isIndirectInvocation(
Tree.MemberOrTypeExpression that) {
Reference prf = that.getTarget();
if (prf==null) {
return true;
}
else {
Declaration d = prf.getDeclaration();
if (!prf.isFunctional() ||
//type parameters are not really callable
//even though they are Functional
d instanceof TypeParameter) {
return true;
}
if (that.getStaticMethodReference()) {
if (d.isStaticallyImportable() ||
isConstructor(d)) {
Tree.QualifiedMemberOrTypeExpression qmte =
(Tree.QualifiedMemberOrTypeExpression)
that;
return isIndirectInvocation(qmte.getPrimary());
}
else {
return true;
}
}
return false;
}
}
static String message(Declaration dec) {
String qualifier;
Scope container = dec.getContainer();
if (container instanceof Declaration) {
Declaration cd = (Declaration) container;
qualifier = " in '" + cd.getName() + "'";
}
else {
qualifier = "";
}
return "'" + dec.getName() + "'" + qualifier;
}
static Node getParameterTypeErrorNode(Tree.Parameter p) {
if (p instanceof Tree.ParameterDeclaration) {
Tree.ParameterDeclaration pd =
(Tree.ParameterDeclaration) p;
return pd.getTypedDeclaration().getType();
}
else {
return p;
}
}
public static Node getTypeErrorNode(Node that) {
if (that instanceof Tree.TypedDeclaration) {
Tree.TypedDeclaration td =
(Tree.TypedDeclaration) that;
Tree.Type type = td.getType();
if (type!=null) {
return type;
}
}
if (that instanceof Tree.TypedArgument) {
Tree.TypedArgument ta =
(Tree.TypedArgument) that;
Tree.Type type = ta.getType();
if (type!=null) {
return type;
}
}
if (that instanceof Tree.FunctionArgument) {
Tree.FunctionArgument fa =
(Tree.FunctionArgument) that;
Tree.Type type = fa.getType();
if (type!=null && type.getToken()!=null) {
return type;
}
}
return that;
}
static void checkIsExactlyForInterop(Unit unit,
boolean isCeylon,
Type parameterType,
Type refinedParameterType,
Node node, String message) {
if (isCeylon) {
// it must be a Ceylon method
checkIsExactly(parameterType,
refinedParameterType,
node, message, 9200);
}
else {
// we're refining a Java method
Type refinedDefiniteType =
unit.getDefiniteType(
refinedParameterType);
checkIsExactlyOneOf(parameterType,
refinedParameterType,
refinedDefiniteType,
node, message);
}
}
public static Type getTupleType(
List<Tree.PositionalArgument> es,
Unit unit,
boolean requireSequential) {
Type result = unit.getEmptyType();
Type ut = unit.getNothingType();
Class td = unit.getTupleDeclaration();
Interface id = unit.getIterableDeclaration();
for (int i=es.size()-1; i>=0; i--) {
Tree.PositionalArgument a = es.get(i);
Type t = a.getTypeModel();
if (t!=null) {
Type et = t; //unit.denotableType(t);
if (a instanceof Tree.SpreadArgument) {
/*if (requireSequential) {
checkSpreadArgumentSequential((Tree.SpreadArgument) a, et);
}*/
ut = unit.getIteratedType(et);
result = spreadType(et, unit, requireSequential);
}
else if (a instanceof Tree.Comprehension) {
ut = et;
Tree.Comprehension c = (Tree.Comprehension) a;
Tree.InitialComprehensionClause icc =
c.getInitialComprehensionClause();
result = icc.getPossiblyEmpty() ?
unit.getSequentialType(et) :
unit.getSequenceType(et);
if (!requireSequential) {
Type it =
appliedType(id, et,
icc.getFirstTypeModel());
result = intersectionType(result, it, unit);
}
}
else {
ut = unionType(ut, et, unit);
result = appliedType(td, ut, et, result);
}
}
}
return result;
}
static Type spreadType(Type et, Unit unit,
boolean requireSequential) {
if (et==null) return null;
if (requireSequential) {
if (unit.isSequentialType(et)) {
//if (et.isTypeParameter()) {
return et;
/*}
else {
// if it's already a subtype of Sequential, erase
// out extraneous information, like that it is a
// String, just keeping information about what
// kind of tuple it is
List<Type> elementTypes = unit.getTupleElementTypes(et);
boolean variadic = unit.isTupleLengthUnbounded(et);
boolean atLeastOne = unit.isTupleVariantAtLeastOne(et);
int minimumLength = unit.getTupleMinimumLength(et);
if (variadic) {
Type spt = elementTypes.get(elementTypes.size()-1);
elementTypes.set(elementTypes.size()-1, unit.getIteratedType(spt));
}
return unit.getTupleType(elementTypes, variadic,
atLeastOne, minimumLength);
}*/
}
else {
// transform any Iterable into a Sequence without
// losing the information that it is nonempty, in
// the case that we know that for sure
Type it = unit.getIteratedType(et);
Type st =
unit.isNonemptyIterableType(et) ?
unit.getSequenceType(it) :
unit.getSequentialType(it);
// unless this is a tuple constructor, remember
// the original Iterable type arguments, to
// account for the possibility that the argument
// to Absent is a type parameter
//return intersectionType(et.getSupertype(unit.getIterableDeclaration()), st, unit);
// for now, just return the sequential type:
return st;
}
}
else {
return et;
}
}
static boolean setTypeConstructor(Tree.Type t,
TypeParameter typeParam) {
Type pt = t.getTypeModel();
if (pt == null) {
return false;
}
else {
/*if (t instanceof Tree.IntersectionType) {
Tree.IntersectionType it =
(Tree.IntersectionType) t;
for (Tree.StaticType st: it.getStaticTypes()) {
if (setTypeConstructor(st,typeParam)) {
pt.setTypeConstructor(true);
pt.setTypeConstructorParameter(typeParam);
}
}
}
else if (t instanceof Tree.UnionType) {
Tree.UnionType it =
(Tree.UnionType) t;
for (Tree.StaticType st: it.getStaticTypes()) {
if (setTypeConstructor(st,typeParam)) {
pt.setTypeConstructor(true);
pt.setTypeConstructorParameter(typeParam);