Fixed semi-colon parsing in C-style for loops
[delight/core.git] / dmd / mangle.c
blob5f8b5c4ff994261ab97e8538dac3969ec0df3a9a
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 char *mangle(Declaration *sthis)
35 OutBuffer buf;
36 char *id;
37 Dsymbol *s;
39 //printf("::mangle(%s)\n", sthis->toChars());
40 s = sthis;
43 //printf("mangle: s = %p, '%s', parent = %p\n", s, s->toChars(), s->parent);
44 if (s->ident)
46 FuncDeclaration *fd = s->isFuncDeclaration();
47 if (s != sthis && fd)
49 id = mangle(fd);
50 buf.prependstring(id);
51 goto L1;
53 else
55 id = s->ident->toChars();
56 int len = strlen(id);
57 char tmp[sizeof(len) * 3 + 1];
58 buf.prependstring(id);
59 sprintf(tmp, "%d", len);
60 buf.prependstring(tmp);
63 else
64 buf.prependstring("0");
65 s = s->parent;
66 } while (s);
68 // buf.prependstring("_D");
69 L1:
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;
81 while (*p) {
82 if (*p != '?')
83 buf.writebyte(*p);
84 p++;
87 else
88 { assert(fd->inferRetType);
91 id = buf.toChars();
92 buf.data = NULL;
93 return id;
96 char *Declaration::mangle()
97 #if __DMC__
98 __out(result)
100 int len = strlen(result);
102 assert(len > 0);
103 //printf("mangle: '%s' => '%s'\n", toChars(), result);
104 for (int i = 0; i < len; i++)
106 assert(result[i] == '_' ||
107 result[i] == '@' ||
108 isalnum(result[i]) || result[i] & 0x80);
111 __body
112 #endif
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
118 switch (linkage)
120 case LINKd:
121 break;
123 case LINKc:
124 case LINKwindows:
125 case LINKpascal:
126 case LINKcpp:
127 return ident->toChars();
129 case LINKdefault:
130 error("forward declaration");
131 return ident->toChars();
133 default:
134 fprintf(stdmsg, "'%s', linkage = %d\n", toChars(), linkage);
135 assert(0);
138 char *p = ::mangle(this);
139 OutBuffer buf;
140 buf.writestring("_D");
141 buf.writestring(p);
142 p = buf.toChars();
143 buf.data = NULL;
144 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
145 return p;
148 char *FuncDeclaration::mangle()
149 #if __DMC__
150 __out(result)
152 assert(strlen(result) > 0);
154 __body
155 #endif
157 if (isMain())
158 return "_Dmain";
160 if (isWinMain() || isDllMain())
161 return ident->toChars();
163 assert(this);
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
188 * names for them.
190 if (ident == Id::Exception)
191 { if (parent->ident == Id::object)
192 parent = NULL;
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 ||
200 this == object ||
201 this == classinfo ||
202 this == Module::moduleinfo ||
203 memcmp(ident->toChars(), "TypeInfo_", 9) == 0
205 parent = NULL;
207 char *id = Dsymbol::mangle();
208 parent = parentsave;
209 return id;
213 char *TemplateInstance::mangle()
215 OutBuffer buf;
216 char *id;
218 #if 0
219 printf("TemplateInstance::mangle() %s", toChars());
220 if (parent)
221 printf(" parent = %s %s", parent->kind(), parent->toChars());
222 printf("\n");
223 #endif
224 id = ident ? ident->toChars() : toChars();
225 if (tempdecl->parent)
227 char *p = tempdecl->parent->mangle();
228 if (p[0] == '_' && p[1] == 'D')
229 p += 2;
230 buf.writestring(p);
232 buf.printf("%"PRIuSIZE"%s", strlen(id), id);
233 id = buf.toChars();
234 buf.data = NULL;
235 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
236 return id;
241 char *Dsymbol::mangle()
243 OutBuffer buf;
244 char *id;
246 #if 0
247 printf("Dsymbol::mangle() '%s'", toChars());
248 if (parent)
249 printf(" parent = %s %s", parent->kind(), parent->toChars());
250 printf("\n");
251 #endif
252 id = ident ? ident->toChars() : toChars();
253 if (parent)
255 char *p = parent->mangle();
256 if (p[0] == '_' && p[1] == 'D')
257 p += 2;
258 buf.writestring(p);
260 buf.printf("%"PRIuSIZE"%s", strlen(id), id);
261 id = buf.toChars();
262 buf.data = NULL;
263 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);
264 return id;