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