skylabs.lang.cpp.mparser.tu
(*
* Copyright (c) 2023-2024 BlueRock Security, Inc.
* This software is distributed under the terms of the BedRock Open-Source License.
* See the LICENSE-BedRock file in the repository root for details.
*)
Require Import skylabs.lang.cpp.mparser.prelude.
Require Import skylabs.lang.cpp.syntax.translation_unit.
Require Import skylabs.lang.cpp.syntax.namemap.
Require Import skylabs.lang.cpp.syntax.untemp.
Require Export skylabs.lang.cpp.syntax.decl.
Require Export skylabs.lang.cpp.syntax.decl.
* Copyright (c) 2023-2024 BlueRock Security, Inc.
* This software is distributed under the terms of the BedRock Open-Source License.
* See the LICENSE-BedRock file in the repository root for details.
*)
Require Import skylabs.lang.cpp.mparser.prelude.
Require Import skylabs.lang.cpp.syntax.translation_unit.
Require Import skylabs.lang.cpp.syntax.namemap.
Require Import skylabs.lang.cpp.syntax.untemp.
Require Export skylabs.lang.cpp.syntax.decl.
Require Export skylabs.lang.cpp.syntax.decl.
Definition with_templates (tu : translation_unit) (mtu : Mtranslation_unit) : translation_unit :=
{|
translation_unit.symbols := tu.(translation_unit.symbols);
translation_unit.types := tu.(translation_unit.types);
translation_unit.namespace_aliases := tu.(translation_unit.namespace_aliases);
translation_unit.initializer := tu.(translation_unit.initializer);
translation_unit.asserts := tu.(translation_unit.asserts);
translation_unit.abi := tu.(translation_unit.abi);
translation_unit.msymbols := mtu.(templates.msymbols);
translation_unit.mtypes := mtu.(templates.mtypes);
translation_unit.maliases := mtu.(templates.maliases);
translation_unit.minstances := mtu.(templates.minstances);
translation_unit.dropped_symbols := tu.(translation_unit.dropped_symbols)
|}.
Module Import Mtranslation_unit.
We work with an exploded Mtranslation_unit and raw trees for
efficiency.
Definition raw_symbol_table : Type := TM.Raw.tree (template MObjValue).
Definition raw_type_table : Type := TM.Raw.tree (template MGlobDecl).
Definition raw_alias_table : Type := TM.Raw.tree (template Mtype).
Definition raw_instance_table : Type := NM.Raw.tree Mtpreinst.
(* Definition raw_name_table : Type := TM.Raw.tree Mname. *)
Definition t : Type :=
raw_symbol_table -> raw_type_table -> raw_alias_table -> raw_instance_table -> (* raw_name_table -> *)
(raw_symbol_table -> raw_type_table -> raw_alias_table -> raw_instance_table -> (* raw_name_table -> *) Mtranslation_unit) ->
Mtranslation_unit.
Definition _symbols (f : raw_symbol_table -> raw_symbol_table) : t :=
fun s t a i k => k (f s) t a i.
Definition _types (f : raw_type_table -> raw_type_table) : t :=
fun s t a i k => k s (f t) a i.
Definition _aliases (f : raw_alias_table -> raw_alias_table) : t :=
fun s t a i k => k s t (f a) i.
Definition _instances (f : raw_instance_table -> raw_instance_table) : t :=
fun s t a i k => k s t a (f i).
(*
Definition _names (f : raw_name_table -> raw_name_table) : t :=
fun s t a i n k => k s t a i (f n).
*)
Definition _skip : t :=
fun s t a i k => k s t a i.
Fixpoint decls' (ds : list t) : t :=
match ds with
| nil => fun s t a i k => k s t a i
| d :: ds => fun s t a i k => d s t a i (fun s t a i => decls' ds s t a i k)
end.
TODO: Do we still need
map_canon?
Definition decls (ds : list t) : Mtranslation_unit :=
decls' ds ∅ ∅ ∅ ∅ $ fun s t a i => {|
templates.msymbols := TM.from_raw s;
templates.mtypes := TM.from_raw t;
templates.maliases := TM.from_raw a;
templates.minstances := NM.from_raw i;
|}.
End Mtranslation_unit.
#[local] Notation K := Mtranslation_unit.t (only parsing).
#[local] Notation Mtemp_params := (list (Mtemp_param * option temp_arg)).
Definition Dtemplated_variable (ps : Mtemp_params) (n : Mname) (t : Mtype) (init : global_init.t) : K :=
_symbols <[n := Template ps $ Ovar t init]>.
Definition Dtemplated_function (ps : Mtemp_params) (n : Mname) (f : MFunc) : K :=
_symbols <[n := Template ps $ Ofunction f]>.
Definition Dtemplated_method (ps : Mtemp_params) (n : Mname) (static : bool) (f : MMethod) : K :=
_symbols <[n := Template ps $ if static then Ofunction $ static_method f else Omethod f]>.
Definition Dtemplated_constructor (ps : Mtemp_params) (n : Mname) (f : MCtor) : K :=
_symbols <[n := Template ps $ Oconstructor f]>.
Definition Dtemplated_destructor (ps : Mtemp_params) (n : Mname) (f : MDtor) : K :=
_symbols <[n := Template ps $ Odestructor f]>.
Definition Dtemplated_type (ps : Mtemp_params) (n : Mname) : K :=
_types <[n := Template ps Gtype]>.
Definition Dtemplated_unsupported (n : Mname) (msg : PrimString.string) : K :=
_types <[n := Template [] $ Gunsupported msg]>.
Definition Dtemplated_struct (ps : Mtemp_params) (n : Mname) (f : option MStruct) : K :=
_types <[n := Template ps $ if f is Some f then Gstruct f else Gtype]>.
Definition Dtemplated_union (ps : Mtemp_params) (n : Mname) (f : option MUnion) : K :=
_types <[n := Template ps $ if f is Some f then Gunion f else Gtype]>.
Definition Dtemplated_enum (ps : Mtemp_params) (n : Mname) (u: Mtype) (cs : list ident) : K :=
_types <[n := Template ps $ Genum u cs]>.
Definition Dtemplated_enum_constant (ps : Mtemp_params) (n : Mname)
(gn : Mglobname) (ut : Mexprtype) (v : N + Z) (init : option MExpr) : K :=
_types $ insert n $
let v := match v with inl n => Echar n ut | inr z => Eint z ut end in
let t := Tenum gn in
Template ps $ Gconstant t $ Some $ Ecast (Cintegral t) v.
Definition Dtemplated_typedef (ps : Mtemp_params) (n : Mname) (t : Mtype) : K :=
_aliases <[n := Template ps t]>.
Definition Dtemplated_static_assert (msg : option bs) (e : MExpr) : K :=
_skip.
(* TODO?: the c : Mname argument should actually be a name, but the parser
setup that we have causes it to be inferred as an Mname. To get around this,
we accept an Mname and then convert it into a name. *)
Definition Dinstantiation (c : Mname) (t : Mname) (xs : list Mtemp_arg) : K :=
match trace.run $ untempN c with
| inl _ => _skip
| inr c =>
_instances <[c := TPreInst t xs]>
end.
Definition Dname (m : Mname) (n : Mname) : K :=
_skip. (* _names <m := n>. *)
decls' ds ∅ ∅ ∅ ∅ $ fun s t a i => {|
templates.msymbols := TM.from_raw s;
templates.mtypes := TM.from_raw t;
templates.maliases := TM.from_raw a;
templates.minstances := NM.from_raw i;
|}.
End Mtranslation_unit.
#[local] Notation K := Mtranslation_unit.t (only parsing).
#[local] Notation Mtemp_params := (list (Mtemp_param * option temp_arg)).
Definition Dtemplated_variable (ps : Mtemp_params) (n : Mname) (t : Mtype) (init : global_init.t) : K :=
_symbols <[n := Template ps $ Ovar t init]>.
Definition Dtemplated_function (ps : Mtemp_params) (n : Mname) (f : MFunc) : K :=
_symbols <[n := Template ps $ Ofunction f]>.
Definition Dtemplated_method (ps : Mtemp_params) (n : Mname) (static : bool) (f : MMethod) : K :=
_symbols <[n := Template ps $ if static then Ofunction $ static_method f else Omethod f]>.
Definition Dtemplated_constructor (ps : Mtemp_params) (n : Mname) (f : MCtor) : K :=
_symbols <[n := Template ps $ Oconstructor f]>.
Definition Dtemplated_destructor (ps : Mtemp_params) (n : Mname) (f : MDtor) : K :=
_symbols <[n := Template ps $ Odestructor f]>.
Definition Dtemplated_type (ps : Mtemp_params) (n : Mname) : K :=
_types <[n := Template ps Gtype]>.
Definition Dtemplated_unsupported (n : Mname) (msg : PrimString.string) : K :=
_types <[n := Template [] $ Gunsupported msg]>.
Definition Dtemplated_struct (ps : Mtemp_params) (n : Mname) (f : option MStruct) : K :=
_types <[n := Template ps $ if f is Some f then Gstruct f else Gtype]>.
Definition Dtemplated_union (ps : Mtemp_params) (n : Mname) (f : option MUnion) : K :=
_types <[n := Template ps $ if f is Some f then Gunion f else Gtype]>.
Definition Dtemplated_enum (ps : Mtemp_params) (n : Mname) (u: Mtype) (cs : list ident) : K :=
_types <[n := Template ps $ Genum u cs]>.
Definition Dtemplated_enum_constant (ps : Mtemp_params) (n : Mname)
(gn : Mglobname) (ut : Mexprtype) (v : N + Z) (init : option MExpr) : K :=
_types $ insert n $
let v := match v with inl n => Echar n ut | inr z => Eint z ut end in
let t := Tenum gn in
Template ps $ Gconstant t $ Some $ Ecast (Cintegral t) v.
Definition Dtemplated_typedef (ps : Mtemp_params) (n : Mname) (t : Mtype) : K :=
_aliases <[n := Template ps t]>.
Definition Dtemplated_static_assert (msg : option bs) (e : MExpr) : K :=
_skip.
(* TODO?: the c : Mname argument should actually be a name, but the parser
setup that we have causes it to be inferred as an Mname. To get around this,
we accept an Mname and then convert it into a name. *)
Definition Dinstantiation (c : Mname) (t : Mname) (xs : list Mtemp_arg) : K :=
match trace.run $ untempN c with
| inl _ => _skip
| inr c =>
_instances <[c := TPreInst t xs]>
end.
Definition Dname (m : Mname) (n : Mname) : K :=
_skip. (* _names <m := n>. *)