comparison dmd/ArrayLengthExp.d @ 130:60bb0fe4563e

dmdfe 2.037 first main iteration
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Thu, 09 Sep 2010 22:51:44 +0100
parents e28b18c23469
children 438eaa11eed4
comparison
equal deleted inserted replaced
129:010eb8f0e18d 130:60bb0fe4563e
1 module dmd.ArrayLengthExp; 1 module dmd.ArrayLengthExp;
2 2
3 import dmd.common; 3 import dmd.common;
4 import dmd.Expression; 4 import dmd.Expression;
5 import dmd.BinExp;
5 import dmd.backend.elem; 6 import dmd.backend.elem;
6 import dmd.UnaExp; 7 import dmd.UnaExp;
7 import dmd.InterState; 8 import dmd.InterState;
8 import dmd.OutBuffer; 9 import dmd.OutBuffer;
9 import dmd.Loc; 10 import dmd.Loc;
11 import dmd.IRState; 12 import dmd.IRState;
12 import dmd.HdrGenState; 13 import dmd.HdrGenState;
13 import dmd.TOK; 14 import dmd.TOK;
14 import dmd.Type; 15 import dmd.Type;
15 import dmd.WANT; 16 import dmd.WANT;
17 import dmd.VarExp;
18 import dmd.VarDeclaration;
19 import dmd.PtrExp;
20 import dmd.Lexer;
21 import dmd.Identifier;
22 import dmd.ExpInitializer;
23 import dmd.DeclarationExp;
24 import dmd.CommaExp;
25 import dmd.AssignExp;
26 import dmd.AddrExp;
16 27
17 import dmd.expression.ArrayLength; 28 import dmd.expression.ArrayLength;
18 29
19 import dmd.backend.Util; 30 import dmd.backend.Util;
20 import dmd.backend.OPER; 31 import dmd.backend.OPER;
26 super(loc, TOK.TOKarraylength, ArrayLengthExp.sizeof, e1); 37 super(loc, TOK.TOKarraylength, ArrayLengthExp.sizeof, e1);
27 } 38 }
28 39
29 override Expression semantic(Scope sc) 40 override Expression semantic(Scope sc)
30 { 41 {
31 Expression e;
32
33 version (LOGSEMANTIC) { 42 version (LOGSEMANTIC) {
34 printf("ArrayLengthExp::semantic('%s')\n", toChars()); 43 printf("ArrayLengthExp::semantic('%s')\n", toChars());
35 } 44 }
36 if (!type) 45 if (!type)
37 { 46 {
41 type = Type.tsize_t; 50 type = Type.tsize_t;
42 } 51 }
43 return this; 52 return this;
44 } 53 }
45 54
55 static Expression rewriteOpAssign(BinExp exp)
56 {
57 Expression e;
58
59 assert(exp.e1.op == TOKarraylength);
60 auto ale = cast(ArrayLengthExp)exp.e1;
61 if (ale.e1.op == TOK.TOKvar)
62 {
63 e = opAssignToOp(exp.loc, exp.op, ale, exp.e2);
64 e = new AssignExp(exp.loc, ale.syntaxCopy(), e);
65 }
66 else
67 {
68 /* auto tmp = &array;
69 * (*tmp).length = (*tmp).length op e2
70 */
71 Identifier id = Lexer.uniqueId("__arraylength");
72 ExpInitializer ei = new ExpInitializer(ale.loc, new AddrExp(ale.loc, ale.e1));
73 VarDeclaration tmp = new VarDeclaration(ale.loc, ale.e1.type.pointerTo(), id, ei);
74
75 Expression e1 = new ArrayLengthExp(ale.loc, new PtrExp(ale.loc, new VarExp(ale.loc, tmp)));
76 Expression elvalue = e1.syntaxCopy();
77 e = opAssignToOp(exp.loc, exp.op, e1, exp.e2);
78 e = new AssignExp(exp.loc, elvalue, e);
79 e = new CommaExp(exp.loc, new DeclarationExp(ale.loc, tmp), e);
80 }
81 return e;
82 }
83
46 override Expression optimize(int result) 84 override Expression optimize(int result)
47 { 85 {
48 //printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars()); 86 //printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars());
49 e1 = e1.optimize(WANTvalue | (result & WANTinterpret)); 87 e1 = e1.optimize(WANTvalue | (result & WANTinterpret));
50 Expression e = this; 88 Expression e = this;