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