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

Does semantic analysis for functions.

Specification Functions

Authors:

Source funcsem.d

bool onlyOneMain(FuncDeclaration fd);
Only one entry point function is allowed. Print error if more than one.
Parameters:
FuncDeclaration fd a "main" function
Returns:
true if haven't seen "main" before
bool isUnique(const FuncDeclaration fd);
Returns:
true if there are no overloads of this function
void funcDeclarationSemantic(Scope* sc, FuncDeclaration funcdecl);
Main semantic routine for functions.
bool functionSemantic(FuncDeclaration fd);
Resolve forward reference of function signature - parameter types, return type, and attributes.
Parameters:
FuncDeclaration fd function declaration
Returns:
false if any errors exist in the signature.
bool functionSemantic3(FuncDeclaration fd);
Resolve forward reference of function body. Returns false if any errors exist in the body.
void declareThis(FuncDeclaration fd, Scope* sc);
Creates and returns the hidden parameters for this function declaration.
Hidden parameters include the this parameter of a class, struct or nested function and the selector parameter for Objective-C methods.
bool checkForwardRef(FuncDeclaration fd, Loc loc);
Check that this function type is properly resolved. If not, report "forward reference error" and return true.
int findVtblIndex(FuncDeclaration fd, Dsymbol[] vtbl);
Find index of function in vtbl[0..length] that this function overrides. Prefer an exact match to a covariant one.
Parameters:
FuncDeclaration fd function
Dsymbol[] vtbl vtable to use
Returns:
-1 didn't find one -2 can't determine because of forward references
BaseClass* overrideInterface(FuncDeclaration fd);
If function is a function in a base class, return that base class.
Parameters:
FuncDeclaration fd function
Returns:
base class if overriding, null if not
enum FuncResolveFlag: ubyte;
Flag used by resolveFuncCall.
standard
issue error messages, solve the call.
quiet
do not issue error message on no match, just return null.
overloadOnly
only resolve overloads, i.e. do not issue error on ambiguous
ufcs
matches and need explicit this.
trying to resolve UFCS call
FuncDeclaration resolveFuncCall(Loc loc, Scope* sc, Dsymbol s, Objects* tiargs, Type tthis, ArgumentList argumentList, FuncResolveFlag flags);
Given a symbol that could be either a FuncDeclaration or a function template, resolve it to a function symbol.
Parameters:
Loc loc instantiation location
Scope* sc instantiation scope
Dsymbol s instantiation symbol
Objects* tiargs initial list of template arguments
Type tthis if !NULL, the this argument type
ArgumentList argumentList arguments to function
FuncResolveFlag flags see FuncResolveFlag.
Returns:
if match is found, then function symbol, else null
Expression addInvariant(AggregateDeclaration ad, VarDeclaration vthis);
Generate Expression to call the invariant.

Input ad aggregate with the invariant vthis variable with 'this'

Returns:
void expression that calls the invariant
FuncDeclaration overloadExactMatch(FuncDeclaration thisfd, Type t);
Find function in overload list that exactly matches t.
int overrides(FuncDeclaration fd1, FuncDeclaration fd2);
Determine if fd1 overrides fd2. Return !=0 if it does.
MATCH leastAsSpecialized(FuncDeclaration f, FuncDeclaration g, ArgumentLabels* names);
Determine partial specialization order of functions f vs g. This is very similar to TemplateDeclaration::leastAsSpecialized().
Parameters:
FuncDeclaration f first function
FuncDeclaration g second function
ArgumentLabels* names argument Labels of parameters(name and location of the name)
Returns:
match 'this' is at least as specialized as g 0 g is more specialized than 'this'
FuncDeclaration overloadModMatch(FuncDeclaration thisfd, Loc loc, Type tthis, ref bool hasOverloads);
Find function in overload list that matches to the 'this' modifier. There's four result types.
  1. If the 'tthis' matches only one candidate, it's an "exact match". Returns the function and 'hasOverloads' is set to false. eg. If 'tthis" is mutable and there's only one mutable method.
  2. If there's two or more match candidates, but a candidate function will be a "better match". Returns the better match function but 'hasOverloads' is set to true. eg. If 'tthis' is mutable, and there's both mutable and const methods, the mutable method will be a better match.
  3. If there's two or more match candidates, but there's no better match, Returns null and 'hasOverloads' is set to true to represent "ambiguous match". eg. If 'tthis' is mutable, and there's two or more mutable methods.
  4. If there's no candidates, it's "no match" and returns null with error report. e.g. If 'tthis' is const but there's no const methods.
int getLevelAndCheck(FuncDeclaration fd, Loc loc, Scope* sc, FuncDeclaration target, Declaration decl);
Determine lexical level difference from fd to nested function target. Issue error if fd cannot call target.
Parameters:
FuncDeclaration fd function
Loc loc location for error messages
Scope* sc context
FuncDeclaration target target of call
Declaration decl The Declaration that triggered this check. Used to provide a better error message only.
Returns:
0 same level

0 decrease nesting by number -1 increase nesting by 1 (target is nested within 'fd') LevelError error

void buildResultVar(FuncDeclaration fd, Scope* sc, Type tret);
Declare result variable lazily.
Statement mergeFrequire(FuncDeclaration fd, Statement sf, Expressions* params);
Merge into this function the 'in' contracts of all it overrides. 'in's are OR'd together, i.e. only one of them needs to pass.
Statement mergeFrequireInclusivePreview(FuncDeclaration fd, Statement sf, Expressions* params);
Merge into this function the 'in' contracts of all it overrides.
void buildEnsureRequire(FuncDeclaration thisfd);
Rewrite contracts as statements.
@safe bool needsFensure(FuncDeclaration fd);
Determine whether an 'out' contract is declared inside the given function or any of its overrides.
Parameters:
FuncDeclaration fd the function to search
Returns:
true found an 'out' contract
Statement mergeFensure(FuncDeclaration fd, Statement sf, Identifier oid, Expressions* params);
Merge into this function the 'out' contracts of all it overrides. 'out's are AND'd together, i.e. all of them need to pass.
void modifyReturns(FuncLiteralDeclaration fld, Scope* sc, Type tret);
Modify all expression type of return statements to tret.
On function literals, return type may be modified based on the context type after its semantic3 is done, in FuncExp::implicitCastTo.
A function() dg = (){ return new B(); } // OK if is(B : A) == true
If B to A conversion is convariant that requires offseet adjusting, all return statements should be adjusted to return expressions typed A.
@safe bool isRootTraitsCompilesScope(Scope* sc);
When a traits(compiles) is used on a function literal call we need to take into account if the body of the function violates any attributes, however, we must not affect the attribute inference on the outer function. The attributes of the function literal still need to be inferred, therefore we need a way to check for the scope that the traits compiles introduces.
Parameters:
Scope* sc scope to be checked for
Returns:
true if the provided scope is the root of the traits compiles list of scopes.
int getLevel(FuncDeclaration fd1, FuncDeclaration fd2, int intypeof);
Determine lexical level difference from fd1 to nested function fd2.
Parameters:
FuncDeclaration fd1 function
FuncDeclaration fd2 target of call
int intypeof !=0 if inside typeof
Returns:
0 same level

0 decrease nesting by number -1 increase nesting by 1 (fd2 is nested within fd1) LevelError error, this cannot call fd2

bool checkEscapingSiblings(FuncDeclaration f, FuncDeclaration outerFunc, void* p = null);
Given a nested function f inside a function outerFunc, check if any sibling callers of f have escaped. If so, mark all the enclosing functions as needing closures. This is recursive: we need to check the callers of our siblings. Note that nested functions can only call lexically earlier nested functions, so loops are impossible.
Parameters:
FuncDeclaration f inner function (nested within outerFunc)
FuncDeclaration outerFunc outer function
void* p for internal recursion use
Returns:
true if any closures were needed
bool needsClosure(FuncDeclaration fd);
Look at all the variables in this function that are referenced by nested functions, and determine if a closure needs to be created for them.
bool checkNRVO(FuncDeclaration fd);
Check all return statements for a function to verify that returning using NRVO is possible.
Returns:
false if the result cannot be returned by hidden reference.
bool setImpure(FuncDeclaration fd, Loc loc, const(char)* fmt, RootObject[] args...);
The function is doing something impure, so mark it as impure.
Parameters:
FuncDeclaration fd function declaration to mark
Loc loc location of impure action
const(char)* fmt format string for error message
RootObject[] args argument to format string
Returns:
true if there's a purity error
bool isReturnIsolated(FuncDeclaration fd);
See if pointers from function parameters, mutable globals, or uplevel functions could leak into return value.
Returns:
true if the function return value is isolated from any inputs to the function
bool isTypeIsolated(FuncDeclaration fd, Type t);

bool isTypeIsolated(FuncDeclaration fd, Type t, ref StringTable!Type parentTypes);
See if pointers from function parameters, mutable globals, or uplevel functions could leak into type t.
Parameters:
Type t type to check if it is isolated
Returns:
true if t is isolated from any inputs to the function
int overloadApply(Dsymbol fstart, scope int delegate(Dsymbol) dg, Scope* sc = null);
Visit each overloaded function/template in turn, and call dg(s) on it. Exit when no more, or dg(s) returns nonzero.
Parameters:
Dsymbol fstart symbol to start from
int delegate(Dsymbol) dg the delegate to be called on the overload
Scope* sc context used to check if symbol is accessible (and therefore visible), can be null
Returns:
==0 continue !=0 done (and the return value from the last dg() call)
bool checkNestedReference(VarDeclaration vd, Scope* sc, Loc loc);
Check to see if this variable is actually in an enclosing function rather than the current one. Update nestedrefs[], closureVars[] and outerVars[].
Returns:
true if error occurs.
bool arrayBoundsCheck(FuncDeclaration fd);
Check to see if array bounds checking code has to be generated
Parameters:
FuncDeclaration fd function for which code is to be generated
Returns:
true if do array bounds checking for the given function