Skip to content

Commit 8738fea

Browse files
committed
Add check attribute
1 parent fe54dae commit 8738fea

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

ocaml/lambda/lambda.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ type local_attribute =
370370
| Never_local (* [@local never] *)
371371
| Default_local (* [@local maybe] or no [@local] attribute *)
372372

373+
type check_attribute =
374+
| Default_check
375+
| Noeffects_check
376+
| Noalloc_check
377+
| Noalloc_exn_check
378+
373379
type function_kind = Curried of {nlocal: int} | Tupled
374380

375381
type let_kind = Strict | Alias | StrictOpt
@@ -389,6 +395,7 @@ type function_attribute = {
389395
inline : inline_attribute;
390396
specialise : specialise_attribute;
391397
local: local_attribute;
398+
check : check_attribute;
392399
is_a_functor: bool;
393400
stub: bool;
394401
}

ocaml/lambda/lambda.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ type local_attribute =
283283
| Never_local (* [@local never] *)
284284
| Default_local (* [@local maybe] or no [@local] attribute *)
285285

286+
type check_attribute =
287+
| Default_check
288+
| Noeffects_check
289+
| Noalloc_check
290+
| Noalloc_exn_check
291+
286292
type function_kind = Curried of {nlocal: int} | Tupled
287293
(* [nlocal] determines how many arguments may be partially applied
288294
before the resulting closure must be locally allocated.
@@ -309,6 +315,7 @@ type function_attribute = {
309315
inline : inline_attribute;
310316
specialise : specialise_attribute;
311317
local: local_attribute;
318+
check : check_attribute;
312319
is_a_functor: bool;
313320
stub: bool;
314321
}

ocaml/lambda/translattribute.ml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ let is_local_attribute = function
3838
| {txt=("local"|"ocaml.local")} -> true
3939
| _ -> false
4040

41+
let is_check_attribute = function
42+
| {txt=("check"|"ocaml.check")} -> true
43+
| _ -> false
44+
4145
let find_attribute p attributes =
4246
let inline_attribute, other_attributes =
4347
List.partition (fun a -> p a.Parsetree.attr_name) attributes
@@ -197,6 +201,20 @@ let parse_local_attribute attr =
197201
]
198202
payload
199203

204+
let parse_check_attribute attr =
205+
match attr with
206+
| None -> Default_check
207+
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload} ->
208+
parse_id_payload txt loc
209+
~default:Default_check
210+
~empty:Default_check
211+
[
212+
"noeffects", Noeffects_check;
213+
"noalloc", Noalloc_check;
214+
"noalloc_exn", Noalloc_exn_check;
215+
]
216+
payload
217+
200218
let get_inline_attribute l =
201219
let attr, _ = find_attribute is_inline_attribute l in
202220
parse_inline_attribute attr
@@ -209,6 +227,10 @@ let get_local_attribute l =
209227
let attr, _ = find_attribute is_local_attribute l in
210228
parse_local_attribute attr
211229

230+
let get_check_attribute l =
231+
let attr, _ = find_attribute is_check_attribute l in
232+
parse_check_attribute attr
233+
212234
let check_local_inline loc attr =
213235
match attr.local, attr.inline with
214236
| Always_local, (Always_inline | Available_inline | Unroll _) ->
@@ -270,6 +292,23 @@ let add_local_attribute expr loc attributes =
270292
(Warnings.Misplaced_attribute "local");
271293
expr
272294

295+
let add_check_attribute expr loc attributes =
296+
match expr, get_check_attribute attributes with
297+
| expr, Default_check -> expr
298+
| Lfunction({ attr = { stub = false } as attr } as funct), check ->
299+
begin match attr.check with
300+
| Default_check -> ()
301+
| Noalloc_check | Noalloc_exn_check | Noeffects_check ->
302+
Location.prerr_warning loc
303+
(Warnings.Duplicated_attribute "check")
304+
end;
305+
let attr = { attr with check } in
306+
Lfunction { funct with attr }
307+
| expr, (Noalloc_check | Noalloc_exn_check | Noeffects_check) ->
308+
Location.prerr_warning loc
309+
(Warnings.Misplaced_attribute "check");
310+
expr
311+
273312
(* Get the [@inlined] attribute payload (or default if not present).
274313
It also returns the expression without this attribute. This is
275314
used to ensure that this attribute is not misplaced: If it
@@ -382,4 +421,7 @@ let add_function_attributes lam loc attr =
382421
let lam =
383422
add_local_attribute lam loc attr
384423
in
424+
let lam =
425+
add_check_attribute lam loc attr
426+
in
385427
lam

0 commit comments

Comments
 (0)