Mercurial > projects > ldc
annotate dmd/mangle.c @ 883:b52d5de7783f
GC defines and linkage changes.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Thu, 08 Jan 2009 18:20:02 +0100 |
parents | 55f6c2e454d7 |
children | b30fe7e1dbb9 |
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 | |
27 char *mangle(Declaration *sthis) | |
28 { | |
29 OutBuffer buf; | |
30 char *id; | |
31 Dsymbol *s; | |
32 | |
33 //printf("::mangle(%s)\n", sthis->toChars()); | |
34 s = sthis; | |
35 do | |
36 { | |
37 //printf("mangle: s = %p, '%s', parent = %p\n", s, s->toChars(), s->parent); | |
38 if (s->ident) | |
39 { | |
40 FuncDeclaration *fd = s->isFuncDeclaration(); | |
41 if (s != sthis && fd) | |
42 { | |
43 id = mangle(fd); | |
44 buf.prependstring(id); | |
45 goto L1; | |
46 } | |
47 else | |
48 { | |
49 id = s->ident->toChars(); | |
50 int len = strlen(id); | |
51 char tmp[sizeof(len) * 3 + 1]; | |
52 buf.prependstring(id); | |
53 sprintf(tmp, "%d", len); | |
54 buf.prependstring(tmp); | |
55 } | |
56 } | |
57 else | |
58 buf.prependstring("0"); | |
59 s = s->parent; | |
60 } while (s); | |
61 | |
62 // buf.prependstring("_D"); | |
63 L1: | |
64 //printf("deco = '%s'\n", sthis->type->deco ? sthis->type->deco : "null"); | |
65 //printf("sthis->type = %s\n", sthis->type->toChars()); | |
66 FuncDeclaration *fd = sthis->isFuncDeclaration(); | |
67 if (fd && (fd->needThis() || fd->isNested())) | |
68 buf.writeByte(Type::needThisPrefix()); | |
69 if (sthis->type->deco) | |
70 buf.writestring(sthis->type->deco); | |
71 else | |
72 { assert(fd->inferRetType); | |
73 } | |
74 | |
75 id = buf.toChars(); | |
76 buf.data = NULL; | |
77 return id; | |
78 } | |
79 | |
80 char *Declaration::mangle() | |
81 #if __DMC__ | |
82 __out(result) | |
83 { | |
84 int len = strlen(result); | |
85 | |
86 assert(len > 0); | |
87 //printf("mangle: '%s' => '%s'\n", toChars(), result); | |
88 for (int i = 0; i < len; i++) | |
89 { | |
90 assert(result[i] == '_' || | |
91 result[i] == '@' || | |
92 isalnum(result[i]) || result[i] & 0x80); | |
93 } | |
94 } | |
95 __body | |
96 #endif | |
97 { | |
98 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent->toChars() : "null", linkage); | |
99 if (!parent || parent->isModule()) // if at global scope | |
100 { | |
101 // If it's not a D declaration, no mangling | |
102 switch (linkage) | |
103 { | |
104 case LINKd: | |
105 break; | |
106 | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
321
diff
changeset
|
107 // LDC |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
321
diff
changeset
|
108 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
|
109 |
1 | 110 case LINKc: |
111 case LINKwindows: | |
112 case LINKpascal: | |
113 case LINKcpp: | |
114 return ident->toChars(); | |
115 | |
116 case LINKdefault: | |
117 error("forward declaration"); | |
118 return ident->toChars(); | |
119 | |
120 default: | |
121 fprintf(stdmsg, "'%s', linkage = %d\n", toChars(), linkage); | |
122 assert(0); | |
123 } | |
124 } | |
125 char *p = ::mangle(this); | |
126 OutBuffer buf; | |
127 buf.writestring("_D"); | |
128 buf.writestring(p); | |
129 p = buf.toChars(); | |
130 buf.data = NULL; | |
131 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p); | |
132 return p; | |
133 } | |
134 | |
135 char *FuncDeclaration::mangle() | |
136 #if __DMC__ | |
137 __out(result) | |
138 { | |
139 assert(strlen(result) > 0); | |
140 } | |
141 __body | |
142 #endif | |
143 { | |
144 if (isMain()) | |
145 return "_Dmain"; | |
146 | |
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
|
147 if (isWinMain() || isDllMain()) |
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
|
148 return ident->toChars(); |
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
|
149 |
1 | 150 assert(this); |
151 return Declaration::mangle(); | |
152 } | |
153 | |
154 char *StructDeclaration::mangle() | |
155 { | |
156 //printf("StructDeclaration::mangle() '%s'\n", toChars()); | |
157 return Dsymbol::mangle(); | |
158 } | |
159 | |
160 | |
161 char *TypedefDeclaration::mangle() | |
162 { | |
163 //printf("TypedefDeclaration::mangle() '%s'\n", toChars()); | |
164 return Dsymbol::mangle(); | |
165 } | |
166 | |
167 | |
168 char *ClassDeclaration::mangle() | |
169 { | |
170 Dsymbol *parentsave = parent; | |
171 | |
172 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars()); | |
173 | |
174 /* These are reserved to the compiler, so keep simple | |
175 * names for them. | |
176 */ | |
177 if (ident == Id::Exception) | |
178 { if (parent->ident == Id::object) | |
179 parent = NULL; | |
180 } | |
181 else if (ident == Id::TypeInfo || | |
182 // ident == Id::Exception || | |
183 ident == Id::TypeInfo_Struct || | |
184 ident == Id::TypeInfo_Class || | |
185 ident == Id::TypeInfo_Typedef || | |
186 ident == Id::TypeInfo_Tuple || | |
187 this == object || | |
188 this == classinfo || | |
189 this == Module::moduleinfo || | |
190 memcmp(ident->toChars(), "TypeInfo_", 9) == 0 | |
191 ) | |
192 parent = NULL; | |
193 | |
194 char *id = Dsymbol::mangle(); | |
195 parent = parentsave; | |
196 return id; | |
197 } | |
198 | |
199 | |
200 char *TemplateInstance::mangle() | |
201 { | |
202 OutBuffer buf; | |
203 char *id; | |
204 | |
205 #if 0 | |
206 printf("TemplateInstance::mangle() %s", toChars()); | |
207 if (parent) | |
208 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
209 printf("\n"); | |
210 #endif | |
211 id = ident ? ident->toChars() : toChars(); | |
212 if (tempdecl->parent) | |
213 { | |
214 char *p = tempdecl->parent->mangle(); | |
215 if (p[0] == '_' && p[1] == 'D') | |
216 p += 2; | |
217 buf.writestring(p); | |
218 } | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
139
diff
changeset
|
219 buf.printf("%"PRIuSIZE"%s", strlen(id), id); |
1 | 220 id = buf.toChars(); |
221 buf.data = NULL; | |
222 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id); | |
223 return id; | |
224 } | |
225 | |
226 | |
321 | 227 char *TemplateMixin::mangle() |
228 { | |
229 OutBuffer buf; | |
230 char *id; | |
231 | |
232 #if 0 | |
233 printf("TemplateMixin::mangle() %s", toChars()); | |
234 if (parent) | |
235 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
236 printf("\n"); | |
237 #endif | |
238 id = ident ? ident->toChars() : toChars(); | |
239 if (parent) | |
240 { | |
241 char *p = parent->mangle(); | |
242 if (p[0] == '_' && p[1] == 'D') | |
243 p += 2; | |
244 buf.writestring(p); | |
245 } | |
246 buf.printf("%"PRIuSIZE"%s", strlen(id), id); | |
247 id = buf.toChars(); | |
248 buf.data = NULL; | |
249 //printf("TemplateMixin::mangle() %s = %s\n", toChars(), id); | |
250 return id; | |
251 } | |
1 | 252 |
253 char *Dsymbol::mangle() | |
254 { | |
255 OutBuffer buf; | |
256 char *id; | |
257 | |
258 #if 0 | |
259 printf("Dsymbol::mangle() '%s'", toChars()); | |
260 if (parent) | |
261 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
262 printf("\n"); | |
263 #endif | |
264 id = ident ? ident->toChars() : toChars(); | |
265 if (parent) | |
266 { | |
267 char *p = parent->mangle(); | |
268 if (p[0] == '_' && p[1] == 'D') | |
269 p += 2; | |
270 buf.writestring(p); | |
271 } | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
139
diff
changeset
|
272 buf.printf("%"PRIuSIZE"%s", strlen(id), id); |
1 | 273 id = buf.toChars(); |
274 buf.data = NULL; | |
275 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id); | |
276 return id; | |
277 } | |
278 | |
279 |