| 1 |
/** |
| 2 |
* Do mangling for C++ linkage. |
| 3 |
* |
| 4 |
* This is the POSIX side of the implementation. |
| 5 |
* It exports two functions to C++, `toCppMangleItanium` and `cppTypeInfoMangleItanium`. |
| 6 |
* |
| 7 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 8 |
* Authors: Walter Bright, https://www.digitalmars.com |
| 9 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 10 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/mangle/cpp.d, _cppmangle.d) |
| 11 |
* Documentation: https://dlang.org/phobos/dmd_cppmangle.html |
| 12 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/mangle/cpp.d |
| 13 |
* |
| 14 |
* References: |
| 15 |
* Follows Itanium C++ ABI 1.86 section 5.1 |
| 16 |
* http://refspecs.linux-foundation.org/cxxabi-1.86.html#mangling |
| 17 |
* which is where the grammar comments come from. |
| 18 |
*/ |
| 19 |
|
| 20 |
module dmd.mangle.cpp; |
| 21 |
|
| 22 |
import core.stdc.stdio; |
| 23 |
|
| 24 |
import dmd.arraytypes; |
| 25 |
import dmd.astenums; |
| 26 |
import dmd.attrib; |
| 27 |
import dmd.declaration; |
| 28 |
import dmd.dsymbol; |
| 29 |
import dmd.dsymbolsem : isGNUABITag; |
| 30 |
import dmd.dtemplate; |
| 31 |
import dmd.errors; |
| 32 |
import dmd.expression; |
| 33 |
import dmd.func; |
| 34 |
import dmd.globals; |
| 35 |
import dmd.id; |
| 36 |
import dmd.identifier; |
| 37 |
import dmd.location; |
| 38 |
import dmd.mtype; |
| 39 |
import dmd.nspace; |
| 40 |
import dmd.root.array; |
| 41 |
import dmd.common.outbuffer; |
| 42 |
import dmd.rootobject; |
| 43 |
import dmd.root.string; |
| 44 |
import dmd.target; |
| 45 |
import dmd.typesem; |
| 46 |
import dmd.visitor; |
| 47 |
|
| 48 |
|
| 49 |
// C++ operators |
| 50 |
enum CppOperator { Unknown, Cast, Assign, Eq, Index, Call, Unary, Binary, OpAssign } |
| 51 |
|
| 52 |
/************** |
| 53 |
* Check if id is a C++ operator |
| 54 |
* Params: |
| 55 |
* id = identifier to be checked |
| 56 |
* Returns: |
| 57 |
* CppOperator, or Unknown if not a C++ operator |
| 58 |
*/ |
| 59 |
package CppOperator isCppOperator(const scope Identifier id) |
| 60 |
{ |
| 61 |
with (Id) with (CppOperator) |
| 62 |
{ |
| 63 |
return (id == opCast) ? Cast : |
| 64 |
(id == opAssign) ? Assign : |
| 65 |
(id == opEquals) ? Eq : |
| 66 |
(id == opIndex) ? Index : |
| 67 |
(id == opCall) ? Call : |
| 68 |
(id == opUnary) ? Unary : |
| 69 |
(id == opBinary) ? Binary : |
| 70 |
(id == opOpAssign) ? OpAssign : |
| 71 |
Unknown ; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/// |
| 76 |
const(char)* toCppMangleItanium(Dsymbol s) |
| 77 |
{ |
| 78 |
//printf("toCppMangleItanium(%s)\n", s.toChars()); |
| 79 |
OutBuffer buf; |
| 80 |
scope CppMangleVisitor v = new CppMangleVisitor(&buf, s.loc); |
| 81 |
v.mangleOf(s); |
| 82 |
if (v.errors) |
| 83 |
fatal(); |
| 84 |
return buf.extractChars(); |
| 85 |
} |
| 86 |
|
| 87 |
/// |
| 88 |
const(char)* cppTypeInfoMangleItanium(Dsymbol s) |
| 89 |
{ |
| 90 |
//printf("cppTypeInfoMangle(%s)\n", s.toChars()); |
| 91 |
OutBuffer buf; |
| 92 |
buf.writestring("_ZTI"); // "TI" means typeinfo structure |
| 93 |
scope CppMangleVisitor v = new CppMangleVisitor(&buf, s.loc); |
| 94 |
v.cpp_mangle_name(s, false); |
| 95 |
if (v.errors) |
| 96 |
fatal(); |
| 97 |
return buf.extractChars(); |
| 98 |
} |
| 99 |
|
| 100 |
/// |
| 101 |
const(char)* cppThunkMangleItanium(FuncDeclaration fd, int offset) |
| 102 |
{ |
| 103 |
//printf("cppThunkMangleItanium(%s)\n", fd.toChars()); |
| 104 |
OutBuffer buf; |
| 105 |
buf.printf("_ZThn%u_", offset); // "Th" means thunk, "n%u" is the call offset |
| 106 |
scope CppMangleVisitor v = new CppMangleVisitor(&buf, fd.loc); |
| 107 |
v.mangle_function_encoding(fd); |
| 108 |
if (v.errors) |
| 109 |
fatal(); |
| 110 |
return buf.extractChars(); |
| 111 |
} |
| 112 |
|
| 113 |
/****************************** |
| 114 |
* Determine if sym is a full aggregate destructor. |
| 115 |
* Params: |
| 116 |
* sym = Dsymbol |
| 117 |
* Returns: |
| 118 |
* true if sym is an aggregate destructor |
| 119 |
*/ |
| 120 |
bool isAggregateDtor(const Dsymbol sym) |
| 121 |
{ |
| 122 |
const dtor = sym.isDtorDeclaration(); |
| 123 |
if (!dtor) |
| 124 |
return false; |
| 125 |
const ad = dtor.isMember(); |
| 126 |
assert(ad); |
| 127 |
return dtor == ad.aggrDtor; |
| 128 |
} |
| 129 |
|
| 130 |
/// Context used when processing pre-semantic AST |
| 131 |
private struct Context |
| 132 |
{ |
| 133 |
/// Template instance of the function being mangled |
| 134 |
TemplateInstance ti; |
| 135 |
/// Function declaration we're mangling |
| 136 |
FuncDeclaration fd; |
| 137 |
/// Current type / expression being processed (semantically analyzed) |
| 138 |
RootObject res; |
| 139 |
|
| 140 |
@disable ref Context opAssign(ref Context other); |
| 141 |
@disable ref Context opAssign(Context other); |
| 142 |
|
| 143 |
/** |
| 144 |
* Helper function to track `res` |
| 145 |
* |
| 146 |
* Params: |
| 147 |
* next = Value to set `this.res` to. |
| 148 |
* If `this.res` is `null`, the expression is not evalutated. |
| 149 |
* This allow this code to be used even when no context is needed. |
| 150 |
* |
| 151 |
* Returns: |
| 152 |
* The previous state of this `Context` object |
| 153 |
*/ |
| 154 |
private Context push(lazy RootObject next) @safe |
| 155 |
{ |
| 156 |
auto r = this.res; |
| 157 |
if (r !is null) |
| 158 |
this.res = next; |
| 159 |
return Context(this.ti, this.fd, r); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Reset the context to a previous one, making any adjustment necessary |
| 164 |
*/ |
| 165 |
private void pop(ref Context prev) @safe |
| 166 |
{ |
| 167 |
this.res = prev.res; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private final class CppMangleVisitor : Visitor |
| 172 |
{ |
| 173 |
/// Context used when processing pre-semantic AST |
| 174 |
private Context context; |
| 175 |
|
| 176 |
ABITagContainer abiTags; /// Container for already-written ABI tags |
| 177 |
Objects components; /// array of components available for substitution |
| 178 |
OutBuffer* buf; /// append the mangling to buf[] |
| 179 |
Loc loc; /// location for use in error messages |
| 180 |
bool errors; /// failed to mangle properly |
| 181 |
|
| 182 |
/** |
| 183 |
* Constructor |
| 184 |
* |
| 185 |
* Params: |
| 186 |
* buf = `OutBuffer` to write the mangling to |
| 187 |
* loc = `Loc` of the symbol being mangled |
| 188 |
*/ |
| 189 |
this(OutBuffer* buf, Loc loc) scope |
| 190 |
{ |
| 191 |
this.buf = buf; |
| 192 |
this.loc = loc; |
| 193 |
} |
| 194 |
|
| 195 |
/***** |
| 196 |
* Entry point. Append mangling to buf[] |
| 197 |
* Params: |
| 198 |
* s = symbol to mangle |
| 199 |
*/ |
| 200 |
void mangleOf(Dsymbol s) |
| 201 |
{ |
| 202 |
if (VarDeclaration vd = s.isVarDeclaration()) |
| 203 |
{ |
| 204 |
mangle_variable(vd, vd.cppnamespace !is null); |
| 205 |
} |
| 206 |
else if (FuncDeclaration fd = s.isFuncDeclaration()) |
| 207 |
{ |
| 208 |
mangle_function(fd); |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
assert(0); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Mangle the return type of a function |
| 218 |
* |
| 219 |
* This is called on a templated function type. |
| 220 |
* Context is set to the `FuncDeclaration`. |
| 221 |
* |
| 222 |
* Params: |
| 223 |
* preSemantic = the `FuncDeclaration`'s `originalType` |
| 224 |
*/ |
| 225 |
void mangleReturnType(TypeFunction preSemantic) |
| 226 |
{ |
| 227 |
auto tf = this.context.res.asFuncDecl().type.isTypeFunction(); |
| 228 |
Type rt = preSemantic.nextOf(); |
| 229 |
// https://issues.dlang.org/show_bug.cgi?id=22739 |
| 230 |
// auto return type means that rt is null. |
| 231 |
// if so, just pick up the type from the instance |
| 232 |
if (!rt) |
| 233 |
rt = tf.nextOf(); |
| 234 |
if (tf.isRef) |
| 235 |
rt = rt.referenceTo(); |
| 236 |
auto prev = this.context.push(tf.nextOf()); |
| 237 |
scope (exit) this.context.pop(prev); |
| 238 |
this.headOfType(rt); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Write a seq-id from an index number, excluding the terminating '_' |
| 243 |
* |
| 244 |
* Params: |
| 245 |
* idx = the index in a substitution list. |
| 246 |
* Note that index 0 has no value, and `S0_` would be the |
| 247 |
* substitution at index 1 in the list. |
| 248 |
* |
| 249 |
* See-Also: |
| 250 |
* https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.seq-id |
| 251 |
*/ |
| 252 |
private void writeSequenceFromIndex(size_t idx) @safe |
| 253 |
{ |
| 254 |
if (idx) |
| 255 |
{ |
| 256 |
void write_seq_id(size_t i) |
| 257 |
{ |
| 258 |
if (i >= 36) |
| 259 |
{ |
| 260 |
write_seq_id(i / 36); |
| 261 |
i %= 36; |
| 262 |
} |
| 263 |
i += (i < 10) ? '0' : 'A' - 10; |
| 264 |
buf.writeByte(cast(char)i); |
| 265 |
} |
| 266 |
|
| 267 |
write_seq_id(idx - 1); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Attempt to perform substitution on `p` |
| 273 |
* |
| 274 |
* If `p` already appeared in the mangling, it is stored as |
| 275 |
* a 'part', and short references in the form of `SX_` can be used. |
| 276 |
* Note that `p` can be anything: template declaration, struct declaration, |
| 277 |
* class declaration, namespace... |
| 278 |
* |
| 279 |
* Params: |
| 280 |
* p = The object to attempt to substitute |
| 281 |
* nested = Whether or not `p` is to be considered nested. |
| 282 |
* When `true`, `N` will be prepended before the substitution. |
| 283 |
* |
| 284 |
* Returns: |
| 285 |
* Whether `p` already appeared in the mangling, |
| 286 |
* and substitution has been written to `this.buf`. |
| 287 |
*/ |
| 288 |
bool substitute(RootObject p, bool nested = false) |
| 289 |
{ |
| 290 |
//printf("substitute %s\n", p ? p.toChars() : null); |
| 291 |
auto i = find(p); |
| 292 |
if (i < 0) |
| 293 |
return false; |
| 294 |
|
| 295 |
//printf("\tmatch\n"); |
| 296 |
/* Sequence is S_, S0_, .., S9_, SA_, ..., SZ_, S10_, ... |
| 297 |
*/ |
| 298 |
if (nested) |
| 299 |
buf.writeByte('N'); |
| 300 |
buf.writeByte('S'); |
| 301 |
writeSequenceFromIndex(i); |
| 302 |
buf.writeByte('_'); |
| 303 |
return true; |
| 304 |
} |
| 305 |
|
| 306 |
/****** |
| 307 |
* See if `p` exists in components[] |
| 308 |
* |
| 309 |
* Note that components can contain `null` entries, |
| 310 |
* as the index used in mangling is based on the index in the array. |
| 311 |
* |
| 312 |
* If called with an object whose dynamic type is `Nspace`, |
| 313 |
* calls the `find(Nspace)` overload. |
| 314 |
* |
| 315 |
* Returns: |
| 316 |
* index if found, -1 if not |
| 317 |
*/ |
| 318 |
int find(RootObject p) |
| 319 |
{ |
| 320 |
//printf("find %p %d %s\n", p, p.dyncast(), p ? p.toChars() : null); |
| 321 |
scope v = new ComponentVisitor(p); |
| 322 |
foreach (i, component; components) |
| 323 |
{ |
| 324 |
if (component) |
| 325 |
component.visitObject(v); |
| 326 |
if (v.result) |
| 327 |
return cast(int)i; |
| 328 |
} |
| 329 |
return -1; |
| 330 |
} |
| 331 |
|
| 332 |
/********************* |
| 333 |
* Append p to components[] |
| 334 |
*/ |
| 335 |
void append(RootObject p) |
| 336 |
{ |
| 337 |
//printf("append %p %d %s\n", p, p.dyncast(), p ? p.toChars() : "null"); |
| 338 |
components.push(p); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Write an identifier preceded by its length |
| 343 |
* |
| 344 |
* Params: |
| 345 |
* ident = `Identifier` to write to `this.buf` |
| 346 |
*/ |
| 347 |
void writeIdentifier(const ref Identifier ident) |
| 348 |
{ |
| 349 |
const name = ident.toString(); |
| 350 |
this.buf.print(name.length); |
| 351 |
this.buf.writestring(name); |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Insert the leftover ABI tags to the buffer |
| 356 |
* |
| 357 |
* This inset ABI tags that hasn't already been written |
| 358 |
* after the mangled name of the function. |
| 359 |
* For more details, see the `abiTags` variable. |
| 360 |
* |
| 361 |
* Params: |
| 362 |
* off = Offset to insert at |
| 363 |
* tf = Type of the function to mangle the return type of |
| 364 |
*/ |
| 365 |
void writeRemainingTags(size_t off, TypeFunction tf) |
| 366 |
{ |
| 367 |
Array!StringExp toWrite; |
| 368 |
leftOver(tf, &this.abiTags.written, &toWrite); |
| 369 |
OutBuffer b2; |
| 370 |
foreach (se; toWrite) |
| 371 |
{ |
| 372 |
auto tag = se.peekString(); |
| 373 |
// We can only insert a slice, and each insert is a memmove, |
| 374 |
// so use a temporary buffer to keep it efficient. |
| 375 |
b2.reset(); |
| 376 |
b2.writestring("B"); |
| 377 |
b2.print(tag.length); |
| 378 |
b2.writestring(tag); |
| 379 |
this.buf.insert(off, b2[]); |
| 380 |
off += b2.length; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
/************************ |
| 385 |
* Determine if symbol is indeed the global ::std namespace. |
| 386 |
* Params: |
| 387 |
* s = symbol to check |
| 388 |
* Returns: |
| 389 |
* true if it is ::std |
| 390 |
*/ |
| 391 |
static bool isStd(Dsymbol s) |
| 392 |
{ |
| 393 |
if (!s) |
| 394 |
return false; |
| 395 |
|
| 396 |
if (auto cnd = s.isCPPNamespaceDeclaration()) |
| 397 |
return isStd(cnd); |
| 398 |
|
| 399 |
return (s.ident == Id.std && // the right name |
| 400 |
s.isNspace() && // g++ disallows global "std" for other than a namespace |
| 401 |
!getQualifier(s)); // at global level |
| 402 |
} |
| 403 |
|
| 404 |
/// Ditto |
| 405 |
static bool isStd(CPPNamespaceDeclaration s) |
| 406 |
{ |
| 407 |
return s && s.cppnamespace is null && s.ident == Id.std; |
| 408 |
} |
| 409 |
|
| 410 |
/************************ |
| 411 |
* Determine if type is a C++ fundamental type. |
| 412 |
* Params: |
| 413 |
* t = type to check |
| 414 |
* Returns: |
| 415 |
* true if it is a fundamental type |
| 416 |
*/ |
| 417 |
static bool isFundamentalType(Type t) |
| 418 |
{ |
| 419 |
// First check the target whether some specific ABI is being followed. |
| 420 |
bool isFundamental = void; |
| 421 |
if (target.cpp.fundamentalType(t, isFundamental)) |
| 422 |
return isFundamental; |
| 423 |
|
| 424 |
if (auto te = t.isTypeEnum()) |
| 425 |
{ |
| 426 |
// Peel off enum type from special types. |
| 427 |
if (te.sym.isSpecial()) |
| 428 |
t = te.memType(); |
| 429 |
} |
| 430 |
|
| 431 |
// Fundamental arithmetic types: |
| 432 |
// 1. integral types: bool, char, int, ... |
| 433 |
// 2. floating point types: float, double, real |
| 434 |
// 3. void |
| 435 |
// 4. null pointer: std::nullptr_t (since C++11) |
| 436 |
if (t.ty == Tvoid || t.ty == Tbool) |
| 437 |
return true; |
| 438 |
if (t.ty == Tnull && global.params.cplusplus >= CppStdRevision.cpp11) |
| 439 |
return true; |
| 440 |
|
| 441 |
return t.isTypeBasic() && (t.isIntegral() || t.isReal()); |
| 442 |
} |
| 443 |
|
| 444 |
/****************************** |
| 445 |
* Write the mangled representation of a template argument. |
| 446 |
* Params: |
| 447 |
* ti = the template instance |
| 448 |
* arg = the template argument index |
| 449 |
*/ |
| 450 |
void template_arg(TemplateInstance ti, size_t arg) |
| 451 |
{ |
| 452 |
TemplateDeclaration td = ti.tempdecl.isTemplateDeclaration(); |
| 453 |
assert(td); |
| 454 |
TemplateParameter tp = (*td.parameters)[arg]; |
| 455 |
RootObject o = (*ti.tiargs)[arg]; |
| 456 |
|
| 457 |
auto prev = this.context.push({ |
| 458 |
TemplateInstance parentti; |
| 459 |
if (this.context.res.dyncast() == DYNCAST.dsymbol) |
| 460 |
parentti = this.context.res.asFuncDecl().parent.isTemplateInstance(); |
| 461 |
else |
| 462 |
{ |
| 463 |
auto parent = this.context.res.asType().toDsymbol(null).parent; |
| 464 |
parentti = parent.isTemplateInstance(); |
| 465 |
// https://issues.dlang.org/show_bug.cgi?id=22760 |
| 466 |
// The template instance may sometimes have the form |
| 467 |
// S1!int.S1, therefore the above instruction might yield null |
| 468 |
if (parentti is null && parent.parent) |
| 469 |
parentti = parent.parent.isTemplateInstance(); |
| 470 |
} |
| 471 |
return (*parentti.tiargs)[arg]; |
| 472 |
}()); |
| 473 |
scope (exit) this.context.pop(prev); |
| 474 |
|
| 475 |
if (tp.isTemplateTypeParameter()) |
| 476 |
{ |
| 477 |
Type t = isType(o); |
| 478 |
assert(t); |
| 479 |
t.accept(this); |
| 480 |
} |
| 481 |
else if (TemplateValueParameter tv = tp.isTemplateValueParameter()) |
| 482 |
{ |
| 483 |
// <expr-primary> ::= L <type> <value number> E # integer literal |
| 484 |
if (tv.valType.isIntegral()) |
| 485 |
{ |
| 486 |
Expression e = isExpression(o); |
| 487 |
assert(e); |
| 488 |
buf.writeByte('L'); |
| 489 |
tv.valType.accept(this); |
| 490 |
auto val = e.toUInteger(); |
| 491 |
if (!tv.valType.isUnsigned() && cast(sinteger_t)val < 0) |
| 492 |
{ |
| 493 |
val = -val; |
| 494 |
buf.writeByte('n'); |
| 495 |
} |
| 496 |
buf.print(val); |
| 497 |
buf.writeByte('E'); |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
.error(ti.loc, "%s `%s` internal compiler error: C++ `%s` template value parameter is not supported", ti.kind, ti.toPrettyChars, tv.valType.toChars()); |
| 502 |
errors = true; |
| 503 |
return; |
| 504 |
} |
| 505 |
} |
| 506 |
else if (tp.isTemplateAliasParameter()) |
| 507 |
{ |
| 508 |
// Passing a function as alias parameter is the same as passing |
| 509 |
// `&function` |
| 510 |
Dsymbol d = isDsymbol(o); |
| 511 |
Expression e = isExpression(o); |
| 512 |
if (d && d.isFuncDeclaration()) |
| 513 |
{ |
| 514 |
// X .. E => template parameter is an expression |
| 515 |
// 'ad' => unary operator ('&') |
| 516 |
// L .. E => is a <expr-primary> |
| 517 |
buf.writestring("XadL"); |
| 518 |
mangle_function(d.isFuncDeclaration()); |
| 519 |
buf.writestring("EE"); |
| 520 |
} |
| 521 |
else if (e && e.isVarExp() && e.isVarExp().var.isVarDeclaration()) |
| 522 |
{ |
| 523 |
VarDeclaration vd = e.isVarExp().var.isVarDeclaration(); |
| 524 |
buf.writeByte('L'); |
| 525 |
mangle_variable(vd, true); |
| 526 |
buf.writeByte('E'); |
| 527 |
} |
| 528 |
else if (d && d.isTemplateDeclaration() && d.isTemplateDeclaration().onemember) |
| 529 |
{ |
| 530 |
if (!substitute(d)) |
| 531 |
{ |
| 532 |
cpp_mangle_name(d, false); |
| 533 |
} |
| 534 |
} |
| 535 |
else |
| 536 |
{ |
| 537 |
.error(ti.loc, "%s `%s` internal compiler error: C++ `%s` template alias parameter is not supported", ti.kind, ti.toPrettyChars, o.toChars()); |
| 538 |
errors = true; |
| 539 |
} |
| 540 |
} |
| 541 |
else if (tp.isTemplateThisParameter()) |
| 542 |
{ |
| 543 |
.error(ti.loc, "%s `%s` internal compiler error: C++ `%s` template this parameter is not supported", ti.kind, ti.toPrettyChars, o.toChars()); |
| 544 |
errors = true; |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
assert(0); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
/****************************** |
| 553 |
* Write the mangled representation of the template arguments. |
| 554 |
* Params: |
| 555 |
* ti = the template instance |
| 556 |
* firstArg = index of the first template argument to mangle |
| 557 |
* (used for operator overloading) |
| 558 |
* Returns: |
| 559 |
* true if any arguments were written |
| 560 |
*/ |
| 561 |
bool template_args(TemplateInstance ti, int firstArg = 0) |
| 562 |
{ |
| 563 |
/* <template-args> ::= I <template-arg>+ E |
| 564 |
*/ |
| 565 |
if (!ti || ti.tiargs.length <= firstArg) // could happen if std::basic_string is not a template |
| 566 |
return false; |
| 567 |
buf.writeByte('I'); |
| 568 |
foreach (i; firstArg .. ti.tiargs.length) |
| 569 |
{ |
| 570 |
TemplateDeclaration td = ti.tempdecl.isTemplateDeclaration(); |
| 571 |
assert(td); |
| 572 |
TemplateParameter tp = (*td.parameters)[i]; |
| 573 |
|
| 574 |
/* |
| 575 |
* <template-arg> ::= <type> # type or template |
| 576 |
* ::= X <expression> E # expression |
| 577 |
* ::= <expr-primary> # simple expressions |
| 578 |
* ::= J <template-arg>* E # argument pack |
| 579 |
* |
| 580 |
* Reference: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.template-arg |
| 581 |
*/ |
| 582 |
if (TemplateTupleParameter tt = tp.isTemplateTupleParameter()) |
| 583 |
{ |
| 584 |
buf.writeByte('J'); // argument pack |
| 585 |
|
| 586 |
// mangle the rest of the arguments as types |
| 587 |
foreach (j; i .. (*ti.tiargs).length) |
| 588 |
{ |
| 589 |
Type t = isType((*ti.tiargs)[j]); |
| 590 |
if (t is null) |
| 591 |
{ |
| 592 |
.error(ti.loc, "%s `%s` internal compiler error: C++ `%s` template value parameter is not supported", ti.kind, ti.toPrettyChars, (*ti.tiargs)[j].toChars()); |
| 593 |
errors = true; |
| 594 |
return false; |
| 595 |
} |
| 596 |
t.accept(this); |
| 597 |
} |
| 598 |
|
| 599 |
buf.writeByte('E'); |
| 600 |
break; |
| 601 |
} |
| 602 |
|
| 603 |
template_arg(ti, i); |
| 604 |
} |
| 605 |
buf.writeByte('E'); |
| 606 |
return true; |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* Write the symbol `p` if not null, then execute the delegate |
| 611 |
* |
| 612 |
* Params: |
| 613 |
* p = Symbol to write |
| 614 |
* dg = Delegate to execute |
| 615 |
*/ |
| 616 |
void writeChained(Dsymbol p, scope void delegate() dg) |
| 617 |
{ |
| 618 |
if (p && !p.isModule()) |
| 619 |
{ |
| 620 |
buf.writestring("N"); |
| 621 |
source_name(p, true); |
| 622 |
dg(); |
| 623 |
buf.writestring("E"); |
| 624 |
} |
| 625 |
else |
| 626 |
dg(); |
| 627 |
} |
| 628 |
|
| 629 |
/** |
| 630 |
* Write the name of `s` to the buffer |
| 631 |
* |
| 632 |
* Params: |
| 633 |
* s = Symbol to write the name of |
| 634 |
* haveNE = Whether `N..E` is already part of the mangling |
| 635 |
* Because `Nspace` and `CPPNamespaceAttribute` can be |
| 636 |
* mixed, this is a mandatory hack. |
| 637 |
*/ |
| 638 |
void source_name(Dsymbol s, bool haveNE = false) |
| 639 |
{ |
| 640 |
version (none) |
| 641 |
{ |
| 642 |
printf("source_name(%s)\n", s.toChars()); |
| 643 |
auto sl = this.buf.peekSlice(); |
| 644 |
assert(sl.length == 0 || haveNE || s.cppnamespace is null || sl != "_ZN"); |
| 645 |
} |
| 646 |
auto ti = s.isTemplateInstance(); |
| 647 |
|
| 648 |
if (!ti) |
| 649 |
{ |
| 650 |
auto ag = s.isAggregateDeclaration(); |
| 651 |
const ident = (ag && ag.pMangleOverride) ? ag.pMangleOverride.id : s.ident; |
| 652 |
this.writeNamespace(s.cppnamespace, () { |
| 653 |
this.writeIdentifier(ident); |
| 654 |
this.abiTags.writeSymbol(s, this); |
| 655 |
}, |
| 656 |
haveNE); |
| 657 |
return; |
| 658 |
} |
| 659 |
|
| 660 |
bool needsTa = false; |
| 661 |
|
| 662 |
// https://issues.dlang.org/show_bug.cgi?id=20413 |
| 663 |
// N..E is not needed when substituting members of the std namespace. |
| 664 |
// This is observed in the GCC and Clang implementations. |
| 665 |
// The Itanium specification is not clear enough on this specific case. |
| 666 |
// References: |
| 667 |
// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.name |
| 668 |
// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-compression |
| 669 |
Dsymbol q = getQualifier(ti.tempdecl); |
| 670 |
Dsymbol ns = ti.tempdecl.cppnamespace; |
| 671 |
const inStd = ns && isStd(ns) || q && isStd(q); |
| 672 |
const isNested = !inStd && (ns || q); |
| 673 |
|
| 674 |
if (substitute(ti.tempdecl, !haveNE && isNested)) |
| 675 |
{ |
| 676 |
template_args(ti); |
| 677 |
if (!haveNE && isNested) |
| 678 |
buf.writeByte('E'); |
| 679 |
return; |
| 680 |
} |
| 681 |
else if (this.writeStdSubstitution(ti, needsTa)) |
| 682 |
{ |
| 683 |
this.abiTags.writeSymbol(ti, this); |
| 684 |
if (needsTa) |
| 685 |
template_args(ti); |
| 686 |
return; |
| 687 |
} |
| 688 |
|
| 689 |
auto ag = ti.aliasdecl ? ti.aliasdecl.isAggregateDeclaration() : null; |
| 690 |
if (ag && ag.pMangleOverride) |
| 691 |
{ |
| 692 |
this.writeNamespace( |
| 693 |
ti.toAlias().cppnamespace, () { |
| 694 |
this.writeIdentifier(ag.pMangleOverride.id); |
| 695 |
if (ag.pMangleOverride.agg && ag.pMangleOverride.agg.isInstantiated()) |
| 696 |
{ |
| 697 |
auto to = ag.pMangleOverride.agg.isInstantiated(); |
| 698 |
append(to); |
| 699 |
this.abiTags.writeSymbol(to.tempdecl, this); |
| 700 |
template_args(to); |
| 701 |
} |
| 702 |
}, haveNE); |
| 703 |
} |
| 704 |
else |
| 705 |
{ |
| 706 |
this.writeNamespace( |
| 707 |
s.cppnamespace, () { |
| 708 |
this.writeIdentifier(ti.tempdecl.toAlias().ident); |
| 709 |
append(ti.tempdecl); |
| 710 |
this.abiTags.writeSymbol(ti.tempdecl, this); |
| 711 |
template_args(ti); |
| 712 |
}, haveNE); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
/******** |
| 717 |
* See if s is actually an instance of a template |
| 718 |
* Params: |
| 719 |
* s = symbol |
| 720 |
* Returns: |
| 721 |
* if s is instance of a template, return the instance, otherwise return s |
| 722 |
*/ |
| 723 |
static Dsymbol getInstance(Dsymbol s) |
| 724 |
{ |
| 725 |
if (Dsymbol p = s.toParent()) |
| 726 |
{ |
| 727 |
if (TemplateInstance ti = p.isTemplateInstance()) |
| 728 |
return ti; |
| 729 |
} |
| 730 |
return s; |
| 731 |
} |
| 732 |
|
| 733 |
/// Get the namespace of a template instance |
| 734 |
CPPNamespaceDeclaration getTiNamespace(TemplateInstance ti) |
| 735 |
{ |
| 736 |
// If we receive a pre-semantic `TemplateInstance`, |
| 737 |
// `cppnamespace` is always `null` |
| 738 |
return ti.tempdecl ? ti.cppnamespace |
| 739 |
: this.context.res.asType().toDsymbol(null).cppnamespace; |
| 740 |
} |
| 741 |
|
| 742 |
/******** |
| 743 |
* Get qualifier for `s`, meaning the symbol |
| 744 |
* that s is in the symbol table of. |
| 745 |
* The module does not count as a qualifier, because C++ |
| 746 |
* does not have modules. |
| 747 |
* Params: |
| 748 |
* s = symbol that may have a qualifier |
| 749 |
* s is rewritten to be TemplateInstance if s is one |
| 750 |
* Returns: |
| 751 |
* qualifier, null if none |
| 752 |
*/ |
| 753 |
static Dsymbol getQualifier(Dsymbol s) |
| 754 |
{ |
| 755 |
Dsymbol p = s.toParent(); |
| 756 |
return (p && !p.isModule()) ? p : null; |
| 757 |
} |
| 758 |
|
| 759 |
// Detect type char |
| 760 |
static bool isChar(RootObject o) |
| 761 |
{ |
| 762 |
Type t = isType(o); |
| 763 |
return (t && t.equals(Type.tchar)); |
| 764 |
} |
| 765 |
|
| 766 |
// Detect type ::std::char_traits<char> |
| 767 |
bool isChar_traits_char(RootObject o) |
| 768 |
{ |
| 769 |
return isIdent_char(Id.char_traits, o); |
| 770 |
} |
| 771 |
|
| 772 |
// Detect type ::std::allocator<char> |
| 773 |
bool isAllocator_char(RootObject o) |
| 774 |
{ |
| 775 |
return isIdent_char(Id.allocator, o); |
| 776 |
} |
| 777 |
|
| 778 |
// Detect type ::std::ident<char> |
| 779 |
bool isIdent_char(Identifier ident, RootObject o) |
| 780 |
{ |
| 781 |
Type t = isType(o); |
| 782 |
if (!t || !t.isTypeStruct()) |
| 783 |
return false; |
| 784 |
Dsymbol s = t.toDsymbol(null); |
| 785 |
if (s.ident != ident) |
| 786 |
return false; |
| 787 |
Dsymbol p = s.toParent(); |
| 788 |
if (!p) |
| 789 |
return false; |
| 790 |
TemplateInstance ti = p.isTemplateInstance(); |
| 791 |
if (!ti) |
| 792 |
return false; |
| 793 |
Dsymbol q = getQualifier(ti); |
| 794 |
const bool inStd = isStd(q) || isStd(this.getTiNamespace(ti)); |
| 795 |
return inStd && ti.tiargs.length == 1 && isChar((*ti.tiargs)[0]); |
| 796 |
} |
| 797 |
|
| 798 |
/*** |
| 799 |
* Detect template args <char, ::std::char_traits<char>> |
| 800 |
* and write st if found. |
| 801 |
* Returns: |
| 802 |
* true if found |
| 803 |
*/ |
| 804 |
bool char_std_char_traits_char(TemplateInstance ti, string st) |
| 805 |
{ |
| 806 |
if (ti.tiargs.length == 2 && |
| 807 |
isChar((*ti.tiargs)[0]) && |
| 808 |
isChar_traits_char((*ti.tiargs)[1])) |
| 809 |
{ |
| 810 |
buf.writestring(st.ptr); |
| 811 |
return true; |
| 812 |
} |
| 813 |
return false; |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
void prefix_name(Dsymbol s) |
| 818 |
{ |
| 819 |
//printf("prefix_name(%s)\n", s.toChars()); |
| 820 |
if (substitute(s)) |
| 821 |
return; |
| 822 |
if (isStd(s)) |
| 823 |
return buf.writestring("St"); |
| 824 |
|
| 825 |
auto si = getInstance(s); |
| 826 |
if (Dsymbol p = getQualifier(si)) |
| 827 |
{ |
| 828 |
if (isStd(p)) |
| 829 |
{ |
| 830 |
bool needsTa; |
| 831 |
auto ti = si.isTemplateInstance(); |
| 832 |
if (this.writeStdSubstitution(ti, needsTa)) |
| 833 |
{ |
| 834 |
this.abiTags.writeSymbol(ti, this); |
| 835 |
if (needsTa) |
| 836 |
{ |
| 837 |
template_args(ti); |
| 838 |
append(ti); |
| 839 |
} |
| 840 |
return; |
| 841 |
} |
| 842 |
buf.writestring("St"); |
| 843 |
} |
| 844 |
else |
| 845 |
prefix_name(p); |
| 846 |
} |
| 847 |
source_name(si, true); |
| 848 |
if (!isStd(si)) |
| 849 |
/* Do this after the source_name() call to keep components[] |
| 850 |
* in the right order. |
| 851 |
* https://issues.dlang.org/show_bug.cgi?id=17947 |
| 852 |
*/ |
| 853 |
append(si); |
| 854 |
} |
| 855 |
|
| 856 |
/** |
| 857 |
* Write common substitution for standard types, such as std::allocator |
| 858 |
* |
| 859 |
* This function assumes that the symbol `ti` is in the namespace `std`. |
| 860 |
* |
| 861 |
* Params: |
| 862 |
* ti = Template instance to consider |
| 863 |
* needsTa = If this function returns `true`, this value indicates |
| 864 |
* if additional template argument mangling is needed |
| 865 |
* |
| 866 |
* Returns: |
| 867 |
* `true` if a special std symbol was found |
| 868 |
*/ |
| 869 |
bool writeStdSubstitution(TemplateInstance ti, out bool needsTa) |
| 870 |
{ |
| 871 |
if (!ti) |
| 872 |
return false; |
| 873 |
if (!isStd(this.getTiNamespace(ti)) && !isStd(getQualifier(ti))) |
| 874 |
return false; |
| 875 |
|
| 876 |
if (ti.name == Id.allocator) |
| 877 |
{ |
| 878 |
buf.writestring("Sa"); |
| 879 |
needsTa = true; |
| 880 |
return true; |
| 881 |
} |
| 882 |
if (ti.name == Id.basic_string) |
| 883 |
{ |
| 884 |
// ::std::basic_string<char, ::std::char_traits<char>, ::std::allocator<char>> |
| 885 |
if (ti.tiargs.length == 3 && |
| 886 |
isChar((*ti.tiargs)[0]) && |
| 887 |
isChar_traits_char((*ti.tiargs)[1]) && |
| 888 |
isAllocator_char((*ti.tiargs)[2])) |
| 889 |
|
| 890 |
{ |
| 891 |
buf.writestring("Ss"); |
| 892 |
return true; |
| 893 |
} |
| 894 |
buf.writestring("Sb"); // ::std::basic_string |
| 895 |
needsTa = true; |
| 896 |
return true; |
| 897 |
} |
| 898 |
|
| 899 |
// ::std::basic_istream<char, ::std::char_traits<char>> |
| 900 |
if (ti.name == Id.basic_istream && |
| 901 |
char_std_char_traits_char(ti, "Si")) |
| 902 |
return true; |
| 903 |
|
| 904 |
// ::std::basic_ostream<char, ::std::char_traits<char>> |
| 905 |
if (ti.name == Id.basic_ostream && |
| 906 |
char_std_char_traits_char(ti, "So")) |
| 907 |
return true; |
| 908 |
|
| 909 |
// ::std::basic_iostream<char, ::std::char_traits<char>> |
| 910 |
if (ti.name == Id.basic_iostream && |
| 911 |
char_std_char_traits_char(ti, "Sd")) |
| 912 |
return true; |
| 913 |
|
| 914 |
return false; |
| 915 |
} |
| 916 |
|
| 917 |
void cpp_mangle_name(Dsymbol s, bool qualified) |
| 918 |
{ |
| 919 |
//printf("cpp_mangle_name(%s, %d)\n", s.toChars(), qualified); |
| 920 |
Dsymbol p = s.toParent(); |
| 921 |
Dsymbol se = s; |
| 922 |
bool write_prefix = true; |
| 923 |
if (p && p.isTemplateInstance()) |
| 924 |
{ |
| 925 |
se = p; |
| 926 |
if (find(p.isTemplateInstance().tempdecl) >= 0) |
| 927 |
write_prefix = false; |
| 928 |
p = p.toParent(); |
| 929 |
} |
| 930 |
if (!p || p.isModule()) |
| 931 |
{ |
| 932 |
source_name(se, false); |
| 933 |
append(s); |
| 934 |
return; |
| 935 |
} |
| 936 |
|
| 937 |
if (!isStd(p) || qualified) |
| 938 |
{ |
| 939 |
buf.writeByte('N'); |
| 940 |
if (write_prefix) |
| 941 |
{ |
| 942 |
if (isStd(p)) |
| 943 |
buf.writestring("St"); |
| 944 |
else |
| 945 |
prefix_name(p); |
| 946 |
} |
| 947 |
source_name(se, true); |
| 948 |
buf.writeByte('E'); |
| 949 |
append(s); |
| 950 |
return; |
| 951 |
} |
| 952 |
/* The N..E is not required if: |
| 953 |
* 1. the parent is 'std' |
| 954 |
* 2. 'std' is the initial qualifier |
| 955 |
* 3. there is no CV-qualifier or a ref-qualifier for a member function |
| 956 |
* ABI 5.1.8 |
| 957 |
*/ |
| 958 |
TemplateInstance ti = se.isTemplateInstance(); |
| 959 |
if (s.ident == Id.allocator) |
| 960 |
{ |
| 961 |
buf.writestring("Sa"); // "Sa" is short for ::std::allocator |
| 962 |
template_args(ti); |
| 963 |
} |
| 964 |
else if (s.ident == Id.basic_string) |
| 965 |
{ |
| 966 |
// ::std::basic_string<char, ::std::char_traits<char>, ::std::allocator<char>> |
| 967 |
if (ti.tiargs.length == 3 && |
| 968 |
isChar((*ti.tiargs)[0]) && |
| 969 |
isChar_traits_char((*ti.tiargs)[1]) && |
| 970 |
isAllocator_char((*ti.tiargs)[2])) |
| 971 |
{ |
| 972 |
buf.writestring("Ss"); |
| 973 |
return; |
| 974 |
} |
| 975 |
buf.writestring("Sb"); // ::std::basic_string |
| 976 |
template_args(ti); |
| 977 |
} |
| 978 |
else |
| 979 |
{ |
| 980 |
// ::std::basic_istream<char, ::std::char_traits<char>> |
| 981 |
if (s.ident == Id.basic_istream) |
| 982 |
{ |
| 983 |
if (char_std_char_traits_char(ti, "Si")) |
| 984 |
return; |
| 985 |
} |
| 986 |
else if (s.ident == Id.basic_ostream) |
| 987 |
{ |
| 988 |
if (char_std_char_traits_char(ti, "So")) |
| 989 |
return; |
| 990 |
} |
| 991 |
else if (s.ident == Id.basic_iostream) |
| 992 |
{ |
| 993 |
if (char_std_char_traits_char(ti, "Sd")) |
| 994 |
return; |
| 995 |
} |
| 996 |
buf.writestring("St"); |
| 997 |
source_name(se, true); |
| 998 |
} |
| 999 |
append(s); |
| 1000 |
} |
| 1001 |
|
| 1002 |
/** |
| 1003 |
* Write CV-qualifiers to the buffer |
| 1004 |
* |
| 1005 |
* CV-qualifiers are 'r': restrict (unused in D), 'V': volatile, 'K': const |
| 1006 |
* |
| 1007 |
* See_Also: |
| 1008 |
* https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.CV-qualifiers |
| 1009 |
*/ |
| 1010 |
void CV_qualifiers(const Type t) |
| 1011 |
{ |
| 1012 |
if (t.isConst()) |
| 1013 |
buf.writeByte('K'); |
| 1014 |
} |
| 1015 |
|
| 1016 |
/** |
| 1017 |
* Mangles a variable |
| 1018 |
* |
| 1019 |
* Params: |
| 1020 |
* d = Variable declaration to mangle |
| 1021 |
* isNested = Whether this variable is nested, e.g. a template parameter |
| 1022 |
* or within a namespace |
| 1023 |
*/ |
| 1024 |
void mangle_variable(VarDeclaration d, bool isNested) |
| 1025 |
{ |
| 1026 |
// fake mangling for fields to fix https://issues.dlang.org/show_bug.cgi?id=16525 |
| 1027 |
if (!(d.storage_class & (STC.extern_ | STC.field | STC.gshared))) |
| 1028 |
{ |
| 1029 |
.error(d.loc, "%s `%s` internal compiler error: C++ static non-`__gshared` non-`extern` variables not supported", d.kind, d.toPrettyChars); |
| 1030 |
errors = true; |
| 1031 |
return; |
| 1032 |
} |
| 1033 |
Dsymbol p = d.toParent(); |
| 1034 |
if (p && !p.isModule()) //for example: char Namespace1::beta[6] should be mangled as "_ZN10Namespace14betaE" |
| 1035 |
{ |
| 1036 |
buf.writestring("_ZN"); |
| 1037 |
prefix_name(p); |
| 1038 |
source_name(d, true); |
| 1039 |
buf.writeByte('E'); |
| 1040 |
} |
| 1041 |
else if (isNested) |
| 1042 |
{ |
| 1043 |
buf.writestring("_Z"); |
| 1044 |
source_name(d, false); |
| 1045 |
} |
| 1046 |
else |
| 1047 |
{ |
| 1048 |
if (auto varTags = ABITagContainer.forSymbol(d)) |
| 1049 |
{ |
| 1050 |
buf.writestring("_Z"); |
| 1051 |
source_name(d, false); |
| 1052 |
return; |
| 1053 |
} |
| 1054 |
if (auto typeTags = ABITagContainer.forSymbol(d.type.toDsymbol(null))) |
| 1055 |
{ |
| 1056 |
buf.writestring("_Z"); |
| 1057 |
source_name(d, false); |
| 1058 |
this.abiTags.write(*this.buf, typeTags); |
| 1059 |
return; |
| 1060 |
} |
| 1061 |
//char beta[6] should mangle as "beta" |
| 1062 |
buf.writestring(d.ident.toString()); |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
void mangle_function(FuncDeclaration d) |
| 1067 |
{ |
| 1068 |
//printf("mangle_function(%s)\n", d.toChars()); |
| 1069 |
/* |
| 1070 |
* <mangled-name> ::= _Z <encoding> |
| 1071 |
*/ |
| 1072 |
buf.writestring("_Z"); |
| 1073 |
this.mangle_function_encoding(d); |
| 1074 |
} |
| 1075 |
|
| 1076 |
void mangle_function_encoding(FuncDeclaration d) |
| 1077 |
{ |
| 1078 |
//printf("mangle_function_encoding(%s)\n", d.toChars()); |
| 1079 |
/* |
| 1080 |
* <encoding> ::= <function name> <bare-function-type> |
| 1081 |
* ::= <data name> |
| 1082 |
* ::= <special-name> |
| 1083 |
*/ |
| 1084 |
TypeFunction tf = d.type.isTypeFunction(); |
| 1085 |
|
| 1086 |
if (TemplateDeclaration ftd = getFuncTemplateDecl(d)) |
| 1087 |
{ |
| 1088 |
/* It's an instance of a function template |
| 1089 |
*/ |
| 1090 |
TemplateInstance ti = d.parent.isTemplateInstance(); |
| 1091 |
assert(ti); |
| 1092 |
this.mangleTemplatedFunction(d, tf, ftd, ti); |
| 1093 |
return; |
| 1094 |
} |
| 1095 |
|
| 1096 |
Dsymbol p = d.toParent(); |
| 1097 |
if (p && !p.isModule() && tf.linkage == LINK.cpp) |
| 1098 |
{ |
| 1099 |
this.mangleNestedFuncPrefix(tf, p); |
| 1100 |
|
| 1101 |
if (auto ctor = d.isCtorDeclaration()) |
| 1102 |
buf.writestring(ctor.isCpCtor ? "C2" : "C1"); |
| 1103 |
else if (d.isAggregateDtor()) |
| 1104 |
buf.writestring("D1"); |
| 1105 |
else if (d.ident && d.ident == Id.opAssign) |
| 1106 |
buf.writestring("aS"); |
| 1107 |
else if (d.ident && d.ident == Id.opEquals) |
| 1108 |
buf.writestring("eq"); |
| 1109 |
else if (d.ident && d.ident == Id.opIndex) |
| 1110 |
buf.writestring("ix"); |
| 1111 |
else if (d.ident && d.ident == Id.opCall) |
| 1112 |
buf.writestring("cl"); |
| 1113 |
else |
| 1114 |
source_name(d, true); |
| 1115 |
buf.writeByte('E'); |
| 1116 |
} |
| 1117 |
else |
| 1118 |
{ |
| 1119 |
source_name(d, false); |
| 1120 |
} |
| 1121 |
|
| 1122 |
// Save offset for potentially writing tags |
| 1123 |
const size_t off = this.buf.length(); |
| 1124 |
|
| 1125 |
// Template args accept extern "C" symbols with special mangling |
| 1126 |
if (tf.linkage == LINK.cpp) |
| 1127 |
mangleFunctionParameters(tf.parameterList); |
| 1128 |
|
| 1129 |
if (!tf.next.isTypeBasic()) |
| 1130 |
this.writeRemainingTags(off, tf); |
| 1131 |
} |
| 1132 |
|
| 1133 |
/** |
| 1134 |
* Recursively mangles a non-scoped namespace |
| 1135 |
* |
| 1136 |
* Parameters: |
| 1137 |
* ns = Namespace to mangle |
| 1138 |
* dg = A delegate to write the identifier in this namespace |
| 1139 |
* haveNE = When `false` (the default), surround the namespace / dg |
| 1140 |
* call with nested name qualifier (`N..E`). |
| 1141 |
* Otherwise, they are already present (e.g. `Nspace` was used). |
| 1142 |
*/ |
| 1143 |
void writeNamespace(CPPNamespaceDeclaration ns, scope void delegate() dg, |
| 1144 |
bool haveNE = false) |
| 1145 |
{ |
| 1146 |
void runDg () { if (dg !is null) dg(); } |
| 1147 |
|
| 1148 |
if (ns is null || ns.ident is null) |
| 1149 |
return runDg(); |
| 1150 |
|
| 1151 |
if (isStd(ns)) |
| 1152 |
{ |
| 1153 |
if (!substitute(ns)) |
| 1154 |
buf.writestring("St"); |
| 1155 |
runDg(); |
| 1156 |
} |
| 1157 |
else if (dg !is null) |
| 1158 |
{ |
| 1159 |
if (!haveNE) |
| 1160 |
buf.writestring("N"); |
| 1161 |
if (!substitute(ns)) |
| 1162 |
{ |
| 1163 |
this.writeNamespace(ns.cppnamespace, null); |
| 1164 |
this.writeIdentifier(ns.ident); |
| 1165 |
append(ns); |
| 1166 |
} |
| 1167 |
dg(); |
| 1168 |
if (!haveNE) |
| 1169 |
buf.writestring("E"); |
| 1170 |
} |
| 1171 |
else if (!substitute(ns)) |
| 1172 |
{ |
| 1173 |
this.writeNamespace(ns.cppnamespace, null); |
| 1174 |
this.writeIdentifier(ns.ident); |
| 1175 |
append(ns); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
/** |
| 1180 |
* Mangles a function template to C++ |
| 1181 |
* |
| 1182 |
* Params: |
| 1183 |
* d = Function declaration |
| 1184 |
* tf = Function type (casted d.type) |
| 1185 |
* ftd = Template declaration (ti.templdecl) |
| 1186 |
* ti = Template instance (d.parent) |
| 1187 |
*/ |
| 1188 |
void mangleTemplatedFunction(FuncDeclaration d, TypeFunction tf, |
| 1189 |
TemplateDeclaration ftd, TemplateInstance ti) |
| 1190 |
{ |
| 1191 |
Dsymbol p = ti.toParent(); |
| 1192 |
// Check if this function is *not* nested |
| 1193 |
if (!p || p.isModule() || tf.linkage != LINK.cpp) |
| 1194 |
{ |
| 1195 |
this.context.ti = ti; |
| 1196 |
this.context.fd = d; |
| 1197 |
this.context.res = d; |
| 1198 |
TypeFunction preSemantic = d.originalType.isTypeFunction(); |
| 1199 |
auto nspace = ti.toParent(); |
| 1200 |
if (nspace && nspace.isNspace()) |
| 1201 |
this.writeChained(ti.toParent(), () => source_name(ti, true)); |
| 1202 |
else |
| 1203 |
source_name(ti, false); |
| 1204 |
this.mangleReturnType(preSemantic); |
| 1205 |
this.mangleFunctionParameters(ParameterList(preSemantic.parameterList.parameters, tf.parameterList.varargs)); |
| 1206 |
return; |
| 1207 |
} |
| 1208 |
|
| 1209 |
// It's a nested function (e.g. a member of an aggregate) |
| 1210 |
this.mangleNestedFuncPrefix(tf, p); |
| 1211 |
|
| 1212 |
if (d.isCtorDeclaration()) |
| 1213 |
{ |
| 1214 |
buf.writestring("C1"); |
| 1215 |
mangleFunctionParameters(tf.parameterList); |
| 1216 |
return; |
| 1217 |
} |
| 1218 |
else if (d.isAggregateDtor()) |
| 1219 |
{ |
| 1220 |
buf.writestring("D1"); |
| 1221 |
mangleFunctionParameters(tf.parameterList); |
| 1222 |
return; |
| 1223 |
} |
| 1224 |
|
| 1225 |
int firstTemplateArg = 0; |
| 1226 |
bool appendReturnType = true; |
| 1227 |
bool isConvertFunc = false; |
| 1228 |
string symName; |
| 1229 |
|
| 1230 |
// test for special symbols |
| 1231 |
CppOperator whichOp = isCppOperator(ti.name); |
| 1232 |
final switch (whichOp) |
| 1233 |
{ |
| 1234 |
case CppOperator.Unknown: |
| 1235 |
break; |
| 1236 |
case CppOperator.Cast: |
| 1237 |
symName = "cv"; |
| 1238 |
firstTemplateArg = 1; |
| 1239 |
isConvertFunc = true; |
| 1240 |
appendReturnType = false; |
| 1241 |
break; |
| 1242 |
case CppOperator.Assign: |
| 1243 |
symName = "aS"; |
| 1244 |
break; |
| 1245 |
case CppOperator.Eq: |
| 1246 |
symName = "eq"; |
| 1247 |
break; |
| 1248 |
case CppOperator.Index: |
| 1249 |
symName = "ix"; |
| 1250 |
break; |
| 1251 |
case CppOperator.Call: |
| 1252 |
symName = "cl"; |
| 1253 |
break; |
| 1254 |
case CppOperator.Unary: |
| 1255 |
case CppOperator.Binary: |
| 1256 |
case CppOperator.OpAssign: |
| 1257 |
TemplateDeclaration td = ti.tempdecl.isTemplateDeclaration(); |
| 1258 |
assert(td); |
| 1259 |
assert(ti.tiargs.length >= 1); |
| 1260 |
TemplateParameter tp = (*td.parameters)[0]; |
| 1261 |
TemplateValueParameter tv = tp.isTemplateValueParameter(); |
| 1262 |
if (!tv || !tv.valType.isString()) |
| 1263 |
break; // expecting a string argument to operators! |
| 1264 |
Expression exp = (*ti.tiargs)[0].isExpression(); |
| 1265 |
StringExp str = exp.toStringExp(); |
| 1266 |
switch (whichOp) |
| 1267 |
{ |
| 1268 |
case CppOperator.Unary: |
| 1269 |
switch (str.peekString()) |
| 1270 |
{ |
| 1271 |
case "*": symName = "de"; goto continue_template; |
| 1272 |
case "++": symName = "pp"; goto continue_template; |
| 1273 |
case "--": symName = "mm"; goto continue_template; |
| 1274 |
case "-": symName = "ng"; goto continue_template; |
| 1275 |
case "+": symName = "ps"; goto continue_template; |
| 1276 |
case "~": symName = "co"; goto continue_template; |
| 1277 |
default: break; |
| 1278 |
} |
| 1279 |
break; |
| 1280 |
case CppOperator.Binary: |
| 1281 |
switch (str.peekString()) |
| 1282 |
{ |
| 1283 |
case ">>": symName = "rs"; goto continue_template; |
| 1284 |
case "<<": symName = "ls"; goto continue_template; |
| 1285 |
case "*": symName = "ml"; goto continue_template; |
| 1286 |
case "-": symName = "mi"; goto continue_template; |
| 1287 |
case "+": symName = "pl"; goto continue_template; |
| 1288 |
case "&": symName = "an"; goto continue_template; |
| 1289 |
case "/": symName = "dv"; goto continue_template; |
| 1290 |
case "%": symName = "rm"; goto continue_template; |
| 1291 |
case "^": symName = "eo"; goto continue_template; |
| 1292 |
case "|": symName = "or"; goto continue_template; |
| 1293 |
default: break; |
| 1294 |
} |
| 1295 |
break; |
| 1296 |
case CppOperator.OpAssign: |
| 1297 |
switch (str.peekString()) |
| 1298 |
{ |
| 1299 |
case "*": symName = "mL"; goto continue_template; |
| 1300 |
case "+": symName = "pL"; goto continue_template; |
| 1301 |
case "-": symName = "mI"; goto continue_template; |
| 1302 |
case "/": symName = "dV"; goto continue_template; |
| 1303 |
case "%": symName = "rM"; goto continue_template; |
| 1304 |
case ">>": symName = "rS"; goto continue_template; |
| 1305 |
case "<<": symName = "lS"; goto continue_template; |
| 1306 |
case "&": symName = "aN"; goto continue_template; |
| 1307 |
case "|": symName = "oR"; goto continue_template; |
| 1308 |
case "^": symName = "eO"; goto continue_template; |
| 1309 |
default: break; |
| 1310 |
} |
| 1311 |
break; |
| 1312 |
default: |
| 1313 |
assert(0); |
| 1314 |
continue_template: |
| 1315 |
firstTemplateArg = 1; |
| 1316 |
break; |
| 1317 |
} |
| 1318 |
break; |
| 1319 |
} |
| 1320 |
if (symName.length == 0) |
| 1321 |
source_name(ti, true); |
| 1322 |
else |
| 1323 |
{ |
| 1324 |
buf.writestring(symName); |
| 1325 |
if (isConvertFunc) |
| 1326 |
template_arg(ti, 0); |
| 1327 |
appendReturnType = template_args(ti, firstTemplateArg) && appendReturnType; |
| 1328 |
} |
| 1329 |
buf.writeByte('E'); |
| 1330 |
if (appendReturnType) |
| 1331 |
headOfType(tf.nextOf()); // mangle return type |
| 1332 |
mangleFunctionParameters(tf.parameterList); |
| 1333 |
} |
| 1334 |
|
| 1335 |
/** |
| 1336 |
* Mangle the parameters of a function |
| 1337 |
* |
| 1338 |
* For templated functions, `context.res` is set to the `FuncDeclaration` |
| 1339 |
* |
| 1340 |
* Params: |
| 1341 |
* parameters = Array of `Parameter` to mangle |
| 1342 |
* varargs = if != 0, this function has varargs parameters |
| 1343 |
*/ |
| 1344 |
void mangleFunctionParameters(ParameterList parameterList) |
| 1345 |
{ |
| 1346 |
int numparams = 0; |
| 1347 |
|
| 1348 |
foreach (n, fparam; parameterList) |
| 1349 |
{ |
| 1350 |
Type t = fparam.type.merge2(); |
| 1351 |
if (fparam.isReference()) |
| 1352 |
t = t.referenceTo(); |
| 1353 |
else if (fparam.isLazy()) |
| 1354 |
{ |
| 1355 |
// Mangle as delegate |
| 1356 |
auto tf = new TypeFunction(ParameterList(), t, LINK.d); |
| 1357 |
auto td = new TypeDelegate(tf); |
| 1358 |
t = td.merge(); |
| 1359 |
} |
| 1360 |
else if (Type cpptype = target.cpp.parameterType(t)) |
| 1361 |
t = cpptype; |
| 1362 |
if (t.ty == Tsarray) |
| 1363 |
{ |
| 1364 |
// Static arrays in D are passed by value; no counterpart in C++ |
| 1365 |
.error(loc, "internal compiler error: unable to pass static array `%s` to extern(C++) function, use pointer instead", |
| 1366 |
t.toChars()); |
| 1367 |
errors = true; |
| 1368 |
return; |
| 1369 |
} |
| 1370 |
auto prev = this.context.push({ |
| 1371 |
TypeFunction tf; |
| 1372 |
if (isDsymbol(this.context.res)) |
| 1373 |
tf = this.context.res.asFuncDecl().type.isTypeFunction(); |
| 1374 |
else |
| 1375 |
tf = this.context.res.asType().isTypeFunction(); |
| 1376 |
assert(tf); |
| 1377 |
return (*tf.parameterList.parameters)[n].type; |
| 1378 |
}()); |
| 1379 |
scope (exit) this.context.pop(prev); |
| 1380 |
|
| 1381 |
if (this.context.ti && global.params.cplusplus >= CppStdRevision.cpp11) |
| 1382 |
handleParamPack(t, this.context.ti.tempdecl.isTemplateDeclaration().parameters); |
| 1383 |
|
| 1384 |
headOfType(t); |
| 1385 |
++numparams; |
| 1386 |
} |
| 1387 |
|
| 1388 |
if (parameterList.varargs == VarArg.variadic) |
| 1389 |
buf.writeByte('z'); |
| 1390 |
else if (!numparams) |
| 1391 |
buf.writeByte('v'); // encode (void) parameters |
| 1392 |
} |
| 1393 |
|
| 1394 |
/****** The rest is type mangling ************/ |
| 1395 |
|
| 1396 |
void error(Type t) |
| 1397 |
{ |
| 1398 |
const(char)* p; |
| 1399 |
if (t.isImmutable()) |
| 1400 |
p = "`immutable` "; |
| 1401 |
else if (t.isShared()) |
| 1402 |
p = "`shared` "; |
| 1403 |
else |
| 1404 |
p = ""; |
| 1405 |
.error(loc, "internal compiler error: %stype `%s` cannot be mapped to C++\n", p, t.toChars()); |
| 1406 |
errors = true; //Fatal, because this error should be handled in frontend |
| 1407 |
} |
| 1408 |
|
| 1409 |
/**************************** |
| 1410 |
* Mangle a type, |
| 1411 |
* treating it as a Head followed by a Tail. |
| 1412 |
* Params: |
| 1413 |
* t = Head of a type |
| 1414 |
*/ |
| 1415 |
void headOfType(Type t) |
| 1416 |
{ |
| 1417 |
if (auto tc = t.isTypeClass()) |
| 1418 |
{ |
| 1419 |
mangleTypeClass(tc, true); |
| 1420 |
} |
| 1421 |
else |
| 1422 |
{ |
| 1423 |
// For value types, strip const/immutable/shared from the head of the type |
| 1424 |
auto prev = this.context.push(this.context.res.asType().mutableOf().unSharedOf()); |
| 1425 |
scope (exit) this.context.pop(prev); |
| 1426 |
t.mutableOf().unSharedOf().accept(this); |
| 1427 |
} |
| 1428 |
} |
| 1429 |
|
| 1430 |
/****** |
| 1431 |
* Write out 1 or 2 character basic type mangling. |
| 1432 |
* Handle const and substitutions. |
| 1433 |
* Params: |
| 1434 |
* t = type to mangle |
| 1435 |
* p = if not 0, then character prefix |
| 1436 |
* c = mangling character |
| 1437 |
*/ |
| 1438 |
void writeBasicType(Type t, char p, char c) |
| 1439 |
{ |
| 1440 |
// Only do substitutions for non-fundamental types. |
| 1441 |
if (!isFundamentalType(t) || t.isConst()) |
| 1442 |
{ |
| 1443 |
if (substitute(t)) |
| 1444 |
return; |
| 1445 |
else |
| 1446 |
append(t); |
| 1447 |
} |
| 1448 |
CV_qualifiers(t); |
| 1449 |
if (p) |
| 1450 |
buf.writeByte(p); |
| 1451 |
buf.writeByte(c); |
| 1452 |
} |
| 1453 |
|
| 1454 |
|
| 1455 |
/**************** |
| 1456 |
* Write structs and enums. |
| 1457 |
* Params: |
| 1458 |
* t = TypeStruct or TypeEnum |
| 1459 |
*/ |
| 1460 |
void doSymbol(Type t) |
| 1461 |
{ |
| 1462 |
if (substitute(t)) |
| 1463 |
return; |
| 1464 |
CV_qualifiers(t); |
| 1465 |
|
| 1466 |
// Handle any target-specific struct types. |
| 1467 |
if (auto tm = target.cpp.typeMangle(t)) |
| 1468 |
{ |
| 1469 |
buf.writestring(tm); |
| 1470 |
} |
| 1471 |
else |
| 1472 |
{ |
| 1473 |
Dsymbol s = t.toDsymbol(null); |
| 1474 |
Dsymbol p = s.toParent(); |
| 1475 |
if (p && p.isTemplateInstance()) |
| 1476 |
{ |
| 1477 |
/* https://issues.dlang.org/show_bug.cgi?id=17947 |
| 1478 |
* Substitute the template instance symbol, not the struct/enum symbol |
| 1479 |
*/ |
| 1480 |
if (substitute(p)) |
| 1481 |
return; |
| 1482 |
} |
| 1483 |
if (!substitute(s)) |
| 1484 |
cpp_mangle_name(s, false); |
| 1485 |
} |
| 1486 |
if (t.isConst()) |
| 1487 |
append(t); |
| 1488 |
} |
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
/************************ |
| 1493 |
* Mangle a class type. |
| 1494 |
* If it's the head, treat the initial pointer as a value type. |
| 1495 |
* Params: |
| 1496 |
* t = class type |
| 1497 |
* head = true for head of a type |
| 1498 |
*/ |
| 1499 |
void mangleTypeClass(TypeClass t, bool head) |
| 1500 |
{ |
| 1501 |
if (t.isImmutable() || t.isShared()) |
| 1502 |
return error(t); |
| 1503 |
|
| 1504 |
/* Mangle as a <pointer to><struct> |
| 1505 |
*/ |
| 1506 |
if (substitute(t)) |
| 1507 |
return; |
| 1508 |
if (!head) |
| 1509 |
CV_qualifiers(t); |
| 1510 |
buf.writeByte('P'); |
| 1511 |
|
| 1512 |
CV_qualifiers(t); |
| 1513 |
|
| 1514 |
{ |
| 1515 |
Dsymbol s = t.toDsymbol(null); |
| 1516 |
Dsymbol p = s.toParent(); |
| 1517 |
if (p && p.isTemplateInstance()) |
| 1518 |
{ |
| 1519 |
/* https://issues.dlang.org/show_bug.cgi?id=17947 |
| 1520 |
* Substitute the template instance symbol, not the class symbol |
| 1521 |
*/ |
| 1522 |
if (substitute(p)) |
| 1523 |
return; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
if (!substitute(t.sym)) |
| 1528 |
{ |
| 1529 |
cpp_mangle_name(t.sym, false); |
| 1530 |
} |
| 1531 |
if (t.isConst()) |
| 1532 |
append(null); // C++ would have an extra type here |
| 1533 |
append(t); |
| 1534 |
} |
| 1535 |
|
| 1536 |
/** |
| 1537 |
* Mangle the prefix of a nested (e.g. member) function |
| 1538 |
* |
| 1539 |
* Params: |
| 1540 |
* tf = Type of the nested function |
| 1541 |
* parent = Parent in which the function is nested |
| 1542 |
*/ |
| 1543 |
void mangleNestedFuncPrefix(TypeFunction tf, Dsymbol parent) |
| 1544 |
{ |
| 1545 |
/* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
| 1546 |
* ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 1547 |
*/ |
| 1548 |
buf.writeByte('N'); |
| 1549 |
CV_qualifiers(tf); |
| 1550 |
|
| 1551 |
/* <prefix> ::= <prefix> <unqualified-name> |
| 1552 |
* ::= <template-prefix> <template-args> |
| 1553 |
* ::= <template-param> |
| 1554 |
* ::= # empty |
| 1555 |
* ::= <substitution> |
| 1556 |
* ::= <prefix> <data-member-prefix> |
| 1557 |
*/ |
| 1558 |
prefix_name(parent); |
| 1559 |
} |
| 1560 |
|
| 1561 |
/** |
| 1562 |
* Write `Dp` (C++11 function parameter pack prefix) if 't' is a TemplateSequenceParameter (T...). |
| 1563 |
* |
| 1564 |
* Params: |
| 1565 |
* t = Parameter type |
| 1566 |
* params = Template parameters of the function |
| 1567 |
*/ |
| 1568 |
private void handleParamPack(Type t, TemplateParameters* params) |
| 1569 |
{ |
| 1570 |
if (t.isTypeReference()) |
| 1571 |
t = t.nextOf(); |
| 1572 |
auto ti = t.isTypeIdentifier(); |
| 1573 |
if (!ti) |
| 1574 |
return; |
| 1575 |
|
| 1576 |
auto idx = templateParamIndex(ti.ident, params); |
| 1577 |
if (idx < params.length && (*params)[idx].isTemplateTupleParameter()) |
| 1578 |
buf.writestring("Dp"); |
| 1579 |
} |
| 1580 |
|
| 1581 |
/** |
| 1582 |
* Helper function to write a `T..._` template index. |
| 1583 |
* |
| 1584 |
* Params: |
| 1585 |
* idx = Index of `param` in the template argument list |
| 1586 |
* param = Template parameter to mangle |
| 1587 |
*/ |
| 1588 |
private void writeTemplateArgIndex(size_t idx, TemplateParameter param) |
| 1589 |
{ |
| 1590 |
// expressions are mangled in <X..E> |
| 1591 |
if (param.isTemplateValueParameter()) |
| 1592 |
buf.writeByte('X'); |
| 1593 |
buf.writeByte('T'); |
| 1594 |
writeSequenceFromIndex(idx); |
| 1595 |
buf.writeByte('_'); |
| 1596 |
if (param.isTemplateValueParameter()) |
| 1597 |
buf.writeByte('E'); |
| 1598 |
} |
| 1599 |
|
| 1600 |
/** |
| 1601 |
* Given an array of template parameters and an identifier, |
| 1602 |
* returns the index of the identifier in that array. |
| 1603 |
* |
| 1604 |
* Params: |
| 1605 |
* ident = Identifier for which substitution is attempted |
| 1606 |
* (e.g. `void func(T)(T param)` => `T` from `T param`) |
| 1607 |
* params = `TemplateParameters` of the enclosing symbol |
| 1608 |
* (in the previous example, `func`'s template parameters) |
| 1609 |
* |
| 1610 |
* Returns: |
| 1611 |
* The index of the identifier match in `params`, |
| 1612 |
* or `params.length` if there wasn't any match. |
| 1613 |
*/ |
| 1614 |
private static size_t templateParamIndex( |
| 1615 |
const ref Identifier ident, TemplateParameters* params) @safe |
| 1616 |
{ |
| 1617 |
foreach (idx, param; *params) |
| 1618 |
if (param.ident == ident) |
| 1619 |
return idx; |
| 1620 |
return params.length; |
| 1621 |
} |
| 1622 |
|
| 1623 |
/** |
| 1624 |
* Given a template instance `t`, write its qualified name |
| 1625 |
* without the template parameter list |
| 1626 |
* |
| 1627 |
* Params: |
| 1628 |
* t = Post-parsing `TemplateInstance` pointing to the symbol |
| 1629 |
* to mangle (one level deep) |
| 1630 |
* dg = Delegate to execute after writing the qualified symbol |
| 1631 |
* |
| 1632 |
*/ |
| 1633 |
private void writeQualified(TemplateInstance t, scope void delegate() dg) |
| 1634 |
{ |
| 1635 |
auto type = isType(this.context.res); |
| 1636 |
if (!type) |
| 1637 |
{ |
| 1638 |
this.writeIdentifier(t.name); |
| 1639 |
return dg(); |
| 1640 |
} |
| 1641 |
auto sym1 = type.toDsymbol(null); |
| 1642 |
if (!sym1) |
| 1643 |
{ |
| 1644 |
this.writeIdentifier(t.name); |
| 1645 |
return dg(); |
| 1646 |
} |
| 1647 |
// Get the template instance |
| 1648 |
auto sym = getQualifier(sym1); |
| 1649 |
auto sym2 = getQualifier(sym); |
| 1650 |
if (sym2 && isStd(sym2)) // Nspace path |
| 1651 |
{ |
| 1652 |
bool unused; |
| 1653 |
assert(sym.isTemplateInstance()); |
| 1654 |
if (this.writeStdSubstitution(sym.isTemplateInstance(), unused)) |
| 1655 |
return dg(); |
| 1656 |
// std names don't require `N..E` |
| 1657 |
buf.writestring("St"); |
| 1658 |
this.writeIdentifier(t.name); |
| 1659 |
this.append(t); |
| 1660 |
return dg(); |
| 1661 |
} |
| 1662 |
else if (sym2) |
| 1663 |
{ |
| 1664 |
buf.writestring("N"); |
| 1665 |
if (!this.substitute(sym2)) |
| 1666 |
sym2.accept(this); |
| 1667 |
} |
| 1668 |
this.writeNamespace( |
| 1669 |
sym1.cppnamespace, () { |
| 1670 |
this.writeIdentifier(t.name); |
| 1671 |
this.append(t); |
| 1672 |
dg(); |
| 1673 |
}); |
| 1674 |
if (sym2) |
| 1675 |
buf.writestring("E"); |
| 1676 |
} |
| 1677 |
|
| 1678 |
extern(C++): |
| 1679 |
|
| 1680 |
alias visit = Visitor.visit; |
| 1681 |
|
| 1682 |
override void visit(TypeNull t) |
| 1683 |
{ |
| 1684 |
if (t.isImmutable() || t.isShared()) |
| 1685 |
return error(t); |
| 1686 |
|
| 1687 |
writeBasicType(t, 'D', 'n'); |
| 1688 |
} |
| 1689 |
|
| 1690 |
override void visit(TypeNoreturn t) |
| 1691 |
{ |
| 1692 |
if (t.isImmutable() || t.isShared()) |
| 1693 |
return error(t); |
| 1694 |
|
| 1695 |
writeBasicType(t, 0, 'v'); // mangle like `void` |
| 1696 |
} |
| 1697 |
|
| 1698 |
override void visit(TypeBasic t) |
| 1699 |
{ |
| 1700 |
if (t.isImmutable() || t.isShared()) |
| 1701 |
return error(t); |
| 1702 |
|
| 1703 |
// Handle any target-specific basic types. |
| 1704 |
if (auto tm = target.cpp.typeMangle(t)) |
| 1705 |
{ |
| 1706 |
// Only do substitutions for non-fundamental types. |
| 1707 |
if (!isFundamentalType(t) || t.isConst()) |
| 1708 |
{ |
| 1709 |
if (substitute(t)) |
| 1710 |
return; |
| 1711 |
else |
| 1712 |
append(t); |
| 1713 |
} |
| 1714 |
CV_qualifiers(t); |
| 1715 |
buf.writestring(tm); |
| 1716 |
return; |
| 1717 |
} |
| 1718 |
|
| 1719 |
/* <builtin-type>: |
| 1720 |
* v void |
| 1721 |
* w wchar_t |
| 1722 |
* b bool |
| 1723 |
* c char |
| 1724 |
* a signed char |
| 1725 |
* h unsigned char |
| 1726 |
* s short |
| 1727 |
* t unsigned short |
| 1728 |
* i int |
| 1729 |
* j unsigned int |
| 1730 |
* l long |
| 1731 |
* m unsigned long |
| 1732 |
* x long long, __int64 |
| 1733 |
* y unsigned long long, __int64 |
| 1734 |
* n __int128 |
| 1735 |
* o unsigned __int128 |
| 1736 |
* f float |
| 1737 |
* d double |
| 1738 |
* e long double, __float80 |
| 1739 |
* g __float128 |
| 1740 |
* z ellipsis |
| 1741 |
* Dd 64 bit IEEE 754r decimal floating point |
| 1742 |
* De 128 bit IEEE 754r decimal floating point |
| 1743 |
* Df 32 bit IEEE 754r decimal floating point |
| 1744 |
* Dh 16 bit IEEE 754r half-precision floating point |
| 1745 |
* Di char32_t |
| 1746 |
* Ds char16_t |
| 1747 |
* u <source-name> # vendor extended type |
| 1748 |
*/ |
| 1749 |
if (t.isImaginary() || t.isComplex()) |
| 1750 |
{ |
| 1751 |
// https://issues.dlang.org/show_bug.cgi?id=22806 |
| 1752 |
// Complex and imaginary types are represented in the same way as |
| 1753 |
// arrays or vectors in C++. First substitute the outer type, then |
| 1754 |
// write out the mangle string of the underlying type. |
| 1755 |
if (substitute(t)) |
| 1756 |
return; |
| 1757 |
append(t); |
| 1758 |
CV_qualifiers(t); |
| 1759 |
|
| 1760 |
if (t.isImaginary()) |
| 1761 |
buf.writeByte('G'); // 'G' means imaginary |
| 1762 |
else |
| 1763 |
buf.writeByte('C'); // 'C' means complex |
| 1764 |
|
| 1765 |
switch (t.ty) |
| 1766 |
{ |
| 1767 |
case Timaginary32: |
| 1768 |
case Tcomplex32: |
| 1769 |
return Type.tfloat32.accept(this); |
| 1770 |
case Timaginary64: |
| 1771 |
case Tcomplex64: |
| 1772 |
return Type.tfloat64.accept(this); |
| 1773 |
case Timaginary80: |
| 1774 |
case Tcomplex80: |
| 1775 |
return Type.tfloat80.accept(this); |
| 1776 |
default: |
| 1777 |
assert(0); |
| 1778 |
} |
| 1779 |
} |
| 1780 |
|
| 1781 |
char c; |
| 1782 |
char p = 0; |
| 1783 |
switch (t.ty) |
| 1784 |
{ |
| 1785 |
case Tvoid: c = 'v'; break; |
| 1786 |
case Tint8: c = 'a'; break; |
| 1787 |
case Tuns8: c = 'h'; break; |
| 1788 |
case Tint16: c = 's'; break; |
| 1789 |
case Tuns16: c = 't'; break; |
| 1790 |
case Tint32: c = 'i'; break; |
| 1791 |
case Tuns32: c = 'j'; break; |
| 1792 |
case Tfloat32: c = 'f'; break; |
| 1793 |
case Tint64: |
| 1794 |
c = target.c.longsize == 8 ? 'l' : 'x'; |
| 1795 |
break; |
| 1796 |
case Tuns64: |
| 1797 |
c = target.c.longsize == 8 ? 'm' : 'y'; |
| 1798 |
break; |
| 1799 |
case Tint128: c = 'n'; break; |
| 1800 |
case Tuns128: c = 'o'; break; |
| 1801 |
case Tfloat64: c = 'd'; break; |
| 1802 |
case Tfloat80: c = 'e'; break; |
| 1803 |
case Tbool: c = 'b'; break; |
| 1804 |
case Tchar: c = 'c'; break; |
| 1805 |
case Twchar: p = 'D'; c = 's'; break; // since C++11 |
| 1806 |
case Tdchar: p = 'D'; c = 'i'; break; // since C++11 |
| 1807 |
|
| 1808 |
default: |
| 1809 |
return error(t); |
| 1810 |
} |
| 1811 |
writeBasicType(t, p, c); |
| 1812 |
} |
| 1813 |
|
| 1814 |
override void visit(TypeVector t) |
| 1815 |
{ |
| 1816 |
if (t.isImmutable() || t.isShared()) |
| 1817 |
return error(t); |
| 1818 |
|
| 1819 |
if (substitute(t)) |
| 1820 |
return; |
| 1821 |
append(t); |
| 1822 |
CV_qualifiers(t); |
| 1823 |
|
| 1824 |
// Handle any target-specific vector types. |
| 1825 |
if (auto tm = target.cpp.typeMangle(t)) |
| 1826 |
{ |
| 1827 |
buf.writestring(tm); |
| 1828 |
} |
| 1829 |
else |
| 1830 |
{ |
| 1831 |
assert(t.basetype && t.basetype.ty == Tsarray); |
| 1832 |
auto tsa = t.basetype.isTypeSArray(); |
| 1833 |
assert(tsa.dim); |
| 1834 |
buf.writestring("Dv"); // -- Gnu ABI v.4 |
| 1835 |
buf.print(tsa.dim.toInteger()); |
| 1836 |
buf.writeByte('_'); |
| 1837 |
t.basetype.nextOf().accept(this); |
| 1838 |
} |
| 1839 |
} |
| 1840 |
|
| 1841 |
override void visit(TypeSArray t) |
| 1842 |
{ |
| 1843 |
if (t.isImmutable() || t.isShared()) |
| 1844 |
return error(t); |
| 1845 |
|
| 1846 |
if (!substitute(t)) |
| 1847 |
append(t); |
| 1848 |
CV_qualifiers(t); |
| 1849 |
buf.writeByte('A'); |
| 1850 |
buf.print(t.dim ? t.dim.toInteger() : 0); |
| 1851 |
buf.writeByte('_'); |
| 1852 |
t.next.accept(this); |
| 1853 |
} |
| 1854 |
|
| 1855 |
override void visit(TypePointer t) |
| 1856 |
{ |
| 1857 |
if (t.isImmutable() || t.isShared()) |
| 1858 |
return error(t); |
| 1859 |
|
| 1860 |
// Check for const - Since we cannot represent C++'s `char* const`, |
| 1861 |
// and `const char* const` (a.k.a `const(char*)` in D) is mangled |
| 1862 |
// the same as `const char*` (`const(char)*` in D), we need to add |
| 1863 |
// an extra `K` if `nextOf()` is `const`, before substitution |
| 1864 |
CV_qualifiers(t); |
| 1865 |
if (substitute(t)) |
| 1866 |
return; |
| 1867 |
buf.writeByte('P'); |
| 1868 |
auto prev = this.context.push(this.context.res.asType().nextOf()); |
| 1869 |
scope (exit) this.context.pop(prev); |
| 1870 |
t.next.accept(this); |
| 1871 |
append(t); |
| 1872 |
} |
| 1873 |
|
| 1874 |
override void visit(TypeReference t) |
| 1875 |
{ |
| 1876 |
if (substitute(t)) |
| 1877 |
return; |
| 1878 |
buf.writeByte('R'); |
| 1879 |
CV_qualifiers(t.nextOf()); |
| 1880 |
headOfType(t.nextOf()); |
| 1881 |
if (t.nextOf().isConst()) |
| 1882 |
append(t.nextOf()); |
| 1883 |
append(t); |
| 1884 |
} |
| 1885 |
|
| 1886 |
override void visit(TypeFunction t) |
| 1887 |
{ |
| 1888 |
/* |
| 1889 |
* <function-type> ::= F [Y] <bare-function-type> E |
| 1890 |
* <bare-function-type> ::= <signature type>+ |
| 1891 |
* # types are possible return type, then parameter types |
| 1892 |
*/ |
| 1893 |
/* ABI says: |
| 1894 |
"The type of a non-static member function is considered to be different, |
| 1895 |
for the purposes of substitution, from the type of a namespace-scope or |
| 1896 |
static member function whose type appears similar. The types of two |
| 1897 |
non-static member functions are considered to be different, for the |
| 1898 |
purposes of substitution, if the functions are members of different |
| 1899 |
classes. In other words, for the purposes of substitution, the class of |
| 1900 |
which the function is a member is considered part of the type of |
| 1901 |
function." |
| 1902 |
|
| 1903 |
BUG: Right now, types of functions are never merged, so our simplistic |
| 1904 |
component matcher always finds them to be different. |
| 1905 |
We should use Type.equals on these, and use different |
| 1906 |
TypeFunctions for non-static member functions, and non-static |
| 1907 |
member functions of different classes. |
| 1908 |
*/ |
| 1909 |
if (substitute(t)) |
| 1910 |
return; |
| 1911 |
buf.writeByte('F'); |
| 1912 |
if (t.linkage == LINK.c) |
| 1913 |
buf.writeByte('Y'); |
| 1914 |
Type tn = t.next; |
| 1915 |
if (t.isRef) |
| 1916 |
tn = tn.referenceTo(); |
| 1917 |
tn.accept(this); |
| 1918 |
mangleFunctionParameters(t.parameterList); |
| 1919 |
buf.writeByte('E'); |
| 1920 |
append(t); |
| 1921 |
} |
| 1922 |
|
| 1923 |
override void visit(TypeStruct t) |
| 1924 |
{ |
| 1925 |
if (t.isImmutable() || t.isShared()) |
| 1926 |
return error(t); |
| 1927 |
//printf("TypeStruct %s\n", t.toChars()); |
| 1928 |
doSymbol(t); |
| 1929 |
} |
| 1930 |
|
| 1931 |
override void visit(TypeEnum t) |
| 1932 |
{ |
| 1933 |
if (t.isImmutable() || t.isShared()) |
| 1934 |
return error(t); |
| 1935 |
|
| 1936 |
/* __c_(u)long(long) and others get special mangling |
| 1937 |
*/ |
| 1938 |
const id = t.sym.ident; |
| 1939 |
//printf("enum id = '%s'\n", id.toChars()); |
| 1940 |
if (id == Id.__c_long) |
| 1941 |
return writeBasicType(t, 0, 'l'); |
| 1942 |
if (id == Id.__c_ulong) |
| 1943 |
return writeBasicType(t, 0, 'm'); |
| 1944 |
if (id == Id.__c_char) |
| 1945 |
return writeBasicType(t, 0, 'c'); |
| 1946 |
if (id == Id.__c_wchar_t) |
| 1947 |
return writeBasicType(t, 0, 'w'); |
| 1948 |
if (id == Id.__c_longlong) |
| 1949 |
return writeBasicType(t, 0, 'x'); |
| 1950 |
if (id == Id.__c_ulonglong) |
| 1951 |
return writeBasicType(t, 0, 'y'); |
| 1952 |
if (id == Id.__c_complex_float) |
| 1953 |
return Type.tcomplex32.accept(this); |
| 1954 |
if (id == Id.__c_complex_double) |
| 1955 |
return Type.tcomplex64.accept(this); |
| 1956 |
if (id == Id.__c_complex_real) |
| 1957 |
return Type.tcomplex80.accept(this); |
| 1958 |
|
| 1959 |
doSymbol(t); |
| 1960 |
} |
| 1961 |
|
| 1962 |
override void visit(TypeClass t) |
| 1963 |
{ |
| 1964 |
mangleTypeClass(t, false); |
| 1965 |
} |
| 1966 |
|
| 1967 |
/** |
| 1968 |
* Performs template parameter substitution |
| 1969 |
* |
| 1970 |
* Mangling is performed on a copy of the post-parsing AST before |
| 1971 |
* any semantic pass is run. |
| 1972 |
* There is no easy way to link a type to the template parameters |
| 1973 |
* once semantic has run, because: |
| 1974 |
* - the `TemplateInstance` installs aliases in its scope to its params |
| 1975 |
* - `AliasDeclaration`s are resolved in many places |
| 1976 |
* - semantic passes are destructive, so the `TypeIdentifier` gets lost |
| 1977 |
* |
| 1978 |
* As a result, the best approach with the current architecture is to: |
| 1979 |
* - Run the visitor on the `originalType` of the function, |
| 1980 |
* looking up any `TypeIdentifier` at the template scope when found. |
| 1981 |
* - Fallback to the post-semantic `TypeFunction` when the identifier is |
| 1982 |
* not a template parameter. |
| 1983 |
*/ |
| 1984 |
override void visit(TypeIdentifier t) |
| 1985 |
{ |
| 1986 |
auto decl = this.context.ti.tempdecl.isTemplateDeclaration(); |
| 1987 |
assert(decl.parameters !is null); |
| 1988 |
auto idx = templateParamIndex(t.ident, decl.parameters); |
| 1989 |
// If not found, default to the post-semantic type |
| 1990 |
if (idx >= decl.parameters.length) |
| 1991 |
return this.context.res.visitObject(this); |
| 1992 |
|
| 1993 |
auto param = (*decl.parameters)[idx]; |
| 1994 |
if (auto type = this.context.res.isType()) |
| 1995 |
CV_qualifiers(type); |
| 1996 |
// Otherwise, attempt substitution (`S_` takes precedence on `T_`) |
| 1997 |
if (this.substitute(param)) |
| 1998 |
return; |
| 1999 |
|
| 2000 |
// If substitution failed, write `TX_` where `X` is the index |
| 2001 |
this.writeTemplateArgIndex(idx, param); |
| 2002 |
this.append(param); |
| 2003 |
// Write the ABI tags, if any |
| 2004 |
if (auto sym = this.context.res.isDsymbol()) |
| 2005 |
this.abiTags.writeSymbol(sym, this); |
| 2006 |
} |
| 2007 |
|
| 2008 |
/// Ditto |
| 2009 |
override void visit(TypeInstance t) |
| 2010 |
{ |
| 2011 |
assert(t.tempinst !is null); |
| 2012 |
t.tempinst.accept(this); |
| 2013 |
} |
| 2014 |
|
| 2015 |
/** |
| 2016 |
* Mangles a `TemplateInstance` |
| 2017 |
* |
| 2018 |
* A `TemplateInstance` can be found either in the parameter, |
| 2019 |
* or the return value. |
| 2020 |
* Arguments to the template instance needs to be mangled but the template |
| 2021 |
* can be partially substituted, so for example the following: |
| 2022 |
* `Container!(T, Val) func16479_12 (alias Container, T, int Val) ()` |
| 2023 |
* will mangle the return value part to "T_IT0_XT1_EE" |
| 2024 |
*/ |
| 2025 |
override void visit(TemplateInstance t) |
| 2026 |
{ |
| 2027 |
// Template names are substituted, but args still need to be written |
| 2028 |
void writeArgs () |
| 2029 |
{ |
| 2030 |
buf.writeByte('I'); |
| 2031 |
// When visiting the arguments, the context will be set to the |
| 2032 |
// resolved type |
| 2033 |
auto analyzed_ti = this.context.res.asType().toDsymbol(null).isInstantiated(); |
| 2034 |
auto prev = this.context; |
| 2035 |
scope (exit) this.context.pop(prev); |
| 2036 |
foreach (idx, RootObject o; *t.tiargs) |
| 2037 |
{ |
| 2038 |
this.context.res = (*analyzed_ti.tiargs)[idx]; |
| 2039 |
o.visitObject(this); |
| 2040 |
} |
| 2041 |
if (analyzed_ti.tiargs.length > t.tiargs.length) |
| 2042 |
{ |
| 2043 |
// If the resolved AST has more args than the parse one, |
| 2044 |
// we have default arguments |
| 2045 |
auto oparams = analyzed_ti.tempdecl.isTemplateDeclaration().origParameters; |
| 2046 |
foreach (idx, arg; (*oparams)[t.tiargs.length .. $]) |
| 2047 |
{ |
| 2048 |
this.context.res = (*analyzed_ti.tiargs)[idx + t.tiargs.length]; |
| 2049 |
|
| 2050 |
if (auto ttp = arg.isTemplateTypeParameter()) |
| 2051 |
ttp.defaultType.accept(this); |
| 2052 |
else if (auto tvp = arg.isTemplateValueParameter()) |
| 2053 |
tvp.defaultValue.accept(this); |
| 2054 |
else if (auto tvp = arg.isTemplateThisParameter()) |
| 2055 |
tvp.defaultType.accept(this); |
| 2056 |
else if (auto tvp = arg.isTemplateAliasParameter()) |
| 2057 |
tvp.defaultAlias.visitObject(this); |
| 2058 |
else |
| 2059 |
assert(0, arg.toString()); |
| 2060 |
} |
| 2061 |
} |
| 2062 |
buf.writeByte('E'); |
| 2063 |
} |
| 2064 |
|
| 2065 |
// `name` is used, not `ident` |
| 2066 |
assert(t.name !is null); |
| 2067 |
assert(t.tiargs !is null); |
| 2068 |
|
| 2069 |
bool needsTa; |
| 2070 |
auto decl = this.context.ti.tempdecl.isTemplateDeclaration(); |
| 2071 |
// Attempt to substitute the template itself |
| 2072 |
auto idx = templateParamIndex(t.name, decl.parameters); |
| 2073 |
if (idx < decl.parameters.length) |
| 2074 |
{ |
| 2075 |
auto param = (*decl.parameters)[idx]; |
| 2076 |
if (auto type = t.getType()) |
| 2077 |
CV_qualifiers(type); |
| 2078 |
if (this.substitute(param)) |
| 2079 |
return; |
| 2080 |
this.writeTemplateArgIndex(idx, param); |
| 2081 |
this.append(param); |
| 2082 |
writeArgs(); |
| 2083 |
} |
| 2084 |
else if (this.writeStdSubstitution(t, needsTa)) |
| 2085 |
{ |
| 2086 |
if (needsTa) |
| 2087 |
writeArgs(); |
| 2088 |
} |
| 2089 |
else if (!this.substitute(t)) |
| 2090 |
this.writeQualified(t, &writeArgs); |
| 2091 |
} |
| 2092 |
|
| 2093 |
/// Ditto |
| 2094 |
override void visit(IntegerExp t) |
| 2095 |
{ |
| 2096 |
this.buf.writeByte('L'); |
| 2097 |
t.type.accept(this); |
| 2098 |
this.buf.print(t.getInteger()); |
| 2099 |
this.buf.writeByte('E'); |
| 2100 |
} |
| 2101 |
|
| 2102 |
override void visit(Nspace t) |
| 2103 |
{ |
| 2104 |
if (auto p = getQualifier(t)) |
| 2105 |
p.accept(this); |
| 2106 |
|
| 2107 |
if (isStd(t)) |
| 2108 |
buf.writestring("St"); |
| 2109 |
else |
| 2110 |
{ |
| 2111 |
this.writeIdentifier(t.ident); |
| 2112 |
this.append(t); |
| 2113 |
} |
| 2114 |
} |
| 2115 |
|
| 2116 |
override void visit(Type t) |
| 2117 |
{ |
| 2118 |
error(t); |
| 2119 |
} |
| 2120 |
|
| 2121 |
void visit(Tuple t) |
| 2122 |
{ |
| 2123 |
assert(0); |
| 2124 |
} |
| 2125 |
} |
| 2126 |
|
| 2127 |
/// Helper code to visit `RootObject`, as it doesn't define `accept`, |
| 2128 |
/// only its direct subtypes do. |
| 2129 |
private void visitObject(V : Visitor)(RootObject o, V this_) |
| 2130 |
{ |
| 2131 |
assert(o !is null); |
| 2132 |
if (Type ta = isType(o)) |
| 2133 |
ta.accept(this_); |
| 2134 |
else if (Expression ea = isExpression(o)) |
| 2135 |
ea.accept(this_); |
| 2136 |
else if (Dsymbol sa = isDsymbol(o)) |
| 2137 |
sa.accept(this_); |
| 2138 |
else if (TemplateParameter t = isTemplateParameter(o)) |
| 2139 |
t.accept(this_); |
| 2140 |
else if (Tuple t = isTuple(o)) |
| 2141 |
// `Tuple` inherits `RootObject` and does not define accept |
| 2142 |
// For this reason, this uses static dispatch on the visitor |
| 2143 |
this_.visit(t); |
| 2144 |
else |
| 2145 |
assert(0, o.toString()); |
| 2146 |
} |
| 2147 |
|
| 2148 |
/// Helper function to safely get a type out of a `RootObject` |
| 2149 |
private Type asType(RootObject o) @safe |
| 2150 |
{ |
| 2151 |
if (Type ta = isType(o)) |
| 2152 |
return ta; |
| 2153 |
|
| 2154 |
// When called with context.res as argument, it can be `FuncDeclaration` |
| 2155 |
if (auto fd = o.asFuncDecl()) |
| 2156 |
return fd.type; |
| 2157 |
assert(0); |
| 2158 |
} |
| 2159 |
|
| 2160 |
/// Helper function to safely get a `FuncDeclaration` out of a `RootObject` |
| 2161 |
private FuncDeclaration asFuncDecl(RootObject o) @safe |
| 2162 |
{ |
| 2163 |
Dsymbol d = isDsymbol(o); |
| 2164 |
assert(d !is null); |
| 2165 |
auto fd = d.isFuncDeclaration(); |
| 2166 |
assert(fd !is null); |
| 2167 |
return fd; |
| 2168 |
} |
| 2169 |
|
| 2170 |
/// Helper class to compare entries in components |
| 2171 |
private extern(C++) final class ComponentVisitor : Visitor |
| 2172 |
{ |
| 2173 |
/// Only one of the following is not `null`, it's always |
| 2174 |
/// the most specialized type, set from the ctor |
| 2175 |
private Nspace namespace; |
| 2176 |
|
| 2177 |
/// Ditto |
| 2178 |
private CPPNamespaceDeclaration namespace2; |
| 2179 |
|
| 2180 |
/// Ditto |
| 2181 |
private TypePointer tpointer; |
| 2182 |
|
| 2183 |
/// Ditto |
| 2184 |
private TypeReference tref; |
| 2185 |
|
| 2186 |
/// Ditto |
| 2187 |
private TypeIdentifier tident; |
| 2188 |
|
| 2189 |
/// Least specialized type |
| 2190 |
private RootObject object; |
| 2191 |
|
| 2192 |
/// Set to the result of the comparison |
| 2193 |
private bool result; |
| 2194 |
|
| 2195 |
public this(RootObject base) @trusted |
| 2196 |
{ |
| 2197 |
switch (base.dyncast()) |
| 2198 |
{ |
| 2199 |
case DYNCAST.dsymbol: |
| 2200 |
if (auto ns = (cast(Dsymbol)base).isNspace()) |
| 2201 |
this.namespace = ns; |
| 2202 |
else if (auto ns = (cast(Dsymbol)base).isCPPNamespaceDeclaration()) |
| 2203 |
this.namespace2 = ns; |
| 2204 |
else |
| 2205 |
goto default; |
| 2206 |
break; |
| 2207 |
|
| 2208 |
case DYNCAST.type: |
| 2209 |
auto t = cast(Type)base; |
| 2210 |
if (auto tp = t.isTypePointer()) |
| 2211 |
this.tpointer = tp; |
| 2212 |
else if (auto tr = t.isTypeReference()) |
| 2213 |
this.tref = tr; |
| 2214 |
else if (auto ti = t.isTypeIdentifier()) |
| 2215 |
this.tident = ti; |
| 2216 |
else |
| 2217 |
goto default; |
| 2218 |
break; |
| 2219 |
|
| 2220 |
// Note: ABI tags are also handled here (they are TupleExp of StringExp) |
| 2221 |
default: |
| 2222 |
this.object = base; |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
/// Introduce base class overloads |
| 2227 |
alias visit = Visitor.visit; |
| 2228 |
|
| 2229 |
/// Least specialized overload of each direct child of `RootObject` |
| 2230 |
public override void visit(Dsymbol o) |
| 2231 |
{ |
| 2232 |
this.result = this.object && this.object == o; |
| 2233 |
} |
| 2234 |
|
| 2235 |
/// Ditto |
| 2236 |
public override void visit(Expression o) |
| 2237 |
{ |
| 2238 |
this.result = this.object && this.object == o; |
| 2239 |
} |
| 2240 |
|
| 2241 |
/// Ditto |
| 2242 |
public void visit(Tuple o) |
| 2243 |
{ |
| 2244 |
this.result = this.object && this.object == o; |
| 2245 |
} |
| 2246 |
|
| 2247 |
/// Ditto |
| 2248 |
public override void visit(Type o) |
| 2249 |
{ |
| 2250 |
this.result = this.object && this.object == o; |
| 2251 |
} |
| 2252 |
|
| 2253 |
/// Ditto |
| 2254 |
public override void visit(TemplateParameter o) |
| 2255 |
{ |
| 2256 |
this.result = this.object && this.object == o; |
| 2257 |
} |
| 2258 |
|
| 2259 |
/** |
| 2260 |
* This overload handles composed types including template parameters |
| 2261 |
* |
| 2262 |
* Components for substitutions include "next" type. |
| 2263 |
* For example, if `ref T` is present, `ref T` and `T` will be present |
| 2264 |
* in the substitution array. |
| 2265 |
* But since we don't have the final/merged type, we cannot rely on |
| 2266 |
* object comparison, and need to recurse instead. |
| 2267 |
*/ |
| 2268 |
public override void visit(TypeReference o) |
| 2269 |
{ |
| 2270 |
if (!this.tref) |
| 2271 |
return; |
| 2272 |
if (this.tref == o) |
| 2273 |
this.result = true; |
| 2274 |
else |
| 2275 |
{ |
| 2276 |
// It might be a reference to a template parameter that we already |
| 2277 |
// saw, so we need to recurse |
| 2278 |
scope v = new ComponentVisitor(this.tref.next); |
| 2279 |
o.next.visitObject(v); |
| 2280 |
this.result = v.result; |
| 2281 |
} |
| 2282 |
} |
| 2283 |
|
| 2284 |
/// Ditto |
| 2285 |
public override void visit(TypePointer o) |
| 2286 |
{ |
| 2287 |
if (!this.tpointer) |
| 2288 |
return; |
| 2289 |
if (this.tpointer == o) |
| 2290 |
this.result = true; |
| 2291 |
else |
| 2292 |
{ |
| 2293 |
// It might be a pointer to a template parameter that we already |
| 2294 |
// saw, so we need to recurse |
| 2295 |
scope v = new ComponentVisitor(this.tpointer.next); |
| 2296 |
o.next.visitObject(v); |
| 2297 |
this.result = v.result; |
| 2298 |
} |
| 2299 |
} |
| 2300 |
|
| 2301 |
/// Ditto |
| 2302 |
public override void visit(TypeIdentifier o) |
| 2303 |
{ |
| 2304 |
/// Since we know they are at the same level, scope resolution will |
| 2305 |
/// give us the same symbol, thus we can just compare ident. |
| 2306 |
this.result = (this.tident && (this.tident.ident == o.ident)); |
| 2307 |
} |
| 2308 |
|
| 2309 |
/** |
| 2310 |
* Overload which accepts a Namespace |
| 2311 |
* |
| 2312 |
* It is very common for large C++ projects to have multiple files sharing |
| 2313 |
* the same `namespace`. If any D project adopts the same approach |
| 2314 |
* (e.g. separating data structures from functions), it will lead to two |
| 2315 |
* `Nspace` objects being instantiated, with different addresses. |
| 2316 |
* At the same time, we cannot compare just any Dsymbol via identifier, |
| 2317 |
* because it messes with templates. |
| 2318 |
* |
| 2319 |
* See_Also: |
| 2320 |
* https://issues.dlang.org/show_bug.cgi?id=18922 |
| 2321 |
* |
| 2322 |
* Params: |
| 2323 |
* ns = C++ namespace to do substitution for |
| 2324 |
*/ |
| 2325 |
public override void visit(Nspace ns) |
| 2326 |
{ |
| 2327 |
this.result = isNamespaceEqual(this.namespace, ns) |
| 2328 |
|| isNamespaceEqual(this.namespace2, ns); |
| 2329 |
} |
| 2330 |
|
| 2331 |
/// Ditto |
| 2332 |
public override void visit(CPPNamespaceDeclaration ns) |
| 2333 |
{ |
| 2334 |
this.result = isNamespaceEqual(this.namespace, ns) |
| 2335 |
|| isNamespaceEqual(this.namespace2, ns); |
| 2336 |
} |
| 2337 |
} |
| 2338 |
|
| 2339 |
/// Transitional functions for `CPPNamespaceDeclaration` / `Nspace` |
| 2340 |
/// Remove when `Nspace` is removed. |
| 2341 |
private bool isNamespaceEqual (Nspace a, Nspace b) |
| 2342 |
{ |
| 2343 |
if (a is null || b is null) |
| 2344 |
return false; |
| 2345 |
return a.equals(b); |
| 2346 |
} |
| 2347 |
|
| 2348 |
/// Ditto |
| 2349 |
private bool isNamespaceEqual (Nspace a, CPPNamespaceDeclaration b) |
| 2350 |
{ |
| 2351 |
return isNamespaceEqual(b, a); |
| 2352 |
} |
| 2353 |
|
| 2354 |
/// Ditto |
| 2355 |
private bool isNamespaceEqual (CPPNamespaceDeclaration a, Nspace b, size_t idx = 0) |
| 2356 |
{ |
| 2357 |
if ((a is null) != (b is null)) |
| 2358 |
return false; |
| 2359 |
if (!a.ident.equals(b.ident)) |
| 2360 |
return false; |
| 2361 |
|
| 2362 |
// We need to see if there's more ident enclosing |
| 2363 |
if (auto pb = b.toParent().isNspace()) |
| 2364 |
return isNamespaceEqual(a.cppnamespace, pb); |
| 2365 |
return a.cppnamespace is null; |
| 2366 |
} |
| 2367 |
|
| 2368 |
/// Returns: |
| 2369 |
/// Whether two `CPPNamespaceDeclaration` are equals |
| 2370 |
private bool isNamespaceEqual (CPPNamespaceDeclaration a, CPPNamespaceDeclaration b) @safe |
| 2371 |
{ |
| 2372 |
if (a is null || b is null) |
| 2373 |
return false; |
| 2374 |
|
| 2375 |
if ((a.cppnamespace is null) != (b.cppnamespace is null)) |
| 2376 |
return false; |
| 2377 |
if (a.ident != b.ident) |
| 2378 |
return false; |
| 2379 |
return a.cppnamespace is null ? true : isNamespaceEqual(a.cppnamespace, b.cppnamespace); |
| 2380 |
} |
| 2381 |
|
| 2382 |
/** |
| 2383 |
* A container for ABI tags |
| 2384 |
* |
| 2385 |
* At its hearth, there is a sorted array of ABI tags having been written |
| 2386 |
* already. ABI tags can be present on parameters, template parameters, |
| 2387 |
* return value, and varaible. ABI tags for a given type needs to be written |
| 2388 |
* sorted. When a function returns a type that has ABI tags, only the tags that |
| 2389 |
* haven't been printed as part of the mangling (e.g. arguments) are written |
| 2390 |
* directly after the function name. |
| 2391 |
* |
| 2392 |
* This means that: |
| 2393 |
* --- |
| 2394 |
* /++ C++ type definitions: |
| 2395 |
* struct [[gnu::abi_tag("tag1")]] Struct1 {}; |
| 2396 |
* struct [[gnu::abi_tag("tag2")]] Struct2 {}; |
| 2397 |
* // Can also be: "tag2", "tag1", since tags are sorted. |
| 2398 |
* struct [[gnu::abi_tag("tag1", "tag2")]] Struct3 {}; |
| 2399 |
* +/ |
| 2400 |
* // Functions definitions: |
| 2401 |
* Struct3 func1 (Struct1); |
| 2402 |
* Struct3 func2 (Struct2); |
| 2403 |
* Struct3 func3 (Struct2, Struct1); |
| 2404 |
* --- |
| 2405 |
* Will be respectively pseudo-mangled (part of interest between stars) as: |
| 2406 |
* "_Z4 func1 *B4tag2* ParamsMangling" (ParamsMangling includes tag1), |
| 2407 |
* "_Z4 func2 *B4tag1* ParamsMangling" (ParamsMangling includes tag2), |
| 2408 |
* "_Z4 func2 *B4tag1* ParamsMangling" (ParamsMangling includes both). |
| 2409 |
* |
| 2410 |
* This is why why need to keep a list of tags that were written, |
| 2411 |
* and insert the missing one after parameter mangling has been written. |
| 2412 |
* Since there's a lot of operations that are not easily doable in DMD |
| 2413 |
* (since we can't use Phobos), this special container is implemented. |
| 2414 |
*/ |
| 2415 |
private struct ABITagContainer |
| 2416 |
{ |
| 2417 |
private Array!StringExp written; |
| 2418 |
|
| 2419 |
static ArrayLiteralExp forSymbol (Dsymbol s) |
| 2420 |
{ |
| 2421 |
if (!s) |
| 2422 |
return null; |
| 2423 |
// If this is a template instance, we want the declaration, |
| 2424 |
// as that's where the UDAs are |
| 2425 |
if (auto ti = s.isTemplateInstance()) |
| 2426 |
s = ti.tempdecl; |
| 2427 |
if (!s.userAttribDecl || !s.userAttribDecl.atts) |
| 2428 |
return null; |
| 2429 |
|
| 2430 |
foreach (exp; *s.userAttribDecl.atts) |
| 2431 |
{ |
| 2432 |
if (isGNUABITag(exp)) |
| 2433 |
return (*exp.isStructLiteralExp().elements)[0] |
| 2434 |
.isArrayLiteralExp(); |
| 2435 |
} |
| 2436 |
return null; |
| 2437 |
} |
| 2438 |
|
| 2439 |
void writeSymbol(Dsymbol s, CppMangleVisitor self) |
| 2440 |
{ |
| 2441 |
auto tale = forSymbol(s); |
| 2442 |
if (!tale) return; |
| 2443 |
if (self.substitute(tale)) |
| 2444 |
return; |
| 2445 |
this.write(*self.buf, tale); |
| 2446 |
} |
| 2447 |
|
| 2448 |
/** |
| 2449 |
* Write an ArrayLiteralExp (expected to be an ABI tag) to the buffer |
| 2450 |
* |
| 2451 |
* Params: |
| 2452 |
* buf = Buffer to write mangling to |
| 2453 |
* ale = GNU ABI tag array literal expression, semantically analyzed |
| 2454 |
*/ |
| 2455 |
void write (ref OutBuffer buf, ArrayLiteralExp ale, bool skipKnown = false) |
| 2456 |
{ |
| 2457 |
void writeElem (StringExp exp) |
| 2458 |
{ |
| 2459 |
const tag = exp.peekString(); |
| 2460 |
buf.writestring("B"); |
| 2461 |
buf.print(tag.length); |
| 2462 |
buf.writestring(tag); |
| 2463 |
} |
| 2464 |
|
| 2465 |
bool match; |
| 2466 |
foreach (exp; *ale.elements) |
| 2467 |
{ |
| 2468 |
auto elem = exp.toStringExp(); |
| 2469 |
auto idx = closestIndex(this.written[], elem, match); |
| 2470 |
if (!match) |
| 2471 |
{ |
| 2472 |
writeElem(elem); |
| 2473 |
this.written.insert(idx, elem); |
| 2474 |
} |
| 2475 |
else if (!skipKnown) |
| 2476 |
writeElem(elem); |
| 2477 |
} |
| 2478 |
} |
| 2479 |
} |
| 2480 |
|
| 2481 |
/** |
| 2482 |
* Returns the closest index to to `exp` in `slice` |
| 2483 |
* |
| 2484 |
* Performs a binary search on `slice` (assumes `slice` is sorted), |
| 2485 |
* and returns either `exp`'s index in `slice` if `exact` is `true`, |
| 2486 |
* or the index at which `exp` can be inserted in `slice` if `exact is `false`. |
| 2487 |
* Inserting `exp` at the return value will keep the array sorted. |
| 2488 |
* |
| 2489 |
* Params: |
| 2490 |
* slice = The sorted slice to search into |
| 2491 |
* exp = The string expression to search for |
| 2492 |
* exact = If `true` on return, `exp` was found in `slice` |
| 2493 |
* |
| 2494 |
* Returns: |
| 2495 |
* Either the index to insert `exp` at (if `exact == false`), |
| 2496 |
* or the index of `exp` in `slice`. |
| 2497 |
*/ |
| 2498 |
private size_t closestIndex (const(StringExp)[] slice, StringExp exp, out bool exact) |
| 2499 |
{ |
| 2500 |
if (!slice.length) return 0; |
| 2501 |
|
| 2502 |
const StringExp* first = slice.ptr; |
| 2503 |
while (true) |
| 2504 |
{ |
| 2505 |
int res = dstrcmp(exp.peekString(), slice[$ / 2].peekString()); |
| 2506 |
if (res == 0) |
| 2507 |
{ |
| 2508 |
exact = true; |
| 2509 |
return (&slice[$/2] - first); |
| 2510 |
} |
| 2511 |
|
| 2512 |
if (slice.length == 1) |
| 2513 |
return (slice.ptr - first) + (res > 0); |
| 2514 |
slice = slice[(res > 0 ? $ / 2 : 0) .. (res > 0 ? $ : $ / 2)]; |
| 2515 |
} |
| 2516 |
} |
| 2517 |
|
| 2518 |
// |
| 2519 |
unittest |
| 2520 |
{ |
| 2521 |
bool match; |
| 2522 |
auto s1 = new StringExp(Loc.initial, "Amande"); |
| 2523 |
auto s2 = new StringExp(Loc.initial, "Baguette"); |
| 2524 |
auto s3 = new StringExp(Loc.initial, "Croissant"); |
| 2525 |
auto s4 = new StringExp(Loc.initial, "Framboises"); |
| 2526 |
auto s5 = new StringExp(Loc.initial, "Proscuitto"); |
| 2527 |
|
| 2528 |
// Found, odd size |
| 2529 |
assert(closestIndex([s1, s2, s3, s4, s5], s1, match) == 0 && match); |
| 2530 |
assert(closestIndex([s1, s2, s3, s4, s5], s2, match) == 1 && match); |
| 2531 |
assert(closestIndex([s1, s2, s3, s4, s5], s3, match) == 2 && match); |
| 2532 |
assert(closestIndex([s1, s2, s3, s4, s5], s4, match) == 3 && match); |
| 2533 |
assert(closestIndex([s1, s2, s3, s4, s5], s5, match) == 4 && match); |
| 2534 |
|
| 2535 |
// Not found, even size |
| 2536 |
assert(closestIndex([s2, s3, s4, s5], s1, match) == 0 && !match); |
| 2537 |
assert(closestIndex([s1, s3, s4, s5], s2, match) == 1 && !match); |
| 2538 |
assert(closestIndex([s1, s2, s4, s5], s3, match) == 2 && !match); |
| 2539 |
assert(closestIndex([s1, s2, s3, s5], s4, match) == 3 && !match); |
| 2540 |
assert(closestIndex([s1, s2, s3, s4], s5, match) == 4 && !match); |
| 2541 |
|
| 2542 |
// Found, even size |
| 2543 |
assert(closestIndex([s1, s2, s3, s4], s1, match) == 0 && match); |
| 2544 |
assert(closestIndex([s1, s2, s3, s4], s2, match) == 1 && match); |
| 2545 |
assert(closestIndex([s1, s2, s3, s4], s3, match) == 2 && match); |
| 2546 |
assert(closestIndex([s1, s2, s3, s4], s4, match) == 3 && match); |
| 2547 |
assert(closestIndex([s1, s3, s4, s5], s5, match) == 3 && match); |
| 2548 |
|
| 2549 |
// Not found, odd size |
| 2550 |
assert(closestIndex([s2, s4, s5], s1, match) == 0 && !match); |
| 2551 |
assert(closestIndex([s1, s4, s5], s2, match) == 1 && !match); |
| 2552 |
assert(closestIndex([s1, s2, s4], s3, match) == 2 && !match); |
| 2553 |
assert(closestIndex([s1, s3, s5], s4, match) == 2 && !match); |
| 2554 |
assert(closestIndex([s1, s2, s4], s5, match) == 3 && !match); |
| 2555 |
} |
| 2556 |
|
| 2557 |
/*** |
| 2558 |
* Visits the return type of a function and writes leftover ABI tags |
| 2559 |
* Params: |
| 2560 |
* tf = Type of the function to mangle the return type of |
| 2561 |
* previous = already written ones |
| 2562 |
* toWrite = where to put StringExp's to be written |
| 2563 |
*/ |
| 2564 |
private |
| 2565 |
void leftOver(TypeFunction tf, const(Array!StringExp)* previous, Array!StringExp* toWrite) |
| 2566 |
{ |
| 2567 |
extern(C++) final class LeftoverVisitor : Visitor |
| 2568 |
{ |
| 2569 |
/// List of tags to write |
| 2570 |
private Array!StringExp* toWrite; |
| 2571 |
/// List of tags to ignore |
| 2572 |
private const(Array!StringExp)* ignore; |
| 2573 |
|
| 2574 |
/// |
| 2575 |
public this(const(Array!StringExp)* previous, Array!StringExp* toWrite) @safe |
| 2576 |
{ |
| 2577 |
this.ignore = previous; |
| 2578 |
this.toWrite = toWrite; |
| 2579 |
} |
| 2580 |
|
| 2581 |
/// Reintroduce base class overloads |
| 2582 |
public alias visit = Visitor.visit; |
| 2583 |
|
| 2584 |
/// Least specialized overload of each direct child of `RootObject` |
| 2585 |
public override void visit(Dsymbol o) |
| 2586 |
{ |
| 2587 |
auto ale = ABITagContainer.forSymbol(o); |
| 2588 |
if (!ale) return; |
| 2589 |
|
| 2590 |
bool match; |
| 2591 |
foreach (elem; *ale.elements) |
| 2592 |
{ |
| 2593 |
auto se = elem.toStringExp(); |
| 2594 |
closestIndex((*this.ignore)[], se, match); |
| 2595 |
if (match) continue; |
| 2596 |
auto idx = closestIndex((*this.toWrite)[], se, match); |
| 2597 |
if (!match) |
| 2598 |
(*this.toWrite).insert(idx, se); |
| 2599 |
} |
| 2600 |
} |
| 2601 |
|
| 2602 |
/// Ditto |
| 2603 |
public override void visit(Type o) |
| 2604 |
{ |
| 2605 |
if (auto sym = o.toDsymbol(null)) |
| 2606 |
sym.accept(this); |
| 2607 |
} |
| 2608 |
|
| 2609 |
/// Composite type |
| 2610 |
public override void visit(TypePointer o) |
| 2611 |
{ |
| 2612 |
o.next.accept(this); |
| 2613 |
} |
| 2614 |
|
| 2615 |
public override void visit(TypeReference o) |
| 2616 |
{ |
| 2617 |
o.next.accept(this); |
| 2618 |
} |
| 2619 |
} |
| 2620 |
|
| 2621 |
scope remainingVisitor = new LeftoverVisitor(previous, toWrite); |
| 2622 |
tf.next.accept(remainingVisitor); |
| 2623 |
} |