Skip to content

Commit bb569d9

Browse files
Aidan63Simn
andauthored
Default value for abstracts (#12351)
* add a_default * let's see what's broken * fix what's broken * Add default value to more target specific sized ints * macro encoder for abstract default value * Have a go at a hxb reader and writer * update abstract printer * remove coroutine test will be added back to the coroutine branch later --------- Co-authored-by: Simon Krajewski <[email protected]>
1 parent 90a2a3c commit bb569d9

File tree

29 files changed

+75
-25
lines changed

29 files changed

+75
-25
lines changed

src-json/meta.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@
180180
"doc": "",
181181
"platforms": ["cpp"]
182182
},
183+
{
184+
"name": "DefaultValue",
185+
"metadata": ":defaultValue",
186+
"doc": "Default value for abstracts",
187+
"targets": ["TAbstract"],
188+
"internal": true
189+
},
183190
{
184191
"name": "DefParam",
185192
"metadata": ":defParam",

src/compiler/hxb/hxbReader.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,10 @@ class hxb_reader
15831583
a.a_write <- self#read_option (fun () -> self#read_field_ref);
15841584
a.a_call <- self#read_option (fun () -> self#read_field_ref);
15851585
a.a_constructor <- self#read_option (fun () -> self#read_field_ref);
1586+
a.a_default <- self#read_option (fun () ->
1587+
let fctx = self#start_texpr in
1588+
let e = self#read_texpr fctx in
1589+
Lazy.from_val e);
15861590

15871591
a.a_ops <- self#read_list (fun () ->
15881592
let i = read_byte ch in

src/compiler/hxb/hxbWriter.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1949,10 +1949,17 @@ module HxbWriter = struct
19491949
in
19501950

19511951
Chunk.write_list writer.chunk a.a_array (write_field_ref writer c CfrStatic);
1952-
Chunk.write_option writer.chunk a.a_read (write_field_ref writer c CfrStatic );
1952+
Chunk.write_option writer.chunk a.a_read (write_field_ref writer c CfrStatic);
19531953
Chunk.write_option writer.chunk a.a_write (write_field_ref writer c CfrStatic);
19541954
Chunk.write_option writer.chunk a.a_call (write_field_ref writer c CfrStatic);
19551955
Chunk.write_option writer.chunk a.a_constructor (write_field_ref writer c CfrStatic);
1956+
Chunk.write_option writer.chunk a.a_default (fun lazy_texpr ->
1957+
let texpr = Lazy.force lazy_texpr in
1958+
let fctx,close = start_texpr writer texpr.epos in
1959+
catch_unbound_ttp (fun () -> write_texpr writer fctx texpr) "default value" None;
1960+
let expr_pre_chunk,expr_chunk = close() in
1961+
Chunk.export_data expr_pre_chunk writer.chunk;
1962+
Chunk.export_data expr_chunk writer.chunk);
19561963

19571964
Chunk.write_list writer.chunk a.a_ops (fun (op, cf) ->
19581965
Chunk.write_u8 writer.chunk (binop_index op);

src/core/tFunctions.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ let null_abstract = {
302302
a_constructor = None;
303303
a_extern = false;
304304
a_enum = false;
305+
a_default = None;
305306
}
306307

307308
let create_dependency mdep origin =

src/core/tOther.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ let mk_abstract m path pos name_pos =
304304
a_extern = false;
305305
a_enum = false;
306306
a_call = None;
307+
a_default = None;
307308
}
308309

309310
module TClass = struct

src/core/tPrinting.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ module Printer = struct
593593
"a_array",s_list ", " (fun cf -> cf.cf_name) a.a_array;
594594
"a_read",s_opt (fun cf -> cf.cf_name) a.a_read;
595595
"a_write",s_opt (fun cf -> cf.cf_name) a.a_write;
596+
"a_default",s_opt (fun lazy_texpr -> lazy_texpr |> Lazy.force |> s_expr_ast true "" s_type) a.a_default;
596597
]
597598

598599
let s_tvar_extra ve =

src/core/tType.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ and tabstract = {
398398
mutable a_constructor : tclass_field option;
399399
mutable a_extern : bool;
400400
mutable a_enum : bool;
401+
mutable a_default : texpr Lazy.t option;
401402
}
402403

403404
and module_type =

src/core/texpr.ml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -547,15 +547,17 @@ module Builder = struct
547547
let index basic e index t p =
548548
mk (TArray (e,mk (TConst (TInt (Int32.of_int index))) basic.tint p)) t p
549549

550-
let default_value t p = match follow_without_null t with
551-
| TAbstract({a_path = ([],"Int")},[]) ->
552-
mk (TConst (TInt (Int32.zero))) t p
553-
| TAbstract({a_path = ([],"Float")},[]) ->
554-
mk (TConst (TFloat "0.0")) t p
555-
| TAbstract({a_path = ([],"Bool")},[]) ->
556-
mk (TConst (TBool false)) t p
557-
| _ ->
550+
let rec default_value t p = match t with
551+
| TAbstract({a_default = Some f},_) ->
552+
{(Lazy.force f) with etype = t; epos = p}
553+
| TInst _ | TEnum _ | TAbstract _ | TFun _ | TAnon _ | TDynamic _ | TMono {tm_type = None} ->
558554
mk (TConst TNull) t p
555+
| TLazy r ->
556+
default_value (lazy_type r) p
557+
| TMono {tm_type = Some t} ->
558+
default_value t p
559+
| TType(td,tl) ->
560+
default_value (apply_typedef td tl) p
559561

560562
let resolve_and_make_static_call c name args p =
561563
ignore(c.cl_build());

src/macro/macroApi.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,8 @@ and encode_tabstract a =
10831083
"to", encode_array ((List.map (fun t -> encode_obj [ "t",encode_type t; "field",vnull]) a.a_to) @ (List.map (fun (t,cf) -> encode_obj [ "t",encode_type t; "field",encode_cfield cf]) a.a_to_field));
10841084
"array", encode_array (List.map encode_cfield a.a_array);
10851085
"resolve", (match a.a_read with None -> vnull | Some cf -> encode_cfref cf);
1086-
"resolveWrite", (match a.a_write with None -> vnull | Some cf -> encode_cfref cf)
1086+
"resolveWrite", (match a.a_write with None -> vnull | Some cf -> encode_cfref cf);
1087+
"defaultValue", (match a.a_default with None -> vnull | Some lazy_texpr -> encode_ref lazy_texpr (fun lazy_texpr -> lazy_texpr |> Lazy.force |> encode_texpr) (fun () -> "default value") )
10871088
]
10881089

10891090
and encode_efield f =

src/typing/typeloadModule.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ module ModuleLevel = struct
172172
let priv = List.mem AbPrivate d.d_flags in
173173
let path = make_path name priv d.d_meta p in
174174
let p_enum_meta = Meta.maybe_get_pos Meta.Enum d.d_meta in
175+
let a_default = try
176+
begin match Meta.get Meta.DefaultValue d.d_meta with
177+
| (_,[e],_) ->
178+
Some (Lazy.from_fun (fun () ->
179+
let ctx = TyperManager.clone_for_expr ctx_m FunStatic FunNotFunction in
180+
type_expr ctx e WithType.value
181+
))
182+
| _ ->
183+
raise Not_found
184+
end
185+
with Not_found ->
186+
None
187+
in
175188
let a = {
176189
a_path = path;
177190
a_private = priv;
@@ -198,6 +211,7 @@ module ModuleLevel = struct
198211
a_constructor = None;
199212
a_extern = List.mem AbExtern d.d_flags;
200213
a_enum = List.mem AbEnum d.d_flags || p_enum_meta <> None;
214+
a_default;
201215
} in
202216
begin match p_enum_meta with
203217
| None when a.a_enum -> a.a_meta <- (Meta.Enum,[],null_pos) :: a.a_meta; (* HAXE5: remove *)

0 commit comments

Comments
 (0)