Skip to content

Commit

Permalink
[macro] delay exclude macro turning types into externs until filters (#…
Browse files Browse the repository at this point in the history
…11685)

* [macro] only turn type excluded by exclude macro into externs in filters

Avoids caching type as extern, which is leading to ugly errors atm

* Turn e_extern and e_excluded into e_flags

* [tests] add test for 11643
  • Loading branch information
kLabz authored Jun 6, 2024
1 parent cc105eb commit c850b65
Show file tree
Hide file tree
Showing 28 changed files with 97 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/codegen/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module Dump = struct
print "\n");
print "}";
| Type.TEnumDecl e ->
print "%s%s%senum %s%s {\n" (s_metas e.e_meta "") (if e.e_private then "private " else "") (if e.e_extern then "extern " else "") (s_type_path path) (params e.e_params);
print "%s%s%senum %s%s {\n" (s_metas e.e_meta "") (if e.e_private then "private " else "") (if has_enum_flag e EnExtern then "extern " else "") (s_type_path path) (params e.e_params);
List.iter (fun n ->
let f = PMap.find n e.e_constrs in
print "\t%s%s;\n" f.ef_name (
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/hxb/hxbReader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ class hxb_reader

method read_enum (e : tenum) =
self#read_common_module_type (Obj.magic e);
e.e_extern <- self#read_bool;
e.e_flags <- read_uleb128 ch;
e.e_names <- self#read_list (fun () -> self#read_string);

method read_typedef (td : tdef) =
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/hxb/hxbWriter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,7 @@ module HxbWriter = struct
let write_enum writer (e : tenum) =
select_type writer e.e_path;
write_common_module_type writer (Obj.magic e);
Chunk.write_bool writer.chunk e.e_extern;
Chunk.write_uleb128 writer.chunk e.e_flags;
Chunk.write_list writer.chunk e.e_names (Chunk.write_string writer.chunk)

let write_typedef writer (td : tdef) =
Expand Down
2 changes: 1 addition & 1 deletion src/core/display/completionItem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ module CompletionModuleType = struct
| TClassDecl c ->
(has_class_flag c CExtern),has_class_flag c CFinal,has_class_flag c CAbstract,(if (has_class_flag c CInterface) then Interface else Class),ctor c
| TEnumDecl en ->
en.e_extern,false,false,Enum,No
(has_enum_flag en EnExtern),false,false,Enum,No
| TTypeDecl td ->
let kind,ctor = match follow td.t_type with
| TAnon _ -> Struct,No
Expand Down
2 changes: 1 addition & 1 deletion src/core/json/genjson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ let generate_enum ctx e =
in
[
"constructors",generate_enum_constructors ();
"isExtern",jbool e.e_extern;
"isExtern",jbool (has_enum_flag e EnExtern)
]

let generate_typedef ctx td =
Expand Down
14 changes: 13 additions & 1 deletion src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ let remove_class_field_flag cf (flag : flag_tclass_field) =
let has_class_field_flag cf (flag : flag_tclass_field) =
has_flag cf.cf_flags (int_of_class_field_flag flag)

let int_of_enum_flag (flag : flag_tenum) =
Obj.magic flag

let add_enum_flag e (flag : flag_tenum) =
e.e_flags <- set_flag e.e_flags (int_of_enum_flag flag)

let remove_enum_flag e (flag : flag_tenum) =
e.e_flags <- unset_flag e.e_flags (int_of_enum_flag flag)

let has_enum_flag e (flag : flag_tenum) =
has_flag e.e_flags (int_of_enum_flag flag)

let int_of_var_flag (flag : flag_tvar) =
Obj.magic flag

Expand Down Expand Up @@ -247,7 +259,7 @@ let null_enum = {
e_using = [];
e_restore = (fun () -> ());
e_type = t_dynamic;
e_extern = false;
e_flags = 0;
e_constrs = PMap.empty;
e_names = [];
}
Expand Down
20 changes: 1 addition & 19 deletions src/core/tOther.ml
Original file line number Diff line number Diff line change
Expand Up @@ -274,25 +274,7 @@ let mk_enum m path pos name_pos =
e_using = [];
e_restore = (fun () -> ());
e_private = false;
e_extern = false;
e_constrs = PMap.empty;
e_names = [];
e_type = mk_mono();
}

let mk_enum m path pos name_pos =
{
e_path = path;
e_module = m;
e_pos = pos;
e_name_pos = name_pos;
e_doc = None;
e_meta = [];
e_params = [];
e_using = [];
e_restore = (fun () -> ());
e_private = false;
e_extern = false;
e_flags = 0;
e_constrs = PMap.empty;
e_names = [];
e_type = mk_mono();
Expand Down
2 changes: 1 addition & 1 deletion src/core/tPrinting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ module Printer = struct
"e_meta",s_metadata en.e_meta;
"e_params",s_type_params (tabs ^ "\t") en.e_params;
"e_type",s_type_kind en.e_type;
"e_extern",string_of_bool en.e_extern;
"e_extern",string_of_bool (has_enum_flag en EnExtern);
"e_constrs",s_list "\n\t" (s_tenum_field (tabs ^ "\t")) (PMap.fold (fun ef acc -> ef :: acc) en.e_constrs []);
"e_names",String.concat ", " en.e_names
]
Expand Down
7 changes: 6 additions & 1 deletion src/core/tType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ and tenum = {
mutable e_restore : unit -> unit;
(* do not insert any fields above *)
mutable e_type : t;
mutable e_extern : bool;
mutable e_flags : int;
mutable e_constrs : (string , tenum_field) PMap.t;
mutable e_names : string list;
}
Expand Down Expand Up @@ -483,6 +483,7 @@ type flag_tclass =
| CAbstract
| CFunctionalInterface
| CUsed (* Marker for DCE *)
| CExcluded (* Marker for exclude macro, turned into CExtern during filters *)

type flag_tclass_field =
| CfPublic
Expand All @@ -506,6 +507,10 @@ let flag_tclass_field_names = [
"CfPublic";"CfStatic";"CfExtern";"CfFinal";"CfModifiesThis";"CfOverride";"CfAbstract";"CfOverload";"CfImpl";"CfEnum";"CfGeneric";"CfDefault";"CfPostProcessed";"CfUsed";"CfMaybeUsed"
]

type flag_tenum =
| EnExtern
| EnExcluded (* Marker for exclude macro, turned into EnExtern during filters *)

type flag_tvar =
| VCaptured
| VFinal
Expand Down
13 changes: 12 additions & 1 deletion src/filters/filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ let check_abstract_as_value e =

(* PASS 2 begin *)

(* Applies exclude macro (which turns types into externs) *)

let apply_macro_exclude com t = match t with
| TClassDecl c when has_class_flag c CExcluded ->
add_class_flag c CExtern
| TEnumDecl e when has_enum_flag e EnExcluded ->
add_enum_flag e EnExtern
| _ ->
()

(* Removes extern and macro fields, also checks for Void fields *)

let remove_extern_fields com t = match t with
Expand Down Expand Up @@ -407,7 +417,7 @@ let check_reserved_type_paths com t =
in
match t with
| TClassDecl c when not (has_class_flag c CExtern) -> check c.cl_path c.cl_pos
| TEnumDecl e when not e.e_extern -> check e.e_path e.e_pos
| TEnumDecl e when not (has_enum_flag e EnExtern) -> check e.e_path e.e_pos
| _ -> ()

(* PASS 3 end *)
Expand Down Expand Up @@ -453,6 +463,7 @@ let destruction tctx detail_times main locals =
(* PASS 2: type filters pre-DCE *)
List.iter (fun t ->
FiltersCommon.remove_generic_base t;
apply_macro_exclude com t;
remove_extern_fields com t;
(* check @:remove metadata before DCE so it is ignored there (issue #2923) *)
check_remove_metadata t;
Expand Down
12 changes: 6 additions & 6 deletions src/generators/gencpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ let is_extern_class class_def =
;;

let is_extern_enum enum_def =
(enum_def.e_extern) || (has_meta_key enum_def.e_meta Meta.Extern)
(has_enum_flag enum_def EnExtern) || (has_meta_key enum_def.e_meta Meta.Extern)
;;

let is_native_class class_def =
Expand Down Expand Up @@ -7080,7 +7080,7 @@ let create_super_dependencies common_ctx =
| _ ->() );
List.iter (fun imp -> if not (has_class_flag (fst imp) CExtern) then deps := (fst imp).cl_path :: !deps) (real_non_native_interfaces class_def.cl_implements);
Hashtbl.add result class_def.cl_path !deps;
| TEnumDecl enum_def when not enum_def.e_extern ->
| TEnumDecl enum_def when not (has_enum_flag enum_def EnExtern) ->
Hashtbl.add result enum_def.e_path [];
| _ -> () );
) common_ctx.types;
Expand Down Expand Up @@ -8474,14 +8474,14 @@ let generate_cppia ctx =
else begin
generate_script_class common_ctx script class_def
end
| TEnumDecl enum_def when enum_def.e_extern -> ()
| TEnumDecl enum_def when has_enum_flag enum_def EnExtern -> ()
| TEnumDecl enum_def ->
let is_internal = is_internal_class enum_def.e_path in
if (is_internal) then
(if (debug>=4) then print_endline (" internal enum " ^ (join_class_path enum_def.e_path ".") ))
else begin
let meta = Texpr.build_metadata common_ctx.basic object_def in
if (enum_def.e_extern) then
if (has_enum_flag enum_def EnExtern) then
(if (debug>=4) then print_endline ("external enum " ^ (join_class_path enum_def.e_path ".") ));
generate_script_enum common_ctx script enum_def meta
end
Expand Down Expand Up @@ -8568,7 +8568,7 @@ let generate_source ctx =
if not ((has_class_flag class_def CInterface) && (is_native_gen_class class_def)) then
exe_classes := (class_def.cl_path, deps, object_def) :: !exe_classes;
end
| TEnumDecl enum_def when enum_def.e_extern -> ()
| TEnumDecl enum_def when has_enum_flag enum_def EnExtern -> ()
| TEnumDecl enum_def ->
let name = class_text enum_def.e_path in
let is_internal = is_internal_class enum_def.e_path in
Expand All @@ -8587,7 +8587,7 @@ let generate_source ctx =
makeId name 0;

let meta = Texpr.build_metadata common_ctx.basic object_def in
if (enum_def.e_extern) then
if (has_enum_flag enum_def EnExtern) then
(if (debug>1) then print_endline ("external enum " ^ name ));
boot_enums := enum_def.e_path :: !boot_enums;
jobs := (fun () -> generate_enum_files ctx enum_def super_deps meta ) :: !jobs;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3672,7 +3672,7 @@ let generate_static_init ctx types main =

free ctx rc;

| TEnumDecl e when not e.e_extern ->
| TEnumDecl e when not (has_enum_flag e EnExtern) ->

let et = enum_class ctx e in
let t = enum_type ctx e in
Expand Down
6 changes: 3 additions & 3 deletions src/generators/genjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ let generate_type ctx = function
(match c.cl_path with
| ([],_) -> ()
| _ -> generate_package_create ctx c.cl_path)
| TEnumDecl e when e.e_extern ->
| TEnumDecl e when has_enum_flag e EnExtern ->
if Meta.has Meta.JsRequire e.e_meta && is_directly_used ctx.com e.e_meta then
generate_require ctx e.e_path e.e_meta
| TEnumDecl e -> generate_enum ctx e
Expand Down Expand Up @@ -1694,7 +1694,7 @@ let alloc_ctx com es_version =

ctx.type_accessor <- (fun t ->
match t with
| TEnumDecl ({ e_extern = true } as e) when not (Meta.has Meta.JsRequire e.e_meta) ->
| TEnumDecl e when (has_enum_flag e EnExtern) && not (Meta.has Meta.JsRequire e.e_meta) ->
dot_path e.e_path
| TClassDecl c ->
let p = get_generated_class_path c in
Expand Down Expand Up @@ -1880,7 +1880,7 @@ let generate com =
else vars in
let vars = if (enums_as_objects && (has_feature ctx "has_enum" || has_feature ctx "Type.resolveEnum")) then "$hxEnums = $hxEnums || {}" :: vars else vars in
let vars,has_dollar_underscore =
if List.exists (function TEnumDecl { e_extern = false } -> true | _ -> false) com.types then
if List.exists (function TEnumDecl e when not (has_enum_flag e EnExtern) -> true | _ -> false) com.types then
"$_" :: vars,ref true
else
vars,ref false
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genjvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2884,7 +2884,7 @@ let generate_enum gctx en =
let generate_module_type ctx mt =
match mt with
| TClassDecl c when not (has_class_flag c CExtern) -> generate_class ctx c
| TEnumDecl en when not en.e_extern -> generate_enum ctx en
| TEnumDecl en when not (has_enum_flag en EnExtern) -> generate_enum ctx en
| _ -> ()

let generate_anons gctx =
Expand Down
6 changes: 3 additions & 3 deletions src/generators/genlua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ let generate_type ctx = function
generate_class ctx c;
check_multireturn ctx c;
| TEnumDecl e ->
if not e.e_extern then generate_enum ctx e
if not (has_enum_flag e EnExtern) then generate_enum ctx e
else ();
| TTypeDecl _ | TAbstractDecl _ -> ()

Expand All @@ -1874,7 +1874,7 @@ let generate_type_forward ctx = function
end
else if Meta.has Meta.LuaRequire c.cl_meta && is_directly_used ctx.com c.cl_meta then
generate_require ctx c.cl_path c.cl_meta
| TEnumDecl e when e.e_extern ->
| TEnumDecl e when has_enum_flag e EnExtern ->
if Meta.has Meta.LuaRequire e.e_meta && is_directly_used ctx.com e.e_meta then
generate_require ctx e.e_path e.e_meta;
| TEnumDecl e ->
Expand Down Expand Up @@ -1930,7 +1930,7 @@ let alloc_ctx com =
match t with
| TClassDecl c when (has_class_flag c CExtern) && not (Meta.has Meta.LuaRequire c.cl_meta)
-> dot_path p
| TEnumDecl { e_extern = true }
| TEnumDecl e when has_enum_flag e EnExtern
-> s_path ctx p
| _ -> s_path ctx p);
ctx
Expand Down
4 changes: 2 additions & 2 deletions src/generators/genneko.ml
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ let gen_type ctx t acc =
else
gen_class ctx c :: acc
| TEnumDecl e ->
if e.e_extern then
if has_enum_flag e EnExtern then
acc
else
gen_enum ctx e :: acc
Expand Down Expand Up @@ -622,7 +622,7 @@ let gen_boot ctx =

let gen_name ctx acc t =
match t with
| TEnumDecl e when e.e_extern ->
| TEnumDecl e when has_enum_flag e EnExtern ->
acc
| TEnumDecl e ->
let p = pos ctx e.e_pos in
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genphp7.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ class class_wrapper (cls) =
*)
class enum_wrapper (enm) =
object (self)
inherit type_wrapper enm.e_path enm.e_meta (not enm.e_extern)
inherit type_wrapper enm.e_path enm.e_meta (not (has_enum_flag enm EnExtern))
(**
Indicates if class initialization method should be executed upon class loaded
*)
Expand Down
4 changes: 2 additions & 2 deletions src/generators/genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ module Generator = struct

let gen_type ctx mt = match mt with
| TClassDecl c -> gen_class ctx c
| TEnumDecl en when not en.e_extern -> gen_enum ctx en
| TEnumDecl en when not (has_enum_flag en EnExtern) -> gen_enum ctx en
| TAbstractDecl {a_path = [],"UInt"} -> ()
| TAbstractDecl {a_path = [],"Enum"} -> ()
| TAbstractDecl {a_path = [],"EnumValue"} when not (has_feature ctx "has_enum") -> ()
Expand Down Expand Up @@ -2339,7 +2339,7 @@ module Generator = struct
List.iter (fun mt ->
match mt with
| TClassDecl c when (has_class_flag c CExtern) -> import c.cl_path c.cl_meta
| TEnumDecl e when e.e_extern -> import e.e_path e.e_meta
| TEnumDecl e when has_enum_flag e EnExtern -> import e.e_path e.e_meta
| _ -> ()
) ctx.com.types

Expand Down
4 changes: 2 additions & 2 deletions src/generators/genswf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ let build_dependencies t =
List.iter add_inherit tp.ttp_class.cl_implements
) c.cl_params;
List.iter add_inherit c.cl_implements;
| TEnumDecl e when not e.e_extern ->
| TEnumDecl e when not (has_enum_flag e EnExtern) ->
PMap.iter (fun _ f -> add_type f.ef_type) e.e_constrs;
| _ -> ());
h := PMap.remove (([],"Int"),DKType) (!h);
Expand Down Expand Up @@ -524,7 +524,7 @@ let generate swf_header com =
if e.f9_cid <> None then List.iter (fun t ->
let extern = (match t with
| TClassDecl c -> (has_class_flag c CExtern)
| TEnumDecl e -> e.e_extern
| TEnumDecl e -> (has_enum_flag e EnExtern)
| TAbstractDecl a -> false
| TTypeDecl t -> false
) in
Expand Down
6 changes: 3 additions & 3 deletions src/generators/genswf9.ml
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ let rec type_id ctx t =
type_path ctx ([],"Function")
| TType ({ t_path = ([],"UInt") as path },_) ->
type_path ctx path
| TEnum ({ e_path = ["flash"],"XmlType"; e_extern = true },_) ->
| TEnum ({ e_path = ["flash"],"XmlType" } as e,_) when has_enum_flag e EnExtern ->
HMPath ([],"String")
| TEnum (e,_) ->
type_path ctx e.e_path
Expand Down Expand Up @@ -284,7 +284,7 @@ let classify ctx t =
KDynamic
| TAbstract ({ a_path = ["flash"],"AnyType" },_) ->
KDynamic
| TEnum ({ e_path = ["flash"],"XmlType"; e_extern = true },_) ->
| TEnum ({ e_path = ["flash"],"XmlType" } as e,_) when has_enum_flag e EnExtern ->
KType (HMPath ([],"String"))
| TEnum (e,_) ->
KType (type_id ctx t)
Expand Down Expand Up @@ -2800,7 +2800,7 @@ let rec generate_type ctx t =
hlf_metas = extract_meta c.cl_meta;
})
| TEnumDecl e ->
if e.e_extern then
if has_enum_flag e EnExtern then
None
else
let meta = Texpr.build_metadata ctx.com.basic t in
Expand Down
Loading

0 comments on commit c850b65

Please sign in to comment.