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