-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.ml
1493 lines (1296 loc) · 50.7 KB
/
infer.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
(**
* Code taken from: https://github.com/tomprimozic/type-systems
*)
open Ast
open Printf
exception Not_implemented of string
exception MapMergeException of string
exception Infer_exception of string
type name = string
[@@deriving show]
(*
type expr =
| Var of name (* variable *)
| Call of expr * expr list (* application *)
| Fun of name * expr list (* abstraction, name of function and args *)
(*| Let of name * expr * expr (* let *)*)
| Let of name * expr (* PHP variables don't have scope *)
| Num of int (* PHP can't infer int or float, use num *)
| String of string
*)
(** PHP programs are lists of expressions/statements *)
type progrem = expr list
type id = int
[@@deriving show]
type level = int
[@@deriving show]
type ty =
| TConst of name (* type constant: `int` or `bool`. TODO: Used? *)
| TApp of ty * ty list (* type application: `list[int]` *)
| TArrow of ty list * ty (* function type: e.g. `(int, int) -> int` *)
| TVar of tvar ref (* type variable *)
| TFixedSizeArray of int * ty
| TDynamicSizeArray of ty
| TNumber
| TString
| TStruct of string * (string * ty) list (* class name, field list *)
| TBoolean
| TUnit
| TUnresolved (* Not yet resolved, as in struct which need to be used to know the type *)
[@@deriving show]
(** TODO: Explain? *)
(** Type variable *)
and tvar =
| Unbound of id * level
| Link of ty (* alias? *)
| Generic of id
[@@deriving show]
let current_id = ref 0
let next_id () =
let id = !current_id in
current_id := id + 1 ;
id
let reset_id () = current_id := 0
let new_var level = TVar (ref (Unbound(next_id (), level)))
let new_gen_var () = TVar (ref (Generic(next_id ())))
exception Error of string
exception Unify_error of string * ty * ty
exception Not_implemented of string
let error msg = raise (Error msg)
(**
* Return file and line information used in error messages
*
* @param pos Pos.pos
* @return string
*)
let get_pos_msg pos =
let line, start, end_ = Pos.info_pos pos in
sprintf "File %S, line %d, characters %d-%d"
(snd Pos.(pos.pos_file)) line start end_
module Env = struct
module StringMap = Map.Make (String)
type env = {
map : ty StringMap.t;
return_ty : ty option;
}
(* ty StringMap.t * ty option (* normal mappings * return type *)*)
let empty : env = {map = StringMap.empty; return_ty = None}
let extend env name ty =
let new_mapping = StringMap.add name ty (env.map) in
{env with map = new_mapping}
let lookup env name = StringMap.find name env.map
let new_return_type env ty : env =
{env with return_ty = ty}
(**
* Return all functions from an environment
* Usefule when injecting namespaces into a function env
*
* @param env
* @return env
*)
let get_functions env : env =
let new_map = StringMap.filter (fun key value ->
match value with
| TStruct _
| TArrow _ -> true
| _ -> false
) env.map
in
{env with map = new_map}
(**
* Add env src to env dest
*
* @param src env
* @param dest env
* @return src + dest env
*)
let merge src dest : env =
let new_map = StringMap.merge (fun key src_val dest_val ->
match src_val, dest_val with
| None, None -> None
| Some a, None -> Some a
| None, Some a -> Some a
| Some a, Some b ->
raise (MapMergeException "Can't merge environments - one key exists in both environments")
) src.map dest.map
in
{dest with map = new_map}
(**
* Print environment
*
* @return unit
*)
let dump (env : env) =
print_endline "[ env = ";
StringMap.iter (fun key value ->
print_endline (sprintf " %s : %s" key (show_ty value))
) env.map;
print_endline (sprintf " return type = %s" (match env.return_ty with Some ty -> show_ty ty | None -> "None"));
print_endline "]"
end
(**
* We store class def here for the Typedast
*
* @TODO Namespaces
*)
let typed_classes : (string, Typedast.def) Hashtbl.t = Hashtbl.create 10
(** Built-in function type *)
type builtin_function = {
builtin_name : string;
builtin_args : fun_param list;
builtin_ty : ty;
}
(**
* List of built-in functions
*)
let builtin_functions = [
(* sqrt *)
{
builtin_name = "sqrt";
builtin_args = [{
param_hint = None;
param_is_reference = false;
param_is_variadic = false;
param_id = (Pos.none, "x");
param_expr = None;
param_modifier = None;
param_user_attributes = [];
}];
builtin_ty = TArrow ([TNumber], TNumber);
}
]
(**
* Return true if function_name is a PHP library function, like
* var_dump, sqrt, etc.
*
* Limited support.
*
* @param function_name string
* @return bool
*)
let is_builtin_function function_name =
List.exists (fun el ->
el.builtin_name = function_name
) builtin_functions
(**
* Map from ty to Typedast.ty
*
* @param ty
* @return Typedast.ty
*)
let rec ty_of_ty (typ : ty) : Typedast.ty =
match typ with
| TVar tvar_ref ->
let tvar = !tvar_ref in
begin match tvar with
| Link ty ->
ty_of_ty ty
(* Problem here is that we have to get "typed type" before
* inference is fully done. Therefore, delay the return value
* by wrapping it into a closure. *)
| _ -> Typedast.Delayed (fun () ->
ty_of_ty (TVar tvar_ref))
end
| TConst _ | TApp _ -> failwith "ty_of_ty: Has no correspondance in Typedast"
| TFixedSizeArray (length, ty) -> Typedast.TFixedSizeArray (length, ty_of_ty ty)
| TDynamicSizeArray ty -> Typedast.TDynamicSizeArray (ty_of_ty ty)
| TNumber -> Typedast.TNumber
| TString -> Typedast.TString
| TBoolean -> Typedast.TBoolean
| TUnit -> Typedast.TUnit
| TStruct (struct_name, tys) ->
(* Getting a bit weird here, where struct type fields point
* to definition of struct refs, so it can be updated as
* the typing algorithm runs. *)
let struct_name = String.sub struct_name 1 (String.length struct_name - 1) in (* Strip leading \ (namespace thing) *)
let typedast_object_ty = try Hashtbl.find typed_classes struct_name with
| Not_found -> failwith ("Found no typed ast object with name " ^ struct_name)
in
let object_fields = begin match typedast_object_ty with
| Typedast.Struct {Typedast.struct_fields} -> struct_fields
| _ -> failwith "Unknown object type: Found no fields"
end in
Typedast.TStruct (struct_name, (List.map (fun (_, t) -> t ) object_fields))
| TUnresolved ->
let none = ref None in
Typedast.TWeak_poly none
| TArrow (args, ret) ->
Typedast.TArrow ([], ty_of_ty ret)
(**
* Binary operation to Typedast op
*
* @param bop
* @return Typedast.bop
*)
let bop_to_typed bop =
match bop with
| Plus -> Typedast.Plus
| Minus -> Typedast.Minus
| Star -> Typedast.Star
| Slash -> Typedast.Slash
| Starstar -> Typedast.Starstar
| Percent -> Typedast.Percent
| _ -> raise (Not_implemented "bop_to_typed")
let og_null_flavor_to_typed = function
| OG_nullthrows -> Typedast.OG_nullthrows
| OG_nullsafe -> Typedast.OG_nullsafe
(**
* Turn type expr into expr_
* Strip Pos, that is
*)
let expr_of_expr_ expr =
match expr with
| (pos, expr_) -> expr_
(**
* @todo What does this do?
*)
let occurs_check_adjust_levels tvar_id tvar_level ty =
let rec f = function
| TVar {contents = Link ty} -> f ty
| TVar {contents = Generic _} -> assert false
| TVar ({contents = Unbound(other_id, other_level)} as other_tvar) ->
if other_id = tvar_id then
error "recursive types"
else
if other_level > tvar_level then
other_tvar := Unbound(other_id, tvar_level)
else
()
| TApp(ty, ty_arg_list) ->
f ty ;
List.iter f ty_arg_list
| TArrow(param_ty_list, return_ty) ->
List.iter f param_ty_list ;
f return_ty
| TNumber | TString | TStruct _ | TBoolean | TUnit | TConst _ | TUnresolved-> ()
| TFixedSizeArray _ | TDynamicSizeArray _ -> ()
in
f ty
(**
* Unifies two types
*
* @return unit? or raises exception
*)
let rec unify ty1 ty2 =
if ty1 == ty2 then () else
match (ty1, ty2) with
| TConst name1, TConst name2 when name1 = name2 -> ()
| TApp(ty1, ty_arg_list1), TApp(ty2, ty_arg_list2) ->
unify ty1 ty2 ;
List.iter2 unify ty_arg_list1 ty_arg_list2
| TArrow(param_ty_list1, return_ty1), TArrow(param_ty_list2, return_ty2) ->
List.iter2 unify param_ty_list1 param_ty_list2 ;
unify return_ty1 return_ty2
| TVar {contents = Link ty1}, ty2 | ty1, TVar {contents = Link ty2} -> unify ty1 ty2
| TVar {contents = Unbound(id1, _)}, TVar {contents = Unbound(id2, _)} when id1 = id2 ->
assert false (* There is only a single instance of a particular type variable. *)
| TVar ({contents = Unbound(id, level)} as tvar), ty
| ty, TVar ({contents = Unbound(id, level)} as tvar) ->
occurs_check_adjust_levels id level ty ;
tvar := Link ty
| _, _ ->
let bt = Printexc.get_backtrace () in
print_endline bt;
raise (Unify_error (("Cannot unify types " ^ show_ty ty1 ^ " and " ^ show_ty ty2), ty1, ty2))
(**
* @todo: What does this do? What is level?
*)
let rec generalize level = function
| TVar {contents = Unbound(id, other_level)} when other_level > level ->
TVar (ref (Generic id))
| TApp(ty, ty_arg_list) ->
TApp(generalize level ty, List.map (generalize level) ty_arg_list)
| TArrow(param_ty_list, return_ty) ->
TArrow(List.map (generalize level) param_ty_list, generalize level return_ty)
| TVar {contents = Link ty} -> generalize level ty
| TVar {contents = Generic _} | TVar {contents = Unbound _}
| TString | TStruct _ | TNumber | TBoolean | TUnit | TUnresolved
| TFixedSizeArray _
| TDynamicSizeArray _
| TConst _ as ty -> ty
(*| ty -> failwith (sprintf "generalize error: %s" (show_ty ty))*)
(**
* @todo instantiate what? type variable?
*)
let instantiate level ty =
let id_var_map = Hashtbl.create 10 in
let rec f ty = match ty with
| TNumber | TString | TStruct _ | TBoolean | TUnit
| TFixedSizeArray _
| TDynamicSizeArray _
| TConst _ | TUnresolved -> ty
| TVar {contents = Link ty} -> f ty
| TVar {contents = Generic id} ->
begin
try
Hashtbl.find id_var_map id
with Not_found ->
let var = new_var level in
Hashtbl.add id_var_map id var ;
var
end
| TVar {contents = Unbound _} -> ty
| TApp(ty, ty_arg_list) ->
TApp(f ty, List.map f ty_arg_list)
| TArrow(param_ty_list, return_ty) ->
TArrow(List.map f param_ty_list, f return_ty)
in
f ty
(**
* Gives type of function?
*
* @param num_params Number of parameters to function
* @param ty ?
* @return ty list * ty List of param types and return type
*)
let rec match_fun_ty num_params = function
| TArrow(param_ty_list, return_ty) ->
if List.length param_ty_list <> num_params then
error "unexpected number of arguments"
else
param_ty_list, return_ty
| TVar {contents = Link ty} -> match_fun_ty num_params ty
| TVar ({contents = Unbound(id, level)} as tvar) ->
let param_ty_list =
let rec f = function
| 0 -> []
| n -> new_var level :: f (n - 1)
in
f num_params
in
let return_ty = new_var level in
tvar := Link (TArrow(param_ty_list, return_ty)) ;
param_ty_list, return_ty
| _ -> error "expected a function"
(**
* Infer type of program
* A program is a list of definitions
*
* @param level ?
* @param defs program
* @return Typedast.program
*)
and infer_program level (defs : def list) : Typedast.program =
let env = Env.empty in
(* Add core functions *)
(* Not used since overloaded support for print/echo
let printd_ty = TArrow ([TNumber], TUnit) in
let env = Env.extend env "printd" printd_ty in
let prints_ty = TArrow ([TString], TUnit) in
let env = Env.extend env "prints" prints_ty in
*)
(**
* Helper function to infer types of defs
* Stmts, funs, classes...
*
* This function basically types the entire program.
*
* @param typed_program Collect typed subexpressions and return
* @return typed_program
*)
let rec aux env level defs (typed_program : Typedast.program) =
(*Env.dump env;*)
match defs with
| [] ->
typed_program
| Stmt stmt :: tail ->
let (typed_stmt, env) = infer_stmt env level stmt in
aux env level tail (typed_program @ [Typedast.Stmt typed_stmt])
| Fun fun_ :: tail ->
let open Namespace_env in
let (_, fn_name) = fun_.f_name in
let namespace_name = fun_.f_namespace.ns_name in
let fn_name = String.sub fn_name 1 (String.length fn_name - 1) in (* Strip leading \ (namespace thing) *)
begin match namespace_name with
| Some _ -> failwith "namespaces not implemented"
| None -> ()
end;
let env_with_fn = Env.get_functions env in
let (typed_fn, env, fn_ty) = infer_fun env_with_fn level fun_ in
let env = Env.extend env fn_name fn_ty in
(*infer_exprs (Env.extend env var_name generalized_ty) level tail;*)
aux env level tail (typed_program @ [typed_fn])
| Class class_ :: tail ->
let (typed_class, env, class_ty) = infer_class env level class_ in
let (_, class_name) = class_.c_name in
let class_name = String.sub class_name 1 (String.length class_name - 1) in (* Strip leading \ (namespace thing) *)
let env = Env.extend env class_name class_ty in
Hashtbl.add typed_classes class_name typed_class;
(*Env.dump env;*)
aux env level tail (typed_program @ [typed_class])
| _ -> raise (Not_implemented "infer_program")
in
aux env level defs []
(**
* Small helper function to infer type of stmt list, which
* is of course unit, and update the env.
*
* @return Typedast.stmt list * env
*)
and infer_block env level (stmts : stmt list) : Typedast.block * Env.env =
let _env = ref env in
let typed_stmts = ref [] in
for i = 0 to List.length stmts - 1 do
let stmt = List.nth stmts i in
let (typed_stmt, env) = infer_stmt !(_env) level stmt in
_env := env;
typed_stmts := !typed_stmts @ [typed_stmt];
done;
!typed_stmts, !_env
(**
* Infer statement
* All statements return type TUnit
*
* @param env
* @param level int
* @param stmt stmt
* @return Typedast.program * env
*)
and infer_stmt (env : Env.env) level (stmt : stmt) : (Typedast.stmt * Env.env) =
(*Env.dump env;*)
match stmt with
| Expr expr ->
let (typed_expr, env, ty) = infer_expr env 0 expr in
(* All expressions in statements must return unit *)
unify TUnit ty;
Typedast.Expr (Typedast.TUnit, typed_expr), env
(*infer_stmts env level tail typed_stmts*)
| Noop ->
(Typedast.Noop, env)
| Block stmts ->
let (block, env) = infer_block env level stmts in
Typedast.Block block, env
| If (e, block1, block2) ->
let (typed_stmts_block1, env) = infer_block env level block1 in
let (typed_stmts_block2, env) = infer_block env level block2 in
let (typed_expr, env, e_ty) = infer_expr env level e in
(* If-clause most be bool *)
unify TBoolean e_ty;
Typedast.If (typed_expr, typed_stmts_block1, typed_stmts_block2), env
| For ((p, Expr_list [start]), (p2, Expr_list [end_]), (p3, Expr_list [step]), body) -> (* TODO: How to infer step? *)
(* Check so start contains an assignement *)
begin match start with
| p, Binop (Eq None, (p4, Lvar (p5, var_name)), _) ->
(try ignore (Env.lookup env var_name) with
| Not_found -> ());
(* This is quite irritating and not necessary.
if already_exists then failwith "For-loop variable must not already exist in environment";
*)
()
| _ ->
failwith "For start must have assignment, like $i = 0"
end;
let (typed_start, env, start_ty) = infer_expr env level start in
let (typed_end_, env, end_ty) = infer_expr env level end_ in
if end_ty != TBoolean then failwith "Condition in for loop must evaluate to true or false";
let (typed_step, env, step_ty) = infer_expr env level step in
let (typed_body, env) = infer_block env level body in
Typedast.For (typed_start, typed_end_, typed_step, typed_body), env
| For _ ->
failwith "Illegal form of for loop - only one expression in each block is allowed"
| Return (pos, expr_opt) ->
let open Env in
(match expr_opt with
| None ->
(match env.return_ty with
| Some ty ->
unify ty TUnit
| None ->
()
);
let env = {env with return_ty = Some TUnit} in
Typedast.Return (pos, None, Typedast.TUnit), env
| Some expr ->
let (typed_expr, env, return_ty) = infer_expr env level expr in
(match env.return_ty with
| Some ty ->
unify return_ty ty
| None ->
()
);
let env = Env.new_return_type env (Some return_ty) in
Typedast.Return (pos, Some typed_expr, ty_of_ty return_ty), env
)
| stmt -> raise (Not_implemented (sprintf "infer_stmt: %s" (show_stmt stmt)))
(**
* Create a typed fun from an untyped one
*
* @param env
* @param Ast.f_param list
* @return f_param list
*)
and create_typed_params env (f_params : Ast.fun_param list) =
printf "create_typed_params\n";
(*Env.dump env;*)
let rec aux (params : fun_param list) typed_params =
match params with
| [] ->
typed_params
| {param_id = (pos, name); param_hint = Some (pos2, (Happly ((pos3, "array"), [])))} :: params ->
let already_exists = try ignore (Env.lookup env name); true with
| Not_found -> false
in
if not already_exists then begin
raise (Infer_exception (sprintf "Array %s cannot be typed. Not used in function?" name))
end;
let ty = Env.lookup env name in
let typed_param = Typedast.({
param_id = (pos, name);
param_type = ty_of_ty ty;
}) in
aux params (typed_params @ [typed_param])
| {param_id = (pos, name); param_hint = Some (pos2, (Happly ((pos3, class_name), [])))} :: params ->
(* Check if class name exists in scope *)
let class_ty = try Env.lookup env class_name with
| Not_found -> raise (Infer_exception ("create_typed_params: found no class with name " ^ class_name))
in
let typed_param = Typedast.({
param_id = (pos, name);
param_type = ty_of_ty class_ty;
}) in
aux params (typed_params @ [typed_param])
| {param_id = (pos, name)} :: params ->
printf "name = %s" name;
let already_exists = try ignore (Env.lookup env name); true with
| Not_found -> false
in
let typed_param = if already_exists then begin
let ty = Env.lookup env name in
Typedast.({
param_id = (pos, name);
param_type = ty_of_ty ty;
})
end else begin
Typedast.({
param_id = (pos, name);
param_type = TUnknown;
})
end in
aux params (typed_params @ [typed_param])
in
aux f_params []
(**
* Infer type of function
*
* @param env Env with functions in same scope as this function
* @param level ?
* @param fun_
* @return Typedast.def * env * ty
*)
and infer_fun (env : Env.env) level fun_ : Typedast.def * Env.env * ty =
let open Env in
let param_list : (string * string option) list= List.map (fun param ->
(* Name of variable *)
let name = match param.param_id with
| _, name ->
name
in
(* Type-hint *)
let hint = match param.param_hint with
| None -> None
| Some (pos, Ast.Happly ((pos2, hint_string), [])) ->
Some hint_string
| Some _ ->
raise (Infer_exception "infer_fun: Unsupported type hint")
in
name, hint
) fun_.f_params in
let param_ty_list = List.map (fun (param_name, param_hint) ->
match param_hint with
(* No hint, so we need a new inferred variable *)
| None ->
new_var level
| Some "array" ->
let t = new_var level in
TDynamicSizeArray t
| Some hint ->
let object_ty = try Env.lookup env hint with
| Not_found -> raise (Infer_exception (sprintf "infer_fun: Found no class with name %s" hint))
in
object_ty
) param_list in
(* New scope for function *)
(* TODO: Global variables? Functions? *)
let empty_env = Env.empty in
let new_env = Env.merge env empty_env in
let fn_env = List.fold_left2
(fun env (param_name, param_hint) param_ty ->
Env.extend env param_name param_ty
)
new_env param_list param_ty_list
in
let body_expr = fun_.f_body in
(* Get the return type from the body of the function *)
let (typed_body_expr, fn_env) = infer_block fn_env level body_expr in
let return_type = match fn_env.return_ty with
| Some ty -> ty
| None -> TUnit
in
print_endline (sprintf "return type = %s" (show_ty return_type));
let typed_fn = Typedast.(Fun {
f_name = fun_.f_name; (* TODO: Fix pos *)
f_params = create_typed_params fn_env fun_.f_params;
f_ret = ty_of_ty return_type;
f_body = typed_body_expr;
}) in
(typed_fn, env, TArrow(param_ty_list, return_type))
(**
* Infer class
* Class can be struct
*
* @param env
* @param level ?
* @param class_
* @return Typedast.class * env * ty
*)
and infer_class (env : Env.env) level class_ : Typedast.def * Env.env * ty =
match class_ with
(*
[(Ast.Class
{ Ast.c_mode = FileInfo.Mpartial; c_user_attributes = []; c_final = true;
c_kind = Ast.Cnormal; c_is_xhp = false; c_name = (<opaque>, "\\Point");
c_tparams = []; c_extends = []; c_implements = [];
c_body = [Ast.ClassVars ([Ast.Public], None, [((<opaque>, "x"), None)]);
Ast.ClassVars ([Ast.Public], None, [((<opaque>, "y"), None)])];
c_namespace = { Namespace_env.ns_uses = <opaque>; ns_name = None };
c_enum = None })]
*)
| {c_final = true; c_body; c_name = (_, class_name)} when c_body_is_only_public c_body ->
let typed_struct_fields = c_body_to_struct c_body in
(* Extract field types from class body *)
let ty_fields = c_body_to_ty c_body in
Typedast.Struct {struct_name = class_name; Typedast.struct_fields = typed_struct_fields}, env, TStruct (class_name, ty_fields)
| _ ->
raise (Not_implemented ("This class type is not implemented: " ^ show_def (Class class_)))
(**
* Return true if class body contains only
* public fields
*
* @param c_body
* @return bool
*)
and c_body_is_only_public c_body =
List.for_all (fun class_elt ->
match class_elt with
| ClassVars ([Public], None, [(_, None)]) ->
true
| _ ->
false
) c_body
(**
* For a struct, take all fields
* and make them typed struct fields
*
* @param c_body
* @return (string, Typedast.ty) list - string = name of field
*)
and c_body_to_struct (c_body : class_elt list) =
List.map (fun class_elt ->
match class_elt with
| ClassVars ([Public], None, [((pos, name), None)]) ->
let none = ref None in
name, Typedast.TWeak_poly none (* Not known before-hand *)
| _ ->
failwith "Internal error: illegal class element"
) c_body
(**
* Convert c_body to a list of field ty
*
* @param c_body
* @return ty list
*)
and c_body_to_ty (c_body : class_elt list) =
List.map (fun field -> match field with
| ClassVars ([Public], None, [((p, field_name), None)]) ->
field_name, TUnresolved
| _ -> failwith "Not a struct"
) c_body
(**
* Converts a typed ast struct to ty (ast)
*
* @param s Typedast.Struct
* @return Ast.ty
*)
and typed_struct_to_ty s =
List.map (fun ty ->
()
) s
(**
* Infer types for an expression and return typed AST, new env and expr type
*
* @param env
* @param level int
* @param exprs expr list
* @return Typedast.expr * env * ty
*)
and infer_expr (env : Env.env) level expr : Typedast.expr * Env.env * ty =
(*Env.dump env;*)
match expr with
| p, True ->
(p, Typedast.True), env, TBoolean
| p, False ->
(p, Typedast.False), env, TBoolean
| p, String (pos, str) ->
(p, Typedast.String (pos, str)), env, TString
| p, String2 (pos, str) ->
failwith "Only strings with '' are supported"
| p, Int (pos, pstring) ->
(p, Typedast.Int (pos, pstring)), env, TNumber
| p, Float (pos, pstring) ->
(p, Typedast.Float (pos, pstring)), env, TNumber
(* Array, like $arr = [], that is init dynamic array *)
| p, Array [] ->
(* Use TWeak_poly *)
let array_type = Typedast.TDynamicSizeArray (Typedast.TWeak_poly (ref None)) in
let t = new_var level in
(p, Typedast.Array ([], array_type)), env, TDynamicSizeArray t
(* Array, like [1, 2, 3] *)
| p, Array array_values ->
(* Check for empty list and abort *)
let length = List.length array_values in
if length = 0 then
raise (Infer_exception (sprintf "No support for empty list yet at %s" (get_pos_msg p)));
let first_element = List.nth array_values 0 in
let first_element_ty = match first_element with
| Ast.AFvalue (pos, first_element_ty) ->
let _, _, ty = infer_expr env level (p, first_element_ty) in
ty
| Ast.AFkvalue _ -> failwith "What is AFkvalue?"
in
print_endline (show_ty first_element_ty);
(* Make sure all array values have same type *)
let have_same_type = List.for_all (fun el ->
print_endline (show_afield el);
match el, first_element_ty with
(* TODO: Have to manually insert what types to support - any other way? *)
| Ast.AFvalue (pos, Ast.Int _), TNumber -> true
| Ast.AFvalue (pos, Ast.Lvar (_, var_name)), TStruct (struct_name, struct_fields) ->
let var_type = Env.lookup env var_name in
begin match var_type with
| TStruct (struct_name2, _) -> struct_name = struct_name2
| _ -> assert false
end
| _ -> false
) array_values in
(* Abort if types differ *)
if not have_same_type then
raise (Infer_exception (sprintf "Values in an array must have same type at %s" (get_pos_msg p)));
let inferred_values = List.map (fun el ->
match el with
| AFvalue expr ->
let result_expr, env, ty = infer_expr env level expr in
Typedast.AFvalue result_expr
| AFkvalue _ -> failwith "What is indeed AFkvalue?"
) array_values
in
(*let (inferred_array, env, inferred_ty) = infer_expr env level (pos3, Ast.Array array_values) in*)
let typed_ty = ty_of_ty first_element_ty in
let array_ty = Typedast.TFixedSizeArray (length, typed_ty) in
(p, Typedast.Array (inferred_values, array_ty)), env, TFixedSizeArray (length, first_element_ty)
(*inferred_values, env, TNumber*)
(* Like $a[1] *)
| p, Array_get ((pos1, (Lvar (pos2, array_var_name))), (Some (pos3, (Int (pos4, index_string))))) ->
(* Check so array is fixed size *)
let array_type = try Env.lookup env array_var_name with
| Not_found -> raise (Infer_exception (sprintf "Tried to get from array, but array is not defined: %s" (get_pos_msg p)))
in
let index = int_of_string index_string in
(* array_type can be, e.g., TFixedSizeArray (3, TNumber) *)
begin match array_type with
| TFixedSizeArray (array_length, element_type) ->
(* With fixed-size array (tuple) we can check out-of-bound *)
if index + 1 > array_length then
raise (Infer_exception (sprintf "Array index out of bound: %s" (get_pos_msg p)));
let typed_lvar, env, level = infer_expr env level (pos1, (Lvar (pos2, array_var_name))) in
(p, Typedast.ArrayFixedSize_get (typed_lvar, (pos3, Typedast.Number (float_of_int index)), ty_of_ty element_type)), env, element_type
| TDynamicSizeArray element_type ->
let typed_lvar, env, level = infer_expr env level (pos1, (Lvar (pos2, array_var_name))) in
(p, Typedast.ArrayDynamicSize_get (typed_lvar, (pos3, Typedast.Number (float_of_int index)), ty_of_ty element_type)), env, element_type
| _ ->
raise (Infer_exception (
sprintf "Tried to access variable %s as an array, but don't know which kind of array it is. Did you type-hint function argument as array? %s"
array_var_name
(get_pos_msg p))
)
end;
(* Like $a[$i] *)
| p, Array_get ((pos1, (Lvar (pos2, array_var_name))), (Some index_expr)) ->
(* Check so array is fixed size *)
let array_type = try Env.lookup env array_var_name with
| Not_found -> raise (Infer_exception (sprintf "Tried to get from array, but array is not defined: %s" (get_pos_msg p)))
in
(* array_type can be, e.g., TFixedSizeArray (3, TNumber) *)
begin match array_type with
| TFixedSizeArray (array_length, element_type) ->
let index_typed_expr, env, ty = infer_expr env level index_expr in
let typed_lvar, env, ty = infer_expr env level (pos1, (Lvar (pos2, array_var_name))) in
(p, Typedast.ArrayFixedSize_get (typed_lvar, index_typed_expr, ty_of_ty element_type)), env, element_type
| TDynamicSizeArray _ ->
raise (Not_implemented (sprintf "Dynamicall sized arrays not not yet implemented: %s" (get_pos_msg p)))
| what ->
raise (Infer_exception (sprintf "Don't know what array type it is at %s" (get_pos_msg p)));
end;
(* Unary minus *)
| p, Unop (Uminus, (pos1, (Float (pos2, pstring)))) ->
(p, Typedast.Float (pos2, "-" ^ pstring)), env, TNumber
| p, Unop (Uminus, (pos1, (Int (pos2, pstring)))) ->
(p, Typedast.Float (pos2, "-" ^ pstring)), env, TNumber
| p, Unop (Uminus, expr) ->
let (typed_expr, env, e_ty) = infer_expr env level expr in
unify TNumber e_ty;
(p, Typedast.Unop (Typedast.Uminus typed_expr, Typedast.TNumber)), env, TNumber
(* > *)
| p, Binop (Gt, lexpr, rexpr) ->
let (typed_lexpr, _, lexpr_ty) = infer_expr env (level + 1) lexpr in
let (typed_rexpr, _, rexpr_ty) = infer_expr env (level + 1) rexpr in
(* Check so that left hand and right hand are numeric *)
unify lexpr_ty TNumber;
unify rexpr_ty TNumber;
(p, Typedast.Binop (Typedast.Gt, typed_lexpr, typed_rexpr, Typedast.TBoolean)), env, TBoolean
(* < *)
| p, Binop (Lt, lexpr, rexpr) ->
let (typed_lexpr, _, lexpr_ty) = infer_expr env (level + 1) lexpr in
let (typed_rexpr, _, rexpr_ty) = infer_expr env (level + 1) rexpr in
(* Check so that left hand and right hand are numeric *)
unify lexpr_ty TNumber;
unify rexpr_ty TNumber;
(p, Typedast.Binop (Typedast.Lt, typed_lexpr, typed_rexpr, Typedast.TBoolean)), env, TBoolean
(* === *)
| p, Binop (EQeqeq, (lpos, lexpr), (rpos, rexpr)) ->
let (typed_lexpr, _, lexpr_ty) = infer_expr env (level + 1) (lpos, lexpr) in
let (typed_rexpr, _, rexpr_ty) = infer_expr env (level + 1) (rpos, rexpr) in
(* Check so that left hand and right hand are the same type *)
unify lexpr_ty rexpr_ty;
(p, Typedast.Binop (Typedast.EQeqeq (ty_of_ty lexpr_ty), typed_lexpr, typed_rexpr, Typedast.TBoolean)), env, TBoolean
(* += and -= *)
| p, Binop (Eq (Some Plus as op), (pos_lvar, Lvar (pos_var_name, var_name)), value_expr)
| p, Binop (Eq (Some Minus as op), (pos_lvar, Lvar (pos_var_name, var_name)), value_expr) ->
let already_exists = try ignore (Env.lookup env var_name); true with
| Not_found -> false
in
(* Left hand-side must already be defined for this operator to work *)
if already_exists then begin
(* Check so left hand-side is a number *)