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