Mercurial > projects > ddmd
annotate dmd/TupleExp.d @ 192:eb38fdcb3e62 default tip
updated to compile with dmd2.062
author | korDen |
---|---|
date | Sat, 02 Mar 2013 01:25:52 -0800 |
parents | b0d41ff5e0df |
children |
rev | line source |
---|---|
72 | 1 module dmd.TupleExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.TupleDeclaration; | |
6 import dmd.backend.elem; | |
7 import dmd.InterState; | |
8 import dmd.WANT; | |
9 import dmd.Type; | |
10 import dmd.OutBuffer; | |
11 import dmd.Loc; | |
12 import dmd.Scope; | |
13 import dmd.InlineCostState; | |
14 import dmd.IRState; | |
15 import dmd.InlineDoState; | |
16 import dmd.HdrGenState; | |
17 import dmd.InlineScanState; | |
18 import dmd.ArrayTypes; | |
19 import dmd.TypeExp; | |
20 import dmd.TypeTuple; | |
21 import dmd.TOK; | |
22 import dmd.TY; | |
23 import dmd.Dsymbol; | |
24 import dmd.DsymbolExp; | |
25 import dmd.DYNCAST; | |
26 import dmd.expression.Util; | |
27 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
28 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
|
29 |
72 | 30 /**************************************** |
31 * Expand tuples. | |
32 */ | |
33 /+ | |
34 void expandTuples(Expressions exps) | |
35 { | |
36 //printf("expandTuples()\n"); | |
37 if (exps) | |
38 { | |
39 for (size_t i = 0; i < exps.dim; i++) | |
40 { Expression arg = cast(Expression)exps.data[i]; | |
41 if (!arg) | |
42 continue; | |
43 | |
44 // Look for tuple with 0 members | |
45 if (arg.op == TOKtype) | |
46 { TypeExp e = cast(TypeExp)arg; | |
47 if (e.type.toBasetype().ty == Ttuple) | |
48 { TypeTuple tt = cast(TypeTuple)e.type.toBasetype(); | |
49 | |
50 if (!tt.arguments || tt.arguments.dim == 0) | |
51 { | |
52 exps.remove(i); | |
53 if (i == exps.dim) | |
54 return; | |
55 i--; | |
56 continue; | |
57 } | |
58 } | |
59 } | |
60 | |
61 // Inline expand all the tuples | |
62 while (arg.op == TOKtuple) | |
63 { TupleExp te = cast(TupleExp)arg; | |
64 | |
65 exps.remove(i); // remove arg | |
66 exps.insert(i, te.exps); // replace with tuple contents | |
67 if (i == exps.dim) | |
68 return; // empty tuple, no more arguments | |
69 arg = cast(Expression)exps.data[i]; | |
70 } | |
71 } | |
72 } | |
73 } | |
74 +/ | |
75 class TupleExp : Expression | |
76 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
77 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
|
78 |
72 | 79 Expressions exps; |
80 | |
81 this(Loc loc, Expressions exps) | |
82 { | |
178 | 83 register(); |
72 | 84 super(loc, TOKtuple, TupleExp.sizeof); |
85 | |
86 this.exps = exps; | |
87 this.type = null; | |
88 } | |
89 | |
90 this(Loc loc, TupleDeclaration tup) | |
91 { | |
178 | 92 register(); |
72 | 93 super(loc, TOKtuple, TupleExp.sizeof); |
94 exps = new Expressions(); | |
95 type = null; | |
96 | |
97 exps.reserve(tup.objects.dim); | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
90
diff
changeset
|
98 foreach (o; tup.objects) |
72 | 99 { |
100 if (auto e = cast(Expression)o) | |
101 { | |
102 e = e.syntaxCopy(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
103 exps.push(e); |
72 | 104 } |
105 else if (auto s = cast(Dsymbol)o) | |
106 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
107 auto e = new DsymbolExp(loc, s); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
108 exps.push(e); |
72 | 109 } |
110 else if (auto t = cast(Type)o) | |
111 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
112 auto e = new TypeExp(loc, t); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
113 exps.push(e); |
72 | 114 } |
115 else | |
116 { | |
117 error("%s is not an expression", o.toString()); | |
118 } | |
119 } | |
120 } | |
121 | |
122 override Expression syntaxCopy() | |
123 { | |
124 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
125 } | |
126 | |
127 override bool equals(Object o) | |
128 { | |
129 TupleExp ne; | |
130 | |
131 if (this == o) | |
132 return 1; | |
133 if ((cast(Expression)o).op == TOKtuple) | |
134 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
135 auto te = cast(TupleExp)o; |
72 | 136 if (exps.dim != te.exps.dim) |
137 return 0; | |
138 for (size_t i = 0; i < exps.dim; i++) | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
139 { auto e1 = exps[i]; |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
140 auto e2 = te.exps[i]; |
72 | 141 |
142 if (!e1.equals(e2)) | |
143 return 0; | |
144 } | |
145 return 1; | |
146 } | |
147 return 0; | |
148 } | |
149 | |
150 override Expression semantic(Scope sc) | |
151 { | |
152 version (LOGSEMANTIC) { | |
153 printf("+TupleExp::semantic(%s)\n", toChars()); | |
154 } | |
155 if (type) | |
156 return this; | |
157 | |
158 // Run semantic() on each argument | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
159 foreach (ref Expression e; exps) |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
160 { |
72 | 161 e = e.semantic(sc); |
162 if (!e.type) | |
163 { error("%s has no value", e.toChars()); | |
164 e.type = Type.terror; | |
165 } | |
166 } | |
167 | |
168 expandTuples(exps); | |
169 if (0 && exps.dim == 1) | |
170 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
171 return exps[0]; |
72 | 172 } |
173 type = new TypeTuple(exps); | |
174 type = type.semantic(loc, sc); | |
175 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
176 return this; | |
177 } | |
178 | |
179 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) | |
180 { | |
181 buf.writestring("tuple("); | |
182 argsToCBuffer(buf, exps, hgs); | |
183 buf.writeByte(')'); | |
184 } | |
185 | |
186 override void scanForNestedRef(Scope sc) | |
187 { | |
188 assert(false); | |
189 } | |
190 | |
191 override void checkEscape() | |
192 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
193 foreach(e; exps) |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
194 { |
72 | 195 e.checkEscape(); |
196 } | |
197 } | |
198 | |
199 override bool checkSideEffect(int flag) | |
200 { | |
201 bool f = false; | |
202 | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
203 foreach(e; exps) |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
204 { |
72 | 205 f |= e.checkSideEffect(2); |
206 } | |
207 if (flag == 0 && f == 0) | |
208 Expression.checkSideEffect(0); | |
209 return f; | |
210 } | |
211 | |
212 override Expression optimize(int result) | |
213 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
214 foreach(ref Expression e; exps) |
72 | 215 { |
216 e = e.optimize(WANTvalue | (result & WANTinterpret)); | |
217 } | |
218 return this; | |
219 } | |
220 | |
221 override Expression interpret(InterState istate) | |
222 { | |
223 assert(false); | |
224 } | |
225 | |
226 override Expression castTo(Scope sc, Type t) | |
227 { | |
228 assert(false); | |
229 } | |
230 | |
231 override elem* toElem(IRState* irs) | |
232 { | |
233 assert(false); | |
234 } | |
235 | |
236 override bool canThrow() | |
237 { | |
238 return arrayExpressionCanThrow(exps); | |
239 } | |
240 | |
241 override int inlineCost(InlineCostState* ics) | |
242 { | |
243 assert(false); | |
244 } | |
245 | |
246 override Expression doInline(InlineDoState ids) | |
247 { | |
248 assert(false); | |
249 } | |
250 | |
251 override Expression inlineScan(InlineScanState* iss) | |
252 { | |
253 assert(false); | |
254 } | |
255 } | |
256 |