2 * Does name mangling for `extern(D)` symbols.
4 * Specification: $(LINK2 https://dlang.org/spec/abi.html#name_mangling, Name Mangling)
6 * Copyright: Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
7 * Authors: Walter Bright, https://www.digitalmars.com
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/dmangle.d, _dmangle.d)
10 * Documentation: https://dlang.org/phobos/dmd_dmangle.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/dmangle.d
12 * References: https://dlang.org/blog/2017/12/20/ds-newfangled-name-mangling/
19 /******************************************************************************
20 * Returns exact mangled name of function.
22 extern (C++) const(char)* mangleExact(FuncDeclaration fd)
27 scope Mangler v = new Mangler(&buf);
29 fd.mangleString = buf.extractChars();
31 return fd.mangleString;
34 extern (C++) void mangleToBuffer(Type t, OutBuffer* buf)
37 buf.writestring(t.deco);
40 scope Mangler v = new Mangler(buf, t);
41 v.visitWithMask(t, 0);
45 extern (C++) void mangleToBuffer(Expression e, OutBuffer* buf)
47 scope Mangler v = new Mangler(buf);
51 extern (C++) void mangleToBuffer(Dsymbol s, OutBuffer* buf)
53 scope Mangler v = new Mangler(buf);
57 extern (C++) void mangleToBuffer(TemplateInstance ti, OutBuffer* buf)
59 scope Mangler v = new Mangler(buf);
60 v.mangleTemplateInstance(ti);
63 /// Returns: `true` if the given character is a valid mangled character
64 package bool isValidMangling(dchar c) nothrow
67 c >= 'A' && c <= 'Z' ||
68 c >= 'a' && c <= 'z' ||
69 c >= '0' && c <= '9' ||
70 c != 0 && strchr("$%().:?@[]_", c) ||
74 // valid mangled characters
77 assert('a'.isValidMangling);
78 assert('B'.isValidMangling);
79 assert('2'.isValidMangling);
80 assert('@'.isValidMangling);
81 assert('_'.isValidMangling);
84 // invalid mangled characters
87 assert(!'-'.isValidMangling);
88 assert(!0.isValidMangling);
89 assert(!'/'.isValidMangling);
90 assert(!'\\'.isValidMangling);
93 /**********************************************
94 * Convert a string representing a type (the deco) and
95 * return its equivalent Type.
97 * deco = string containing the deco
99 * null for failed to convert
103 public Type decoToType(const(char)[] deco)
105 //printf("decoToType(): %.*s\n", cast(int)deco.length, deco.ptr);
106 if (auto sv = Type.stringtable.lookup(deco))
110 Type t = cast(Type)sv.value;
119 /***************************************** private ***************************************/
124 import core.stdc.ctype;
125 import core.stdc.stdio;
126 import core.stdc.string;
128 import dmd.aggregate;
129 import dmd.arraytypes;
131 import dmd.declaration;
134 import dmd.dtemplate;
135 import dmd.expression;
139 import dmd.identifier;
141 import dmd.root.ctfloat;
142 import dmd.common.outbuffer;
144 import dmd.root.string;
145 import dmd.root.stringtable;
151 private immutable char[TMAX] mangleChar =
186 Tfunction : 'F', // D function
193 // M // has this, or scope
194 // N // Nh:vector Ng:wild Nn:noreturn
197 // Q // Type/symbol/identifier backward reference
202 // W // Windows function
203 // X // variadic T t...)
204 // Y // variadic T t,...)
205 // Z // not variadic, end of parameters
207 // '@' shouldn't appear anywhere in the deco'd names
219 Tnoreturn : '@', // becomes 'Nn'
224 foreach (i, mangle; mangleChar)
226 if (mangle == char.init)
228 fprintf(stderr, "ty = %u\n", cast(uint)i);
234 private extern (C++) final class Mangler : Visitor
236 alias visit = Visitor.visit;
238 static assert(Key.sizeof == size_t.sizeof);
243 extern (D) this(OutBuffer* buf, Type rootType = null)
246 this.backref = Backref(rootType);
249 void mangleSymbol(Dsymbol s)
254 void mangleType(Type t)
256 if (!backref.addRefToType(buf, t))
260 void mangleIdentifier(Identifier id, Dsymbol s)
262 if (!backref.addRefToIdentifier(buf, id))
263 toBuffer(buf, id.toString(), s);
266 ////////////////////////////////////////////////////////////////////////////
267 /**************************************************
270 void visitWithMask(Type t, ubyte modMask)
272 if (modMask != t.mod)
274 MODtoDecoBuffer(buf, t.mod);
279 override void visit(Type t)
281 tyToDecoBuffer(buf, t.ty);
284 override void visit(TypeNext t)
287 visitWithMask(t.next, t.mod);
290 override void visit(TypeVector t)
292 buf.writestring("Nh");
293 visitWithMask(t.basetype, t.mod);
296 override void visit(TypeSArray t)
300 buf.print(t.dim.toInteger());
302 visitWithMask(t.next, t.mod);
305 override void visit(TypeDArray t)
309 visitWithMask(t.next, t.mod);
312 override void visit(TypeAArray t)
315 visitWithMask(t.index, 0);
316 visitWithMask(t.next, t.mod);
319 override void visit(TypeFunction t)
321 //printf("TypeFunction.toDecoBuffer() t = %p %s\n", t, t.toChars());
322 //static int nest; if (++nest == 50) *(char*)0=0;
323 mangleFuncType(t, t, t.mod, t.next);
326 void mangleFuncType(TypeFunction t, TypeFunction ta, ubyte modMask, Type tret)
328 //printf("mangleFuncType() %s\n", t.toChars());
331 // printf("TypeFunction.mangleFuncType() t = %s inuse\n", t.toChars());
332 t.inuse = 2; // flag error to caller
336 if (modMask != t.mod)
337 MODtoDecoBuffer(buf, t.mod);
340 final switch (t.linkage)
364 buf.writestring("Na");
366 buf.writestring("Nb");
368 buf.writestring("Nc");
370 buf.writestring("Nd");
372 buf.writestring("Ni");
374 if (ta.isreturn && !ta.isreturninferred)
375 buf.writestring("Nj");
376 else if (ta.isScopeQual && !ta.isscopeinferred)
377 buf.writestring("Nl");
380 buf.writestring("Nm");
385 buf.writestring("Ne");
388 buf.writestring("Nf");
394 // Write argument types
395 foreach (idx, param; t.parameterList)
396 mangleParameter(param);
397 //if (buf.data[buf.length - 1] == '@') assert(0);
398 buf.writeByte('Z' - t.parameterList.varargs); // mark end of arg list
400 visitWithMask(tret, 0);
404 override void visit(TypeIdentifier t)
407 auto name = t.ident.toString();
408 buf.print(cast(int)name.length);
409 buf.writestring(name);
412 override void visit(TypeEnum t)
418 override void visit(TypeStruct t)
420 //printf("TypeStruct.toDecoBuffer('%s') = '%s'\n", t.toChars(), name);
425 override void visit(TypeClass t)
427 //printf("TypeClass.toDecoBuffer('%s' mod=%x) = '%s'\n", t.toChars(), mod, name);
432 override void visit(TypeTuple t)
434 //printf("TypeTuple.toDecoBuffer() t = %p, %s\n", t, t.toChars());
436 Parameter._foreach(t.arguments, (idx, param) {
437 mangleParameter(param);
443 override void visit(TypeNull t)
448 override void visit(TypeNoreturn t)
450 buf.writestring("Nn");
453 ////////////////////////////////////////////////////////////////////////////
454 void mangleDecl(Declaration sthis)
458 mangleIdentifier(sthis.ident, sthis);
459 if (FuncDeclaration fd = sthis.isFuncDeclaration())
461 mangleFunc(fd, false);
465 visitWithMask(sthis.type, 0);
471 void mangleParent(Dsymbol s)
473 //printf("mangleParent() %s %s\n", s.kind(), s.toChars());
475 if (TemplateInstance ti = s.isTemplateInstance())
476 p = ti.isTemplateMixin() ? ti.parent : ti.tempdecl.parent;
481 uint localNum = s.localNum;
483 auto ti = p.isTemplateInstance();
484 if (ti && !ti.isTemplateMixin())
486 localNum = ti.tempdecl.localNum;
487 mangleTemplateInstance(ti);
489 else if (p.getIdent())
491 mangleIdentifier(p.ident, s);
492 if (FuncDeclaration f = p.isFuncDeclaration())
499 writeLocalParent(buf, localNum);
503 void mangleFunc(FuncDeclaration fd, bool inParent)
505 //printf("deco = '%s'\n", fd.type.deco ? fd.type.deco : "null");
506 //printf("fd.type = %s\n", fd.type.toChars());
507 if (fd.needThis() || fd.isNested())
510 if (!fd.type || fd.type.ty == Terror)
512 // never should have gotten here, but could be the result of
513 // failed speculative compilation
514 buf.writestring("9__error__FZ");
516 //printf("[%s] %s no type\n", fd.loc.toChars(), fd.toChars());
517 //assert(0); // don't mangle function until semantic3 done.
521 TypeFunction tf = fd.type.isTypeFunction();
522 TypeFunction tfo = fd.originalType.isTypeFunction();
523 mangleFuncType(tf, tfo, 0, null);
527 visitWithMask(fd.type, 0);
531 override void visit(Declaration d)
533 //printf("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d)\n",
534 // d, d.toChars(), d.parent ? d.parent.toChars() : "null", d.linkage);
535 if (const id = externallyMangledIdentifier(d))
540 buf.writestring("_D");
544 const slice = (*buf)[];
545 assert(slice.length);
546 for (size_t pos; pos < slice.length; )
550 const s = utf_decodeChar(slice, pos, c);
551 assert(s is null, s);
552 assert(c.isValidMangling, "The mangled name '" ~ slice ~ "' " ~
553 "contains an invalid character: " ~ slice[ppos..pos]);
558 /******************************************************************************
559 * Normally FuncDeclaration and FuncAliasDeclaration have overloads.
560 * If and only if there is no overloads, mangle() could return
561 * exact mangled name.
564 * void foo(long) {} // _D4test3fooFlZv
565 * void foo(string) {} // _D4test3fooFAyaZv
567 * // from FuncDeclaration.mangle().
568 * pragma(msg, foo.mangleof); // prints unexact mangled name "4test3foo"
569 * // by calling Dsymbol.mangle()
571 * // from FuncAliasDeclaration.mangle()
572 * pragma(msg, __traits(getOverloads, test, "foo")[0].mangleof); // "_D4test3fooFlZv"
573 * pragma(msg, __traits(getOverloads, test, "foo")[1].mangleof); // "_D4test3fooFAyaZv"
575 * If a function has no overloads, .mangleof property still returns exact mangled name.
578 * pragma(msg, bar.mangleof); // still prints "_D4test3barFZv"
579 * // by calling FuncDeclaration.mangleExact().
581 override void visit(FuncDeclaration fd)
586 visit(cast(Dsymbol)fd);
590 override void visit(FuncAliasDeclaration fd)
592 FuncDeclaration f = fd.toAliasFunc();
593 FuncAliasDeclaration fa = f.isFuncAliasDeclaration();
594 if (!fd.hasOverloads && !fa)
604 visit(cast(Dsymbol)fd);
607 override void visit(OverDeclaration od)
611 visit(cast(Dsymbol)od);
614 if (FuncDeclaration fd = od.aliassym.isFuncDeclaration())
622 if (TemplateDeclaration td = od.aliassym.isTemplateDeclaration())
624 if (td.overnext is null)
630 visit(cast(Dsymbol)od);
633 void mangleExact(FuncDeclaration fd)
635 assert(!fd.isFuncAliasDeclaration());
636 if (fd.mangleOverride)
638 buf.writestring(fd.mangleOverride);
643 buf.writestring("_Dmain");
646 if (fd.isWinMain() || fd.isDllMain())
648 buf.writestring(fd.ident.toString());
651 visit(cast(Declaration)fd);
654 override void visit(VarDeclaration vd)
656 if (vd.mangleOverride)
658 buf.writestring(vd.mangleOverride);
661 visit(cast(Declaration)vd);
664 override void visit(AggregateDeclaration ad)
666 ClassDeclaration cd = ad.isClassDeclaration();
667 Dsymbol parentsave = ad.parent;
670 /* These are reserved to the compiler, so keep simple
673 if (cd.ident == Id.Exception && cd.parent.ident == Id.object || cd.ident == Id.TypeInfo || cd.ident == Id.TypeInfo_Struct || cd.ident == Id.TypeInfo_Class || cd.ident == Id.TypeInfo_Tuple || cd == ClassDeclaration.object || cd == Type.typeinfoclass || cd == Module.moduleinfo || strncmp(cd.ident.toChars(), "TypeInfo_", 9) == 0)
675 // Don't mangle parent
679 visit(cast(Dsymbol)ad);
680 ad.parent = parentsave;
683 override void visit(TemplateInstance ti)
687 printf("TemplateInstance.mangle() %p %s", ti, ti.toChars());
689 printf(" parent = %s %s", ti.parent.kind(), ti.parent.toChars());
693 ti.error("is not defined");
697 if (ti.isTemplateMixin() && ti.ident)
698 mangleIdentifier(ti.ident, ti);
700 mangleTemplateInstance(ti);
703 void mangleTemplateInstance(TemplateInstance ti)
705 TemplateDeclaration tempdecl = ti.tempdecl.isTemplateDeclaration();
708 // Use "__U" for the symbols declared inside template constraint.
709 const char T = ti.members ? 'T' : 'U';
710 buf.printf("__%c", T);
711 mangleIdentifier(tempdecl.ident, tempdecl);
713 auto args = ti.tiargs;
714 size_t nparams = tempdecl.parameters.dim - (tempdecl.isVariadic() ? 1 : 0);
715 for (size_t i = 0; i < args.dim; i++)
719 Expression ea = isExpression(o);
720 Dsymbol sa = isDsymbol(o);
721 Tuple va = isTuple(o);
722 //printf("\to [%d] %p ta %p ea %p sa %p va %p\n", i, o, ta, ea, sa, va);
723 if (i < nparams && (*tempdecl.parameters)[i].specialization())
724 buf.writeByte('H'); // https://issues.dlang.org/show_bug.cgi?id=6574
728 visitWithMask(ta, 0);
732 // Don't interpret it yet, it might actually be an alias template parameter.
733 // Only constfold manifest constants, not const/immutable lvalues, see https://issues.dlang.org/show_bug.cgi?id=17339.
734 enum keepLvalue = true;
735 ea = ea.optimize(WANTvalue, keepLvalue);
736 if (auto ev = ea.isVarExp())
742 if (auto et = ea.isThisExp())
748 if (auto ef = ea.isFuncExp())
758 if (ea.op == EXP.tuple)
760 ea.error("tuple is not a valid template value argument");
763 // Now that we know it is not an alias, we MUST obtain a value
764 uint olderr = global.errors;
765 ea = ea.ctfeInterpret();
766 if (ea.op == EXP.error || olderr != global.errors)
769 /* Use type mangling that matches what it would be for a function parameter
771 visitWithMask(ea.type, 0);
778 if (sa.isDeclaration() && !sa.isOverDeclaration())
780 Declaration d = sa.isDeclaration();
782 if (auto fad = d.isFuncAliasDeclaration())
783 d = fad.toAliasFunc();
784 if (d.mangleOverride)
787 toBuffer(buf, d.mangleOverride, d);
790 if (const id = externallyMangledIdentifier(d))
793 toBuffer(buf, id, d);
796 if (!d.type || !d.type.deco)
798 ti.error("forward reference of %s `%s`", d.kind(), d.toChars());
807 assert(i + 1 == args.dim); // must be last one
817 override void visit(Dsymbol s)
821 printf("Dsymbol.mangle() '%s'", s.toChars());
823 printf(" parent = %s %s", s.parent.kind(), s.parent.toChars());
828 mangleIdentifier(s.ident, s);
830 toBuffer(buf, s.toString(), s);
831 //printf("Dsymbol.mangle() %s = %s\n", s.toChars(), id);
834 ////////////////////////////////////////////////////////////////////////////
835 override void visit(Expression e)
837 e.error("expression `%s` is not a valid template value argument", e.toChars());
840 override void visit(IntegerExp e)
842 const v = e.toInteger();
843 if (cast(sinteger_t)v < 0)
855 override void visit(RealExp e)
858 realToMangleBuffer(buf, e.value);
861 override void visit(ComplexExp e)
864 realToMangleBuffer(buf, e.toReal());
865 buf.writeByte('c'); // separate the two
866 realToMangleBuffer(buf, e.toImaginary());
869 override void visit(NullExp e)
874 override void visit(StringExp e)
879 /* Write string in UTF-8 format
890 const slice = e.peekWstring();
891 for (size_t u = 0; u < e.len;)
894 if (const s = utf_decodeWchar(slice, u, c))
895 e.error("%.*s", cast(int)s.length, s.ptr);
905 const slice = e.peekDstring();
908 if (!utf_isValidDchar(c))
909 e.error("invalid UCS-32 char \\U%08x", c);
920 buf.reserve(1 + 11 + 2 * q.length);
923 buf.writeByte('_'); // nbytes <= 11
924 auto slice = buf.allocate(2 * q.length);
927 char hi = (c >> 4) & 0xF;
928 slice[i * 2] = cast(char)(hi < 10 ? hi + '0' : hi - 10 + 'a');
930 slice[i * 2 + 1] = cast(char)(lo < 10 ? lo + '0' : lo - 10 + 'a');
934 override void visit(ArrayLiteralExp e)
936 const dim = e.elements ? e.elements.dim : 0;
939 foreach (i; 0 .. dim)
945 override void visit(AssocArrayLiteralExp e)
947 const dim = e.keys.dim;
950 foreach (i; 0 .. dim)
952 (*e.keys)[i].accept(this);
953 (*e.values)[i].accept(this);
957 override void visit(StructLiteralExp e)
959 const dim = e.elements ? e.elements.dim : 0;
962 foreach (i; 0 .. dim)
964 Expression ex = (*e.elements)[i];
968 buf.writeByte('v'); // 'v' for void
972 override void visit(FuncExp e)
981 ////////////////////////////////////////////////////////////////////////////
983 void mangleParameter(Parameter p)
985 // https://dlang.org/spec/abi.html#Parameter
987 auto stc = p.storageClass;
989 // Inferred storage classes don't get mangled in
990 if (stc & STC.scopeinferred)
991 stc &= ~(STC.scope_ | STC.scopeinferred);
992 if (stc & STC.returninferred)
993 stc &= ~(STC.return_ | STC.returninferred);
995 // much like hdrgen.stcToBuffer()
997 const isout = (stc & STC.out_) != 0;
998 final switch (buildScopeRef(stc))
1001 case ScopeRef.Scope:
1003 case ScopeRef.Return:
1004 case ScopeRef.RefScope:
1007 case ScopeRef.ReturnScope: rrs = "NkM"; goto L1; // return scope
1008 case ScopeRef.ReturnRef: rrs = isout ? "NkJ" : "NkK"; goto L1; // return ref
1009 case ScopeRef.ReturnRef_Scope: rrs = isout ? "MNkJ" : "MNkK"; goto L1; // scope return ref
1010 case ScopeRef.Ref_ReturnScope: rrs = isout ? "NkMJ" : "NkMK"; goto L1; // return scope ref
1012 buf.writestring(rrs);
1013 stc &= ~(STC.out_ | STC.scope_ | STC.ref_ | STC.return_);
1017 if (stc & STC.scope_)
1018 buf.writeByte('M'); // scope
1020 if (stc & STC.return_)
1021 buf.writestring("Nk"); // return
1023 switch (stc & (STC.IOR | STC.lazy_))
1030 case STC.in_ | STC.ref_:
1031 buf.writestring("IK");
1045 printf("storageClass = x%llx\n", stc & (STC.IOR | STC.lazy_));
1049 visitWithMask(p.type, (stc & STC.in_) ? MODFlags.const_ : 0);
1053 /***************************************
1054 * Manage back reference mangling
1056 private struct Backref
1059 * Back references a non-basic type
1061 * The encoded mangling is
1062 * 'Q' <relative position of first occurrence of type>
1065 * t = the type to encode via back referencing
1068 * true if the type was found. A back reference has been encoded.
1069 * false if the type was not found. The current position is saved for later back references.
1071 bool addRefToType(OutBuffer* buf, Type t)
1073 if (t.isTypeBasic())
1077 * https://issues.dlang.org/show_bug.cgi?id=21591
1079 * Special case for unmerged TypeFunctions: use the generic merged
1080 * function type as backref cache key to avoid missed backrefs.
1082 * Merging is based on mangling, so we need to avoid an infinite
1083 * recursion by excluding the case where `t` is the root type passed to
1084 * `mangleToBuffer()`.
1088 if (t.isFunction_Delegate_PtrToFunction())
1094 return backrefImpl(buf, types, t);
1098 * Back references a single identifier
1100 * The encoded mangling is
1101 * 'Q' <relative position of first occurrence of type>
1104 * id = the identifier to encode via back referencing
1107 * true if the identifier was found. A back reference has been encoded.
1108 * false if the identifier was not found. The current position is saved for later back references.
1110 bool addRefToIdentifier(OutBuffer* buf, Identifier id)
1112 return backrefImpl(buf, idents, id);
1117 extern(D) bool backrefImpl(T)(OutBuffer* buf, ref AssocArray!(T, size_t) aa, T key)
1119 auto p = aa.getLvalue(key);
1122 const offset = *p - 1;
1123 writeBackRef(buf, buf.length - offset);
1126 *p = buf.length + 1;
1130 Type rootType; /// avoid infinite recursion
1131 AssocArray!(Type, size_t) types; /// Type => (offset+1) in buf
1132 AssocArray!(Identifier, size_t) idents; /// Identifier => (offset+1) in buf
1136 /***********************
1137 * Mangle basic type ty to buf.
1140 private void tyToDecoBuffer(OutBuffer* buf, int ty)
1142 const c = mangleChar[ty];
1145 buf.writeByte(ty == Tint128 ? 'i' : 'k');
1148 /*********************************
1151 private void MODtoDecoBuffer(OutBuffer* buf, MOD mod)
1157 case MODFlags.const_:
1160 case MODFlags.immutable_:
1163 case MODFlags.shared_:
1166 case MODFlags.shared_ | MODFlags.const_:
1167 buf.writestring("Ox");
1170 buf.writestring("Ng");
1172 case MODFlags.wildconst:
1173 buf.writestring("Ngx");
1175 case MODFlags.shared_ | MODFlags.wild:
1176 buf.writestring("ONg");
1178 case MODFlags.shared_ | MODFlags.wildconst:
1179 buf.writestring("ONgx");
1188 * writes a back reference with the relative position encoded with base 26
1189 * using upper case letters for all digits but the last digit which uses
1190 * a lower case letter.
1191 * The decoder has to look up the referenced position to determine
1192 * whether the back reference is an identifier (starts with a digit)
1193 * or a type (starts with a letter).
1196 * buf = buffer to write to
1197 * pos = relative position to encode
1200 void writeBackRef(OutBuffer* buf, size_t pos)
1205 while (pos >= mul * base)
1209 auto dig = cast(ubyte)(pos / mul);
1210 buf.writeByte('A' + dig);
1214 buf.writeByte('a' + cast(ubyte)pos);
1218 /************************************************************
1219 * Write length prefixed string to buf.
1222 extern (D) void toBuffer(OutBuffer* buf, const(char)[] id, Dsymbol s)
1224 const len = id.length;
1225 if (buf.length + len >= 8 * 1024 * 1024) // 8 megs ought be enough for anyone
1226 s.error("excessive length %llu for symbol, possible recursive expansion?", cast(ulong)(buf.length + len));
1230 buf.writestring(id);
1236 * There can be multiple different declarations in the same
1237 * function that have the same mangled name.
1238 * This results in localNum having a non-zero number, which
1239 * is used to add a fake parent of the form `__Sddd` to make
1240 * the mangled names unique.
1241 * https://issues.dlang.org/show_bug.cgi?id=20565
1243 * buf = buffer to write to
1244 * localNum = local symbol number
1247 void writeLocalParent(OutBuffer* buf, uint localNum)
1256 buf.printf("%u__S%u", ndigits + 3, localNum);
1259 /*************************
1260 * Write real to buffer.
1262 * buf = buffer to write to
1263 * value = real to write
1266 void realToMangleBuffer(OutBuffer* buf, real_t value)
1268 /* Rely on %A to get portable mangling.
1269 * Must munge result to get only identifier characters.
1271 * Possible values from %A => mangled result
1275 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79
1278 if (CTFloat.isNaN(value))
1280 buf.writestring("NAN"); // no -NAN bugs
1284 if (value < CTFloat.zero)
1290 if (CTFloat.isInfinity(value))
1292 buf.writestring("INF");
1296 char[36] buffer = void;
1297 // 'A' format yields [-]0xh.hhhhp+-d
1298 const n = CTFloat.sprint(buffer.ptr, 'A', value);
1299 assert(n < buffer.length);
1300 foreach (const c; buffer[2 .. n])
1319 /************************************************************
1320 * Try to obtain an externally mangled identifier from a declaration.
1321 * If the declaration is at global scope or mixed in at global scope,
1322 * the user might want to call it externally, so an externally mangled
1323 * name is returned. Member functions or nested functions can't be called
1324 * externally in C, so in that case null is returned. C++ does support
1325 * namespaces, so extern(C++) always gives a C++ mangled name.
1327 * See also: https://issues.dlang.org/show_bug.cgi?id=20012
1330 * d = declaration to mangle
1333 * an externally mangled name or null if the declaration cannot be called externally
1336 extern (D) const(char)[] externallyMangledIdentifier(Declaration d)
1338 assert(!d.mangleOverride, "mangle overrides should have been handled earlier");
1340 const par = d.toParent(); //toParent() skips over mixin templates
1341 if (!par || par.isModule() || d.linkage == LINK.cpp ||
1342 (d.linkage == LINK.c && d.isCsymbol() &&
1343 (d.isFuncDeclaration() ||
1344 (d.isVarDeclaration() && d.isDataseg() && d.storage_class & STC.extern_))))
1346 if (d.linkage != LINK.d && d.localNum)
1347 d.error("the same declaration cannot be in multiple scopes with non-D linkage");
1349 const l = d.linkage == LINK.system ? target.systemLinkage() : d.linkage;
1357 return d.ident.toString();
1360 const p = target.cpp.toMangle(d);
1361 return p.toDString();
1364 d.error("forward declaration");
1365 return d.ident.toString();