Mercurial > projects > ddmd
annotate dmd/DotIdExp.d @ 114:e28b18c23469
added a module dmd.common for commonly used stuff
it currently holds code for consistency checking of predefined versions
also added a VisualD project file
author | Trass3r |
---|---|
date | Wed, 01 Sep 2010 18:21:58 +0200 |
parents | ceda59b4d255 |
children | 60bb0fe4563e |
rev | line source |
---|---|
72 | 1 module dmd.DotIdExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.Identifier; | |
6 import dmd.IntegerExp; | |
7 import dmd.Type; | |
8 import dmd.TY; | |
9 import dmd.ScopeExp; | |
10 import dmd.StringExp; | |
11 import dmd.PtrExp; | |
12 import dmd.TypePointer; | |
13 import dmd.Dsymbol; | |
14 import dmd.EnumMember; | |
15 import dmd.VarDeclaration; | |
16 import dmd.ThisExp; | |
17 import dmd.DotVarExp; | |
18 import dmd.VarExp; | |
19 import dmd.CommaExp; | |
20 import dmd.FuncDeclaration; | |
21 import dmd.OverloadSet; | |
22 import dmd.OverExp; | |
23 import dmd.TypeExp; | |
24 import dmd.TupleDeclaration; | |
25 import dmd.ScopeDsymbol; | |
26 import dmd.Import; | |
27 import dmd.Id; | |
28 import dmd.TupleExp; | |
29 import dmd.ArrayTypes; | |
30 import dmd.UnaExp; | |
31 import dmd.OutBuffer; | |
32 import dmd.Loc; | |
33 import dmd.Scope; | |
34 import dmd.TOK; | |
35 import dmd.HdrGenState; | |
36 import dmd.ClassDeclaration; | |
37 import dmd.StructDeclaration; | |
38 import dmd.AggregateDeclaration; | |
39 import dmd.DotExp; | |
40 import dmd.Global; | |
41 import dmd.IdentifierExp; | |
42 import dmd.CallExp; | |
43 import dmd.PREC; | |
44 | |
45 import dmd.expression.Util; | |
46 | |
0 | 47 class DotIdExp : UnaExp |
48 { | |
49 Identifier ident; | |
50 | |
51 this(Loc loc, Expression e, Identifier ident) | |
52 { | |
72 | 53 super(loc, TOK.TOKdot, DotIdExp.sizeof, e); |
0 | 54 this.ident = ident; |
55 } | |
56 | |
72 | 57 override Expression semantic(Scope sc) |
0 | 58 { |
72 | 59 Expression e; |
60 Expression eleft; | |
61 Expression eright; | |
62 | |
63 version (LOGSEMANTIC) { | |
64 printf("DotIdExp.semantic(this = %p, '%s')\n", this, toChars()); | |
65 //printf("e1.op = %d, '%s'\n", e1.op, Token.toChars(e1.op)); | |
66 } | |
67 | |
68 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
69 | |
70 static if (false) { | |
71 /* Don't do semantic analysis if we'll be converting | |
72 * it to a string. | |
73 */ | |
74 if (ident == Id.stringof) | |
75 { | |
76 char *s = e1.toChars(); | |
77 e = new StringExp(loc, s, strlen(s), 'c'); | |
78 e = e.semantic(sc); | |
79 return e; | |
80 } | |
81 } | |
82 | |
83 /* Special case: rewrite this.id and super.id | |
84 * to be classtype.id and baseclasstype.id | |
85 * if we have no this pointer. | |
86 */ | |
87 if ((e1.op == TOK.TOKthis || e1.op == TOK.TOKsuper) && !hasThis(sc)) | |
88 { | |
89 ClassDeclaration cd; | |
90 StructDeclaration sd; | |
91 AggregateDeclaration ad; | |
92 | |
93 ad = sc.getStructClassScope(); | |
94 if (ad) | |
95 { | |
96 cd = ad.isClassDeclaration(); | |
97 if (cd) | |
98 { | |
99 if (e1.op == TOK.TOKthis) | |
100 { | |
101 e = typeDotIdExp(loc, cd.type, ident); | |
102 return e.semantic(sc); | |
103 } | |
104 else if (cd.baseClass && e1.op == TOK.TOKsuper) | |
105 { | |
106 e = typeDotIdExp(loc, cd.baseClass.type, ident); | |
107 return e.semantic(sc); | |
108 } | |
109 } | |
110 else | |
111 { | |
112 sd = ad.isStructDeclaration(); | |
113 if (sd) | |
114 { | |
115 if (e1.op == TOK.TOKthis) | |
116 { | |
117 e = typeDotIdExp(loc, sd.type, ident); | |
118 return e.semantic(sc); | |
119 } | |
120 } | |
121 } | |
122 } | |
123 } | |
124 | |
125 UnaExp.semantic(sc); | |
126 | |
127 if (e1.op == TOK.TOKdotexp) | |
128 { | |
129 DotExp de = cast(DotExp)e1; | |
130 eleft = de.e1; | |
131 eright = de.e2; | |
132 } | |
133 else | |
134 { | |
135 e1 = resolveProperties(sc, e1); | |
136 eleft = null; | |
137 eright = e1; | |
138 } | |
139 | |
140 version (DMDV2) { | |
141 if (e1.op == TOK.TOKtuple && ident == Id.offsetof) | |
142 { | |
143 /* 'distribute' the .offsetof to each of the tuple elements. | |
144 */ | |
145 TupleExp te = cast(TupleExp)e1; | |
146 Expressions exps = new Expressions(); | |
147 exps.setDim(te.exps.dim); | |
148 for (int i = 0; i < exps.dim; i++) | |
149 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
150 auto ee = te.exps[i]; |
72 | 151 ee = ee.semantic(sc); |
152 ee = new DotIdExp(e.loc, ee, Id.offsetof); | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
153 exps[i] = ee; |
72 | 154 } |
155 e = new TupleExp(loc, exps); | |
156 e = e.semantic(sc); | |
157 return e; | |
158 } | |
159 } | |
160 | |
161 if (e1.op == TOK.TOKtuple && ident == Id.length) | |
162 { | |
163 TupleExp te = cast(TupleExp)e1; | |
164 e = new IntegerExp(loc, te.exps.dim, Type.tsize_t); | |
165 return e; | |
166 } | |
167 | |
168 if (e1.op == TOK.TOKdottd) | |
169 { | |
170 error("template %s does not have property %s", e1.toChars(), ident.toChars()); | |
171 return e1; | |
172 } | |
173 | |
174 if (!e1.type) | |
175 { | |
176 error("expression %s does not have property %s", e1.toChars(), ident.toChars()); | |
177 return e1; | |
178 } | |
179 | |
180 Type t1b = e1.type.toBasetype(); | |
181 | |
182 if (eright.op == TOK.TOKimport) // also used for template alias's | |
183 { | |
184 ScopeExp ie = cast(ScopeExp)eright; | |
185 | |
186 /* Disable access to another module's private imports. | |
187 * The check for 'is sds our current module' is because | |
188 * the current module should have access to its own imports. | |
189 */ | |
190 Dsymbol s = ie.sds.search(loc, ident, | |
191 (ie.sds.isModule() && ie.sds != sc.module_) ? 1 : 0); | |
192 if (s) | |
193 { | |
194 s = s.toAlias(); | |
195 checkDeprecated(sc, s); | |
196 | |
197 EnumMember em = s.isEnumMember(); | |
198 if (em) | |
199 { | |
200 e = em.value; | |
201 e = e.semantic(sc); | |
202 return e; | |
203 } | |
204 | |
205 VarDeclaration v = s.isVarDeclaration(); | |
206 if (v) | |
207 { | |
208 //printf("DotIdExp. Identifier '%s' is a variable, type '%s'\n", toChars(), v.type.toChars()); | |
209 if (v.inuse) | |
210 { | |
211 error("circular reference to '%s'", v.toChars()); | |
212 type = Type.tint32; | |
213 return this; | |
214 } | |
215 type = v.type; | |
216 if (v.needThis()) | |
217 { | |
218 if (!eleft) | |
219 eleft = new ThisExp(loc); | |
220 e = new DotVarExp(loc, eleft, v); | |
221 e = e.semantic(sc); | |
222 } | |
223 else | |
224 { | |
225 e = new VarExp(loc, v); | |
226 if (eleft) | |
227 { | |
228 e = new CommaExp(loc, eleft, e); | |
229 e.type = v.type; | |
230 } | |
231 } | |
232 return e.deref(); | |
233 } | |
234 | |
235 FuncDeclaration f = s.isFuncDeclaration(); | |
236 if (f) | |
237 { | |
238 //printf("it's a function\n"); | |
239 if (f.needThis()) | |
240 { | |
241 if (!eleft) | |
242 eleft = new ThisExp(loc); | |
243 e = new DotVarExp(loc, eleft, f); | |
244 e = e.semantic(sc); | |
245 } | |
246 else | |
247 { | |
248 e = new VarExp(loc, f, 1); | |
249 if (eleft) | |
250 { e = new CommaExp(loc, eleft, e); | |
251 e.type = f.type; | |
252 } | |
253 } | |
254 return e; | |
255 } | |
256 version (DMDV2) { | |
257 OverloadSet o = s.isOverloadSet(); | |
258 if (o) | |
259 { | |
260 //printf("'%s' is an overload set\n", o.toChars()); | |
261 return new OverExp(o); | |
262 } | |
263 } | |
264 | |
265 Type t = s.getType(); | |
266 if (t) | |
267 { | |
268 return new TypeExp(loc, t); | |
269 } | |
270 | |
271 TupleDeclaration tup = s.isTupleDeclaration(); | |
272 if (tup) | |
273 { | |
274 if (eleft) | |
275 error("cannot have e.tuple"); | |
276 e = new TupleExp(loc, tup); | |
277 e = e.semantic(sc); | |
278 return e; | |
279 } | |
280 | |
281 ScopeDsymbol sds = s.isScopeDsymbol(); | |
282 if (sds) | |
283 { | |
284 //printf("it's a ScopeDsymbol\n"); | |
285 e = new ScopeExp(loc, sds); | |
286 e = e.semantic(sc); | |
287 if (eleft) | |
288 e = new DotExp(loc, eleft, e); | |
289 return e; | |
290 } | |
291 | |
292 Import imp = s.isImport(); | |
293 if (imp) | |
294 { | |
295 ScopeExp iee = new ScopeExp(loc, imp.pkg); | |
296 return iee.semantic(sc); | |
297 } | |
298 | |
299 // BUG: handle other cases like in IdentifierExp.semantic() | |
300 version (DEBUG) { | |
301 printf("s = '%s', kind = '%s'\n", s.toChars(), s.kind()); | |
302 } | |
303 assert(0); | |
304 } | |
305 else if (ident is Id.stringof_) | |
306 { | |
307 string ss = ie.toChars(); | |
308 e = new StringExp(loc, ss, 'c'); | |
309 e = e.semantic(sc); | |
310 return e; | |
311 } | |
312 error("undefined identifier %s", toChars()); | |
313 type = Type.tvoid; | |
314 return this; | |
315 } | |
316 else if (t1b.ty == TY.Tpointer && | |
317 ident !is Id.init_ && ident !is Id.__sizeof && | |
318 ident !is Id.alignof_ && ident !is Id.offsetof && | |
319 ident !is Id.mangleof_ && ident !is Id.stringof_) | |
320 { /* Rewrite: | |
321 * p.ident | |
322 * as: | |
323 * (*p).ident | |
324 */ | |
325 e = new PtrExp(loc, e1); | |
326 e.type = (cast(TypePointer)t1b).next; | |
327 return e.type.dotExp(sc, e, ident); | |
328 } | |
329 ///version (DMDV2) { | |
330 else if (t1b.ty == TY.Tarray || | |
331 t1b.ty == TY.Tsarray || | |
332 t1b.ty == TY.Taarray) | |
333 { | |
334 /* If ident is not a valid property, rewrite: | |
335 * e1.ident | |
336 * as: | |
337 * .ident(e1) | |
338 */ | |
339 uint errors = global.errors; | |
340 global.gag++; | |
109 | 341 Type t1 = e1.type; |
72 | 342 e = e1.type.dotExp(sc, e1, ident); |
343 global.gag--; | |
344 if (errors != global.errors) // if failed to find the property | |
345 { | |
346 global.errors = errors; | |
109 | 347 e1.type = t1; // kludge to restore type |
72 | 348 e = new DotIdExp(loc, new IdentifierExp(loc, Id.empty), ident); |
349 e = new CallExp(loc, e, e1); | |
350 } | |
351 e = e.semantic(sc); | |
352 return e; | |
353 } | |
354 ///} | |
355 else | |
356 { | |
357 e = e1.type.dotExp(sc, e1, ident); | |
358 e = e.semantic(sc); | |
359 return e; | |
0 | 360 } |
361 } | |
362 | |
72 | 363 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 364 { |
72 | 365 //printf("DotIdExp.toCBuffer()\n"); |
366 expToCBuffer(buf, hgs, e1, PREC.PREC_primary); | |
367 buf.writeByte('.'); | |
0 | 368 buf.writestring(ident.toChars()); |
369 } | |
370 | |
72 | 371 override void dump(int i) |
0 | 372 { |
373 assert(false); | |
374 } | |
375 } | |
376 |