Desugared.Name_resolution
Builds a context that allows for mapping each name to a precise uid, taking lexical scopes into account
type unique_rulename =
| Ambiguous of Catala_utils.Pos.t list
| Unique of Shared_ast.RuleName.t Catala_utils.Mark.pos
type scope_def_context = {
default_exception_rulename : unique_rulename option;
label_idmap : Shared_ast.LabelName.t Shared_ast.Ident.Map.t;
}
type scope_context = {
var_idmap : Shared_ast.scope_var_or_subscope Shared_ast.Ident.Map.t;
All variables, including scope variables and subscopes
*)scope_defs_contexts : scope_def_context Ast.ScopeDef.Map.t;
What is the default rule to refer to for unnamed exceptions, if any
*)scope_in_struct : Shared_ast.StructName.t;
scope_out_struct : Shared_ast.StructName.t;
sub_scopes : Shared_ast.ScopeName.Set.t;
Other scopes referred to by this scope. Used for dependency analysis
*)scope_visibility : Shared_ast.visibility;
}
Inside a scope, we distinguish between the variables and the subscopes.
type struct_context = Shared_ast.typ Shared_ast.StructField.Map.t
Types of the fields of a struct
type enum_context = Shared_ast.typ Shared_ast.EnumConstructor.Map.t
Types of the payloads of the cases of an enum
type var_sig = {
var_sig_typ : Shared_ast.typ;
var_sig_is_condition : bool;
var_sig_parameters : (Catala_utils.Uid.MarkedString.info * Shared_ast.typ) list
Catala_utils.Mark.pos
option;
var_sig_io : Surface.Ast.scope_decl_context_io;
var_sig_states_idmap : Shared_ast.StateName.t Shared_ast.Ident.Map.t;
var_sig_states_list : Shared_ast.StateName.t list;
}
type typedef =
| TStruct of Shared_ast.StructName.t
| TEnum of Shared_ast.EnumName.t
| TScope of Shared_ast.ScopeName.t * Shared_ast.scope_info
Implicitly defined output struct
*)Capitalised type names share a namespace on the user side, but may correspond to only one of the following
type module_context = {
current_module : Shared_ast.ModuleName.t option;
The current path being processed. Used for generating the Uids.
*)typedefs : typedef Shared_ast.Ident.Map.t;
Gathers the names of the scopes, structs and enums
*)field_idmap : Shared_ast.StructField.t Shared_ast.StructName.Map.t
Shared_ast.Ident.Map.t;
The names of the struct fields. Names of fields can be shared between different structs. Note that fields from submodules are included here for the root module, because disambiguating there is helpful.
*)constructor_idmap : Shared_ast.EnumConstructor.t Shared_ast.EnumName.Map.t
Shared_ast.Ident.Map.t;
The names of the enum constructors. Constructor names can be shared between different enums. Note that constructors from its submodules are included here for the root module, because disambiguating there is helpful.
*)topdefs : Shared_ast.TopdefName.t Shared_ast.Ident.Map.t;
Global definitions
*)used_modules : Shared_ast.ModuleName.t Shared_ast.Ident.Map.t;
Module aliases and the modules they point to
*)is_external : bool;
}
Context for name resolution, valid within a given module
type context = {
scopes : scope_context Shared_ast.ScopeName.Map.t;
For each scope, its context
*)topdefs : (Shared_ast.typ * Shared_ast.visibility) Shared_ast.TopdefName.Map.t;
Types associated with the global definitions
*)structs : (struct_context * Shared_ast.visibility) Shared_ast.StructName.Map.t;
For each struct, its context
*)enums : (enum_context * Shared_ast.visibility) Shared_ast.EnumName.Map.t;
For each enum, its context
*)var_typs : var_sig Shared_ast.ScopeVar.Map.t;
The signatures of each scope variable declared
*)modules : module_context Shared_ast.ModuleName.Map.t;
The map to the interfaces of all modules (transitively) used by the program. References are made through local.used_modules
local : module_context;
Local context of the root module corresponding to the program being analysed
*)}
Global context used throughout Surface.Desugaring
val raise_unsupported_feature : string -> Catala_utils.Pos.t -> 'a
Temporary function raising an error message saying that a feature is not supported yet
val raise_unknown_identifier :
string ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
'a
Function to call whenever an identifier used somewhere has not been declared in the program previously
val get_modname :
context ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
Shared_ast.ModuleName.t
Emits a user error if the module name is not found
val get_module_ctx :
context ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
context
Emits a user error if the module name is not found
val get_var_typ : context -> Shared_ast.ScopeVar.t -> Shared_ast.typ
Gets the type associated to an uid
val is_var_cond : context -> Shared_ast.ScopeVar.t -> bool
val get_var_io :
context ->
Shared_ast.ScopeVar.t ->
Surface.Ast.scope_decl_context_io
val get_scope_context : context -> Shared_ast.ScopeName.t -> scope_context
Get the corresponding scope context from the context, looking up into nested submodules as necessary, following the path information in the scope name
val get_var_uid :
Shared_ast.ScopeName.t ->
context ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
Shared_ast.ScopeVar.t
Get the variable uid inside the scope given in argument
val get_subscope_uid :
Shared_ast.ScopeName.t ->
context ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
Shared_ast.ScopeVar.t
Get the subscope uid inside the scope given in argument
val is_subscope_uid :
Shared_ast.ScopeName.t ->
context ->
Shared_ast.Ident.t ->
bool
is_subscope_uid scope_uid ctxt y
returns true if y
belongs to the subscopes of scope_uid
.
val belongs_to :
context ->
Shared_ast.ScopeVar.t ->
Shared_ast.ScopeName.t ->
bool
Checks if the var_uid belongs to the scope scope_uid
val get_params :
context ->
Ast.ScopeDef.t ->
(Catala_utils.Uid.MarkedString.info * Shared_ast.typ) list
Catala_utils.Mark.pos
option
val is_def_cond : context -> Ast.ScopeDef.t -> bool
val is_type_cond : Surface.Ast.typ -> bool
val get_def_key :
Surface.Ast.scope_var ->
Surface.Ast.lident Catala_utils.Mark.pos option ->
Shared_ast.ScopeName.t ->
context ->
Catala_utils.Pos.t ->
Ast.ScopeDef.t
Usage: get_def_key var_name var_state scope_uid ctxt pos
val surface_rule_to_def : Surface.Ast.rule -> Surface.Ast.definition
Translates a Surface.rule
into the corresponding Surface.definition
val get_enum :
context ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
Shared_ast.EnumName.t
Find an enum definition from the typedefs, failing if there is none or it has a different kind
val get_struct :
context ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
Shared_ast.StructName.t
Find a struct definition from the typedefs (possibly an implicit output struct from a scope), failing if there is none or it has a different kind
val get_scope :
context ->
Shared_ast.Ident.t Catala_utils.Mark.pos ->
Shared_ast.ScopeName.t
Find a scope definition from the typedefs, failing if there is none or it has a different kind
val module_ctx :
context ->
Surface.Ast.path ->
Catala_utils.Uid.Path.t * context
Returns the context corresponding to the given module path; raises a user error if the module is not found
val process_type : context -> Surface.Ast.typ -> Shared_ast.typ
Convert a surface base type to an AST type
val form_context :
(Surface.Ast.program * Shared_ast.ModuleName.t Shared_ast.Ident.Map.t) ->
(Surface.Ast.module_content * Shared_ast.ModuleName.t Shared_ast.Ident.Map.t)
Shared_ast.ModuleName.Map.t ->
context
Derive the context from metadata, in one pass over the declarations