Skip to content

Commit 6414bf8

Browse files
authored
Deal with _hx_new (#12005)
* rename to _hx_new * test adjust * add CfAbstractConstructor * add a_constructor * deal with overloads * forgot one * deal with a_constructor in hxb
1 parent 79e018d commit 6414bf8

19 files changed

+46
-30
lines changed

src/compiler/hxb/hxbReader.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,7 @@ class hxb_reader
15891589
a.a_read <- self#read_option (fun () -> self#read_field_ref);
15901590
a.a_write <- self#read_option (fun () -> self#read_field_ref);
15911591
a.a_call <- self#read_option (fun () -> self#read_field_ref);
1592+
a.a_constructor <- self#read_option (fun () -> self#read_field_ref);
15921593

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

src/compiler/hxb/hxbWriter.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,7 @@ module HxbWriter = struct
19181918
Chunk.write_option writer.chunk a.a_read (write_field_ref writer c CfrStatic );
19191919
Chunk.write_option writer.chunk a.a_write (write_field_ref writer c CfrStatic);
19201920
Chunk.write_option writer.chunk a.a_call (write_field_ref writer c CfrStatic);
1921+
Chunk.write_option writer.chunk a.a_constructor (write_field_ref writer c CfrStatic);
19211922

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

src/core/abstract.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ let rec follow_with_forward_ctor ?(build=false) t = match follow t with
155155
| TAbstract(a,tl) as t ->
156156
if build then build_abstract a;
157157
if Meta.has Meta.ForwardNew a.a_meta && not (match a.a_impl with
158-
| Some c -> PMap.mem "_new" c.cl_statics
158+
| Some c -> a.a_constructor <> None
159159
| None -> false
160160
) then
161161
follow_with_forward_ctor (get_underlying_type ~return_first:true a tl)

src/core/display/completionItem.ml

+5-8
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,11 @@ module CompletionModuleType = struct
182182
raise Exit
183183

184184
let of_module_type mt =
185-
let actor a = match a.a_impl with
186-
| None -> No
187-
| Some c ->
188-
try
189-
let cf = PMap.find "_new" c.cl_statics in
190-
if (has_class_flag c CExtern) || (has_class_field_flag cf CfPublic) then Yes else YesButPrivate
191-
with Not_found ->
192-
No
185+
let actor a = match a.a_impl,a.a_constructor with
186+
| Some c,Some cf ->
187+
if (has_class_flag c CExtern) || (has_class_field_flag cf CfPublic) then Yes else YesButPrivate
188+
| _ ->
189+
No
193190
in
194191
let ctor c =
195192
try

src/core/inheritDoc.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ and get_target_doc ctx e_target =
181181
| TAbstract ({ a_impl = Some c }, _) ->
182182
let c_opt, cf =
183183
let field_name =
184-
if field_name = "new" then "_new"
184+
if field_name = "new" then "_hx_new"
185185
else field_name
186186
in
187187
get_class_field c field_name

src/core/tFunctions.ml

+1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ let null_abstract = {
299299
a_read = None;
300300
a_write = None;
301301
a_call = None;
302+
a_constructor = None;
302303
a_extern = false;
303304
a_enum = false;
304305
}

src/core/tOther.ml

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ let mk_abstract m path pos name_pos =
300300
a_this = mk_mono();
301301
a_read = None;
302302
a_write = None;
303+
a_constructor = None;
303304
a_extern = false;
304305
a_enum = false;
305306
a_call = None;

src/core/tType.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ and tabstract = {
382382
mutable a_read : tclass_field option;
383383
mutable a_write : tclass_field option;
384384
mutable a_call : tclass_field option;
385+
mutable a_constructor : tclass_field option;
385386
mutable a_extern : bool;
386387
mutable a_enum : bool;
387388
}
@@ -511,10 +512,11 @@ type flag_tclass_field =
511512
| CfUsed (* Marker for DCE *)
512513
| CfMaybeUsed (* Marker for DCE *)
513514
| CfNoLookup (* Field cannot be accessed by-name. *)
515+
| CfAbstractConstructor
514516

515517
(* Order has to match declaration for printing*)
516518
let flag_tclass_field_names = [
517-
"CfPublic";"CfStatic";"CfExtern";"CfFinal";"CfModifiesThis";"CfOverride";"CfAbstract";"CfOverload";"CfImpl";"CfEnum";"CfGeneric";"CfDefault";"CfPostProcessed";"CfUsed";"CfMaybeUsed";"CfNoLookup"
519+
"CfPublic";"CfStatic";"CfExtern";"CfFinal";"CfModifiesThis";"CfOverride";"CfAbstract";"CfOverload";"CfImpl";"CfEnum";"CfGeneric";"CfDefault";"CfPostProcessed";"CfUsed";"CfMaybeUsed";"CfNoLookup";"CfAbstractConstructor"
518520
]
519521

520522
type flag_tenum =

src/optimization/inline.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ let inline_default_config cf t =
232232
let inline_config cls_opt cf call_args return_type =
233233
match cls_opt with
234234
| Some ({cl_kind = KAbstractImpl _}) when has_class_field_flag cf CfImpl ->
235-
let t = if cf.cf_name = "_new" then
235+
let t = if has_class_field_flag cf CfAbstractConstructor then
236236
return_type
237237
else if call_args = [] then
238238
raise_typing_error "Invalid abstract implementation function" cf.cf_pos
@@ -580,7 +580,7 @@ class inline_state ctx ethis params cf f p = object(self)
580580
(match follow ethis.etype with
581581
| TAnon a -> (match !(a.a_status) with
582582
| ClassStatics {cl_kind = KAbstractImpl a } when has_class_field_flag cf CfImpl ->
583-
if cf.cf_name <> "_new" then begin
583+
if not (has_class_field_flag cf CfAbstractConstructor) then begin
584584
(* the first argument must unify with a_this for abstract implementation functions *)
585585
let tb = (TFun(("",false,map_type a.a_this) :: (List.tl tl),tret)) in
586586
unify_raise mt tb p

src/typing/callUnification.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ let unify_field_call ctx fa el_typed el p inline =
232232
cfl,Some c,false,TClass.get_map_function c tl,(fun t -> t)
233233
| FHAbstract(a,tl,c) ->
234234
let map = apply_params a.a_params tl in
235-
let tmap = if fa.fa_field.cf_name = "_new" (* TODO: BAD BAD BAD BAD *) then (fun t -> t) else (fun t -> map a.a_this) in
235+
let tmap = if has_class_field_flag fa.fa_field CfAbstractConstructor then (fun t -> t) else (fun t -> map a.a_this) in
236236
expand_overloads fa.fa_field,Some c,true,map,tmap
237237
in
238238
let is_forced_inline = is_forced_inline co fa.fa_field in

src/typing/calls.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let make_call ctx e params t ?(force_inline=false) p =
5454
(match cl, ctx.c.curclass.cl_kind, params with
5555
| Some c, KAbstractImpl _, { eexpr = TLocal { v_meta = v_meta } } :: _ when c == ctx.c.curclass ->
5656
if
57-
f.cf_name <> "_new"
57+
not (has_class_field_flag f CfAbstractConstructor)
5858
&& has_meta Meta.This v_meta
5959
&& has_class_field_flag f CfModifiesThis
6060
then

src/typing/fieldAccess.ml

+9-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,15 @@ let get_constructor_access c tl p =
163163
| _ -> c, tl
164164
in
165165
let cf, fh = match c.cl_kind with
166-
| KAbstractImpl a -> PMap.find "_new" c.cl_statics, FHAbstract(a,tl,c)
167-
| _ -> Type.get_constructor c, FHInstance(c,tl)
166+
| KAbstractImpl a ->
167+
begin match a.a_constructor with
168+
| None ->
169+
raise Not_found
170+
| Some cf ->
171+
cf,FHAbstract(a,tl,c)
172+
end
173+
| _ ->
174+
Type.get_constructor c, FHInstance(c,tl)
168175
in
169176
create e_static cf fh false p
170177
with Not_found ->

src/typing/typeloadFields.ml

+13-8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type field_init_ctx = {
5454
is_abstract : bool;
5555
is_macro : bool;
5656
is_abstract_member : bool;
57+
is_abstract_constructor : bool;
5758
is_display_field : bool;
5859
is_field_debug : bool;
5960
is_generic : bool;
@@ -271,7 +272,7 @@ let transform_abstract_field com this_t a_t a f =
271272
);
272273
f_type = Some a_t;
273274
} in
274-
{ f with cff_name = "_new",pos f.cff_name; cff_kind = FFun fu; cff_meta = meta }
275+
{ f with cff_name = "_hx_new",pos f.cff_name; cff_kind = FFun fu; cff_meta = meta }
275276
| FFun fu when not stat ->
276277
if Meta.has Meta.From f.cff_meta then raise_typing_error "@:from cast functions must be static" f.cff_pos;
277278
{ f with cff_kind = FFun fu }
@@ -529,6 +530,7 @@ let create_field_context ctx cctx cff is_display_file display_modifier =
529530
is_field_debug = cctx.is_class_debug || Meta.has (Meta.Custom ":debug.typeload") cff.cff_meta;
530531
display_modifier = display_modifier;
531532
is_abstract_member = is_abstract_member;
533+
is_abstract_constructor = is_abstract_member && fst cff.cff_name = "_hx_new";
532534
is_generic = Meta.has Meta.Generic cff.cff_meta;
533535
field_kind = field_kind;
534536
do_bind = (((not ((has_class_flag c CExtern) || !is_extern) || is_inline) && not is_abstract && not (has_class_flag c CInterface)) || field_kind = CfrInit);
@@ -631,7 +633,7 @@ let check_field_display ctx fctx c cf =
631633
let scope, cf = match c.cl_kind with
632634
| KAbstractImpl _ ->
633635
if has_class_field_flag cf CfImpl then
634-
(if cf.cf_name = "_new" then
636+
(if fctx.is_abstract_constructor then
635637
CFSConstructor, {cf with cf_name = "new"}
636638
else
637639
CFSMember, cf)
@@ -927,6 +929,7 @@ let create_variable (ctx,cctx,fctx) c f cf t eo p =
927929
cf
928930

929931
let check_abstract (ctx,cctx,fctx) a c cf fd t ret p =
932+
if fctx.is_abstract_constructor && a.a_constructor = None (* TODO: this is pretty dumb, it deals with the overload case *) then a.a_constructor <- Some cf;
930933
let m = mk_mono() in
931934
let ta = TAbstract(a,List.map (fun _ -> mk_mono()) a.a_params) in
932935
let tthis = if fctx.is_abstract_member || Meta.has Meta.To cf.cf_meta then monomorphs a.a_params a.a_this else a.a_this in
@@ -978,10 +981,11 @@ let check_abstract (ctx,cctx,fctx) a c cf fd t ret p =
978981
cf.cf_meta <- (Meta.MultiType,[],null_pos) :: cf.cf_meta;
979982
let r = make_lazy ctx.g t (fun () ->
980983
let args = if is_multitype_cast then begin
981-
let ctor = try
982-
PMap.find "_new" c.cl_statics
983-
with Not_found ->
984-
raise_typing_error "Constructor of multi-type abstract must be defined before the individual @:to-functions are" cf.cf_pos
984+
let ctor = match a.a_constructor with
985+
| Some cf ->
986+
cf
987+
| None ->
988+
raise_typing_error "Constructor of multi-type abstract must be defined before the individual @:to-functions are" cf.cf_pos
985989
in
986990
(* delay ctx PFinal (fun () -> unify ctx m tthis f.cff_pos); *)
987991
let args = match follow (monomorphs a.a_params ctor.cf_type) with
@@ -1087,7 +1091,7 @@ let check_abstract (ctx,cctx,fctx) a c cf fd t ret p =
10871091
| _ -> ();
10881092
in
10891093
List.iter check_meta cf.cf_meta;
1090-
if cf.cf_name = "_new" && Meta.has Meta.MultiType a.a_meta then fctx.do_bind <- false;
1094+
if fctx.is_abstract_constructor && Meta.has Meta.MultiType a.a_meta then fctx.do_bind <- false;
10911095
if fd.f_expr = None then begin
10921096
if fctx.is_inline then missing_expression ctx.com fctx "Inline functions must have an expression" cf.cf_pos;
10931097
if fd.f_type = None then raise_typing_error ("Functions without expressions must have an explicit return type") cf.cf_pos;
@@ -1160,7 +1164,7 @@ let setup_args_ret ctx cctx fctx name fd p =
11601164
maybe_use_property_type fd.f_type (fun () -> match Lazy.force mk with MKGetter | MKSetter -> true | _ -> false) def
11611165
end in
11621166
let abstract_this = match cctx.abstract with
1163-
| Some a when fctx.is_abstract_member && name <> "_new" (* TODO: this sucks *) && not fctx.is_macro ->
1167+
| Some a when fctx.is_abstract_member && not fctx.is_abstract_constructor && not fctx.is_macro ->
11641168
Some a.a_this
11651169
| _ ->
11661170
None
@@ -1271,6 +1275,7 @@ let create_method (ctx,cctx,fctx) c f cf fd p =
12711275
add_class_field_flag cf CfAbstract;
12721276
end;
12731277
if fctx.is_abstract_member then add_class_field_flag cf CfImpl;
1278+
if fctx.is_abstract_constructor then add_class_field_flag cf CfAbstractConstructor;
12741279
if fctx.is_generic then add_class_field_flag cf CfGeneric;
12751280
begin match fctx.default with
12761281
| Some p ->

src/typing/typeloadModule.ml

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ module ModuleLevel = struct
197197
a_read = None;
198198
a_write = None;
199199
a_call = None;
200+
a_constructor = None;
200201
a_extern = List.mem AbExtern d.d_flags;
201202
a_enum = List.mem AbEnum d.d_flags || p_enum_meta <> None;
202203
} in

src/typing/typer.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ and type_array_comprehension ctx e with_type p =
14791479
]) v.v_type p
14801480

14811481
and type_return ?(implicit=false) ctx e with_type p =
1482-
let is_abstract_ctor = ctx.e.curfun = FunMemberAbstract && ctx.f.curfield.cf_name = "_new" in
1482+
let is_abstract_ctor = ctx.e.curfun = FunMemberAbstract && has_class_field_flag ctx.f.curfield CfAbstractConstructor in
14831483
match e with
14841484
| None when is_abstract_ctor ->
14851485
let e_cast = mk (TCast(get_this ctx p,None)) ctx.e.ret p in

src/typing/typerBase.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ let assign_to_this_is_allowed ctx =
188188
| KAbstractImpl _ ->
189189
(match ctx.f.curfield.cf_kind with
190190
| Method MethInline -> true
191-
| Method _ when ctx.f.curfield.cf_name = "_new" -> true
191+
| Method _ when has_class_field_flag ctx.f.curfield CfAbstractConstructor -> true
192192
| _ -> false
193193
)
194194
| _ -> false

src/typing/typerDisplay.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ let handle_display ctx e_ast dk mode with_type =
619619
| TClassDecl c -> has_constructor c
620620
| TAbstractDecl a -> (match Abstract.follow_with_forward_ctor ~build:true (TAbstract(a,extract_param_types a.a_params)) with
621621
| TInst(c,_) -> has_constructor c
622-
| TAbstract({a_impl = Some c},_) -> PMap.mem "_new" c.cl_statics
622+
| TAbstract(a,_) -> a.a_constructor <> None
623623
| _ -> false)
624624
| _ -> false
625625
end
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Macro.hx:21: main
2-
Macro.hx:21: _new
2+
Macro.hx:21: _hx_new
33
Main.hx:4: abc

tests/optimization/src/issues/Issue3713.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private class C<T> {
1919

2020
class Issue3713 {
2121
@:js('
22-
var b = BImpl._new();
22+
var b = BImpl._hx_new();
2323
var c_x = 1;
2424
')
2525
@:analyzer(no_const_propagation, no_local_dce)

0 commit comments

Comments
 (0)