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