Mercurial > projects > ddmd
annotate dmd/ArrayLengthExp.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | e3afd1303184 |
children |
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; |
174 | 20 import dmd.PREC; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
21 import dmd.VarDeclaration; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
22 import dmd.PtrExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
23 import dmd.Lexer; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
24 import dmd.Identifier; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
25 import dmd.ExpInitializer; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
26 import dmd.DeclarationExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
27 import dmd.CommaExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
28 import dmd.AssignExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
29 import dmd.AddrExp; |
0 | 30 |
31 import dmd.expression.ArrayLength; | |
173 | 32 import dmd.expression.Util; |
0 | 33 |
34 import dmd.backend.Util; | |
72 | 35 import dmd.backend.OPER; |
36 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
37 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
38 |
0 | 39 class ArrayLengthExp : UnaExp |
40 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
41 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
42 |
0 | 43 this(Loc loc, Expression e1) |
44 { | |
178 | 45 register(); |
0 | 46 super(loc, TOK.TOKarraylength, ArrayLengthExp.sizeof, e1); |
47 } | |
48 | |
72 | 49 override Expression semantic(Scope sc) |
0 | 50 { |
51 version (LOGSEMANTIC) { | |
52 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
53 } | |
54 if (!type) | |
55 { | |
56 UnaExp.semantic(sc); | |
57 e1 = resolveProperties(sc, e1); | |
58 | |
59 type = Type.tsize_t; | |
60 } | |
61 return this; | |
62 } | |
63 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
64 static Expression rewriteOpAssign(BinExp exp) |
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 Expression e; |
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 assert(exp.e1.op == TOKarraylength); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
69 auto ale = cast(ArrayLengthExp)exp.e1; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
70 if (ale.e1.op == TOK.TOKvar) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
71 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
72 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
|
73 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
|
74 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
75 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
76 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
77 /* auto tmp = &array; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
78 * (*tmp).length = (*tmp).length op e2 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
79 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
80 Identifier id = Lexer.uniqueId("__arraylength"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
81 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
|
82 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
|
83 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
84 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
|
85 Expression elvalue = e1.syntaxCopy(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
86 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
|
87 e = new AssignExp(exp.loc, elvalue, e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
88 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
|
89 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
90 return e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
91 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
92 |
72 | 93 override Expression optimize(int result) |
0 | 94 { |
95 //printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars()); | |
96 e1 = e1.optimize(WANTvalue | (result & WANTinterpret)); | |
97 Expression e = this; | |
98 if (e1.op == TOKstring || e1.op == TOKarrayliteral || e1.op == TOKassocarrayliteral) | |
99 { | |
100 e = ArrayLength(type, e1); | |
101 } | |
102 return e; | |
103 } | |
104 | |
72 | 105 override Expression interpret(InterState istate) |
0 | 106 { |
162 | 107 Expression e; |
108 Expression e1; | |
109 | |
110 version (LOG) { | |
111 printf("ArrayLengthExp.interpret() %s\n", toChars()); | |
112 } | |
113 e1 = this.e1.interpret(istate); | |
114 if (e1 is EXP_CANT_INTERPRET) | |
115 goto Lcant; | |
116 if (e1.op == TOKstring || e1.op == TOKarrayliteral || e1.op == TOKassocarrayliteral) | |
117 { | |
118 e = ArrayLength(type, e1); | |
119 } | |
120 else if (e1.op == TOKnull) | |
121 { | |
122 e = new IntegerExp(loc, 0, type); | |
123 } | |
124 else | |
125 goto Lcant; | |
126 return e; | |
127 | |
128 Lcant: | |
129 return EXP_CANT_INTERPRET; | |
0 | 130 } |
131 | |
72 | 132 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 133 { |
174 | 134 expToCBuffer(buf, hgs, e1, PREC_primary); |
135 buf.writestring(".length"); | |
0 | 136 } |
137 | |
72 | 138 override elem* toElem(IRState* irs) |
0 | 139 { |
140 elem *e = e1.toElem(irs); | |
141 e = el_una(OP64_32, type.totym(), e); | |
142 el_setLoc(e,loc); | |
143 return e; | |
144 } | |
145 } | |
146 |