Mercurial > projects > ddmd
annotate dmd/ArrayLengthExp.d @ 132:c494af1dba80
Fixes for dmd 2.037
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Fri, 10 Sep 2010 19:14:09 +0100 |
parents | 60bb0fe4563e |
children | 438eaa11eed4 |
rev | line source |
---|---|
72 | 1 module dmd.ArrayLengthExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
5 import dmd.BinExp; |
72 | 6 import dmd.backend.elem; |
7 import dmd.UnaExp; | |
8 import dmd.InterState; | |
9 import dmd.OutBuffer; | |
10 import dmd.Loc; | |
11 import dmd.Scope; | |
12 import dmd.IRState; | |
13 import dmd.HdrGenState; | |
0 | 14 import dmd.TOK; |
15 import dmd.Type; | |
16 import dmd.WANT; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
17 import dmd.VarExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
18 import dmd.VarDeclaration; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
19 import dmd.PtrExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
20 import dmd.Lexer; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
21 import dmd.Identifier; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
22 import dmd.ExpInitializer; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
23 import dmd.DeclarationExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
24 import dmd.CommaExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
25 import dmd.AssignExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
26 import dmd.AddrExp; |
0 | 27 |
28 import dmd.expression.ArrayLength; | |
29 | |
30 import dmd.backend.Util; | |
72 | 31 import dmd.backend.OPER; |
32 | |
0 | 33 class ArrayLengthExp : UnaExp |
34 { | |
35 this(Loc loc, Expression e1) | |
36 { | |
37 super(loc, TOK.TOKarraylength, ArrayLengthExp.sizeof, e1); | |
38 } | |
39 | |
72 | 40 override Expression semantic(Scope sc) |
0 | 41 { |
42 version (LOGSEMANTIC) { | |
43 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
44 } | |
45 if (!type) | |
46 { | |
47 UnaExp.semantic(sc); | |
48 e1 = resolveProperties(sc, e1); | |
49 | |
50 type = Type.tsize_t; | |
51 } | |
52 return this; | |
53 } | |
54 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
55 static Expression rewriteOpAssign(BinExp exp) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
56 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
57 Expression e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
58 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
59 assert(exp.e1.op == TOKarraylength); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
60 auto ale = cast(ArrayLengthExp)exp.e1; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
61 if (ale.e1.op == TOK.TOKvar) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
62 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
63 e = opAssignToOp(exp.loc, exp.op, ale, exp.e2); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
64 e = new AssignExp(exp.loc, ale.syntaxCopy(), e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
65 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
66 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
67 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
68 /* auto tmp = &array; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
69 * (*tmp).length = (*tmp).length op e2 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
70 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
71 Identifier id = Lexer.uniqueId("__arraylength"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
72 ExpInitializer ei = new ExpInitializer(ale.loc, new AddrExp(ale.loc, ale.e1)); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
73 VarDeclaration tmp = new VarDeclaration(ale.loc, ale.e1.type.pointerTo(), id, ei); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
74 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
75 Expression e1 = new ArrayLengthExp(ale.loc, new PtrExp(ale.loc, new VarExp(ale.loc, tmp))); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
76 Expression elvalue = e1.syntaxCopy(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
77 e = opAssignToOp(exp.loc, exp.op, e1, exp.e2); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
78 e = new AssignExp(exp.loc, elvalue, e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
79 e = new CommaExp(exp.loc, new DeclarationExp(ale.loc, tmp), e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
80 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
81 return e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
82 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
83 |
72 | 84 override Expression optimize(int result) |
0 | 85 { |
86 //printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars()); | |
87 e1 = e1.optimize(WANTvalue | (result & WANTinterpret)); | |
88 Expression e = this; | |
89 if (e1.op == TOKstring || e1.op == TOKarrayliteral || e1.op == TOKassocarrayliteral) | |
90 { | |
91 e = ArrayLength(type, e1); | |
92 } | |
93 return e; | |
94 } | |
95 | |
72 | 96 override Expression interpret(InterState istate) |
0 | 97 { |
98 assert(false); | |
99 } | |
100 | |
72 | 101 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 102 { |
103 assert(false); | |
104 } | |
105 | |
72 | 106 override elem* toElem(IRState* irs) |
0 | 107 { |
108 elem *e = e1.toElem(irs); | |
109 e = el_una(OP64_32, type.totym(), e); | |
110 el_setLoc(e,loc); | |
111 return e; | |
112 } | |
113 } | |
114 |