2022-03-08 17:03: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>
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
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
|
2020-06-22 17:16:55 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
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
|
2020-06-22 17:16:55 +03:00
|
|
|
the License. *)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Builds a context that allows for mapping each name to a precise uid, taking
|
|
|
|
lexical scopes into account *)
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** {1 Name resolution context} *)
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
type unique_rulename = Ambiguous of Pos.t list | Unique of RuleName.t Mark.pos
|
2021-01-22 07:47:48 +03:00
|
|
|
|
2022-01-03 20:39:59 +03:00
|
|
|
type scope_def_context = {
|
|
|
|
default_exception_rulename : unique_rulename option;
|
2023-04-18 15:39:38 +03:00
|
|
|
label_idmap : LabelName.t Ident.Map.t;
|
2022-01-03 20:39:59 +03:00
|
|
|
}
|
|
|
|
|
2022-10-21 16:47:17 +03:00
|
|
|
type scope_var_or_subscope =
|
|
|
|
| ScopeVar of ScopeVar.t
|
2023-08-30 18:49:29 +03:00
|
|
|
| SubScope of SubScopeName.t * ScopeName.t
|
2022-10-21 16:47:17 +03:00
|
|
|
|
2020-09-12 21:22:47 +03:00
|
|
|
type scope_context = {
|
2023-04-18 15:39:38 +03:00
|
|
|
var_idmap : scope_var_or_subscope Ident.Map.t;
|
2022-10-21 16:47:17 +03:00
|
|
|
(** All variables, including scope variables and subscopes *)
|
2023-04-18 11:31:44 +03:00
|
|
|
scope_defs_contexts : scope_def_context Ast.ScopeDef.Map.t;
|
2021-01-21 08:07:09 +03:00
|
|
|
(** What is the default rule to refer to for unnamed exceptions, if any *)
|
2022-11-21 12:12:45 +03:00
|
|
|
sub_scopes : ScopeName.Set.t;
|
2022-10-21 16:47:17 +03:00
|
|
|
(** Other scopes referred to by this scope. Used for dependency analysis *)
|
2020-09-12 21:22:47 +03:00
|
|
|
}
|
|
|
|
(** Inside a scope, we distinguish between the variables and the subscopes. *)
|
2020-07-15 17:14:11 +03:00
|
|
|
|
2022-11-21 12:12:45 +03:00
|
|
|
type struct_context = typ StructField.Map.t
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Types of the fields of a struct *)
|
2020-12-04 18:40:17 +03:00
|
|
|
|
2022-11-21 12:12:45 +03:00
|
|
|
type enum_context = typ EnumConstructor.Map.t
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Types of the payloads of the cases of an enum *)
|
2020-12-04 18:40:17 +03:00
|
|
|
|
2022-02-05 02:04:19 +03:00
|
|
|
type var_sig = {
|
2022-08-25 18:29:00 +03:00
|
|
|
var_sig_typ : typ;
|
2022-02-05 02:04:19 +03:00
|
|
|
var_sig_is_condition : bool;
|
2023-02-28 16:40:05 +03:00
|
|
|
var_sig_parameters :
|
2023-05-17 16:44:57 +03:00
|
|
|
(Uid.MarkedString.info * Shared_ast.typ) list Mark.pos option;
|
2022-11-07 15:50:28 +03:00
|
|
|
var_sig_io : Surface.Ast.scope_decl_context_io;
|
2023-04-18 15:39:38 +03:00
|
|
|
var_sig_states_idmap : StateName.t Ident.Map.t;
|
2022-08-25 13:09:51 +03:00
|
|
|
var_sig_states_list : StateName.t list;
|
2022-02-05 02:04:19 +03:00
|
|
|
}
|
|
|
|
|
2022-10-21 16:47:17 +03:00
|
|
|
(** Capitalised type names share a namespace on the user side, but may
|
|
|
|
correspond to only one of the following *)
|
|
|
|
type typedef =
|
|
|
|
| TStruct of StructName.t
|
|
|
|
| TEnum of EnumName.t
|
2023-08-16 01:04:45 +03:00
|
|
|
| TScope of ScopeName.t * scope_info (** Implicitly defined output struct *)
|
2022-10-21 16:47:17 +03:00
|
|
|
|
2020-07-15 18:47:16 +03:00
|
|
|
type context = {
|
2023-08-30 18:49:29 +03:00
|
|
|
path : Uid.Path.t;
|
2023-04-18 15:39:38 +03:00
|
|
|
typedefs : typedef Ident.Map.t;
|
2022-10-21 16:47:17 +03:00
|
|
|
(** Gathers the names of the scopes, structs and enums *)
|
2023-04-18 15:39:38 +03:00
|
|
|
field_idmap : StructField.t StructName.Map.t Ident.Map.t;
|
2022-03-08 17:03:14 +03:00
|
|
|
(** The names of the struct fields. Names of fields can be shared between
|
|
|
|
different structs *)
|
2023-04-18 15:39:38 +03:00
|
|
|
constructor_idmap : EnumConstructor.t EnumName.Map.t Ident.Map.t;
|
2022-03-08 17:03:14 +03:00
|
|
|
(** The names of the enum constructors. Constructor names can be shared
|
|
|
|
between different enums *)
|
2022-11-21 12:12:45 +03:00
|
|
|
scopes : scope_context ScopeName.Map.t; (** For each scope, its context *)
|
2023-04-18 15:39:38 +03:00
|
|
|
topdefs : TopdefName.t Ident.Map.t; (** Global definitions *)
|
2023-08-10 17:52:39 +03:00
|
|
|
topdef_types : typ TopdefName.Map.t;
|
2022-11-21 12:12:45 +03:00
|
|
|
structs : struct_context StructName.Map.t;
|
|
|
|
(** For each struct, its context *)
|
|
|
|
enums : enum_context EnumName.Map.t; (** For each enum, its context *)
|
|
|
|
var_typs : var_sig ScopeVar.Map.t;
|
2022-02-05 02:04:19 +03:00
|
|
|
(** The signatures of each scope variable declared *)
|
2023-08-10 17:52:39 +03:00
|
|
|
modules : context ModuleName.Map.t;
|
2020-07-15 18:47:16 +03:00
|
|
|
}
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Main context used throughout {!module: Surface.Desugaring} *)
|
|
|
|
|
|
|
|
(** {1 Helpers} *)
|
2020-07-15 18:47:16 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Temporary function raising an error message saying that a feature is not
|
|
|
|
supported yet *)
|
2020-08-07 13:51:51 +03:00
|
|
|
let raise_unsupported_feature (msg : string) (pos : Pos.t) =
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error pos "Unsupported feature: %s" msg
|
2020-08-07 13:51:51 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Function to call whenever an identifier used somewhere has not been declared
|
|
|
|
in the program previously *)
|
2023-04-18 15:39:38 +03:00
|
|
|
let raise_unknown_identifier (msg : string) (ident : Ident.t Mark.pos) =
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Mark.get ident)
|
2023-06-07 19:15:14 +03:00
|
|
|
"@{<yellow>\"%s\"@}: unknown identifier %s" (Mark.remove ident) msg
|
2020-07-16 15:46:40 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Gets the type associated to an uid *)
|
2022-08-25 18:29:00 +03:00
|
|
|
let get_var_typ (ctxt : context) (uid : ScopeVar.t) : typ =
|
2022-11-21 12:12:45 +03:00
|
|
|
(ScopeVar.Map.find uid ctxt.var_typs).var_sig_typ
|
2020-12-31 02:28:26 +03:00
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
let is_var_cond (ctxt : context) (uid : ScopeVar.t) : bool =
|
2022-11-21 12:12:45 +03:00
|
|
|
(ScopeVar.Map.find uid ctxt.var_typs).var_sig_is_condition
|
2022-02-05 02:04:19 +03:00
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
let get_var_io (ctxt : context) (uid : ScopeVar.t) :
|
|
|
|
Surface.Ast.scope_decl_context_io =
|
2022-11-21 12:12:45 +03:00
|
|
|
(ScopeVar.Map.find uid ctxt.var_typs).var_sig_io
|
2020-08-03 23:03:05 +03:00
|
|
|
|
2023-08-31 11:07:57 +03:00
|
|
|
let get_scope_context (ctxt : context) (scope : ScopeName.t) : scope_context =
|
|
|
|
let rec remove_common_prefix curpath scpath =
|
|
|
|
match curpath, scpath with
|
|
|
|
| m1 :: cp, m2 :: sp when ModuleName.equal m1 m2 ->
|
|
|
|
remove_common_prefix cp sp
|
2023-08-30 18:49:29 +03:00
|
|
|
| _ -> scpath
|
|
|
|
in
|
|
|
|
let path = remove_common_prefix ctxt.path (ScopeName.path scope) in
|
|
|
|
let ctxt =
|
|
|
|
List.fold_left (fun ctx m -> ModuleName.Map.find m ctx.modules) ctxt path
|
|
|
|
in
|
|
|
|
ScopeName.Map.find scope ctxt.scopes
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Get the variable uid inside the scope given in argument *)
|
2022-03-08 17:03:14 +03:00
|
|
|
let get_var_uid
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope_uid : ScopeName.t)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2023-04-18 15:39:38 +03:00
|
|
|
((x, pos) : Ident.t Mark.pos) : ScopeVar.t =
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope = get_scope_context ctxt scope_uid in
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find_opt x scope.var_idmap with
|
2022-10-21 16:47:17 +03:00
|
|
|
| Some (ScopeVar uid) -> uid
|
|
|
|
| _ ->
|
2022-05-12 16:10:55 +03:00
|
|
|
raise_unknown_identifier
|
2023-07-12 12:48:46 +03:00
|
|
|
(Format.asprintf "for a variable of scope %a" ScopeName.format scope_uid)
|
2022-05-12 16:10:55 +03:00
|
|
|
(x, pos)
|
2020-12-14 17:23:04 +03:00
|
|
|
|
|
|
|
(** Get the subscope uid inside the scope given in argument *)
|
2022-03-08 17:03:14 +03:00
|
|
|
let get_subscope_uid
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope_uid : ScopeName.t)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2023-04-18 15:39:38 +03:00
|
|
|
((y, pos) : Ident.t Mark.pos) : SubScopeName.t =
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope = get_scope_context ctxt scope_uid in
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find_opt y scope.var_idmap with
|
2022-10-21 16:47:17 +03:00
|
|
|
| Some (SubScope (sub_uid, _sub_id)) -> sub_uid
|
|
|
|
| _ -> raise_unknown_identifier "for a subscope of this scope" (y, pos)
|
2020-12-14 17:23:04 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [is_subscope_uid scope_uid ctxt y] returns true if [y] belongs to the
|
|
|
|
subscopes of [scope_uid]. *)
|
2023-04-18 15:39:38 +03:00
|
|
|
let is_subscope_uid (scope_uid : ScopeName.t) (ctxt : context) (y : Ident.t) :
|
|
|
|
bool =
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope = get_scope_context ctxt scope_uid in
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find_opt y scope.var_idmap with
|
2022-10-21 16:47:17 +03:00
|
|
|
| Some (SubScope _) -> true
|
|
|
|
| _ -> false
|
2020-12-14 17:23:04 +03:00
|
|
|
|
|
|
|
(** Checks if the var_uid belongs to the scope scope_uid *)
|
2022-08-25 13:09:51 +03:00
|
|
|
let belongs_to (ctxt : context) (uid : ScopeVar.t) (scope_uid : ScopeName.t) :
|
|
|
|
bool =
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope = get_scope_context ctxt scope_uid in
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.exists
|
2022-10-21 16:47:17 +03:00
|
|
|
(fun _ -> function
|
|
|
|
| ScopeVar var_uid -> ScopeVar.equal uid var_uid
|
|
|
|
| _ -> false)
|
2020-12-14 17:23:04 +03:00
|
|
|
scope.var_idmap
|
|
|
|
|
|
|
|
(** Retrieves the type of a scope definition from the context *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let get_def_typ (ctxt : context) (def : Ast.ScopeDef.t) : typ =
|
2020-12-14 17:23:04 +03:00
|
|
|
match def with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Ast.ScopeDef.SubScopeVar (_, x, _)
|
2022-03-08 17:03:14 +03:00
|
|
|
(* we don't need to look at the subscope prefix because [x] is already the uid
|
|
|
|
referring back to the original subscope *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Ast.ScopeDef.Var (x, _) ->
|
2022-05-12 16:10:55 +03:00
|
|
|
get_var_typ ctxt x
|
2020-12-31 02:28:26 +03:00
|
|
|
|
2023-02-28 16:40:05 +03:00
|
|
|
(** Retrieves the type of a scope definition from the context *)
|
|
|
|
let get_params (ctxt : context) (def : Ast.ScopeDef.t) :
|
2023-05-17 16:44:57 +03:00
|
|
|
(Uid.MarkedString.info * typ) list Mark.pos option =
|
2023-02-28 16:40:05 +03:00
|
|
|
match def with
|
|
|
|
| Ast.ScopeDef.SubScopeVar (_, x, _)
|
|
|
|
(* we don't need to look at the subscope prefix because [x] is already the uid
|
|
|
|
referring back to the original subscope *)
|
|
|
|
| Ast.ScopeDef.Var (x, _) ->
|
|
|
|
(ScopeVar.Map.find x ctxt.var_typs).var_sig_parameters
|
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
let is_def_cond (ctxt : context) (def : Ast.ScopeDef.t) : bool =
|
2020-12-31 02:28:26 +03:00
|
|
|
match def with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Ast.ScopeDef.SubScopeVar (_, x, _)
|
2022-03-08 17:03:14 +03:00
|
|
|
(* we don't need to look at the subscope prefix because [x] is already the uid
|
|
|
|
referring back to the original subscope *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Ast.ScopeDef.Var (x, _) ->
|
2022-05-12 16:10:55 +03:00
|
|
|
is_var_cond ctxt x
|
2020-12-14 17:23:04 +03:00
|
|
|
|
2022-10-21 16:47:17 +03:00
|
|
|
let get_enum ctxt id =
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find (Mark.remove id) ctxt.typedefs with
|
2022-10-21 16:47:17 +03:00
|
|
|
| TEnum id -> id
|
|
|
|
| TStruct sid ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-10-21 16:47:17 +03:00
|
|
|
[
|
2023-05-17 16:44:57 +03:00
|
|
|
None, Mark.get id;
|
|
|
|
Some "Structure defined at", Mark.get (StructName.get_info sid);
|
2022-10-21 16:47:17 +03:00
|
|
|
]
|
|
|
|
"Expecting an enum, but found a structure"
|
|
|
|
| TScope (sid, _) ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-10-21 16:47:17 +03:00
|
|
|
[
|
2023-05-17 16:44:57 +03:00
|
|
|
None, Mark.get id;
|
|
|
|
Some "Scope defined at", Mark.get (ScopeName.get_info sid);
|
2022-10-21 16:47:17 +03:00
|
|
|
]
|
|
|
|
"Expecting an enum, but found a scope"
|
2023-08-15 17:57:52 +03:00
|
|
|
| exception Ident.Map.Not_found _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Mark.get id) "No enum named %s found"
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove id)
|
2022-10-21 16:47:17 +03:00
|
|
|
|
|
|
|
let get_struct ctxt id =
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find (Mark.remove id) ctxt.typedefs with
|
2022-11-17 19:13:35 +03:00
|
|
|
| TStruct id | TScope (_, { out_struct_name = id; _ }) -> id
|
2022-10-21 16:47:17 +03:00
|
|
|
| TEnum eid ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-10-21 16:47:17 +03:00
|
|
|
[
|
2023-05-17 16:44:57 +03:00
|
|
|
None, Mark.get id;
|
|
|
|
Some "Enum defined at", Mark.get (EnumName.get_info eid);
|
2022-10-21 16:47:17 +03:00
|
|
|
]
|
2023-08-10 17:52:39 +03:00
|
|
|
"Expecting a struct, but found an enum"
|
2023-08-15 17:57:52 +03:00
|
|
|
| exception Ident.Map.Not_found _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Mark.get id) "No struct named %s found"
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove id)
|
2022-10-21 16:47:17 +03:00
|
|
|
|
|
|
|
let get_scope ctxt id =
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find (Mark.remove id) ctxt.typedefs with
|
2022-10-21 16:47:17 +03:00
|
|
|
| TScope (id, _) -> id
|
|
|
|
| TEnum eid ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-10-21 16:47:17 +03:00
|
|
|
[
|
2023-05-17 16:44:57 +03:00
|
|
|
None, Mark.get id;
|
|
|
|
Some "Enum defined at", Mark.get (EnumName.get_info eid);
|
2022-10-21 16:47:17 +03:00
|
|
|
]
|
|
|
|
"Expecting an scope, but found an enum"
|
|
|
|
| TStruct sid ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-10-21 16:47:17 +03:00
|
|
|
[
|
2023-05-17 16:44:57 +03:00
|
|
|
None, Mark.get id;
|
|
|
|
Some "Structure defined at", Mark.get (StructName.get_info sid);
|
2022-10-21 16:47:17 +03:00
|
|
|
]
|
|
|
|
"Expecting an scope, but found a structure"
|
2023-08-15 17:57:52 +03:00
|
|
|
| exception Ident.Map.Not_found _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Mark.get id) "No scope named %s found"
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove id)
|
2022-10-21 16:47:17 +03:00
|
|
|
|
2023-08-16 01:04:45 +03:00
|
|
|
let rec module_ctx ctxt path =
|
|
|
|
match path with
|
2023-08-10 17:52:39 +03:00
|
|
|
| [] -> ctxt
|
2023-09-15 15:56:35 +03:00
|
|
|
| modname :: path -> (
|
2023-08-30 18:49:29 +03:00
|
|
|
let modname = ModuleName.of_string modname in
|
2023-08-16 01:04:45 +03:00
|
|
|
match ModuleName.Map.find_opt modname ctxt.modules with
|
|
|
|
| None ->
|
2023-09-19 19:21:14 +03:00
|
|
|
Message.raise_spanned_error (ModuleName.pos modname)
|
|
|
|
"Module \"%a\" not found" ModuleName.format modname
|
2023-08-16 01:04:45 +03:00
|
|
|
| Some ctxt -> module_ctx ctxt path)
|
2023-08-10 17:52:39 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** {1 Declarations pass} *)
|
|
|
|
|
2020-07-16 13:04:23 +03:00
|
|
|
(** Process a subscope declaration *)
|
2022-03-08 17:03:14 +03:00
|
|
|
let process_subscope_decl
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(decl : Surface.Ast.scope_decl_context_scope) : context =
|
2020-09-12 21:22:47 +03:00
|
|
|
let name, name_pos = decl.scope_decl_context_scope_name in
|
2023-08-10 17:52:39 +03:00
|
|
|
let (path, subscope), s_pos = decl.scope_decl_context_scope_sub_scope in
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope_ctxt = get_scope_context ctxt scope in
|
2023-08-10 17:52:39 +03:00
|
|
|
match Ident.Map.find_opt (Mark.remove subscope) scope_ctxt.var_idmap with
|
2020-09-12 21:22:47 +03:00
|
|
|
| Some use ->
|
2022-10-21 16:47:17 +03:00
|
|
|
let info =
|
|
|
|
match use with
|
|
|
|
| ScopeVar v -> ScopeVar.get_info v
|
|
|
|
| SubScope (ssc, _) -> SubScopeName.get_info ssc
|
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2023-05-17 16:44:57 +03:00
|
|
|
[Some "first use", Mark.get info; Some "second use", s_pos]
|
2023-08-16 01:04:45 +03:00
|
|
|
"Subscope name @{<yellow>\"%s\"@} already used" (Mark.remove subscope)
|
2020-07-16 15:46:40 +03:00
|
|
|
| None ->
|
2022-08-17 18:14:29 +03:00
|
|
|
let sub_scope_uid = SubScopeName.fresh (name, name_pos) in
|
2022-05-12 16:10:55 +03:00
|
|
|
let original_subscope_uid =
|
2023-08-10 17:52:39 +03:00
|
|
|
let ctxt = module_ctx ctxt path in
|
|
|
|
get_scope ctxt subscope
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
|
|
|
let scope_ctxt =
|
2022-03-08 17:03:14 +03:00
|
|
|
{
|
2022-05-12 16:10:55 +03:00
|
|
|
scope_ctxt with
|
2022-10-21 16:47:17 +03:00
|
|
|
var_idmap =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.add name
|
2023-08-30 18:49:29 +03:00
|
|
|
(SubScope (sub_scope_uid, original_subscope_uid))
|
2022-10-21 16:47:17 +03:00
|
|
|
scope_ctxt.var_idmap;
|
2022-11-21 12:12:45 +03:00
|
|
|
sub_scopes =
|
|
|
|
ScopeName.Set.add original_subscope_uid scope_ctxt.sub_scopes;
|
2022-03-08 17:03:14 +03:00
|
|
|
}
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
{ ctxt with scopes = ScopeName.Map.add scope scope_ctxt ctxt.scopes }
|
2020-07-16 13:04:23 +03:00
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
let is_type_cond ((typ, _) : Surface.Ast.typ) =
|
2020-12-31 02:28:26 +03:00
|
|
|
match typ with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Base Surface.Ast.Condition
|
|
|
|
| Surface.Ast.Func { arg_typ = _; return_typ = Surface.Ast.Condition, _ } ->
|
2022-05-12 16:10:55 +03:00
|
|
|
true
|
2020-12-31 02:28:26 +03:00
|
|
|
| _ -> false
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Process a basic type (all types except function types) *)
|
2022-03-08 17:03:14 +03:00
|
|
|
let rec process_base_typ
|
2022-05-12 16:10:55 +03:00
|
|
|
(ctxt : context)
|
2023-05-17 16:44:57 +03:00
|
|
|
((typ, typ_pos) : Surface.Ast.base_typ Mark.pos) : typ =
|
2020-08-06 16:18:40 +03:00
|
|
|
match typ with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Condition -> TLit TBool, typ_pos
|
|
|
|
| Surface.Ast.Data (Surface.Ast.Collection t) ->
|
2022-08-25 13:09:51 +03:00
|
|
|
( TArray
|
2023-05-17 16:44:57 +03:00
|
|
|
(process_base_typ ctxt (Surface.Ast.Data (Mark.remove t), Mark.get t)),
|
2022-05-12 16:10:55 +03:00
|
|
|
typ_pos )
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Data (Surface.Ast.Primitive prim) -> (
|
2022-05-12 16:10:55 +03:00
|
|
|
match prim with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Integer -> TLit TInt, typ_pos
|
|
|
|
| Surface.Ast.Decimal -> TLit TRat, typ_pos
|
|
|
|
| Surface.Ast.Money -> TLit TMoney, typ_pos
|
|
|
|
| Surface.Ast.Duration -> TLit TDuration, typ_pos
|
|
|
|
| Surface.Ast.Date -> TLit TDate, typ_pos
|
|
|
|
| Surface.Ast.Boolean -> TLit TBool, typ_pos
|
|
|
|
| Surface.Ast.Text -> raise_unsupported_feature "text type" typ_pos
|
2023-01-04 18:12:36 +03:00
|
|
|
| Surface.Ast.Named ([], (ident, _pos)) -> (
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find_opt ident ctxt.typedefs with
|
2023-08-16 01:04:45 +03:00
|
|
|
| Some (TStruct s_uid) -> TStruct s_uid, typ_pos
|
|
|
|
| Some (TEnum e_uid) -> TEnum e_uid, typ_pos
|
2022-11-17 19:13:35 +03:00
|
|
|
| Some (TScope (_, scope_str)) ->
|
2023-08-16 01:04:45 +03:00
|
|
|
TStruct scope_str.out_struct_name, typ_pos
|
2022-10-21 16:47:17 +03:00
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error typ_pos
|
2023-06-07 19:15:14 +03:00
|
|
|
"Unknown type @{<yellow>\"%s\"@}, not a struct or enum previously \
|
|
|
|
declared"
|
2023-01-04 18:12:36 +03:00
|
|
|
ident)
|
2023-09-15 15:56:35 +03:00
|
|
|
| Surface.Ast.Named (modul :: path, id) -> (
|
2023-08-30 18:49:29 +03:00
|
|
|
let modul = ModuleName.of_string modul in
|
2023-08-10 17:52:39 +03:00
|
|
|
match ModuleName.Map.find_opt modul ctxt.modules with
|
|
|
|
| None ->
|
2023-09-15 15:56:35 +03:00
|
|
|
Message.raise_spanned_error (ModuleName.pos modul)
|
2023-08-16 01:04:45 +03:00
|
|
|
"This refers to module %a, which was not found" ModuleName.format
|
|
|
|
modul
|
2023-08-10 17:52:39 +03:00
|
|
|
| Some mod_ctxt ->
|
2023-08-16 01:04:45 +03:00
|
|
|
process_base_typ mod_ctxt
|
|
|
|
Surface.Ast.(Data (Primitive (Named (path, id))), typ_pos)))
|
2020-08-06 16:18:40 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Process a type (function or not) *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let process_type (ctxt : context) ((naked_typ, typ_pos) : Surface.Ast.typ) : typ
|
|
|
|
=
|
2022-08-25 18:29:00 +03:00
|
|
|
match naked_typ with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Base base_typ -> process_base_typ ctxt (base_typ, typ_pos)
|
|
|
|
| Surface.Ast.Func { arg_typ; return_typ } ->
|
2023-02-24 12:02:12 +03:00
|
|
|
let targs = List.map (fun (_, t) -> process_base_typ ctxt t) arg_typ in
|
|
|
|
TArrow (targs, process_base_typ ctxt return_typ), typ_pos
|
2020-08-06 16:18:40 +03:00
|
|
|
|
2020-07-16 13:04:23 +03:00
|
|
|
(** Process data declaration *)
|
2022-03-08 17:03:14 +03:00
|
|
|
let process_data_decl
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(decl : Surface.Ast.scope_decl_context_data) : context =
|
2020-07-16 15:46:40 +03:00
|
|
|
(* First check the type of the context data *)
|
2020-12-04 20:48:16 +03:00
|
|
|
let data_typ = process_type ctxt decl.scope_decl_context_item_typ in
|
2020-12-31 02:28:26 +03:00
|
|
|
let is_cond = is_type_cond decl.scope_decl_context_item_typ in
|
2020-08-04 13:44:56 +03:00
|
|
|
let name, pos = decl.scope_decl_context_item_name in
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope_ctxt = get_scope_context ctxt scope in
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find_opt name scope_ctxt.var_idmap with
|
2020-09-12 21:22:47 +03:00
|
|
|
| Some use ->
|
2022-10-21 16:47:17 +03:00
|
|
|
let info =
|
|
|
|
match use with
|
|
|
|
| ScopeVar v -> ScopeVar.get_info v
|
|
|
|
| SubScope (ssc, _) -> SubScopeName.get_info ssc
|
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2023-05-17 16:44:57 +03:00
|
|
|
[Some "First use:", Mark.get info; Some "Second use:", pos]
|
2023-06-07 19:15:14 +03:00
|
|
|
"Variable name @{<yellow>\"%s\"@} already used" name
|
2020-09-12 21:22:47 +03:00
|
|
|
| None ->
|
2022-08-25 13:09:51 +03:00
|
|
|
let uid = ScopeVar.fresh (name, pos) in
|
2022-05-12 16:10:55 +03:00
|
|
|
let scope_ctxt =
|
2020-09-12 21:22:47 +03:00
|
|
|
{
|
2022-05-12 16:10:55 +03:00
|
|
|
scope_ctxt with
|
2023-04-18 15:39:38 +03:00
|
|
|
var_idmap = Ident.Map.add name (ScopeVar uid) scope_ctxt.var_idmap;
|
2020-09-12 21:22:47 +03:00
|
|
|
}
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
|
|
|
let states_idmap, states_list =
|
|
|
|
List.fold_right
|
2023-04-18 15:39:38 +03:00
|
|
|
(fun state_id ((states_idmap : StateName.t Ident.Map.t), states_list) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
let state_id_name = Mark.remove state_id in
|
2023-04-18 15:39:38 +03:00
|
|
|
if Ident.Map.mem state_id_name states_idmap then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error_full
|
2023-04-27 16:37:11 +03:00
|
|
|
[
|
|
|
|
( Some
|
2023-06-07 19:15:14 +03:00
|
|
|
(fun ppf ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"First instance of state @{<yellow>\"%s\"@}:"
|
2023-06-07 19:10:50 +03:00
|
|
|
state_id_name),
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get state_id );
|
2023-04-27 16:37:11 +03:00
|
|
|
( Some
|
2023-06-07 19:15:14 +03:00
|
|
|
(fun ppf ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"Second instance of state @{<yellow>\"%s\"@}:"
|
2023-06-07 19:10:50 +03:00
|
|
|
state_id_name),
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get
|
2023-04-18 15:39:38 +03:00
|
|
|
(Ident.Map.find state_id_name states_idmap
|
2023-04-27 16:37:11 +03:00
|
|
|
|> StateName.get_info) );
|
|
|
|
]
|
|
|
|
"There are two states with the same name for the same variable: \
|
|
|
|
this is ambiguous. Please change the name of either states.";
|
2022-08-25 13:09:51 +03:00
|
|
|
let state_uid = StateName.fresh state_id in
|
2023-04-18 15:39:38 +03:00
|
|
|
( Ident.Map.add state_id_name state_uid states_idmap,
|
2022-05-12 16:10:55 +03:00
|
|
|
state_uid :: states_list ))
|
2023-04-18 15:39:38 +03:00
|
|
|
decl.scope_decl_context_item_states (Ident.Map.empty, [])
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2023-02-27 11:50:42 +03:00
|
|
|
let var_sig_parameters =
|
2023-02-28 16:40:05 +03:00
|
|
|
Option.map
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.map (List.map (fun (lbl, typ) -> lbl, process_type ctxt typ)))
|
2023-02-27 11:50:42 +03:00
|
|
|
decl.scope_decl_context_item_parameters
|
|
|
|
in
|
2022-05-12 16:10:55 +03:00
|
|
|
{
|
|
|
|
ctxt with
|
2022-11-21 12:12:45 +03:00
|
|
|
scopes = ScopeName.Map.add scope scope_ctxt ctxt.scopes;
|
2022-05-12 16:10:55 +03:00
|
|
|
var_typs =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.add uid
|
2022-05-12 16:10:55 +03:00
|
|
|
{
|
|
|
|
var_sig_typ = data_typ;
|
|
|
|
var_sig_is_condition = is_cond;
|
2023-02-27 11:50:42 +03:00
|
|
|
var_sig_parameters;
|
2022-05-12 16:10:55 +03:00
|
|
|
var_sig_io = decl.scope_decl_context_item_attribute;
|
|
|
|
var_sig_states_idmap = states_idmap;
|
|
|
|
var_sig_states_list = states_list;
|
|
|
|
}
|
|
|
|
ctxt.var_typs;
|
|
|
|
}
|
2020-07-16 13:04:23 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Process a struct declaration *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let process_struct_decl (ctxt : context) (sdecl : Surface.Ast.struct_decl) :
|
|
|
|
context =
|
2022-10-21 16:47:17 +03:00
|
|
|
let s_uid = get_struct ctxt sdecl.struct_decl_name in
|
|
|
|
if sdecl.struct_decl_fields = [] then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get sdecl.struct_decl_name)
|
2022-03-28 15:43:38 +03:00
|
|
|
"The struct %s does not have any fields; give it some for Catala to be \
|
|
|
|
able to accept it."
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove sdecl.struct_decl_name);
|
2020-12-04 20:02:49 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun ctxt (fdecl, _) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
let f_uid = StructField.fresh fdecl.Surface.Ast.struct_decl_field_name in
|
2020-12-04 20:02:49 +03:00
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
field_idmap =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.update
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove fdecl.Surface.Ast.struct_decl_field_name)
|
2020-12-04 20:02:49 +03:00
|
|
|
(fun uids ->
|
|
|
|
match uids with
|
2022-11-21 12:12:45 +03:00
|
|
|
| None -> Some (StructName.Map.singleton s_uid f_uid)
|
|
|
|
| Some uids -> Some (StructName.Map.add s_uid f_uid uids))
|
2020-12-04 20:02:49 +03:00
|
|
|
ctxt.field_idmap;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
structs =
|
2022-11-21 12:12:45 +03:00
|
|
|
StructName.Map.update s_uid
|
2020-12-04 20:02:49 +03:00
|
|
|
(fun fields ->
|
|
|
|
match fields with
|
|
|
|
| None ->
|
2022-05-12 16:10:55 +03:00
|
|
|
Some
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructField.Map.singleton f_uid
|
2022-11-07 15:50:28 +03:00
|
|
|
(process_type ctxt fdecl.Surface.Ast.struct_decl_field_typ))
|
2020-12-04 20:02:49 +03:00
|
|
|
| Some fields ->
|
2022-05-12 16:10:55 +03:00
|
|
|
Some
|
2022-11-21 12:12:45 +03:00
|
|
|
(StructField.Map.add f_uid
|
2022-11-07 15:50:28 +03:00
|
|
|
(process_type ctxt fdecl.Surface.Ast.struct_decl_field_typ)
|
2022-05-12 16:10:55 +03:00
|
|
|
fields))
|
2020-12-04 20:02:49 +03:00
|
|
|
ctxt.structs;
|
|
|
|
})
|
|
|
|
ctxt sdecl.struct_decl_fields
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Process an enum declaration *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let process_enum_decl (ctxt : context) (edecl : Surface.Ast.enum_decl) : context
|
|
|
|
=
|
2022-10-21 16:47:17 +03:00
|
|
|
let e_uid = get_enum ctxt edecl.enum_decl_name in
|
2022-03-28 15:43:38 +03:00
|
|
|
if List.length edecl.enum_decl_cases = 0 then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get edecl.enum_decl_name)
|
2022-03-28 15:43:38 +03:00
|
|
|
"The enum %s does not have any cases; give it some for Catala to be able \
|
|
|
|
to accept it."
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove edecl.enum_decl_name);
|
2020-12-04 20:02:49 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun ctxt (cdecl, cdecl_pos) ->
|
2022-11-07 15:50:28 +03:00
|
|
|
let c_uid = EnumConstructor.fresh cdecl.Surface.Ast.enum_decl_case_name in
|
2020-12-04 20:02:49 +03:00
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
constructor_idmap =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.update
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove cdecl.Surface.Ast.enum_decl_case_name)
|
2020-12-04 20:02:49 +03:00
|
|
|
(fun uids ->
|
|
|
|
match uids with
|
2022-11-21 12:12:45 +03:00
|
|
|
| None -> Some (EnumName.Map.singleton e_uid c_uid)
|
|
|
|
| Some uids -> Some (EnumName.Map.add e_uid c_uid uids))
|
2020-12-04 20:02:49 +03:00
|
|
|
ctxt.constructor_idmap;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
enums =
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumName.Map.update e_uid
|
2020-12-04 20:02:49 +03:00
|
|
|
(fun cases ->
|
|
|
|
let typ =
|
2022-11-07 15:50:28 +03:00
|
|
|
match cdecl.Surface.Ast.enum_decl_case_typ with
|
2022-08-25 13:09:51 +03:00
|
|
|
| None -> TLit TUnit, cdecl_pos
|
2020-12-04 20:48:16 +03:00
|
|
|
| Some typ -> process_type ctxt typ
|
2020-12-04 20:02:49 +03:00
|
|
|
in
|
|
|
|
match cases with
|
2022-11-21 12:12:45 +03:00
|
|
|
| None -> Some (EnumConstructor.Map.singleton c_uid typ)
|
|
|
|
| Some fields -> Some (EnumConstructor.Map.add c_uid typ fields))
|
2020-12-04 20:02:49 +03:00
|
|
|
ctxt.enums;
|
|
|
|
})
|
|
|
|
ctxt edecl.enum_decl_cases
|
|
|
|
|
2023-08-31 17:54:45 +03:00
|
|
|
let process_topdef ctxt def =
|
|
|
|
let uid =
|
|
|
|
Ident.Map.find (Mark.remove def.Surface.Ast.topdef_name) ctxt.topdefs
|
|
|
|
in
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
topdef_types =
|
|
|
|
TopdefName.Map.add uid
|
|
|
|
(process_type ctxt def.Surface.Ast.topdef_type)
|
|
|
|
ctxt.topdef_types;
|
|
|
|
}
|
|
|
|
|
2022-10-21 16:47:17 +03:00
|
|
|
(** Process an item declaration *)
|
|
|
|
let process_item_decl
|
|
|
|
(scope : ScopeName.t)
|
|
|
|
(ctxt : context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(decl : Surface.Ast.scope_decl_context_item) : context =
|
2022-10-21 16:47:17 +03:00
|
|
|
match decl with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.ContextData data_decl -> process_data_decl scope ctxt data_decl
|
|
|
|
| Surface.Ast.ContextScope sub_decl ->
|
|
|
|
process_subscope_decl scope ctxt sub_decl
|
2022-10-21 16:47:17 +03:00
|
|
|
|
|
|
|
(** Process a scope declaration *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let process_scope_decl (ctxt : context) (decl : Surface.Ast.scope_decl) :
|
|
|
|
context =
|
2022-10-21 16:47:17 +03:00
|
|
|
let scope_uid = get_scope ctxt decl.scope_decl_name in
|
|
|
|
let ctxt =
|
|
|
|
List.fold_left
|
2023-05-17 16:44:57 +03:00
|
|
|
(fun ctxt item -> process_item_decl scope_uid ctxt (Mark.remove item))
|
2022-10-21 16:47:17 +03:00
|
|
|
ctxt decl.scope_decl_context
|
|
|
|
in
|
|
|
|
(* Add an implicit struct def for the scope output type *)
|
|
|
|
let output_fields =
|
|
|
|
List.fold_right
|
|
|
|
(fun item acc ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove item with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.ContextData
|
2022-10-21 16:47:17 +03:00
|
|
|
({
|
|
|
|
scope_decl_context_item_attribute =
|
|
|
|
{ scope_decl_context_io_output = true, _; _ };
|
|
|
|
_;
|
|
|
|
} as data) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.add (Mark.get item)
|
2022-10-21 16:47:17 +03:00
|
|
|
{
|
2022-11-07 15:50:28 +03:00
|
|
|
Surface.Ast.struct_decl_field_name =
|
|
|
|
data.scope_decl_context_item_name;
|
|
|
|
Surface.Ast.struct_decl_field_typ =
|
|
|
|
data.scope_decl_context_item_typ;
|
2022-10-21 16:47:17 +03:00
|
|
|
}
|
|
|
|
:: acc
|
|
|
|
| _ -> acc)
|
|
|
|
decl.scope_decl_context []
|
|
|
|
in
|
|
|
|
if output_fields = [] then
|
|
|
|
(* we allow scopes without output variables, and still define their (empty)
|
|
|
|
output struct for convenience *)
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
structs =
|
2022-11-21 12:12:45 +03:00
|
|
|
StructName.Map.add
|
2022-10-21 16:47:17 +03:00
|
|
|
(get_struct ctxt decl.scope_decl_name)
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.Map.empty ctxt.structs;
|
2022-10-21 16:47:17 +03:00
|
|
|
}
|
|
|
|
else
|
2022-11-17 19:13:35 +03:00
|
|
|
let ctxt =
|
|
|
|
process_struct_decl ctxt
|
|
|
|
{
|
|
|
|
struct_decl_name = decl.scope_decl_name;
|
|
|
|
struct_decl_fields = output_fields;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
let out_struct_fields =
|
2023-08-30 18:49:29 +03:00
|
|
|
let sco = get_scope_context ctxt scope_uid in
|
2022-11-17 19:13:35 +03:00
|
|
|
let str = get_struct ctxt decl.scope_decl_name in
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.fold
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun id var svmap ->
|
|
|
|
match var with
|
|
|
|
| SubScope _ -> svmap
|
|
|
|
| ScopeVar v -> (
|
|
|
|
try
|
|
|
|
let field =
|
2023-04-18 15:39:38 +03:00
|
|
|
StructName.Map.find str (Ident.Map.find id ctxt.field_idmap)
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.add v field svmap
|
2023-08-15 17:57:52 +03:00
|
|
|
with StructName.Map.Not_found _ | Ident.Map.Not_found _ -> svmap))
|
2022-11-21 12:12:45 +03:00
|
|
|
sco.var_idmap ScopeVar.Map.empty
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
|
|
|
let typedefs =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.update
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove decl.scope_decl_name)
|
2022-11-17 19:13:35 +03:00
|
|
|
(function
|
2023-08-10 17:52:39 +03:00
|
|
|
| Some (TScope (scope, { in_struct_name; out_struct_name; _ })) ->
|
2023-08-16 01:04:45 +03:00
|
|
|
Some
|
|
|
|
(TScope
|
|
|
|
(scope, { in_struct_name; out_struct_name; out_struct_fields }))
|
2022-11-17 19:13:35 +03:00
|
|
|
| _ -> assert false)
|
|
|
|
ctxt.typedefs
|
|
|
|
in
|
|
|
|
{ ctxt with typedefs }
|
2022-10-21 16:47:17 +03:00
|
|
|
|
|
|
|
let typedef_info = function
|
|
|
|
| TStruct t -> StructName.get_info t
|
|
|
|
| TEnum t -> EnumName.get_info t
|
|
|
|
| TScope (s, _) -> ScopeName.get_info s
|
|
|
|
|
2021-03-12 19:07:25 +03:00
|
|
|
(** Process the names of all declaration items *)
|
2023-05-17 16:44:57 +03:00
|
|
|
let process_name_item (ctxt : context) (item : Surface.Ast.code_item Mark.pos) :
|
|
|
|
context =
|
2021-04-29 18:46:56 +03:00
|
|
|
let raise_already_defined_error (use : Uid.MarkedString.info) name pos msg =
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error_full
|
2023-06-07 19:15:14 +03:00
|
|
|
[
|
|
|
|
( Some (fun ppf -> Format.pp_print_string ppf "First definition:"),
|
|
|
|
Mark.get use );
|
|
|
|
Some (fun ppf -> Format.pp_print_string ppf "Second definition:"), pos;
|
|
|
|
]
|
2023-06-07 19:10:50 +03:00
|
|
|
"%s name @{<yellow>\"%s\"@} already defined" msg name
|
2021-03-12 19:07:25 +03:00
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove item with
|
2022-10-21 16:47:17 +03:00
|
|
|
| ScopeDecl decl ->
|
2022-05-12 16:10:55 +03:00
|
|
|
let name, pos = decl.scope_decl_name in
|
|
|
|
(* Checks if the name is already used *)
|
2022-10-21 16:47:17 +03:00
|
|
|
Option.iter
|
|
|
|
(fun use ->
|
|
|
|
raise_already_defined_error (typedef_info use) name pos "scope")
|
2023-04-18 15:39:38 +03:00
|
|
|
(Ident.Map.find_opt name ctxt.typedefs);
|
2023-08-30 18:49:29 +03:00
|
|
|
let scope_uid = ScopeName.fresh ctxt.path (name, pos) in
|
|
|
|
let in_struct_name = StructName.fresh ctxt.path (name ^ "_in", pos) in
|
|
|
|
let out_struct_name = StructName.fresh ctxt.path (name, pos) in
|
2022-10-21 16:47:17 +03:00
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
typedefs =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.add name
|
2022-11-17 19:13:35 +03:00
|
|
|
(TScope
|
|
|
|
( scope_uid,
|
|
|
|
{
|
2023-08-10 17:52:39 +03:00
|
|
|
in_struct_name;
|
|
|
|
out_struct_name;
|
2022-11-21 12:12:45 +03:00
|
|
|
out_struct_fields = ScopeVar.Map.empty;
|
2022-11-17 19:13:35 +03:00
|
|
|
} ))
|
2022-10-21 16:47:17 +03:00
|
|
|
ctxt.typedefs;
|
|
|
|
scopes =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeName.Map.add scope_uid
|
2022-10-21 16:47:17 +03:00
|
|
|
{
|
2023-04-18 15:39:38 +03:00
|
|
|
var_idmap = Ident.Map.empty;
|
2023-04-18 11:31:44 +03:00
|
|
|
scope_defs_contexts = Ast.ScopeDef.Map.empty;
|
2022-11-21 12:12:45 +03:00
|
|
|
sub_scopes = ScopeName.Set.empty;
|
2022-10-21 16:47:17 +03:00
|
|
|
}
|
|
|
|
ctxt.scopes;
|
|
|
|
}
|
|
|
|
| StructDecl sdecl ->
|
2022-05-12 16:10:55 +03:00
|
|
|
let name, pos = sdecl.struct_decl_name in
|
2022-10-21 16:47:17 +03:00
|
|
|
Option.iter
|
|
|
|
(fun use ->
|
|
|
|
raise_already_defined_error (typedef_info use) name pos "struct")
|
2023-04-18 15:39:38 +03:00
|
|
|
(Ident.Map.find_opt name ctxt.typedefs);
|
2023-08-30 18:49:29 +03:00
|
|
|
let s_uid = StructName.fresh ctxt.path sdecl.struct_decl_name in
|
2022-10-21 16:47:17 +03:00
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
typedefs =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.add
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove sdecl.struct_decl_name)
|
2022-10-21 16:47:17 +03:00
|
|
|
(TStruct s_uid) ctxt.typedefs;
|
|
|
|
}
|
|
|
|
| EnumDecl edecl ->
|
2022-05-12 16:10:55 +03:00
|
|
|
let name, pos = edecl.enum_decl_name in
|
2022-10-21 16:47:17 +03:00
|
|
|
Option.iter
|
|
|
|
(fun use ->
|
|
|
|
raise_already_defined_error (typedef_info use) name pos "enum")
|
2023-04-18 15:39:38 +03:00
|
|
|
(Ident.Map.find_opt name ctxt.typedefs);
|
2023-08-30 18:49:29 +03:00
|
|
|
let e_uid = EnumName.fresh ctxt.path edecl.enum_decl_name in
|
2022-10-21 16:47:17 +03:00
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
typedefs =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.add
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove edecl.enum_decl_name)
|
2022-10-21 16:47:17 +03:00
|
|
|
(TEnum e_uid) ctxt.typedefs;
|
|
|
|
}
|
2021-03-12 19:07:25 +03:00
|
|
|
| ScopeUse _ -> ctxt
|
2023-01-23 14:19:36 +03:00
|
|
|
| Topdef def ->
|
|
|
|
let name, pos = def.topdef_name in
|
|
|
|
Option.iter
|
|
|
|
(fun use ->
|
|
|
|
raise_already_defined_error (TopdefName.get_info use) name pos
|
|
|
|
"toplevel definition")
|
2023-04-18 15:39:38 +03:00
|
|
|
(Ident.Map.find_opt name ctxt.topdefs);
|
2023-08-30 18:49:29 +03:00
|
|
|
let uid = TopdefName.fresh ctxt.path def.topdef_name in
|
2023-08-31 17:54:45 +03:00
|
|
|
{ ctxt with topdefs = Ident.Map.add name uid ctxt.topdefs }
|
2021-03-12 19:07:25 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Process a code item that is a declaration *)
|
2023-05-17 16:44:57 +03:00
|
|
|
let process_decl_item (ctxt : context) (item : Surface.Ast.code_item Mark.pos) :
|
|
|
|
context =
|
|
|
|
match Mark.remove item with
|
2020-12-04 20:02:49 +03:00
|
|
|
| ScopeDecl decl -> process_scope_decl ctxt decl
|
|
|
|
| StructDecl sdecl -> process_struct_decl ctxt sdecl
|
|
|
|
| EnumDecl edecl -> process_enum_decl ctxt edecl
|
|
|
|
| ScopeUse _ -> ctxt
|
2023-08-31 17:54:45 +03:00
|
|
|
| Topdef def -> process_topdef ctxt def
|
2020-07-16 13:04:23 +03:00
|
|
|
|
|
|
|
(** Process a code block *)
|
2022-03-08 17:03:14 +03:00
|
|
|
let process_code_block
|
2023-08-10 17:52:39 +03:00
|
|
|
(process_item : context -> Surface.Ast.code_item Mark.pos -> context)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2023-08-16 01:04:45 +03:00
|
|
|
(block : Surface.Ast.code_block) : context =
|
2020-09-12 21:22:47 +03:00
|
|
|
List.fold_left (fun ctxt decl -> process_item ctxt decl) ctxt block
|
2020-07-16 13:04:23 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Process a law structure, only considering the code blocks *)
|
2022-03-08 17:03:14 +03:00
|
|
|
let rec process_law_structure
|
2023-08-10 17:52:39 +03:00
|
|
|
(process_item : context -> Surface.Ast.code_item Mark.pos -> context)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2023-08-16 01:04:45 +03:00
|
|
|
(s : Surface.Ast.law_structure) : context =
|
2020-10-04 02:25:37 +03:00
|
|
|
match s with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.LawHeading (_, children) ->
|
2022-05-12 16:10:55 +03:00
|
|
|
List.fold_left
|
2023-08-10 17:52:39 +03:00
|
|
|
(fun ctxt child -> process_law_structure process_item ctxt child)
|
2022-05-12 16:10:55 +03:00
|
|
|
ctxt children
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.CodeBlock (block, _, _) ->
|
2023-08-10 17:52:39 +03:00
|
|
|
process_code_block process_item ctxt block
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.LawInclude _ | Surface.Ast.LawText _ -> ctxt
|
2023-09-05 16:00:55 +03:00
|
|
|
| Surface.Ast.ModuleDef _ | Surface.Ast.ModuleUse _ -> ctxt
|
2020-07-16 13:04:23 +03:00
|
|
|
|
2020-12-18 15:13:51 +03:00
|
|
|
(** {1 Scope uses pass} *)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let get_def_key
|
2023-01-04 18:12:36 +03:00
|
|
|
(name : Surface.Ast.scope_var)
|
2023-05-17 16:44:57 +03:00
|
|
|
(state : Surface.Ast.lident Mark.pos option)
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope_uid : ScopeName.t)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(pos : Pos.t) : Ast.ScopeDef.t =
|
2022-11-21 12:12:45 +03:00
|
|
|
let scope_ctxt = ScopeName.Map.find scope_uid ctxt.scopes in
|
2021-01-22 07:47:48 +03:00
|
|
|
match name with
|
2022-05-12 16:10:55 +03:00
|
|
|
| [x] ->
|
|
|
|
let x_uid = get_var_uid scope_uid ctxt x in
|
2022-11-21 12:12:45 +03:00
|
|
|
let var_sig = ScopeVar.Map.find x_uid ctxt.var_typs in
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDef.Var
|
2022-05-12 16:10:55 +03:00
|
|
|
( x_uid,
|
|
|
|
match state with
|
|
|
|
| Some state -> (
|
|
|
|
try
|
|
|
|
Some
|
2023-04-18 15:39:38 +03:00
|
|
|
(Ident.Map.find (Mark.remove state) var_sig.var_sig_states_idmap)
|
2023-08-15 17:57:52 +03:00
|
|
|
with Ident.Map.Not_found _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-05-12 16:10:55 +03:00
|
|
|
[
|
2023-05-17 16:44:57 +03:00
|
|
|
None, Mark.get state;
|
|
|
|
Some "Variable declaration:", Mark.get (ScopeVar.get_info x_uid);
|
2022-05-12 16:10:55 +03:00
|
|
|
]
|
|
|
|
"This identifier is not a state declared for variable %a."
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeVar.format x_uid)
|
2022-05-12 16:10:55 +03:00
|
|
|
| None ->
|
2023-04-18 15:39:38 +03:00
|
|
|
if not (Ident.Map.is_empty var_sig.var_sig_states_idmap) then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-05-12 16:10:55 +03:00
|
|
|
[
|
2023-05-17 16:44:57 +03:00
|
|
|
None, Mark.get x;
|
|
|
|
Some "Variable declaration:", Mark.get (ScopeVar.get_info x_uid);
|
2022-05-12 16:10:55 +03:00
|
|
|
]
|
|
|
|
"This definition does not indicate which state has to be \
|
|
|
|
considered for variable %a."
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeVar.format x_uid
|
2022-05-12 16:10:55 +03:00
|
|
|
else None )
|
|
|
|
| [y; x] ->
|
2023-08-31 11:07:57 +03:00
|
|
|
let (subscope_uid, subscope_real_uid) : SubScopeName.t * ScopeName.t =
|
2023-04-18 15:39:38 +03:00
|
|
|
match Ident.Map.find_opt (Mark.remove y) scope_ctxt.var_idmap with
|
2022-10-26 12:09:15 +03:00
|
|
|
| Some (SubScope (v, u)) -> v, u
|
|
|
|
| Some _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error pos
|
2022-11-03 17:18:51 +03:00
|
|
|
"Invalid access to input variable, %a is not a subscope"
|
2023-05-17 16:44:57 +03:00
|
|
|
Print.lit_style (Mark.remove y)
|
2022-10-26 12:09:15 +03:00
|
|
|
| None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error pos "No definition found for subscope %a"
|
2023-05-17 16:44:57 +03:00
|
|
|
Print.lit_style (Mark.remove y)
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2023-08-31 11:07:57 +03:00
|
|
|
let x_uid = get_var_uid subscope_real_uid ctxt x in
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDef.SubScopeVar (subscope_uid, x_uid, pos)
|
2022-06-01 12:13:14 +03:00
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error pos
|
2022-06-01 12:13:14 +03:00
|
|
|
"This line is defining a quantity that is neither a scope variable nor a \
|
|
|
|
subscope variable. In particular, it is not possible to define struct \
|
|
|
|
fields individually in Catala."
|
2021-01-22 07:47:48 +03:00
|
|
|
|
2022-11-18 18:12:14 +03:00
|
|
|
let update_def_key_ctx
|
|
|
|
(d : Surface.Ast.definition)
|
|
|
|
(def_key_ctx : scope_def_context) : scope_def_context =
|
|
|
|
(* First, we update the def key context with information about the
|
|
|
|
definition's label*)
|
|
|
|
let def_key_ctx =
|
|
|
|
match d.Surface.Ast.definition_label with
|
|
|
|
| None -> def_key_ctx
|
|
|
|
| Some label ->
|
|
|
|
let new_label_idmap =
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.update (Mark.remove label)
|
2022-11-18 18:12:14 +03:00
|
|
|
(fun existing_label ->
|
|
|
|
match existing_label with
|
|
|
|
| Some existing_label -> Some existing_label
|
|
|
|
| None -> Some (LabelName.fresh label))
|
|
|
|
def_key_ctx.label_idmap
|
|
|
|
in
|
|
|
|
{ def_key_ctx with label_idmap = new_label_idmap }
|
|
|
|
in
|
|
|
|
(* And second, we update the map of default rulenames for unlabeled
|
|
|
|
exceptions *)
|
|
|
|
match d.Surface.Ast.definition_exception_to with
|
|
|
|
(* If this definition is an exception, it cannot be a default definition *)
|
|
|
|
| UnlabeledException | ExceptionToLabel _ -> def_key_ctx
|
|
|
|
(* If it is not an exception, we need to distinguish between several cases *)
|
|
|
|
| NotAnException -> (
|
|
|
|
match def_key_ctx.default_exception_rulename with
|
|
|
|
(* There was already a default definition for this key. If we need it, it is
|
|
|
|
ambiguous *)
|
|
|
|
| Some old ->
|
|
|
|
{
|
|
|
|
def_key_ctx with
|
|
|
|
default_exception_rulename =
|
|
|
|
Some
|
|
|
|
(Ambiguous
|
2023-05-17 16:44:57 +03:00
|
|
|
([Mark.get d.definition_name]
|
2022-11-18 18:12:14 +03:00
|
|
|
@
|
|
|
|
match old with Ambiguous old -> old | Unique (_, pos) -> [pos]));
|
|
|
|
}
|
|
|
|
(* No definition has been set yet for this key *)
|
|
|
|
| None -> (
|
|
|
|
match d.Surface.Ast.definition_label with
|
|
|
|
(* This default definition has a label. This is not allowed for unlabeled
|
|
|
|
exceptions *)
|
|
|
|
| Some _ ->
|
|
|
|
{
|
|
|
|
def_key_ctx with
|
|
|
|
default_exception_rulename =
|
2023-05-17 16:44:57 +03:00
|
|
|
Some (Ambiguous [Mark.get d.definition_name]);
|
2022-11-18 18:12:14 +03:00
|
|
|
}
|
|
|
|
(* This is a possible default definition for this key. We create and store
|
|
|
|
a fresh rulename *)
|
|
|
|
| None ->
|
|
|
|
{
|
|
|
|
def_key_ctx with
|
|
|
|
default_exception_rulename =
|
2023-05-17 16:44:57 +03:00
|
|
|
Some (Unique (d.definition_id, Mark.get d.definition_name));
|
2022-11-18 18:12:14 +03:00
|
|
|
}))
|
|
|
|
|
|
|
|
let empty_def_key_ctx =
|
|
|
|
{
|
|
|
|
(* Here, this is the first time we encounter a definition for this
|
|
|
|
definition key *)
|
|
|
|
default_exception_rulename = None;
|
2023-04-18 15:39:38 +03:00
|
|
|
label_idmap = Ident.Map.empty;
|
2022-11-18 18:12:14 +03:00
|
|
|
}
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let process_definition
|
2022-05-12 16:10:55 +03:00
|
|
|
(ctxt : context)
|
2022-08-12 23:42:39 +03:00
|
|
|
(s_name : ScopeName.t)
|
2022-11-07 15:50:28 +03:00
|
|
|
(d : Surface.Ast.definition) : context =
|
2022-01-03 20:39:59 +03:00
|
|
|
(* We update the definition context inside the big context *)
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
scopes =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeName.Map.update s_name
|
2022-01-03 20:39:59 +03:00
|
|
|
(fun (s_ctxt : scope_context option) ->
|
|
|
|
let def_key =
|
2022-03-08 17:03:14 +03:00
|
|
|
get_def_key
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove d.definition_name)
|
2022-03-08 17:03:14 +03:00
|
|
|
d.definition_state s_name ctxt
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get d.definition_name)
|
2022-01-03 20:39:59 +03:00
|
|
|
in
|
|
|
|
match s_ctxt with
|
|
|
|
| None -> assert false (* should not happen *)
|
|
|
|
| Some s_ctxt ->
|
2022-05-12 16:10:55 +03:00
|
|
|
Some
|
|
|
|
{
|
|
|
|
s_ctxt with
|
|
|
|
scope_defs_contexts =
|
2023-04-18 11:31:44 +03:00
|
|
|
Ast.ScopeDef.Map.update def_key
|
2022-05-12 16:10:55 +03:00
|
|
|
(fun def_key_ctx ->
|
2022-11-18 18:12:14 +03:00
|
|
|
Some
|
|
|
|
(update_def_key_ctx d
|
|
|
|
(Option.value ~default:empty_def_key_ctx def_key_ctx)))
|
2022-05-12 16:10:55 +03:00
|
|
|
s_ctxt.scope_defs_contexts;
|
|
|
|
})
|
2022-01-03 20:39:59 +03:00
|
|
|
ctxt.scopes;
|
|
|
|
}
|
2020-12-18 15:13:51 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let process_scope_use_item
|
2022-08-12 23:42:39 +03:00
|
|
|
(s_name : ScopeName.t)
|
2022-03-08 17:03:14 +03:00
|
|
|
(ctxt : context)
|
2023-05-17 16:44:57 +03:00
|
|
|
(sitem : Surface.Ast.scope_use_item Mark.pos) : context =
|
|
|
|
match Mark.remove sitem with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Rule r -> process_definition ctxt s_name (Surface.Ast.rule_to_def r)
|
2020-12-18 15:13:51 +03:00
|
|
|
| Definition d -> process_definition ctxt s_name d
|
|
|
|
| _ -> ctxt
|
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
let process_scope_use (ctxt : context) (suse : Surface.Ast.scope_use) : context
|
|
|
|
=
|
2020-12-18 15:13:51 +03:00
|
|
|
let s_name =
|
2022-10-21 16:47:17 +03:00
|
|
|
match
|
2023-04-18 15:39:38 +03:00
|
|
|
Ident.Map.find_opt
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove suse.Surface.Ast.scope_use_name)
|
2022-10-21 16:47:17 +03:00
|
|
|
ctxt.typedefs
|
|
|
|
with
|
|
|
|
| Some (TScope (sn, _)) -> sn
|
|
|
|
| _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get suse.Surface.Ast.scope_use_name)
|
2023-06-07 19:15:14 +03:00
|
|
|
"@{<yellow>\"%s\"@}: this scope has not been declared anywhere, is it \
|
|
|
|
a typo?"
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove suse.Surface.Ast.scope_use_name)
|
2020-12-18 15:13:51 +03:00
|
|
|
in
|
2022-11-07 15:50:28 +03:00
|
|
|
List.fold_left
|
|
|
|
(process_scope_use_item s_name)
|
|
|
|
ctxt suse.Surface.Ast.scope_use_items
|
2020-12-18 15:13:51 +03:00
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let process_use_item (ctxt : context) (item : Surface.Ast.code_item Mark.pos) :
|
|
|
|
context =
|
|
|
|
match Mark.remove item with
|
2023-01-23 14:19:36 +03:00
|
|
|
| ScopeDecl _ | StructDecl _ | EnumDecl _ | Topdef _ -> ctxt
|
2020-12-18 15:13:51 +03:00
|
|
|
| ScopeUse suse -> process_scope_use ctxt suse
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** {1 API} *)
|
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let empty_ctxt =
|
|
|
|
{
|
2023-08-30 18:49:29 +03:00
|
|
|
path = [];
|
2023-08-10 17:52:39 +03:00
|
|
|
typedefs = Ident.Map.empty;
|
|
|
|
scopes = ScopeName.Map.empty;
|
|
|
|
topdefs = Ident.Map.empty;
|
|
|
|
topdef_types = TopdefName.Map.empty;
|
|
|
|
var_typs = ScopeVar.Map.empty;
|
|
|
|
structs = StructName.Map.empty;
|
|
|
|
field_idmap = Ident.Map.empty;
|
|
|
|
enums = EnumName.Map.empty;
|
|
|
|
constructor_idmap = Ident.Map.empty;
|
|
|
|
modules = ModuleName.Map.empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
let import_module modules (name, intf) =
|
2023-09-15 15:56:35 +03:00
|
|
|
let mname = ModuleName.of_string name in
|
2023-08-30 18:49:29 +03:00
|
|
|
let ctxt = { empty_ctxt with modules; path = [mname] } in
|
2023-08-16 01:04:45 +03:00
|
|
|
let ctxt = List.fold_left process_name_item ctxt intf in
|
|
|
|
let ctxt = List.fold_left process_decl_item ctxt intf in
|
2023-08-10 17:52:39 +03:00
|
|
|
let ctxt = { ctxt with modules = empty_ctxt.modules } in
|
2023-08-16 01:04:45 +03:00
|
|
|
(* No submodules at the moment, a module may use the ones loaded before it,
|
|
|
|
but doesn't reexport them *)
|
2023-08-30 18:49:29 +03:00
|
|
|
ModuleName.Map.add mname ctxt modules
|
2023-08-10 17:52:39 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Derive the context from metadata, in one pass over the declarations *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let form_context (prgm : Surface.Ast.program) : context =
|
2023-08-10 17:52:39 +03:00
|
|
|
let modules =
|
|
|
|
List.fold_left import_module ModuleName.Map.empty prgm.program_modules
|
2020-07-16 13:04:23 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let ctxt = { empty_ctxt with modules } in
|
2023-08-16 00:52:03 +03:00
|
|
|
let rec gather_var_sigs acc modules =
|
2023-08-16 01:04:45 +03:00
|
|
|
(* Scope vars from imported modules need to be accessible directly for
|
|
|
|
definitions through submodules *)
|
|
|
|
ModuleName.Map.fold
|
|
|
|
(fun _modname mctx acc ->
|
2023-08-16 00:52:03 +03:00
|
|
|
let acc = gather_var_sigs acc mctx.modules in
|
|
|
|
ScopeVar.Map.union (fun _ _ -> assert false) acc mctx.var_typs)
|
|
|
|
modules acc
|
|
|
|
in
|
|
|
|
let ctxt =
|
|
|
|
{ ctxt with var_typs = gather_var_sigs ScopeVar.Map.empty ctxt.modules }
|
|
|
|
in
|
2020-09-12 21:22:47 +03:00
|
|
|
let ctxt =
|
|
|
|
List.fold_left
|
2023-08-10 17:52:39 +03:00
|
|
|
(process_law_structure process_name_item)
|
|
|
|
ctxt prgm.program_items
|
2020-09-12 21:22:47 +03:00
|
|
|
in
|
2021-03-12 19:07:25 +03:00
|
|
|
let ctxt =
|
|
|
|
List.fold_left
|
2023-08-10 17:52:39 +03:00
|
|
|
(process_law_structure process_decl_item)
|
2021-03-12 19:07:25 +03:00
|
|
|
ctxt prgm.program_items
|
|
|
|
in
|
2020-12-18 15:13:51 +03:00
|
|
|
let ctxt =
|
|
|
|
List.fold_left
|
2023-08-10 17:52:39 +03:00
|
|
|
(process_law_structure process_use_item)
|
2020-12-18 15:13:51 +03:00
|
|
|
ctxt prgm.program_items
|
|
|
|
in
|
2020-12-06 19:48:15 +03:00
|
|
|
ctxt
|