-
Notifications
You must be signed in to change notification settings - Fork 125
/
opaMapToIdent.ml
212 lines (176 loc) · 6.65 KB
/
opaMapToIdent.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
(*
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/>.
*)
module Format = BaseFormat
(*
We test strictly than the val_ 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 (
#<If:OPACAPI_LOOSE>
()
#<Else>
OManager.printf "OPACAPI violation, on ident %S@\n" s ;
OManager.printf "You should use opacapi for inserting identifiers from the stdlib@." ;
assert false
#<End>
)
(* ******************************************************************)
(* Side type and utils **********************************************)
(* ******************************************************************)
type side = [`client | `server]
let other_side = function
| `client -> `server
| `server -> `client
(* ******************************************************************)
(* Link name <-> ident **********************************************)
(* ******************************************************************)
(*
Two functions that is used to insert types or variables
Since everything is renamed with the surfaceAst, you cannot insert a
coercion to list in the AST (list is not going to be defined)
*)
(* Reference to the maps*)
let r_var = ref (StringMap.empty : Ident.t list StringMap.t)
let r_var_client = ref (StringMap.empty : Ident.t list StringMap.t)
let r_type = ref (StringMap.empty : Ident.t list StringMap.t)
(* registering the references to be able to save them *)
let () =
PassTracker.register_global_ref r_var;
PassTracker.register_global_ref r_var_client;
PassTracker.register_global_ref r_type
let get_rmap = function
| `server -> r_var
| `client -> r_var_client
let print_side = function
| `server -> "server"
| `client -> "client"
let val_no_opacapi_check ?(side=`server) s =
match StringMap.find s !(get_rmap side) with
| [i] -> i
| l -> OManager.i_error "Found multiple ident for %s : %a" s (Format.pp_list " " (fun f i -> Format.fprintf f "%s" (Ident.to_uniq_string i))) l
let val_noerr ?(side=`server) s =
opacapi_check s ;
val_no_opacapi_check ~side s
let pp_stringmap f map =
StringMap.iter
(fun k _v ->
Format.fprintf f "%S " k)
map
let val_ ?(side=`server) s =
try val_noerr ~side s
with Not_found ->
OManager.i_error
"OpaMapToIdent %s: Not found: %S\nIt contains:@\n%a@\n"
(print_side side) s
pp_stringmap
!(get_rmap side)
let default_pos ?(label=Annot.nolabel "OpaMapToIdent") name =
QmlAst.Ident (label, Ident.source name)
let typed_val ?(label=Annot.nolabel "OpaMapToIdent") ?(side=`server) ?(ty=[]) ?(ty_row=[]) name annotmap gamma =
try
let ident = val_ ~side name in
let tsc = QmlTypes.Env.Ident.find ident gamma in
let ty = QmlTypes.Scheme.specialize ~typeident:ident ~ty ~ty_row tsc in
let (annotmap, ident) = QmlAstCons.TypedExpr.ident annotmap ident ty in
let annotmap = QmlAnnotMap.add_tsc_inst (QmlAst.QAnnot.expr ident) tsc annotmap in
(annotmap, ident)
with Not_found ->
let context = QmlError.Context.annoted_expr annotmap (default_pos ~label name) in
QmlError.cond_violation QmlAlphaConv.Check.unbound_id context
"Missing ident"
let typ s =
opacapi_check s ;
try
match StringMap.find s !r_type with
| [i] -> i
| l -> OManager.i_error "Found multiple ident for %s : %a" s (Format.pp_list " " (fun f i -> Format.fprintf f "%s" (Ident.to_uniq_string i))) l
with Not_found ->
OManager.i_error
"OpaMapToIdent: Type not found: %S\nIt contains:@\n%a@\n"
s
pp_stringmap
!r_type
let specialized_typ ?(ty = []) ?(ty_row = []) name gamma =
let typeident = typ name in
let (scheme, _) = QmlTypes.Env.TypeIdent.find ~visibility_applies:false typeident gamma in
QmlTypes.Scheme.specialize ~typeident ~ty ~ty_row scheme
let val_opt ?(side=`server) s =
try Some (val_noerr ~side s) with Not_found -> None
let val_add ?(side=`server) s =
let new_s = Ident.next s in
let r_var = get_rmap side in
r_var := StringMap.replace s (function | None -> [new_s] | Some l -> new_s::l) !r_var;
new_s
let val_unsafe_add ?(side=`server) s =
let new_s = Ident.next s in
let r_var = get_rmap side in
r_var := StringMap.replace s (function | None -> [new_s] | Some l -> new_s::l) !r_var;
new_s
let set_val_map ?(side=`server) v = (get_rmap side) := v
let set_typ_map v = r_type := v
let get_val_map ?(side=`server) () = !(get_rmap side)
let iter_val_map ?(side=`server) = fun f -> StringMap.iter f !(get_rmap side)
let map_val_map ?(side=`server) = fun f -> StringMap.map f !(get_rmap side)
let fold_val_map ?(side=(`server)) = fun f -> StringMap.fold f !(get_rmap side)
(* Special add and get for start_server value *)
let start_server = ref None
let str_start_server = "``"
let val_start_server () =
Option.map
(fun i -> val_ i)
!start_server
let val_start_server_add () =
match !start_server with
| Some _ -> failwith("start_server")
| None ->
let ident = Ident.next "run_services" in
start_server := (Some str_start_server);
set_val_map (StringMap.add str_start_server [ident] (get_val_map ()));
ident
(** Hack for opacapi - To be cleanned by introduce dependencies
beetween initialisations values (css, etc...) and init server *)
let _ = Opacapi.(!!) str_start_server
let get_toplevel_vars () = StringMap.elts !r_var
let filter f =
let fil map =
StringMap.fold
(fun s i acc ->
match List.filter f i with
| [] -> acc
| l -> StringMap.add s l acc)
map StringMap.empty
in
r_var := fil !r_var;
r_var_client := fil !r_var_client
let reset () =
set_val_map ~side:`server StringMap.empty;
set_val_map ~side:`client StringMap.empty;
set_typ_map StringMap.empty;
start_server := None
let pp fmt () =
Format.fprintf fmt
"OpaMapToIdent (types) contains:@\n%a@\n"
pp_stringmap
!r_type