mirror of
https://github.com/CatalaLang/catala.git
synced 2024-11-08 07:51:43 +03:00
b9156bb60e
Previously we had some heuristics in the backends trying to achieve this with a lot of holes ; this should be much more solid, relying on `Bindlib` to do the correct renamings. **Note1**: it's not plugged into the backends other than OCaml at the moment. **Note2**: the related, obsolete heuristics haven't been cleaned out yet **Note3**: we conservatively suppose a single namespace at the moment. This is required for e.g. Python, but it forces vars named like struct fields to be renamed, which is more verbose in e.g. OCaml. The renaming engine could be improved to support different namespaces, with a way to select how to route the different kinds of identifiers into them. Similarly, customisation for what needs to be uppercase or lowercase is not available yet. **Note4**: besides excluding keywords, we should also be careful to exclude (or namespace): - the idents used in the runtime (e.g. `o_add_int_int`) - the dynamically generated idents (e.g. `embed_*`) **Note5**: module names themselves aren't handled yet. The reason is that they must be discoverable by the user, and even need to match the filenames, etc. In other words, imagine that `Mod` is a keyword in the target language. You can't rename a module called `Mod` to `Mod1` without knowing the whole module context, because that would destroy the mapping for a module already called `Mod1`. A reliable solution would be to translate all module names to e.g. `CatalaModule_*`, which we can assume will never conflict with any built-in, and forbid idents starting with that prefix. We may also want to restrict their names to ASCII ? Currently we use a projection, but what if I have two modules called `Là` and `La` ?
90 lines
3.4 KiB
OCaml
90 lines
3.4 KiB
OCaml
(* This file is part of the Catala compiler, a specification language for tax
|
|
and social benefits computation rules. Copyright (C) 2020-2022 Inria,
|
|
contributor: Denis Merigoux <denis.merigoux@inria.fr>, Alain Delaët-Tixeuil
|
|
<alain.delaet--tixeuil@inria.fr>, Louis Gesbert <louis.gesbert@inria.fr>
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
License for the specific language governing permissions and limitations under
|
|
the License. *)
|
|
|
|
(** Functions handling the code item structures of [shared_ast], in particular
|
|
the scopes *)
|
|
|
|
open Catala_utils
|
|
open Definitions
|
|
|
|
(** {2 Traversal functions} *)
|
|
|
|
val map_exprs_in_lets :
|
|
?typ:(typ -> typ) ->
|
|
f:('expr1 -> 'expr2 boxed) ->
|
|
varf:('expr1 Var.t -> 'expr2 Var.t) ->
|
|
'expr1 scope_body_expr ->
|
|
'expr2 scope_body_expr Bindlib.box
|
|
(** Usage
|
|
[map_exprs_in_lets ~f:(fun e -> ...) ~varf:(fun var -> ...) scope_body_expr],
|
|
where [e] is the right-hand-side of a scope let or the result of the scope
|
|
body, and [var] represents the left-hand-side variable of a scope let.
|
|
[~varf] is usually the identity function or [Var.translate] when the map
|
|
sends the expression to a new flavor of the shared AST. If [~reset_types] is
|
|
activated, then the resulting types in the scope let left-hand-sides will be
|
|
reset to [TAny]. *)
|
|
|
|
val map_exprs :
|
|
?typ:(typ -> typ) ->
|
|
f:('expr1 -> 'expr2 boxed) ->
|
|
varf:('expr1 Var.t -> 'expr2 Var.t) ->
|
|
'expr1 code_item_list ->
|
|
'expr2 code_item_list Bindlib.box
|
|
(** This is the main map visitor for all the expressions inside all the scopes
|
|
of the program. *)
|
|
|
|
val map_last_item :
|
|
varf:(('a, 'm) naked_gexpr Bindlib.var -> 'e2 Bindlib.var) ->
|
|
('a, 'm) naked_gexpr list ->
|
|
'e2 list Bindlib.box
|
|
|
|
(** Helper function to handle the [code_item_list] terminator when manually
|
|
mapping on [code_item_list] *)
|
|
|
|
val fold_exprs :
|
|
f:('acc -> 'expr -> typ -> 'acc) -> init:'acc -> 'expr code_item_list -> 'acc
|
|
|
|
(** {2 Conversions} *)
|
|
|
|
val to_expr : decl_ctx -> ('a any, 'm) gexpr scope_body -> ('a, 'm) boxed_gexpr
|
|
(** Usage: [to_expr ctx body scope_position] where [scope_position] corresponds
|
|
to the line of the scope declaration for instance. *)
|
|
|
|
val unfold :
|
|
decl_ctx -> ((_, 'm) gexpr as 'e) code_item_list -> ScopeName.t -> 'e boxed
|
|
|
|
val typ : _ scope_body -> typ
|
|
(** builds the arrow type for the specified scope *)
|
|
|
|
val input_type : typ -> Runtime.io_input Mark.pos -> typ
|
|
(** Returns the correct input type for scope input variables: this is [typ] for
|
|
non-reentrant variables, but for reentrant variables, it is nested in a
|
|
[TDefault], which only applies to the return type on functions. Note that
|
|
this doesn't take thunking into account (thunking is added during the
|
|
scopelang->dcalc translation) *)
|
|
|
|
val rename_ids :
|
|
Expr.Renaming.context ->
|
|
((_ any, 'm) gexpr as 'e) code_item_list ->
|
|
'e code_item_list
|
|
|
|
(** {2 Analysis and tests} *)
|
|
|
|
val free_vars_body_expr : 'e scope_body_expr -> 'e Var.Set.t
|
|
val free_vars_item : 'e code_item -> 'e Var.Set.t
|
|
val free_vars : 'e code_item_list -> 'e Var.Set.t
|