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>>

var2name

table<string;string>

var2expr

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

type2type

table<string;string>

blockArgName

table<string;string>

annArg

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

This structure contains collection of subsitution rules for a template.

32.1. 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.

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.

replaceAnnotationArgument(self: Template; name: string const; cb: lambda<(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.2. Template application

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

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<(rules:templates_boost::Template -const):void> const)

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<(rules:templates_boost::Template -const):void> const)

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.3. 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.4. 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.5. 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<(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.6. 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.