forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmangle.d
1369 lines (1249 loc) · 38.2 KB
/
dmangle.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Does name mangling for `extern(D)` symbols.
*
* Specification: $(LINK2 https://dlang.org/spec/abi.html#name_mangling, Name Mangling)
*
* Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
* Authors: Walter Bright, https://www.digitalmars.com
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/dmangle.d, _dmangle.d)
* Documentation: https://dlang.org/phobos/dmd_dmangle.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/dmangle.d
* References: https://dlang.org/blog/2017/12/20/ds-newfangled-name-mangling/
*/
module dmd.dmangle;
/******************************************************************************
* Returns exact mangled name of function.
*/
const(char)* mangleExact(FuncDeclaration fd)
{
//printf("mangleExact()\n");
if (!fd.mangleString)
{
OutBuffer buf;
auto backref = Backref(null);
scope Mangler v = new Mangler(buf, &backref);
v.mangleExact(fd);
fd.mangleString = buf.extractChars();
}
return fd.mangleString;
}
void mangleToBuffer(Type t, ref OutBuffer buf)
{
//printf("mangleToBuffer t()\n");
if (t.deco)
buf.writestring(t.deco);
else
{
auto backref = Backref(t);
mangleType(t, 0, buf, backref);
//printf("%s\n", buf.peekChars());
}
}
void mangleToBuffer(Expression e, ref OutBuffer buf)
{
//printf("mangleToBuffer e()\n");
auto backref = Backref(null);
scope Mangler v = new Mangler(buf, &backref);
e.accept(v);
}
void mangleToBuffer(Dsymbol s, ref OutBuffer buf)
{
//printf("mangleToBuffer s(%s)\n", s.toChars());
auto backref = Backref(null);
scope Mangler v = new Mangler(buf, &backref);
s.accept(v);
}
void mangleToBuffer(TemplateInstance ti, ref OutBuffer buf)
{
//printf("mangleToBuffer ti()\n");
auto backref = Backref(null);
scope Mangler v = new Mangler(buf, &backref);
v.mangleTemplateInstance(ti);
}
/// Returns: `true` if the given character is a valid mangled character
package bool isValidMangling(dchar c) nothrow
{
import dmd.common.charactertables;
return
c >= 'A' && c <= 'Z' ||
c >= 'a' && c <= 'z' ||
c >= '0' && c <= '9' ||
c != 0 && strchr("$%().:?@[]_", c) ||
isAnyIdentifierCharacter(c);
}
// valid mangled characters
unittest
{
assert('a'.isValidMangling);
assert('B'.isValidMangling);
assert('2'.isValidMangling);
assert('@'.isValidMangling);
assert('_'.isValidMangling);
}
// invalid mangled characters
unittest
{
assert(!'-'.isValidMangling);
assert(!0.isValidMangling);
assert(!'/'.isValidMangling);
assert(!'\\'.isValidMangling);
}
/**********************************************
* Convert a string representing a type (the deco) and
* return its equivalent Type.
* Params:
* deco = string containing the deco
* Returns:
* null for failed to convert
* Type for succeeded
*/
public Type decoToType(const(char)[] deco)
{
//printf("decoToType(): %.*s\n", cast(int)deco.length, deco.ptr);
if (auto sv = Type.stringtable.lookup(deco))
{
if (sv.value)
{
Type t = cast(Type)sv.value;
assert(t.deco);
return t;
}
}
return null;
}
/***************************************** private ***************************************/
private:
import core.stdc.ctype;
import core.stdc.stdio;
import core.stdc.string;
import dmd.aggregate;
import dmd.arraytypes;
import dmd.astenums;
import dmd.basicmangle;
import dmd.dclass;
import dmd.declaration;
import dmd.dinterpret;
import dmd.dmodule;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.errors;
import dmd.expression;
import dmd.func;
import dmd.funcsem;
import dmd.globals;
import dmd.id;
import dmd.identifier;
import dmd.mtype;
import dmd.root.ctfloat;
import dmd.common.outbuffer;
import dmd.optimize;
import dmd.root.aav;
import dmd.root.string;
import dmd.root.stringtable;
import dmd.root.utf;
import dmd.target;
import dmd.tokens;
import dmd.visitor;
/************************************************
* Append the mangling of type `t` to `buf`.
* Params:
* t = type to mangle
* modMask = mod bits currently applying to t
* buf = buffer to append mangling to
* backref = state of back references (updated)
*/
void mangleType(Type t, ubyte modMask, ref OutBuffer buf, ref Backref backref)
{
void visitWithMask(Type t, ubyte modMask)
{
void mangleSymbol(Dsymbol s)
{
scope Mangler v = new Mangler(buf, &backref);
v.mangleSymbol(s);
}
void visitType(Type t)
{
tyToDecoBuffer(buf, t.ty);
}
void visitTypeNext(TypeNext t)
{
visitType(t);
visitWithMask(t.next, t.mod);
}
void visitTypeVector(TypeVector t)
{
buf.writestring("Nh");
visitWithMask(t.basetype, t.mod);
}
void visitTypeSArray(TypeSArray t)
{
visitType(t);
if (t.dim)
buf.print(t.dim.toInteger());
if (t.next)
visitWithMask(t.next, t.mod);
}
void visitTypeDArray(TypeDArray t)
{
visitType(t);
if (t.next)
visitWithMask(t.next, t.mod);
}
void visitTypeAArray(TypeAArray t)
{
visitType(t);
visitWithMask(t.index, 0);
visitWithMask(t.next, t.mod);
}
void visitTypeFunction(TypeFunction t)
{
//printf("TypeFunction.toDecoBuffer() t = %p %s\n", t, t.toChars());
//static int nest; if (++nest == 50) *(char*)0=0;
mangleFuncType(t, t, t.mod, t.next, buf, backref);
}
void visitTypeIdentifier(TypeIdentifier t)
{
visitType(t);
auto name = t.ident.toString();
buf.print(cast(int)name.length);
buf.writestring(name);
}
void visitTypeEnum(TypeEnum t)
{
visitType(t);
mangleSymbol(t.sym);
}
void visitTypeStruct(TypeStruct t)
{
//printf("TypeStruct.toDecoBuffer('%s') = '%s'\n", t.toChars(), name);
visitType(t);
mangleSymbol(t.sym);
}
void visitTypeClass(TypeClass t)
{
//printf("TypeClass.toDecoBuffer('%s' mod=%x) = '%s'\n", t.toChars(), mod, name);
visitType(t);
mangleSymbol(t.sym);
}
void visitTypeTuple(TypeTuple t)
{
//printf("TypeTuple.toDecoBuffer() t = %p, %s\n", t, t.toChars());
visitType(t);
Parameter._foreach(t.arguments, (idx, param) {
mangleParameter(param, buf, backref);
return 0;
});
buf.writeByte('Z');
}
void visitTypeNull(TypeNull t)
{
visitType(t);
}
void visitTypeNoreturn(TypeNoreturn t)
{
buf.writestring("Nn");
}
if (modMask != t.mod)
{
MODtoDecoBuffer(buf, t.mod);
}
if (backref.addRefToType(buf, t))
return;
switch (t.ty)
{
case Tpointer:
case Treference:
case Tdelegate:
case Tslice: visitTypeNext (cast(TypeNext)t); break;
case Tarray: visitTypeDArray (t.isTypeDArray()); break;
case Tsarray: visitTypeSArray (t.isTypeSArray()); break;
case Taarray: visitTypeAArray (t.isTypeAArray()); break;
case Tfunction: visitTypeFunction (t.isTypeFunction()); break;
case Tident: visitTypeIdentifier(t.isTypeIdentifier()); break;
case Tclass: visitTypeClass (t.isTypeClass()); break;
case Tstruct: visitTypeStruct (t.isTypeStruct()); break;
case Tenum: visitTypeEnum (t.isTypeEnum()); break;
case Ttuple: visitTypeTuple (t.isTypeTuple()); break;
case Tnull: visitTypeNull (t.isTypeNull()); break;
case Tvector: visitTypeVector (t.isTypeVector()); break;
case Tnoreturn: visitTypeNoreturn (t.isTypeNoreturn); break;
case Terror:
break; // ignore errors
default: visitType(t); break;
}
}
visitWithMask(t, modMask);
}
/*************************************************************
*/
void mangleFuncType(TypeFunction t, TypeFunction ta, ubyte modMask, Type tret, ref OutBuffer buf, ref Backref backref)
{
//printf("mangleFuncType() %s\n", t.toChars());
if (t.inuse && tret)
{
// printf("TypeFunction.mangleFuncType() t = %s inuse\n", t.toChars());
t.inuse = 2; // flag error to caller
return;
}
t.inuse++;
if (modMask != t.mod)
MODtoDecoBuffer(buf, t.mod);
char mc;
final switch (t.linkage)
{
case LINK.default_:
case LINK.d:
mc = 'F';
break;
case LINK.c:
mc = 'U';
break;
case LINK.windows:
mc = 'W';
break;
case LINK.cpp:
mc = 'R';
break;
case LINK.objc:
mc = 'Y';
break;
case LINK.system:
assert(0);
}
buf.writeByte(mc);
if (ta.purity)
buf.writestring("Na");
if (ta.isnothrow)
buf.writestring("Nb");
if (ta.isref)
buf.writestring("Nc");
if (ta.isproperty)
buf.writestring("Nd");
if (ta.isnogc)
buf.writestring("Ni");
// `return scope` must be in that order
if (ta.isreturnscope && !ta.isreturninferred)
{
buf.writestring("NjNl");
}
else
{
// when return ref, the order is `scope return`
if (ta.isScopeQual && !ta.isscopeinferred)
buf.writestring("Nl");
if (ta.isreturn && !ta.isreturninferred)
buf.writestring("Nj");
}
if (ta.islive)
buf.writestring("Nm");
switch (ta.trust)
{
case TRUST.trusted:
buf.writestring("Ne");
break;
case TRUST.safe:
buf.writestring("Nf");
break;
default:
break;
}
// Write argument types
foreach (idx, param; t.parameterList)
mangleParameter(param, buf, backref);
//if (buf.data[buf.length - 1] == '@') assert(0);
buf.writeByte('Z' - t.parameterList.varargs); // mark end of arg list
if (tret !is null)
mangleType(tret, 0, buf, backref);
t.inuse--;
}
/*************************************************************
*/
void mangleParameter(Parameter p, ref OutBuffer buf, ref Backref backref)
{
// https://dlang.org/spec/abi.html#Parameter
auto stc = p.storageClass;
// Inferred storage classes don't get mangled in
if (stc & STC.scopeinferred)
stc &= ~(STC.scope_ | STC.scopeinferred);
if (stc & STC.returninferred)
stc &= ~(STC.return_ | STC.returninferred);
// much like hdrgen.stcToBuffer()
string rrs;
const isout = (stc & STC.out_) != 0;
final switch (buildScopeRef(stc))
{
case ScopeRef.None:
case ScopeRef.Scope:
case ScopeRef.Ref:
case ScopeRef.Return:
case ScopeRef.RefScope:
break;
case ScopeRef.ReturnScope: rrs = "NkM"; goto L1; // return scope
case ScopeRef.ReturnRef: rrs = isout ? "NkJ" : "NkK"; goto L1; // return ref
case ScopeRef.ReturnRef_Scope: rrs = isout ? "MNkJ" : "MNkK"; goto L1; // scope return ref
case ScopeRef.Ref_ReturnScope: rrs = isout ? "NkMJ" : "NkMK"; goto L1; // return scope ref
L1:
buf.writestring(rrs);
stc &= ~(STC.out_ | STC.scope_ | STC.ref_ | STC.return_);
break;
}
if (stc & STC.scope_)
buf.writeByte('M'); // scope
if (stc & STC.return_)
buf.writestring("Nk"); // return
switch (stc & ((STC.IOR | STC.lazy_) & ~STC.constscoperef))
{
case 0:
break;
case STC.in_:
buf.writeByte('I');
break;
case STC.in_ | STC.ref_:
buf.writestring("IK");
break;
case STC.out_:
buf.writeByte('J');
break;
case STC.ref_:
buf.writeByte('K');
break;
case STC.lazy_:
buf.writeByte('L');
break;
default:
debug
{
printf("storageClass = x%llx\n", stc & (STC.IOR | STC.lazy_));
}
assert(0);
}
mangleType(p.type, (stc & STC.in_) ? MODFlags.const_ : 0, buf, backref);
}
private extern (C++) final class Mangler : Visitor
{
alias visit = Visitor.visit;
public:
static assert(Key.sizeof == size_t.sizeof);
OutBuffer* buf;
Backref* backref;
extern (D) this(ref OutBuffer buf, Backref* backref) @trusted
{
this.buf = &buf;
this.backref = backref;
}
void mangleSymbol(Dsymbol s)
{
s.accept(this);
}
void mangleIdentifier(Identifier id, Dsymbol s)
{
if (!backref.addRefToIdentifier(*buf, id))
toBuffer(*buf, id.toString(), s);
}
void mangleInteger(dinteger_t v)
{
if (cast(sinteger_t) v < 0)
{
buf.writeByte('N');
buf.print(-v);
}
else
{
buf.writeByte('i');
buf.print(v);
}
}
////////////////////////////////////////////////////////////////////////////
void mangleDecl(Declaration sthis)
{
mangleParent(sthis);
assert(sthis.ident);
mangleIdentifier(sthis.ident, sthis);
if (FuncDeclaration fd = sthis.isFuncDeclaration())
{
mangleFunc(fd, false);
}
else if (sthis.type)
{
mangleType(sthis.type, 0, *buf, *backref);
}
else
assert(0);
}
void mangleParent(Dsymbol s)
{
//printf("mangleParent() %s %s\n", s.kind(), s.toChars());
Dsymbol p;
if (TemplateInstance ti = s.isTemplateInstance())
p = ti.isTemplateMixin() ? ti.parent : ti.tempdecl.parent;
else
p = s.parent;
if (p)
{
uint localNum = s.localNum;
mangleParent(p);
auto ti = p.isTemplateInstance();
if (ti && !ti.isTemplateMixin())
{
localNum = ti.tempdecl.localNum;
mangleTemplateInstance(ti);
}
else if (p.getIdent())
{
mangleIdentifier(p.ident, s);
if (FuncDeclaration f = p.isFuncDeclaration())
mangleFunc(f, true);
}
else
buf.writeByte('0');
if (localNum)
writeLocalParent(*buf, localNum);
}
}
void mangleFunc(FuncDeclaration fd, bool inParent)
{
//printf("deco = '%s'\n", fd.type.deco ? fd.type.deco : "null");
//printf("fd.type = %s\n", fd.type.toChars());
if (fd.needThis() || fd.isNested())
buf.writeByte('M');
if (!fd.type || fd.type.ty == Terror)
{
// never should have gotten here, but could be the result of
// failed speculative compilation
buf.writestring("9__error__FZ");
//printf("[%s] %s no type\n", fd.loc.toChars(), fd.toChars());
//assert(0); // don't mangle function until semantic3 done.
}
else if (inParent)
{
TypeFunction tf = fd.type.isTypeFunction();
TypeFunction tfo = fd.originalType.isTypeFunction();
mangleFuncType(tf, tfo, 0, null, *buf, *backref);
}
else
{
mangleType(fd.type, 0, *buf, *backref);
}
}
override void visit(Declaration d)
{
//printf("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d)\n",
// d, d.toChars(), d.parent ? d.parent.toChars() : "null", d.linkage);
if (const id = externallyMangledIdentifier(d))
{
buf.writestring(id);
return;
}
buf.writestring("_D");
mangleDecl(d);
debug
{
const slice = (*buf)[];
assert(slice.length);
for (size_t pos; pos < slice.length; )
{
dchar c;
auto ppos = pos;
const s = utf_decodeChar(slice, pos, c);
assert(s is null, s);
assert(c.isValidMangling, "The mangled name '" ~ slice ~ "' " ~
"contains an invalid character: " ~ slice[ppos..pos]);
}
}
}
/******************************************************************************
* Normally FuncDeclaration and FuncAliasDeclaration have overloads.
* If and only if there is no overloads, mangle() could return
* exact mangled name.
*
* module test;
* void foo(long) {} // _D4test3fooFlZv
* void foo(string) {} // _D4test3fooFAyaZv
*
* // from FuncDeclaration.mangle().
* pragma(msg, foo.mangleof); // prints unexact mangled name "4test3foo"
* // by calling Dsymbol.mangle()
*
* // from FuncAliasDeclaration.mangle()
* pragma(msg, __traits(getOverloads, test, "foo")[0].mangleof); // "_D4test3fooFlZv"
* pragma(msg, __traits(getOverloads, test, "foo")[1].mangleof); // "_D4test3fooFAyaZv"
*
* If a function has no overloads, .mangleof property still returns exact mangled name.
*
* void bar() {}
* pragma(msg, bar.mangleof); // still prints "_D4test3barFZv"
* // by calling FuncDeclaration.mangleExact().
*/
override void visit(FuncDeclaration fd)
{
if (fd.isUnique())
mangleExact(fd);
else
visit(cast(Dsymbol)fd);
}
// ditto
override void visit(FuncAliasDeclaration fd)
{
FuncDeclaration f = fd.toAliasFunc();
FuncAliasDeclaration fa = f.isFuncAliasDeclaration();
if (!fd.hasOverloads && !fa)
{
mangleExact(f);
return;
}
if (fa)
{
mangleSymbol(fa);
return;
}
visit(cast(Dsymbol)fd);
}
override void visit(OverDeclaration od)
{
if (od.overnext)
{
visit(cast(Dsymbol)od);
return;
}
if (FuncDeclaration fd = od.aliassym.isFuncDeclaration())
{
if (fd.isUnique())
{
mangleExact(fd);
return;
}
}
if (TemplateDeclaration td = od.aliassym.isTemplateDeclaration())
{
if (td.overnext is null)
{
mangleSymbol(td);
return;
}
}
visit(cast(Dsymbol)od);
}
void mangleExact(FuncDeclaration fd)
{
assert(!fd.isFuncAliasDeclaration());
if (fd.mangleOverride)
{
buf.writestring(fd.mangleOverride);
return;
}
if (fd.isMain())
{
buf.writestring("_Dmain");
return;
}
if (fd.isWinMain() || fd.isDllMain())
{
buf.writestring(fd.ident.toString());
return;
}
visit(cast(Declaration)fd);
}
override void visit(VarDeclaration vd)
{
if (vd.mangleOverride)
{
buf.writestring(vd.mangleOverride);
return;
}
visit(cast(Declaration)vd);
}
override void visit(AggregateDeclaration ad)
{
ClassDeclaration cd = ad.isClassDeclaration();
Dsymbol parentsave = ad.parent;
if (cd)
{
/* These are reserved to the compiler, so keep simple
* names for them.
*/
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)
{
// Don't mangle parent
ad.parent = null;
}
}
visit(cast(Dsymbol)ad);
ad.parent = parentsave;
}
override void visit(TemplateInstance ti)
{
version (none)
{
printf("TemplateInstance.mangle() %p %s", ti, ti.toChars());
if (ti.parent)
printf(" parent = %s %s", ti.parent.kind(), ti.parent.toChars());
printf("\n");
}
if (!ti.tempdecl)
error(ti.loc, "%s `%s` is not defined", ti.kind, ti.toPrettyChars);
else
mangleParent(ti);
if (ti.isTemplateMixin() && ti.ident)
mangleIdentifier(ti.ident, ti);
else
mangleTemplateInstance(ti);
}
void mangleTemplateInstance(TemplateInstance ti)
{
TemplateDeclaration tempdecl = ti.tempdecl.isTemplateDeclaration();
assert(tempdecl);
// Use "__U" for the symbols declared inside template constraint.
const char T = ti.members ? 'T' : 'U';
buf.printf("__%c", T);
mangleIdentifier(tempdecl.ident, tempdecl);
auto args = ti.tiargs;
size_t nparams = tempdecl.parameters.length - (tempdecl.isVariadic() ? 1 : 0);
for (size_t i = 0; i < args.length; i++)
{
auto o = (*args)[i];
Type ta = isType(o);
Expression ea = isExpression(o);
Dsymbol sa = isDsymbol(o);
Tuple va = isTuple(o);
//printf("\to [%d] %p ta %p ea %p sa %p va %p\n", i, o, ta, ea, sa, va);
if (i < nparams && (*tempdecl.parameters)[i].specialization())
buf.writeByte('H'); // https://issues.dlang.org/show_bug.cgi?id=6574
if (ta)
{
buf.writeByte('T');
mangleType(ta, 0, *buf, *backref);
}
else if (ea)
{
// Don't interpret it yet, it might actually be an alias template parameter.
// Only constfold manifest constants, not const/immutable lvalues, see https://issues.dlang.org/show_bug.cgi?id=17339.
enum keepLvalue = true;
ea = ea.optimize(WANTvalue, keepLvalue);
if (auto ev = ea.isVarExp())
{
sa = ev.var;
ea = null;
goto Lsa;
}
if (auto et = ea.isThisExp())
{
sa = et.var;
ea = null;
goto Lsa;
}
if (auto ef = ea.isFuncExp())
{
if (ef.td)
sa = ef.td;
else
sa = ef.fd;
ea = null;
goto Lsa;
}
buf.writeByte('V');
if (ea.op == EXP.tuple)
{
error(ea.loc, "sequence is not a valid template value argument");
continue;
}
// Now that we know it is not an alias, we MUST obtain a value
uint olderr = global.errors;
ea = ea.ctfeInterpret();
if (ea.op == EXP.error || olderr != global.errors)
continue;
/* Use type mangling that matches what it would be for a function parameter
*/
mangleType(ea.type, 0, *buf, *backref);
ea.accept(this);
}
else if (sa)
{
Lsa:
sa = sa.toAlias();
if (sa.isDeclaration() && !sa.isOverDeclaration())
{
Declaration d = sa.isDeclaration();
if (auto fad = d.isFuncAliasDeclaration())
d = fad.toAliasFunc();
if (d.mangleOverride)
{
buf.writeByte('X');
toBuffer(*buf, d.mangleOverride, d);
continue;
}
if (const id = externallyMangledIdentifier(d))
{
buf.writeByte('X');
toBuffer(*buf, id, d);
continue;
}
if (!d.type || !d.type.deco)
{
error(ti.loc, "%s `%s` forward reference of %s `%s`", ti.kind, ti.toPrettyChars, d.kind(), d.toChars());
continue;
}
}
buf.writeByte('S');
mangleSymbol(sa);
}
else if (va)
{
assert(i + 1 == args.length); // must be last one
args = &va.objects;
i = -cast(size_t)1;
}
else
assert(0);
}
buf.writeByte('Z');
}
override void visit(Dsymbol s)
{
version (none)
{
printf("Dsymbol.mangle() '%s'", s.toChars());
if (s.parent)
printf(" parent = %s %s", s.parent.kind(), s.parent.toChars());
printf("\n");
}
if (s.parent && s.ident)
{
if (auto m = s.parent.isModule())
{
if (m.filetype == FileType.c)
{
/* C types at global level get mangled into the __C global namespace
* to get the same mangling regardless of which module it
* is declared in. This works because types are the same if the mangling
* is the same.
*/
mangleIdentifier(Id.ImportC, s); // parent
mangleIdentifier(s.ident, s);
return;
}
}
}
mangleParent(s);
if (s.ident)
mangleIdentifier(s.ident, s);
else
toBuffer(*buf, s.toString(), s);
//printf("Dsymbol.mangle() %s = %s\n", s.toChars(), id);
}
////////////////////////////////////////////////////////////////////////////
override void visit(Expression e)
{
if (!e.type.isTypeError())
error(e.loc, "expression `%s` is not a valid template value argument", e.toChars());
}
override void visit(IntegerExp e)
{
mangleInteger(e.toInteger());
}
override void visit(RealExp e)
{
buf.writeByte('e');
realToMangleBuffer(*buf, e.value);
}
override void visit(ComplexExp e)
{
buf.writeByte('c');
realToMangleBuffer(*buf, e.toReal());
buf.writeByte('c'); // separate the two
realToMangleBuffer(*buf, e.toImaginary());
}
override void visit(NullExp e)
{
buf.writeByte('n');
}
override void visit(StringExp e)
{
char m;
OutBuffer tmp;
const(char)[] q;
void mangleAsArray()
{
buf.writeByte('A');
buf.print(e.len);
foreach (i; 0 .. e.len)
mangleInteger(e.getIndex(i));
}
/* Write string in UTF-8 format
*/
switch (e.sz)
{
case 1:
m = 'a';
q = e.peekString();
break;
case 2:
{
m = 'w';
const slice = e.peekWstring();
for (size_t u = 0; u < e.len;)
{
dchar c;
if (const s = utf_decodeWchar(slice, u, c))
return mangleAsArray();
else
tmp.writeUTF8(c);
}
q = tmp[];
break;
}
case 4:
{
m = 'd';
const slice = e.peekDstring();
foreach (c; slice)
{
if (!utf_isValidDchar(c))
return mangleAsArray();
else
tmp.writeUTF8(c);
}
q = tmp[];
break;