| 1 |
/** |
| 2 |
* Handle introspection functionality of the `__traits()` construct. |
| 3 |
* |
| 4 |
* Specification: $(LINK2 https://dlang.org/spec/traits.html, Traits) |
| 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/traits.d, _traits.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_traits.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/traits.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.traits; |
| 15 |
|
| 16 |
import core.stdc.stdio; |
| 17 |
|
| 18 |
import dmd.aggregate; |
| 19 |
import dmd.arraytypes; |
| 20 |
import dmd.astcodegen; |
| 21 |
import dmd.astenums; |
| 22 |
import dmd.attrib; |
| 23 |
import dmd.attribsem; |
| 24 |
import dmd.canthrow; |
| 25 |
import dmd.dclass; |
| 26 |
import dmd.declaration; |
| 27 |
import dmd.dimport; |
| 28 |
import dmd.dinterpret; |
| 29 |
import dmd.dmodule; |
| 30 |
import dmd.dscope; |
| 31 |
import dmd.dsymbol; |
| 32 |
import dmd.dsymbolsem; |
| 33 |
import dmd.dtemplate; |
| 34 |
import dmd.errors; |
| 35 |
import dmd.errorsink; |
| 36 |
import dmd.expression; |
| 37 |
import dmd.expressionsem; |
| 38 |
import dmd.func; |
| 39 |
import dmd.funcsem; |
| 40 |
import dmd.globals; |
| 41 |
import dmd.hdrgen; |
| 42 |
import dmd.id; |
| 43 |
import dmd.identifier; |
| 44 |
import dmd.location; |
| 45 |
import dmd.mangle : decoToType; |
| 46 |
import dmd.mtype; |
| 47 |
import dmd.nogc; |
| 48 |
import dmd.optimize; |
| 49 |
import dmd.parse; |
| 50 |
import dmd.root.array; |
| 51 |
import dmd.root.speller; |
| 52 |
import dmd.root.stringtable; |
| 53 |
import dmd.target; |
| 54 |
import dmd.tokens; |
| 55 |
import dmd.typesem; |
| 56 |
import dmd.visitor; |
| 57 |
import dmd.rootobject; |
| 58 |
import dmd.common.outbuffer; |
| 59 |
import dmd.root.string; |
| 60 |
|
| 61 |
enum LOGSEMANTIC = false; |
| 62 |
|
| 63 |
/************************ TraitsExp ************************************/ |
| 64 |
|
| 65 |
/************************************** |
| 66 |
* Convert `Expression` or `Type` to corresponding `Dsymbol`, additionally |
| 67 |
* stripping off expression contexts. |
| 68 |
* |
| 69 |
* Some symbol related `__traits` ignore arguments expression contexts. |
| 70 |
* For example: |
| 71 |
* ---- |
| 72 |
* struct S { void f() {} } |
| 73 |
* S s; |
| 74 |
* pragma(msg, __traits(isNested, s.f)); |
| 75 |
* // s.f is `DotVarExp`, but `__traits(isNested)`` needs a `FuncDeclaration`. |
| 76 |
* ---- |
| 77 |
* |
| 78 |
* This is used for that common `__traits` behavior. |
| 79 |
* |
| 80 |
* Input: |
| 81 |
* oarg object to get the symbol for |
| 82 |
* Returns: |
| 83 |
* Dsymbol the corresponding symbol for oarg |
| 84 |
*/ |
| 85 |
private Dsymbol getDsymbolWithoutExpCtx(RootObject oarg) |
| 86 |
{ |
| 87 |
if (auto e = isExpression(oarg)) |
| 88 |
{ |
| 89 |
if (auto dve = e.isDotVarExp()) |
| 90 |
return dve.var; |
| 91 |
if (auto dte = e.isDotTemplateExp()) |
| 92 |
return dte.td; |
| 93 |
} |
| 94 |
return getDsymbol(oarg); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Fill an array of target size_t values that indicate possible pointer words in memory |
| 99 |
* if interpreted as the type given as argument. |
| 100 |
* One bit in the array per pointer-sized piece of memory |
| 101 |
* Params: |
| 102 |
* loc = location for error messages |
| 103 |
* t = type to generate pointer bitmap from |
| 104 |
* data = array forming the bitmap |
| 105 |
* eSink = error message sink |
| 106 |
* Returns: |
| 107 |
* size of the type `t` in bytes, ulong.max on error |
| 108 |
*/ |
| 109 |
ulong getTypePointerBitmap(Loc loc, Type t, ref Array!(ulong) data, ErrorSink eSink) |
| 110 |
{ |
| 111 |
auto tc = t.isTypeClass(); |
| 112 |
const ulong sz = (tc && !tc.sym.isInterfaceDeclaration()) |
| 113 |
? tc.sym.AggregateDeclaration.size(loc) |
| 114 |
: t.size(loc); |
| 115 |
if (sz == SIZE_INVALID) |
| 116 |
return ulong.max; |
| 117 |
|
| 118 |
const sz_size_t = Type.tsize_t.size(loc); // size of target's size_t |
| 119 |
assert(sz_size_t <= ulong.sizeof); |
| 120 |
if (sz > sz.max - sz_size_t) |
| 121 |
{ |
| 122 |
eSink.error(loc, "size overflow for type `%s`", t.toChars()); |
| 123 |
return ulong.max; |
| 124 |
} |
| 125 |
|
| 126 |
const ulong bitsPerElement = sz_size_t * 8; // bits used in each array element |
| 127 |
const ulong cntptr = (sz + sz_size_t - 1) / sz_size_t; // pointers have same size as sz_size_t |
| 128 |
const ulong length = (cntptr + bitsPerElement - 1) / bitsPerElement; // a bit per pointer |
| 129 |
|
| 130 |
data.setDim(cast(size_t)length); |
| 131 |
data.zero(); |
| 132 |
|
| 133 |
ulong offset; |
| 134 |
bool error; // sticky error indicator |
| 135 |
|
| 136 |
void visit(Type t) |
| 137 |
{ |
| 138 |
void setpointer(ulong off) |
| 139 |
{ |
| 140 |
ulong ptroff = off / sz_size_t; |
| 141 |
data[cast(size_t)(ptroff / bitsPerElement)] |= 1L << (ptroff % bitsPerElement); |
| 142 |
} |
| 143 |
|
| 144 |
void visitType(Type t) |
| 145 |
{ |
| 146 |
Type tb = t.toBasetype(); |
| 147 |
if (tb != t) |
| 148 |
visit(tb); |
| 149 |
} |
| 150 |
|
| 151 |
void visitError(TypeError t) |
| 152 |
{ |
| 153 |
visitType(t); |
| 154 |
} |
| 155 |
|
| 156 |
void visitBasic(TypeBasic t) |
| 157 |
{ |
| 158 |
if (t.ty == Tvoid) |
| 159 |
setpointer(offset); |
| 160 |
} |
| 161 |
|
| 162 |
void visitVector(TypeVector t) |
| 163 |
{ |
| 164 |
} |
| 165 |
|
| 166 |
void visitSArray(TypeSArray t) |
| 167 |
{ |
| 168 |
ulong arrayoff = offset; |
| 169 |
ulong nextsize = t.next.size(); |
| 170 |
if (nextsize == SIZE_INVALID) |
| 171 |
error = true; |
| 172 |
ulong dim = t.dim.toInteger(); |
| 173 |
for (ulong i = 0; i < dim; i++) |
| 174 |
{ |
| 175 |
offset = arrayoff + i * nextsize; |
| 176 |
visit(t.next); |
| 177 |
} |
| 178 |
offset = arrayoff; |
| 179 |
} |
| 180 |
|
| 181 |
void visitDArray(TypeDArray t) |
| 182 |
{ |
| 183 |
setpointer(offset + sz_size_t); |
| 184 |
} |
| 185 |
|
| 186 |
// dynamic array is {length,ptr} |
| 187 |
void visitAArray(TypeAArray t) |
| 188 |
{ |
| 189 |
setpointer(offset); |
| 190 |
} |
| 191 |
|
| 192 |
void visitPointer(TypePointer t) |
| 193 |
{ |
| 194 |
if (t.nextOf().ty != Tfunction) // don't mark function pointers |
| 195 |
setpointer(offset); |
| 196 |
} |
| 197 |
|
| 198 |
void visitReference(TypeReference t) |
| 199 |
{ |
| 200 |
setpointer(offset); |
| 201 |
} |
| 202 |
|
| 203 |
void visitClass(TypeClass t) |
| 204 |
{ |
| 205 |
setpointer(offset); |
| 206 |
} |
| 207 |
|
| 208 |
void visitFunction(TypeFunction t) |
| 209 |
{ |
| 210 |
} |
| 211 |
|
| 212 |
void visitDelegate(TypeDelegate t) |
| 213 |
{ |
| 214 |
setpointer(offset); |
| 215 |
} |
| 216 |
|
| 217 |
void visitEnum(TypeEnum t) |
| 218 |
{ |
| 219 |
visitType(t); |
| 220 |
} |
| 221 |
|
| 222 |
void visitTuple(TypeTuple t) |
| 223 |
{ |
| 224 |
visitType(t); |
| 225 |
} |
| 226 |
|
| 227 |
void visitNull(TypeNull t) |
| 228 |
{ |
| 229 |
// always a null pointer |
| 230 |
} |
| 231 |
|
| 232 |
void visitNoreturn(TypeNoreturn t) |
| 233 |
{ |
| 234 |
} |
| 235 |
|
| 236 |
void visitStruct(TypeStruct t) |
| 237 |
{ |
| 238 |
ulong structoff = offset; |
| 239 |
foreach (v; t.sym.fields) |
| 240 |
{ |
| 241 |
offset = structoff + v.offset; |
| 242 |
if (v.type.ty == Tclass) |
| 243 |
setpointer(offset); |
| 244 |
else |
| 245 |
visit(v.type); |
| 246 |
} |
| 247 |
offset = structoff; |
| 248 |
} |
| 249 |
|
| 250 |
void visitDefaultCase(Type t) |
| 251 |
{ |
| 252 |
//printf("ty = %d\n", t.ty); |
| 253 |
assert(0); |
| 254 |
} |
| 255 |
|
| 256 |
mixin VisitType!void visit; |
| 257 |
visit.VisitType(t); |
| 258 |
} |
| 259 |
|
| 260 |
if (auto tcx = t.isTypeClass()) |
| 261 |
{ |
| 262 |
// a "toplevel" class is treated as an instance, while TypeClass fields are treated as references |
| 263 |
void visitTopLevelClass(TypeClass t) |
| 264 |
{ |
| 265 |
ulong classoff = offset; |
| 266 |
// skip vtable-ptr and monitor |
| 267 |
if (t.sym.baseClass) |
| 268 |
visitTopLevelClass(t.sym.baseClass.type.isTypeClass()); |
| 269 |
foreach (v; t.sym.fields) |
| 270 |
{ |
| 271 |
offset = classoff + v.offset; |
| 272 |
visit(v.type); |
| 273 |
} |
| 274 |
offset = classoff; |
| 275 |
} |
| 276 |
|
| 277 |
visitTopLevelClass(tcx); |
| 278 |
} |
| 279 |
else |
| 280 |
visit(t); |
| 281 |
return error ? ulong.max : sz; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* get an array of size_t values that indicate possible pointer words in memory |
| 286 |
* if interpreted as the type given as argument |
| 287 |
* the first array element is the size of the type for independent interpretation |
| 288 |
* of the array |
| 289 |
* following elements bits represent one word (4/8 bytes depending on the target |
| 290 |
* architecture). If set the corresponding memory might contain a pointer/reference. |
| 291 |
* |
| 292 |
* Returns: [T.sizeof, pointerbit0-31/63, pointerbit32/64-63/128, ...] |
| 293 |
*/ |
| 294 |
private Expression pointerBitmap(TraitsExp e, ErrorSink eSink) |
| 295 |
{ |
| 296 |
if (!e.args || e.args.length != 1) |
| 297 |
{ |
| 298 |
eSink.error(e.loc, "a single type expected for trait pointerBitmap"); |
| 299 |
return ErrorExp.get(); |
| 300 |
} |
| 301 |
|
| 302 |
Type t = getType((*e.args)[0]); |
| 303 |
if (!t) |
| 304 |
{ |
| 305 |
eSink.error(e.loc, "`%s` is not a type", (*e.args)[0].toChars()); |
| 306 |
return ErrorExp.get(); |
| 307 |
} |
| 308 |
|
| 309 |
Array!(ulong) data; |
| 310 |
const ulong sz = getTypePointerBitmap(e.loc, t, data, eSink); |
| 311 |
if (sz == ulong.max) |
| 312 |
return ErrorExp.get(); |
| 313 |
|
| 314 |
auto exps = new Expressions(data.length + 1); |
| 315 |
(*exps)[0] = new IntegerExp(e.loc, sz, Type.tsize_t); // [0] is size in bytes of t |
| 316 |
foreach (size_t i; 1 .. exps.length) |
| 317 |
(*exps)[i] = new IntegerExp(e.loc, data[cast(size_t) (i - 1)], Type.tsize_t); |
| 318 |
|
| 319 |
auto ale = new ArrayLiteralExp(e.loc, Type.tsize_t.sarrayOf(data.length + 1), exps); |
| 320 |
return ale; |
| 321 |
} |
| 322 |
|
| 323 |
Expression semanticTraits(TraitsExp e, Scope* sc) |
| 324 |
{ |
| 325 |
static if (LOGSEMANTIC) |
| 326 |
{ |
| 327 |
printf("TraitsExp::semantic() %s\n", e.toChars()); |
| 328 |
} |
| 329 |
|
| 330 |
if (e.ident != Id.compiles && |
| 331 |
e.ident != Id.isSame && |
| 332 |
e.ident != Id.identifier && |
| 333 |
e.ident != Id.getProtection && e.ident != Id.getVisibility && |
| 334 |
e.ident != Id.getAttributes) |
| 335 |
{ |
| 336 |
// Pretend we're in a deprecated scope so that deprecation messages |
| 337 |
// aren't triggered when checking if a symbol is deprecated |
| 338 |
const save = sc.stc; |
| 339 |
if (e.ident == Id.isDeprecated) |
| 340 |
sc.stc |= STC.deprecated_; |
| 341 |
Scope* sc2 = sc.startCTFE(); |
| 342 |
scope(exit) { sc2.endCTFE(); } |
| 343 |
if (!TemplateInstance.semanticTiargs(e.loc, sc2, e.args, 1)) |
| 344 |
{ |
| 345 |
sc.stc = save; |
| 346 |
return ErrorExp.get(); |
| 347 |
} |
| 348 |
sc.stc = save; |
| 349 |
} |
| 350 |
size_t dim = e.args ? e.args.length : 0; |
| 351 |
|
| 352 |
Expression dimError(int expected) |
| 353 |
{ |
| 354 |
error(e.loc, "expected %d arguments for `%s` but had %d", expected, e.ident.toChars(), cast(int)dim); |
| 355 |
return ErrorExp.get(); |
| 356 |
} |
| 357 |
|
| 358 |
static IntegerExp True() |
| 359 |
{ |
| 360 |
return IntegerExp.createBool(true); |
| 361 |
} |
| 362 |
|
| 363 |
static IntegerExp False() |
| 364 |
{ |
| 365 |
return IntegerExp.createBool(false); |
| 366 |
} |
| 367 |
|
| 368 |
/******** |
| 369 |
* Gets the function type from a given AST node |
| 370 |
* if the node is a function of some sort. |
| 371 |
* Params: |
| 372 |
* o = an AST node to check for a `TypeFunction` |
| 373 |
* fdp = if `o` is a FuncDeclaration then fdp is set to that, otherwise `null` |
| 374 |
* Returns: |
| 375 |
* a type node if `o` is a declaration of |
| 376 |
* a delegate, function, function-pointer or a variable of the former. |
| 377 |
* Otherwise, `null`. |
| 378 |
*/ |
| 379 |
static TypeFunction toTypeFunction(RootObject o, out FuncDeclaration fdp) |
| 380 |
{ |
| 381 |
Type t; |
| 382 |
if (auto s = getDsymbolWithoutExpCtx(o)) |
| 383 |
{ |
| 384 |
if (auto fd = s.isFuncDeclaration()) |
| 385 |
{ |
| 386 |
t = fd.type; |
| 387 |
fdp = fd; |
| 388 |
} |
| 389 |
else if (auto vd = s.isVarDeclaration()) |
| 390 |
t = vd.type; |
| 391 |
else |
| 392 |
t = isType(o); |
| 393 |
} |
| 394 |
else |
| 395 |
t = isType(o); |
| 396 |
|
| 397 |
if (t) |
| 398 |
{ |
| 399 |
if (auto tf = t.isFunction_Delegate_PtrToFunction()) |
| 400 |
return tf; |
| 401 |
} |
| 402 |
|
| 403 |
return null; |
| 404 |
} |
| 405 |
|
| 406 |
IntegerExp isX(T)(bool delegate(T) fp) |
| 407 |
{ |
| 408 |
if (!dim) |
| 409 |
return False(); |
| 410 |
foreach (o; *e.args) |
| 411 |
{ |
| 412 |
static if (is(T == Type)) |
| 413 |
auto y = getType(o); |
| 414 |
|
| 415 |
static if (is(T : Dsymbol)) |
| 416 |
{ |
| 417 |
auto s = getDsymbolWithoutExpCtx(o); |
| 418 |
if (!s) |
| 419 |
return False(); |
| 420 |
} |
| 421 |
static if (is(T == Dsymbol)) |
| 422 |
alias y = s; |
| 423 |
static if (is(T == Declaration)) |
| 424 |
auto y = s.isDeclaration(); |
| 425 |
static if (is(T == FuncDeclaration)) |
| 426 |
auto y = s.isFuncDeclaration(); |
| 427 |
|
| 428 |
if (!y || !fp(y)) |
| 429 |
return False(); |
| 430 |
} |
| 431 |
return True(); |
| 432 |
} |
| 433 |
|
| 434 |
alias isTypeX = isX!Type; |
| 435 |
alias isDsymX = isX!Dsymbol; |
| 436 |
alias isDeclX = isX!Declaration; |
| 437 |
alias isFuncX = isX!FuncDeclaration; |
| 438 |
|
| 439 |
Expression isPkgX(bool function(Package) fp) |
| 440 |
{ |
| 441 |
return isDsymX((Dsymbol sym) { |
| 442 |
Package p = resolveIsPackage(sym); |
| 443 |
return (p !is null) && fp(p); |
| 444 |
}); |
| 445 |
} |
| 446 |
|
| 447 |
if (e.ident == Id.isArithmetic) |
| 448 |
{ |
| 449 |
return isTypeX(t => t.isIntegral() || t.isFloating()); |
| 450 |
} |
| 451 |
if (e.ident == Id.isFloating) |
| 452 |
{ |
| 453 |
return isTypeX(t => t.isFloating()); |
| 454 |
} |
| 455 |
if (e.ident == Id.isIntegral) |
| 456 |
{ |
| 457 |
return isTypeX(t => t.isIntegral()); |
| 458 |
} |
| 459 |
if (e.ident == Id.isScalar) |
| 460 |
{ |
| 461 |
return isTypeX(t => t.isScalar()); |
| 462 |
} |
| 463 |
if (e.ident == Id.isUnsigned) |
| 464 |
{ |
| 465 |
return isTypeX(t => t.isUnsigned()); |
| 466 |
} |
| 467 |
if (e.ident == Id.isAssociativeArray) |
| 468 |
{ |
| 469 |
return isTypeX(t => t.toBasetype().ty == Taarray); |
| 470 |
} |
| 471 |
if (e.ident == Id.isDeprecated) |
| 472 |
{ |
| 473 |
if (isTypeX(t => t.isComplex() || t.isImaginary()).toBool().hasValue(true)) |
| 474 |
return True(); |
| 475 |
return isDsymX(t => t.isDeprecated()); |
| 476 |
} |
| 477 |
if (e.ident == Id.isFuture) |
| 478 |
{ |
| 479 |
return isDeclX(t => t.isFuture()); |
| 480 |
} |
| 481 |
if (e.ident == Id.isStaticArray) |
| 482 |
{ |
| 483 |
return isTypeX(t => t.toBasetype().ty == Tsarray); |
| 484 |
} |
| 485 |
if (e.ident == Id.isAbstractClass) |
| 486 |
{ |
| 487 |
return isTypeX((t) |
| 488 |
{ |
| 489 |
auto c = t.toBasetype().isTypeClass(); |
| 490 |
return c && c.sym.isAbstract(); |
| 491 |
}); |
| 492 |
} |
| 493 |
if (e.ident == Id.isFinalClass) |
| 494 |
{ |
| 495 |
return isTypeX((t) |
| 496 |
{ |
| 497 |
const c = t.toBasetype().isTypeClass(); |
| 498 |
return c && (c.sym.storage_class & STC.final_) != 0; |
| 499 |
}); |
| 500 |
} |
| 501 |
if (e.ident == Id.isTemplate) |
| 502 |
{ |
| 503 |
if (dim != 1) |
| 504 |
return dimError(1); |
| 505 |
|
| 506 |
return isDsymX((s) |
| 507 |
{ |
| 508 |
if (!s.toAlias().isOverloadable()) |
| 509 |
return false; |
| 510 |
return overloadApply(s, |
| 511 |
sm => sm.isTemplateDeclaration() !is null) != 0; |
| 512 |
}); |
| 513 |
} |
| 514 |
if (e.ident == Id.isBitfield) |
| 515 |
{ |
| 516 |
if (dim != 1) |
| 517 |
return dimError(1); |
| 518 |
|
| 519 |
return isDsymX((s) |
| 520 |
{ |
| 521 |
s = s.toAlias(); |
| 522 |
return s.isBitFieldDeclaration() !is null; |
| 523 |
}); |
| 524 |
} |
| 525 |
if (e.ident == Id.isPOD) |
| 526 |
{ |
| 527 |
if (dim != 1) |
| 528 |
return dimError(1); |
| 529 |
|
| 530 |
auto o = (*e.args)[0]; |
| 531 |
auto t = isType(o); |
| 532 |
if (!t) |
| 533 |
{ |
| 534 |
error(e.loc, "type expected as second argument of __traits `%s` instead of `%s`", |
| 535 |
e.ident.toChars(), o.toChars()); |
| 536 |
return ErrorExp.get(); |
| 537 |
} |
| 538 |
|
| 539 |
Type tb = t.baseElemOf(); |
| 540 |
auto ts = tb.isTypeStruct(); |
| 541 |
if (auto sd = ts ? ts.sym : null) |
| 542 |
{ |
| 543 |
return sd.isPOD() ? True() : False(); |
| 544 |
} |
| 545 |
return True(); |
| 546 |
} |
| 547 |
if (e.ident == Id.hasCopyConstructor || |
| 548 |
e.ident == Id.hasMoveConstructor || |
| 549 |
e.ident == Id.hasPostblit) |
| 550 |
{ |
| 551 |
if (dim != 1) |
| 552 |
return dimError(1); |
| 553 |
|
| 554 |
auto o = (*e.args)[0]; |
| 555 |
auto t = isType(o); |
| 556 |
if (!t) |
| 557 |
{ |
| 558 |
error(e.loc, "type expected as second argument of __traits `%s` instead of `%s`", |
| 559 |
e.ident.toChars(), o.toChars()); |
| 560 |
return ErrorExp.get(); |
| 561 |
} |
| 562 |
|
| 563 |
Type tb = t.baseElemOf(); |
| 564 |
auto ts = tb.isTypeStruct(); |
| 565 |
if (auto sd = ts ? ts.sym : null) |
| 566 |
{ |
| 567 |
bool result; |
| 568 |
if (e.ident == Id.hasPostblit) |
| 569 |
result = sd.postblit !is null; |
| 570 |
else if (e.ident == Id. hasCopyConstructor) |
| 571 |
result = sd.hasCopyCtor; |
| 572 |
else |
| 573 |
result = sd.hasMoveCtor; |
| 574 |
return result ? True() : False(); |
| 575 |
} |
| 576 |
return False(); |
| 577 |
} |
| 578 |
if (e.ident == Id.isCopyable) |
| 579 |
{ |
| 580 |
if (dim != 1) |
| 581 |
return dimError(1); |
| 582 |
|
| 583 |
auto o = (*e.args)[0]; |
| 584 |
auto t = isType(o); |
| 585 |
if (!t) |
| 586 |
{ |
| 587 |
error(e.loc, "type expected as second argument of __traits `%s` instead of `%s`", |
| 588 |
e.ident.toChars(), o.toChars()); |
| 589 |
return ErrorExp.get(); |
| 590 |
} |
| 591 |
|
| 592 |
t = t.toBasetype(); // get the base in case `t` is an `enum` |
| 593 |
|
| 594 |
if (auto ts = t.isTypeStruct()) |
| 595 |
{ |
| 596 |
ts.sym.dsymbolSemantic(sc); |
| 597 |
} |
| 598 |
|
| 599 |
return isCopyable(t) ? True() : False(); |
| 600 |
} |
| 601 |
|
| 602 |
if (e.ident == Id.isNested) |
| 603 |
{ |
| 604 |
if (dim != 1) |
| 605 |
return dimError(1); |
| 606 |
|
| 607 |
auto o = (*e.args)[0]; |
| 608 |
auto s = getDsymbolWithoutExpCtx(o); |
| 609 |
if (!s) |
| 610 |
{ |
| 611 |
} |
| 612 |
else if (auto ad = s.isAggregateDeclaration()) |
| 613 |
{ |
| 614 |
return ad.isNested() ? True() : False(); |
| 615 |
} |
| 616 |
else if (auto fd = s.isFuncDeclaration()) |
| 617 |
{ |
| 618 |
return fd.isNested() ? True() : False(); |
| 619 |
} |
| 620 |
|
| 621 |
error(e.loc, "aggregate or function expected instead of `%s`", o.toChars()); |
| 622 |
return ErrorExp.get(); |
| 623 |
} |
| 624 |
if (e.ident == Id.isDisabled) |
| 625 |
{ |
| 626 |
if (dim != 1) |
| 627 |
return dimError(1); |
| 628 |
|
| 629 |
return isDeclX(f => f.isDisabled()); |
| 630 |
} |
| 631 |
if (e.ident == Id.isAbstractFunction) |
| 632 |
{ |
| 633 |
if (dim != 1) |
| 634 |
return dimError(1); |
| 635 |
|
| 636 |
return isFuncX(f => f.isAbstract()); |
| 637 |
} |
| 638 |
if (e.ident == Id.isVirtualFunction) |
| 639 |
{ |
| 640 |
// @@@DEPRECATED2.121@@@ |
| 641 |
// Deprecated in 2.101 - Can be removed from 2.121 |
| 642 |
deprecation(e.loc, "`traits(isVirtualFunction)` is deprecated. Use `traits(isVirtualMethod)` instead"); |
| 643 |
|
| 644 |
if (dim != 1) |
| 645 |
return dimError(1); |
| 646 |
|
| 647 |
return isFuncX(f => f.isVirtual()); |
| 648 |
} |
| 649 |
if (e.ident == Id.isVirtualMethod) |
| 650 |
{ |
| 651 |
if (dim != 1) |
| 652 |
return dimError(1); |
| 653 |
|
| 654 |
return isFuncX(f => f.isVirtualMethod()); |
| 655 |
} |
| 656 |
if (e.ident == Id.isFinalFunction) |
| 657 |
{ |
| 658 |
if (dim != 1) |
| 659 |
return dimError(1); |
| 660 |
|
| 661 |
return isFuncX(f => f.isFinalFunc()); |
| 662 |
} |
| 663 |
if (e.ident == Id.isOverrideFunction) |
| 664 |
{ |
| 665 |
if (dim != 1) |
| 666 |
return dimError(1); |
| 667 |
|
| 668 |
return isFuncX(f => f.isOverride()); |
| 669 |
} |
| 670 |
if (e.ident == Id.isStaticFunction) |
| 671 |
{ |
| 672 |
if (dim != 1) |
| 673 |
return dimError(1); |
| 674 |
|
| 675 |
return isFuncX(f => !f.needThis() && !f.isNested()); |
| 676 |
} |
| 677 |
if (e.ident == Id.isModule) |
| 678 |
{ |
| 679 |
if (dim != 1) |
| 680 |
return dimError(1); |
| 681 |
|
| 682 |
return isPkgX(p => p.isModule() || p.isPackageMod()); |
| 683 |
} |
| 684 |
if (e.ident == Id.isPackage) |
| 685 |
{ |
| 686 |
if (dim != 1) |
| 687 |
return dimError(1); |
| 688 |
|
| 689 |
return isPkgX(p => p.isModule() is null); |
| 690 |
} |
| 691 |
if (e.ident == Id.isRef) |
| 692 |
{ |
| 693 |
if (dim != 1) |
| 694 |
return dimError(1); |
| 695 |
|
| 696 |
return isDeclX(d => d.isRef()); |
| 697 |
} |
| 698 |
if (e.ident == Id.isOut) |
| 699 |
{ |
| 700 |
if (dim != 1) |
| 701 |
return dimError(1); |
| 702 |
|
| 703 |
return isDeclX(d => d.isOut()); |
| 704 |
} |
| 705 |
if (e.ident == Id.isLazy) |
| 706 |
{ |
| 707 |
if (dim != 1) |
| 708 |
return dimError(1); |
| 709 |
|
| 710 |
return isDeclX(d => (d.storage_class & STC.lazy_) != 0); |
| 711 |
} |
| 712 |
if (e.ident == Id.isCOMClass) |
| 713 |
{ |
| 714 |
if (dim != 1) |
| 715 |
return dimError(1); |
| 716 |
|
| 717 |
auto o = (*e.args)[0]; |
| 718 |
auto s = getDsymbol(o); |
| 719 |
AggregateDeclaration agg; |
| 720 |
|
| 721 |
if (!s || ((agg = s.isAggregateDeclaration()) is null)) |
| 722 |
{ |
| 723 |
error(e.loc, "argument to `__traits(isCOMClass, %s)` is not a declaration", o.toChars()); |
| 724 |
return ErrorExp.get(); |
| 725 |
} |
| 726 |
|
| 727 |
if (ClassDeclaration cd = agg.isClassDeclaration()) |
| 728 |
return cd.com ? True() : False(); |
| 729 |
return False(); |
| 730 |
} |
| 731 |
if (e.ident == Id.identifier) |
| 732 |
{ |
| 733 |
// Get identifier for symbol as a string literal |
| 734 |
/* Specify 0 for bit 0 of the flags argument to semanticTiargs() so that |
| 735 |
* a symbol should not be folded to a constant. |
| 736 |
* Bit 1 means don't convert Parameter to Type if Parameter has an identifier |
| 737 |
*/ |
| 738 |
if (!TemplateInstance.semanticTiargs(e.loc, sc, e.args, 2)) |
| 739 |
return ErrorExp.get(); |
| 740 |
if (dim != 1) |
| 741 |
return dimError(1); |
| 742 |
|
| 743 |
auto o = (*e.args)[0]; |
| 744 |
Identifier id; |
| 745 |
if (auto po = isParameter(o)) |
| 746 |
{ |
| 747 |
if (!po.ident) |
| 748 |
{ |
| 749 |
error(e.loc, "argument `%s` has no identifier", po.type.toChars()); |
| 750 |
return ErrorExp.get(); |
| 751 |
} |
| 752 |
id = po.ident; |
| 753 |
} |
| 754 |
else |
| 755 |
{ |
| 756 |
Dsymbol s = getDsymbolWithoutExpCtx(o); |
| 757 |
if (!s || !s.ident) |
| 758 |
{ |
| 759 |
error(e.loc, "argument `%s` has no identifier", o.toChars()); |
| 760 |
return ErrorExp.get(); |
| 761 |
} |
| 762 |
id = s.ident; |
| 763 |
} |
| 764 |
|
| 765 |
auto se = new StringExp(e.loc, id.toString()); |
| 766 |
return se.expressionSemantic(sc); |
| 767 |
} |
| 768 |
if (e.ident == Id.fullyQualifiedName) // https://dlang.org/spec/traits.html#fullyQualifiedName |
| 769 |
{ |
| 770 |
if (dim != 1) |
| 771 |
return dimError(1); |
| 772 |
|
| 773 |
Scope* sc2 = sc.push(); |
| 774 |
sc2.copyFlagsFrom(sc); |
| 775 |
sc2.noAccessCheck = true; |
| 776 |
sc2.ignoresymbolvisibility = true; |
| 777 |
bool ok = TemplateInstance.semanticTiargs(e.loc, sc2, e.args, 1); |
| 778 |
sc2.pop(); |
| 779 |
if (!ok) |
| 780 |
return ErrorExp.get(); |
| 781 |
|
| 782 |
const(char)[] fqn; |
| 783 |
auto o = (*e.args)[0]; |
| 784 |
if (auto s = getDsymbolWithoutExpCtx(o)) |
| 785 |
{ |
| 786 |
if (s.semanticRun == PASS.initial) |
| 787 |
s.dsymbolSemantic(null); |
| 788 |
|
| 789 |
fqn = s.toPrettyChars().toDString(); |
| 790 |
} |
| 791 |
else if (auto t = getType(o)) |
| 792 |
{ |
| 793 |
fqn = t.toPrettyChars(true).toDString(); |
| 794 |
} |
| 795 |
else |
| 796 |
{ |
| 797 |
if (!isError(o)) |
| 798 |
error(e.loc, "argument `%s` has no identifier", o.toChars()); |
| 799 |
return ErrorExp.get(); |
| 800 |
} |
| 801 |
assert(fqn); |
| 802 |
auto se = new StringExp(e.loc, fqn); |
| 803 |
return se.expressionSemantic(sc); |
| 804 |
|
| 805 |
} |
| 806 |
if (e.ident == Id.getBitfieldOffset || e.ident == Id.getBitfieldWidth) |
| 807 |
{ |
| 808 |
if (dim != 1) |
| 809 |
return dimError(1); |
| 810 |
|
| 811 |
auto o = (*e.args)[0]; |
| 812 |
auto s = getDsymbolWithoutExpCtx(o); |
| 813 |
if (!s) |
| 814 |
{ |
| 815 |
error(e.loc, "bitfield symbol expected not `%s`", o.toChars()); |
| 816 |
return ErrorExp.get(); |
| 817 |
} |
| 818 |
|
| 819 |
auto vd = s.toAlias.isVarDeclaration(); |
| 820 |
if (!vd || !(vd.storage_class & STC.field)) |
| 821 |
{ |
| 822 |
error(e.loc, "bitfield symbol expected not %s `%s`", s.kind, s.toPrettyChars); |
| 823 |
return ErrorExp.get(); |
| 824 |
} |
| 825 |
|
| 826 |
uint fieldWidth; |
| 827 |
uint bitOffset; |
| 828 |
if (auto bf = vd.isBitFieldDeclaration()) |
| 829 |
{ |
| 830 |
fieldWidth = bf.fieldWidth; |
| 831 |
bitOffset = bf.bitOffset; |
| 832 |
} |
| 833 |
else // just a regular field |
| 834 |
{ |
| 835 |
const sz = size(vd.type); |
| 836 |
assert(sz < uint.max / 8); // overflow check |
| 837 |
fieldWidth = cast(uint)sz * 8; |
| 838 |
bitOffset = 0; |
| 839 |
} |
| 840 |
uint value = e.ident == Id.getBitfieldOffset ? bitOffset : fieldWidth; |
| 841 |
return new IntegerExp(e.loc, value, Type.tuns32); |
| 842 |
} |
| 843 |
if (e.ident == Id.getProtection || e.ident == Id.getVisibility) |
| 844 |
{ |
| 845 |
if (dim != 1) |
| 846 |
return dimError(1); |
| 847 |
|
| 848 |
Scope* sc2 = sc.push(); |
| 849 |
sc2.copyFlagsFrom(sc); |
| 850 |
sc2.noAccessCheck = true; |
| 851 |
sc2.ignoresymbolvisibility = true; |
| 852 |
bool ok = TemplateInstance.semanticTiargs(e.loc, sc2, e.args, 1); |
| 853 |
sc2.pop(); |
| 854 |
if (!ok) |
| 855 |
return ErrorExp.get(); |
| 856 |
|
| 857 |
auto o = (*e.args)[0]; |
| 858 |
auto s = getDsymbolWithoutExpCtx(o); |
| 859 |
if (!s) |
| 860 |
{ |
| 861 |
if (!isError(o)) |
| 862 |
error(e.loc, "argument `%s` has no visibility", o.toChars()); |
| 863 |
return ErrorExp.get(); |
| 864 |
} |
| 865 |
if (s.semanticRun == PASS.initial) |
| 866 |
s.dsymbolSemantic(null); |
| 867 |
|
| 868 |
auto protName = visibilityToString(s.visible().kind); // TODO: How about package(names) |
| 869 |
assert(protName); |
| 870 |
auto se = new StringExp(e.loc, protName); |
| 871 |
return se.expressionSemantic(sc); |
| 872 |
} |
| 873 |
if (e.ident == Id.parent) |
| 874 |
{ |
| 875 |
if (dim != 1) |
| 876 |
return dimError(1); |
| 877 |
|
| 878 |
auto o = (*e.args)[0]; |
| 879 |
auto s = getDsymbolWithoutExpCtx(o); |
| 880 |
if (s) |
| 881 |
{ |
| 882 |
// https://issues.dlang.org/show_bug.cgi?id=12496 |
| 883 |
// Consider: |
| 884 |
// class T1 |
| 885 |
// { |
| 886 |
// class C(uint value) { } |
| 887 |
// } |
| 888 |
// __traits(parent, T1.C!2) |
| 889 |
if (auto ad = s.isAggregateDeclaration()) // `s` is `C` |
| 890 |
{ |
| 891 |
if (ad.isNested()) // `C` is nested |
| 892 |
{ |
| 893 |
if (auto p = s.toParent()) // `C`'s parent is `C!2`, believe it or not |
| 894 |
{ |
| 895 |
if (auto ti = p.isTemplateInstance()) // `C!2` is a template instance |
| 896 |
{ |
| 897 |
s = p; // `C!2`'s parent is `T1` |
| 898 |
auto td = ti.tempdecl; |
| 899 |
if (td) |
| 900 |
s = td; // get the declaration context just in case there's two contexts |
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
if (auto fd = s.isFuncDeclaration()) // https://issues.dlang.org/show_bug.cgi?id=8943 |
| 907 |
s = fd.toAliasFunc(); |
| 908 |
if (!s.isImport()) // https://issues.dlang.org/show_bug.cgi?id=8922 |
| 909 |
s = s.toParent(); |
| 910 |
} |
| 911 |
if (!s || s.isImport()) |
| 912 |
{ |
| 913 |
error(e.loc, "argument `%s` has no parent", o.toChars()); |
| 914 |
return ErrorExp.get(); |
| 915 |
} |
| 916 |
|
| 917 |
if (auto f = s.isFuncDeclaration()) |
| 918 |
{ |
| 919 |
if (auto td = getFuncTemplateDecl(f)) |
| 920 |
{ |
| 921 |
if (td.overroot) // if not start of overloaded list of TemplateDeclaration's |
| 922 |
td = td.overroot; // then get the start |
| 923 |
Expression ex = new TemplateExp(e.loc, td, f); |
| 924 |
ex = ex.expressionSemantic(sc); |
| 925 |
return ex; |
| 926 |
} |
| 927 |
if (auto fld = f.isFuncLiteralDeclaration()) |
| 928 |
{ |
| 929 |
// Directly translate to VarExp instead of FuncExp |
| 930 |
Expression ex = new VarExp(e.loc, fld, true); |
| 931 |
return ex.expressionSemantic(sc); |
| 932 |
} |
| 933 |
} |
| 934 |
return symbolToExp(s, e.loc, sc, false); |
| 935 |
} |
| 936 |
if (e.ident == Id.child) |
| 937 |
{ |
| 938 |
if (dim != 2) |
| 939 |
return dimError(2); |
| 940 |
|
| 941 |
Expression ex; |
| 942 |
auto op = (*e.args)[0]; |
| 943 |
if (auto symp = getDsymbol(op)) |
| 944 |
ex = new DsymbolExp(e.loc, symp); |
| 945 |
else if (auto exp = op.isExpression()) |
| 946 |
ex = exp; |
| 947 |
else |
| 948 |
{ |
| 949 |
error(e.loc, "symbol or expression expected as first argument of __traits `child` instead of `%s`", op.toChars()); |
| 950 |
return ErrorExp.get(); |
| 951 |
} |
| 952 |
|
| 953 |
ex = ex.expressionSemantic(sc); |
| 954 |
auto oc = (*e.args)[1]; |
| 955 |
auto symc = getDsymbol(oc); |
| 956 |
if (!symc) |
| 957 |
{ |
| 958 |
error(e.loc, "symbol expected as second argument of __traits `child` instead of `%s`", oc.toChars()); |
| 959 |
return ErrorExp.get(); |
| 960 |
} |
| 961 |
|
| 962 |
if (auto d = symc.isDeclaration()) |
| 963 |
ex = new DotVarExp(e.loc, ex, d); |
| 964 |
else if (auto td = symc.isTemplateDeclaration()) |
| 965 |
ex = new DotExp(e.loc, ex, new TemplateExp(e.loc, td)); |
| 966 |
else if (auto ti = symc.isScopeDsymbol()) |
| 967 |
ex = new DotExp(e.loc, ex, new ScopeExp(e.loc, ti)); |
| 968 |
else |
| 969 |
assert(0); |
| 970 |
|
| 971 |
ex = ex.expressionSemantic(sc); |
| 972 |
return ex; |
| 973 |
} |
| 974 |
if (e.ident == Id.toType) |
| 975 |
{ |
| 976 |
if (dim != 1) |
| 977 |
return dimError(1); |
| 978 |
|
| 979 |
auto ex = isExpression((*e.args)[0]); |
| 980 |
if (!ex) |
| 981 |
{ |
| 982 |
error(e.loc, "expression expected as second argument of __traits `%s`", e.ident.toChars()); |
| 983 |
return ErrorExp.get(); |
| 984 |
} |
| 985 |
ex = ex.ctfeInterpret(); |
| 986 |
|
| 987 |
StringExp se = semanticString(sc, ex, "__traits(toType, string)"); |
| 988 |
if (!se) |
| 989 |
{ |
| 990 |
return ErrorExp.get(); |
| 991 |
} |
| 992 |
Type t = decoToType(se.toUTF8(sc).peekString()); |
| 993 |
if (!t) |
| 994 |
{ |
| 995 |
error(e.loc, "cannot determine `%s`", e.toChars()); |
| 996 |
return ErrorExp.get(); |
| 997 |
} |
| 998 |
return (new TypeExp(e.loc, t)).expressionSemantic(sc); |
| 999 |
} |
| 1000 |
if (e.ident == Id.hasMember || |
| 1001 |
e.ident == Id.getMember || |
| 1002 |
e.ident == Id.getOverloads || |
| 1003 |
e.ident == Id.getVirtualMethods || |
| 1004 |
e.ident == Id.getVirtualFunctions) |
| 1005 |
{ |
| 1006 |
if (dim != 2 && !(dim == 3 && e.ident == Id.getOverloads)) |
| 1007 |
return dimError(2); |
| 1008 |
|
| 1009 |
auto o = (*e.args)[0]; |
| 1010 |
auto ex = isExpression((*e.args)[1]); |
| 1011 |
if (!ex) |
| 1012 |
{ |
| 1013 |
error(e.loc, "expression expected as second argument of __traits `%s`", e.ident.toChars()); |
| 1014 |
return ErrorExp.get(); |
| 1015 |
} |
| 1016 |
ex = ex.ctfeInterpret(); |
| 1017 |
|
| 1018 |
bool includeTemplates = false; |
| 1019 |
if (dim == 3 && e.ident == Id.getOverloads) |
| 1020 |
{ |
| 1021 |
auto b = isExpression((*e.args)[2]); |
| 1022 |
b = b.ctfeInterpret(); |
| 1023 |
if (!b.type.equals(Type.tbool)) |
| 1024 |
{ |
| 1025 |
error(e.loc, "`bool` expected as third argument of `__traits(getOverloads)`, not `%s` of type `%s`", b.toChars(), b.type.toChars()); |
| 1026 |
return ErrorExp.get(); |
| 1027 |
} |
| 1028 |
includeTemplates = b.toBool().get(); |
| 1029 |
} |
| 1030 |
|
| 1031 |
StringExp se = ex.toStringExp(); |
| 1032 |
if (!se || se.len == 0) |
| 1033 |
{ |
| 1034 |
error(e.loc, "string expected as second argument of __traits `%s` instead of `%s`", e.ident.toChars(), ex.toChars()); |
| 1035 |
return ErrorExp.get(); |
| 1036 |
} |
| 1037 |
se = se.toUTF8(sc); |
| 1038 |
|
| 1039 |
if (se.sz != 1) |
| 1040 |
{ |
| 1041 |
error(e.loc, "string must be chars"); |
| 1042 |
return ErrorExp.get(); |
| 1043 |
} |
| 1044 |
auto id = Identifier.idPool(se.peekString()); |
| 1045 |
|
| 1046 |
/* Prefer a Type, because getDsymbol(Type) can lose type modifiers. |
| 1047 |
Then a Dsymbol, because it might need some runtime contexts. |
| 1048 |
*/ |
| 1049 |
|
| 1050 |
Dsymbol sym = getDsymbol(o); |
| 1051 |
|
| 1052 |
if (sym && e.ident == Id.hasMember) |
| 1053 |
{ |
| 1054 |
if (auto sm = sym.search(e.loc, id)) |
| 1055 |
return True(); |
| 1056 |
|
| 1057 |
// https://issues.dlang.org/show_bug.cgi?id=23951 |
| 1058 |
if (auto decl = sym.isDeclaration()) |
| 1059 |
{ |
| 1060 |
ex = typeDotIdExp(e.loc, decl.type, id); |
| 1061 |
goto doSemantic; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
if (auto t = isType(o)) |
| 1066 |
ex = typeDotIdExp(e.loc, t, id); |
| 1067 |
else if (sym) |
| 1068 |
{ |
| 1069 |
ex = new DsymbolExp(e.loc, sym); |
| 1070 |
ex = new DotIdExp(e.loc, ex, id); |
| 1071 |
} |
| 1072 |
else if (auto ex2 = isExpression(o)) |
| 1073 |
ex = new DotIdExp(e.loc, ex2, id); |
| 1074 |
else |
| 1075 |
{ |
| 1076 |
error(e.loc, "invalid first argument"); |
| 1077 |
return ErrorExp.get(); |
| 1078 |
} |
| 1079 |
doSemantic: |
| 1080 |
// ignore symbol visibility and disable access checks for these traits |
| 1081 |
Scope* scx = sc.push(); |
| 1082 |
scx.ignoresymbolvisibility = true; |
| 1083 |
scx.noAccessCheck = true; |
| 1084 |
scope (exit) scx.pop(); |
| 1085 |
|
| 1086 |
if (e.ident == Id.hasMember) |
| 1087 |
{ |
| 1088 |
/* Take any errors as meaning it wasn't found |
| 1089 |
*/ |
| 1090 |
ex = ex.trySemantic(scx); |
| 1091 |
return ex ? True() : False(); |
| 1092 |
} |
| 1093 |
else if (e.ident == Id.getMember) |
| 1094 |
{ |
| 1095 |
if (auto die = ex.isDotIdExp()) |
| 1096 |
// Prevent semantic() from replacing Symbol with its initializer |
| 1097 |
die.wantsym = true; |
| 1098 |
ex = ex.expressionSemantic(scx); |
| 1099 |
return ex; |
| 1100 |
} |
| 1101 |
else if (e.ident == Id.getVirtualFunctions || |
| 1102 |
e.ident == Id.getVirtualMethods || |
| 1103 |
e.ident == Id.getOverloads) |
| 1104 |
{ |
| 1105 |
const errors = global.errors; |
| 1106 |
Expression eorig = ex; |
| 1107 |
ex = ex.expressionSemantic(scx); |
| 1108 |
if (errors < global.errors) |
| 1109 |
error(e.loc, "`%s` cannot be resolved", eorig.toChars()); |
| 1110 |
|
| 1111 |
if (e.ident == Id.getVirtualFunctions) |
| 1112 |
{ |
| 1113 |
// @@@DEPRECATED2.121@@@ |
| 1114 |
// Deprecated in 2.101 - Can be removed from 2.121 |
| 1115 |
deprecation(e.loc, "`traits(getVirtualFunctions)` is deprecated. Use `traits(getVirtualMethods)` instead"); |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* Create tuple of functions of ex |
| 1119 |
*/ |
| 1120 |
auto exps = new Expressions(); |
| 1121 |
Dsymbol f; |
| 1122 |
if (auto ve = ex.isVarExp) |
| 1123 |
{ |
| 1124 |
if (ve.var.isFuncDeclaration() || ve.var.isOverDeclaration()) |
| 1125 |
f = ve.var; |
| 1126 |
ex = null; |
| 1127 |
} |
| 1128 |
else if (auto dve = ex.isDotVarExp) |
| 1129 |
{ |
| 1130 |
if (dve.var.isFuncDeclaration() || dve.var.isOverDeclaration()) |
| 1131 |
f = dve.var; |
| 1132 |
if (dve.e1.op == EXP.dotType || dve.e1.op == EXP.this_) |
| 1133 |
ex = null; |
| 1134 |
else |
| 1135 |
ex = dve.e1; |
| 1136 |
} |
| 1137 |
else if (auto te = ex.isTemplateExp) |
| 1138 |
{ |
| 1139 |
auto td = te.td; |
| 1140 |
f = td; |
| 1141 |
if (td && td.funcroot) |
| 1142 |
f = td.funcroot; |
| 1143 |
ex = null; |
| 1144 |
} |
| 1145 |
else if (auto dte = ex.isDotTemplateExp) |
| 1146 |
{ |
| 1147 |
auto td = dte.td; |
| 1148 |
f = td; |
| 1149 |
if (td && td.funcroot) |
| 1150 |
f = td.funcroot; |
| 1151 |
ex = null; |
| 1152 |
if (dte.e1.op != EXP.dotType && dte.e1.op != EXP.this_) |
| 1153 |
ex = dte.e1; |
| 1154 |
} |
| 1155 |
bool[string] funcTypeHash; |
| 1156 |
|
| 1157 |
/* Compute the function signature and insert it in the |
| 1158 |
* hashtable, if not present. This is needed so that |
| 1159 |
* traits(getOverlods, F3, "visit") does not count `int visit(int)` |
| 1160 |
* twice in the following example: |
| 1161 |
* |
| 1162 |
* ============================================= |
| 1163 |
* interface F1 { int visit(int);} |
| 1164 |
* interface F2 { int visit(int); void visit(); } |
| 1165 |
* interface F3 : F2, F1 {} |
| 1166 |
*============================================== |
| 1167 |
*/ |
| 1168 |
void insertInterfaceInheritedFunction(FuncDeclaration fd, Expression e) |
| 1169 |
{ |
| 1170 |
auto signature = fd.type.toString(); |
| 1171 |
//printf("%s - %s\n", fd.toChars, signature); |
| 1172 |
if (signature !in funcTypeHash) |
| 1173 |
{ |
| 1174 |
funcTypeHash[signature] = true; |
| 1175 |
exps.push(e); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
int dg(Dsymbol s) |
| 1180 |
{ |
| 1181 |
auto fd = s.isFuncDeclaration(); |
| 1182 |
if (!fd) |
| 1183 |
{ |
| 1184 |
if (includeTemplates) |
| 1185 |
{ |
| 1186 |
if (auto td = s.isTemplateDeclaration()) |
| 1187 |
{ |
| 1188 |
// if td is part of an overload set we must take a copy |
| 1189 |
// which shares the same `instances` cache but without |
| 1190 |
// `overroot` and `overnext` set to avoid overload |
| 1191 |
// behaviour in the result. |
| 1192 |
if (td.overnext !is null) |
| 1193 |
{ |
| 1194 |
if (td.instances is null) |
| 1195 |
{ |
| 1196 |
// create an empty AA just to copy it |
| 1197 |
scope ti = new TemplateInstance(Loc.initial, Id.empty, null); |
| 1198 |
auto tib = TemplateInstanceBox(ti); |
| 1199 |
td.instances[tib] = null; |
| 1200 |
td.instances.clear(); |
| 1201 |
} |
| 1202 |
td = td.syntaxCopy(null); |
| 1203 |
import core.stdc.string : memcpy; |
| 1204 |
memcpy(cast(void*) td, cast(void*) s, |
| 1205 |
__traits(classInstanceSize, TemplateDeclaration)); |
| 1206 |
td.overroot = null; |
| 1207 |
td.overnext = null; |
| 1208 |
} |
| 1209 |
|
| 1210 |
auto e = ex ? new DotTemplateExp(Loc.initial, ex, td) |
| 1211 |
: new DsymbolExp(Loc.initial, td); |
| 1212 |
exps.push(e); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
if (e.ident == Id.getVirtualFunctions && !fd.isVirtual()) |
| 1218 |
return 0; |
| 1219 |
if (e.ident == Id.getVirtualMethods && !fd.isVirtualMethod()) |
| 1220 |
return 0; |
| 1221 |
|
| 1222 |
auto fa = new FuncAliasDeclaration(fd.ident, fd, false); |
| 1223 |
fa.visibility = fd.visibility; |
| 1224 |
|
| 1225 |
auto e = ex ? new DotVarExp(Loc.initial, ex, fa, false) |
| 1226 |
: new DsymbolExp(Loc.initial, fa, false); |
| 1227 |
|
| 1228 |
// if the parent is an interface declaration |
| 1229 |
// we must check for functions with the same signature |
| 1230 |
// in different inherited interfaces |
| 1231 |
if (sym && sym.isInterfaceDeclaration()) |
| 1232 |
insertInterfaceInheritedFunction(fd, e); |
| 1233 |
else |
| 1234 |
exps.push(e); |
| 1235 |
return 0; |
| 1236 |
} |
| 1237 |
|
| 1238 |
InterfaceDeclaration ifd = null; |
| 1239 |
if (sym) |
| 1240 |
ifd = sym.isInterfaceDeclaration(); |
| 1241 |
// If the symbol passed as a parameter is an |
| 1242 |
// interface that inherits other interfaces |
| 1243 |
overloadApply(f, &dg); |
| 1244 |
if (ifd && ifd.interfaces && f) |
| 1245 |
{ |
| 1246 |
// check the overloads of each inherited interface individually |
| 1247 |
foreach (bc; ifd.interfaces) |
| 1248 |
{ |
| 1249 |
if (auto fd = bc.sym.search(e.loc, f.ident)) |
| 1250 |
overloadApply(fd, &dg); |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
auto tup = new TupleExp(e.loc, exps); |
| 1255 |
return tup.expressionSemantic(scx); |
| 1256 |
} |
| 1257 |
else |
| 1258 |
assert(0); |
| 1259 |
} |
| 1260 |
if (e.ident == Id.classInstanceSize || e.ident == Id.classInstanceAlignment) |
| 1261 |
{ |
| 1262 |
if (dim != 1) |
| 1263 |
return dimError(1); |
| 1264 |
|
| 1265 |
auto o = (*e.args)[0]; |
| 1266 |
auto s = getDsymbol(o); |
| 1267 |
auto cd = s ? s.isClassDeclaration() : null; |
| 1268 |
if (!cd) |
| 1269 |
{ |
| 1270 |
error(e.loc, "first argument is not a class"); |
| 1271 |
return ErrorExp.get(); |
| 1272 |
} |
| 1273 |
if (cd.sizeok != Sizeok.done) |
| 1274 |
{ |
| 1275 |
cd.size(e.loc); |
| 1276 |
} |
| 1277 |
if (cd.sizeok != Sizeok.done) |
| 1278 |
{ |
| 1279 |
error(e.loc, "%s `%s` is forward referenced", cd.kind(), cd.toChars()); |
| 1280 |
return ErrorExp.get(); |
| 1281 |
} |
| 1282 |
|
| 1283 |
return new IntegerExp(e.loc, e.ident == Id.classInstanceSize ? cd.structsize : cd.alignsize, Type.tsize_t); |
| 1284 |
} |
| 1285 |
if (e.ident == Id.getAliasThis) |
| 1286 |
{ |
| 1287 |
if (dim != 1) |
| 1288 |
return dimError(1); |
| 1289 |
|
| 1290 |
auto o = (*e.args)[0]; |
| 1291 |
auto s = getDsymbol(o); |
| 1292 |
auto ad = s ? s.isAggregateDeclaration() : null; |
| 1293 |
|
| 1294 |
auto exps = new Expressions(); |
| 1295 |
if (ad && ad.aliasthis) |
| 1296 |
exps.push(new StringExp(e.loc, ad.aliasthis.ident.toString())); |
| 1297 |
Expression ex = new TupleExp(e.loc, exps); |
| 1298 |
ex = ex.expressionSemantic(sc); |
| 1299 |
return ex; |
| 1300 |
} |
| 1301 |
if (e.ident == Id.getAttributes) |
| 1302 |
{ |
| 1303 |
/* Specify 0 for bit 0 of the flags argument to semanticTiargs() so that |
| 1304 |
* a symbol should not be folded to a constant. |
| 1305 |
* Bit 1 means don't convert Parameter to Type if Parameter has an identifier |
| 1306 |
*/ |
| 1307 |
if (!TemplateInstance.semanticTiargs(e.loc, sc, e.args, 3)) |
| 1308 |
return ErrorExp.get(); |
| 1309 |
|
| 1310 |
if (dim != 1) |
| 1311 |
return dimError(1); |
| 1312 |
|
| 1313 |
auto o = (*e.args)[0]; |
| 1314 |
auto po = isParameter(o); |
| 1315 |
auto s = getDsymbolWithoutExpCtx(o); |
| 1316 |
auto typeOfArg = isType(o); |
| 1317 |
UserAttributeDeclaration udad = null; |
| 1318 |
if (po) |
| 1319 |
{ |
| 1320 |
udad = po.userAttribDecl; |
| 1321 |
} |
| 1322 |
else if (s) |
| 1323 |
{ |
| 1324 |
// @@@DEPRECATION 2.100.2 |
| 1325 |
if (auto fd = s.isFuncDeclaration()) |
| 1326 |
{ |
| 1327 |
if (fd.overnext) |
| 1328 |
{ |
| 1329 |
deprecation(e.loc, "`__traits(getAttributes)` may only be used for individual functions, not the overload set `%s`", fd.toChars()); |
| 1330 |
deprecationSupplemental(e.loc, "the result of `__traits(getOverloads)` may be used to select the desired function to extract attributes from"); |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
// @@@DEPRECATION 2.100.2 |
| 1335 |
if (auto td = s.isTemplateDeclaration()) |
| 1336 |
{ |
| 1337 |
if (td.overnext) |
| 1338 |
{ |
| 1339 |
deprecation(e.loc, "`__traits(getAttributes)` may only be used for individual functions, not the overload set `%s`", td.ident.toChars()); |
| 1340 |
deprecationSupplemental(e.loc, "the result of `__traits(getOverloads)` may be used to select the desired function to extract attributes from"); |
| 1341 |
} |
| 1342 |
} |
| 1343 |
if (s.isImport()) |
| 1344 |
{ |
| 1345 |
s = s.isImport().mod; |
| 1346 |
} |
| 1347 |
//printf("getAttributes %s, attrs = %p, scope = %p\n", s.toChars(), s.userAttribDecl, s._scope); |
| 1348 |
udad = s.userAttribDecl; |
| 1349 |
} |
| 1350 |
else if (typeOfArg) |
| 1351 |
{ |
| 1352 |
// If there is a type but no symbol, do nothing rather than erroring. |
| 1353 |
} |
| 1354 |
else |
| 1355 |
{ |
| 1356 |
version (none) |
| 1357 |
{ |
| 1358 |
Expression x = isExpression(o); |
| 1359 |
Type t = isType(o); |
| 1360 |
if (x) |
| 1361 |
printf("e = %s %s\n", EXPtoString(x.op).ptr, x.toChars()); |
| 1362 |
if (t) |
| 1363 |
printf("t = %d %s\n", t.ty, t.toChars()); |
| 1364 |
} |
| 1365 |
error(e.loc, "first argument is not a symbol"); |
| 1366 |
return ErrorExp.get(); |
| 1367 |
} |
| 1368 |
|
| 1369 |
auto exps = udad ? udad.getAttributes() : new Expressions(); |
| 1370 |
auto tup = new TupleExp(e.loc, exps); |
| 1371 |
return tup.expressionSemantic(sc); |
| 1372 |
} |
| 1373 |
if (e.ident == Id.getFunctionAttributes) |
| 1374 |
{ |
| 1375 |
/* Extract all function attributes as a tuple (const/shared/inout/pure/nothrow/etc) except UDAs. |
| 1376 |
* https://dlang.org/spec/traits.html#getFunctionAttributes |
| 1377 |
*/ |
| 1378 |
if (dim != 1) |
| 1379 |
return dimError(1); |
| 1380 |
|
| 1381 |
FuncDeclaration fd; |
| 1382 |
TypeFunction tf = toTypeFunction((*e.args)[0], fd); |
| 1383 |
|
| 1384 |
if (!tf) |
| 1385 |
{ |
| 1386 |
error(e.loc, "first argument is not a function"); |
| 1387 |
return ErrorExp.get(); |
| 1388 |
} |
| 1389 |
|
| 1390 |
// https://issues.dlang.org/show_bug.cgi?id=19706 |
| 1391 |
// When getting the attributes of the instance of a |
| 1392 |
// templated member function semantic tiargs does |
| 1393 |
// not perform semantic3 on the instance. |
| 1394 |
// For more information see FuncDeclaration.functionSemantic. |
| 1395 |
// For getFunctionAttributes it is mandatory to do |
| 1396 |
// attribute inference. |
| 1397 |
if (fd && fd.parent && fd.parent.isTemplateInstance) |
| 1398 |
{ |
| 1399 |
functionSemantic3(fd); |
| 1400 |
tf = fd.type.isTypeFunction(); |
| 1401 |
} |
| 1402 |
|
| 1403 |
auto mods = new Expressions(); |
| 1404 |
|
| 1405 |
void addToMods(string str) |
| 1406 |
{ |
| 1407 |
mods.push(new StringExp(Loc.initial, str)); |
| 1408 |
} |
| 1409 |
tf.modifiersApply(&addToMods); |
| 1410 |
tf.attributesApply(&addToMods, TRUSTformatSystem); |
| 1411 |
|
| 1412 |
auto tup = new TupleExp(e.loc, mods); |
| 1413 |
return tup.expressionSemantic(sc); |
| 1414 |
} |
| 1415 |
if (e.ident == Id.isReturnOnStack) |
| 1416 |
{ |
| 1417 |
/* Extract as a boolean if function return value is on the stack |
| 1418 |
* https://dlang.org/spec/traits.html#isReturnOnStack |
| 1419 |
*/ |
| 1420 |
if (dim != 1) |
| 1421 |
return dimError(1); |
| 1422 |
|
| 1423 |
RootObject o = (*e.args)[0]; |
| 1424 |
FuncDeclaration fd; |
| 1425 |
TypeFunction tf = toTypeFunction(o, fd); |
| 1426 |
|
| 1427 |
if (!tf) |
| 1428 |
{ |
| 1429 |
error(e.loc, "argument to `__traits(isReturnOnStack, %s)` is not a function", o.toChars()); |
| 1430 |
return ErrorExp.get(); |
| 1431 |
} |
| 1432 |
|
| 1433 |
bool value = target.isReturnOnStack(tf, fd && fd.needThis()); |
| 1434 |
return IntegerExp.createBool(value); |
| 1435 |
} |
| 1436 |
if (e.ident == Id.getFunctionVariadicStyle) |
| 1437 |
{ |
| 1438 |
/* Accept a symbol or a type. Returns one of the following: |
| 1439 |
* "none" not a variadic function |
| 1440 |
* "argptr" extern(D) void dstyle(...), use `__argptr` and `__arguments` |
| 1441 |
* "stdarg" extern(C) void cstyle(int, ...), use core.stdc.stdarg |
| 1442 |
* "typesafe" void typesafe(T[] ...) |
| 1443 |
* "KR" old K+R style |
| 1444 |
*/ |
| 1445 |
// get symbol linkage as a string |
| 1446 |
if (dim != 1) |
| 1447 |
return dimError(1); |
| 1448 |
|
| 1449 |
LINK link; |
| 1450 |
VarArg varargs; |
| 1451 |
auto o = (*e.args)[0]; |
| 1452 |
|
| 1453 |
FuncDeclaration fd; |
| 1454 |
TypeFunction tf = toTypeFunction(o, fd); |
| 1455 |
|
| 1456 |
if (tf) |
| 1457 |
{ |
| 1458 |
link = tf.linkage; |
| 1459 |
varargs = tf.parameterList.varargs; |
| 1460 |
} |
| 1461 |
else |
| 1462 |
{ |
| 1463 |
if (!fd) |
| 1464 |
{ |
| 1465 |
error(e.loc, "argument to `__traits(getFunctionVariadicStyle, %s)` is not a function", o.toChars()); |
| 1466 |
return ErrorExp.get(); |
| 1467 |
} |
| 1468 |
link = fd._linkage; |
| 1469 |
varargs = fd.getParameterList().varargs; |
| 1470 |
} |
| 1471 |
string style; |
| 1472 |
final switch (varargs) |
| 1473 |
{ |
| 1474 |
case VarArg.none: style = "none"; break; |
| 1475 |
case VarArg.variadic: style = (link == LINK.d) |
| 1476 |
? "argptr" |
| 1477 |
: "stdarg"; break; |
| 1478 |
case VarArg.KRvariadic: style = "KR"; break; |
| 1479 |
case VarArg.typesafe: style = "typesafe"; break; |
| 1480 |
} |
| 1481 |
auto se = new StringExp(e.loc, style); |
| 1482 |
return se.expressionSemantic(sc); |
| 1483 |
} |
| 1484 |
if (e.ident == Id.getParameterStorageClasses) |
| 1485 |
{ |
| 1486 |
/* Accept a function symbol or a type, followed by a parameter index. |
| 1487 |
* Returns a tuple of strings of the parameter's storage classes. |
| 1488 |
*/ |
| 1489 |
// get symbol linkage as a string |
| 1490 |
if (dim != 2) |
| 1491 |
return dimError(2); |
| 1492 |
|
| 1493 |
auto o = (*e.args)[0]; |
| 1494 |
auto o1 = (*e.args)[1]; |
| 1495 |
|
| 1496 |
ParameterList fparams; |
| 1497 |
|
| 1498 |
CallExp ce; |
| 1499 |
if (auto exp = isExpression(o)) |
| 1500 |
ce = exp.isCallExp(); |
| 1501 |
|
| 1502 |
if (ce) |
| 1503 |
{ |
| 1504 |
fparams = ce.f.getParameterList(); |
| 1505 |
} |
| 1506 |
else |
| 1507 |
{ |
| 1508 |
FuncDeclaration fd; |
| 1509 |
auto tf = toTypeFunction(o, fd); |
| 1510 |
if (tf) |
| 1511 |
fparams = tf.parameterList; |
| 1512 |
else if (fd) |
| 1513 |
fparams = fd.getParameterList(); |
| 1514 |
else |
| 1515 |
{ |
| 1516 |
error(e.loc, "first argument to `__traits(getParameterStorageClasses, %s, %s)` is not a function or a function call", |
| 1517 |
o.toChars(), o1.toChars()); |
| 1518 |
return ErrorExp.get(); |
| 1519 |
} |
| 1520 |
} |
| 1521 |
|
| 1522 |
// Avoid further analysis for invalid functions leading to misleading error messages |
| 1523 |
if (!fparams.parameters) |
| 1524 |
return ErrorExp.get(); |
| 1525 |
|
| 1526 |
STC stc; |
| 1527 |
|
| 1528 |
// Set stc to storage class of the ith parameter |
| 1529 |
auto ex = isExpression((*e.args)[1]); |
| 1530 |
if (!ex) |
| 1531 |
{ |
| 1532 |
error(e.loc, "expression expected as second argument of `__traits(getParameterStorageClasses, %s, %s)`", |
| 1533 |
o.toChars(), o1.toChars()); |
| 1534 |
return ErrorExp.get(); |
| 1535 |
} |
| 1536 |
ex = ex.ctfeInterpret(); |
| 1537 |
auto ii = ex.toUInteger(); |
| 1538 |
if (ii >= fparams.length) |
| 1539 |
{ |
| 1540 |
error(e.loc, "parameter index must be in range 0..%u not %s", cast(uint)fparams.length, ex.toChars()); |
| 1541 |
return ErrorExp.get(); |
| 1542 |
} |
| 1543 |
|
| 1544 |
uint n = cast(uint)ii; |
| 1545 |
Parameter p = fparams[n]; |
| 1546 |
stc = p.storageClass; |
| 1547 |
|
| 1548 |
// This mirrors hdrgen.visit(Parameter p) |
| 1549 |
if (p.type && p.type.mod & MODFlags.shared_) |
| 1550 |
stc &= ~STC.shared_; |
| 1551 |
|
| 1552 |
auto exps = new Expressions; |
| 1553 |
|
| 1554 |
void push(string s) |
| 1555 |
{ |
| 1556 |
exps.push(new StringExp(e.loc, s)); |
| 1557 |
} |
| 1558 |
|
| 1559 |
if (stc & STC.auto_) |
| 1560 |
push("auto"); |
| 1561 |
if (stc & STC.return_) |
| 1562 |
push("return"); |
| 1563 |
|
| 1564 |
if (stc & STC.out_) |
| 1565 |
push("out"); |
| 1566 |
else if (stc & STC.in_) |
| 1567 |
push("in"); |
| 1568 |
else if (stc & STC.ref_) |
| 1569 |
push("ref"); |
| 1570 |
else if (stc & STC.lazy_) |
| 1571 |
push("lazy"); |
| 1572 |
else if (stc & STC.alias_) |
| 1573 |
push("alias"); |
| 1574 |
|
| 1575 |
if (stc & STC.const_) |
| 1576 |
push("const"); |
| 1577 |
if (stc & STC.immutable_) |
| 1578 |
push("immutable"); |
| 1579 |
if (stc & STC.wild) |
| 1580 |
push("inout"); |
| 1581 |
if (stc & STC.shared_) |
| 1582 |
push("shared"); |
| 1583 |
if (stc & STC.scope_ && !(stc & STC.scopeinferred)) |
| 1584 |
push("scope"); |
| 1585 |
|
| 1586 |
auto tup = new TupleExp(e.loc, exps); |
| 1587 |
return tup.expressionSemantic(sc); |
| 1588 |
} |
| 1589 |
if (e.ident == Id.getLinkage) |
| 1590 |
{ |
| 1591 |
// get symbol linkage as a string |
| 1592 |
if (dim != 1) |
| 1593 |
return dimError(1); |
| 1594 |
|
| 1595 |
LINK link; |
| 1596 |
auto o = (*e.args)[0]; |
| 1597 |
|
| 1598 |
FuncDeclaration fd; |
| 1599 |
TypeFunction tf = toTypeFunction(o, fd); |
| 1600 |
|
| 1601 |
if (tf) |
| 1602 |
{ |
| 1603 |
link = fd ? fd.toAliasFunc()._linkage : tf.linkage; |
| 1604 |
} |
| 1605 |
else |
| 1606 |
{ |
| 1607 |
auto s = getDsymbol(o); |
| 1608 |
Declaration d; |
| 1609 |
AggregateDeclaration agg; |
| 1610 |
if (!s || ((d = s.isDeclaration()) is null && (agg = s.isAggregateDeclaration()) is null)) |
| 1611 |
{ |
| 1612 |
error(e.loc, "argument to `__traits(getLinkage, %s)` is not a declaration", o.toChars()); |
| 1613 |
return ErrorExp.get(); |
| 1614 |
} |
| 1615 |
|
| 1616 |
if (d !is null) |
| 1617 |
link = d._linkage; |
| 1618 |
else |
| 1619 |
{ |
| 1620 |
// Resolves forward references |
| 1621 |
if (agg.sizeok != Sizeok.done) |
| 1622 |
{ |
| 1623 |
agg.size(e.loc); |
| 1624 |
if (agg.sizeok != Sizeok.done) |
| 1625 |
{ |
| 1626 |
error(e.loc, "%s `%s` is forward referenced", agg.kind(), agg.toChars()); |
| 1627 |
return ErrorExp.get(); |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
final switch (agg.classKind) |
| 1632 |
{ |
| 1633 |
case ClassKind.d: |
| 1634 |
link = LINK.d; |
| 1635 |
break; |
| 1636 |
case ClassKind.cpp: |
| 1637 |
link = LINK.cpp; |
| 1638 |
break; |
| 1639 |
case ClassKind.objc: |
| 1640 |
link = LINK.objc; |
| 1641 |
break; |
| 1642 |
case ClassKind.c: |
| 1643 |
link = LINK.c; |
| 1644 |
break; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
} |
| 1648 |
auto linkage = linkageToChars(link); |
| 1649 |
auto se = new StringExp(e.loc, linkage.toDString()); |
| 1650 |
return se.expressionSemantic(sc); |
| 1651 |
} |
| 1652 |
if (e.ident == Id.allMembers || |
| 1653 |
e.ident == Id.derivedMembers) |
| 1654 |
{ |
| 1655 |
if (dim != 1) |
| 1656 |
return dimError(1); |
| 1657 |
|
| 1658 |
auto o = (*e.args)[0]; |
| 1659 |
auto s = getDsymbol(o); |
| 1660 |
if (!s) |
| 1661 |
{ |
| 1662 |
error(e.loc, "in expression `%s` `%s` can't have members", e.toChars(), o.toChars()); |
| 1663 |
errorSupplemental(e.loc, "`%s` must evaluate to either a module, a struct, an union, a class, an interface or a template instantiation", o.toChars()); |
| 1664 |
|
| 1665 |
return ErrorExp.get(); |
| 1666 |
} |
| 1667 |
if (auto imp = s.isImport()) |
| 1668 |
{ |
| 1669 |
// https://issues.dlang.org/show_bug.cgi?id=9692 |
| 1670 |
// https://issues.dlang.org/show_bug.cgi?id=20008 |
| 1671 |
if (imp.pkg) |
| 1672 |
s = imp.pkg; |
| 1673 |
} |
| 1674 |
|
| 1675 |
// https://issues.dlang.org/show_bug.cgi?id=16044 |
| 1676 |
if (auto p = s.isPackage()) |
| 1677 |
{ |
| 1678 |
if (auto pm = p.isPackageMod()) |
| 1679 |
s = pm; |
| 1680 |
} |
| 1681 |
|
| 1682 |
auto sds = s.isScopeDsymbol(); |
| 1683 |
if (!sds || sds.isTemplateDeclaration()) |
| 1684 |
{ |
| 1685 |
error(e.loc, "in expression `%s` %s `%s` has no members", e.toChars(), s.kind(), s.toChars()); |
| 1686 |
errorSupplemental(e.loc, "`%s` must evaluate to either a module, a struct, an union, a class, an interface or a template instantiation", s.toChars()); |
| 1687 |
return ErrorExp.get(); |
| 1688 |
} |
| 1689 |
|
| 1690 |
auto idents = new Identifiers(); |
| 1691 |
|
| 1692 |
int pushIdentsDg(size_t n, Dsymbol sm) |
| 1693 |
{ |
| 1694 |
if (!sm) |
| 1695 |
return 1; |
| 1696 |
|
| 1697 |
// skip local symbols, such as static foreach loop variables |
| 1698 |
if (auto decl = sm.isDeclaration()) |
| 1699 |
{ |
| 1700 |
if (decl.storage_class & STC.local) |
| 1701 |
{ |
| 1702 |
return 0; |
| 1703 |
} |
| 1704 |
// skip 'this' context pointers |
| 1705 |
else if (decl.isThisDeclaration()) |
| 1706 |
return 0; |
| 1707 |
} |
| 1708 |
|
| 1709 |
// https://issues.dlang.org/show_bug.cgi?id=20915 |
| 1710 |
// skip version and debug identifiers |
| 1711 |
if (sm.isVersionSymbol() || sm.isDebugSymbol()) |
| 1712 |
return 0; |
| 1713 |
|
| 1714 |
//printf("\t[%i] %s %s\n", i, sm.kind(), sm.toChars()); |
| 1715 |
if (sm.ident) |
| 1716 |
{ |
| 1717 |
// https://issues.dlang.org/show_bug.cgi?id=10096 |
| 1718 |
// https://issues.dlang.org/show_bug.cgi?id=10100 |
| 1719 |
// Skip over internal members in __traits(allMembers) |
| 1720 |
if ((sm.isCtorDeclaration() && sm.ident != Id.ctor) || |
| 1721 |
(sm.isDtorDeclaration() && sm.ident != Id.dtor) || |
| 1722 |
(sm.isPostBlitDeclaration() && sm.ident != Id.postblit) || |
| 1723 |
sm.isInvariantDeclaration() || |
| 1724 |
sm.isUnitTestDeclaration()) |
| 1725 |
|
| 1726 |
{ |
| 1727 |
return 0; |
| 1728 |
} |
| 1729 |
if (sm.ident == Id.empty) |
| 1730 |
{ |
| 1731 |
return 0; |
| 1732 |
} |
| 1733 |
if (sm.isTypeInfoDeclaration()) // https://issues.dlang.org/show_bug.cgi?id=15177 |
| 1734 |
return 0; |
| 1735 |
if ((!sds.isModule() && !sds.isPackage()) && sm.isImport()) // https://issues.dlang.org/show_bug.cgi?id=17057 |
| 1736 |
return 0; |
| 1737 |
|
| 1738 |
//printf("\t%s\n", sm.ident.toChars()); |
| 1739 |
|
| 1740 |
/* Skip if already present in idents[] |
| 1741 |
*/ |
| 1742 |
foreach (id; *idents) |
| 1743 |
{ |
| 1744 |
if (id == sm.ident) |
| 1745 |
return 0; |
| 1746 |
|
| 1747 |
// Avoid using strcmp in the first place due to the performance impact in an O(N^2) loop. |
| 1748 |
debug |
| 1749 |
{ |
| 1750 |
import core.stdc.string : strcmp; |
| 1751 |
assert(strcmp(id.toChars(), sm.ident.toChars()) != 0); |
| 1752 |
} |
| 1753 |
} |
| 1754 |
idents.push(sm.ident); |
| 1755 |
} |
| 1756 |
else if (auto ed = sm.isEnumDeclaration()) |
| 1757 |
{ |
| 1758 |
_foreach(null, ed.members, &pushIdentsDg); |
| 1759 |
} |
| 1760 |
return 0; |
| 1761 |
} |
| 1762 |
|
| 1763 |
_foreach(sc, sds.members, &pushIdentsDg); |
| 1764 |
auto cd = sds.isClassDeclaration(); |
| 1765 |
if (cd && e.ident == Id.allMembers) |
| 1766 |
{ |
| 1767 |
if (cd.semanticRun < PASS.semanticdone) |
| 1768 |
cd.dsymbolSemantic(null); // https://issues.dlang.org/show_bug.cgi?id=13668 |
| 1769 |
// Try to resolve forward reference |
| 1770 |
|
| 1771 |
void pushBaseMembersDg(ClassDeclaration cd) |
| 1772 |
{ |
| 1773 |
for (size_t i = 0; i < cd.baseclasses.length; i++) |
| 1774 |
{ |
| 1775 |
auto cb = (*cd.baseclasses)[i].sym; |
| 1776 |
assert(cb); |
| 1777 |
_foreach(null, cb.members, &pushIdentsDg); |
| 1778 |
if (cb.baseclasses.length) |
| 1779 |
pushBaseMembersDg(cb); |
| 1780 |
} |
| 1781 |
} |
| 1782 |
|
| 1783 |
pushBaseMembersDg(cd); |
| 1784 |
} |
| 1785 |
|
| 1786 |
// Turn Identifiers into StringExps reusing the allocated array |
| 1787 |
assert(Expressions.sizeof == Identifiers.sizeof); |
| 1788 |
auto exps = cast(Expressions*)idents; |
| 1789 |
foreach (i, id; *idents) |
| 1790 |
{ |
| 1791 |
auto se = new StringExp(e.loc, id.toString()); |
| 1792 |
(*exps)[i] = se; |
| 1793 |
} |
| 1794 |
|
| 1795 |
/* Making this a tuple is more flexible, as it can be statically unrolled. |
| 1796 |
* To make an array literal, enclose __traits in [ ]: |
| 1797 |
* [ __traits(allMembers, ...) ] |
| 1798 |
*/ |
| 1799 |
Expression ex = new TupleExp(e.loc, exps); |
| 1800 |
ex = ex.expressionSemantic(sc); |
| 1801 |
return ex; |
| 1802 |
} |
| 1803 |
if (e.ident == Id.compiles) |
| 1804 |
{ |
| 1805 |
/* Determine if all the objects - types, expressions, or symbols - |
| 1806 |
* compile without error |
| 1807 |
*/ |
| 1808 |
if (!dim) |
| 1809 |
return False(); |
| 1810 |
|
| 1811 |
foreach (o; *e.args) |
| 1812 |
{ |
| 1813 |
const errors = global.startGagging(); |
| 1814 |
Scope* sc2 = sc.push(); |
| 1815 |
sc2.tinst = null; |
| 1816 |
sc2.minst = null; // this is why code for these are not emitted to object file |
| 1817 |
sc2.copyFlagsFrom(sc); |
| 1818 |
sc2.ctfe = false; |
| 1819 |
sc2.condition = false; |
| 1820 |
sc2.traitsCompiles = true; |
| 1821 |
sc2.fullinst = true; |
| 1822 |
|
| 1823 |
bool err = false; |
| 1824 |
|
| 1825 |
auto t = isType(o); |
| 1826 |
auto ex = isExpression(o); |
| 1827 |
if (t) |
| 1828 |
{ |
| 1829 |
Dsymbol s; |
| 1830 |
t.resolve(e.loc, sc2, ex, t, s); |
| 1831 |
if (t) |
| 1832 |
{ |
| 1833 |
t.typeSemantic(e.loc, sc2); |
| 1834 |
if (t.ty == Terror) |
| 1835 |
err = true; |
| 1836 |
} |
| 1837 |
else if (s && s.errors) |
| 1838 |
err = true; |
| 1839 |
} |
| 1840 |
if (ex) |
| 1841 |
{ |
| 1842 |
ex = ex.expressionSemantic(sc2); |
| 1843 |
ex = resolvePropertiesOnly(sc2, ex); |
| 1844 |
ex = ex.optimize(WANTvalue); |
| 1845 |
if (sc2.func && sc2.func.type.isTypeFunction()) |
| 1846 |
{ |
| 1847 |
const tf = sc2.func.type.isTypeFunction(); |
| 1848 |
err |= tf.isNothrow && canThrow(ex, sc2.func, null); |
| 1849 |
} |
| 1850 |
ex = checkGC(sc2, ex); |
| 1851 |
if (ex.op == EXP.error) |
| 1852 |
err = true; |
| 1853 |
} |
| 1854 |
|
| 1855 |
// Carefully detach the scope from the parent and throw it away as |
| 1856 |
// we only need it to evaluate the expression |
| 1857 |
// https://issues.dlang.org/show_bug.cgi?id=15428 |
| 1858 |
sc2.detach(); |
| 1859 |
|
| 1860 |
if (global.endGagging(errors) || err) |
| 1861 |
{ |
| 1862 |
return False(); |
| 1863 |
} |
| 1864 |
} |
| 1865 |
return True(); |
| 1866 |
} |
| 1867 |
if (e.ident == Id.isSame) |
| 1868 |
{ |
| 1869 |
/* Determine if two symbols are the same |
| 1870 |
*/ |
| 1871 |
if (dim != 2) |
| 1872 |
return dimError(2); |
| 1873 |
|
| 1874 |
// https://issues.dlang.org/show_bug.cgi?id=20761 |
| 1875 |
// tiarg semantic may expand in place the list of arguments, for example: |
| 1876 |
// |
| 1877 |
// before tiarg sema: __traits(isSame, seq!(0,0), seq!(1,1)) |
| 1878 |
// after : __traits(isSame, 0, 0, 1, 1) |
| 1879 |
// |
| 1880 |
// so we split in two lists |
| 1881 |
Objects ob1; |
| 1882 |
ob1.push((*e.args)[0]); |
| 1883 |
Objects ob2; |
| 1884 |
ob2.push((*e.args)[1]); |
| 1885 |
if (!TemplateInstance.semanticTiargs(e.loc, sc, &ob1, 0)) |
| 1886 |
return ErrorExp.get(); |
| 1887 |
if (!TemplateInstance.semanticTiargs(e.loc, sc, &ob2, 0)) |
| 1888 |
return ErrorExp.get(); |
| 1889 |
if (ob1.length != ob2.length) |
| 1890 |
return False(); |
| 1891 |
foreach (immutable i; 0 .. ob1.length) |
| 1892 |
if (!ob1[i].isSame(ob2[i], sc)) |
| 1893 |
return False(); |
| 1894 |
return True(); |
| 1895 |
} |
| 1896 |
if (e.ident == Id.getUnitTests) |
| 1897 |
{ |
| 1898 |
if (dim != 1) |
| 1899 |
return dimError(1); |
| 1900 |
|
| 1901 |
auto o = (*e.args)[0]; |
| 1902 |
auto s = getDsymbolWithoutExpCtx(o); |
| 1903 |
if (!s) |
| 1904 |
{ |
| 1905 |
error(e.loc, "argument `%s` to __traits(getUnitTests) must be a module or aggregate", |
| 1906 |
o.toChars()); |
| 1907 |
return ErrorExp.get(); |
| 1908 |
} |
| 1909 |
if (auto imp = s.isImport()) // https://issues.dlang.org/show_bug.cgi?id=10990 |
| 1910 |
s = imp.mod; |
| 1911 |
|
| 1912 |
auto sds = s.isScopeDsymbol(); |
| 1913 |
if (!sds || sds.isTemplateDeclaration()) |
| 1914 |
{ |
| 1915 |
error(e.loc, "argument `%s` to __traits(getUnitTests) must be a module or aggregate, not a %s", |
| 1916 |
s.toChars(), s.kind()); |
| 1917 |
return ErrorExp.get(); |
| 1918 |
} |
| 1919 |
|
| 1920 |
auto exps = new Expressions(); |
| 1921 |
if (global.params.useUnitTests) |
| 1922 |
{ |
| 1923 |
bool[void*] uniqueUnitTests; |
| 1924 |
|
| 1925 |
void symbolDg(Dsymbol s) |
| 1926 |
{ |
| 1927 |
if (auto ad = s.isAttribDeclaration()) |
| 1928 |
{ |
| 1929 |
ad.include(null).foreachDsymbol(&symbolDg); |
| 1930 |
} |
| 1931 |
else if (auto tm = s.isTemplateMixin()) |
| 1932 |
{ |
| 1933 |
tm.members.foreachDsymbol(&symbolDg); |
| 1934 |
} |
| 1935 |
else if (auto ud = s.isUnitTestDeclaration()) |
| 1936 |
{ |
| 1937 |
if (cast(void*)ud in uniqueUnitTests) |
| 1938 |
return; |
| 1939 |
|
| 1940 |
uniqueUnitTests[cast(void*)ud] = true; |
| 1941 |
|
| 1942 |
auto ad = new FuncAliasDeclaration(ud.ident, ud, false); |
| 1943 |
ad.visibility = ud.visibility; |
| 1944 |
|
| 1945 |
auto e = new DsymbolExp(Loc.initial, ad, false); |
| 1946 |
exps.push(e); |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
sds.members.foreachDsymbol(&symbolDg); |
| 1951 |
} |
| 1952 |
auto te = new TupleExp(e.loc, exps); |
| 1953 |
return te.expressionSemantic(sc); |
| 1954 |
} |
| 1955 |
if (e.ident == Id.getVirtualIndex) |
| 1956 |
{ |
| 1957 |
if (dim != 1) |
| 1958 |
return dimError(1); |
| 1959 |
|
| 1960 |
auto o = (*e.args)[0]; |
| 1961 |
auto s = getDsymbolWithoutExpCtx(o); |
| 1962 |
|
| 1963 |
auto fd = s ? s.isFuncDeclaration() : null; |
| 1964 |
if (!fd) |
| 1965 |
{ |
| 1966 |
error(e.loc, "first argument to __traits(getVirtualIndex) must be a function"); |
| 1967 |
return ErrorExp.get(); |
| 1968 |
} |
| 1969 |
|
| 1970 |
fd = fd.toAliasFunc(); // Necessary to support multiple overloads. |
| 1971 |
return new IntegerExp(e.loc, fd.vtblIndex, Type.tptrdiff_t); |
| 1972 |
} |
| 1973 |
if (e.ident == Id.getPointerBitmap) |
| 1974 |
{ |
| 1975 |
return pointerBitmap(e, global.errorSink); |
| 1976 |
} |
| 1977 |
if (e.ident == Id.initSymbol) |
| 1978 |
{ |
| 1979 |
if (dim != 1) |
| 1980 |
return dimError(1); |
| 1981 |
|
| 1982 |
auto o = (*e.args)[0]; |
| 1983 |
|
| 1984 |
ErrorExp badArgument() |
| 1985 |
{ |
| 1986 |
error(e.loc, "struct / class type expected as argument to __traits(initSymbol) instead of `%s`", o.toChars()); |
| 1987 |
return ErrorExp.get(); |
| 1988 |
} |
| 1989 |
|
| 1990 |
Type t = isType(o); |
| 1991 |
|
| 1992 |
if (!t || t.isTypeEnum()) |
| 1993 |
return badArgument(); |
| 1994 |
|
| 1995 |
AggregateDeclaration ad = isAggregate(t); |
| 1996 |
|
| 1997 |
// Interfaces don't have an init symbol and hence cause linker errors |
| 1998 |
if (!ad || ad.isInterfaceDeclaration()) |
| 1999 |
return badArgument(); |
| 2000 |
|
| 2001 |
Declaration d = new SymbolDeclaration(ad.loc, ad); |
| 2002 |
d.type = Type.tvoid.arrayOf().constOf(); |
| 2003 |
d.storage_class |= STC.rvalue; |
| 2004 |
return new VarExp(e.loc, d); |
| 2005 |
} |
| 2006 |
if (e.ident == Id.isZeroInit) |
| 2007 |
{ |
| 2008 |
if (dim != 1) |
| 2009 |
return dimError(1); |
| 2010 |
|
| 2011 |
auto o = (*e.args)[0]; |
| 2012 |
Type t = isType(o); |
| 2013 |
if (!t) |
| 2014 |
{ |
| 2015 |
error(e.loc, "type expected as second argument of __traits `%s` instead of `%s`", |
| 2016 |
e.ident.toChars(), o.toChars()); |
| 2017 |
return ErrorExp.get(); |
| 2018 |
} |
| 2019 |
|
| 2020 |
// https://issues.dlang.org/show_bug.cgi?id=23534 |
| 2021 |
// |
| 2022 |
// For enums, we need to get the enum initializer |
| 2023 |
// (the first enum member), not the initializer of the |
| 2024 |
// type of the enum members. |
| 2025 |
Type tb = t.isTypeEnum ? t : t.baseElemOf(); |
| 2026 |
return tb.isZeroInit(e.loc) ? True() : False(); |
| 2027 |
} |
| 2028 |
if (e.ident == Id.getTargetInfo) |
| 2029 |
{ |
| 2030 |
if (dim != 1) |
| 2031 |
return dimError(1); |
| 2032 |
|
| 2033 |
auto ex = isExpression((*e.args)[0]); |
| 2034 |
StringExp se = ex ? ex.ctfeInterpret().toStringExp() : null; |
| 2035 |
if (!ex || !se || se.len == 0) |
| 2036 |
{ |
| 2037 |
error(e.loc, "string expected as argument of __traits `%s` instead of `%s`", e.ident.toChars(), (*e.args)[0].toChars()); |
| 2038 |
return ErrorExp.get(); |
| 2039 |
} |
| 2040 |
se = se.toUTF8(sc); |
| 2041 |
|
| 2042 |
const slice = se.peekString(); |
| 2043 |
Expression r = target.getTargetInfo(slice.ptr, e.loc); // BUG: reliance on terminating 0 |
| 2044 |
if (!r) |
| 2045 |
{ |
| 2046 |
error(e.loc, "`getTargetInfo` key `\"%.*s\"` not supported by this implementation", |
| 2047 |
cast(int)slice.length, slice.ptr); |
| 2048 |
return ErrorExp.get(); |
| 2049 |
} |
| 2050 |
return r.expressionSemantic(sc); |
| 2051 |
} |
| 2052 |
if (e.ident == Id.getLocation) |
| 2053 |
{ |
| 2054 |
if (dim != 1) |
| 2055 |
return dimError(1); |
| 2056 |
auto arg0 = (*e.args)[0]; |
| 2057 |
Dsymbol s = getDsymbolWithoutExpCtx(arg0); |
| 2058 |
if (!s || !s.loc.isValid() || s.isModule()) |
| 2059 |
{ |
| 2060 |
error(e.loc, "can only get the location of a symbol, not `%s`", s ? s.toPrettyChars() : arg0.toChars()); |
| 2061 |
return ErrorExp.get(); |
| 2062 |
} |
| 2063 |
|
| 2064 |
const fd = s.isFuncDeclaration(); |
| 2065 |
// FIXME:td.overnext is always set, even when using an index on it |
| 2066 |
//const td = s.isTemplateDeclaration(); |
| 2067 |
if ((fd && fd.overnext) /*|| (td && td.overnext)*/) |
| 2068 |
{ |
| 2069 |
error(e.loc, "cannot get location of an overload set, " ~ |
| 2070 |
"use `__traits(getOverloads, ..., \"%s\"%s)[N]` " ~ |
| 2071 |
"to get the Nth overload", |
| 2072 |
arg0.toChars(), /*td ? ", true".ptr :*/ "".ptr); |
| 2073 |
return ErrorExp.get(); |
| 2074 |
} |
| 2075 |
|
| 2076 |
auto exps = new Expressions(3); |
| 2077 |
(*exps)[0] = new StringExp(e.loc, s.loc.filename.toDString()); |
| 2078 |
(*exps)[1] = new IntegerExp(e.loc, s.loc.linnum,Type.tint32); |
| 2079 |
(*exps)[2] = new IntegerExp(e.loc, s.loc.charnum,Type.tint32); |
| 2080 |
auto tup = new TupleExp(e.loc, exps); |
| 2081 |
return tup.expressionSemantic(sc); |
| 2082 |
} |
| 2083 |
if (e.ident == Id.getCppNamespaces) |
| 2084 |
{ |
| 2085 |
auto o = (*e.args)[0]; |
| 2086 |
auto s = getDsymbolWithoutExpCtx(o); |
| 2087 |
auto exps = new Expressions(0); |
| 2088 |
if (auto d = s.isDeclaration()) |
| 2089 |
{ |
| 2090 |
if (d.inuse) |
| 2091 |
{ |
| 2092 |
.error(d.loc, "%s `%s` circular reference in `__traits(GetCppNamespaces,...)`", d.kind, d.toPrettyChars); |
| 2093 |
return ErrorExp.get(); |
| 2094 |
} |
| 2095 |
d.inuse = 1; |
| 2096 |
} |
| 2097 |
|
| 2098 |
/** |
| 2099 |
Prepend the namespaces in the linked list `ns` to `es`. |
| 2100 |
|
| 2101 |
Returns: true if `ns` contains an `ErrorExp`. |
| 2102 |
*/ |
| 2103 |
bool prependNamespaces(Expressions* es, CPPNamespaceDeclaration ns) |
| 2104 |
{ |
| 2105 |
// Semantic processing will convert `extern(C++, "a", "b", "c")` |
| 2106 |
// into `extern(C++, "a") extern(C++, "b") extern(C++, "c")`, |
| 2107 |
// creating a linked list what `a`'s `cppnamespace` points to `b`, |
| 2108 |
// and `b`'s points to `c`. Our entry point is `a`. |
| 2109 |
for (; ns !is null; ns = ns.cppnamespace) |
| 2110 |
{ |
| 2111 |
ns.dsymbolSemantic(sc); |
| 2112 |
|
| 2113 |
if (ns.exp.isErrorExp()) |
| 2114 |
return true; |
| 2115 |
|
| 2116 |
auto se = ns.exp.toStringExp(); |
| 2117 |
// extern(C++, (emptyTuple)) |
| 2118 |
// struct D {} |
| 2119 |
// will produce a blank ident |
| 2120 |
if (!se.len) |
| 2121 |
continue; |
| 2122 |
es.insert(0, se); |
| 2123 |
} |
| 2124 |
return false; |
| 2125 |
} |
| 2126 |
for (auto p = s; !p.isModule(); p = p.toParent()) |
| 2127 |
{ |
| 2128 |
p.dsymbolSemantic(sc); |
| 2129 |
auto pp = p.toParent(); |
| 2130 |
if (pp.isTemplateInstance()) |
| 2131 |
{ |
| 2132 |
if (!p.cppnamespace) |
| 2133 |
continue; |
| 2134 |
//if (!p.toParent().cppnamespace) |
| 2135 |
// continue; |
| 2136 |
auto inner = new Expressions(0); |
| 2137 |
auto outer = new Expressions(0); |
| 2138 |
if (prependNamespaces(inner, p.cppnamespace)) return ErrorExp.get(); |
| 2139 |
if (prependNamespaces(outer, pp.cppnamespace)) return ErrorExp.get(); |
| 2140 |
|
| 2141 |
size_t i = 0; |
| 2142 |
while(i < outer.length && ((*inner)[i]) == (*outer)[i]) |
| 2143 |
i++; |
| 2144 |
|
| 2145 |
foreach_reverse (ns; (*inner)[][i .. $]) |
| 2146 |
exps.insert(0, ns); |
| 2147 |
continue; |
| 2148 |
} |
| 2149 |
|
| 2150 |
if (p.isNspace()) |
| 2151 |
exps.insert(0, new StringExp(p.loc, p.ident.toString())); |
| 2152 |
|
| 2153 |
if (prependNamespaces(exps, p.cppnamespace)) |
| 2154 |
return ErrorExp.get(); |
| 2155 |
} |
| 2156 |
if (auto d = s.isDeclaration()) |
| 2157 |
d.inuse = 0; |
| 2158 |
auto tup = new TupleExp(e.loc, exps); |
| 2159 |
return tup.expressionSemantic(sc); |
| 2160 |
} |
| 2161 |
//https://issues.dlang.org/show_bug.cgi?id=22291 |
| 2162 |
if (e.ident == Id.parameters) |
| 2163 |
{ |
| 2164 |
//No args are valid |
| 2165 |
if (e.args) |
| 2166 |
{ |
| 2167 |
char[] contents = cast(char[]) e.args.toString(); |
| 2168 |
contents = contents[1..$]; |
| 2169 |
contents[$-1] = '\0'; |
| 2170 |
error(e.loc, "`__traits(parameters)` cannot have arguments, but `%s` was supplied", contents.ptr); |
| 2171 |
return ErrorExp.get(); |
| 2172 |
} |
| 2173 |
|
| 2174 |
auto fd = sc.getEnclosingFunction(); |
| 2175 |
if (!fd) |
| 2176 |
{ |
| 2177 |
error(e.loc, "`__traits(parameters)` may only be used inside a function"); |
| 2178 |
return ErrorExp.get(); |
| 2179 |
} |
| 2180 |
|
| 2181 |
auto tf = fd.type.isTypeFunction(); |
| 2182 |
assert(tf); |
| 2183 |
auto exps = new Expressions(0); |
| 2184 |
int addParameterDG(size_t idx, Parameter x) |
| 2185 |
{ |
| 2186 |
assert(x.ident); |
| 2187 |
exps.push(new IdentifierExp(e.loc, x.ident)); |
| 2188 |
return 0; |
| 2189 |
} |
| 2190 |
/* |
| 2191 |
This is required since not all "parameters" actually have a name |
| 2192 |
until they (tuples) are expanded e.g. an anonymous tuple parameter's |
| 2193 |
contents get given names but not the tuple itself. |
| 2194 |
*/ |
| 2195 |
Parameter._foreach(tf.parameterList.parameters, &addParameterDG); |
| 2196 |
auto tup = new TupleExp(e.loc, exps); |
| 2197 |
return tup.expressionSemantic(sc); |
| 2198 |
} |
| 2199 |
|
| 2200 |
/* Can't find the identifier. Try a spell check for a better error message |
| 2201 |
*/ |
| 2202 |
traitNotFound(e); |
| 2203 |
|
| 2204 |
return ErrorExp.get(); |
| 2205 |
} |
| 2206 |
|
| 2207 |
/// compare arguments of __traits(isSame) |
| 2208 |
private bool isSame(RootObject o1, RootObject o2, Scope* sc) |
| 2209 |
{ |
| 2210 |
static FuncLiteralDeclaration isLambda(RootObject oarg) |
| 2211 |
{ |
| 2212 |
if (auto t = isDsymbol(oarg)) |
| 2213 |
{ |
| 2214 |
if (auto td = t.isTemplateDeclaration()) |
| 2215 |
{ |
| 2216 |
if (td.members && td.members.length == 1) |
| 2217 |
{ |
| 2218 |
if (auto fd = (*td.members)[0].isFuncLiteralDeclaration()) |
| 2219 |
return fd; |
| 2220 |
} |
| 2221 |
} |
| 2222 |
} |
| 2223 |
else if (auto ea = isExpression(oarg)) |
| 2224 |
{ |
| 2225 |
if (ea.op == EXP.function_) |
| 2226 |
{ |
| 2227 |
if (auto fe = ea.isFuncExp()) |
| 2228 |
return fe.fd; |
| 2229 |
} |
| 2230 |
} |
| 2231 |
return null; |
| 2232 |
} |
| 2233 |
|
| 2234 |
auto l1 = isLambda(o1); |
| 2235 |
auto l2 = isLambda(o2); |
| 2236 |
|
| 2237 |
if (l1 && l2) |
| 2238 |
{ |
| 2239 |
import dmd.lambdacomp : isSameFuncLiteral; |
| 2240 |
if (isSameFuncLiteral(l1, l2, sc)) |
| 2241 |
return true; |
| 2242 |
} |
| 2243 |
|
| 2244 |
// https://issues.dlang.org/show_bug.cgi?id=12001, allow isSame, <BasicType>, <BasicType> |
| 2245 |
Type t1 = isType(o1); |
| 2246 |
Type t2 = isType(o2); |
| 2247 |
if (t1 && t2 && t1.equals(t2)) |
| 2248 |
return true; |
| 2249 |
|
| 2250 |
auto s1 = getDsymbol(o1); |
| 2251 |
auto s2 = getDsymbol(o2); |
| 2252 |
//printf("isSame: %s, %s\n", o1.toChars(), o2.toChars()); |
| 2253 |
version (none) |
| 2254 |
{ |
| 2255 |
printf("o1: %p\n", o1); |
| 2256 |
printf("o2: %p\n", o2); |
| 2257 |
if (!s1) |
| 2258 |
{ |
| 2259 |
if (auto ea = isExpression(o1)) |
| 2260 |
printf("%s\n", ea.toChars()); |
| 2261 |
if (auto ta = isType(o1)) |
| 2262 |
printf("%s\n", ta.toChars()); |
| 2263 |
return false; |
| 2264 |
} |
| 2265 |
else |
| 2266 |
printf("%s %s\n", s1.kind(), s1.toChars()); |
| 2267 |
} |
| 2268 |
if (!s1 && !s2) |
| 2269 |
{ |
| 2270 |
auto ea1 = isExpression(o1); |
| 2271 |
auto ea2 = isExpression(o2); |
| 2272 |
if (ea1 && ea2) |
| 2273 |
{ |
| 2274 |
if (ea1.equals(ea2)) |
| 2275 |
return true; |
| 2276 |
} |
| 2277 |
} |
| 2278 |
if (!s1 || !s2) |
| 2279 |
return false; |
| 2280 |
|
| 2281 |
s1 = s1.toAlias(); |
| 2282 |
s2 = s2.toAlias(); |
| 2283 |
|
| 2284 |
if (auto fa1 = s1.isFuncAliasDeclaration()) |
| 2285 |
s1 = fa1.toAliasFunc(); |
| 2286 |
if (auto fa2 = s2.isFuncAliasDeclaration()) |
| 2287 |
s2 = fa2.toAliasFunc(); |
| 2288 |
|
| 2289 |
// https://issues.dlang.org/show_bug.cgi?id=11259 |
| 2290 |
// compare import symbol to a package symbol |
| 2291 |
static bool cmp(Dsymbol s1, Dsymbol s2) |
| 2292 |
{ |
| 2293 |
auto imp = s1.isImport(); |
| 2294 |
return imp && imp.pkg && imp.pkg == s2.isPackage(); |
| 2295 |
} |
| 2296 |
|
| 2297 |
if (cmp(s1,s2) || cmp(s2,s1)) |
| 2298 |
return true; |
| 2299 |
|
| 2300 |
if (s1 == s2) |
| 2301 |
return true; |
| 2302 |
|
| 2303 |
// https://issues.dlang.org/show_bug.cgi?id=18771 |
| 2304 |
// OverloadSets are equal if they contain the same functions |
| 2305 |
auto overSet1 = s1.isOverloadSet(); |
| 2306 |
if (!overSet1) |
| 2307 |
return false; |
| 2308 |
|
| 2309 |
auto overSet2 = s2.isOverloadSet(); |
| 2310 |
if (!overSet2) |
| 2311 |
return false; |
| 2312 |
|
| 2313 |
if (overSet1.a.length != overSet2.a.length) |
| 2314 |
return false; |
| 2315 |
|
| 2316 |
// OverloadSets contain array of Dsymbols => O(n*n) |
| 2317 |
// to compare for equality as the order of overloads |
| 2318 |
// might not be the same |
| 2319 |
Lnext: |
| 2320 |
foreach(overload1; overSet1.a) |
| 2321 |
{ |
| 2322 |
foreach(overload2; overSet2.a) |
| 2323 |
{ |
| 2324 |
if (overload1 == overload2) |
| 2325 |
continue Lnext; |
| 2326 |
} |
| 2327 |
return false; |
| 2328 |
} |
| 2329 |
return true; |
| 2330 |
} |
| 2331 |
|
| 2332 |
|
| 2333 |
/*********************************** |
| 2334 |
* A trait was not found. Give a decent error message |
| 2335 |
* by trying a spell check. |
| 2336 |
* Params: |
| 2337 |
* e = the offending trait |
| 2338 |
*/ |
| 2339 |
private void traitNotFound(TraitsExp e) |
| 2340 |
{ |
| 2341 |
__gshared const StringTable!bool traitsStringTable; |
| 2342 |
__gshared bool initialized; |
| 2343 |
|
| 2344 |
if (!initialized) |
| 2345 |
{ |
| 2346 |
initialized = true; // lazy initialization |
| 2347 |
|
| 2348 |
// All possible traits |
| 2349 |
__gshared Identifier*[59] idents = |
| 2350 |
[ |
| 2351 |
&Id.allMembers, |
| 2352 |
&Id.child, |
| 2353 |
&Id.classInstanceAlignment, |
| 2354 |
&Id.classInstanceSize, |
| 2355 |
&Id.compiles, |
| 2356 |
&Id.derivedMembers, |
| 2357 |
&Id.fullyQualifiedName, |
| 2358 |
&Id.getAliasThis, |
| 2359 |
&Id.getAttributes, |
| 2360 |
&Id.getFunctionAttributes, |
| 2361 |
&Id.getFunctionVariadicStyle, |
| 2362 |
&Id.getLinkage, |
| 2363 |
&Id.getLocation, |
| 2364 |
&Id.getMember, |
| 2365 |
&Id.getOverloads, |
| 2366 |
&Id.getParameterStorageClasses, |
| 2367 |
&Id.getPointerBitmap, |
| 2368 |
&Id.getProtection, |
| 2369 |
&Id.getTargetInfo, |
| 2370 |
&Id.getUnitTests, |
| 2371 |
&Id.getVirtualFunctions, |
| 2372 |
&Id.getVirtualIndex, |
| 2373 |
&Id.getVirtualMethods, |
| 2374 |
&Id.getVisibility, |
| 2375 |
&Id.hasCopyConstructor, |
| 2376 |
&Id.hasMember, |
| 2377 |
&Id.hasPostblit, |
| 2378 |
&Id.identifier, |
| 2379 |
&Id.isAbstractClass, |
| 2380 |
&Id.isAbstractFunction, |
| 2381 |
&Id.isArithmetic, |
| 2382 |
&Id.isAssociativeArray, |
| 2383 |
&Id.isCopyable, |
| 2384 |
&Id.isDeprecated, |
| 2385 |
&Id.isDisabled, |
| 2386 |
&Id.isFinalClass, |
| 2387 |
&Id.isFinalFunction, |
| 2388 |
&Id.isFloating, |
| 2389 |
&Id.isFuture, |
| 2390 |
&Id.isIntegral, |
| 2391 |
&Id.isLazy, |
| 2392 |
&Id.isModule, |
| 2393 |
&Id.isNested, |
| 2394 |
&Id.isOut, |
| 2395 |
&Id.isOverrideFunction, |
| 2396 |
&Id.isPackage, |
| 2397 |
&Id.isPOD, |
| 2398 |
&Id.isRef, |
| 2399 |
&Id.isReturnOnStack, |
| 2400 |
&Id.isSame, |
| 2401 |
&Id.isScalar, |
| 2402 |
&Id.isStaticArray, |
| 2403 |
&Id.isStaticFunction, |
| 2404 |
&Id.isUnsigned, |
| 2405 |
&Id.isVirtualFunction, |
| 2406 |
&Id.isVirtualMethod, |
| 2407 |
&Id.isZeroInit, |
| 2408 |
&Id.parameters, |
| 2409 |
&Id.parent, |
| 2410 |
]; |
| 2411 |
|
| 2412 |
StringTable!(bool)* stringTable = cast(StringTable!(bool)*) &traitsStringTable; |
| 2413 |
stringTable._init(idents.length); |
| 2414 |
|
| 2415 |
foreach (id; idents) |
| 2416 |
{ |
| 2417 |
auto sv = stringTable.insert((*id).toString(), true); |
| 2418 |
assert(sv); |
| 2419 |
} |
| 2420 |
} |
| 2421 |
|
| 2422 |
static const(char)[] trait_search_fp(const(char)[] seed, out int cost) |
| 2423 |
{ |
| 2424 |
//printf("trait_search_fp('%s')\n", seed); |
| 2425 |
if (!seed.length) |
| 2426 |
return null; |
| 2427 |
cost = 0; // all the same cost |
| 2428 |
const sv = traitsStringTable.lookup(seed); |
| 2429 |
return sv ? sv.toString() : null; |
| 2430 |
} |
| 2431 |
|
| 2432 |
if (auto sub = speller!trait_search_fp(e.ident.toString())) |
| 2433 |
error(e.loc, "unrecognized trait `%s`, did you mean `%.*s`?", e.ident.toChars(), cast(int) sub.length, sub.ptr); |
| 2434 |
else |
| 2435 |
error(e.loc, "unrecognized trait `%s`", e.ident.toChars()); |
| 2436 |
} |