Mercurial > projects > ldc
annotate dmd/mangle.c @ 1351:8d501abecd24
Initial (but disabled) fix for ticket #294 , the actual part that fixes the bug is in a #if 0 block as I'm afraid it will cause regressions. I'm most likely not going to be around tonight, and maybe not tomorrow as well, so I'm pushing it in case someone wants to run some serious testing/investigate the problem noted in llvmhelpers.cpp : realignOffset .
author | Tomas Lindquist Olsen <tomas.l.olsen gmail com> |
---|---|
date | Thu, 14 May 2009 17:20:17 +0200 |
parents | e961851fb8be |
children | 8026319762be |
rev | line source |
---|---|
1 | 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 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
27 #if TARGET_LINUX || TARGET_OSX |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
28 char *cpp_mangle(Dsymbol *s); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
29 #endif |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
30 |
1 | 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()) // 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 | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
321
diff
changeset
|
111 // LDC |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
321
diff
changeset
|
112 case LINKintrinsic: |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
321
diff
changeset
|
113 |
1 | 114 case LINKc: |
115 case LINKwindows: | |
116 case LINKpascal: | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
117 return ident->toChars(); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
118 |
1 | 119 case LINKcpp: |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
120 #if DMDV2 && (TARGET_LINUX || TARGET_OSX) |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
121 return cpp_mangle(this); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
122 #else |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
123 // Windows C++ mangling is done by C++ back end |
1 | 124 return ident->toChars(); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
125 #endif |
1 | 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()) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
156 return (char *)"_Dmain"; |
1 | 157 |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
158 if (isWinMain() || isDllMain()) |
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
159 return ident->toChars(); |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
1
diff
changeset
|
160 |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
161 assert(this); |
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
162 return Declaration::mangle(); |
1 | 163 } |
164 | |
165 char *StructDeclaration::mangle() | |
166 { | |
167 //printf("StructDeclaration::mangle() '%s'\n", toChars()); | |
168 return Dsymbol::mangle(); | |
169 } | |
170 | |
171 | |
172 char *TypedefDeclaration::mangle() | |
173 { | |
174 //printf("TypedefDeclaration::mangle() '%s'\n", toChars()); | |
175 return Dsymbol::mangle(); | |
176 } | |
177 | |
178 | |
179 char *ClassDeclaration::mangle() | |
180 { | |
181 Dsymbol *parentsave = parent; | |
182 | |
183 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars()); | |
184 | |
185 /* These are reserved to the compiler, so keep simple | |
186 * names for them. | |
187 */ | |
188 if (ident == Id::Exception) | |
189 { if (parent->ident == Id::object) | |
190 parent = NULL; | |
191 } | |
192 else if (ident == Id::TypeInfo || | |
193 // ident == Id::Exception || | |
194 ident == Id::TypeInfo_Struct || | |
195 ident == Id::TypeInfo_Class || | |
196 ident == Id::TypeInfo_Typedef || | |
197 ident == Id::TypeInfo_Tuple || | |
198 this == object || | |
199 this == classinfo || | |
200 this == Module::moduleinfo || | |
201 memcmp(ident->toChars(), "TypeInfo_", 9) == 0 | |
202 ) | |
203 parent = NULL; | |
204 | |
205 char *id = Dsymbol::mangle(); | |
206 parent = parentsave; | |
207 return id; | |
208 } | |
209 | |
210 | |
211 char *TemplateInstance::mangle() | |
212 { | |
213 OutBuffer buf; | |
214 char *id; | |
215 | |
216 #if 0 | |
217 printf("TemplateInstance::mangle() %s", toChars()); | |
218 if (parent) | |
219 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
220 printf("\n"); | |
221 #endif | |
222 id = ident ? ident->toChars() : toChars(); | |
223 if (tempdecl->parent) | |
224 { | |
225 char *p = tempdecl->parent->mangle(); | |
226 if (p[0] == '_' && p[1] == 'D') | |
227 p += 2; | |
228 buf.writestring(p); | |
229 } | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
230 buf.printf("%zu%s", strlen(id), id); |
1 | 231 id = buf.toChars(); |
232 buf.data = NULL; | |
233 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id); | |
234 return id; | |
235 } | |
236 | |
237 | |
321 | 238 char *TemplateMixin::mangle() |
239 { | |
240 OutBuffer buf; | |
241 char *id; | |
242 | |
243 #if 0 | |
244 printf("TemplateMixin::mangle() %s", toChars()); | |
245 if (parent) | |
246 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
247 printf("\n"); | |
248 #endif | |
249 id = ident ? ident->toChars() : toChars(); | |
250 if (parent) | |
251 { | |
252 char *p = parent->mangle(); | |
253 if (p[0] == '_' && p[1] == 'D') | |
254 p += 2; | |
255 buf.writestring(p); | |
256 } | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
257 buf.printf("%zu%s", strlen(id), id); |
321 | 258 id = buf.toChars(); |
259 buf.data = NULL; | |
260 //printf("TemplateMixin::mangle() %s = %s\n", toChars(), id); | |
261 return id; | |
262 } | |
1 | 263 |
264 char *Dsymbol::mangle() | |
265 { | |
266 OutBuffer buf; | |
267 char *id; | |
268 | |
269 #if 0 | |
270 printf("Dsymbol::mangle() '%s'", toChars()); | |
271 if (parent) | |
272 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
273 printf("\n"); | |
274 #endif | |
275 id = ident ? ident->toChars() : toChars(); | |
276 if (parent) | |
277 { | |
278 char *p = parent->mangle(); | |
279 if (p[0] == '_' && p[1] == 'D') | |
280 p += 2; | |
281 buf.writestring(p); | |
282 } | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
283 buf.printf("%zu%s", strlen(id), id); |
1 | 284 id = buf.toChars(); |
285 buf.data = NULL; | |
286 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id); | |
287 return id; | |
288 } | |
289 | |
290 |