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
)
80 buf
.writestring(sthis
->type
->deco
);
82 { assert(fd
->inferRetType
);
90 char *Declaration::mangle()
94 int len
= strlen(result
);
97 //printf("mangle: '%s' => '%s'\n", toChars(), result);
98 for (int i
= 0; i
< len
; i
++)
100 assert(result
[i
] == '_' ||
102 isalnum(result
[i
]) || result
[i
] & 0x80);
108 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent->toChars() : "null", linkage);
109 if (!parent
|| parent
->isModule() || linkage
== LINKcpp
) // if at global scope
111 // If it's not a D declaration, no mangling
120 return ident
->toChars();
123 #if IN_GCC || TARGET_LINUX
124 return cpp_mangle(this);
126 // Windows C++ mangling is done by C++ back end
127 return ident
->toChars();
131 error("forward declaration");
132 return ident
->toChars();
135 fprintf(stdmsg
, "'%s', linkage = %d\n", toChars(), linkage
);
139 char *p
= ::mangle(this);
141 buf
.writestring("_D");
145 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
149 char *FuncDeclaration::mangle()
153 assert(strlen(result
) > 0);
161 if (isWinMain() || isDllMain())
162 return ident
->toChars();
165 return Declaration::mangle();
169 char *StructDeclaration::mangle()
171 //printf("StructDeclaration::mangle() '%s'\n", toChars());
172 return Dsymbol::mangle();
176 char *TypedefDeclaration::mangle()
178 //printf("TypedefDeclaration::mangle() '%s'\n", toChars());
179 return Dsymbol::mangle();
183 char *ClassDeclaration::mangle()
185 Dsymbol
*parentsave
= parent
;
187 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars());
189 /* These are reserved to the compiler, so keep simple
192 if (ident
== Id::Exception
)
193 { if (parent
->ident
== Id::object
)
196 else if (ident
== Id::TypeInfo
||
197 // ident == Id::Exception ||
198 ident
== Id::TypeInfo_Struct
||
199 ident
== Id::TypeInfo_Class
||
200 ident
== Id::TypeInfo_Typedef
||
201 ident
== Id::TypeInfo_Tuple
||
204 this == Module::moduleinfo
||
205 memcmp(ident
->toChars(), "TypeInfo_", 9) == 0
209 char *id
= Dsymbol::mangle();
215 char *TemplateInstance::mangle()
221 printf("TemplateInstance::mangle() %s", toChars());
223 printf(" parent = %s %s", parent
->kind(), parent
->toChars());
226 id
= ident
? ident
->toChars() : toChars();
227 if (tempdecl
->parent
)
229 char *p
= tempdecl
->parent
->mangle();
230 if (p
[0] == '_' && p
[1] == 'D')
234 buf
.printf("%"PRIuSIZE
"%s", strlen(id
), id
);
237 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
243 char *Dsymbol::mangle()
249 printf("Dsymbol::mangle() '%s'", toChars());
251 printf(" parent = %s %s", parent
->kind(), parent
->toChars());
254 id
= ident
? ident
->toChars() : toChars();
257 char *p
= parent
->mangle();
258 if (p
[0] == '_' && p
[1] == 'D')
262 buf
.printf("%"PRIuSIZE
"%s", strlen(id
), id
);
265 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);