Skip to content

Commit

Permalink
tf_args expr (#7439)
Browse files Browse the repository at this point in the history
* change tf_args to hold a texpr

see #4287

* allow enum fields as default function argument values

see #4287

* [php] fix enum instances as default values for function arguments (#57)

* [php] fix enum instances as default values for function arguments

* minor

* support expr default args on As3

* apply Hugh patch

* apply Hugh patch

* fix HL
  • Loading branch information
Simn committed Oct 10, 2018
1 parent ade4c02 commit c76e7b3
Show file tree
Hide file tree
Showing 22 changed files with 208 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/codegen/gencommon/closuresToClass.ml
Expand Up @@ -730,7 +730,7 @@ struct
| Some const ->
{ eexpr = TIf(
{ elocal with eexpr = TBinop(Ast.OpEq, elocal, null elocal.etype elocal.epos); etype = basic.tbool },
{ elocal with eexpr = TConst(const); etype = const_type basic const t },
const,
Some ( mk_cast t elocal )
); etype = t; epos = elocal.epos }
in
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/gencommon/enumToClass.ml
Expand Up @@ -138,7 +138,7 @@ struct
let cf = mk_class_field name ef_type true pos (Method MethNormal) cf_params in
cf.cf_meta <- [];

let tf_args = List.map (fun (name,opt,t) -> (alloc_var name t, if opt then Some TNull else None) ) params in
let tf_args = List.map (fun (name,opt,t) -> (alloc_var name t, if opt then Some (Texpr.Builder.make_null t null_pos) else None) ) params in
let arr_decl = mk_nativearray_decl gen t_dynamic (List.map (fun (v,_) -> mk_local v pos) tf_args) pos in
let expr = {
eexpr = TFunction({
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/gencommon/gencommon.ml
Expand Up @@ -1315,6 +1315,6 @@ let get_type gen path =
try Hashtbl.find gen.gtypes path with | Not_found -> raise (TypeNotFound path)


let fun_args (l : (tvar * tconstant option) list)=
let fun_args l =
List.map (fun (v,s) -> (v.v_name, (s <> None), v.v_type)) l

2 changes: 1 addition & 1 deletion src/core/json/genjson.ml
Expand Up @@ -300,7 +300,7 @@ and generate_tconstant ctx ct =
and generate_tfunction ctx tf =
let generate_arg (v,cto) = jobject [
"v",generate_tvar ctx v;
"value",jopt (generate_tconstant ctx) cto;
"value",jopt (generate_texpr ctx) cto;
] in
jobject [
"args",jlist generate_arg tf.tf_args;
Expand Down
2 changes: 1 addition & 1 deletion src/core/texpr.ml
Expand Up @@ -284,7 +284,7 @@ let set_default basic a c p =
let t = a.v_type in
let ve = mk (TLocal a) t p in
let cond = TBinop (OpEq,ve,mk (TConst TNull) t p) in
mk (TIf (Builder.mk_parent (mk cond basic.tbool p), mk (TBinop (OpAssign,ve,mk (TConst c) t p)) t p,None)) basic.tvoid p
mk (TIf (Builder.mk_parent (mk cond basic.tbool p), mk (TBinop (OpAssign,ve,c)) t p,None)) basic.tvoid p

(*
Tells if the constructor might be called without any issue whatever its parameters
Expand Down
10 changes: 5 additions & 5 deletions src/core/type.ml
Expand Up @@ -112,7 +112,7 @@ and tvar = {
}

and tfunc = {
tf_args : (tvar * tconstant option) list;
tf_args : (tvar * texpr option) list;
tf_type : t;
tf_expr : texpr;
}
Expand Down Expand Up @@ -1166,7 +1166,7 @@ let rec s_expr s_type e =
| Prefix -> sprintf "(%s %s)" (s_unop op) (loop e)
| Postfix -> sprintf "(%s %s)" (loop e) (s_unop op))
| TFunction f ->
let args = slist (fun (v,o) -> sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match o with None -> "" | Some c -> " = " ^ s_const c)) f.tf_args in
let args = slist (fun (v,o) -> sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match o with None -> "" | Some c -> " = " ^ loop c)) f.tf_args in
sprintf "Function(%s) : %s = %s" args (s_type f.tf_type) (loop f.tf_expr)
| TVar (v,eo) ->
sprintf "Vars %s" (sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match eo with None -> "" | Some e -> " = " ^ loop e))
Expand Down Expand Up @@ -1229,7 +1229,7 @@ let rec s_expr_pretty print_var_ids tabs top_level s_type e =
| Prefix -> sprintf "%s %s" (s_unop op) (loop e)
| Postfix -> sprintf "%s %s" (loop e) (s_unop op))
| TFunction f ->
let args = clist (fun (v,o) -> sprintf "%s:%s%s" (local v) (s_type v.v_type) (match o with None -> "" | Some c -> " = " ^ s_const c)) f.tf_args in
let args = clist (fun (v,o) -> sprintf "%s:%s%s" (local v) (s_type v.v_type) (match o with None -> "" | Some c -> " = " ^ loop c)) f.tf_args in
sprintf "%s(%s) %s" (if top_level then "" else "function") args (loop f.tf_expr)
| TVar (v,eo) ->
sprintf "var %s" (sprintf "%s%s" (local v) (match eo with None -> "" | Some e -> " = " ^ loop e))
Expand Down Expand Up @@ -1320,7 +1320,7 @@ let rec s_expr_ast print_var_ids tabs s_type e =
| TNew (c,tl,el) -> tag "New" ((s_type (TInst(c,tl))) :: (List.map loop el))
| TFunction f ->
let arg (v,cto) =
tag "Arg" ~t:(Some v.v_type) ~extra_tabs:"\t" (match cto with None -> [local v None] | Some ct -> [local v None;const ct None])
tag "Arg" ~t:(Some v.v_type) ~extra_tabs:"\t" (match cto with None -> [local v None] | Some ct -> [local v None;loop ct])
in
tag "Function" ((List.map arg f.tf_args) @ [loop f.tf_expr])
| TVar (v,eo) -> var v (match eo with None -> [] | Some e -> [loop e])
Expand Down Expand Up @@ -2697,7 +2697,7 @@ module TExprToExpr = struct
| TNew (c,pl,el) -> ENew ((match (try convert_type (TInst (c,pl)) with Exit -> convert_type (TInst (c,[]))) with CTPath p -> p,null_pos | _ -> assert false),List.map convert_expr el)
| TUnop (op,p,e) -> EUnop (op,p,convert_expr e)
| TFunction f ->
let arg (v,c) = (v.v_name,v.v_pos), false, v.v_meta, mk_type_hint v.v_type null_pos, (match c with None -> None | Some c -> Some (EConst (tconst_to_const c),e.epos)) in
let arg (v,c) = (v.v_name,v.v_pos), false, v.v_meta, mk_type_hint v.v_type null_pos, (match c with None -> None | Some c -> Some (convert_expr c)) in
EFunction (None,{ f_params = []; f_args = List.map arg f.tf_args; f_type = mk_type_hint f.tf_type null_pos; f_expr = Some (convert_expr f.tf_expr) })
| TVar (v,eo) ->
EVars ([(v.v_name,v.v_pos), v.v_final, mk_type_hint v.v_type v.v_pos, eopt eo])
Expand Down
9 changes: 4 additions & 5 deletions src/filters/defaultArguments.ml
Expand Up @@ -33,8 +33,7 @@ let gen_check basic t nullable_var const pos =
(is_null t1) <> (is_null t2)
in

let const_t = const_type basic const t in
let const = mk (TConst const) const_t pos in
let const_t = const.etype in
let const = if needs_cast t const_t then mk_cast const t pos else const in

let arg = make_local nullable_var pos in
Expand All @@ -45,10 +44,10 @@ let gen_check basic t nullable_var const pos =

let add_opt com block pos (var,opt) =
match opt with
| None | Some TNull ->
| None | Some {eexpr = TConst TNull} ->
(var,opt)
| Some (TString str) ->
block := Texpr.set_default com.basic var (TString str) pos :: !block;
| Some ({eexpr = TConst (TString str)} as e) ->
block := Texpr.set_default com.basic var e pos :: !block;
(var, opt)
| Some const ->
let basic = com.basic in
Expand Down
14 changes: 8 additions & 6 deletions src/generators/genas3.ml
Expand Up @@ -374,18 +374,18 @@ let gen_constant ctx p = function
| TThis -> spr ctx (this ctx)
| TSuper -> spr ctx "super"

let gen_function_header ctx name f params p =
let rec gen_function_header ctx name f params p =
let old = ctx.in_value in
let old_t = ctx.local_types in
let old_bi = ctx.block_inits in
ctx.in_value <- None;
ctx.local_types <- List.map snd params @ ctx.local_types;
let init () =
List.iter (fun (v,o) -> match o with
| Some c when is_nullable v.v_type && c <> TNull ->
| Some c when is_nullable v.v_type && c.eexpr <> TConst TNull ->
newline ctx;
print ctx "if(%s==null) %s=" v.v_name v.v_name;
gen_constant ctx p c;
gen_expr ctx c;
| _ -> ()
) f.tf_args;
ctx.block_inits <- None;
Expand All @@ -410,9 +410,11 @@ let gen_function_header ctx name f params p =
match c with
| None ->
if ctx.constructor_block then print ctx " = %s" (default_value tstr);
| Some c ->
| Some ({eexpr = TConst _ } as e) ->
spr ctx " = ";
gen_constant ctx p c
gen_expr ctx e
| _ ->
spr ctx " = null"
) f.tf_args;
print ctx ") : %s " (type_str ctx f.tf_type p);
(fun () ->
Expand All @@ -421,7 +423,7 @@ let gen_function_header ctx name f params p =
ctx.block_inits <- old_bi;
)

let rec gen_call ctx e el r =
and gen_call ctx e el r =
match e.eexpr , el with
| TCall (x,_) , el ->
spr ctx "(";
Expand Down

0 comments on commit c76e7b3

Please sign in to comment.