Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

dmd.dtemplate

Defines TemplateDeclaration, TemplateInstance and a few utilities
This modules holds the two main template types: TemplateDeclaration, which is the user-provided declaration of a template, and TemplateInstance, which is an instance of a TemplateDeclaration with specific arguments.

Template Parameter Additionally, the classes for template parameters are defined in this module. The base class, TemplateParameter, is inherited by:

  • TemplateTypeParameter
  • TemplateThisParameter
  • TemplateValueParameter
  • TemplateAliasParameter
  • TemplateTupleParameter

Templates semantic The start of the template instantiation process looks like this:

  • A TypeInstance or TypeIdentifier is encountered. TypeInstance have a bang (e.g. Foo!(arg)) while TypeIdentifier don't.
  • A TemplateInstance is instantiated
  • Semantic is run on the TemplateInstance (see dmd.dsymbolsem)
  • The TemplateInstance search for its TemplateDeclaration, runs semantic on the template arguments and deduce the best match among the possible overloads.
  • The TemplateInstance search for existing instances with the same arguments, and uses it if found.
  • Otherwise, the rest of semantic is run on the TemplateInstance.

Authors:

Source dtemplate.d

pure nothrow @nogc @trusted inout(Expression) isExpression(inout RootObject o);
These functions substitute for dynamic_cast. dynamic_cast does not work on earlier versions of gcc.
pure nothrow @nogc @safe bool isError(const RootObject o);
Is this Object an error?
pure nothrow @nogc @safe bool arrayObjectIsError(ref const Objects args);
Are any of the Objects an error?
pure nothrow @nogc @safe inout(Type) getType(inout RootObject o);
Try to get arg as a type.
class TemplateDeclaration: dmd.dsymbol.ScopeDsymbol;
[mixin] template Identifier (parameters) [Constraint] https://dlang.org/spec/template.html https://dlang.org/spec/template-mixin.html
bool isTrivialAliasSeq;
matches pattern template AliasSeq(T...) { alias AliasSeq = T; }
bool isTrivialAlias;
matches pattern template Alias(T) { alias Alias = qualifiers(T); }
bool deprecated_;
this template declaration is deprecated
bool isCmacro;
Whether this template is a translation of a C macro
Expression lastConstraint;
the constraint after the last failed evaluation
Array!Expression lastConstraintNegs;
its negative parts
Objects* lastConstraintTiargs;
template instance arguments for lastConstraint
const(char)* toCharsNoConstraints() const;
Similar to toChars, but does not print the template constraints
TemplateTupleParameter isVariadic();
Check if the last template parameter is a tuple one, and returns it if so, else returns null.
Returns:
The last template parameter if it's a TemplateTupleParameter
bool isOverloadable() const;
We can overload templates.
bool reliesOnTident(Type t, TemplateParameters* tparams, size_t iStart = 0);
Check whether the type t representation relies on one or more the template parameters.
Parameters:
Type t Tested type, if null, returns false.
TemplateParameters* tparams Template parameters.
size_t iStart Start index of tparams to limit the tested parameters. If it's nonzero, tparams[0..iStart] will be excluded from the test target.
bool reliesOnTemplateParameters(Type t, TemplateParameter[] tparams);
Check whether the type t representation relies on one or more the template parameters.
Parameters:
Type t Tested type, if null, returns false.
TemplateParameter[] tparams Template parameters.
abstract class TemplateParameter: dmd.ast_node.ASTNode;
class TemplateTypeParameter: dmd.dtemplate.TemplateParameter;

Syntax ident : specType = defaultType

class TemplateThisParameter: dmd.dtemplate.TemplateTypeParameter;

Syntax this ident : specType = defaultType

class TemplateValueParameter: dmd.dtemplate.TemplateParameter;

Syntax valType ident : specValue = defaultValue

class TemplateAliasParameter: dmd.dtemplate.TemplateParameter;

Syntax specType ident : specAlias = defaultAlias

class TemplateTupleParameter: dmd.dtemplate.TemplateParameter;

Syntax ident ...

class TemplateInstance: dmd.dsymbol.ScopeDsymbol;

Given foo!(args) => name = foo tiargs = args

TemplateInstance inst;
If this is not null and it has a value that is not the current object, then this field points to an existing template instance and that object has been duplicated into us.
If this object is a duplicate, the memberOf field will be set to a root module (passed on CLI).
This information is useful to deduplicate analysis that may occur after semantic 3 has completed.
See Also:
memberOf
Expressions* fargs;
For function template, these are the function fnames(name and loc of it) and arguments Relevant because different resolutions of auto ref parameters create different template instances even with the same template arguments
Module memberOf;
If this is not null then this template instance appears in a root module's members.

Note This is not useful for determining duplication status of this template instance. Use the field inst for determining if a template instance has been duplicated into this object.

See Also:
inst
final pure nothrow @nogc @property @safe bool semantictiargsdone() const;
has semanticTiargs() been done?
final pure nothrow @nogc @property @safe bool havetempdecl() const;
if used second constructor
final pure nothrow @nogc @property @safe bool gagged() const;
if the instantiation is done with error gagging
this(Loc loc, TemplateDeclaration td, Objects* tiargs) scope;
This constructor is only called when we figured out which function template to instantiate.
final void printInstantiationTrace(Classification cl = Classification.error, const(uint) max_shown = global.params.v.errorSupplementCount());
Given an error instantiating the TemplateInstance, give the nested TemplateInstance instantiations that got us here. Those are a list threaded into the nested scopes.
Parameters:
Classification cl classification of this trace as printing either errors or deprecations
const(uint) max_shown maximum number of trace elements printed (controlled with -v/-verror-limit)
final Identifier getIdent();
Lazily generate identifier for template instance. This is because 75% of the ident's are never needed.
final Identifier genIdent(Objects* args);
This instance needs an identifier for name mangling purposes. Create one by taking the template declaration name and adding the type signature for it.
@safe bool definitelyValueParameter(Expression e);
Return true if e could be valid only as a template value parameter. Return false if it might be an alias or tuple. (Note that even in this case, it could still turn out to be a value).
class TemplateMixin: dmd.dtemplate.TemplateInstance;

Syntax mixin MixinTemplateName [TemplateArguments] Identifier;

struct TemplateStats;
Collect and print statistics on template instantiations.
static void incInstance(const TemplateDeclaration td, const TemplateInstance ti, bool listInstances);
Add this instance
Parameters:
TemplateDeclaration td template declaration
TemplateInstance ti instance of td
bool listInstances keep track of instances of templates
static void incUnique(const TemplateDeclaration td, const TemplateInstance ti);
Add this unique instance
void printTemplateStats(bool listInstances, ErrorSink eSink);
Print informational statistics on template instantiations.
Parameters:
bool listInstances list instances of templates
ErrorSink eSink where the print is sent