-
Notifications
You must be signed in to change notification settings - Fork 125
/
bslTags.ml
328 lines (271 loc) · 8.63 KB
/
bslTags.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
(*
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 String = Base.String
(* TODO: a safe module InfixOperators, not in base.ml
open InfixOperators
*)
let (@*) = InfixOperator.(@*)
let (|>) = InfixOperator.(|>)
(* <!> If you add anything there, update *parse* and *parsed_t* functions *)
type t = {
backend_restriction : StringSet.t option ;
no_projection : StringSet.t option ;
opaname : bool ;
raise_ : bool ;
restricted : string list option option ;
second_order : bool ;
cps_bypass : bool ;
opacapi : bool ;
}
type parsed_t = ( string * string option ) list
let default = {
backend_restriction = None ;
no_projection = Some StringSet.empty ;
opaname = true ;
raise_ = false ;
restricted = None ;
second_order = false ;
cps_bypass = false ;
opacapi = false ;
}
(* for errors message, please put all tags there *)
let known_tags = [
"backend" ;
"opaname" ;
"normalize" ;
"raise" ;
"restricted" ;
"no-projection";
"cps-bypass" ;
"opacapi" ;
]
let is_default t = ( t == default )
let string_of_restricted = function
| None -> "no restriction"
| Some None -> "compiler only (any pass)"
| Some Some list -> String.concat_map ~left:"compiler only (pass(es) : " ~right:")" ", " (fun s -> s) list
type 'a pprinter = 'a Base.Format.pprinter
let pp = Format.fprintf
let pp_string = Format.pp_print_string
let pp_list = Base.Format.pp_list
let pp_meta_string fmt s = Format.fprintf fmt "%S" s
let pp_option name pp' fmt = function
| None -> pp_string fmt name
| Some o -> pp fmt "%s:%a" name pp' o
let pp_tag fmt (tag, attribute) = pp_option tag pp_string fmt attribute
let pp_meta_tag fmt (tag, attribute) =
Format.fprintf fmt "(%S, %a)" tag (Option.pp_meta pp_meta_string) attribute
let pp_meta fmt parsed_t =
Format.fprintf fmt "[ %a ]" (pp_list " ; " pp_meta_tag) parsed_t
(* following guidelines about errors *)
type error =
| Unknown_tag of (string * string option)
| RaiseInconsistency
exception Exception of error
let pp_error fmt = function
| Unknown_tag ((typo, _) as tag) ->
Format.fprintf fmt "Unknown tag [ %a , ... ]@\n" pp_tag tag ;
Format.fprintf fmt "%a" (HintUtils.pp_suggestion known_tags) typo
| RaiseInconsistency ->
Format.fprintf fmt "Inconsistency on tag [ raise , ...]@\n" ;
Format.fprintf fmt "This tag is not compatible with @{<bright>cps-bypass@}, nor @{<bright>no-projection(:cps?)@}"
let error_unknown_tag tag = raise (Exception (Unknown_tag tag))
let check_tags tags =
let inconsistency () = raise (Exception RaiseInconsistency) in
let () =
if tags.raise_
then
let () =
match tags.no_projection with
| None -> inconsistency ()
| Some set -> if StringSet.mem "cps" set then inconsistency ()
in
let () =
if tags.cps_bypass then inconsistency ()
in
()
in
()
let parse_aux =
let fold t = function
| "no-projection", None ->
{ t with no_projection = None }
| "no-projection", Some s ->
(match t.no_projection with
| None -> t
| Some old -> { t with no_projection = Some (StringSet.add s old) })
| "opaname", _ ->
(* opaname is now set by default, but we keep the syntax for backward compatibility *)
{ t with opaname = true }
| "normalize", _ ->
{ t with opaname = false }
| "raise", _ ->
{ t with raise_ = true }
| "restricted", opt ->
begin
let restricted =
match opt with
| None -> Some None
| Some pass ->
begin
match t.restricted with
| None -> Some (Some [pass])
| Some None -> Some None
| Some (Some other_pass) -> Some (Some (pass::other_pass))
end
in
{ t with restricted = restricted }
end
| "backend", opt ->
let opt = Option.default "anybackend" opt in
{ t with
backend_restriction =
Some (StringSet.add opt (Option.default StringSet.empty t.backend_restriction))
}
(* internal attributes : should not be parsed, but are generated by bslregister *)
| "second-order", _ ->
{ t with second_order = true }
| "cps-bypass", _ ->
{ t with cps_bypass = true }
| "opacapi", _ ->
{ t with opacapi = true }
| tag ->
error_unknown_tag tag
in
List.fold_left fold default
let parse ?pos parsed_t =
match pos with
| None -> parse_aux parsed_t
| Some pos -> (
try
let tags = parse_aux parsed_t in
check_tags tags ;
tags
with
| Exception error ->
OManager.printf "%a" FilePos.pp_citation pos ;
OManager.error "%a@\n" pp_error error
)
(* reversing : used for code generation. *)
let parsed_t_backend t acc =
let tag = "backend" in
match t.backend_restriction with
| None -> acc
| Some set ->
StringSet.fold (
fun backend acc -> (tag, Some backend) :: acc
) set acc
let parsed_t_no_projection t acc =
let no_projection = "no-projection" in
match t.no_projection with
| None -> (no_projection, None) :: acc
| Some set ->
if StringSet.is_empty set
then acc
else
StringSet.fold (
fun elt acc -> (no_projection, Some elt) :: acc
) set acc
let parsed_t_opaname t acc =
let normalize = "normalize" in
match t.opaname with
| false -> (normalize, None) :: acc
| true -> acc
let parsed_t_raise t acc =
let raise_ = "raise" in
match t.raise_ with
| true -> (raise_, None) :: acc
| false -> acc
let parsed_t_restricted t acc =
let restricted = "restricted" in
match t.restricted with
| None -> acc
| Some None -> (restricted, None) :: acc
| Some (Some passes) ->
List.fold_left (
fun acc pass -> (restricted, Some pass) :: acc
) acc passes
let parsed_t_second_order t acc =
let second_order = "second-order" in
if t.second_order
then (second_order, None) :: acc
else acc
let parsed_t_cps_bypass t acc =
let cps_bypass = "cps-bypass" in
if t.cps_bypass
then (cps_bypass, None) :: acc
else acc
let parsed_t_opacapi t acc =
let opacapi = "opacapi" in
if t.opacapi
then (opacapi, None) :: acc
else acc
let parsed_t t =
let (||>) acc f = f t acc in
[]
||> parsed_t_backend
||> parsed_t_no_projection
||> parsed_t_opaname
||> parsed_t_raise
||> parsed_t_restricted
||> parsed_t_second_order
||> parsed_t_cps_bypass
||> parsed_t_opacapi
|> List.sort Pervasives.compare
(** {6 Printing} *)
let pp fmt t =
let parsed_t = parsed_t t in
pp fmt "[@[<4>@ %a @]]" (pp_list " ;@ " pp_tag) parsed_t
module Q = QmlAst
let authorized_bypass ~restriction tags =
#<If:BSL_NO_RESTRICTION>
true
#<Else>
match restriction with
| None -> (
match tags.restricted with
| None -> true
| Some _ -> false
)
| Some pass -> (
match tags.restricted with
| None
| Some None -> true
| Some (Some authorized_passes) -> List.mem pass authorized_passes
)
#<End>
let authorized_bypass_as_expr tags bypass =
match bypass with
| Q.Bypass _ ->
authorized_bypass ~restriction:None tags
| Q.Directive (_, `restricted_bypass restriction, _, _) ->
authorized_bypass ~restriction:(Some restriction) tags
| _ -> assert false (* this function is not meant to be called with anything else *)
type passname = string
let do_projection {no_projection = no_projection} pass_name =
match no_projection with
| None -> false
| Some s -> not (StringSet.mem pass_name s)
let never_projected {no_projection = no_projection} =
match no_projection with
| None -> true
| Some _ -> false
let string_of_no_projection = function
| None -> "never project"
| Some s ->
if StringSet.is_empty s then
"always project"
else
String.concat_map ~left:"always project but for the following passes: " ", "
(fun x -> x) (StringSet.elements s)