Mercurial > projects > ddmd
annotate dmd/BinExp.d @ 179:cd48cb899aee
Updated to dmd2.040
author | korDen |
---|---|
date | Sun, 17 Oct 2010 20:56:07 +0400 |
parents | e3afd1303184 |
children | b0d41ff5e0df |
rev | line source |
---|---|
0 | 1 module dmd.BinExp; |
2 | |
114 | 3 import dmd.common; |
63 | 4 import dmd.SliceExp; |
5 import dmd.IndexExp; | |
6 import dmd.StructDeclaration; | |
7 import dmd.expression.ArrayLength; | |
8 import dmd.expression.Equal; | |
9 import dmd.expression.Index; | |
10 import dmd.ArrayLiteralExp; | |
11 import dmd.AssocArrayLiteralExp; | |
12 import dmd.StringExp; | |
13 import dmd.TypeSArray; | |
14 import dmd.PtrExp; | |
15 import dmd.SymOffExp; | |
16 import dmd.Declaration; | |
17 import dmd.StructLiteralExp; | |
0 | 18 import dmd.Expression; |
63 | 19 import dmd.interpret.Util; |
20 import dmd.GlobalExpressions; | |
176 | 21 import dmd.Global; |
63 | 22 import dmd.Cast; |
23 import dmd.CastExp; | |
24 import dmd.VarDeclaration; | |
25 import dmd.DotVarExp; | |
0 | 26 import dmd.Loc; |
27 import dmd.ClassDeclaration; | |
28 import dmd.OutBuffer; | |
29 import dmd.HdrGenState; | |
30 import dmd.TOK; | |
31 import dmd.IRState; | |
32 import dmd.Scope; | |
33 import dmd.Type; | |
34 import dmd.InterState; | |
35 import dmd.InlineCostState; | |
36 import dmd.InlineScanState; | |
37 import dmd.InlineDoState; | |
38 import dmd.AggregateDeclaration; | |
39 import dmd.Identifier; | |
40 import dmd.MATCH; | |
41 import dmd.declaration.Match; | |
42 import dmd.ArrayTypes; | |
43 import dmd.TY; | |
44 import dmd.TypeClass; | |
45 import dmd.TypeStruct; | |
46 import dmd.Dsymbol; | |
47 import dmd.FuncDeclaration; | |
48 import dmd.TemplateDeclaration; | |
49 import dmd.DotIdExp; | |
50 import dmd.ErrorExp; | |
51 import dmd.WANT; | |
52 import dmd.IntegerExp; | |
53 import dmd.MulExp; | |
54 import dmd.Token; | |
55 import dmd.PREC; | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
56 import dmd.StringValue; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
57 import dmd.StringTable; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
58 import dmd.Parameter; |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
59 import dmd.Statement; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
60 import dmd.ForeachRangeStatement; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
61 import dmd.ArrayLengthExp; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
62 import dmd.IdentifierExp; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
63 import dmd.ExpStatement; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
64 import dmd.CompoundStatement; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
65 import dmd.TypeFunction; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
66 import dmd.LINK; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
67 import dmd.Lexer; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
68 import dmd.ReturnStatement; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
69 import dmd.Id; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
70 import dmd.STC; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
71 import dmd.PROT; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
72 import dmd.VarExp; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
73 import dmd.CallExp; |
0 | 74 |
75 import dmd.expression.Util; | |
76 | |
77 import dmd.backend.elem; | |
78 import dmd.backend.Util; | |
79 | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
80 import dmd.backend.iasm : binary; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
81 |
63 | 82 import std.exception : assumeUnique; |
83 import core.stdc.stdlib : calloc; | |
0 | 84 import std.stdio : writef; |
85 | |
86 /************************************** | |
87 * Combine types. | |
88 * Output: | |
89 * *pt merged type, if *pt is not null | |
90 * *pe1 rewritten e1 | |
91 * *pe2 rewritten e2 | |
92 * Returns: | |
93 * !=0 success | |
94 * 0 failed | |
95 */ | |
96 | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
97 /************************************** |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
98 * Hash table of array op functions already generated or known about. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
99 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
100 |
0 | 101 int typeMerge(Scope sc, Expression e, Type* pt, Expression* pe1, Expression* pe2) |
102 { | |
60 | 103 //printf("typeMerge() %.*s op %.*s\n", (*pe1).toChars(), (*pe2).toChars()); |
0 | 104 //dump(0); |
105 | |
106 Expression e1 = (*pe1).integralPromotions(sc); | |
107 Expression e2 = (*pe2).integralPromotions(sc); | |
108 | |
109 Type t1 = e1.type; | |
110 Type t2 = e2.type; | |
111 assert(t1); | |
112 Type t = t1; | |
113 | |
114 //if (t1) printf("\tt1 = %s\n", t1.toChars()); | |
115 //if (t2) printf("\tt2 = %s\n", t2.toChars()); | |
116 debug { | |
117 if (!t2) writef("\te2 = '%s'\n", e2.toChars()); | |
118 } | |
119 assert(t2); | |
120 | |
121 Type t1b = t1.toBasetype(); | |
122 Type t2b = t2.toBasetype(); | |
123 | |
124 TY ty = cast(TY)Type.impcnvResult[t1b.ty][t2b.ty]; | |
125 if (ty != TY.Terror) | |
126 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
127 auto ty1 = cast(TY)Type.impcnvType1[t1b.ty][t2b.ty]; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
128 auto ty2 = cast(TY)Type.impcnvType2[t1b.ty][t2b.ty]; |
0 | 129 |
130 if (t1b.ty == ty1) // if no promotions | |
131 { | |
132 if (t1 == t2) | |
133 { | |
134 t = t1; | |
135 goto Lret; | |
136 } | |
137 | |
138 if (t1b == t2b) | |
139 { | |
140 t = t1b; | |
141 goto Lret; | |
142 } | |
143 } | |
144 | |
145 t = Type.basic[ty]; | |
146 | |
147 t1 = Type.basic[ty1]; | |
148 t2 = Type.basic[ty2]; | |
149 | |
150 e1 = e1.castTo(sc, t1); | |
151 e2 = e2.castTo(sc, t2); | |
152 //printf("after typeCombine():\n"); | |
153 //dump(0); | |
154 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2); | |
155 goto Lret; | |
156 } | |
157 | |
158 t1 = t1b; | |
159 t2 = t2b; | |
160 | |
161 Lagain: | |
162 if (t1 == t2) | |
163 { | |
164 } | |
165 else if (t1.ty == TY.Tpointer && t2.ty == TY.Tpointer) | |
166 { | |
167 // Bring pointers to compatible type | |
168 Type t1n = t1.nextOf(); | |
169 Type t2n = t2.nextOf(); | |
170 | |
171 if (t1n == t2n) { | |
172 ; | |
173 } else if (t1n.ty == TY.Tvoid) {// pointers to void are always compatible | |
174 t = t2; | |
175 } else if (t2n.ty == TY.Tvoid) { | |
176 ; | |
177 } else if (t1n.mod != t2n.mod) { | |
178 t1 = t1n.mutableOf().constOf().pointerTo(); | |
179 t2 = t2n.mutableOf().constOf().pointerTo(); | |
180 t = t1; | |
181 goto Lagain; | |
182 } else if (t1n.ty == TY.Tclass && t2n.ty == TY.Tclass) { | |
183 ClassDeclaration cd1 = t1n.isClassHandle(); | |
184 ClassDeclaration cd2 = t2n.isClassHandle(); | |
185 int offset; | |
186 | |
187 if (cd1.isBaseOf(cd2, &offset)) | |
188 { | |
189 if (offset) | |
190 e2 = e2.castTo(sc, t); | |
191 } | |
192 else if (cd2.isBaseOf(cd1, &offset)) | |
193 { | |
194 t = t2; | |
195 if (offset) | |
196 e1 = e1.castTo(sc, t); | |
197 } | |
198 else | |
199 goto Lincompatible; | |
200 } else { | |
201 goto Lincompatible; | |
202 } | |
203 } | |
204 else if ((t1.ty == TY.Tsarray || t1.ty == TY.Tarray) && | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
205 (e2.op == TOK.TOKnull && t2.ty == TY.Tpointer && t2.nextOf().ty == TY.Tvoid || |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
206 e2.op == TOK.TOKarrayliteral && t2.ty == TY.Tsarray && t2.nextOf().ty == TY.Tvoid && (cast(TypeSArray)t2).dim.toInteger() == 0) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
207 ) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
208 { /* (T[n] op void*) => T[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
209 * (T[] op void*) => T[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
210 * (T[n] op void[0]) => T[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
211 * (T[] op void[0]) => T[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
212 */ |
0 | 213 goto Lx1; |
214 } | |
215 else if ((t2.ty == TY.Tsarray || t2.ty == TY.Tarray) && | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
216 (e1.op == TOK.TOKnull && t1.ty == TY.Tpointer && t1.nextOf().ty == TY.Tvoid || |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
217 e1.op == TOK.TOKarrayliteral && t1.ty == TY.Tsarray && t1.nextOf().ty == TY.Tvoid && (cast(TypeSArray)t1).dim.toInteger() == 0) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
218 ) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
219 { /* (void* op T[n]) => T[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
220 * (void* op T[]) => T[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
221 * (void[0] op T[n]) => T[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
222 * (void[0] op T[]) => T[] |
0 | 223 */ |
224 goto Lx2; | |
225 } | |
226 else if ((t1.ty == TY.Tsarray || t1.ty == TY.Tarray) && t1.implicitConvTo(t2)) | |
227 { | |
228 goto Lt2; | |
229 } | |
230 else if ((t2.ty == TY.Tsarray || t2.ty == TY.Tarray) && t2.implicitConvTo(t1)) | |
231 { | |
232 goto Lt1; | |
233 } | |
234 /* If one is mutable and the other invariant, then retry | |
235 * with both of them as const | |
236 */ | |
237 else if ((t1.ty == TY.Tsarray || t1.ty == TY.Tarray || t1.ty == TY.Tpointer) && | |
238 (t2.ty == TY.Tsarray || t2.ty == TY.Tarray || t2.ty == TY.Tpointer) && | |
239 t1.nextOf().mod != t2.nextOf().mod | |
240 ) | |
241 { | |
242 if (t1.ty == TY.Tpointer) | |
243 t1 = t1.nextOf().mutableOf().constOf().pointerTo(); | |
244 else | |
245 t1 = t1.nextOf().mutableOf().constOf().arrayOf(); | |
246 | |
247 if (t2.ty == TY.Tpointer) | |
248 t2 = t2.nextOf().mutableOf().constOf().pointerTo(); | |
249 else | |
250 t2 = t2.nextOf().mutableOf().constOf().arrayOf(); | |
251 t = t1; | |
252 goto Lagain; | |
253 } | |
254 else if (t1.ty == TY.Tclass || t2.ty == TY.Tclass) | |
255 { | |
256 while (1) | |
257 { | |
258 int i1 = e2.implicitConvTo(t1); | |
259 int i2 = e1.implicitConvTo(t2); | |
260 | |
261 if (i1 && i2) | |
262 { | |
263 // We have the case of class vs. void*, so pick class | |
264 if (t1.ty == TY.Tpointer) | |
265 i1 = 0; | |
266 else if (t2.ty == TY.Tpointer) | |
267 i2 = 0; | |
268 } | |
269 | |
270 if (i2) | |
271 { | |
272 goto Lt2; | |
273 } | |
274 else if (i1) | |
275 { | |
276 goto Lt1; | |
277 } | |
278 else if (t1.ty == TY.Tclass && t2.ty == TY.Tclass) | |
174 | 279 { |
0 | 280 TypeClass tc1 = cast(TypeClass)t1; |
281 TypeClass tc2 = cast(TypeClass)t2; | |
282 | |
283 /* Pick 'tightest' type | |
284 */ | |
285 ClassDeclaration cd1 = tc1.sym.baseClass; | |
286 ClassDeclaration cd2 = tc2.sym.baseClass; | |
287 | |
288 if (cd1 && cd2) | |
289 { t1 = cd1.type; | |
290 t2 = cd2.type; | |
291 } | |
292 else if (cd1) | |
293 t1 = cd1.type; | |
294 else if (cd2) | |
295 t2 = cd2.type; | |
296 else | |
297 goto Lincompatible; | |
298 } | |
299 else | |
300 goto Lincompatible; | |
301 } | |
302 } | |
303 else if (t1.ty == TY.Tstruct && t2.ty == TY.Tstruct) | |
304 { | |
305 if ((cast(TypeStruct)t1).sym != (cast(TypeStruct)t2).sym) | |
306 goto Lincompatible; | |
307 } | |
308 else if ((e1.op == TOK.TOKstring || e1.op == TOK.TOKnull) && e1.implicitConvTo(t2)) | |
309 { | |
310 goto Lt2; | |
311 } | |
312 else if ((e2.op == TOK.TOKstring || e2.op == TOK.TOKnull) && e2.implicitConvTo(t1)) | |
313 { | |
314 goto Lt1; | |
315 } | |
316 else if (t1.ty == TY.Tsarray && t2.ty == TY.Tsarray && | |
317 e2.implicitConvTo(t1.nextOf().arrayOf())) | |
318 { | |
319 Lx1: | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
320 t = t1.nextOf().arrayOf(); // T[] |
0 | 321 e1 = e1.castTo(sc, t); |
322 e2 = e2.castTo(sc, t); | |
323 } | |
324 else if (t1.ty == TY.Tsarray && t2.ty == TY.Tsarray && | |
325 e1.implicitConvTo(t2.nextOf().arrayOf())) | |
326 { | |
327 Lx2: | |
328 t = t2.nextOf().arrayOf(); | |
329 e1 = e1.castTo(sc, t); | |
330 e2 = e2.castTo(sc, t); | |
331 } | |
332 else if (t1.isintegral() && t2.isintegral()) | |
333 { | |
334 assert(0); | |
335 } | |
98 | 336 else if (e1.isArrayOperand() && t1.ty == TY.Tarray && |
0 | 337 e2.implicitConvTo(t1.nextOf())) |
338 { // T[] op T | |
339 e2 = e2.castTo(sc, t1.nextOf()); | |
340 t = t1.nextOf().arrayOf(); | |
341 } | |
98 | 342 else if (e2.isArrayOperand() && t2.ty == TY.Tarray && |
0 | 343 e1.implicitConvTo(t2.nextOf())) |
344 { // T op T[] | |
345 e1 = e1.castTo(sc, t2.nextOf()); | |
346 t = t2.nextOf().arrayOf(); | |
347 | |
348 //printf("test %s\n", e.toChars()); | |
349 e1 = e1.optimize(WANT.WANTvalue); | |
350 if (e && e.isCommutative() && e1.isConst()) | |
351 { /* Swap operands to minimize number of functions generated | |
352 */ | |
353 //printf("swap %s\n", e.toChars()); | |
354 Expression tmp = e1; | |
355 e1 = e2; | |
356 e2 = tmp; | |
357 } | |
358 } | |
359 else | |
360 { | |
361 Lincompatible: | |
362 return 0; | |
363 } | |
364 Lret: | |
365 if (!*pt) | |
366 *pt = t; | |
367 *pe1 = e1; | |
368 *pe2 = e2; | |
369 static if (false) { | |
370 printf("-typeMerge() %s op %s\n", e1.toChars(), e2.toChars()); | |
371 if (e1.type) printf("\tt1 = %s\n", e1.type.toChars()); | |
372 if (e2.type) printf("\tt2 = %s\n", e2.type.toChars()); | |
373 printf("\ttype = %s\n", t.toChars()); | |
374 } | |
375 //dump(0); | |
376 return 1; | |
377 | |
378 | |
379 Lt1: | |
380 e2 = e2.castTo(sc, t1); | |
381 t = t1; | |
382 goto Lret; | |
383 | |
384 Lt2: | |
385 e1 = e1.castTo(sc, t2); | |
386 t = t2; | |
387 goto Lret; | |
388 } | |
389 | |
390 class BinExp : Expression | |
391 { | |
392 Expression e1; | |
393 Expression e2; | |
394 | |
395 this(Loc loc, TOK op, int size, Expression e1, Expression e2) | |
396 { | |
178 | 397 register(); |
398 | |
0 | 399 super(loc, op, size); |
400 this.e1 = e1; | |
401 this.e2 = e2; | |
402 } | |
174 | 403 |
72 | 404 override Expression syntaxCopy() |
0 | 405 { |
406 BinExp e = cast(BinExp)copy(); | |
407 e.type = null; | |
408 e.e1 = e.e1.syntaxCopy(); | |
409 e.e2 = e.e2.syntaxCopy(); | |
410 | |
411 return e; | |
412 } | |
413 | |
72 | 414 override Expression semantic(Scope sc) |
0 | 415 { |
416 version (LOGSEMANTIC) { | |
60 | 417 printf("BinExp.semantic('%.*s')\n", toChars()); |
0 | 418 } |
419 e1 = e1.semantic(sc); | |
420 if (!e1.type && !(op == TOK.TOKassign && e1.op == TOK.TOKdottd)) // a.template = e2 | |
421 { | |
422 error("%s has no value", e1.toChars()); | |
423 e1.type = Type.terror; | |
424 } | |
425 e2 = e2.semantic(sc); | |
426 if (!e2.type) | |
427 { | |
428 error("%s has no value", e2.toChars()); | |
429 e2.type = Type.terror; | |
430 } | |
431 return this; | |
432 } | |
174 | 433 |
0 | 434 Expression semanticp(Scope sc) |
435 { | |
60 | 436 BinExp.semantic(sc); |
174 | 437 |
60 | 438 e1 = resolveProperties(sc, e1); |
439 e2 = resolveProperties(sc, e2); | |
0 | 440 return this; |
441 } | |
174 | 442 |
56 | 443 /*************************** |
444 * Common semantic routine for some xxxAssignExp's. | |
445 */ | |
0 | 446 Expression commonSemanticAssign(Scope sc) |
447 { | |
56 | 448 Expression e; |
449 | |
450 if (!type) | |
451 { | |
452 BinExp.semantic(sc); | |
453 e2 = resolveProperties(sc, e2); | |
454 | |
455 e = op_overload(sc); | |
456 if (e) | |
457 return e; | |
458 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
459 if (e1.op == TOK.TOKarraylength) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
460 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
461 e = ArrayLengthExp.rewriteOpAssign(this); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
462 e = e.semantic(sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
463 return e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
464 } |
174 | 465 |
56 | 466 if (e1.op == TOKslice) |
174 | 467 { |
56 | 468 // T[] op= ... |
469 typeCombine(sc); | |
470 type = e1.type; | |
471 return arrayOp(sc); | |
472 } | |
473 | |
474 e1 = e1.modifiableLvalue(sc, e1); | |
475 e1.checkScalar(); | |
476 type = e1.type; | |
477 if (type.toBasetype().ty == Tbool) | |
478 { | |
479 error("operator not allowed on bool expression %s", toChars()); | |
480 } | |
481 typeCombine(sc); | |
482 e1.checkArithmetic(); | |
483 e2.checkArithmetic(); | |
484 | |
485 if (op == TOKmodass && e2.type.iscomplex()) | |
486 { error("cannot perform modulo complex arithmetic"); | |
487 return new ErrorExp(); | |
488 } | |
489 } | |
490 return this; | |
0 | 491 } |
492 | |
493 Expression commonSemanticAssignIntegral(Scope sc) | |
494 { | |
495 Expression e; | |
496 | |
497 if (!type) | |
498 { | |
499 BinExp.semantic(sc); | |
500 e2 = resolveProperties(sc, e2); | |
501 | |
502 e = op_overload(sc); | |
503 if (e) | |
504 return e; | |
505 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
506 if (e1.op == TOKarraylength) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
507 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
508 e = ArrayLengthExp.rewriteOpAssign(this); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
509 e = e.semantic(sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
510 return e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
511 } |
174 | 512 |
0 | 513 if (e1.op == TOK.TOKslice) |
514 { // T[] op= ... | |
515 typeCombine(sc); | |
516 type = e1.type; | |
517 return arrayOp(sc); | |
518 } | |
519 | |
520 e1 = e1.modifiableLvalue(sc, e1); | |
521 e1.checkScalar(); | |
522 type = e1.type; | |
523 if (type.toBasetype().ty == TY.Tbool) | |
524 { | |
525 e2 = e2.implicitCastTo(sc, type); | |
526 } | |
527 | |
528 typeCombine(sc); | |
529 e1.checkIntegral(); | |
530 e2.checkIntegral(); | |
531 } | |
532 | |
533 return this; | |
534 } | |
174 | 535 |
72 | 536 override bool checkSideEffect(int flag) |
0 | 537 { |
538 switch (op) { | |
539 case TOK.TOKplusplus: | |
540 case TOK.TOKminusminus: | |
541 case TOK.TOKassign: | |
542 case TOK.TOKconstruct: | |
543 case TOK.TOKblit: | |
544 case TOK.TOKaddass: | |
545 case TOK.TOKminass: | |
546 case TOK.TOKcatass: | |
547 case TOK.TOKmulass: | |
548 case TOK.TOKdivass: | |
549 case TOK.TOKmodass: | |
550 case TOK.TOKshlass: | |
551 case TOK.TOKshrass: | |
552 case TOK.TOKushrass: | |
553 case TOK.TOKandass: | |
554 case TOK.TOKorass: | |
555 case TOK.TOKxorass: | |
135 | 556 case TOK.TOKpowass: |
0 | 557 case TOK.TOKin: |
558 case TOK.TOKremove: | |
559 return true; | |
560 | |
561 default: | |
562 return Expression.checkSideEffect(flag); | |
563 } | |
564 } | |
174 | 565 |
72 | 566 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 567 { |
568 expToCBuffer(buf, hgs, e1, precedence[op]); | |
569 buf.writeByte(' '); | |
570 buf.writestring(Token.toChars(op)); | |
571 buf.writeByte(' '); | |
572 expToCBuffer(buf, hgs, e2, cast(PREC)(precedence[op] + 1)); | |
573 } | |
574 | |
575 /**************************************** | |
576 * Scale addition/subtraction to/from pointer. | |
577 */ | |
578 Expression scaleFactor(Scope sc) | |
579 { | |
580 ulong stride; | |
581 Type t1b = e1.type.toBasetype(); | |
582 Type t2b = e2.type.toBasetype(); | |
583 | |
584 if (t1b.ty == Tpointer && t2b.isintegral()) | |
174 | 585 { |
0 | 586 // Need to adjust operator by the stride |
587 // Replace (ptr + int) with (ptr + (int * stride)) | |
588 Type t = Type.tptrdiff_t; | |
589 | |
590 stride = t1b.nextOf().size(loc); | |
591 if (!t.equals(t2b)) | |
592 e2 = e2.castTo(sc, t); | |
593 e2 = new MulExp(loc, e2, new IntegerExp(Loc(0), stride, t)); | |
594 e2.type = t; | |
595 type = e1.type; | |
596 } | |
597 else if (t2b.ty == Tpointer && t1b.isintegral()) | |
174 | 598 { |
0 | 599 // Need to adjust operator by the stride |
600 // Replace (int + ptr) with (ptr + (int * stride)) | |
601 Type t = Type.tptrdiff_t; | |
602 Expression e; | |
603 | |
604 stride = t2b.nextOf().size(loc); | |
605 if (!t.equals(t1b)) | |
606 e = e1.castTo(sc, t); | |
607 else | |
608 e = e1; | |
609 e = new MulExp(loc, e, new IntegerExp(Loc(0), stride, t)); | |
610 e.type = t; | |
611 type = e2.type; | |
612 e1 = e2; | |
613 e2 = e; | |
614 } | |
615 return this; | |
616 } | |
174 | 617 |
0 | 618 /************************************ |
619 * Bring leaves to common type. | |
620 */ | |
621 Expression typeCombine(Scope sc) | |
622 { | |
623 Type t1 = e1.type.toBasetype(); | |
624 Type t2 = e2.type.toBasetype(); | |
625 | |
626 if (op == TOK.TOKmin || op == TOK.TOKadd) | |
627 { | |
628 if (t1.ty == TY.Tstruct) | |
629 { | |
630 if (t2.ty == TY.Tstruct && (cast(TypeStruct)t1).sym is (cast(TypeStruct)t2).sym) | |
631 goto Lerror; | |
632 } | |
633 else if (t1.ty == TY.Tclass) | |
634 { | |
635 if (t2.ty == TY.Tclass) | |
636 goto Lerror; | |
637 } | |
638 } | |
639 | |
640 if (!typeMerge(sc, this, &type, &e1, &e2)) | |
641 goto Lerror; | |
642 return this; | |
643 | |
644 Lerror: | |
645 incompatibleTypes(); | |
646 type = Type.terror; | |
647 e1 = new ErrorExp(); | |
648 e2 = new ErrorExp(); | |
649 return this; | |
650 } | |
651 | |
72 | 652 override Expression optimize(int result) |
0 | 653 { |
654 //printf("BinExp.optimize(result = %d) %s\n", result, toChars()); | |
655 if (op != TOK.TOKconstruct && op != TOK.TOKblit) // don't replace const variable with its initializer | |
656 e1 = e1.optimize(result); | |
657 | |
658 e2 = e2.optimize(result); | |
659 if (op == TOK.TOKshlass || op == TOK.TOKshrass || op == TOK.TOKushrass) | |
660 { | |
661 if (e2.isConst() == 1) | |
662 { | |
663 long i2 = e2.toInteger(); | |
664 ulong sz = e1.type.size() * 8; | |
665 | |
666 if (i2 < 0 || i2 > sz) | |
174 | 667 { |
0 | 668 error("shift assign by %jd is outside the range 0..%zu", i2, sz); |
669 e2 = new IntegerExp(0); | |
670 } | |
671 } | |
672 } | |
673 | |
674 return this; | |
675 } | |
676 | |
677 bool isunsigned() | |
678 { | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
679 return e1.type.isunsigned() || e2.type.isunsigned(); |
0 | 680 } |
174 | 681 |
0 | 682 void incompatibleTypes() |
683 { | |
684 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
685 e1.toChars(), Token.toChars(op), e2.toChars(), | |
686 e1.type.toChars(), e2.type.toChars()); | |
687 } | |
688 | |
72 | 689 override void dump(int indent) |
0 | 690 { |
691 assert(false); | |
692 } | |
693 | |
154
14feb7ae01a6
* changed the build system to build a release version if the debug one compiles
trass3r
parents:
135
diff
changeset
|
694 override void scanForNestedRef(Scope sc) |
0 | 695 { |
123 | 696 e1.scanForNestedRef(sc); |
697 e2.scanForNestedRef(sc); | |
0 | 698 } |
699 | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
700 Expression interpretCommon(InterState istate, Expression function(Type, Expression, Expression) fp) |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
701 { |
116 | 702 Expression e; |
703 Expression e1; | |
704 Expression e2; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
705 |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
706 version(LOG) |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
707 { |
116 | 708 writef("BinExp::interpretCommon() %s\n", toChars()); |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
709 } |
116 | 710 e1 = this.e1.interpret(istate); |
711 if (e1 is EXP_CANT_INTERPRET) | |
712 goto Lcant; | |
713 if (e1.isConst() != 1) | |
714 goto Lcant; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
715 |
116 | 716 e2 = this.e2.interpret(istate); |
717 if (e2 is EXP_CANT_INTERPRET) | |
718 goto Lcant; | |
719 if (e2.isConst() != 1) | |
720 goto Lcant; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
721 |
116 | 722 e = fp(type, e1, e2); |
723 return e; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
724 |
116 | 725 Lcant: |
726 return EXP_CANT_INTERPRET; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
727 } |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
728 |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
729 Expression interpretCommon2(InterState istate, Expression function(TOK, Type, Expression, Expression) fp) |
0 | 730 { |
116 | 731 Expression e; |
732 Expression e1; | |
733 Expression e2; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
734 |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
735 version(LOG) |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
736 { |
116 | 737 writef("BinExp::interpretCommon2() %s\n", toChars()); |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
738 } |
116 | 739 e1 = this.e1.interpret(istate); |
740 if (e1 is EXP_CANT_INTERPRET) | |
741 goto Lcant; | |
742 if (e1.isConst() != 1 && | |
743 e1.op != TOKnull && | |
744 e1.op != TOKstring && | |
745 e1.op != TOKarrayliteral && | |
746 e1.op != TOKstructliteral) | |
747 goto Lcant; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
748 |
116 | 749 e2 = this.e2.interpret(istate); |
750 if (e2 is EXP_CANT_INTERPRET) | |
751 goto Lcant; | |
752 if (e2.isConst() != 1 && | |
753 e2.op != TOKnull && | |
754 e2.op != TOKstring && | |
755 e2.op != TOKarrayliteral && | |
756 e2.op != TOKstructliteral) | |
757 goto Lcant; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
758 |
116 | 759 e = fp(op, type, e1, e2); |
760 return e; | |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
761 |
116 | 762 Lcant: |
763 return EXP_CANT_INTERPRET; | |
0 | 764 } |
174 | 765 |
116 | 766 Expression interpretAssignCommon(InterState istate, Expression function(Type, Expression, Expression) fp, int post = 0) |
0 | 767 { |
116 | 768 version (LOG) |
769 { | |
770 writef("BinExp.interpretAssignCommon() %.*s\n", toChars()); | |
63 | 771 } |
772 Expression e = EXP_CANT_INTERPRET; | |
773 Expression e1 = this.e1; | |
774 | |
775 if (fp) | |
776 { | |
777 if (e1.op == TOKcast) | |
174 | 778 { |
63 | 779 CastExp ce = cast(CastExp)e1; |
780 e1 = ce.e1; | |
781 } | |
782 } | |
783 if (e1 is EXP_CANT_INTERPRET) | |
784 return e1; | |
785 Expression e2 = this.e2.interpret(istate); | |
786 if (e2 is EXP_CANT_INTERPRET) | |
787 return e2; | |
174 | 788 |
63 | 789 // Chase down rebinding of out and ref. |
179 | 790 if (e1.op == TOKvar) |
63 | 791 { |
792 VarExp ve = cast(VarExp)e1; | |
793 VarDeclaration v = ve.var.isVarDeclaration(); | |
794 if (v && v.value && v.value.op == TOKvar) | |
795 { | |
796 VarExp ve2 = cast(VarExp)v.value; | |
797 if (ve2.var.isSymbolDeclaration()) | |
174 | 798 { |
63 | 799 // This can happen if v is a struct initialized to |
800 // 0 using an __initZ SymbolDeclaration from | |
801 // TypeStruct.defaultInit() | |
802 } | |
803 else | |
804 e1 = v.value; | |
805 } | |
806 else if (v && v.value && (v.value.op==TOKindex || v.value.op == TOKdotvar)) | |
807 { | |
135 | 808 // It is no longer a TOKvar, eg when a[4] is passed by ref. |
174 | 809 e1 = v.value; |
63 | 810 } |
811 } | |
812 | |
813 // To reduce code complexity of handling dotvar expressions, | |
814 // extract the aggregate now. | |
815 Expression aggregate; | |
179 | 816 if (e1.op == TOKdotvar) |
817 { | |
63 | 818 aggregate = (cast(DotVarExp)e1).e1; |
819 // Get rid of 'this'. | |
820 if (aggregate.op == TOKthis && istate.localThis) | |
174 | 821 aggregate = istate.localThis; |
63 | 822 } |
179 | 823 if (e1.op == TOKthis && istate.localThis) |
824 e1 = istate.localThis; | |
174 | 825 |
63 | 826 /* Assignment to variable of the form: |
827 * v = e2 | |
828 */ | |
829 if (e1.op == TOKvar) | |
830 { | |
831 VarExp ve = cast(VarExp)e1; | |
832 VarDeclaration v = ve.var.isVarDeclaration(); | |
833 assert(v); | |
135 | 834 if (v && !v.isCTFE()) |
174 | 835 { |
63 | 836 // Can't modify global or static data |
837 error("%s cannot be modified at compile time", v.toChars()); | |
838 return EXP_CANT_INTERPRET; | |
839 } | |
135 | 840 if (v && v.isCTFE()) |
63 | 841 { |
842 Expression ev = v.value; | |
843 if (fp && !ev) | |
174 | 844 { |
63 | 845 error("variable %s is used before initialization", v.toChars()); |
846 return e; | |
847 } | |
848 if (fp) | |
849 e2 = (*fp)(v.type, ev, e2); | |
850 else | |
174 | 851 { |
63 | 852 /* Look for special case of struct being initialized with 0. |
853 */ | |
854 if (v.type.toBasetype().ty == Tstruct && e2.op == TOKint64) | |
855 { | |
135 | 856 e2 = v.type.defaultInitLiteral(Loc(0)); |
63 | 857 } |
858 e2 = Cast(v.type, v.type, e2); | |
859 } | |
860 if (e2 is EXP_CANT_INTERPRET) | |
861 return e2; | |
862 | |
179 | 863 if (istate) |
864 addVarToInterstate(istate, v); | |
63 | 865 v.value = e2; |
866 e = Cast(type, type, post ? ev : e2); | |
867 } | |
868 } | |
869 else if (e1.op == TOKdotvar && aggregate.op == TOKdotvar) | |
174 | 870 { |
63 | 871 // eg v.u.var = e2, v[3].u.var = e2, etc. |
872 error("Nested struct assignment %s is not yet supported in CTFE", toChars()); | |
873 } | |
874 /* Assignment to struct member of the form: | |
875 * v.var = e2 | |
876 */ | |
877 else if (e1.op == TOKdotvar && aggregate.op == TOKvar) | |
174 | 878 { |
63 | 879 VarDeclaration v = (cast(VarExp)aggregate).var.isVarDeclaration(); |
880 | |
135 | 881 if (!v.isCTFE()) |
174 | 882 { |
63 | 883 // Can't modify global or static data |
884 error("%s cannot be modified at compile time", v.toChars()); | |
885 return EXP_CANT_INTERPRET; | |
886 } else { | |
887 // Chase down rebinding of out and ref | |
888 if (v.value && v.value.op == TOKvar) | |
889 { | |
890 VarExp ve2 = cast(VarExp)v.value; | |
891 if (ve2.var.isSymbolDeclaration()) | |
174 | 892 { |
63 | 893 // This can happen if v is a struct initialized to |
894 // 0 using an __initZ SymbolDeclaration from | |
895 // TypeStruct.defaultInit() | |
896 } | |
897 else | |
898 v = ve2.var.isVarDeclaration(); | |
899 assert(v); | |
900 } | |
901 } | |
902 if (fp && !v.value) | |
174 | 903 { |
63 | 904 error("variable %s is used before initialization", v.toChars()); |
905 return e; | |
906 } | |
907 if (v.value is null && v.init.isVoidInitializer()) | |
174 | 908 { |
63 | 909 /* Since a void initializer initializes to undefined |
910 * values, it is valid here to use the default initializer. | |
911 * No attempt is made to determine if someone actually relies | |
912 * on the void value - to do that we'd need a VoidExp. | |
913 * That's probably a good enhancement idea. | |
914 */ | |
179 | 915 v.value = v.type.defaultInitLiteral(Loc(0)); |
63 | 916 } |
917 Expression vie = v.value; | |
179 | 918 assert(vie !is EXP_CANT_INTERPRET); |
63 | 919 if (vie.op == TOKvar) |
920 { | |
921 Declaration d = (cast(VarExp)vie).var; | |
922 vie = getVarExp(e1.loc, istate, d); | |
923 } | |
924 if (vie.op != TOKstructliteral) | |
179 | 925 { |
926 error("Cannot assign %s=%s in CTFE", v.toChars(), vie.toChars()); | |
63 | 927 return EXP_CANT_INTERPRET; |
179 | 928 } |
63 | 929 StructLiteralExp se = cast(StructLiteralExp)vie; |
930 VarDeclaration vf = (cast(DotVarExp)e1).var.isVarDeclaration(); | |
931 if (!vf) | |
932 return EXP_CANT_INTERPRET; | |
933 int fieldi = se.getFieldIndex(type, vf.offset); | |
934 if (fieldi == -1) | |
935 return EXP_CANT_INTERPRET; | |
936 Expression ev = se.getField(type, vf.offset); | |
937 if (fp) | |
938 e2 = (*fp)(type, ev, e2); | |
939 else | |
940 e2 = Cast(type, type, e2); | |
941 if (e2 is EXP_CANT_INTERPRET) | |
942 return e2; | |
943 | |
944 addVarToInterstate(istate, v); | |
945 | |
946 /* Create new struct literal reflecting updated fieldi | |
947 */ | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
948 auto expsx = changeOneElement(se.elements, fieldi, e2); |
63 | 949 v.value = new StructLiteralExp(se.loc, se.sd, expsx); |
950 v.value.type = se.type; | |
951 | |
952 e = Cast(type, type, post ? ev : e2); | |
953 } | |
954 /* Assignment to struct member of the form: | |
955 * *(symoffexp) = e2 | |
956 */ | |
957 else if (e1.op == TOKstar && (cast(PtrExp)e1).e1.op == TOKsymoff) | |
174 | 958 { |
63 | 959 SymOffExp soe = cast(SymOffExp)(cast(PtrExp)e1).e1; |
960 VarDeclaration v = soe.var.isVarDeclaration(); | |
961 | |
135 | 962 if (!v.isCTFE()) |
63 | 963 { |
964 error("%s cannot be modified at compile time", v.toChars()); | |
965 return EXP_CANT_INTERPRET; | |
966 } | |
967 if (fp && !v.value) | |
174 | 968 { |
63 | 969 error("variable %s is used before initialization", v.toChars()); |
970 return e; | |
971 } | |
972 Expression vie = v.value; | |
973 if (vie.op == TOKvar) | |
974 { | |
975 Declaration d = (cast(VarExp)vie).var; | |
976 vie = getVarExp(e1.loc, istate, d); | |
977 } | |
978 if (vie.op != TOKstructliteral) | |
979 return EXP_CANT_INTERPRET; | |
980 StructLiteralExp se = cast(StructLiteralExp)vie; | |
981 int fieldi = se.getFieldIndex(type, soe.offset); | |
982 if (fieldi == -1) | |
983 return EXP_CANT_INTERPRET; | |
984 Expression ev = se.getField(type, soe.offset); | |
985 if (fp) | |
986 e2 = (*fp)(type, ev, e2); | |
987 else | |
988 e2 = Cast(type, type, e2); | |
989 if (e2 is EXP_CANT_INTERPRET) | |
990 return e2; | |
991 | |
992 addVarToInterstate(istate, v); | |
993 | |
994 /* Create new struct literal reflecting updated fieldi | |
995 */ | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
996 auto expsx = changeOneElement(se.elements, fieldi, e2); |
63 | 997 v.value = new StructLiteralExp(se.loc, se.sd, expsx); |
998 v.value.type = se.type; | |
999 | |
1000 e = Cast(type, type, post ? ev : e2); | |
1001 } | |
1002 /* Assignment to array element of the form: | |
1003 * a[i] = e2 | |
1004 */ | |
1005 else if (e1.op == TOKindex && (cast(IndexExp)e1).e1.op == TOKvar) | |
174 | 1006 { |
63 | 1007 IndexExp ie = cast(IndexExp)e1; |
1008 VarExp ve = cast(VarExp)ie.e1; | |
1009 VarDeclaration v = ve.var.isVarDeclaration(); | |
135 | 1010 if (!v || !v.isCTFE()) |
63 | 1011 { |
1012 error("%s cannot be modified at compile time", v ? v.toChars(): "void"); | |
1013 return EXP_CANT_INTERPRET; | |
1014 } | |
1015 if (v.value && v.value.op == TOKvar) | |
1016 { | |
1017 VarExp ve2 = cast(VarExp)v.value; | |
1018 if (ve2.var.isSymbolDeclaration()) | |
174 | 1019 { |
63 | 1020 // This can happen if v is a struct initialized to |
1021 // 0 using an __initZ SymbolDeclaration from | |
1022 // TypeStruct.defaultInit() | |
1023 } | |
1024 else | |
1025 v = ve2.var.isVarDeclaration(); | |
1026 assert(v); | |
1027 } | |
1028 if (!v.value) | |
1029 { | |
1030 if (fp) | |
174 | 1031 { |
63 | 1032 error("variable %s is used before initialization", v.toChars()); |
1033 return e; | |
1034 } | |
1035 | |
1036 Type t = v.type.toBasetype(); | |
1037 if (t.ty == Tsarray) | |
1038 { | |
1039 /* This array was void initialized. Create a | |
1040 * default initializer for it. | |
1041 * What we should do is fill the array literal with | |
1042 * null data, so use-before-initialized can be detected. | |
1043 * But we're too lazy at the moment to do it, as that | |
1044 * involves redoing Index() and whoever calls it. | |
1045 */ | |
1046 | |
1047 size_t dim = cast(size_t)(cast(TypeSArray)t).dim.toInteger(); | |
1048 v.value = createBlockDuplicatedArrayLiteral(v.type, | |
1049 v.type.defaultInit(Loc(0)), dim); | |
1050 } | |
1051 else | |
1052 return EXP_CANT_INTERPRET; | |
1053 } | |
1054 | |
1055 ArrayLiteralExp ae = null; | |
1056 AssocArrayLiteralExp aae = null; | |
1057 StringExp se = null; | |
1058 if (v.value.op == TOKarrayliteral) | |
1059 ae = cast(ArrayLiteralExp)v.value; | |
1060 else if (v.value.op == TOKassocarrayliteral) | |
1061 aae = cast(AssocArrayLiteralExp)v.value; | |
1062 else if (v.value.op == TOKstring) | |
1063 se = cast(StringExp)v.value; | |
1064 else if (v.value.op == TOKnull) | |
1065 { | |
1066 // This would be a runtime segfault | |
1067 error("Cannot index null array %.*s", v.toChars()); | |
1068 return EXP_CANT_INTERPRET; | |
1069 } | |
1070 else | |
1071 return EXP_CANT_INTERPRET; | |
1072 | |
1073 /* Set the $ variable | |
1074 */ | |
1075 Expression ee = ArrayLength(Type.tsize_t, v.value); | |
1076 if (ee !is EXP_CANT_INTERPRET && ie.lengthVar) | |
1077 ie.lengthVar.value = ee; | |
1078 Expression index = ie.e2.interpret(istate); | |
1079 if (index is EXP_CANT_INTERPRET) | |
1080 return EXP_CANT_INTERPRET; | |
1081 Expression ev; | |
1082 if (fp || ae || se) // not for aae, because key might not be there | |
1083 { | |
1084 ev = Index(type, v.value, index); | |
1085 if (ev is EXP_CANT_INTERPRET) | |
1086 return EXP_CANT_INTERPRET; | |
1087 } | |
1088 | |
1089 if (fp) | |
1090 e2 = (*fp)(type, ev, e2); | |
1091 else | |
1092 e2 = Cast(type, type, e2); | |
1093 if (e2 is EXP_CANT_INTERPRET) | |
1094 return e2; | |
174 | 1095 |
63 | 1096 addVarToInterstate(istate, v); |
1097 if (ae) | |
1098 { | |
1099 /* Create new array literal reflecting updated elem | |
1100 */ | |
1101 int elemi = cast(int)index.toInteger(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1102 auto expsx = changeOneElement(ae.elements, elemi, e2); |
63 | 1103 v.value = new ArrayLiteralExp(ae.loc, expsx); |
1104 v.value.type = ae.type; | |
1105 } | |
1106 else if (aae) | |
1107 { | |
1108 /* Create new associative array literal reflecting updated key/value | |
1109 */ | |
1110 Expressions keysx = aae.keys; | |
1111 Expressions valuesx = new Expressions(); | |
1112 valuesx.setDim(aae.values.dim); | |
1113 int updated = 0; | |
1114 for (size_t j = valuesx.dim; j; ) | |
174 | 1115 { |
63 | 1116 j--; |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1117 Expression ekey = aae.keys[j]; |
63 | 1118 Expression ex = Equal(TOKequal, Type.tbool, ekey, index); |
1119 if (ex is EXP_CANT_INTERPRET) | |
1120 return EXP_CANT_INTERPRET; | |
1121 if (ex.isBool(true)) | |
174 | 1122 { |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1123 valuesx[j] = e2; |
63 | 1124 updated = 1; |
1125 } | |
1126 else | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1127 valuesx[j] = aae.values[j]; |
63 | 1128 } |
1129 if (!updated) | |
174 | 1130 { |
63 | 1131 // Append index/e2 to keysx[]/valuesx[] |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1132 valuesx.push(e2); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1133 keysx = keysx.copy(); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1134 keysx.push(index); |
63 | 1135 } |
1136 v.value = new AssocArrayLiteralExp(aae.loc, keysx, valuesx); | |
1137 v.value.type = aae.type; | |
1138 } | |
1139 else if (se) | |
1140 { | |
1141 /* Create new string literal reflecting updated elem | |
1142 */ | |
1143 int elemi = cast(int)index.toInteger(); | |
1144 char* s; | |
1145 s = cast(char*)calloc(se.len + 1, se.sz); | |
1146 memcpy(s, se.string_, se.len * se.sz); | |
1147 dchar value = cast(dchar)e2.toInteger(); | |
1148 switch (se.sz) | |
1149 { | |
1150 case 1: s[elemi] = cast(char)value; break; | |
1151 case 2: (cast(wchar*)s)[elemi] = cast(wchar)value; break; | |
1152 case 4: (cast(dchar*)s)[elemi] = value; break; | |
1153 default: | |
1154 assert(0); | |
1155 break; | |
1156 } | |
1157 StringExp se2 = new StringExp(se.loc, assumeUnique(s[0..se.len])); | |
1158 se2.committed = se.committed; | |
1159 se2.postfix = se.postfix; | |
1160 se2.type = se.type; | |
1161 v.value = se2; | |
1162 } | |
1163 else | |
1164 assert(0); | |
1165 | |
1166 e = Cast(type, type, post ? ev : e2); | |
1167 } | |
174 | 1168 |
63 | 1169 /* Assignment to struct element in array, of the form: |
1170 * a[i].var = e2 | |
1171 */ | |
1172 else if (e1.op == TOKdotvar && aggregate.op == TOKindex && | |
1173 (cast(IndexExp)aggregate).e1.op == TOKvar) | |
1174 { | |
1175 IndexExp ie = cast(IndexExp)aggregate; | |
1176 VarExp ve = cast(VarExp)(ie.e1); | |
1177 VarDeclaration v = ve.var.isVarDeclaration(); | |
135 | 1178 if (!v || !v.isCTFE()) |
63 | 1179 { |
1180 error("%s cannot be modified at compile time", v ? v.toChars(): "void"); | |
1181 return EXP_CANT_INTERPRET; | |
1182 } | |
1183 Type t = ve.type.toBasetype(); | |
1184 ArrayLiteralExp ae = cast(ArrayLiteralExp)v.value; | |
1185 if (!ae) | |
1186 { | |
1187 // assignment to one element in an uninitialized (static) array. | |
1188 // This is quite difficult, because defaultInit() for a struct is a VarExp, | |
1189 // not a StructLiteralExp. | |
1190 Type t2 = v.type.toBasetype(); | |
1191 if (t2.ty != Tsarray) | |
1192 { | |
1193 error("Cannot index an uninitialized variable"); | |
1194 return EXP_CANT_INTERPRET; | |
1195 } | |
1196 | |
1197 Type telem = (cast(TypeSArray)t2).nextOf().toBasetype(); | |
1198 if (telem.ty != Tstruct) { return EXP_CANT_INTERPRET; } | |
1199 | |
1200 // Create a default struct literal... | |
135 | 1201 Expression structinit = telem.defaultInitLiteral(v.loc); |
63 | 1202 |
1203 // ... and use to create a blank array literal | |
1204 size_t dim = cast(size_t)(cast(TypeSArray)t2).dim.toInteger(); | |
1205 ae = createBlockDuplicatedArrayLiteral(v.type, structinit, dim); | |
1206 v.value = ae; | |
1207 } | |
1208 if (cast(Expression)(ae.elements) is EXP_CANT_INTERPRET) | |
1209 { | |
1210 // Note that this would be a runtime segfault | |
1211 error("Cannot index null array %s", v.toChars()); | |
1212 return EXP_CANT_INTERPRET; | |
1213 } | |
1214 // Set the $ variable | |
1215 Expression ee = ArrayLength(Type.tsize_t, v.value); | |
1216 if (ee !is EXP_CANT_INTERPRET && ie.lengthVar) | |
1217 ie.lengthVar.value = ee; | |
1218 // Determine the index, and check that it's OK. | |
1219 Expression index = ie.e2.interpret(istate); | |
1220 if (index is EXP_CANT_INTERPRET) | |
1221 return EXP_CANT_INTERPRET; | |
1222 | |
1223 int elemi = cast(int)index.toInteger(); | |
1224 if (elemi >= ae.elements.dim) | |
1225 { | |
174 | 1226 error("array index %d is out of bounds %s[0..%d]", elemi, |
63 | 1227 v.toChars(), ae.elements.dim); |
1228 return EXP_CANT_INTERPRET; | |
1229 } | |
1230 // Get old element | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1231 auto vie = ae.elements[elemi]; |
63 | 1232 if (vie.op != TOKstructliteral) |
1233 return EXP_CANT_INTERPRET; | |
1234 | |
1235 // Work out which field needs to be changed | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1236 auto se = cast(StructLiteralExp)vie; |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
1237 auto vf = (cast(DotVarExp)e1).var.isVarDeclaration(); |
63 | 1238 if (!vf) |
1239 return EXP_CANT_INTERPRET; | |
1240 | |
1241 int fieldi = se.getFieldIndex(type, vf.offset); | |
1242 if (fieldi == -1) | |
1243 return EXP_CANT_INTERPRET; | |
174 | 1244 |
63 | 1245 Expression ev = se.getField(type, vf.offset); |
1246 if (fp) | |
1247 e2 = (*fp)(type, ev, e2); | |
1248 else | |
1249 e2 = Cast(type, type, e2); | |
1250 if (e2 == EXP_CANT_INTERPRET) | |
1251 return e2; | |
1252 | |
1253 // Create new struct literal reflecting updated field | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1254 auto expsx = changeOneElement(se.elements, fieldi, e2); |
63 | 1255 Expression newstruct = new StructLiteralExp(se.loc, se.sd, expsx); |
1256 | |
1257 // Create new array literal reflecting updated struct elem | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1258 ae.elements = changeOneElement(ae.elements, elemi, newstruct); |
63 | 1259 return ae; |
1260 } | |
1261 /* Slice assignment, initialization of static arrays | |
1262 * a[] = e | |
1263 */ | |
1264 else if (e1.op == TOKslice && (cast(SliceExp)e1).e1.op == TOKvar) | |
1265 { | |
1266 SliceExp sexp = cast(SliceExp)e1; | |
1267 VarExp ve = cast(VarExp)(sexp.e1); | |
1268 VarDeclaration v = ve.var.isVarDeclaration(); | |
135 | 1269 if (!v || !v.isCTFE()) |
63 | 1270 { |
1271 error("%s cannot be modified at compile time", v.toChars()); | |
1272 return EXP_CANT_INTERPRET; | |
1273 } | |
1274 // Chase down rebinding of out and ref | |
1275 if (v.value && v.value.op == TOKvar) | |
1276 { | |
1277 VarExp ve2 = cast(VarExp)v.value; | |
1278 if (ve2.var.isSymbolDeclaration()) | |
174 | 1279 { |
63 | 1280 // This can happen if v is a struct initialized to |
1281 // 0 using an __initZ SymbolDeclaration from | |
1282 // TypeStruct.defaultInit() | |
1283 } | |
1284 else | |
1285 v = ve2.var.isVarDeclaration(); | |
1286 assert(v); | |
1287 } | |
1288 /* Set the $ variable | |
1289 */ | |
1290 Expression ee = v.value ? ArrayLength(Type.tsize_t, v.value) | |
1291 : EXP_CANT_INTERPRET; | |
1292 if (ee !is EXP_CANT_INTERPRET && sexp.lengthVar) | |
1293 sexp.lengthVar.value = ee; | |
1294 Expression upper = null; | |
1295 Expression lower = null; | |
1296 if (sexp.upr) | |
1297 { | |
1298 upper = sexp.upr.interpret(istate); | |
1299 if (upper is EXP_CANT_INTERPRET) | |
1300 return EXP_CANT_INTERPRET; | |
1301 } | |
1302 if (sexp.lwr) | |
1303 { | |
1304 lower = sexp.lwr.interpret(istate); | |
1305 if (lower is EXP_CANT_INTERPRET) | |
1306 return EXP_CANT_INTERPRET; | |
1307 } | |
1308 Type t = v.type.toBasetype(); | |
1309 size_t dim; | |
174 | 1310 if (t.ty == Tsarray) |
63 | 1311 dim = cast(size_t)(cast(TypeSArray)t).dim.toInteger(); |
1312 else if (t.ty == Tarray) | |
1313 { | |
1314 if (!v.value || v.value.op == TOKnull) | |
1315 { | |
1316 error("cannot assign to null array %s", v.toChars()); | |
1317 return EXP_CANT_INTERPRET; | |
1318 } | |
1319 if (v.value.op == TOKarrayliteral) | |
1320 dim = (cast(ArrayLiteralExp)v.value).elements.dim; | |
1321 else if (v.value.op ==TOKstring) | |
179 | 1322 dim = (cast(StringExp)v.value).len; |
63 | 1323 } |
1324 else | |
1325 { | |
1326 error("%s cannot be evaluated at compile time", toChars()); | |
1327 return EXP_CANT_INTERPRET; | |
1328 } | |
1329 int upperbound = upper ? cast(int)upper.toInteger() : dim; | |
1330 int lowerbound = lower ? cast(int)lower.toInteger() : 0; | |
1331 | |
1332 if ((cast(int)lowerbound < 0) || (upperbound > dim)) | |
1333 { | |
1334 error("Array bounds [0..%d] exceeded in slice [%d..%d]", dim, lowerbound, upperbound); | |
1335 return EXP_CANT_INTERPRET; | |
1336 } | |
179 | 1337 // Could either be slice assignment (v[] = e[]), |
1338 // or block assignment (v[] = val). | |
1339 // For the former, we check that the lengths match. | |
1340 bool isSliceAssignment = (e2.op == TOKarrayliteral) | |
1341 || (e2.op == TOKstring); | |
1342 size_t srclen = 0; | |
1343 if (e2.op == TOKarrayliteral) | |
1344 srclen = (cast(ArrayLiteralExp)e2).elements.dim; | |
1345 else if (e2.op == TOKstring) | |
1346 srclen = (cast(StringExp)e2).len; | |
1347 if (isSliceAssignment && srclen != (upperbound - lowerbound)) | |
63 | 1348 { |
179 | 1349 error("Array length mismatch assigning [0..%d] to [%d..%d]", srclen, lowerbound, upperbound); |
1350 return e; | |
63 | 1351 } |
1352 if (e2.op == TOKarrayliteral) | |
1353 { | |
1354 // Static array assignment from literal | |
174 | 1355 ArrayLiteralExp ae = cast(ArrayLiteralExp)e2; |
63 | 1356 if (upperbound - lowerbound == dim) |
1357 v.value = ae; | |
1358 else | |
1359 { | |
179 | 1360 ArrayLiteralExp existing; |
1361 // Only modifying part of the array. Must create a new array literal. | |
1362 // If the existing array is uninitialized (this can only happen | |
1363 // with static arrays), create it. | |
1364 if (v.value && v.value.op == TOKarrayliteral) | |
1365 existing = cast(ArrayLiteralExp)v.value; | |
1366 else // this can only happen with static arrays | |
1367 existing = createBlockDuplicatedArrayLiteral(v.type, v.type.defaultInit(Loc(0)), dim); | |
1368 | |
63 | 1369 // value[] = value[0..lower] ~ ae ~ value[upper..$] |
1370 existing.elements = spliceElements(existing.elements, ae.elements, lowerbound); | |
1371 v.value = existing; | |
1372 } | |
1373 return e2; | |
1374 } | |
1375 else if (e2.op == TOKstring) | |
1376 { | |
1377 StringExp se = cast(StringExp)e2; | |
179 | 1378 if (upperbound-lowerbound == dim) |
1379 v.value = e2; | |
1380 else | |
1381 { | |
1382 if (!v.value) | |
1383 v.value = createBlockDuplicatedStringLiteral(se.type, cast(dchar)se.type.defaultInit(Loc(0)).toInteger(), dim, se.sz); | |
1384 if (v.value.op==TOKstring) | |
1385 v.value = spliceStringExp(cast(StringExp)v.value, se, lowerbound); | |
1386 else | |
1387 error("String slice assignment is not yet supported in CTFE"); | |
1388 } | |
1389 return e2; | |
1390 } | |
1391 else if (t.nextOf().ty == e2.type.ty) | |
1392 { | |
1393 // Static array block assignment | |
1394 if (upperbound - lowerbound == dim) | |
1395 v.value = createBlockDuplicatedArrayLiteral(v.type, e2, dim); | |
1396 else | |
1397 { | |
1398 ArrayLiteralExp existing; | |
1399 // Only modifying part of the array. Must create a new array literal. | |
1400 // If the existing array is uninitialized (this can only happen | |
1401 // with static arrays), create it. | |
1402 if (v.value && v.value.op == TOKarrayliteral) | |
1403 existing = cast(ArrayLiteralExp)v.value; | |
1404 else // this can only happen with static arrays | |
1405 existing = createBlockDuplicatedArrayLiteral(v.type, v.type.defaultInit(Loc(0)), dim); | |
1406 // value[] = value[0..lower] ~ ae ~ value[upper..$] | |
1407 existing.elements = spliceElements(existing.elements, | |
1408 createBlockDuplicatedArrayLiteral(v.type, e2, upperbound-lowerbound).elements, | |
1409 lowerbound); | |
1410 v.value = existing; | |
1411 } | |
1412 | |
63 | 1413 return e2; |
1414 } | |
1415 else | |
1416 { | |
1417 error("Slice operation %s cannot be evaluated at compile time", toChars()); | |
1418 return e; | |
1419 } | |
1420 } | |
1421 else | |
1422 { | |
1423 error("%s cannot be evaluated at compile time", toChars()); | |
1424 version (DEBUG) { | |
1425 dump(0); | |
1426 } | |
1427 } | |
1428 return e; | |
0 | 1429 } |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
1430 |
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
1431 version(DMDV2) |
72 | 1432 override bool canThrow() |
0 | 1433 { |
1434 return e1.canThrow() || e2.canThrow(); | |
1435 } | |
174 | 1436 |
73 | 1437 // generate an error if this is a nonsensical *=,/=, or %=, eg real *= imaginary |
1438 void checkComplexMulAssign() | |
1439 { | |
1440 // Any multiplication by an imaginary or complex number yields a complex result. | |
1441 // r *= c, i*=c, r*=i, i*=i are all forbidden operations. | |
1442 string opstr = Token.toChars(op); | |
1443 if ( e1.type.isreal() && e2.type.iscomplex()) | |
1444 { | |
1445 error("%s %s %s is undefined. Did you mean %s %s %s.re ?", | |
174 | 1446 e1.type.toChars(), opstr, e2.type.toChars(), |
73 | 1447 e1.type.toChars(), opstr, e2.type.toChars()); |
1448 } | |
1449 else if (e1.type.isimaginary() && e2.type.iscomplex()) | |
1450 { | |
1451 error("%s %s %s is undefined. Did you mean %s %s %s.im ?", | |
1452 e1.type.toChars(), opstr, e2.type.toChars(), | |
1453 e1.type.toChars(), opstr, e2.type.toChars()); | |
1454 } | |
1455 else if ((e1.type.isreal() || e1.type.isimaginary()) && e2.type.isimaginary()) | |
1456 { | |
1457 error("%s %s %s is an undefined operation", e1.type.toChars(), | |
1458 opstr, e2.type.toChars()); | |
1459 } | |
1460 } | |
1461 | |
1462 // generate an error if this is a nonsensical += or -=, eg real += imaginary | |
1463 void checkComplexAddAssign() | |
1464 { | |
1465 // Addition or subtraction of a real and an imaginary is a complex result. | |
1466 // Thus, r+=i, r+=c, i+=r, i+=c are all forbidden operations. | |
1467 if ( (e1.type.isreal() && (e2.type.isimaginary() || e2.type.iscomplex())) || | |
174 | 1468 (e1.type.isimaginary() && (e2.type.isreal() || e2.type.iscomplex())) |
73 | 1469 ) |
1470 { | |
1471 error("%s %s %s is undefined (result is complex)", | |
1472 e1.type.toChars(), Token.toChars(op), e2.type.toChars()); | |
1473 } | |
1474 } | |
1475 | |
0 | 1476 |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1477 /*********************************** |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1478 * Construct the array operation expression. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1479 */ |
0 | 1480 Expression arrayOp(Scope sc) |
1481 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1482 //printf("BinExp.arrayOp() %s\n", toChars()); |
174 | 1483 |
73 | 1484 if (type.toBasetype().nextOf().toBasetype().ty == Tvoid) |
1485 { | |
1486 error("Cannot perform array operations on void[] arrays"); | |
1487 return new ErrorExp(); | |
1488 } | |
174 | 1489 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
1490 auto arguments = new Expressions(); |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1491 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1492 /* The expression to generate an array operation for is mangled |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1493 * into a name to use as the array operation function name. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1494 * Mangle in the operands and operators in RPN order, and type. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1495 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1496 scope OutBuffer buf = new OutBuffer(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1497 buf.writestring("_array"); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1498 buildArrayIdent(buf, arguments); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1499 buf.writeByte('_'); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1500 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1501 /* Append deco of array element type |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1502 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1503 version (DMDV2) { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1504 buf.writestring(type.toBasetype().nextOf().toBasetype().mutableOf().deco); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1505 } else { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1506 buf.writestring(type.toBasetype().nextOf().toBasetype().deco); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1507 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1508 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1509 size_t namelen = buf.offset; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1510 buf.writeByte(0); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1511 immutable(char)* name = cast(immutable(char)*)buf.extractData(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1512 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1513 /* Look up name in hash table |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1514 */ |
178 | 1515 auto s = name[0..namelen]; |
1516 Object* sv = global.arrayfuncs.update(s); | |
1517 FuncDeclaration fd = cast(FuncDeclaration)*sv; | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1518 if (!fd) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1519 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1520 /* Some of the array op functions are written as library functions, |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1521 * presumably to optimize them with special CPU vector instructions. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1522 * List those library functions here, in alpha order. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1523 */ |
175 | 1524 enum const(char)*[] libArrayopFuncs = |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1525 [ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1526 "_arrayExpSliceAddass_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1527 "_arrayExpSliceAddass_d", // T[]+=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1528 "_arrayExpSliceAddass_f", // T[]+=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1529 "_arrayExpSliceAddass_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1530 "_arrayExpSliceAddass_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1531 "_arrayExpSliceAddass_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1532 "_arrayExpSliceAddass_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1533 "_arrayExpSliceAddass_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1534 "_arrayExpSliceAddass_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1535 "_arrayExpSliceAddass_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1536 "_arrayExpSliceAddass_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1537 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1538 "_arrayExpSliceDivass_d", // T[]/=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1539 "_arrayExpSliceDivass_f", // T[]/=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1540 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1541 "_arrayExpSliceMinSliceAssign_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1542 "_arrayExpSliceMinSliceAssign_d", // T[]=T-T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1543 "_arrayExpSliceMinSliceAssign_f", // T[]=T-T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1544 "_arrayExpSliceMinSliceAssign_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1545 "_arrayExpSliceMinSliceAssign_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1546 "_arrayExpSliceMinSliceAssign_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1547 "_arrayExpSliceMinSliceAssign_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1548 "_arrayExpSliceMinSliceAssign_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1549 "_arrayExpSliceMinSliceAssign_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1550 "_arrayExpSliceMinSliceAssign_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1551 "_arrayExpSliceMinSliceAssign_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1552 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1553 "_arrayExpSliceMinass_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1554 "_arrayExpSliceMinass_d", // T[]-=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1555 "_arrayExpSliceMinass_f", // T[]-=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1556 "_arrayExpSliceMinass_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1557 "_arrayExpSliceMinass_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1558 "_arrayExpSliceMinass_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1559 "_arrayExpSliceMinass_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1560 "_arrayExpSliceMinass_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1561 "_arrayExpSliceMinass_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1562 "_arrayExpSliceMinass_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1563 "_arrayExpSliceMinass_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1564 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1565 "_arrayExpSliceMulass_d", // T[]*=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1566 "_arrayExpSliceMulass_f", // T[]*=T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1567 "_arrayExpSliceMulass_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1568 "_arrayExpSliceMulass_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1569 "_arrayExpSliceMulass_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1570 "_arrayExpSliceMulass_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1571 "_arrayExpSliceMulass_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1572 "_arrayExpSliceMulass_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1573 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1574 "_arraySliceExpAddSliceAssign_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1575 "_arraySliceExpAddSliceAssign_d", // T[]=T[]+T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1576 "_arraySliceExpAddSliceAssign_f", // T[]=T[]+T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1577 "_arraySliceExpAddSliceAssign_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1578 "_arraySliceExpAddSliceAssign_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1579 "_arraySliceExpAddSliceAssign_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1580 "_arraySliceExpAddSliceAssign_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1581 "_arraySliceExpAddSliceAssign_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1582 "_arraySliceExpAddSliceAssign_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1583 "_arraySliceExpAddSliceAssign_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1584 "_arraySliceExpAddSliceAssign_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1585 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1586 "_arraySliceExpDivSliceAssign_d", // T[]=T[]/T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1587 "_arraySliceExpDivSliceAssign_f", // T[]=T[]/T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1588 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1589 "_arraySliceExpMinSliceAssign_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1590 "_arraySliceExpMinSliceAssign_d", // T[]=T[]-T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1591 "_arraySliceExpMinSliceAssign_f", // T[]=T[]-T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1592 "_arraySliceExpMinSliceAssign_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1593 "_arraySliceExpMinSliceAssign_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1594 "_arraySliceExpMinSliceAssign_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1595 "_arraySliceExpMinSliceAssign_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1596 "_arraySliceExpMinSliceAssign_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1597 "_arraySliceExpMinSliceAssign_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1598 "_arraySliceExpMinSliceAssign_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1599 "_arraySliceExpMinSliceAssign_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1600 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1601 "_arraySliceExpMulSliceAddass_d", // T[] += T[]*T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1602 "_arraySliceExpMulSliceAddass_f", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1603 "_arraySliceExpMulSliceAddass_r", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1604 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1605 "_arraySliceExpMulSliceAssign_d", // T[]=T[]*T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1606 "_arraySliceExpMulSliceAssign_f", // T[]=T[]*T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1607 "_arraySliceExpMulSliceAssign_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1608 "_arraySliceExpMulSliceAssign_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1609 "_arraySliceExpMulSliceAssign_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1610 "_arraySliceExpMulSliceAssign_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1611 "_arraySliceExpMulSliceAssign_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1612 "_arraySliceExpMulSliceAssign_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1613 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1614 "_arraySliceExpMulSliceMinass_d", // T[] -= T[]*T |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1615 "_arraySliceExpMulSliceMinass_f", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1616 "_arraySliceExpMulSliceMinass_r", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1617 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1618 "_arraySliceSliceAddSliceAssign_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1619 "_arraySliceSliceAddSliceAssign_d", // T[]=T[]+T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1620 "_arraySliceSliceAddSliceAssign_f", // T[]=T[]+T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1621 "_arraySliceSliceAddSliceAssign_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1622 "_arraySliceSliceAddSliceAssign_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1623 "_arraySliceSliceAddSliceAssign_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1624 "_arraySliceSliceAddSliceAssign_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1625 "_arraySliceSliceAddSliceAssign_r", // T[]=T[]+T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1626 "_arraySliceSliceAddSliceAssign_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1627 "_arraySliceSliceAddSliceAssign_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1628 "_arraySliceSliceAddSliceAssign_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1629 "_arraySliceSliceAddSliceAssign_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1630 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1631 "_arraySliceSliceAddass_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1632 "_arraySliceSliceAddass_d", // T[]+=T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1633 "_arraySliceSliceAddass_f", // T[]+=T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1634 "_arraySliceSliceAddass_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1635 "_arraySliceSliceAddass_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1636 "_arraySliceSliceAddass_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1637 "_arraySliceSliceAddass_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1638 "_arraySliceSliceAddass_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1639 "_arraySliceSliceAddass_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1640 "_arraySliceSliceAddass_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1641 "_arraySliceSliceAddass_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1642 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1643 "_arraySliceSliceMinSliceAssign_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1644 "_arraySliceSliceMinSliceAssign_d", // T[]=T[]-T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1645 "_arraySliceSliceMinSliceAssign_f", // T[]=T[]-T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1646 "_arraySliceSliceMinSliceAssign_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1647 "_arraySliceSliceMinSliceAssign_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1648 "_arraySliceSliceMinSliceAssign_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1649 "_arraySliceSliceMinSliceAssign_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1650 "_arraySliceSliceMinSliceAssign_r", // T[]=T[]-T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1651 "_arraySliceSliceMinSliceAssign_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1652 "_arraySliceSliceMinSliceAssign_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1653 "_arraySliceSliceMinSliceAssign_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1654 "_arraySliceSliceMinSliceAssign_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1655 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1656 "_arraySliceSliceMinass_a", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1657 "_arraySliceSliceMinass_d", // T[]-=T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1658 "_arraySliceSliceMinass_f", // T[]-=T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1659 "_arraySliceSliceMinass_g", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1660 "_arraySliceSliceMinass_h", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1661 "_arraySliceSliceMinass_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1662 "_arraySliceSliceMinass_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1663 "_arraySliceSliceMinass_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1664 "_arraySliceSliceMinass_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1665 "_arraySliceSliceMinass_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1666 "_arraySliceSliceMinass_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1667 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1668 "_arraySliceSliceMulSliceAssign_d", // T[]=T[]*T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1669 "_arraySliceSliceMulSliceAssign_f", // T[]=T[]*T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1670 "_arraySliceSliceMulSliceAssign_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1671 "_arraySliceSliceMulSliceAssign_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1672 "_arraySliceSliceMulSliceAssign_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1673 "_arraySliceSliceMulSliceAssign_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1674 "_arraySliceSliceMulSliceAssign_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1675 "_arraySliceSliceMulSliceAssign_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1676 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1677 "_arraySliceSliceMulass_d", // T[]*=T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1678 "_arraySliceSliceMulass_f", // T[]*=T[] |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1679 "_arraySliceSliceMulass_i", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1680 "_arraySliceSliceMulass_k", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1681 "_arraySliceSliceMulass_s", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1682 "_arraySliceSliceMulass_t", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1683 "_arraySliceSliceMulass_u", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1684 "_arraySliceSliceMulass_w", |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1685 ]; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1686 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1687 int i = binary(name, libArrayopFuncs.ptr, libArrayopFuncs.length); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1688 if (i == -1) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1689 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1690 debug { // Make sure our array is alphabetized |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1691 for (i = 0; i < libArrayopFuncs.length; i++) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1692 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1693 if (strcmp(name, libArrayopFuncs[i]) == 0) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1694 assert(false); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1695 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1696 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1697 /* Not in library, so generate it. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1698 * Construct the function body: |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1699 * foreach (i; 0 .. p.length) for (size_t i = 0; i < p.length; i++) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1700 * loopbody; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1701 * return p; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1702 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1703 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
1704 auto fparams = new Parameters(); |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1705 Expression loopbody = buildArrayLoop(fparams); |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
123
diff
changeset
|
1706 auto p = fparams[0 /*fparams.dim - 1*/]; |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1707 version (DMDV1) { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1708 // for (size_t i = 0; i < p.length; i++) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1709 Initializer init = new ExpInitializer(0, new IntegerExp(0, 0, Type.tsize_t)); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1710 Dsymbol d = new VarDeclaration(0, Type.tsize_t, Id.p, init); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1711 Statement s1 = new ForStatement(0, |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1712 new DeclarationStatement(0, d), |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1713 new CmpExp(TOKlt, 0, new IdentifierExp(0, Id.p), new ArrayLengthExp(0, new IdentifierExp(0, p.ident))), |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1714 new PostExp(TOKplusplus, 0, new IdentifierExp(0, Id.p)), |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1715 new ExpStatement(0, loopbody)); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1716 } else { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1717 // foreach (i; 0 .. p.length) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1718 Statement s1 = new ForeachRangeStatement(Loc(0), TOKforeach, |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
1719 new Parameter(STC.STCundefined, null, Id.p, null), |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1720 new IntegerExp(Loc(0), 0, Type.tint32), |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1721 new ArrayLengthExp(Loc(0), new IdentifierExp(Loc(0), p.ident)), |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1722 new ExpStatement(Loc(0), loopbody)); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1723 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1724 Statement s2 = new ReturnStatement(Loc(0), new IdentifierExp(Loc(0), p.ident)); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1725 //printf("s2: %s\n", s2.toChars()); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1726 Statement fbody = new CompoundStatement(Loc(0), s1, s2); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1727 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1728 /* Construct the function |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1729 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1730 TypeFunction ftype = new TypeFunction(fparams, type, 0, LINKc); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1731 //printf("ftype: %s\n", ftype.toChars()); |
178 | 1732 fd = new FuncDeclaration(Loc(0), Loc(0), Lexer.idPool(s), STCundefined, ftype); |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1733 fd.fbody = fbody; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1734 fd.protection = PROT.PROTpublic; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1735 fd.linkage = LINKc; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1736 |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
1737 sc.module_.importedFrom.members.push(fd); |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1738 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1739 sc = sc.push(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1740 sc.parent = sc.module_.importedFrom; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1741 sc.stc = STCundefined; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1742 sc.linkage = LINKc; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1743 fd.semantic(sc); |
79 | 1744 fd.semantic2(sc); |
1745 fd.semantic3(sc); | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1746 sc.pop(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1747 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1748 else |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1749 { /* In library, refer to it. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1750 */ |
178 | 1751 fd = FuncDeclaration.genCfunc(type, s); |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1752 } |
178 | 1753 *sv = fd; // cache symbol in hash table |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1754 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1755 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1756 /* Call the function fd(arguments) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1757 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1758 Expression ec = new VarExp(Loc(0), fd); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1759 Expression e = new CallExp(loc, ec, arguments); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1760 e.type = type; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
1761 return e; |
0 | 1762 } |
1763 | |
72 | 1764 override int inlineCost(InlineCostState* ics) |
0 | 1765 { |
1766 return 1 + e1.inlineCost(ics) + e2.inlineCost(ics); | |
1767 } | |
174 | 1768 |
72 | 1769 override Expression doInline(InlineDoState ids) |
0 | 1770 { |
1771 BinExp be = cast(BinExp)copy(); | |
1772 | |
1773 be.e1 = e1.doInline(ids); | |
1774 be.e2 = e2.doInline(ids); | |
1775 return be; | |
1776 } | |
174 | 1777 |
72 | 1778 override Expression inlineScan(InlineScanState* iss) |
0 | 1779 { |
1780 e1 = e1.inlineScan(iss); | |
1781 e2 = e2.inlineScan(iss); | |
1782 return this; | |
1783 } | |
1784 | |
1785 Expression op_overload(Scope sc) | |
1786 { | |
1787 //printf("BinExp.op_overload() (%s)\n", toChars()); | |
1788 | |
1789 AggregateDeclaration ad; | |
1790 Type t1 = e1.type.toBasetype(); | |
1791 Type t2 = e2.type.toBasetype(); | |
1792 Identifier id = opId(); | |
1793 Identifier id_r = opId_r(); | |
1794 | |
1795 Match m; | |
1796 scope Expressions args1 = new Expressions(); | |
1797 scope Expressions args2 = new Expressions(); | |
1798 int argsset = 0; | |
1799 | |
1800 AggregateDeclaration ad1; | |
1801 if (t1.ty == TY.Tclass) | |
1802 ad1 = (cast(TypeClass)t1).sym; | |
1803 else if (t1.ty == TY.Tstruct) | |
1804 ad1 = (cast(TypeStruct)t1).sym; | |
1805 else | |
1806 ad1 = null; | |
1807 | |
1808 AggregateDeclaration ad2; | |
1809 if (t2.ty == TY.Tclass) | |
1810 ad2 = (cast(TypeClass)t2).sym; | |
1811 else if (t2.ty == TY.Tstruct) | |
1812 ad2 = (cast(TypeStruct)t2).sym; | |
1813 else | |
1814 ad2 = null; | |
1815 | |
1816 Dsymbol s = null; | |
1817 Dsymbol s_r = null; | |
1818 FuncDeclaration fd = null; | |
1819 TemplateDeclaration td = null; | |
1820 if (ad1 && id) | |
1821 { | |
1822 s = search_function(ad1, id); | |
1823 } | |
1824 if (ad2 && id_r) | |
1825 { | |
1826 s_r = search_function(ad2, id_r); | |
1827 } | |
1828 | |
1829 if (s || s_r) | |
1830 { | |
1831 /* Try: | |
1832 * a.opfunc(b) | |
1833 * b.opfunc_r(a) | |
1834 * and see which is better. | |
1835 */ | |
1836 Expression e; | |
1837 FuncDeclaration lastf; | |
1838 | |
1839 args1.setDim(1); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1840 args1[0] = e1; |
0 | 1841 args2.setDim(1); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1842 args2[0] = e2; |
0 | 1843 argsset = 1; |
1844 | |
1845 ///memset(&m, 0, sizeof(m)); | |
1846 m.last = MATCH.MATCHnomatch; | |
1847 | |
1848 if (s) | |
1849 { | |
1850 fd = s.isFuncDeclaration(); | |
1851 if (fd) | |
1852 { | |
1853 overloadResolveX(&m, fd, null, args2); | |
1854 } | |
1855 else | |
174 | 1856 { |
0 | 1857 td = s.isTemplateDeclaration(); |
1858 templateResolve(&m, td, sc, loc, null, null, args2); | |
1859 } | |
1860 } | |
174 | 1861 |
0 | 1862 lastf = m.lastf; |
1863 | |
1864 if (s_r) | |
1865 { | |
1866 fd = s_r.isFuncDeclaration(); | |
1867 if (fd) | |
1868 { | |
1869 overloadResolveX(&m, fd, null, args1); | |
1870 } | |
1871 else | |
174 | 1872 { |
0 | 1873 td = s_r.isTemplateDeclaration(); |
1874 templateResolve(&m, td, sc, loc, null, null, args1); | |
1875 } | |
1876 } | |
1877 | |
1878 if (m.count > 1) | |
1879 { | |
1880 // Error, ambiguous | |
1881 error("overloads %s and %s both match argument list for %s", | |
1882 m.lastf.type.toChars(), | |
1883 m.nextf.type.toChars(), | |
1884 m.lastf.toChars()); | |
1885 } | |
1886 else if (m.last == MATCH.MATCHnomatch) | |
1887 { | |
1888 m.lastf = m.anyf; | |
1889 } | |
1890 | |
1891 if (op == TOK.TOKplusplus || op == TOK.TOKminusminus) | |
1892 // Kludge because operator overloading regards e++ and e-- | |
1893 // as unary, but it's implemented as a binary. | |
1894 // Rewrite (e1 ++ e2) as e1.postinc() | |
1895 // Rewrite (e1 -- e2) as e1.postdec() | |
1896 e = build_overload(loc, sc, e1, null, id); | |
1897 else if (lastf && m.lastf == lastf || m.last == MATCH.MATCHnomatch) | |
1898 // Rewrite (e1 op e2) as e1.opfunc(e2) | |
1899 e = build_overload(loc, sc, e1, e2, id); | |
1900 else | |
1901 // Rewrite (e1 op e2) as e2.opfunc_r(e1) | |
1902 e = build_overload(loc, sc, e2, e1, id_r); | |
1903 return e; | |
1904 } | |
1905 | |
1906 if (isCommutative()) | |
1907 { | |
1908 s = null; | |
1909 s_r = null; | |
1910 if (ad1 && id_r) | |
1911 { | |
1912 s_r = search_function(ad1, id_r); | |
1913 } | |
1914 if (ad2 && id) | |
1915 { | |
1916 s = search_function(ad2, id); | |
1917 } | |
1918 | |
1919 if (s || s_r) | |
1920 { | |
1921 /* Try: | |
1922 * a.opfunc_r(b) | |
1923 * b.opfunc(a) | |
1924 * and see which is better. | |
1925 */ | |
1926 | |
1927 if (!argsset) | |
174 | 1928 { |
0 | 1929 args1.setDim(1); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1930 args1[0] = e1; |
0 | 1931 args2.setDim(1); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1932 args2[0] = e2; |
0 | 1933 } |
1934 | |
1935 ///memset(&m, 0, sizeof(m)); | |
1936 m.last = MATCH.MATCHnomatch; | |
1937 | |
1938 if (s_r) | |
1939 { | |
1940 fd = s_r.isFuncDeclaration(); | |
1941 if (fd) | |
1942 { | |
1943 overloadResolveX(&m, fd, null, args2); | |
1944 } | |
1945 else | |
1946 { td = s_r.isTemplateDeclaration(); | |
1947 templateResolve(&m, td, sc, loc, null, null, args2); | |
1948 } | |
1949 } | |
1950 FuncDeclaration lastf = m.lastf; | |
1951 | |
1952 if (s) | |
1953 { | |
1954 fd = s.isFuncDeclaration(); | |
1955 if (fd) | |
1956 { | |
1957 overloadResolveX(&m, fd, null, args1); | |
1958 } | |
1959 else | |
1960 { td = s.isTemplateDeclaration(); | |
1961 templateResolve(&m, td, sc, loc, null, null, args1); | |
1962 } | |
1963 } | |
1964 | |
1965 if (m.count > 1) | |
1966 { | |
1967 // Error, ambiguous | |
1968 error("overloads %s and %s both match argument list for %s", | |
1969 m.lastf.type.toChars(), | |
1970 m.nextf.type.toChars(), | |
1971 m.lastf.toChars()); | |
1972 } | |
1973 else if (m.last == MATCH.MATCHnomatch) | |
1974 { | |
1975 m.lastf = m.anyf; | |
1976 } | |
1977 | |
1978 Expression e; | |
1979 if (lastf && m.lastf == lastf || id_r && m.last == MATCH.MATCHnomatch) | |
1980 // Rewrite (e1 op e2) as e1.opfunc_r(e2) | |
1981 e = build_overload(loc, sc, e1, e2, id_r); | |
1982 else | |
1983 // Rewrite (e1 op e2) as e2.opfunc(e1) | |
1984 e = build_overload(loc, sc, e2, e1, id); | |
1985 | |
1986 // When reversing operands of comparison operators, | |
1987 // need to reverse the sense of the op | |
1988 switch (op) | |
1989 { | |
1990 case TOK.TOKlt: op = TOK.TOKgt; break; | |
1991 case TOK.TOKgt: op = TOK.TOKlt; break; | |
1992 case TOK.TOKle: op = TOK.TOKge; break; | |
1993 case TOK.TOKge: op = TOK.TOKle; break; | |
1994 | |
1995 // Floating point compares | |
1996 case TOK.TOKule: op = TOK.TOKuge; break; | |
1997 case TOK.TOKul: op = TOK.TOKug; break; | |
1998 case TOK.TOKuge: op = TOK.TOKule; break; | |
1999 case TOK.TOKug: op = TOK.TOKul; break; | |
2000 | |
2001 // These are symmetric | |
2002 case TOK.TOKunord: | |
2003 case TOK.TOKlg: | |
2004 case TOK.TOKleg: | |
2005 case TOK.TOKue: | |
2006 break; | |
123 | 2007 default: |
2008 break; /// | |
0 | 2009 } |
2010 | |
2011 return e; | |
2012 } | |
2013 } | |
2014 | |
2015 version (DMDV2) { | |
2016 // Try alias this on first operand | |
2017 if (ad1 && ad1.aliasthis) | |
2018 { | |
2019 /* Rewrite (e1 op e2) as: | |
2020 * (e1.aliasthis op e2) | |
2021 */ | |
2022 Expression e1 = new DotIdExp(loc, this.e1, ad1.aliasthis.ident); | |
2023 Expression e = copy(); | |
2024 (cast(BinExp)e).e1 = e1; | |
2025 e = e.semantic(sc); | |
2026 return e; | |
2027 } | |
2028 | |
2029 // Try alias this on second operand | |
2030 if (ad2 && ad2.aliasthis) | |
2031 { | |
2032 /* Rewrite (e1 op e2) as: | |
2033 * (e1 op e2.aliasthis) | |
2034 */ | |
2035 Expression e2 = new DotIdExp(loc, this.e2, ad2.aliasthis.ident); | |
2036 Expression e = copy(); | |
2037 (cast(BinExp)e).e2 = e2; | |
2038 e = e.semantic(sc); | |
2039 return e; | |
2040 } | |
2041 } | |
2042 return null; | |
2043 } | |
2044 | |
2045 elem* toElemBin(IRState* irs, int op) | |
2046 { | |
2047 //printf("toElemBin() '%s'\n", toChars()); | |
2048 | |
2049 tym_t tym = type.totym(); | |
2050 | |
2051 elem* el = e1.toElem(irs); | |
2052 elem* er = e2.toElem(irs); | |
2053 elem* e = el_bin(op,tym,el,er); | |
2054 el_setLoc(e,loc); | |
2055 | |
2056 return e; | |
2057 } | |
123 | 2058 final void AssignExp_buildArrayIdent(OutBuffer buf, Expressions arguments, string Str) |
174 | 2059 { |
2060 /* Evaluate assign expressions right to left | |
2061 */ | |
2062 e2.buildArrayIdent(buf, arguments); | |
2063 e1.buildArrayIdent(buf, arguments); | |
2064 buf.writestring(Str); | |
2065 buf.writestring("ass"); | |
123 | 2066 } |
174 | 2067 |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
2068 final void Exp_buildArrayIdent(OutBuffer buf, Expressions arguments, string Str) |
174 | 2069 { |
2070 /* Evaluate assign expressions left to right | |
2071 */ | |
2072 e1.buildArrayIdent(buf, arguments); | |
2073 e2.buildArrayIdent(buf, arguments); | |
2074 buf.writestring(Str); | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
2075 } |
174 | 2076 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
2077 final Expression AssignExp_buildArrayLoop(AssignExpType)(Parameters fparams)// if (is (AssignExpType : AssignExp)) |
123 | 2078 { |
2079 /* Evaluate assign expressions right to left | |
2080 */ | |
2081 Expression ex2 = e2.buildArrayLoop(fparams); | |
2082 Expression ex1 = e1.buildArrayLoop(fparams); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
123
diff
changeset
|
2083 auto param = fparams[0]; |
123 | 2084 param.storageClass = STCundefined; |
2085 Expression e = new AssignExpType(Loc(0), ex1, ex2); | |
2086 return e; | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
2087 } |
174 | 2088 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
2089 final Expression Exp_buildArrayLoop(ExpType)(Parameters fparams) if (is (ExpType : BinExp)) |
123 | 2090 { |
2091 /* Evaluate assign expressions left to right | |
2092 */ | |
2093 Expression ex1 = e1.buildArrayLoop(fparams); | |
2094 Expression ex2 = e2.buildArrayLoop(fparams); | |
2095 Expression e = new ExpType(Loc(0), ex1, ex2); | |
2096 return e; | |
2097 } | |
174 | 2098 } |