| 1 |
/** |
| 2 |
* Does the semantic passes on enums. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/enumsem.d, _enumsem.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_enumsem.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/enumsem.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.enumsem; |
| 13 |
|
| 14 |
import core.stdc.stdio; |
| 15 |
import core.stdc.string; |
| 16 |
|
| 17 |
import dmd.aggregate; |
| 18 |
import dmd.aliasthis; |
| 19 |
import dmd.arraytypes; |
| 20 |
import dmd.astcodegen; |
| 21 |
import dmd.astenums; |
| 22 |
import dmd.attrib; |
| 23 |
import dmd.blockexit; |
| 24 |
import dmd.clone; |
| 25 |
import dmd.cond; |
| 26 |
import dmd.compiler; |
| 27 |
import dmd.dcast; |
| 28 |
import dmd.dclass; |
| 29 |
import dmd.declaration; |
| 30 |
import dmd.denum; |
| 31 |
import dmd.dimport; |
| 32 |
import dmd.dinterpret; |
| 33 |
import dmd.dmodule; |
| 34 |
import dmd.dscope; |
| 35 |
import dmd.dstruct; |
| 36 |
import dmd.dsymbol; |
| 37 |
import dmd.dsymbolsem; |
| 38 |
import dmd.dtemplate; |
| 39 |
import dmd.dversion; |
| 40 |
import dmd.errors; |
| 41 |
import dmd.escape; |
| 42 |
import dmd.expression; |
| 43 |
import dmd.expressionsem; |
| 44 |
import dmd.func; |
| 45 |
import dmd.funcsem; |
| 46 |
import dmd.globals; |
| 47 |
import dmd.id; |
| 48 |
import dmd.identifier; |
| 49 |
import dmd.importc; |
| 50 |
import dmd.init; |
| 51 |
import dmd.initsem; |
| 52 |
import dmd.intrange; |
| 53 |
import dmd.hdrgen; |
| 54 |
import dmd.location; |
| 55 |
import dmd.mtype; |
| 56 |
import dmd.mustuse; |
| 57 |
import dmd.nogc; |
| 58 |
import dmd.nspace; |
| 59 |
import dmd.objc; |
| 60 |
import dmd.opover; |
| 61 |
import dmd.optimize; |
| 62 |
import dmd.parse; |
| 63 |
import dmd.root.array; |
| 64 |
import dmd.root.filename; |
| 65 |
import dmd.common.outbuffer; |
| 66 |
import dmd.root.rmem; |
| 67 |
import dmd.rootobject; |
| 68 |
import dmd.root.utf; |
| 69 |
import dmd.semantic2; |
| 70 |
import dmd.semantic3; |
| 71 |
import dmd.sideeffect; |
| 72 |
import dmd.statementsem; |
| 73 |
import dmd.staticassert; |
| 74 |
import dmd.tokens; |
| 75 |
import dmd.utils; |
| 76 |
import dmd.statement; |
| 77 |
import dmd.target; |
| 78 |
import dmd.templateparamsem; |
| 79 |
import dmd.typesem; |
| 80 |
import dmd.visitor; |
| 81 |
|
| 82 |
|
| 83 |
/********************************* |
| 84 |
* Perform semantic analysis on enum declaration `em` |
| 85 |
*/ |
| 86 |
void enumSemantic(Scope* sc, EnumDeclaration ed) |
| 87 |
{ |
| 88 |
//printf("EnumDeclaration::semantic(sd = %p, '%s') %s\n", sc.scopesym, sc.scopesym.toChars(), ed.toChars()); |
| 89 |
//printf("EnumDeclaration::semantic() %p %s\n", ed, ed.toChars()); |
| 90 |
if (ed.semanticRun >= PASS.semanticdone) |
| 91 |
return; // semantic() already completed |
| 92 |
if (ed.semanticRun == PASS.semantic) |
| 93 |
{ |
| 94 |
assert(ed.memtype); |
| 95 |
error(ed.loc, "circular reference to enum base type `%s`", ed.memtype.toChars()); |
| 96 |
ed.errors = true; |
| 97 |
ed.semanticRun = PASS.semanticdone; |
| 98 |
return; |
| 99 |
} |
| 100 |
Scope* scx = null; |
| 101 |
if (ed._scope) |
| 102 |
{ |
| 103 |
sc = ed._scope; |
| 104 |
scx = ed._scope; // save so we don't make redundant copies |
| 105 |
ed._scope = null; |
| 106 |
} |
| 107 |
|
| 108 |
if (!sc) |
| 109 |
return; |
| 110 |
|
| 111 |
ed.parent = sc.parent; |
| 112 |
ed.type = ed.type.typeSemantic(ed.loc, sc); |
| 113 |
|
| 114 |
ed.visibility = sc.visibility; |
| 115 |
if (sc.stc & STC.deprecated_) |
| 116 |
ed.isdeprecated = true; |
| 117 |
ed.userAttribDecl = sc.userAttribDecl; |
| 118 |
ed.cppnamespace = sc.namespace; |
| 119 |
|
| 120 |
ed.semanticRun = PASS.semantic; |
| 121 |
checkGNUABITag(ed, sc.linkage); |
| 122 |
checkMustUseReserved(ed); |
| 123 |
|
| 124 |
if (!ed.members && !ed.memtype) // enum ident; |
| 125 |
{ |
| 126 |
ed.semanticRun = PASS.semanticdone; |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if (!ed.symtab) |
| 131 |
ed.symtab = new DsymbolTable(); |
| 132 |
|
| 133 |
/* The separate, and distinct, cases are: |
| 134 |
* 1. enum { ... } |
| 135 |
* 2. enum : memtype { ... } |
| 136 |
* 3. enum ident { ... } |
| 137 |
* 4. enum ident : memtype { ... } |
| 138 |
* 5. enum ident : memtype; |
| 139 |
* 6. enum ident; |
| 140 |
*/ |
| 141 |
|
| 142 |
if (ed.memtype) |
| 143 |
{ |
| 144 |
ed.memtype = ed.memtype.typeSemantic(ed.loc, sc); |
| 145 |
|
| 146 |
/* Check to see if memtype is forward referenced |
| 147 |
*/ |
| 148 |
if (auto te = ed.memtype.isTypeEnum()) |
| 149 |
{ |
| 150 |
auto sym = te.toDsymbol(sc).isEnumDeclaration(); |
| 151 |
// Special enums like __c_[u]long[long] are fine to forward reference |
| 152 |
// see https://issues.dlang.org/show_bug.cgi?id=20599 |
| 153 |
if (!sym.isSpecial() && (!sym.memtype || !sym.members || !sym.symtab || sym._scope)) |
| 154 |
{ |
| 155 |
// memtype is forward referenced, so try again later |
| 156 |
deferDsymbolSemantic(sc, ed, scx); |
| 157 |
//printf("\tdeferring %s\n", toChars()); |
| 158 |
ed.semanticRun = PASS.initial; |
| 159 |
return; |
| 160 |
} |
| 161 |
else |
| 162 |
// Ensure that semantic is run to detect. e.g. invalid forward references |
| 163 |
sym.dsymbolSemantic(sc); |
| 164 |
} |
| 165 |
if (ed.memtype.ty == Tvoid) |
| 166 |
{ |
| 167 |
.error(ed.loc, "%s `%s` base type must not be `void`", ed.kind, ed.toPrettyChars); |
| 168 |
ed.memtype = Type.terror; |
| 169 |
} |
| 170 |
if (ed.memtype.ty == Terror) |
| 171 |
{ |
| 172 |
ed.errors = true; |
| 173 |
// poison all the members |
| 174 |
ed.members.foreachDsymbol( (s) { s.errors = true; } ); |
| 175 |
ed.semanticRun = PASS.semanticdone; |
| 176 |
return; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (!ed.members) // enum ident : memtype; |
| 181 |
{ |
| 182 |
ed.semanticRun = PASS.semanticdone; |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
if (ed.members.length == 0) |
| 187 |
{ |
| 188 |
.error(ed.loc, "%s `%s` enum `%s` must have at least one member", ed.kind, ed.toPrettyChars, ed.toChars()); |
| 189 |
ed.errors = true; |
| 190 |
ed.semanticRun = PASS.semanticdone; |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
if (!sc.inCfile) // C enum remains incomplete until members are done |
| 195 |
ed.semanticRun = PASS.semanticdone; |
| 196 |
|
| 197 |
version (none) |
| 198 |
{ |
| 199 |
// @@@DEPRECATED_2.110@@@ https://dlang.org/deprecate.html#scope%20as%20a%20type%20constraint |
| 200 |
// Deprecated in 2.100 |
| 201 |
// Make an error in 2.110 |
| 202 |
if (sc.stc & STC.scope_) |
| 203 |
deprecation(ed.loc, "`scope` as a type constraint is deprecated. Use `scope` at the usage site."); |
| 204 |
} |
| 205 |
|
| 206 |
Scope* sce; |
| 207 |
if (ed.isAnonymous()) |
| 208 |
sce = sc; |
| 209 |
else |
| 210 |
{ |
| 211 |
sce = sc.push(ed); |
| 212 |
sce.parent = ed; |
| 213 |
} |
| 214 |
sce = sce.startCTFE(); |
| 215 |
sce.setNoFree(); // needed for getMaxMinValue() |
| 216 |
|
| 217 |
/* Each enum member gets the sce scope |
| 218 |
*/ |
| 219 |
ed.members.foreachDsymbol( (s) |
| 220 |
{ |
| 221 |
if (EnumMember em = s.isEnumMember()) |
| 222 |
em._scope = sce; |
| 223 |
}); |
| 224 |
|
| 225 |
/* addMember() is not called when the EnumDeclaration appears as a function statement, |
| 226 |
* so we have to do what addMember() does and install the enum members in the right symbol |
| 227 |
* table |
| 228 |
*/ |
| 229 |
addEnumMembersToSymtab(ed, sc, sc.getScopesym()); |
| 230 |
|
| 231 |
if (sc.inCfile) |
| 232 |
{ |
| 233 |
/* C11 6.7.2.2 |
| 234 |
*/ |
| 235 |
Type commonType = ed.memtype; |
| 236 |
if (!commonType) |
| 237 |
commonType = Type.tint32; |
| 238 |
ulong nextValue = 0; // C11 6.7.2.2-3 first member value defaults to 0 |
| 239 |
|
| 240 |
// C11 6.7.2.2-2 value must be representable as an int. |
| 241 |
// The sizemask represents all values that int will fit into, |
| 242 |
// from 0..uint.max. We want to cover int.min..uint.max. |
| 243 |
IntRange ir = IntRange.fromType(commonType); |
| 244 |
|
| 245 |
void emSemantic(EnumMember em, ref ulong nextValue) |
| 246 |
{ |
| 247 |
static void errorReturn(EnumMember em) |
| 248 |
{ |
| 249 |
em.value = ErrorExp.get(); |
| 250 |
em.errors = true; |
| 251 |
em.semanticRun = PASS.semanticdone; |
| 252 |
} |
| 253 |
|
| 254 |
em.semanticRun = PASS.semantic; |
| 255 |
em.type = commonType; |
| 256 |
em._linkage = LINK.c; |
| 257 |
em.storage_class |= STC.manifest; |
| 258 |
if (em.value) |
| 259 |
{ |
| 260 |
Expression e = em.value; |
| 261 |
assert(e.dyncast() == DYNCAST.expression); |
| 262 |
|
| 263 |
/* To merge the type of e with commonType, add 0 of type commonType |
| 264 |
*/ |
| 265 |
if (!ed.memtype) |
| 266 |
e = new AddExp(em.loc, e, new IntegerExp(em.loc, 0, commonType)); |
| 267 |
|
| 268 |
e = e.expressionSemantic(sc); |
| 269 |
e = resolveProperties(sc, e); |
| 270 |
e = e.integralPromotions(sc); |
| 271 |
e = e.ctfeInterpret(); |
| 272 |
if (e.op == EXP.error) |
| 273 |
return errorReturn(em); |
| 274 |
auto ie = e.isIntegerExp(); |
| 275 |
if (!ie) |
| 276 |
{ |
| 277 |
// C11 6.7.2.2-2 |
| 278 |
.error(em.loc, "%s `%s` enum member must be an integral constant expression, not `%s` of type `%s`", em.kind, em.toPrettyChars, e.toChars(), e.type.toChars()); |
| 279 |
return errorReturn(em); |
| 280 |
} |
| 281 |
if (ed.memtype && !ir.contains(getIntRange(ie))) |
| 282 |
{ |
| 283 |
// C11 6.7.2.2-2 |
| 284 |
.error(em.loc, "%s `%s` enum member value `%s` does not fit in `%s`", em.kind, em.toPrettyChars, e.toChars(), commonType.toChars()); |
| 285 |
return errorReturn(em); |
| 286 |
} |
| 287 |
nextValue = ie.toInteger(); |
| 288 |
if (!ed.memtype) |
| 289 |
commonType = e.type; |
| 290 |
em.value = new IntegerExp(em.loc, nextValue, commonType); |
| 291 |
} |
| 292 |
else |
| 293 |
{ |
| 294 |
// C11 6.7.2.2-3 add 1 to value of previous enumeration constant |
| 295 |
bool first = (em == (*em.ed.members)[0]); |
| 296 |
if (!first) |
| 297 |
{ |
| 298 |
Expression max = getProperty(commonType, null, em.loc, Id.max, 0); |
| 299 |
if (nextValue == max.toInteger()) |
| 300 |
{ |
| 301 |
.error(em.loc, "%s `%s` initialization with `%s+1` causes overflow for type `%s`", em.kind, em.toPrettyChars, max.toChars(), commonType.toChars()); |
| 302 |
return errorReturn(em); |
| 303 |
} |
| 304 |
nextValue += 1; |
| 305 |
} |
| 306 |
em.value = new IntegerExp(em.loc, nextValue, commonType); |
| 307 |
} |
| 308 |
em.type = commonType; |
| 309 |
em.semanticRun = PASS.semanticdone; |
| 310 |
} |
| 311 |
|
| 312 |
ed.members.foreachDsymbol( (s) |
| 313 |
{ |
| 314 |
if (EnumMember em = s.isEnumMember()) |
| 315 |
emSemantic(em, nextValue); |
| 316 |
}); |
| 317 |
|
| 318 |
if (!ed.memtype) |
| 319 |
{ |
| 320 |
// cast all members to commonType |
| 321 |
ed.members.foreachDsymbol( (s) |
| 322 |
{ |
| 323 |
if (EnumMember em = s.isEnumMember()) |
| 324 |
{ |
| 325 |
em.type = commonType; |
| 326 |
// optimize out the cast so that other parts of the compiler can |
| 327 |
// assume that an integral enum's members are `IntegerExp`s. |
| 328 |
// https://issues.dlang.org/show_bug.cgi?id=24504 |
| 329 |
em.value = em.value.castTo(sc, commonType).optimize(WANTvalue); |
| 330 |
} |
| 331 |
}); |
| 332 |
} |
| 333 |
|
| 334 |
ed.memtype = commonType; |
| 335 |
ed.semanticRun = PASS.semanticdone; |
| 336 |
return; |
| 337 |
} |
| 338 |
|
| 339 |
ed.members.foreachDsymbol( (s) |
| 340 |
{ |
| 341 |
if (EnumMember em = s.isEnumMember()) |
| 342 |
em.dsymbolSemantic(em._scope); |
| 343 |
}); |
| 344 |
//printf("ed.defaultval = %lld\n", ed.defaultval); |
| 345 |
|
| 346 |
//if (ed.defaultval) printf("ed.defaultval: %s %s\n", ed.defaultval.toChars(), ed.defaultval.type.toChars()); |
| 347 |
//printf("members = %s\n", members.toChars()); |
| 348 |
} |
| 349 |
|
| 350 |
Expression getDefaultValue(EnumDeclaration ed, Loc loc) |
| 351 |
{ |
| 352 |
Expression handleErrors(){ |
| 353 |
ed.defaultval = ErrorExp.get(); |
| 354 |
return ed.defaultval; |
| 355 |
} |
| 356 |
//printf("EnumDeclaration::getDefaultValue() %p %s\n", this, toChars()); |
| 357 |
// https://issues.dlang.org/show_bug.cgi?id=23904 |
| 358 |
// Return ed.defaultval only if it is not ErrorExp. |
| 359 |
// A speculative context may set ed.defaultval to ErrorExp; |
| 360 |
// subsequent non-speculative contexts need to be able |
| 361 |
// to print the error. |
| 362 |
if (ed.defaultval && !ed.defaultval.isErrorExp()) |
| 363 |
return ed.defaultval; |
| 364 |
|
| 365 |
if (ed.isCsymbol()) |
| 366 |
return ed.memtype.defaultInit(loc, true); |
| 367 |
|
| 368 |
if (ed._scope) |
| 369 |
dsymbolSemantic(ed, ed._scope); |
| 370 |
if (ed.errors) |
| 371 |
return handleErrors(); |
| 372 |
if (!ed.members) |
| 373 |
{ |
| 374 |
if (ed.isSpecial()) |
| 375 |
{ |
| 376 |
/* Allow these special enums to not need a member list |
| 377 |
*/ |
| 378 |
return ed.defaultval = ed.memtype.defaultInit(loc); |
| 379 |
} |
| 380 |
|
| 381 |
error(loc, "%s `%s` is opaque and has no default initializer", ed.kind, ed.toPrettyChars); |
| 382 |
return handleErrors(); |
| 383 |
} |
| 384 |
|
| 385 |
foreach (const i; 0 .. ed.members.length) |
| 386 |
{ |
| 387 |
if (EnumMember em = (*ed.members)[i].isEnumMember()) |
| 388 |
{ |
| 389 |
if (em.semanticRun < PASS.semanticdone) |
| 390 |
{ |
| 391 |
error(loc, "%s `%s` forward reference of `%s.init`", ed.kind, ed.toPrettyChars, ed.toChars()); |
| 392 |
return handleErrors(); |
| 393 |
} |
| 394 |
|
| 395 |
ed.defaultval = em.value; |
| 396 |
return ed.defaultval; |
| 397 |
} |
| 398 |
} |
| 399 |
return handleErrors(); |
| 400 |
} |
| 401 |
|
| 402 |
Type getMemtype(EnumDeclaration ed, Loc loc) |
| 403 |
{ |
| 404 |
if (ed._scope) |
| 405 |
{ |
| 406 |
/* Enum is forward referenced. We don't need to resolve the whole thing, |
| 407 |
* just the base type |
| 408 |
*/ |
| 409 |
if (ed.memtype) |
| 410 |
{ |
| 411 |
Loc locx = loc.isValid() ? loc : ed.loc; |
| 412 |
ed.memtype = ed.memtype.typeSemantic(locx, ed._scope); |
| 413 |
} |
| 414 |
else |
| 415 |
{ |
| 416 |
// Run semantic to get the type from a possible first member value |
| 417 |
dsymbolSemantic(ed, ed._scope); |
| 418 |
} |
| 419 |
} |
| 420 |
if (!ed.memtype) |
| 421 |
{ |
| 422 |
if (!ed.isAnonymous() && (ed.members || ed.semanticRun >= PASS.semanticdone)) |
| 423 |
ed.memtype = Type.tint32; |
| 424 |
else |
| 425 |
{ |
| 426 |
Loc locx = loc.isValid() ? loc : ed.loc; |
| 427 |
error(locx, "is forward referenced looking for base type"); |
| 428 |
return Type.terror; |
| 429 |
} |
| 430 |
} |
| 431 |
return ed.memtype; |
| 432 |
} |
| 433 |
|
| 434 |
/********************************* |
| 435 |
* Perform semantic analysis on enum member `em` |
| 436 |
*/ |
| 437 |
void enumMemberSemantic(Scope* sc, EnumMember em) |
| 438 |
{ |
| 439 |
//printf("EnumMember::semantic() %s\n", em.toChars()); |
| 440 |
|
| 441 |
void errorReturn() |
| 442 |
{ |
| 443 |
em.errors = true; |
| 444 |
em.semanticRun = PASS.semanticdone; |
| 445 |
} |
| 446 |
|
| 447 |
if (em.errors || em.semanticRun >= PASS.semanticdone) |
| 448 |
return; |
| 449 |
if (em.semanticRun == PASS.semantic) |
| 450 |
{ |
| 451 |
.error(em.loc, "%s `%s` circular reference to `enum` member", em.kind, em.toPrettyChars); |
| 452 |
return errorReturn(); |
| 453 |
} |
| 454 |
assert(em.ed); |
| 455 |
|
| 456 |
em.ed.dsymbolSemantic(sc); |
| 457 |
if (em.ed.errors) |
| 458 |
return errorReturn(); |
| 459 |
if (em.errors || em.semanticRun >= PASS.semanticdone) |
| 460 |
return; |
| 461 |
|
| 462 |
if (em._scope) |
| 463 |
sc = em._scope; |
| 464 |
if (!sc) |
| 465 |
return; |
| 466 |
|
| 467 |
em.semanticRun = PASS.semantic; |
| 468 |
|
| 469 |
em.visibility = em.ed.isAnonymous() ? em.ed.visibility : Visibility(Visibility.Kind.public_); |
| 470 |
em._linkage = LINK.d; |
| 471 |
em.storage_class |= STC.manifest; |
| 472 |
|
| 473 |
// https://issues.dlang.org/show_bug.cgi?id=9701 |
| 474 |
if (em.ed.isAnonymous()) |
| 475 |
{ |
| 476 |
if (em.userAttribDecl) |
| 477 |
em.userAttribDecl.userAttribDecl = em.ed.userAttribDecl; |
| 478 |
else |
| 479 |
em.userAttribDecl = em.ed.userAttribDecl; |
| 480 |
} |
| 481 |
|
| 482 |
// Eval UDA in this same scope. Issues 19344, 20835, 21122 |
| 483 |
if (em.userAttribDecl) |
| 484 |
{ |
| 485 |
// Set scope but avoid extra sc.uda attachment inside setScope() |
| 486 |
auto inneruda = em.userAttribDecl.userAttribDecl; |
| 487 |
em.userAttribDecl.setScope(sc); |
| 488 |
em.userAttribDecl.userAttribDecl = inneruda; |
| 489 |
em.userAttribDecl.dsymbolSemantic(sc); |
| 490 |
} |
| 491 |
|
| 492 |
// The first enum member is special |
| 493 |
bool first = (em == (*em.ed.members)[0]); |
| 494 |
|
| 495 |
if (em.origType) |
| 496 |
{ |
| 497 |
em.origType = em.origType.typeSemantic(em.loc, sc); |
| 498 |
em.type = em.origType; |
| 499 |
assert(em.value); // "type id;" is not a valid enum member declaration |
| 500 |
} |
| 501 |
|
| 502 |
if (em.value) |
| 503 |
{ |
| 504 |
Expression e = em.value; |
| 505 |
assert(e.dyncast() == DYNCAST.expression); |
| 506 |
if (em.ed.memtype) |
| 507 |
e = inferType(e, em.ed.memtype); |
| 508 |
e = e.expressionSemantic(sc); |
| 509 |
e = resolveProperties(sc, e); |
| 510 |
e = e.ctfeInterpret(); |
| 511 |
if (e.op == EXP.error) |
| 512 |
return errorReturn(); |
| 513 |
if (first && !em.ed.memtype && !em.ed.isAnonymous()) |
| 514 |
{ |
| 515 |
em.ed.memtype = e.type; |
| 516 |
if (em.ed.memtype.ty == Terror) |
| 517 |
{ |
| 518 |
em.ed.errors = true; |
| 519 |
return errorReturn(); |
| 520 |
} |
| 521 |
if (em.ed.memtype.ty != Terror) |
| 522 |
{ |
| 523 |
/* https://issues.dlang.org/show_bug.cgi?id=11746 |
| 524 |
* All of named enum members should have same type |
| 525 |
* with the first member. If the following members were referenced |
| 526 |
* during the first member semantic, their types should be unified. |
| 527 |
*/ |
| 528 |
em.ed.members.foreachDsymbol( (s) |
| 529 |
{ |
| 530 |
EnumMember enm = s.isEnumMember(); |
| 531 |
if (!enm || enm == em || enm.semanticRun < PASS.semanticdone || enm.origType) |
| 532 |
return; |
| 533 |
|
| 534 |
//printf("[%d] em = %s, em.semanticRun = %d\n", i, toChars(), em.semanticRun); |
| 535 |
Expression ev = enm.value; |
| 536 |
ev = ev.implicitCastTo(sc, em.ed.memtype); |
| 537 |
ev = ev.ctfeInterpret(); |
| 538 |
ev = ev.castTo(sc, em.ed.type); |
| 539 |
if (ev.op == EXP.error) |
| 540 |
em.ed.errors = true; |
| 541 |
enm.value = ev; |
| 542 |
}); |
| 543 |
|
| 544 |
if (em.ed.errors) |
| 545 |
{ |
| 546 |
em.ed.memtype = Type.terror; |
| 547 |
return errorReturn(); |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if (em.ed.memtype && !em.origType) |
| 553 |
{ |
| 554 |
e = e.implicitCastTo(sc, em.ed.memtype); |
| 555 |
e = e.ctfeInterpret(); |
| 556 |
|
| 557 |
// save origValue for better json output |
| 558 |
em.origValue = e; |
| 559 |
|
| 560 |
if (!em.ed.isAnonymous()) |
| 561 |
{ |
| 562 |
e = e.castTo(sc, em.ed.type.addMod(e.type.mod)); // https://issues.dlang.org/show_bug.cgi?id=12385 |
| 563 |
e = e.ctfeInterpret(); |
| 564 |
} |
| 565 |
} |
| 566 |
else if (em.origType) |
| 567 |
{ |
| 568 |
e = e.implicitCastTo(sc, em.origType); |
| 569 |
e = e.ctfeInterpret(); |
| 570 |
assert(em.ed.isAnonymous()); |
| 571 |
|
| 572 |
// save origValue for better json output |
| 573 |
em.origValue = e; |
| 574 |
} |
| 575 |
em.value = e; |
| 576 |
// https://issues.dlang.org/show_bug.cgi?id=24311 |
| 577 |
// First enum member is .init value, which gets put into static segment |
| 578 |
if (first) |
| 579 |
lowerStaticAAs(e, sc); |
| 580 |
} |
| 581 |
else if (first) |
| 582 |
{ |
| 583 |
Type t; |
| 584 |
if (em.ed.memtype) |
| 585 |
t = em.ed.memtype; |
| 586 |
else |
| 587 |
{ |
| 588 |
t = Type.tint32; |
| 589 |
if (!em.ed.isAnonymous()) |
| 590 |
em.ed.memtype = t; |
| 591 |
} |
| 592 |
const errors = global.startGagging(); |
| 593 |
Expression e = new IntegerExp(em.loc, 0, t); |
| 594 |
e = e.ctfeInterpret(); |
| 595 |
if (global.endGagging(errors)) |
| 596 |
{ |
| 597 |
error(em.loc, "cannot generate 0 value of type `%s` for `%s`", |
| 598 |
t.toChars(), em.toChars()); |
| 599 |
} |
| 600 |
// save origValue for better json output |
| 601 |
em.origValue = e; |
| 602 |
|
| 603 |
if (!em.ed.isAnonymous()) |
| 604 |
{ |
| 605 |
e = e.castTo(sc, em.ed.type); |
| 606 |
e = e.ctfeInterpret(); |
| 607 |
} |
| 608 |
em.value = e; |
| 609 |
} |
| 610 |
else |
| 611 |
{ |
| 612 |
/* Find the previous enum member, |
| 613 |
* and set this to be the previous value + 1 |
| 614 |
*/ |
| 615 |
EnumMember emprev = null; |
| 616 |
em.ed.members.foreachDsymbol( (s) |
| 617 |
{ |
| 618 |
if (auto enm = s.isEnumMember()) |
| 619 |
{ |
| 620 |
if (enm == em) |
| 621 |
return 1; // found |
| 622 |
emprev = enm; |
| 623 |
} |
| 624 |
return 0; // continue |
| 625 |
}); |
| 626 |
|
| 627 |
assert(emprev); |
| 628 |
if (emprev.semanticRun < PASS.semanticdone) // if forward reference |
| 629 |
emprev.dsymbolSemantic(emprev._scope); // resolve it |
| 630 |
if (emprev.errors) |
| 631 |
return errorReturn(); |
| 632 |
|
| 633 |
auto errors = global.startGagging(); |
| 634 |
Expression eprev = emprev.value; |
| 635 |
assert(eprev); |
| 636 |
// .toHeadMutable() due to https://issues.dlang.org/show_bug.cgi?id=18645 |
| 637 |
Type tprev = eprev.type.toHeadMutable().equals(em.ed.type.toHeadMutable()) |
| 638 |
? em.ed.memtype |
| 639 |
: eprev.type; |
| 640 |
/* |
| 641 |
https://issues.dlang.org/show_bug.cgi?id=20777 |
| 642 |
Previously this used getProperty, which doesn't consider anything user defined, |
| 643 |
this construct does do that and thus fixes the bug. |
| 644 |
*/ |
| 645 |
Expression emax = DotIdExp.create(em.ed.loc, new TypeExp(em.ed.loc, tprev), Id.max); |
| 646 |
emax = emax.expressionSemantic(sc); |
| 647 |
emax = emax.ctfeInterpret(); |
| 648 |
|
| 649 |
// check that (eprev != emax) |
| 650 |
Expression e = new EqualExp(EXP.equal, em.loc, eprev, emax); |
| 651 |
e = e.expressionSemantic(sc); |
| 652 |
e = e.ctfeInterpret(); |
| 653 |
if (global.endGagging(errors)) |
| 654 |
{ |
| 655 |
// display an introductory error before showing what actually failed |
| 656 |
error(em.loc, "cannot check `%s` value for overflow", em.toPrettyChars()); |
| 657 |
// rerun to show errors |
| 658 |
Expression e2 = DotIdExp.create(em.ed.loc, new TypeExp(em.ed.loc, tprev), Id.max); |
| 659 |
e2 = e2.expressionSemantic(sc); |
| 660 |
e2 = e2.ctfeInterpret(); |
| 661 |
e2 = new EqualExp(EXP.equal, em.loc, eprev, e2); |
| 662 |
e2 = e2.expressionSemantic(sc); |
| 663 |
e2 = e2.ctfeInterpret(); |
| 664 |
} |
| 665 |
// now any errors are for generating a value |
| 666 |
if (e.toInteger()) |
| 667 |
{ |
| 668 |
auto mt = em.ed.memtype; |
| 669 |
if (!mt) |
| 670 |
mt = eprev.type; |
| 671 |
.error(em.loc, "%s `%s` initialization with `%s.%s+1` causes overflow for type `%s`", em.kind, em.toPrettyChars, |
| 672 |
emprev.ed.toChars(), emprev.toChars(), mt.toChars()); |
| 673 |
return errorReturn(); |
| 674 |
} |
| 675 |
errors = global.startGagging(); |
| 676 |
// Now set e to (eprev + 1) |
| 677 |
e = new AddExp(em.loc, eprev, IntegerExp.literal!1); |
| 678 |
e = e.expressionSemantic(sc); |
| 679 |
e = e.castTo(sc, eprev.type); |
| 680 |
e = e.ctfeInterpret(); |
| 681 |
if (global.endGagging(errors)) |
| 682 |
{ |
| 683 |
error(em.loc, "cannot generate value for `%s`", em.toPrettyChars()); |
| 684 |
// rerun to show errors |
| 685 |
Expression e2 = new AddExp(em.loc, eprev, IntegerExp.literal!1); |
| 686 |
e2 = e2.expressionSemantic(sc); |
| 687 |
e2 = e2.castTo(sc, eprev.type); |
| 688 |
e2 = e2.ctfeInterpret(); |
| 689 |
} |
| 690 |
// save origValue (without cast) for better json output |
| 691 |
if (e.op != EXP.error) // avoid duplicate diagnostics |
| 692 |
{ |
| 693 |
assert(emprev.origValue); |
| 694 |
em.origValue = new AddExp(em.loc, emprev.origValue, IntegerExp.literal!1); |
| 695 |
em.origValue = em.origValue.expressionSemantic(sc); |
| 696 |
em.origValue = em.origValue.ctfeInterpret(); |
| 697 |
} |
| 698 |
|
| 699 |
if (e.op == EXP.error) |
| 700 |
return errorReturn(); |
| 701 |
if (e.type.isFloating()) |
| 702 |
{ |
| 703 |
// Check that e != eprev (not always true for floats) |
| 704 |
Expression etest = new EqualExp(EXP.equal, em.loc, e, eprev); |
| 705 |
etest = etest.expressionSemantic(sc); |
| 706 |
etest = etest.ctfeInterpret(); |
| 707 |
if (etest.toInteger()) |
| 708 |
{ |
| 709 |
.error(em.loc, "%s `%s` has inexact value due to loss of precision", em.kind, em.toPrettyChars); |
| 710 |
return errorReturn(); |
| 711 |
} |
| 712 |
} |
| 713 |
em.value = e; |
| 714 |
} |
| 715 |
if (!em.origType) |
| 716 |
em.type = em.value.type; |
| 717 |
|
| 718 |
assert(em.origValue); |
| 719 |
em.semanticRun = PASS.semanticdone; |
| 720 |
} |