diff --git a/compiler/scalc/ast.ml b/compiler/scalc/ast.ml index 862f6bdaa..234527f75 100644 --- a/compiler/scalc/ast.ml +++ b/compiler/scalc/ast.ml @@ -97,11 +97,12 @@ type scope_body = { scope_body_name : ScopeName.t; scope_body_var : FuncName.t; scope_body_func : func; + scope_body_visibility : visibility; } type code_item = - | SVar of { var : VarName.t; expr : expr; typ : typ } - | SFunc of { var : FuncName.t; func : func } + | SVar of { var : VarName.t; expr : expr; typ : typ; visibility : visibility } + | SFunc of { var : FuncName.t; func : func; visibility : visibility } | SScope of scope_body type ctx = { decl_ctx : decl_ctx; modules : VarName.t ModuleName.Map.t } diff --git a/compiler/scalc/from_lcalc.ml b/compiler/scalc/from_lcalc.ml index 724806fca..c2feb32f9 100644 --- a/compiler/scalc/from_lcalc.ml +++ b/compiler/scalc/from_lcalc.ml @@ -617,9 +617,10 @@ let translate_program ~(config : translation_config) (p : 'm L.program) : func_return_typ = TStruct body.scope_body_output_struct, input_pos; }; + scope_body_visibility = body.scope_body_visibility; } :: rev_items ) - | Topdef (name, topdef_ty, _vis, (EAbs abs, m)) -> + | Topdef (name, topdef_ty, visibility, (EAbs abs, m)) -> (* Toplevel function def *) let (block, expr, _ren_ctx_inner), args_id = let args_a, expr, ctxt_inner = unmbind ctxt abs.binder in @@ -657,9 +658,10 @@ let translate_program ~(config : translation_config) (p : 'm L.program) : | TAny, pos_any -> TAny, pos_any | _ -> failwith "should not happen"); }; + visibility; } :: rev_items ) - | Topdef (name, topdef_ty, _vis, expr) -> + | Topdef (name, topdef_ty, visibility, expr) -> (* Toplevel constant def *) let block, expr, _ren_ctx_inner = let ctxt = { ctxt with context_name = TopdefName.base name } in @@ -673,7 +675,9 @@ let translate_program ~(config : translation_config) (p : 'm L.program) : statements, we lift its computation into an auxiliary function *) let rev_items, ctxt = if (block :> (A.stmt * Pos.t) list) = [] then - A.SVar { var = var_id; expr; typ = topdef_ty } :: rev_items, ctxt + ( A.SVar { var = var_id; expr; typ = topdef_ty; visibility } + :: rev_items, + ctxt ) else let pos = Mark.get expr in let func_name, ctxt = @@ -687,6 +691,7 @@ let translate_program ~(config : translation_config) (p : 'm L.program) : var = var_id; expr = A.EApp { f = EFunc func_id, pos; args = [] }, pos; typ = topdef_ty; + visibility; } :: A.SFunc { @@ -699,6 +704,7 @@ let translate_program ~(config : translation_config) (p : 'm L.program) : ~tail:[A.SReturn expr, Mark.get expr]; A.func_return_typ = topdef_ty; }; + visibility = Private; } :: rev_items, ctxt ) diff --git a/compiler/scalc/print.ml b/compiler/scalc/print.ml index 90ec1fb4e..03d659997 100644 --- a/compiler/scalc/print.ml +++ b/compiler/scalc/print.ml @@ -202,14 +202,14 @@ let format_item decl_ctx ?debug ppf def = Print.keyword ppf "let "; let () = match def with - | SVar { var; expr; typ = _ } -> + | SVar { var; expr; typ = _; visibility = _ } -> format_var_name ppf var; Print.punctuation ppf " ="; Format.pp_close_box ppf (); Format.pp_print_space ppf (); format_expr decl_ctx ?debug ppf expr | SScope { scope_body_var = var; scope_body_func = func; _ } - | SFunc { var; func } -> + | SFunc { var; func; visibility = _ } -> format_func_name ppf var; Format.pp_print_list (fun ppf (arg, ty) -> diff --git a/compiler/scalc/to_c.ml b/compiler/scalc/to_c.ml index f8b33e065..9cdd1fa9c 100644 --- a/compiler/scalc/to_c.ml +++ b/compiler/scalc/to_c.ml @@ -676,6 +676,7 @@ let format_main (fmt : Format.formatter) (p : Ast.program) = scope_body_func = { func_params = [(_, (TStruct ts, _))]; _ }; scope_body_var = var; scope_body_name = name; + scope_body_visibility = Public; } -> let input_struct = StructName.Map.find ts p.ctx.decl_ctx.ctx_structs @@ -750,18 +751,12 @@ let format_program List.fold_left (fun env code_item -> match code_item with - | SVar { var; expr; typ } -> + | SVar { var; expr; typ; visibility } -> (* Global variables are turned into inline functions without parameters that perform lazy evaluation: {[ inline foo_type foo() { static foo_type foo = NULL; return (foo ? foo : foo = foo_init()); } ]} NOTE: "inline" is not defined in C89 *) - let public = - (* TODO: Ugh! Pass this info into scalc ! *) - Re.( - execp - (compile (seq [str "__"; diff any digit])) - (VarName.to_string var)) - in + let public = visibility = Public in ppboth_if public (fun ppf -> Format.fprintf ppf "@,@[@[%s%a" (if public then "" else "static") @@ -782,20 +777,20 @@ let format_program expr; Format.fprintf ppc "@;<1 -2>}@]@,"; { env with global_vars = VarName.Set.add var env.global_vars } - | SFunc { var; func } - | SScope { scope_body_var = var; scope_body_func = func; _ } -> + | SFunc { var; func; visibility } + | SScope + { + scope_body_var = var; + scope_body_func = func; + scope_body_visibility = visibility; + _; + } -> let { func_params; func_body; func_return_typ } = func in let local_vars = VarName.Set.of_list (List.map (fun (v, _) -> Mark.remove v) func_params) in - let public = - (* TODO: Ugh! Pass this info into scalc ! *) - Re.( - execp - (compile (seq [str "__"; diff any digit])) - (FuncName.to_string var)) - in + let public = visibility = Public in ppboth_if public (fun ppf -> Format.fprintf ppf "@,@[@[%s%a@ @[(%a)@]@]" (if public then "" else "static ") diff --git a/compiler/scalc/to_python.ml b/compiler/scalc/to_python.ml index 15eb3a43b..1e39d3e4d 100644 --- a/compiler/scalc/to_python.ml +++ b/compiler/scalc/to_python.ml @@ -554,10 +554,10 @@ let format_ctx (type_ordering : TypeIdent.t list) (fmt : Format.formatter) ctx : (type_ordering @ scope_structs) let format_code_item ctx fmt = function - | SVar { var; expr; typ = _ } -> + | SVar { var; expr; typ = _; visibility = _ } -> Format.fprintf fmt "@[%a = (@,%a@;<0 -4>)@]@," VarName.format var (format_expression ctx) expr - | SFunc { var; func } + | SFunc { var; func; visibility = _ } | SScope { scope_body_var = var; scope_body_func = func; _ } -> let { Ast.func_params; Ast.func_body; _ } = func in Format.fprintf fmt "@[@[def %a(@,%a@;<0 -2>):@]@ %a@]@,"