forked from HaxeFoundation/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyper.ml
3078 lines (3004 loc) · 99.8 KB
/
typer.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
(*
* Haxe Compiler
* Copyright (c)2005-2008 Nicolas Cannasse
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
open Ast
open Type
open Common
open Typecore
(* ---------------------------------------------------------------------- *)
(* TOOLS *)
type switch_mode =
| CMatch of (tenum_field * (string * t) option list option * pos)
| CExpr of texpr
type access_mode =
| MGet
| MSet
| MCall
exception DisplayTypes of t list
exception DisplayFields of (string * t * documentation) list
type access_kind =
| AKNo of string
| AKExpr of texpr
| AKField of texpr * tclass_field
| AKSet of texpr * string * t * string
| AKInline of texpr * tclass_field * t
| AKMacro of texpr * tclass_field
| AKUsing of texpr * tclass * tclass_field * texpr
let mk_infos ctx p params =
let file = if ctx.in_macro then p.pfile else if Common.defined ctx.com "absolute_path" then Common.get_full_path p.pfile else Filename.basename p.pfile in
(EObjectDecl (
("fileName" , (EConst (String file) , p)) ::
("lineNumber" , (EConst (Int (string_of_int (Lexer.get_error_line p))),p)) ::
("className" , (EConst (String (s_type_path ctx.curclass.cl_path)),p)) ::
if ctx.curmethod = "" then
params
else
("methodName", (EConst (String ctx.curmethod),p)) :: params
) ,p)
let check_assign ctx e =
match e.eexpr with
| TLocal _ | TArray _ | TField _ ->
()
| TConst TThis | TTypeExpr _ when ctx.untyped ->
()
| _ ->
error "Invalid assign" e.epos
type type_class =
| KInt
| KFloat
| KString
| KUnk
| KDyn
| KOther
| KParam of t
let rec classify t =
match follow t with
| TInst ({ cl_path = ([],"Int") },[]) -> KInt
| TInst ({ cl_path = ([],"Float") },[]) -> KFloat
| TInst ({ cl_path = ([],"String") },[]) -> KString
| TInst ({ cl_kind = KTypeParameter ctl },_) when List.exists (fun t -> match classify t with KInt | KFloat -> true | _ -> false) ctl -> KParam t
| TMono r when !r = None -> KUnk
| TDynamic _ -> KDyn
| _ -> KOther
let object_field f =
let pf = Parser.quoted_ident_prefix in
let pflen = String.length pf in
if String.length f >= pflen && String.sub f 0 pflen = pf then String.sub f pflen (String.length f - pflen), false else f, true
let type_field_rec = ref (fun _ _ _ _ _ -> assert false)
let rec is_pos_infos = function
| TMono r ->
(match !r with
| Some t -> is_pos_infos t
| _ -> false)
| TLazy f ->
is_pos_infos (!f())
| TType ({ t_path = ["haxe"] , "PosInfos" },[]) ->
true
| TType (t,tl) ->
is_pos_infos (apply_params t.t_types tl t.t_type)
| _ ->
false
let field_type ctx c pl f p =
match f.cf_params with
| [] -> f.cf_type
| l ->
let monos = List.map (fun _ -> mk_mono()) l in
List.iter2 (fun m (name,t) ->
match follow t with
| TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
let constr = List.map (fun t ->
let t = apply_params f.cf_params monos t in
(* only apply params if not static : in that case no param is passed *)
let t = (if pl = [] then t else apply_params c.cl_types pl t) in
t
) constr in
delay_late ctx (fun() ->
List.iter (fun ct ->
try
Type.unify m ct
with Unify_error l ->
display_error ctx (error_msg (Unify (Constraint_failure (f.cf_name ^ "." ^ name) :: l))) p;
let pc = pos_t ct in
if pc <> Ast.null_pos then display_error ctx "Constraint was defined here" pc;
) constr
);
| _ -> ()
) monos l;
apply_params l monos f.cf_type
let class_field ctx c pl name p =
raw_class_field (fun f -> field_type ctx c pl f p) c name
(* checks if class cs(ource) can access field cf of class ct(arget) *)
let can_access cs ct cf stat =
let rec loop ct =
((is_parent ct cs) && (PMap.mem cf.cf_name (if stat then ct.cl_statics else ct.cl_fields)))
|| match ct.cl_super with
| Some (ct,_) -> loop ct
| None -> false
in
cf.cf_public || loop ct
(* removes the first argument of the class field's function type and all its overloads *)
let prepare_using_field cf = match cf.cf_type with
| TFun((_,_,tf) :: args,ret) ->
let rec loop acc overloads = match overloads with
| ({cf_type = TFun((_,_,tfo) :: args,ret)} as cfo) :: l ->
let tfo = apply_params cfo.cf_params (List.map snd cf.cf_params) tfo in
(* ignore overloads which have a different first argument *)
if Type.type_iseq tf tfo then loop ({cfo with cf_type = TFun(args,ret)} :: acc) l else loop acc l
| _ :: l ->
loop acc l
| [] ->
acc
in
{cf with cf_overloads = loop [] cf.cf_overloads; cf_type = TFun(args,ret)}
| _ -> cf
(* ---------------------------------------------------------------------- *)
(* PASS 3 : type expression & check structure *)
let rec base_types t =
let tl = ref [] in
let rec loop t = (match t with
| TInst(cl, params) ->
(match cl.cl_kind with
| KTypeParameter tl -> List.iter loop tl
| _ -> ());
List.iter (fun (ic, ip) ->
let t = apply_params cl.cl_types params (TInst (ic,ip)) in
loop t
) cl.cl_implements;
(match cl.cl_super with None -> () | Some (csup, pl) ->
let t = apply_params cl.cl_types params (TInst (csup,pl)) in
loop t);
tl := t :: !tl;
| TType (td,pl) ->
loop (apply_params td.t_types pl td.t_type);
(* prioritize the most generic definition *)
tl := t :: !tl;
| TLazy f -> loop (!f())
| TMono r -> (match !r with None -> () | Some t -> loop t)
| _ -> tl := t :: !tl) in
loop t;
!tl
let rec unify_min_raise ctx (el:texpr list) : t =
match el with
| [] -> mk_mono()
| [e] -> e.etype
| _ ->
let rec chk_null e = is_null e.etype ||
match e.eexpr with
| TConst TNull -> true
| TBlock el ->
(match List.rev el with
| [] -> false
| e :: _ -> chk_null e)
| TParenthesis e -> chk_null e
| _ -> false
in
(* First pass: Try normal unification and find out if null is involved. *)
let rec loop t = function
| [] ->
false, t
| e :: el ->
let t = if chk_null e then ctx.t.tnull t else t in
try
unify_raise ctx e.etype t e.epos;
loop t el
with Error (Unify _,_) -> try
unify_raise ctx t e.etype e.epos;
loop (if is_null t then ctx.t.tnull e.etype else e.etype) el
with Error (Unify _,_) ->
true, t
in
let has_error, t = loop (mk_mono()) el in
if not has_error then
t
else try
(* specific case for const anon : we don't want to hide fields but restrict their common type *)
let fcount = ref (-1) in
let field_count a =
PMap.fold (fun _ acc -> acc + 1) a.a_fields 0
in
let expr f = match f.cf_expr with None -> mk (TBlock []) f.cf_type f.cf_pos | Some e -> e in
let fields = List.fold_left (fun acc e ->
match follow e.etype with
| TAnon a when !(a.a_status) = Const ->
a.a_status := Closed;
if !fcount = -1 then begin
fcount := field_count a;
PMap.map (fun f -> [expr f]) a.a_fields
end else begin
if !fcount <> field_count a then raise Not_found;
PMap.mapi (fun n el -> expr (PMap.find n a.a_fields) :: el) acc
end
| _ ->
raise Not_found
) PMap.empty el in
let fields = PMap.foldi (fun n el acc ->
let t = try unify_min_raise ctx el with Error (Unify _, _) -> raise Not_found in
PMap.add n (mk_field n t (List.hd el).epos) acc
) fields PMap.empty in
TAnon { a_fields = fields; a_status = ref Closed }
with Not_found ->
(* Second pass: Get all base types (interfaces, super classes and their interfaces) of most general type.
Then for each additional type filter all types that do not unify. *)
let common_types = base_types t in
let dyn_types = List.fold_left (fun acc t ->
let rec loop c =
has_meta ":unifyMinDynamic" c.cl_meta || (match c.cl_super with None -> false | Some (c,_) -> loop c)
in
match t with
| TInst (c,params) when params <> [] && loop c ->
TInst (c,List.map (fun _ -> t_dynamic) params) :: acc
| _ -> acc
) [] common_types in
let common_types = ref (match List.rev dyn_types with [] -> common_types | l -> common_types @ l) in
let loop e =
let first_error = ref None in
let filter t = (try unify_raise ctx e.etype t e.epos; true
with Error (Unify l, p) as err -> if !first_error = None then first_error := Some(err); false)
in
common_types := List.filter filter !common_types;
match !common_types, !first_error with
| [], Some err -> raise err
| _ -> ()
in
List.iter loop (List.tl el);
List.hd !common_types
let unify_min ctx el =
try unify_min_raise ctx el
with Error (Unify l,p) ->
if not ctx.untyped then display_error ctx (error_msg (Unify l)) p;
(List.hd el).etype
let rec unify_call_params ctx cf el args r p inline =
let next() =
match cf with
| Some (TInst(c,pl),{ cf_overloads = o :: l }) ->
let args, ret = (match field_type ctx c pl o p with
| TFun (tl,t) -> tl, t
| _ -> assert false
) in
Some (unify_call_params ctx (Some (TInst(c,pl),{ o with cf_overloads = l })) el args ret p inline)
| Some (t,{ cf_overloads = o :: l }) ->
let args, ret = (match Type.field_type o with
| TFun (tl,t) -> tl, t
| _ -> assert false
) in
Some (unify_call_params ctx (Some (t, { o with cf_overloads = l })) el args ret p inline)
| _ ->
None
in
let error acc txt =
match next() with
| Some l -> l
| None ->
let format_arg = (fun (name,opt,_) -> (if opt then "?" else "") ^ name) in
let argstr = "Function " ^ (match cf with None -> "" | Some (_,f) -> "'" ^ f.cf_name ^ "' ") ^ "requires " ^ (if args = [] then "no arguments" else "arguments : " ^ String.concat ", " (List.map format_arg args)) in
display_error ctx (txt ^ " arguments\n" ^ argstr) p;
List.rev (List.map fst acc), (TFun(args,r))
in
let arg_error ul name opt p =
match next() with
| Some l -> l
| None -> raise (Error (Stack (Unify ul,Custom ("For " ^ (if opt then "optional " else "") ^ "function argument '" ^ name ^ "'")), p))
in
let rec no_opt = function
| [] -> []
| ({ eexpr = TConst TNull },true) :: l -> no_opt l
| l -> List.map fst l
in
let rec default_value t =
if is_pos_infos t then
let infos = mk_infos ctx p [] in
let e = type_expr ctx infos true in
(e, true)
else
(null (ctx.t.tnull t) p, true)
in
let rec loop acc l l2 skip =
match l , l2 with
| [] , [] ->
if not (inline && ctx.g.doinline) && (match ctx.com.platform with Flash8 | Flash | Js -> true | _ -> false) then
List.rev (no_opt acc), (TFun(args,r))
else
List.rev (List.map fst acc), (TFun(args,r))
| [] , (_,false,_) :: _ ->
error (List.fold_left (fun acc (_,_,t) -> default_value t :: acc) acc l2) "Not enough"
| [] , (name,true,t) :: l ->
loop (default_value t :: acc) [] l skip
| _ , [] ->
(match List.rev skip with
| [] -> error acc "Too many"
| [name,ul] -> arg_error ul name true p
| _ -> error acc "Invalid")
| ee :: l, (name,opt,t) :: l2 ->
try
let e = type_expr_with_type ctx ee (Some t) true in
unify_raise ctx e.etype t e.epos;
loop ((e,false) :: acc) l l2 skip
with
Error (Unify ul,_) ->
if opt then
loop (default_value t :: acc) (ee :: l) l2 ((name,ul) :: skip)
else
arg_error ul name false (snd ee)
in
loop [] el args []
let rec type_module_type ctx t tparams p =
match t with
| TClassDecl c ->
let t_tmp = {
t_path = fst c.cl_path, "#" ^ snd c.cl_path;
t_module = c.cl_module;
t_doc = None;
t_pos = c.cl_pos;
t_type = TAnon {
a_fields = c.cl_statics;
a_status = ref (Statics c);
};
t_private = true;
t_types = [];
t_meta = no_meta;
} in
mk (TTypeExpr (TClassDecl c)) (TType (t_tmp,[])) p
| TEnumDecl e ->
let types = (match tparams with None -> List.map (fun _ -> mk_mono()) e.e_types | Some l -> l) in
let fl = PMap.fold (fun f acc ->
PMap.add f.ef_name {
cf_name = f.ef_name;
cf_public = true;
cf_type = f.ef_type;
cf_kind = (match follow f.ef_type with
| TFun _ -> Method MethNormal
| _ -> Var { v_read = AccNormal; v_write = AccNo }
);
cf_pos = e.e_pos;
cf_doc = None;
cf_meta = no_meta;
cf_expr = None;
cf_params = [];
cf_overloads = [];
} acc
) e.e_constrs PMap.empty in
let t_tmp = {
t_path = fst e.e_path, "#" ^ snd e.e_path;
t_module = e.e_module;
t_doc = None;
t_pos = e.e_pos;
t_type = TAnon {
a_fields = fl;
a_status = ref (EnumStatics e);
};
t_private = true;
t_types = e.e_types;
t_meta = no_meta;
} in
mk (TTypeExpr (TEnumDecl e)) (TType (t_tmp,types)) p
| TTypeDecl s ->
let t = apply_params s.t_types (List.map (fun _ -> mk_mono()) s.t_types) s.t_type in
match follow t with
| TEnum (e,params) ->
type_module_type ctx (TEnumDecl e) (Some params) p
| TInst (c,params) ->
type_module_type ctx (TClassDecl c) (Some params) p
| _ ->
error (s_type_path s.t_path ^ " is not a value") p
let type_type ctx tpath p =
type_module_type ctx (Typeload.load_type_def ctx p { tpackage = fst tpath; tname = snd tpath; tparams = []; tsub = None }) None p
let get_constructor ctx c params p =
let ct, f = (try Type.get_constructor (fun f -> field_type ctx c params f p) c with Not_found -> error (s_type_path c.cl_path ^ " does not have a constructor") p) in
apply_params c.cl_types params ct, f
let make_call ctx e params t p =
try
let ethis, fname = (match e.eexpr with TField (ethis,fname) -> ethis, fname | _ -> raise Exit) in
let f, cl = (match follow ethis.etype with
| TInst (c,params) -> snd (try Type.class_field c fname with Not_found -> raise Exit), Some c
| TAnon a -> (try PMap.find fname a.a_fields with Not_found -> raise Exit), (match !(a.a_status) with Statics c -> Some c | _ -> None)
| _ -> raise Exit
) in
if ctx.com.display || f.cf_kind <> Method MethInline then raise Exit;
let is_extern = (match cl with
| Some { cl_extern = true } -> true
| _ when has_meta ":extern" f.cf_meta -> true
| _ -> false
) in
(* can not inline generic instance calls here *)
(match cl with Some {cl_kind = KGenericInstance _} -> raise Exit | _ -> ());
if not ctx.g.doinline && not is_extern then raise Exit;
ignore(follow f.cf_type); (* force evaluation *)
let params = List.map (ctx.g.do_optimize ctx) params in
(match f.cf_expr with
| Some { eexpr = TFunction fd } ->
(match Optimizer.type_inline ctx f fd ethis params t p is_extern with
| None ->
if is_extern then error "Inline could not be done" p;
raise Exit;
| Some e -> e)
| _ ->
error "Recursive inline is not supported" p)
with Exit ->
mk (TCall (e,params)) t p
let rec acc_get ctx g p =
match g with
| AKNo f -> error ("Field " ^ f ^ " cannot be accessed for reading") p
| AKExpr e | AKField (e,_) -> e
| AKSet _ -> assert false
| AKUsing (et,_,_,e) ->
(* build a closure with first parameter applied *)
(match follow et.etype with
| TFun (_ :: args,ret) ->
let tcallb = TFun (args,ret) in
let twrap = TFun ([("_e",false,e.etype)],tcallb) in
let args = List.map (fun (n,_,t) -> alloc_var n t) args in
let ve = alloc_var "_e" e.etype in
let ecall = make_call ctx et (List.map (fun v -> mk (TLocal v) v.v_type p) (ve :: args)) ret p in
let ecallb = mk (TFunction {
tf_args = List.map (fun v -> v,None) args;
tf_type = ret;
tf_expr = mk (TReturn (Some ecall)) t_dynamic p;
}) tcallb p in
let ewrap = mk (TFunction {
tf_args = [ve,None];
tf_type = tcallb;
tf_expr = mk (TReturn (Some ecallb)) t_dynamic p;
}) twrap p in
make_call ctx ewrap [e] tcallb p
| _ -> assert false)
| AKInline (e,f,t) ->
ignore(follow f.cf_type); (* force computing *)
(match f.cf_expr with
| None ->
if ctx.com.display then
mk (TClosure (e,f.cf_name)) t p
else
error "Recursive inline is not supported" p
| Some { eexpr = TFunction _ } ->
let chk_class c = if (c.cl_extern || has_meta ":extern" f.cf_meta) && not (has_meta ":runtime" f.cf_meta) then display_error ctx "Can't create closure on an inline extern method" p in
(match follow e.etype with
| TInst (c,_) -> chk_class c
| TAnon a -> (match !(a.a_status) with Statics c -> chk_class c | _ -> ())
| _ -> ());
mk (TClosure (e,f.cf_name)) t p
| Some e ->
let rec loop e = Type.map_expr loop { e with epos = p } in
loop e)
| AKMacro _ ->
assert false
let error_require r p =
let r = if r = "sys" then
"a system platform (php,neko,cpp,etc.)"
else try
if String.sub r 0 5 <> "flash" then raise Exit;
let _, v = ExtString.String.replace (String.sub r 5 (String.length r - 5)) "_" "." in
"flash version " ^ v ^ " (use -swf-version " ^ v ^ ")"
with _ ->
"'" ^ r ^ "' to be enabled"
in
error ("Accessing this field require " ^ r) p
let field_access ctx mode f t e p =
let fnormal() = AKField ((mk (TField (e,f.cf_name)) t p),f) in
let normal() =
match follow e.etype with
| TAnon a ->
(match !(a.a_status) with
| EnumStatics e ->
AKField ((mk (TEnumField (e,f.cf_name)) t p),f)
| _ -> fnormal())
| _ -> fnormal()
in
match f.cf_kind with
| Method m ->
if mode = MSet && m <> MethDynamic && not ctx.untyped then error "Cannot rebind this method : please use 'dynamic' before method declaration" p;
(match m, mode with
| MethInline, _ -> AKInline (e,f,t)
| MethMacro, MGet -> display_error ctx "Macro functions must be called immediatly" p; normal()
| MethMacro, MCall -> AKMacro (e,f)
| _ , MGet ->
AKExpr (mk (TClosure (e,f.cf_name)) t p)
| _ -> normal())
| Var v ->
match (match mode with MGet | MCall -> v.v_read | MSet -> v.v_write) with
| AccNo ->
(match follow e.etype with
| TInst (c,_) when is_parent c ctx.curclass -> normal()
| TAnon a ->
(match !(a.a_status) with
| Opened when mode = MSet ->
f.cf_kind <- Var { v with v_write = AccNormal };
normal()
| Statics c2 when ctx.curclass == c2 -> normal()
| _ -> if ctx.untyped then normal() else AKNo f.cf_name)
| _ ->
if ctx.untyped then normal() else AKNo f.cf_name)
| AccNormal ->
(*
if we are reading from a read-only variable on an anonymous object, it might actually be a method, so make sure to create a closure
*)
let is_maybe_method() =
match v.v_write, follow t, follow e.etype with
| (AccNo | AccNever), TFun _, TAnon a ->
(match !(a.a_status) with
| Statics _ | EnumStatics _ -> false
| _ -> true)
| _ -> false
in
if mode = MGet && is_maybe_method() then
AKExpr (mk (TClosure (e,f.cf_name)) t p)
else
normal()
| AccCall m ->
if m = ctx.curmethod && (match e.eexpr with TConst TThis -> true | TTypeExpr (TClassDecl c) when c == ctx.curclass -> true | _ -> false) then
let prefix = (match ctx.com.platform with Flash when Common.defined ctx.com "as3" -> "$" | _ -> "") in
AKExpr (mk (TField (e,prefix ^ f.cf_name)) t p)
else if mode = MSet then
AKSet (e,m,t,f.cf_name)
else
AKExpr (make_call ctx (mk (TField (e,m)) (tfun [] t) p) [] t p)
| AccResolve ->
let fstring = mk (TConst (TString f.cf_name)) ctx.t.tstring p in
let tresolve = tfun [ctx.t.tstring] t in
AKExpr (make_call ctx (mk (TField (e,"resolve")) tresolve p) [fstring] t p)
| AccNever ->
if ctx.untyped then normal() else AKNo f.cf_name
| AccInline ->
AKInline (e,f,t)
| AccRequire r ->
error_require r p
let using_field ctx mode e i p =
if mode = MSet then raise Not_found;
let rec loop = function
| [] ->
raise Not_found
| TEnumDecl _ :: l | TTypeDecl _ :: l ->
loop l
| TClassDecl c :: l ->
try
let f = PMap.find i c.cl_statics in
let t = field_type ctx c [] f p in
(match follow t with
| TFun ((_,_,t0) :: args,r) ->
let t0 = (try match t0 with
| TType({t_path=["haxe";"macro"], ("ExprOf"|"ExprRequire")}, [t]) ->
(try unify_raise ctx e.etype t p with Error (Unify _,_) -> raise Not_found); t;
| _ -> raise Not_found
with Not_found ->
(try unify_raise ctx e.etype t0 p with Error (Unify _,_) -> raise Not_found); t0) in
if follow e.etype == t_dynamic && follow t0 != t_dynamic then raise Not_found;
let et = type_module_type ctx (TClassDecl c) None p in
AKUsing (mk (TField (et,i)) t p,c,f,e)
| _ -> raise Not_found)
with Not_found ->
loop l
in
loop ctx.local_using
let get_this ctx p =
match ctx.curfun with
| FStatic ->
error "Cannot access this from a static function" p
| FMemberLocal ->
if ctx.untyped then display_error ctx "Cannot access this in 'untyped' mode : use either '__this__' or var 'me = this' (transitional)" p;
let v = (match ctx.vthis with
| None ->
let v = gen_local ctx ctx.tthis in
ctx.vthis <- Some v;
v
| Some v ->
ctx.locals <- PMap.add v.v_name v ctx.locals;
v
) in
mk (TLocal v) ctx.tthis p
| FConstructor | FMember ->
mk (TConst TThis) ctx.tthis p
let type_ident_raise ?(imported_enums=true) ctx i p mode =
match i with
| "true" ->
if mode = MGet then
AKExpr (mk (TConst (TBool true)) ctx.t.tbool p)
else
AKNo i
| "false" ->
if mode = MGet then
AKExpr (mk (TConst (TBool false)) ctx.t.tbool p)
else
AKNo i
| "this" ->
if mode = MGet then
AKExpr (get_this ctx p)
else
AKNo i
| "super" ->
let t = (match ctx.curclass.cl_super with
| None -> error "Current class does not have a superclass" p
| Some (c,params) -> TInst(c,params)
) in
(match ctx.curfun with
| FMember | FConstructor -> ()
| FStatic -> error "Cannot access super inside a static function" p;
| FMemberLocal -> error "Cannot access super inside a local function" p);
if mode = MSet || not ctx.in_super_call then
if mode = MGet && ctx.com.display then
AKExpr (mk (TConst TSuper) t p)
else
AKNo i
else begin
ctx.in_super_call <- false;
AKExpr (mk (TConst TSuper) t p)
end
| "null" ->
if mode = MGet then
AKExpr (null (mk_mono()) p)
else
AKNo i
| _ ->
try
let v = PMap.find i ctx.locals in
(match v.v_extra with
| Some (params,e) ->
let t = monomorphs params v.v_type in
(match e with
| Some ({ eexpr = TFunction f } as e) ->
(* create a fake class with a fake field to emulate inlining *)
let c = mk_class ctx.current (["local"],v.v_name) e.epos in
let cf = { (mk_field v.v_name v.v_type e.epos) with cf_params = params; cf_expr = Some e; cf_kind = Method MethInline } in
c.cl_extern <- true;
c.cl_fields <- PMap.add cf.cf_name cf PMap.empty;
AKInline (mk (TConst TNull) (TInst (c,[])) p, cf, t)
| _ ->
AKExpr (mk (TLocal v) t p))
| _ ->
AKExpr (mk (TLocal v) v.v_type p))
with Not_found -> try
(* member variable lookup *)
if ctx.curfun = FStatic then raise Not_found;
let t , f = class_field ctx ctx.curclass [] i p in
field_access ctx mode f t (get_this ctx p) p
with Not_found -> try
(* lookup using on 'this' *)
if ctx.curfun = FStatic then raise Not_found;
(match using_field ctx mode (mk (TConst TThis) ctx.tthis p) i p with
| AKUsing (et,c,f,_) -> AKUsing (et,c,f,get_this ctx p)
| _ -> assert false)
with Not_found -> try
(* static variable lookup *)
let f = PMap.find i ctx.curclass.cl_statics in
let e = type_type ctx ctx.curclass.cl_path p in
(* check_locals_masking already done in type_type *)
field_access ctx mode f (field_type ctx ctx.curclass [] f p) e p
with Not_found ->
if not imported_enums then raise Not_found;
(* lookup imported enums *)
let rec loop l =
match l with
| [] -> raise Not_found
| t :: l ->
match t with
| TClassDecl _ ->
loop l
| TTypeDecl t ->
(match follow t.t_type with
| TEnum (e,_) -> loop ((TEnumDecl e) :: l)
| _ -> loop l)
| TEnumDecl e ->
try
let ef = PMap.find i e.e_constrs in
mk (TEnumField (e,i)) (monomorphs e.e_types ef.ef_type) p
with
Not_found -> loop l
in
let e = loop ctx.local_types in
if mode = MSet then
AKNo i
else
AKExpr e
let rec type_field ctx e i p mode =
let no_field() =
if not ctx.untyped then display_error ctx (s_type (print_context()) e.etype ^ " has no field " ^ i) p;
AKExpr (mk (TField (e,i)) (mk_mono()) p)
in
match follow e.etype with
| TInst (c,params) ->
let rec loop_dyn c params =
match c.cl_dynamic with
| Some t ->
let t = apply_params c.cl_types params t in
if (mode = MGet || mode = MCall) && PMap.mem "resolve" c.cl_fields then
AKExpr (make_call ctx (mk (TField (e,"resolve")) (tfun [ctx.t.tstring] t) p) [Codegen.type_constant ctx.com (String i) p] t p)
else
AKExpr (mk (TField (e,i)) t p)
| None ->
match c.cl_super with
| None -> raise Not_found
| Some (c,params) -> loop_dyn c params
in
(try
let t , f = class_field ctx c params i p in
if e.eexpr = TConst TSuper && (match f.cf_kind with Var _ -> true | _ -> false) && Common.platform ctx.com Flash then error "Cannot access superclass variable for calling : needs to be a proper method" p;
if not (can_access ctx.curclass c f false) && not ctx.untyped then display_error ctx ("Cannot access to private field " ^ i) p;
field_access ctx mode f (apply_params c.cl_types params t) e p
with Not_found -> try
using_field ctx mode e i p
with Not_found -> try
loop_dyn c params
with Not_found ->
if PMap.mem i c.cl_statics then error ("Cannot access static field " ^ i ^ " from a class instance") p;
(*
This is a fix to deal with optimize_completion which will call iterator()
on the expression for/in, which vectors do no have.
*)
if ctx.com.display && i = "iterator" && c.cl_path = (["flash"],"Vector") then begin
let it = TAnon {
a_fields = PMap.add "next" (mk_field "next" (TFun([],List.hd params)) p) PMap.empty;
a_status = ref Closed;
} in
AKExpr (mk (TField (e,i)) (TFun([],it)) p)
end else
no_field())
| TDynamic t ->
(try
using_field ctx mode e i p
with Not_found ->
AKExpr (mk (TField (e,i)) t p))
| TAnon a ->
(try
let f = PMap.find i a.a_fields in
if not f.cf_public && not ctx.untyped then begin
match !(a.a_status) with
| Closed -> () (* always allow anon private fields access *)
| Statics c when is_parent c ctx.curclass -> ()
| _ -> display_error ctx ("Cannot access to private field " ^ i) p
end;
field_access ctx mode f (match !(a.a_status) with Statics c -> field_type ctx c [] f p | _ -> Type.field_type f) e p
with Not_found ->
if is_closed a then try
using_field ctx mode e i p
with Not_found ->
no_field()
else
let f = {
cf_name = i;
cf_type = mk_mono();
cf_doc = None;
cf_meta = no_meta;
cf_public = true;
cf_pos = p;
cf_kind = Var { v_read = AccNormal; v_write = (match mode with MSet -> AccNormal | MGet | MCall -> AccNo) };
cf_expr = None;
cf_params = [];
cf_overloads = [];
} in
a.a_fields <- PMap.add i f a.a_fields;
field_access ctx mode f (Type.field_type f) e p
)
| TMono r ->
if ctx.untyped && (match ctx.com.platform with Flash8 -> Common.defined ctx.com "swf-mark" | _ -> false) then ctx.com.warning "Mark" p;
let f = {
cf_name = i;
cf_type = mk_mono();
cf_doc = None;
cf_meta = no_meta;
cf_public = true;
cf_pos = p;
cf_kind = Var { v_read = AccNormal; v_write = (match mode with MSet -> AccNormal | MGet | MCall -> AccNo) };
cf_expr = None;
cf_params = [];
cf_overloads = [];
} in
let x = ref Opened in
let t = TAnon { a_fields = PMap.add i f PMap.empty; a_status = x } in
ctx.opened <- x :: ctx.opened;
r := Some t;
field_access ctx mode f (Type.field_type f) e p
| _ ->
try using_field ctx mode e i p with Not_found -> no_field()
let type_callback ctx e params p =
let e = type_expr ctx e true in
let args,ret = match follow e.etype with TFun(args, ret) -> args, ret | _ -> error "First parameter of callback is not a function" p in
let vexpr v = mk (TLocal v) v.v_type p in
let acount = ref 0 in
let alloc_name n =
if n = "" || String.length n > 2 then begin
incr acount;
"a" ^ string_of_int !acount;
end else
n
in
let rec loop args params given_args missing_args ordered_args = match args, params with
| [], [] -> given_args,missing_args,ordered_args
| [], _ -> error "Too many callback arguments" p
| (n,o,t) :: args , [] when o ->
let a = match ctx.com.platform with
| _ when is_pos_infos t ->
let infos = mk_infos ctx p [] in
ordered_args @ [type_expr ctx infos true]
| Neko | Php -> (ordered_args @ [(mk (TConst TNull) t_dynamic p)])
| _ -> ordered_args
in
loop args [] given_args missing_args a
| (n,o,t) :: _ , (EConst(Ident "_"),p) :: _ when ctx.com.platform = Flash && o && not (is_nullable t) ->
error "Usage of _ is currently not supported for optional non-nullable arguments on flash9" p
| (n,o,t) :: args , ([] as params)
| (n,o,t) :: args , (EConst(Ident "_"),_) :: params ->
let v = alloc_var (alloc_name n) (if o then ctx.t.tnull t else t) in
loop args params given_args (missing_args @ [v,o]) (ordered_args @ [vexpr v])
| (n,o,t) :: args , param :: params ->
let e = type_expr ctx param true in
unify ctx e.etype t p;
let v = alloc_var (alloc_name n) t in
loop args params (given_args @ [v,o,Some e]) missing_args (ordered_args @ [vexpr v])
in
let given_args,missing_args,ordered_args = loop args params [] [] [] in
let loc = alloc_var "f" e.etype in
let given_args = (loc,false,Some e) :: given_args in
let inner_fun_args l = List.map (fun (v,o) -> v.v_name, o, v.v_type) l in
let t_inner = TFun(inner_fun_args missing_args, ret) in
let call = make_call ctx (vexpr loc) ordered_args ret p in
let func = mk (TFunction {
tf_args = List.map (fun (v,o) -> v, if o then Some TNull else None) missing_args;
tf_type = ret;
tf_expr = mk (TReturn (Some call)) ret p;
}) t_inner p in
let outer_fun_args l = List.map (fun (v,o,_) -> v.v_name, o, v.v_type) l in
let func = mk (TFunction {
tf_args = List.map (fun (v,_,_) -> v,None) given_args;
tf_type = t_inner;
tf_expr = mk (TReturn (Some func)) t_inner p;
}) (TFun(outer_fun_args given_args, t_inner)) p in
make_call ctx func (List.map (fun (_,_,e) -> (match e with Some e -> e | None -> assert false)) given_args) t_inner p
(*
We want to try unifying as an integer and apply side effects.
However, in case the value is not a normal Monomorph but one issued
from a Dynamic relaxation, we will instead unify with float since
we don't want to accidentaly truncate the value
*)
let unify_int ctx e k =
let is_dynamic t =
match follow t with
| TDynamic _ -> true
| _ -> false
in
let is_dynamic_array t =
match follow t with
| TInst (_,[p]) -> is_dynamic p
| _ -> true
in
let is_dynamic_field t f =
match follow t with
| TAnon a ->
(try is_dynamic (PMap.find f a.a_fields).cf_type with Not_found -> false)
| TInst (c,pl) ->
(try is_dynamic (apply_params c.cl_types pl (fst (Type.class_field c f))) with Not_found -> false)
| _ ->
true
in
let is_dynamic_return t =
match follow t with
| TFun (_,r) -> is_dynamic r
| _ -> true
in
(*
This is some quick analysis that matches the most common cases of dynamic-to-mono convertions
*)
let rec maybe_dynamic_mono e =
match e.eexpr with
| TLocal _ -> is_dynamic e.etype
| TArray({ etype = t } as e,_) -> is_dynamic_array t || maybe_dynamic_rec e t
| TField({ etype = t } as e,f) -> is_dynamic_field t f || maybe_dynamic_rec e t
| TCall({ etype = t } as e,_) -> is_dynamic_return t || maybe_dynamic_rec e t
| TParenthesis e -> maybe_dynamic_mono e
| TIf (_,a,Some b) -> maybe_dynamic_mono a || maybe_dynamic_mono b
| _ -> false
and maybe_dynamic_rec e t =
match follow t with
| TMono _ | TDynamic _ -> maybe_dynamic_mono e
(* we might have inferenced a tmono into a single field *)
| TAnon a when !(a.a_status) = Opened -> maybe_dynamic_mono e
| _ -> false
in
match k with
| KUnk | KDyn when maybe_dynamic_mono e ->
unify ctx e.etype ctx.t.tfloat e.epos;
false
| _ ->
unify ctx e.etype ctx.t.tint e.epos;
true
let rec type_binop ctx op e1 e2 p =
match op with
| OpAssign ->
let e1 = type_access ctx (fst e1) (snd e1) MSet in
let e2 = type_expr_with_type ctx e2 (match e1 with AKNo _ | AKInline _ | AKUsing _ | AKMacro _ -> None | AKExpr e | AKField (e,_) | AKSet(e,_,_,_) -> Some e.etype) in
(match e1 with
| AKNo s -> error ("Cannot access field or identifier " ^ s ^ " for writing") p
| AKExpr e1 | AKField (e1,_) ->
unify ctx e2.etype e1.etype p;
check_assign ctx e1;
(match e1.eexpr , e2.eexpr with
| TLocal i1 , TLocal i2 when i1 == i2 -> error "Assigning a value to itself" p
| TField ({ eexpr = TConst TThis },i1) , TField ({ eexpr = TConst TThis },i2) when i1 = i2 ->
error "Assigning a value to itself" p
| _ , _ -> ());
mk (TBinop (op,e1,e2)) e1.etype p
| AKSet (e,m,t,_) ->
unify ctx e2.etype t p;
make_call ctx (mk (TField (e,m)) (tfun [t] t) p) [e2] t p
| AKInline _ | AKUsing _ | AKMacro _ ->
assert false)
| OpAssignOp op ->
(match type_access ctx (fst e1) (snd e1) MSet with
| AKNo s -> error ("Cannot access field or identifier " ^ s ^ " for writing") p
| AKExpr e | AKField (e,_) ->
let eop = type_binop ctx op e1 e2 p in
(match eop.eexpr with
| TBinop (_,_,e2) ->
unify ctx eop.etype e.etype p;
check_assign ctx e;
mk (TBinop (OpAssignOp op,e,e2)) e.etype p;
| _ ->
assert false)
| AKSet (e,m,t,f) ->
let l = save_locals ctx in
let v = gen_local ctx e.etype in
let ev = mk (TLocal v) e.etype p in
let get = type_binop ctx op (EField ((EConst (Ident v.v_name),p),f),p) e2 p in
unify ctx get.etype t p;
l();
mk (TBlock [
mk (TVars [v,Some e]) ctx.t.tvoid p;
make_call ctx (mk (TField (ev,m)) (tfun [t] t) p) [get] t p
]) t p
| AKInline _ | AKUsing _ | AKMacro _ ->
assert false)
| _ ->
let e1 = type_expr ctx e1 in
let e2 = type_expr ctx e2 in