10.5. Template application helpers

The TEMPLATES_BOOST module extends template utilities with high-level macros for common code generation patterns, including template function generation, type-parameterized struct creation, and compile-time code expansion.

See also decltype macro and template function annotation for decltype and [template] annotations.

All functions and symbols are in “templates_boost” module, use require to get access to it.

require daslib/templates_boost

10.5.1. Structures

templates_boost::Template

This structure contains collection of substitution rules for a template.

Fields
  • kaboomVar : table<string;tuple<prefix:string;suffix:string>> - variable field access replacement rules

  • call2name : table<string;string> - call name replacement rules

  • field2name : table<string;string> - field name replacement rules

  • var2name : table<string;string> - variable name replacement rules

  • var2expr : table<string;smart_ptr< Expression>> - variable expression replacement rules

  • var2exprList : table<string;array<smart_ptr< Expression>>> - variable expression list replacement rules

  • type2type : table<string;string> - type name replacement rules

  • type2etype : table<string; TypeDeclPtr> - type to type declaration replacement rules

  • struct2etype : table< Structure?; TypeDeclPtr> - structure to type declaration replacement rules

  • blockArgName : table<string;string> - block argument name replacement rules

  • annArg : table<string;lambda<(ann: AnnotationDeclaration):void>> - annotation argument replacement rules

  • blkArg : table<string;array< VariablePtr>> - block argument replacement rules

  • tag2expr : table<string;smart_ptr< Expression>> - tag expression replacement rules

10.5.2. Call macros

templates_boost::qmacro_variable

This macro implements expression reification for variables.

templates_boost::qmacro_expr

This macro implements first line of the expression block reification ‘qmacro_expr’

templates_boost::qmacro_function

This macro implements expression reification for functions.

templates_boost::qmacro_block_to_array

This macro implements expression block to array reification ‘qmacro_block_to_array’

templates_boost::qmacro_template_class

Call macro for quoting named template class methods. This macro implements expression reification for the named expressions (function, variable, etc.)

templates_boost::qmacro_method

This macro implements expression reification for class methods.

templates_boost::qmacro_template_function

Call macro for quoting named template functions. This macro implements expression reification for the named expressions (function, variable, etc.)

templates_boost::qmacro

This macro implements expression reification ‘qmacro’

templates_boost::qmacro_block

This macro implements expression block reification ‘qmacro_block’

templates_boost::qmacro_type

This macro implements type declaration reification ‘qmacro_type’

10.5.3. Template rules

templates_boost::kaboomVarField(self: Template; name: string; prefix: string; suffix: string)

Adds a rule to to the template to replace a variable field access with a prefix and suffix. I.e. foo.bar into prefix + bar + suffix

Arguments
  • self : Template

  • name : string

  • prefix : string

  • suffix : string

10.5.3.1. renameCall

templates_boost::renameCall(self: Template; name: string; newName: string)

Adds a rule to the template to rename a call.

Arguments
  • self : Template

  • name : string

  • newName : string

templates_boost::renameCall(self: Template; name: string; newName: das_string)

10.5.3.2. renameField

templates_boost::renameField(self: Template; name: string; newName: string)

Adds a rule to the template to rename any field lookup (., ?., as, is, etc)

Arguments
  • self : Template

  • name : string

  • newName : string

templates_boost::renameField(self: Template; name: string; newName: das_string)

10.5.3.3. renameVariable

templates_boost::renameVariable(self: Template; name: string; newName: string)

Adds a rule to the template to rename a variable.

Arguments
  • self : Template

  • name : string

  • newName : string

templates_boost::renameVariable(self: Template; name: string; newName: das_string)

templates_boost::replaceAnnotationArgument(self: Template; name: string; cb: lambda<(var ann:AnnotationDeclaration):void>)

Adds a rule to the template to replace an annotation argument with the result of a callback.

Arguments
templates_boost::replaceArgumentWithList(self: Template; name: string; blka: array<VariablePtr>)

Adds a rule to the template to replace a block argument with a list of variables.

Arguments
templates_boost::replaceBlockArgument(self: Template; name: string; newName: string)

Adds a rule to the template to rename a block argument.

Arguments
  • self : Template

  • name : string

  • newName : string

templates_boost::replaceStructWithTypeDecl(self: Template; pstruct: Structure?; expr: TypeDeclPtr)

Adds a rule to the template to replace a type alias with another type alias, specified by type declaration.

Arguments
templates_boost::replaceType(self: Template; name: string; newName: string)

Adds a rule to the template to replace a type alias with another type alias, specified by name.

Arguments
  • self : Template

  • name : string

  • newName : string

templates_boost::replaceTypeWithTypeDecl(self: Template; name: string; expr: TypeDeclPtr)

Adds a rule to the template to replace a type alias with another type alias, specified by type declaration.

Arguments
templates_boost::replaceVarTag(self: Template; name: string; expr: smart_ptr<Expression>)

Adds a rule to the template to replace a variable tag with an expression.

Arguments
templates_boost::replaceVariable(self: Template; name: string; expr: smart_ptr<Expression>)

Adds a rule to the template to replace a variable with an expression.

Arguments

10.5.3.4. replaceVariableWithList

templates_boost::replaceVariableWithList(self: Template; name: string; expr: dasvector`smart_ptr`Expression)

Adds a rule to the template to replace a variable with an expression list.

Arguments
  • self : Template

  • name : string

  • expr : vector<smart_ptr<Expression>>

templates_boost::replaceVariableWithList(self: Template; name: string; expr: array<ExpressionPtr>)

10.5.4. Template application

10.5.4.1. apply_template

templates_boost::apply_template(rules: Template; at: LineInfo; typ: smart_ptr<TypeDecl>; forceAt: bool = true) : TypeDeclPtr()

Applies the template to the given expression. If forceAt is set, the resulting expression will have the same line info as ‘at’.

Arguments
templates_boost::apply_template(rules: Template; at: LineInfo; expr: smart_ptr<Expression>; forceAt: bool = true) : ExpressionPtr()
templates_boost::apply_template(at: LineInfo; expr: smart_ptr<Expression>&; blk: block<(var rules:Template):void>) : ExpressionPtr()
templates_boost::apply_template(at: LineInfo; typ: smart_ptr<TypeDecl>&; blk: block<(var rules:Template):void>) : TypeDeclPtr()
templates_boost::apply_template(expr: smart_ptr<Expression>&; blk: block<(var rules:Template):void>) : ExpressionPtr()

10.5.5. Expression helpers

templates_boost::expression_at(expr: ExpressionPtr; at: LineInfo) : ExpressionPtr()

Force expression location, then return it.

Arguments
templates_boost::remove_deref(varname: string; expr: smart_ptr<Expression>)

Removes dereferences of the variable varname from the expression. This is typically used when replacing ‘workhorse’ variable with constant.

Arguments
templates_boost::visit_expression(expr: ExpressionPtr; adapter: smart_ptr<VisitorAdapter>)

Visits the expression with the given visitor adapter.

Arguments

10.5.6. Expression generation

10.5.6.1. make_expression_block

templates_boost::make_expression_block(exprs: dasvector`smart_ptr`Expression) : smart_ptr<ExprBlock>()

Create ExprBlock and move all expressions from expr to the list of the block.

Arguments
  • exprs : vector<smart_ptr<Expression>>

templates_boost::make_expression_block(exprs: array<ExpressionPtr>) : smart_ptr<ExprBlock>()

10.5.7. Block helpers

templates_boost::move_unquote_block(expr: ExpressionPtr) : smart_ptr<ExprBlock>()

Moves the corresponding block subexpression expression from the ExprMakeBlock.

Arguments
templates_boost::unquote_block(expr: ExpressionPtr) : smart_ptr<ExprBlock>()

Returns the corresponding block subexpression expression from the ExprMakeBlock.

Arguments

10.5.8. Global variable helpers

templates_boost::add_global_let(mod: Module?; vname: string; vat: LineInfo; value: ExpressionPtr) : bool()

Add global variable to the module, given name and initial value. Variable type will be constant.

Arguments
templates_boost::add_global_private_let(mod: Module?; vname: string; vat: LineInfo; value: ExpressionPtr) : bool()

Add global variable to the module, given name and initial value. It will be private, and type will be constant.

Arguments
templates_boost::add_global_private_var(mod: Module?; vname: string; vat: LineInfo; value: ExpressionPtr) : bool()

Add global variable to the module, given name and initial value. It will be private.

Arguments

10.5.8.1. add_global_var

templates_boost::add_global_var(mod: Module?; vname: string; typ: TypeDeclPtr; vat: LineInfo; priv: bool; blk: block<(var v:VariablePtr):void>) : bool()

Add global variable to the module, given name and type. priv specifies if the variable is private to the block.

Arguments
templates_boost::add_global_var(mod: Module?; vname: string; vat: LineInfo; value: ExpressionPtr) : bool()
templates_boost::add_global_var(mod: Module?; vname: string; typ: TypeDeclPtr; vat: LineInfo; priv: bool) : bool()

10.5.9. Hygienic names

templates_boost::make_unique_private_name(prefix: string; vat: LineInfo) : string()

Generates unique private name for the variable, given prefix and line info.

Warning

The assumption is that line info is unique for the context of the unique name generation. If it is not, additional measures must be taken to ensure uniqueness of prefix.

Arguments

10.5.10. Quoting macros

templates_boost::apply_qblock(expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : ExpressionPtr()

Implementation details for the expression reification. This is a block reification.

Arguments
templates_boost::apply_qblock_expr(expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : ExpressionPtr()

Implementation details for the expression reification. This is a first line of the block as expression reification.

Arguments
templates_boost::apply_qblock_to_array(expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : array<ExpressionPtr>()

Implementation details for the expression reification. This is a block reification.

Arguments
templates_boost::apply_qmacro(expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : ExpressionPtr()

Implementation details for the expression reification.

Arguments
templates_boost::apply_qmacro_function(fname: string; expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : FunctionPtr()

Implementation details for reification. This is a function generation reification.

Arguments
templates_boost::apply_qmacro_method(fname: string; parent: StructurePtr; expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : FunctionPtr()

Implementation details for reification. This is a class method function generation reification.

Arguments
templates_boost::apply_qmacro_template_class(instance_name: string; template_type: smart_ptr<TypeDecl>; blk: block<(var rules:Template):void>) : TypeDeclPtr()

Implementation details for the expression reification. This is a template class instantiation reification.

Arguments
  • instance_name : string

  • template_type : smart_ptr< TypeDecl>

  • blk : block<(rules: Template):void>

templates_boost::apply_qmacro_template_function(func: FunctionPtr; blk: block<(var rules:Template):void>) : FunctionPtr()

Applies template rules to a function, cloning it with substituted types.

Arguments
templates_boost::apply_qmacro_variable(vname: string; expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : VariablePtr()

Implementation details for reification. This is a variable generation reification.

Arguments
templates_boost::apply_qtype(expr: smart_ptr<Expression>; blk: block<(var rules:Template):void>) : TypeDeclPtr()

Implementation details for the expression reification. This is a type declaration reification.

Arguments

10.5.11. Type pointer helpers

templates_boost::add_array_ptr_ref(a: array<smart_ptr<auto(TT)>>) : array<smart_ptr<TT>>()

Implementation details for the reification. This adds any array to the rules.

Arguments
  • a : array<smart_ptr<auto(TT)>>

10.5.11.1. add_type_ptr_ref

templates_boost::add_type_ptr_ref(st: StructurePtr; flags: TypeDeclFlags) : TypeDeclPtr()

Implementation details for the reification. Creates a type declaration from a structure smart pointer.

Arguments
templates_boost::add_type_ptr_ref(st: EnumerationPtr; flags: TypeDeclFlags) : TypeDeclPtr()
templates_boost::add_type_ptr_ref(a: TypeDeclPtr; flags: TypeDeclFlags) : TypeDeclPtr()
templates_boost::add_type_ptr_ref(anything: auto(TT); flags: TypeDeclFlags) : TypeDeclPtr()
templates_boost::add_type_ptr_ref(st: Structure?; flags: TypeDeclFlags) : TypeDeclPtr()
templates_boost::add_type_ptr_ref(st: Enumeration?; flags: TypeDeclFlags) : TypeDeclPtr()

10.5.12. Structure helpers

templates_boost::add_structure_field(cls: StructurePtr; name: string; t: TypeDeclPtr; init: ExpressionPtr) : int()

Adds a field to the structure.

Arguments

10.5.13. Class generation

templates_boost::enum_class_type(st: auto) : auto()

return underlying type for the enumeration

Arguments
  • st : auto

10.5.13.1. make_class

templates_boost::make_class(name: string; baseClass: StructurePtr; mod: Module?) : smart_ptr<Structure>()

Creates a class structure derived from baseClass. Adds __rtti, __finalize fields.

Arguments
templates_boost::make_class(name: string; mod: Module?) : smart_ptr<Structure>()
templates_boost::make_class(name: string; baseClass: Structure?; mod: Module?) : smart_ptr<Structure>()

templates_boost::make_class_constructor(cls: StructurePtr; ctor: FunctionPtr) : smart_ptr<Function>()

Adds a class constructor from a constructor function.

Arguments
templates_boost::modify_to_class_member(cls: StructurePtr; fun: FunctionPtr; isExplicit: bool; Constant: bool)

Modifies function to be a member of a particular class.

Arguments