Skip to content

Commit

Permalink
add TEnumIndex and make matcher generate that instead of Type.enumInd…
Browse files Browse the repository at this point in the history
…ex call
  • Loading branch information
nadako committed Jun 14, 2017
1 parent acb32e1 commit c3dc791
Show file tree
Hide file tree
Showing 32 changed files with 155 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/display/display.ml
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ module Diagnostics = struct
()
| TConst _ | TLocal _ | TTypeExpr _ | TFunction _ when not in_value ->
no_effect e.epos;
| TConst _ | TLocal _ | TTypeExpr _ | TEnumParameter _ | TVar _ ->
| TConst _ | TLocal _ | TTypeExpr _ | TEnumParameter _ | TEnumIndex _ | TVar _ ->
()
| TFunction tf ->
loop false tf.tf_expr
Expand Down
2 changes: 1 addition & 1 deletion src/generators/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ let rec constructor_side_effects e =
true
| TField (_,FEnum _) ->
false
| TUnop _ | TArray _ | TField _ | TEnumParameter _ | TCall _ | TNew _ | TFor _ | TWhile _ | TSwitch _ | TReturn _ | TThrow _ ->
| TUnop _ | TArray _ | TField _ | TEnumParameter _ | TEnumIndex _ | TCall _ | TNew _ | TFor _ | TWhile _ | TSwitch _ | TReturn _ | TThrow _ ->
true
| TBinop _ | TTry _ | TIf _ | TBlock _ | TVar _
| TFunction _ | TArrayDecl _ | TObjectDecl _
Expand Down
4 changes: 4 additions & 0 deletions src/generators/genas3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@ and gen_expr ctx e =
gen_expr ctx e1;
spr ctx ")";
gen_field_access ctx e1.etype (field_name s)
| TEnumIndex e ->
gen_value ctx e;
print ctx ".index";
| TEnumParameter (e,_,i) ->
gen_value ctx e;
print ctx ".params[%i]" i;
Expand Down Expand Up @@ -882,6 +885,7 @@ and gen_value ctx e =
| TBinop _
| TField _
| TEnumParameter _
| TEnumIndex _
| TTypeExpr _
| TParenthesis _
| TObjectDecl _
Expand Down
12 changes: 9 additions & 3 deletions src/generators/gencommon/enumToClass.ml
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ struct
let name = "enum_to_class_exprf"
let priority = solve_deps name [DBefore TArrayTransform.priority]

let configure gen t opt_get_native_enum_tag =
let configure gen t mk_enum_index_call =
let rec run e =
let get_converted_enum_type et =
let en, eparams = match follow (gen.gfollow#run_f et) with
Expand All @@ -265,6 +265,12 @@ struct
in

match e.eexpr with
| TEnumIndex f ->
let f = run f in
(try
mk_field_access gen {f with etype = get_converted_enum_type f.etype} "index" e.epos
with Not_found ->
mk_enum_index_call f e.epos)
| TCall (({eexpr = TField(_, FStatic({cl_path=[],"Type"},{cf_name="enumIndex"}))} as left), [f]) ->
let f = run f in
(try
Expand All @@ -289,7 +295,7 @@ struct

end;;

let configure gen opt_get_native_enum_tag convert_all convert_if_has_meta enum_base_class param_enum_class =
let configure gen convert_all convert_if_has_meta enum_base_class param_enum_class mk_enum_index_call =
let t = new_t () in
EnumToClassModf.configure gen t convert_all convert_if_has_meta enum_base_class param_enum_class;
EnumToClassExprf.configure gen t opt_get_native_enum_tag
EnumToClassExprf.configure gen t mk_enum_index_call
26 changes: 17 additions & 9 deletions src/generators/gencommon/enumToClass2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ end;;


module EnumToClass2Exprf = struct
let init com ec_tbl =
let init com ec_tbl mk_enum_index_call =
let v_as = alloc_var "__as__" t_dynamic in
let rec run e =
let get_converted_enum_classes et =
Expand All @@ -347,14 +347,22 @@ module EnumToClass2Exprf = struct
in
Hashtbl.find ec_tbl en.e_path
in

let mk_converted_enum_index_access f =
let cl = (get_converted_enum_classes f.etype).base in
let e_enum = { f with etype = TInst (cl, []) } in
Codegen.field e_enum "_hx_index" com.basic.tint e.epos
in
match e.eexpr with
| TEnumIndex f ->
let f = run f in
(try
mk_converted_enum_index_access f
with Not_found ->
mk_enum_index_call f e.epos)
| TCall ({ eexpr = TField (_, FStatic ({ cl_path = ([], "Type") }, { cf_name = "enumIndex" })) } as left, [f]) ->
let f = run f in
(try
let cl = (get_converted_enum_classes f.etype).base in
let e_enum = { f with etype = TInst (cl, []) } in
Codegen.field e_enum "_hx_index" com.basic.tint e.epos
mk_converted_enum_index_access f
with Not_found ->
{ e with eexpr = TCall(left, [f]) })
| TEnumParameter(f, ef, i) ->
Expand All @@ -381,12 +389,12 @@ module EnumToClass2Exprf = struct
let name = "enum_to_class2_exprf"
let priority = solve_deps name []

let configure gen ec_tbl =
let run = init gen.gcon ec_tbl in
let configure gen ec_tbl mk_enum_index_call =
let run = init gen.gcon ec_tbl mk_enum_index_call in
gen.gexpr_filters#add name (PCustom priority) run
end;;

let configure gen enum_base_class =
let configure gen enum_base_class mk_enum_index_call =
let ec_tbl = Hashtbl.create 10 in
EnumToClass2Modf.configure gen ec_tbl enum_base_class;
EnumToClass2Exprf.configure gen ec_tbl;
EnumToClass2Exprf.configure gen ec_tbl mk_enum_index_call;
1 change: 1 addition & 0 deletions src/generators/gencommon/expressionUnwrap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ let rec shallow_expr_type expr : shallow_expr_type =
| TBinop _
| TField _
| TEnumParameter _
| TEnumIndex _
| TTypeExpr _
| TObjectDecl _
| TArrayDecl _
Expand Down
1 change: 1 addition & 0 deletions src/generators/gencommon/switchToIf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ let configure gen (should_convert:texpr->bool) =
begin
try
match (simplify_expr cond).eexpr with
| TEnumIndex enum
| TCall ({ eexpr = TField (_, FStatic ({ cl_path = [],"Type" }, { cf_name = "enumIndex" })) }, [enum]) ->
let real_enum =
match enum.etype with
Expand Down
25 changes: 16 additions & 9 deletions src/generators/gencpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,7 @@ let is_gc_element ctx member_type =
| TCppWrapped _
| TCppScalarArray _
| TCppInst _
| TCppInterface _
| TCppInterface _
| TCppClass
-> true
| _ -> false
Expand Down Expand Up @@ -2423,6 +2423,10 @@ let retype_expression ctx request_type function_args function_type expression_tr
let retypedObj = retype TCppDynamic enumObj in
CppEnumParameter( retypedObj, enumField, enumIndex ), cpp_cast_variant_type_of (cpp_type_of (get_nth_type enumField enumIndex))

| TEnumIndex enumObj ->
let retypedObj = retype TCppDynamic enumObj in
CppEnumIndex retypedObj, TCppScalar "int"

| TConst TThis ->
uses_this := Some !this_real;
CppThis(!this_real), if !this_real=ThisDynamic then TCppDynamic else cpp_type_of expr.etype
Expand Down Expand Up @@ -3162,7 +3166,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
Using the 'typedef hack', where we use typedef X<T> = T, allows the
haxe compiler to use these types interchangeably. We then work
out the correct way to convert between them when one is expected, but another provided.
TCppFunction: these do not really interact with the haxe function type, T
Since they are implemented with cpp::Function, conversion to/from Dynamic should happen automatically
CallableData<T> = T;
Expand Down Expand Up @@ -3394,9 +3398,9 @@ let gen_cpp_ast_expression_tree ctx class_name func_name function_args function_

| CppEnumIndex(obj) ->
gen obj;
(*if cpp_is_dynamic_type obj.cpptype then*)
if cpp_is_dynamic_type obj.cpptype then
out ".StaticCast< ::hx::EnumBase >()";
out "->getIndex()"
out "->_hx_getIndex()"

| CppNullAccess -> out ("hx::Throw(" ^ strq "Null access" ^ ")")
| CppFunction(func,_) ->
Expand Down Expand Up @@ -6233,7 +6237,7 @@ let generate_class_files baseCtx super_deps constructor_deps class_def inScripta
List.iter dump_script_static class_def.cl_ordered_statics;

output_cpp "static hx::ScriptNamedFunction __scriptableFunctions[] = {\n";
let dump_func f isStaticFlag =
let dump_func f isStaticFlag =
let s = try Hashtbl.find sigs f.cf_name with Not_found -> "v" in
output_cpp (" hx::ScriptNamedFunction(\"" ^ f.cf_name ^ "\",__s_" ^ f.cf_name ^ ",\"" ^ s ^ "\", " ^ isStaticFlag ^ " ),\n" )
in
Expand Down Expand Up @@ -7559,6 +7563,9 @@ class script_writer ctx filename asciiOut =
in
this#write ( (this#op IaEnumI) ^ (this#typeText (TEnum(enum,[])) ) ^ (string_of_int i) ^ "\n");
this#gen_expression expr;
| TEnumIndex expr ->
this#write ( (this#op IaCallMember) ^ (this#typeTextString "hx::EnumBase") ^ " " ^ (this#stringText "__Index") ^ "0" ^ (this#commentOf ("Enum index") ) ^ "\n");
this#gen_expression expr;
| TSwitch (condition,cases,optional_default) ->
this#write ( (this#op IaSwitch) ^ (string_of_int (List.length cases)) ^ " " ^
(match optional_default with None -> "0" | Some _ -> "1") ^ "\n");
Expand Down Expand Up @@ -7743,7 +7750,7 @@ class script_writer ctx filename asciiOut =
gen_expression e;

| CppCompare(_, left, right, op) ->
this#writeOpLine (IaBinOp op);
this#writeOpLine (IaBinOp op);
gen_expression left;
gen_expression right;

Expand Down Expand Up @@ -7870,7 +7877,7 @@ class script_writer ctx filename asciiOut =
| CppCastScalar(expr,_) -> match_expr expr
| CppCastVariant(expr) -> match_expr expr
| CppCastStatic(expr,_) -> match_expr expr
| CppNullAccess ->
| CppNullAccess ->
this#writeOpLine IaThrow;
this#begin_expr;
this#writeCppPos expression;
Expand Down Expand Up @@ -8137,13 +8144,13 @@ let generate_source ctx =
let id = gen_hash32 seed class_name in
(* reserve first 100 ids for runtime *)
if id < Int32.of_int 100 || Hashtbl.mem existingIds id then
makeId class_name (seed+100)
makeId class_name (seed+100)
else begin
Hashtbl.add existingIds id true;
Hashtbl.add ctx.ctx_type_ids class_name id;
end in
makeId name 0;

build_xml := !build_xml ^ (get_class_code class_def Meta.BuildXml);
if (has_init_field class_def) then
init_classes := class_def.cl_path :: !init_classes;
Expand Down
7 changes: 6 additions & 1 deletion src/generators/gencs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ let generate con =
| TObjectDecl _ -> write w "[ obj decl not supported ]"; if !strict_mode then assert false
| TFunction _ -> write w "[ func decl not supported ]"; if !strict_mode then assert false
| TEnumParameter _ -> write w "[ enum parameter not supported ]"; if !strict_mode then assert false
| TEnumIndex _ -> write w "[ enum index not supported ]"; if !strict_mode then assert false
)
and do_call w e el =
let params, el = extract_tparams [] el in
Expand Down Expand Up @@ -2727,7 +2728,11 @@ let generate con =
ClosuresToClass.configure gen closure_t;

let enum_base = (get_cl (get_type gen (["haxe";"lang"],"Enum")) ) in
EnumToClass2.configure gen enum_base;
let type_enumindex = mk_static_field_access_infer gen.gclasses.cl_type "enumIndex" null_pos [] in
let mk_enum_index_call e p =
mk (TCall (type_enumindex, [e])) gen.gcon.basic.tint p
in
EnumToClass2.configure gen enum_base mk_enum_index_call;

InterfaceVarsDeleteModf.configure gen;

Expand Down
2 changes: 2 additions & 0 deletions src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,8 @@ and eval_expr ctx e =
r
| _ ->
abort ("Unknown native call " ^ v.v_name) e.epos)
| TEnumIndex v ->
get_enum_index ctx v
| TCall ({ eexpr = TField (_,FStatic ({ cl_path = [],"Type" },{ cf_name = "enumIndex" })) },[{ eexpr = TCast(v,_) }]) when (match follow v.etype with TEnum _ -> true | _ -> false) ->
get_enum_index ctx v
| TCall ({ eexpr = TField (_,FStatic ({ cl_path = [],"Type" },{ cf_name = "enumIndex" })) },[v]) when (match follow v.etype with TEnum _ -> true | _ -> false) ->
Expand Down
7 changes: 6 additions & 1 deletion src/generators/genjava.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,7 @@ let generate con =
| TObjectDecl _ -> write w "[ obj decl not supported ]"; if !strict_mode then assert false
| TFunction _ -> write w "[ func decl not supported ]"; if !strict_mode then assert false
| TEnumParameter _ -> write w "[ enum parameter not supported ]"; if !strict_mode then assert false
| TEnumIndex _ -> write w "[ enum index not supported ]"; if !strict_mode then assert false
in
expr_s w e
in
Expand Down Expand Up @@ -2105,7 +2106,11 @@ let generate con =

let enum_base = (get_cl (get_type gen (["haxe";"lang"],"Enum")) ) in
let param_enum_base = (get_cl (get_type gen (["haxe";"lang"],"ParamEnum")) ) in
EnumToClass.configure gen (None) false true enum_base param_enum_base;
let type_enumindex = mk_static_field_access_infer gen.gclasses.cl_type "enumIndex" null_pos [] in
let mk_enum_index_call e p =
mk (TCall (type_enumindex, [e])) gen.gcon.basic.tint p
in
EnumToClass.configure gen false true enum_base param_enum_base mk_enum_index_call;

InterfaceVarsDeleteModf.configure gen;

Expand Down
4 changes: 4 additions & 0 deletions src/generators/genjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ and gen_expr ctx e =
print ctx "($_=";
gen_value ctx x;
print ctx ",$bind($_,$_%s))" (if Meta.has Meta.SelfCall f.cf_meta then "" else (field f.cf_name)))
| TEnumIndex x ->
gen_value ctx x;
print ctx "[1]"
| TEnumParameter (x,_,i) ->
gen_value ctx x;
print ctx "[%i]" (i + 2)
Expand Down Expand Up @@ -865,6 +868,7 @@ and gen_value ctx e =
| TBinop _
| TField _
| TEnumParameter _
| TEnumIndex _
| TTypeExpr _
| TParenthesis _
| TObjectDecl _
Expand Down
4 changes: 4 additions & 0 deletions src/generators/genlua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ and gen_expr ?(local=true) ctx e = begin
| TEnumParameter (x,_,i) ->
gen_value ctx x;
print ctx "[%i]" (i + 2)
| TEnumIndex x ->
gen_value ctx x;
print ctx "[1]"
| TField (x, (FInstance(_,_,f) | FStatic(_,f) | FAnon(f))) when Meta.has Meta.SelfCall f.cf_meta ->
gen_value ctx x;
| TField ({ eexpr = TConst(TInt _ | TFloat _| TString _| TBool _) } as e , ((FInstance _ | FAnon _) as ef)) ->
Expand Down Expand Up @@ -1041,6 +1044,7 @@ and gen_value ctx e =
| TBinop _
| TField _
| TEnumParameter _
| TEnumIndex _
| TTypeExpr _
| TParenthesis _
| TObjectDecl _
Expand Down
2 changes: 2 additions & 0 deletions src/generators/genneko.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ and gen_expr ctx e =
| _ -> assert false)
| TEnumParameter (e,_,i) ->
EArray (field p (gen_expr ctx e) "args",int p i),p
| TEnumIndex e ->
field p (gen_expr ctx e) "index"
| TField (e,f) ->
field p (gen_expr ctx e) (field_name f)
| TTypeExpr t ->
Expand Down
6 changes: 6 additions & 0 deletions src/generators/genphp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,11 @@ and gen_expr ctx e =
gen_value ctx e1;
spr ctx ")";
print ctx "->params[%d]" i;
| TEnumIndex e1 ->
spr ctx "_hx_deref(";
gen_value ctx e1;
spr ctx ")";
print ctx "->index";
| TField (e1,s) ->
gen_tfield ctx e e1 s
| TTypeExpr t ->
Expand Down Expand Up @@ -1804,6 +1809,7 @@ and gen_value ctx e =
| TArray _
| TBinop _
| TEnumParameter _
| TEnumIndex _
| TField _
| TParenthesis _
| TObjectDecl _
Expand Down
10 changes: 10 additions & 0 deletions src/generators/genphp7.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,7 @@ class code_writer (ctx:Common.context) hx_type_path php_name =
| TCast (expr, mtype) -> self#write_expr_cast expr mtype
| TMeta (_, expr) -> self#write_expr expr
| TEnumParameter (expr, constructor, index) -> self#write_expr_enum_parameter expr constructor index
| TEnumIndex expr -> self#write_expr_enum_index expr
);
expr_hierarchy <- List.tl expr_hierarchy
(**
Expand Down Expand Up @@ -2740,6 +2741,15 @@ class code_writer (ctx:Common.context) hx_type_path php_name =
| _ -> self#write_expr expr
);
self#write ("->params[" ^ (string_of_int index) ^ "]")
(**
Write TEnumIndex expression to output buffer
*)
method write_expr_enum_index expr =
(match expr.eexpr with
| TConst TNull -> self#write "(null)"
| _ -> self#write_expr expr
);
self#write "->index"
(**
Writes argument for function declarations or calls
*)
Expand Down
6 changes: 6 additions & 0 deletions src/generators/genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,10 @@ module Transformer = struct
let e1 = trans true [] e1 in
let p = { ae.a_expr with eexpr = TEnumParameter(e1.a_expr,ef,i)} in
lift true e1.a_blocks p
| (_, TEnumIndex e1) ->
let e1 = trans true [] e1 in
let p = { ae.a_expr with eexpr = TEnumIndex e1.a_expr } in
lift true e1.a_blocks p
| (true, TIf(econd, eif, eelse)) ->
(let econd1 = trans true [] econd in
let eif1 = trans true [] eif in
Expand Down Expand Up @@ -1189,6 +1193,8 @@ module Printer = struct
handle_keywords v.v_name
| TEnumParameter(e1,_,index) ->
Printf.sprintf "%s.params[%i]" (print_expr pctx e1) index
| TEnumIndex e1 ->
Printf.sprintf "%s.index" (print_expr pctx e1)
| TArray(e1,e2) when (is_type1 "" "list")(e1.etype) || is_underlying_array e1.etype ->
print_tarray_list pctx e1 e2
| TArray({etype = t} as e1,e2) when is_anon_or_dynamic t ->
Expand Down
Loading

0 comments on commit c3dc791

Please sign in to comment.