forked from paulfitz/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeload.ml
3014 lines (2939 loc) · 111 KB
/
typeload.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright (C)2005-2013 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*)
open Ast
open Type
open Common
open Typecore
(*
Build module structure : should be atomic - no type loading is possible
*)
let make_module ctx mpath file tdecls loadp =
let decls = ref [] in
let make_path name priv =
if List.exists (fun (t,_) -> snd (t_path t) = name) !decls then error ("Type name " ^ name ^ " is already defined in this module") loadp;
if priv then (fst mpath @ ["_" ^ snd mpath], name) else (fst mpath, name)
in
let m = {
m_id = alloc_mid();
m_path = mpath;
m_types = [];
m_extra = module_extra (Common.unique_full_path file) (Common.get_signature ctx.com) (file_time file) (if ctx.in_macro then MMacro else MCode);
} in
let pt = ref None in
let rec make_decl acc decl =
let p = snd decl in
let acc = (match fst decl with
| EImport _ | EUsing _ ->
(match !pt with
| None -> acc
| Some pt ->
display_error ctx "import and using may not appear after a type declaration" p;
error "Previous type declaration found here" pt)
| EClass d ->
pt := Some p;
let priv = List.mem HPrivate d.d_flags in
let path = make_path d.d_name priv in
let c = mk_class m path p in
c.cl_module <- m;
c.cl_private <- priv;
c.cl_doc <- d.d_doc;
c.cl_meta <- d.d_meta;
decls := (TClassDecl c, decl) :: !decls;
acc
| EEnum d ->
pt := Some p;
let priv = List.mem EPrivate d.d_flags in
let path = make_path d.d_name priv in
let e = {
e_path = path;
e_module = m;
e_pos = p;
e_doc = d.d_doc;
e_meta = d.d_meta;
e_params = [];
e_private = priv;
e_extern = List.mem EExtern d.d_flags;
e_constrs = PMap.empty;
e_names = [];
e_type = {
t_path = [], "Enum<" ^ (s_type_path path) ^ ">";
t_module = m;
t_doc = None;
t_pos = p;
t_type = mk_mono();
t_private = true;
t_params = [];
t_meta = [];
};
} in
decls := (TEnumDecl e, decl) :: !decls;
acc
| ETypedef d ->
pt := Some p;
let priv = List.mem EPrivate d.d_flags in
let path = make_path d.d_name priv in
let t = {
t_path = path;
t_module = m;
t_pos = p;
t_doc = d.d_doc;
t_private = priv;
t_params = [];
t_type = mk_mono();
t_meta = d.d_meta;
} in
decls := (TTypeDecl t, decl) :: !decls;
acc
| EAbstract d ->
let priv = List.mem APrivAbstract d.d_flags in
let path = make_path d.d_name priv in
let a = {
a_path = path;
a_private = priv;
a_module = m;
a_pos = p;
a_doc = d.d_doc;
a_params = [];
a_meta = d.d_meta;
a_from = [];
a_to = [];
a_from_field = [];
a_to_field = [];
a_ops = [];
a_unops = [];
a_impl = None;
a_array = [];
a_this = mk_mono();
} in
decls := (TAbstractDecl a, decl) :: !decls;
match d.d_data with
| [] when Meta.has Meta.CoreType a.a_meta ->
a.a_this <- t_dynamic;
acc
| fields ->
let rec loop = function
| [] ->
let params = List.map (fun t -> TPType (CTPath { tname = t.tp_name; tparams = []; tsub = None; tpackage = [] })) d.d_params in
CTPath { tpackage = []; tname = d.d_name; tparams = params; tsub = None }
| AIsType t :: _ -> t
| _ :: l -> loop l
in
let this_t = loop d.d_flags in
let fields = List.map (fun f ->
let stat = List.mem AStatic f.cff_access in
let p = f.cff_pos in
match f.cff_kind with
| FProp (("get" | "never"),("set" | "never"),_,_) when not stat ->
(* TODO: hack to avoid issues with abstract property generation on As3 *)
if Common.defined ctx.com Define.As3 then f.cff_meta <- (Meta.Extern,[],p) :: f.cff_meta;
{ f with cff_access = AStatic :: f.cff_access; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
| FProp _ when not stat ->
display_error ctx "Member property accessors must be get/set or never" p;
f
| FFun fu when f.cff_name = "new" && not stat ->
let init p = (EVars ["this",Some this_t,None],p) in
let ret p = (EReturn (Some (EConst (Ident "this"),p)),p) in
if Meta.has Meta.MultiType a.a_meta then begin
if List.mem AInline f.cff_access then error "MultiType constructors cannot be inline" f.cff_pos;
if fu.f_expr <> None then error "MultiType constructors cannot have a body" f.cff_pos;
end;
let has_call e =
let rec loop e = match fst e with
| ECall _ -> raise Exit
| _ -> Ast.map_expr loop e
in
try ignore(loop e); false with Exit -> true
in
let fu = {
fu with
f_expr = (match fu.f_expr with
| None -> if Meta.has Meta.MultiType a.a_meta then Some (EConst (Ident "null"),p) else None
| Some (EBlock [EBinop (OpAssign,(EConst (Ident "this"),_),e),_],_ | EBinop (OpAssign,(EConst (Ident "this"),_),e),_) when not (has_call e) ->
Some (EReturn (Some e), pos e)
| Some (EBlock el,p) -> Some (EBlock (init p :: el @ [ret p]),p)
| Some e -> Some (EBlock [init p;e;ret p],p)
);
f_type = Some this_t;
} in
{ f with cff_name = "_new"; cff_access = AStatic :: f.cff_access; cff_kind = FFun fu; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
| FFun fu when not stat ->
if Meta.has Meta.From f.cff_meta then error "@:from cast functions must be static" f.cff_pos;
let fu = { fu with f_args = (if List.mem AMacro f.cff_access then fu.f_args else ("this",false,Some this_t,None) :: fu.f_args) } in
{ f with cff_kind = FFun fu; cff_access = AStatic :: f.cff_access; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
| _ ->
f
) fields in
let meta = ref [] in
if has_meta Meta.Dce a.a_meta then meta := (Meta.Dce,[],p) :: !meta;
let acc = make_decl acc (EClass { d_name = d.d_name ^ "_Impl_"; d_flags = [HPrivate]; d_data = fields; d_doc = None; d_params = []; d_meta = !meta },p) in
(match !decls with
| (TClassDecl c,_) :: _ ->
List.iter (fun m -> match m with
| ((Meta.Build | Meta.CoreApi | Meta.Allow | Meta.Access | Meta.Enum | Meta.Dce),_,_) ->
c.cl_meta <- m :: c.cl_meta;
| _ ->
()
) a.a_meta;
a.a_impl <- Some c;
c.cl_kind <- KAbstractImpl a
| _ -> assert false);
acc
) in
decl :: acc
in
let tdecls = List.fold_left make_decl [] tdecls in
let decls = List.rev !decls in
m.m_types <- List.map fst decls;
m, decls, List.rev tdecls
let parse_file com file p =
let ch = (try open_in_bin file with _ -> error ("Could not open " ^ file) p) in
let t = Common.timer "parsing" in
Lexer.init file true;
incr stats.s_files_parsed;
let data = (try Parser.parse com (Lexing.from_channel ch) with e -> close_in ch; t(); raise e) in
close_in ch;
t();
Common.log com ("Parsed " ^ file);
data
let parse_hook = ref parse_file
let type_module_hook = ref (fun _ _ _ -> None)
let type_function_params_rec = ref (fun _ _ _ _ -> assert false)
let return_partial_type = ref false
let type_function_arg ctx t e opt p =
if opt then
let e = (match e with None -> Some (EConst (Ident "null"),p) | _ -> e) in
ctx.t.tnull t, e
else
let t = match e with Some (EConst (Ident "null"),p) -> ctx.t.tnull t | _ -> t in
t, e
let type_var_field ctx t e stat p =
if stat then ctx.curfun <- FunStatic else ctx.curfun <- FunMember;
let e = type_expr ctx e (WithType t) in
let e = (!cast_or_unify_ref) ctx t e p in
match t with
| TType ({ t_path = ([],"UInt") },[]) | TAbstract ({ a_path = ([],"UInt") },[]) when stat -> { e with etype = t }
| _ -> e
let apply_macro ctx mode path el p =
let cpath, meth = (match List.rev (ExtString.String.nsplit path ".") with
| meth :: name :: pack -> (List.rev pack,name), meth
| _ -> error "Invalid macro path" p
) in
ctx.g.do_macro ctx mode cpath meth el p
(** since load_type_def and load_instance are used in PASS2, they should not access the structure of a type **)
(*
load a type or a subtype definition
*)
let rec load_type_def ctx p t =
let no_pack = t.tpackage = [] in
let tname = (match t.tsub with None -> t.tname | Some n -> n) in
try
if t.tsub <> None then raise Not_found;
List.find (fun t2 ->
let tp = t_path t2 in
tp = (t.tpackage,tname) || (no_pack && snd tp = tname)
) (ctx.m.curmod.m_types @ ctx.m.module_types)
with
Not_found ->
let next() =
let t, m = (try
t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
with Error (Module_not_found _,p2) as e when p == p2 ->
match t.tpackage with
| "std" :: l ->
let t = { t with tpackage = l } in
t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
| _ -> raise e
) in
let tpath = (t.tpackage,tname) in
try
List.find (fun t -> not (t_infos t).mt_private && t_path t = tpath) m.m_types
with
Not_found -> raise (Error (Type_not_found (m.m_path,tname),p))
in
(* lookup in wildcard imported packages *)
try
if not no_pack then raise Exit;
let rec loop = function
| [] -> raise Exit
| wp :: l ->
try
load_type_def ctx p { t with tpackage = wp }
with
| Error (Module_not_found _,p2)
| Error (Type_not_found _,p2) when p == p2 -> loop l
in
loop ctx.m.wildcard_packages
with Exit ->
(* lookup in our own package - and its upper packages *)
let rec loop = function
| [] -> raise Exit
| (_ :: lnext) as l ->
try
load_type_def ctx p { t with tpackage = List.rev l }
with
| Error (Module_not_found _,p2)
| Error (Type_not_found _,p2) when p == p2 -> loop lnext
in
try
if not no_pack then raise Exit;
(match fst ctx.m.curmod.m_path with
| [] -> raise Exit
| x :: _ ->
(* this can occur due to haxe remoting : a module can be
already defined in the "js" package and is not allowed
to access the js classes *)
try
(match PMap.find x ctx.com.package_rules with
| Forbidden -> raise Exit
| _ -> ())
with Not_found -> ());
loop (List.rev (fst ctx.m.curmod.m_path));
with
Exit -> next()
let check_param_constraints ctx types t pl c p =
match follow t with
| TMono _ -> ()
| _ ->
let ctl = (match c.cl_kind with KTypeParameter l -> l | _ -> []) in
List.iter (fun ti ->
let ti = apply_params types pl ti in
let ti = (match follow ti with
| TInst ({ cl_kind = KGeneric } as c,pl) ->
(* if we solve a generic contraint, let's substitute with the actual generic instance before unifying *)
let _,_, f = ctx.g.do_build_instance ctx (TClassDecl c) p in
f pl
| _ -> ti
) in
try
unify_raise ctx t ti p
with Error(Unify l,p) ->
if not ctx.untyped then display_error ctx (error_msg (Unify (Constraint_failure (s_type_path c.cl_path) :: l))) p;
) ctl
let requires_value_meta com co =
Common.defined com Define.DocGen || (match co with
| None -> false
| Some c -> c.cl_extern || Meta.has Meta.Rtti c.cl_meta)
let generate_value_meta com co cf args =
if requires_value_meta com co then begin
let values = List.fold_left (fun acc (name,_,_,eo) -> match eo with Some e -> (name,e) :: acc | _ -> acc) [] args in
match values with
| [] -> ()
| _ -> cf.cf_meta <- ((Meta.Value,[EObjectDecl values,cf.cf_pos],cf.cf_pos) :: cf.cf_meta)
end
(* build an instance from a full type *)
let rec load_instance ctx t p allow_no_params =
try
if t.tpackage <> [] || t.tsub <> None then raise Not_found;
let pt = List.assoc t.tname ctx.type_params in
if t.tparams <> [] then error ("Class type parameter " ^ t.tname ^ " can't have parameters") p;
pt
with Not_found ->
let mt = load_type_def ctx p t in
let is_generic,is_generic_build = match mt with
| TClassDecl {cl_kind = KGeneric} -> true,false
| TClassDecl {cl_kind = KGenericBuild _} -> false,true
| _ -> false,false
in
let types , path , f = ctx.g.do_build_instance ctx mt p in
let is_rest = is_generic_build && (match types with ["Rest",_] -> true | _ -> false) in
if allow_no_params && t.tparams = [] && not is_rest then begin
let pl = ref [] in
pl := List.map (fun (name,t) ->
match follow t with
| TInst (c,_) ->
let t = mk_mono() in
if c.cl_kind <> KTypeParameter [] || is_generic then delay ctx PCheckConstraint (fun() -> check_param_constraints ctx types t (!pl) c p);
t;
| _ -> assert false
) types;
f (!pl)
end else if path = ([],"Dynamic") then
match t.tparams with
| [] -> t_dynamic
| [TPType t] -> TDynamic (load_complex_type ctx p t)
| _ -> error "Too many parameters for Dynamic" p
else begin
if not is_rest && List.length types <> List.length t.tparams then error ("Invalid number of type parameters for " ^ s_type_path path) p;
let tparams = List.map (fun t ->
match t with
| TPExpr e ->
let name = (match fst e with
| EConst (String s) -> "S" ^ s
| EConst (Int i) -> "I" ^ i
| EConst (Float f) -> "F" ^ f
| _ -> "Expr"
) in
let c = mk_class null_module ([],name) p in
c.cl_kind <- KExpr e;
TInst (c,[])
| TPType t -> load_complex_type ctx p t
) t.tparams in
let rec loop tl1 tl2 is_rest = match tl1,tl2 with
| t :: tl1,(name,t2) :: tl2 ->
let isconst = (match t with TInst ({ cl_kind = KExpr _ },_) -> true | _ -> false) in
if isconst <> (name = "Const") && t != t_dynamic && name <> "Rest" then error (if isconst then "Constant value unexpected here" else "Constant value excepted as type parameter") p;
let is_rest = is_rest || name = "Rest" && is_generic_build in
let t = match follow t2 with
| TInst ({ cl_kind = KTypeParameter [] }, []) when not is_generic ->
t
| TInst (c,[]) ->
let r = exc_protect ctx (fun r ->
r := (fun() -> t);
delay ctx PCheckConstraint (fun() -> check_param_constraints ctx types t tparams c p);
t
) "constraint" in
delay ctx PForce (fun () -> ignore(!r()));
TLazy r
| _ -> assert false
in
t :: loop tl1 tl2 is_rest
| [],[] ->
[]
| [],["Rest",_] when is_generic_build ->
[]
| [],_ ->
error ("Not enough type parameters for " ^ s_type_path path) p
| t :: tl,[] ->
if is_rest then
t :: loop tl [] true
else
error ("Too many parameters for " ^ s_type_path path) p
in
let params = loop tparams types false in
f params
end
(*
build an instance from a complex type
*)
and load_complex_type ctx p t =
match t with
| CTParent t -> load_complex_type ctx p t
| CTPath t -> load_instance ctx t p false
| CTOptional _ -> error "Optional type not allowed here" p
| CTExtend (tl,l) ->
(match load_complex_type ctx p (CTAnonymous l) with
| TAnon a as ta ->
let is_redefined cf1 a2 =
try
let cf2 = PMap.find cf1.cf_name a2.a_fields in
let st = s_type (print_context()) in
if not (type_iseq cf1.cf_type cf2.cf_type) then begin
display_error ctx ("Cannot redefine field " ^ cf1.cf_name ^ " with different type") p;
display_error ctx ("First type was " ^ (st cf1.cf_type)) cf1.cf_pos;
error ("Second type was " ^ (st cf2.cf_type)) cf2.cf_pos
end else
true
with Not_found ->
false
in
let mk_extension t =
match follow t with
| TInst ({cl_kind = KTypeParameter _},_) ->
error "Cannot structurally extend type parameters" p
| TInst (c,tl) ->
ctx.com.warning "Structurally extending classes is deprecated and will be removed" p;
let c2 = mk_class null_module (fst c.cl_path,"+" ^ snd c.cl_path) p in
c2.cl_private <- true;
PMap.iter (fun f _ ->
try
ignore(class_field c tl f);
error ("Cannot redefine field " ^ f) p
with
Not_found -> ()
) a.a_fields;
(* do NOT tag as extern - for protect *)
c2.cl_kind <- KExtension (c,tl);
c2.cl_super <- Some (c,tl);
c2.cl_fields <- a.a_fields;
TInst (c2,[])
| TMono _ ->
error "Loop found in cascading signatures definitions. Please change order/import" p
| TAnon a2 ->
PMap.iter (fun _ cf -> ignore(is_redefined cf a2)) a.a_fields;
TAnon { a_fields = (PMap.foldi PMap.add a.a_fields a2.a_fields); a_status = ref (Extend [t]); }
| _ -> error "Can only extend classes and structures" p
in
let loop t = match follow t with
| TAnon a2 ->
PMap.iter (fun f cf ->
if not (is_redefined cf a) then
a.a_fields <- PMap.add f cf a.a_fields
) a2.a_fields
| _ ->
error "Multiple structural extension is only allowed for structures" p
in
let il = List.map (fun t -> load_instance ctx t p false) tl in
let tr = ref None in
let t = TMono tr in
let r = exc_protect ctx (fun r ->
r := (fun _ -> t);
tr := Some (match il with
| [i] ->
mk_extension i
| _ ->
List.iter loop il;
a.a_status := Extend il;
ta);
t
) "constraint" in
delay ctx PForce (fun () -> ignore(!r()));
TLazy r
| _ -> assert false)
| CTAnonymous l ->
let rec loop acc f =
let n = f.cff_name in
let p = f.cff_pos in
if PMap.mem n acc then error ("Duplicate field declaration : " ^ n) p;
let topt = function
| None -> error ("Explicit type required for field " ^ n) p
| Some t -> load_complex_type ctx p t
in
let no_expr = function
| None -> ()
| Some (_,p) -> error "Expression not allowed here" p
in
let pub = ref true in
let dyn = ref false in
let params = ref [] in
List.iter (fun a ->
match a with
| APublic -> ()
| APrivate -> pub := false;
| ADynamic when (match f.cff_kind with FFun _ -> true | _ -> false) -> dyn := true
| AStatic | AOverride | AInline | ADynamic | AMacro -> error ("Invalid access " ^ Ast.s_access a) p
) f.cff_access;
let t , access = (match f.cff_kind with
| FVar (Some (CTPath({tpackage=[];tname="Void"})), _) | FProp (_,_,Some (CTPath({tpackage=[];tname="Void"})),_) ->
error "Fields of type Void are not allowed in structures" p
| FVar (t, e) ->
no_expr e;
topt t, Var { v_read = AccNormal; v_write = AccNormal }
| FFun fd ->
params := (!type_function_params_rec) ctx fd f.cff_name p;
no_expr fd.f_expr;
let old = ctx.type_params in
ctx.type_params <- !params @ old;
let args = List.map (fun (name,o,t,e) -> no_expr e; name, o, topt t) fd.f_args in
let t = TFun (args,topt fd.f_type), Method (if !dyn then MethDynamic else MethNormal) in
ctx.type_params <- old;
t
| FProp (i1,i2,t,e) ->
no_expr e;
let access m get =
match m with
| "null" -> AccNo
| "never" -> AccNever
| "default" -> AccNormal
| "dynamic" -> AccCall
| "get" when get -> AccCall
| "set" when not get -> AccCall
| x when get && x = "get_" ^ n -> AccCall
| x when not get && x = "set_" ^ n -> AccCall
| _ ->
error "Custom property access is no longer supported in Haxe 3" f.cff_pos;
in
let t = (match t with None -> error "Type required for structure property" p | Some t -> t) in
load_complex_type ctx p t, Var { v_read = access i1 true; v_write = access i2 false }
) in
let t = if Meta.has Meta.Optional f.cff_meta then ctx.t.tnull t else t in
let cf = {
cf_name = n;
cf_type = t;
cf_pos = p;
cf_public = !pub;
cf_kind = access;
cf_params = !params;
cf_expr = None;
cf_doc = f.cff_doc;
cf_meta = f.cff_meta;
cf_overloads = [];
} in
init_meta_overloads ctx None cf;
PMap.add n cf acc
in
mk_anon (List.fold_left loop PMap.empty l)
| CTFunction (args,r) ->
match args with
| [CTPath { tpackage = []; tparams = []; tname = "Void" }] ->
TFun ([],load_complex_type ctx p r)
| _ ->
TFun (List.map (fun t ->
let t, opt = (match t with CTOptional t -> t, true | _ -> t,false) in
"",opt,load_complex_type ctx p t
) args,load_complex_type ctx p r)
and init_meta_overloads ctx co cf =
let overloads = ref [] in
let filter_meta m = match m with
| ((Meta.Overload | Meta.Value),_,_) -> false
| _ -> true
in
let cf_meta = List.filter filter_meta cf.cf_meta in
cf.cf_meta <- List.filter (fun m ->
match m with
| (Meta.Overload,[(EFunction (fname,f),p)],_) ->
if fname <> None then error "Function name must not be part of @:overload" p;
(match f.f_expr with Some (EBlock [], _) -> () | _ -> error "Overload must only declare an empty method body {}" p);
let old = ctx.type_params in
(match cf.cf_params with
| [] -> ()
| l -> ctx.type_params <- List.filter (fun t -> not (List.mem t l)) ctx.type_params);
let params = (!type_function_params_rec) ctx f cf.cf_name p in
ctx.type_params <- params @ ctx.type_params;
let topt = function None -> error "Explicit type required" p | Some t -> load_complex_type ctx p t in
let args = List.map (fun (a,opt,t,_) -> a,opt,topt t) f.f_args in
let cf = { cf with cf_type = TFun (args,topt f.f_type); cf_params = params; cf_meta = cf_meta} in
generate_value_meta ctx.com co cf f.f_args;
overloads := cf :: !overloads;
ctx.type_params <- old;
false
| (Meta.Overload,[],_) when ctx.com.config.pf_overload ->
let topt (n,_,t) = match t with | TMono t when !t = None -> error ("Explicit type required for overload functions\nFor function argument '" ^ n ^ "'") cf.cf_pos | _ -> () in
(match follow cf.cf_type with
| TFun (args,_) -> List.iter topt args
| _ -> () (* could be a variable *));
true
| (Meta.Overload,[],p) ->
error "This platform does not support this kind of overload declaration. Try @:overload(function()... {}) instead" p
| (Meta.Overload,_,p) ->
error "Invalid @:overload metadata format" p
| _ ->
true
) cf.cf_meta;
cf.cf_overloads <- (List.rev !overloads)
let hide_params ctx =
let old_m = ctx.m in
let old_type_params = ctx.type_params in
let old_deps = ctx.g.std.m_extra.m_deps in
ctx.m <- {
curmod = ctx.g.std;
module_types = [];
module_using = [];
module_globals = PMap.empty;
wildcard_packages = [];
};
ctx.type_params <- [];
(fun() ->
ctx.m <- old_m;
ctx.type_params <- old_type_params;
(* restore dependencies that might be have been wronly inserted *)
ctx.g.std.m_extra.m_deps <- old_deps;
)
(*
load a type while ignoring the current imports or local types
*)
let load_core_type ctx name =
let show = hide_params ctx in
let t = load_instance ctx { tpackage = []; tname = name; tparams = []; tsub = None; } null_pos false in
show();
add_dependency ctx.m.curmod (match t with
| TInst (c,_) -> c.cl_module
| TType (t,_) -> t.t_module
| TAbstract (a,_) -> a.a_module
| TEnum (e,_) -> e.e_module
| _ -> assert false);
t
let t_iterator ctx =
let show = hide_params ctx in
match load_type_def ctx null_pos { tpackage = []; tname = "Iterator"; tparams = []; tsub = None } with
| TTypeDecl t ->
show();
add_dependency ctx.m.curmod t.t_module;
if List.length t.t_params <> 1 then assert false;
let pt = mk_mono() in
apply_params t.t_params [pt] t.t_type, pt
| _ ->
assert false
(*
load either a type t or Null<Unknown> if not defined
*)
let load_type_opt ?(opt=false) ctx p t =
let t = (match t with None -> mk_mono() | Some t -> load_complex_type ctx p t) in
if opt then ctx.t.tnull t else t
(* ---------------------------------------------------------------------- *)
(* Structure check *)
let valid_redefinition ctx f1 t1 f2 t2 =
let valid t1 t2 =
Type.unify t1 t2;
if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)]);
in
let t1, t2 = (match f1.cf_params, f2.cf_params with
| [], [] -> t1, t2
| l1, l2 when List.length l1 = List.length l2 ->
let to_check = ref [] in
let monos = List.map2 (fun (name,p1) (_,p2) ->
(match follow p1, follow p2 with
| TInst ({ cl_kind = KTypeParameter ct1 } as c1,pl1), TInst ({ cl_kind = KTypeParameter ct2 } as c2,pl2) ->
(match ct1, ct2 with
| [], [] -> ()
| _, _ when List.length ct1 = List.length ct2 ->
(* if same constraints, they are the same type *)
let check monos =
List.iter2 (fun t1 t2 ->
try
let t1 = apply_params l1 monos (apply_params c1.cl_params pl1 t1) in
let t2 = apply_params l2 monos (apply_params c2.cl_params pl2 t2) in
type_eq EqStrict t1 t2
with Unify_error l ->
raise (Unify_error (Unify_custom "Constraints differ" :: l))
) ct1 ct2
in
to_check := check :: !to_check;
| _ ->
raise (Unify_error [Unify_custom "Different number of constraints"]))
| _ -> ());
TInst (mk_class null_module ([],name) Ast.null_pos,[])
) l1 l2 in
List.iter (fun f -> f monos) !to_check;
apply_params l1 monos t1, apply_params l2 monos t2
| _ ->
(* ignore type params, will create other errors later *)
t1, t2
) in
match f1.cf_kind,f2.cf_kind with
| Method m1, Method m2 when not (m1 = MethDynamic) && not (m2 = MethDynamic) ->
begin match follow t1, follow t2 with
| TFun (args1,r1) , TFun (args2,r2) -> (
if not (List.length args1 = List.length args2) then raise (Unify_error [Unify_custom "Different number of function arguments"]);
try
List.iter2 (fun (n,o1,a1) (_,o2,a2) ->
if o1 <> o2 then raise (Unify_error [Not_matching_optional n]);
(try valid a2 a1 with Unify_error _ -> raise (Unify_error [Cannot_unify(a1,a2)]))
) args1 args2;
valid r1 r2
with Unify_error l ->
raise (Unify_error (Cannot_unify (t1,t2) :: l)))
| _ ->
assert false
end
| _,(Var { v_write = AccNo | AccNever }) ->
(* write variance *)
valid t2 t1
| _,(Var { v_read = AccNo | AccNever }) ->
(* read variance *)
valid t1 t2
| _ , _ ->
(* in case args differs, or if an interface var *)
type_eq EqStrict t1 t2;
if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)])
let copy_meta meta_src meta_target sl =
let meta = ref meta_target in
List.iter (fun (m,e,p) ->
if List.mem m sl then meta := (m,e,p) :: !meta
) meta_src;
!meta
let same_overload_args t1 t2 f1 f2 =
if List.length f1.cf_params <> List.length f2.cf_params then
false
else
let rec follow_skip_null t = match t with
| TMono r ->
(match !r with
| Some t -> follow_skip_null t
| _ -> t)
| TLazy f ->
follow_skip_null (!f())
| TType ({ t_path = [],"Null" } as t, [p]) ->
TType(t,[follow p])
| TType (t,tl) ->
follow_skip_null (apply_params t.t_params tl t.t_type)
| _ -> t
in
let same_arg t1 t2 =
let t1 = follow_skip_null t1 in
let t2 = follow_skip_null t2 in
match follow_skip_null t1, follow_skip_null t2 with
| TType _, TType _ -> type_iseq t1 t2
| TType _, _
| _, TType _ -> false
| _ -> type_iseq t1 t2
in
match follow (apply_params f1.cf_params (List.map (fun (_,t) -> t) f2.cf_params) t1), follow t2 with
| TFun(a1,_), TFun(a2,_) ->
(try
List.for_all2 (fun (_,_,t1) (_,_,t2) ->
same_arg t1 t2) a1 a2
with | Invalid_argument("List.for_all2") ->
false)
| _ -> assert false
(** retrieves all overloads from class c and field i, as (Type.t * tclass_field) list *)
let rec get_overloads c i =
let ret = try
let f = PMap.find i c.cl_fields in
(f.cf_type, f) :: (List.map (fun f -> f.cf_type, f) f.cf_overloads)
with | Not_found -> []
in
let rsup = match c.cl_super with
| None when c.cl_interface ->
let ifaces = List.concat (List.map (fun (c,tl) ->
List.map (fun (t,f) -> apply_params c.cl_params tl t, f) (get_overloads c i)
) c.cl_implements) in
ret @ ifaces
| None -> ret
| Some (c,tl) ->
ret @ ( List.map (fun (t,f) -> apply_params c.cl_params tl t, f) (get_overloads c i) )
in
ret @ (List.filter (fun (t,f) -> not (List.exists (fun (t2,f2) -> same_overload_args t t2 f f2) ret)) rsup)
let check_overloads ctx c =
(* check if field with same signature was declared more than once *)
List.iter (fun f ->
if Meta.has Meta.Overload f.cf_meta then
List.iter (fun f2 ->
try
ignore (List.find (fun f3 -> f3 != f2 && same_overload_args f2.cf_type f3.cf_type f2 f3) (f :: f.cf_overloads));
display_error ctx ("Another overloaded field of same signature was already declared : " ^ f2.cf_name) f2.cf_pos
with | Not_found -> ()
) (f :: f.cf_overloads)) (c.cl_ordered_fields @ c.cl_ordered_statics)
let check_overriding ctx c =
match c.cl_super with
| None ->
(match c.cl_overrides with
| [] -> ()
| i :: _ ->
display_error ctx ("Field " ^ i.cf_name ^ " is declared 'override' but doesn't override any field") i.cf_pos)
| _ when c.cl_extern && Meta.has Meta.CsNative c.cl_meta -> () (* -net-lib specific: do not check overrides on extern CsNative classes *)
| Some (csup,params) ->
PMap.iter (fun i f ->
let p = f.cf_pos in
let check_field f get_super_field is_overload = try
(if is_overload && not (Meta.has Meta.Overload f.cf_meta) then
display_error ctx ("Missing @:overload declaration for field " ^ i) p);
let t, f2 = get_super_field csup i in
(* allow to define fields that are not defined for this platform version in superclass *)
(match f2.cf_kind with
| Var { v_read = AccRequire _ } -> raise Not_found;
| _ -> ());
if ctx.com.config.pf_overload && (Meta.has Meta.Overload f2.cf_meta && not (Meta.has Meta.Overload f.cf_meta)) then
display_error ctx ("Field " ^ i ^ " should be declared with @:overload since it was already declared as @:overload in superclass") p
else if not (List.memq f c.cl_overrides) then
display_error ctx ("Field " ^ i ^ " should be declared with 'override' since it is inherited from superclass") p
else if not f.cf_public && f2.cf_public then
display_error ctx ("Field " ^ i ^ " has less visibility (public/private) than superclass one") p
else (match f.cf_kind, f2.cf_kind with
| _, Method MethInline ->
display_error ctx ("Field " ^ i ^ " is inlined and cannot be overridden") p
| a, b when a = b -> ()
| Method MethInline, Method MethNormal ->
() (* allow to redefine a method as inlined *)
| _ ->
display_error ctx ("Field " ^ i ^ " has different property access than in superclass") p);
if has_meta Meta.Final f2.cf_meta then display_error ctx ("Cannot override @:final method " ^ i) p;
try
let t = apply_params csup.cl_params params t in
valid_redefinition ctx f f.cf_type f2 t
with
Unify_error l ->
display_error ctx ("Field " ^ i ^ " overloads parent class with different or incomplete type") p;
display_error ctx (error_msg (Unify l)) p;
with
Not_found ->
if List.memq f c.cl_overrides then
let msg = if is_overload then
("Field " ^ i ^ " is declared 'override' but no compatible overload was found")
else
("Field " ^ i ^ " is declared 'override' but doesn't override any field")
in
display_error ctx msg p
in
if ctx.com.config.pf_overload && Meta.has Meta.Overload f.cf_meta then begin
let overloads = get_overloads csup i in
List.iter (fun (t,f2) ->
(* check if any super class fields are vars *)
match f2.cf_kind with
| Var _ ->
display_error ctx ("A variable named '" ^ f2.cf_name ^ "' was already declared in a superclass") f.cf_pos
| _ -> ()
) overloads;
List.iter (fun f ->
(* find the exact field being overridden *)
check_field f (fun csup i ->
List.find (fun (t,f2) ->
same_overload_args f.cf_type (apply_params csup.cl_params params t) f f2
) overloads
) true
) (f :: f.cf_overloads)
end else
check_field f (fun csup i ->
let _, t, f2 = raw_class_field (fun f -> f.cf_type) csup params i in
t, f2) false
) c.cl_fields
let class_field_no_interf c i =
try
let f = PMap.find i c.cl_fields in
f.cf_type , f
with Not_found ->
match c.cl_super with
| None ->
raise Not_found
| Some (c,tl) ->
(* rec over class_field *)
let _, t , f = raw_class_field (fun f -> f.cf_type) c tl i in
apply_params c.cl_params tl t , f
let rec check_interface ctx c intf params =
let p = c.cl_pos in
let rec check_field i f =
(if ctx.com.config.pf_overload then
List.iter (function
| f2 when f != f2 ->
check_field i f2
| _ -> ()) f.cf_overloads);
let is_overload = ref false in
try
let t2, f2 = class_field_no_interf c i in
let t2, f2 =
if ctx.com.config.pf_overload && (f2.cf_overloads <> [] || Meta.has Meta.Overload f2.cf_meta) then
let overloads = get_overloads c i in
is_overload := true;
let t = (apply_params intf.cl_params params f.cf_type) in
List.find (fun (t1,f1) -> same_overload_args t t1 f f1) overloads
else
t2, f2
in
ignore(follow f2.cf_type); (* force evaluation *)
let p = (match f2.cf_expr with None -> p | Some e -> e.epos) in
let mkind = function
| MethNormal | MethInline -> 0
| MethDynamic -> 1
| MethMacro -> 2
in
if f.cf_public && not f2.cf_public && not (Meta.has Meta.CompilerGenerated f.cf_meta) then
display_error ctx ("Field " ^ i ^ " should be public as requested by " ^ s_type_path intf.cl_path) p
else if not (unify_kind f2.cf_kind f.cf_kind) || not (match f.cf_kind, f2.cf_kind with Var _ , Var _ -> true | Method m1, Method m2 -> mkind m1 = mkind m2 | _ -> false) then
display_error ctx ("Field " ^ i ^ " has different property access than in " ^ s_type_path intf.cl_path ^ " (" ^ s_kind f2.cf_kind ^ " should be " ^ s_kind f.cf_kind ^ ")") p
else try
valid_redefinition ctx f2 t2 f (apply_params intf.cl_params params f.cf_type)
with
Unify_error l ->
if not (Meta.has Meta.CsNative c.cl_meta && c.cl_extern) then begin
display_error ctx ("Field " ^ i ^ " has different type than in " ^ s_type_path intf.cl_path) p;
display_error ctx (error_msg (Unify l)) p;
end
with
| Not_found when not c.cl_interface ->
let msg = if !is_overload then
let ctx = print_context() in
let args = match follow f.cf_type with | TFun(args,_) -> String.concat ", " (List.map (fun (n,o,t) -> (if o then "?" else "") ^ n ^ " : " ^ (s_type ctx t)) args) | _ -> assert false in
"No suitable overload for " ^ i ^ "( " ^ args ^ " ), as needed by " ^ s_type_path intf.cl_path ^ " was found"
else
("Field " ^ i ^ " needed by " ^ s_type_path intf.cl_path ^ " is missing")
in
display_error ctx msg p
| Not_found -> ()
in
PMap.iter check_field intf.cl_fields;
List.iter (fun (i2,p2) ->
check_interface ctx c i2 (List.map (apply_params intf.cl_params params) p2)
) intf.cl_implements
let check_interfaces ctx c =
match c.cl_path with
| "Proxy" :: _ , _ -> ()
| _ when c.cl_extern && Meta.has Meta.CsNative c.cl_meta -> ()
| _ ->
List.iter (fun (intf,params) -> check_interface ctx c intf params) c.cl_implements
let rec return_flow ctx e =
let error() = display_error ctx "A return is missing here" e.epos; raise Exit in
let return_flow = return_flow ctx in
match e.eexpr with
| TReturn _ | TThrow _ -> ()
| TParenthesis e | TMeta(_,e) ->
return_flow e
| TBlock el ->
let rec loop = function
| [] -> error()
| [e] -> return_flow e
| { eexpr = TReturn _ } :: _ | { eexpr = TThrow _ } :: _ -> ()
| _ :: l -> loop l
in
loop el
| TIf (_,e1,Some e2) ->
return_flow e1;
return_flow e2;