-
Notifications
You must be signed in to change notification settings - Fork 8
/
verify.sml
1453 lines (1250 loc) · 44.3 KB
/
verify.sml
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
(* -*- mode: sml; mode: font-lock; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil -*- *)
structure Verify = struct
open LogErr
type RIB = Ast.FIXTURES
type ENV = { returnType: Ast.TYPE_EXPR option,
strict: bool,
ribs: RIB list }
fun withReturnType { returnType=_, strict, ribs } returnType =
{ returnType=returnType, strict=strict, ribs=ribs }
fun withRibs { returnType, strict, ribs=_ } ribs =
{ returnType=returnType, strict=strict, ribs=ribs }
fun withStrict { returnType, strict=_, ribs } strict =
{ returnType=returnType, strict=strict, ribs=ribs }
fun withRib { returnType, strict, ribs} extn =
{ returnType=returnType, strict=strict, ribs=extn :: ribs }
(* Local tracing machinery *)
val doTrace = ref false
fun trace ss = if (!doTrace) then LogErr.log ("[verify] " :: ss) else ()
fun error ss = LogErr.verifyError ss
(****************************** standard types *************************)
(* TODO: what is the proper way to resolve these built-ins? *)
(* FIXME: change Ast to have a variant of TypeName(?) that should be looked up in the global class table *)
fun builtInType (s:string) : Ast.TYPE_EXPR
= Ast.NominalType (Name.intrinsic s)
val boolType = builtInType "boolean"
val numberType = builtInType "number"
val doubleType = builtInType "double"
val decimalType = builtInType "decimal"
val intType = builtInType "int"
val uintType = builtInType "uint"
val stringType = builtInType "string"
val regexpType = builtInType "regexp"
val exceptionType = builtInType "exception"
val namespaceType = builtInType "Namespace"
val typeType = builtInType "Type"
val undefinedType = Ast.SpecialType Ast.Undefined
val nullType = Ast.SpecialType Ast.Null
val anyType = Ast.SpecialType Ast.Any
(****************************** misc auxiliary functions *************************)
fun assert b s = if b then () else (raise Fail s)
fun checkForDuplicates [] = ()
| checkForDuplicates (x::xs) =
if List.exists (fn y => x = y) xs
then error ["concurrent definition"]
else checkForDuplicates xs
fun unOptionDefault NONE def = def
| unOptionDefault (SOME v) _ = v
fun flattenOptionList NONE = []
| flattenOptionList (SOME l) = l
val gensymCounter : int ref = ref 0
fun gensym (s) =
let
in
gensymCounter := 1+(!gensymCounter);
s^"$"^(Int.toString (!gensymCounter))
end
(************************* Normalized types *********************************)
type TYPE_VALUE = Ast.TYPE_EXPR (* Invariant: normalized *)
(* A normalized type is one of
and TYPE_EXPR =
SpecialType of SPECIAL_TY
| UnionType of TYPE_EXPR list
| ArrayType of TYPE_EXPR list
| FunctionType of FUNC_TYPE
| ObjectType of FIELD_TYPE list
| AppType of
{ base: TYPE_EXPR, -- not a function type
args: TYPE_EXPR list }
| NullableType of
{ expr:TYPE_EXPR,
nullable:bool }
| InstanceType of
{ name: NAME,
typeParams: IDENT list,
ty: TYPE_EXPR,
isDynamic: bool }
| NominalType of NAME
and excludes
| TypeName of IDENT_EXPR
| ElementTypeRef of (TYPE_EXPR * int)
| FieldTypeRef of (TYPE_EXPR * IDENT)
*)
(************************* Substitution on Types *********************************)
(* TODO: normalized types?? *)
fun substTypeExpr (s:(Ast.IDENT*TYPE_VALUE) list) (t:TYPE_VALUE):Ast.TYPE_EXPR =
let in
case t of
Ast.UnionType ts =>
Ast.UnionType (map (substTypeExpr s) ts)
| Ast.ArrayType ts =>
Ast.ArrayType (map (substTypeExpr s) ts)
| Ast.AppType {base, args} =>
Ast.AppType {base=substTypeExpr s base, args=map (substTypeExpr s) args}
| Ast.NullableType {expr, nullable}
=> Ast.NullableType {expr=substTypeExpr s expr, nullable=nullable}
| Ast.TypeName ((Ast.Identifier {ident=ident, openNamespaces=_})) =>
let in
case List.find
(fn (id,ty) => id=ident)
s
of
(* TODO: we're dropping the nullable here, is that right? *)
SOME (_,ty) => ty
| NONE => t
end
| Ast.ObjectType fields =>
Ast.ObjectType (map (fn {name,ty} =>
{name=name,ty=substTypeExpr s ty})
fields)
| Ast.SpecialType st => Ast.SpecialType st
| Ast.FunctionType { typeParams, params, result, thisType, hasRest, minArgs } =>
(* Need to uniquify typeParams to avoid capture *)
let val oldNew = map (fn id => (id, gensym id)) typeParams
val nuSub =
map
(fn (oldId,newId) =>
(oldId,
Ast.TypeName (Ast.Identifier {ident=newId,openNamespaces=[]})))
oldNew
val bothSubs = fn t => substTypeExpr s (substTypeExpr nuSub t)
val nuTypeParams = map (fn (oldId,newId) => newId) oldNew
(* val nuParams = map (fn t => substVarBinding s (substVarBinding nuSub t)) params *)
in
Ast.FunctionType {typeParams=nuTypeParams,
params = List.map bothSubs params,
result = bothSubs result,
thisType = Option.map bothSubs thisType,
hasRest=hasRest, minArgs = minArgs }
end
end
(************************* Handling Types *********************************)
fun verifyTypeExpr (env:ENV)
(ty:Ast.TYPE_EXPR)
: TYPE_VALUE =
let in
trace ["type checking and normalizing a type"];
if (!doTrace) then Pretty.ppType ty else ();
case ty of
Ast.SpecialType _ =>
ty
| Ast.UnionType tys =>
Ast.UnionType (verifyTypeExprs env tys)
| Ast.ArrayType tys =>
Ast.ArrayType (verifyTypeExprs env tys)
| Ast.NullableType {expr=ty,nullable} =>
Ast.NullableType {expr=verifyTypeExpr env ty, nullable=nullable}
| Ast.AppType {base,args} =>
let val base' = verifyTypeExpr env base
val args' = verifyTypeExprs env args
in case base' of
Ast.FunctionType {typeParams,params,result,thisType,hasRest,minArgs} =>
let val _ = assert (length args = length typeParams);
val sub = ListPair.zip (typeParams,args)
fun applySub t = substTypeExpr sub t
val result =
Ast.FunctionType { typeParams=[],
params=map (substTypeExpr sub) params,
result=substTypeExpr sub result,
thisType= Option.map (substTypeExpr sub) thisType,
hasRest=hasRest, minArgs=minArgs }
in
verifyTypeExpr env result
end
| _ => Ast.AppType {base=base', args=args'}
end
| Ast.FunctionType {typeParams, params, result, thisType, hasRest, minArgs} =>
let
(* Add the type parameters to the environment. *)
val env' : ENV =
withRib env (List.map
(fn id => (Ast.PropName (Name.internal id),
Ast.TypeVarFixture))
typeParams)
val params' = verifyTypeExprs env' params
val result' = verifyTypeExpr env' result
val thisType' = Option.map (verifyTypeExpr env') thisType
in
Ast.FunctionType { typeParams=typeParams, params=params', result=result',
thisType=thisType', hasRest=hasRest, minArgs=minArgs }
end
| Ast.ObjectType fields =>
let val fields' =
map (fn {name,ty} => {name=name, ty=verifyTypeExpr env ty})
fields
val names = map (fn {name,ty} => name) fields
val _ = checkForDuplicates names
in
Ast.ObjectType fields'
end
| _ =>
let in
Pretty.ppType ty;
unimplError ["verifyTypeExpr"]
end
end
and verifyTypeExprs (env:ENV)
(tys:Ast.TYPE_EXPR list)
: TYPE_VALUE list =
(List.map (verifyTypeExpr env) tys)
(*
* TODO: when type checking a function body, handle CalledEval
* TODO: during type checking, if you see naked invocations of "eval" (that
* exact name), raise CalledEval
*)
fun mergeTypes t1 t2 =
(*FIXME*)
t1
(************************* Compatibility *********************************)
fun checkCompatible (t1:TYPE_VALUE)
(t2:TYPE_VALUE)
: unit =
if isCompatible t1 t2
then ()
else let in
TextIO.print ("Types are not compatible\n");
Pretty.ppType t1;
Pretty.ppType t2;
verifyError ["Types are not compatible"]
end
and isCompatible (t1:TYPE_VALUE)
(t2:TYPE_VALUE)
: bool =
let
in
trace ["Checking compatible - First type:"];
if (!doTrace) then Pretty.ppType t1 else ();
trace ["Second type: "];
if (!doTrace) then Pretty.ppType t2 else ();
(t1=t2) orelse
(t1=anyType) orelse
(t2=anyType) orelse
case (t1,t2) of
(Ast.UnionType types1,_) =>
List.all (fn t => isCompatible t t2) types1
| (_, Ast.UnionType types2) =>
(* t1 must exist in types2 *)
List.exists (fn t => isCompatible t1 t) types2
| (Ast.ArrayType types1, Ast.ArrayType types2) =>
(* arrays are invariant, every entry should be compatible in both directions *)
let fun check (h1::t1) (h2::t2) =
(isCompatible h1 h2)
andalso
(isCompatible h2 h1)
andalso
(case (t1,t2) of
([],[]) => true
| ([],_::_) => check [h1] t2
| (_::_,[]) => check t1 [h2]
| (_::_,_::_) => check t1 t2)
in
check types1 types2
end
| (Ast.ArrayType _,
Ast.TypeName (Ast.Identifier {ident="Array", openNamespaces=[]}))
=> true
| (Ast.ArrayType _,
Ast.TypeName (Ast.Identifier {ident="Object", openNamespaces=[]}))
=> true
| (Ast.FunctionType _,
Ast.TypeName (Ast.Identifier {ident="Function", openNamespaces=[]}))
=> true
| (Ast.FunctionType _,
Ast.TypeName (Ast.Identifier {ident="Object", openNamespaces=[]}))
=> true
| (Ast.AppType {base=base1,args=args1}, Ast.AppType {base=base2,args=args2}) =>
(* We keep types normalized wrt beta-reduction,
* so base1 and base2 must be class or interface types.
* Type arguments are covariant, and so must be intra-compatible - CHECK
*)
false
| (Ast.ObjectType fields1, Ast.ObjectType fields2) =>
false
| (Ast.FunctionType
{typeParams=typeParams1,
params =params1,
result =result1,
thisType=thisType1,
hasRest =hasRest1,
minArgs=minArgs},
Ast.FunctionType
{typeParams=typeParams2,
params=params2,
result=result2,
thisType=thisType2,
hasRest=hasRest2,
minArgs=minArgs2}) =>
let
in
(* TODO: Assume for now that functions are not polymorphic *)
assert (typeParams1 = [] andalso typeParams2=[]) "cannot handle polymorphic fns";
assert (not hasRest1 andalso not hasRest2) "cannot handle rest args";
ListPair.all (fn (t1,t2) => isCompatible t1 t2) (params1,params2)
andalso
isCompatible result1 result2
end
(* catch all *)
| _ => unimplError ["isCompatible"]
end
fun checkBicompatible (ty1:TYPE_VALUE)
(ty2:TYPE_VALUE)
: unit =
let in
checkCompatible ty1 ty2;
checkCompatible ty2 ty1
end
fun checkConvertible (ty1:TYPE_VALUE)
(ty2:TYPE_VALUE)
: unit =
(* TODO: int to float, etc, and to() methods *)
checkCompatible ty1 ty2
(******************** Verification **************************************************)
(*
HEAD
*)
and verifyInits (env:ENV) (inits:Ast.INITS)
: Ast.INITS =
List.map (fn (name, expr) =>
let
val (expr', t) = verifyExpr env expr
in
(name, expr')
end)
inits
and verifyHead (env:ENV) ((fixtures, inits):Ast.HEAD)
: Ast.HEAD =
(fixtures, verifyInits env inits)
(*
EXPR
*)
and verifyExpr (env:ENV)
(expr:Ast.EXPR)
: (Ast.EXPR * Ast.TYPE_EXPR) =
let
fun verifySub e =
let
val (e,t) = verifyExpr env e
in
e
end
fun return (e, t) =
(Ast.ExpectedTypeExpr (t, e), t)
val dummyType = Ast.SpecialType Ast.Any
in
case expr of
Ast.TernaryExpr (t, e1, e2, e3) =>
let
val e1' = verifySub e1
val e2' = verifySub e2
val e3' = verifySub e3
in
return (Ast.TernaryExpr (t, e1', e2', e3'), dummyType)
end
| Ast.BinaryExpr (b, e1, e2) =>
let
val e1' = verifySub e1
val e2' = verifySub e2
in
return (Ast.BinaryExpr (b, e1', e2'), dummyType)
end
| Ast.ExpectedTypeExpr (t, e) =>
internalError ["Defn produced ExpectedTypeExpr"]
| Ast.BinaryTypeExpr (b, e, te) =>
let
val e' = verifySub e
val te' = verifyTypeExpr env te
in
return (Ast.BinaryTypeExpr (b, e', te'), dummyType)
end
| Ast.UnaryExpr (u, e) =>
let
val e' = verifySub e
in
return (Ast.UnaryExpr (u, e'), dummyType)
end
| Ast.TypeExpr t =>
let
val t' = verifyTypeExpr env t
in
return (Ast.TypeExpr t', dummyType)
end
| Ast.ThisExpr =>
return (Ast.ThisExpr, dummyType)
| Ast.YieldExpr eo =>
let
val eo' = Option.map verifySub eo
in
return (Ast.YieldExpr eo', dummyType)
end
| Ast.SuperExpr eo =>
let
val eo' = Option.map verifySub eo
in
return (Ast.SuperExpr eo', dummyType)
end
| Ast.LiteralExpr le =>
(* TODO *)
return (Ast.LiteralExpr le, dummyType)
| Ast.CallExpr {func, actuals} =>
let
val func' = verifySub func
val actuals' = List.map verifySub actuals
in
return (Ast.CallExpr { func = func',
actuals = actuals' }, dummyType)
end
| Ast.ApplyTypeExpr { expr, actuals } =>
let
val expr' = verifySub expr
val actuals' = List.map (verifyTypeExpr env) actuals
in
return (Ast.ApplyTypeExpr { expr = expr',
actuals = actuals' }, dummyType)
end
| Ast.LetExpr { defs, body, head } =>
let
val defs' = defs (* TODO *)
val head' = Option.map (verifyHead env) head
(* TODO: verify body with `head' fixtures in env *)
val body' = verifySub body
in
return (Ast.LetExpr { defs = defs',
body = body',
head = head' }, dummyType)
end
| Ast.NewExpr { obj, actuals } =>
let
val obj' = verifySub obj
val actuals' = List.map verifySub actuals
in
return (Ast.NewExpr { obj = obj',
actuals = actuals' }, dummyType)
end
| Ast.ObjectRef { base, ident, pos } =>
let
val base' = verifySub base
val ident' = ident (* TODO *)
in
return (Ast.ObjectRef { base=base', ident=ident', pos=pos }, dummyType)
end
| Ast.LexicalRef { ident, pos } =>
let
val ident' = ident (* TODO *)
in
return (Ast.LexicalRef { ident=ident', pos=pos }, dummyType)
end
| Ast.SetExpr (a, le, re) =>
let
val le' = verifySub le
val re' = verifySub re
in
return (Ast.SetExpr (a, le', re'), dummyType)
end
| Ast.GetTemp n =>
(* TODO: these only occur on the RHS of compiled destructuring assignments. how to type-check? *)
return (Ast.GetTemp n, dummyType)
| Ast.GetParam n =>
internalError ["GetParam not eliminated by Defn"]
| Ast.ListExpr es =>
let
val es' = List.map verifySub es
in
return (Ast.ListExpr es', dummyType)
end
| Ast.SliceExpr (a, b, c) =>
let
val a' = verifySub a
val b' = verifySub b
val c' = verifySub c
in
return (Ast.SliceExpr (a, b, c), dummyType)
end
| Ast.InitExpr (it, head, inits) =>
let
val it' = it (* TODO *)
val head' = verifyHead env head
val inits' = verifyInits env inits
in
return (Ast.InitExpr (it', head', inits'), dummyType)
end
end
and verifyExprs (env:ENV)
(exprs:Ast.EXPR list)
: Ast.EXPR list * Ast.TYPE_EXPR list =
let
val es = ListPair.unzip (map (verifyExpr env) exprs)
in
es
end
and verifyExprAndCheck (env:ENV)
(expr:Ast.EXPR)
(expectedType:TYPE_VALUE)
: Ast.EXPR =
let val (expr',ty') = verifyExpr env expr
val _ = if #strict env
then checkCompatible ty' expectedType
else ()
in
expr'
end
(*
STMT
*)
and verifyStmt (env:ENV)
(stmt:Ast.STMT)
: Ast.STMT =
let fun verifySub s = verifyStmt env s
in
case stmt of
Ast.EmptyStmt =>
Ast.EmptyStmt
| Ast.ExprStmt e =>
let
val (expr,ty) = verifyExpr env e
in
Ast.ExprStmt expr
end
| Ast.ForInStmt fe => (*TODO*)
Ast.ForInStmt fe
| Ast.ThrowStmt es =>
let val (es',_) = verifyExpr env es
in
Ast.ThrowStmt es'
end
| Ast.ReturnStmt es =>
let val (es',ty) = verifyExpr env es
in
if #strict env
then
case #returnType env of
NONE => verifyError ["return not allowed here"]
| SOME retTy => checkCompatible ty retTy
else ();
Ast.ReturnStmt es'
end
| Ast.BreakStmt i =>
Ast.BreakStmt i
| Ast.ContinueStmt i =>
Ast.ContinueStmt i
| Ast.BlockStmt block =>
Ast.BlockStmt (verifyBlock env block)
| Ast.ClassBlock { ns, ident, name, block } =>
Ast.ClassBlock { ns=ns, ident=ident, name=name,
block=verifyBlock env block }
| Ast.LabeledStmt (id, s) =>
Ast.LabeledStmt (id, verifySub s)
| Ast.LetStmt block =>
Ast.LetStmt (verifyBlock env block)
| Ast.WhileStmt {cond,body,labels,fixtures=NONE} =>
Ast.WhileStmt {cond=verifyExprAndCheck env cond boolType,
body=verifySub body,
labels=labels,
fixtures=NONE}
| Ast.DoWhileStmt {cond,body,labels,fixtures=NONE} =>
Ast.DoWhileStmt {cond=verifyExprAndCheck env cond boolType,
body=verifySub body,
labels=labels,
fixtures=NONE}
| Ast.ForStmt { defn=_, fixtures, init, cond, update, labels, body } =>
let val fixtures' = verifyFixturesOption env fixtures
val env' = withRib env fixtures'
val init' = verifyStmts env' init
val cond' = verifyExprAndCheck env' cond boolType
val (update',_) = verifyExpr env' update
val body' = verifyStmt env' body
in
Ast.ForStmt { defn=NONE, fixtures=SOME fixtures', init=init', cond=cond',
update=update', labels=labels, body=body' }
end
| Ast.IfStmt {cnd, els, thn} =>
Ast.IfStmt {cnd=verifyExprAndCheck env cnd boolType,
els=verifySub els,
thn=verifySub thn}
| Ast.WithStmt {obj, ty, body} => (*TODO*)
Ast.WithStmt {obj=obj, ty=ty, body=body}
| Ast.TryStmt {block, catches, finally} =>
Ast.TryStmt {block=verifyBlock env block,
catches=List.map (verifyCatchClause env) catches,
finally=Option.map (verifyBlock env) finally }
| Ast.SwitchStmt {cond, cases, mode, labels} => (*TODO*)
Ast.SwitchStmt {cond=cond, cases=cases, mode=mode, labels=labels}
| Ast.SwitchTypeStmt {cond, ty, cases} => (*TODO*)
Ast.SwitchTypeStmt {cond=cond, ty=ty, cases=cases}
| Ast.Dxns x => (*TODO*)
Ast.Dxns x
| _ => error ["Shouldn't happen: failed to match in Verify.verifyStmt"]
end
and verifyCatchClause (env:ENV)
({bindings, ty, fixtures, block}:Ast.CATCH_CLAUSE)
: Ast.CATCH_CLAUSE =
let val fixtures' = verifyFixturesOption env fixtures
val env' = withRib env fixtures'
val block' = verifyBlock env' block
in
{bindings=bindings, ty=ty,
fixtures=SOME fixtures', block=block'}
end
and verifyStmts (env) (stmts:Ast.STMT list)
: Ast.STMT list =
List.map (verifyStmt env) stmts
and verifyBlock (env:ENV)
(b:Ast.BLOCK)
: Ast.BLOCK =
let
in case b of
Ast.Block { head, body, pos, pragmas=pragmas, defns=defns } =>
let
val _ = LogErr.setPos pos
val head = Option.map (verifyHead env) head
val body = verifyStmts env body
in
Ast.Block { pragmas = pragmas,
defns = defns,
body = body,
head = head,
pos = pos }
end
end
(*
FIXTURES
*)
and verifyFixture (env:ENV)
(f:Ast.FIXTURE)
: Ast.FIXTURE = (*TODO*)
let in
case f of
Ast.NamespaceFixture ns =>
Ast.NamespaceFixture ns
| Ast.ClassFixture (Ast.Cls {name, extends, implements, classFixtures, instanceFixtures,
instanceInits, constructor, classType, instanceType }) =>
(*TODO*)
f
| Ast.TypeVarFixture =>
Ast.TypeVarFixture
| Ast.TypeFixture ty =>
Ast.TypeFixture (verifyTypeExpr env ty)
| Ast.ValFixture {ty, readOnly} =>
Ast.ValFixture {ty=verifyTypeExpr env ty, readOnly=readOnly}
| Ast.MethodFixture { func, ty, readOnly, override, final } =>
(* TODO *)
f
| _ => unimplError ["in verifyFixture"]
end
and verifyFixturesOption (env:ENV)
(fs:Ast.FIXTURES option)
: Ast.FIXTURES =
let in
case fs of
SOME fixtures => map (fn (name,fixture) => (name,verifyFixture env fixture)) fixtures
| _ => internalError ["missing fixtures"]
end
(*
PROGRAM
*)
and topEnv () = { ribs = [!Defn.topFixtures],
strict = false,
returnType = NONE }
and verifyPackage (p:Ast.PACKAGE)
: Ast.PACKAGE =
raise UnimplError
and verifyProgram (p:Ast.PROGRAM)
: Ast.PROGRAM =
let
in case p of
{ packages, fixtures, block } =>
let
val _ = LogErr.setPos NONE
val e = topEnv ()
val block = verifyBlock e block
val result = { packages = packages,
block = block,
fixtures = fixtures }
in
trace ["verification complete"];
(if !doTrace
then Pretty.ppProgram result
else ());
result
end
end
end
(********************************** OLD **************************************
(*
* INVARIANTS:
* - all typed libraries in host environment must be DontDelete
* - all typed libraries in host environment must carry compatible runtime type constraints
*)
(****************************** type environments *************************)
type ID = FIXTURE_NAME
fun checkForDuplicateExtension extensions =
let val (names, _) = ListPair.unzip extensions
in
checkForDuplicates names
end
fun extendEnv (env:TYPE_ENV) (id:ID) (k:KIND) : TYPE_ENV = (id,k)::env
fun extendEnvWithTypeVars (params:ID list) (env:TYPE_ENV):TYPE_ENV =
let in
checkForDuplicates params;
foldl (fn (p,env) => extendEnv env p TypeVar) env params
end
fun extendEnvs (env:TYPE_ENV) (ext:TYPE_ENV) =
let in
ext @ env
end
fun fixtureNameToString (TempName i) = "TempName" ^ (Int.toString(i))
| fixtureNameToString (PropName { ns, id }) = id
fun lookupIdNamespace (env:TYPE_ENV)
(id:Ast.IDENT)
(ns:NAMESPACE)
: KIND option =
case List.find (fn (i,_) => i=PropName {id=id,ns=ns}) env of
NONE => NONE
| SOME (_,k) => SOME k
fun lookupIdNamespaces (env:TYPE_ENV)
(id:Ast.IDENT)
(nss : NAMESPACE list)
: KIND option =
let val theMatches
= List.mapPartial (lookupIdNamespace env id) nss
in
case theMatches of
[] => NONE
| [k] => SOME k
(******************** Expressions **************************************************)
and verifyIdentExpr (env as {env,this,...}:RIB)
(ide:Ast.IDENT_EXPR)
: Ast.TYPE_EXPR =
let
in
case ide of
Ast.QualifiedIdentifier { qual, ident } =>
let in
checkCompatible (verifyExpr env qual) namespaceType;
anyType
end
| Ast.QualifiedExpression { qual, expr } =>
let in
checkCompatible (verifyExpr env qual) namespaceType;
checkCompatible (verifyExpr env qual) stringType;
anyType
end
| Ast.AttributeIdentifier idexpr =>
let in
verifyIdentExpr env idexpr;
anyType
end
| Ast.Identifier { ident, openNamespaces } =>
let val k : KIND = lookupIdNamespacess env ident openNamespaces
in
case k of
TypeVar => verifyError ["Attempt to refer to type variable ",
ident,
" as a program variable"]
| ProgVar (ty,read_only) => ty
end
end
and verifyExpr (env as {env,this,...}:RIB)
(e:EXPR)
: Ast.TYPE_EXPR =
let
in
TextIO.print ("type checking expr: env len " ^ (Int.toString (List.length env)) ^"\n");
Pretty.ppExpr e;
TextIO.print "\n";
case e of
LiteralExpr LiteralNull => nullType
| LiteralExpr (LiteralInt _) => intType
| LiteralExpr (LiteralUInt _) => uintType
| LiteralExpr (LiteralDecimal _) => decimalType
| LiteralExpr (LiteralDouble _) => doubleType
| LiteralExpr (LiteralBoolean _) => boolType
| LiteralExpr (LiteralString _) => stringType
| LiteralExpr (LiteralRegExp _) => regexpType
| LiteralExpr (LiteralArray { exprs, ty }) =>
(* EXAMPLES:
[a, b, c] : [int, Boolean, String]
[a, b, c] : Array
[a, b, c] : *
[a, b, c] : Object
*)
let val annotatedTy = unOptionDefault ty anyType
val inferredTy = ArrayType (map (fn elt => verifyExpr env elt) exprs)
in
checkCompatible inferredTy annotatedTy;
annotatedTy
end
| LiteralExpr (LiteralObject { expr=fields, ty }) =>
let val annotatedTy = unOptionDefault ty anyType
val inferredTy = ObjectType (map (verifyField env) fields)
in
checkCompatible inferredTy annotatedTy;
annotatedTy
end
| LiteralExpr (LiteralFunction (Func { param=(fixtures,inits), block, ty, ... }))
=>
let
val env1 = verifyFunctionType env ty
val extensions = verifyFixtures env1 fixtures
val env2 = withEnvExtn env1 extensions
in
checkForDuplicateExtension extensions;
(** FIXME: inits are now settings and are BINDINGS
verifyStmts env2 inits;
*)
verifyBlock env2 block;
Ast.FunctionType ty
end
| LexicalRef { ident, pos } =>
verifyIdentExpr env ident
| ListExpr l => List.last (List.map (verifyExpr env) l)
| LetExpr {defs=_, body, head=SOME (fixtures,inits) } => (* FIXME: inits added *)
let val extensions = verifyFixtures env fixtures
in
checkForDuplicateExtension extensions;
verifyExpr (withEnvExtn env extensions) body
end
| ThisExpr => this
| UnaryExpr (unop, arg) => verifyUnaryExpr env unop arg
| BinaryExpr (binop, lhs, rhs ) => verifyBinaryExpr env (binop, lhs, rhs)
| BinaryTypeExpr (binop, lhs, rhs ) => verifyBinaryTypeExpr env (binop, lhs, rhs)
| TrinaryExpr (triop, a,b,c ) => verifyTrinaryExpr env (triop, a,b,c)
| CallExpr { func, actuals } => verifyCallExpr env func actuals
| ApplyTypeExpr {expr, actuals} =>
(* Can only instantiate Functions, classes, and interfaces *)
let val exprTy = verifyExpr env expr;
val typeParams =
case exprTy of
Ast.FunctionType {typeParams, ...} => typeParams
(* TODO: class and interface types *)
| _ => verifyError ["Cannot instantiate a non-polymorphic type"]
in
List.app (fn t => verifyTypeExpr env t) actuals;
if (List.length typeParams) = (List.length actuals)
then ()
else verifyError ["Wrong number of type arguments"];
normalizeType (Ast.AppType { base=exprTy, args=actuals })
end
| TypeExpr ty =>
let in
verifyTypeExpr env ty;
typeType
end
| _ => (TextIO.print "verifyExpr incomplete: "; Pretty.ppExpr e; raise Match)
end
(*
and LITERAL =
| LiteralXML of EXPR list
| LiteralNamespace of NAMESPACE
| LiteralObject of
{ name: EXPR,
init: EXPR } list
and EXPR =
| YieldExpr of EXPR option
| SuperExpr of EXPR option
| Ref of { base: EXPR option,
ident: Ast.IDENT_EXPR }
| NewExpr of { obj: EXPR,x
actuals: EXPR list }
and Ast.IDENT_EXPR =
Ast.QualifiedIdentifier of { qual : EXPR,
ident : USTRING }