Mercurial > projects > ddmd
annotate dmd/Declaration.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | 9f4e5ac4f0a3 |
children | 83a36bdd5d14 |
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 { | |
91 Type type; | |
92 Type originalType; // before semantic analysis | |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
93 StorageClass storage_class = STC.STCundefined; |
0 | 94 PROT protection = PROT.PROTundefined; |
95 LINK linkage = LINK.LINKdefault; | |
96 int inuse; // used to detect cycles | |
97 | |
98 this(Identifier id) | |
99 { | |
178 | 100 register(); |
0 | 101 super(id); |
102 } | |
103 | |
72 | 104 override void semantic(Scope sc) |
0 | 105 { |
106 } | |
107 | |
72 | 108 override string kind() |
0 | 109 { |
110 assert(false); | |
111 } | |
112 | |
72 | 113 override uint size(Loc loc) |
0 | 114 { |
115 assert(false); | |
116 } | |
117 | |
118 /************************************* | |
119 * Check to see if declaration can be modified in this context (sc). | |
120 * Issue error if not. | |
121 */ | |
122 void checkModify(Loc loc, Scope sc, Type t) | |
123 { | |
124 if (sc.incontract && isParameter()) | |
125 error(loc, "cannot modify parameter '%s' in contract", toChars()); | |
126 | |
127 if (isCtorinit()) | |
128 { | |
129 // It's only modifiable if inside the right constructor | |
130 Dsymbol s = sc.func; | |
131 while (true) | |
132 { | |
133 FuncDeclaration fd = null; | |
134 if (s) | |
135 fd = s.isFuncDeclaration(); | |
136 if (fd && ((fd.isCtorDeclaration() && storage_class & STC.STCfield) || | |
137 (fd.isStaticCtorDeclaration() && !(storage_class & STC.STCfield))) && | |
138 fd.toParent() == toParent() | |
139 ) | |
140 { | |
141 VarDeclaration v = isVarDeclaration(); | |
142 assert(v); | |
143 v.ctorinit = 1; | |
144 //printf("setting ctorinit\n"); | |
145 } | |
146 else | |
147 { | |
148 if (s) | |
149 { | |
150 s = s.toParent2(); | |
151 continue; | |
152 } | |
153 else | |
154 { | |
155 string p = isStatic() ? "static " : ""; | |
156 error(loc, "can only initialize %sconst %s inside %sconstructor", p, toChars(), p); | |
157 } | |
158 } | |
159 break; | |
160 } | |
161 } | |
162 else | |
163 { | |
164 VarDeclaration v = isVarDeclaration(); | |
165 if (v && v.canassign == 0) | |
166 { | |
167 string p = null; | |
168 if (isConst()) | |
169 p = "const"; | |
135 | 170 else if (isImmutable()) |
0 | 171 p = "immutable"; |
172 else if (storage_class & STC.STCmanifest) | |
173 p = "enum"; | |
174 else if (!t.isAssignable()) | |
175 p = "struct with immutable members"; | |
176 if (p) | |
177 { | |
178 error(loc, "cannot modify %s", p); | |
179 } | |
180 } | |
181 } | |
182 } | |
183 | |
72 | 184 override void emitComment(Scope sc) |
0 | 185 { |
186 assert(false); | |
187 } | |
188 | |
79 | 189 override void toJsonBuffer(OutBuffer buf) |
190 { | |
191 //writef("Declaration.toJsonBuffer()\n"); | |
192 buf.writestring("{\n"); | |
193 | |
194 JsonProperty(buf, Pname, toChars()); | |
195 JsonProperty(buf, Pkind, kind()); | |
196 if (type) | |
197 JsonProperty(buf, Ptype, type.toChars()); | |
198 | |
199 if (comment) | |
200 JsonProperty(buf, Pcomment, comment); | |
201 | |
202 if (loc.linnum) | |
203 JsonProperty(buf, Pline, loc.linnum); | |
204 | |
205 TypedefDeclaration td = isTypedefDeclaration(); | |
206 if (td) | |
207 { | |
208 JsonProperty(buf, "base", td.basetype.toChars()); | |
209 } | |
210 | |
211 JsonRemoveComma(buf); | |
212 buf.writestring("}\n"); | |
213 } | |
214 | |
72 | 215 override void toDocBuffer(OutBuffer buf) |
0 | 216 { |
217 assert(false); | |
218 } | |
219 | |
72 | 220 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
|
221 /+out (result) |
0 | 222 { |
223 try | |
224 { | |
225 int len = result.length; | |
226 | |
227 assert(len > 0); | |
228 //printf("mangle: '%s' => '%s'\n", toChars(), result); | |
229 for (int i = 0; i < len; i++) | |
230 { | |
231 assert(result[i] == '_' || result[i] == '@' || isalnum(result[i]) || result[i] & 0x80); | |
232 } | |
233 } catch { | |
234 writef("Incorrect mangle: '%s'\n", result); | |
235 assert(false); | |
236 } | |
237 } | |
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
|
238 body+/ |
0 | 239 { |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
240 version(Bug3602) { writef( "Bug3602: Uncomment outblock when fixed\n" ); } |
0 | 241 //writef("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent.toChars() : "null", linkage); |
242 if (!parent || parent.isModule() || linkage == LINK.LINKcpp) // if at global scope | |
243 { | |
244 // If it's not a D declaration, no mangling | |
245 switch (linkage) | |
246 { | |
247 case LINK.LINKd: | |
248 break; | |
249 | |
250 case LINK.LINKc: | |
251 case LINK.LINKwindows: | |
252 case LINK.LINKpascal: | |
253 return ident.toChars(); | |
254 | |
255 case LINK.LINKcpp: | |
256 version (CPP_MANGLE) { | |
184
9f4e5ac4f0a3
One step closer to building on posix.
Jacob Carlborg <doob@me.com>
parents:
183
diff
changeset
|
257 return cpp_mangle(this); |
0 | 258 } else { |
259 // Windows C++ mangling is done by C++ back end | |
260 return ident.toChars(); | |
261 } | |
262 | |
263 case LINK.LINKdefault: | |
264 error("forward declaration"); | |
265 return ident.toChars(); | |
266 | |
267 default: | |
268 writef("'%s', linkage = %d\n", toChars(), linkage); | |
269 assert(0); | |
270 } | |
271 } | |
272 | |
273 string p = .mangle(this); | |
274 scope OutBuffer buf = new OutBuffer(); | |
275 buf.writestring("_D"); | |
276 buf.writestring(p); | |
277 p = buf.toChars(); | |
278 buf.data = null; | |
279 //writef("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent.toChars() : "null", linkage, p); | |
280 return p; | |
281 } | |
282 | |
139 | 283 bool isStatic() { return (storage_class & STC.STCstatic) != 0; } |
0 | 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 | |
139 | 305 bool isCtorinit() { return (storage_class & STC.STCctorinit) != 0; } |
0 | 306 |
139 | 307 bool isFinal() { return (storage_class & STC.STCfinal) != 0; } |
0 | 308 |
309 bool isAbstract() { return (storage_class & STC.STCabstract) != 0; } | |
310 | |
311 bool isConst() { return (storage_class & STC.STCconst) != 0; } | |
312 | |
139 | 313 bool isImmutable() { return (storage_class & STC.STCimmutable) != 0; } |
0 | 314 |
139 | 315 bool isAuto() { return (storage_class & STC.STCauto) != 0; } |
0 | 316 |
139 | 317 bool isScope() { return (storage_class & (STC.STCscope | STC.STCauto)) != 0; } |
0 | 318 |
139 | 319 bool isSynchronized() { return (storage_class & STC.STCsynchronized) != 0; } |
0 | 320 |
139 | 321 bool isParameter() { return (storage_class & STC.STCparameter) != 0; } |
0 | 322 |
72 | 323 override bool isDeprecated() { return (storage_class & STC.STCdeprecated) != 0; } |
0 | 324 |
139 | 325 bool isOverride() { return (storage_class & STC.STCoverride) != 0; } |
0 | 326 |
139 | 327 bool isIn() { return (storage_class & STC.STCin) != 0; } |
0 | 328 |
139 | 329 bool isOut() { return (storage_class & STC.STCout) != 0; } |
0 | 330 |
139 | 331 bool isRef() { return (storage_class & STC.STCref) != 0; } |
0 | 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 } |