| 1 |
/** |
| 2 |
* This module contains the implementation of the C++ header generation available through |
| 3 |
* the command line switch -Hc. |
| 4 |
* |
| 5 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 6 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 7 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 8 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/dtoh.d, _dtoh.d) |
| 9 |
* Documentation: https://dlang.org/phobos/dmd_dtoh.html |
| 10 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/dtoh.d |
| 11 |
*/ |
| 12 |
module dmd.dtoh; |
| 13 |
|
| 14 |
import core.stdc.stdio; |
| 15 |
import core.stdc.string; |
| 16 |
import core.stdc.ctype; |
| 17 |
|
| 18 |
import dmd.astcodegen; |
| 19 |
import dmd.astenums; |
| 20 |
import dmd.arraytypes; |
| 21 |
import dmd.attrib; |
| 22 |
import dmd.dsymbol; |
| 23 |
import dmd.dsymbolsem; |
| 24 |
import dmd.errors; |
| 25 |
import dmd.globals; |
| 26 |
import dmd.hdrgen; |
| 27 |
import dmd.id; |
| 28 |
import dmd.identifier; |
| 29 |
import dmd.location; |
| 30 |
import dmd.root.filename; |
| 31 |
import dmd.visitor; |
| 32 |
import dmd.tokens; |
| 33 |
import dmd.typesem; |
| 34 |
|
| 35 |
import dmd.common.outbuffer; |
| 36 |
import dmd.utils; |
| 37 |
|
| 38 |
//debug = Debug_DtoH; |
| 39 |
|
| 40 |
// Generate asserts to validate the header |
| 41 |
//debug = Debug_DtoH_Checks; |
| 42 |
|
| 43 |
/** |
| 44 |
* Generates a C++ header containing bindings for all `extern(C[++])` declarations |
| 45 |
* found in the supplied modules. |
| 46 |
* |
| 47 |
* Params: |
| 48 |
* ms = the modules |
| 49 |
* |
| 50 |
* Notes: |
| 51 |
* - the header is written to `<global.params.cxxhdrdir>/<global.params.cxxhdrfile>` |
| 52 |
* or `stdout` if no explicit file was specified |
| 53 |
* - bindings conform to the C++ standard defined in `global.params.cplusplus` |
| 54 |
* - ignored declarations are mentioned in a comment if `global.params.doCxxHdrGeneration` |
| 55 |
* is set to `CxxHeaderMode.verbose` |
| 56 |
*/ |
| 57 |
void genCppHdrFiles(ref Modules ms) |
| 58 |
{ |
| 59 |
initialize(); |
| 60 |
|
| 61 |
OutBuffer fwd; |
| 62 |
OutBuffer done; |
| 63 |
OutBuffer decl; |
| 64 |
|
| 65 |
// enable indent by spaces on buffers |
| 66 |
fwd.doindent = true; |
| 67 |
fwd.spaces = true; |
| 68 |
decl.doindent = true; |
| 69 |
decl.spaces = true; |
| 70 |
|
| 71 |
scope v = new ToCppBuffer(&fwd, &done, &decl); |
| 72 |
|
| 73 |
// Conditionally include another buffer for sanity checks |
| 74 |
debug (Debug_DtoH_Checks) |
| 75 |
{ |
| 76 |
OutBuffer check; |
| 77 |
check.doindent = true; |
| 78 |
check.spaces = true; |
| 79 |
v.checkbuf = ✓ |
| 80 |
} |
| 81 |
|
| 82 |
OutBuffer buf; |
| 83 |
buf.doindent = true; |
| 84 |
buf.spaces = true; |
| 85 |
|
| 86 |
foreach (m; ms) |
| 87 |
m.accept(v); |
| 88 |
|
| 89 |
if (global.params.cxxhdr.fullOutput) |
| 90 |
buf.printf("// Automatically generated by %s Compiler v%d", global.compileEnv.vendor.ptr, global.versionNumber()); |
| 91 |
else |
| 92 |
buf.printf("// Automatically generated by %s Compiler", global.compileEnv.vendor.ptr); |
| 93 |
|
| 94 |
buf.writenl(); |
| 95 |
buf.writenl(); |
| 96 |
buf.writestringln("#pragma once"); |
| 97 |
buf.writenl(); |
| 98 |
hashInclude(buf, "<assert.h>"); |
| 99 |
hashInclude(buf, "<math.h>"); |
| 100 |
hashInclude(buf, "<stddef.h>"); |
| 101 |
hashInclude(buf, "<stdint.h>"); |
| 102 |
// buf.writestring(buf, "#include <stdio.h>\n"); |
| 103 |
// buf.writestring("#include <string.h>\n"); |
| 104 |
|
| 105 |
// Emit array compatibility because extern(C++) types may have slices |
| 106 |
// as members (as opposed to function parameters) |
| 107 |
if (v.hasDArray) |
| 108 |
buf.writestring(` |
| 109 |
#ifdef CUSTOM_D_ARRAY_TYPE |
| 110 |
#define _d_dynamicArray CUSTOM_D_ARRAY_TYPE |
| 111 |
#else |
| 112 |
/// Represents a D [] array |
| 113 |
template<typename T> |
| 114 |
struct _d_dynamicArray final |
| 115 |
{ |
| 116 |
size_t length; |
| 117 |
T *ptr; |
| 118 |
|
| 119 |
_d_dynamicArray() : length(0), ptr(NULL) { } |
| 120 |
|
| 121 |
_d_dynamicArray(size_t length_in, T *ptr_in) |
| 122 |
: length(length_in), ptr(ptr_in) { } |
| 123 |
|
| 124 |
T& operator[](const size_t idx) { |
| 125 |
assert(idx < length); |
| 126 |
return ptr[idx]; |
| 127 |
} |
| 128 |
|
| 129 |
const T& operator[](const size_t idx) const { |
| 130 |
assert(idx < length); |
| 131 |
return ptr[idx]; |
| 132 |
} |
| 133 |
}; |
| 134 |
#endif |
| 135 |
`); |
| 136 |
|
| 137 |
if (v.hasExternSystem) |
| 138 |
buf.writestring(` |
| 139 |
#ifndef _WIN32 |
| 140 |
#define EXTERN_SYSTEM_AFTER __stdcall |
| 141 |
#define EXTERN_SYSTEM_BEFORE |
| 142 |
#else |
| 143 |
#define EXTERN_SYSTEM_AFTER |
| 144 |
#define EXTERN_SYSTEM_BEFORE extern "C" |
| 145 |
#endif |
| 146 |
`); |
| 147 |
|
| 148 |
if (v.hasReal) |
| 149 |
{ |
| 150 |
hashIf(buf, "!defined(_d_real)"); |
| 151 |
{ |
| 152 |
hashDefine(buf, "_d_real long double"); |
| 153 |
} |
| 154 |
hashEndIf(buf); |
| 155 |
} |
| 156 |
buf.writenl(); |
| 157 |
// buf.writestringln("// fwd:"); |
| 158 |
buf.write(&fwd); |
| 159 |
if (fwd.length > 0) |
| 160 |
buf.writenl(); |
| 161 |
|
| 162 |
// buf.writestringln("// done:"); |
| 163 |
buf.write(&done); |
| 164 |
|
| 165 |
// buf.writestringln("// decl:"); |
| 166 |
buf.write(&decl); |
| 167 |
|
| 168 |
debug (Debug_DtoH_Checks) |
| 169 |
{ |
| 170 |
// buf.writestringln("// check:"); |
| 171 |
buf.writestring(` |
| 172 |
#if OFFSETS |
| 173 |
template <class T> |
| 174 |
size_t getSlotNumber(int dummy, ...) |
| 175 |
{ |
| 176 |
T c; |
| 177 |
va_list ap; |
| 178 |
va_start(ap, dummy); |
| 179 |
|
| 180 |
void *f = va_arg(ap, void*); |
| 181 |
for (size_t i = 0; ; i++) |
| 182 |
{ |
| 183 |
if ( (*(void***)&c)[i] == f) |
| 184 |
return i; |
| 185 |
} |
| 186 |
va_end(ap); |
| 187 |
} |
| 188 |
|
| 189 |
void testOffsets() |
| 190 |
{ |
| 191 |
`); |
| 192 |
buf.write(&check); |
| 193 |
buf.writestring(` |
| 194 |
} |
| 195 |
#endif |
| 196 |
`); |
| 197 |
} |
| 198 |
|
| 199 |
// prevent trailing newlines |
| 200 |
version (Windows) |
| 201 |
while (buf.length >= 4 && buf[$-4..$] == "\r\n\r\n") |
| 202 |
buf.remove(buf.length - 2, 2); |
| 203 |
else |
| 204 |
while (buf.length >= 2 && buf[$-2..$] == "\n\n") |
| 205 |
buf.remove(buf.length - 1, 1); |
| 206 |
|
| 207 |
|
| 208 |
if (global.params.cxxhdr.name is null) |
| 209 |
{ |
| 210 |
// Write to stdout; assume it succeeds |
| 211 |
size_t n = fwrite(buf[].ptr, 1, buf.length, stdout); |
| 212 |
assert(n == buf.length); // keep gcc happy about return values |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
const(char)[] name = FileName.combine(global.params.cxxhdr.dir, global.params.cxxhdr.name); |
| 217 |
if (!writeFile(Loc.initial, name, buf[])) |
| 218 |
return fatal(); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
private: |
| 223 |
|
| 224 |
/**************************************************** |
| 225 |
* Visitor that writes bindings for `extern(C[++]` declarations. |
| 226 |
*/ |
| 227 |
extern(C++) final class ToCppBuffer : Visitor |
| 228 |
{ |
| 229 |
alias visit = Visitor.visit; |
| 230 |
public: |
| 231 |
enum EnumKind |
| 232 |
{ |
| 233 |
Int, |
| 234 |
Numeric, |
| 235 |
String, |
| 236 |
Enum, |
| 237 |
Other |
| 238 |
} |
| 239 |
|
| 240 |
/// Namespace providing the actual AST nodes |
| 241 |
alias AST = ASTCodegen; |
| 242 |
|
| 243 |
/// Visited nodes |
| 244 |
bool[void*] visited; |
| 245 |
|
| 246 |
/// Forward declared nodes (which might not be emitted yet) |
| 247 |
bool[void*] forwarded; |
| 248 |
|
| 249 |
/// Buffer for forward declarations |
| 250 |
OutBuffer* fwdbuf; |
| 251 |
|
| 252 |
/// Buffer for integrity checks |
| 253 |
debug (Debug_DtoH_Checks) OutBuffer* checkbuf; |
| 254 |
|
| 255 |
/// Buffer for declarations that must emitted before the currently |
| 256 |
/// visited node but can't be forward declared (see `includeSymbol`) |
| 257 |
OutBuffer* donebuf; |
| 258 |
|
| 259 |
/// Default buffer for the currently visited declaration |
| 260 |
OutBuffer* buf; |
| 261 |
|
| 262 |
/// The generated header uses `real` emitted as `_d_real`? |
| 263 |
bool hasReal = false; |
| 264 |
|
| 265 |
/// The generated header has extern(System) functions, |
| 266 |
/// which needs support macros in the header |
| 267 |
bool hasExternSystem = false; |
| 268 |
|
| 269 |
/// There are functions taking slices, which need a compatibility struct for C++ |
| 270 |
bool hasDArray = false; |
| 271 |
|
| 272 |
/// The generated header should contain comments for skipped declarations? |
| 273 |
const bool printIgnored; |
| 274 |
|
| 275 |
/// State specific to the current context which depends |
| 276 |
/// on the currently visited node and it's parents |
| 277 |
static struct Context |
| 278 |
{ |
| 279 |
/// Default linkage in the current scope (e.g. LINK.c inside `extern(C) { ... }`) |
| 280 |
LINK linkage = LINK.d; |
| 281 |
|
| 282 |
/// Enclosing class / struct / union |
| 283 |
AST.AggregateDeclaration adparent; |
| 284 |
|
| 285 |
/// Enclosing template declaration |
| 286 |
AST.TemplateDeclaration tdparent; |
| 287 |
|
| 288 |
/// Identifier of the currently visited `VarDeclaration` |
| 289 |
/// (required to write variables of funtion pointers) |
| 290 |
Identifier ident; |
| 291 |
|
| 292 |
/// Original type of the currently visited declaration |
| 293 |
AST.Type origType; |
| 294 |
|
| 295 |
/// Last written visibility level applying to the current scope |
| 296 |
AST.Visibility.Kind currentVisibility; |
| 297 |
|
| 298 |
/// Currently applicable storage classes |
| 299 |
AST.STC storageClass; |
| 300 |
|
| 301 |
/// How many symbols were ignored |
| 302 |
int ignoredCounter; |
| 303 |
|
| 304 |
/// Currently visited types are required by another declaration |
| 305 |
/// and hence must be emitted |
| 306 |
bool mustEmit; |
| 307 |
|
| 308 |
/// Processing a type that can be forward referenced |
| 309 |
bool forwarding; |
| 310 |
|
| 311 |
/// Inside of an anonymous struct/union (AnonDeclaration) |
| 312 |
bool inAnonymousDecl; |
| 313 |
} |
| 314 |
|
| 315 |
/// Informations about the current context in the AST |
| 316 |
Context context; |
| 317 |
|
| 318 |
// Generates getter-setter methods to replace the use of alias this |
| 319 |
// This should be replaced by a `static foreach` once the gdc tester |
| 320 |
// gets upgraded to version 10 (to support `static foreach`). |
| 321 |
private extern(D) static string generateMembers() @safe |
| 322 |
{ |
| 323 |
string result = ""; |
| 324 |
foreach(member; __traits(allMembers, Context)) |
| 325 |
{ |
| 326 |
result ~= "ref auto " ~ member ~ "() { return context." ~ member ~ "; }\n"; |
| 327 |
} |
| 328 |
return result; |
| 329 |
} |
| 330 |
mixin(generateMembers()); |
| 331 |
|
| 332 |
this(OutBuffer* fwdbuf, OutBuffer* donebuf, OutBuffer* buf) scope |
| 333 |
{ |
| 334 |
this.fwdbuf = fwdbuf; |
| 335 |
this.donebuf = donebuf; |
| 336 |
this.buf = buf; |
| 337 |
this.printIgnored = global.params.cxxhdr.fullOutput; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Emits `dsym` into `donebuf` s.t. it is declared before the currently |
| 342 |
* visited symbol that written to `buf`. |
| 343 |
* |
| 344 |
* Temporarily clears `context` to behave as if it was visited normally. |
| 345 |
*/ |
| 346 |
private void includeSymbol(AST.Dsymbol dsym) |
| 347 |
{ |
| 348 |
debug (Debug_DtoH) |
| 349 |
{ |
| 350 |
printf("[includeSymbol(AST.Dsymbol) enter] %s\n", dsym.toChars()); |
| 351 |
scope(exit) printf("[includeSymbol(AST.Dsymbol) exit] %s\n", dsym.toChars()); |
| 352 |
} |
| 353 |
|
| 354 |
auto ptr = cast(void*) dsym in visited; |
| 355 |
if (ptr && *ptr) |
| 356 |
return; |
| 357 |
|
| 358 |
// Temporary replacement for `buf` which is appended to `donebuf` |
| 359 |
OutBuffer decl; |
| 360 |
decl.doindent = true; |
| 361 |
decl.spaces = true; |
| 362 |
scope (exit) donebuf.write(&decl); |
| 363 |
|
| 364 |
auto ctxStash = this.context; |
| 365 |
auto bufStash = this.buf; |
| 366 |
|
| 367 |
this.context = Context.init; |
| 368 |
this.buf = &decl; |
| 369 |
this.mustEmit = true; |
| 370 |
|
| 371 |
dsym.accept(this); |
| 372 |
|
| 373 |
this.context = ctxStash; |
| 374 |
this.buf = bufStash; |
| 375 |
} |
| 376 |
|
| 377 |
/// Determines what kind of enum `type` is (see `EnumKind`) |
| 378 |
private EnumKind getEnumKind(AST.Type type) |
| 379 |
{ |
| 380 |
if (type) switch (type.ty) |
| 381 |
{ |
| 382 |
case AST.Tint32: |
| 383 |
return EnumKind.Int; |
| 384 |
case AST.Tbool, |
| 385 |
AST.Tchar, AST.Twchar, AST.Tdchar, |
| 386 |
AST.Tint8, AST.Tuns8, |
| 387 |
AST.Tint16, AST.Tuns16, |
| 388 |
AST.Tuns32, |
| 389 |
AST.Tint64, AST.Tuns64: |
| 390 |
return EnumKind.Numeric; |
| 391 |
case AST.Tarray: |
| 392 |
if (type.isString()) |
| 393 |
return EnumKind.String; |
| 394 |
break; |
| 395 |
case AST.Tenum: |
| 396 |
return EnumKind.Enum; |
| 397 |
default: |
| 398 |
break; |
| 399 |
} |
| 400 |
return EnumKind.Other; |
| 401 |
} |
| 402 |
|
| 403 |
/// Determines the type used to represent `type` in C++. |
| 404 |
/// Returns: `const [w,d]char*` for `[w,d]string` or `type` |
| 405 |
private AST.Type determineEnumType(AST.Type type) |
| 406 |
{ |
| 407 |
if (auto arr = type.isTypeDArray()) |
| 408 |
{ |
| 409 |
switch (arr.next.ty) |
| 410 |
{ |
| 411 |
case AST.Tchar: return AST.Type.tchar.constOf.pointerTo; |
| 412 |
case AST.Twchar: return AST.Type.twchar.constOf.pointerTo; |
| 413 |
case AST.Tdchar: return AST.Type.tdchar.constOf.pointerTo; |
| 414 |
default: break; |
| 415 |
} |
| 416 |
} |
| 417 |
return type; |
| 418 |
} |
| 419 |
|
| 420 |
/// Writes a final `;` and insert an empty line outside of aggregates |
| 421 |
private void writeDeclEnd() @safe |
| 422 |
{ |
| 423 |
buf.writestringln(";"); |
| 424 |
|
| 425 |
if (!adparent) |
| 426 |
buf.writenl(); |
| 427 |
} |
| 428 |
|
| 429 |
/// Writes the corresponding access specifier if necessary |
| 430 |
private void writeProtection(const AST.Visibility.Kind kind) |
| 431 |
{ |
| 432 |
// Don't write visibility for global declarations |
| 433 |
if (!adparent || inAnonymousDecl) |
| 434 |
return; |
| 435 |
|
| 436 |
string token; |
| 437 |
|
| 438 |
switch(kind) with(AST.Visibility.Kind) |
| 439 |
{ |
| 440 |
case none, private_: |
| 441 |
if (this.currentVisibility == AST.Visibility.Kind.private_) |
| 442 |
return; |
| 443 |
this.currentVisibility = AST.Visibility.Kind.private_; |
| 444 |
token = "private:"; |
| 445 |
break; |
| 446 |
|
| 447 |
case package_, protected_: |
| 448 |
if (this.currentVisibility == AST.Visibility.Kind.protected_) |
| 449 |
return; |
| 450 |
this.currentVisibility = AST.Visibility.Kind.protected_; |
| 451 |
token = "protected:"; |
| 452 |
break; |
| 453 |
|
| 454 |
case undefined, public_, export_: |
| 455 |
if (this.currentVisibility == AST.Visibility.Kind.public_) |
| 456 |
return; |
| 457 |
this.currentVisibility = AST.Visibility.Kind.public_; |
| 458 |
token = "public:"; |
| 459 |
break; |
| 460 |
|
| 461 |
default: |
| 462 |
printf("Unexpected visibility: %d!\n", kind); |
| 463 |
assert(0); |
| 464 |
} |
| 465 |
|
| 466 |
buf.level--; |
| 467 |
buf.writestringln(token); |
| 468 |
buf.level++; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Writes an identifier into `buf` and checks for reserved identifiers. The |
| 473 |
* parameter `canFix` determines how this function handles C++ keywords: |
| 474 |
* |
| 475 |
* `false` => Raise a warning and print the identifier as-is |
| 476 |
* `true` => Append an underscore to the identifier |
| 477 |
* |
| 478 |
* Params: |
| 479 |
* s = the symbol denoting the identifier |
| 480 |
* canFixup = whether the identifier may be changed without affecting |
| 481 |
* binary compatibility |
| 482 |
*/ |
| 483 |
private void writeIdentifier(const AST.Dsymbol s, const bool canFix = false) |
| 484 |
{ |
| 485 |
if (const mn = getMangleOverride(s)) |
| 486 |
return buf.writestring(mn); |
| 487 |
|
| 488 |
writeIdentifier(s.ident, s.loc, s.kind(), canFix); |
| 489 |
} |
| 490 |
|
| 491 |
/** Overload of `writeIdentifier` used for all AST nodes not descending from Dsymbol **/ |
| 492 |
private void writeIdentifier(const Identifier ident, const Loc loc, const char* kind, const bool canFix = false) |
| 493 |
{ |
| 494 |
bool needsFix; |
| 495 |
|
| 496 |
void warnCxxCompat(const(char)* reason) |
| 497 |
{ |
| 498 |
if (canFix) |
| 499 |
{ |
| 500 |
needsFix = true; |
| 501 |
return; |
| 502 |
} |
| 503 |
|
| 504 |
__gshared bool warned = false; |
| 505 |
warning(loc, "%s `%s` is a %s", kind, ident.toChars(), reason); |
| 506 |
|
| 507 |
if (!warned) |
| 508 |
{ |
| 509 |
warningSupplemental(loc, "The generated C++ header will contain " ~ |
| 510 |
"identifiers that are keywords in C++"); |
| 511 |
warned = true; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
if (global.params.useWarnings != DiagnosticReporting.off || canFix) |
| 516 |
{ |
| 517 |
// Warn about identifiers that are keywords in C++. |
| 518 |
if (auto kc = keywordClass(ident)) |
| 519 |
warnCxxCompat(kc); |
| 520 |
} |
| 521 |
buf.writestring(ident.toString()); |
| 522 |
if (needsFix) |
| 523 |
buf.writeByte('_'); |
| 524 |
} |
| 525 |
|
| 526 |
/// Checks whether `t` is a type that can be exported to C++ |
| 527 |
private bool isSupportedType(AST.Type t) |
| 528 |
{ |
| 529 |
if (!t) |
| 530 |
{ |
| 531 |
assert(tdparent); |
| 532 |
return true; |
| 533 |
} |
| 534 |
|
| 535 |
switch (t.ty) |
| 536 |
{ |
| 537 |
// Nested types |
| 538 |
case AST.Tarray: |
| 539 |
case AST.Tsarray: |
| 540 |
case AST.Tpointer: |
| 541 |
case AST.Treference: |
| 542 |
case AST.Tdelegate: |
| 543 |
return isSupportedType((cast(AST.TypeNext) t).next); |
| 544 |
|
| 545 |
// Function pointers |
| 546 |
case AST.Tfunction: |
| 547 |
{ |
| 548 |
auto tf = cast(AST.TypeFunction) t; |
| 549 |
if (!isSupportedType(tf.next)) |
| 550 |
return false; |
| 551 |
foreach (_, param; tf.parameterList) |
| 552 |
{ |
| 553 |
if (!isSupportedType(param.type)) |
| 554 |
return false; |
| 555 |
} |
| 556 |
return true; |
| 557 |
} |
| 558 |
|
| 559 |
// Noreturn has a different mangling |
| 560 |
case AST.Tnoreturn: |
| 561 |
|
| 562 |
// _Imaginary is C only. |
| 563 |
case AST.Timaginary32: |
| 564 |
case AST.Timaginary64: |
| 565 |
case AST.Timaginary80: |
| 566 |
return false; |
| 567 |
default: |
| 568 |
return true; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
override void visit(AST.Dsymbol s) |
| 573 |
{ |
| 574 |
debug (Debug_DtoH) |
| 575 |
{ |
| 576 |
mixin(traceVisit!s); |
| 577 |
import dmd.asttypename; |
| 578 |
printf("[AST.Dsymbol enter] %s\n", s.astTypeName().ptr); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
override void visit(AST.Import i) |
| 583 |
{ |
| 584 |
debug (Debug_DtoH) mixin(traceVisit!i); |
| 585 |
|
| 586 |
/// Writes `using <alias_> = <sym.ident>` into `buf` |
| 587 |
const(char*) writeImport(AST.Dsymbol sym, const Identifier alias_) |
| 588 |
{ |
| 589 |
/// `using` was introduced in C++ 11 and only works for types... |
| 590 |
if (global.params.cplusplus < CppStdRevision.cpp11) |
| 591 |
return "requires C++11"; |
| 592 |
|
| 593 |
if (auto ad = sym.isAliasDeclaration()) |
| 594 |
{ |
| 595 |
sym = ad.toAlias(); |
| 596 |
ad = sym.isAliasDeclaration(); |
| 597 |
|
| 598 |
// Might be an alias to a basic type |
| 599 |
if (ad && !ad.aliassym && ad.type) |
| 600 |
goto Emit; |
| 601 |
} |
| 602 |
|
| 603 |
// Restricted to types and other aliases |
| 604 |
if (!sym.isScopeDsymbol() && !sym.isAggregateDeclaration()) |
| 605 |
return "only supports types"; |
| 606 |
|
| 607 |
// Write `using <alias_> = `<sym>` |
| 608 |
Emit: |
| 609 |
buf.writestring("using "); |
| 610 |
writeIdentifier(alias_, i.loc, "renamed import"); |
| 611 |
buf.writestring(" = "); |
| 612 |
// Start at module scope to avoid collisions with local symbols |
| 613 |
if (this.context.adparent) |
| 614 |
buf.writestring("::"); |
| 615 |
buf.writestring(sym.ident.toString()); |
| 616 |
writeDeclEnd(); |
| 617 |
return null; |
| 618 |
} |
| 619 |
|
| 620 |
// Only missing without semantic analysis |
| 621 |
// FIXME: Templates need work due to missing parent & imported module |
| 622 |
if (!i.parent) |
| 623 |
{ |
| 624 |
assert(tdparent); |
| 625 |
ignored("`%s` because it's inside of a template declaration", i.toChars()); |
| 626 |
return; |
| 627 |
} |
| 628 |
|
| 629 |
// Non-public imports don't create new symbols, include as needed |
| 630 |
if (i.visibility.kind < AST.Visibility.Kind.public_) |
| 631 |
return; |
| 632 |
|
| 633 |
// Symbols from static imports should be emitted inline |
| 634 |
if (i.isstatic) |
| 635 |
return; |
| 636 |
|
| 637 |
const isLocal = !i.parent.isModule(); |
| 638 |
|
| 639 |
// Need module for symbol lookup |
| 640 |
assert(i.mod); |
| 641 |
|
| 642 |
// Emit an alias for each public module member |
| 643 |
if (isLocal && i.names.length == 0) |
| 644 |
{ |
| 645 |
assert(i.mod.symtab); |
| 646 |
|
| 647 |
// Sort alphabetically s.t. slight changes in semantic don't cause |
| 648 |
// massive changes in the order of declarations |
| 649 |
AST.Dsymbols entries; |
| 650 |
entries.reserve(i.mod.symtab.length); |
| 651 |
|
| 652 |
foreach (entry; i.mod.symtab.tab.asRange) |
| 653 |
{ |
| 654 |
// Skip anonymous / invisible members |
| 655 |
import dmd.access : symbolIsVisible; |
| 656 |
if (!entry.key.isAnonymous() && symbolIsVisible(i, entry.value)) |
| 657 |
entries.push(entry.value); |
| 658 |
} |
| 659 |
|
| 660 |
// Seperate function because of a spurious dual-context deprecation |
| 661 |
static int compare(const AST.Dsymbol* a, const AST.Dsymbol* b) |
| 662 |
{ |
| 663 |
return strcmp(a.ident.toChars(), b.ident.toChars()); |
| 664 |
} |
| 665 |
entries.sort!compare(); |
| 666 |
|
| 667 |
foreach (sym; entries) |
| 668 |
{ |
| 669 |
includeSymbol(sym); |
| 670 |
if (auto err = writeImport(sym, sym.ident)) |
| 671 |
ignored("public import for `%s` because `using` %s", sym.ident.toChars(), err); |
| 672 |
} |
| 673 |
return; |
| 674 |
} |
| 675 |
|
| 676 |
// Include all public imports and emit using declarations for each alias |
| 677 |
foreach (const idx, name; i.names) |
| 678 |
{ |
| 679 |
// Search the imported symbol |
| 680 |
auto sym = i.mod.search(Loc.initial, name); |
| 681 |
assert(sym); // Missing imports should error during semantic |
| 682 |
|
| 683 |
includeSymbol(sym); |
| 684 |
|
| 685 |
// Detect the assigned name for renamed import |
| 686 |
auto alias_ = i.aliases[idx]; |
| 687 |
if (!alias_) |
| 688 |
continue; |
| 689 |
|
| 690 |
if (auto err = writeImport(sym, alias_)) |
| 691 |
ignored("renamed import `%s = %s` because `using` %s", alias_.toChars(), name.toChars(), err); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
override void visit(AST.AttribDeclaration pd) |
| 696 |
{ |
| 697 |
debug (Debug_DtoH) mixin(traceVisit!pd); |
| 698 |
|
| 699 |
Dsymbols* decl = pd.include(null); |
| 700 |
if (!decl) |
| 701 |
return; |
| 702 |
|
| 703 |
foreach (s; *decl) |
| 704 |
{ |
| 705 |
if (adparent || s.visible().kind >= AST.Visibility.Kind.public_) |
| 706 |
s.accept(this); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
override void visit(AST.StorageClassDeclaration scd) |
| 711 |
{ |
| 712 |
debug (Debug_DtoH) mixin(traceVisit!scd); |
| 713 |
|
| 714 |
const stcStash = this.storageClass; |
| 715 |
this.storageClass |= scd.stc; |
| 716 |
visit(cast(AST.AttribDeclaration) scd); |
| 717 |
this.storageClass = stcStash; |
| 718 |
} |
| 719 |
|
| 720 |
override void visit(AST.LinkDeclaration ld) |
| 721 |
{ |
| 722 |
debug (Debug_DtoH) mixin(traceVisit!ld); |
| 723 |
|
| 724 |
auto save = linkage; |
| 725 |
linkage = ld.linkage; |
| 726 |
visit(cast(AST.AttribDeclaration)ld); |
| 727 |
linkage = save; |
| 728 |
} |
| 729 |
|
| 730 |
override void visit(AST.CPPMangleDeclaration md) |
| 731 |
{ |
| 732 |
debug (Debug_DtoH) mixin(traceVisit!md); |
| 733 |
|
| 734 |
const oldLinkage = this.linkage; |
| 735 |
this.linkage = LINK.cpp; |
| 736 |
visit(cast(AST.AttribDeclaration) md); |
| 737 |
this.linkage = oldLinkage; |
| 738 |
} |
| 739 |
|
| 740 |
override void visit(AST.Module m) |
| 741 |
{ |
| 742 |
debug (Debug_DtoH) mixin(traceVisit!m); |
| 743 |
|
| 744 |
foreach (s; *m.members) |
| 745 |
{ |
| 746 |
if (s.visible().kind < AST.Visibility.Kind.public_) |
| 747 |
continue; |
| 748 |
s.accept(this); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
override void visit(AST.FuncDeclaration fd) |
| 753 |
{ |
| 754 |
debug (Debug_DtoH) mixin(traceVisit!fd); |
| 755 |
|
| 756 |
if (cast(void*)fd in visited) |
| 757 |
return; |
| 758 |
// printf("FuncDeclaration %s %s\n", fd.toPrettyChars(), fd.type.toChars()); |
| 759 |
visited[cast(void*)fd] = true; |
| 760 |
|
| 761 |
// silently ignore non-user-defined destructors |
| 762 |
if (fd.isGenerated && fd.isDtorDeclaration()) |
| 763 |
return; |
| 764 |
|
| 765 |
// Note that tf might be null for templated (member) functions |
| 766 |
auto tf = cast(AST.TypeFunction)fd.type; |
| 767 |
if ((tf && (tf.linkage != LINK.c || adparent) && tf.linkage != LINK.cpp && tf.linkage != LINK.windows) || (!tf && fd.isPostBlitDeclaration())) |
| 768 |
{ |
| 769 |
ignored("function %s because of linkage", fd.toPrettyChars()); |
| 770 |
return checkFunctionNeedsPlaceholder(fd); |
| 771 |
} |
| 772 |
if (fd.mangleOverride && tf && tf.linkage != LINK.c) |
| 773 |
{ |
| 774 |
ignored("function %s because C++ doesn't support explicit mangling", fd.toPrettyChars()); |
| 775 |
return checkFunctionNeedsPlaceholder(fd); |
| 776 |
} |
| 777 |
if (!adparent && !fd.fbody) |
| 778 |
{ |
| 779 |
ignored("function %s because it is extern", fd.toPrettyChars()); |
| 780 |
return; |
| 781 |
} |
| 782 |
if (fd.visibility.kind == AST.Visibility.Kind.none || fd.visibility.kind == AST.Visibility.Kind.private_) |
| 783 |
{ |
| 784 |
ignored("function %s because it is private", fd.toPrettyChars()); |
| 785 |
return; |
| 786 |
} |
| 787 |
if (tf && !isSupportedType(tf.next)) |
| 788 |
{ |
| 789 |
ignored("function %s because its return type cannot be mapped to C++", fd.toPrettyChars()); |
| 790 |
return checkFunctionNeedsPlaceholder(fd); |
| 791 |
} |
| 792 |
if (tf) foreach (i, fparam; tf.parameterList) |
| 793 |
{ |
| 794 |
if (!isSupportedType(fparam.type)) |
| 795 |
{ |
| 796 |
ignored("function %s because one of its parameters has type `%s` which cannot be mapped to C++", |
| 797 |
fd.toPrettyChars(), fparam.type.toChars()); |
| 798 |
return checkFunctionNeedsPlaceholder(fd); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
if (tf && tf.next) |
| 803 |
{ |
| 804 |
// Ensure return type is declared before a function that returns that is declared. |
| 805 |
if (auto sty = tf.next.isTypeStruct()) |
| 806 |
ensureDeclared(sty.sym); |
| 807 |
//else if (auto cty = tf.next.isTypeClass()) |
| 808 |
// includeSymbol(cty.sym); // classes are returned by pointer only need to forward declare |
| 809 |
//else if (auto ety = tf.next.isTypeEnum()) |
| 810 |
// ensureDeclared(ety.sym); |
| 811 |
} |
| 812 |
|
| 813 |
writeProtection(fd.visibility.kind); |
| 814 |
|
| 815 |
if (fd._linkage == LINK.system) |
| 816 |
{ |
| 817 |
hasExternSystem = true; |
| 818 |
buf.writestring("EXTERN_SYSTEM_BEFORE "); |
| 819 |
} |
| 820 |
else if (tf && tf.linkage == LINK.c) |
| 821 |
buf.writestring("extern \"C\" "); |
| 822 |
else if (tf && tf.linkage == LINK.windows) |
| 823 |
{ |
| 824 |
// __stdcall is printed after return type |
| 825 |
} |
| 826 |
else if (!adparent) |
| 827 |
buf.writestring("extern "); |
| 828 |
if (adparent && fd.isStatic()) |
| 829 |
buf.writestring("static "); |
| 830 |
else if (adparent && ( |
| 831 |
// Virtual functions in non-templated classes |
| 832 |
(fd.vtblIndex != -1 && !fd.isOverride()) || |
| 833 |
|
| 834 |
// Virtual functions in templated classes (fd.vtblIndex still -1) |
| 835 |
(tdparent && adparent.isClassDeclaration() && !(this.storageClass & AST.STC.final_ || fd.isFinal)))) |
| 836 |
buf.writestring("virtual "); |
| 837 |
|
| 838 |
debug (Debug_DtoH_Checks) |
| 839 |
if (adparent && !tdparent) |
| 840 |
{ |
| 841 |
auto s = adparent.search(Loc.initial, fd.ident); |
| 842 |
auto cd = adparent.isClassDeclaration(); |
| 843 |
|
| 844 |
if (!(adparent.storage_class & AST.STC.abstract_) && |
| 845 |
!(cd && cd.isAbstract()) && |
| 846 |
s is fd && !fd.overnext) |
| 847 |
{ |
| 848 |
const cn = adparent.ident.toChars(); |
| 849 |
const fn = fd.ident.toChars(); |
| 850 |
const vi = fd.vtblIndex; |
| 851 |
|
| 852 |
checkbuf.printf("assert(getSlotNumber <%s>(0, &%s::%s) == %d);", |
| 853 |
cn, cn, fn, vi); |
| 854 |
checkbuf.writenl(); |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
if (adparent && fd.isDisabled && global.params.cplusplus < CppStdRevision.cpp11) |
| 859 |
writeProtection(AST.Visibility.Kind.private_); |
| 860 |
funcToBuffer(tf, fd); |
| 861 |
if (adparent) |
| 862 |
{ |
| 863 |
if (tf && (tf.isConst() || tf.isImmutable())) |
| 864 |
buf.writestring(" const"); |
| 865 |
if (global.params.cplusplus >= CppStdRevision.cpp11) |
| 866 |
{ |
| 867 |
if (fd.vtblIndex != -1 && !(adparent.storage_class & AST.STC.final_) && fd.isFinalFunc()) |
| 868 |
buf.writestring(" final"); |
| 869 |
if (fd.isOverride()) |
| 870 |
buf.writestring(" override"); |
| 871 |
} |
| 872 |
if (fd.isAbstract()) |
| 873 |
buf.writestring(" = 0"); |
| 874 |
else if (global.params.cplusplus >= CppStdRevision.cpp11 && fd.isDisabled()) |
| 875 |
buf.writestring(" = delete"); |
| 876 |
} |
| 877 |
buf.writestringln(";"); |
| 878 |
if (adparent && fd.isDisabled && global.params.cplusplus < CppStdRevision.cpp11) |
| 879 |
writeProtection(AST.Visibility.Kind.public_); |
| 880 |
|
| 881 |
if (!adparent) |
| 882 |
buf.writenl(); |
| 883 |
|
| 884 |
} |
| 885 |
|
| 886 |
/++ |
| 887 |
+ Checks whether `fd` is a function that requires a dummy declaration |
| 888 |
+ instead of simply emitting the declaration (because it would cause |
| 889 |
+ ABI / behaviour issues). This includes: |
| 890 |
+ |
| 891 |
+ - virtual functions to ensure proper vtable layout |
| 892 |
+ - destructors that would break RAII |
| 893 |
+/ |
| 894 |
private void checkFunctionNeedsPlaceholder(AST.FuncDeclaration fd) |
| 895 |
{ |
| 896 |
// Omit redundant declarations - the slot was already |
| 897 |
// reserved in the base class |
| 898 |
if (fd.isVirtual() && fd.isIntroducing()) |
| 899 |
{ |
| 900 |
// Hide placeholders because they are not ABI compatible |
| 901 |
writeProtection(AST.Visibility.Kind.private_); |
| 902 |
|
| 903 |
__gshared int counter; // Ensure unique names in all cases |
| 904 |
buf.printf("virtual void __vtable_slot_%u();", counter++); |
| 905 |
buf.writenl(); |
| 906 |
} |
| 907 |
else if (fd.isDtorDeclaration()) |
| 908 |
{ |
| 909 |
// Create inaccessible dtor to prevent code from keeping instances that |
| 910 |
// need to be destroyed on the C++ side (but cannot call the dtor) |
| 911 |
writeProtection(AST.Visibility.Kind.private_); |
| 912 |
buf.writeByte('~'); |
| 913 |
buf.writestring(adparent.ident.toString()); |
| 914 |
buf.writestringln("();"); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
override void visit(AST.UnitTestDeclaration utd) |
| 919 |
{ |
| 920 |
debug (Debug_DtoH) mixin(traceVisit!utd); |
| 921 |
} |
| 922 |
|
| 923 |
override void visit(AST.VarDeclaration vd) |
| 924 |
{ |
| 925 |
debug (Debug_DtoH) mixin(traceVisit!vd); |
| 926 |
|
| 927 |
if (!shouldEmitAndMarkVisited(vd)) |
| 928 |
return; |
| 929 |
|
| 930 |
// Tuple field are expanded into multiple VarDeclarations |
| 931 |
// (we'll visit them later) |
| 932 |
if (vd.type && vd.type.isTypeTuple()) |
| 933 |
{ |
| 934 |
assert(vd.aliasTuple); |
| 935 |
vd.toAlias().accept(this); |
| 936 |
return; |
| 937 |
} |
| 938 |
|
| 939 |
if (vd.originalType && vd.type == AST.Type.tsize_t) |
| 940 |
origType = vd.originalType; |
| 941 |
scope(exit) origType = null; |
| 942 |
|
| 943 |
if (!vd.alignment.isDefault() && !vd.alignment.isUnknown()) |
| 944 |
{ |
| 945 |
buf.printf("// Ignoring var %s alignment %d", vd.toChars(), vd.alignment.get()); |
| 946 |
buf.writenl(); |
| 947 |
} |
| 948 |
|
| 949 |
// Determine the variable type which might be missing inside of |
| 950 |
// template declarations. Infer the type from the initializer then |
| 951 |
AST.Type type = vd.type; |
| 952 |
if (!type) |
| 953 |
{ |
| 954 |
assert(tdparent); |
| 955 |
|
| 956 |
// Just a precaution, implicit type without initializer should be rejected |
| 957 |
if (!vd._init) |
| 958 |
return; |
| 959 |
|
| 960 |
if (auto ei = vd._init.isExpInitializer()) |
| 961 |
type = ei.exp.type; |
| 962 |
|
| 963 |
// Can happen if the expression needs further semantic |
| 964 |
if (!type) |
| 965 |
{ |
| 966 |
ignored("%s because the type could not be determined", vd.toPrettyChars()); |
| 967 |
return; |
| 968 |
} |
| 969 |
|
| 970 |
// Apply const/immutable to the inferred type |
| 971 |
if (vd.storage_class & (AST.STC.const_ | AST.STC.immutable_)) |
| 972 |
type = type.constOf(); |
| 973 |
} |
| 974 |
|
| 975 |
if (vd.storage_class & AST.STC.manifest) |
| 976 |
{ |
| 977 |
EnumKind kind = getEnumKind(type); |
| 978 |
|
| 979 |
if (vd.visibility.kind == AST.Visibility.Kind.none || vd.visibility.kind == AST.Visibility.Kind.private_) |
| 980 |
{ |
| 981 |
ignored("enum `%s` because it is `%s`.", vd.toPrettyChars(), AST.visibilityToChars(vd.visibility.kind)); |
| 982 |
return; |
| 983 |
} |
| 984 |
|
| 985 |
writeProtection(vd.visibility.kind); |
| 986 |
|
| 987 |
final switch (kind) |
| 988 |
{ |
| 989 |
case EnumKind.Int, EnumKind.Numeric: |
| 990 |
// 'enum : type' is only available from C++-11 onwards. |
| 991 |
if (global.params.cplusplus < CppStdRevision.cpp11) |
| 992 |
goto case; |
| 993 |
buf.writestring("enum : "); |
| 994 |
determineEnumType(type).accept(this); |
| 995 |
buf.writestring(" { "); |
| 996 |
writeIdentifier(vd, true); |
| 997 |
buf.writestring(" = "); |
| 998 |
auto ie = AST.initializerToExpression(vd._init).isIntegerExp(); |
| 999 |
visitInteger(ie.toInteger(), type); |
| 1000 |
buf.writestring(" };"); |
| 1001 |
break; |
| 1002 |
|
| 1003 |
case EnumKind.String, EnumKind.Enum: |
| 1004 |
buf.writestring("static "); |
| 1005 |
auto target = determineEnumType(type); |
| 1006 |
target.accept(this); |
| 1007 |
buf.writestring(" const "); |
| 1008 |
writeIdentifier(vd, true); |
| 1009 |
buf.writestring(" = "); |
| 1010 |
auto e = AST.initializerToExpression(vd._init); |
| 1011 |
printExpressionFor(target, e); |
| 1012 |
buf.writestring(";"); |
| 1013 |
break; |
| 1014 |
|
| 1015 |
case EnumKind.Other: |
| 1016 |
ignored("enum `%s` because type `%s` is currently not supported for enum constants.", vd.toPrettyChars(), type.toChars()); |
| 1017 |
return; |
| 1018 |
} |
| 1019 |
buf.writenl(); |
| 1020 |
buf.writenl(); |
| 1021 |
return; |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (vd.storage_class & (AST.STC.static_ | AST.STC.extern_ | AST.STC.gshared) || |
| 1025 |
vd.parent && vd.parent.isModule()) |
| 1026 |
{ |
| 1027 |
const vdLinkage = vd.resolvedLinkage(); |
| 1028 |
if (vdLinkage != LINK.c && vdLinkage != LINK.cpp && !(tdparent && (this.linkage == LINK.c || this.linkage == LINK.cpp))) |
| 1029 |
{ |
| 1030 |
ignored("variable %s because of linkage", vd.toPrettyChars()); |
| 1031 |
return; |
| 1032 |
} |
| 1033 |
if (vd.mangleOverride && vdLinkage != LINK.c) |
| 1034 |
{ |
| 1035 |
ignored("variable %s because C++ doesn't support explicit mangling", vd.toPrettyChars()); |
| 1036 |
return; |
| 1037 |
} |
| 1038 |
if (!isSupportedType(type)) |
| 1039 |
{ |
| 1040 |
ignored("variable %s because its type cannot be mapped to C++", vd.toPrettyChars()); |
| 1041 |
return; |
| 1042 |
} |
| 1043 |
if (auto kc = keywordClass(vd.ident)) |
| 1044 |
{ |
| 1045 |
ignored("variable %s because its name is a %s", vd.toPrettyChars(), kc); |
| 1046 |
return; |
| 1047 |
} |
| 1048 |
writeProtection(vd.visibility.kind); |
| 1049 |
if (vdLinkage == LINK.c) |
| 1050 |
buf.writestring("extern \"C\" "); |
| 1051 |
else if (!adparent) |
| 1052 |
buf.writestring("extern "); |
| 1053 |
if (adparent) |
| 1054 |
buf.writestring("static "); |
| 1055 |
typeToBuffer(type, vd); |
| 1056 |
writeDeclEnd(); |
| 1057 |
return; |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (adparent) |
| 1061 |
{ |
| 1062 |
writeProtection(vd.visibility.kind); |
| 1063 |
typeToBuffer(type, vd, true); |
| 1064 |
buf.writestringln(";"); |
| 1065 |
|
| 1066 |
debug (Debug_DtoH_Checks) |
| 1067 |
{ |
| 1068 |
checkbuf.level++; |
| 1069 |
const pn = adparent.ident.toChars(); |
| 1070 |
const vn = vd.ident.toChars(); |
| 1071 |
const vo = vd.offset; |
| 1072 |
checkbuf.printf("assert(offsetof(%s, %s) == %d);", |
| 1073 |
pn, vn, vo); |
| 1074 |
checkbuf.writenl(); |
| 1075 |
checkbuf.level--; |
| 1076 |
} |
| 1077 |
return; |
| 1078 |
} |
| 1079 |
|
| 1080 |
visit(cast(AST.Dsymbol)vd); |
| 1081 |
} |
| 1082 |
|
| 1083 |
override void visit(AST.TypeInfoDeclaration tid) |
| 1084 |
{ |
| 1085 |
debug (Debug_DtoH) mixin(traceVisit!tid); |
| 1086 |
} |
| 1087 |
|
| 1088 |
override void visit(AST.AliasDeclaration ad) |
| 1089 |
{ |
| 1090 |
debug (Debug_DtoH) mixin(traceVisit!ad); |
| 1091 |
|
| 1092 |
// Declared in object.d but already included in `#include`s |
| 1093 |
if (ad.ident == Id._size_t || ad.ident == Id._ptrdiff_t) |
| 1094 |
return; |
| 1095 |
|
| 1096 |
if (!shouldEmitAndMarkVisited(ad)) |
| 1097 |
return; |
| 1098 |
|
| 1099 |
writeProtection(ad.visibility.kind); |
| 1100 |
|
| 1101 |
if (auto t = ad.type) |
| 1102 |
{ |
| 1103 |
if (t.ty == AST.Tdelegate || t.ty == AST.Tident) |
| 1104 |
{ |
| 1105 |
visit(cast(AST.Dsymbol)ad); |
| 1106 |
return; |
| 1107 |
} |
| 1108 |
|
| 1109 |
// for function pointers we need to original type |
| 1110 |
if (ad.originalType && ad.type.ty == AST.Tpointer && |
| 1111 |
(cast(AST.TypePointer)t).nextOf.ty == AST.Tfunction) |
| 1112 |
{ |
| 1113 |
origType = ad.originalType; |
| 1114 |
} |
| 1115 |
scope(exit) origType = null; |
| 1116 |
|
| 1117 |
buf.writestring("typedef "); |
| 1118 |
typeToBuffer(origType !is null ? origType : t, ad); |
| 1119 |
writeDeclEnd(); |
| 1120 |
return; |
| 1121 |
} |
| 1122 |
if (!ad.aliassym) |
| 1123 |
{ |
| 1124 |
assert(0); |
| 1125 |
} |
| 1126 |
if (auto ti = ad.aliassym.isTemplateInstance()) |
| 1127 |
{ |
| 1128 |
visitTi(ti); |
| 1129 |
return; |
| 1130 |
} |
| 1131 |
if (auto sd = ad.aliassym.isStructDeclaration()) |
| 1132 |
{ |
| 1133 |
buf.writestring("typedef "); |
| 1134 |
sd.type.accept(this); |
| 1135 |
buf.writestring(" "); |
| 1136 |
writeIdentifier(ad); |
| 1137 |
writeDeclEnd(); |
| 1138 |
return; |
| 1139 |
} |
| 1140 |
else if (auto td = ad.aliassym.isTemplateDeclaration()) |
| 1141 |
{ |
| 1142 |
if (global.params.cplusplus < CppStdRevision.cpp11) |
| 1143 |
{ |
| 1144 |
ignored("%s because `using` declarations require C++ 11", ad.toPrettyChars()); |
| 1145 |
return; |
| 1146 |
} |
| 1147 |
|
| 1148 |
printTemplateParams(td); |
| 1149 |
buf.writestring("using "); |
| 1150 |
writeIdentifier(ad); |
| 1151 |
buf.writestring(" = "); |
| 1152 |
writeFullName(td); |
| 1153 |
buf.writeByte('<'); |
| 1154 |
|
| 1155 |
foreach (const idx, const p; *td.parameters) |
| 1156 |
{ |
| 1157 |
if (idx) |
| 1158 |
buf.writestring(", "); |
| 1159 |
writeIdentifier(p.ident, p.loc, "parameter", true); |
| 1160 |
} |
| 1161 |
buf.writestringln(">;"); |
| 1162 |
return; |
| 1163 |
} |
| 1164 |
|
| 1165 |
auto fd = ad.aliassym.isFuncDeclaration(); |
| 1166 |
|
| 1167 |
if (fd && (fd.isGenerated() || fd.isDtorDeclaration())) |
| 1168 |
{ |
| 1169 |
// Ignore. It's taken care of while visiting FuncDeclaration |
| 1170 |
return; |
| 1171 |
} |
| 1172 |
|
| 1173 |
// Recognize member function aliases, e.g. alias visit = Parent.visit; |
| 1174 |
if (adparent && fd) |
| 1175 |
{ |
| 1176 |
auto pd = fd.isMember(); |
| 1177 |
if (!pd) |
| 1178 |
{ |
| 1179 |
ignored("%s because free functions cannot be aliased in C++", ad.toPrettyChars()); |
| 1180 |
} |
| 1181 |
else if (global.params.cplusplus < CppStdRevision.cpp11) |
| 1182 |
{ |
| 1183 |
ignored("%s because `using` declarations require C++ 11", ad.toPrettyChars()); |
| 1184 |
} |
| 1185 |
else if (ad.ident != fd.ident) |
| 1186 |
{ |
| 1187 |
ignored("%s because `using` cannot rename functions in aggregates", ad.toPrettyChars()); |
| 1188 |
} |
| 1189 |
else if (fd.toAliasFunc().parent.isTemplateMixin()) |
| 1190 |
{ |
| 1191 |
// Member's of template mixins are directly emitted into the aggregate |
| 1192 |
} |
| 1193 |
else |
| 1194 |
{ |
| 1195 |
buf.writestring("using "); |
| 1196 |
|
| 1197 |
// Print prefix of the base class if this function originates from a superclass |
| 1198 |
// because alias might be resolved through multiple classes, e.g. |
| 1199 |
// e.g. for alias visit = typeof(super).visit in the visitors |
| 1200 |
if (!fd.isIntroducing()) |
| 1201 |
printPrefix(ad.toParent().isClassDeclaration().baseClass); |
| 1202 |
else |
| 1203 |
printPrefix(pd); |
| 1204 |
|
| 1205 |
buf.writestring(fd.ident.toChars()); |
| 1206 |
buf.writestringln(";"); |
| 1207 |
} |
| 1208 |
return; |
| 1209 |
} |
| 1210 |
|
| 1211 |
ignored("%s %s", ad.aliassym.kind(), ad.aliassym.toPrettyChars()); |
| 1212 |
} |
| 1213 |
|
| 1214 |
override void visit(AST.Nspace ns) |
| 1215 |
{ |
| 1216 |
debug (Debug_DtoH) mixin(traceVisit!ns); |
| 1217 |
handleNspace(ns, ns.members); |
| 1218 |
} |
| 1219 |
|
| 1220 |
override void visit(AST.CPPNamespaceDeclaration ns) |
| 1221 |
{ |
| 1222 |
debug (Debug_DtoH) mixin(traceVisit!ns); |
| 1223 |
handleNspace(ns, ns.decl); |
| 1224 |
} |
| 1225 |
|
| 1226 |
/// Writes the namespace declaration and visits all members |
| 1227 |
private void handleNspace(AST.Dsymbol namespace, Dsymbols* members) |
| 1228 |
{ |
| 1229 |
buf.writestring("namespace "); |
| 1230 |
writeIdentifier(namespace); |
| 1231 |
buf.writenl(); |
| 1232 |
buf.writestring("{"); |
| 1233 |
buf.writenl(); |
| 1234 |
buf.level++; |
| 1235 |
foreach(decl;(*members)) |
| 1236 |
{ |
| 1237 |
decl.accept(this); |
| 1238 |
} |
| 1239 |
buf.level--; |
| 1240 |
buf.writestring("}"); |
| 1241 |
buf.writenl(); |
| 1242 |
} |
| 1243 |
|
| 1244 |
override void visit(AST.AnonDeclaration ad) |
| 1245 |
{ |
| 1246 |
debug (Debug_DtoH) mixin(traceVisit!ad); |
| 1247 |
|
| 1248 |
const anonStash = inAnonymousDecl; |
| 1249 |
inAnonymousDecl = true; |
| 1250 |
scope (exit) inAnonymousDecl = anonStash; |
| 1251 |
|
| 1252 |
buf.writestringln(ad.isunion ? "union" : "struct"); |
| 1253 |
buf.writestringln("{"); |
| 1254 |
buf.level++; |
| 1255 |
foreach (s; *ad.decl) |
| 1256 |
{ |
| 1257 |
s.accept(this); |
| 1258 |
} |
| 1259 |
buf.level--; |
| 1260 |
buf.writestringln("};"); |
| 1261 |
} |
| 1262 |
|
| 1263 |
private bool memberField(AST.VarDeclaration vd) @safe |
| 1264 |
{ |
| 1265 |
if (!vd.type || !vd.type.deco || !vd.ident) |
| 1266 |
return false; |
| 1267 |
if (!vd.isField()) |
| 1268 |
return false; |
| 1269 |
if (vd.type.ty == AST.Tfunction) |
| 1270 |
return false; |
| 1271 |
if (vd.type.ty == AST.Tsarray) |
| 1272 |
return false; |
| 1273 |
return true; |
| 1274 |
} |
| 1275 |
|
| 1276 |
override void visit(AST.StructDeclaration sd) |
| 1277 |
{ |
| 1278 |
debug (Debug_DtoH) mixin(traceVisit!sd); |
| 1279 |
|
| 1280 |
if (!shouldEmitAndMarkVisited(sd)) |
| 1281 |
return; |
| 1282 |
|
| 1283 |
const ignoredStash = this.ignoredCounter; |
| 1284 |
scope (exit) this.ignoredCounter = ignoredStash; |
| 1285 |
|
| 1286 |
pushAlignToBuffer(sd.alignment); |
| 1287 |
|
| 1288 |
writeProtection(sd.visibility.kind); |
| 1289 |
|
| 1290 |
const structAsClass = sd.cppmangle == CPPMANGLE.asClass; |
| 1291 |
if (sd.isUnionDeclaration()) |
| 1292 |
buf.writestring("union "); |
| 1293 |
else |
| 1294 |
buf.writestring(structAsClass ? "class " : "struct "); |
| 1295 |
|
| 1296 |
writeIdentifier(sd); |
| 1297 |
if (!sd.members) |
| 1298 |
{ |
| 1299 |
buf.writestringln(";"); |
| 1300 |
buf.writenl(); |
| 1301 |
return; |
| 1302 |
} |
| 1303 |
|
| 1304 |
// D structs are always final |
| 1305 |
if (!sd.isUnionDeclaration()) |
| 1306 |
buf.writestring(" final"); |
| 1307 |
|
| 1308 |
buf.writenl(); |
| 1309 |
buf.writestring("{"); |
| 1310 |
|
| 1311 |
const protStash = this.currentVisibility; |
| 1312 |
this.currentVisibility = structAsClass ? AST.Visibility.Kind.private_ : AST.Visibility.Kind.public_; |
| 1313 |
scope (exit) this.currentVisibility = protStash; |
| 1314 |
|
| 1315 |
buf.level++; |
| 1316 |
buf.writenl(); |
| 1317 |
auto save = adparent; |
| 1318 |
adparent = sd; |
| 1319 |
|
| 1320 |
foreach (m; *sd.members) |
| 1321 |
{ |
| 1322 |
m.accept(this); |
| 1323 |
} |
| 1324 |
// Generate default ctor |
| 1325 |
if (!sd.noDefaultCtor && !sd.isUnionDeclaration()) |
| 1326 |
{ |
| 1327 |
writeProtection(AST.Visibility.Kind.public_); |
| 1328 |
buf.printf("%s()", sd.ident.toChars()); |
| 1329 |
size_t varCount; |
| 1330 |
bool first = true; |
| 1331 |
buf.level++; |
| 1332 |
foreach (vd; sd.fields) |
| 1333 |
{ |
| 1334 |
if (!memberField(vd) || vd.overlapped) |
| 1335 |
continue; |
| 1336 |
varCount++; |
| 1337 |
|
| 1338 |
if (!vd._init && !vd.type.isTypeBasic() && !vd.type.isTypePointer && !vd.type.isTypeStruct && |
| 1339 |
!vd.type.isTypeClass && !vd.type.isTypeDArray && !vd.type.isTypeSArray) |
| 1340 |
{ |
| 1341 |
continue; |
| 1342 |
} |
| 1343 |
if (vd._init && vd._init.isVoidInitializer()) |
| 1344 |
continue; |
| 1345 |
|
| 1346 |
if (first) |
| 1347 |
{ |
| 1348 |
buf.writestringln(" :"); |
| 1349 |
first = false; |
| 1350 |
} |
| 1351 |
else |
| 1352 |
{ |
| 1353 |
buf.writestringln(","); |
| 1354 |
} |
| 1355 |
writeIdentifier(vd, true); |
| 1356 |
buf.writeByte('('); |
| 1357 |
|
| 1358 |
if (vd._init) |
| 1359 |
{ |
| 1360 |
auto e = AST.initializerToExpression(vd._init); |
| 1361 |
printExpressionFor(vd.type, e, true); |
| 1362 |
} |
| 1363 |
buf.printf(")"); |
| 1364 |
} |
| 1365 |
buf.level--; |
| 1366 |
buf.writenl(); |
| 1367 |
buf.writestringln("{"); |
| 1368 |
buf.writestringln("}"); |
| 1369 |
auto ctor = sd.ctor ? sd.ctor.isFuncDeclaration() : null; |
| 1370 |
if (varCount && (!ctor || ctor.storage_class & AST.STC.disable)) |
| 1371 |
{ |
| 1372 |
buf.printf("%s(", sd.ident.toChars()); |
| 1373 |
first = true; |
| 1374 |
foreach (vd; sd.fields) |
| 1375 |
{ |
| 1376 |
if (!memberField(vd) || vd.overlapped) |
| 1377 |
continue; |
| 1378 |
if (!first) |
| 1379 |
buf.writestring(", "); |
| 1380 |
assert(vd.type); |
| 1381 |
assert(vd.ident); |
| 1382 |
typeToBuffer(vd.type, vd, true); |
| 1383 |
// Don't print default value for first parameter to not clash |
| 1384 |
// with the default ctor defined above |
| 1385 |
if (!first) |
| 1386 |
{ |
| 1387 |
buf.writestring(" = "); |
| 1388 |
printExpressionFor(vd.type, findDefaultInitializer(vd)); |
| 1389 |
} |
| 1390 |
first = false; |
| 1391 |
} |
| 1392 |
buf.writestring(") :"); |
| 1393 |
buf.level++; |
| 1394 |
buf.writenl(); |
| 1395 |
|
| 1396 |
first = true; |
| 1397 |
foreach (vd; sd.fields) |
| 1398 |
{ |
| 1399 |
if (!memberField(vd) || vd.overlapped) |
| 1400 |
continue; |
| 1401 |
|
| 1402 |
if (first) |
| 1403 |
first = false; |
| 1404 |
else |
| 1405 |
buf.writestringln(","); |
| 1406 |
|
| 1407 |
writeIdentifier(vd, true); |
| 1408 |
buf.writeByte('('); |
| 1409 |
writeIdentifier(vd, true); |
| 1410 |
buf.writeByte(')'); |
| 1411 |
} |
| 1412 |
buf.writenl(); |
| 1413 |
buf.writestringln("{}"); |
| 1414 |
buf.level--; |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
buf.level--; |
| 1419 |
adparent = save; |
| 1420 |
buf.writestringln("};"); |
| 1421 |
|
| 1422 |
popAlignToBuffer(sd.alignment); |
| 1423 |
buf.writenl(); |
| 1424 |
|
| 1425 |
// Workaround because size triggers a forward-reference error |
| 1426 |
// for struct templates (the size is undetermined even if the |
| 1427 |
// size doesn't depend on the parameters) |
| 1428 |
debug (Debug_DtoH_Checks) |
| 1429 |
if (!tdparent) |
| 1430 |
{ |
| 1431 |
checkbuf.level++; |
| 1432 |
const sn = sd.ident.toChars(); |
| 1433 |
const sz = sd.size(Loc.initial); |
| 1434 |
checkbuf.printf("assert(sizeof(%s) == %llu);", sn, sz); |
| 1435 |
checkbuf.writenl(); |
| 1436 |
checkbuf.level--; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
/// Starts a custom alignment section using `#pragma pack` if |
| 1441 |
/// `alignment` specifies a custom alignment |
| 1442 |
private void pushAlignToBuffer(structalign_t alignment) |
| 1443 |
{ |
| 1444 |
// DMD ensures alignment is a power of two |
| 1445 |
//assert(alignment > 0 && ((alignment & (alignment - 1)) == 0), |
| 1446 |
// "Invalid alignment size"); |
| 1447 |
|
| 1448 |
// When no alignment is specified, `uint.max` is the default |
| 1449 |
// FIXME: alignment is 0 for structs templated members |
| 1450 |
if (alignment.isDefault() || (tdparent && alignment.isUnknown())) |
| 1451 |
{ |
| 1452 |
return; |
| 1453 |
} |
| 1454 |
|
| 1455 |
buf.printf("#pragma pack(push, %d)", alignment.get()); |
| 1456 |
buf.writenl(); |
| 1457 |
} |
| 1458 |
|
| 1459 |
/// Ends a custom alignment section using `#pragma pack` if |
| 1460 |
/// `alignment` specifies a custom alignment |
| 1461 |
private void popAlignToBuffer(structalign_t alignment) @safe |
| 1462 |
{ |
| 1463 |
if (alignment.isDefault() || (tdparent && alignment.isUnknown())) |
| 1464 |
return; |
| 1465 |
|
| 1466 |
buf.writestringln("#pragma pack(pop)"); |
| 1467 |
} |
| 1468 |
|
| 1469 |
override void visit(AST.ClassDeclaration cd) |
| 1470 |
{ |
| 1471 |
debug (Debug_DtoH) mixin(traceVisit!cd); |
| 1472 |
|
| 1473 |
if (cd.baseClass && shouldEmit(cd)) |
| 1474 |
includeSymbol(cd.baseClass); |
| 1475 |
|
| 1476 |
if (!shouldEmitAndMarkVisited(cd)) |
| 1477 |
return; |
| 1478 |
|
| 1479 |
writeProtection(cd.visibility.kind); |
| 1480 |
|
| 1481 |
const classAsStruct = cd.cppmangle == CPPMANGLE.asStruct; |
| 1482 |
buf.writestring(classAsStruct ? "struct " : "class "); |
| 1483 |
writeIdentifier(cd); |
| 1484 |
|
| 1485 |
if (cd.storage_class & AST.STC.final_ || (tdparent && this.storageClass & AST.STC.final_)) |
| 1486 |
buf.writestring(" final"); |
| 1487 |
|
| 1488 |
assert(cd.baseclasses); |
| 1489 |
|
| 1490 |
foreach (i, base; *cd.baseclasses) |
| 1491 |
{ |
| 1492 |
buf.writestring(i == 0 ? " : public " : ", public "); |
| 1493 |
|
| 1494 |
// Base classes/interfaces might depend on template parameters, |
| 1495 |
// e.g. class A(T) : B!T { ... } |
| 1496 |
if (base.sym is null) |
| 1497 |
{ |
| 1498 |
base.type.accept(this); |
| 1499 |
} |
| 1500 |
else |
| 1501 |
{ |
| 1502 |
writeFullName(base.sym); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
if (!cd.members) |
| 1507 |
{ |
| 1508 |
buf.writestring(";"); |
| 1509 |
buf.writenl(); |
| 1510 |
buf.writenl(); |
| 1511 |
return; |
| 1512 |
} |
| 1513 |
|
| 1514 |
buf.writenl(); |
| 1515 |
buf.writestringln("{"); |
| 1516 |
|
| 1517 |
const protStash = this.currentVisibility; |
| 1518 |
this.currentVisibility = classAsStruct ? AST.Visibility.Kind.public_ : AST.Visibility.Kind.private_; |
| 1519 |
scope (exit) this.currentVisibility = protStash; |
| 1520 |
|
| 1521 |
auto save = adparent; |
| 1522 |
adparent = cd; |
| 1523 |
buf.level++; |
| 1524 |
foreach (m; *cd.members) |
| 1525 |
{ |
| 1526 |
m.accept(this); |
| 1527 |
} |
| 1528 |
buf.level--; |
| 1529 |
adparent = save; |
| 1530 |
|
| 1531 |
buf.writestringln("};"); |
| 1532 |
buf.writenl(); |
| 1533 |
} |
| 1534 |
|
| 1535 |
override void visit(AST.EnumDeclaration ed) |
| 1536 |
{ |
| 1537 |
debug (Debug_DtoH) mixin(traceVisit!ed); |
| 1538 |
|
| 1539 |
if (!shouldEmitAndMarkVisited(ed)) |
| 1540 |
return; |
| 1541 |
|
| 1542 |
if (ed.isSpecial()) |
| 1543 |
{ |
| 1544 |
//ignored("%s because it is a special C++ type", ed.toPrettyChars()); |
| 1545 |
return; |
| 1546 |
} |
| 1547 |
|
| 1548 |
// we need to know a bunch of stuff about the enum... |
| 1549 |
bool isAnonymous = ed.ident is null; |
| 1550 |
const isOpaque = !ed.members; |
| 1551 |
AST.Type type = ed.memtype; |
| 1552 |
if (!type && !isOpaque) |
| 1553 |
{ |
| 1554 |
// check all keys have matching type |
| 1555 |
foreach (_m; *ed.members) |
| 1556 |
{ |
| 1557 |
auto m = _m.isEnumMember(); |
| 1558 |
if (!type) |
| 1559 |
type = m.type; |
| 1560 |
else if (m.type !is type) |
| 1561 |
{ |
| 1562 |
type = null; |
| 1563 |
break; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
} |
| 1567 |
EnumKind kind = getEnumKind(type); |
| 1568 |
|
| 1569 |
if (isOpaque) |
| 1570 |
{ |
| 1571 |
// Opaque enums were introduced in C++ 11 (workaround?) |
| 1572 |
if (global.params.cplusplus < CppStdRevision.cpp11) |
| 1573 |
{ |
| 1574 |
ignored("%s because opaque enums require C++ 11", ed.toPrettyChars()); |
| 1575 |
return; |
| 1576 |
} |
| 1577 |
// Opaque enum defaults to int but the type might not be set |
| 1578 |
else if (!type) |
| 1579 |
{ |
| 1580 |
kind = EnumKind.Int; |
| 1581 |
} |
| 1582 |
// Cannot apply namespace workaround for non-integral types |
| 1583 |
else if (kind != EnumKind.Int && kind != EnumKind.Numeric) |
| 1584 |
{ |
| 1585 |
ignored("enum %s because of its base type", ed.toPrettyChars()); |
| 1586 |
return; |
| 1587 |
} |
| 1588 |
} |
| 1589 |
|
| 1590 |
// determine if this is an enum, or just a group of manifest constants |
| 1591 |
bool manifestConstants = !isOpaque && (!type || (isAnonymous && kind == EnumKind.Other)); |
| 1592 |
assert(!manifestConstants || isAnonymous); |
| 1593 |
|
| 1594 |
writeProtection(ed.visibility.kind); |
| 1595 |
|
| 1596 |
// write the enum header |
| 1597 |
if (!manifestConstants) |
| 1598 |
{ |
| 1599 |
if (kind == EnumKind.Int || kind == EnumKind.Numeric) |
| 1600 |
{ |
| 1601 |
buf.writestring("enum"); |
| 1602 |
// D enums are strong enums, but there exists only a direct mapping |
| 1603 |
// with 'enum class' from C++-11 onwards. |
| 1604 |
if (global.params.cplusplus >= CppStdRevision.cpp11) |
| 1605 |
{ |
| 1606 |
if (!isAnonymous) |
| 1607 |
{ |
| 1608 |
buf.writestring(" class "); |
| 1609 |
writeIdentifier(ed); |
| 1610 |
} |
| 1611 |
if (kind == EnumKind.Numeric) |
| 1612 |
{ |
| 1613 |
buf.writestring(" : "); |
| 1614 |
determineEnumType(type).accept(this); |
| 1615 |
} |
| 1616 |
} |
| 1617 |
else if (!isAnonymous) |
| 1618 |
{ |
| 1619 |
buf.writeByte(' '); |
| 1620 |
writeIdentifier(ed); |
| 1621 |
} |
| 1622 |
} |
| 1623 |
else |
| 1624 |
{ |
| 1625 |
buf.writestring("namespace"); |
| 1626 |
if(!isAnonymous) |
| 1627 |
{ |
| 1628 |
buf.writeByte(' '); |
| 1629 |
writeIdentifier(ed); |
| 1630 |
} |
| 1631 |
} |
| 1632 |
// Opaque enums have no members, hence skip the body |
| 1633 |
if (isOpaque) |
| 1634 |
{ |
| 1635 |
buf.writestringln(";"); |
| 1636 |
return; |
| 1637 |
} |
| 1638 |
else |
| 1639 |
{ |
| 1640 |
buf.writenl(); |
| 1641 |
buf.writestringln("{"); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
// emit constant for each member |
| 1646 |
if (!manifestConstants) |
| 1647 |
buf.level++; |
| 1648 |
|
| 1649 |
foreach (_m; *ed.members) |
| 1650 |
{ |
| 1651 |
auto m = _m.isEnumMember(); |
| 1652 |
AST.Type memberType = type ? type : m.type; |
| 1653 |
const EnumKind memberKind = type ? kind : getEnumKind(memberType); |
| 1654 |
|
| 1655 |
if (!manifestConstants && (kind == EnumKind.Int || kind == EnumKind.Numeric)) |
| 1656 |
{ |
| 1657 |
// C++-98 compatible enums must use the typename as a prefix to avoid |
| 1658 |
// collisions with other identifiers in scope. For consistency with D, |
| 1659 |
// the enum member `Type.member` is emitted as `Type_member` in C++-98. |
| 1660 |
if (!isAnonymous && global.params.cplusplus < CppStdRevision.cpp11) |
| 1661 |
{ |
| 1662 |
writeIdentifier(ed); |
| 1663 |
buf.writeByte('_'); |
| 1664 |
} |
| 1665 |
writeIdentifier(m, true); |
| 1666 |
buf.writestring(" = "); |
| 1667 |
|
| 1668 |
auto ie = cast(AST.IntegerExp)m.value; |
| 1669 |
visitInteger(ie.toInteger(), memberType); |
| 1670 |
buf.writestring(","); |
| 1671 |
} |
| 1672 |
else if (global.params.cplusplus >= CppStdRevision.cpp11 && |
| 1673 |
manifestConstants && (memberKind == EnumKind.Int || memberKind == EnumKind.Numeric)) |
| 1674 |
{ |
| 1675 |
buf.writestring("enum : "); |
| 1676 |
determineEnumType(memberType).accept(this); |
| 1677 |
buf.writestring(" { "); |
| 1678 |
writeIdentifier(m, true); |
| 1679 |
buf.writestring(" = "); |
| 1680 |
|
| 1681 |
auto ie = cast(AST.IntegerExp)m.value; |
| 1682 |
visitInteger(ie.toInteger(), memberType); |
| 1683 |
buf.writestring(" };"); |
| 1684 |
} |
| 1685 |
else |
| 1686 |
{ |
| 1687 |
buf.writestring("static "); |
| 1688 |
auto target = determineEnumType(memberType); |
| 1689 |
target.accept(this); |
| 1690 |
buf.writestring(" const "); |
| 1691 |
writeIdentifier(m, true); |
| 1692 |
buf.writestring(" = "); |
| 1693 |
printExpressionFor(target, m.origValue); |
| 1694 |
buf.writestring(";"); |
| 1695 |
} |
| 1696 |
buf.writenl(); |
| 1697 |
} |
| 1698 |
|
| 1699 |
if (!manifestConstants) |
| 1700 |
buf.level--; |
| 1701 |
// write the enum tail |
| 1702 |
if (!manifestConstants) |
| 1703 |
buf.writestring("};"); |
| 1704 |
buf.writenl(); |
| 1705 |
buf.writenl(); |
| 1706 |
} |
| 1707 |
|
| 1708 |
override void visit(AST.EnumMember em) |
| 1709 |
{ |
| 1710 |
assert(em.ed); |
| 1711 |
|
| 1712 |
// Members of anonymous members are reachable without referencing the |
| 1713 |
// EnumDeclaration, e.g. public import foo : someEnumMember; |
| 1714 |
if (em.ed.isAnonymous()) |
| 1715 |
{ |
| 1716 |
visit(em.ed); |
| 1717 |
return; |
| 1718 |
} |
| 1719 |
|
| 1720 |
assert(false, "This node type should be handled in the EnumDeclaration"); |
| 1721 |
} |
| 1722 |
|
| 1723 |
override void visit(AST.TupleDeclaration tup) |
| 1724 |
{ |
| 1725 |
debug (Debug_DtoH) mixin(traceVisit!tup); |
| 1726 |
|
| 1727 |
tup.foreachVar((s) { s.accept(this); }); |
| 1728 |
} |
| 1729 |
|
| 1730 |
/** |
| 1731 |
* Prints a member/parameter/variable declaration into `buf`. |
| 1732 |
* |
| 1733 |
* Params: |
| 1734 |
* t = the type (used if `this.origType` is null) |
| 1735 |
* s = the symbol denoting the identifier |
| 1736 |
* canFixup = whether the identifier may be changed without affecting |
| 1737 |
* binary compatibility (forwarded to `writeIdentifier`) |
| 1738 |
*/ |
| 1739 |
private void typeToBuffer(AST.Type t, AST.Dsymbol s, const bool canFixup = false) |
| 1740 |
{ |
| 1741 |
debug (Debug_DtoH) |
| 1742 |
{ |
| 1743 |
printf("[typeToBuffer(AST.Type, AST.Dsymbol) enter] %s sym %s\n", t.toChars(), s.toChars()); |
| 1744 |
scope(exit) printf("[typeToBuffer(AST.Type, AST.Dsymbol) exit] %s sym %s\n", t.toChars(), s.toChars()); |
| 1745 |
} |
| 1746 |
|
| 1747 |
// The context pointer (represented as `ThisDeclaration`) is named |
| 1748 |
// `this` but accessible via `outer` |
| 1749 |
if (auto td = s.isThisDeclaration()) |
| 1750 |
{ |
| 1751 |
import dmd.id; |
| 1752 |
this.ident = Id.outer; |
| 1753 |
} |
| 1754 |
else |
| 1755 |
this.ident = s.ident; |
| 1756 |
|
| 1757 |
auto type = origType !is null ? origType : t; |
| 1758 |
AST.Dsymbol customLength; |
| 1759 |
|
| 1760 |
// Check for quirks that are usually resolved during semantic |
| 1761 |
if (tdparent) |
| 1762 |
{ |
| 1763 |
// Declarations within template declarations might use TypeAArray |
| 1764 |
// instead of TypeSArray when the length is not an IntegerExp, |
| 1765 |
// e.g. int[SOME_CONSTANT] |
| 1766 |
if (auto taa = type.isTypeAArray()) |
| 1767 |
{ |
| 1768 |
// Try to resolve the symbol from the key if it's not an actual type |
| 1769 |
Identifier id; |
| 1770 |
if (auto ti = taa.index.isTypeIdentifier()) |
| 1771 |
id = ti.ident; |
| 1772 |
|
| 1773 |
if (id) |
| 1774 |
{ |
| 1775 |
auto sym = findSymbol(id, adparent ? adparent : tdparent); |
| 1776 |
if (!sym) |
| 1777 |
{ |
| 1778 |
// Couldn't resolve, assume actual AA |
| 1779 |
} |
| 1780 |
else if (AST.isType(sym)) |
| 1781 |
{ |
| 1782 |
// a real associative array, forward to visit |
| 1783 |
} |
| 1784 |
else if (auto vd = sym.isVarDeclaration()) |
| 1785 |
{ |
| 1786 |
// Actually a static array with length symbol |
| 1787 |
customLength = sym; |
| 1788 |
type = taa.next; // visit the element type, length is written below |
| 1789 |
} |
| 1790 |
else |
| 1791 |
{ |
| 1792 |
printf("Resolved unexpected symbol while determining static array length: %s\n", sym.toChars()); |
| 1793 |
fflush(stdout); |
| 1794 |
fatal(); |
| 1795 |
} |
| 1796 |
} |
| 1797 |
} |
| 1798 |
} |
| 1799 |
type.accept(this); |
| 1800 |
if (this.ident) |
| 1801 |
{ |
| 1802 |
buf.writeByte(' '); |
| 1803 |
// Custom identifier doesn't need further checks |
| 1804 |
if (this.ident !is s.ident) |
| 1805 |
buf.writestring(this.ident.toString()); |
| 1806 |
else |
| 1807 |
writeIdentifier(s, canFixup); |
| 1808 |
|
| 1809 |
} |
| 1810 |
this.ident = null; |
| 1811 |
|
| 1812 |
// Size is either taken from the type or resolved above |
| 1813 |
auto tsa = t.isTypeSArray(); |
| 1814 |
if (tsa || customLength) |
| 1815 |
{ |
| 1816 |
buf.writeByte('['); |
| 1817 |
if (tsa) |
| 1818 |
tsa.dim.accept(this); |
| 1819 |
else |
| 1820 |
writeFullName(customLength); |
| 1821 |
buf.writeByte(']'); |
| 1822 |
} |
| 1823 |
else if (t.isTypeNoreturn()) |
| 1824 |
buf.writestring("[0]"); |
| 1825 |
} |
| 1826 |
|
| 1827 |
override void visit(AST.Type t) |
| 1828 |
{ |
| 1829 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1830 |
printf("Invalid type: %s\n", t.toPrettyChars()); |
| 1831 |
assert(0); |
| 1832 |
} |
| 1833 |
|
| 1834 |
override void visit(AST.TypeNoreturn t) |
| 1835 |
{ |
| 1836 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1837 |
|
| 1838 |
buf.writestring("/* noreturn */ char"); |
| 1839 |
} |
| 1840 |
|
| 1841 |
override void visit(AST.TypeIdentifier t) |
| 1842 |
{ |
| 1843 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1844 |
|
| 1845 |
// Try to resolve the referenced symbol |
| 1846 |
if (auto sym = findSymbol(t.ident)) |
| 1847 |
ensureDeclared(outermostSymbol(sym)); |
| 1848 |
|
| 1849 |
if (t.idents.length) |
| 1850 |
buf.writestring("typename "); |
| 1851 |
|
| 1852 |
writeIdentifier(t.ident, t.loc, "type", tdparent !is null); |
| 1853 |
|
| 1854 |
foreach (arg; t.idents) |
| 1855 |
{ |
| 1856 |
buf.writestring("::"); |
| 1857 |
|
| 1858 |
import dmd.rootobject; |
| 1859 |
// Is this even possible? |
| 1860 |
if (arg.dyncast != DYNCAST.identifier) |
| 1861 |
{ |
| 1862 |
printf("arg.dyncast() = %d\n", arg.dyncast()); |
| 1863 |
assert(false); |
| 1864 |
} |
| 1865 |
buf.writestring((cast(Identifier) arg).toChars()); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
override void visit(AST.TypeNull t) |
| 1870 |
{ |
| 1871 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1872 |
|
| 1873 |
if (global.params.cplusplus >= CppStdRevision.cpp11) |
| 1874 |
buf.writestring("nullptr_t"); |
| 1875 |
else |
| 1876 |
buf.writestring("void*"); |
| 1877 |
|
| 1878 |
} |
| 1879 |
|
| 1880 |
override void visit(AST.TypeTypeof t) |
| 1881 |
{ |
| 1882 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1883 |
|
| 1884 |
assert(t.exp); |
| 1885 |
|
| 1886 |
if (t.exp.type) |
| 1887 |
{ |
| 1888 |
t.exp.type.accept(this); |
| 1889 |
} |
| 1890 |
else if (t.exp.isThisExp()) |
| 1891 |
{ |
| 1892 |
// Short circuit typeof(this) => <Aggregate name> |
| 1893 |
assert(adparent); |
| 1894 |
buf.writestring(adparent.ident.toChars()); |
| 1895 |
} |
| 1896 |
else |
| 1897 |
{ |
| 1898 |
// Relying on C++'s typeof might produce wrong results |
| 1899 |
// but it's the best we've got here. |
| 1900 |
buf.writestring("typeof("); |
| 1901 |
t.exp.accept(this); |
| 1902 |
buf.writeByte(')'); |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
override void visit(AST.TypeBasic t) |
| 1907 |
{ |
| 1908 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1909 |
|
| 1910 |
if (t.isConst() || t.isImmutable()) |
| 1911 |
buf.writestring("const "); |
| 1912 |
string typeName; |
| 1913 |
switch (t.ty) |
| 1914 |
{ |
| 1915 |
case AST.Tvoid: typeName = "void"; break; |
| 1916 |
case AST.Tbool: typeName = "bool"; break; |
| 1917 |
case AST.Tchar: typeName = "char"; break; |
| 1918 |
case AST.Twchar: typeName = "char16_t"; break; |
| 1919 |
case AST.Tdchar: typeName = "char32_t"; break; |
| 1920 |
case AST.Tint8: typeName = "int8_t"; break; |
| 1921 |
case AST.Tuns8: typeName = "uint8_t"; break; |
| 1922 |
case AST.Tint16: typeName = "int16_t"; break; |
| 1923 |
case AST.Tuns16: typeName = "uint16_t"; break; |
| 1924 |
case AST.Tint32: typeName = "int32_t"; break; |
| 1925 |
case AST.Tuns32: typeName = "uint32_t"; break; |
| 1926 |
case AST.Tint64: typeName = "int64_t"; break; |
| 1927 |
case AST.Tuns64: typeName = "uint64_t"; break; |
| 1928 |
case AST.Tfloat32: typeName = "float"; break; |
| 1929 |
case AST.Tfloat64: typeName = "double"; break; |
| 1930 |
case AST.Tfloat80: |
| 1931 |
typeName = "_d_real"; |
| 1932 |
hasReal = true; |
| 1933 |
break; |
| 1934 |
case AST.Tcomplex32: typeName = "_Complex float"; break; |
| 1935 |
case AST.Tcomplex64: typeName = "_Complex double"; break; |
| 1936 |
case AST.Tcomplex80: |
| 1937 |
typeName = "_Complex _d_real"; |
| 1938 |
hasReal = true; |
| 1939 |
break; |
| 1940 |
// ???: This is not strictly correct, but it should be ignored |
| 1941 |
// in all places where it matters most (variables, functions, ...). |
| 1942 |
case AST.Timaginary32: typeName = "float"; break; |
| 1943 |
case AST.Timaginary64: typeName = "double"; break; |
| 1944 |
case AST.Timaginary80: |
| 1945 |
typeName = "_d_real"; |
| 1946 |
hasReal = true; |
| 1947 |
break; |
| 1948 |
default: |
| 1949 |
//t.print(); |
| 1950 |
assert(0); |
| 1951 |
} |
| 1952 |
buf.writestring(typeName); |
| 1953 |
} |
| 1954 |
|
| 1955 |
override void visit(AST.TypePointer t) |
| 1956 |
{ |
| 1957 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1958 |
|
| 1959 |
auto ts = t.next.isTypeStruct(); |
| 1960 |
if (ts && !strcmp(ts.sym.ident.toChars(), "__va_list_tag")) |
| 1961 |
{ |
| 1962 |
buf.writestring("va_list"); |
| 1963 |
return; |
| 1964 |
} |
| 1965 |
|
| 1966 |
// Pointer targets can be forward referenced |
| 1967 |
const fwdSave = forwarding; |
| 1968 |
forwarding = true; |
| 1969 |
scope (exit) forwarding = fwdSave; |
| 1970 |
|
| 1971 |
t.next.accept(this); |
| 1972 |
if (t.next.ty != AST.Tfunction) |
| 1973 |
buf.writeByte('*'); |
| 1974 |
if (t.isConst() || t.isImmutable()) |
| 1975 |
buf.writestring(" const"); |
| 1976 |
} |
| 1977 |
|
| 1978 |
override void visit(AST.TypeSArray t) |
| 1979 |
{ |
| 1980 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1981 |
t.next.accept(this); |
| 1982 |
} |
| 1983 |
|
| 1984 |
override void visit(AST.TypeAArray t) |
| 1985 |
{ |
| 1986 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 1987 |
AST.Type.tvoidptr.accept(this); |
| 1988 |
} |
| 1989 |
|
| 1990 |
override void visit(AST.TypeFunction tf) |
| 1991 |
{ |
| 1992 |
debug (Debug_DtoH) mixin(traceVisit!tf); |
| 1993 |
|
| 1994 |
tf.next.accept(this); |
| 1995 |
buf.writeByte('('); |
| 1996 |
buf.writeByte('*'); |
| 1997 |
if (ident) |
| 1998 |
buf.writestring(ident.toChars()); |
| 1999 |
ident = null; |
| 2000 |
buf.writeByte(')'); |
| 2001 |
buf.writeByte('('); |
| 2002 |
foreach (i, fparam; tf.parameterList) |
| 2003 |
{ |
| 2004 |
if (i) |
| 2005 |
buf.writestring(", "); |
| 2006 |
fparam.accept(this); |
| 2007 |
} |
| 2008 |
if (tf.parameterList.varargs) |
| 2009 |
{ |
| 2010 |
if (tf.parameterList.parameters.length && tf.parameterList.varargs == 1) |
| 2011 |
buf.writestring(", "); |
| 2012 |
buf.writestring("..."); |
| 2013 |
} |
| 2014 |
buf.writeByte(')'); |
| 2015 |
} |
| 2016 |
|
| 2017 |
/// Writes the type that represents `ed` into `buf`. |
| 2018 |
/// (Might not be `ed` for special enums or enums that were emitted as namespaces) |
| 2019 |
private void enumToBuffer(AST.EnumDeclaration ed) |
| 2020 |
{ |
| 2021 |
debug (Debug_DtoH) mixin(traceVisit!ed); |
| 2022 |
|
| 2023 |
if (ed.isSpecial()) |
| 2024 |
{ |
| 2025 |
if (ed.ident == DMDType.c_long) |
| 2026 |
buf.writestring("long"); |
| 2027 |
else if (ed.ident == DMDType.c_ulong) |
| 2028 |
buf.writestring("unsigned long"); |
| 2029 |
else if (ed.ident == DMDType.c_longlong) |
| 2030 |
buf.writestring("long long"); |
| 2031 |
else if (ed.ident == DMDType.c_ulonglong) |
| 2032 |
buf.writestring("unsigned long long"); |
| 2033 |
else if (ed.ident == DMDType.c_long_double) |
| 2034 |
buf.writestring("long double"); |
| 2035 |
else if (ed.ident == DMDType.c_char) |
| 2036 |
buf.writestring("char"); |
| 2037 |
else if (ed.ident == DMDType.c_wchar_t) |
| 2038 |
buf.writestring("wchar_t"); |
| 2039 |
else if (ed.ident == DMDType.c_complex_float) |
| 2040 |
buf.writestring("_Complex float"); |
| 2041 |
else if (ed.ident == DMDType.c_complex_double) |
| 2042 |
buf.writestring("_Complex double"); |
| 2043 |
else if (ed.ident == DMDType.c_complex_real) |
| 2044 |
buf.writestring("_Complex long double"); |
| 2045 |
else |
| 2046 |
{ |
| 2047 |
//ed.print(); |
| 2048 |
assert(0); |
| 2049 |
} |
| 2050 |
return; |
| 2051 |
} |
| 2052 |
|
| 2053 |
const kind = getEnumKind(ed.memtype); |
| 2054 |
|
| 2055 |
// Check if the enum was emitted as a real enum |
| 2056 |
if (kind == EnumKind.Int || kind == EnumKind.Numeric) |
| 2057 |
{ |
| 2058 |
writeFullName(ed); |
| 2059 |
} |
| 2060 |
else |
| 2061 |
{ |
| 2062 |
// Use the base type if the enum was emitted as a namespace |
| 2063 |
buf.printf("/* %s */ ", ed.ident.toChars()); |
| 2064 |
ed.memtype.accept(this); |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
override void visit(AST.TypeEnum t) |
| 2069 |
{ |
| 2070 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 2071 |
|
| 2072 |
if (t.isConst() || t.isImmutable()) |
| 2073 |
buf.writestring("const "); |
| 2074 |
enumToBuffer(t.sym); |
| 2075 |
} |
| 2076 |
|
| 2077 |
override void visit(AST.TypeStruct t) |
| 2078 |
{ |
| 2079 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 2080 |
|
| 2081 |
if (t.isConst() || t.isImmutable()) |
| 2082 |
buf.writestring("const "); |
| 2083 |
writeFullName(t.sym); |
| 2084 |
} |
| 2085 |
|
| 2086 |
override void visit(AST.TypeDArray t) |
| 2087 |
{ |
| 2088 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 2089 |
|
| 2090 |
hasDArray = true; |
| 2091 |
|
| 2092 |
if (t.isConst() || t.isImmutable()) |
| 2093 |
buf.writestring("const "); |
| 2094 |
buf.writestring("_d_dynamicArray< "); |
| 2095 |
t.next.accept(this); |
| 2096 |
buf.writestring(" >"); |
| 2097 |
} |
| 2098 |
|
| 2099 |
override void visit(AST.TypeInstance t) |
| 2100 |
{ |
| 2101 |
visitTi(t.tempinst); |
| 2102 |
} |
| 2103 |
|
| 2104 |
private void visitTi(AST.TemplateInstance ti) |
| 2105 |
{ |
| 2106 |
debug (Debug_DtoH) mixin(traceVisit!ti); |
| 2107 |
|
| 2108 |
// Ensure that the TD appears before the instance |
| 2109 |
if (auto td = findTemplateDeclaration(ti)) |
| 2110 |
ensureDeclared(td); |
| 2111 |
|
| 2112 |
foreach (o; *ti.tiargs) |
| 2113 |
{ |
| 2114 |
if (!AST.isType(o)) |
| 2115 |
return; |
| 2116 |
} |
| 2117 |
buf.writestring(ti.name.toChars()); |
| 2118 |
buf.writeByte('<'); |
| 2119 |
foreach (i, o; *ti.tiargs) |
| 2120 |
{ |
| 2121 |
if (i) |
| 2122 |
buf.writestring(", "); |
| 2123 |
if (auto tt = AST.isType(o)) |
| 2124 |
{ |
| 2125 |
tt.accept(this); |
| 2126 |
} |
| 2127 |
else |
| 2128 |
{ |
| 2129 |
//ti.print(); |
| 2130 |
//o.print(); |
| 2131 |
assert(0); |
| 2132 |
} |
| 2133 |
} |
| 2134 |
buf.writestring(" >"); |
| 2135 |
} |
| 2136 |
|
| 2137 |
override void visit(AST.TemplateDeclaration td) |
| 2138 |
{ |
| 2139 |
debug (Debug_DtoH) mixin(traceVisit!td); |
| 2140 |
|
| 2141 |
if (!shouldEmitAndMarkVisited(td)) |
| 2142 |
return; |
| 2143 |
|
| 2144 |
if (!td.parameters || !td.onemember || (!td.onemember.isStructDeclaration && !td.onemember.isClassDeclaration && !td.onemember.isFuncDeclaration)) |
| 2145 |
{ |
| 2146 |
visit(cast(AST.Dsymbol)td); |
| 2147 |
return; |
| 2148 |
} |
| 2149 |
|
| 2150 |
// Explicitly disallow templates with non-type parameters or specialization. |
| 2151 |
foreach (p; *td.parameters) |
| 2152 |
{ |
| 2153 |
if (!p.isTemplateTypeParameter() || p.specialization()) |
| 2154 |
{ |
| 2155 |
visit(cast(AST.Dsymbol)td); |
| 2156 |
return; |
| 2157 |
} |
| 2158 |
} |
| 2159 |
|
| 2160 |
auto save = tdparent; |
| 2161 |
tdparent = td; |
| 2162 |
const bookmark = buf.length; |
| 2163 |
printTemplateParams(td); |
| 2164 |
|
| 2165 |
const oldIgnored = this.ignoredCounter; |
| 2166 |
td.onemember.accept(this); |
| 2167 |
|
| 2168 |
// Remove "template<...>" if the symbol could not be emitted |
| 2169 |
if (oldIgnored != this.ignoredCounter) |
| 2170 |
buf.setsize(bookmark); |
| 2171 |
|
| 2172 |
tdparent = save; |
| 2173 |
} |
| 2174 |
|
| 2175 |
/// Writes the template<...> header for the supplied template declaration |
| 2176 |
private void printTemplateParams(const AST.TemplateDeclaration td) |
| 2177 |
{ |
| 2178 |
buf.writestring("template <"); |
| 2179 |
bool first = true; |
| 2180 |
foreach (p; *td.parameters) |
| 2181 |
{ |
| 2182 |
if (first) |
| 2183 |
first = false; |
| 2184 |
else |
| 2185 |
buf.writestring(", "); |
| 2186 |
buf.writestring("typename "); |
| 2187 |
writeIdentifier(p.ident, p.loc, "template parameter", true); |
| 2188 |
} |
| 2189 |
buf.writestringln(">"); |
| 2190 |
} |
| 2191 |
|
| 2192 |
/// Emit declarations of the TemplateMixin in the current scope |
| 2193 |
override void visit(AST.TemplateMixin tm) |
| 2194 |
{ |
| 2195 |
debug (Debug_DtoH) mixin(traceVisit!tm); |
| 2196 |
|
| 2197 |
auto members = tm.members; |
| 2198 |
|
| 2199 |
// members are missing for instances inside of TemplateDeclarations, e.g. |
| 2200 |
// template Foo(T) { mixin Bar!T; } |
| 2201 |
if (!members) |
| 2202 |
{ |
| 2203 |
if (auto td = findTemplateDeclaration(tm)) |
| 2204 |
members = td.members; // Emit members of the template |
| 2205 |
else |
| 2206 |
return; // Cannot emit mixin |
| 2207 |
} |
| 2208 |
|
| 2209 |
foreach (s; *members) |
| 2210 |
{ |
| 2211 |
// kind is undefined without semantic |
| 2212 |
const kind = s.visible().kind; |
| 2213 |
if (kind == AST.Visibility.Kind.public_ || kind == AST.Visibility.Kind.undefined) |
| 2214 |
s.accept(this); |
| 2215 |
} |
| 2216 |
} |
| 2217 |
|
| 2218 |
/** |
| 2219 |
* Finds a symbol with the identifier `name` by iterating the linked list of parent |
| 2220 |
* symbols, starting from `context`. |
| 2221 |
* |
| 2222 |
* Returns: the symbol or `null` if missing |
| 2223 |
*/ |
| 2224 |
private AST.Dsymbol findSymbol(Identifier name, AST.Dsymbol context) |
| 2225 |
{ |
| 2226 |
// Follow the declaration context |
| 2227 |
for (auto par = context; par; par = par.toParentDecl()) |
| 2228 |
{ |
| 2229 |
// Check that `name` doesn't refer to a template parameter |
| 2230 |
if (auto td = par.isTemplateDeclaration()) |
| 2231 |
{ |
| 2232 |
foreach (const p; *td.parameters) |
| 2233 |
{ |
| 2234 |
if (p.ident == name) |
| 2235 |
return null; |
| 2236 |
} |
| 2237 |
} |
| 2238 |
|
| 2239 |
if (auto mem = findMember(par, name)) |
| 2240 |
{ |
| 2241 |
return mem; |
| 2242 |
} |
| 2243 |
} |
| 2244 |
return null; |
| 2245 |
} |
| 2246 |
|
| 2247 |
/// ditto |
| 2248 |
private AST.Dsymbol findSymbol(Identifier name) |
| 2249 |
{ |
| 2250 |
AST.Dsymbol sym; |
| 2251 |
if (adparent) |
| 2252 |
sym = findSymbol(name, adparent); |
| 2253 |
|
| 2254 |
if (!sym && tdparent) |
| 2255 |
sym = findSymbol(name, tdparent); |
| 2256 |
|
| 2257 |
return sym; |
| 2258 |
} |
| 2259 |
|
| 2260 |
/// Finds the template declaration for instance `ti` |
| 2261 |
private AST.TemplateDeclaration findTemplateDeclaration(AST.TemplateInstance ti) |
| 2262 |
{ |
| 2263 |
if (ti.tempdecl) |
| 2264 |
return ti.tempdecl.isTemplateDeclaration(); |
| 2265 |
|
| 2266 |
assert(tdparent); // Only missing inside of templates |
| 2267 |
|
| 2268 |
// Search for the TemplateDeclaration, starting from the enclosing scope |
| 2269 |
// if known or the enclosing template. |
| 2270 |
auto sym = findSymbol(ti.name, ti.parent ? ti.parent : tdparent); |
| 2271 |
return sym ? sym.isTemplateDeclaration() : null; |
| 2272 |
} |
| 2273 |
|
| 2274 |
override void visit(AST.TypeClass t) |
| 2275 |
{ |
| 2276 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 2277 |
|
| 2278 |
// Classes are emitted as pointer and hence can be forwarded |
| 2279 |
const fwdSave = forwarding; |
| 2280 |
forwarding = true; |
| 2281 |
scope (exit) forwarding = fwdSave; |
| 2282 |
|
| 2283 |
if (t.isConst() || t.isImmutable()) |
| 2284 |
buf.writestring("const "); |
| 2285 |
writeFullName(t.sym); |
| 2286 |
buf.writeByte('*'); |
| 2287 |
if (t.isConst() || t.isImmutable()) |
| 2288 |
buf.writestring(" const"); |
| 2289 |
} |
| 2290 |
|
| 2291 |
/** |
| 2292 |
* Writes the function signature to `buf`. |
| 2293 |
* |
| 2294 |
* Params: |
| 2295 |
* tf = fd's type |
| 2296 |
* fd = the function to print |
| 2297 |
*/ |
| 2298 |
private void funcToBuffer(AST.TypeFunction tf, AST.FuncDeclaration fd) |
| 2299 |
{ |
| 2300 |
debug (Debug_DtoH) |
| 2301 |
{ |
| 2302 |
printf("[funcToBuffer(AST.TypeFunction) enter] %s\n", fd.toChars()); |
| 2303 |
scope(exit) printf("[funcToBuffer(AST.TypeFunction) exit] %s\n", fd.toChars()); |
| 2304 |
} |
| 2305 |
|
| 2306 |
auto originalType = cast(AST.TypeFunction)fd.originalType; |
| 2307 |
|
| 2308 |
if (fd.isCtorDeclaration() || fd.isDtorDeclaration()) |
| 2309 |
{ |
| 2310 |
if (fd.isDtorDeclaration()) |
| 2311 |
{ |
| 2312 |
buf.writeByte('~'); |
| 2313 |
} |
| 2314 |
buf.writestring(adparent.toChars()); |
| 2315 |
if (!tf) |
| 2316 |
{ |
| 2317 |
assert(fd.isDtorDeclaration()); |
| 2318 |
buf.writestring("()"); |
| 2319 |
return; |
| 2320 |
} |
| 2321 |
} |
| 2322 |
else |
| 2323 |
{ |
| 2324 |
import dmd.root.string : toDString; |
| 2325 |
assert(tf.next, fd.loc.toChars().toDString()); |
| 2326 |
|
| 2327 |
tf.next == AST.Type.tsize_t ? originalType.next.accept(this) : tf.next.accept(this); |
| 2328 |
if (tf.isRef) |
| 2329 |
buf.writeByte('&'); |
| 2330 |
buf.writeByte(' '); |
| 2331 |
|
| 2332 |
if (fd._linkage == LINK.system) |
| 2333 |
{ |
| 2334 |
buf.writestring("EXTERN_SYSTEM_AFTER "); |
| 2335 |
} |
| 2336 |
else if (tf.linkage == LINK.windows) |
| 2337 |
{ |
| 2338 |
buf.writestring("__stdcall "); |
| 2339 |
} |
| 2340 |
writeIdentifier(fd); |
| 2341 |
} |
| 2342 |
|
| 2343 |
buf.writeByte('('); |
| 2344 |
foreach (i, fparam; tf.parameterList) |
| 2345 |
{ |
| 2346 |
if (i) |
| 2347 |
buf.writestring(", "); |
| 2348 |
if (fparam.type == AST.Type.tsize_t && originalType) |
| 2349 |
{ |
| 2350 |
fparam = originalType.parameterList[i]; |
| 2351 |
} |
| 2352 |
fparam.accept(this); |
| 2353 |
} |
| 2354 |
if (tf.parameterList.varargs) |
| 2355 |
{ |
| 2356 |
if (tf.parameterList.parameters.length && tf.parameterList.varargs == 1) |
| 2357 |
buf.writestring(", "); |
| 2358 |
buf.writestring("..."); |
| 2359 |
} |
| 2360 |
buf.writeByte(')'); |
| 2361 |
} |
| 2362 |
|
| 2363 |
override void visit(AST.Parameter p) |
| 2364 |
{ |
| 2365 |
debug (Debug_DtoH) mixin(traceVisit!p); |
| 2366 |
|
| 2367 |
ident = p.ident; |
| 2368 |
|
| 2369 |
{ |
| 2370 |
// Reference parameters can be forwarded |
| 2371 |
const fwdStash = this.forwarding; |
| 2372 |
this.forwarding = !!(p.storageClass & AST.STC.ref_); |
| 2373 |
p.type.accept(this); |
| 2374 |
this.forwarding = fwdStash; |
| 2375 |
} |
| 2376 |
|
| 2377 |
if (p.storageClass & (AST.STC.ref_ | AST.STC.out_)) |
| 2378 |
buf.writeByte('&'); |
| 2379 |
buf.writeByte(' '); |
| 2380 |
if (ident) |
| 2381 |
// FIXME: Parameter is missing a Loc |
| 2382 |
writeIdentifier(ident, Loc.initial, "parameter", true); |
| 2383 |
ident = null; |
| 2384 |
|
| 2385 |
if (p.defaultArg) |
| 2386 |
{ |
| 2387 |
//printf("%s %d\n", p.defaultArg.toChars, p.defaultArg.op); |
| 2388 |
buf.writestring(" = "); |
| 2389 |
// Always emit the FDN of a symbol for the default argument, |
| 2390 |
// to avoid generating an ambiguous assignment. |
| 2391 |
auto save = adparent; |
| 2392 |
adparent = null; |
| 2393 |
printExpressionFor(p.type, p.defaultArg); |
| 2394 |
adparent = save; |
| 2395 |
} |
| 2396 |
} |
| 2397 |
|
| 2398 |
/** |
| 2399 |
* Prints `exp` as an expression of type `target` while inserting |
| 2400 |
* appropriate code when implicit conversion does not translate |
| 2401 |
* directly to C++, e.g. from an enum to its base type. |
| 2402 |
* |
| 2403 |
* Params: |
| 2404 |
* target = the type `exp` is converted to |
| 2405 |
* exp = the expression to print |
| 2406 |
* isCtor = if `exp` is a ctor argument |
| 2407 |
*/ |
| 2408 |
private void printExpressionFor(AST.Type target, AST.Expression exp, const bool isCtor = false) |
| 2409 |
{ |
| 2410 |
/// Determines if a static_cast is required |
| 2411 |
static bool needsCast(AST.Type target, AST.Expression exp) |
| 2412 |
{ |
| 2413 |
// import std.stdio; |
| 2414 |
// writefln("%s:%s: target = %s, type = %s (%s)", exp.loc.linnum, exp.loc.charnum, target, exp.type, exp.op); |
| 2415 |
|
| 2416 |
auto source = exp.type; |
| 2417 |
|
| 2418 |
// DotVarExp resolve conversions, e.g from an enum to its base type |
| 2419 |
if (auto dve = exp.isDotVarExp()) |
| 2420 |
source = dve.var.type; |
| 2421 |
|
| 2422 |
if (!source) |
| 2423 |
// Defensively assume that the cast is required |
| 2424 |
return true; |
| 2425 |
|
| 2426 |
// Conversions from enum class to base type require static_cast |
| 2427 |
if (global.params.cplusplus >= CppStdRevision.cpp11 && |
| 2428 |
source.isTypeEnum && !target.isTypeEnum) |
| 2429 |
return true; |
| 2430 |
|
| 2431 |
return false; |
| 2432 |
} |
| 2433 |
|
| 2434 |
// Slices are emitted as a special struct, hence we need to fix up |
| 2435 |
// any expression initialising a slice variable/member |
| 2436 |
if (auto ta = target.isTypeDArray()) |
| 2437 |
{ |
| 2438 |
if (exp.isNullExp()) |
| 2439 |
{ |
| 2440 |
if (isCtor) |
| 2441 |
{ |
| 2442 |
// Don't emit, use default ctor |
| 2443 |
} |
| 2444 |
else if (global.params.cplusplus >= CppStdRevision.cpp11) |
| 2445 |
{ |
| 2446 |
// Prefer initializer list |
| 2447 |
buf.writestring("{}"); |
| 2448 |
} |
| 2449 |
else |
| 2450 |
{ |
| 2451 |
// Write __d_dynamic_array<TYPE>() |
| 2452 |
visit(ta); |
| 2453 |
buf.writestring("()"); |
| 2454 |
} |
| 2455 |
return; |
| 2456 |
} |
| 2457 |
|
| 2458 |
if (auto se = exp.isStringExp()) |
| 2459 |
{ |
| 2460 |
// Rewrite as <length> + <literal> pair optionally |
| 2461 |
// wrapped in a initializer list/ctor call |
| 2462 |
|
| 2463 |
const initList = global.params.cplusplus >= CppStdRevision.cpp11; |
| 2464 |
if (!isCtor) |
| 2465 |
{ |
| 2466 |
if (initList) |
| 2467 |
buf.writestring("{ "); |
| 2468 |
else |
| 2469 |
{ |
| 2470 |
visit(ta); |
| 2471 |
buf.writestring("( "); |
| 2472 |
} |
| 2473 |
} |
| 2474 |
|
| 2475 |
buf.printf("%zu, ", se.len); |
| 2476 |
visit(se); |
| 2477 |
|
| 2478 |
if (!isCtor) |
| 2479 |
buf.writestring(initList ? " }" : " )"); |
| 2480 |
|
| 2481 |
return; |
| 2482 |
} |
| 2483 |
} |
| 2484 |
else if (auto ce = exp.isCastExp()) |
| 2485 |
{ |
| 2486 |
buf.writeByte('('); |
| 2487 |
if (ce.to) |
| 2488 |
ce.to.accept(this); |
| 2489 |
else if (ce.e1.type) |
| 2490 |
// Try the expression type with modifiers in case of cast(const) in templates |
| 2491 |
ce.e1.type.castMod(ce.mod).accept(this); |
| 2492 |
else |
| 2493 |
// Fallback, not necessarily correct but the best we've got here |
| 2494 |
target.accept(this); |
| 2495 |
buf.writestring(") "); |
| 2496 |
ce.e1.accept(this); |
| 2497 |
} |
| 2498 |
else if (needsCast(target, exp)) |
| 2499 |
{ |
| 2500 |
buf.writestring("static_cast<"); |
| 2501 |
target.accept(this); |
| 2502 |
buf.writestring(">("); |
| 2503 |
exp.accept(this); |
| 2504 |
buf.writeByte(')'); |
| 2505 |
} |
| 2506 |
else |
| 2507 |
{ |
| 2508 |
exp.accept(this); |
| 2509 |
} |
| 2510 |
} |
| 2511 |
|
| 2512 |
override void visit(AST.Expression e) |
| 2513 |
{ |
| 2514 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2515 |
|
| 2516 |
// Valid in most cases, others should be overridden below |
| 2517 |
// to use the appropriate operators (:: and ->) |
| 2518 |
buf.writestring(e.toString()); |
| 2519 |
} |
| 2520 |
|
| 2521 |
override void visit(AST.UnaExp e) |
| 2522 |
{ |
| 2523 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2524 |
|
| 2525 |
buf.writestring(expToString(e.op)); |
| 2526 |
e.e1.accept(this); |
| 2527 |
} |
| 2528 |
|
| 2529 |
override void visit(AST.BinExp e) |
| 2530 |
{ |
| 2531 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2532 |
|
| 2533 |
e.e1.accept(this); |
| 2534 |
buf.writeByte(' '); |
| 2535 |
buf.writestring(expToString(e.op)); |
| 2536 |
buf.writeByte(' '); |
| 2537 |
e.e2.accept(this); |
| 2538 |
} |
| 2539 |
|
| 2540 |
/// Translates operator `op` into the C++ representation |
| 2541 |
private extern(D) static string expToString(const EXP op) |
| 2542 |
{ |
| 2543 |
switch (op) with (EXP) |
| 2544 |
{ |
| 2545 |
case identity: return "=="; |
| 2546 |
case notIdentity: return "!="; |
| 2547 |
default: |
| 2548 |
return EXPtoString(op); |
| 2549 |
} |
| 2550 |
} |
| 2551 |
|
| 2552 |
override void visit(AST.VarExp e) |
| 2553 |
{ |
| 2554 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2555 |
|
| 2556 |
// Local members don't need another prefix and might've been renamed |
| 2557 |
if (e.var.isThis()) |
| 2558 |
{ |
| 2559 |
includeSymbol(e.var); |
| 2560 |
writeIdentifier(e.var, true); |
| 2561 |
} |
| 2562 |
else |
| 2563 |
writeFullName(e.var); |
| 2564 |
} |
| 2565 |
|
| 2566 |
/// Partially prints the FQN including parent aggregates |
| 2567 |
private void printPrefix(AST.Dsymbol var) |
| 2568 |
{ |
| 2569 |
if (!var || var is adparent || var.isModule()) |
| 2570 |
return; |
| 2571 |
|
| 2572 |
writeFullName(var); |
| 2573 |
buf.writestring("::"); |
| 2574 |
} |
| 2575 |
|
| 2576 |
override void visit(AST.CallExp e) |
| 2577 |
{ |
| 2578 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2579 |
|
| 2580 |
// Dereferencing function pointers requires additional braces: (*f)(args) |
| 2581 |
const isFp = e.e1.isPtrExp(); |
| 2582 |
if (isFp) |
| 2583 |
buf.writeByte('('); |
| 2584 |
else if (e.f) |
| 2585 |
includeSymbol(outermostSymbol(e.f)); |
| 2586 |
|
| 2587 |
e.e1.accept(this); |
| 2588 |
|
| 2589 |
if (isFp) buf.writeByte(')'); |
| 2590 |
|
| 2591 |
assert(e.arguments); |
| 2592 |
buf.writeByte('('); |
| 2593 |
foreach (i, arg; *e.arguments) |
| 2594 |
{ |
| 2595 |
if (i) |
| 2596 |
buf.writestring(", "); |
| 2597 |
arg.accept(this); |
| 2598 |
} |
| 2599 |
buf.writeByte(')'); |
| 2600 |
} |
| 2601 |
|
| 2602 |
override void visit(AST.DotVarExp e) |
| 2603 |
{ |
| 2604 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2605 |
|
| 2606 |
if (auto sym = symbolFromType(e.e1.type)) |
| 2607 |
includeSymbol(outermostSymbol(sym)); |
| 2608 |
|
| 2609 |
// Accessing members through a pointer? |
| 2610 |
if (auto pe = e.e1.isPtrExp) |
| 2611 |
{ |
| 2612 |
pe.e1.accept(this); |
| 2613 |
buf.writestring("->"); |
| 2614 |
} |
| 2615 |
else |
| 2616 |
{ |
| 2617 |
e.e1.accept(this); |
| 2618 |
buf.writeByte('.'); |
| 2619 |
} |
| 2620 |
|
| 2621 |
// Should only be used to access non-static members |
| 2622 |
assert(e.var.isThis()); |
| 2623 |
|
| 2624 |
writeIdentifier(e.var, true); |
| 2625 |
} |
| 2626 |
|
| 2627 |
override void visit(AST.DotIdExp e) |
| 2628 |
{ |
| 2629 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2630 |
|
| 2631 |
e.e1.accept(this); |
| 2632 |
buf.writestring("::"); |
| 2633 |
buf.writestring(e.ident.toChars()); |
| 2634 |
} |
| 2635 |
|
| 2636 |
override void visit(AST.ScopeExp e) |
| 2637 |
{ |
| 2638 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2639 |
|
| 2640 |
// Usually a template instance in a TemplateDeclaration |
| 2641 |
if (auto ti = e.sds.isTemplateInstance()) |
| 2642 |
visitTi(ti); |
| 2643 |
else |
| 2644 |
writeFullName(e.sds); |
| 2645 |
} |
| 2646 |
|
| 2647 |
override void visit(AST.NullExp e) |
| 2648 |
{ |
| 2649 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2650 |
|
| 2651 |
if (global.params.cplusplus >= CppStdRevision.cpp11) |
| 2652 |
buf.writestring("nullptr"); |
| 2653 |
else |
| 2654 |
buf.writestring("NULL"); |
| 2655 |
} |
| 2656 |
|
| 2657 |
override void visit(AST.ArrayLiteralExp e) |
| 2658 |
{ |
| 2659 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2660 |
buf.writestring("arrayliteral"); |
| 2661 |
} |
| 2662 |
|
| 2663 |
override void visit(AST.StringExp e) |
| 2664 |
{ |
| 2665 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2666 |
|
| 2667 |
if (e.sz == 2) |
| 2668 |
buf.writeByte('u'); |
| 2669 |
else if (e.sz == 4) |
| 2670 |
buf.writeByte('U'); |
| 2671 |
buf.writeByte('"'); |
| 2672 |
|
| 2673 |
foreach (i; 0 .. e.len) |
| 2674 |
{ |
| 2675 |
writeCharLiteral(*buf, e.getCodeUnit(i)); |
| 2676 |
} |
| 2677 |
buf.writeByte('"'); |
| 2678 |
} |
| 2679 |
|
| 2680 |
override void visit(AST.RealExp e) |
| 2681 |
{ |
| 2682 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2683 |
|
| 2684 |
import dmd.root.ctfloat : CTFloat; |
| 2685 |
|
| 2686 |
// Special case NaN and Infinity because floatToBuffer |
| 2687 |
// uses D literals (`nan` and `infinity`) |
| 2688 |
if (CTFloat.isNaN(e.value)) |
| 2689 |
{ |
| 2690 |
buf.writestring("NAN"); |
| 2691 |
} |
| 2692 |
else if (CTFloat.isInfinity(e.value)) |
| 2693 |
{ |
| 2694 |
if (e.value < CTFloat.zero) |
| 2695 |
buf.writeByte('-'); |
| 2696 |
buf.writestring("INFINITY"); |
| 2697 |
} |
| 2698 |
else |
| 2699 |
{ |
| 2700 |
import dmd.hdrgen; |
| 2701 |
// Hex floating point literals were introduced in C++ 17 |
| 2702 |
const allowHex = global.params.cplusplus >= CppStdRevision.cpp17; |
| 2703 |
floatToBuffer(e.type, e.value, *buf, allowHex); |
| 2704 |
} |
| 2705 |
} |
| 2706 |
|
| 2707 |
override void visit(AST.IntegerExp e) |
| 2708 |
{ |
| 2709 |
debug (Debug_DtoH) mixin(traceVisit!e); |
| 2710 |
visitInteger(e.toInteger, e.type); |
| 2711 |
} |
| 2712 |
|
| 2713 |
/// Writes `v` as type `t` into `buf` |
| 2714 |
private void visitInteger(dinteger_t v, AST.Type t) |
| 2715 |
{ |
| 2716 |
debug (Debug_DtoH) mixin(traceVisit!t); |
| 2717 |
|
| 2718 |
switch (t.ty) |
| 2719 |
{ |
| 2720 |
case AST.Tenum: |
| 2721 |
auto te = cast(AST.TypeEnum)t; |
| 2722 |
buf.writestring("("); |
| 2723 |
enumToBuffer(te.sym); |
| 2724 |
buf.writestring(")"); |
| 2725 |
visitInteger(v, te.sym.memtype); |
| 2726 |
break; |
| 2727 |
case AST.Tbool: |
| 2728 |
buf.writestring(v ? "true" : "false"); |
| 2729 |
break; |
| 2730 |
case AST.Tint8: |
| 2731 |
buf.printf("%d", cast(byte)v); |
| 2732 |
break; |
| 2733 |
case AST.Tuns8: |
| 2734 |
buf.printf("%uu", cast(ubyte)v); |
| 2735 |
break; |
| 2736 |
case AST.Tint16: |
| 2737 |
buf.printf("%d", cast(short)v); |
| 2738 |
break; |
| 2739 |
case AST.Tuns16: |
| 2740 |
case AST.Twchar: |
| 2741 |
buf.printf("%uu", cast(ushort)v); |
| 2742 |
break; |
| 2743 |
case AST.Tint32: |
| 2744 |
case AST.Tdchar: |
| 2745 |
buf.printf("%d", cast(int)v); |
| 2746 |
break; |
| 2747 |
case AST.Tuns32: |
| 2748 |
buf.printf("%uu", cast(uint)v); |
| 2749 |
break; |
| 2750 |
case AST.Tint64: |
| 2751 |
buf.printf("%lldLL", v); |
| 2752 |
break; |
| 2753 |
case AST.Tuns64: |
| 2754 |
buf.printf("%lluLLU", v); |
| 2755 |
break; |
| 2756 |
case AST.Tchar: |
| 2757 |
if (v > 0x20 && v < 0x80) |
| 2758 |
buf.printf("'%c'", cast(int)v); |
| 2759 |
else |
| 2760 |
buf.printf("%uu", cast(ubyte)v); |
| 2761 |
break; |
| 2762 |
default: |
| 2763 |
//t.print(); |
| 2764 |
assert(0); |
| 2765 |
} |
| 2766 |
} |
| 2767 |
|
| 2768 |
override void visit(AST.StructLiteralExp sle) |
| 2769 |
{ |
| 2770 |
debug (Debug_DtoH) mixin(traceVisit!sle); |
| 2771 |
|
| 2772 |
const isUnion = sle.sd.isUnionDeclaration(); |
| 2773 |
sle.sd.type.accept(this); |
| 2774 |
buf.writeByte('('); |
| 2775 |
foreach(i, e; *sle.elements) |
| 2776 |
{ |
| 2777 |
if (i) |
| 2778 |
buf.writestring(", "); |
| 2779 |
|
| 2780 |
auto vd = sle.sd.fields[i]; |
| 2781 |
|
| 2782 |
// Expression may be null for unspecified elements |
| 2783 |
if (!e) |
| 2784 |
e = findDefaultInitializer(vd); |
| 2785 |
|
| 2786 |
printExpressionFor(vd.type, e); |
| 2787 |
|
| 2788 |
// Only emit the initializer of the first union member |
| 2789 |
if (isUnion) |
| 2790 |
break; |
| 2791 |
} |
| 2792 |
buf.writeByte(')'); |
| 2793 |
} |
| 2794 |
|
| 2795 |
/// Finds the default initializer for the given VarDeclaration |
| 2796 |
private static AST.Expression findDefaultInitializer(AST.VarDeclaration vd) |
| 2797 |
{ |
| 2798 |
if (vd._init && !vd._init.isVoidInitializer()) |
| 2799 |
return AST.initializerToExpression(vd._init); |
| 2800 |
if (auto ts = vd.type.isTypeStruct()) |
| 2801 |
{ |
| 2802 |
if (!ts.sym.noDefaultCtor && !ts.sym.isUnionDeclaration()) |
| 2803 |
{ |
| 2804 |
// Generate a call to the default constructor that we've generated. |
| 2805 |
auto sle = new AST.StructLiteralExp(Loc.initial, ts.sym, new AST.Expressions(0)); |
| 2806 |
sle.type = vd.type; |
| 2807 |
return sle; |
| 2808 |
} |
| 2809 |
else |
| 2810 |
return vd.type.defaultInitLiteral(Loc.initial); |
| 2811 |
} |
| 2812 |
else |
| 2813 |
return vd.type.defaultInitLiteral(Loc.initial); |
| 2814 |
} |
| 2815 |
|
| 2816 |
static if (__VERSION__ < 2092) |
| 2817 |
{ |
| 2818 |
private void ignored(const char* format, ...) nothrow |
| 2819 |
{ |
| 2820 |
this.ignoredCounter++; |
| 2821 |
|
| 2822 |
import core.stdc.stdarg; |
| 2823 |
if (!printIgnored) |
| 2824 |
return; |
| 2825 |
|
| 2826 |
va_list ap; |
| 2827 |
va_start(ap, format); |
| 2828 |
buf.writestring("// Ignored "); |
| 2829 |
buf.vprintf(format, ap); |
| 2830 |
buf.writenl(); |
| 2831 |
va_end(ap); |
| 2832 |
} |
| 2833 |
} |
| 2834 |
else |
| 2835 |
{ |
| 2836 |
/// Writes a formatted message into `buf` if `printIgnored` is true |
| 2837 |
/// and increments `ignoredCounter` |
| 2838 |
pragma(printf) |
| 2839 |
private void ignored(const char* format, ...) nothrow |
| 2840 |
{ |
| 2841 |
this.ignoredCounter++; |
| 2842 |
|
| 2843 |
import core.stdc.stdarg; |
| 2844 |
if (!printIgnored) |
| 2845 |
return; |
| 2846 |
|
| 2847 |
va_list ap; |
| 2848 |
va_start(ap, format); |
| 2849 |
buf.writestring("// Ignored "); |
| 2850 |
buf.vprintf(format, ap); |
| 2851 |
buf.writenl(); |
| 2852 |
va_end(ap); |
| 2853 |
} |
| 2854 |
} |
| 2855 |
|
| 2856 |
/** |
| 2857 |
* Determines whether `s` should be emitted. This requires that `sym` |
| 2858 |
* - is `extern(C[++]`) |
| 2859 |
* - is not instantiated from a template (visits the `TemplateDeclaration` instead) |
| 2860 |
* |
| 2861 |
* Params: |
| 2862 |
* sym = the symbol |
| 2863 |
* |
| 2864 |
* Returns: whether `sym` should be emitted |
| 2865 |
*/ |
| 2866 |
private bool shouldEmit(AST.Dsymbol sym) |
| 2867 |
{ |
| 2868 |
import dmd.aggregate : ClassKind; |
| 2869 |
debug (Debug_DtoH) |
| 2870 |
{ |
| 2871 |
printf("[shouldEmitAndMarkVisited enter] %s\n", sym.toPrettyChars()); |
| 2872 |
scope(exit) printf("[shouldEmitAndMarkVisited exit] %s\n", sym.toPrettyChars()); |
| 2873 |
} |
| 2874 |
|
| 2875 |
// Template *instances* should not be emitted |
| 2876 |
if (sym.isInstantiated()) |
| 2877 |
return false; |
| 2878 |
|
| 2879 |
// Matching linkage (except extern(C) classes which don't make sense) |
| 2880 |
if (linkage == LINK.cpp || (linkage == LINK.c && !sym.isClassDeclaration())) |
| 2881 |
return true; |
| 2882 |
|
| 2883 |
// Check against the internal information which might be missing, e.g. inside of template declarations |
| 2884 |
if (auto dec = sym.isDeclaration()) |
| 2885 |
{ |
| 2886 |
const l = dec.resolvedLinkage(); |
| 2887 |
return l == LINK.cpp || l == LINK.c; |
| 2888 |
} |
| 2889 |
|
| 2890 |
if (auto ad = sym.isAggregateDeclaration()) |
| 2891 |
return ad.classKind == ClassKind.cpp; |
| 2892 |
|
| 2893 |
return false; |
| 2894 |
} |
| 2895 |
|
| 2896 |
/** |
| 2897 |
* Determines whether `s` should be emitted. This requires that `sym` |
| 2898 |
* - was not visited before |
| 2899 |
* - is `extern(C[++]`) |
| 2900 |
* - is not instantiated from a template (visits the `TemplateDeclaration` instead) |
| 2901 |
* The result is cached in the visited nodes array. |
| 2902 |
* |
| 2903 |
* Params: |
| 2904 |
* sym = the symbol |
| 2905 |
* |
| 2906 |
* Returns: whether `sym` should be emitted |
| 2907 |
**/ |
| 2908 |
private bool shouldEmitAndMarkVisited(AST.Dsymbol sym) |
| 2909 |
{ |
| 2910 |
debug (Debug_DtoH) |
| 2911 |
{ |
| 2912 |
printf("[shouldEmitAndMarkVisited enter] %s\n", sym.toPrettyChars()); |
| 2913 |
scope(exit) printf("[shouldEmitAndMarkVisited exit] %s\n", sym.toPrettyChars()); |
| 2914 |
} |
| 2915 |
|
| 2916 |
auto statePtr = (cast(void*) sym) in visited; |
| 2917 |
|
| 2918 |
// `sym` was already emitted or skipped and isn't required |
| 2919 |
if (statePtr && (*statePtr || !mustEmit)) |
| 2920 |
return false; |
| 2921 |
|
| 2922 |
// Template *instances* should not be emitted, forward to the declaration |
| 2923 |
if (auto ti = sym.isInstantiated()) |
| 2924 |
{ |
| 2925 |
auto td = findTemplateDeclaration(ti); |
| 2926 |
assert(td); |
| 2927 |
visit(td); |
| 2928 |
return false; |
| 2929 |
} |
| 2930 |
|
| 2931 |
// Required or matching linkage (except extern(C) classes which don't make sense) |
| 2932 |
bool res = mustEmit || linkage == LINK.cpp || (linkage == LINK.c && !sym.isClassDeclaration()); |
| 2933 |
if (!res) |
| 2934 |
{ |
| 2935 |
// Check against the internal information which might be missing, e.g. inside of template declarations |
| 2936 |
if (auto dec = sym.isDeclaration()) |
| 2937 |
{ |
| 2938 |
const l = dec.resolvedLinkage(); |
| 2939 |
res = (l == LINK.cpp || l == LINK.c); |
| 2940 |
} |
| 2941 |
} |
| 2942 |
|
| 2943 |
// Remember result for later calls |
| 2944 |
if (statePtr) |
| 2945 |
*statePtr = res; |
| 2946 |
else |
| 2947 |
visited[(cast(void*) sym)] = res; |
| 2948 |
|
| 2949 |
// Print a warning when the symbol is ignored for the first time |
| 2950 |
// Might not be correct if it is required by symbol the is visited |
| 2951 |
// AFTER the current node |
| 2952 |
if (!statePtr && !res) |
| 2953 |
ignored("%s %s because of linkage", sym.kind(), sym.toPrettyChars()); |
| 2954 |
|
| 2955 |
return res; |
| 2956 |
} |
| 2957 |
|
| 2958 |
/** |
| 2959 |
* Ensures that `sym` is declared before the current position in `buf` by |
| 2960 |
* either creating a forward reference in `fwdbuf` if possible or |
| 2961 |
* calling `includeSymbol` to emit the entire declaration into `donebuf`. |
| 2962 |
*/ |
| 2963 |
private void ensureDeclared(AST.Dsymbol sym) |
| 2964 |
{ |
| 2965 |
auto par = sym.toParent2(); |
| 2966 |
auto ed = sym.isEnumDeclaration(); |
| 2967 |
|
| 2968 |
// Eagerly include the symbol if we cannot create a valid forward declaration |
| 2969 |
// Forwarding of scoped enums requires C++11 or above |
| 2970 |
if (!forwarding || (par && !par.isModule()) || (ed && global.params.cplusplus < CppStdRevision.cpp11)) |
| 2971 |
{ |
| 2972 |
// Emit the entire enclosing declaration if any |
| 2973 |
includeSymbol(outermostSymbol(sym)); |
| 2974 |
return; |
| 2975 |
} |
| 2976 |
|
| 2977 |
auto ti = sym.isInstantiated(); |
| 2978 |
auto td = ti ? findTemplateDeclaration(ti) : null; |
| 2979 |
auto check = cast(void*) (td ? td : sym); |
| 2980 |
|
| 2981 |
// Omit redundant fwd-declaration if we already emitted the entire declaration |
| 2982 |
if (visited.get(check, false)) |
| 2983 |
return; |
| 2984 |
|
| 2985 |
// Already created a fwd-declaration? |
| 2986 |
if (check in forwarded) |
| 2987 |
return; |
| 2988 |
forwarded[check] = true; |
| 2989 |
|
| 2990 |
// Print template<...> |
| 2991 |
if (ti) |
| 2992 |
{ |
| 2993 |
auto bufSave = buf; |
| 2994 |
buf = fwdbuf; |
| 2995 |
printTemplateParams(td); |
| 2996 |
buf = bufSave; |
| 2997 |
} |
| 2998 |
|
| 2999 |
// Determine the kind of symbol that is forwared: struct, ... |
| 3000 |
const(char)* kind; |
| 3001 |
|
| 3002 |
if (auto ad = sym.isAggregateDeclaration()) |
| 3003 |
{ |
| 3004 |
// Look for extern(C++, class) <some aggregate> |
| 3005 |
if (ad.cppmangle == CPPMANGLE.def) |
| 3006 |
kind = ad.kind(); |
| 3007 |
else if (ad.cppmangle == CPPMANGLE.asStruct) |
| 3008 |
kind = "struct"; |
| 3009 |
else |
| 3010 |
kind = "class"; |
| 3011 |
} |
| 3012 |
else if (ed) |
| 3013 |
{ |
| 3014 |
// Only called from enumToBuffer, so should always be emitted as an actual enum |
| 3015 |
kind = "enum class"; |
| 3016 |
} |
| 3017 |
else |
| 3018 |
kind = sym.kind(); // Should be unreachable but just to be sure |
| 3019 |
|
| 3020 |
fwdbuf.writestring(kind); |
| 3021 |
fwdbuf.writeByte(' '); |
| 3022 |
fwdbuf.writestring(sym.toChars()); |
| 3023 |
fwdbuf.writestringln(";"); |
| 3024 |
} |
| 3025 |
|
| 3026 |
/** |
| 3027 |
* Writes the qualified name of `sym` into `buf` including parent |
| 3028 |
* symbols and template parameters. |
| 3029 |
* |
| 3030 |
* Params: |
| 3031 |
* sym = the symbol |
| 3032 |
* mustInclude = whether sym may not be forward declared |
| 3033 |
*/ |
| 3034 |
private void writeFullName(AST.Dsymbol sym, const bool mustInclude = false) |
| 3035 |
in |
| 3036 |
{ |
| 3037 |
assert(sym); |
| 3038 |
assert(sym.ident, sym.toString()); |
| 3039 |
// Should never be called directly with a TI, only onemember |
| 3040 |
assert(!sym.isTemplateInstance(), sym.toString()); |
| 3041 |
} |
| 3042 |
do |
| 3043 |
{ |
| 3044 |
debug (Debug_DtoH) |
| 3045 |
{ |
| 3046 |
printf("[writeFullName enter] %s\n", sym.toPrettyChars()); |
| 3047 |
scope(exit) printf("[writeFullName exit] %s\n", sym.toPrettyChars()); |
| 3048 |
} |
| 3049 |
|
| 3050 |
// Explicit `pragma(mangle, "<some string>` overrides the declared name |
| 3051 |
if (auto mn = getMangleOverride(sym)) |
| 3052 |
return buf.writestring(mn); |
| 3053 |
|
| 3054 |
/// Checks whether `sym` is nested in `par` and hence doesn't need the FQN |
| 3055 |
static bool isNestedIn(AST.Dsymbol sym, AST.Dsymbol par) |
| 3056 |
{ |
| 3057 |
while (par) |
| 3058 |
{ |
| 3059 |
if (sym is par) |
| 3060 |
return true; |
| 3061 |
par = par.toParent(); |
| 3062 |
} |
| 3063 |
return false; |
| 3064 |
} |
| 3065 |
AST.TemplateInstance ti; |
| 3066 |
bool nested; |
| 3067 |
|
| 3068 |
// Check if the `sym` is nested into another symbol and hence requires `Parent::sym` |
| 3069 |
if (auto par = sym.toParent()) |
| 3070 |
{ |
| 3071 |
// toParent() yields the template instance if `sym` is the onemember of a TI |
| 3072 |
ti = par.isTemplateInstance(); |
| 3073 |
|
| 3074 |
// Skip the TI because Foo!int.Foo is folded into Foo<int> |
| 3075 |
if (ti) par = ti.toParent(); |
| 3076 |
|
| 3077 |
// Prefix the name with any enclosing declaration |
| 3078 |
// Stop at either module or enclosing aggregate |
| 3079 |
nested = !par.isModule(); |
| 3080 |
if (nested && !isNestedIn(par, adparent)) |
| 3081 |
{ |
| 3082 |
writeFullName(par, true); |
| 3083 |
buf.writestring("::"); |
| 3084 |
} |
| 3085 |
} |
| 3086 |
|
| 3087 |
if (!nested) |
| 3088 |
{ |
| 3089 |
// Cannot forward the symbol when called recursively |
| 3090 |
// for a nested symbol |
| 3091 |
if (mustInclude) |
| 3092 |
includeSymbol(sym); |
| 3093 |
else |
| 3094 |
ensureDeclared(sym); |
| 3095 |
} |
| 3096 |
|
| 3097 |
if (ti) |
| 3098 |
visitTi(ti); |
| 3099 |
else |
| 3100 |
buf.writestring(sym.ident.toString()); |
| 3101 |
} |
| 3102 |
|
| 3103 |
/// Returns: Explicit mangling for `sym` if present |
| 3104 |
extern(D) static const(char)[] getMangleOverride(const AST.Dsymbol sym) @safe |
| 3105 |
{ |
| 3106 |
if (auto decl = sym.isDeclaration()) |
| 3107 |
return decl.mangleOverride; |
| 3108 |
|
| 3109 |
return null; |
| 3110 |
} |
| 3111 |
} |
| 3112 |
|
| 3113 |
/// Namespace for identifiers used to represent special enums in C++ |
| 3114 |
struct DMDType |
| 3115 |
{ |
| 3116 |
__gshared Identifier c_long; |
| 3117 |
__gshared Identifier c_ulong; |
| 3118 |
__gshared Identifier c_longlong; |
| 3119 |
__gshared Identifier c_ulonglong; |
| 3120 |
__gshared Identifier c_long_double; |
| 3121 |
__gshared Identifier c_char; |
| 3122 |
__gshared Identifier c_wchar_t; |
| 3123 |
__gshared Identifier c_complex_float; |
| 3124 |
__gshared Identifier c_complex_double; |
| 3125 |
__gshared Identifier c_complex_real; |
| 3126 |
|
| 3127 |
static void _init() |
| 3128 |
{ |
| 3129 |
c_long = Identifier.idPool("__c_long"); |
| 3130 |
c_ulong = Identifier.idPool("__c_ulong"); |
| 3131 |
c_longlong = Identifier.idPool("__c_longlong"); |
| 3132 |
c_ulonglong = Identifier.idPool("__c_ulonglong"); |
| 3133 |
c_long_double = Identifier.idPool("__c_long_double"); |
| 3134 |
c_wchar_t = Identifier.idPool("__c_wchar_t"); |
| 3135 |
c_char = Identifier.idPool("__c_char"); |
| 3136 |
c_complex_float = Identifier.idPool("__c_complex_float"); |
| 3137 |
c_complex_double = Identifier.idPool("__c_complex_double"); |
| 3138 |
c_complex_real = Identifier.idPool("__c_complex_real"); |
| 3139 |
} |
| 3140 |
} |
| 3141 |
|
| 3142 |
/// Initializes all data structures used by the header generator |
| 3143 |
void initialize() |
| 3144 |
{ |
| 3145 |
__gshared bool initialized; |
| 3146 |
|
| 3147 |
if (!initialized) |
| 3148 |
{ |
| 3149 |
initialized = true; |
| 3150 |
|
| 3151 |
DMDType._init(); |
| 3152 |
} |
| 3153 |
} |
| 3154 |
|
| 3155 |
/// Writes `#if <content>` into the supplied buffer |
| 3156 |
void hashIf(ref OutBuffer buf, string content) @safe |
| 3157 |
{ |
| 3158 |
buf.writestring("#if "); |
| 3159 |
buf.writestringln(content); |
| 3160 |
} |
| 3161 |
|
| 3162 |
/// Writes `#elif <content>` into the supplied buffer |
| 3163 |
void hashElIf(ref OutBuffer buf, string content) @safe |
| 3164 |
{ |
| 3165 |
buf.writestring("#elif "); |
| 3166 |
buf.writestringln(content); |
| 3167 |
} |
| 3168 |
|
| 3169 |
/// Writes `#endif` into the supplied buffer |
| 3170 |
void hashEndIf(ref OutBuffer buf) @safe |
| 3171 |
{ |
| 3172 |
buf.writestringln("#endif"); |
| 3173 |
} |
| 3174 |
|
| 3175 |
/// Writes `#define <content>` into the supplied buffer |
| 3176 |
void hashDefine(ref OutBuffer buf, string content) @safe |
| 3177 |
{ |
| 3178 |
buf.writestring("#define "); |
| 3179 |
buf.writestringln(content); |
| 3180 |
} |
| 3181 |
|
| 3182 |
/// Writes `#include <content>` into the supplied buffer |
| 3183 |
void hashInclude(ref OutBuffer buf, string content) @safe |
| 3184 |
{ |
| 3185 |
buf.writestring("#include "); |
| 3186 |
buf.writestringln(content); |
| 3187 |
} |
| 3188 |
|
| 3189 |
/// Determines whether `ident` is a reserved keyword in C++ |
| 3190 |
/// Returns: the kind of keyword or `null` |
| 3191 |
const(char*) keywordClass(const Identifier ident) |
| 3192 |
{ |
| 3193 |
if (!ident) |
| 3194 |
return null; |
| 3195 |
|
| 3196 |
const name = ident.toString(); |
| 3197 |
switch (name) |
| 3198 |
{ |
| 3199 |
// C++ operators |
| 3200 |
case "and": |
| 3201 |
case "and_eq": |
| 3202 |
case "bitand": |
| 3203 |
case "bitor": |
| 3204 |
case "compl": |
| 3205 |
case "not": |
| 3206 |
case "not_eq": |
| 3207 |
case "or": |
| 3208 |
case "or_eq": |
| 3209 |
case "xor": |
| 3210 |
case "xor_eq": |
| 3211 |
return "special operator in C++"; |
| 3212 |
|
| 3213 |
// C++ keywords |
| 3214 |
case "_Complex": |
| 3215 |
case "const_cast": |
| 3216 |
case "delete": |
| 3217 |
case "dynamic_cast": |
| 3218 |
case "explicit": |
| 3219 |
case "friend": |
| 3220 |
case "inline": |
| 3221 |
case "mutable": |
| 3222 |
case "namespace": |
| 3223 |
case "operator": |
| 3224 |
case "register": |
| 3225 |
case "reinterpret_cast": |
| 3226 |
case "signed": |
| 3227 |
case "static_cast": |
| 3228 |
case "typedef": |
| 3229 |
case "typename": |
| 3230 |
case "unsigned": |
| 3231 |
case "using": |
| 3232 |
case "virtual": |
| 3233 |
case "volatile": |
| 3234 |
return "keyword in C++"; |
| 3235 |
|
| 3236 |
// Common macros imported by this header |
| 3237 |
// stddef.h |
| 3238 |
case "offsetof": |
| 3239 |
case "NULL": |
| 3240 |
return "default macro in C++"; |
| 3241 |
|
| 3242 |
// C++11 keywords |
| 3243 |
case "alignas": |
| 3244 |
case "alignof": |
| 3245 |
case "char16_t": |
| 3246 |
case "char32_t": |
| 3247 |
case "constexpr": |
| 3248 |
case "decltype": |
| 3249 |
case "noexcept": |
| 3250 |
case "nullptr": |
| 3251 |
case "static_assert": |
| 3252 |
case "thread_local": |
| 3253 |
case "wchar_t": |
| 3254 |
if (global.params.cplusplus >= CppStdRevision.cpp11) |
| 3255 |
return "keyword in C++11"; |
| 3256 |
return null; |
| 3257 |
|
| 3258 |
// C++20 keywords |
| 3259 |
case "char8_t": |
| 3260 |
case "consteval": |
| 3261 |
case "constinit": |
| 3262 |
// Concepts-related keywords |
| 3263 |
case "concept": |
| 3264 |
case "requires": |
| 3265 |
// Coroutines-related keywords |
| 3266 |
case "co_await": |
| 3267 |
case "co_yield": |
| 3268 |
case "co_return": |
| 3269 |
if (global.params.cplusplus >= CppStdRevision.cpp20) |
| 3270 |
return "keyword in C++20"; |
| 3271 |
return null; |
| 3272 |
case "restrict": |
| 3273 |
case "_Alignas": |
| 3274 |
case "_Alignof": |
| 3275 |
case "_Atomic": |
| 3276 |
case "_Bool": |
| 3277 |
//case "_Complex": // handled above in C++ |
| 3278 |
case "_Generic": |
| 3279 |
case "_Imaginary": |
| 3280 |
case "_Noreturn": |
| 3281 |
case "_Static_assert": |
| 3282 |
case "_Thread_local": |
| 3283 |
case "_assert": |
| 3284 |
case "_import": |
| 3285 |
//case "__...": handled in default case below |
| 3286 |
return "Keyword in C"; |
| 3287 |
|
| 3288 |
default: |
| 3289 |
// Identifiers starting with __ are reserved |
| 3290 |
if (name.length >= 2 && name[0..2] == "__") |
| 3291 |
return "reserved identifier in C++"; |
| 3292 |
|
| 3293 |
return null; |
| 3294 |
} |
| 3295 |
} |
| 3296 |
|
| 3297 |
/// Finds the outermost symbol if `sym` is nested. |
| 3298 |
/// Returns `sym` if it appears at module scope |
| 3299 |
ASTCodegen.Dsymbol outermostSymbol(ASTCodegen.Dsymbol sym) |
| 3300 |
{ |
| 3301 |
assert(sym); |
| 3302 |
while (true) |
| 3303 |
{ |
| 3304 |
auto par = sym.toParent(); |
| 3305 |
if (!par || par.isModule()) |
| 3306 |
return sym; |
| 3307 |
sym = par; |
| 3308 |
} |
| 3309 |
} |
| 3310 |
|
| 3311 |
/// Fetches the symbol for user-defined types from the type `t` |
| 3312 |
/// if `t` is either `TypeClass`, `TypeStruct` or `TypeEnum` |
| 3313 |
ASTCodegen.Dsymbol symbolFromType(ASTCodegen.Type t) @safe |
| 3314 |
{ |
| 3315 |
if (auto tc = t.isTypeClass()) |
| 3316 |
return tc.sym; |
| 3317 |
if (auto ts = t.isTypeStruct()) |
| 3318 |
return ts.sym; |
| 3319 |
if (auto te = t.isTypeEnum()) |
| 3320 |
return te.sym; |
| 3321 |
return null; |
| 3322 |
} |
| 3323 |
|
| 3324 |
/** |
| 3325 |
* Searches `sym` for a member with the given name. |
| 3326 |
* |
| 3327 |
* This method usually delegates to `Dsymbol.search` but might also |
| 3328 |
* manually check the members if the symbol did not receive semantic |
| 3329 |
* analysis. |
| 3330 |
* |
| 3331 |
* Params: |
| 3332 |
* sym = symbol to search |
| 3333 |
* name = identifier of the requested symbol |
| 3334 |
* |
| 3335 |
* Returns: the symbol or `null` if not found |
| 3336 |
*/ |
| 3337 |
ASTCodegen.Dsymbol findMember(ASTCodegen.Dsymbol sym, Identifier name) |
| 3338 |
{ |
| 3339 |
if (auto mem = sym.search(Loc.initial, name, ASTCodegen.SearchOpt.ignoreErrors)) |
| 3340 |
return mem; |
| 3341 |
|
| 3342 |
// search doesn't work for declarations inside of uninstantiated |
| 3343 |
// `TemplateDeclaration`s due to the missing symtab. |
| 3344 |
if (sym.semanticRun >= ASTCodegen.PASS.semanticdone) |
| 3345 |
return null; |
| 3346 |
|
| 3347 |
// Manually check the members if present |
| 3348 |
auto sds = sym.isScopeDsymbol(); |
| 3349 |
if (!sds || !sds.members) |
| 3350 |
return null; |
| 3351 |
|
| 3352 |
/// Recursively searches for `name` without entering nested aggregates, ... |
| 3353 |
static ASTCodegen.Dsymbol search(ASTCodegen.Dsymbols* members, Identifier name) |
| 3354 |
{ |
| 3355 |
foreach (mem; *members) |
| 3356 |
{ |
| 3357 |
if (mem.ident == name) |
| 3358 |
return mem; |
| 3359 |
|
| 3360 |
// Look inside of private:, ... |
| 3361 |
if (auto ad = mem.isAttribDeclaration()) |
| 3362 |
{ |
| 3363 |
if (auto s = search(ad.decl, name)) |
| 3364 |
return s; |
| 3365 |
} |
| 3366 |
} |
| 3367 |
return null; |
| 3368 |
} |
| 3369 |
|
| 3370 |
return search(sds.members, name); |
| 3371 |
} |
| 3372 |
|
| 3373 |
debug (Debug_DtoH) |
| 3374 |
{ |
| 3375 |
/// Generates code to trace the entry and exit of the enclosing `visit` function |
| 3376 |
string traceVisit(alias node)() |
| 3377 |
{ |
| 3378 |
const type = typeof(node).stringof; |
| 3379 |
const method = __traits(hasMember, node, "toPrettyChars") ? "toPrettyChars" : "toChars"; |
| 3380 |
const arg = __traits(identifier, node) ~ '.' ~ method; |
| 3381 |
|
| 3382 |
return `printf("[` ~ type ~ ` enter] %s\n", ` ~ arg ~ `()); |
| 3383 |
scope(exit) printf("[` ~ type ~ ` exit] %s\n", ` ~ arg ~ `());`; |
| 3384 |
} |
| 3385 |
} |