2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2007 by Digital Mars
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
11 /* NOTE: This file has been patched from the original DMD distribution to
12 work with the GDC compiler.
14 Modified by David Friedman, December 2006
25 #include "declaration.h"
26 #include "aggregate.h"
33 char *mangle(Declaration
*sthis
)
39 //printf("::mangle(%s)\n", sthis->toChars());
43 //printf("mangle: s = %p, '%s', parent = %p\n", s, s->toChars(), s->parent);
46 FuncDeclaration
*fd
= s
->isFuncDeclaration();
50 buf
.prependstring(id
);
55 id
= s
->ident
->toChars();
57 char tmp
[sizeof(len
) * 3 + 1];
58 buf
.prependstring(id
);
59 sprintf(tmp
, "%d", len
);
60 buf
.prependstring(tmp
);
64 buf
.prependstring("0");
68 // buf.prependstring("_D");
70 //printf("deco = '%s'\n", sthis->type->deco ? sthis->type->deco : "null");
71 //printf("sthis->type = %s\n", sthis->type->toChars());
72 FuncDeclaration
*fd
= sthis
->isFuncDeclaration();
73 if (fd
&& (fd
->needThis() || fd
->isNested()))
74 buf
.writeByte(Type::needThisPrefix());
75 if (sthis
->type
->deco
)
77 /* It's too hard to figure out when a symbol came from D and when from Dlt,
78 * so strip the maybe IDs.
80 char *p
= sthis
->type
->deco
;
88 { assert(fd
->inferRetType
);
96 char *Declaration::mangle()
100 int len
= strlen(result
);
103 //printf("mangle: '%s' => '%s'\n", toChars(), result);
104 for (int i
= 0; i
< len
; i
++)
106 assert(result
[i
] == '_' ||
108 isalnum(result
[i
]) || result
[i
] & 0x80);
114 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent->toChars() : "null", linkage);
115 if (!parent
|| parent
->isModule()) // if at global scope
117 // If it's not a D declaration, no mangling
127 return ident
->toChars();
130 error("forward declaration");
131 return ident
->toChars();
134 fprintf(stdmsg
, "'%s', linkage = %d\n", toChars(), linkage
);
138 char *p
= ::mangle(this);
140 buf
.writestring("_D");
144 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
148 char *FuncDeclaration::mangle()
152 assert(strlen(result
) > 0);
160 if (isWinMain() || isDllMain())
161 return ident
->toChars();
164 return Declaration::mangle();
167 char *StructDeclaration::mangle()
169 //printf("StructDeclaration::mangle() '%s'\n", toChars());
170 return Dsymbol::mangle();
174 char *TypedefDeclaration::mangle()
176 //printf("TypedefDeclaration::mangle() '%s'\n", toChars());
177 return Dsymbol::mangle();
181 char *ClassDeclaration::mangle()
183 Dsymbol
*parentsave
= parent
;
185 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars());
187 /* These are reserved to the compiler, so keep simple
190 if (ident
== Id::Exception
)
191 { if (parent
->ident
== Id::object
)
194 else if (ident
== Id::TypeInfo
||
195 // ident == Id::Exception ||
196 ident
== Id::TypeInfo_Struct
||
197 ident
== Id::TypeInfo_Class
||
198 ident
== Id::TypeInfo_Typedef
||
199 ident
== Id::TypeInfo_Tuple
||
202 this == Module::moduleinfo
||
203 memcmp(ident
->toChars(), "TypeInfo_", 9) == 0
207 char *id
= Dsymbol::mangle();
213 char *TemplateInstance::mangle()
219 printf("TemplateInstance::mangle() %s", toChars());
221 printf(" parent = %s %s", parent
->kind(), parent
->toChars());
224 id
= ident
? ident
->toChars() : toChars();
225 if (tempdecl
->parent
)
227 char *p
= tempdecl
->parent
->mangle();
228 if (p
[0] == '_' && p
[1] == 'D')
232 buf
.printf("%"PRIuSIZE
"%s", strlen(id
), id
);
235 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
241 char *Dsymbol::mangle()
247 printf("Dsymbol::mangle() '%s'", toChars());
249 printf(" parent = %s %s", parent
->kind(), parent
->toChars());
252 id
= ident
? ident
->toChars() : toChars();
255 char *p
= parent
->mangle();
256 if (p
[0] == '_' && p
[1] == 'D')
260 buf
.printf("%"PRIuSIZE
"%s", strlen(id
), id
);
263 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);