Mercurial > projects > ldc
annotate dmd/mangle.c @ 305:2b72433d5c8c trunk
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
Fixed problems with label collisions when using labels inside inline asm. LabelStatement is now easily reached given its
Identifier, which should be useful elsewhere too.
Enabled inline asm for building the lib/compiler/llvmdc runtime code, fixing branches out of asm makes this possible.
author | lindquist |
---|---|
date | Fri, 27 Jun 2008 22:04:35 +0200 |
parents | 0ab29b838084 |
children | 571959608194 |
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 | |
107 case LINKc: | |
108 case LINKwindows: | |
109 case LINKpascal: | |
110 case LINKcpp: | |
111 return ident->toChars(); | |
112 | |
113 case LINKdefault: | |
114 error("forward declaration"); | |
115 return ident->toChars(); | |
116 | |
117 default: | |
118 fprintf(stdmsg, "'%s', linkage = %d\n", toChars(), linkage); | |
119 assert(0); | |
120 } | |
121 } | |
122 char *p = ::mangle(this); | |
123 OutBuffer buf; | |
124 buf.writestring("_D"); | |
125 buf.writestring(p); | |
126 p = buf.toChars(); | |
127 buf.data = NULL; | |
128 //printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p); | |
129 return p; | |
130 } | |
131 | |
132 char *FuncDeclaration::mangle() | |
133 #if __DMC__ | |
134 __out(result) | |
135 { | |
136 assert(strlen(result) > 0); | |
137 } | |
138 __body | |
139 #endif | |
140 { | |
141 if (isMain()) | |
142 return "_Dmain"; | |
143 | |
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
|
144 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
|
145 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
|
146 |
1 | 147 assert(this); |
148 return Declaration::mangle(); | |
149 } | |
150 | |
151 char *StructDeclaration::mangle() | |
152 { | |
153 //printf("StructDeclaration::mangle() '%s'\n", toChars()); | |
154 return Dsymbol::mangle(); | |
155 } | |
156 | |
157 | |
158 char *TypedefDeclaration::mangle() | |
159 { | |
160 //printf("TypedefDeclaration::mangle() '%s'\n", toChars()); | |
161 return Dsymbol::mangle(); | |
162 } | |
163 | |
164 | |
165 char *ClassDeclaration::mangle() | |
166 { | |
167 Dsymbol *parentsave = parent; | |
168 | |
169 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars()); | |
170 | |
171 /* These are reserved to the compiler, so keep simple | |
172 * names for them. | |
173 */ | |
174 if (ident == Id::Exception) | |
175 { if (parent->ident == Id::object) | |
176 parent = NULL; | |
177 } | |
178 else if (ident == Id::TypeInfo || | |
179 // ident == Id::Exception || | |
180 ident == Id::TypeInfo_Struct || | |
181 ident == Id::TypeInfo_Class || | |
182 ident == Id::TypeInfo_Typedef || | |
183 ident == Id::TypeInfo_Tuple || | |
184 this == object || | |
185 this == classinfo || | |
186 this == Module::moduleinfo || | |
187 memcmp(ident->toChars(), "TypeInfo_", 9) == 0 | |
188 ) | |
189 parent = NULL; | |
190 | |
191 char *id = Dsymbol::mangle(); | |
192 parent = parentsave; | |
193 return id; | |
194 } | |
195 | |
196 | |
197 char *TemplateInstance::mangle() | |
198 { | |
199 OutBuffer buf; | |
200 char *id; | |
201 | |
202 #if 0 | |
203 printf("TemplateInstance::mangle() %s", toChars()); | |
204 if (parent) | |
205 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
206 printf("\n"); | |
207 #endif | |
208 id = ident ? ident->toChars() : toChars(); | |
209 if (tempdecl->parent) | |
210 { | |
211 char *p = tempdecl->parent->mangle(); | |
212 if (p[0] == '_' && p[1] == 'D') | |
213 p += 2; | |
214 buf.writestring(p); | |
215 } | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
139
diff
changeset
|
216 buf.printf("%"PRIuSIZE"%s", strlen(id), id); |
1 | 217 id = buf.toChars(); |
218 buf.data = NULL; | |
219 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id); | |
220 return id; | |
221 } | |
222 | |
223 | |
224 | |
225 char *Dsymbol::mangle() | |
226 { | |
227 OutBuffer buf; | |
228 char *id; | |
229 | |
230 #if 0 | |
231 printf("Dsymbol::mangle() '%s'", toChars()); | |
232 if (parent) | |
233 printf(" parent = %s %s", parent->kind(), parent->toChars()); | |
234 printf("\n"); | |
235 #endif | |
236 id = ident ? ident->toChars() : toChars(); | |
237 if (parent) | |
238 { | |
239 char *p = parent->mangle(); | |
240 if (p[0] == '_' && p[1] == 'D') | |
241 p += 2; | |
242 buf.writestring(p); | |
243 } | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
139
diff
changeset
|
244 buf.printf("%"PRIuSIZE"%s", strlen(id), id); |
1 | 245 id = buf.toChars(); |
246 buf.data = NULL; | |
247 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id); | |
248 return id; | |
249 } | |
250 | |
251 |