32. Template application helpers

The templates boost module implements collection of helper macros and functions to accompany AST.

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

require daslib/templates_boost
Template

Template fields are

kaboomVar

table<string;tuple<prefix:string;suffix:string>>

call2name

table<string;string>

field2name

table<string;string>

var2name

table<string;string>

var2expr

table<string;smart_ptr< ast::Expression >>

var2exprList

table<string;array<smart_ptr< ast::Expression >>>

type2type

table<string;string>

type2etype

table<string; TypeDeclPtr >

blockArgName

table<string;string>

annArg

table<string;lambda<(ann: rtti::AnnotationDeclaration ):void>>

blkArg

table<string;array< VariablePtr >>

tag2expr

table<string;smart_ptr< ast::Expression >>

This structure contains collection of subsitution rules for a template.

32.1. Call macros

qmacro_expr

This macro implements qmacro_expr expression reification. Expected input is a block expression (ExprMakeBlock over ExprBlock). It applies reification rules to the expression, and returns first expression in the block. .. _call-macro-templates_boost-qmacro_variable:

qmacro_variable

This macro implements qmacro_variable expression reification. Expected input is are variable name and type expression (type<…>). Result is a new VariablePtr with the matching name and type. .. _call-macro-templates_boost-qmacro_block_to_array:

qmacro_block_to_array

This macro implements qmacro_block_to_array expression reification. Expected input is a block expression (ExprMakeBlock over ExprBlock). It applies reification rules to the expression, and returns array with contents of the ‘list’ section of the block.

qmacro_function

This macro implements qmacro_function expression reification. Expected input is a block expression (ExprMakeBlock over ExprBlock). It applies reification rules to the expression, and returns a FunctionPtr. New function matches block signature, as well as the block body. .. _call-macro-templates_boost-qmacro:

qmacro

This macro implements qmacro expression reification. It applies reification rules to the expression, and returns direct result of the substitution.

qmacro_method

This macro implements expression reification for class methods.

qmacro_block

This macro implements qmacro_block expression reification. Expected input is a block expression (ExprMakeBlock over ExprBlock). It applies reification rules to the expression, and returns unquoted ExprBlock. .. _call-macro-templates_boost-qmacro_type:

qmacro_type

This macro implements qmacro_type expression reification. Expected input is a type expression (type<…>). Result is TypeDeclPtr of a new type matching subtype of the type expression.

32.2. Template rules

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

argument

argument type

self

templates_boost::Template

name

string const

prefix

string const

suffix

string const

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

replaceVariable(self: Template; name: string const; expr: smart_ptr<ast::Expression>)

argument

argument type

self

templates_boost::Template

name

string const

expr

smart_ptr< ast::Expression >

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

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

argument

argument type

self

templates_boost::Template

name

string const

newName

string const

Adds a rule to the template to rename a variable.

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

argument

argument type

self

templates_boost::Template

name

string const

newName

builtin::das_string const

Adds a rule to the template to rename a variable.

replaceType(self: Template; name: string const; newName: string const)

argument

argument type

self

templates_boost::Template

name

string const

newName

string const

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

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

argument

argument type

self

templates_boost::Template

name

string const

cb

lambda<(ann: rtti::AnnotationDeclaration ):void>

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

replaceBlockArgument(self: Template; name: string const; newName: string const)

argument

argument type

self

templates_boost::Template

name

string const

newName

string const

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

32.3. Template application

apply_template(rules: Template; at: LineInfo const; expr: smart_ptr<ast::Expression>; forceAt: bool const)

apply_template returns ExpressionPtr

argument

argument type

rules

templates_boost::Template

at

rtti::LineInfo const

expr

smart_ptr< ast::Expression >

forceAt

bool const

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

apply_template(at: LineInfo const; expr: smart_ptr<ast::Expression>&; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_template returns ExpressionPtr

argument

argument type

at

rtti::LineInfo const

expr

smart_ptr< ast::Expression >&

blk

block<(rules: templates_boost::Template ):void> const

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

apply_template(expr: smart_ptr<ast::Expression>&; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_template returns ExpressionPtr

argument

argument type

expr

smart_ptr< ast::Expression >&

blk

block<(rules: templates_boost::Template ):void> const

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

32.4. Expression helpers

remove_deref(varname: string const; expr: smart_ptr<ast::Expression>)

argument

argument type

varname

string const

expr

smart_ptr< ast::Expression >

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

32.5. Block helpers

unquote_block(expr: ExpressionPtr)

unquote_block returns smart_ptr< ast::ExprBlock >

argument

argument type

expr

ExpressionPtr

Returns the corresponding block subexpression expression form the ExprMakeBlock.

move_unquote_block(expr: ExpressionPtr)

move_unquote_block returns smart_ptr< ast::ExprBlock >

argument

argument type

expr

ExpressionPtr

Moves the corresponding block subexpression expression form the ExprMakeBlock.

32.6. Global variable helpers

add_global_var(mod: rtti::Module? const; vname: string const; vat: LineInfo const; value: ExpressionPtr)

add_global_var returns bool

argument

argument type

mod

rtti::Module ? const

vname

string const

vat

rtti::LineInfo const

value

ExpressionPtr

Adds global variable to the module, given name and initial value. Global variables type is would be inferred from the initial value. priv specifies if the variable is private to the block.

add_global_var(mod: rtti::Module? const; vname: string const; typ: TypeDeclPtr; vat: LineInfo const; priv: bool const; blk: block<(var v:smart_ptr<ast::Variable> -const):void> const)

add_global_var returns bool

argument

argument type

mod

rtti::Module ? const

vname

string const

typ

TypeDeclPtr

vat

rtti::LineInfo const

priv

bool const

blk

block<(v: VariablePtr ):void> const

Adds global variable to the module, given name and initial value. Global variables type is would be inferred from the initial value. priv specifies if the variable is private to the block.

add_global_var(mod: rtti::Module? const; vname: string const; typ: TypeDeclPtr; vat: LineInfo const; priv: bool const)

add_global_var returns bool

argument

argument type

mod

rtti::Module ? const

vname

string const

typ

TypeDeclPtr

vat

rtti::LineInfo const

priv

bool const

Adds global variable to the module, given name and initial value. Global variables type is would be inferred from the initial value. priv specifies if the variable is private to the block.

add_global_let(mod: rtti::Module? const; vname: string const; vat: LineInfo const; value: ExpressionPtr)

add_global_let returns bool

argument

argument type

mod

rtti::Module ? const

vname

string const

vat

rtti::LineInfo const

value

ExpressionPtr

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

add_global_private_var(mod: rtti::Module? const; vname: string const; vat: LineInfo const; value: ExpressionPtr)

add_global_private_var returns bool

argument

argument type

mod

rtti::Module ? const

vname

string const

vat

rtti::LineInfo const

value

ExpressionPtr

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

add_global_private_let(mod: rtti::Module? const; vname: string const; vat: LineInfo const; value: ExpressionPtr)

add_global_private_let returns bool

argument

argument type

mod

rtti::Module ? const

vname

string const

vat

rtti::LineInfo const

value

ExpressionPtr

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

32.7. Hygenic names

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

make_unique_private_name returns string

argument

argument type

prefix

string const

vat

rtti::LineInfo const

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

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.

32.8. Uncategorized

replaceVarTag(self: Template; name: string const; expr: smart_ptr<ast::Expression>)

argument

argument type

self

templates_boost::Template

name

string const

expr

smart_ptr< ast::Expression >

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

replaceArgumentWithList(self: Template; name: string const; blka: array<smart_ptr<ast::Variable>> const)

argument

argument type

self

templates_boost::Template

name

string const

blka

array< VariablePtr > const

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

replaceVariableWithList(self: Template; name: string const; expr: array<smart_ptr<ast::Expression>> const)

argument

argument type

self

templates_boost::Template

name

string const

expr

array< ExpressionPtr > const

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

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

argument

argument type

self

templates_boost::Template

name

string const

expr

vector<smart_ptr<Expression>> const

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

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

argument

argument type

self

templates_boost::Template

name

string const

newName

string const

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

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

argument

argument type

self

templates_boost::Template

name

string const

newName

builtin::das_string const

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

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

argument

argument type

self

templates_boost::Template

name

string const

expr

TypeDeclPtr

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

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

argument

argument type

self

templates_boost::Template

name

string const

newName

string const

Adds a rule to the template to rename a call.

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

argument

argument type

self

templates_boost::Template

name

string const

newName

builtin::das_string const

Adds a rule to the template to rename a call.

make_expression_block(exprs: array<smart_ptr<ast::Expression>>)

make_expression_block returns smart_ptr< ast::ExprBlock >

argument

argument type

exprs

array< ExpressionPtr >

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

make_expression_block(exprs: dasvector`smart_ptr`Expression)

make_expression_block returns smart_ptr< ast::ExprBlock >

argument

argument type

exprs

vector<smart_ptr<Expression>>

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

add_type_ptr_ref(a: TypeDeclPtr; flags: TypeDeclFlags)

add_type_ptr_ref returns TypeDeclPtr

argument

argument type

a

TypeDeclPtr

flags

TypeDeclFlags

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

add_type_ptr_ref(st: StructurePtr; flags: TypeDeclFlags)

add_type_ptr_ref returns TypeDeclPtr

argument

argument type

st

StructurePtr

flags

TypeDeclFlags

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

apply_qmacro(expr: smart_ptr<ast::Expression>; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_qmacro returns ExpressionPtr

argument

argument type

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

Implementation details for the expression reificaiton. This is a generat expression reification.

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

apply_qblock returns ExpressionPtr

argument

argument type

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

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

apply_qblock_to_array(expr: smart_ptr<ast::Expression>; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_qblock_to_array returns array< ExpressionPtr >

argument

argument type

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

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

apply_qblock_expr(expr: smart_ptr<ast::Expression>; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_qblock_expr returns ExpressionPtr

argument

argument type

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

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

apply_qtype(expr: smart_ptr<ast::Expression>; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_qtype returns TypeDeclPtr

argument

argument type

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

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

expression_at(expr: ExpressionPtr; at: LineInfo const)

expression_at returns ExpressionPtr

argument

argument type

expr

ExpressionPtr

at

rtti::LineInfo const

Force expression location, than return it.

emplace_new(arr: array<smart_ptr<ast::Expression>>; expr: ExpressionPtr)

argument

argument type

arr

array< ExpressionPtr >

expr

ExpressionPtr

Unifies emplace and emplace_new for the array<VariablePtr>

emplace_new(arr: array<smart_ptr<ast::Variable>>; expr: VariablePtr)

argument

argument type

arr

array< VariablePtr >

expr

VariablePtr

Unifies emplace and emplace_new for the array<VariablePtr>

apply_qmacro_function(fname: string const; expr: smart_ptr<ast::Expression>; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_qmacro_function returns FunctionPtr

argument

argument type

fname

string const

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

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

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

apply_qmacro_method returns FunctionPtr

argument

argument type

fname

string const

parent

StructurePtr

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

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

apply_qmacro_variable(vname: string const; expr: smart_ptr<ast::Expression>; blk: block<(var rules:templates_boost::Template -const):void> const)

apply_qmacro_variable returns VariablePtr

argument

argument type

vname

string const

expr

smart_ptr< ast::Expression >

blk

block<(rules: templates_boost::Template ):void> const

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

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

add_structure_field returns int const

argument

argument type

cls

StructurePtr

name

string const

t

TypeDeclPtr

init

ExpressionPtr

Adds a field to the structure.

make_class(name: string const; mod: rtti::Module? const)

make_class returns smart_ptr< ast::Structure >

argument

argument type

name

string const

mod

rtti::Module ? const

Creates a class structure. Adds __rtti, __finalize fields.

make_class(name: string const; baseClass: StructurePtr; mod: rtti::Module? const)

make_class returns smart_ptr< ast::Structure >

argument

argument type

name

string const

baseClass

StructurePtr

mod

rtti::Module ? const

Creates a class structure. Adds __rtti, __finalize fields.

make_class(name: string const; baseClass: ast::Structure? const; mod: rtti::Module? const)

make_class returns smart_ptr< ast::Structure >

argument

argument type

name

string const

baseClass

ast::Structure ? const

mod

rtti::Module ? const

Creates a class structure. Adds __rtti, __finalize fields.

make_class_constructor(cls: StructurePtr; ctor: FunctionPtr)

make_class_constructor returns smart_ptr< ast::Function >

argument

argument type

cls

StructurePtr

ctor

FunctionPtr

Adds a class constructor from a constructor function.

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

argument

argument type

cls

StructurePtr

fun

FunctionPtr

isExplicit

bool const

Constant

bool const

Modifies function to be a member of a particular class.

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

add_array_ptr_ref returns array<smart_ptr<TT>>

argument

argument type

a

array<smart_ptr<auto(TT)>>

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