forked from HaxeFoundation/haxe
-
Notifications
You must be signed in to change notification settings - Fork 3
/
matcher.ml
1441 lines (1369 loc) · 44.7 KB
/
matcher.ml
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
(*
* Copyright (C)2005-2013 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*)
open Ast
open Common
open Type
open Typecore
type pvar = tvar * pos
type con_def =
| CEnum of tenum * tenum_field
| CConst of tconstant
| CAny
| CType of module_type
| CArray of int
| CFields of int * (string * tclass_field) list
| CExpr of texpr
and con = {
c_def : con_def;
c_type : t;
c_pos : pos;
}
and st_def =
| SVar of tvar
| SField of st * tclass_field
| SEnum of st * tenum_field * int
| SArray of st * int
| STuple of st * int * int
and st = {
st_def : st_def;
st_type : t;
st_pos : pos;
}
and dt =
| Switch of st * (con * dt) list
| Bind of ((tvar * pos) * st) list * dt
| Goto of int
| Expr of int
| Guard of int * dt * dt option
(* Pattern *)
type pat_def =
| PAny
| PVar of pvar
| PCon of con * pat list
| POr of pat * pat
| PBind of pvar * pat
| PTuple of pat array
and pat = {
p_def : pat_def;
p_type : t;
p_pos : pos;
}
type out = {
mutable o_pos : pos;
o_id : int;
o_catch_all : bool;
mutable o_num_paths : int;
}
type pat_vec = pat array * out
type pat_matrix = pat_vec list
(* Context *)
type pattern_ctx = {
mutable pc_locals : (string, pvar) PMap.t;
mutable pc_sub_vars : (string, pvar) PMap.t option;
mutable pc_reify : bool;
mutable pc_is_complex : bool;
}
type matcher = {
ctx : typer;
need_val : bool;
dt_lut : dt DynArray.t;
dt_cache : (dt,int) Hashtbl.t;
mutable dt_count : int;
mutable outcomes : out list;
mutable toplevel_or : bool;
mutable has_extractor : bool;
mutable expr_map : (int,texpr * texpr option) PMap.t;
mutable is_exhaustive : bool;
}
type type_finiteness =
| Infinite (* type has inifite constructors (e.g. Int, String) *)
| CompileTimeFinite (* type is considered finite only at compile-time but has inifite possible run-time values (enum abstracts) *)
| RunTimeFinite (* type is truly finite (Bool, enums) *)
exception Not_exhaustive of pat * st
exception Unrecognized_pattern of Ast.expr
let arity con = match con.c_def with
| CEnum (_,{ef_type = TFun(args,_)}) -> List.length args
| CEnum _ -> 0
| CConst _ -> 0
| CType mt -> 0
| CArray i -> i
| CFields (i,_) -> i
| CExpr _ -> 0
| CAny -> 0
let mk_st def t p = {
st_def = def;
st_type = t;
st_pos = p;
}
let mk_out mctx id e eg is_catch_all p =
let out = {
o_pos = p;
o_id = id;
o_catch_all = is_catch_all;
o_num_paths = 0;
} in
mctx.outcomes <- out :: mctx.outcomes;
mctx.expr_map <- PMap.add id (e,eg) mctx.expr_map;
out
let clone_out mctx out p =
let out = {out with o_pos = p; } in
mctx.outcomes <- out :: mctx.outcomes;
out
let get_guard mctx id =
snd (PMap.find id mctx.expr_map)
let get_expr mctx id =
fst (PMap.find id mctx.expr_map)
let mk_pat pdef t p = {
p_def = pdef;
p_type = t;
p_pos = p;
}
let mk_con cdef t p = {
c_def = cdef;
c_type = t;
c_pos = p;
}
let mk_con_pat cdef pl t p = mk_pat (PCon(mk_con cdef t p,pl)) t p
let mk_any t p = mk_pat PAny t p
let any = mk_any t_dynamic Ast.null_pos
let fake_tuple_type = TInst(mk_class null_module ([],"-Tuple") null_pos, [])
let mk_type_pat ctx mt t p =
let rec loop = function
| TClassDecl _ -> "Class"
| TEnumDecl _ -> "Enum"
| TAbstractDecl a when Meta.has Meta.RuntimeValue a.a_meta -> "Class"
| TTypeDecl t ->
begin match follow (monomorphs t.t_params t.t_type) with
| TInst(c,_) -> loop (TClassDecl c)
| TEnum(en,_) -> loop (TEnumDecl en)
| TAbstract(a,_) -> loop (TAbstractDecl a)
| _ -> error "Cannot use this type as a value" p
end
| _ -> error "Cannot use this type as a value" p
in
let tcl = Typeload.load_instance ctx {tname=loop mt;tpackage=[];tsub=None;tparams=[]} p true in
let t2 = match tcl with TAbstract(a,_) -> TAbstract(a,[mk_mono()]) | _ -> assert false in
unify ctx t t2 p;
mk_con_pat (CType mt) [] t2 p
let mk_subs st con =
let map = match follow st.st_type with
| TInst(c,pl) -> apply_params c.cl_params pl
| TEnum(en,pl) -> apply_params en.e_params pl
| TAbstract(a,pl) -> apply_params a.a_params pl
| _ -> fun t -> t
in
match con.c_def with
| CFields (_,fl) -> List.map (fun (s,cf) -> mk_st (SField(st,cf)) (map cf.cf_type) st.st_pos) fl
| CEnum (en,({ef_type = TFun _} as ef)) ->
let rec loop t = match follow t with
| TEnum(_,pl) -> pl
| TAbstract({a_path = [],"EnumValue"},[]) -> []
| TAbstract(a,pl) -> loop (Abstract.get_underlying_type a pl)
| _ -> []
in
let pl = loop con.c_type in
begin match apply_params en.e_params pl (monomorphs ef.ef_params ef.ef_type) with
| TFun(args,r) ->
ExtList.List.mapi (fun i (_,_,t) ->
mk_st (SEnum(st,ef,i)) t st.st_pos
) args
| _ ->
assert false
end
| CArray 0 -> []
| CArray i ->
let t = match follow con.c_type with TInst({cl_path=[],"Array"},[t]) -> t | TDynamic _ as t -> t | _ -> assert false in
ExtList.List.init i (fun i -> mk_st (SArray(st,i)) t st.st_pos)
| CEnum _ | CConst _ | CType _ | CExpr _ | CAny ->
[]
let get_tuple_params t = match t with
| TFun(tl,tr) when tr == fake_tuple_type -> Some tl
| _ -> None
(* Printing *)
let s_type = s_type (print_context())
let rec s_con con = match con.c_def with
| CEnum(_,ef) -> ef.ef_name
| CAny -> "_"
| CConst c -> s_const c
| CType mt -> s_type_path (t_path mt)
| CArray i -> "[" ^(string_of_int i) ^ "]"
| CFields (_,fl) -> String.concat "," (List.map (fun (s,_) -> s) fl)
| CExpr e -> s_expr s_type e
let rec s_pat pat = match pat.p_def with
| PVar (v,_) -> v.v_name
| PCon (c,[]) -> s_con c
| PCon (c,pl) -> s_con c ^ "(" ^ (String.concat "," (List.map s_pat pl)) ^ ")"
| POr (pat1,pat2) -> s_pat pat1 ^ " | " ^ s_pat pat2
| PAny -> "_"
| PBind((v,_),pat) -> v.v_name ^ "=" ^ s_pat pat
| PTuple pl -> "(" ^ (String.concat " " (Array.to_list (Array.map s_pat pl))) ^ ")"
let rec s_pat_vec pl =
String.concat " " (Array.to_list (Array.map s_pat pl))
let rec s_pat_matrix pmat =
String.concat "\n" (List.map (fun (pl,out) -> (s_pat_vec pl) ^ "->" ^ "") pmat)
let st_args l r v =
(if l > 0 then (String.concat "," (ExtList.List.make l "_")) ^ "," else "")
^ v ^
(if r > 0 then "," ^ (String.concat "," (ExtList.List.make r "_")) else "")
let rec s_st st =
(match st.st_def with
| SVar v -> v.v_name
| SEnum (st,ef,i) -> s_st st ^ "." ^ ef.ef_name ^ "." ^ (string_of_int i)
| SArray (st,i) -> s_st st ^ "[" ^ (string_of_int i) ^ "]"
| STuple (st,i,a) -> "(" ^ (st_args i (a - i - 1) (s_st st)) ^ ")"
| SField (st,cf) -> s_st st ^ "." ^ cf.cf_name)
(* Pattern parsing *)
let unify_enum_field en pl ef t =
let t2 = match follow ef.ef_type with
| TFun(_,r) -> r
| t2 -> t2
in
let t2 = (apply_params en.e_params pl (monomorphs ef.ef_params t2)) in
Type.unify t2 t
let unify ctx a b p =
try unify_raise ctx a b p with Error (Unify l,p) -> error (error_msg (Unify l)) p
let rec is_value_type = function
| TMono r ->
(match !r with None -> false | Some t -> is_value_type t)
| TType (t,tl) ->
is_value_type (apply_params t.t_params tl t.t_type)
| TInst({cl_path=[],"String"},[]) ->
true
| TAbstract _ ->
true
| _ ->
false
(* Determines if a type allows null-matching. This is similar to is_nullable, but it infers Null<T> on monomorphs,
and enums are not considered nullable *)
let rec matches_null ctx t = match t with
| TMono r ->
(match !r with None -> r := Some (ctx.t.tnull (mk_mono())); true | Some t -> matches_null ctx t)
| TType ({ t_path = ([],"Null") },[_]) ->
true
| TLazy f ->
matches_null ctx (!f())
| TType (t,tl) ->
matches_null ctx (apply_params t.t_params tl t.t_type)
| TFun _ | TEnum _ ->
false
| TAbstract (a,_) -> not (Meta.has Meta.NotNull a.a_meta)
| _ ->
true
let to_pattern ctx e t =
let perror p = error "Unrecognized pattern" p in
let verror n p = error ("Variable " ^ n ^ " must appear exactly once in each sub-pattern") p in
let mk_var tctx s t p =
let v = match tctx.pc_sub_vars with
| Some vmap -> fst (try PMap.find s vmap with Not_found -> verror s p)
| None -> alloc_var s t
in
unify ctx t v.v_type p;
if PMap.mem s tctx.pc_locals then verror s p;
tctx.pc_locals <- PMap.add s (v,p) tctx.pc_locals;
v
in
let rec loop pctx e t =
let p = pos e in
match fst e with
| ECheckType(e, CTPath({tpackage=["haxe";"macro"]; tname="Expr"})) ->
let old = pctx.pc_reify in
pctx.pc_reify <- true;
let e = loop pctx e t in
pctx.pc_reify <- old;
e
| EParenthesis e ->
loop pctx e t
| ECast(e1,None) ->
loop pctx e1 t
| EConst(Ident "null") ->
if not (matches_null ctx t) then error ("Null-patterns are only allowed on nullable types (found " ^ (s_type t) ^ ")") p;
mk_con_pat (CConst TNull) [] t p
| EConst((Ident ("false" | "true") | Int _ | String _ | Float _) as c) ->
let e = Codegen.type_constant ctx.com c p in
unify ctx e.etype t p;
let c = match e.eexpr with TConst c -> c | _ -> assert false in
mk_con_pat (CConst c) [] t p
| EMeta((Meta.Macro,[],_),(ECall (e1,args),_)) ->
let path, field, args = Codegen.get_macro_path ctx e1 args p in
begin match ctx.g.do_macro ctx MExpr path field args p with
| Some e -> loop pctx e t
| None -> error "Macro failure" p
end
| EField _ ->
let e = type_expr ctx e (WithType t) in
let e = match Optimizer.make_constant_expression ctx ~concat_strings:true e with Some e -> e | None -> e in
(match e.eexpr with
| TConst c | TCast({eexpr = TConst c},None) ->
mk_con_pat (CConst c) [] t p
| TTypeExpr mt ->
mk_type_pat ctx mt t p
| TField(_, FStatic(_,cf)) when is_value_type cf.cf_type ->
mk_con_pat (CExpr e) [] cf.cf_type p
| TField(_, FEnum(en,ef)) ->
begin try
unify_enum_field en (List.map (fun _ -> mk_mono()) en.e_params) ef t
with Unify_error l ->
error (error_msg (Unify l)) p
end;
mk_con_pat (CEnum(en,ef)) [] t p
| _ -> error "Constant expression expected" p)
| ECall(ec,el) ->
let ec = type_expr ctx ec (WithType t) in
(match follow ec.etype with
| TEnum(en,pl)
| TFun(_,TEnum(en,pl)) ->
let ef = match ec.eexpr with
| TField (_,FEnum (_,f)) -> f
| _ -> error ("Expected constructor for enum " ^ (s_type_path en.e_path)) p
in
let monos = List.map (fun _ -> mk_mono()) ef.ef_params in
let tl,r = match apply_params en.e_params pl (apply_params ef.ef_params monos ef.ef_type) with
| TFun(args,r) ->
unify ctx r t p;
List.map (fun (n,_,t) -> t) args,r
| _ -> error "No arguments expected" p
in
let rec loop2 i el tl = match el,tl with
| (EConst(Ident "_"),pany) :: [], t :: tl ->
let pat = mk_pat PAny t_dynamic pany in
(ExtList.List.make ((List.length tl) + 1) pat)
| e :: el, t :: tl ->
let pat = loop pctx e t in
pat :: loop2 (i + 1) el tl
| e :: _, [] ->
error "Too many arguments" (pos e);
| [],_ :: _ ->
error "Not enough arguments" p;
| [],[] ->
[]
in
let el = loop2 0 el tl in
List.iter2 (fun m (_,t) -> match follow m with TMono _ -> Type.unify m t | _ -> ()) monos ef.ef_params;
pctx.pc_is_complex <- true;
mk_con_pat (CEnum(en,ef)) el r p
| _ -> perror p)
| EConst(Ident "_") ->
begin match get_tuple_params t with
| Some tl ->
let pl = List.map (fun (_,_,t) -> mk_any t p) tl in
mk_pat (PTuple (Array.of_list pl)) t_dynamic p
| None ->
mk_any t p
end
| EConst(Ident s) ->
begin try
let ec = match follow t with
| TEnum(en,pl) ->
let ef = try
PMap.find s en.e_constrs
with Not_found when not (is_lower_ident s) ->
error (string_error s en.e_names ("Expected constructor for enum " ^ (s_type_path en.e_path))) p
in
(match ef.ef_type with
| TFun (args,_) ->
let msg = Printf.sprintf "Enum constructor %s.%s requires parameters %s"
(s_type_path en.e_path)
ef.ef_name
(String.concat ", " (List.map (fun (n,_,t) -> n ^ ":" ^ (s_type t)) args))
in
error msg p
| _ -> ());
let et = mk (TTypeExpr (TEnumDecl en)) (TAnon { a_fields = PMap.empty; a_status = ref (EnumStatics en) }) p in
mk (TField (et,FEnum (en,ef))) (apply_params en.e_params pl ef.ef_type) p
| TAbstract({a_impl = Some c} as a,_) when Meta.has Meta.Enum a.a_meta ->
let cf = PMap.find s c.cl_statics in
Type.unify (follow cf.cf_type) t;
let e = begin match cf.cf_expr with
| Some ({eexpr = TConst c | TCast({eexpr = TConst c},None)} as e) -> e
| _ -> raise Not_found
end in
e
| _ ->
let old = ctx.untyped in
ctx.untyped <- true;
let e = try type_expr ctx e (WithType t) with _ -> ctx.untyped <- old; raise Not_found in
ctx.untyped <- old;
e
in
let ec = match Optimizer.make_constant_expression ctx ~concat_strings:true ec with Some e -> e | None -> ec in
(match ec.eexpr with
| TField (_,FEnum (en,ef)) ->
begin try unify_raise ctx ec.etype t ec.epos with Error (Unify _,_) -> raise Not_found end;
begin try
unify_enum_field en (List.map (fun _ -> mk_mono()) en.e_params) ef t;
with Unify_error l ->
error (error_msg (Unify l)) p
end;
mk_con_pat (CEnum(en,ef)) [] t p
| TConst c | TCast({eexpr = TConst c},None) ->
begin try unify_raise ctx ec.etype t ec.epos with Error (Unify _,_) -> raise Not_found end;
unify ctx ec.etype t p;
mk_con_pat (CConst c) [] t p
| TTypeExpr mt ->
mk_type_pat ctx mt t p
| _ ->
raise Not_found);
with Not_found ->
begin match get_tuple_params t with
| Some tl ->
let s = String.concat "," (List.map (fun (_,_,t) -> s_type t) tl) in
error ("Pattern should be tuple [" ^ s ^ "]") p
| None ->
if not (is_lower_ident s) && s.[0] <> '`' then error "Capture variables must be lower-case" p;
let v = mk_var pctx s t p in
mk_pat (PVar (v,p)) v.v_type p
end
end
| (EObjectDecl fl) ->
let is_matchable cf = match cf.cf_kind with Method _ -> false | _ -> true in
let is_valid_field_name fields co n p =
try
let cf = PMap.find n fields in
begin match co with
| Some c when not (Typer.can_access ctx c cf false) -> error ("Cannot match against private field " ^ n) p
| _ -> ()
end
with Not_found ->
error ((s_type t) ^ " has no field " ^ n ^ " that can be matched against") p;
in
pctx.pc_is_complex <- true;
let loop_fields fields =
let sl,pl,i = PMap.foldi (fun n cf (sl,pl,i) ->
if not (is_matchable cf) then
sl,pl,i
else
let pat = try
if pctx.pc_reify && cf.cf_name = "pos" then raise Not_found;
loop pctx (List.assoc cf.cf_name fl) cf.cf_type
with Not_found ->
(mk_any cf.cf_type p)
in
(n,cf) :: sl,pat :: pl,i + 1
) fields ([],[],0) in
mk_con_pat (CFields(i,sl)) pl t p
in
let fields = match follow t with
| TAnon {a_fields = fields} ->
fields
| TInst(c,tl) ->
let fields = ref PMap.empty in
let rec loop c tl =
begin match c.cl_super with
| Some (csup,tlsup) -> loop csup (List.map (apply_params c.cl_params tl) tlsup)
| None -> ()
end;
PMap.iter (fun n cf -> fields := PMap.add n {cf with cf_type = apply_params c.cl_params tl (monomorphs cf.cf_params cf.cf_type)} !fields) c.cl_fields
in
loop c tl;
!fields
| TAbstract({a_impl = Some c} as a,tl) ->
let fields = List.fold_left (fun acc cf ->
if Meta.has Meta.Impl cf.cf_meta then
PMap.add cf.cf_name cf acc
else acc
) PMap.empty c.cl_ordered_statics in
PMap.map (fun cf -> {cf with cf_type = apply_params a.a_params tl (monomorphs cf.cf_params cf.cf_type)}) fields
| _ ->
error ((s_type t) ^ " cannot be matched against a structure") p
in
List.iter (fun (n,(_,p)) -> is_valid_field_name fields None n p) fl;
loop_fields fields
| EArrayDecl [] ->
mk_con_pat (CArray 0) [] t p
| EArrayDecl el ->
pctx.pc_is_complex <- true;
begin match follow t with
| TInst({cl_path=[],"Array"},[t2]) | (TDynamic _ as t2) ->
let pl = ExtList.List.mapi (fun i e ->
loop pctx e t2
) el in
mk_con_pat (CArray (List.length el)) pl t p
| TFun(tl,tr) when tr == fake_tuple_type ->
let pl = try
List.map2 (fun e (_,_,t) -> loop pctx e t) el tl
with Invalid_argument _ ->
error ("Invalid number of arguments: expected " ^ (string_of_int (List.length tl)) ^ ", found " ^ (string_of_int (List.length el))) p
in
mk_pat (PTuple (Array.of_list pl)) t p
| _ ->
error ((s_type t) ^ " should be Array") p
end
| EBinop(OpAssign,(EConst(Ident s),p2),e1) ->
let v = mk_var pctx s t p in
let pat1 = loop pctx e1 t in
mk_pat (PBind((v,p),pat1)) t p2
| EBinop(OpOr,(EBinop(OpOr,e1,e2),p2),e3) ->
loop pctx (EBinop(OpOr,e1,(EBinop(OpOr,e2,e3),p2)),p) t
| EBinop(OpOr,e1,e2) ->
let old = pctx.pc_locals in
let pat1 = loop pctx e1 t in
begin match pat1.p_def with
| PAny | PVar _ ->
display_error ctx "This pattern is unused" (pos e2);
pat1
| _ ->
let pctx2 = {
pc_sub_vars = Some pctx.pc_locals;
pc_locals = old;
pc_reify = pctx.pc_reify;
pc_is_complex = pctx.pc_is_complex;
} in
let pat2 = loop pctx2 e2 t in
pctx.pc_is_complex <- pctx2.pc_is_complex;
PMap.iter (fun s (_,p) -> if not (PMap.mem s pctx2.pc_locals) then verror s p) pctx.pc_locals;
mk_pat (POr(pat1,pat2)) pat2.p_type (punion pat1.p_pos pat2.p_pos);
end
| _ ->
raise (Unrecognized_pattern e)
in
let pctx = {
pc_locals = PMap.empty;
pc_sub_vars = None;
pc_reify = false;
pc_is_complex = false;
} in
let x = loop pctx e t in
x, pctx.pc_locals, pctx.pc_is_complex
let get_pattern_locals ctx e t =
try
let _,locals,_ = to_pattern ctx e t in
PMap.foldi (fun n v acc -> PMap.add n v acc) locals PMap.empty
with Unrecognized_pattern _ ->
PMap.empty
(* Match compilation *)
let expr_eq e1 e2 = e1 == e2 || match e1.eexpr,e2.eexpr with
| TConst ct1,TConst ct2 ->
ct1 = ct2
| TField(_,FStatic(c1,cf1)),TField(_,FStatic(c2,cf2)) ->
c1 == c2 && cf1.cf_name = cf2.cf_name
| _ ->
false
let unify_con con1 con2 = match con1.c_def,con2.c_def with
| CExpr e1, CExpr e2 ->
expr_eq e1 e2
| CConst c1,CConst c2 ->
c1 = c2
| CEnum(e1,ef1),CEnum(e2,ef2) ->
e1 == e2 && ef1.ef_name = ef2.ef_name
| CFields (i1,fl1),CFields (i2,fl2) ->
(try
List.iter (fun (s,_) -> if not (List.mem_assoc s fl1) then raise Not_found) fl2;
true
with Not_found ->
false)
| CType mt1,CType mt2 ->
t_path mt1 = t_path mt2
| CArray a1, CArray a2 ->
a1 == a2
| CAny, CAny ->
true
| _ ->
false
let array_tl arr = Array.sub arr 1 (Array.length arr - 1)
let spec mctx con pmat =
let a = arity con in
let r = DynArray.create () in
let add pv out =
DynArray.add r (pv,out)
in
let rec loop2 pv out = match pv.(0).p_def with
| PCon(c2,pl) when unify_con c2 con ->
add (Array.append (Array.of_list pl) (array_tl pv)) out
| PCon(c2,pl) ->
()
| PAny | PVar _->
add (Array.append (Array.make a (mk_any (pv.(0).p_type) (pv.(0).p_pos))) (array_tl pv)) out
| PBind(_,pat) ->
loop2 (Array.append [|pat|] (array_tl pv)) out
| PTuple tl ->
loop2 tl out
| POr _ ->
assert false
in
let rec loop pmat = match pmat with
| (pv,out) :: pl ->
loop2 pv out;
loop pl
| [] ->
()
in
loop pmat;
DynArray.to_list r
let default mctx pmat =
let r = DynArray.create () in
let add pv out =
DynArray.add r (pv,out)
in
let rec loop2 pv out = match pv.(0).p_def with
| PCon _ ->
()
| PAny | PVar _->
add (array_tl pv) out
| PBind(_,pat) ->
loop2 (Array.append [|pat|] (array_tl pv)) out
| PTuple tl ->
loop2 tl out
| POr _ ->
assert false
in
let rec loop pmat = match pmat with
| (pv,out) :: pl ->
loop2 pv out;
loop pl;
| [] ->
()
in
loop pmat;
DynArray.to_list r
let pick_column pmat =
let rec loop i pv = if Array.length pv = 0 then -1 else match pv.(0).p_def with
| PVar _ | PAny ->
loop (i + 1) (array_tl pv)
| PTuple pl ->
loop i pl
| _ ->
i
in
loop 0 (fst (List.hd pmat))
let swap_pmat_columns i pmat =
List.map (fun (pv,out) ->
let pv = match pv with [|{p_def = PTuple pt}|] -> pt | _ -> pv in
let tmp = pv.(i) in
Array.set pv i pv.(0);
Array.set pv 0 tmp;
pv,out
) pmat
let swap_columns i (row : 'a list) : 'a list =
match row with
| rh :: rt ->
let rec loop count acc col = match col with
| [] -> acc
| ch :: cl when i = count ->
ch :: (List.rev acc) @ [rh] @ cl
| ch :: cl ->
loop (count + 1) (ch :: acc) cl
in
loop 1 [] rt
| _ ->
[]
let expand_or mctx (pmat : pat_matrix) =
let rec loop pat = match pat.p_def with
| POr(pat1,pat2) ->
let pat1 = loop pat1 in
let pat2 = loop pat2 in
pat1 @ pat2
| PBind(v,pat1) ->
let pat1 = loop pat1 in
List.map (fun pat1 ->
{pat with p_def = PBind(v,pat1)}
) pat1
| PTuple(pl) ->
let pat1 = loop pl.(0) in
List.map (fun pat1 ->
let a1 = Array.copy pl in
a1.(0) <- pat1;
{pat with p_def = PTuple a1}
) pat1
| _ ->
[pat]
in
let rec loop2 pmat = match pmat with
| (pv,out) :: pmat ->
let pat = loop pv.(0) in
let pat' = ExtList.List.mapi (fun i pat ->
(* TODO: This should really be active, but currently causes problems with or-patterns in
tuples (issue #2610). We will disable this for the 3.1.0 release, which means issue
#2508 is open again. *)
(* let out = if i = 0 then out else clone_out mctx out pat.p_pos in *)
let a1 = Array.copy pv in
a1.(0) <- pat;
a1,out
) pat in
pat' @ (loop2 pmat)
| [] ->
[]
in
loop2 pmat
let column_sigma mctx st pmat =
let acc = ref [] in
let bindings = ref [] in
let unguarded = Hashtbl.create 0 in
let add c g =
if not (List.exists (fun c2 -> unify_con c2 c) !acc) then acc := c :: !acc;
if not g then Hashtbl.replace unguarded c.c_def true;
in
let bind_st out st v =
if not (List.exists (fun ((v2,p),_) -> v2.v_id == (fst v).v_id) !bindings) then bindings := (v,st) :: !bindings
in
let rec loop pmat = match pmat with
| (pv,out) :: pr ->
let rec loop2 out = function
| PCon (c,_) ->
add c ((get_guard mctx out.o_id) <> None);
| PVar v ->
bind_st out st v;
| PBind(v,pat) ->
bind_st out st v;
loop2 out pat.p_def
| PAny ->
()
| PTuple tl ->
loop2 out tl.(0).p_def
| POr _ ->
assert false
in
loop2 out pv.(0).p_def;
loop pr
| [] ->
()
in
loop pmat;
List.rev_map (fun con -> con,not (Hashtbl.mem unguarded con.c_def)) !acc,!bindings
let rec all_ctors mctx t =
let h = ref PMap.empty in
if is_explicit_null t then h := PMap.add (CConst TNull) Ast.null_pos !h;
match follow t with
| TAbstract({a_path = [],"Bool"},_) ->
h := PMap.add (CConst(TBool true)) Ast.null_pos !h;
h := PMap.add (CConst(TBool false)) Ast.null_pos !h;
h,RunTimeFinite
| TAbstract({a_impl = Some c} as a,pl) when Meta.has Meta.Enum a.a_meta ->
List.iter (fun cf ->
ignore(follow cf.cf_type);
if Meta.has Meta.Impl cf.cf_meta then match cf.cf_expr with
| Some {eexpr = TConst c | TCast ({eexpr = TConst c},None)} -> h := PMap.add (CConst c) cf.cf_pos !h
| _ -> ()
) c.cl_ordered_statics;
h,CompileTimeFinite
| TAbstract(a,pl) when not (Meta.has Meta.CoreType a.a_meta) -> all_ctors mctx (Abstract.get_underlying_type a pl)
| TInst({cl_path=[],"String"},_)
| TInst({cl_path=[],"Array"},_) ->
h,Infinite
| TEnum(en,pl) ->
PMap.iter (fun _ ef ->
let tc = monomorphs mctx.ctx.type_params t in
try unify_enum_field en pl ef tc;
h := PMap.add (CEnum(en,ef)) ef.ef_pos !h
with Unify_error _ ->
()
) en.e_constrs;
h,RunTimeFinite
| TAnon a ->
h,CompileTimeFinite
| TInst(_,_) ->
h,CompileTimeFinite
| _ ->
h,Infinite
let rec collapse_pattern pl = match pl with
| pat :: [] ->
pat
| pat :: pl ->
let pat2 = collapse_pattern pl in
mk_pat (POr(pat,pat2)) pat.p_type (punion pat.p_pos pat2.p_pos)
| [] ->
assert false
let bind_remaining out pv stl =
let rec loop stl pv =
if Array.length pv = 0 then
[]
else
match stl,pv.(0).p_def with
| st :: stl,PAny ->
loop stl (array_tl pv)
| st :: stl,PVar v ->
(v,st) :: loop stl (array_tl pv)
| stl,PTuple pl ->
loop stl pl
| _ :: _,_->
loop stl (array_tl pv)
| [],_ ->
[]
in
loop stl pv
let get_cache mctx dt =
match dt with Goto _ -> dt | _ ->
try
Goto (Hashtbl.find mctx.dt_cache dt)
with Not_found ->
Hashtbl.replace mctx.dt_cache dt mctx.dt_count;
mctx.dt_count <- mctx.dt_count + 1;
DynArray.add mctx.dt_lut dt;
dt
let rec compile mctx stl pmat toplevel =
let guard id dt1 dt2 = get_cache mctx (Guard(id,dt1,dt2)) in
let expr id = get_cache mctx (Expr id) in
let bind bl dt = get_cache mctx (Bind(bl,dt)) in
let switch st cl = get_cache mctx (Switch(st,cl)) in
get_cache mctx (match pmat with
| [] ->
(match stl with
| st :: stl ->
let all,inf = all_ctors mctx st.st_type in
let pl = PMap.foldi (fun cd p acc -> (mk_con_pat cd [] t_dynamic p) :: acc) !all [] in
begin match pl,inf with
| _,Infinite
| [],_ ->
raise (Not_exhaustive(any,st))
| _ ->
raise (Not_exhaustive(collapse_pattern pl,st))
end
| _ ->
(* This can happen in cases a value is required and all default cases are guarded (issue #3150).
Not a particularly elegant solution, may want to revisit this later. *)
raise Exit)
| ([|{p_def = PTuple pt}|],out) :: pl ->
compile mctx stl ((pt,out) :: pl) toplevel
| (pv,out) :: pl ->
let i = pick_column pmat in
if i = -1 then begin
out.o_num_paths <- out.o_num_paths + 1;
let bl = bind_remaining out pv stl in
let dt = match (get_guard mctx out.o_id) with
| None ->
expr out.o_id
| Some _ ->
let dt = match pl,mctx.need_val with
| [],false ->
None
| _ ->
Some (compile mctx stl pl false)
in
guard out.o_id (expr out.o_id) dt
in
(if bl = [] then dt else bind bl dt)
end else if i > 0 then begin
let pmat = swap_pmat_columns i pmat in
let stls = swap_columns i stl in
compile mctx stls pmat toplevel
end else begin
let st_head,st_tail = match stl with st :: stl -> st,stl | _ -> assert false in
let pmat = expand_or mctx pmat in
let sigma,bl = column_sigma mctx st_head pmat in
let all,inf = all_ctors mctx pv.(0).p_type in
let cases = List.map (fun (c,g) ->
if not g then all := PMap.remove c.c_def !all;
let spec = spec mctx c pmat in
let hsubs = mk_subs st_head c in
let subs = hsubs @ st_tail in
let dt = compile mctx subs spec false in
c,dt
) sigma in
let def = default mctx pmat in
let dt = match def,cases with
| _ when inf = RunTimeFinite && PMap.is_empty !all ->
switch st_head cases
| [],_ when inf = CompileTimeFinite && PMap.is_empty !all ->
switch st_head cases
| [],_ when inf = Infinite && not mctx.need_val && toplevel ->
(* ignore exhaustiveness, but mark context so we do not generate @:exhaustive metadata *)
mctx.is_exhaustive <- false;
switch st_head cases
| [],_ when inf = Infinite ->
raise (Not_exhaustive(any,st_head))
| [],_ ->
let pl = PMap.foldi (fun cd p acc -> (mk_con_pat cd [] t_dynamic p) :: acc) !all [] in
(* toplevel null can be omitted because the French dig runtime errors (issue #3054) *)
if toplevel && (match pl with
| [{p_def = PCon ({c_def = (CConst TNull)},_)}] -> true
| _ -> false) then
switch st_head cases
else
raise (Not_exhaustive(collapse_pattern pl,st_head))
| def,[] ->
compile mctx st_tail def false
| def,_ ->
let cdef = mk_con CAny t_dynamic st_head.st_pos in
let def = try
compile mctx st_tail def false
with Exit ->
raise (Not_exhaustive(any,st_head))
in
let cases = cases @ [cdef,def] in
switch st_head cases
in
if bl = [] then dt else bind bl dt
end)
let rec collapse_case el = match el with
| e :: [] ->
e
| e :: el ->
let e2 = collapse_case el in
EBinop(OpOr,e,e2),punion (pos e) (pos e2)
| [] ->
assert false
let mk_const ctx p = function
| TString s -> mk (TConst (TString s)) ctx.com.basic.tstring p
| TInt i -> mk (TConst (TInt i)) ctx.com.basic.tint p
| TFloat f -> mk (TConst (TFloat f)) ctx.com.basic.tfloat p
| TBool b -> mk (TConst (TBool b)) ctx.com.basic.tbool p
| TNull -> mk (TConst TNull) (ctx.com.basic.tnull (mk_mono())) p
| _ -> error "Unsupported constant" p
let rec convert_st ctx st = match st.st_def with
| SVar v -> mk (TLocal v) v.v_type st.st_pos
| SField (sts,cf) ->
let e = convert_st ctx sts in
Typer.acc_get ctx (Typer.type_field ctx e cf.cf_name st.st_pos Typer.MGet) st.st_pos
| SArray (sts,i) -> mk (TArray(convert_st ctx sts,mk_const ctx st.st_pos (TInt (Int32.of_int i)))) st.st_type st.st_pos
| STuple (st,_,_) -> convert_st ctx st
| SEnum (sts,ef,i) -> mk (TEnumParameter(convert_st ctx sts, ef, i)) st.st_type st.st_pos
let convert_con ctx con = match con.c_def with
| CConst c -> mk_const ctx con.c_pos c
| CType mt -> mk (TTypeExpr mt) t_dynamic con.c_pos
| CExpr e -> e
| CEnum(e,ef) -> mk_const ctx con.c_pos (TInt (Int32.of_int ef.ef_index))
| CArray i -> mk_const ctx con.c_pos (TInt (Int32.of_int i))