catala/compiler/surface/name_resolution.mli

117 lines
5.2 KiB
OCaml
Raw Normal View History

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
(** {1 Name resolution context} *)
type ident = string
2021-02-12 19:20:14 +03:00
type typ = Scopelang.Ast.typ
2021-02-12 19:20:14 +03:00
type unique_rulename = Ambiguous of Pos.t list | Unique of Desugared.Ast.RuleName.t
2021-02-12 19:20:14 +03:00
type scope_def_context = {
default_exception_rulename : unique_rulename option;
label_idmap : Desugared.Ast.RuleName.t Desugared.Ast.IdentMap.t;
}
type scope_context = {
2021-02-12 19:20:14 +03:00
var_idmap : Scopelang.Ast.ScopeVar.t Desugared.Ast.IdentMap.t; (** Scope variables *)
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 *)
sub_scopes_idmap : Scopelang.Ast.SubScopeName.t Desugared.Ast.IdentMap.t;
2021-02-12 19:20:14 +03:00
(** Sub-scopes variables *)
sub_scopes : Scopelang.Ast.ScopeName.t Scopelang.Ast.SubScopeMap.t;
2021-02-12 19:20:14 +03:00
(** To what scope sub-scopes refer to? *)
}
2021-02-12 19:20:14 +03:00
(** Inside a scope, we distinguish between the variables and the subscopes. *)
type struct_context = typ Pos.marked Scopelang.Ast.StructFieldMap.t
(** Types of the fields of a struct *)
type enum_context = typ Pos.marked Scopelang.Ast.EnumConstructorMap.t
(** Types of the payloads of the cases of an enum *)
type context = {
local_var_idmap : Scopelang.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 *)
scope_idmap : Scopelang.Ast.ScopeName.t Desugared.Ast.IdentMap.t; (** The names of the scopes *)
struct_idmap : Scopelang.Ast.StructName.t Desugared.Ast.IdentMap.t;
2021-02-12 19:20:14 +03:00
(** The names of the structs *)
field_idmap : Scopelang.Ast.StructFieldName.t Scopelang.Ast.StructMap.t Desugared.Ast.IdentMap.t;
(** The names of the struct fields. Names of fields can be shared between different structs *)
enum_idmap : Scopelang.Ast.EnumName.t Desugared.Ast.IdentMap.t; (** The names of the enums *)
constructor_idmap :
2021-02-12 19:20:14 +03:00
Scopelang.Ast.EnumConstructor.t Scopelang.Ast.EnumMap.t Desugared.Ast.IdentMap.t;
(** 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 *)
structs : struct_context Scopelang.Ast.StructMap.t; (** For each struct, its context *)
enums : enum_context Scopelang.Ast.EnumMap.t; (** For each enum, its context *)
var_typs : (typ Pos.marked * bool) (* is it a condition? *) Scopelang.Ast.ScopeVarMap.t;
(** The types of each scope variable declared *)
}
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
2021-02-12 20:16:06 +03:00
(** Temporary function raising an error message saying that a feature is not supported yet *)
2021-02-12 19:20:14 +03:00
2021-02-12 20:16:06 +03:00
val raise_unknown_identifier : string -> ident Pos.marked -> '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 *)
val get_var_typ : context -> Scopelang.Ast.ScopeVar.t -> typ Pos.marked
2021-02-12 20:16:06 +03:00
(** Gets the type associated to an uid *)
2021-02-12 19:20:14 +03:00
val is_var_cond : context -> Scopelang.Ast.ScopeVar.t -> bool
2021-02-12 19:20:14 +03:00
val get_var_uid :
2021-02-12 20:16:06 +03:00
Scopelang.Ast.ScopeName.t -> context -> ident Pos.marked -> Scopelang.Ast.ScopeVar.t
(** Get the variable uid inside the scope given in argument *)
2021-02-12 19:20:14 +03:00
val get_subscope_uid :
2021-02-12 20:16:06 +03:00
Scopelang.Ast.ScopeName.t -> context -> ident Pos.marked -> Scopelang.Ast.SubScopeName.t
(** Get the subscope uid inside the scope given in argument *)
2021-02-12 19:20:14 +03:00
val is_subscope_uid : Scopelang.Ast.ScopeName.t -> context -> ident -> bool
2021-02-12 20:16:06 +03:00
(** [is_subscope_uid scope_uid ctxt y] returns true if [y] belongs to the subscopes of [scope_uid]. *)
2021-02-12 19:20:14 +03:00
2021-02-12 20:16:06 +03:00
val belongs_to : context -> Scopelang.Ast.ScopeVar.t -> Scopelang.Ast.ScopeName.t -> bool
2021-02-12 19:20:14 +03:00
(** Checks if the var_uid belongs to the scope scope_uid *)
val get_def_typ : context -> Desugared.Ast.ScopeDef.t -> typ Pos.marked
2021-02-12 20:16:06 +03:00
(** Retrieves the type of a scope definition from the context *)
2021-02-12 19:20:14 +03:00
val is_def_cond : context -> Desugared.Ast.ScopeDef.t -> bool
2021-02-12 19:20:14 +03:00
val is_type_cond : Ast.typ Pos.marked -> bool
2021-02-12 20:16:06 +03:00
val add_def_local_var : context -> ident Pos.marked -> context * Scopelang.Ast.Var.t
2021-02-12 19:20:14 +03:00
(** Adds a binding to the context *)
val get_def_key :
2021-02-12 20:16:06 +03:00
Ast.qident -> Scopelang.Ast.ScopeName.t -> context -> Pos.t -> Desugared.Ast.ScopeDef.t
2021-02-12 19:20:14 +03:00
(** {1 API} *)
val form_context : Ast.program -> context
2021-02-12 20:16:06 +03:00
(** Derive the context from metadata, in one pass over the declarations *)