Mercurial > projects > ddmd
annotate dmd/DotVarExp.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | 9e39c7de8438 |
children | b0d41ff5e0df |
rev | line source |
---|---|
72 | 1 module dmd.DotVarExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.Declaration; | |
6 import dmd.backend.elem; | |
7 import dmd.UnaExp; | |
8 import dmd.InterState; | |
9 import dmd.OutBuffer; | |
10 import dmd.Loc; | |
11 import dmd.Scope; | |
12 import dmd.IRState; | |
13 import dmd.HdrGenState; | |
14 import dmd.TOK; | |
15 import dmd.TupleDeclaration; | |
16 import dmd.ArrayTypes; | |
17 import dmd.DsymbolExp; | |
18 import dmd.TupleExp; | |
19 import dmd.Global; | |
20 import dmd.Type; | |
21 import dmd.Dsymbol; | |
22 import dmd.AggregateDeclaration; | |
23 import dmd.VarDeclaration; | |
24 import dmd.WANT; | |
25 import dmd.TY; | |
26 import dmd.ErrorExp; | |
27 import dmd.FuncDeclaration; | |
28 import dmd.STC; | |
29 import dmd.GlobalExpressions; | |
30 import dmd.VarExp; | |
31 import dmd.StructLiteralExp; | |
32 import dmd.PREC; | |
33 | |
34 import dmd.expression.Util; | |
35 import dmd.codegen.Util; | |
36 import dmd.backend.Util; | |
37 import dmd.backend.mTY; | |
38 import dmd.backend.OPER; | |
39 import dmd.backend.TYM; | |
40 | |
0 | 41 class DotVarExp : UnaExp |
42 { | |
43 Declaration var; | |
44 | |
73 | 45 bool hasOverloads; |
0 | 46 |
73 | 47 this(Loc loc, Expression e, Declaration var, bool hasOverloads = false) |
0 | 48 { |
72 | 49 super(loc, TOK.TOKdotvar, DotVarExp.sizeof, e); |
50 //printf("DotVarExp()\n"); | |
51 this.var = var; | |
52 this.hasOverloads = hasOverloads; | |
0 | 53 } |
54 | |
72 | 55 override Expression semantic(Scope sc) |
0 | 56 { |
72 | 57 version (LOGSEMANTIC) { |
58 printf("DotVarExp.semantic('%s')\n", toChars()); | |
59 } | |
60 if (!type) | |
61 { | |
62 var = var.toAlias().isDeclaration(); | |
63 | |
64 TupleDeclaration tup = var.isTupleDeclaration(); | |
65 if (tup) | |
66 { | |
67 /* Replace: | |
68 * e1.tuple(a, b, c) | |
69 * with: | |
70 * tuple(e1.a, e1.b, e1.c) | |
71 */ | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
72 auto exps = new Expressions; |
72 | 73 |
74 exps.reserve(tup.objects.dim); | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
75 foreach (o; tup.objects) |
72 | 76 { |
77 if (auto e = cast(Expression)o) | |
78 { | |
79 if (e.op != TOK.TOKdsymbol) | |
80 error("%s is not a member", e.toChars()); | |
81 else | |
82 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
83 auto ve = cast(DsymbolExp)e; |
72 | 84 e = new DotVarExp(loc, e1, ve.s.isDeclaration()); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
85 exps.push(e); |
72 | 86 } |
87 } else { | |
88 error("%s is not an expression", o.toString()); | |
89 } | |
90 } | |
91 Expression e = new TupleExp(loc, exps); | |
92 e = e.semantic(sc); | |
93 return e; | |
94 } | |
95 | |
96 e1 = e1.semantic(sc); | |
97 type = var.type; | |
98 if (!type && global.errors) | |
99 { | |
100 // var is goofed up, just return 0 | |
101 return new ErrorExp(); | |
102 } | |
103 assert(type); | |
104 | |
105 if (!var.isFuncDeclaration()) // for functions, do checks after overload resolution | |
106 { | |
107 Type t1 = e1.type; | |
108 if (t1.ty == TY.Tpointer) | |
109 t1 = t1.nextOf(); | |
110 | |
111 type = type.addMod(t1.mod); | |
112 | |
113 Dsymbol vparent = var.toParent(); | |
114 AggregateDeclaration ad = vparent ? vparent.isAggregateDeclaration() : null; | |
115 e1 = getRightThis(loc, sc, ad, e1, var); | |
116 if (!sc.noaccesscheck) | |
117 accessCheck(loc, sc, e1, var); | |
118 | |
119 VarDeclaration v = var.isVarDeclaration(); | |
120 Expression e = expandVar(WANT.WANTvalue, v); | |
121 if (e) | |
122 return e; | |
123 } | |
124 } | |
125 //printf("-DotVarExp.semantic('%s')\n", toChars()); | |
0 | 126 return this; |
127 } | |
128 | |
96 | 129 override bool isLvalue() |
0 | 130 { |
96 | 131 return true; |
0 | 132 } |
133 | |
72 | 134 override Expression toLvalue(Scope sc, Expression e) |
0 | 135 { |
72 | 136 //printf("DotVarExp::toLvalue(%s)\n", toChars()); |
0 | 137 return this; |
138 } | |
139 | |
72 | 140 override Expression modifiableLvalue(Scope sc, Expression e) |
0 | 141 { |
72 | 142 static if (false) { |
143 printf("DotVarExp::modifiableLvalue(%s)\n", toChars()); | |
144 printf("e1.type = %s\n", e1.type.toChars()); | |
145 printf("var.type = %s\n", var.type.toChars()); | |
146 } | |
147 | |
148 if (var.isCtorinit()) | |
149 { | |
150 // It's only modifiable if inside the right constructor | |
151 Dsymbol s = sc.func; | |
152 while (true) | |
153 { | |
154 FuncDeclaration fd = null; | |
155 if (s) | |
156 fd = s.isFuncDeclaration(); | |
157 if (fd && ((fd.isCtorDeclaration() && var.storage_class & STC.STCfield) || | |
158 (fd.isStaticCtorDeclaration() && !(var.storage_class & STC.STCfield))) && | |
159 fd.toParent() == var.toParent() && e1.op == TOK.TOKthis) | |
160 { | |
161 VarDeclaration v = var.isVarDeclaration(); | |
162 assert(v); | |
163 v.ctorinit = 1; | |
164 //printf("setting ctorinit\n"); | |
165 } | |
166 else | |
167 { | |
168 if (s) | |
169 { | |
170 s = s.toParent2(); | |
171 continue; | |
172 } | |
173 else | |
174 { | |
175 string p = var.isStatic() ? "static " : ""; | |
176 error("can only initialize %sconst member %s inside %sconstructor", p, var.toChars(), p); | |
177 } | |
178 } | |
179 break; | |
180 } | |
181 } | |
182 else | |
183 { | |
184 version (DMDV2) { | |
185 Type t1 = e1.type.toBasetype(); | |
186 | |
187 if (!t1.isMutable() || (t1.ty == TY.Tpointer && !t1.nextOf().isMutable()) || | |
188 !var.type.isMutable() || !var.type.isAssignable() || var.storage_class & STC.STCmanifest) | |
189 { | |
135 | 190 error("cannot modify const/immutable/inout expression %s", toChars()); |
72 | 191 } |
192 } | |
193 } | |
194 | |
0 | 195 return this; |
196 } | |
197 | |
72 | 198 override Expression optimize(int result) |
0 | 199 { |
96 | 200 //writef("DotVarExp.optimize(result = x%x) %s\n", result, toChars()); |
72 | 201 e1 = e1.optimize(result); |
202 | |
96 | 203 Expression e = e1; |
204 | |
72 | 205 if (e1.op == TOK.TOKvar) |
206 { | |
207 VarExp ve = cast(VarExp)e1; | |
208 VarDeclaration v = ve.var.isVarDeclaration(); | |
96 | 209 e = expandVar(result, v); |
72 | 210 } |
96 | 211 if (e && e.op == TOK.TOKstructliteral) |
212 { | |
213 StructLiteralExp sle = cast(StructLiteralExp) e; | |
72 | 214 VarDeclaration vf = var.isVarDeclaration(); |
215 if (vf) | |
216 { | |
96 | 217 e = sle.getField(type, vf.offset); |
72 | 218 if (e && e !is EXP_CANT_INTERPRET) |
219 return e; | |
220 } | |
221 } | |
222 | |
0 | 223 return this; |
224 } | |
225 | |
72 | 226 override Expression interpret(InterState istate) |
0 | 227 { |
123 | 228 Expression e = EXP_CANT_INTERPRET; |
229 | |
230 version (LOG) { | |
231 printf("DotVarExp.interpret() %.*s\n", toChars()); | |
232 } | |
233 | |
234 Expression ex = e1.interpret(istate); | |
235 if (ex !is EXP_CANT_INTERPRET) | |
236 { | |
237 if (ex.op == TOKstructliteral) | |
238 { | |
239 StructLiteralExp se = cast(StructLiteralExp)ex; | |
240 VarDeclaration v = var.isVarDeclaration(); | |
241 if (v) | |
242 { | |
243 e = se.getField(type, v.offset); | |
244 if (!e) | |
135 | 245 { |
246 error("couldn't find field %s in %s", v.toChars(), type.toChars()); | |
123 | 247 e = EXP_CANT_INTERPRET; |
135 | 248 } |
123 | 249 return e; |
250 } | |
135 | 251 } |
252 else | |
253 { | |
123 | 254 error("%s.%s is not yet implemented at compile time", ex.toChars(), var.toChars()); |
255 } | |
256 } | |
257 | |
258 version (LOG) { | |
259 if (e is EXP_CANT_INTERPRET) | |
260 printf("DotVarExp.interpret() %.*s = EXP_CANT_INTERPRET\n", toChars()); | |
261 } | |
262 return e; | |
0 | 263 } |
264 | |
72 | 265 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 266 { |
72 | 267 expToCBuffer(buf, hgs, e1, PREC.PREC_primary); |
268 buf.writeByte('.'); | |
0 | 269 buf.writestring(var.toChars()); |
270 } | |
271 | |
72 | 272 override void dump(int indent) |
0 | 273 { |
274 assert(false); | |
275 } | |
276 | |
72 | 277 override elem* toElem(IRState* irs) |
0 | 278 { |
72 | 279 // *(&e + offset) |
280 //printf("DotVarExp.toElem('%s')\n", toChars()); | |
281 | |
282 VarDeclaration v = var.isVarDeclaration(); | |
283 if (!v) | |
284 { | |
285 error("%s is not a field, but a %s", var.toChars(), var.kind()); | |
286 } | |
287 | |
288 elem* e = e1.toElem(irs); | |
289 Type tb1 = e1.type.toBasetype(); | |
290 | |
291 if (tb1.ty != TY.Tclass && tb1.ty != TY.Tpointer) | |
292 //e = el_una(OPaddr, TYnptr, e); | |
293 e = addressElem(e, tb1); | |
294 | |
295 e = el_bin(OPER.OPadd, TYM.TYnptr, e, el_long(TYM.TYint, v ? v.offset : 0)); | |
296 e = el_una(OPER.OPind, type.totym(), e); | |
297 if (tybasic(e.Ety) == TYM.TYstruct) | |
298 { | |
299 e.Enumbytes = cast(uint)type.size(); | |
300 } | |
301 el_setLoc(e,loc); | |
302 | |
0 | 303 return e; |
304 } | |
305 } | |
306 |