| 1 |
/** |
| 2 |
* Evaluate compile-time conditionals, such as `static if` `version` and `debug`. |
| 3 |
* |
| 4 |
* Specification: $(LINK2 https://dlang.org/spec/version.html, Conditional Compilation) |
| 5 |
* |
| 6 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 7 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 8 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 9 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/cond.d, _cond.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_cond.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/cond.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.cond; |
| 15 |
|
| 16 |
import core.stdc.string; |
| 17 |
import dmd.arraytypes; |
| 18 |
import dmd.astenums; |
| 19 |
import dmd.ast_node; |
| 20 |
import dmd.dcast; |
| 21 |
import dmd.dinterpret; |
| 22 |
import dmd.dmodule; |
| 23 |
import dmd.dscope; |
| 24 |
import dmd.dsymbol; |
| 25 |
import dmd.errors; |
| 26 |
import dmd.expression; |
| 27 |
import dmd.expressionsem : evalStaticCondition; |
| 28 |
import dmd.globals; |
| 29 |
import dmd.identifier; |
| 30 |
import dmd.location; |
| 31 |
import dmd.mtype; |
| 32 |
import dmd.optimize; |
| 33 |
import dmd.typesem; |
| 34 |
import dmd.common.outbuffer; |
| 35 |
import dmd.rootobject; |
| 36 |
import dmd.root.string; |
| 37 |
import dmd.tokens; |
| 38 |
import dmd.utils; |
| 39 |
import dmd.visitor; |
| 40 |
import dmd.id; |
| 41 |
import dmd.statement; |
| 42 |
import dmd.declaration; |
| 43 |
import dmd.dstruct; |
| 44 |
import dmd.func; |
| 45 |
|
| 46 |
/*********************************************************** |
| 47 |
*/ |
| 48 |
|
| 49 |
enum Include : ubyte |
| 50 |
{ |
| 51 |
notComputed, /// not computed yet |
| 52 |
yes, /// include the conditional code |
| 53 |
no, /// do not include the conditional code |
| 54 |
} |
| 55 |
|
| 56 |
extern (C++) abstract class Condition : ASTNode |
| 57 |
{ |
| 58 |
Loc loc; |
| 59 |
|
| 60 |
Include inc; |
| 61 |
|
| 62 |
override final DYNCAST dyncast() const |
| 63 |
{ |
| 64 |
return DYNCAST.condition; |
| 65 |
} |
| 66 |
|
| 67 |
extern (D) this(Loc loc) @safe |
| 68 |
{ |
| 69 |
this.loc = loc; |
| 70 |
} |
| 71 |
|
| 72 |
abstract Condition syntaxCopy(); |
| 73 |
|
| 74 |
abstract int include(Scope* sc); |
| 75 |
|
| 76 |
inout(DebugCondition) isDebugCondition() inout |
| 77 |
{ |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
inout(VersionCondition) isVersionCondition() inout |
| 82 |
{ |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
inout(StaticIfCondition) isStaticIfCondition() inout |
| 87 |
{ |
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
override void accept(Visitor v) |
| 92 |
{ |
| 93 |
v.visit(this); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/*********************************************************** |
| 98 |
* Implements common functionality for StaticForeachDeclaration and |
| 99 |
* StaticForeachStatement This performs the necessary lowerings before |
| 100 |
* dmd.statementsem.makeTupleForeach can be used to expand the |
| 101 |
* corresponding `static foreach` declaration or statement. |
| 102 |
*/ |
| 103 |
|
| 104 |
extern (C++) final class StaticForeach : RootObject |
| 105 |
{ |
| 106 |
extern(D) static immutable tupleFieldName = "tuple"; // used in lowering |
| 107 |
|
| 108 |
Loc loc; |
| 109 |
|
| 110 |
/*************** |
| 111 |
* Not `null` iff the `static foreach` is over an aggregate. In |
| 112 |
* this case, it contains the corresponding ForeachStatement. For |
| 113 |
* StaticForeachDeclaration, the body is `null`. |
| 114 |
*/ |
| 115 |
ForeachStatement aggrfe; |
| 116 |
/*************** |
| 117 |
* Not `null` iff the `static foreach` is over a range. Exactly |
| 118 |
* one of the `aggrefe` and `rangefe` fields is not null. See |
| 119 |
* `aggrfe` field for more details. |
| 120 |
*/ |
| 121 |
ForeachRangeStatement rangefe; |
| 122 |
|
| 123 |
/*************** |
| 124 |
* true if it is necessary to expand a tuple into multiple |
| 125 |
* variables (see lowerNonArrayAggregate). |
| 126 |
*/ |
| 127 |
bool needExpansion = false; |
| 128 |
|
| 129 |
extern (D) this(Loc loc, ForeachStatement aggrfe, ForeachRangeStatement rangefe) @safe |
| 130 |
{ |
| 131 |
assert(!!aggrfe ^ !!rangefe); |
| 132 |
|
| 133 |
this.loc = loc; |
| 134 |
this.aggrfe = aggrfe; |
| 135 |
this.rangefe = rangefe; |
| 136 |
} |
| 137 |
|
| 138 |
extern (D) StaticForeach syntaxCopy() |
| 139 |
{ |
| 140 |
return new StaticForeach( |
| 141 |
loc, |
| 142 |
aggrfe ? aggrfe.syntaxCopy() : null, |
| 143 |
rangefe ? rangefe.syntaxCopy() : null |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/***************************************** |
| 148 |
* Wrap a statement into a function literal and call it. |
| 149 |
* |
| 150 |
* Params: |
| 151 |
* loc = The source location. |
| 152 |
* s = The statement. |
| 153 |
* Returns: |
| 154 |
* AST of the expression `(){ s; }()` with location loc. |
| 155 |
*/ |
| 156 |
extern(D) Expression wrapAndCall(Loc loc, Statement s) |
| 157 |
{ |
| 158 |
auto tf = new TypeFunction(ParameterList(), null, LINK.default_, STC.none); |
| 159 |
auto fd = new FuncLiteralDeclaration(loc, loc, tf, TOK.reserved, null); |
| 160 |
fd.fbody = s; |
| 161 |
auto fe = new FuncExp(loc, fd); |
| 162 |
auto ce = new CallExp(loc, fe, new Expressions()); |
| 163 |
return ce; |
| 164 |
} |
| 165 |
|
| 166 |
/***************************************** |
| 167 |
* Create a `foreach` statement from `aggrefe/rangefe` with given |
| 168 |
* `foreach` variables and body `s`. |
| 169 |
* |
| 170 |
* Params: |
| 171 |
* loc = The source location. |
| 172 |
* parameters = The foreach variables. |
| 173 |
* s = The `foreach` body. |
| 174 |
* Returns: |
| 175 |
* `foreach (parameters; aggregate) s;` or |
| 176 |
* `foreach (parameters; lower .. upper) s;` |
| 177 |
* Where aggregate/lower, upper are as for the current StaticForeach. |
| 178 |
*/ |
| 179 |
extern(D) Statement createForeach(Loc loc, Parameters* parameters, Statement s) |
| 180 |
{ |
| 181 |
if (aggrfe) |
| 182 |
{ |
| 183 |
return new ForeachStatement(loc, aggrfe.op, parameters, aggrfe.aggr, s, loc); |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
assert(rangefe && parameters.length == 1); |
| 188 |
return new ForeachRangeStatement(loc, rangefe.op, (*parameters)[0], rangefe.lwr, rangefe.upr, s, loc); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/***************************************** |
| 193 |
* For a `static foreach` with multiple loop variables, the |
| 194 |
* aggregate is lowered to an array of tuples. As D does not have |
| 195 |
* built-in tuples, we need a suitable tuple type. This generates |
| 196 |
* a `struct` that serves as the tuple type. This type is only |
| 197 |
* used during CTFE and hence its typeinfo will not go to the |
| 198 |
* object file. |
| 199 |
* |
| 200 |
* Params: |
| 201 |
* loc = The source location. |
| 202 |
* e = The expressions we wish to store in the tuple. |
| 203 |
* sc = The current scope. |
| 204 |
* Returns: |
| 205 |
* A struct type of the form |
| 206 |
* struct Tuple |
| 207 |
* { |
| 208 |
* typeof(AliasSeq!(e)) tuple; |
| 209 |
* } |
| 210 |
*/ |
| 211 |
|
| 212 |
extern(D) TypeStruct createTupleType(Loc loc, Expressions* e, Scope* sc) |
| 213 |
{ // TODO: move to druntime? |
| 214 |
auto sid = Identifier.generateId("Tuple"); |
| 215 |
auto sdecl = new StructDeclaration(loc, sid, false); |
| 216 |
sdecl.storage_class |= STC.static_; |
| 217 |
sdecl.members = new Dsymbols(); |
| 218 |
auto fid = Identifier.idPool(tupleFieldName); |
| 219 |
auto ty = new TypeTypeof(loc, new TupleExp(loc, e)); |
| 220 |
sdecl.members.push(new VarDeclaration(loc, ty, fid, null, STC.none)); |
| 221 |
auto r = cast(TypeStruct)sdecl.type; |
| 222 |
if (global.params.useTypeInfo && Type.dtypeinfo) |
| 223 |
r.vtinfo = TypeInfoStructDeclaration.create(r); // prevent typeinfo from going to object file |
| 224 |
return r; |
| 225 |
} |
| 226 |
|
| 227 |
/***************************************** |
| 228 |
* Create the AST for an instantiation of a suitable tuple type. |
| 229 |
* |
| 230 |
* Params: |
| 231 |
* loc = The source location. |
| 232 |
* type = A Tuple type, created with createTupleType. |
| 233 |
* e = The expressions we wish to store in the tuple. |
| 234 |
* Returns: |
| 235 |
* An AST for the expression `Tuple(e)`. |
| 236 |
*/ |
| 237 |
|
| 238 |
extern(D) Expression createTuple(Loc loc, TypeStruct type, Expressions* e) @safe |
| 239 |
{ // TODO: move to druntime? |
| 240 |
return new CallExp(loc, new TypeExp(loc, type), e); |
| 241 |
} |
| 242 |
|
| 243 |
/***************************************** |
| 244 |
* Returns: |
| 245 |
* `true` iff ready to call `dmd.statementsem.makeTupleForeach`. |
| 246 |
*/ |
| 247 |
extern(D) bool ready() |
| 248 |
{ |
| 249 |
return aggrfe && aggrfe.aggr && aggrfe.aggr.type && aggrfe.aggr.type.toBasetype().ty == Ttuple; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/*********************************************************** |
| 254 |
*/ |
| 255 |
extern (C++) class DVCondition : Condition |
| 256 |
{ |
| 257 |
Identifier ident; |
| 258 |
Module mod; |
| 259 |
|
| 260 |
extern (D) this(Loc loc, Module mod, Identifier ident) @safe |
| 261 |
{ |
| 262 |
super(loc); |
| 263 |
this.mod = mod; |
| 264 |
this.ident = ident; |
| 265 |
} |
| 266 |
|
| 267 |
override final DVCondition syntaxCopy() |
| 268 |
{ |
| 269 |
return this; // don't need to copy |
| 270 |
} |
| 271 |
|
| 272 |
override void accept(Visitor v) |
| 273 |
{ |
| 274 |
v.visit(this); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/*********************************************************** |
| 279 |
*/ |
| 280 |
extern (C++) final class DebugCondition : DVCondition |
| 281 |
{ |
| 282 |
/** |
| 283 |
* Add an user-supplied identifier to the list of global debug identifiers |
| 284 |
* |
| 285 |
* Can be called from either the driver or a `debug = Ident;` statement. |
| 286 |
* Unlike version identifier, there isn't any reserved debug identifier |
| 287 |
* so no validation takes place. |
| 288 |
* |
| 289 |
* Params: |
| 290 |
* ident = identifier to add |
| 291 |
*/ |
| 292 |
deprecated("Kept for C++ compat - Use the string overload instead") |
| 293 |
static void addGlobalIdent(const(char)* ident) |
| 294 |
{ |
| 295 |
addGlobalIdent(ident[0 .. ident.strlen]); |
| 296 |
} |
| 297 |
|
| 298 |
/// Ditto |
| 299 |
extern(D) static void addGlobalIdent(string ident) |
| 300 |
{ |
| 301 |
// Overload necessary for string literals |
| 302 |
addGlobalIdent(cast(const(char)[])ident); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
/// Ditto |
| 307 |
extern(D) static void addGlobalIdent(const(char)[] ident) |
| 308 |
{ |
| 309 |
global.debugids.push(Identifier.idPool(ident)); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Instantiate a new `DebugCondition` |
| 315 |
* |
| 316 |
* Params: |
| 317 |
* mod = Module this node belongs to |
| 318 |
* ident = Identifier required for this condition to pass. |
| 319 |
* If `null`, this conditiion will use an integer level. |
| 320 |
* loc = Location in the source file |
| 321 |
*/ |
| 322 |
extern (D) this(Loc loc, Module mod, Identifier ident) @safe |
| 323 |
{ |
| 324 |
super(loc, mod, ident); |
| 325 |
} |
| 326 |
|
| 327 |
override int include(Scope* sc) |
| 328 |
{ |
| 329 |
//printf("DebugCondition::include() level = %d, debuglevel = %d\n", level, global.params.debuglevel); |
| 330 |
if (inc != Include.notComputed) |
| 331 |
{ |
| 332 |
return inc == Include.yes; |
| 333 |
} |
| 334 |
inc = Include.no; |
| 335 |
bool definedInModule = false; |
| 336 |
if (ident) |
| 337 |
{ |
| 338 |
if (mod.debugids && findCondition(*mod.debugids, ident)) |
| 339 |
{ |
| 340 |
inc = Include.yes; |
| 341 |
definedInModule = true; |
| 342 |
} |
| 343 |
else if (findCondition(global.debugids, ident)) |
| 344 |
inc = Include.yes; |
| 345 |
else |
| 346 |
{ |
| 347 |
if (!mod.debugidsNot) |
| 348 |
mod.debugidsNot = new Identifiers(); |
| 349 |
mod.debugidsNot.push(ident); |
| 350 |
} |
| 351 |
} |
| 352 |
else if (global.params.debugEnabled) |
| 353 |
inc = Include.yes; |
| 354 |
|
| 355 |
if (!definedInModule) |
| 356 |
printDepsConditional(sc, this, "depsDebug "); |
| 357 |
return (inc == Include.yes); |
| 358 |
} |
| 359 |
|
| 360 |
override inout(DebugCondition) isDebugCondition() inout |
| 361 |
{ |
| 362 |
return this; |
| 363 |
} |
| 364 |
|
| 365 |
override void accept(Visitor v) |
| 366 |
{ |
| 367 |
v.visit(this); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Node to represent a version condition |
| 373 |
* |
| 374 |
* A version condition is of the form: |
| 375 |
* --- |
| 376 |
* version (Identifier) |
| 377 |
* --- |
| 378 |
* In user code. |
| 379 |
* This class also provides means to add version identifier |
| 380 |
* to the list of global (cross module) identifiers. |
| 381 |
*/ |
| 382 |
extern (C++) final class VersionCondition : DVCondition |
| 383 |
{ |
| 384 |
/** |
| 385 |
* Check if a given version identifier is reserved. |
| 386 |
* |
| 387 |
* Params: |
| 388 |
* ident = identifier being checked |
| 389 |
* |
| 390 |
* Returns: |
| 391 |
* `true` if it is reserved, `false` otherwise |
| 392 |
*/ |
| 393 |
extern(D) private static bool isReserved(const(char)[] ident) @safe |
| 394 |
{ |
| 395 |
// This list doesn't include "D_*" versions, see the last return |
| 396 |
switch (ident) |
| 397 |
{ |
| 398 |
case "AArch64": |
| 399 |
case "AIX": |
| 400 |
case "all": |
| 401 |
case "Alpha": |
| 402 |
case "Alpha_HardFloat": |
| 403 |
case "Alpha_SoftFloat": |
| 404 |
case "Android": |
| 405 |
case "ARM": |
| 406 |
case "ARM_HardFloat": |
| 407 |
case "ARM_SoftFloat": |
| 408 |
case "ARM_SoftFP": |
| 409 |
case "ARM_Thumb": |
| 410 |
case "AsmJS": |
| 411 |
case "assert": |
| 412 |
case "AVR": |
| 413 |
case "BigEndian": |
| 414 |
case "BSD": |
| 415 |
case "CppRuntime_LLVM": |
| 416 |
case "CppRuntime_DigitalMars": |
| 417 |
case "CppRuntime_GNU": |
| 418 |
case "CppRuntime_Microsoft": |
| 419 |
case "CppRuntime_Sun": |
| 420 |
case "CRuntime_Bionic": |
| 421 |
case "CRuntime_DigitalMars": |
| 422 |
case "CRuntime_Glibc": |
| 423 |
case "CRuntime_Microsoft": |
| 424 |
case "CRuntime_Musl": |
| 425 |
case "CRuntime_Newlib": |
| 426 |
case "CRuntime_UClibc": |
| 427 |
case "CRuntime_WASI": |
| 428 |
case "Cygwin": |
| 429 |
case "DigitalMars": |
| 430 |
case "DragonFlyBSD": |
| 431 |
case "Emscripten": |
| 432 |
case "ELFv1": |
| 433 |
case "ELFv2": |
| 434 |
case "Epiphany": |
| 435 |
case "FreeBSD": |
| 436 |
case "FreeStanding": |
| 437 |
case "GNU": |
| 438 |
case "Haiku": |
| 439 |
case "HPPA": |
| 440 |
case "HPPA64": |
| 441 |
case "Hurd": |
| 442 |
case "IA64": |
| 443 |
case "iOS": |
| 444 |
case "LDC": |
| 445 |
case "linux": |
| 446 |
case "LittleEndian": |
| 447 |
case "LoongArch32": |
| 448 |
case "LoongArch64": |
| 449 |
case "LoongArch_HardFloat": |
| 450 |
case "LoongArch_SoftFloat": |
| 451 |
case "MinGW": |
| 452 |
case "MIPS32": |
| 453 |
case "MIPS64": |
| 454 |
case "MIPS_EABI": |
| 455 |
case "MIPS_HardFloat": |
| 456 |
case "MIPS_N32": |
| 457 |
case "MIPS_N64": |
| 458 |
case "MIPS_O32": |
| 459 |
case "MIPS_O64": |
| 460 |
case "MIPS_SoftFloat": |
| 461 |
case "MSP430": |
| 462 |
case "NetBSD": |
| 463 |
case "none": |
| 464 |
case "NVPTX": |
| 465 |
case "NVPTX64": |
| 466 |
case "OpenBSD": |
| 467 |
case "OSX": |
| 468 |
case "PlayStation": |
| 469 |
case "PlayStation4": |
| 470 |
case "Posix": |
| 471 |
case "PPC": |
| 472 |
case "PPC64": |
| 473 |
case "PPC_HardFloat": |
| 474 |
case "PPC_SoftFloat": |
| 475 |
case "RISCV32": |
| 476 |
case "RISCV64": |
| 477 |
case "S390": |
| 478 |
case "S390X": |
| 479 |
case "SDC": |
| 480 |
case "SH": |
| 481 |
case "SkyOS": |
| 482 |
case "Solaris": |
| 483 |
case "SPARC": |
| 484 |
case "SPARC64": |
| 485 |
case "SPARC_HardFloat": |
| 486 |
case "SPARC_SoftFloat": |
| 487 |
case "SPARC_V8Plus": |
| 488 |
case "SystemZ": |
| 489 |
case "SysV3": |
| 490 |
case "SysV4": |
| 491 |
case "TVOS": |
| 492 |
case "unittest": |
| 493 |
case "VisionOS": |
| 494 |
case "WASI": |
| 495 |
case "WatchOS": |
| 496 |
case "WebAssembly": |
| 497 |
case "Win32": |
| 498 |
case "Win64": |
| 499 |
case "Windows": |
| 500 |
case "Xtensa": |
| 501 |
case "X86": |
| 502 |
case "X86_64": |
| 503 |
return true; |
| 504 |
|
| 505 |
default: |
| 506 |
// Anything that starts with "D_" is reserved |
| 507 |
return (ident.length >= 2 && ident[0 .. 2] == "D_"); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Raises an error if a version identifier is reserved. |
| 513 |
* |
| 514 |
* Called when setting a version identifier, e.g. `-version=identifier` |
| 515 |
* parameter to the compiler or `version = Foo` in user code. |
| 516 |
* |
| 517 |
* Params: |
| 518 |
* loc = Where the identifier is set |
| 519 |
* ident = identifier being checked (ident[$] must be '\0') |
| 520 |
*/ |
| 521 |
extern(D) static void checkReserved(Loc loc, const(char)[] ident) |
| 522 |
{ |
| 523 |
if (isReserved(ident)) |
| 524 |
error(loc, "version identifier `%s` is reserved and cannot be set", |
| 525 |
ident.ptr); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Add an user-supplied global identifier to the list |
| 530 |
* |
| 531 |
* Only called from the driver for `-version=Ident` parameters. |
| 532 |
* Will raise an error if the identifier is reserved. |
| 533 |
* |
| 534 |
* Params: |
| 535 |
* ident = identifier to add |
| 536 |
*/ |
| 537 |
deprecated("Kept for C++ compat - Use the string overload instead") |
| 538 |
static void addGlobalIdent(const(char)* ident) |
| 539 |
{ |
| 540 |
addGlobalIdent(ident[0 .. ident.strlen]); |
| 541 |
} |
| 542 |
|
| 543 |
/// Ditto |
| 544 |
extern(D) static void addGlobalIdent(string ident) |
| 545 |
{ |
| 546 |
// Overload necessary for string literals |
| 547 |
addGlobalIdent(cast(const(char)[])ident); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
/// Ditto |
| 552 |
extern(D) static void addGlobalIdent(const(char)[] ident) |
| 553 |
{ |
| 554 |
checkReserved(Loc.initial, ident); |
| 555 |
addPredefinedGlobalIdent(ident); |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Add any global identifier to the list, without checking |
| 560 |
* if it's predefined |
| 561 |
* |
| 562 |
* Only called from the driver after platform detection, |
| 563 |
* and internally. |
| 564 |
* |
| 565 |
* Params: |
| 566 |
* ident = identifier to add (ident[$] must be '\0') |
| 567 |
*/ |
| 568 |
deprecated("Kept for C++ compat - Use the string overload instead") |
| 569 |
static void addPredefinedGlobalIdent(const(char)* ident) |
| 570 |
{ |
| 571 |
addPredefinedGlobalIdent(ident.toDString()); |
| 572 |
} |
| 573 |
|
| 574 |
/// Ditto |
| 575 |
extern(D) static void addPredefinedGlobalIdent(string ident) |
| 576 |
{ |
| 577 |
// Forward: Overload necessary for string literal |
| 578 |
addPredefinedGlobalIdent(cast(const(char)[])ident); |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
/// Ditto |
| 583 |
extern(D) static void addPredefinedGlobalIdent(const(char)[] ident) |
| 584 |
{ |
| 585 |
global.versionids.push(Identifier.idPool(ident)); |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* Instantiate a new `VersionCondition` |
| 590 |
* |
| 591 |
* Params: |
| 592 |
* mod = Module this node belongs to |
| 593 |
* ident = Identifier required for this condition to pass. |
| 594 |
* If `null`, this conditiion will use an integer level. |
| 595 |
* loc = Location in the source file |
| 596 |
*/ |
| 597 |
extern (D) this(Loc loc, Module mod, Identifier ident) @safe |
| 598 |
{ |
| 599 |
super(loc, mod, ident); |
| 600 |
} |
| 601 |
|
| 602 |
override int include(Scope* sc) |
| 603 |
{ |
| 604 |
//printf("VersionCondition::include() level = %d, versionlevel = %d\n", level, global.params.versionlevel); |
| 605 |
//if (ident) printf("\tident = '%s'\n", ident.toChars()); |
| 606 |
if (inc != Include.notComputed) |
| 607 |
{ |
| 608 |
return inc == Include.yes; |
| 609 |
} |
| 610 |
|
| 611 |
inc = Include.no; |
| 612 |
bool definedInModule = false; |
| 613 |
if (ident) |
| 614 |
{ |
| 615 |
if (mod.versionids && findCondition(*mod.versionids, ident)) |
| 616 |
{ |
| 617 |
inc = Include.yes; |
| 618 |
definedInModule = true; |
| 619 |
} |
| 620 |
else if (findCondition(global.versionids, ident)) |
| 621 |
inc = Include.yes; |
| 622 |
else |
| 623 |
{ |
| 624 |
if (!mod.versionidsNot) |
| 625 |
mod.versionidsNot = new Identifiers(); |
| 626 |
mod.versionidsNot.push(ident); |
| 627 |
} |
| 628 |
} |
| 629 |
if (!definedInModule && |
| 630 |
(!ident || (!isReserved(ident.toString()) && ident != Id._unittest && ident != Id._assert))) |
| 631 |
{ |
| 632 |
printDepsConditional(sc, this, "depsVersion "); |
| 633 |
} |
| 634 |
return (inc == Include.yes); |
| 635 |
} |
| 636 |
|
| 637 |
override inout(VersionCondition) isVersionCondition() inout |
| 638 |
{ |
| 639 |
return this; |
| 640 |
} |
| 641 |
|
| 642 |
override void accept(Visitor v) |
| 643 |
{ |
| 644 |
v.visit(this); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
/*********************************************************** |
| 649 |
*/ |
| 650 |
extern (C++) final class StaticIfCondition : Condition |
| 651 |
{ |
| 652 |
Expression exp; |
| 653 |
|
| 654 |
extern (D) this(Loc loc, Expression exp) @safe |
| 655 |
{ |
| 656 |
super(loc); |
| 657 |
this.exp = exp; |
| 658 |
} |
| 659 |
|
| 660 |
override StaticIfCondition syntaxCopy() |
| 661 |
{ |
| 662 |
return new StaticIfCondition(loc, exp.syntaxCopy()); |
| 663 |
} |
| 664 |
|
| 665 |
override int include(Scope* sc) |
| 666 |
{ |
| 667 |
// printf("StaticIfCondition::include(sc = %p) this=%p inc = %d\n", sc, this, inc); |
| 668 |
|
| 669 |
int errorReturn() |
| 670 |
{ |
| 671 |
if (!global.gag) |
| 672 |
inc = Include.no; // so we don't see the error message again |
| 673 |
return 0; |
| 674 |
} |
| 675 |
|
| 676 |
if (inc != Include.notComputed) |
| 677 |
{ |
| 678 |
return inc == Include.yes; |
| 679 |
} |
| 680 |
|
| 681 |
if (!sc) |
| 682 |
{ |
| 683 |
error(loc, "`static if` conditional cannot be at global scope"); |
| 684 |
inc = Include.no; |
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
import dmd.staticcond; |
| 689 |
bool errors; |
| 690 |
|
| 691 |
bool result = evalStaticCondition(sc, exp, exp, errors); |
| 692 |
|
| 693 |
// Prevent repeated condition evaluation. |
| 694 |
// See: fail_compilation/fail7815.d |
| 695 |
if (inc != Include.notComputed) |
| 696 |
return (inc == Include.yes); |
| 697 |
if (errors) |
| 698 |
return errorReturn(); |
| 699 |
if (result) |
| 700 |
inc = Include.yes; |
| 701 |
else |
| 702 |
inc = Include.no; |
| 703 |
return (inc == Include.yes); |
| 704 |
} |
| 705 |
|
| 706 |
override void accept(Visitor v) |
| 707 |
{ |
| 708 |
v.visit(this); |
| 709 |
} |
| 710 |
|
| 711 |
override inout(StaticIfCondition) isStaticIfCondition() inout |
| 712 |
{ |
| 713 |
return this; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
/**************************************** |
| 719 |
* Find `ident` in an array of identifiers. |
| 720 |
* Params: |
| 721 |
* ids = array of identifiers |
| 722 |
* ident = identifier to search for |
| 723 |
* Returns: |
| 724 |
* true if found |
| 725 |
*/ |
| 726 |
bool findCondition(ref Identifiers ids, Identifier ident) @safe nothrow pure |
| 727 |
{ |
| 728 |
foreach (id; ids) |
| 729 |
{ |
| 730 |
if (id == ident) |
| 731 |
return true; |
| 732 |
} |
| 733 |
return false; |
| 734 |
} |
| 735 |
|
| 736 |
// Helper for printing dependency information |
| 737 |
private void printDepsConditional(Scope* sc, DVCondition condition, const(char)[] depType) |
| 738 |
{ |
| 739 |
if (!global.params.moduleDeps.buffer || global.params.moduleDeps.name) |
| 740 |
return; |
| 741 |
OutBuffer* ob = global.params.moduleDeps.buffer; |
| 742 |
Module imod = sc ? sc._module : condition.mod; |
| 743 |
if (!imod) |
| 744 |
return; |
| 745 |
ob.writestring(depType); |
| 746 |
ob.writestring(imod.toPrettyChars()); |
| 747 |
ob.writestring(" ("); |
| 748 |
escapePath(ob, imod.srcfile.toChars()); |
| 749 |
ob.writestring(") : "); |
| 750 |
if (condition.ident) |
| 751 |
ob.writestring(condition.ident.toString()); |
| 752 |
ob.writeByte('\n'); |
| 753 |
} |