Module Stan_math_backend.Cpp

A set of data types representing the C++ we generate

type identifier = string
val identifier_of_sexp : Sexplib0.Sexp.t -> identifier
val sexp_of_identifier : identifier -> Sexplib0.Sexp.t
type type_ =
  1. | Auto
  2. | Void
  3. | Int
  4. | Double
  5. | Complex of type_
  6. | TemplateType of identifier
  7. | StdVector of type_
    (*

    A std::vector. For Eigen Vectors, use Matrix with a row or column size of 1

    *)
  8. | Array of type_ * int
  9. | Tuple of type_ list
  10. | TypeLiteral of identifier
    (*

    Used for things like Eigen::Index

    *)
  11. | Matrix of type_ * int * int * Middle.Mem_pattern.t
  12. | Ref of type_
  13. | Const of type_
  14. | Pointer of type_
  15. | TypeTrait of identifier * type_ list
    (*

    e.g. stan::promote_scalar, stan:base_type

    *)

C++ types

val type__of_sexp : Sexplib0.Sexp.t -> type_
val sexp_of_type_ : type_ -> Sexplib0.Sexp.t
module Types : sig ... end

Helpers for constructing types

type operator =
  1. | Multiply
  2. | Divide
  3. | Add
  4. | Subtract
  5. | Eq
  6. | LEq
  7. | GEq
  8. | Lthn
  9. | Gthn
  10. | And
  11. | Or
val operator_of_sexp : Sexplib0.Sexp.t -> operator
val sexp_of_operator : operator -> Sexplib0.Sexp.t
type expr =
  1. | Literal of string
    (*

    printed as-is

    *)
  2. | Var of identifier
  3. | VarRef of identifier
  4. | Parens of expr
  5. | FunCall of identifier * type_ list * expr list
  6. | MethodCall of expr * identifier * type_ list * expr list
  7. | StaticMethodCall of type_ * identifier * type_ list * expr list
  8. | Constructor of type_ * expr list
    (*

    printed as type(expr1, expr2, ...)

    *)
  9. | InitializerExpr of type_ * expr list
    (*

    printed as type{expr1, expr2, ...}

    *)
  10. | ArrayLiteral of expr list
  11. | TernaryIf of expr * expr * expr
  12. | Cast of type_ * expr
  13. | Index of expr * expr
  14. | Deref of expr
  15. | AllocNew of type_ * expr list
  16. | OperatorNew of identifier * type_ * expr list
    (*

    See operator new for distinctions between allocating and placing news

    *)
  17. | Assign of expr * expr
    (*

    NB: Not all exprs are valid lvalues!

    *)
  18. | StreamInsertion of expr * expr list
    (*

    Corresponds to operator<<

    *)
  19. | BinOp of expr * operator * expr
  20. | PMinus of expr
  21. | Increment of expr
val expr_of_sexp : Sexplib0.Sexp.t -> expr
val sexp_of_expr : expr -> Sexplib0.Sexp.t
module Exprs : sig ... end

Some helper values and functions

module Expression_syntax : sig ... end

Some operators to make streams and method calls look more like the resultant C++

type init =
  1. | Assignment of expr
  2. | Construction of expr list
  3. | InitializerList of expr list
  4. | Uninitialized
val init_of_sexp : Sexplib0.Sexp.t -> init
val sexp_of_init : init -> Sexplib0.Sexp.t
type variable_defn = {
  1. static : bool;
  2. constexpr : bool;
  3. type_ : type_;
  4. name : identifier;
  5. init : init;
}
val make_variable_defn : ?static:bool -> ?constexpr:bool -> type_:type_ -> name:identifier -> ?init:init -> unit -> variable_defn
val variable_defn_of_sexp : Sexplib0.Sexp.t -> variable_defn
val sexp_of_variable_defn : variable_defn -> Sexplib0.Sexp.t
type stmt =
  1. | Expression of expr
  2. | VariableDefn of variable_defn
  3. | For of variable_defn * expr * expr * stmt
  4. | ForEach of type_ * identifier * expr * stmt
  5. | While of expr * stmt
  6. | IfElse of expr * stmt * stmt option
  7. | TryCatch of stmt list * type_ * identifier * stmt list
  8. | Block of stmt list
  9. | Return of expr option
  10. | Throw of expr
  11. | Break
  12. | Continue
  13. | Using of string * type_ option
  14. | Comment of string
val stmt_of_sexp : Sexplib0.Sexp.t -> stmt
val sexp_of_stmt : stmt -> Sexplib0.Sexp.t
module Stmts : sig ... end

Helpers for common statement constructs

module Decls : sig ... end

Declarations which get re-used often in the generated model

type template_parameter =
  1. | Typename of string
    (*

    The name of a template typename

    *)
  2. | RequireAllCondition of [ `Exact of string | `OneOf of string list ] * type_
    (*

    A C++ type trait (e.g. is_arithmetic) and the template name which needs to satisfy that. These are collated into one require_all_t<>

    *)
  3. | Require of string * string list
  4. | Bool of string
    (*

    A named boolean template type

    *)
val template_parameter_of_sexp : Sexplib0.Sexp.t -> template_parameter
val sexp_of_template_parameter : template_parameter -> Sexplib0.Sexp.t
type cv_qualifiers =
  1. | Const
  2. | Final
  3. | NoExcept
val cv_qualifiers_of_sexp : Sexplib0.Sexp.t -> cv_qualifiers
val sexp_of_cv_qualifiers : cv_qualifiers -> Sexplib0.Sexp.t
type fun_defn = {
  1. templates_init : template_parameter list list * bool;
    (*

    Double list since some functions (mainly reduce_sum functors) need two sets of templates

    *)
  2. inline : bool;
  3. return_type : type_;
  4. name : identifier;
  5. args : (type_ * string) list;
  6. cv_qualifiers : cv_qualifiers list;
  7. body : stmt list option;
}
val make_fun_defn : ?templates_init:(template_parameter list list * bool) -> ?inline:bool -> return_type:type_ -> name:identifier -> ?args:(type_ * string) list -> ?cv_qualifiers:cv_qualifiers list -> ?body:stmt list -> unit -> fun_defn
val fun_defn_of_sexp : Sexplib0.Sexp.t -> fun_defn
val sexp_of_fun_defn : fun_defn -> Sexplib0.Sexp.t
val split_fun_decl_defn : fun_defn -> fun_defn * fun_defn
type constructor = {
  1. args : (type_ * string) list;
  2. init_list : (identifier * expr list) list;
  3. body : stmt list;
}
val make_constructor : ?args:(type_ * string) list -> ?init_list:(identifier * expr list) list -> ?body:stmt list -> unit -> constructor
val constructor_of_sexp : Sexplib0.Sexp.t -> constructor
val sexp_of_constructor : constructor -> Sexplib0.Sexp.t
type directive =
  1. | Include of string
  2. | IfNDef of string * defn list
  3. | MacroApply of string * string list

Incomplete list of C++ preprocessor directives

and class_defn = {
  1. class_name : identifier;
  2. final : bool;
  3. public_base : type_;
  4. private_members : defn list;
  5. public_members : defn list;
  6. constructor : constructor;
  7. destructor_body : stmt list;
}

The Stan model class always has a non-default constructor and destructor

and struct_defn = {
  1. param : template_parameter option;
  2. struct_name : identifier;
  3. body : defn list;
}
and defn =
  1. | FunDef of fun_defn
  2. | Class of class_defn
  3. | Struct of struct_defn
  4. | GlobalVariableDefn of variable_defn
  5. | GlobalComment of string
  6. | GlobalUsing of string * type_ option
  7. | Namespace of identifier * defn list
  8. | Preprocessor of directive
val directive_of_sexp : Sexplib0.Sexp.t -> directive
val class_defn_of_sexp : Sexplib0.Sexp.t -> class_defn
val struct_defn_of_sexp : Sexplib0.Sexp.t -> struct_defn
val defn_of_sexp : Sexplib0.Sexp.t -> defn
val sexp_of_directive : directive -> Sexplib0.Sexp.t
val sexp_of_class_defn : class_defn -> Sexplib0.Sexp.t
val sexp_of_struct_defn : struct_defn -> Sexplib0.Sexp.t
val sexp_of_defn : defn -> Sexplib0.Sexp.t
val make_class_defn : name:identifier -> public_base:type_ -> ?final:bool -> private_members:defn list -> public_members:defn list -> constructor:constructor -> ?destructor_body:stmt list -> unit -> class_defn
val make_struct_defn : param:template_parameter option -> name:identifier -> body:defn list -> unit -> struct_defn
type program = defn list

Much like in C++, we define a translation unit as a list of definitions

val program_of_sexp : Sexplib0.Sexp.t -> program
val sexp_of_program : program -> Sexplib0.Sexp.t
module Printing : sig ... end

Pretty-printing of the C++ type

module Tests : sig ... end