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 #if IN_GCC || TARGET_LINUX
34 char *cpp_mangle(Dsymbol
*s
);
37 char *mangle(Declaration
*sthis
)
43 //printf("::mangle(%s)\n", sthis->toChars());
47 //printf("mangle: s = %p, '%s', parent = %p\n", s, s->toChars(), s->parent);
50 FuncDeclaration
*fd
= s
->isFuncDeclaration();
54 buf
.prependstring(id
);
59 id
= s
->ident
->toChars();
61 char tmp
[sizeof(len
) * 3 + 1];
62 buf
.prependstring(id
);
63 sprintf(tmp
, "%d", len
);
64 buf
.prependstring(tmp
);
68 buf
.prependstring("0");
72 // buf.prependstring("_D");
74 //printf("deco = '%s'\n", sthis->type->deco ? sthis->type->deco : "null");
75 //printf("sthis->type = %s\n", sthis->type->toChars());
76 FuncDeclaration
*fd
= sthis
->isFuncDeclaration();
77 if (fd
&& (fd
->needThis() || fd
->isNested()))
78 buf
.writeByte(Type::needThisPrefix());
79 if (sthis
->type
->deco
)
81 /* It's too hard to figure out when a symbol came from D and when from Dlt,
82 * so strip the maybe IDs.
84 char *p
= sthis
->type
->deco
;
92 { assert(fd
->inferRetType
);
100 char *Declaration::mangle()
104 int len
= strlen(result
);
107 //printf("mangle: '%s' => '%s'\n", toChars(), result);
108 for (int i
= 0; i
< len
; i
++)
110 assert(result
[i
] == '_' ||
112 isalnum(result
[i
]) || result
[i
] & 0x80);
118 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent->toChars() : "null", linkage);
119 if (!parent
|| parent
->isModule() || // if at global scope
120 linkage
== LINKcpp
||
121 linkage
== LINKgobject
)
123 // If it's not a D declaration, no mangling
132 return ident
->toChars();
135 { ClassDeclaration
*cd
= parent
->isClassDeclaration();
139 char *cp
= cd
->ident
->toChars();
141 while (char c
= *cp
) {
146 buf
.writeByte(tolower(c
));
157 buf
.writestring(ident
->toChars());
158 char *p
= buf
.toChars();
163 return ident
->toChars();
166 #if IN_GCC || TARGET_LINUX
167 return cpp_mangle(this);
169 // Windows C++ mangling is done by C++ back end
170 return ident
->toChars();
174 error("forward declaration");
175 return ident
->toChars();
178 fprintf(stdmsg
, "'%s', linkage = %d\n", toChars(), linkage
);
182 char *p
= ::mangle(this);
184 buf
.writestring("_D");
188 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
192 char *FuncDeclaration::mangle()
196 assert(strlen(result
) > 0);
204 if (isWinMain() || isDllMain())
205 return ident
->toChars();
208 return Declaration::mangle();
212 char *StructDeclaration::mangle()
214 //printf("StructDeclaration::mangle() '%s'\n", toChars());
215 return Dsymbol::mangle();
219 char *TypedefDeclaration::mangle()
221 //printf("TypedefDeclaration::mangle() '%s'\n", toChars());
222 return Dsymbol::mangle();
226 char *ClassDeclaration::mangle()
228 Dsymbol
*parentsave
= parent
;
230 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars());
232 /* These are reserved to the compiler, so keep simple
235 if (ident
== Id::Exception
)
236 { if (parent
->ident
== Id::object
)
239 else if (ident
== Id::TypeInfo
||
240 // ident == Id::Exception ||
241 ident
== Id::TypeInfo_Struct
||
242 ident
== Id::TypeInfo_Class
||
243 ident
== Id::TypeInfo_Typedef
||
244 ident
== Id::TypeInfo_Tuple
||
247 this == Module::moduleinfo
||
248 memcmp(ident
->toChars(), "TypeInfo_", 9) == 0
252 char *id
= Dsymbol::mangle();
258 char *TemplateInstance::mangle()
264 printf("TemplateInstance::mangle() %s", toChars());
266 printf(" parent = %s %s", parent
->kind(), parent
->toChars());
269 id
= ident
? ident
->toChars() : toChars();
270 if (tempdecl
->parent
)
272 char *p
= tempdecl
->parent
->mangle();
273 if (p
[0] == '_' && p
[1] == 'D')
277 buf
.printf("%"PRIuSIZE
"%s", strlen(id
), id
);
280 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
286 char *Dsymbol::mangle()
292 printf("Dsymbol::mangle() '%s'", toChars());
294 printf(" parent = %s %s", parent
->kind(), parent
->toChars());
297 id
= ident
? ident
->toChars() : toChars();
300 char *p
= parent
->mangle();
301 if (p
[0] == '_' && p
[1] == 'D')
305 buf
.printf("%"PRIuSIZE
"%s", strlen(id
), id
);
308 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);