| 1 |
/** |
| 2 |
* Defines a function declaration. |
| 3 |
* |
| 4 |
* Includes: |
| 5 |
* - function/delegate literals |
| 6 |
* - function aliases |
| 7 |
* - (static/shared) constructors/destructors/post-blits |
| 8 |
* - `invariant` |
| 9 |
* - `unittest` |
| 10 |
* |
| 11 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 12 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 13 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 14 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/func.d, _func.d) |
| 15 |
* Documentation: https://dlang.org/phobos/dmd_func.html |
| 16 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/func.d |
| 17 |
*/ |
| 18 |
|
| 19 |
module dmd.func; |
| 20 |
|
| 21 |
import core.stdc.stdio; |
| 22 |
import core.stdc.string; |
| 23 |
import dmd.aggregate; |
| 24 |
import dmd.arraytypes; |
| 25 |
import dmd.astenums; |
| 26 |
import dmd.blockexit; |
| 27 |
import dmd.gluelayer; |
| 28 |
import dmd.dcast; |
| 29 |
import dmd.dclass; |
| 30 |
import dmd.declaration; |
| 31 |
import dmd.delegatize; |
| 32 |
import dmd.dmodule; |
| 33 |
import dmd.dscope; |
| 34 |
import dmd.dstruct; |
| 35 |
import dmd.dsymbol; |
| 36 |
import dmd.dtemplate; |
| 37 |
import dmd.escape; |
| 38 |
import dmd.expression; |
| 39 |
import dmd.funcsem : overloadApply; |
| 40 |
import dmd.globals; |
| 41 |
import dmd.hdrgen; |
| 42 |
import dmd.id; |
| 43 |
import dmd.identifier; |
| 44 |
import dmd.init; |
| 45 |
import dmd.location; |
| 46 |
import dmd.mtype; |
| 47 |
import dmd.objc; |
| 48 |
import dmd.common.outbuffer; |
| 49 |
import dmd.rootobject; |
| 50 |
import dmd.root.string; |
| 51 |
import dmd.root.stringtable; |
| 52 |
import dmd.statement; |
| 53 |
import dmd.tokens; |
| 54 |
import dmd.visitor; |
| 55 |
|
| 56 |
version (IN_GCC) {} |
| 57 |
else version (IN_LLVM) {} |
| 58 |
else version = MARS; |
| 59 |
|
| 60 |
/// Inline Status |
| 61 |
enum ILS : ubyte |
| 62 |
{ |
| 63 |
uninitialized, /// not computed yet |
| 64 |
no, /// cannot inline |
| 65 |
yes, /// can inline |
| 66 |
} |
| 67 |
|
| 68 |
enum BUILTIN : ubyte |
| 69 |
{ |
| 70 |
unknown = 255, /// not known if this is a builtin |
| 71 |
unimp = 0, /// this is not a builtin |
| 72 |
gcc, /// this is a GCC builtin |
| 73 |
llvm, /// this is an LLVM builtin |
| 74 |
sin, |
| 75 |
cos, |
| 76 |
tan, |
| 77 |
sqrt, |
| 78 |
fabs, |
| 79 |
ldexp, |
| 80 |
log, |
| 81 |
log2, |
| 82 |
log10, |
| 83 |
exp, |
| 84 |
expm1, |
| 85 |
exp2, |
| 86 |
round, |
| 87 |
floor, |
| 88 |
ceil, |
| 89 |
trunc, |
| 90 |
copysign, |
| 91 |
pow, |
| 92 |
fmin, |
| 93 |
fmax, |
| 94 |
fma, |
| 95 |
isnan, |
| 96 |
isinfinity, |
| 97 |
isfinite, |
| 98 |
bsf, |
| 99 |
bsr, |
| 100 |
bswap, |
| 101 |
popcnt, |
| 102 |
yl2x, |
| 103 |
yl2xp1, |
| 104 |
toPrecFloat, |
| 105 |
toPrecDouble, |
| 106 |
toPrecReal, |
| 107 |
ctfeWrite, |
| 108 |
} |
| 109 |
|
| 110 |
private struct FUNCFLAG |
| 111 |
{ |
| 112 |
bool purityInprocess; /// working on determining purity |
| 113 |
bool safetyInprocess; /// working on determining safety |
| 114 |
bool nothrowInprocess; /// working on determining nothrow |
| 115 |
bool nogcInprocess; /// working on determining @nogc |
| 116 |
bool saferD; /// do -preview=safer checks if this function has default safety |
| 117 |
bool scopeInprocess; /// infer `return` and `scope` for parameters |
| 118 |
bool inlineScanned; /// function has been scanned for inline possibilities |
| 119 |
bool hasCatches; /// function has try-catch statements |
| 120 |
bool skipCodegen; /// do not generate code for this function. |
| 121 |
bool printf; /// is a printf-like function |
| 122 |
|
| 123 |
bool scanf; /// is a scanf-like function |
| 124 |
bool noreturn; /// the function does not return |
| 125 |
bool isNRVO = true; /// Support for named return value optimization |
| 126 |
bool isNaked; /// The function is 'naked' (see inline ASM) |
| 127 |
bool isGenerated; /// The function is compiler generated (e.g. `opCmp`) |
| 128 |
bool isIntroducing; /// If this function introduces the overload set |
| 129 |
bool hasSemantic3Errors; /// If errors in semantic3 this function's frame ptr |
| 130 |
bool hasNoEH; /// No exception unwinding is needed |
| 131 |
bool inferRetType; /// Return type is to be inferred |
| 132 |
bool hasDualContext; /// has a dual-context 'this' parameter |
| 133 |
|
| 134 |
bool hasAlwaysInlines; /// Contains references to functions that must be inlined |
| 135 |
bool isCrtCtor; /// Has attribute pragma(crt_constructor) |
| 136 |
bool isCrtDtor; /// Has attribute pragma(crt_destructor) |
| 137 |
bool hasEscapingSiblings;/// Has sibling functions that escape |
| 138 |
bool computedEscapingSiblings; /// `hasEscapingSiblings` has been computed |
| 139 |
bool dllImport; /// __declspec(dllimport) |
| 140 |
bool dllExport; /// __declspec(dllexport) |
| 141 |
|
| 142 |
bool hasReturnExp; /// Has return exp; statement |
| 143 |
bool hasInlineAsm; /// Has asm{} statement |
| 144 |
bool hasMultipleReturnExp; /// Has multiple return exp; statements |
| 145 |
} |
| 146 |
|
| 147 |
/*********************************************************** |
| 148 |
* Tuple of result identifier (possibly null) and statement. |
| 149 |
* This is used to store out contracts: out(id){ ensure } |
| 150 |
*/ |
| 151 |
extern (C++) struct Ensure |
| 152 |
{ |
| 153 |
Identifier id; |
| 154 |
Statement ensure; |
| 155 |
|
| 156 |
Ensure syntaxCopy() |
| 157 |
{ |
| 158 |
return Ensure(id, ensure.syntaxCopy()); |
| 159 |
} |
| 160 |
|
| 161 |
/***************************************** |
| 162 |
* Do syntax copy of an array of Ensure's. |
| 163 |
*/ |
| 164 |
static Ensures* arraySyntaxCopy(Ensures* a) |
| 165 |
{ |
| 166 |
if (!a) |
| 167 |
return null; |
| 168 |
|
| 169 |
Ensures* b = a.copy(); |
| 170 |
foreach (i, e; *a) |
| 171 |
(*b)[i] = e.syntaxCopy(); |
| 172 |
return b; |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
/*********************************************************** |
| 178 |
* Most functions don't have contracts, so save memory by grouping |
| 179 |
* this information into a separate struct |
| 180 |
*/ |
| 181 |
private struct ContractInfo |
| 182 |
{ |
| 183 |
Statements* frequires; /// in contracts |
| 184 |
Ensures* fensures; /// out contracts |
| 185 |
Statement frequire; /// lowered in contract |
| 186 |
Statement fensure; /// lowered out contract |
| 187 |
FuncDeclaration fdrequire; /// function that does the in contract |
| 188 |
FuncDeclaration fdensure; /// function that does the out contract |
| 189 |
Expressions* fdrequireParams; /// argument list for __require |
| 190 |
Expressions* fdensureParams; /// argument list for __ensure |
| 191 |
} |
| 192 |
|
| 193 |
/*********************************************************** |
| 194 |
*/ |
| 195 |
extern (C++) class FuncDeclaration : Declaration |
| 196 |
{ |
| 197 |
Statement fbody; /// function body |
| 198 |
|
| 199 |
FuncDeclarations foverrides; /// functions this function overrides |
| 200 |
|
| 201 |
private ContractInfo* contracts; /// contract information |
| 202 |
|
| 203 |
const(char)* mangleString; /// mangled symbol created from mangleExact() |
| 204 |
|
| 205 |
VarDeclaration vresult; /// result variable for out contracts |
| 206 |
LabelDsymbol returnLabel; /// where the return goes |
| 207 |
|
| 208 |
bool[size_t] isTypeIsolatedCache; /// cache for the potentially very expensive isTypeIsolated check |
| 209 |
|
| 210 |
// used to prevent symbols in different |
| 211 |
// scopes from having the same name |
| 212 |
DsymbolTable localsymtab; |
| 213 |
VarDeclaration vthis; /// 'this' parameter (member and nested) |
| 214 |
VarDeclaration v_arguments; /// '_arguments' parameter |
| 215 |
|
| 216 |
VarDeclaration v_argptr; /// '_argptr' variable |
| 217 |
VarDeclarations* parameters; /// Array of VarDeclaration's for parameters |
| 218 |
DsymbolTable labtab; /// statement label symbol table |
| 219 |
Dsymbol overnext; /// next in overload list |
| 220 |
FuncDeclaration overnext0; /// next in overload list (only used during IFTI) |
| 221 |
Loc endloc; /// location of closing curly bracket |
| 222 |
int vtblIndex = -1; /// for member functions, index into vtbl[] |
| 223 |
|
| 224 |
ILS inlineStatusStmt = ILS.uninitialized; |
| 225 |
ILS inlineStatusExp = ILS.uninitialized; |
| 226 |
PINLINE inlining = PINLINE.default_; |
| 227 |
|
| 228 |
int inlineNest; /// !=0 if nested inline |
| 229 |
|
| 230 |
ForeachStatement fes; /// if foreach body, this is the foreach |
| 231 |
BaseClass* interfaceVirtual; /// if virtual, but only appears in base interface vtbl[] |
| 232 |
/** if !=NULL, then this is the type |
| 233 |
of the 'introducing' function |
| 234 |
this one is overriding |
| 235 |
*/ |
| 236 |
Type tintro; |
| 237 |
|
| 238 |
STC storage_class2; /// storage class for template onemember's |
| 239 |
|
| 240 |
// Things that should really go into Scope |
| 241 |
|
| 242 |
VarDeclaration nrvo_var; /// variable to replace with shidden |
| 243 |
Symbol* shidden; /// hidden pointer passed to function |
| 244 |
|
| 245 |
ReturnStatements* returns; |
| 246 |
|
| 247 |
GotoStatements* gotos; /// Gotos with forward references |
| 248 |
|
| 249 |
version (MARS) |
| 250 |
{ |
| 251 |
VarDeclarations* alignSectionVars; /// local variables with alignment needs larger than stackAlign |
| 252 |
Symbol* salignSection; /// pointer to aligned section, if any |
| 253 |
} |
| 254 |
|
| 255 |
/// set if this is a known, builtin function we can evaluate at compile time |
| 256 |
BUILTIN builtin = BUILTIN.unknown; |
| 257 |
|
| 258 |
/// set if someone took the address of this function |
| 259 |
int tookAddressOf; |
| 260 |
|
| 261 |
bool requiresClosure; // this function needs a closure |
| 262 |
|
| 263 |
/** local variables in this function which are referenced by nested functions |
| 264 |
* (They'll get put into the "closure" for this function.) |
| 265 |
*/ |
| 266 |
VarDeclarations closureVars; |
| 267 |
|
| 268 |
/** Outer variables which are referenced by this nested function |
| 269 |
* (the inverse of closureVars) |
| 270 |
*/ |
| 271 |
VarDeclarations outerVars; |
| 272 |
|
| 273 |
// Most recent encountered `main` (`WinMain` or `DllMain`) function. |
| 274 |
// Track it to give error messages for multiple entrypoints |
| 275 |
__gshared FuncDeclaration lastMain; |
| 276 |
|
| 277 |
/// Sibling nested functions which called this one |
| 278 |
FuncDeclarations siblingCallers; |
| 279 |
|
| 280 |
FuncDeclarations* inlinedNestedCallees; |
| 281 |
|
| 282 |
/// In case of failed `@safe` inference, store the error that made the function `@system` for |
| 283 |
/// better diagnostics |
| 284 |
AttributeViolation* safetyViolation; |
| 285 |
AttributeViolation* nogcViolation; |
| 286 |
AttributeViolation* pureViolation; |
| 287 |
AttributeViolation* nothrowViolation; |
| 288 |
|
| 289 |
/// See the `FUNCFLAG` struct |
| 290 |
import dmd.common.bitfields; |
| 291 |
mixin(generateBitFields!(FUNCFLAG, uint)); |
| 292 |
|
| 293 |
/** |
| 294 |
* Data for a function declaration that is needed for the Objective-C |
| 295 |
* integration. |
| 296 |
*/ |
| 297 |
ObjcFuncDeclaration objc; |
| 298 |
|
| 299 |
extern (D) this(Loc loc, Loc endloc, Identifier ident, STC storage_class, Type type, bool noreturn = false) |
| 300 |
{ |
| 301 |
super(DSYM.funcDeclaration, loc, ident); |
| 302 |
//.printf("FuncDeclaration(id = '%s', type = %s)\n", ident.toChars(), type.toChars()); |
| 303 |
//.printf("storage_class = x%llx\n", storage_class); |
| 304 |
this.storage_class = storage_class; |
| 305 |
this.type = type; |
| 306 |
if (type) |
| 307 |
{ |
| 308 |
// Normalize storage_class, because function-type related attributes |
| 309 |
// are already set in the 'type' in parsing phase. |
| 310 |
this.storage_class &= ~(STC.TYPECTOR | STC.FUNCATTR); |
| 311 |
} |
| 312 |
this.endloc = endloc; |
| 313 |
if (noreturn) |
| 314 |
this.noreturn = true; |
| 315 |
|
| 316 |
/* The type given for "infer the return type" is a TypeFunction with |
| 317 |
* NULL for the return type. |
| 318 |
*/ |
| 319 |
if (type && type.nextOf() is null) |
| 320 |
this.inferRetType = true; |
| 321 |
} |
| 322 |
|
| 323 |
static FuncDeclaration create(Loc loc, Loc endloc, Identifier id, StorageClass storage_class, Type type, bool noreturn = false) |
| 324 |
{ |
| 325 |
return new FuncDeclaration(loc, endloc, id, cast(STC) storage_class, type, noreturn); |
| 326 |
} |
| 327 |
|
| 328 |
final nothrow pure @safe |
| 329 |
{ |
| 330 |
private ref ContractInfo getContracts() |
| 331 |
{ |
| 332 |
if (!contracts) |
| 333 |
contracts = new ContractInfo(); |
| 334 |
return *contracts; |
| 335 |
} |
| 336 |
|
| 337 |
// getters |
| 338 |
inout(Statements*) frequires() inout { return contracts ? contracts.frequires : null; } |
| 339 |
inout(Ensures*) fensures() inout { return contracts ? contracts.fensures : null; } |
| 340 |
inout(Statement) frequire() inout { return contracts ? contracts.frequire: null; } |
| 341 |
inout(Statement) fensure() inout { return contracts ? contracts.fensure : null; } |
| 342 |
inout(FuncDeclaration) fdrequire() inout { return contracts ? contracts.fdrequire : null; } |
| 343 |
inout(FuncDeclaration) fdensure() inout { return contracts ? contracts.fdensure: null; } |
| 344 |
inout(Expressions*) fdrequireParams() inout { return contracts ? contracts.fdrequireParams: null; } |
| 345 |
inout(Expressions*) fdensureParams() inout { return contracts ? contracts.fdensureParams: null; } |
| 346 |
|
| 347 |
extern (D) private static string generateContractSetter(string field, string type) |
| 348 |
{ |
| 349 |
return type ~ " " ~ field ~ "(" ~ type ~ " param)" ~ |
| 350 |
"{ |
| 351 |
if (!param && !contracts) return null; |
| 352 |
return getContracts()." ~ field ~ " = param; |
| 353 |
}"; |
| 354 |
} |
| 355 |
|
| 356 |
mixin(generateContractSetter("frequires", "Statements*")); |
| 357 |
mixin(generateContractSetter("fensures", "Ensures*")); |
| 358 |
mixin(generateContractSetter("frequire", "Statement")); |
| 359 |
mixin(generateContractSetter("fensure", "Statement")); |
| 360 |
mixin(generateContractSetter("fdrequire", "FuncDeclaration")); |
| 361 |
mixin(generateContractSetter("fdensure", "FuncDeclaration")); |
| 362 |
mixin(generateContractSetter("fdrequireParams", "Expressions*")); |
| 363 |
mixin(generateContractSetter("fdensureParams", "Expressions*")); |
| 364 |
} |
| 365 |
|
| 366 |
override FuncDeclaration syntaxCopy(Dsymbol s) |
| 367 |
{ |
| 368 |
//printf("FuncDeclaration::syntaxCopy('%s')\n", toChars()); |
| 369 |
FuncDeclaration f = s ? cast(FuncDeclaration)s |
| 370 |
: new FuncDeclaration(loc, endloc, ident, storage_class, type.syntaxCopy(), this.noreturn != 0); |
| 371 |
f.frequires = frequires ? Statement.arraySyntaxCopy(frequires) : null; |
| 372 |
f.fensures = fensures ? Ensure.arraySyntaxCopy(fensures) : null; |
| 373 |
f.fbody = fbody ? fbody.syntaxCopy() : null; |
| 374 |
return f; |
| 375 |
} |
| 376 |
|
| 377 |
override final bool equals(const RootObject o) const |
| 378 |
{ |
| 379 |
if (this == o) |
| 380 |
return true; |
| 381 |
|
| 382 |
auto s = isDsymbol(o); |
| 383 |
if (!s) |
| 384 |
return false; |
| 385 |
|
| 386 |
auto fd1 = this; |
| 387 |
auto fd2 = s.isFuncDeclaration(); |
| 388 |
if (!fd2) |
| 389 |
return false; |
| 390 |
|
| 391 |
auto fa1 = fd1.isFuncAliasDeclaration(); |
| 392 |
auto faf1 = fa1 ? fa1.toAliasFunc() : fd1; |
| 393 |
|
| 394 |
auto fa2 = fd2.isFuncAliasDeclaration(); |
| 395 |
auto faf2 = fa2 ? fa2.toAliasFunc() : fd2; |
| 396 |
|
| 397 |
if (fa1 && fa2) |
| 398 |
return faf1.equals(faf2) && fa1.hasOverloads == fa2.hasOverloads; |
| 399 |
|
| 400 |
bool b1 = fa1 !is null; |
| 401 |
if (b1 && faf1.isUnique() && !fa1.hasOverloads) |
| 402 |
b1 = false; |
| 403 |
|
| 404 |
bool b2 = fa2 !is null; |
| 405 |
if (b2 && faf2.isUnique() && !fa2.hasOverloads) |
| 406 |
b2 = false; |
| 407 |
|
| 408 |
if (b1 != b2) |
| 409 |
return false; |
| 410 |
|
| 411 |
return faf1.toParent().equals(faf2.toParent()) && |
| 412 |
faf1.ident.equals(faf2.ident) && |
| 413 |
faf1.type.equals(faf2.type); |
| 414 |
} |
| 415 |
|
| 416 |
/**************************************************** |
| 417 |
* Overload this FuncDeclaration with the new one f. |
| 418 |
* Return true if successful; i.e. no conflict. |
| 419 |
*/ |
| 420 |
override bool overloadInsert(Dsymbol s) |
| 421 |
{ |
| 422 |
//printf("FuncDeclaration::overloadInsert(s = %s) this = %s\n", s.toChars(), toChars()); |
| 423 |
assert(s != this); |
| 424 |
if (AliasDeclaration ad = s.isAliasDeclaration()) |
| 425 |
{ |
| 426 |
if (overnext) |
| 427 |
return overnext.overloadInsert(ad); |
| 428 |
if (!ad.aliassym && ad.type.ty != Tident && ad.type.ty != Tinstance && ad.type.ty != Ttypeof) |
| 429 |
{ |
| 430 |
//printf("\tad = '%s'\n", ad.type.toChars()); |
| 431 |
return false; |
| 432 |
} |
| 433 |
overnext = ad; |
| 434 |
//printf("\ttrue: no conflict\n"); |
| 435 |
return true; |
| 436 |
} |
| 437 |
TemplateDeclaration td = s.isTemplateDeclaration(); |
| 438 |
if (td) |
| 439 |
{ |
| 440 |
if (!td.funcroot) |
| 441 |
td.funcroot = this; |
| 442 |
if (overnext) |
| 443 |
return overnext.overloadInsert(td); |
| 444 |
overnext = td; |
| 445 |
return true; |
| 446 |
} |
| 447 |
FuncDeclaration fd = s.isFuncDeclaration(); |
| 448 |
if (!fd) |
| 449 |
return false; |
| 450 |
|
| 451 |
if (overnext) |
| 452 |
{ |
| 453 |
td = overnext.isTemplateDeclaration(); |
| 454 |
if (td) |
| 455 |
fd.overloadInsert(td); |
| 456 |
else |
| 457 |
return overnext.overloadInsert(fd); |
| 458 |
} |
| 459 |
overnext = fd; |
| 460 |
//printf("\ttrue: no conflict\n"); |
| 461 |
return true; |
| 462 |
} |
| 463 |
|
| 464 |
/******************************************** |
| 465 |
* find function template root in overload list |
| 466 |
*/ |
| 467 |
extern (D) final TemplateDeclaration findTemplateDeclRoot() |
| 468 |
{ |
| 469 |
FuncDeclaration f = this; |
| 470 |
while (f && f.overnext) |
| 471 |
{ |
| 472 |
//printf("f.overnext = %p %s\n", f.overnext, f.overnext.toChars()); |
| 473 |
if (TemplateDeclaration td = f.overnext.isTemplateDeclaration()) |
| 474 |
return td; |
| 475 |
f = f.overnext.isFuncDeclaration(); |
| 476 |
} |
| 477 |
return null; |
| 478 |
} |
| 479 |
|
| 480 |
/******************************************** |
| 481 |
* Returns true if function was declared |
| 482 |
* directly or indirectly in a unittest block |
| 483 |
*/ |
| 484 |
final bool inUnittest() |
| 485 |
{ |
| 486 |
Dsymbol f = this; |
| 487 |
do |
| 488 |
{ |
| 489 |
if (f.isUnitTestDeclaration()) |
| 490 |
return true; |
| 491 |
f = f.toParent(); |
| 492 |
} |
| 493 |
while (f); |
| 494 |
return false; |
| 495 |
} |
| 496 |
|
| 497 |
/******************************** |
| 498 |
* Searches for a label with the given identifier. This function will insert a new |
| 499 |
* `LabelDsymbol` into `labtab` if it does not contain a mapping for `ident`. |
| 500 |
* |
| 501 |
* Params: |
| 502 |
* ident = identifier of the requested label |
| 503 |
* loc = location used when creating a new `LabelDsymbol` |
| 504 |
* |
| 505 |
* Returns: the `LabelDsymbol` for `ident` |
| 506 |
*/ |
| 507 |
final LabelDsymbol searchLabel(Identifier ident, Loc loc) |
| 508 |
{ |
| 509 |
Dsymbol s; |
| 510 |
if (!labtab) |
| 511 |
labtab = new DsymbolTable(); // guess we need one |
| 512 |
|
| 513 |
s = labtab.lookup(ident); |
| 514 |
if (!s) |
| 515 |
{ |
| 516 |
s = new LabelDsymbol(ident, loc); |
| 517 |
labtab.insert(s); |
| 518 |
} |
| 519 |
return cast(LabelDsymbol)s; |
| 520 |
} |
| 521 |
|
| 522 |
/***************************************** |
| 523 |
* Determine lexical level difference from `this` to nested function `fd`. |
| 524 |
* Params: |
| 525 |
* fd = target of call |
| 526 |
* intypeof = !=0 if inside typeof |
| 527 |
* Returns: |
| 528 |
* 0 same level |
| 529 |
* >0 decrease nesting by number |
| 530 |
* -1 increase nesting by 1 (`fd` is nested within `this`) |
| 531 |
* LevelError error, `this` cannot call `fd` |
| 532 |
*/ |
| 533 |
extern (D) final int getLevel(FuncDeclaration fd, int intypeof) |
| 534 |
{ |
| 535 |
//printf("FuncDeclaration::getLevel(fd = '%s')\n", fd.toChars()); |
| 536 |
Dsymbol fdparent = fd.toParent2(); |
| 537 |
if (fdparent == this) |
| 538 |
return -1; |
| 539 |
|
| 540 |
Dsymbol s = this; |
| 541 |
int level = 0; |
| 542 |
while (fd != s && fdparent != s.toParent2()) |
| 543 |
{ |
| 544 |
//printf("\ts = %s, '%s'\n", s.kind(), s.toChars()); |
| 545 |
if (auto thisfd = s.isFuncDeclaration()) |
| 546 |
{ |
| 547 |
if (!thisfd.isNested() && !thisfd.vthis && !intypeof) |
| 548 |
return LevelError; |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
if (auto thiscd = s.isAggregateDeclaration()) |
| 553 |
{ |
| 554 |
/* AggregateDeclaration::isNested returns true only when |
| 555 |
* it has a hidden pointer. |
| 556 |
* But, calling the function belongs unrelated lexical scope |
| 557 |
* is still allowed inside typeof. |
| 558 |
* |
| 559 |
* struct Map(alias fun) { |
| 560 |
* typeof({ return fun(); }) RetType; |
| 561 |
* // No member function makes Map struct 'not nested'. |
| 562 |
* } |
| 563 |
*/ |
| 564 |
if (!thiscd.isNested() && !intypeof) |
| 565 |
return LevelError; |
| 566 |
} |
| 567 |
else |
| 568 |
return LevelError; |
| 569 |
} |
| 570 |
|
| 571 |
s = s.toParentP(fd); |
| 572 |
assert(s); |
| 573 |
level++; |
| 574 |
} |
| 575 |
return level; |
| 576 |
} |
| 577 |
|
| 578 |
enum LevelError = -2; |
| 579 |
|
| 580 |
override const(char)* toPrettyChars(bool QualifyTypes = false) |
| 581 |
{ |
| 582 |
if (isMain()) |
| 583 |
return "D main"; |
| 584 |
return Dsymbol.toPrettyChars(QualifyTypes); |
| 585 |
} |
| 586 |
|
| 587 |
/** for diagnostics, e.g. 'int foo(int x, int y) pure' */ |
| 588 |
final const(char)* toFullSignature() |
| 589 |
{ |
| 590 |
OutBuffer buf; |
| 591 |
functionToBufferWithIdent(type.toTypeFunction(), buf, toChars(), isStatic); |
| 592 |
return buf.extractChars(); |
| 593 |
} |
| 594 |
|
| 595 |
final bool isMain() const |
| 596 |
{ |
| 597 |
return ident == Id.main && resolvedLinkage() != LINK.c && !isMember() && !isNested(); |
| 598 |
} |
| 599 |
|
| 600 |
final bool isCMain() const |
| 601 |
{ |
| 602 |
return ident == Id.main && resolvedLinkage() == LINK.c && !isMember() && !isNested(); |
| 603 |
} |
| 604 |
|
| 605 |
final bool isWinMain() const |
| 606 |
{ |
| 607 |
//printf("FuncDeclaration::isWinMain() %s\n", toChars()); |
| 608 |
version (none) |
| 609 |
{ |
| 610 |
bool x = ident == Id.WinMain && resolvedLinkage() != LINK.c && !isMember(); |
| 611 |
printf("%s\n", x ? "yes" : "no"); |
| 612 |
return x; |
| 613 |
} |
| 614 |
else |
| 615 |
{ |
| 616 |
return ident == Id.WinMain && resolvedLinkage() != LINK.c && !isMember(); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
final bool isDllMain() const |
| 621 |
{ |
| 622 |
return ident == Id.DllMain && resolvedLinkage() != LINK.c && !isMember(); |
| 623 |
} |
| 624 |
|
| 625 |
final bool isRtInit() const |
| 626 |
{ |
| 627 |
return ident == Id.rt_init && resolvedLinkage() == LINK.c && !isMember() && !isNested(); |
| 628 |
} |
| 629 |
|
| 630 |
override final bool isExport() const |
| 631 |
{ |
| 632 |
return visibility.kind == Visibility.Kind.export_ || dllExport; |
| 633 |
} |
| 634 |
|
| 635 |
override final bool isImportedSymbol() const |
| 636 |
{ |
| 637 |
//printf("isImportedSymbol()\n"); |
| 638 |
//printf("protection = %d\n", visibility); |
| 639 |
return (visibility.kind == Visibility.Kind.export_ || dllImport) && !fbody; |
| 640 |
} |
| 641 |
|
| 642 |
override final bool isCodeseg() const pure nothrow @nogc @safe |
| 643 |
{ |
| 644 |
return true; // functions are always in the code segment |
| 645 |
} |
| 646 |
|
| 647 |
override final bool isOverloadable() const |
| 648 |
{ |
| 649 |
return true; // functions can be overloaded |
| 650 |
} |
| 651 |
|
| 652 |
/*********************************** |
| 653 |
* Override so it can work even if semantic() hasn't yet |
| 654 |
* been run. |
| 655 |
*/ |
| 656 |
override final bool isAbstract() |
| 657 |
{ |
| 658 |
if (storage_class & STC.abstract_) |
| 659 |
return true; |
| 660 |
if (semanticRun >= PASS.semanticdone) |
| 661 |
return false; |
| 662 |
|
| 663 |
if (_scope) |
| 664 |
{ |
| 665 |
if (_scope.stc & STC.abstract_) |
| 666 |
return true; |
| 667 |
parent = _scope.parent; |
| 668 |
Dsymbol parent = toParent(); |
| 669 |
if (parent.isInterfaceDeclaration()) |
| 670 |
return true; |
| 671 |
} |
| 672 |
return false; |
| 673 |
} |
| 674 |
|
| 675 |
extern (D) final uint saveFlags() |
| 676 |
{ |
| 677 |
return bitFields; |
| 678 |
} |
| 679 |
|
| 680 |
extern (D) final uint restoreFlags(uint f) |
| 681 |
{ |
| 682 |
bitFields = f; |
| 683 |
return bitFields; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
/************************************** |
| 688 |
* The function is doing something that may throw an exception, register that in case nothrow is being inferred |
| 689 |
* |
| 690 |
* Params: |
| 691 |
* loc = location of action |
| 692 |
* format = format string for error message |
| 693 |
* args = arguments to format string |
| 694 |
*/ |
| 695 |
extern (D) final void setThrow(Loc loc, const(char)* format, RootObject[] args...) |
| 696 |
{ |
| 697 |
if (nothrowInprocess && !nothrowViolation) |
| 698 |
{ |
| 699 |
nothrowViolation = new AttributeViolation(loc, format, args); // action that requires GC |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
/************************************** |
| 704 |
* The function calls non-`nothrow` function f, register that in case nothrow is being inferred |
| 705 |
* Params: |
| 706 |
* loc = location of call |
| 707 |
* fd = function being called |
| 708 |
*/ |
| 709 |
extern (D) final void setThrowCall(Loc loc, FuncDeclaration fd) |
| 710 |
{ |
| 711 |
if (nothrowInprocess && !nothrowViolation) |
| 712 |
{ |
| 713 |
nothrowViolation = new AttributeViolation(loc, fd); // action that requires GC |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
/**************************************** |
| 718 |
* Determine if function needs a static frame pointer. |
| 719 |
* Returns: |
| 720 |
* `true` if function is really nested within other function. |
| 721 |
* Contracts: |
| 722 |
* If isNested() returns true, isThis() should return false, |
| 723 |
* unless the function needs a dual-context pointer. |
| 724 |
*/ |
| 725 |
bool isNested() const |
| 726 |
{ |
| 727 |
auto f = toAliasFunc(); |
| 728 |
//printf("\ttoParent2() = '%s'\n", f.toParent2().toChars()); |
| 729 |
return ((f.storage_class & STC.static_) == 0) && |
| 730 |
(f._linkage == LINK.d) && |
| 731 |
(f.toParent2().isFuncDeclaration() !is null || |
| 732 |
f.toParent2() !is f.toParentLocal()); |
| 733 |
} |
| 734 |
|
| 735 |
/**************************************** |
| 736 |
* Determine if function is a non-static member function |
| 737 |
* that has an implicit 'this' expression. |
| 738 |
* Returns: |
| 739 |
* The aggregate it is a member of, or null. |
| 740 |
* Contracts: |
| 741 |
* Both isThis() and isNested() should return true if function needs a dual-context pointer, |
| 742 |
* otherwise if isThis() returns true, isNested() should return false. |
| 743 |
*/ |
| 744 |
override inout(AggregateDeclaration) isThis() inout |
| 745 |
{ |
| 746 |
//printf("+FuncDeclaration::isThis() '%s'\n", toChars()); |
| 747 |
auto ad = (storage_class & STC.static_) ? .objc.isThis(this) : isMemberLocal(); |
| 748 |
//printf("-FuncDeclaration::isThis() %p\n", ad); |
| 749 |
return ad; |
| 750 |
} |
| 751 |
|
| 752 |
override final bool needThis() |
| 753 |
{ |
| 754 |
//printf("FuncDeclaration::needThis() '%s'\n", toChars()); |
| 755 |
return toAliasFunc().isThis() !is null; |
| 756 |
} |
| 757 |
|
| 758 |
// Determine if a function is pedantically virtual |
| 759 |
final bool isVirtualMethod() |
| 760 |
{ |
| 761 |
if (toAliasFunc() != this) |
| 762 |
return toAliasFunc().isVirtualMethod(); |
| 763 |
|
| 764 |
//printf("FuncDeclaration::isVirtualMethod() %s\n", toChars()); |
| 765 |
if (!isVirtual()) |
| 766 |
return false; |
| 767 |
// If it's a final method, and does not override anything, then it is not virtual |
| 768 |
if (isFinalFunc() && foverrides.length == 0) |
| 769 |
{ |
| 770 |
return false; |
| 771 |
} |
| 772 |
return true; |
| 773 |
} |
| 774 |
|
| 775 |
// Determine if function goes into virtual function pointer table |
| 776 |
bool isVirtual() const |
| 777 |
{ |
| 778 |
if (toAliasFunc() != this) |
| 779 |
return toAliasFunc().isVirtual(); |
| 780 |
|
| 781 |
auto p = toParent(); |
| 782 |
|
| 783 |
if (!isMember || !p.isClassDeclaration) |
| 784 |
return false; |
| 785 |
|
| 786 |
if (p.isClassDeclaration.classKind == ClassKind.objc) |
| 787 |
return .objc.isVirtual(this); |
| 788 |
|
| 789 |
version (none) |
| 790 |
{ |
| 791 |
printf("FuncDeclaration::isVirtual(%s)\n", toChars()); |
| 792 |
printf("isMember:%p isStatic:%d private:%d ctor:%d !Dlinkage:%d\n", isMember(), isStatic(), visibility == Visibility.Kind.private_, isCtorDeclaration(), linkage != LINK.d); |
| 793 |
printf("result is %d\n", isMember() && !(isStatic() || visibility == Visibility.Kind.private_ || visibility == Visibility.Kind.package_) && p.isClassDeclaration() && !(p.isInterfaceDeclaration() && isFinalFunc())); |
| 794 |
} |
| 795 |
return !(isStatic() || visibility.kind == Visibility.Kind.private_ || visibility.kind == Visibility.Kind.package_) && !(p.isInterfaceDeclaration() && isFinalFunc()); |
| 796 |
} |
| 797 |
|
| 798 |
final bool isFinalFunc() const |
| 799 |
{ |
| 800 |
if (toAliasFunc() != this) |
| 801 |
return toAliasFunc().isFinalFunc(); |
| 802 |
|
| 803 |
version (none) |
| 804 |
{{ |
| 805 |
auto cd = toParent().isClassDeclaration(); |
| 806 |
printf("FuncDeclaration::isFinalFunc(%s), %x\n", toChars(), Declaration.isFinal()); |
| 807 |
printf("%p %d %d %d\n", isMember(), isStatic(), Declaration.isFinal(), ((cd = toParent().isClassDeclaration()) !is null && cd.storage_class & STC.final_)); |
| 808 |
printf("result is %d\n", isMember() && (Declaration.isFinal() || (cd !is null && cd.storage_class & STC.final_))); |
| 809 |
if (cd) |
| 810 |
printf("\tmember of %s\n", cd.toChars()); |
| 811 |
}} |
| 812 |
if (!isMember()) |
| 813 |
return false; |
| 814 |
if (Declaration.isFinal()) |
| 815 |
return true; |
| 816 |
auto cd = toParent().isClassDeclaration(); |
| 817 |
return (cd !is null) && (cd.storage_class & STC.final_); |
| 818 |
} |
| 819 |
|
| 820 |
bool addPreInvariant() |
| 821 |
{ |
| 822 |
auto ad = isThis(); |
| 823 |
ClassDeclaration cd = ad ? ad.isClassDeclaration() : null; |
| 824 |
return (ad && !(cd && cd.isCPPclass()) && global.params.useInvariants == CHECKENABLE.on && (visibility.kind == Visibility.Kind.protected_ || visibility.kind == Visibility.Kind.public_ || visibility.kind == Visibility.Kind.export_) && !this.isNaked()); |
| 825 |
} |
| 826 |
|
| 827 |
bool addPostInvariant() |
| 828 |
{ |
| 829 |
auto ad = isThis(); |
| 830 |
ClassDeclaration cd = ad ? ad.isClassDeclaration() : null; |
| 831 |
return (ad && !(cd && cd.isCPPclass()) && ad.inv && global.params.useInvariants == CHECKENABLE.on && (visibility.kind == Visibility.Kind.protected_ || visibility.kind == Visibility.Kind.public_ || visibility.kind == Visibility.Kind.export_) && !this.isNaked()); |
| 832 |
} |
| 833 |
|
| 834 |
override const(char)* kind() const |
| 835 |
{ |
| 836 |
return this.isGenerated() ? "generated function" : "function"; |
| 837 |
} |
| 838 |
|
| 839 |
/******************************************** |
| 840 |
* Returns: |
| 841 |
* true if there are no overloads of this function |
| 842 |
*/ |
| 843 |
final bool isUnique() const |
| 844 |
{ |
| 845 |
bool result = false; |
| 846 |
overloadApply(cast() this, (Dsymbol s) |
| 847 |
{ |
| 848 |
auto f = s.isFuncDeclaration(); |
| 849 |
auto td = s.isTemplateDeclaration(); |
| 850 |
if (!f && !td) |
| 851 |
return 0; |
| 852 |
if (result) |
| 853 |
{ |
| 854 |
result = false; |
| 855 |
return 1; // ambiguous, done |
| 856 |
} |
| 857 |
else |
| 858 |
{ |
| 859 |
result = true; |
| 860 |
return 0; |
| 861 |
} |
| 862 |
}); |
| 863 |
return result; |
| 864 |
} |
| 865 |
|
| 866 |
/******************************* |
| 867 |
* Look at all the variables in this function that are referenced |
| 868 |
* by nested functions, and determine if a closure needs to be |
| 869 |
* created for them. |
| 870 |
*/ |
| 871 |
final bool needsClosure() |
| 872 |
{ |
| 873 |
/* Need a closure for all the closureVars[] if any of the |
| 874 |
* closureVars[] are accessed by a |
| 875 |
* function that escapes the scope of this function. |
| 876 |
* We take the conservative approach and decide that a function needs |
| 877 |
* a closure if it: |
| 878 |
* 1) is a virtual function |
| 879 |
* 2) has its address taken |
| 880 |
* 3) has a parent that escapes |
| 881 |
* 4) calls another nested function that needs a closure |
| 882 |
* |
| 883 |
* Note that since a non-virtual function can be called by |
| 884 |
* a virtual one, if that non-virtual function accesses a closure |
| 885 |
* var, the closure still has to be taken. Hence, we check for isThis() |
| 886 |
* instead of isVirtual(). (thanks to David Friedman) |
| 887 |
* |
| 888 |
* When the function returns a local struct or class, `requiresClosure` |
| 889 |
* is already set to `true` upon entering this function when the |
| 890 |
* struct/class refers to a local variable and a closure is needed. |
| 891 |
*/ |
| 892 |
//printf("FuncDeclaration::needsClosure() %s\n", toPrettyChars()); |
| 893 |
|
| 894 |
if (requiresClosure) |
| 895 |
goto Lyes; |
| 896 |
|
| 897 |
for (size_t i = 0; i < closureVars.length; i++) |
| 898 |
{ |
| 899 |
VarDeclaration v = closureVars[i]; |
| 900 |
//printf("\tv = %s\n", v.toChars()); |
| 901 |
|
| 902 |
for (size_t j = 0; j < v.nestedrefs.length; j++) |
| 903 |
{ |
| 904 |
FuncDeclaration f = v.nestedrefs[j]; |
| 905 |
assert(f != this); |
| 906 |
|
| 907 |
/* __require and __ensure will always get called directly, |
| 908 |
* so they never make outer functions closure. |
| 909 |
*/ |
| 910 |
if (f.ident == Id.require || f.ident == Id.ensure) |
| 911 |
continue; |
| 912 |
|
| 913 |
//printf("\t\tf = %p, %s, isVirtual=%d, isThis=%p, tookAddressOf=%d\n", f, f.toChars(), f.isVirtual(), f.isThis(), f.tookAddressOf); |
| 914 |
|
| 915 |
/* Look to see if f escapes. We consider all parents of f within |
| 916 |
* this, and also all siblings which call f; if any of them escape, |
| 917 |
* so does f. |
| 918 |
* Mark all affected functions as requiring closures. |
| 919 |
*/ |
| 920 |
for (Dsymbol s = f; s && s != this; s = s.toParentP(this)) |
| 921 |
{ |
| 922 |
FuncDeclaration fx = s.isFuncDeclaration(); |
| 923 |
if (!fx) |
| 924 |
continue; |
| 925 |
if (fx.isThis() || fx.tookAddressOf) |
| 926 |
{ |
| 927 |
//printf("\t\tfx = %s, isVirtual=%d, isThis=%p, tookAddressOf=%d\n", fx.toChars(), fx.isVirtual(), fx.isThis(), fx.tookAddressOf); |
| 928 |
|
| 929 |
/* Mark as needing closure any functions between this and f |
| 930 |
*/ |
| 931 |
markAsNeedingClosure((fx == f) ? fx.toParentP(this) : fx, this); |
| 932 |
|
| 933 |
requiresClosure = true; |
| 934 |
} |
| 935 |
|
| 936 |
/* We also need to check if any sibling functions that |
| 937 |
* called us, have escaped. This is recursive: we need |
| 938 |
* to check the callers of our siblings. |
| 939 |
*/ |
| 940 |
if (checkEscapingSiblings(fx, this)) |
| 941 |
requiresClosure = true; |
| 942 |
|
| 943 |
/* https://issues.dlang.org/show_bug.cgi?id=12406 |
| 944 |
* Iterate all closureVars to mark all descendant |
| 945 |
* nested functions that access to the closing context of this function. |
| 946 |
*/ |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
if (requiresClosure) |
| 951 |
goto Lyes; |
| 952 |
|
| 953 |
return false; |
| 954 |
|
| 955 |
Lyes: |
| 956 |
return true; |
| 957 |
} |
| 958 |
|
| 959 |
/*********************************************** |
| 960 |
* Determine if function's variables are referenced by a function |
| 961 |
* nested within it. |
| 962 |
*/ |
| 963 |
final bool hasNestedFrameRefs() |
| 964 |
{ |
| 965 |
if (closureVars.length) |
| 966 |
return true; |
| 967 |
|
| 968 |
/* If a virtual function has contracts, assume its variables are referenced |
| 969 |
* by those contracts, even if they aren't. Because they might be referenced |
| 970 |
* by the overridden or overriding function's contracts. |
| 971 |
* This can happen because frequire and fensure are implemented as nested functions, |
| 972 |
* and they can be called directly by an overriding function and the overriding function's |
| 973 |
* context had better match, or |
| 974 |
* https://issues.dlang.org/show_bug.cgi?id=7335 will bite. |
| 975 |
*/ |
| 976 |
if (fdrequire || fdensure) |
| 977 |
return true; |
| 978 |
|
| 979 |
if (foverrides.length && isVirtualMethod()) |
| 980 |
{ |
| 981 |
for (size_t i = 0; i < foverrides.length; i++) |
| 982 |
{ |
| 983 |
FuncDeclaration fdv = foverrides[i]; |
| 984 |
if (fdv.hasNestedFrameRefs()) |
| 985 |
return true; |
| 986 |
} |
| 987 |
} |
| 988 |
return false; |
| 989 |
} |
| 990 |
|
| 991 |
/********************************************* |
| 992 |
* Returns: the function's parameter list, and whether |
| 993 |
* it is variadic or not. |
| 994 |
*/ |
| 995 |
final ParameterList getParameterList() |
| 996 |
{ |
| 997 |
if (type) |
| 998 |
{ |
| 999 |
if (TypeFunction fdtype = type.isTypeFunction()) // Could also be TypeError |
| 1000 |
return fdtype.parameterList; |
| 1001 |
} |
| 1002 |
|
| 1003 |
return ParameterList(null, VarArg.none); |
| 1004 |
} |
| 1005 |
|
| 1006 |
/********************************** |
| 1007 |
* Generate a FuncDeclaration for a runtime library function. |
| 1008 |
*/ |
| 1009 |
extern(D) static FuncDeclaration genCfunc(Parameters* fparams, Type treturn, const(char)* name, STC stc = STC.none) |
| 1010 |
{ |
| 1011 |
return genCfunc(fparams, treturn, Identifier.idPool(name[0 .. strlen(name)]), stc); |
| 1012 |
} |
| 1013 |
|
| 1014 |
extern(D) static FuncDeclaration genCfunc(Parameters* fparams, Type treturn, Identifier id, STC stc = STC.none) |
| 1015 |
{ |
| 1016 |
FuncDeclaration fd; |
| 1017 |
TypeFunction tf; |
| 1018 |
Dsymbol s; |
| 1019 |
__gshared DsymbolTable st = null; |
| 1020 |
|
| 1021 |
//printf("genCfunc(name = '%s')\n", id.toChars()); |
| 1022 |
//printf("treturn\n\t"); treturn.print(); |
| 1023 |
|
| 1024 |
// See if already in table |
| 1025 |
if (!st) |
| 1026 |
st = new DsymbolTable(); |
| 1027 |
s = st.lookup(id); |
| 1028 |
if (s) |
| 1029 |
{ |
| 1030 |
fd = s.isFuncDeclaration(); |
| 1031 |
assert(fd); |
| 1032 |
assert(fd.type.nextOf().equals(treturn)); |
| 1033 |
} |
| 1034 |
else |
| 1035 |
{ |
| 1036 |
tf = new TypeFunction(ParameterList(fparams), treturn, LINK.c, stc); |
| 1037 |
fd = new FuncDeclaration(Loc.initial, Loc.initial, id, STC.static_, tf); |
| 1038 |
fd.visibility = Visibility(Visibility.Kind.public_); |
| 1039 |
fd._linkage = LINK.c; |
| 1040 |
|
| 1041 |
st.insert(fd); |
| 1042 |
} |
| 1043 |
return fd; |
| 1044 |
} |
| 1045 |
|
| 1046 |
inout(FuncDeclaration) toAliasFunc() inout @safe |
| 1047 |
{ |
| 1048 |
return this; |
| 1049 |
} |
| 1050 |
|
| 1051 |
override void accept(Visitor v) |
| 1052 |
{ |
| 1053 |
v.visit(this); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
/** |
| 1058 |
Checks for mismatching modifiers between `lhsMod` and `rhsMod` and prints the |
| 1059 |
mismatching modifiers to `buf`. |
| 1060 |
|
| 1061 |
The modifiers of the `lhsMod` mismatching the ones with the `rhsMod` are printed, i.e. |
| 1062 |
lhs(shared) vs. rhs() prints "`shared`", wheras lhs() vs rhs(shared) prints "non-shared". |
| 1063 |
|
| 1064 |
Params: |
| 1065 |
buf = output buffer to write to |
| 1066 |
lhsMod = modifier on the left-hand side |
| 1067 |
lhsMod = modifier on the right-hand side |
| 1068 |
|
| 1069 |
Returns: |
| 1070 |
|
| 1071 |
A tuple with `isMutable` and `isNotShared` set |
| 1072 |
if the `lhsMod` is missing those modifiers (compared to rhs). |
| 1073 |
*/ |
| 1074 |
auto MODMatchToBuffer(OutBuffer* buf, ubyte lhsMod, ubyte rhsMod) |
| 1075 |
{ |
| 1076 |
static struct Mismatches |
| 1077 |
{ |
| 1078 |
bool isNotShared; |
| 1079 |
bool isMutable; |
| 1080 |
} |
| 1081 |
|
| 1082 |
Mismatches mismatches; |
| 1083 |
|
| 1084 |
bool bothMutable = ((lhsMod & rhsMod) == 0); |
| 1085 |
bool sharedMismatch = ((lhsMod ^ rhsMod) & MODFlags.shared_) != 0; |
| 1086 |
bool sharedMismatchOnly = ((lhsMod ^ rhsMod) == MODFlags.shared_); |
| 1087 |
|
| 1088 |
if (lhsMod & MODFlags.shared_) |
| 1089 |
buf.writestring("`shared` "); |
| 1090 |
else if (sharedMismatch && !(lhsMod & MODFlags.immutable_)) |
| 1091 |
{ |
| 1092 |
buf.writestring("non-shared "); |
| 1093 |
mismatches.isNotShared = true; |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (bothMutable && sharedMismatchOnly) |
| 1097 |
{ |
| 1098 |
} |
| 1099 |
else if (lhsMod & MODFlags.immutable_) |
| 1100 |
buf.writestring("`immutable` "); |
| 1101 |
else if (lhsMod & MODFlags.const_) |
| 1102 |
buf.writestring("`const` "); |
| 1103 |
else if (lhsMod & MODFlags.wild) |
| 1104 |
buf.writestring("`inout` "); |
| 1105 |
else |
| 1106 |
{ |
| 1107 |
buf.writestring("mutable "); |
| 1108 |
mismatches.isMutable = true; |
| 1109 |
} |
| 1110 |
|
| 1111 |
return mismatches; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/// |
| 1115 |
unittest |
| 1116 |
{ |
| 1117 |
OutBuffer buf; |
| 1118 |
auto mismatches = MODMatchToBuffer(&buf, MODFlags.shared_, 0); |
| 1119 |
assert(buf[] == "`shared` "); |
| 1120 |
assert(!mismatches.isNotShared); |
| 1121 |
|
| 1122 |
buf.setsize(0); |
| 1123 |
mismatches = MODMatchToBuffer(&buf, 0, MODFlags.shared_); |
| 1124 |
assert(buf[] == "non-shared "); |
| 1125 |
assert(mismatches.isNotShared); |
| 1126 |
|
| 1127 |
buf.setsize(0); |
| 1128 |
mismatches = MODMatchToBuffer(&buf, MODFlags.const_, 0); |
| 1129 |
assert(buf[] == "`const` "); |
| 1130 |
assert(!mismatches.isMutable); |
| 1131 |
|
| 1132 |
buf.setsize(0); |
| 1133 |
mismatches = MODMatchToBuffer(&buf, 0, MODFlags.const_); |
| 1134 |
assert(buf[] == "mutable "); |
| 1135 |
assert(mismatches.isMutable); |
| 1136 |
} |
| 1137 |
|
| 1138 |
/* For all functions between outerFunc and f, mark them as needing |
| 1139 |
* a closure. |
| 1140 |
*/ |
| 1141 |
private void markAsNeedingClosure(Dsymbol f, FuncDeclaration outerFunc) |
| 1142 |
{ |
| 1143 |
for (Dsymbol sx = f; sx && sx != outerFunc; sx = sx.toParentP(outerFunc)) |
| 1144 |
{ |
| 1145 |
FuncDeclaration fy = sx.isFuncDeclaration(); |
| 1146 |
if (fy && fy.closureVars.length) |
| 1147 |
{ |
| 1148 |
/* fy needs a closure if it has closureVars[], |
| 1149 |
* because the frame pointer in the closure will be accessed. |
| 1150 |
*/ |
| 1151 |
fy.requiresClosure = true; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
/******** |
| 1157 |
* Given a nested function f inside a function outerFunc, check |
| 1158 |
* if any sibling callers of f have escaped. If so, mark |
| 1159 |
* all the enclosing functions as needing closures. |
| 1160 |
* This is recursive: we need to check the callers of our siblings. |
| 1161 |
* Note that nested functions can only call lexically earlier nested |
| 1162 |
* functions, so loops are impossible. |
| 1163 |
* Params: |
| 1164 |
* f = inner function (nested within outerFunc) |
| 1165 |
* outerFunc = outer function |
| 1166 |
* p = for internal recursion use |
| 1167 |
* Returns: |
| 1168 |
* true if any closures were needed |
| 1169 |
*/ |
| 1170 |
bool checkEscapingSiblings(FuncDeclaration f, FuncDeclaration outerFunc, void* p = null) |
| 1171 |
{ |
| 1172 |
static struct PrevSibling |
| 1173 |
{ |
| 1174 |
PrevSibling* p; |
| 1175 |
FuncDeclaration f; |
| 1176 |
} |
| 1177 |
|
| 1178 |
if (f.computedEscapingSiblings) |
| 1179 |
return f.hasEscapingSiblings; |
| 1180 |
|
| 1181 |
PrevSibling ps; |
| 1182 |
ps.p = cast(PrevSibling*)p; |
| 1183 |
ps.f = f; |
| 1184 |
|
| 1185 |
//printf("checkEscapingSiblings(f = %s, outerfunc = %s)\n", f.toChars(), outerFunc.toChars()); |
| 1186 |
bool bAnyClosures = false; |
| 1187 |
for (size_t i = 0; i < f.siblingCallers.length; ++i) |
| 1188 |
{ |
| 1189 |
FuncDeclaration g = f.siblingCallers[i]; |
| 1190 |
if (g.isThis() || g.tookAddressOf) |
| 1191 |
{ |
| 1192 |
markAsNeedingClosure(g, outerFunc); |
| 1193 |
bAnyClosures = true; |
| 1194 |
} |
| 1195 |
|
| 1196 |
for (auto parent = g.toParentP(outerFunc); parent && parent !is outerFunc; parent = parent.toParentP(outerFunc)) |
| 1197 |
{ |
| 1198 |
// A parent of the sibling had its address taken. |
| 1199 |
// Assume escaping of parent affects its children, so needs propagating. |
| 1200 |
// see https://issues.dlang.org/show_bug.cgi?id=19679 |
| 1201 |
FuncDeclaration parentFunc = parent.isFuncDeclaration; |
| 1202 |
if (parentFunc && parentFunc.tookAddressOf) |
| 1203 |
{ |
| 1204 |
markAsNeedingClosure(parentFunc, outerFunc); |
| 1205 |
bAnyClosures = true; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
PrevSibling* prev = cast(PrevSibling*)p; |
| 1210 |
while (1) |
| 1211 |
{ |
| 1212 |
if (!prev) |
| 1213 |
{ |
| 1214 |
bAnyClosures |= checkEscapingSiblings(g, outerFunc, &ps); |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
if (prev.f == g) |
| 1218 |
break; |
| 1219 |
prev = prev.p; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
f.hasEscapingSiblings = bAnyClosures; |
| 1223 |
f.computedEscapingSiblings = true; |
| 1224 |
//printf("\t%d\n", bAnyClosures); |
| 1225 |
return bAnyClosures; |
| 1226 |
} |
| 1227 |
|
| 1228 |
/*********************************************************** |
| 1229 |
* Used as a way to import a set of functions from another scope into this one. |
| 1230 |
*/ |
| 1231 |
extern (C++) final class FuncAliasDeclaration : FuncDeclaration |
| 1232 |
{ |
| 1233 |
FuncDeclaration funcalias; |
| 1234 |
bool hasOverloads; |
| 1235 |
|
| 1236 |
extern (D) this(Identifier ident, FuncDeclaration funcalias, bool hasOverloads = true) |
| 1237 |
{ |
| 1238 |
super(funcalias.loc, funcalias.endloc, ident, funcalias.storage_class, funcalias.type); |
| 1239 |
assert(funcalias != this); |
| 1240 |
this.funcalias = funcalias; |
| 1241 |
this.dsym = DSYM.funcAliasDeclaration; |
| 1242 |
|
| 1243 |
this.hasOverloads = hasOverloads; |
| 1244 |
if (hasOverloads) |
| 1245 |
{ |
| 1246 |
if (FuncAliasDeclaration fad = funcalias.isFuncAliasDeclaration()) |
| 1247 |
this.hasOverloads = fad.hasOverloads; |
| 1248 |
} |
| 1249 |
else |
| 1250 |
{ |
| 1251 |
// for internal use |
| 1252 |
assert(!funcalias.isFuncAliasDeclaration()); |
| 1253 |
this.hasOverloads = false; |
| 1254 |
} |
| 1255 |
userAttribDecl = funcalias.userAttribDecl; |
| 1256 |
} |
| 1257 |
|
| 1258 |
override const(char)* kind() const |
| 1259 |
{ |
| 1260 |
return "function alias"; |
| 1261 |
} |
| 1262 |
|
| 1263 |
override inout(FuncDeclaration) toAliasFunc() inout |
| 1264 |
{ |
| 1265 |
return funcalias.toAliasFunc(); |
| 1266 |
} |
| 1267 |
|
| 1268 |
override void accept(Visitor v) |
| 1269 |
{ |
| 1270 |
v.visit(this); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
/*********************************************************** |
| 1275 |
*/ |
| 1276 |
extern (C++) final class FuncLiteralDeclaration : FuncDeclaration |
| 1277 |
{ |
| 1278 |
TOK tok; // TOK.function_ or TOK.delegate_ |
| 1279 |
Type treq; // target of return type inference |
| 1280 |
|
| 1281 |
// backend |
| 1282 |
bool deferToObj; |
| 1283 |
|
| 1284 |
extern (D) this(Loc loc, Loc endloc, Type type, TOK tok, ForeachStatement fes, Identifier id = null, STC storage_class = STC.none) |
| 1285 |
{ |
| 1286 |
super(loc, endloc, null, storage_class, type); |
| 1287 |
this.dsym = DSYM.funcLiteralDeclaration; |
| 1288 |
this.ident = id ? id : Id.empty; |
| 1289 |
this.tok = tok; |
| 1290 |
this.fes = fes; |
| 1291 |
// Always infer scope for function literals |
| 1292 |
// See https://issues.dlang.org/show_bug.cgi?id=20362 |
| 1293 |
this.scopeInprocess = true; |
| 1294 |
//printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this.ident.toChars(), type.toChars()); |
| 1295 |
} |
| 1296 |
|
| 1297 |
override FuncLiteralDeclaration syntaxCopy(Dsymbol s) |
| 1298 |
{ |
| 1299 |
//printf("FuncLiteralDeclaration::syntaxCopy('%s')\n", toChars()); |
| 1300 |
assert(!s); |
| 1301 |
auto f = new FuncLiteralDeclaration(loc, endloc, type.syntaxCopy(), tok, fes, ident, storage_class & STC.auto_); |
| 1302 |
f.treq = treq; // don't need to copy |
| 1303 |
FuncDeclaration.syntaxCopy(f); |
| 1304 |
return f; |
| 1305 |
} |
| 1306 |
|
| 1307 |
override bool isNested() const |
| 1308 |
{ |
| 1309 |
//printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars()); |
| 1310 |
return (tok != TOK.function_) && !isThis(); |
| 1311 |
} |
| 1312 |
|
| 1313 |
override inout(AggregateDeclaration) isThis() inout |
| 1314 |
{ |
| 1315 |
return tok == TOK.delegate_ ? super.isThis() : null; |
| 1316 |
} |
| 1317 |
|
| 1318 |
override bool isVirtual() const |
| 1319 |
{ |
| 1320 |
return false; |
| 1321 |
} |
| 1322 |
|
| 1323 |
override bool addPreInvariant() |
| 1324 |
{ |
| 1325 |
return false; |
| 1326 |
} |
| 1327 |
|
| 1328 |
override bool addPostInvariant() |
| 1329 |
{ |
| 1330 |
return false; |
| 1331 |
} |
| 1332 |
|
| 1333 |
override const(char)* kind() const |
| 1334 |
{ |
| 1335 |
// GCC requires the (char*) casts |
| 1336 |
return (tok != TOK.function_) ? "delegate" : "function"; |
| 1337 |
} |
| 1338 |
|
| 1339 |
override const(char)* toPrettyChars(bool QualifyTypes = false) |
| 1340 |
{ |
| 1341 |
if (parent) |
| 1342 |
{ |
| 1343 |
if (TemplateInstance ti = parent.isTemplateInstance()) |
| 1344 |
return ti.tempdecl.toPrettyChars(QualifyTypes); |
| 1345 |
} |
| 1346 |
return Dsymbol.toPrettyChars(QualifyTypes); |
| 1347 |
} |
| 1348 |
|
| 1349 |
override void accept(Visitor v) |
| 1350 |
{ |
| 1351 |
v.visit(this); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
/*********************************************************** |
| 1356 |
*/ |
| 1357 |
extern (C++) final class CtorDeclaration : FuncDeclaration |
| 1358 |
{ |
| 1359 |
bool isCpCtor; // copy constructor |
| 1360 |
bool isMoveCtor; // move constructor (aka rvalue constructor) |
| 1361 |
extern (D) this(Loc loc, Loc endloc, STC stc, Type type) |
| 1362 |
{ |
| 1363 |
super(loc, endloc, Id.ctor, stc, type); |
| 1364 |
this.dsym = DSYM.ctorDeclaration; |
| 1365 |
//printf("CtorDeclaration(loc = %s) %s %p\n", loc.toChars(), toChars(), this); |
| 1366 |
} |
| 1367 |
|
| 1368 |
override CtorDeclaration syntaxCopy(Dsymbol s) |
| 1369 |
{ |
| 1370 |
assert(!s); |
| 1371 |
auto f = new CtorDeclaration(loc, endloc, storage_class, type.syntaxCopy()); |
| 1372 |
FuncDeclaration.syntaxCopy(f); |
| 1373 |
return f; |
| 1374 |
} |
| 1375 |
|
| 1376 |
override const(char)* kind() const |
| 1377 |
{ |
| 1378 |
return isCpCtor ? "copy constructor" : "constructor"; |
| 1379 |
} |
| 1380 |
|
| 1381 |
override bool isVirtual() const |
| 1382 |
{ |
| 1383 |
return false; |
| 1384 |
} |
| 1385 |
|
| 1386 |
override bool addPreInvariant() |
| 1387 |
{ |
| 1388 |
return false; |
| 1389 |
} |
| 1390 |
|
| 1391 |
override bool addPostInvariant() |
| 1392 |
{ |
| 1393 |
return (isThis() && vthis && global.params.useInvariants == CHECKENABLE.on); |
| 1394 |
} |
| 1395 |
|
| 1396 |
override void accept(Visitor v) |
| 1397 |
{ |
| 1398 |
v.visit(this); |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
/*********************************************************** |
| 1403 |
*/ |
| 1404 |
extern (C++) final class PostBlitDeclaration : FuncDeclaration |
| 1405 |
{ |
| 1406 |
extern (D) this(Loc loc, Loc endloc, STC stc, Identifier id) |
| 1407 |
{ |
| 1408 |
super(loc, endloc, id, stc, null); |
| 1409 |
this.dsym = DSYM.postBlitDeclaration; |
| 1410 |
} |
| 1411 |
|
| 1412 |
override PostBlitDeclaration syntaxCopy(Dsymbol s) |
| 1413 |
{ |
| 1414 |
assert(!s); |
| 1415 |
auto dd = new PostBlitDeclaration(loc, endloc, storage_class, ident); |
| 1416 |
FuncDeclaration.syntaxCopy(dd); |
| 1417 |
return dd; |
| 1418 |
} |
| 1419 |
|
| 1420 |
override bool isVirtual() const |
| 1421 |
{ |
| 1422 |
return false; |
| 1423 |
} |
| 1424 |
|
| 1425 |
override bool addPreInvariant() |
| 1426 |
{ |
| 1427 |
return false; |
| 1428 |
} |
| 1429 |
|
| 1430 |
override bool addPostInvariant() |
| 1431 |
{ |
| 1432 |
return (isThis() && vthis && global.params.useInvariants == CHECKENABLE.on); |
| 1433 |
} |
| 1434 |
|
| 1435 |
override bool overloadInsert(Dsymbol s) |
| 1436 |
{ |
| 1437 |
return false; // cannot overload postblits |
| 1438 |
} |
| 1439 |
|
| 1440 |
override void accept(Visitor v) |
| 1441 |
{ |
| 1442 |
v.visit(this); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
/*********************************************************** |
| 1447 |
*/ |
| 1448 |
extern (C++) final class DtorDeclaration : FuncDeclaration |
| 1449 |
{ |
| 1450 |
extern (D) this(Loc loc, Loc endloc) |
| 1451 |
{ |
| 1452 |
super(loc, endloc, Id.dtor, STC.none, null); |
| 1453 |
this.dsym = DSYM.dtorDeclaration; |
| 1454 |
} |
| 1455 |
|
| 1456 |
extern (D) this(Loc loc, Loc endloc, STC stc, Identifier id) |
| 1457 |
{ |
| 1458 |
super(loc, endloc, id, stc, null); |
| 1459 |
this.dsym = DSYM.dtorDeclaration; |
| 1460 |
} |
| 1461 |
|
| 1462 |
override DtorDeclaration syntaxCopy(Dsymbol s) |
| 1463 |
{ |
| 1464 |
assert(!s); |
| 1465 |
auto dd = new DtorDeclaration(loc, endloc, storage_class, ident); |
| 1466 |
FuncDeclaration.syntaxCopy(dd); |
| 1467 |
return dd; |
| 1468 |
} |
| 1469 |
|
| 1470 |
override const(char)* kind() const |
| 1471 |
{ |
| 1472 |
return "destructor"; |
| 1473 |
} |
| 1474 |
|
| 1475 |
override bool isVirtual() const |
| 1476 |
{ |
| 1477 |
// D dtor's don't get put into the vtbl[] |
| 1478 |
// this is a hack so that extern(C++) destructors report as virtual, which are manually added to the vtable |
| 1479 |
return vtblIndex != -1; |
| 1480 |
} |
| 1481 |
|
| 1482 |
override bool addPreInvariant() |
| 1483 |
{ |
| 1484 |
return (isThis() && vthis && global.params.useInvariants == CHECKENABLE.on); |
| 1485 |
} |
| 1486 |
|
| 1487 |
override bool addPostInvariant() |
| 1488 |
{ |
| 1489 |
return false; |
| 1490 |
} |
| 1491 |
|
| 1492 |
override bool overloadInsert(Dsymbol s) |
| 1493 |
{ |
| 1494 |
return false; // cannot overload destructors |
| 1495 |
} |
| 1496 |
|
| 1497 |
override void accept(Visitor v) |
| 1498 |
{ |
| 1499 |
v.visit(this); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
/*********************************************************** |
| 1504 |
*/ |
| 1505 |
extern (C++) class StaticCtorDeclaration : FuncDeclaration |
| 1506 |
{ |
| 1507 |
extern (D) this(Loc loc, Loc endloc, STC stc) |
| 1508 |
{ |
| 1509 |
super(loc, endloc, Identifier.generateIdWithLoc("_staticCtor", loc), STC.static_ | stc, null); |
| 1510 |
this.dsym = DSYM.staticCtorDeclaration; |
| 1511 |
} |
| 1512 |
|
| 1513 |
extern (D) this(Loc loc, Loc endloc, string name, STC stc) |
| 1514 |
{ |
| 1515 |
super(loc, endloc, Identifier.generateIdWithLoc(name, loc), STC.static_ | stc, null); |
| 1516 |
this.dsym = DSYM.staticCtorDeclaration; |
| 1517 |
} |
| 1518 |
|
| 1519 |
override StaticCtorDeclaration syntaxCopy(Dsymbol s) |
| 1520 |
{ |
| 1521 |
assert(!s); |
| 1522 |
auto scd = new StaticCtorDeclaration(loc, endloc, storage_class); |
| 1523 |
FuncDeclaration.syntaxCopy(scd); |
| 1524 |
return scd; |
| 1525 |
} |
| 1526 |
|
| 1527 |
override final inout(AggregateDeclaration) isThis() inout @nogc nothrow pure @safe |
| 1528 |
{ |
| 1529 |
return null; |
| 1530 |
} |
| 1531 |
|
| 1532 |
override final bool isVirtual() const @nogc nothrow pure @safe |
| 1533 |
{ |
| 1534 |
return false; |
| 1535 |
} |
| 1536 |
|
| 1537 |
override final bool addPreInvariant() @nogc nothrow pure @safe |
| 1538 |
{ |
| 1539 |
return false; |
| 1540 |
} |
| 1541 |
|
| 1542 |
override final bool addPostInvariant() @nogc nothrow pure @safe |
| 1543 |
{ |
| 1544 |
return false; |
| 1545 |
} |
| 1546 |
|
| 1547 |
override void accept(Visitor v) |
| 1548 |
{ |
| 1549 |
v.visit(this); |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
/*********************************************************** |
| 1554 |
*/ |
| 1555 |
extern (C++) final class SharedStaticCtorDeclaration : StaticCtorDeclaration |
| 1556 |
{ |
| 1557 |
/// Exclude this constructor from cyclic dependency check |
| 1558 |
bool standalone; |
| 1559 |
|
| 1560 |
extern (D) this(Loc loc, Loc endloc, STC stc) |
| 1561 |
{ |
| 1562 |
super(loc, endloc, "_sharedStaticCtor", stc); |
| 1563 |
this.dsym = DSYM.sharedStaticCtorDeclaration; |
| 1564 |
} |
| 1565 |
|
| 1566 |
override SharedStaticCtorDeclaration syntaxCopy(Dsymbol s) |
| 1567 |
{ |
| 1568 |
assert(!s); |
| 1569 |
auto scd = new SharedStaticCtorDeclaration(loc, endloc, storage_class); |
| 1570 |
FuncDeclaration.syntaxCopy(scd); |
| 1571 |
return scd; |
| 1572 |
} |
| 1573 |
|
| 1574 |
override void accept(Visitor v) |
| 1575 |
{ |
| 1576 |
v.visit(this); |
| 1577 |
} |
| 1578 |
} |
| 1579 |
|
| 1580 |
/*********************************************************** |
| 1581 |
*/ |
| 1582 |
extern (C++) class StaticDtorDeclaration : FuncDeclaration |
| 1583 |
{ |
| 1584 |
VarDeclaration vgate; // 'gate' variable |
| 1585 |
|
| 1586 |
extern (D) this(Loc loc, Loc endloc, STC stc) |
| 1587 |
{ |
| 1588 |
super(loc, endloc, Identifier.generateIdWithLoc("_staticDtor", loc), STC.static_ | stc, null); |
| 1589 |
this.dsym = DSYM.staticDtorDeclaration; |
| 1590 |
} |
| 1591 |
|
| 1592 |
extern (D) this(Loc loc, Loc endloc, string name, STC stc) |
| 1593 |
{ |
| 1594 |
super(loc, endloc, Identifier.generateIdWithLoc(name, loc), STC.static_ | stc, null); |
| 1595 |
this.dsym = DSYM.staticDtorDeclaration; |
| 1596 |
} |
| 1597 |
|
| 1598 |
override StaticDtorDeclaration syntaxCopy(Dsymbol s) |
| 1599 |
{ |
| 1600 |
assert(!s); |
| 1601 |
auto sdd = new StaticDtorDeclaration(loc, endloc, storage_class); |
| 1602 |
FuncDeclaration.syntaxCopy(sdd); |
| 1603 |
return sdd; |
| 1604 |
} |
| 1605 |
|
| 1606 |
override final inout(AggregateDeclaration) isThis() inout |
| 1607 |
{ |
| 1608 |
return null; |
| 1609 |
} |
| 1610 |
|
| 1611 |
override final bool isVirtual() const |
| 1612 |
{ |
| 1613 |
return false; |
| 1614 |
} |
| 1615 |
|
| 1616 |
override final bool addPreInvariant() |
| 1617 |
{ |
| 1618 |
return false; |
| 1619 |
} |
| 1620 |
|
| 1621 |
override final bool addPostInvariant() |
| 1622 |
{ |
| 1623 |
return false; |
| 1624 |
} |
| 1625 |
|
| 1626 |
override void accept(Visitor v) |
| 1627 |
{ |
| 1628 |
v.visit(this); |
| 1629 |
} |
| 1630 |
} |
| 1631 |
|
| 1632 |
/*********************************************************** |
| 1633 |
*/ |
| 1634 |
extern (C++) final class SharedStaticDtorDeclaration : StaticDtorDeclaration |
| 1635 |
{ |
| 1636 |
extern (D) this(Loc loc, Loc endloc, STC stc) |
| 1637 |
{ |
| 1638 |
super(loc, endloc, "_sharedStaticDtor", stc); |
| 1639 |
this.dsym = DSYM.sharedStaticDtorDeclaration; |
| 1640 |
} |
| 1641 |
|
| 1642 |
override SharedStaticDtorDeclaration syntaxCopy(Dsymbol s) |
| 1643 |
{ |
| 1644 |
assert(!s); |
| 1645 |
auto sdd = new SharedStaticDtorDeclaration(loc, endloc, storage_class); |
| 1646 |
FuncDeclaration.syntaxCopy(sdd); |
| 1647 |
return sdd; |
| 1648 |
} |
| 1649 |
|
| 1650 |
override void accept(Visitor v) |
| 1651 |
{ |
| 1652 |
v.visit(this); |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
/*********************************************************** |
| 1657 |
*/ |
| 1658 |
extern (C++) final class InvariantDeclaration : FuncDeclaration |
| 1659 |
{ |
| 1660 |
extern (D) this(Loc loc, Loc endloc, STC stc, Identifier id, Statement fbody) |
| 1661 |
{ |
| 1662 |
// Make a unique invariant for now; we'll fix it up as we add it to the aggregate invariant list. |
| 1663 |
super(loc, endloc, id ? id : Identifier.generateId("__invariant"), stc, null); |
| 1664 |
this.fbody = fbody; |
| 1665 |
this.dsym = DSYM.invariantDeclaration; |
| 1666 |
} |
| 1667 |
|
| 1668 |
override InvariantDeclaration syntaxCopy(Dsymbol s) |
| 1669 |
{ |
| 1670 |
assert(!s); |
| 1671 |
auto id = new InvariantDeclaration(loc, endloc, storage_class, null, null); |
| 1672 |
FuncDeclaration.syntaxCopy(id); |
| 1673 |
return id; |
| 1674 |
} |
| 1675 |
|
| 1676 |
override bool isVirtual() const |
| 1677 |
{ |
| 1678 |
return false; |
| 1679 |
} |
| 1680 |
|
| 1681 |
override bool addPreInvariant() |
| 1682 |
{ |
| 1683 |
return false; |
| 1684 |
} |
| 1685 |
|
| 1686 |
override bool addPostInvariant() |
| 1687 |
{ |
| 1688 |
return false; |
| 1689 |
} |
| 1690 |
|
| 1691 |
override void accept(Visitor v) |
| 1692 |
{ |
| 1693 |
v.visit(this); |
| 1694 |
} |
| 1695 |
|
| 1696 |
extern (D) void fixupInvariantIdent(size_t offset) |
| 1697 |
{ |
| 1698 |
OutBuffer idBuf; |
| 1699 |
idBuf.writestring("__invariant"); |
| 1700 |
idBuf.print(offset); |
| 1701 |
|
| 1702 |
ident = Identifier.idPool(idBuf[]); |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
/*********************************************************** |
| 1708 |
*/ |
| 1709 |
extern (C++) final class UnitTestDeclaration : FuncDeclaration |
| 1710 |
{ |
| 1711 |
char* codedoc; // for documented unittest |
| 1712 |
|
| 1713 |
// toObjFile() these nested functions after this one |
| 1714 |
FuncDeclarations deferredNested; |
| 1715 |
|
| 1716 |
extern (D) this(Loc loc, Loc endloc, STC stc, char* codedoc) |
| 1717 |
{ |
| 1718 |
super(loc, endloc, Identifier.generateIdWithLoc("__unittest", loc), stc, null); |
| 1719 |
this.codedoc = codedoc; |
| 1720 |
this.dsym = DSYM.unitTestDeclaration; |
| 1721 |
} |
| 1722 |
|
| 1723 |
override UnitTestDeclaration syntaxCopy(Dsymbol s) |
| 1724 |
{ |
| 1725 |
assert(!s); |
| 1726 |
auto utd = new UnitTestDeclaration(loc, endloc, storage_class, codedoc); |
| 1727 |
FuncDeclaration.syntaxCopy(utd); |
| 1728 |
return utd; |
| 1729 |
} |
| 1730 |
|
| 1731 |
override inout(AggregateDeclaration) isThis() inout |
| 1732 |
{ |
| 1733 |
return null; |
| 1734 |
} |
| 1735 |
|
| 1736 |
override bool isVirtual() const |
| 1737 |
{ |
| 1738 |
return false; |
| 1739 |
} |
| 1740 |
|
| 1741 |
override bool addPreInvariant() |
| 1742 |
{ |
| 1743 |
return false; |
| 1744 |
} |
| 1745 |
|
| 1746 |
override bool addPostInvariant() |
| 1747 |
{ |
| 1748 |
return false; |
| 1749 |
} |
| 1750 |
|
| 1751 |
override void accept(Visitor v) |
| 1752 |
{ |
| 1753 |
v.visit(this); |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
/*********************************************************** |
| 1758 |
*/ |
| 1759 |
extern (C++) final class NewDeclaration : FuncDeclaration |
| 1760 |
{ |
| 1761 |
extern (D) this(Loc loc, STC stc) |
| 1762 |
{ |
| 1763 |
super(loc, Loc.initial, Id.classNew, STC.static_ | stc, null); |
| 1764 |
this.dsym = DSYM.newDeclaration; |
| 1765 |
} |
| 1766 |
|
| 1767 |
override NewDeclaration syntaxCopy(Dsymbol s) |
| 1768 |
{ |
| 1769 |
assert(!s); |
| 1770 |
auto f = new NewDeclaration(loc, storage_class); |
| 1771 |
FuncDeclaration.syntaxCopy(f); |
| 1772 |
return f; |
| 1773 |
} |
| 1774 |
|
| 1775 |
override const(char)* kind() const |
| 1776 |
{ |
| 1777 |
return "allocator"; |
| 1778 |
} |
| 1779 |
|
| 1780 |
override bool isVirtual() const |
| 1781 |
{ |
| 1782 |
return false; |
| 1783 |
} |
| 1784 |
|
| 1785 |
override bool addPreInvariant() |
| 1786 |
{ |
| 1787 |
return false; |
| 1788 |
} |
| 1789 |
|
| 1790 |
override bool addPostInvariant() |
| 1791 |
{ |
| 1792 |
return false; |
| 1793 |
} |
| 1794 |
|
| 1795 |
override void accept(Visitor v) |
| 1796 |
{ |
| 1797 |
v.visit(this); |
| 1798 |
} |
| 1799 |
} |
| 1800 |
|
| 1801 |
/// Stores a reason why a function failed to infer a function attribute like `@safe` or `pure` |
| 1802 |
/// |
| 1803 |
/// Has two modes: |
| 1804 |
/// - a regular safety error, stored in `action` |
| 1805 |
/// - a call to a function without the attribute, which is a special case, because in that case, |
| 1806 |
/// that function might recursively also have a `AttributeViolation`. This way, in case |
| 1807 |
/// of a big call stack, the error can go down all the way to the root cause. |
| 1808 |
struct AttributeViolation |
| 1809 |
{ |
| 1810 |
Loc loc; /// location of error |
| 1811 |
|
| 1812 |
FuncDeclaration fd; /// function is the focus of the violation |
| 1813 |
|
| 1814 |
// -- OR -- |
| 1815 |
|
| 1816 |
string action; /// Action that made the attribute fail to get inferred |
| 1817 |
|
| 1818 |
this(Loc loc, FuncDeclaration fd) { this.loc = loc; this.fd = fd; } |
| 1819 |
|
| 1820 |
this(Loc loc, const(char)* fmt, RootObject[] args) |
| 1821 |
{ |
| 1822 |
this.loc = loc; |
| 1823 |
assert(args.length <= 4); // expand if necessary |
| 1824 |
OutBuffer buf; |
| 1825 |
buf.printf(fmt, |
| 1826 |
args.length > 0 && args[0] ? args[0].toErrMsg() : "", |
| 1827 |
args.length > 1 && args[1] ? args[1].toErrMsg() : "", |
| 1828 |
args.length > 2 && args[2] ? args[2].toErrMsg() : "", |
| 1829 |
args.length > 3 && args[3] ? args[3].toErrMsg() : "", |
| 1830 |
); |
| 1831 |
this.action = buf.extractSlice(); |
| 1832 |
} |
| 1833 |
} |