comparison dmd/mangle.c @ 1:c53b6e3fe49a trunk

[svn r5] Initial commit. Most things are very rough.
author lindquist
date Sat, 01 Sep 2007 21:43:27 +0200
parents
children 0ab29b838084
comparison
equal deleted inserted replaced
0:a9e71648e74d 1:c53b6e3fe49a
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
144 assert(this);
145
146 return Declaration::mangle();
147 }
148
149 char *StructDeclaration::mangle()
150 {
151 //printf("StructDeclaration::mangle() '%s'\n", toChars());
152 return Dsymbol::mangle();
153 }
154
155
156 char *TypedefDeclaration::mangle()
157 {
158 //printf("TypedefDeclaration::mangle() '%s'\n", toChars());
159 return Dsymbol::mangle();
160 }
161
162
163 char *ClassDeclaration::mangle()
164 {
165 Dsymbol *parentsave = parent;
166
167 //printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars());
168
169 /* These are reserved to the compiler, so keep simple
170 * names for them.
171 */
172 if (ident == Id::Exception)
173 { if (parent->ident == Id::object)
174 parent = NULL;
175 }
176 else if (ident == Id::TypeInfo ||
177 // ident == Id::Exception ||
178 ident == Id::TypeInfo_Struct ||
179 ident == Id::TypeInfo_Class ||
180 ident == Id::TypeInfo_Typedef ||
181 ident == Id::TypeInfo_Tuple ||
182 this == object ||
183 this == classinfo ||
184 this == Module::moduleinfo ||
185 memcmp(ident->toChars(), "TypeInfo_", 9) == 0
186 )
187 parent = NULL;
188
189 char *id = Dsymbol::mangle();
190 parent = parentsave;
191 return id;
192 }
193
194
195 char *TemplateInstance::mangle()
196 {
197 OutBuffer buf;
198 char *id;
199
200 #if 0
201 printf("TemplateInstance::mangle() %s", toChars());
202 if (parent)
203 printf(" parent = %s %s", parent->kind(), parent->toChars());
204 printf("\n");
205 #endif
206 id = ident ? ident->toChars() : toChars();
207 if (tempdecl->parent)
208 {
209 char *p = tempdecl->parent->mangle();
210 if (p[0] == '_' && p[1] == 'D')
211 p += 2;
212 buf.writestring(p);
213 }
214 buf.printf("%zu%s", strlen(id), id);
215 id = buf.toChars();
216 buf.data = NULL;
217 //printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
218 return id;
219 }
220
221
222
223 char *Dsymbol::mangle()
224 {
225 OutBuffer buf;
226 char *id;
227
228 #if 0
229 printf("Dsymbol::mangle() '%s'", toChars());
230 if (parent)
231 printf(" parent = %s %s", parent->kind(), parent->toChars());
232 printf("\n");
233 #endif
234 id = ident ? ident->toChars() : toChars();
235 if (parent)
236 {
237 char *p = parent->mangle();
238 if (p[0] == '_' && p[1] == 'D')
239 p += 2;
240 buf.writestring(p);
241 }
242 buf.printf("%zu%s", strlen(id), id);
243 id = buf.toChars();
244 buf.data = NULL;
245 //printf("Dsymbol::mangle() %s = %s\n", toChars(), id);
246 return id;
247 }
248
249