Mercurial > projects > ddmd
annotate dmd/Declaration.d @ 14:2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sun, 04 Apr 2010 02:06:32 +0100 |
parents | 10317f0c89a5 |
children | 5c9b78899f5d |
rev | line source |
---|---|
0 | 1 module dmd.Declaration; |
2 | |
3 import dmd.Dsymbol; | |
4 import dmd.Type; | |
5 import dmd.PROT; | |
6 import dmd.LINK; | |
7 import dmd.Identifier; | |
8 import dmd.Scope; | |
9 import dmd.Loc; | |
10 import dmd.STC; | |
11 import dmd.FuncDeclaration; | |
12 import dmd.VarDeclaration; | |
13 import dmd.OutBuffer; | |
14 | |
15 import std.stdio : writef; | |
16 | |
17 import core.stdc.ctype; | |
18 import core.stdc.stdio : sprintf; | |
19 | |
20 string mangle(Declaration sthis) | |
21 { | |
22 scope OutBuffer buf = new OutBuffer(); | |
23 | |
24 string id; | |
25 Dsymbol s = sthis; | |
26 | |
27 //printf(".mangle(%s)\n", sthis.toChars()); | |
28 do | |
29 { | |
30 //printf("mangle: s = %p, '%s', parent = %p\n", s, s.toChars(), s.parent); | |
31 if (s.ident) | |
32 { | |
33 FuncDeclaration fd = s.isFuncDeclaration(); | |
34 if (s !is sthis && fd) | |
35 { | |
36 id = mangle(fd); | |
37 buf.prependstring(id); | |
38 goto L1; | |
39 } | |
40 else | |
41 { | |
42 id = s.ident.toChars(); | |
43 int len = id.length; | |
44 char tmp[len.sizeof * 3 + 1]; | |
45 buf.prependstring(id); | |
46 len = sprintf(tmp.ptr, "%d".ptr, len); | |
47 buf.prependstring(tmp[0..len]); | |
48 } | |
49 } | |
50 else | |
51 buf.prependstring("0"); | |
52 s = s.parent; | |
53 } while (s); | |
54 | |
55 // buf.prependstring("_D"); | |
56 L1: | |
57 //printf("deco = '%s'\n", sthis.type.deco ? sthis.type.deco : "null"); | |
58 //printf("sthis.type = %s\n", sthis.type.toChars()); | |
59 FuncDeclaration fd = sthis.isFuncDeclaration(); | |
60 if (fd && (fd.needThis() || fd.isNested())) | |
61 buf.writeByte(Type.needThisPrefix()); | |
62 if (sthis.type.deco) | |
63 buf.writestring(sthis.type.deco); | |
64 else | |
65 { | |
66 debug { | |
67 if (!fd.inferRetType) | |
68 writef("%s\n", fd.toChars()); | |
69 } | |
70 assert(fd.inferRetType); | |
71 } | |
72 | |
73 id = buf.extractString(); | |
74 return id; | |
75 } | |
76 | |
77 class Declaration : Dsymbol | |
78 { | |
79 Type type; | |
80 Type originalType; // before semantic analysis | |
81 STC storage_class = STC.STCundefined; | |
82 PROT protection = PROT.PROTundefined; | |
83 LINK linkage = LINK.LINKdefault; | |
84 int inuse; // used to detect cycles | |
85 | |
86 this(Identifier id) | |
87 { | |
88 super(id); | |
89 } | |
90 | |
91 void semantic(Scope sc) | |
92 { | |
93 assert(false); | |
94 } | |
95 | |
96 string kind() | |
97 { | |
98 assert(false); | |
99 } | |
100 | |
101 uint size(Loc loc) | |
102 { | |
103 assert(false); | |
104 } | |
105 | |
106 /************************************* | |
107 * Check to see if declaration can be modified in this context (sc). | |
108 * Issue error if not. | |
109 */ | |
110 void checkModify(Loc loc, Scope sc, Type t) | |
111 { | |
112 if (sc.incontract && isParameter()) | |
113 error(loc, "cannot modify parameter '%s' in contract", toChars()); | |
114 | |
115 if (isCtorinit()) | |
116 { | |
117 // It's only modifiable if inside the right constructor | |
118 Dsymbol s = sc.func; | |
119 while (true) | |
120 { | |
121 FuncDeclaration fd = null; | |
122 if (s) | |
123 fd = s.isFuncDeclaration(); | |
124 if (fd && ((fd.isCtorDeclaration() && storage_class & STC.STCfield) || | |
125 (fd.isStaticCtorDeclaration() && !(storage_class & STC.STCfield))) && | |
126 fd.toParent() == toParent() | |
127 ) | |
128 { | |
129 VarDeclaration v = isVarDeclaration(); | |
130 assert(v); | |
131 v.ctorinit = 1; | |
132 //printf("setting ctorinit\n"); | |
133 } | |
134 else | |
135 { | |
136 if (s) | |
137 { | |
138 s = s.toParent2(); | |
139 continue; | |
140 } | |
141 else | |
142 { | |
143 string p = isStatic() ? "static " : ""; | |
144 error(loc, "can only initialize %sconst %s inside %sconstructor", p, toChars(), p); | |
145 } | |
146 } | |
147 break; | |
148 } | |
149 } | |
150 else | |
151 { | |
152 VarDeclaration v = isVarDeclaration(); | |
153 if (v && v.canassign == 0) | |
154 { | |
155 string p = null; | |
156 if (isConst()) | |
157 p = "const"; | |
158 else if (isInvariant()) | |
159 p = "immutable"; | |
160 else if (storage_class & STC.STCmanifest) | |
161 p = "enum"; | |
162 else if (!t.isAssignable()) | |
163 p = "struct with immutable members"; | |
164 if (p) | |
165 { | |
166 error(loc, "cannot modify %s", p); | |
167 } | |
168 } | |
169 } | |
170 } | |
171 | |
172 void emitComment(Scope sc) | |
173 { | |
174 assert(false); | |
175 } | |
176 | |
177 void toDocBuffer(OutBuffer buf) | |
178 { | |
179 assert(false); | |
180 } | |
181 | |
182 string mangle() | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
183 /+out (result) |
0 | 184 { |
185 try | |
186 { | |
187 int len = result.length; | |
188 | |
189 assert(len > 0); | |
190 //printf("mangle: '%s' => '%s'\n", toChars(), result); | |
191 for (int i = 0; i < len; i++) | |
192 { | |
193 assert(result[i] == '_' || result[i] == '@' || isalnum(result[i]) || result[i] & 0x80); | |
194 } | |
195 } catch { | |
196 writef("Incorrect mangle: '%s'\n", result); | |
197 assert(false); | |
198 } | |
199 } | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
200 body+/ |
0 | 201 { |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
202 version(Bug3602) { writef( "Bug3602: Uncomment outblock when fixed" ); } |
0 | 203 //writef("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent.toChars() : "null", linkage); |
204 if (!parent || parent.isModule() || linkage == LINK.LINKcpp) // if at global scope | |
205 { | |
206 // If it's not a D declaration, no mangling | |
207 switch (linkage) | |
208 { | |
209 case LINK.LINKd: | |
210 break; | |
211 | |
212 case LINK.LINKc: | |
213 case LINK.LINKwindows: | |
214 case LINK.LINKpascal: | |
215 return ident.toChars(); | |
216 | |
217 case LINK.LINKcpp: | |
218 version (CPP_MANGLE) { | |
219 return cpp_mangle(this); | |
220 } else { | |
221 // Windows C++ mangling is done by C++ back end | |
222 return ident.toChars(); | |
223 } | |
224 | |
225 case LINK.LINKdefault: | |
226 assert(false); | |
227 error("forward declaration"); | |
228 return ident.toChars(); | |
229 | |
230 default: | |
231 writef("'%s', linkage = %d\n", toChars(), linkage); | |
232 assert(0); | |
233 } | |
234 } | |
235 | |
236 string p = .mangle(this); | |
237 scope OutBuffer buf = new OutBuffer(); | |
238 buf.writestring("_D"); | |
239 buf.writestring(p); | |
240 p = buf.toChars(); | |
241 buf.data = null; | |
242 //writef("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent.toChars() : "null", linkage, p); | |
243 return p; | |
244 } | |
245 | |
246 int isStatic() { return storage_class & STC.STCstatic; } | |
247 | |
248 bool isStaticConstructor() | |
249 { | |
250 return false; | |
251 } | |
252 | |
253 bool isStaticDestructor() | |
254 { | |
255 return false; | |
256 } | |
257 | |
258 bool isDelete() | |
259 { | |
260 return false; | |
261 } | |
262 | |
263 bool isDataseg() | |
264 { | |
265 return false; | |
266 } | |
267 | |
268 bool isThreadlocal() | |
269 { | |
270 assert(false); | |
271 } | |
272 | |
273 bool isCodeseg() | |
274 { | |
275 return false; | |
276 } | |
277 | |
278 int isCtorinit() { return storage_class & STC.STCctorinit; } | |
279 | |
280 int isFinal() { return storage_class & STC.STCfinal; } | |
281 | |
282 bool isAbstract() { return (storage_class & STC.STCabstract) != 0; } | |
283 | |
284 bool isConst() { return (storage_class & STC.STCconst) != 0; } | |
285 | |
286 int isInvariant() { return storage_class & STC.STCinvariant; } | |
287 | |
288 int isAuto() { return storage_class & STC.STCauto; } | |
289 | |
290 int isScope() { return storage_class & (STC.STCscope | STC.STCauto); } | |
291 | |
292 int isSynchronized() { return storage_class & STC.STCsynchronized; } | |
293 | |
294 int isParameter() { return storage_class & STC.STCparameter; } | |
295 | |
296 bool isDeprecated() { return (storage_class & STC.STCdeprecated) != 0; } | |
297 | |
298 int isOverride() { return storage_class & STC.STCoverride; } | |
299 | |
300 int isIn() { return storage_class & STC.STCin; } | |
301 | |
302 int isOut() { return storage_class & STC.STCout; } | |
303 | |
304 int isRef() { return storage_class & STC.STCref; } | |
305 | |
306 PROT prot() | |
307 { | |
308 return protection; | |
309 } | |
310 | |
311 Declaration isDeclaration() { return this; } | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
312 } |