1587
|
1
|
|
2 // Compiler implementation of the D programming language
|
|
3 // Copyright (c) 1999-2009 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 || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS
|
|
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 {
|
|
77 #ifdef DEBUG
|
|
78 if (!fd->inferRetType)
|
|
79 printf("%s\n", fd->toChars());
|
|
80 #endif
|
|
81 assert(fd && fd->inferRetType);
|
|
82 }
|
|
83
|
|
84 id = buf.toChars();
|
|
85 buf.data = NULL;
|
|
86 return id;
|
|
87 }
|
|
88
|
|
89 char *Declaration::mangle()
|
|
90 #if __DMC__
|
|
91 __out(result)
|
|
92 {
|
|
93 int len = strlen(result);
|
|
94
|
|
95 assert(len > 0);
|
|
96 //printf("mangle: '%s' => '%s'\n", toChars(), result);
|
|
97 for (int i = 0; i < len; i++)
|
|
98 {
|
|
99 assert(result[i] == '_' ||
|
|
100 result[i] == '@' ||
|
|
101 isalnum(result[i]) || result[i] & 0x80);
|
|
102 }
|
|
103 }
|
|
104 __body
|
|
105 #endif
|
|
106 {
|
|
107 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent->toChars() : "null", linkage);
|
|
108 if (!parent || parent->isModule()) // if at global scope
|
|
109 {
|
|
110 // If it's not a D declaration, no mangling
|
|
111 switch (linkage)
|
|
112 {
|
|
113 case LINKd:
|
|
114 break;
|
|
115
|
|
116 // LDC
|
|
117 case LINKintrinsic:
|
|
118
|
|
119 case LINKc:
|
|
120 case LINKwindows:
|
|
121 case LINKpascal:
|
|
122 return ident->toChars();
|
|
123
|
|
124 case LINKcpp:
|
|
125 #if DMDV2 && (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS)
|
|
126 return cpp_mangle(this);
|
|
127 #else
|
|
128 // Windows C++ mangling is done by C++ back end
|
|
129 return ident->toChars();
|
|
130 #endif
|
|
131
|
|
132 case LINKdefault:
|
|
133 error("forward declaration");
|
|
134 return ident->toChars();
|
|
135
|
|
136 default:
|
|
137 fprintf(stdmsg, "'%s', linkage = %d\n", toChars(), linkage);
|
|
138 assert(0);
|
|
139 }
|
|
140 }
|
|
141 char *p = ::mangle(this);
|
|
142 OutBuffer buf;
|
|
143 buf.writestring("_D");
|
|
144 buf.writestring(p);
|
|
145 p = buf.toChars();
|
|
146 buf.data = NULL;
|
|
147 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
|
|
148 return p;
|
|
149 }
|
|
150
|
|
151 char *FuncDeclaration::mangle()
|
|
152 #if __DMC__
|
|
153 __out(result)
|
|
154 {
|
|
155 assert(strlen(result) > 0);
|
|
156 }
|
|
157 __body
|
|
158 #endif
|
|
159 {
|
|
160 if (isMain())
|
|
161 return (char *)"_Dmain";
|
|
162
|
|
163 if (isWinMain() || isDllMain())
|
|
164 return ident->toChars();
|
|
165
|
|
166 assert(this);
|
|
167 return Declaration::mangle();
|
|
168 }
|
|
169
|
|
170 char *StructDeclaration::mangle()
|
|
171 {
|
|
172 //printf("StructDeclaration::mangle() '%s'\n", toChars());
|
|
173 return Dsymbol::mangle();
|
|
174 }
|
|
175
|
|
176
|
|
177 char *TypedefDeclaration::mangle()
|
|
178 {
|
|
179 //printf("TypedefDeclaration::mangle() '%s'\n", toChars());
|
|
180 return Dsymbol::mangle();
|
|
181 }
|
|
182
|
|
183
|
|
184 char *ClassDeclaration::mangle()
|
|
185 {
|
|
186 Dsymbol *parentsave = parent;
|
|
187
|
|
188 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars());
|
|
189
|
|
190 /* These are reserved to the compiler, so keep simple
|
|
191 * names for them.
|
|
192 */
|
|
193 if (ident == Id::Exception)
|
|
194 { if (parent->ident == Id::object)
|
|
195 parent = NULL;
|
|
196 }
|
|
197 else if (ident == Id::TypeInfo ||
|
|
198 // ident == Id::Exception ||
|
|
199 ident == Id::TypeInfo_Struct ||
|
|
200 ident == Id::TypeInfo_Class ||
|
|
201 ident == Id::TypeInfo_Typedef ||
|
|
202 ident == Id::TypeInfo_Tuple ||
|
|
203 this == object ||
|
|
204 this == classinfo ||
|
|
205 this == Module::moduleinfo ||
|
|
206 memcmp(ident->toChars(), "TypeInfo_", 9) == 0
|
|
207 )
|
|
208 parent = NULL;
|
|
209
|
|
210 char *id = Dsymbol::mangle();
|
|
211 parent = parentsave;
|
|
212 return id;
|
|
213 }
|
|
214
|
|
215
|
|
216 char *TemplateInstance::mangle()
|
|
217 {
|
|
218 OutBuffer buf;
|
|
219 char *id;
|
|
220
|
|
221 #if 0
|
|
222 printf("TemplateInstance::mangle() %s", toChars());
|
|
223 if (parent)
|
|
224 printf(" parent = %s %s", parent->kind(), parent->toChars());
|
|
225 printf("\n");
|
|
226 #endif
|
|
227 id = ident ? ident->toChars() : toChars();
|
|
228 if (!tempdecl)
|
|
229 error("is not defined");
|
|
230 else if (tempdecl->parent)
|
|
231 {
|
|
232 char *p = tempdecl->parent->mangle();
|
|
233 if (p[0] == '_' && p[1] == 'D')
|
|
234 p += 2;
|
|
235 buf.writestring(p);
|
|
236 }
|
|
237 buf.printf("%zu%s", strlen(id), id);
|
|
238 id = buf.toChars();
|
|
239 buf.data = NULL;
|
|
240 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
|
|
241 return id;
|
|
242 }
|
|
243
|
|
244
|
|
245 char *TemplateMixin::mangle()
|
|
246 {
|
|
247 OutBuffer buf;
|
|
248 char *id;
|
|
249
|
|
250 #if 0
|
|
251 printf("TemplateMixin::mangle() %s", toChars());
|
|
252 if (parent)
|
|
253 printf(" parent = %s %s", parent->kind(), parent->toChars());
|
|
254 printf("\n");
|
|
255 #endif
|
|
256 id = ident ? ident->toChars() : toChars();
|
|
257 if (parent)
|
|
258 {
|
|
259 char *p = parent->mangle();
|
|
260 if (p[0] == '_' && p[1] == 'D')
|
|
261 p += 2;
|
|
262 buf.writestring(p);
|
|
263 }
|
|
264 buf.printf("%zu%s", strlen(id), id);
|
|
265 id = buf.toChars();
|
|
266 buf.data = NULL;
|
|
267 //printf("TemplateMixin::mangle() %s = %s\n", toChars(), id);
|
|
268 return id;
|
|
269 }
|
|
270
|
|
271 char *Dsymbol::mangle()
|
|
272 {
|
|
273 OutBuffer buf;
|
|
274 char *id;
|
|
275
|
|
276 #if 0
|
|
277 printf("Dsymbol::mangle() '%s'", toChars());
|
|
278 if (parent)
|
|
279 printf(" parent = %s %s", parent->kind(), parent->toChars());
|
|
280 printf("\n");
|
|
281 #endif
|
|
282 id = ident ? ident->toChars() : toChars();
|
|
283 if (parent)
|
|
284 {
|
|
285 char *p = parent->mangle();
|
|
286 if (p[0] == '_' && p[1] == 'D')
|
|
287 p += 2;
|
|
288 buf.writestring(p);
|
|
289 }
|
|
290 buf.printf("%zu%s", strlen(id), id);
|
|
291 id = buf.toChars();
|
|
292 buf.data = NULL;
|
|
293 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);
|
|
294 return id;
|
|
295 }
|
|
296
|
|
297
|