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