comparison dmd2/mangle.c @ 758:f04dde6e882c

Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:38:48 +0100
parents
children 638d16625da2
comparison
equal deleted inserted replaced
757:2c730d530c98 758:f04dde6e882c
1
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.
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <assert.h>
15
16 #include "root.h"
17
18 #include "init.h"
19 #include "declaration.h"
20 #include "aggregate.h"
21 #include "mtype.h"
22 #include "attrib.h"
23 #include "template.h"
24 #include "id.h"
25 #include "module.h"
26
27 #if TARGET_LINUX
28 char *cpp_mangle(Dsymbol *s);
29 #endif
30
31 char *mangle(Declaration *sthis)
32 {
33 OutBuffer buf;
34 char *id;
35 Dsymbol *s;
36
37 //printf("::mangle(%s)\n", sthis->toChars());
38 s = sthis;
39 do
40 {
41 //printf("mangle: s = %p, '%s', parent = %p\n", s, s->toChars(), s->parent);
42 if (s->ident)
43 {
44 FuncDeclaration *fd = s->isFuncDeclaration();
45 if (s != sthis && fd)
46 {
47 id = mangle(fd);
48 buf.prependstring(id);
49 goto L1;
50 }
51 else
52 {
53 id = s->ident->toChars();
54 int len = strlen(id);
55 char tmp[sizeof(len) * 3 + 1];
56 buf.prependstring(id);
57 sprintf(tmp, "%d", len);
58 buf.prependstring(tmp);
59 }
60 }
61 else
62 buf.prependstring("0");
63 s = s->parent;
64 } while (s);
65
66 // buf.prependstring("_D");
67 L1:
68 //printf("deco = '%s'\n", sthis->type->deco ? sthis->type->deco : "null");
69 //printf("sthis->type = %s\n", sthis->type->toChars());
70 FuncDeclaration *fd = sthis->isFuncDeclaration();
71 if (fd && (fd->needThis() || fd->isNested()))
72 buf.writeByte(Type::needThisPrefix());
73 if (sthis->type->deco)
74 buf.writestring(sthis->type->deco);
75 else
76 { assert(fd->inferRetType);
77 }
78
79 id = buf.toChars();
80 buf.data = NULL;
81 return id;
82 }
83
84 char *Declaration::mangle()
85 #if __DMC__
86 __out(result)
87 {
88 int len = strlen(result);
89
90 assert(len > 0);
91 //printf("mangle: '%s' => '%s'\n", toChars(), result);
92 for (int i = 0; i < len; i++)
93 {
94 assert(result[i] == '_' ||
95 result[i] == '@' ||
96 isalnum(result[i]) || result[i] & 0x80);
97 }
98 }
99 __body
100 #endif
101 {
102 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent->toChars() : "null", linkage);
103 if (!parent || parent->isModule() || linkage == LINKcpp) // if at global scope
104 {
105 // If it's not a D declaration, no mangling
106 switch (linkage)
107 {
108 case LINKd:
109 break;
110
111 // LDC
112 case LINKintrinsic:
113
114 case LINKc:
115 case LINKwindows:
116 case LINKpascal:
117 return ident->toChars();
118
119 case LINKcpp:
120 #if TARGET_LINUX
121 return cpp_mangle(this);
122 #else
123 // Windows C++ mangling is done by C++ back end
124 return ident->toChars();
125 #endif
126
127 case LINKdefault:
128 error("forward declaration");
129 return ident->toChars();
130
131 default:
132 fprintf(stdmsg, "'%s', linkage = %d\n", toChars(), linkage);
133 assert(0);
134 }
135 }
136 char *p = ::mangle(this);
137 OutBuffer buf;
138 buf.writestring("_D");
139 buf.writestring(p);
140 p = buf.toChars();
141 buf.data = NULL;
142 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
143 return p;
144 }
145
146 char *FuncDeclaration::mangle()
147 #if __DMC__
148 __out(result)
149 {
150 assert(strlen(result) > 0);
151 }
152 __body
153 #endif
154 {
155 if (isMain())
156 return (char *)"_Dmain";
157
158 if (isWinMain() || isDllMain())
159 return ident->toChars();
160
161 assert(this);
162 return Declaration::mangle();
163 }
164
165
166 char *StructDeclaration::mangle()
167 {
168 //printf("StructDeclaration::mangle() '%s'\n", toChars());
169 return Dsymbol::mangle();
170 }
171
172
173 char *TypedefDeclaration::mangle()
174 {
175 //printf("TypedefDeclaration::mangle() '%s'\n", toChars());
176 return Dsymbol::mangle();
177 }
178
179
180 char *ClassDeclaration::mangle()
181 {
182 Dsymbol *parentsave = parent;
183
184 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars());
185
186 /* These are reserved to the compiler, so keep simple
187 * names for them.
188 */
189 if (ident == Id::Exception)
190 { if (parent->ident == Id::object)
191 parent = NULL;
192 }
193 else if (ident == Id::TypeInfo ||
194 // ident == Id::Exception ||
195 ident == Id::TypeInfo_Struct ||
196 ident == Id::TypeInfo_Class ||
197 ident == Id::TypeInfo_Typedef ||
198 ident == Id::TypeInfo_Tuple ||
199 this == object ||
200 this == classinfo ||
201 this == Module::moduleinfo ||
202 memcmp(ident->toChars(), "TypeInfo_", 9) == 0
203 )
204 parent = NULL;
205
206 char *id = Dsymbol::mangle();
207 parent = parentsave;
208 return id;
209 }
210
211
212 char *TemplateInstance::mangle()
213 {
214 OutBuffer buf;
215 char *id;
216
217 #if 0
218 printf("TemplateInstance::mangle() %s", toChars());
219 if (parent)
220 printf(" parent = %s %s", parent->kind(), parent->toChars());
221 printf("\n");
222 #endif
223 id = ident ? ident->toChars() : toChars();
224 if (tempdecl->parent)
225 {
226 char *p = tempdecl->parent->mangle();
227 if (p[0] == '_' && p[1] == 'D')
228 p += 2;
229 buf.writestring(p);
230 }
231 buf.printf("%"PRIuSIZE"%s", strlen(id), id);
232 id = buf.toChars();
233 buf.data = NULL;
234 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
235 return id;
236 }
237
238
239 char *TemplateMixin::mangle()
240 {
241 OutBuffer buf;
242 char *id;
243
244 #if 0
245 printf("TemplateMixin::mangle() %s", toChars());
246 if (parent)
247 printf(" parent = %s %s", parent->kind(), parent->toChars());
248 printf("\n");
249 #endif
250 id = ident ? ident->toChars() : toChars();
251 if (parent)
252 {
253 char *p = parent->mangle();
254 if (p[0] == '_' && p[1] == 'D')
255 p += 2;
256 buf.writestring(p);
257 }
258 buf.printf("%"PRIuSIZE"%s", strlen(id), id);
259 id = buf.toChars();
260 buf.data = NULL;
261 //printf("TemplateMixin::mangle() %s = %s\n", toChars(), id);
262 return id;
263 }
264
265 char *Dsymbol::mangle()
266 {
267 OutBuffer buf;
268 char *id;
269
270 #if 0
271 printf("Dsymbol::mangle() '%s'", toChars());
272 if (parent)
273 printf(" parent = %s %s", parent->kind(), parent->toChars());
274 printf("\n");
275 #endif
276 id = ident ? ident->toChars() : toChars();
277 if (parent)
278 {
279 char *p = parent->mangle();
280 if (p[0] == '_' && p[1] == 'D')
281 p += 2;
282 buf.writestring(p);
283 }
284 buf.printf("%"PRIuSIZE"%s", strlen(id), id);
285 id = buf.toChars();
286 buf.data = NULL;
287 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);
288 return id;
289 }
290
291