skylabs.lang.cpp.syntax.mcore

(*
 * Copyright (c) 2024-2026 SkyLabs AI, 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.syntax.prelude.
Require Export skylabs.lang.cpp.syntax.core.
Require Export skylabs.lang.cpp.syntax.decl.

#[local] Set Primitive Projections.

Naming Conventions

BRiCk uses a single AST to represent both templated (sometimes called "meta") terms as well as non-templated terms. We use the M prefix for types when arguments are intended to be templated terms.
**Alternatives** Alternatively, we could also use Definitions. These are a bit heavier weight, but would be preserved when printing terms.

C++ with templates

Notation Mname := name (only parsing).
Notation Mglobname := globname (only parsing).
Notation Mobj_name := obj_name (only parsing).
Notation Mtype := type (only parsing).
Notation Mexprtype := exprtype (only parsing).
Notation Mdecltype := decltype (only parsing).
Notation MCast := Cast (only parsing).
Notation Moperator_impl := operator_impl (only parsing).
Notation MMethodRef := MethodRef (only parsing).
Notation MExpr := Expr (only parsing).
Notation Mfunction_type := function_type (only parsing).
Notation Mtemp_param := temp_param (only parsing).
Notation Mtemp_arg := temp_arg (only parsing).
Notation Matomic_name := atomic_name (only parsing).
Notation MBindingDecl := BindingDecl (only parsing).
Notation MVarDecl := VarDecl (only parsing).
Notation MStmt := Stmt (only parsing).
Notation MUnion := Union (only parsing).
Notation MStruct := Struct (only parsing).
Notation MFunctionBody := FunctionBody (only parsing).
Notation MFunc := Func (only parsing).
Notation MMethod := Method (only parsing).
Notation MCtor := Ctor (only parsing).
Notation MDtor := Dtor (only parsing).
Notation MMember := Member (only parsing).
Notation Mfield_name := field_name.t (only parsing).
Notation MInitPath := InitPath (only parsing).

Template pre-instances

A template file maps the symbol or type name of a template instantiation (bound in a translation unit) to a template pre-instance comprising the instance's template name (bound in a template file) and arguments.
Definition temp_name : Set := name.

Section tpreinst.
  Record tpreinst : Set := TPreInst {
    tpreinst_name : temp_name;
    tpreinst_args : list temp_arg;
  }.

  #[global] Instance tpreinst_inhabited : Inhabited tpreinst.
  Proof. solve_inhabited. Qed.

  #[global] Instance tpreinst_eq_dec : EqDecision tpreinst.
  Proof. solve_decision. Defined.
End tpreinst.
Add Printing Constructor tpreinst.

Template instances

Section tinst.
  #[local] Set Universe Polymorphism.
  #[local] Set Polymorphic Inductive Cumulativity.
  #[local] Unset Auto Template Polymorphism.
  Universe uV.
  Context {V : Type@{uV}}.

  Record tinst : Type@{uV} := TInst {
    tinst_params : list temp_param;
    tinst_args : list temp_arg;
    tinst_value : V;
  }.

  #[global] Instance tinst_inhabited `{!Inhabited V} : Inhabited tinst.
  Proof. solve_inhabited. Qed.

  #[global] Instance tinst_eq_dec
      `{!EqDecision V} :
    EqDecision tinst.
  Proof. solve_decision. Defined.
End tinst.
Add Printing Constructor tinst.
#[global] Arguments tinst : clear implicits.
#[global] Arguments TInst {_} _ _ & _ : assert.

Templated values

int foo() is an ObjValue template<typename T> int foo(T) is a template ObjValue and would be represented as Template [Ptype "T"] (Ofunction ....(Tparam "T") ...)
Section template.
  #[local] Set Universe Polymorphism.
  #[local] Set Polymorphic Inductive Cumulativity.
  #[local] Unset Auto Template Polymorphism.
  Universe uV.
  Context {V : Type@{uV}}.

  Record template : Type@{uV} := Template {
    template_params : list (temp_param * option temp_arg);
    template_value : V;
  }.

  #[global] Instance template_inhabited `{!Inhabited V} : Inhabited template.
  Proof. solve_inhabited. Qed.

  #[global] Instance template_eq_dec `{!EqDecision V} :
    EqDecision template.
  Proof. solve_decision. Defined.
End template.
Add Printing Constructor template.
#[global] Arguments template : clear implicits.
#[global] Arguments Template {_} _ & _ : assert.

#[universes(polymorphic)]
Section template.
  Import UPoly.
  #[global] Instance template_fmap : FMap template := fun A B f t =>
    Template t.(template_params) (f t.(template_value)).

  #[global] Instance template_traverse : Traverse template := fun F _ _ _ A B f t =>
    Template t.(template_params) <$> f t.(template_value).
End template.

Notation Mtpreinst := tpreinst (only parsing).
Notation Mtinst := tinst (only parsing).
Notation MInitializer := Initializer (only parsing).