| 1 |
/** |
| 2 |
* Performs the semantic2 stage, which deals with initializer expressions. |
| 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/semantic2.d, _semantic2.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_semantic2.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/semantic2.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.semantic2; |
| 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.timetrace; |
| 25 |
import dmd.clone; |
| 26 |
import dmd.dcast; |
| 27 |
import dmd.dclass; |
| 28 |
import dmd.declaration; |
| 29 |
import dmd.denum; |
| 30 |
import dmd.dimport; |
| 31 |
import dmd.dinterpret; |
| 32 |
import dmd.dmodule; |
| 33 |
import dmd.dscope; |
| 34 |
import dmd.dstruct; |
| 35 |
import dmd.dsymbol; |
| 36 |
import dmd.dsymbolsem; |
| 37 |
import dmd.dtemplate; |
| 38 |
import dmd.dversion; |
| 39 |
import dmd.errors; |
| 40 |
import dmd.escape; |
| 41 |
import dmd.expression; |
| 42 |
import dmd.expressionsem; |
| 43 |
import dmd.func; |
| 44 |
import dmd.funcsem; |
| 45 |
import dmd.globals; |
| 46 |
import dmd.id; |
| 47 |
import dmd.identifier; |
| 48 |
import dmd.init; |
| 49 |
import dmd.initsem; |
| 50 |
import dmd.hdrgen; |
| 51 |
import dmd.mtype; |
| 52 |
import dmd.nogc; |
| 53 |
import dmd.nspace; |
| 54 |
import dmd.objc; |
| 55 |
import dmd.opover; |
| 56 |
import dmd.parse; |
| 57 |
import dmd.root.filename; |
| 58 |
import dmd.common.outbuffer; |
| 59 |
import dmd.root.rmem; |
| 60 |
import dmd.root.string : toDString; |
| 61 |
import dmd.rootobject; |
| 62 |
import dmd.root.utf; |
| 63 |
import dmd.sideeffect; |
| 64 |
import dmd.statementsem; |
| 65 |
import dmd.staticassert; |
| 66 |
import dmd.tokens; |
| 67 |
import dmd.statement; |
| 68 |
import dmd.target; |
| 69 |
import dmd.templateparamsem; |
| 70 |
import dmd.typesem; |
| 71 |
import dmd.visitor; |
| 72 |
|
| 73 |
enum LOG = false; |
| 74 |
|
| 75 |
|
| 76 |
/************************************* |
| 77 |
* Does semantic analysis on initializers and members of aggregates. |
| 78 |
*/ |
| 79 |
void semantic2(Dsymbol dsym, Scope* sc) |
| 80 |
{ |
| 81 |
scope v = new Semantic2Visitor(sc); |
| 82 |
dsym.accept(v); |
| 83 |
} |
| 84 |
|
| 85 |
private extern(C++) final class Semantic2Visitor : Visitor |
| 86 |
{ |
| 87 |
alias visit = Visitor.visit; |
| 88 |
Scope* sc; |
| 89 |
this(Scope* sc) scope @safe |
| 90 |
{ |
| 91 |
this.sc = sc; |
| 92 |
} |
| 93 |
|
| 94 |
override void visit(Dsymbol) {} |
| 95 |
|
| 96 |
override void visit(StaticAssert sa) |
| 97 |
{ |
| 98 |
//printf("StaticAssert::semantic2() %s\n", sa.toChars()); |
| 99 |
if (const e = sa.exp.isStringExp()) |
| 100 |
{ |
| 101 |
// deprecated in 2.107 |
| 102 |
deprecation(e.loc, "static assert condition cannot be a string literal"); |
| 103 |
deprecationSupplemental(e.loc, "If intentional, use `%s !is null` instead to preserve behaviour", |
| 104 |
e.toChars()); |
| 105 |
} |
| 106 |
auto sds = new ScopeDsymbol(); |
| 107 |
sc = sc.push(sds); |
| 108 |
sc.tinst = null; |
| 109 |
sc.minst = null; |
| 110 |
|
| 111 |
import dmd.staticcond; |
| 112 |
bool errors; |
| 113 |
bool result = evalStaticCondition(sc, sa.exp, sa.exp, errors); |
| 114 |
sc = sc.pop(); |
| 115 |
if (errors) |
| 116 |
{ |
| 117 |
errorSupplemental(sa.loc, "while evaluating: `static assert(%s)`", sa.exp.toChars()); |
| 118 |
return; |
| 119 |
} |
| 120 |
else if (result) |
| 121 |
return; |
| 122 |
|
| 123 |
staticAssertFail(sa, sc); |
| 124 |
} |
| 125 |
|
| 126 |
override void visit(TemplateInstance tempinst) |
| 127 |
{ |
| 128 |
if (tempinst.semanticRun >= PASS.semantic2) |
| 129 |
return; |
| 130 |
tempinst.semanticRun = PASS.semantic2; |
| 131 |
static if (LOG) |
| 132 |
{ |
| 133 |
printf("+TemplateInstance.semantic2('%s')\n", tempinst.toChars()); |
| 134 |
scope(exit) printf("-TemplateInstance.semantic2('%s')\n", tempinst.toChars()); |
| 135 |
} |
| 136 |
if (tempinst.errors || !tempinst.members) |
| 137 |
return; |
| 138 |
|
| 139 |
TemplateDeclaration tempdecl = tempinst.tempdecl.isTemplateDeclaration(); |
| 140 |
assert(tempdecl); |
| 141 |
|
| 142 |
sc = tempdecl._scope; |
| 143 |
assert(sc); |
| 144 |
sc = sc.push(tempinst.argsym); |
| 145 |
sc = sc.push(tempinst); |
| 146 |
sc.tinst = tempinst; |
| 147 |
sc.minst = tempinst.minst; |
| 148 |
|
| 149 |
const needGagging = (tempinst.gagged && !global.gag); |
| 150 |
const olderrors = global.errors; |
| 151 |
const oldGaggedErrors = needGagging ? global.startGagging() : -1; |
| 152 |
|
| 153 |
for (size_t i = 0; i < tempinst.members.length; i++) |
| 154 |
{ |
| 155 |
Dsymbol s = (*tempinst.members)[i]; |
| 156 |
static if (LOG) |
| 157 |
{ |
| 158 |
printf("\tmember '%s', kind = '%s'\n", s.toChars(), s.kind()); |
| 159 |
} |
| 160 |
s.semantic2(sc); |
| 161 |
if (tempinst.gagged && global.errors != olderrors) |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
if (global.errors != olderrors) |
| 166 |
{ |
| 167 |
if (!tempinst.errors) |
| 168 |
{ |
| 169 |
if (!tempdecl.literal) |
| 170 |
.error(tempinst.loc, "%s `%s` error instantiating", tempinst.kind, tempinst.toPrettyChars); |
| 171 |
if (tempinst.tinst) |
| 172 |
tempinst.tinst.printInstantiationTrace(); |
| 173 |
} |
| 174 |
tempinst.errors = true; |
| 175 |
} |
| 176 |
if (needGagging) |
| 177 |
global.endGagging(oldGaggedErrors); |
| 178 |
|
| 179 |
sc = sc.pop(); |
| 180 |
sc.pop(); |
| 181 |
} |
| 182 |
|
| 183 |
override void visit(TemplateMixin tmix) |
| 184 |
{ |
| 185 |
if (tmix.semanticRun >= PASS.semantic2) |
| 186 |
return; |
| 187 |
tmix.semanticRun = PASS.semantic2; |
| 188 |
static if (LOG) |
| 189 |
{ |
| 190 |
printf("+TemplateMixin.semantic2('%s')\n", tmix.toChars()); |
| 191 |
scope(exit) printf("-TemplateMixin.semantic2('%s')\n", tmix.toChars()); |
| 192 |
} |
| 193 |
if (!tmix.members) |
| 194 |
return; |
| 195 |
|
| 196 |
assert(sc); |
| 197 |
sc = sc.push(tmix.argsym); |
| 198 |
sc = sc.push(tmix); |
| 199 |
sc.tinst = tmix; |
| 200 |
sc.minst = tmix.minst; |
| 201 |
for (size_t i = 0; i < tmix.members.length; i++) |
| 202 |
{ |
| 203 |
Dsymbol s = (*tmix.members)[i]; |
| 204 |
static if (LOG) |
| 205 |
{ |
| 206 |
printf("\tmember '%s', kind = '%s'\n", s.toChars(), s.kind()); |
| 207 |
} |
| 208 |
s.semantic2(sc); |
| 209 |
} |
| 210 |
sc = sc.pop(); |
| 211 |
sc.pop(); |
| 212 |
} |
| 213 |
|
| 214 |
override void visit(VarDeclaration vd) |
| 215 |
{ |
| 216 |
if (vd.semanticRun < PASS.semanticdone && vd.inuse) |
| 217 |
return; |
| 218 |
|
| 219 |
//printf("VarDeclaration::semantic2('%s')\n", toChars()); |
| 220 |
sc = sc.push(); |
| 221 |
sc.varDecl = vd; |
| 222 |
scope(exit) sc = sc.pop(); |
| 223 |
|
| 224 |
if (vd.aliasTuple) // if it's a tuple |
| 225 |
{ |
| 226 |
vd.aliasTuple.accept(this); |
| 227 |
vd.semanticRun = PASS.semantic2done; |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
checkGNUABITag(vd, vd._linkage); |
| 232 |
|
| 233 |
if (vd._init && !vd.toParent().isFuncDeclaration()) |
| 234 |
{ |
| 235 |
vd.inuse++; |
| 236 |
|
| 237 |
/* https://issues.dlang.org/show_bug.cgi?id=20280 |
| 238 |
* |
| 239 |
* Template instances may import modules that have not |
| 240 |
* finished semantic1. |
| 241 |
*/ |
| 242 |
if (!vd.type) |
| 243 |
vd.dsymbolSemantic(sc); |
| 244 |
|
| 245 |
|
| 246 |
// https://issues.dlang.org/show_bug.cgi?id=14166 |
| 247 |
// https://issues.dlang.org/show_bug.cgi?id=20417 |
| 248 |
// Don't run CTFE for the temporary variables inside typeof or __traits(compiles) |
| 249 |
vd._init = vd._init.initializerSemantic(sc, vd.type, sc.intypeof == 1 || sc.traitsCompiles ? INITnointerpret : INITinterpret); |
| 250 |
lowerStaticAAs(vd, sc); |
| 251 |
vd.inuse--; |
| 252 |
} |
| 253 |
if (vd._init && vd.storage_class & STC.manifest) |
| 254 |
{ |
| 255 |
/* Cannot initializer enums with CTFE classreferences and addresses of struct literals. |
| 256 |
* Scan initializer looking for them. Issue error if found. |
| 257 |
*/ |
| 258 |
if (ExpInitializer ei = vd._init.isExpInitializer()) |
| 259 |
{ |
| 260 |
static bool hasInvalidEnumInitializer(Expression e) |
| 261 |
{ |
| 262 |
static bool arrayHasInvalidEnumInitializer(Expressions* elems) |
| 263 |
{ |
| 264 |
foreach (e; *elems) |
| 265 |
{ |
| 266 |
if (e && hasInvalidEnumInitializer(e)) |
| 267 |
return true; |
| 268 |
} |
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
if (e.op == EXP.classReference) |
| 273 |
return true; |
| 274 |
if (e.op == EXP.address && (cast(AddrExp)e).e1.op == EXP.structLiteral) |
| 275 |
return true; |
| 276 |
if (e.op == EXP.arrayLiteral) |
| 277 |
return arrayHasInvalidEnumInitializer((cast(ArrayLiteralExp)e).elements); |
| 278 |
if (e.op == EXP.structLiteral) |
| 279 |
return arrayHasInvalidEnumInitializer((cast(StructLiteralExp)e).elements); |
| 280 |
if (e.op == EXP.assocArrayLiteral) |
| 281 |
{ |
| 282 |
auto ae = cast(AssocArrayLiteralExp)e; |
| 283 |
return arrayHasInvalidEnumInitializer(ae.values) || |
| 284 |
arrayHasInvalidEnumInitializer(ae.keys); |
| 285 |
} |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
if (hasInvalidEnumInitializer(ei.exp)) |
| 290 |
.error(vd.loc, "%s `%s` : Unable to initialize enum with class or pointer to struct. Use static const variable instead.", vd.kind, vd.toPrettyChars); |
| 291 |
} |
| 292 |
} |
| 293 |
else if (vd._init && vd.isThreadlocal()) |
| 294 |
{ |
| 295 |
// Cannot initialize a thread-local class or pointer to struct variable with a literal |
| 296 |
// that itself is a thread-local reference and would need dynamic initialization also. |
| 297 |
if (vd.type.ty == Tclass && vd.type.isMutable() && !vd.type.isShared()) |
| 298 |
{ |
| 299 |
ExpInitializer ei = vd._init.isExpInitializer(); |
| 300 |
if (ei && ei.exp.op == EXP.classReference) |
| 301 |
.error(vd.loc, "%s `%s` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead.", vd.kind, vd.toPrettyChars); |
| 302 |
} |
| 303 |
else if (vd.type.ty == Tpointer && vd.type.nextOf().ty == Tstruct && vd.type.nextOf().isMutable() && !vd.type.nextOf().isShared()) |
| 304 |
{ |
| 305 |
ExpInitializer ei = vd._init.isExpInitializer(); |
| 306 |
if (ei && ei.exp.op == EXP.address && (cast(AddrExp)ei.exp).e1.op == EXP.structLiteral) |
| 307 |
.error(vd.loc, "%s `%s` is a thread-local pointer to struct and cannot have a static initializer. Use `static this()` to initialize instead.", vd.kind, vd.toPrettyChars); |
| 308 |
} |
| 309 |
} |
| 310 |
vd.semanticRun = PASS.semantic2done; |
| 311 |
} |
| 312 |
|
| 313 |
override void visit(Module mod) |
| 314 |
{ |
| 315 |
//printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent); |
| 316 |
if (mod.semanticRun != PASS.semanticdone) // semantic() not completed yet - could be recursive call |
| 317 |
return; |
| 318 |
mod.semanticRun = PASS.semantic2; |
| 319 |
// Note that modules get their own scope, from scratch. |
| 320 |
// This is so regardless of where in the syntax a module |
| 321 |
// gets imported, it is unaffected by context. |
| 322 |
Scope* sc = Scope.createGlobal(mod, global.errorSink); // create root scope |
| 323 |
//printf("Module = %p\n", sc.scopesym); |
| 324 |
if (mod.members) |
| 325 |
{ |
| 326 |
// Pass 2 semantic routines: do initializers and function bodies |
| 327 |
for (size_t i = 0; i < mod.members.length; i++) |
| 328 |
{ |
| 329 |
Dsymbol s = (*mod.members)[i]; |
| 330 |
s.semantic2(sc); |
| 331 |
} |
| 332 |
} |
| 333 |
if (mod.userAttribDecl) |
| 334 |
{ |
| 335 |
mod.userAttribDecl.semantic2(sc); |
| 336 |
} |
| 337 |
sc = sc.pop(); |
| 338 |
sc.pop(); |
| 339 |
mod.semanticRun = PASS.semantic2done; |
| 340 |
//printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent); |
| 341 |
} |
| 342 |
|
| 343 |
override void visit(FuncDeclaration fd) |
| 344 |
{ |
| 345 |
if (fd.semanticRun >= PASS.semantic2done) |
| 346 |
return; |
| 347 |
|
| 348 |
if (fd.semanticRun < PASS.semanticdone && !fd.errors) |
| 349 |
{ |
| 350 |
/* https://issues.dlang.org/show_bug.cgi?id=21614 |
| 351 |
* |
| 352 |
* Template instances may import modules that have not |
| 353 |
* finished semantic1. |
| 354 |
*/ |
| 355 |
fd.dsymbolSemantic(sc); |
| 356 |
} |
| 357 |
assert(fd.semanticRun <= PASS.semantic2); |
| 358 |
fd.semanticRun = PASS.semantic2; |
| 359 |
|
| 360 |
timeTraceBeginEvent(TimeTraceEventType.sema2); |
| 361 |
scope(exit) timeTraceEndEvent(TimeTraceEventType.sema2, fd); |
| 362 |
|
| 363 |
//printf("FuncDeclaration::semantic2 [%s] fd: %s type: %s\n", fd.loc.toChars(), fd.toChars(), fd.type ? fd.type.toChars() : "".ptr); |
| 364 |
|
| 365 |
// Only check valid functions which have a body to avoid errors |
| 366 |
// for multiple declarations, e.g. |
| 367 |
// void foo(); |
| 368 |
// void foo(); |
| 369 |
if (fd.fbody && fd.overnext && !fd.errors) |
| 370 |
{ |
| 371 |
// Always starts the lookup from 'this', because the conflicts with |
| 372 |
// previous overloads are already reported. |
| 373 |
alias f1 = fd; |
| 374 |
auto tf1 = cast(TypeFunction) f1.type; |
| 375 |
auto parent1 = f1.toParent2(); |
| 376 |
const linkage1 = f1.resolvedLinkage(); |
| 377 |
|
| 378 |
overloadApply(f1, (Dsymbol s) |
| 379 |
{ |
| 380 |
auto f2 = s.isFuncDeclaration(); |
| 381 |
if (!f2 || f1 == f2 || f2.errors) |
| 382 |
return 0; |
| 383 |
|
| 384 |
// Don't have to check conflict between declaration and definition. |
| 385 |
if (f2.fbody is null) |
| 386 |
return 0; |
| 387 |
|
| 388 |
// Functions with different manglings can never conflict |
| 389 |
if (linkage1 != f2.resolvedLinkage()) |
| 390 |
return 0; |
| 391 |
|
| 392 |
// Functions with different names never conflict |
| 393 |
// (they can form overloads sets introduced by an alias) |
| 394 |
if (f1.ident != f2.ident) |
| 395 |
return 0; |
| 396 |
|
| 397 |
// Functions with different parents never conflict |
| 398 |
// (E.g. when aliasing a free function into a struct) |
| 399 |
if (parent1 != f2.toParent2()) |
| 400 |
return 0; |
| 401 |
|
| 402 |
/* Check for overload merging with base class member functions. |
| 403 |
* |
| 404 |
* class B { void foo() {} } |
| 405 |
* class D : B { |
| 406 |
* override void foo() {} // B.foo appears as f2 |
| 407 |
* alias foo = B.foo; |
| 408 |
* } |
| 409 |
*/ |
| 410 |
if (f1.overrides(f2)) |
| 411 |
return 0; |
| 412 |
|
| 413 |
auto tf2 = cast(TypeFunction) f2.type; |
| 414 |
|
| 415 |
// Overloading based on storage classes |
| 416 |
if (tf1.mod != tf2.mod || ((f1.storage_class ^ f2.storage_class) & STC.static_)) |
| 417 |
return 0; |
| 418 |
|
| 419 |
// @@@DEPRECATED_2.112@@@ |
| 420 |
// This test doesn't catch identical functions that differ only |
| 421 |
// in explicit/implicit `@system` - a deprecation has now been |
| 422 |
// added below, remove `false` after deprecation period is over. |
| 423 |
const sameAttr = tf1.attributesEqual(tf2, false); |
| 424 |
const sameParams = tf1.parameterList == tf2.parameterList; |
| 425 |
|
| 426 |
// Allow the hack to declare overloads with different parameters/STC's |
| 427 |
if (parent1.isModule() && |
| 428 |
linkage1 != LINK.d && linkage1 != LINK.cpp && |
| 429 |
(!sameAttr || !sameParams) |
| 430 |
) |
| 431 |
{ |
| 432 |
.error(f2.loc, "%s `%s` cannot overload `extern(%s)` function at %s", f2.kind, f2.toPrettyChars, |
| 433 |
linkageToChars(f1._linkage), |
| 434 |
f1.loc.toChars()); |
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
// Different parameters don't conflict in extern(C++/D) |
| 439 |
if (!sameParams) |
| 440 |
return 0; |
| 441 |
|
| 442 |
// Different attributes don't conflict in extern(D) |
| 443 |
if (!sameAttr && linkage1 == LINK.d) |
| 444 |
{ |
| 445 |
// @@@DEPRECATED_2.112@@@ |
| 446 |
// Same as 2.104 deprecation, but also catching explicit/implicit `@system` |
| 447 |
// At the end of deprecation period, fix Type.attributesEqual and remove |
| 448 |
// this condition, as well as the error for extern(C) functions above. |
| 449 |
if (sameAttr != tf1.attributesEqual(tf2)) |
| 450 |
{ |
| 451 |
.deprecation(f2.loc, "%s `%s` cannot overload `extern(%s)` function at %s", f2.kind, f2.toPrettyChars, |
| 452 |
linkageToChars(f1._linkage), |
| 453 |
f1.loc.toChars()); |
| 454 |
} |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
.error(f2.loc, "%s `%s%s` conflicts with previous declaration at %s", |
| 459 |
f2.kind(), |
| 460 |
f2.toPrettyChars(), |
| 461 |
parametersTypeToChars(tf2.parameterList), |
| 462 |
f1.loc.toChars()); |
| 463 |
f2.type = Type.terror; |
| 464 |
f2.errors = true; |
| 465 |
return 0; |
| 466 |
}); |
| 467 |
} |
| 468 |
if (!fd.type || fd.type.ty != Tfunction) |
| 469 |
return; |
| 470 |
TypeFunction f = cast(TypeFunction) fd.type; |
| 471 |
|
| 472 |
checkGNUABITag(fd, fd._linkage); |
| 473 |
//semantic for parameters' UDAs |
| 474 |
foreach (i, param; f.parameterList) |
| 475 |
{ |
| 476 |
if (param && param.userAttribDecl) |
| 477 |
param.userAttribDecl.semantic2(sc); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
override void visit(Import i) |
| 482 |
{ |
| 483 |
//printf("Import::semantic2('%s')\n", toChars()); |
| 484 |
if (!i.mod) |
| 485 |
return; |
| 486 |
|
| 487 |
i.mod.semantic2(null); |
| 488 |
if (i.mod.needmoduleinfo) |
| 489 |
{ |
| 490 |
//printf("module5 %s because of %s\n", sc._module.toChars(), mod.toChars()); |
| 491 |
if (sc) |
| 492 |
sc._module.needmoduleinfo = 1; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
override void visit(Nspace ns) |
| 497 |
{ |
| 498 |
if (ns.semanticRun >= PASS.semantic2) |
| 499 |
return; |
| 500 |
ns.semanticRun = PASS.semantic2; |
| 501 |
static if (LOG) |
| 502 |
{ |
| 503 |
printf("+Nspace::semantic2('%s')\n", ns.toChars()); |
| 504 |
scope(exit) printf("-Nspace::semantic2('%s')\n", ns.toChars()); |
| 505 |
} |
| 506 |
checkGNUABITag(ns, LINK.cpp); |
| 507 |
if (!ns.members) |
| 508 |
return; |
| 509 |
|
| 510 |
assert(sc); |
| 511 |
sc = sc.push(ns); |
| 512 |
sc.linkage = LINK.cpp; |
| 513 |
foreach (s; *ns.members) |
| 514 |
{ |
| 515 |
static if (LOG) |
| 516 |
{ |
| 517 |
printf("\tmember '%s', kind = '%s'\n", s.toChars(), s.kind()); |
| 518 |
} |
| 519 |
s.semantic2(sc); |
| 520 |
} |
| 521 |
sc.pop(); |
| 522 |
} |
| 523 |
|
| 524 |
override void visit(AttribDeclaration ad) |
| 525 |
{ |
| 526 |
Dsymbols* d = ad.include(sc); |
| 527 |
if (!d) |
| 528 |
return; |
| 529 |
|
| 530 |
Scope* sc2 = ad.newScope(sc); |
| 531 |
for (size_t i = 0; i < d.length; i++) |
| 532 |
{ |
| 533 |
Dsymbol s = (*d)[i]; |
| 534 |
s.semantic2(sc2); |
| 535 |
} |
| 536 |
if (sc2 != sc) |
| 537 |
sc2.pop(); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Run the DeprecatedDeclaration's semantic2 phase then its members. |
| 542 |
* |
| 543 |
* The message set via a `DeprecatedDeclaration` can be either of: |
| 544 |
* - a string literal |
| 545 |
* - an enum |
| 546 |
* - a static immutable |
| 547 |
* So we need to call ctfe to resolve it. |
| 548 |
* Afterward forwards to the members' semantic2. |
| 549 |
*/ |
| 550 |
override void visit(DeprecatedDeclaration dd) |
| 551 |
{ |
| 552 |
getMessage(dd); |
| 553 |
visit(cast(AttribDeclaration)dd); |
| 554 |
} |
| 555 |
|
| 556 |
override void visit(AlignDeclaration ad) |
| 557 |
{ |
| 558 |
ad.getAlignment(sc); |
| 559 |
visit(cast(AttribDeclaration)ad); |
| 560 |
} |
| 561 |
|
| 562 |
override void visit(CPPNamespaceDeclaration decl) |
| 563 |
{ |
| 564 |
checkGNUABITag(decl, LINK.cpp); |
| 565 |
visit(cast(AttribDeclaration)decl); |
| 566 |
} |
| 567 |
|
| 568 |
override void visit(UserAttributeDeclaration uad) |
| 569 |
{ |
| 570 |
if (!uad.decl || !uad.atts || !uad.atts.length || !uad._scope) |
| 571 |
return visit(cast(AttribDeclaration)uad); |
| 572 |
|
| 573 |
Expression* lastTag; |
| 574 |
static void eval(Scope* sc, Expressions* exps, ref Expression* lastTag) |
| 575 |
{ |
| 576 |
foreach (ref Expression e; *exps) |
| 577 |
{ |
| 578 |
if (!e) |
| 579 |
continue; |
| 580 |
|
| 581 |
e = e.expressionSemantic(sc); |
| 582 |
if (definitelyValueParameter(e)) |
| 583 |
e = e.ctfeInterpret(); |
| 584 |
if (e.op == EXP.tuple) |
| 585 |
{ |
| 586 |
TupleExp te = cast(TupleExp)e; |
| 587 |
eval(sc, te.exps, lastTag); |
| 588 |
} |
| 589 |
|
| 590 |
// Handles compiler-recognized `core.attribute.gnuAbiTag` |
| 591 |
if (isGNUABITag(e)) |
| 592 |
doGNUABITagSemantic(e, lastTag); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
uad._scope = null; |
| 597 |
eval(sc, uad.atts, lastTag); |
| 598 |
visit(cast(AttribDeclaration)uad); |
| 599 |
} |
| 600 |
|
| 601 |
override void visit(AggregateDeclaration ad) |
| 602 |
{ |
| 603 |
//printf("AggregateDeclaration::semantic2(%s) type = %s, errors = %d\n", ad.toChars(), ad.type.toChars(), ad.errors); |
| 604 |
if (!ad.members) |
| 605 |
return; |
| 606 |
|
| 607 |
if (ad._scope) |
| 608 |
{ |
| 609 |
.error(ad.loc, "%s `%s` has forward references", ad.kind, ad.toPrettyChars); |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
checkGNUABITag(ad, ad.classKind == ClassKind.cpp ? LINK.cpp : LINK.d); |
| 614 |
|
| 615 |
auto sc2 = ad.newScope(sc); |
| 616 |
|
| 617 |
ad.determineSize(ad.loc); |
| 618 |
|
| 619 |
for (size_t i = 0; i < ad.members.length; i++) |
| 620 |
{ |
| 621 |
Dsymbol s = (*ad.members)[i]; |
| 622 |
//printf("\t[%d] %s\n", i, s.toChars()); |
| 623 |
s.semantic2(sc2); |
| 624 |
} |
| 625 |
|
| 626 |
sc2.pop(); |
| 627 |
} |
| 628 |
|
| 629 |
override void visit(ClassDeclaration cd) |
| 630 |
{ |
| 631 |
/// Checks that the given class implements all methods of its interfaces. |
| 632 |
static void checkInterfaceImplementations(ClassDeclaration cd) |
| 633 |
{ |
| 634 |
foreach (base; cd.interfaces) |
| 635 |
{ |
| 636 |
// https://issues.dlang.org/show_bug.cgi?id=22729 |
| 637 |
// interfaces that have errors or that |
| 638 |
// inherit from interfaces that have errors |
| 639 |
// might have an uninitialized vtable |
| 640 |
if (!base.sym.vtbl.length) |
| 641 |
continue; |
| 642 |
|
| 643 |
// first entry is ClassInfo reference |
| 644 |
auto methods = base.sym.vtbl[base.sym.vtblOffset .. $]; |
| 645 |
|
| 646 |
foreach (m; methods) |
| 647 |
{ |
| 648 |
auto ifd = m.isFuncDeclaration; |
| 649 |
assert(ifd); |
| 650 |
|
| 651 |
if (ifd.objc.isOptional) |
| 652 |
continue; |
| 653 |
|
| 654 |
auto type = ifd.type.toTypeFunction(); |
| 655 |
auto fd = cd.findFunc(ifd.ident, type); |
| 656 |
|
| 657 |
if (fd && !fd.isAbstract) |
| 658 |
{ |
| 659 |
//printf(" found\n"); |
| 660 |
// Check that calling conventions match |
| 661 |
if (fd._linkage != ifd._linkage) |
| 662 |
.error(fd.loc, "%s `%s` linkage doesn't match interface function", fd.kind, fd.toPrettyChars); |
| 663 |
|
| 664 |
// Check that it is current |
| 665 |
//printf("newinstance = %d fd.toParent() = %s ifd.toParent() = %s\n", |
| 666 |
//newinstance, fd.toParent().toChars(), ifd.toParent().toChars()); |
| 667 |
if (fd.toParent() != cd && ifd.toParent() == base.sym) |
| 668 |
.error(cd.loc, "%s `%s` interface function `%s` is not implemented", cd.kind, cd.toPrettyChars, ifd.toFullSignature()); |
| 669 |
} |
| 670 |
else |
| 671 |
{ |
| 672 |
//printf(" not found %p\n", fd); |
| 673 |
// BUG: should mark this class as abstract? |
| 674 |
if (!cd.isAbstract()) |
| 675 |
.error(cd.loc, "%s `%s` interface function `%s` is not implemented", cd.kind, cd.toPrettyChars, ifd.toFullSignature()); |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
if (cd.semanticRun >= PASS.semantic2done) |
| 682 |
return; |
| 683 |
assert(cd.semanticRun <= PASS.semantic2); |
| 684 |
cd.semanticRun = PASS.semantic2; |
| 685 |
|
| 686 |
checkInterfaceImplementations(cd); |
| 687 |
visit(cast(AggregateDeclaration) cd); |
| 688 |
} |
| 689 |
|
| 690 |
override void visit(InterfaceDeclaration cd) |
| 691 |
{ |
| 692 |
visit(cast(AggregateDeclaration) cd); |
| 693 |
} |
| 694 |
|
| 695 |
override void visit(TupleDeclaration td) |
| 696 |
{ |
| 697 |
td.foreachVar((s) { s.accept(this); }); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Perform semantic analysis specific to the GNU ABI tags |
| 703 |
* |
| 704 |
* The GNU ABI tags are a feature introduced in C++11, specific to g++ |
| 705 |
* and the Itanium ABI. |
| 706 |
* They are mandatory for C++ interfacing, simply because the templated struct |
| 707 |
*`std::basic_string`, of which the ubiquitous `std::string` is a instantiation |
| 708 |
* of, uses them. |
| 709 |
* |
| 710 |
* Params: |
| 711 |
* e = Expression to perform semantic on |
| 712 |
* See `Semantic2Visitor.visit(UserAttributeDeclaration)` |
| 713 |
* lastTag = When `!is null`, we already saw an ABI tag. |
| 714 |
* To simplify implementation and reflection code, |
| 715 |
* only one ABI tag object is allowed per symbol |
| 716 |
* (but it can have multiple tags as it's an array exp). |
| 717 |
*/ |
| 718 |
private void doGNUABITagSemantic(ref Expression e, ref Expression* lastTag) |
| 719 |
{ |
| 720 |
import dmd.mangle : isValidMangling; |
| 721 |
// When `@gnuAbiTag` is used, the type will be the UDA, not the struct literal |
| 722 |
if (e.op == EXP.type) |
| 723 |
{ |
| 724 |
error(e.loc, "`@%s` at least one argument expected", Id.udaGNUAbiTag.toChars()); |
| 725 |
return; |
| 726 |
} |
| 727 |
|
| 728 |
// Definition is in `core.attributes`. If it's not a struct literal, |
| 729 |
// it shouldn't have passed semantic, hence the `assert`. |
| 730 |
auto sle = e.isStructLiteralExp(); |
| 731 |
if (sle is null) |
| 732 |
{ |
| 733 |
assert(global.errors); |
| 734 |
return; |
| 735 |
} |
| 736 |
// The definition of `gnuAttributes` only have 1 member, `string[] tags` |
| 737 |
assert(sle.elements && sle.elements.length == 1); |
| 738 |
// `gnuAbiTag`'s constructor is defined as `this(string[] tags...)` |
| 739 |
auto ale = (*sle.elements)[0].isArrayLiteralExp(); |
| 740 |
if (ale is null) |
| 741 |
{ |
| 742 |
error(e.loc, "`@%s` at least one argument expected", Id.udaGNUAbiTag.toChars()); |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
// Check that it's the only tag on the symbol |
| 747 |
if (lastTag !is null) |
| 748 |
{ |
| 749 |
const str1 = (*lastTag.isStructLiteralExp().elements)[0].toString(); |
| 750 |
const str2 = ale.toString(); |
| 751 |
error(e.loc, "only one `@%s` allowed per symbol", Id.udaGNUAbiTag.toChars()); |
| 752 |
errorSupplemental(e.loc, "instead of `@%s @%s`, use `@%s(%.*s, %.*s)`", |
| 753 |
lastTag.toChars(), e.toChars(), Id.udaGNUAbiTag.toChars(), |
| 754 |
// Avoid [ ... ] |
| 755 |
cast(int)str1.length - 2, str1.ptr + 1, |
| 756 |
cast(int)str2.length - 2, str2.ptr + 1); |
| 757 |
return; |
| 758 |
} |
| 759 |
lastTag = &e; |
| 760 |
|
| 761 |
// We already know we have a valid array literal of strings. |
| 762 |
// Now checks that elements are valid. |
| 763 |
foreach (idx, elem; *ale.elements) |
| 764 |
{ |
| 765 |
const str = elem.toStringExp().peekString(); |
| 766 |
if (!str.length) |
| 767 |
{ |
| 768 |
error(e.loc, "argument `%d` to `@%s` cannot be %s", cast(int)(idx + 1), |
| 769 |
Id.udaGNUAbiTag.toChars(), |
| 770 |
elem.isNullExp() ? "`null`".ptr : "empty".ptr); |
| 771 |
continue; |
| 772 |
} |
| 773 |
|
| 774 |
foreach (c; str) |
| 775 |
{ |
| 776 |
if (!c.isValidMangling()) |
| 777 |
{ |
| 778 |
error(e.loc, "`@%s` char `0x%02x` not allowed in mangling", |
| 779 |
Id.udaGNUAbiTag.toChars(), c); |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
// Valid element |
| 784 |
} |
| 785 |
// Since ABI tags need to be sorted, we sort them in place |
| 786 |
// It might be surprising for users that inspects the UDAs, |
| 787 |
// but it's a concession to practicality. |
| 788 |
// Casts are unfortunately necessary as `implicitConvTo` is not |
| 789 |
// `const` (and nor is `StringExp`, by extension). |
| 790 |
static int predicate(const scope Expression* e1, const scope Expression* e2) |
| 791 |
{ |
| 792 |
return (cast(Expression*)e1).toStringExp().compare((cast(Expression*)e2).toStringExp()); |
| 793 |
} |
| 794 |
ale.elements.sort!predicate; |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* Try lower a variable's Associative Array initializer to a newaa struct |
| 799 |
* so it can be put in static data. |
| 800 |
* Params: |
| 801 |
* vd = Variable to lower |
| 802 |
* sc = Scope |
| 803 |
*/ |
| 804 |
void lowerStaticAAs(VarDeclaration vd, Scope* sc) |
| 805 |
{ |
| 806 |
if (vd.storage_class & STC.manifest) |
| 807 |
return; |
| 808 |
if (auto ei = vd._init.isExpInitializer()) |
| 809 |
lowerStaticAAs(ei.exp, sc); |
| 810 |
} |
| 811 |
|
| 812 |
/** |
| 813 |
* Try lower all Associative Array literals in an expression to a newaa struct |
| 814 |
* so it can be put in static data. |
| 815 |
* Params: |
| 816 |
* e = Expression to traverse |
| 817 |
* sc = Scope |
| 818 |
*/ |
| 819 |
void lowerStaticAAs(Expression e, Scope* sc) |
| 820 |
{ |
| 821 |
scope v = new StaticAAVisitor(sc); |
| 822 |
e.accept(v); |
| 823 |
} |
| 824 |
|
| 825 |
/// Visit Associative Array literals and lower them to structs for static initialization |
| 826 |
private extern(C++) final class StaticAAVisitor : SemanticTimeTransitiveVisitor |
| 827 |
{ |
| 828 |
alias visit = SemanticTimeTransitiveVisitor.visit; |
| 829 |
Scope* sc; |
| 830 |
|
| 831 |
this(Scope* sc) scope @safe |
| 832 |
{ |
| 833 |
this.sc = sc; |
| 834 |
} |
| 835 |
|
| 836 |
override void visit(AssocArrayLiteralExp aaExp) |
| 837 |
{ |
| 838 |
if (!verifyHookExist(aaExp.loc, *sc, Id._aaAsStruct, "initializing static associative arrays", Id.object)) |
| 839 |
return; |
| 840 |
|
| 841 |
Expression hookFunc = new IdentifierExp(aaExp.loc, Id.empty); |
| 842 |
hookFunc = new DotIdExp(aaExp.loc, hookFunc, Id.object); |
| 843 |
hookFunc = new DotIdExp(aaExp.loc, hookFunc, Id._aaAsStruct); |
| 844 |
auto arguments = new Expressions(); |
| 845 |
arguments.push(aaExp); |
| 846 |
Expression loweredExp = new CallExp(aaExp.loc, hookFunc, arguments); |
| 847 |
|
| 848 |
sc = sc.startCTFE(); |
| 849 |
loweredExp = loweredExp.expressionSemantic(sc); |
| 850 |
loweredExp = resolveProperties(sc, loweredExp); |
| 851 |
sc = sc.endCTFE(); |
| 852 |
loweredExp = loweredExp.ctfeInterpret(); |
| 853 |
|
| 854 |
aaExp.lowering = loweredExp; |
| 855 |
|
| 856 |
semanticTypeInfo(sc, loweredExp.type); |
| 857 |
} |
| 858 |
|
| 859 |
// https://issues.dlang.org/show_bug.cgi?id=24602 |
| 860 |
// TODO: Is this intionally not visited by SemanticTimeTransitiveVisitor? |
| 861 |
override void visit(ClassReferenceExp crExp) |
| 862 |
{ |
| 863 |
this.visit(crExp.value); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
/** |
| 868 |
* Given a static assert with a failing condition, print an error |
| 869 |
* Params: |
| 870 |
* sa = Static assert with failing condition |
| 871 |
* sc = scope for evaluating assert message and printing context |
| 872 |
*/ |
| 873 |
void staticAssertFail(StaticAssert sa, Scope* sc) |
| 874 |
{ |
| 875 |
if (sa.msgs) |
| 876 |
{ |
| 877 |
OutBuffer msgbuf; |
| 878 |
for (size_t i = 0; i < sa.msgs.length; i++) |
| 879 |
{ |
| 880 |
Expression e = (*sa.msgs)[i]; |
| 881 |
sc = sc.startCTFE(); |
| 882 |
e = e.expressionSemantic(sc); |
| 883 |
e = resolveProperties(sc, e); |
| 884 |
sc = sc.endCTFE(); |
| 885 |
e = ctfeInterpretForPragmaMsg(e); |
| 886 |
if (e.op == EXP.error) |
| 887 |
{ |
| 888 |
errorSupplemental(sa.loc, "while evaluating `static assert` argument `%s`", (*sa.msgs)[i].toChars()); |
| 889 |
if (!global.gag) |
| 890 |
fatal(); |
| 891 |
return; |
| 892 |
} |
| 893 |
if (StringExp se = e.toStringExp()) |
| 894 |
{ |
| 895 |
const slice = se.toUTF8(sc).peekString(); |
| 896 |
// Hack to keep old formatting to avoid changing error messages everywhere |
| 897 |
if (sa.msgs.length == 1) |
| 898 |
msgbuf.printf("\"%.*s\"", cast(int)slice.length, slice.ptr); |
| 899 |
else |
| 900 |
msgbuf.printf("%.*s", cast(int)slice.length, slice.ptr); |
| 901 |
} |
| 902 |
else |
| 903 |
msgbuf.printf("%s", e.toChars()); |
| 904 |
} |
| 905 |
error(sa.loc, "static assert: %s", msgbuf.extractChars()); |
| 906 |
} |
| 907 |
else |
| 908 |
error(sa.loc, "static assert: `%s` is false", sa.exp.toChars()); |
| 909 |
if (sc.tinst) |
| 910 |
sc.tinst.printInstantiationTrace(); |
| 911 |
if (!global.gag) |
| 912 |
fatal(); |
| 913 |
} |