Fixed parsing of maybe types in statements
[delight/core.git] / dmd2 / mangle.c
blobe853e97f9ade8d30b31cca8f7c36c7e5fdcb6b3d
2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2007 by Digital Mars
4 // All Rights Reserved
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
17 #include <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include <assert.h>
22 #include "root.h"
24 #include "init.h"
25 #include "declaration.h"
26 #include "aggregate.h"
27 #include "mtype.h"
28 #include "attrib.h"
29 #include "template.h"
30 #include "id.h"
31 #include "module.h"
33 #if IN_GCC || TARGET_LINUX
34 char *cpp_mangle(Dsymbol *s);
35 #endif
37 char *mangle(Declaration *sthis)
39 OutBuffer buf;
40 char *id;
41 Dsymbol *s;
43 //printf("::mangle(%s)\n", sthis->toChars());
44 s = sthis;
47 //printf("mangle: s = %p, '%s', parent = %p\n", s, s->toChars(), s->parent);
48 if (s->ident)
50 FuncDeclaration *fd = s->isFuncDeclaration();
51 if (s != sthis && fd)
53 id = mangle(fd);
54 buf.prependstring(id);
55 goto L1;
57 else
59 id = s->ident->toChars();
60 int len = strlen(id);
61 char tmp[sizeof(len) * 3 + 1];
62 buf.prependstring(id);
63 sprintf(tmp, "%d", len);
64 buf.prependstring(tmp);
67 else
68 buf.prependstring("0");
69 s = s->parent;
70 } while (s);
72 // buf.prependstring("_D");
73 L1:
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);
81 else
82 { assert(fd->inferRetType);
85 id = buf.toChars();
86 buf.data = NULL;
87 return id;
90 char *Declaration::mangle()
91 #if __DMC__
92 __out(result)
94 int len = strlen(result);
96 assert(len > 0);
97 //printf("mangle: '%s' => '%s'\n", toChars(), result);
98 for (int i = 0; i < len; i++)
100 assert(result[i] == '_' ||
101 result[i] == '@' ||
102 isalnum(result[i]) || result[i] & 0x80);
105 __body
106 #endif
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
112 switch (linkage)
114 case LINKd:
115 break;
117 case LINKc:
118 case LINKwindows:
119 case LINKpascal:
120 return ident->toChars();
122 case LINKcpp:
123 #if IN_GCC || TARGET_LINUX
124 return cpp_mangle(this);
125 #else
126 // Windows C++ mangling is done by C++ back end
127 return ident->toChars();
128 #endif
130 case LINKdefault:
131 error("forward declaration");
132 return ident->toChars();
134 default:
135 fprintf(stdmsg, "'%s', linkage = %d\n", toChars(), linkage);
136 assert(0);
139 char *p = ::mangle(this);
140 OutBuffer buf;
141 buf.writestring("_D");
142 buf.writestring(p);
143 p = buf.toChars();
144 buf.data = NULL;
145 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
146 return p;
149 char *FuncDeclaration::mangle()
150 #if __DMC__
151 __out(result)
153 assert(strlen(result) > 0);
155 __body
156 #endif
158 if (isMain())
159 return "_Dmain";
161 if (isWinMain() || isDllMain())
162 return ident->toChars();
164 assert(this);
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
190 * names for them.
192 if (ident == Id::Exception)
193 { if (parent->ident == Id::object)
194 parent = NULL;
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 ||
202 this == object ||
203 this == classinfo ||
204 this == Module::moduleinfo ||
205 memcmp(ident->toChars(), "TypeInfo_", 9) == 0
207 parent = NULL;
209 char *id = Dsymbol::mangle();
210 parent = parentsave;
211 return id;
215 char *TemplateInstance::mangle()
217 OutBuffer buf;
218 char *id;
220 #if 0
221 printf("TemplateInstance::mangle() %s", toChars());
222 if (parent)
223 printf(" parent = %s %s", parent->kind(), parent->toChars());
224 printf("\n");
225 #endif
226 id = ident ? ident->toChars() : toChars();
227 if (tempdecl->parent)
229 char *p = tempdecl->parent->mangle();
230 if (p[0] == '_' && p[1] == 'D')
231 p += 2;
232 buf.writestring(p);
234 buf.printf("%"PRIuSIZE"%s", strlen(id), id);
235 id = buf.toChars();
236 buf.data = NULL;
237 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
238 return id;
243 char *Dsymbol::mangle()
245 OutBuffer buf;
246 char *id;
248 #if 0
249 printf("Dsymbol::mangle() '%s'", toChars());
250 if (parent)
251 printf(" parent = %s %s", parent->kind(), parent->toChars());
252 printf("\n");
253 #endif
254 id = ident ? ident->toChars() : toChars();
255 if (parent)
257 char *p = parent->mangle();
258 if (p[0] == '_' && p[1] == 'D')
259 p += 2;
260 buf.writestring(p);
262 buf.printf("%"PRIuSIZE"%s", strlen(id), id);
263 id = buf.toChars();
264 buf.data = NULL;
265 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);
266 return id;