2021-02-12 19:20:14 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Nicolas Chataing <nicolas.chataing@ens.fr> Denis Merigoux
|
|
|
|
<denis.merigoux@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. *)
|
|
|
|
|
|
|
|
(** Builds a context that allows for mapping each name to a precise uid, taking
|
|
|
|
lexical scopes into account *)
|
|
|
|
|
|
|
|
open Utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
(** {1 Name resolution context} *)
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type ident = string
|
|
|
|
type typ = Scopelang.Ast.typ
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-01-05 17:57:18 +03:00
|
|
|
type unique_rulename =
|
|
|
|
| Ambiguous of Pos.t list
|
2022-05-30 12:20:48 +03:00
|
|
|
| Unique of Desugared.Ast.RuleName.t Marked.pos
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-01-03 20:39:59 +03:00
|
|
|
type scope_def_context = {
|
|
|
|
default_exception_rulename : unique_rulename option;
|
2022-01-04 20:19:15 +03:00
|
|
|
label_idmap : Desugared.Ast.LabelName.t Desugared.Ast.IdentMap.t;
|
2022-01-03 20:39:59 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type scope_context = {
|
2022-02-28 19:19:06 +03:00
|
|
|
var_idmap : Desugared.Ast.ScopeVar.t Desugared.Ast.IdentMap.t;
|
|
|
|
(** Scope variables *)
|
2022-01-03 20:39:59 +03:00
|
|
|
scope_defs_contexts : scope_def_context Desugared.Ast.ScopeDefMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** What is the default rule to refer to for unnamed exceptions, if any *)
|
2022-08-17 18:14:29 +03:00
|
|
|
sub_scopes_idmap : SubScopeName.t Desugared.Ast.IdentMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Sub-scopes variables *)
|
2022-08-12 23:42:39 +03:00
|
|
|
sub_scopes : ScopeName.t Scopelang.Ast.SubScopeMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** To what scope sub-scopes refer to? *)
|
2021-02-11 20:48:59 +03:00
|
|
|
}
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Inside a scope, we distinguish between the variables and the subscopes. *)
|
|
|
|
|
2022-08-17 18:14:29 +03:00
|
|
|
type struct_context = typ Marked.pos StructFieldMap.t
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Types of the fields of a struct *)
|
|
|
|
|
2022-08-17 18:14:29 +03:00
|
|
|
type enum_context = typ Marked.pos EnumConstructorMap.t
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Types of the payloads of the cases of an enum *)
|
|
|
|
|
2022-02-05 02:04:19 +03:00
|
|
|
type var_sig = {
|
2022-05-30 12:20:48 +03:00
|
|
|
var_sig_typ : typ Marked.pos;
|
2022-02-05 02:04:19 +03:00
|
|
|
var_sig_is_condition : bool;
|
2022-02-07 12:30:36 +03:00
|
|
|
var_sig_io : Ast.scope_decl_context_io;
|
2022-02-28 17:40:19 +03:00
|
|
|
var_sig_states_idmap : Desugared.Ast.StateName.t Desugared.Ast.IdentMap.t;
|
|
|
|
var_sig_states_list : Desugared.Ast.StateName.t list;
|
2022-02-05 02:04:19 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type context = {
|
2022-02-28 20:34:32 +03:00
|
|
|
local_var_idmap : Desugared.Ast.Var.t Desugared.Ast.IdentMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Inside a definition, local variables can be introduced by functions
|
|
|
|
arguments or pattern matching *)
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_idmap : ScopeName.t Desugared.Ast.IdentMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The names of the scopes *)
|
2022-08-12 23:42:39 +03:00
|
|
|
struct_idmap : StructName.t Desugared.Ast.IdentMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The names of the structs *)
|
2022-08-16 11:04:01 +03:00
|
|
|
field_idmap : StructFieldName.t StructMap.t Desugared.Ast.IdentMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The names of the struct fields. Names of fields can be shared between
|
|
|
|
different structs *)
|
2022-08-12 23:42:39 +03:00
|
|
|
enum_idmap : EnumName.t Desugared.Ast.IdentMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The names of the enums *)
|
2022-08-16 11:04:01 +03:00
|
|
|
constructor_idmap : EnumConstructor.t EnumMap.t Desugared.Ast.IdentMap.t;
|
2021-02-12 19:20:14 +03:00
|
|
|
(** The names of the enum constructors. Constructor names can be shared
|
|
|
|
between different enums *)
|
|
|
|
scopes : scope_context Scopelang.Ast.ScopeMap.t;
|
|
|
|
(** For each scope, its context *)
|
2022-08-16 11:04:01 +03:00
|
|
|
structs : struct_context StructMap.t; (** For each struct, its context *)
|
|
|
|
enums : enum_context EnumMap.t; (** For each enum, its context *)
|
2022-02-28 19:19:06 +03:00
|
|
|
var_typs : var_sig Desugared.Ast.ScopeVarMap.t;
|
2022-02-05 02:04:19 +03:00
|
|
|
(** The signatures of each scope variable declared *)
|
2021-02-11 20:48:59 +03:00
|
|
|
}
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Main context used throughout {!module: Surface.Desugaring} *)
|
|
|
|
|
|
|
|
(** {1 Helpers} *)
|
|
|
|
|
|
|
|
val raise_unsupported_feature : string -> Pos.t -> 'a
|
|
|
|
(** Temporary function raising an error message saying that a feature is not
|
|
|
|
supported yet *)
|
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
val raise_unknown_identifier : string -> ident Marked.pos -> 'a
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Function to call whenever an identifier used somewhere has not been declared
|
|
|
|
in the program previously *)
|
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
val get_var_typ : context -> Desugared.Ast.ScopeVar.t -> typ Marked.pos
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Gets the type associated to an uid *)
|
|
|
|
|
2022-02-28 19:19:06 +03:00
|
|
|
val is_var_cond : context -> Desugared.Ast.ScopeVar.t -> bool
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-02-28 19:19:06 +03:00
|
|
|
val get_var_io :
|
|
|
|
context -> Desugared.Ast.ScopeVar.t -> Ast.scope_decl_context_io
|
2022-02-05 02:04:19 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val get_var_uid :
|
2022-08-16 11:04:01 +03:00
|
|
|
ScopeName.t -> context -> ident Marked.pos -> Desugared.Ast.ScopeVar.t
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Get the variable uid inside the scope given in argument *)
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val get_subscope_uid :
|
2022-08-17 18:14:29 +03:00
|
|
|
ScopeName.t -> context -> ident Marked.pos -> SubScopeName.t
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Get the subscope uid inside the scope given in argument *)
|
|
|
|
|
2022-08-12 23:42:39 +03:00
|
|
|
val is_subscope_uid : ScopeName.t -> context -> ident -> bool
|
2021-02-12 19:20:14 +03:00
|
|
|
(** [is_subscope_uid scope_uid ctxt y] returns true if [y] belongs to the
|
|
|
|
subscopes of [scope_uid]. *)
|
|
|
|
|
2022-08-16 11:04:01 +03:00
|
|
|
val belongs_to : context -> Desugared.Ast.ScopeVar.t -> ScopeName.t -> bool
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Checks if the var_uid belongs to the scope scope_uid *)
|
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
val get_def_typ : context -> Desugared.Ast.ScopeDef.t -> typ Marked.pos
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Retrieves the type of a scope definition from the context *)
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val is_def_cond : context -> Desugared.Ast.ScopeDef.t -> bool
|
2022-05-30 12:20:48 +03:00
|
|
|
val is_type_cond : Ast.typ Marked.pos -> bool
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-06-03 17:40:03 +03:00
|
|
|
val add_def_local_var : context -> ident -> context * Desugared.Ast.Var.t
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Adds a binding to the context *)
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val get_def_key :
|
2022-02-28 19:19:06 +03:00
|
|
|
Ast.qident ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Ast.ident Marked.pos option ->
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeName.t ->
|
2022-02-28 19:19:06 +03:00
|
|
|
context ->
|
|
|
|
Pos.t ->
|
|
|
|
Desugared.Ast.ScopeDef.t
|
|
|
|
(** Usage: [get_def_key var_name var_state scope_uid ctxt pos]*)
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
(** {1 API} *)
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
val form_context : Ast.program -> context
|
2021-02-12 19:20:14 +03:00
|
|
|
(** Derive the context from metadata, in one pass over the declarations *)
|