/
TypeCheckerStmVisitor.java
1556 lines (1312 loc) · 52.7 KB
/
TypeCheckerStmVisitor.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
/*
* #%~
* The VDM Type Checker
* %%
* Copyright (C) 2008 - 2014 Overture
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #~%
*/
package org.overture.typechecker.visitor;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.Map.Entry;
import org.overture.ast.analysis.AnalysisException;
import org.overture.ast.analysis.intf.IQuestionAnswer;
import org.overture.ast.definitions.AExplicitOperationDefinition;
import org.overture.ast.definitions.AImplicitOperationDefinition;
import org.overture.ast.definitions.AInstanceVariableDefinition;
import org.overture.ast.definitions.AMultiBindListDefinition;
import org.overture.ast.definitions.PDefinition;
import org.overture.ast.definitions.SClassDefinition;
import org.overture.ast.expressions.ABooleanConstExp;
import org.overture.ast.expressions.ASelfExp;
import org.overture.ast.expressions.AVariableExp;
import org.overture.ast.expressions.PExp;
import org.overture.ast.factory.AstFactory;
import org.overture.ast.intf.lex.ILexNameToken;
import org.overture.ast.lex.Dialect;
import org.overture.ast.lex.LexNameToken;
import org.overture.ast.lex.LexStringToken;
import org.overture.ast.patterns.ADefPatternBind;
import org.overture.ast.patterns.AExpressionPattern;
import org.overture.ast.patterns.ASetBind;
import org.overture.ast.patterns.ATypeBind;
import org.overture.ast.patterns.PBind;
import org.overture.ast.statements.AAlwaysStm;
import org.overture.ast.statements.AAssignmentStm;
import org.overture.ast.statements.AAtomicStm;
import org.overture.ast.statements.ABlockSimpleBlockStm;
import org.overture.ast.statements.ACallObjectStm;
import org.overture.ast.statements.ACallStm;
import org.overture.ast.statements.ACaseAlternativeStm;
import org.overture.ast.statements.ACasesStm;
import org.overture.ast.statements.AClassInvariantStm;
import org.overture.ast.statements.ACyclesStm;
import org.overture.ast.statements.ADurationStm;
import org.overture.ast.statements.AElseIfStm;
import org.overture.ast.statements.AErrorCase;
import org.overture.ast.statements.AErrorStm;
import org.overture.ast.statements.AExitStm;
import org.overture.ast.statements.AExternalClause;
import org.overture.ast.statements.AForAllStm;
import org.overture.ast.statements.AForIndexStm;
import org.overture.ast.statements.AForPatternBindStm;
import org.overture.ast.statements.AIdentifierStateDesignator;
import org.overture.ast.statements.AIfStm;
import org.overture.ast.statements.ALetBeStStm;
import org.overture.ast.statements.ALetStm;
import org.overture.ast.statements.ANonDeterministicSimpleBlockStm;
import org.overture.ast.statements.ANotYetSpecifiedStm;
import org.overture.ast.statements.APeriodicStm;
import org.overture.ast.statements.AReturnStm;
import org.overture.ast.statements.ASkipStm;
import org.overture.ast.statements.ASpecificationStm;
import org.overture.ast.statements.ASporadicStm;
import org.overture.ast.statements.AStartStm;
import org.overture.ast.statements.AStopStm;
import org.overture.ast.statements.ASubclassResponsibilityStm;
import org.overture.ast.statements.ATixeStm;
import org.overture.ast.statements.ATixeStmtAlternative;
import org.overture.ast.statements.ATrapStm;
import org.overture.ast.statements.AWhileStm;
import org.overture.ast.statements.PStateDesignator;
import org.overture.ast.statements.PStm;
import org.overture.ast.statements.SSimpleBlockStm;
import org.overture.ast.typechecker.NameScope;
import org.overture.ast.types.ABooleanBasicType;
import org.overture.ast.types.AClassType;
import org.overture.ast.types.AFunctionType;
import org.overture.ast.types.AOperationType;
import org.overture.ast.types.ASeq1SeqType;
import org.overture.ast.types.ASet1SetType;
import org.overture.ast.types.SSetType;
import org.overture.ast.types.AUnionType;
import org.overture.ast.types.AUnknownType;
import org.overture.ast.types.AVoidReturnType;
import org.overture.ast.types.AVoidType;
import org.overture.ast.types.PType;
import org.overture.ast.util.PTypeSet;
import org.overture.config.Settings;
import org.overture.typechecker.Environment;
import org.overture.typechecker.FlatCheckedEnvironment;
import org.overture.typechecker.FlatEnvironment;
import org.overture.typechecker.PrivateClassEnvironment;
import org.overture.typechecker.PublicClassEnvironment;
import org.overture.typechecker.TypeCheckInfo;
import org.overture.typechecker.TypeCheckerErrors;
import org.overture.typechecker.assistant.type.AClassTypeAssistantTC;
import org.overture.typechecker.utilities.type.QualifiedDefinition;
public class TypeCheckerStmVisitor extends AbstractTypeCheckVisitor
{
public TypeCheckerStmVisitor(
IQuestionAnswer<TypeCheckInfo, PType> typeCheckVisitor)
{
super(typeCheckVisitor);
}
@Override
public PType caseAAlwaysStm(AAlwaysStm node, TypeCheckInfo question)
throws AnalysisException
{
node.getAlways().apply(THIS, question);
node.setType(node.getBody().apply(THIS, question));
return node.getType();
}
@Override
public PType caseAAssignmentStm(AAssignmentStm node, TypeCheckInfo question)
throws AnalysisException
{
node.setTargetType(node.getTarget().apply(THIS, new TypeCheckInfo(question.assistantFactory, question.env)));
node.setExpType(node.getExp().apply(THIS, new TypeCheckInfo(question.assistantFactory, question.env, question.scope, null, node.getTargetType(), null)));
if (!question.assistantFactory.getTypeComparator().compatible(node.getTargetType(), node.getExpType()))
{
TypeCheckerErrors.report(3239, "Incompatible types in assignment", node.getLocation(), node);
TypeCheckerErrors.detail2("Target", node.getTarget().getType(), "Expression", node.getExp().getType());
}
node.setClassDefinition(question.env.findClassDefinition());
node.setStateDefinition(question.env.findStateDefinition());
AClassTypeAssistantTC assist = question.assistantFactory.createAClassTypeAssistant();
node.setInConstructor(assist.inConstructor(question.env));
if (node.getInConstructor())
{
// Mark assignment target as initialized (so no warnings)
PDefinition state;
state = targetDefinition(node.getTarget(), question);
if (state instanceof AInstanceVariableDefinition)
{
AInstanceVariableDefinition iv = (AInstanceVariableDefinition) state;
iv.setInitialized(true);
}
}
node.setType(AstFactory.newAVoidType(node.getLocation()));
return node.getType();
}
@Override
public PType caseAAtomicStm(AAtomicStm node, TypeCheckInfo question)
throws AnalysisException
{
for (AAssignmentStm stmt : node.getAssignments())
{
stmt.apply(THIS, question);
}
node.setType(AstFactory.newAVoidType(node.getLocation()));
return node.getType();
}
@Override
public PType caseAForPatternBindStm(AForPatternBindStm node,
TypeCheckInfo question) throws AnalysisException
{
PType stype = node.getExp().apply(THIS, new TypeCheckInfo(question.assistantFactory, question.env, question.scope));
Environment local = question.env;
if (question.assistantFactory.createPTypeAssistant().isSeq(stype))
{
node.setSeqType(question.assistantFactory.createPTypeAssistant().getSeq(stype));
node.getPatternBind().apply(THIS, new TypeCheckInfo(question.assistantFactory, question.env, question.scope));
List<PDefinition> defs = getDefinitions(node.getPatternBind());
question.assistantFactory.createPDefinitionListAssistant().typeCheck(defs, THIS, new TypeCheckInfo(question.assistantFactory, question.env, question.scope));
local = new FlatCheckedEnvironment(question.assistantFactory, defs, question.env, question.scope);
PType rt = node.getStatement().apply(THIS, new TypeCheckInfo(question.assistantFactory, local, question.scope));
if (!(node.getSeqType() instanceof ASeq1SeqType))
{
// Add () to the return type, as we may not enter the loop at all
rt = AstFactory.newAUnionType(node.getLocation(), rt, AstFactory.newAVoidType(node.getLocation()));
}
local.unusedCheck();
node.setType(rt);
return rt;
}
else
{
TypeCheckerErrors.report(3223, "Expecting sequence type after 'in'", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
}
@Override
public PType defaultSSimpleBlockStm(SSimpleBlockStm node,
TypeCheckInfo question) throws AnalysisException
{
boolean notreached = false;
PTypeSet rtypes = new PTypeSet(question.assistantFactory);
PType last = null;
for (PStm stmt : node.getStatements())
{
PType stype = stmt.apply(THIS, question);
if (notreached)
{
TypeCheckerErrors.warning(5006, "Statement will not be reached", stmt.getLocation(), stmt);
} else
{
last = stype;
notreached = true;
if (stype instanceof AUnionType)
{
AUnionType ust = (AUnionType) stype;
for (PType t : ust.getTypes())
{
addOneType(rtypes, t);
if (t instanceof AVoidType || t instanceof AUnknownType)
{
notreached = false;
}
}
} else
{
addOneType(rtypes, stype);
if (stype instanceof AVoidType
|| stype instanceof AUnknownType)
{
notreached = false;
}
}
}
}
// If the last statement reached has a void component, add this to the
// overall
// return type, as the block may return nothing.
if (last != null
&& (question.assistantFactory.createPTypeAssistant().isType(last, AVoidType.class) || question.assistantFactory.createPTypeAssistant().isUnknown(last)))
{
rtypes.add(AstFactory.newAVoidType(node.getLocation()));
}
node.setType(rtypes.isEmpty() ? AstFactory.newAVoidType(node.getLocation())
: rtypes.getType(node.getLocation()));
return node.getType();
}
@Override
public PType caseABlockSimpleBlockStm(ABlockSimpleBlockStm node,
TypeCheckInfo question) throws AnalysisException
{
// Each dcl definition is in scope for later definitions...
Environment local = question.env;
for (PDefinition d : node.getAssignmentDefs())
{
local = new FlatCheckedEnvironment(question.assistantFactory, d, local, question.scope); // cumulative
question.assistantFactory.createPDefinitionAssistant().implicitDefinitions(d, local);
d.setType(question.assistantFactory.createPTypeAssistant().typeResolve(d.getType(), null, THIS, question));
d.apply(THIS, new TypeCheckInfo(question.assistantFactory, local, question.scope, question.qualifiers, d.getType(), question.returnType));
}
// For type checking purposes, the definitions are treated as
// local variables. At runtime (below) they have to be treated
// more like (updatable) state.
PType r = defaultSSimpleBlockStm(node, new TypeCheckInfo(question.assistantFactory, local, question.scope, null, null, question.returnType));
local.unusedCheck(question.env);
node.setType(r);
return r;
}
@Override
public PType caseACallObjectStm(ACallObjectStm node, TypeCheckInfo question)
throws AnalysisException
{
PType dtype = node.getDesignator().apply(THIS, question);
if (!question.assistantFactory.createPTypeAssistant().isClass(dtype, question.env))
{
TypeCheckerErrors.report(3207, "Object designator is not an object type", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
AClassType ctype = question.assistantFactory.createPTypeAssistant().getClassType(dtype, question.env);
SClassDefinition classdef = ctype.getClassdef();
SClassDefinition self = question.env.findClassDefinition();
Environment classenv = null;
if (self == classdef
|| question.assistantFactory.createPDefinitionAssistant().hasSupertype(self, question.assistantFactory.createPDefinitionAssistant().getType(classdef)))
{
// All fields visible. Note that protected fields are inherited
// into "locals" so they are effectively private
classenv = new PrivateClassEnvironment(question.assistantFactory, self);
} else
{
// Only public fields externally visible
classenv = new PublicClassEnvironment(question.assistantFactory, classdef);
}
if (node.getClassname() == null)
{
node.setField(new LexNameToken(ctype.getName().getName(), node.getFieldname().getName(), node.getFieldname().getLocation()));
} else
{
node.setField(node.getClassname());
}
node.getField().getLocation().executable(true);
List<PType> atypes = getArgTypes(node.getArgs(), THIS, question);
node.getField().setTypeQualifier(atypes);
PDefinition fdef = classenv.findName(node.getField(), question.scope);
AClassTypeAssistantTC assist = question.assistantFactory.createAClassTypeAssistant();
if (assist.isConstructor(fdef) && !assist.inConstructor(question.env))
{
TypeCheckerErrors.report(3337, "Cannot call a constructor from here", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
// Special code for the deploy method of CPU
if (Settings.dialect == Dialect.VDM_RT
&& node.getField().getModule().equals("CPU")
&& node.getField().getName().equals("deploy"))
{
if (!question.assistantFactory.createPTypeAssistant().isType(atypes.get(0), AClassType.class))
{
TypeCheckerErrors.report(3280, "Argument to deploy must be an object", node.getArgs().get(0).getLocation(), node.getArgs().get(0));
}
node.setType(AstFactory.newAVoidType(node.getLocation()));
return node.getType();
} else if (Settings.dialect == Dialect.VDM_RT
&& node.getField().getModule().equals("CPU")
&& node.getField().getName().equals("setPriority"))
{
if (!(atypes.get(0) instanceof AOperationType))
{
TypeCheckerErrors.report(3290, "Argument to setPriority must be an operation", node.getArgs().get(0).getLocation(), node.getArgs().get(0));
} else
{
// Convert the variable expression to a string...
AVariableExp a1 = (AVariableExp) node.getArgs().get(0);
node.getArgs().remove(0);
node.getArgs().add(0, AstFactory.newAStringLiteralExp(new LexStringToken(a1.getName().getExplicit(true).getFullName(), a1.getLocation())));
if (a1.getName().getModule().equals(a1.getName().getName())) // it's a
// constructor
{
TypeCheckerErrors.report(3291, "Argument to setPriority cannot be a constructor", node.getArgs().get(0).getLocation(), node.getArgs().get(0));
}
}
node.setType(AstFactory.newAVoidType(node.getLocation()));
return node.getType();
} else if (fdef == null)
{
TypeCheckerErrors.report(3209, "Member " + node.getField()
+ " is not in scope", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
} else
{
question.assistantFactory.createPDefinitionAssistant();
if (question.assistantFactory.createPDefinitionAssistant().isStatic(fdef)
&& !question.env.isStatic())
{
// warning(5005, "Should invoke member " + field +
// " from a static context");
}
}
PType type = question.assistantFactory.createPDefinitionAssistant().getType(fdef);
if (question.assistantFactory.createPTypeAssistant().isOperation(type))
{
AOperationType optype = question.assistantFactory.createPTypeAssistant().getOperation(type);
PDefinition encl = question.env.getEnclosingDefinition();
if (encl != null && encl.getAccess().getPure() && !optype.getPure())
{
TypeCheckerErrors.report(3339, "Cannot call impure operation from a pure operation", node.getLocation(), node);
}
optype.apply(THIS, question);
node.getField().setTypeQualifier(optype.getParameters());
checkArgTypes(type, optype.getParameters(), atypes, question); // Not
// necessary?
node.setType(optype.getResult());
return question.assistantFactory.createPTypeAssistant().checkReturnType(question.returnType, node.getType(), node.getLocation());
} else if (question.assistantFactory.createPTypeAssistant().isFunction(type))
{
// This is the case where a function is called as an operation
// without
// a "return" statement.
AFunctionType ftype = question.assistantFactory.createPTypeAssistant().getFunction(type);
ftype.apply(THIS, question);
node.getField().setTypeQualifier(ftype.getParameters());
checkArgTypes(type, ftype.getParameters(), atypes, question); // Not
// necessary?
node.setType(ftype.getResult());
return question.assistantFactory.createPTypeAssistant().checkReturnType(question.returnType, node.getType(), node.getLocation());
} else
{
TypeCheckerErrors.report(3210, "Object member is neither a function nor an operation", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
}
@Override
public PType caseACallStm(ACallStm node, TypeCheckInfo question)
throws AnalysisException
{
List<PType> atypes = getArgTypes(node.getArgs(), THIS, question);
if (question.env.isVDMPP())
{
node.getName().setTypeQualifier(atypes);
}
PDefinition opdef = question.env.findName(node.getName(), question.scope);
node.setRootdef(opdef);
if (opdef == null)
{
TypeCheckerErrors.report(3213, "Operation " + node.getName()
+ " is not in scope", node.getLocation(), node);
question.env.listAlternatives(node.getName());
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
if (question.env.isVDMPP() && node.getName().getExplicit())
{
// A call like X`op() is local if X is in our hierarchy
// else it's a static call of a different class.
SClassDefinition self = question.env.findClassDefinition();
PType ctype = opdef.getClassDefinition().getType();
if (!question.assistantFactory.createPDefinitionAssistant().hasSupertype(self, ctype)
&& opdef.getAccess().getStatic() == null)
{
TypeCheckerErrors.report(3324, "Operation " + node.getName()
+ " is not static", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
}
AClassTypeAssistantTC assist = question.assistantFactory.createAClassTypeAssistant();
if (assist.isConstructor(opdef) && !assist.inConstructor(question.env))
{
TypeCheckerErrors.report(3337, "Cannot call a constructor from here", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
if (!question.assistantFactory.createPDefinitionAssistant().isStatic(opdef)
&& question.env.isStatic())
{
TypeCheckerErrors.report(3214, "Cannot call " + node.getName()
+ " from static context", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
PType type = question.assistantFactory.createPDefinitionAssistant().getType(opdef);
if (question.assistantFactory.createPTypeAssistant().isOperation(type))
{
AOperationType optype = question.assistantFactory.createPTypeAssistant().getOperation(type);
question.assistantFactory.createPTypeAssistant().typeResolve(optype, null, THIS, question);
PDefinition encl = question.env.getEnclosingDefinition();
if (encl != null && encl.getAccess().getPure() && !optype.getPure())
{
TypeCheckerErrors.report(3339, "Cannot call impure operation from a pure operation", node.getLocation(), node);
}
// Reset the name's qualifier with the actual operation type so
// that runtime search has a simple TypeComparator call.
if (question.env.isVDMPP())
{
node.getName().setTypeQualifier(optype.getParameters());
}
checkArgTypes(node, optype, optype.getParameters(), atypes, question);
node.setType(optype.getResult());
return question.assistantFactory.createPTypeAssistant().checkReturnType(question.returnType, optype.getResult(), node.getLocation());
} else if (question.assistantFactory.createPTypeAssistant().isFunction(type))
{
// This is the case where a function is called as an operation
// without
// a "return" statement.
AFunctionType ftype = question.assistantFactory.createPTypeAssistant().getFunction(type);
question.assistantFactory.createPTypeAssistant().typeResolve(ftype, null, THIS, question);
// Reset the name's qualifier with the actual function type so
// that runtime search has a simple TypeComparator call.
if (question.env.isVDMPP())
{
node.getName().setTypeQualifier(ftype.getParameters());
}
checkArgTypes(node, ftype, ftype.getParameters(), atypes, question);
node.setType(ftype.getResult());
return question.assistantFactory.createPTypeAssistant().checkReturnType(question.returnType, ftype.getResult(), node.getLocation());
} else
{
TypeCheckerErrors.report(3210, "Name is neither a function nor an operation", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
}
// TODO correct, possibly wrong typecheck implementation
@Override
public PType caseACaseAlternativeStm(ACaseAlternativeStm node,
TypeCheckInfo question) throws AnalysisException
{
if (node.getDefs().size() == 0)
{
node.setDefs(new LinkedList<PDefinition>());
question.assistantFactory.createPPatternAssistant().typeResolve(node.getPattern(), THIS, question);
if (node.getPattern() instanceof AExpressionPattern)
{
// Only expression patterns need type checking...
AExpressionPattern ep = (AExpressionPattern) node.getPattern();
PType ptype = ep.getExp().apply(THIS, question);
if (!question.assistantFactory.getTypeComparator().compatible(ptype, node.getCtype()))
{
TypeCheckerErrors.report(3311, "Pattern cannot match", node.getPattern().getLocation(), node.getPattern());
}
}
question.assistantFactory.createPPatternAssistant().typeResolve(node.getPattern(), THIS, question);
ACasesStm stm = (ACasesStm) node.parent();
node.getDefs().addAll(question.assistantFactory.createPPatternAssistant().getDefinitions(node.getPattern(), stm.getExp().getType(), NameScope.LOCAL));
}
question.assistantFactory.createPDefinitionListAssistant().typeCheck(node.getDefs(), THIS, question);
if (!question.assistantFactory.createPPatternAssistant().matches(node.getPattern(), node.getCtype()))
{
TypeCheckerErrors.report(3311, "Pattern cannot match", node.getPattern().getLocation(), node.getPattern());
}
Environment local = new FlatCheckedEnvironment(question.assistantFactory, node.getDefs(), question.env, question.scope);
PType r = node.getResult().apply(THIS, new TypeCheckInfo(question.assistantFactory, local, question.scope, null, null, question.returnType));
local.unusedCheck();
return r;
}
@Override
public PType caseACasesStm(ACasesStm node, TypeCheckInfo question)
throws AnalysisException
{
PType expType = node.getExp().apply(THIS, question);
PTypeSet rtypes = new PTypeSet(question.assistantFactory);
boolean always = false;
for (ACaseAlternativeStm c : node.getCases())
{
c.setCtype(expType);
rtypes.add(c.apply(THIS, question));
always = always || question.assistantFactory.createPPatternAssistant().alwaysMatches(c.getPattern(), expType);
}
if (node.getOthers() != null)
{
rtypes.add(node.getOthers().apply(THIS, question));
}
else if (!always)
{
rtypes.add(AstFactory.newAVoidType(node.getLocation()));
}
node.setType(rtypes.getType(node.getLocation()));
return node.getType();
}
@Override
public PType caseAClassInvariantStm(AClassInvariantStm node,
TypeCheckInfo question)
{
// Definitions already checked.
node.setType(AstFactory.newABooleanBasicType(node.getLocation()));
return node.getType();
}
@Override
public PType caseACyclesStm(ACyclesStm node, TypeCheckInfo question)
throws AnalysisException
{
PDefinition encl = question.env.getEnclosingDefinition();
if (encl != null && encl.getAccess().getPure())
{
TypeCheckerErrors.report(3346, "Cannot use cycles in pure operations", node.getLocation(), node);
}
Environment newEnv = new FlatEnvironment(question.assistantFactory, question.env, true);
TypeCheckInfo functional = question.newInfo(newEnv);
PType argType = node.getCycles().apply(THIS, functional);
if (!question.assistantFactory.getTypeComparator().compatible(AstFactory.newANatNumericBasicType(node.getLocation()), argType))
{
TypeCheckerErrors.report(3282, "Arguments to cycles must be a nat", node.getLocation(), node);
TypeCheckerErrors.detail("Actual", argType);
}
return node.getStatement().apply(THIS, question);
}
// TODO: Missing the other DefStatement
@Override
public PType caseALetStm(ALetStm node, TypeCheckInfo question)
throws AnalysisException
{
node.setType(typeCheckLet(node, node.getLocalDefs(),node.getStatement(),question));
return node.getType();
}
@Override
public PType caseADurationStm(ADurationStm node, TypeCheckInfo question)
throws AnalysisException
{
PDefinition encl = question.env.getEnclosingDefinition();
if (encl != null && encl.getAccess().getPure())
{
TypeCheckerErrors.report(3346, "Cannot use duration in pure operations", node.getLocation(), node);
}
Environment newEnv = new FlatEnvironment(question.assistantFactory, question.env, true);
TypeCheckInfo functional = question.newInfo(newEnv);
PType argType = node.getDuration().apply(THIS, functional);
if (!question.assistantFactory.getTypeComparator().compatible(AstFactory.newANatNumericBasicType(node.getLocation()), argType))
{
TypeCheckerErrors.report(3281, "Arguments to duration must be a nat", node.getLocation(), node);
TypeCheckerErrors.detail("Actual", argType);
}
return node.getStatement().apply(THIS, question);
}
@Override
public PType caseAElseIfStm(AElseIfStm node, TypeCheckInfo question)
throws AnalysisException
{
node.setType(typeCheckAElseIf(node, node.getLocation(), node.getElseIf(), node.getThenStm(), question));
return node.getType();
}
@Override
public PType caseAErrorStm(AErrorStm node, TypeCheckInfo question)
{
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType(); // Because we terminate anyway
}
@Override
public PType caseAExitStm(AExitStm node, TypeCheckInfo question)
throws AnalysisException
{
if (node.getExpression() != null)
{
node.setExpType(node.getExpression().apply(THIS, question));
}
// This is unknown because the statement doesn't actually return a
// value - so if this is the only statement in a body, it is not a
// type error (should return the same type as the definition return
// type).
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
@Override
public PType caseAForAllStm(AForAllStm node, TypeCheckInfo question)
throws AnalysisException
{
node.setType(node.getSet().apply(THIS, question));
question.assistantFactory.createPPatternAssistant().typeResolve(node.getPattern(), THIS, question);
if (question.assistantFactory.createPTypeAssistant().isSet(node.getType()))
{
SSetType st = question.assistantFactory.createPTypeAssistant().getSet(node.getType());
List<PDefinition> defs = question.assistantFactory.createPPatternAssistant().getDefinitions(node.getPattern(), st.getSetof(), NameScope.LOCAL);
Environment local = new FlatCheckedEnvironment(question.assistantFactory, defs, question.env, question.scope);
PType rt = node.getStatement().apply(THIS, new TypeCheckInfo(question.assistantFactory, local, question.scope));
if (!(st instanceof ASet1SetType))
{
// Add () to the return type, as we may not enter the loop at all
rt = AstFactory.newAUnionType(node.getLocation(), rt, AstFactory.newAVoidType(node.getLocation()));
}
local.unusedCheck();
node.setType(rt);
return rt;
} else
{
TypeCheckerErrors.report(3219, "For all statement does not contain a set type", node.getLocation(), node);
node.setType(AstFactory.newAUnknownType(node.getLocation()));
return node.getType();
}
}
@Override
public PType caseAForIndexStm(AForIndexStm node, TypeCheckInfo question)
throws AnalysisException
{
PType ft = node.getFrom().apply(THIS, question);
PType tt = node.getTo().apply(THIS, question);
if (!question.assistantFactory.createPTypeAssistant().isNumeric(ft))
{
TypeCheckerErrors.report(3220, "From type is not numeric", node.getLocation(), node);
}
if (!question.assistantFactory.createPTypeAssistant().isNumeric(tt))
{
TypeCheckerErrors.report(3221, "To type is not numeric", node.getLocation(), node);
}
if (node.getBy() != null)
{
PType bt = node.getBy().apply(THIS, question);
if (!question.assistantFactory.createPTypeAssistant().isNumeric(bt))
{
TypeCheckerErrors.report(3222, "By type is not numeric", node.getLocation(), node);
}
}
PDefinition vardef = AstFactory.newALocalDefinition(node.getVar().getLocation(), node.getVar(), NameScope.LOCAL, ft);
Environment local = new FlatCheckedEnvironment(question.assistantFactory, vardef, question.env, question.scope);
PType rt = node.getStatement().apply(THIS, new TypeCheckInfo(question.assistantFactory, local, question.scope));
AUnionType rtu = AstFactory.newAUnionType(node.getLocation(), rt, AstFactory.newAVoidType(node.getLocation()));
local.unusedCheck();
node.setType(rtu);
return rtu;
}
@Override
public PType caseAIfStm(AIfStm node, TypeCheckInfo question)
throws AnalysisException
{
node.setType(typeCheckIf(node.getLocation(), node.getIfExp(), node.getThenStm(), node.getElseIf(), node.getElseStm(), question));//rtypes.getType(node.getLocation()));
return node.getType();
}
@Override
public PType caseALetBeStStm(ALetBeStStm node, TypeCheckInfo question)
throws AnalysisException
{
Entry<PType, AMultiBindListDefinition> res = typecheckLetBeSt(node, node.getLocation(), node.getBind(), node.getSuchThat(), node.getStatement(), question);
node.setDef(res.getValue());
node.setType(res.getKey());
return node.getType();
}
@Override
public PType caseANonDeterministicSimpleBlockStm(
ANonDeterministicSimpleBlockStm node, TypeCheckInfo question)
throws AnalysisException
{
PDefinition encl = question.env.getEnclosingDefinition();
if (encl != null && encl.getAccess().getPure())
{
TypeCheckerErrors.report(3346, "Cannot use non-deterministic statement in pure operations",
node.getLocation(), node);
}
PTypeSet rtypes = new PTypeSet(question.assistantFactory);
int rcount = 0;
for (PStm stmt : node.getStatements())
{
PType stype = stmt.apply(THIS, question);
if (question.assistantFactory.createPTypeAssistant().isType(stype, AUnionType.class))
{
AUnionType ust = (AUnionType) stype;
for (PType t : ust.getTypes())
{
if (addOne(rtypes, t))
{
rcount++;
}
}
} else
{
if (addOne(rtypes, stype))
{
rcount++;
}
}
}
if (rcount > 1)
{
TypeCheckerErrors.warning(5016, "Some statements will not be reached", node.getLocation(), node);
}
node.setType(rtypes.isEmpty() ? AstFactory.newAVoidType(node.getLocation())
: rtypes.getType(node.getLocation()));
return node.getType();
}
@Override
public PType caseANotYetSpecifiedStm(ANotYetSpecifiedStm node,
TypeCheckInfo question)
{
node.setType(typeCheckANotYetSpecifiedExp(node,node.getLocation()));
return node.getType();
}
@Override
public PType caseAReturnStm(AReturnStm node, TypeCheckInfo question)
throws AnalysisException
{
PDefinition encl = question.env.getEnclosingDefinition();
boolean inConstructor = false;
if (encl instanceof AExplicitOperationDefinition)
{
AExplicitOperationDefinition op = (AExplicitOperationDefinition) encl;
inConstructor = op.getIsConstructor();
} else if (encl instanceof AImplicitOperationDefinition)
{
AImplicitOperationDefinition op = (AImplicitOperationDefinition) encl;
inConstructor = op.getIsConstructor();
}
if (inConstructor && !(node.getExpression() instanceof ASelfExp))
{
TypeCheckerErrors.report(3326, "Constructor can only return 'self'", node.getLocation(), node);
}
if (node.getExpression() == null)
{
node.setType(AstFactory.newAVoidReturnType(node.getLocation()));
} else
{
node.setType(node.getExpression().apply(THIS, question));
}
return question.assistantFactory.createPTypeAssistant().checkReturnType(question.returnType, node.getType(), node.getLocation());
}
@Override
public PType caseASkipStm(ASkipStm node, TypeCheckInfo question)
{
node.setType(AstFactory.newAVoidType(node.getLocation()));
return node.getType();
}
@Override
public PType caseASpecificationStm(ASpecificationStm node,
TypeCheckInfo question) throws AnalysisException
{
List<PDefinition> defs = new LinkedList<PDefinition>();
// Now we build local definitions for each of the externals, so
// that they can be added to the local environment, while the
// global state is made inaccessible.
if (node.getExternals() != null)
{
for (AExternalClause clause : node.getExternals())
{
for (ILexNameToken name : clause.getIdentifiers())
{
if (question.env.findName(name, NameScope.STATE) == null)
{
TypeCheckerErrors.report(3274, "External variable is not in scope: "
+ name, name.getLocation(), name);
} else
{
defs.add(AstFactory.newALocalDefinition(name.getLocation(), name, NameScope.STATE, clause.getType()));
}
}
}
}
if (node.getErrors() != null)
{
for (AErrorCase err : node.getErrors())
{
PType lt = err.getLeft().apply(THIS, question);
PType rt = err.getRight().apply(THIS, question);
if (!question.assistantFactory.createPTypeAssistant().isType(lt, ABooleanBasicType.class))
{
TypeCheckerErrors.report(3275, "Error clause must be a boolean", err.getLeft().getLocation(), err.getLeft());
}
if (!question.assistantFactory.createPTypeAssistant().isType(rt, ABooleanBasicType.class))
{
TypeCheckerErrors.report(3275, "Error clause must be a boolean", err.getRight().getLocation(), err.getRight());
}
}
}
question.assistantFactory.createPDefinitionListAssistant().typeCheck(defs, THIS, question);
Environment local = new FlatEnvironment(question.assistantFactory, defs, question.env); // NB. No
// check
// //Unused
if (node.getPrecondition() != null
&& !question.assistantFactory.createPTypeAssistant().isType(node.getPrecondition().apply(THIS, new TypeCheckInfo(question.assistantFactory, local, NameScope.NAMESANDSTATE)), ABooleanBasicType.class))
{
TypeCheckerErrors.report(3233, "Precondition is not a boolean expression", node.getPrecondition().getLocation(), node.getPrecondition());
}
if (node.getPostcondition() != null
&& !question.assistantFactory.createPTypeAssistant().isType(node.getPostcondition().apply(THIS, new TypeCheckInfo(question.assistantFactory, local, NameScope.NAMESANDANYSTATE)), ABooleanBasicType.class))
{
TypeCheckerErrors.report(3234, "Postcondition is not a boolean expression", node.getPostcondition().getLocation(), node.getPostcondition());
}
node.setType(AstFactory.newAVoidType(node.getLocation()));
return node.getType();
}
@Override
public PType caseATrapStm(ATrapStm node, TypeCheckInfo question)
throws AnalysisException
{
PTypeSet rtypes = new PTypeSet(question.assistantFactory);
PStm body = node.getBody();
PType bt = body.apply(THIS, question);