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