Mercurial > projects > ddmd
comparison dmd/interpret/Util.d @ 179:cd48cb899aee
Updated to dmd2.040
author | korDen |
---|---|
date | Sun, 17 Oct 2010 20:56:07 +0400 |
parents | fe2e1b93e88f |
children |
comparison
equal
deleted
inserted
replaced
178:e3afd1303184 | 179:cd48cb899aee |
---|---|
4 import dmd.StructDeclaration; | 4 import dmd.StructDeclaration; |
5 import dmd.Expression; | 5 import dmd.Expression; |
6 import dmd.FuncDeclaration; | 6 import dmd.FuncDeclaration; |
7 import dmd.InterState; | 7 import dmd.InterState; |
8 import dmd.ArrayTypes; | 8 import dmd.ArrayTypes; |
9 import dmd.StringExp; | |
9 import dmd.GlobalExpressions; | 10 import dmd.GlobalExpressions; |
10 import dmd.TOK; | 11 import dmd.TOK; |
11 import dmd.AssocArrayLiteralExp; | 12 import dmd.AssocArrayLiteralExp; |
12 import dmd.IntegerExp; | 13 import dmd.IntegerExp; |
14 import dmd.Id; | |
13 import dmd.Type; | 15 import dmd.Type; |
14 import dmd.Declaration; | 16 import dmd.Declaration; |
15 import dmd.Loc; | 17 import dmd.Loc; |
16 import dmd.ArrayLiteralExp; | 18 import dmd.ArrayLiteralExp; |
17 import dmd.TypeAArray; | 19 import dmd.TypeAArray; |
22 import dmd.SymbolDeclaration; | 24 import dmd.SymbolDeclaration; |
23 import dmd.StructLiteralExp; | 25 import dmd.StructLiteralExp; |
24 import dmd.VarDeclaration; | 26 import dmd.VarDeclaration; |
25 import dmd.Util; | 27 import dmd.Util; |
26 | 28 |
29 import core.memory; | |
30 import core.stdc.string; | |
31 | |
27 version(DMDV1) | 32 version(DMDV1) |
28 { | 33 { |
29 Expression interpret_aaLen(InterState istate, Expressions arguments) | 34 Expression interpret_aaLen(InterState istate, Expressions arguments) |
30 { | 35 { |
31 if (!arguments || arguments.dim != 1) | 36 if (!arguments || arguments.dim != 1) |
138 VarDeclaration v = d.isVarDeclaration(); | 143 VarDeclaration v = d.isVarDeclaration(); |
139 SymbolDeclaration s = d.isSymbolDeclaration(); | 144 SymbolDeclaration s = d.isSymbolDeclaration(); |
140 if (v) | 145 if (v) |
141 { | 146 { |
142 ///version (DMDV2) { | 147 ///version (DMDV2) { |
148 /* Magic variable __ctfe always returns true when interpreting | |
149 */ | |
150 if (v.ident == Id.ctfe) | |
151 return new IntegerExp(loc, 1, Type.tbool); | |
152 | |
143 if ((v.isConst() || v.isImmutable() || v.storage_class & STCmanifest) && v.init && !v.value) | 153 if ((v.isConst() || v.isImmutable() || v.storage_class & STCmanifest) && v.init && !v.value) |
144 ///} else { | 154 ///} else { |
145 /// if (v.isConst() && v.init) | 155 /// if (v.isConst() && v.init) |
146 ///} | 156 ///} |
147 { | 157 { |
148 e = v.init.toExpression(); | 158 e = v.init.toExpression(); |
149 if (e && !e.type) | 159 if (e && !e.type) |
150 e.type = v.type; | 160 e.type = v.type; |
151 } | 161 } |
162 else if (v.isCTFE() && !v.value) | |
163 { | |
164 if (v.init) | |
165 { | |
166 e = v.init.toExpression(); | |
167 e = e.interpret(istate); | |
168 } | |
169 else // This should never happen | |
170 e = v.type.defaultInitLiteral(Loc(0)); | |
171 } | |
172 | |
152 else | 173 else |
153 { | 174 { |
154 e = v.value; | 175 e = v.value; |
155 if (!v.isCTFE()) | 176 if (!v.isCTFE()) |
156 { | 177 { |
196 } | 217 } |
197 return expsx; | 218 return expsx; |
198 } | 219 } |
199 | 220 |
200 /*************************************** | 221 /*************************************** |
201 * Returns oldelems[0..insertpoint] ~ newelems ~ oldelems[insertpoint..$] | 222 * Returns oldelems[0..insertpoint] ~ newelems ~ oldelems[insertpoint+newelems.length..$] |
202 */ | 223 */ |
203 Expressions spliceElements(Expressions oldelems, Expressions newelems, size_t insertpoint) | 224 Expressions spliceElements(Expressions oldelems, Expressions newelems, size_t insertpoint) |
204 { | 225 { |
205 auto expsx = new Expressions(); | 226 auto expsx = new Expressions(); |
206 expsx.setDim(oldelems.dim); | 227 expsx.setDim(oldelems.dim); |
210 expsx[j] = newelems[j - insertpoint]; | 231 expsx[j] = newelems[j - insertpoint]; |
211 else | 232 else |
212 expsx[j] = oldelems[j]; | 233 expsx[j] = oldelems[j]; |
213 } | 234 } |
214 return expsx; | 235 return expsx; |
236 } | |
237 | |
238 /*************************************** | |
239 * Returns oldstr[0..insertpoint] ~ newstr ~ oldstr[insertpoint+newlen..$] | |
240 */ | |
241 StringExp spliceStringExp(StringExp oldstr, StringExp newstr, size_t insertpoint) | |
242 { | |
243 assert(oldstr.sz==newstr.sz); | |
244 char* s; | |
245 size_t oldlen = oldstr.len; | |
246 size_t newlen = newstr.len; | |
247 size_t sz = oldstr.sz; | |
248 s = cast(char*)GC.calloc(oldlen + 1, sz); | |
249 memcpy(s, oldstr.string_, oldlen * sz); | |
250 memcpy(s + insertpoint * sz, newstr.string_, newlen * sz); | |
251 StringExp se2 = new StringExp(oldstr.loc, cast(string)s[0..oldlen]); | |
252 se2.committed = oldstr.committed; | |
253 se2.postfix = oldstr.postfix; | |
254 se2.type = oldstr.type; | |
255 return se2; | |
256 } | |
257 | |
258 /****************************** | |
259 * Create a string literal consisting of 'value' duplicated 'dim' times. | |
260 */ | |
261 StringExp createBlockDuplicatedStringLiteral(Type type, dchar value, size_t dim, int sz) | |
262 { | |
263 char* s; | |
264 s = cast(char*)GC.calloc(dim + 1, sz); | |
265 for (int elemi = 0; elemi < dim; ++elemi) | |
266 { | |
267 switch (sz) | |
268 { | |
269 case 1: s[elemi] = cast(char)value; break; | |
270 case 2: (cast(wchar*)s)[elemi] = cast(wchar)value; break; | |
271 case 4: (cast(dchar*)s)[elemi] = value; break; | |
272 default: assert(0); | |
273 } | |
274 } | |
275 StringExp se = new StringExp(Loc(0), cast(string)s[0..dim]); | |
276 se.type = type; | |
277 return se; | |
215 } | 278 } |
216 | 279 |
217 /****************************** | 280 /****************************** |
218 * Create an array literal consisting of 'elem' duplicated 'dim' times. | 281 * Create an array literal consisting of 'elem' duplicated 'dim' times. |
219 */ | 282 */ |