dmd.func
Defines a function declaration.
Includes:
- function/delegate literals
- function aliases
- (static/shared) constructors/destructors/post-blits
invariantunittest
License
Source: func.d
Documentation: https://dlang.org/phobos/dmd_func.html
-
Declaration
enumILS: ubyte;Inline Status
-
Declaration
uninitializednot computed yet
-
Declaration
nocannot inline
-
Declaration
yescan inline
-
-
Declaration
structEnsure;Tuple of result identifier (possibly
null) and statement. This is used to store out contracts: out(id){ ensure }-
Declaration
static Ensures*arraySyntaxCopy(Ensures*a);Do syntax copy of an array of Ensure's.
-
-
Declaration
classFuncDeclaration: dmd.declaration.Declaration;-
Declaration
Statements*frequires;in contracts
-
Declaration
Ensures*fensures;out contracts
-
Declaration
Statementfrequire;lowered in contract
-
Declaration
Statementfensure;lowered out contract
-
Declaration
Statementfbody;function body
-
Declaration
FuncDeclarationsfoverrides;functions this function overrides
-
Declaration
FuncDeclarationfdrequire;function that does the in contract
-
Declaration
FuncDeclarationfdensure;function that does the out contract
-
Declaration
Expressions*fdrequireParams;argument list for __require
-
Declaration
Expressions*fdensureParams;argument list for __ensure
-
Declaration
const(char)*mangleString;mangled symbol created from mangleExact()
-
Declaration
VarDeclarationvresult;result variable for out contracts
-
Declaration
LabelDsymbolreturnLabel;where the return goes
-
Declaration
bool[size_t]isTypeIsolatedCache;cache for the potentially very expensive isTypeIsolated check
-
Declaration
VarDeclarationvthis;'this' parameter (member and nested)
-
Declaration
VarDeclarationv_arguments;'arguments' parameter
-
Declaration
VarDeclarationv_argptr;'argptr' variable
-
Declaration
VarDeclarations*parameters;Array of VarDeclaration's for
parameters -
Declaration
DsymbolTablelabtab;statement label symbol table
-
Declaration
Dsymbolovernext;next in overload list
-
Declaration
FuncDeclarationovernext0;next in overload list (only used during IFTI)
-
Declaration
Locendloc;location of closing curly bracket
-
Declaration
intvtblIndex;for member functions, index into vtbl[]
-
Declaration
intinlineNest;!=0 if nested inline
-
Declaration
ForeachStatementfes;if foreach body, this is the foreach
-
Declaration
BaseClass*interfaceVirtual;if virtual, but only appears in base interface vtbl[]
-
Declaration
Typetintro;if !=NULL, then this is the type of the 'introducing' function this one is overriding
-
Declaration
StorageClassstorage_class2;storage class for template onemember's
-
Declaration
inthasReturnExp;1 if there's a return exp; statement 2 if there's a throw statement 4 if there's an assert(0) 8 if there's inline asm 16 if there are multiple return statements
-
Declaration
VarDeclarationnrvo_var;variable to replace with shidden
-
Declaration
Symbol*shidden;hidden pointer passed to function
-
Declaration
GotoStatements*gotos;Gotos with forward references
-
Declaration
BUILTINbuiltin;set if this is a known,
builtinfunction we can evaluate at compile time -
Declaration
inttookAddressOf;set if someone took the address of this function
-
Declaration
VarDeclarationsclosureVars;local variables in this function which are referenced by nested functions (They'll get put into the "closure" for this function.)
-
Declaration
VarDeclarationsouterVars;Outer variables which are referenced by this nested function (the inverse of closureVars)
-
Declaration
FuncDeclarationssiblingCallers;Sibling nested functions which called this one
-
Declaration
AttributeViolation*safetyViolation;In case of failed
@safeinference, store the error that made the function@systemfor better diagnostics -
Declaration
ObjcFuncDeclarationobjc;Data for a function declaration that is needed for the Objective-C integration.
-
Declaration
final boolfunctionSemantic();Resolve forward reference of function signature - parameter types, return type, and attributes.
Return Value
falseif any errors exist in the signature. -
Declaration
final boolfunctionSemantic3();Resolve forward reference of function body. Returns
falseif any errors exist in the body. -
Declaration
final boolcheckForwardRef(const ref Locloc);Check that this function type is properly resolved. If not, report "forward reference error" and return
true. -
Declaration
final voiddeclareThis(Scope*sc);Creates and returns the hidden parameters for this function declaration.
Discussion
Hidden parameters include the
thisparameter of a class, struct or nested function and the selector parameter for Objective-C methods. -
Declaration
final intoverrides(FuncDeclarationfd);Determine if 'this'
overridesfd. Return !=0 if it does. -
Declaration
final intfindVtblIndex(Dsymbols*vtbl, intdim);Find index of function in
vtbl[0..dim] that this function overrides. Prefer an exact match to a covariant one.Parameters
Dsymbols*vtblvtable to use
intdimmaximal vtable dimension
Return Value
-1 didn't find one -2 can't determine because of forward references
-
Declaration
final BaseClass*overrideInterface();If function a function in a base class, return that base class.
Return Value
base class if overriding,
nullif not -
Declaration
booloverloadInsert(Dsymbols);Overload this FuncDeclaration with the new one f. Return
trueif successful; i.e. no conflict. -
Declaration
final FuncDeclarationoverloadExactMatch(Typet);Find function in overload list that exactly matches
t. -
Declaration
final FuncDeclarationoverloadModMatch(const ref Locloc, Typetthis, ref boolhasOverloads);Find function in overload list that matches to the 'this' modifier. There's four result types.
Discussion
- If the '
tthis' matches only one candidate, it's an "exact match". Returns the function and 'hasOverloads' is set tofalse. eg. If 'tthis" is mutable and there's only one mutable method. - 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 totrue. eg. If 'tthis' is mutable, and there's both mutable and const methods, the mutable method will be a better match. - If there's two or more match candidates, but there's no better match,
Returns
nulland 'hasOverloads' is set totrueto represent "ambiguous match". eg. If 'tthis' is mutable, and there's two or more mutable methods. - If there's no candidates, it's "no match" and returns
nullwith error report. e.g. If 'tthis' is const but there's no const methods.
- If the '
-
Declaration
final TemplateDeclarationfindTemplateDeclRoot();find function template root in overload list
-
Declaration
final boolinUnittest();Returns
trueif function was declared directly or indirectly in a unittest block -
Declaration
final MATCHleastAsSpecialized(FuncDeclarationg);Determine partial specialization order of 'this' vs
g. This is very similar to TemplateDeclaration::leastAsSpecialized().Return Value
match 'this' is at least as specialized as
g0gis more specialized than 'this' -
Declaration
final LabelDsymbolsearchLabel(Identifierident, const ref Locloc= Loc.initial);Searches for a label with the given identifier. This function will insert a new
LabelDsymbolintolabtabif it does not contain a mapping for.identParameters
Identifieridentidentifier of the requested label
Locloclocation used when creating a new
LabelDsymbolReturn Value
the
LabelDsymbolforident -
Declaration
final intgetLevel(FuncDeclarationfd, intintypeof);Determine lexical level difference from
thisto nested function.fdParameters
FuncDeclarationfdtarget of call
intintypeof!=0 if inside typeof
Return Value
0 same level
0 decrease nesting by number -1 increase nesting by 1 (
is nested withinfdthis) LevelError error,thiscannot callfd -
Declaration
final intgetLevelAndCheck(const ref Locloc, Scope*sc, FuncDeclarationfd, Declarationdecl);Determine lexical level difference from
thisto nested function. Issue error iffdthiscannot call.fdParameters
Locloclocation for error messages
Scope*sccontext
FuncDeclarationfdtarget of call
DeclarationdeclThe
Declarationthat triggered this check. Used to provide a better error message only.Return Value
0 same level
0 decrease nesting by number -1 increase nesting by 1 (
is nested within 'this') LevelError errorfd -
Declaration
final const(char)*toFullSignature();for diagnostics, e.g. 'int foo(int x, int y) pure'
-
Declaration
final boolisAbstract();Override so it can work even if semantic() hasn't yet been run.
-
Declaration
final boolcanInferAttributes(Scope*sc);Decide if attributes for this function can be inferred from examining the function body.
Return Value
trueif can -
Declaration
final voidinitInferAttributes();Initialize for inferring the attributes of this function.
-
Declaration
final boolsetImpure();The function is doing something impure, so mark it as impure. If there's a purity error, return
true. -
Declaration
final boolsetUnsafe(boolgag= false, Locloc= Loc.init, const(char)*fmt= null, RootObjectarg0= null, RootObjectarg1= null, RootObjectarg2= null);The function is doing something unsafe, so mark it as unsafe.
Parameters
boolgagsurpress error message (used in escape.d)
Locloclocation of error
const(char)*fmtprintf-style format string
RootObjectarg0(optional) argument for first %s format specifier
RootObjectarg1(optional) argument for second %s format specifier
RootObjectarg2(optional) argument for third %s format specifier
Return Value
whether there's a safe error
-
Declaration
final boolsetUnsafeCall(FuncDeclarationf);The function is calling
@systemfunction, so mark it as unsafe.fParameters
FuncDeclarationffunction being called (needed for diagnostic of inferred functions)
Return Value
whether there's a safe error
-
Declaration
final boolsetGC();The function is doing something that may allocate with the GC, so mark it as not nogc (not no-how).
Return Value
trueif function is marked as @nogc, meaning a user error occurred -
Declaration
final boolisReturnIsolated();See if pointers from function parameters, mutable globals, or uplevel functions could leak into return value.
Return Value
trueif the function return value is isolated from any inputs to the function -
Declaration
final boolisTypeIsolated(Typet);
final boolisTypeIsolated(Typet, ref StringTable!TypeparentTypes);See if pointers from function parameters, mutable globals, or uplevel functions could leak into type
.tParameters
Typettype to check if it is isolated
Return Value
trueifis isolated from any inputs to the functiont -
Declaration
const boolisNested();Determine if function needs a static frame pointer.
Return Value
trueif function is really nested within other function.Contracts: If
isNested() returnstrue, isThis() should returnfalse, unless the function needs a dual-context pointer. -
Declaration
inout inout(AggregateDeclaration)isThis();Determine if function is a non-static member function that has an implicit 'this' expression.
Return Value
The aggregate it is a member of, or
null.Contracts: Both
isThis() and isNested() should returntrueif function needs a dual-context pointer, otherwise ifisThis() returnstrue, isNested() should returnfalse. -
Declaration
final const boolisUnique();Return Value
trueif there are no overloads of this function -
Declaration
final boolcheckNestedReference(Scope*sc, const ref Locloc);In the current function, we are calling 'this' function.
- Check to see if the current function can call 'this' function, issue error if not.
- If the current function is not the parent of 'this' function, then add the current function to the list of siblings of 'this' function.
- If the current function is a literal, and it's accessing an uplevel scope, then mark it as a delegate.
trueif error occurs. -
Declaration
final boolneedsClosure();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.
-
Declaration
final boolcheckClosure();Check that the function contains any closure. If it's @nogc, report suitable errors. This is mostly consistent with FuncDeclaration::needsClosure().
Return Value
trueif any errors occur. -
Declaration
final boolhasNestedFrameRefs();Determine if function's variables are referenced by a function nested within it.
-
Declaration
final voidbuildResultVar(Scope*sc, Typetret);Declare result variable lazily.
-
Declaration
final StatementmergeFrequire(Statementsf, 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.
-
Declaration
final StatementmergeFrequireInclusivePreview(Statementsf, Expressions*params);Merge into this function the 'in' contracts of all it overrides.
-
Declaration
static boolneedsFensure(FuncDeclarationfd);Determine whether an 'out' contract is declared inside the given function or any of its overrides.
Parameters
FuncDeclarationfdthe function to search
Return Value
truefound an 'out' contract -
Declaration
final voidbuildEnsureRequire();Rewrite contracts as statements.
-
Declaration
final StatementmergeFensure(Statementsf, Identifieroid, 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.
-
Declaration
final ParameterListgetParameterList();Return Value
the function's parameter list, and whether it is variadic or not.
-
Declaration
static FuncDeclarationgenCfunc(Parameters*fparams, Typetreturn, const(char)*name, StorageClassstc= 0);Generate a FuncDeclaration for a runtime library function.
-
Declaration
final boolcheckNRVO();Check all return statements for a function to verify that returning using NRVO is possible.
Return Value
falseif the result cannot be returned by hidden reference.
-
-
Declaration
ExpressionaddInvariant(AggregateDeclarationad, VarDeclarationvthis);Generate Expression to call the invariant.
Input:
adaggregate with the invariantvthisvariable with 'this'Return Value
void expression that calls the invariant
-
Declaration
intoverloadApply(Dsymbolfstart, 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, ordg(s) returns nonzero.Parameters
Dsymbolfstartsymbol to start from
int delegate(Dsymbol)dgthe delegate to be called on the overload
Scope*sccontext used to check if symbol is accessible (and therefore visible), can be
nullReturn Value
==0 continue !=0 done (and the return value from the last
dg() call) -
Declaration
autoMODMatchToBuffer(OutBuffer*buf, ubytelhsMod, ubyterhsMod);Checks for mismatching modifiers between
andlhsModand prints the mismatching modifiers torhsMod.bufDiscussion
The modifiers of the
mismatching the ones with thelhsModare printed, i.e. lhs(shared) vs. rhs() prints "rhsModshared", wheras lhs() vs rhs(shared) prints "non-shared".Parameters
OutBuffer*bufoutput buffer to write to
ubytelhsModmodifier on the left-hand side
ubytelhsModmodifier on the right-hand side
Return Value
A tuple with
isMutableandisNotSharedset if theis missing those modifiers (compared to rhs).lhsModExamples
OutBuffer buf; auto mismatches = MODMatchToBuffer(&buf, MODFlags.shared_, 0); assert(buf[] == "`shared` "); assert(!mismatches.isNotShared); buf.setsize(0); mismatches = MODMatchToBuffer(&buf, 0, MODFlags.shared_); assert(buf[] == "non-shared "); assert(mismatches.isNotShared); buf.setsize(0); mismatches = MODMatchToBuffer(&buf, MODFlags.const_, 0); assert(buf[] == "`const` "); assert(!mismatches.isMutable); buf.setsize(0); mismatches = MODMatchToBuffer(&buf, 0, MODFlags.const_); assert(buf[] == "mutable "); assert(mismatches.isMutable);
-
Declaration
enumFuncResolveFlag: ubyte;Flag used by .
-
Declaration
standardissue error messages, solve the call.
-
Declaration
quietdo not issue error message on no match, just return
null. -
Declaration
overloadOnlyonly resolve overloads, i.e. do not issue error on ambiguous
-
-
Declaration
FuncDeclarationresolveFuncCall(const ref Locloc, Scope*sc, Dsymbols, Objects*tiargs, Typetthis, Expressions*fargs, FuncResolveFlagflags);Given a symbol that could be either a FuncDeclaration or a function template, resolve it to a function symbol.
Parameters
Loclocinstantiation location
Scope*scinstantiation scope
Dsymbolsinstantiation symbol
Objects*tiargsinitial list of template arguments
Typetthisif !NULL, the
thisargument typeExpressions*fargsarguments to function
FuncResolveFlagflagssee .
Return Value
if match is found, then function symbol, else
null -
Declaration
TypegetIndirection(Typet);Returns an indirect type one step from
t. -
Declaration
classFuncAliasDeclaration: dmd.func.FuncDeclaration;Used as a way to import a set of functions from another scope into this one.
-
Declaration
classFuncLiteralDeclaration: dmd.func.FuncDeclaration;-
Declaration
voidmodifyReturns(Scope*sc, Typetret);Modify all expression type of return statements to
tret.Discussion
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.
-
-
Declaration
classCtorDeclaration: dmd.func.FuncDeclaration; -
Declaration
classPostBlitDeclaration: dmd.func.FuncDeclaration; -
Declaration
classDtorDeclaration: dmd.func.FuncDeclaration; -
Declaration
classStaticCtorDeclaration: dmd.func.FuncDeclaration; -
Declaration
classSharedStaticCtorDeclaration: dmd.func.StaticCtorDeclaration; -
Declaration
classStaticDtorDeclaration: dmd.func.FuncDeclaration; -
Declaration
classSharedStaticDtorDeclaration: dmd.func.StaticDtorDeclaration; -
Declaration
classInvariantDeclaration: dmd.func.FuncDeclaration; -
Declaration
classUnitTestDeclaration: dmd.func.FuncDeclaration; -
Declaration
classNewDeclaration: dmd.func.FuncDeclaration; -
Declaration
boolsetUnsafe(Scope*sc, boolgag= false, Locloc= Loc.init, const(char)*fmt= null, RootObjectarg0= null, RootObjectarg1= null, RootObjectarg2= null);A statement / expression in this scope is not
@safe, so mark the enclosing function as@systemParameters
Scope*scscope that the unsafe statement / expression is in
boolgagsurpress error message (used in escape.d)
Locloclocation of error
const(char)*fmtprintf-style format string
RootObjectarg0(optional) argument for first %s format specifier
RootObjectarg1(optional) argument for second %s format specifier
RootObjectarg2(optional) argument for third %s format specifier
Return Value
whether there's a safe error
-
Declaration
boolsetUnsafePreview(Scope*sc, FeatureStatefs, boolgag, Locloc, const(char)*msg, RootObjectarg0= null, RootObjectarg1= null, RootObjectarg2= null);Like
setUnsafe, but for safety errors still behind preview switchesDiscussion
Given a
FeatureState, for example dip1000 / dip25 / systemVariables, the behavior changes based on the setting:fs- In case of
-revert=, it does nothing.fs - In case of
-preview=, it's the same asfssetUnsafe - By default, print a deprecation in
@safefunctions, or store an attribute violation in inferred functions.
Parameters
Scope*scused to find affected function/variable, and for checking whether we are in a deprecated / speculative scope
FeatureStatefsfeature state from the preview flag
boolgagsurpress error message
Locloclocation of error
const(char)*msgprintf-style format string
RootObjectarg0(optional) argument for first %s format specifier
RootObjectarg1(optional) argument for second %s format specifier
RootObjectarg2(optional) argument for third %s format specifier
Return Value
whether an actual safe error (not deprecation) occured
- In case of
-
Declaration
structAttributeViolation;Stores a reason why a function failed to infer a function attribute like
@safeorpureDiscussion
Has two modes:
- a regular safety error, stored in (fmtStr, arg0, arg1)
- a call to a function without the attribute, which is a special case, because in that case,
. This way, in case of a big call stack, the error can go down all the way to the root cause. TheAttributeViolationFunctionDeclarationis then stored inarg0andfmtStrmust benull.-
Declaration
Locloc;location of error
-
Declaration
const(char)*fmtStr;printf-style format string
-
Declaration
RootObjectarg0;
RootObjectarg1;
RootObjectarg2;Arguments for up to two
%sformat specifiers in format string
-
Declaration
voiderrorSupplementalInferredSafety(FuncDeclarationfd, intmaxDepth, booldeprecation);Print the reason why
was inferredfd@systemas a supplemental errorParameters
FuncDeclarationfdfunction to check
intmaxDepthup to how many functions deep to report errors
booldeprecationprint deprecations instead of errors