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