forked from HaxeFoundation/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyper.ml
1890 lines (1843 loc) · 56.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 option * t) list option)
| CExpr of texpr
type access_mode =
| MGet
| MSet
| MCall
exception Display of t
type access_kind =
| AccNo of string
| AccExpr of texpr
| AccSet of texpr * string * t * string
| AccInline of texpr * tclass_field * t
| AccUsing of texpr * texpr
let mk_infos ctx p params =
(EObjectDecl (
("fileName" , (EConst (String (Filename.basename p.pfile)) , 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_locals_masking ctx e =
let path = (match e.eexpr with
| TEnumField (e,_)
| TTypeExpr (TEnumDecl e) ->
Some e.e_path
| TTypeExpr (TClassDecl c) ->
Some c.cl_path
| _ -> None
) in
match path with
| Some ([],name) | Some (name::_,_) when PMap.mem name ctx.locals ->
error ("Local variable " ^ name ^ " is preventing usage of this type here") e.epos;
| _ -> ()
let check_assign ctx e =
match e.eexpr with
| TLocal _ | TArray _ | TField _ ->
()
| TTypeExpr _ when ctx.untyped ->
()
| _ ->
error "Invalid assign" e.epos
type type_class =
| KInt
| KFloat
| KString
| KUnk
| KDyn
| KOther
| KParam of t
let 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; cl_implements = [{ cl_path = ([],"Float")},[]] },[]) -> KParam t
| TInst ({ cl_kind = KTypeParameter; cl_implements = [{ cl_path = ([],"Int")},[]] },[]) -> KParam t
| TMono r when !r = None -> KUnk
| TDynamic _ -> KDyn
| _ -> KOther
let type_field_rec = ref (fun _ _ _ _ _ -> assert false)
(* ---------------------------------------------------------------------- *)
(* PASS 3 : type expression & check structure *)
let type_expr_with_type ctx e t =
match e with
| (EFunction _,_) ->
let old = ctx.param_type in
(try
ctx.param_type <- t;
let e = type_expr ctx e true in
ctx.param_type <- old;
e
with
exc ->
ctx.param_type <- old;
raise exc)
| _ ->
type_expr ctx e true
let unify_call_params ctx name el args p inline =
let error txt =
let format_arg = (fun (name,opt,_) -> (if opt then "?" else "") ^ name) in
let argstr = "Function " ^ (match name with None -> "" | Some n -> "'" ^ n ^ "' ") ^ "requires " ^ (if args = [] then "no arguments" else "arguments : " ^ String.concat ", " (List.map format_arg args)) in
display_error ctx (txt ^ " arguments\n" ^ argstr) p
in
let arg_error ul name opt p =
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 =
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
in
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 t p, true)
in
let rec loop acc l l2 skip =
match l , l2 with
| [] , [] ->
if not (inline && ctx.doinline) && (Common.defined ctx.com "flash" || Common.defined ctx.com "js") then
List.rev (no_opt acc)
else
List.rev (List.map fst acc)
| [] , (_,false,_) :: _ ->
error "Not enough";
[]
| [] , (name,true,t) :: l ->
loop (default_value t :: acc) [] l skip
| _ , [] ->
(match List.rev skip with
| [] -> error "Too many"
| [name,ul] -> arg_error ul name true p
| _ -> error "Invalid");
[]
| ee :: l, (name,opt,t) :: l2 ->
let e = type_expr_with_type ctx ee (Some t) in
try
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 e.epos
in
loop [] el args []
let type_local ctx i p =
(* local lookup *)
let t = PMap.find i ctx.locals in
let i = (try PMap.find i ctx.locals_map with Not_found -> i) in
mk (TLocal i) t p
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_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 = [];
} in
let e = mk (TTypeExpr (TClassDecl c)) (TType (t_tmp,[])) p in
check_locals_masking ctx e;
e
| 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_get = NormalAccess;
cf_set = (match follow f.ef_type with TFun _ -> MethodAccess false | _ -> NoAccess);
cf_doc = None;
cf_meta = [];
cf_expr = None;
cf_params = [];
} acc
) e.e_constrs PMap.empty in
let t_tmp = {
t_path = fst e.e_path, "#" ^ snd e.e_path;
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 = [];
} in
let e = mk (TTypeExpr (TEnumDecl e)) (TType (t_tmp,types)) p in
check_locals_masking ctx e;
e
| 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 c p =
let rec loop c =
match c.cl_constructor with
| Some f -> f
| None ->
if not c.cl_extern then raise Not_found;
match c.cl_super with
| None -> raise Not_found
| Some (csup,[]) -> loop csup
| Some (_,_) -> error (s_type_path c.cl_path ^ " must define its own constructor") p
in
try
loop c
with Not_found ->
error (s_type_path c.cl_path ^ " does not have a constructor") p
let make_call ctx e params t p =
try
if not ctx.doinline then raise Exit;
let ethis, fname = (match e.eexpr with TField (ethis,fname) -> ethis, fname | _ -> raise Exit) in
let f = (match follow ethis.etype with
| TInst (c,params) -> snd (try class_field c fname with Not_found -> raise Exit)
| TAnon a -> (try PMap.find fname a.a_fields with Not_found -> raise Exit)
| _ -> raise Exit
) in
if f.cf_get <> InlineAccess then raise Exit;
ignore(follow f.cf_type); (* force evaluation *)
(match f.cf_expr with
| Some { eexpr = TFunction fd } ->
(match Optimizer.type_inline ctx f fd ethis params t p with
| None -> 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
| AccNo f -> error ("Field " ^ f ^ " cannot be accessed for reading") p
| AccExpr e -> e
| AccSet _ -> assert false
| AccUsing (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 ecall = make_call ctx et (List.map (fun (n,_,t) -> mk (TLocal n) t p) (("_e",false,e.etype) :: args)) ret p in
let ecallb = mk (TFunction {
tf_args = List.map (fun (n,_,t) -> n,None,t) args;
tf_type = ret;
tf_expr = mk (TReturn (Some ecall)) t_dynamic p;
}) tcallb p in
let ewrap = mk (TFunction {
tf_args = [("_e",None,e.etype)];
tf_type = tcallb;
tf_expr = mk (TReturn (Some ecallb)) t_dynamic p;
}) twrap p in
make_call ctx ewrap [e] tcallb p
| _ -> assert false)
| AccInline (e,f,t) ->
ignore(follow f.cf_type); (* force computing *)
match f.cf_expr with
| None -> error "Recursive inline is not supported" p
| Some { eexpr = TFunction _ } -> 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
let field_access ctx mode f t e p =
let normal() = AccExpr (mk (TField (e,f.cf_name)) t p) in
match (match mode with MGet | MCall -> f.cf_get | MSet -> f.cf_set) with
| NoAccess ->
(match follow e.etype with
| TInst (c,_) when is_parent c ctx.curclass -> normal()
| TAnon a ->
(match !(a.a_status) with
| Statics c2 when ctx.curclass == c2 -> normal()
| _ -> if ctx.untyped then normal() else AccNo f.cf_name)
| _ ->
if ctx.untyped then normal() else AccNo f.cf_name)
| MethodAccess false when not ctx.untyped ->
error "Cannot rebind this method : please use 'dynamic' before method declaration" p
| NormalAccess | MethodAccess _ ->
(*
creates a closure if we're reading a normal method
or a read-only variable (which could be a method)
*)
(match mode, f.cf_set with
| MGet, MethodAccess _ -> AccExpr (mk (TClosure (e,f.cf_name)) t p)
| MGet, NoAccess | MGet, NeverAccess when (match follow t with TFun _ -> true | _ -> false) -> AccExpr (mk (TClosure (e,f.cf_name)) t p)
| _ ->
match follow e.etype with
| TAnon a -> (match !(a.a_status) with EnumStatics e -> AccExpr (mk (TEnumField (e,f.cf_name)) t p) | _ -> normal())
| _ -> normal())
| CallAccess m ->
if m = ctx.curmethod && (match e.eexpr with TConst TThis -> true | TTypeExpr (TClassDecl c) when c == ctx.curclass -> true | _ -> false) then
let prefix = if Common.defined ctx.com "as3" then "$" else "" in
AccExpr (mk (TField (e,prefix ^ f.cf_name)) t p)
else if mode = MSet then
AccSet (e,m,t,f.cf_name)
else
AccExpr (make_call ctx (mk (TField (e,m)) (tfun [] t) p) [] t p)
| ResolveAccess ->
let fstring = mk (TConst (TString f.cf_name)) ctx.api.tstring p in
let tresolve = tfun [ctx.api.tstring] t in
AccExpr (make_call ctx (mk (TField (e,"resolve")) tresolve p) [fstring] t p)
| NeverAccess ->
AccNo f.cf_name
| InlineAccess ->
AccInline (e,f,t)
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 f in
(match follow t with
| TFun ((_,_,t0) :: args,r) ->
(try unify_raise ctx e.etype t0 p with Error (Unify _,_) -> raise Not_found);
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
AccUsing (mk (TField (et,i)) t p,e)
| _ -> raise Not_found)
with Not_found ->
loop l
in
loop ctx.local_using
let type_ident ctx i is_type p mode =
match i with
| "true" ->
if mode = MGet then
AccExpr (mk (TConst (TBool true)) ctx.api.tbool p)
else
AccNo i
| "false" ->
if mode = MGet then
AccExpr (mk (TConst (TBool false)) ctx.api.tbool p)
else
AccNo i
| "this" ->
if not ctx.untyped && ctx.in_static then error "Cannot access this from a static function" p;
if mode = MGet then
AccExpr (mk (TConst TThis) ctx.tthis p)
else
AccNo 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
if ctx.in_static then error "Cannot access super from a static function" p;
if mode = MSet || not ctx.super_call then
AccNo i
else begin
ctx.super_call <- false;
AccExpr (mk (TConst TSuper) t p)
end
| "null" ->
if mode = MGet then
AccExpr (null (mk_mono()) p)
else
AccNo i
| "here" ->
let infos = mk_infos ctx p [] in
let e = type_expr ctx infos true in
if mode = MGet then
AccExpr { e with etype = Typeload.load_instance ctx { tpackage = ["haxe"]; tname = "PosInfos"; tparams = []; tsub = None } p false }
else
AccNo i
| _ ->
try
let e = type_local ctx i p in
AccExpr e
with Not_found -> try
(* member variable lookup *)
if ctx.in_static then raise Not_found;
let t , f = class_field ctx.curclass i in
field_access ctx mode f t (mk (TConst TThis) ctx.tthis p) p
with Not_found -> try
using_field ctx mode (mk (TConst TThis) ctx.tthis p) i p
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
field_access ctx mode f (field_type f) e p
with Not_found -> try
(* lookup imported *)
let rec loop l =
match l with
| [] -> raise Not_found
| t :: l ->
match t with
| TClassDecl _ | TTypeDecl _ ->
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
check_locals_masking ctx e;
if mode = MSet then
AccNo i
else
AccExpr e
with Not_found -> try
(* lookup type *)
if not is_type then raise Not_found;
let e = (try type_type ctx ([],i) p with Error (Module_not_found ([],name),_) when name = i -> raise Not_found) in
AccExpr e
with Not_found ->
if ctx.untyped then
AccExpr (mk (TLocal i) (mk_mono()) p)
else begin
if ctx.in_static && PMap.mem i ctx.curclass.cl_fields then error ("Cannot access " ^ i ^ " in static function") p;
raise (Error (Unknown_ident i,p))
end
let type_matching ctx (enum,params) (e,p) ecases first_case =
let invalid() = raise (Error (Invalid_enum_matching,p)) in
let needs n = error ("This constructor needs " ^ string_of_int n ^ " parameters") p in
let constr name =
if PMap.mem name (!ecases) then error "This constructor has already been used" p;
ecases := PMap.add name () (!ecases);
try
PMap.find name enum.e_constrs
with
Not_found -> error ("This constructor is not part of the enum " ^ s_type_path enum.e_path) p
in
match e with
| EConst (Ident name) | EConst (Type name) ->
let c = constr name in
(match c.ef_type with
| TFun (l,_) -> needs (List.length l)
| TEnum _ -> ()
| _ -> assert false
);
(c,None)
| ECall ((EConst (Ident name),_),el)
| ECall ((EConst (Type name),_),el) ->
let c = constr name in
let args = (match c.ef_type with
| TFun (l,_) ->
if List.length l <> List.length el then needs (List.length l);
List.map (fun (_,_,t) -> apply_params enum.e_types params t) l
| TEnum _ -> error "This constructor does not take any parameter" p
| _ -> assert false
) in
let idents = List.map2 (fun (e,_) t ->
match e with
| EConst (Ident "_") ->
None , t
| EConst (Ident name) | EConst (Type name) ->
let name = (if first_case then add_local ctx name t else try PMap.find name ctx.locals_map with Not_found -> name) in
Some name , t
| _ -> invalid()
) el args in
(c,Some idents)
| _ ->
invalid()
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;
AccExpr (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 && PMap.mem "resolve" c.cl_fields then
AccExpr (make_call ctx (mk (TField (e,"resolve")) (tfun [ctx.api.tstring] t) p) [Typeload.type_constant ctx (String i) p] t p)
else
AccExpr (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 c i in
if e.eexpr = TConst TSuper && f.cf_set = NormalAccess && Common.platform ctx.com Flash9 then error "Cannot access superclass variable for calling : needs to be a proper method" p;
if not f.cf_public && not (is_parent c ctx.curclass) && 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;
no_field())
| TDynamic t ->
AccExpr (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 (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 = [];
cf_public = true;
cf_get = NormalAccess;
cf_set = (match mode with MSet -> NormalAccess | MGet | MCall -> NoAccess);
cf_expr = None;
cf_params = [];
} in
a.a_fields <- PMap.add i f a.a_fields;
field_access ctx mode f (field_type f) e p
)
| TMono r ->
if ctx.untyped && Common.defined ctx.com "swf-mark" && Common.defined ctx.com "flash" then ctx.com.warning "Mark" p;
let f = {
cf_name = i;
cf_type = mk_mono();
cf_doc = None;
cf_meta = [];
cf_public = true;
cf_get = NormalAccess;
cf_set = (match mode with MSet -> NormalAccess | MGet | MCall -> NoAccess);
cf_expr = None;
cf_params = [];
} 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 (field_type f) e p
| _ ->
try using_field ctx mode e i p with Not_found -> no_field()
(*
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 -> true)
| _ -> true
in
let is_dynamic_return t =
match follow t with
| TFun (_,r) -> is_dynamic r
| _ -> true
in
let maybe_dynamic_mono() =
match e.eexpr with
| TLocal _ when not (is_dynamic e.etype) -> false
| TArray({ etype = t },_) when not (is_dynamic_array t) -> false
| TField({ etype = t },f) when not (is_dynamic_field t f) -> false
| TCall({ etype = t },_) when not (is_dynamic_return t) -> false
| _ -> true
in
match k with
| KUnk | KDyn when maybe_dynamic_mono() ->
unify ctx e.etype ctx.api.tfloat e.epos;
false
| _ ->
unify ctx e.etype ctx.api.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 AccNo _ | AccInline _ | AccUsing _ -> None | AccExpr e | AccSet(e,_,_,_) -> Some e.etype) in
(match e1 with
| AccNo s -> error ("Cannot access field or identifier " ^ s ^ " for writing") p
| AccExpr e1 ->
unify ctx e2.etype e1.etype p;
check_assign ctx e1;
(match e1.eexpr , e2.eexpr with
| TLocal i1 , TLocal i2
| 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
| AccSet (e,m,t,_) ->
unify ctx e2.etype t p;
make_call ctx (mk (TField (e,m)) (tfun [t] t) p) [e2] t p
| AccInline _ | AccUsing _ ->
assert false)
| OpAssignOp op ->
(match type_access ctx (fst e1) (snd e1) MSet with
| AccNo s -> error ("Cannot access field or identifier " ^ s ^ " for writing") p
| AccExpr 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)
| AccSet (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),p),f),p) e2 p in
unify ctx get.etype t p;
l();
mk (TBlock [
mk (TVars [v,e.etype,Some e]) ctx.api.tvoid p;
make_call ctx (mk (TField (ev,m)) (tfun [t] t) p) [get] t p
]) t p
| AccInline _ | AccUsing _ ->
assert false)
| _ ->
let e1 = type_expr ctx e1 in
let e2 = type_expr ctx e2 in
let mk_op t = mk (TBinop (op,e1,e2)) t p in
match op with
| OpAdd ->
mk_op (match classify e1.etype, classify e2.etype with
| KInt , KInt ->
ctx.api.tint
| KFloat , KInt
| KInt, KFloat
| KFloat, KFloat ->
ctx.api.tfloat
| KUnk , KInt ->
if unify_int ctx e1 KUnk then ctx.api.tint else ctx.api.tfloat
| KUnk , KFloat
| KUnk , KString ->
unify ctx e1.etype e2.etype e1.epos;
e1.etype
| KInt , KUnk ->
if unify_int ctx e2 KUnk then ctx.api.tint else ctx.api.tfloat
| KFloat , KUnk
| KString , KUnk ->
unify ctx e2.etype e1.etype e2.epos;
e2.etype
| _ , KString
| _ , KDyn ->
e2.etype
| KString , _
| KDyn , _ ->
e1.etype
| KUnk , KUnk ->
let ok1 = unify_int ctx e1 KUnk in
let ok2 = unify_int ctx e2 KUnk in
if ok1 && ok2 then ctx.api.tint else ctx.api.tfloat
| KParam t1, KParam t2 when t1 == t2 ->
t1
| KParam t, KInt | KInt, KParam t ->
t
| KParam _, KFloat | KFloat, KParam _ | KParam _, KParam _ ->
ctx.api.tfloat
| KParam _, _
| _, KParam _
| KOther, _
| _ , KOther ->
let pr = print_context() in
error ("Cannot add " ^ s_type pr e1.etype ^ " and " ^ s_type pr e2.etype) p
)
| OpAnd
| OpOr
| OpXor
| OpShl
| OpShr
| OpUShr ->
let i = ctx.api.tint in
unify ctx e1.etype i e1.epos;
unify ctx e2.etype i e2.epos;
mk_op i
| OpMod
| OpMult
| OpDiv
| OpSub ->
let result = ref (if op = OpDiv then ctx.api.tfloat else ctx.api.tint) in
(match classify e1.etype, classify e2.etype with
| KFloat, KFloat ->
result := ctx.api.tfloat
| KParam t1, KParam t2 when t1 == t2 ->
if op <> OpDiv then result := t1
| KParam _, KParam _ ->
result := ctx.api.tfloat
| KParam t, KInt | KInt, KParam t ->
if op <> OpDiv then result := t
| KParam _, KFloat | KFloat, KParam _ ->
result := ctx.api.tfloat
| KFloat, k ->
ignore(unify_int ctx e2 k);
result := ctx.api.tfloat
| k, KFloat ->
ignore(unify_int ctx e1 k);
result := ctx.api.tfloat
| k1 , k2 ->
let ok1 = unify_int ctx e1 k1 in
let ok2 = unify_int ctx e2 k2 in
if not ok1 || not ok2 then result := ctx.api.tfloat;
);
mk_op !result
| OpEq
| OpNotEq ->
(try
unify_raise ctx e1.etype e2.etype p
with
Error (Unify _,_) -> unify ctx e2.etype e1.etype p);
mk_op ctx.api.tbool
| OpGt
| OpGte
| OpLt
| OpLte ->
(match classify e1.etype, classify e2.etype with
| KInt , KInt | KInt , KFloat | KFloat , KInt | KFloat , KFloat | KString , KString -> ()
| KInt , KUnk -> ignore(unify_int ctx e2 KUnk)
| KFloat , KUnk | KString , KUnk -> unify ctx e2.etype e1.etype e2.epos
| KUnk , KInt -> ignore(unify_int ctx e1 KUnk)
| KUnk , KFloat | KUnk , KString -> unify ctx e1.etype e2.etype e1.epos
| KUnk , KUnk ->
ignore(unify_int ctx e1 KUnk);
ignore(unify_int ctx e2 KUnk);
| KDyn , KInt | KDyn , KFloat | KDyn , KString -> ()
| KInt , KDyn | KFloat , KDyn | KString , KDyn -> ()
| KDyn , KDyn -> ()
| KParam _ , x | x , KParam _ when x <> KString && x <> KOther -> ()
| KDyn , KUnk
| KUnk , KDyn
| KString , KInt
| KString , KFloat
| KInt , KString
| KFloat , KString
| KParam _ , _
| _ , KParam _
| KOther , _
| _ , KOther ->
let pr = print_context() in
error ("Cannot compare " ^ s_type pr e1.etype ^ " and " ^ s_type pr e2.etype) p
);
mk_op ctx.api.tbool
| OpBoolAnd
| OpBoolOr ->
let b = ctx.api.tbool in
unify ctx e1.etype b p;
unify ctx e2.etype b p;
mk_op b
| OpInterval ->
let i = ctx.api.tint in
let t = Typeload.load_core_type ctx "IntIter" in
unify ctx e1.etype i e1.epos;
unify ctx e2.etype i e2.epos;
mk (TNew ((match t with TInst (c,[]) -> c | _ -> assert false),[],[e1;e2])) t p
| OpAssign
| OpAssignOp _ ->
assert false
and type_unop ctx op flag e p =
let set = (op = Increment || op = Decrement) in
let acc = type_access ctx (fst e) (snd e) (if set then MSet else MGet) in
let access e =
let t = (match op with
| Not ->
unify ctx e.etype ctx.api.tbool e.epos;
ctx.api.tbool
| Increment
| Decrement
| Neg
| NegBits ->
if set then check_assign ctx e;
(match classify e.etype with
| KFloat -> ctx.api.tfloat
| KParam t ->
unify ctx e.etype ctx.api.tfloat e.epos;
t
| k ->
if unify_int ctx e k then ctx.api.tint else ctx.api.tfloat)
) in
match op, e.eexpr with
| Neg , TConst (TInt i) -> mk (TConst (TInt (Int32.neg i))) t p
| _ -> mk (TUnop (op,flag,e)) t p
in
match acc with
| AccExpr e -> access e
| AccInline _ | AccUsing _ when not set -> access (acc_get ctx acc p)
| AccNo s ->
error ("The field or identifier " ^ s ^ " is not accessible for " ^ (if set then "writing" else "reading")) p
| AccInline _ | AccUsing _ ->
error "This kind of operation is not supported" p
| AccSet (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 op = (match op with Increment -> OpAdd | Decrement -> OpSub | _ -> assert false) in
let one = (EConst (Int "1"),p) in
let eget = (EField ((EConst (Ident v),p),f),p) in
match flag with
| Prefix ->
let get = type_binop ctx op eget one p in
unify ctx get.etype t p;
l();
mk (TBlock [
mk (TVars [v,e.etype,Some e]) ctx.api.tvoid p;
make_call ctx (mk (TField (ev,m)) (tfun [t] t) p) [get] t p
]) t p
| Postfix ->
let v2 = gen_local ctx t in
let ev2 = mk (TLocal v2) t p in
let get = type_expr ctx eget in
let plusone = type_binop ctx op (EConst (Ident v2),p) one p in
unify ctx get.etype t p;
l();
mk (TBlock [
mk (TVars [v,e.etype,Some e; v2,t,Some get]) ctx.api.tvoid p;
make_call ctx (mk (TField (ev,m)) (tfun [plusone.etype] t) p) [plusone] t p;
ev2
]) t p
and type_switch ctx e cases def need_val p =
let e = type_expr ctx e in
let t = ref (if need_val then mk_mono() else ctx.api.tvoid) in
let rec lookup_enum l =
match l with
| [] -> None
| (ECall ((EConst (Type name),p),_),_) :: l
| (ECall ((EConst (Ident name),p),_),_) :: l
| (EConst (Ident name),p) :: l
| (EConst (Type name),p) :: l ->
(try
let e = acc_get ctx (type_ident ctx name false p MGet) p in
(match e.eexpr with
| TEnumField (e,_) -> Some (e, List.map (fun _ -> mk_mono()) e.e_types)
| _ -> None)
with
Error (Unknown_ident _,_) -> lookup_enum l)
| _ ->
None
in
let enum = ref (match follow e.etype with
| TEnum ({ e_path = [],"Bool" },_)
| TEnum ({ e_path = ["flash"],_ ; e_extern = true },_) ->
None
| TEnum (e,params) -> Some (e,params)
| TMono r ->
(match lookup_enum (List.concat (List.map fst cases)) with
| None -> None
| Some (en,params) as k ->
r := Some (TEnum (en,params));
k)
| _ -> None
) in
let unify_val e =
if need_val then begin
try
(match e.eexpr with
| TBlock [{ eexpr = TConst TNull }] -> t := ctx.api.tnull !t;
| _ -> ());
unify_raise ctx e.etype (!t) e.epos;
if is_null e.etype then t := ctx.api.tnull !t;
with Error (Unify _,_) -> try
unify_raise ctx (!t) e.etype e.epos;
t := if is_null !t then ctx.api.tnull e.etype else e.etype;
with Error (Unify _,_) ->
(* will display the error *)
unify ctx e.etype (!t) e.epos;
end;
in
let first = ref true in
let ecases = ref PMap.empty in
let type_case e e1 =
let e1 = type_expr ctx e1 in
(* this inversion is needed *)
unify ctx e.etype e1.etype e1.epos;
CExpr e1
in
let cases = List.map (fun (el,e2) ->
let locals = save_locals ctx in
let first_case = ref true in
let el = List.map (fun e1 ->
let v = (match !enum with
| Some en ->
(try
CMatch (type_matching ctx en e1 ecases !first_case)
with
Error (Invalid_enum_matching,_) when !first ->
enum := None;
type_case e e1)
| None ->
type_case e e1
) in
first_case := false;
first := false;
v
) el in
if el = [] then error "Case must match at least one expression" (pos e2);
let e2 = (match fst e2 with
| EBlock [] -> mk (TConst TNull) ctx.api.tvoid (pos e2)
| _ -> type_expr ctx ~need_val e2
) in
locals();
unify_val e2;
(el,e2)
) cases in
let def = (match def with
| None ->
(match !enum with
| None -> ()
| Some (e,_) ->
let l = PMap.fold (fun c acc ->
if PMap.mem c.ef_name (!ecases) then acc else c.ef_name :: acc
) e.e_constrs [] in
match l with
| [] -> ()
| _ -> display_error ctx ("Some constructors are not matched : " ^ String.concat "," l) p
);
if need_val then Some (null (mk_mono()) p) else None