-
Notifications
You must be signed in to change notification settings - Fork 125
/
qmlAst.ml
executable file
·1429 lines (1195 loc) · 48.5 KB
/
qmlAst.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 © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA 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 Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(*
Authors: 2006 - 2009, Henri Binsztok <Henri.Binsztok@mlstate.com>
2009, Vincent Benayoun <Vincent.Benayoun@mlstate.com>
2009, Mikolaj Konarski <Mikolaj.Konarski@mlstate.com>
2009, Mathieu Barbin <Mathieu.Barbin@mlstate.com>
2009, Louis Gesbert <Louis.Gesbert@mlstate.com>
2009, Mehdi Bouaziz <Mehdi.Bouaziz@mlstate.com>
2009, David Rajchenbach-Teller <David.Teller@mlstate.com>
*)
(**
The complete AST for QML.
*)
(*
The purpose is that the module who have to deal with AST should only do a 'open QmlAst'
- interfaces bring lot of work on maintainability, so let's try to reduce the number of it
- but then, the construction of type (actually, this is like an implementation) is hidden
by having the type in the first level of this 100% shared file
This file has no mli, because it mostly contains type declaration,
and few implementation, then the mli would be the same (duplication)
*)
(* depends *)
module String = BaseString
module List = BaseList
(* aliases *)
module QTV = QmlTypeVars
(* shorthands *)
module TypeVar = QmlTypeVars.TypeVar
module RowVar = QmlTypeVars.RowVar
module ColVar = QmlTypeVars.ColVar
(**
A literal constant
*)
type const_expr =
| Int of int (** simplify the life in opa / ml code generator *)
| Float of float
| String of string
| Char of char
(**
The type of a simple value.
Please keep them sorted alphabetically, for tests and display.
*)
type const_ty =
| TyChar
| TyFloat
| TyInt
| TyNull (** empty type: nothing should really have this type *)
| TyString
(** build_meta will also be moved *)
module Const =
struct
(** Return the type of a simple value.*)
let type_of = function
| Int _ -> TyInt
| Float _ -> TyFloat
| String _ -> TyString
| Char _ -> TyChar
(** Return a human-readable version of a simple value.*)
let string_of_expr = function
| Int i -> string_of_int i
| Float f -> string_of_float f
| String s -> Printf.sprintf "%S" s
| Char c -> Printf.sprintf "%C" c
let pp_expr fmt expr = Format.pp_print_string fmt (string_of_expr expr)
(** Return a human-readable version of a simple type.*)
let string_of_ty = function
| TyInt -> "int"
(** if we reswitch to Int64.t, to preserve confusion in declaration of type (e.g. by using register directives in libbsl) it is preferable to print int64 instead of int *)
| TyFloat -> "float"
| TyString -> "string"
| TyChar -> "char"
| TyNull -> "null"
let pp_ty fmt ty = Format.pp_print_string fmt (string_of_ty ty)
(** Return the OCaml type corresponding to this simple type.
Used to generate OCaml source code.*)
let ocamlbsl_string_of_ty = function
(* | TyInt -> "Int64.t" *)
| t -> string_of_ty t
(** Return the C type corresponding to this simple type.
Used to generate C source code. *)
let cbsl_string_of_ty = function
| TyInt -> "long long int"
| TyFloat -> "double"
| TyString -> "char *"
| TyChar -> "char"
| TyNull -> assert false (* "void *" ?? *)
(** meta : keep it not too far from the definition *)
let meta = function
| TyInt -> "TyInt"
| TyFloat -> "TyFloat"
| TyString -> "TyString"
| TyChar -> "TyChar"
| TyNull -> "TyNull"
end
(** This module contains the structures for DB definitions,
and unpreprocessed paths *)
module Db =
struct
(** The kind of a path, ie the type of access it is *)
type kind =
| Option
(** Check if the path exists in the database, and return the lazy-read as
option if it does. Errors while reading are still filled with
default values *)
| Default
(** Normal read (any error/missing data returns a default value) *)
| Valpath
(** Firstclass value-path, does not read right away, but bound to the
current state of the db *)
| Ref
(** No operation is done, just build a pointer to the database. used for
operations on paths (like writes) *)
(** The paths as specified in definitions (eg. with possible definition of keys) *)
type path_decl_key =
| Decl_fld of string
(** A field name, for standard /db/xxx paths *)
| Decl_int
(** Declares an intmap *)
| Decl_string
(** Declares a string map *)
| Decl_set of string list list
(** Declares a set with unicity determined by list of keys
(empty means whole element) *)
type path_decl = path_decl_key list
(** The extensible type for DB definitions (the "database xxx" level 0
directive). For now, only used to specify the data storage location (any
path, absolute or relative, ending with the filename prefix). DbGen sets
this to [~/.mlstate/<progname>/default] by default (based on argv.(0)) *)
type engine =
[
|`db3 of string option (* path *)
|`meta
|`client of string option * int option (* server:port *)
]
type options =
[
|`engine of engine
|`mountpoint of string list
]
type 'expr db_constraint = (** The type of DB constraints as specified by the user. To be extended *)
(* /!\ this is WIP, and most of it is dummy for now *)
| C_Ordering of 'expr
(** For maps: give an ordering relationship for returned results
(expr is of type [tkeys -> tkeys -> int] if the type stored at the path is [map(tkeys,'a)]) *)
| C_Inclusion of path_decl
(** Db_Constraint(p1, Db_Inclusion p2) means that any path satisfying p1 is an element of
the set pointed to by path p2 *)
| C_Validation of 'expr
(** Provide a validation function to run on the data before writing it at the given path
(expr is of type [typath -> bool], where typath is the type stored at the path) *)
| C_Inverse of path_decl
(** cross-reference between the elements of two paths and their
respective parents, for example [/authors[]/biblio] and
[/books[]/authors], meaning that for any element of [/books] [B] and any
element of [/authors] [A], [B] \in [A/biblio] is equivalent to [A] \in
[B/authors] *)
| C_Private
(** marks a path as private: accesses will only be allowed on the full path,
not directly on children. Implies that the sub-tree can't contain partial
records. This constraint is propagated to all sub-nodes *)
type ('expr,'ty) db_def =
| Db_TypeDecl of path_decl * 'ty
| Db_Default of path_decl * 'expr
| Db_Alias of path_decl * path_decl
| Db_Constraint of path_decl * 'expr db_constraint
| Db_Virtual of path_decl * 'expr
let path_kind_to_string = function
| Default -> "" | Option -> "?" | Valpath -> "!" | Ref -> "@"
let engine_to_string opt =
match opt with
| `db3 s -> "@local(" ^ Option.default "" s ^ ")"
| `meta -> "@meta"
| `client (h, p) ->
let h = Option.default "" h in
let p = match p with None -> "" | Some p -> ":" ^ string_of_int p in
"@shared(" ^ h ^ p ^ ")"
let options_to_string opts =
String.concat_map " "
(function
| `engine s -> engine_to_string s
| `mountpoint _sl -> ""
) opts
let path_decl_key_to_string = function
| Decl_fld s -> "/"^s
| Decl_int -> "[_]" (* "[]" in qml. This is only valid for default defs. Should be "[int]" once available in OPA *)
| Decl_string -> "[_]" (* same remark, should be "[string]" *)
| Decl_set [] -> "[_]"
| Decl_set l ->
String.concat_map "; " (fun l -> "[{"^ String.concat ", " l ^"}]") l
(* not yet usable *)
let path_decl_to_string = String.concat_map "" path_decl_key_to_string
let constraint_to_string _e_t_s = function
| C_Private -> "full"
| _ -> "[[database constraint]]" (* todo (but doesn't have a syntax yet) *)
let def_to_string e_t_s ty_t_s = function
| Db_TypeDecl (pd,ty) -> Printf.sprintf "db %s : %s" (path_decl_to_string pd) (ty_t_s ty)
| Db_Alias (pd,pd') -> Printf.sprintf "db %s alias %s" (path_decl_to_string pd) (path_decl_to_string pd')
| Db_Default (pd,e) -> Printf.sprintf "db %s = %s" (path_decl_to_string pd) (e_t_s e)
| Db_Constraint (pd,cstr) -> Printf.sprintf "db (%s %s)" (path_decl_to_string pd) (constraint_to_string e_t_s cstr)
| Db_Virtual (pd,e) -> Printf.sprintf "db %s := %s" (path_decl_to_string pd) (e_t_s e)
let print_constraint _p_e out = function
| C_Private -> Format.fprintf out "full"
| _ -> Format.fprintf out "<FIXME database constraint>"
let print_def p_e p_ty out = function
| Db_TypeDecl (pd,ty) ->
Format.fprintf out "@[<hv 2>db %s :@ %a@]" (path_decl_to_string pd) p_ty ty
| Db_Alias (pd,pd') ->
Format.fprintf out "@[<hv 2>db %s :@ %s@]" (path_decl_to_string pd) (path_decl_to_string pd')
| Db_Default (pd,e) ->
Format.fprintf out "@[<hv 2>db %s =@ %a@]" (path_decl_to_string pd) p_e e
| Db_Constraint (pd,cstr) ->
Format.fprintf out "@[db (%s %a)@]" (path_decl_to_string pd) (print_constraint p_e) cstr
| Db_Virtual (pd, e) ->
Format.fprintf out "@[db %s : = %a@]" (path_decl_to_string pd) p_e e
module TU = Traverse.Utils
let sub_db_constraint sub_e _sub_ty = function
| C_Ordering e ->
TU.wrap (fun e -> C_Ordering e) (sub_e e)
| C_Inclusion p ->
TU.wrap (fun e -> C_Inclusion e) (TU.sub_ignore p)
| C_Validation e ->
TU.wrap (fun e -> C_Validation e) (sub_e e)
| C_Inverse p ->
TU.wrap (fun e -> C_Inverse e) (TU.sub_ignore p)
| C_Private as x ->
TU.sub_ignore x
let sub_db_def sub_e sub_ty = function
| Db_TypeDecl (p,ty) ->
TU.wrap (fun (p,ty) -> Db_TypeDecl (p,ty)) (TU.sub_2 TU.sub_ignore sub_ty (p,ty))
| Db_Alias (p,p') ->
TU.wrap (fun (p,p') -> Db_Alias (p,p')) (TU.sub_2 TU.sub_ignore TU.sub_ignore (p,p'))
| Db_Default (p,e) ->
TU.wrap (fun (p,e) -> Db_Default (p,e)) (TU.sub_2 TU.sub_ignore sub_e (p,e))
| Db_Constraint (p,c) ->
TU.wrap (fun (p,c) -> Db_Constraint (p,c)) (TU.sub_2 TU.sub_ignore (sub_db_constraint sub_e sub_ty) (p,c))
| Db_Virtual (p,e) ->
TU.wrap (fun (p,e) -> Db_Virtual (p,e)) (TU.sub_2 TU.sub_ignore sub_e (p,e))
let foldmap_expr f acc dbdef =
let cons, subs = sub_db_def TU.sub_current TU.sub_ignore dbdef in
let acc, subs = List.fold_left_map f acc subs in
acc, cons subs
let foldmap_ty f acc dbdef =
let cons, subs = sub_db_def TU.sub_ignore TU.sub_current dbdef in
let acc, subs = List.fold_left_map f acc subs in
acc, cons subs
end
(**
Sometimes, modules redefines locally a module Ident.
This type is for referencing the ident used in QML lang,
which are standard [Ident.t]
*)
type ident = Ident.t
(**
This module defines a mechanism for type variables. This mechanism
is used for actual type variables (e.g. ['a], ['b], etc.) but also
for row variables and for column variables.
These are just synonyms for the fresh generation module (defined in
"fresh.ml").
*)
module TypeIdent :
sig
type tag = Concrete | Extern
type uniq
type t = private
| Raw of Ident.t
| Processed of uniq
(** to be used in printers *)
val to_printable_string : t -> string
(** gives back the original name of the type *)
val to_string : t -> string
val to_debug_string : t -> string
val of_string : ?check:bool -> string -> t (* should be used only by the parser *)
val of_ident : Ident.t -> t
val new_concrete : t -> t
val new_external_ty : t -> t
val is_already_known : t -> bool
val is_external_ty : t -> bool
val compare_names : t -> t -> int
val equal_names : t -> t -> bool
val compare : t -> t -> int
val equal : t -> t -> bool
val hash : t -> int
end =
struct
(* We use a distinction between abstract and extern in order
to do the right cast with the data in qmltoplevel.
For others application, there is no distinction *)
type tag = Concrete | Extern
type uniq = tag * Ident.t
type t = Raw of Ident.t | Processed of uniq
let is_already_known = function
| Raw _ -> false
| Processed _ -> true
let to_debug_string id =
let tag_to_string = function
| Concrete -> ""
| Extern -> "(ext)"
in
match id with
| Raw x -> Printf.sprintf "``_ty_raw_%s" (Ident.to_string x)
| Processed (t, i) -> Printf.sprintf "``_ty_%s_%s" (Ident.to_string i) (tag_to_string t)
(*
We test strictly than the of_string function is called only on
identifiers registred in opacapi, using the opacapi interface.
The physical test asserts than the string corresponding to the
identifier is not duplicated in the code containing the insertion.
*)
let opacapi_check s =
let is_in_opacapi =
try
let ss = Hashtbl.find Opacapi.table s in
s == ss
with
| Not_found -> false
in
if not is_in_opacapi
then (
OManager.printf "OPACAPI violation, on ident %S@\n" s;
OManager.printf "You should use opacapi for inserting types from the stdlib@.";
assert false
)
let to_string = function
| Raw x
| Processed (_,x) -> Ident.original_name x
let to_printable_string = to_string
let of_string ?(check=true) name =
if check then opacapi_check name;
Raw (Ident.source name)
let of_ident ident = Raw ident
(* ************************************************************************ *)
(** {b Visibility}: Not exported outside this module. *)
(* ************************************************************************ *)
let new_ident tag = function
| Raw name -> Processed (tag, name)
| Processed (_, i) -> Processed (tag, i)
let new_concrete id = new_ident Concrete id
let new_external_ty id = new_ident Extern id
let is_external_ty = function
| Raw _ ->
(* The identifier should have already been please processed. *)
assert false
| Processed (t, _) -> t = Extern
(* TODO-REFACT: when TypeIdent won't have no more Processed | Raw
constructor, this must be removed and replaced by a simple
Ident.compare. *)
let compare_names x y =
match x, y with
| Processed (_, x), Processed (_, y) -> Ident.compare x y
| Raw x, Processed (_, y)
| Processed (_, x), Raw y
| Raw x, Raw y -> Ident.compare x y
(* FIXME this comparison is completely crazy, it defines an equality that
* is not transitive! *)
(* TODO-REFACT: when TypeIdent won't have no more Processed | Raw
constructor, this must be removed and replaced by a simple
Ident.compare. *)
let compare x y =
match x, y with
| Processed (_, i), Processed (_, j) -> Ident.compare i j
| Raw x, Processed (_, y)
| Processed (_, x), Raw y
| Raw x, Raw y -> Ident.compare x y
(* TODO-REFACT: compare is the same thing than compare_names. Remove one.
equal is the same thing than equal_names. Remove one. *)
let equal x y = compare x y = 0
let equal_names x y = compare_names x y = 0
(*
<!> keep the following property :
equal(t1, t2) => hash(t1) = hash(t2)
*)
let hash = function
| Processed (_, i)
| Raw i ->
Ident.hash i
end
(**
Options for types definitions, from toplevel directives.
Extensible, using {[{ QmlAst.ty_def_options with }]} syntax.
The warn_x_field is used until we have at least 2 infos.
*)
type ty_def_options = {
opacapi : bool ;
warn_x_field : unit ;
}
(**
The default value for type definitions options.
Used for default value for easyer extensibility of
the options in [ty_def_options]
*)
let ty_def_options = {
opacapi = false ;
warn_x_field = () ;
}
let pp_ty_def_options fmt opt =
let () =
if opt.opacapi
then Format.pp_print_string fmt "@opacapi "
in
()
type typeident = TypeIdent.t
type ty =
| TypeConst of const_ty
| TypeVar of typevar
| TypeArrow of ty list * ty (**The type of a function.*)
| TypeRecord of ty_row
| TypeSum of ty_col
| TypeSumSugar of ty list
(**Inelegant duplication; used to allow type names in sums; transformed later by the typer to regular TypeSum.
Possible fix: differently typed ASTs at different stages to handle raw & preprocessed types*)
| TypeName of ty list * typeident
| TypeAbstract
(**represents *only* the keyword "abstract" or "extern" in the AST *)
| TypeForall of typevar list * rowvar list * colvar list * ty
and fields = (string * ty) list
and ty_row =
| TyRow of fields * rowvar option
and ty_col =
| TyCol of fields list * colvar option
and typevar = TypeVar.t
and rowvar = RowVar.t
and colvar = ColVar.t
let typeNull : ty = TypeConst TyNull
let column_to_records : ty_col -> ty list =
function (TyCol (l, _)) ->
let constant_row_to_record l = TypeRecord (TyRow (l, None)) in
List.map constant_row_to_record l
module EqualsTy : sig
type t = ty
(** comparisons without environment, so without unfolding of named types *)
val equal : ty -> ty -> bool
val compare : ty -> ty -> int
(** comparison modulo alpha-renaming of typevars, rowvars, colvars
* if the [absorb] flag is on, it means that [Some row or col var] is equal to [None]
(but if row or col vars are present on both sides, they must be consistent)
* if the [collapse] flag is on, then distinct variables from the first type are allowed
to be collapsed in the second one (warning this option is not symmetric)
* if the [byname] flag is on, the more permissive comparison by name is used on named types
*)
val equal_alpha : ?collapse:bool -> ?absorb:bool -> ?byname:bool -> ty -> ty -> bool
val compare_alpha : ?collapse:bool -> ?absorb:bool -> ?byname:bool -> ty -> ty -> int
end =
struct
type t = ty
let compare_gen cmp_ti cmp_tv cmp_rv cmp_cv =
let rec compare t1 t2 =
if t1 == t2 then 0 (* speedup *) else
match t1, t2 with
| TypeConst c1, TypeConst c2 -> Pervasives.compare c1 c2
| TypeVar v1, TypeVar v2 -> cmp_tv v1 v2
| TypeVar _, _ -> 1
| _, TypeVar _ -> -1
| TypeArrow (lt11,t12), TypeArrow (lt21,t22) ->
let c = List.make_compare compare lt11 lt21 in
if c <> 0 then c else
compare t12 t22
| TypeRecord (TyRow (tfs,tv)), TypeRecord (TyRow (ufs, uv)) ->
let c = cmp_rv tv uv in
if c <> 0 then c else compare_fields tfs ufs
| TypeName (tel1, n1), TypeName (tel2, n2) ->
let c = cmp_ti n1 n2 in
if c <> 0 then c else
List.make_compare compare tel1 tel2
| TypeSum (TyCol (ts1, cv1)), TypeSum (TyCol (ts2, cv2)) ->
let c = cmp_cv cv1 cv2 in
if c <> 0 then c else List.make_compare compare_fields ts1 ts2
| TypeSumSugar ts1, TypeSumSugar ts2 ->
List.make_compare compare ts1 ts2
(* some unpleasant overlap between TypeRecord and TypeSum *)
| TypeRecord (TyRow (tfs, None)), TypeSum (TyCol ([ufs], None)) ->
compare_fields tfs ufs
| TypeSum (TyCol ([ufs], None)), TypeRecord (TyRow (tfs, None)) ->
compare_fields ufs tfs
(* different root constructors, so unequal by Pervasives.compare *)
(* This fix may sux if explicitely quantified typevar are not unique *)
(*| TypeForall(_,_,_,t1), TypeForall( _, _, _,t2) -> compare t1 t2*)
| _ -> Pervasives.compare t1 t2
and compare_fields f1 f2 =
let cmp_field (tfn, tft) (ufn, uft) =
let c = Pervasives.compare tfn ufn in
if c <> 0 then c
else compare tft uft
in
List.make_compare cmp_field f1 f2
in compare
let compare = compare_gen TypeIdent.compare TypeVar.compare (Option.make_compare RowVar.compare) (Option.make_compare ColVar.compare)
let equal t1 t2 = (compare t1 t2 = 0)
let compare_alpha ?(collapse=false) ?(absorb=false) ?(byname=false) t1 t2 =
let cmp_ti = if byname then TypeIdent.compare_names else TypeIdent.compare in
let hashtbl_size = 8 in
let option_make =
if absorb
then fun cmp v1 v2 -> Option.default 0 (Option.map2 cmp v1 v2)
else Option.make_compare in
let cmp_factory =
if collapse
then fun cmp ->
let h = Hashtbl.create hashtbl_size in
fun v1 v2 ->
try let w = Hashtbl.find h v1 in cmp w v2
with Not_found -> Hashtbl.add h v1 v2; 0
else fun cmp ->
let h1 = Hashtbl.create hashtbl_size in
let h2 = Hashtbl.create hashtbl_size in
fun v1 v2 ->
try let w = Hashtbl.find h1 v1 in cmp w v2
with Not_found -> Hashtbl.add h1 v1 v2;
if Hashtbl.mem h2 v2 then cmp v1 v2 else (Hashtbl.add h2 v2 v1; 0)
in
let cmp_tv = cmp_factory TypeVar.compare
and cmp_rv = cmp_factory (option_make RowVar.compare)
and cmp_cv = cmp_factory (option_make ColVar.compare) in
compare_gen cmp_ti cmp_tv cmp_rv cmp_cv t1 t2
let equal_alpha ?(collapse=false) ?(absorb=false) ?(byname=false) t1 t2 =
compare_alpha ~collapse ~absorb ~byname t1 t2 = 0
end
type annotmap = ty QmlAnnotMap.gen_annotmap
type pat_rowvar = [
| `closed
(**
represent the absence of dots in the pattern, as in:
{[
| { foo = bar }
]}
*)
| `open_
(**
represent the dots in the pattern, as in:
{[
| { foo = bar ; ... }
]}
*)
(*
| `autocomplete
(**
represent the syntax '_' for asking the
typer to complete the pattern.
*)
*)
]
type pat =
| PatRecord of Annot.label * (string * pat) list * pat_rowvar
| PatConst of Annot.label * const_expr
| PatVar of Annot.label * Ident.t
| PatAny of Annot.label
| PatCoerce of Annot.label * pat * ty
| PatAs of Annot.label * pat * Ident.t
(*
?) PatOr
let x = ... in match x with
| truc
| machin
| bidule
| a_lot_of_patterns
-> do a_big_bloc_of_code()
it will be represented into a lot of sames big blocs of codes... (it sux)
?) guard
match x with
| 0 -> 4
| x when mod(x, 2) == 0 -> 5
| 3 -> 42
| _ -> 5 / 0
we cant do this, because "when" is not possible with this kind of pattern.
*)
(** {6 Directives} *)
type tagged_string_kind =
| Rpc_use | Rpc_def
| Type_def | Type_use
| Client_closure_use
(** Types directive *)
type type_directive = [
| `coerce
(**
Coercion. Currently unused. Should replace Coerce from the AST
*)
| `opensums
(**
Adds deeply column variables to allow subtyping of sum types
*)
| `openrecord
(**
Adds a row variable to allow subtyping of record types
*)
| `module_
(**
Indicate that the underlying record is a module, for type generalization purpose
*)
| `module_field_lifting
(** Indicates that the embedded expression was initially a module field
that has been extracted from the module and transformed into an
expression bound at the same level than the module to be used later
to really create the expression of module.
[SurfaceAstDependencies.rewrite_module] takes each field of a module
and transform it into a let-definition with the same name than the
field. Then, once done for all the fields, the module expression is
rebuilt by setting each field to its corresponding artificial
let-definition.
This transformation pass must signal the let-definitions it creates
this way by embedding it into the present directive. Hence, normally
this directive should only host [QmlAst.LetIn] expressions kind.
Being able to track these let-definitions is required to remind that
type variables introduced by explicit type annotations in a module
field must be generalizable at the exit of the definition of this
field. Because the transformation transforms fields into
let-definitions, without the directive, it is impossible to know that
type variables of a let-definitions can be generalized at the exit of
the definition and not at the exit of the current toplevel phrase. *)
| `unsafe_cast
(**
As [Obj.magic]
*)
| `nonexpansive
(**
Tell the value restriction pass to generalize.
*)
| `warncoerce
(**
Used to check after typing that some expressions have an expected types.
In case the expression has not the given type, the pass_WarnCoerce raise
a warning of the class typer.warncoerce. This class can be turned on --warn-error.
Essentially used for checking that an expression after a 'do' should have type void.
*)
]
(**
Directives inserted by the new slicer
(it doesn't use any directives)
*)
type simple_slicer_directive =
[ `ajax_call of [`sync | `async]
(** remote call client -> server *)
| `ajax_publish of [`sync | `async]
(** only appear at the top of an expression in a newval
indicate that a server-side stub should be produced for the binding
*)
| `comet_call (** symmetric directive to `ajax_call *)
| `comet_publish(** symmetric directive to `ajax_publish *)
| `insert_server_value of Ident.t
(** only appears at the top of an expression in a newval in the client code
the ident in the variant is a server-side identifier
indicate that the expression on the client should be replaced at server
startup by the value of the server identifier
*)
| `sliced_expr ] (** see the description in surfaceAst *)
type slicer_directive =
[ `side_annotation of [ `server
| `client
| `both
| `prefer_server
| `prefer_client
| `prefer_both
| `both_implem ]
| `visibility_annotation of [ `public of [`sync | `async | `funaction] | `private_ ]
]
(** Fun actions *)
type fun_action_content =
| Deserialize
| Client_id
type fun_action_directive = [
| `fun_action of fun_action_content option
(** put by the parser in the ast at, for instance: <div onclick={@fun_action(some_expr)}/>
(except that actually the magicFunAction is under the @fun_action *)
]
(** Thread context *)
type thread_context_directive = [
| `thread_context (** Returns the context stored in the current continuation *)
| `with_thread_context (** Update the context stored in the current continuation *)
]
(**
Coding directives
*)
type coding_directive = [
| `deprecated
| `todo
]
(** CPS and concurrency *)
type cps_directive = [
| `spawn (**Spawn a task for concurrent execution. Handled by CPS transform.*)
| `wait (**Wait until a task has completed. Handled by CPS transform.*)
| `callcc (** \@callcc( fun k -> ApplyCont (k, e) ) returns e. \@uncps is too restrictive, and is deprecated. *)
| `atomic (**Specify that an expression should be executed in one go, without interruptions. Handled by CPS transform.*)
| `immovable (**Specify that an expression should be executed only where it was spawned. Handled by CPS transform.*)
| `cps_stack_lambda of (Obj.t as 'il_cident) option ref (** used to mark the continuation of a lambda
appears exclusively right under a lambda *)
| `cps_stack_apply of ((Obj.t as 'il_cident) option ref option * string option * string option)
(** used to mark applications where the stack needs to be updated
the first element is the continuation of the closest enclosing lambda (marked with @cps_stack_lambda)
(if any)
the second element is the name of called function if any
the third element is the position of the application, if any
*)
| `asynchronous_toplevel
(**
Tag for using partially the toplevel-concurrency mode when the option is not activated.
As we have no way to put directives at toplevel in the AST, we count on the luck and the
great passes magical spirit for the directive to stay arround the expression associated
to the binded identifier in a NewVal.
*)
| `may_cps
(**
For using the cps_bypass instead of a bypass
*)
| `apply_cont
(**
Indicate that the underlying apply was translated in qml from an ApplyConst in IL
Used by the js backend to know when to treat tail calls
*)
]
(** Exceptions. Unsafely typed for now, only for experimentation purposes *)
type exception_directive = [
| `throw (** Takes an expression, evaluates it and raises it as an exception *)
| `catch (** Takes a function of exceptions, and an expression to evaluate. Any
exception thrown during this evaluation and not already caught is
passed directly to the function given, and the result of this
application is the return value of the directive *)
]
type deprecated_directive = [
| `box
| `unbox
| `unbox_option
| `enrich
| `eval
]
(** Used for extend magic function. *)
type opavalue_directive = [
| `stringifier (** Extend OpaValue.to_string *)
| `comparator (** Extend OpaValue.compare *)
| `serializer (** Extend serialization system *)
| `xmlizer (** Extend Xml.of_alpha (ie magic_xml)*)
]
type doctype_access_directive =
[ `private_ (* visible only in the current module *)
| `public (* visible to everyone *)
| `package (* visible only in the current package *)
]
(*
If you want to add a new directive, it should appear:
- in qmlPrinters line ~650 if the variant holds some data line
- in qmlPrinters line ~400 in any case
- in qmlAstCons.Directive
- in qmlflat/qmlFlatCompiler
- qmltop/qmlTopEval
Expression in directive are no more allowed.
You will use the Nary support for such directives.
*)
type qml_directive = [
| cps_directive
| coding_directive
| exception_directive
| fun_action_directive
| simple_slicer_directive
| slicer_directive
| thread_context_directive
| type_directive
| deprecated_directive
| `closure_create of Ident.t * int * ((ty, unit) QmlGenericScheme.tsc) option
(** definition of a closure with its implementation, arity, and type scheme *)
| `closure_apply
(** application of a closure to a list of arguments *)
| `closure_create_no_function of Ident.t * int * ((ty, unit) QmlGenericScheme.tsc) option
(** definition of a closure without its implementation (the ident is unbound at this point) *)
| `closure_define_function of Ident.t * Ident.t * ((ty, unit) QmlGenericScheme.tsc) option
(** definition of the implementation field of the closure *)
| `assert_ (**As [assert]. : if --no-assert is enabled, all this directive without exception are ignored ('assert false' too) *)
| `fail (**As [assert false], with a message. : always fails, no matter if --no-assert is enabled or not. type : 'a *)
| `typeof (** -> WIP, don't use (yet) *)
| `expand of Big_int.big_int option (**Marker for macro (function) that are macro-expanded, the integer represents the number of unrolling the compiler is authorised to do, it must do at least one *)
| `restricted_bypass of string
(** this directive should be produced by any pass inserting some restricted bypass,
with a static string identifier to identify the pass.
If a bypass is tagged using [bsltags] to be restricted, then checks are done to be sure that the call is authorized *)
| `create_lazy_record
(** For use by the database.
[`create_lazy_record] creates a lazy record,
with an optional information [info] (ie the expr list is either [e] or [e;info])
Specifies that an expression evaluating to a record
should be executed only when the record is effectively accessed.*)
(** Explicit instantiation directives *)
| `apply_ty_arg of ty list * ty_row list * ty_col list
| `abstract_ty_arg of TypeVar.t list * RowVar.t list * ColVar.t list
| `doctype of (string list * doctype_access_directive)
| `hybrid_value
(** Directive for qmljs. First expression is a client
function whose type must be (string -> 'a).
This expression is optional (the default value being \@unsafe_cast).
Second expression is a string, that expression must be computed
on the server. *)
| `backend_ident of string
(** A hack for passes that need to introduce unbound identifiers
when they know the backend is going to generates the definitions
Should be used only at the very end of the compilation passes
since many passes don't want unbound identifiers
*)
| `tracker of PassTracker.t
| `js_ident
(** Get the js name of an ident, used by serialize of fun
action. *)
| `llarray
(**
Syntax creation for a low level array, from expression list of a static length.
Used for optimisation traduction from huge structure, e.g. in js serialization.
*)
| `specialize of [ `strict | `polymorphic ]
(**
Used to replace magic functions by standard functions at compile time
when the time is known at the call site
It takes a list of at least one expression, the first one being the general case
and the other expressions being the specialized cases.
In case of a strict specialization, during the pass_SimplifyMagic an error
is raised if the type cannot be specialized.
*)
| `partial_apply of int option (* original arity of the function, guaranteed to be filled by lambda lifting, None means 'undisclosed information' :) *)
| `full_apply of int (* size of the env *)
| `lifted_lambda of int * Ident.t option (* size of the env and the toplevel name of the declaration from where it was lifted
* (meaningful between lambda lifting and explicit instantiation, because
* ei adds @lifted_lambda on declarations that are not really lifted, so
* what would the value be?) *)
(**
Used to indicate a partial application
Must be around an Apply node
*)
| `at_init
(**
Used to specify that a value must be computed during initialization, not inside a lambda
*)
| opavalue_directive
| `tagged_string of string * tagged_string_kind
(**
Instead of putting static strings in the ast, we put these directives in the ast
It allows latter passes to pattern match the ast easily to find the use and definition
of types
for instance, [\@typeof(void)] will become [{TyName_ident = \@tagged_string ("void", `type_use); TyName_args = \[\]}]
*)
| `recval
(** should appear only in newvalrec and letrecin
indicate it was explicitely allowed by the programmer
to have a recursive expression that is not a lambda
(and the validity of the recursion is checked at runtime)
*)
]
(* TODO: remove Coerce form AST and use this instead; same for Parser; maybe others ? *)
and expr =
| Const of Annot.label * const_expr
| Ident of Annot.label * Ident.t
| LetIn of Annot.label * (Ident.t * expr) list * expr
| LetRecIn of Annot.label * (Ident.t * expr) list * expr
| Lambda of Annot.label * Ident.t list * expr
| Apply of Annot.label * expr * expr list
| Match of Annot.label * expr * (pat * expr) list
| Record of Annot.label * (string * expr) list
| Dot of Annot.label * expr * string
| ExtendRecord of Annot.label * string * expr * expr
| Bypass of Annot.label * BslKey.t