Mercurial > projects > ldc
annotate dmd/mangle.c @ 1574:72f00d740665
Fix bug #356. Our sorting function was wrong if a value was between two NULL pointers.
author | Benjamin Kramer <benny.kra@gmail.com> |
---|---|
date | Sat, 22 Aug 2009 11:45:20 +0200 |
parents | 8026319762be |
children | def7a1d494fd |
rev | line source |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
1 | 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 | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
27 #if TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
1103
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: |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
120 #if DMDV2 && (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS) |
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(); | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
223 if (!tempdecl) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
224 error("is not defined"); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
225 else if (tempdecl->parent) |
1 | 226 { |
227 char *p = tempdecl->parent->mangle(); | |
228 if (p[0] == '_' && p[1] == 'D') | |
229 p += 2; | |
230 buf.writestring(p); | |
231 } | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
232 buf.printf("%zu%s", strlen(id), id); |
1 | 233 id = buf.toChars(); |
234 buf.data = NULL; | |
235 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id); | |
236 return id; | |
237 } | |
238 | |
239 | |
321 | 240 char *TemplateMixin::mangle() |
241 { | |
242 OutBuffer buf; | |
243 char *id; | |
244 | |
245 #if 0 | |
246 printf("TemplateMixin::mangle() %s", toChars()); | |
247 if (parent) | |
248 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
249 printf("\n"); | |
250 #endif | |
251 id = ident ? ident->toChars() : toChars(); | |
252 if (parent) | |
253 { | |
254 char *p = parent->mangle(); | |
255 if (p[0] == '_' && p[1] == 'D') | |
256 p += 2; | |
257 buf.writestring(p); | |
258 } | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
259 buf.printf("%zu%s", strlen(id), id); |
321 | 260 id = buf.toChars(); |
261 buf.data = NULL; | |
262 //printf("TemplateMixin::mangle() %s = %s\n", toChars(), id); | |
263 return id; | |
264 } | |
1 | 265 |
266 char *Dsymbol::mangle() | |
267 { | |
268 OutBuffer buf; | |
269 char *id; | |
270 | |
271 #if 0 | |
272 printf("Dsymbol::mangle() '%s'", toChars()); | |
273 if (parent) | |
274 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
275 printf("\n"); | |
276 #endif | |
277 id = ident ? ident->toChars() : toChars(); | |
278 if (parent) | |
279 { | |
280 char *p = parent->mangle(); | |
281 if (p[0] == '_' && p[1] == 'D') | |
282 p += 2; | |
283 buf.writestring(p); | |
284 } | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
723
diff
changeset
|
285 buf.printf("%zu%s", strlen(id), id); |
1 | 286 id = buf.toChars(); |
287 buf.data = NULL; | |
288 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id); | |
289 return id; | |
290 } | |
291 | |
292 |