-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.ml
2122 lines (1984 loc) · 65.8 KB
/
type.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
type path = string list * string
type field_kind =
| Var of var_kind
| Method of method_kind
and var_kind = {
v_read : var_access;
v_write : var_access;
}
and var_access =
| AccNormal
| AccNo (* can't be accessed outside of the class itself and its subclasses *)
| AccNever (* can't be accessed, even in subclasses *)
| AccResolve (* call resolve("field") when accessed *)
| AccCall (* perform a method call when accessed *)
| AccInline (* similar to Normal but inline when accessed *)
| AccRequire of string * string option (* set when @:require(cond) fails *)
and method_kind =
| MethNormal
| MethInline
| MethDynamic
| MethMacro
type t =
| TMono of t option ref
| TEnum of tenum * tparams
| TInst of tclass * tparams
| TType of tdef * tparams
| TFun of (string * bool * t) list * t
| TAnon of tanon
| TDynamic of t
| TLazy of (unit -> t) ref
| TAbstract of tabstract * tparams
and tparams = t list
and type_params = (string * t) list
and tconstant =
| TInt of int32
| TFloat of string
| TString of string
| TBool of bool
| TNull
| TThis
| TSuper
and tvar = {
mutable v_id : int;
mutable v_name : string;
mutable v_type : t;
mutable v_capture : bool;
mutable v_extra : (type_params * texpr option) option;
mutable v_meta : metadata;
}
and tfunc = {
tf_args : (tvar * tconstant option) list;
tf_type : t;
tf_expr : texpr;
}
and anon_status =
| Closed
| Opened
| Const
| Extend of t list
| Statics of tclass
| EnumStatics of tenum
| AbstractStatics of tabstract
and tanon = {
mutable a_fields : (string, tclass_field) PMap.t;
a_status : anon_status ref;
}
and texpr_expr =
| TConst of tconstant
| TLocal of tvar
| TArray of texpr * texpr
| TBinop of Ast.binop * texpr * texpr
| TField of texpr * tfield_access
| TTypeExpr of module_type
| TParenthesis of texpr
| TObjectDecl of (string * texpr) list
| TArrayDecl of texpr list
| TCall of texpr * texpr list
| TNew of tclass * tparams * texpr list
| TUnop of Ast.unop * Ast.unop_flag * texpr
| TFunction of tfunc
| TVar of tvar * texpr option
| TBlock of texpr list
| TFor of tvar * texpr * texpr
| TIf of texpr * texpr * texpr option
| TWhile of texpr * texpr * Ast.while_flag
| TSwitch of texpr * (texpr list * texpr) list * texpr option
| TTry of texpr * (tvar * texpr) list
| TReturn of texpr option
| TBreak
| TContinue
| TThrow of texpr
| TCast of texpr * module_type option
| TMeta of metadata_entry * texpr
| TEnumParameter of texpr * tenum_field * int
and tfield_access =
| FInstance of tclass * tparams * tclass_field
| FStatic of tclass * tclass_field
| FAnon of tclass_field
| FDynamic of string
| FClosure of (tclass * tparams) option * tclass_field (* None class = TAnon *)
| FEnum of tenum * tenum_field
and texpr = {
eexpr : texpr_expr;
etype : t;
epos : Ast.pos;
}
and tclass_field = {
mutable cf_name : string;
mutable cf_type : t;
mutable cf_public : bool;
cf_pos : pos;
mutable cf_doc : Ast.documentation;
mutable cf_meta : metadata;
mutable cf_kind : field_kind;
mutable cf_params : type_params;
mutable cf_expr : texpr option;
mutable cf_overloads : tclass_field list;
}
and tclass_kind =
| KNormal
| KTypeParameter of t list
| KExtension of tclass * tparams
| KExpr of Ast.expr
| KGeneric
| KGenericInstance of tclass * tparams
| KMacroType
| KGenericBuild of class_field list
| KAbstractImpl of tabstract
and metadata = Ast.metadata
and tinfos = {
mt_path : path;
mt_module : module_def;
mt_pos : Ast.pos;
mt_private : bool;
mt_doc : Ast.documentation;
mutable mt_meta : metadata;
mt_params : type_params;
}
and tclass = {
mutable cl_path : path;
mutable cl_module : module_def;
mutable cl_pos : Ast.pos;
mutable cl_private : bool;
mutable cl_doc : Ast.documentation;
mutable cl_meta : metadata;
mutable cl_params : type_params;
(* do not insert any fields above *)
mutable cl_kind : tclass_kind;
mutable cl_extern : bool;
mutable cl_interface : bool;
mutable cl_super : (tclass * tparams) option;
mutable cl_implements : (tclass * tparams) list;
mutable cl_fields : (string , tclass_field) PMap.t;
mutable cl_statics : (string, tclass_field) PMap.t;
mutable cl_ordered_statics : tclass_field list;
mutable cl_ordered_fields : tclass_field list;
mutable cl_dynamic : t option;
mutable cl_array_access : t option;
mutable cl_constructor : tclass_field option;
mutable cl_init : texpr option;
mutable cl_overrides : tclass_field list;
mutable cl_build : unit -> bool;
mutable cl_restore : unit -> unit;
}
and tenum_field = {
ef_name : string;
ef_type : t;
ef_pos : Ast.pos;
ef_doc : Ast.documentation;
ef_index : int;
ef_params : type_params;
mutable ef_meta : metadata;
}
and tenum = {
mutable e_path : path;
e_module : module_def;
e_pos : Ast.pos;
e_private : bool;
e_doc : Ast.documentation;
mutable e_meta : metadata;
mutable e_params : type_params;
(* do not insert any fields above *)
e_type : tdef;
mutable e_extern : bool;
mutable e_constrs : (string , tenum_field) PMap.t;
mutable e_names : string list;
}
and tdef = {
t_path : path;
t_module : module_def;
t_pos : Ast.pos;
t_private : bool;
t_doc : Ast.documentation;
mutable t_meta : metadata;
mutable t_params : type_params;
(* do not insert any fields above *)
mutable t_type : t;
}
and tabstract = {
mutable a_path : path;
a_module : module_def;
a_pos : Ast.pos;
a_private : bool;
a_doc : Ast.documentation;
mutable a_meta : metadata;
mutable a_params : type_params;
(* do not insert any fields above *)
mutable a_ops : (Ast.binop * tclass_field) list;
mutable a_unops : (Ast.unop * unop_flag * tclass_field) list;
mutable a_impl : tclass option;
mutable a_this : t;
mutable a_from : t list;
mutable a_from_field : (t * tclass_field) list;
mutable a_to : t list;
mutable a_to_field : (t * tclass_field) list;
mutable a_array : tclass_field list;
}
and module_type =
| TClassDecl of tclass
| TEnumDecl of tenum
| TTypeDecl of tdef
| TAbstractDecl of tabstract
and module_def = {
m_id : int;
m_path : path;
mutable m_types : module_type list;
m_extra : module_def_extra;
}
and module_def_extra = {
m_file : string;
m_sign : string;
mutable m_time : float;
mutable m_dirty : bool;
mutable m_added : int;
mutable m_mark : int;
mutable m_deps : (int,module_def) PMap.t;
mutable m_processed : int;
mutable m_kind : module_kind;
mutable m_binded_res : (string, string) PMap.t;
mutable m_macro_calls : string list;
mutable m_if_feature : (string *(tclass * tclass_field * bool)) list;
mutable m_features : (string,bool) Hashtbl.t;
}
and module_kind =
| MCode
| MMacro
| MFake
| MSub
| MExtern
and dt =
| DTSwitch of texpr * (texpr * dt) list * dt option
| DTBind of ((tvar * pos) * texpr) list * dt
| DTGoto of int
| DTExpr of texpr
| DTGuard of texpr * dt * dt option
and decision_tree = {
dt_dt_lookup : dt array;
dt_first : int;
dt_type : t;
dt_var_init : (tvar * texpr option) list;
dt_is_complex : bool;
}
(* ======= General utility ======= *)
let alloc_var =
let uid = ref 0 in
(fun ?(meta=[]) n t -> incr uid; { v_name = n; v_type = t; v_id = !uid; v_capture = false; v_extra = None; v_meta = meta })
let alloc_unbound_var n t =
alloc_var ~meta:[Meta.Unbound,[],null_pos] n t
let alloc_mid =
let mid = ref 0 in
(fun() -> incr mid; !mid)
let mk e t p = { eexpr = e; etype = t; epos = p }
let mk_block e =
match e.eexpr with
| TBlock _ -> e
| _ -> mk (TBlock [e]) e.etype e.epos
let mk_cast e t p = mk (TCast(e,None)) t p
let null t p = mk (TConst TNull) t p
let mk_mono() = TMono (ref None)
let rec t_dynamic = TDynamic t_dynamic
let tfun pl r = TFun (List.map (fun t -> "",false,t) pl,r)
let fun_args l = List.map (fun (a,c,t) -> a, c <> None, t) l
let mk_class m path pos =
{
cl_path = path;
cl_module = m;
cl_pos = pos;
cl_doc = None;
cl_meta = [];
cl_private = false;
cl_kind = KNormal;
cl_extern = false;
cl_interface = false;
cl_params = [];
cl_super = None;
cl_implements = [];
cl_fields = PMap.empty;
cl_ordered_statics = [];
cl_ordered_fields = [];
cl_statics = PMap.empty;
cl_dynamic = None;
cl_array_access = None;
cl_constructor = None;
cl_init = None;
cl_overrides = [];
cl_build = (fun() -> true);
cl_restore = (fun() -> ());
}
let module_extra file sign time kind =
{
m_file = file;
m_sign = sign;
m_dirty = false;
m_added = 0;
m_mark = 0;
m_time = time;
m_processed = 0;
m_deps = PMap.empty;
m_kind = kind;
m_binded_res = PMap.empty;
m_macro_calls = [];
m_if_feature = [];
m_features = Hashtbl.create 0;
}
let mk_field name t p = {
cf_name = name;
cf_type = t;
cf_pos = p;
cf_doc = None;
cf_meta = [];
cf_public = true;
cf_kind = Var { v_read = AccNormal; v_write = AccNormal };
cf_expr = None;
cf_params = [];
cf_overloads = [];
}
let null_module = {
m_id = alloc_mid();
m_path = [] , "";
m_types = [];
m_extra = module_extra "" "" 0. MFake;
}
let null_class =
let c = mk_class null_module ([],"") Ast.null_pos in
c.cl_private <- true;
c
let null_field = mk_field "" t_dynamic Ast.null_pos
let null_abstract = {
a_path = ([],"");
a_module = null_module;
a_pos = null_pos;
a_private = true;
a_doc = None;
a_meta = [];
a_params = [];
a_ops = [];
a_unops = [];
a_impl = None;
a_this = t_dynamic;
a_from = [];
a_from_field = [];
a_to = [];
a_to_field = [];
a_array = [];
}
let add_dependency m mdep =
if m != null_module && m != mdep then m.m_extra.m_deps <- PMap.add mdep.m_id mdep m.m_extra.m_deps
let arg_name (a,_) = a.v_name
let t_infos t : tinfos =
match t with
| TClassDecl c -> Obj.magic c
| TEnumDecl e -> Obj.magic e
| TTypeDecl t -> Obj.magic t
| TAbstractDecl a -> Obj.magic a
let t_path t = (t_infos t).mt_path
let rec is_parent csup c =
if c == csup || List.exists (fun (i,_) -> is_parent csup i) c.cl_implements then
true
else match c.cl_super with
| None -> false
| Some (c,_) -> is_parent csup c
let map loop t =
match t with
| TMono r ->
(match !r with
| None -> t
| Some t -> loop t) (* erase*)
| TEnum (_,[]) | TInst (_,[]) | TType (_,[]) ->
t
| TEnum (e,tl) ->
TEnum (e, List.map loop tl)
| TInst (c,tl) ->
TInst (c, List.map loop tl)
| TType (t2,tl) ->
TType (t2,List.map loop tl)
| TAbstract (a,tl) ->
TAbstract (a,List.map loop tl)
| TFun (tl,r) ->
TFun (List.map (fun (s,o,t) -> s, o, loop t) tl,loop r)
| TAnon a ->
let fields = PMap.map (fun f -> { f with cf_type = loop f.cf_type }) a.a_fields in
begin match !(a.a_status) with
| Opened ->
a.a_fields <- fields;
t
| _ ->
TAnon {
a_fields = fields;
a_status = a.a_status;
}
end
| TLazy f ->
let ft = !f() in
let ft2 = loop ft in
if ft == ft2 then t else ft2
| TDynamic t2 ->
if t == t2 then t else TDynamic (loop t2)
let follow_class = function
| { cl_kind = KGenericInstance(c, _)} -> c
| c -> c
let get_type_list tl = function
| { cl_kind = KGenericInstance(_, tl) } -> tl
| _ -> tl
(* substitute parameters with other types *)
let apply_params cparams params t =
match cparams with
| [] -> t
| _ ->
let rec loop l1 l2 =
match l1, l2 with
| [] , [] -> []
| (x,TLazy f) :: l1, _ -> loop ((x,(!f)()) :: l1) l2
| (_,t1) :: l1 , t2 :: l2 -> (t1,t2) :: loop l1 l2
| _ -> assert false
in
let subst = loop cparams params in
let rec loop t =
try
List.assq t subst
with Not_found ->
match t with
| TMono r ->
(match !r with
| None -> t
| Some t -> loop t)
| TEnum (e,tl) ->
(match tl with
| [] -> t
| _ -> TEnum (e,List.map loop tl))
| TType (t2,tl) ->
(match tl with
| [] -> t
| _ -> TType (t2,List.map loop tl))
| TAbstract (a,tl) ->
(match tl with
| [] -> t
| _ -> TAbstract (a,List.map loop tl))
| TInst (c,tl) ->
(match tl with
| [] ->
t
| [TMono r] ->
(match !r with
| Some tt when t == tt ->
(* for dynamic *)
let pt = mk_mono() in
let t = TInst (c,[pt]) in
(match pt with TMono r -> r := Some t | _ -> assert false);
t
| _ -> TInst (c,List.map loop tl))
| _ ->
TInst (c,List.map loop tl))
| TFun (tl,r) ->
TFun (List.map (fun (s,o,t) -> s, o, loop t) tl,loop r)
| TAnon a ->
let fields = PMap.map (fun f -> { f with cf_type = loop f.cf_type }) a.a_fields in
begin match !(a.a_status) with
| Opened ->
a.a_fields <- fields;
t
| _ ->
TAnon {
a_fields = fields;
a_status = a.a_status;
}
end
| TLazy f ->
let ft = !f() in
let ft2 = loop ft in
if ft == ft2 then
t
else
ft2
| TDynamic t2 ->
if t == t2 then
t
else
TDynamic (loop t2)
in
loop t
let monomorphs eparams t =
apply_params eparams (List.map (fun _ -> mk_mono()) eparams) t
let rec follow t =
match t with
| TMono r ->
(match !r with
| Some t -> follow t
| _ -> t)
| TLazy f ->
follow (!f())
| TType (t,tl) ->
follow (apply_params t.t_params tl t.t_type)
| _ -> t
let rec is_nullable = function
| TMono r ->
(match !r with None -> false | Some t -> is_nullable t)
| TType ({ t_path = ([],"Null") },[_]) ->
true
| TLazy f ->
is_nullable (!f())
| TType (t,tl) ->
is_nullable (apply_params t.t_params tl t.t_type)
| TFun _ ->
false
(*
Type parameters will most of the time be nullable objects, so we don't want to make it hard for users
to have to specify Null<T> all over the place, so while they could be a basic type, let's assume they will not.
This will still cause issues with inlining and haxe.rtti.Generic. In that case proper explicit Null<T> is required to
work correctly with basic types. This could still be fixed by redoing a nullability inference on the typed AST.
| TInst ({ cl_kind = KTypeParameter },_) -> false
*)
| TAbstract (a,_) when Meta.has Meta.CoreType a.a_meta ->
not (Meta.has Meta.NotNull a.a_meta)
| TAbstract (a,tl) ->
not (Meta.has Meta.NotNull a.a_meta) && is_nullable (apply_params a.a_params tl a.a_this)
| _ ->
true
let rec is_null ?(no_lazy=false) = function
| TMono r ->
(match !r with None -> false | Some t -> is_null t)
| TType ({ t_path = ([],"Null") },[t]) ->
not (is_nullable (follow t))
| TLazy f ->
if no_lazy then raise Exit else is_null (!f())
| TType (t,tl) ->
is_null (apply_params t.t_params tl t.t_type)
| _ ->
false
(* Determines if we have a Null<T>. Unlike is_null, this returns true even if the wrapped type is nullable itself. *)
let rec is_explicit_null = function
| TMono r ->
(match !r with None -> false | Some t -> is_null t)
| TType ({ t_path = ([],"Null") },[t]) ->
true
| TLazy f ->
is_null (!f())
| TType (t,tl) ->
is_null (apply_params t.t_params tl t.t_type)
| _ ->
false
let rec has_mono t = match t with
| TMono r ->
(match !r with None -> true | Some t -> has_mono t)
| TInst(_,pl) | TEnum(_,pl) | TAbstract(_,pl) | TType(_,pl) ->
List.exists has_mono pl
| TDynamic _ ->
false
| TFun(args,r) ->
has_mono r || List.exists (fun (_,_,t) -> has_mono t) args
| TAnon a ->
PMap.fold (fun cf b -> has_mono cf.cf_type || b) a.a_fields false
| TLazy r ->
has_mono (!r())
let concat e1 e2 =
let e = (match e1.eexpr, e2.eexpr with
| TBlock el1, TBlock el2 -> TBlock (el1@el2)
| TBlock el, _ -> TBlock (el @ [e2])
| _, TBlock el -> TBlock (e1 :: el)
| _ , _ -> TBlock [e1;e2]
) in
mk e e2.etype (punion e1.epos e2.epos)
let is_closed a = !(a.a_status) <> Opened
let type_of_module_type = function
| TClassDecl c -> TInst (c,List.map snd c.cl_params)
| TEnumDecl e -> TEnum (e,List.map snd e.e_params)
| TTypeDecl t -> TType (t,List.map snd t.t_params)
| TAbstractDecl a -> TAbstract (a,List.map snd a.a_params)
(* ======= Field utility ======= *)
let field_name f =
match f with
| FAnon f | FInstance (_,_,f) | FStatic (_,f) | FClosure (_,f) -> f.cf_name
| FEnum (_,f) -> f.ef_name
| FDynamic n -> n
let extract_field = function
| FAnon f | FInstance (_,_,f) | FStatic (_,f) | FClosure (_,f) -> Some f
| _ -> None
let is_extern_field f =
match f.cf_kind with
| Method _ -> false
| Var { v_read = AccNormal | AccInline | AccNo } | Var { v_write = AccNormal | AccNo } -> false
| _ -> not (Meta.has Meta.IsVar f.cf_meta)
let field_type f =
match f.cf_params with
| [] -> f.cf_type
| l -> monomorphs l f.cf_type
let rec raw_class_field build_type c tl i =
let apply = apply_params c.cl_params tl in
try
let f = PMap.find i c.cl_fields in
Some (c,tl), build_type f , f
with Not_found -> try (match c.cl_constructor with
| Some ctor when i = "new" -> Some (c,tl), build_type ctor,ctor
| _ -> raise Not_found)
with Not_found -> try
match c.cl_super with
| None ->
raise Not_found
| Some (c,tl) ->
let c2 , t , f = raw_class_field build_type c (List.map apply tl) i in
c2, apply_params c.cl_params tl t , f
with Not_found ->
match c.cl_kind with
| KTypeParameter tl ->
let rec loop = function
| [] ->
raise Not_found
| t :: ctl ->
match follow t with
| TAnon a ->
(try
let f = PMap.find i a.a_fields in
None, build_type f, f
with
Not_found -> loop ctl)
| TInst (c,tl) ->
(try
let c2, t , f = raw_class_field build_type c (List.map apply tl) i in
c2, apply_params c.cl_params tl t, f
with
Not_found -> loop ctl)
| _ ->
loop ctl
in
loop tl
| _ ->
if not c.cl_interface then raise Not_found;
(*
an interface can implements other interfaces without
having to redeclare its fields
*)
let rec loop = function
| [] ->
raise Not_found
| (c,tl) :: l ->
try
let c2, t , f = raw_class_field build_type c (List.map apply tl) i in
c2, apply_params c.cl_params tl t, f
with
Not_found -> loop l
in
loop c.cl_implements
let class_field = raw_class_field field_type
let quick_field t n =
match follow t with
| TInst (c,tl) ->
let c, _, f = raw_class_field (fun f -> f.cf_type) c tl n in
(match c with None -> FAnon f | Some (c,tl) -> FInstance (c,tl,f))
| TAnon a ->
(match !(a.a_status) with
| EnumStatics e ->
let ef = PMap.find n e.e_constrs in
FEnum(e,ef)
| Statics c ->
FStatic (c,PMap.find n c.cl_statics)
| AbstractStatics a ->
begin match a.a_impl with
| Some c ->
let cf = PMap.find n c.cl_statics in
FStatic(c,cf) (* is that right? *)
| _ ->
raise Not_found
end
| _ ->
FAnon (PMap.find n a.a_fields))
| TDynamic _ ->
FDynamic n
| TEnum _ | TMono _ | TAbstract _ | TFun _ ->
raise Not_found
| TLazy _ | TType _ ->
assert false
let quick_field_dynamic t s =
try quick_field t s
with Not_found -> FDynamic s
let rec get_constructor build_type c =
match c.cl_constructor, c.cl_super with
| Some c, _ -> build_type c, c
| None, None -> raise Not_found
| None, Some (csup,cparams) ->
let t, c = get_constructor build_type csup in
apply_params csup.cl_params cparams t, c
(* ======= Printing ======= *)
let print_context() = ref []
let rec s_type_kind t =
let map tl = String.concat ", " (List.map s_type_kind tl) in
match t with
| TMono r ->
begin match !r with
| None -> "TMono (None)"
| Some t -> "TMono (Some (" ^ (s_type_kind t) ^ "))"
end
| TEnum(en,tl) -> Printf.sprintf "TEnum(%s, [%s])" (s_type_path en.e_path) (map tl)
| TInst(c,tl) -> Printf.sprintf "TInst(%s, [%s])" (s_type_path c.cl_path) (map tl)
| TType(t,tl) -> Printf.sprintf "TType(%s, [%s])" (s_type_path t.t_path) (map tl)
| TAbstract(a,tl) -> Printf.sprintf "TAbstract(%s, [%s])" (s_type_path a.a_path) (map tl)
| TFun(tl,r) -> Printf.sprintf "TFun([%s], %s)" (String.concat ", " (List.map (fun (n,b,t) -> Printf.sprintf "%s%s:%s" (if b then "?" else "") n (s_type_kind t)) tl)) (s_type_kind r)
| TAnon an -> "TAnon"
| TDynamic t2 -> "TDynamic"
| TLazy _ -> "TLazy"
let rec s_type ctx t =
match t with
| TMono r ->
(match !r with
| None -> Printf.sprintf "Unknown<%d>" (try List.assq t (!ctx) with Not_found -> let n = List.length !ctx in ctx := (t,n) :: !ctx; n)
| Some t -> s_type ctx t)
| TEnum (e,tl) ->
Ast.s_type_path e.e_path ^ s_type_params ctx tl
| TInst (c,tl) ->
(match c.cl_kind with
| KExpr e -> Ast.s_expr e
| _ -> Ast.s_type_path c.cl_path ^ s_type_params ctx tl)
| TType (t,tl) ->
Ast.s_type_path t.t_path ^ s_type_params ctx tl
| TAbstract (a,tl) ->
Ast.s_type_path a.a_path ^ s_type_params ctx tl
| TFun ([],t) ->
"Void -> " ^ s_fun ctx t false
| TFun (l,t) ->
String.concat " -> " (List.map (fun (s,b,t) ->
(if b then "?" else "") ^ (if s = "" then "" else s ^ " : ") ^ s_fun ctx t true
) l) ^ " -> " ^ s_fun ctx t false
| TAnon a ->
let fl = PMap.fold (fun f acc -> ((if Meta.has Meta.Optional f.cf_meta then " ?" else " ") ^ f.cf_name ^ " : " ^ s_type ctx f.cf_type) :: acc) a.a_fields [] in
"{" ^ (if not (is_closed a) then "+" else "") ^ String.concat "," fl ^ " }"
| TDynamic t2 ->
"Dynamic" ^ s_type_params ctx (if t == t2 then [] else [t2])
| TLazy f ->
s_type ctx (!f())
and s_fun ctx t void =
match t with
| TFun _ ->
"(" ^ s_type ctx t ^ ")"
| TAbstract ({ a_path = ([],"Void") },[]) when void ->
"(" ^ s_type ctx t ^ ")"
| TMono r ->
(match !r with
| None -> s_type ctx t
| Some t -> s_fun ctx t void)
| TLazy f ->
s_fun ctx (!f()) void
| _ ->
s_type ctx t
and s_type_params ctx = function
| [] -> ""
| l -> "<" ^ String.concat ", " (List.map (s_type ctx) l) ^ ">"
let s_access is_read = function
| AccNormal -> "default"
| AccNo -> "null"
| AccNever -> "never"
| AccResolve -> "resolve"
| AccCall -> if is_read then "get" else "set"
| AccInline -> "inline"
| AccRequire (n,_) -> "require " ^ n
let s_kind = function
| Var { v_read = AccNormal; v_write = AccNormal } -> "var"
| Var v -> "(" ^ s_access true v.v_read ^ "," ^ s_access false v.v_write ^ ")"
| Method m ->
match m with
| MethNormal -> "method"
| MethDynamic -> "dynamic method"
| MethInline -> "inline method"
| MethMacro -> "macro method"
let s_expr_kind e =
match e.eexpr with
| TConst _ -> "Const"
| TLocal _ -> "Local"
| TArray (_,_) -> "Array"
| TBinop (_,_,_) -> "Binop"
| TEnumParameter (_,_,_) -> "EnumParameter"
| TField (_,_) -> "Field"
| TTypeExpr _ -> "TypeExpr"
| TParenthesis _ -> "Parenthesis"
| TObjectDecl _ -> "ObjectDecl"
| TArrayDecl _ -> "ArrayDecl"
| TCall (_,_) -> "Call"
| TNew (_,_,_) -> "New"
| TUnop (_,_,_) -> "Unop"
| TFunction _ -> "Function"
| TVar _ -> "Vars"
| TBlock _ -> "Block"
| TFor (_,_,_) -> "For"
| TIf (_,_,_) -> "If"
| TWhile (_,_,_) -> "While"
| TSwitch (_,_,_) -> "Switch"
| TTry (_,_) -> "Try"
| TReturn _ -> "Return"
| TBreak -> "Break"
| TContinue -> "Continue"
| TThrow _ -> "Throw"
| TCast _ -> "Cast"
| TMeta _ -> "Meta"
let s_const = function
| TInt i -> Int32.to_string i
| TFloat s -> s
| TString s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)
| TBool b -> if b then "true" else "false"
| TNull -> "null"
| TThis -> "this"
| TSuper -> "super"
let rec s_expr s_type e =
let sprintf = Printf.sprintf in
let slist f l = String.concat "," (List.map f l) in
let loop = s_expr s_type in
let s_var v = v.v_name ^ ":" ^ string_of_int v.v_id ^ if v.v_capture then "[c]" else "" in
let str = (match e.eexpr with
| TConst c ->
"Const " ^ s_const c
| TLocal v ->
"Local " ^ s_var v
| TArray (e1,e2) ->
sprintf "%s[%s]" (loop e1) (loop e2)
| TBinop (op,e1,e2) ->
sprintf "(%s %s %s)" (loop e1) (s_binop op) (loop e2)
| TEnumParameter (e1,_,i) ->
sprintf "%s[%i]" (loop e1) i
| TField (e,f) ->
let fstr = (match f with
| FStatic (c,f) -> "static(" ^ s_type_path c.cl_path ^ "." ^ f.cf_name ^ ")"
| FInstance (c,_,f) -> "inst(" ^ s_type_path c.cl_path ^ "." ^ f.cf_name ^ " : " ^ s_type f.cf_type ^ ")"
| FClosure (c,f) -> "closure(" ^ (match c with None -> f.cf_name | Some (c,_) -> s_type_path c.cl_path ^ "." ^ f.cf_name) ^ ")"
| FAnon f -> "anon(" ^ f.cf_name ^ ")"
| FEnum (en,f) -> "enum(" ^ s_type_path en.e_path ^ "." ^ f.ef_name ^ ")"
| FDynamic f -> "dynamic(" ^ f ^ ")"
) in
sprintf "%s.%s" (loop e) fstr
| TTypeExpr m ->
sprintf "TypeExpr %s" (s_type_path (t_path m))
| TParenthesis e ->
sprintf "Parenthesis %s" (loop e)
| TObjectDecl fl ->
sprintf "ObjectDecl {%s}" (slist (fun (f,e) -> sprintf "%s : %s" f (loop e)) fl)
| TArrayDecl el ->
sprintf "ArrayDecl [%s]" (slist loop el)
| TCall (e,el) ->
sprintf "Call %s(%s)" (loop e) (slist loop el)
| TNew (c,pl,el) ->
sprintf "New %s%s(%s)" (s_type_path c.cl_path) (match pl with [] -> "" | l -> sprintf "<%s>" (slist s_type l)) (slist loop el)
| TUnop (op,f,e) ->
(match f with
| Prefix -> sprintf "(%s %s)" (s_unop op) (loop e)
| Postfix -> sprintf "(%s %s)" (loop e) (s_unop op))
| TFunction f ->
let args = slist (fun (v,o) -> sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match o with None -> "" | Some c -> " = " ^ s_const c)) f.tf_args in
sprintf "Function(%s) : %s = %s" args (s_type f.tf_type) (loop f.tf_expr)
| TVar (v,eo) ->
sprintf "Vars %s" (sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match eo with None -> "" | Some e -> " = " ^ loop e))
| TBlock el ->
sprintf "Block {\n%s}" (String.concat "" (List.map (fun e -> sprintf "%s;\n" (loop e)) el))
| TFor (v,econd,e) ->
sprintf "For (%s : %s in %s,%s)" (s_var v) (s_type v.v_type) (loop econd) (loop e)
| TIf (e,e1,e2) ->
sprintf "If (%s,%s%s)" (loop e) (loop e1) (match e2 with None -> "" | Some e -> "," ^ loop e)
| TWhile (econd,e,flag) ->
(match flag with
| NormalWhile -> sprintf "While (%s,%s)" (loop econd) (loop e)
| DoWhile -> sprintf "DoWhile (%s,%s)" (loop e) (loop econd))
| TSwitch (e,cases,def) ->
sprintf "Switch (%s,(%s)%s)" (loop e) (slist (fun (cl,e) -> sprintf "case %s: %s" (slist loop cl) (loop e)) cases) (match def with None -> "" | Some e -> "," ^ loop e)
| TTry (e,cl) ->
sprintf "Try %s(%s) " (loop e) (slist (fun (v,e) -> sprintf "catch( %s : %s ) %s" (s_var v) (s_type v.v_type) (loop e)) cl)
| TReturn None ->
"Return"
| TReturn (Some e) ->
sprintf "Return %s" (loop e)
| TBreak ->