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

Template semantics.
Authors:
struct TemplateInstanceBox;
This struct is needed for TemplateInstance to be the key in an associative array. Fixing https://issues.dlang.org/show_bug.cgi?id=15813 would make it unnecessary.
void templateDeclarationSemantic(Scope* sc, TemplateDeclaration tempdecl);
Perform semantic analysis on template.
Parameters:
Scope* sc context
TemplateDeclaration tempdecl template declaration
bool findTempDecl(TemplateInstance ti, Scope* sc, WithScopeSymbol* pwithsym);
Find template declaration corresponding to template instance.
Returns:
false if finding fails.

Note This function is reentrant against error occurrence. If returns false, any members of this object won't be modified, and repetition call will reproduce same error.

MATCH matchArg(TemplateParameter tp, Loc instLoc, Scope* sc, Objects* tiargs, size_t i, TemplateParameters* parameters, ref Objects dedtypes, Declaration* psparam);
Match to a particular TemplateParameter.

Input instLoc location that the template is instantiated. tiargs[] actual arguments to template instance i i'th argument parameters[] template parameters dedtypes[] deduced arguments to template instance *psparam set to symbol declared and initialized to dedtypes[i]

bool updateTempDecl(TemplateInstance ti, Scope* sc, Dsymbol s);
Confirm s is a valid template, then store it.

Input sc s candidate symbol of template. It may be: TemplateDeclaration FuncDeclaration with findTemplateDeclRoot() != NULL OverloadSet which contains candidates

Returns:
true if updating succeeds.
MATCH matchWithInstance(Scope* sc, TemplateDeclaration td, TemplateInstance ti, ref Objects dedtypes, ArgumentList argumentList, int flag);
Given that ti is an instance of this TemplateDeclaration, deduce the types of the parameters to this, and store those deduced types in dedtypes[].
Parameters:
Scope* sc context
TemplateDeclaration td template
TemplateInstance ti instance of td
Objects dedtypes fill in with deduced types
ArgumentList argumentList arguments to template instance
int flag 1 - don't do semantic() because of dummy types 2 - don't change types in matchArg()
Returns:
match level.
bool isDiscardable(TemplateInstance ti);
Returns:
true if the instances' innards are discardable.
The idea of this function is to see if the template instantiation can be 100% replaced with its eponymous member. All other members can be discarded, even in the compiler to free memory (for example, the template could be expanded in a region allocator, deemed trivial, the end result copied back out independently and the entire region freed), and can be elided entirely from the binary.
The current implementation affects code that generally looks like:
template foo(args...) {
    some_basic_type_or_string helper() { .... }
    enum foo = helper();
}
since it was the easiest starting point of implementation but it can and should be expanded more later.
bool needsCodegen(TemplateInstance ti);
Returns true if this is not instantiated in non-root module, and is a part of non-speculative instantiatiation.

Note minst does not stabilize until semantic analysis is completed, so don't call this function during semantic analysis to return precise result.

const(char)* getConstraintEvalError(TemplateDeclaration td, ref const(char)* tip);
Destructively get the error message from the last constraint evaluation
Parameters:
TemplateDeclaration td TemplateDeclaration
const(char)* tip tip to show after printing all overloads
bool findBestMatch(TemplateInstance ti, Scope* sc, ArgumentList argumentList);
Find the TemplateDeclaration that matches this TemplateInstance best.
Parameters:
TemplateInstance ti TemplateInstance
Scope* sc the scope this TemplateInstance resides in
ArgumentList argumentList function arguments in case of a template function
Returns:
true if a match was found, false otherwise
MATCH leastAsSpecialized(Scope* sc, TemplateDeclaration td, TemplateDeclaration td2, ArgumentList argumentList);
Determine partial specialization order of td vs td2.
Parameters:
Scope* sc context
TemplateDeclaration td first template
TemplateDeclaration td2 second template
ArgumentList argumentList arguments to template
Returns:
MATCH - td is at least as specialized as td2 MATCH.nomatch - td2 is more specialized than td
void declareParameters(TemplateInstance ti, Scope* sc);
Declare parameters of template instance, initialize them with the template instance arguments.
bool semanticTiargs(TemplateInstance ti, Scope* sc);
Run semantic on the elements of ti.tiargs.

Input ti = template instance whose tiargs should have semantic done sc = scope

Returns:
false if one or more arguments have errors.

Note This function is reentrant against error occurrence. If returns false, all elements of tiargs won't be modified.

bool TemplateInstance_semanticTiargs(Loc loc, Scope* sc, Objects* tiargs, int flags, TupleDeclaration atd = null);
Run semantic of tiargs as arguments of template.

Input loc sc tiargs array of template arguments flags 1: replace const variables with their initializers 2: don't devolve Parameter to Type atd tuple being optimized. If found, it's not expanded here but in AliasAssign semantic.

Returns:
false if one or more arguments have errors.
void functionResolve(ref MatchAccumulator m, Dsymbol dstart, Loc loc, Scope* sc, Objects* tiargs, Type tthis, ArgumentList argumentList, void delegate(const(char)*) scope errorHelper = null);
Given function arguments, figure out which template function to expand, and return matching result.
Parameters:
MatchAccumulator m matching result
Dsymbol dstart the root of overloaded function templates
Loc loc instantiation location
Scope* sc instantiation scope
Objects* tiargs initial list of template arguments
Type tthis if !NULL, the 'this' pointer argument
ArgumentList argumentList arguments to function
void delegate(const(char)*) scope errorHelper delegate to send error message to if not null