1
|
1
|
|
2 // Compiler implementation of the D programming language
|
|
3 // Copyright (c) 1999-2007 by Digital Mars
|
|
4 // All Rights Reserved
|
|
5 // written by Walter Bright
|
|
6 // http://www.digitalmars.com
|
|
7 // License for redistribution is by either the Artistic License
|
|
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
9 // See the included readme.txt for details.
|
|
10
|
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <assert.h>
|
|
14
|
|
15 #include "mem.h"
|
|
16
|
|
17 #include "statement.h"
|
|
18 #include "expression.h"
|
|
19 #include "cond.h"
|
|
20 #include "init.h"
|
|
21 #include "staticassert.h"
|
|
22 #include "mtype.h"
|
|
23 #include "scope.h"
|
|
24 #include "declaration.h"
|
|
25 #include "aggregate.h"
|
|
26 #include "id.h"
|
|
27
|
|
28 #define LOG 0
|
|
29
|
|
30 struct InterState
|
|
31 {
|
|
32 InterState *caller; // calling function's InterState
|
|
33 FuncDeclaration *fd; // function being interpreted
|
|
34 Dsymbols vars; // variables used in this function
|
|
35 Statement *start; // if !=NULL, start execution at this statement
|
|
36 Statement *gotoTarget; // target of EXP_GOTO_INTERPRET result
|
|
37
|
|
38 InterState();
|
|
39 };
|
|
40
|
|
41 InterState::InterState()
|
|
42 {
|
|
43 memset(this, 0, sizeof(InterState));
|
|
44 }
|
|
45
|
|
46 Expression *interpret_aaLen(InterState *istate, Expressions *arguments);
|
|
47 Expression *interpret_aaKeys(InterState *istate, Expressions *arguments);
|
|
48 Expression *interpret_aaValues(InterState *istate, Expressions *arguments);
|
|
49
|
|
50 /*************************************
|
|
51 * Attempt to interpret a function given the arguments.
|
|
52 * Input:
|
|
53 * istate state for calling function (NULL if none)
|
|
54 * Return result expression if successful, NULL if not.
|
|
55 */
|
|
56
|
|
57 Expression *FuncDeclaration::interpret(InterState *istate, Expressions *arguments)
|
|
58 {
|
|
59 #if LOG
|
|
60 printf("FuncDeclaration::interpret() %s\n", toChars());
|
|
61 printf("cantInterpret = %d, semanticRun = %d\n", cantInterpret, semanticRun);
|
|
62 #endif
|
|
63 if (global.errors)
|
|
64 return NULL;
|
|
65 if (ident == Id::aaLen)
|
|
66 return interpret_aaLen(istate, arguments);
|
|
67 else if (ident == Id::aaKeys)
|
|
68 return interpret_aaKeys(istate, arguments);
|
|
69 else if (ident == Id::aaValues)
|
|
70 return interpret_aaValues(istate, arguments);
|
|
71
|
|
72 if (cantInterpret || semanticRun == 1)
|
|
73 return NULL;
|
|
74
|
|
75 if (needThis() || isNested() || !fbody)
|
|
76 { cantInterpret = 1;
|
|
77 return NULL;
|
|
78 }
|
|
79
|
19
|
80 //printf("test2 %d, %p\n", semanticRun, scope);
|
1
|
81 if (semanticRun == 0 && scope)
|
|
82 {
|
|
83 semantic3(scope);
|
|
84 }
|
|
85 if (semanticRun < 2)
|
|
86 return NULL;
|
|
87
|
|
88 Type *tb = type->toBasetype();
|
|
89 assert(tb->ty == Tfunction);
|
|
90 TypeFunction *tf = (TypeFunction *)tb;
|
|
91 Type *tret = tf->next->toBasetype();
|
|
92 if (tf->varargs /*|| tret->ty == Tvoid*/)
|
|
93 { cantInterpret = 1;
|
|
94 return NULL;
|
|
95 }
|
|
96
|
|
97 if (tf->parameters)
|
|
98 { size_t dim = Argument::dim(tf->parameters);
|
|
99 for (size_t i = 0; i < dim; i++)
|
|
100 { Argument *arg = Argument::getNth(tf->parameters, i);
|
|
101 if (arg->storageClass & STClazy)
|
|
102 { cantInterpret = 1;
|
|
103 return NULL;
|
|
104 }
|
|
105 }
|
|
106 }
|
|
107
|
|
108 InterState istatex;
|
|
109 istatex.caller = istate;
|
|
110 istatex.fd = this;
|
|
111
|
|
112 Expressions vsave;
|
|
113 size_t dim = 0;
|
|
114 if (arguments)
|
|
115 {
|
|
116 dim = arguments->dim;
|
|
117 assert(!dim || parameters->dim == dim);
|
|
118 vsave.setDim(dim);
|
|
119
|
|
120 for (size_t i = 0; i < dim; i++)
|
|
121 { Expression *earg = (Expression *)arguments->data[i];
|
|
122 Argument *arg = Argument::getNth(tf->parameters, i);
|
|
123 VarDeclaration *v = (VarDeclaration *)parameters->data[i];
|
|
124 vsave.data[i] = v->value;
|
|
125 #if LOG
|
|
126 printf("arg[%d] = %s\n", i, earg->toChars());
|
|
127 #endif
|
|
128 if (arg->storageClass & (STCout | STCref))
|
|
129 {
|
|
130 /* Bind out or ref parameter to the corresponding
|
|
131 * variable v2
|
|
132 */
|
|
133 if (!istate || earg->op != TOKvar)
|
|
134 return NULL; // can't bind to non-interpreted vars
|
|
135
|
|
136 VarDeclaration *v2;
|
|
137 while (1)
|
|
138 {
|
|
139 VarExp *ve = (VarExp *)earg;
|
|
140 v2 = ve->var->isVarDeclaration();
|
|
141 if (!v2)
|
|
142 return NULL;
|
|
143 if (!v2->value || v2->value->op != TOKvar)
|
|
144 break;
|
|
145 earg = v2->value;
|
|
146 }
|
|
147
|
|
148 v->value = new VarExp(earg->loc, v2);
|
|
149
|
|
150 /* Don't restore the value of v2 upon function return
|
|
151 */
|
|
152 assert(istate);
|
|
153 for (size_t i = 0; i < istate->vars.dim; i++)
|
|
154 { VarDeclaration *v = (VarDeclaration *)istate->vars.data[i];
|
|
155 if (v == v2)
|
|
156 { istate->vars.data[i] = NULL;
|
|
157 break;
|
|
158 }
|
|
159 }
|
|
160 }
|
|
161 else
|
|
162 { /* Value parameters
|
|
163 */
|
|
164 earg = earg->interpret(&istatex);
|
|
165 if (earg == EXP_CANT_INTERPRET)
|
|
166 return NULL;
|
|
167 v->value = earg;
|
|
168 }
|
|
169 #if LOG
|
|
170 printf("interpreted arg[%d] = %s\n", i, earg->toChars());
|
|
171 #endif
|
|
172 }
|
|
173 }
|
|
174
|
|
175 /* Save the values of the local variables used
|
|
176 */
|
|
177 Expressions valueSaves;
|
|
178 if (istate)
|
|
179 {
|
|
180 //printf("saving state...\n");
|
|
181 valueSaves.setDim(istate->vars.dim);
|
|
182 for (size_t i = 0; i < istate->vars.dim; i++)
|
|
183 { VarDeclaration *v = (VarDeclaration *)istate->vars.data[i];
|
|
184 if (v)
|
|
185 {
|
|
186 //printf("\tsaving [%d] %s = %s\n", i, v->toChars(), v->value->toChars());
|
|
187 valueSaves.data[i] = v->value;
|
|
188 v->value = NULL;
|
|
189 }
|
|
190 }
|
|
191 }
|
|
192
|
|
193 Expression *e = NULL;
|
|
194
|
|
195 while (1)
|
|
196 {
|
|
197 e = fbody->interpret(&istatex);
|
|
198 if (e == EXP_CANT_INTERPRET)
|
|
199 {
|
|
200 #if LOG
|
|
201 printf("function body failed to interpret\n");
|
|
202 #endif
|
|
203 e = NULL;
|
|
204 }
|
|
205
|
|
206 /* This is how we deal with a recursive statement AST
|
|
207 * that has arbitrary goto statements in it.
|
|
208 * Bubble up a 'result' which is the target of the goto
|
|
209 * statement, then go recursively down the AST looking
|
|
210 * for that statement, then execute starting there.
|
|
211 */
|
|
212 if (e == EXP_GOTO_INTERPRET)
|
|
213 {
|
|
214 istatex.start = istatex.gotoTarget; // set starting statement
|
|
215 istatex.gotoTarget = NULL;
|
|
216 }
|
|
217 else
|
|
218 break;
|
|
219 }
|
|
220
|
|
221 /* Restore the parameter values
|
|
222 */
|
|
223 for (size_t i = 0; i < dim; i++)
|
|
224 {
|
|
225 VarDeclaration *v = (VarDeclaration *)parameters->data[i];
|
|
226 v->value = (Expression *)vsave.data[i];
|
|
227 }
|
|
228
|
|
229 if (istate)
|
|
230 {
|
|
231 /* Restore the variable values
|
|
232 */
|
|
233 for (size_t i = 0; i < istate->vars.dim; i++)
|
|
234 { VarDeclaration *v = (VarDeclaration *)istate->vars.data[i];
|
|
235 if (v)
|
|
236 v->value = (Expression *)valueSaves.data[i];
|
|
237 }
|
|
238 }
|
|
239
|
|
240 return e;
|
|
241 }
|
|
242
|
|
243 /******************************** Statement ***************************/
|
|
244
|
|
245 #define START() \
|
|
246 if (istate->start) \
|
|
247 { if (istate->start != this) \
|
|
248 return NULL; \
|
|
249 istate->start = NULL; \
|
|
250 }
|
|
251
|
|
252 /***********************************
|
|
253 * Interpret the statement.
|
|
254 * Returns:
|
|
255 * NULL continue to next statement
|
|
256 * EXP_CANT_INTERPRET cannot interpret statement at compile time
|
|
257 * !NULL expression from return statement
|
|
258 */
|
|
259
|
|
260 Expression *Statement::interpret(InterState *istate)
|
|
261 {
|
|
262 #if LOG
|
|
263 printf("Statement::interpret()\n");
|
|
264 #endif
|
|
265 START()
|
|
266 return EXP_CANT_INTERPRET;
|
|
267 }
|
|
268
|
|
269 Expression *ExpStatement::interpret(InterState *istate)
|
|
270 {
|
|
271 #if LOG
|
|
272 printf("ExpStatement::interpret(%s)\n", exp ? exp->toChars() : "");
|
|
273 #endif
|
|
274 START()
|
|
275 if (exp)
|
|
276 {
|
|
277 Expression *e = exp->interpret(istate);
|
|
278 if (e == EXP_CANT_INTERPRET)
|
|
279 {
|
19
|
280 //printf("-ExpStatement::interpret(): %p\n", e);
|
1
|
281 return EXP_CANT_INTERPRET;
|
|
282 }
|
|
283 }
|
|
284 return NULL;
|
|
285 }
|
|
286
|
|
287 Expression *CompoundStatement::interpret(InterState *istate)
|
|
288 { Expression *e = NULL;
|
|
289
|
|
290 #if LOG
|
|
291 printf("CompoundStatement::interpret()\n");
|
|
292 #endif
|
|
293 if (istate->start == this)
|
|
294 istate->start = NULL;
|
|
295 if (statements)
|
|
296 {
|
|
297 for (size_t i = 0; i < statements->dim; i++)
|
|
298 { Statement *s = (Statement *)statements->data[i];
|
|
299
|
|
300 if (s)
|
|
301 {
|
|
302 e = s->interpret(istate);
|
|
303 if (e)
|
|
304 break;
|
|
305 }
|
|
306 }
|
|
307 }
|
|
308 #if LOG
|
|
309 printf("-CompoundStatement::interpret() %p\n", e);
|
|
310 #endif
|
|
311 return e;
|
|
312 }
|
|
313
|
|
314 Expression *UnrolledLoopStatement::interpret(InterState *istate)
|
|
315 { Expression *e = NULL;
|
|
316
|
|
317 #if LOG
|
|
318 printf("UnrolledLoopStatement::interpret()\n");
|
|
319 #endif
|
|
320 if (istate->start == this)
|
|
321 istate->start = NULL;
|
|
322 if (statements)
|
|
323 {
|
|
324 for (size_t i = 0; i < statements->dim; i++)
|
|
325 { Statement *s = (Statement *)statements->data[i];
|
|
326
|
|
327 e = s->interpret(istate);
|
|
328 if (e == EXP_CANT_INTERPRET)
|
|
329 break;
|
|
330 if (e == EXP_CONTINUE_INTERPRET)
|
|
331 { e = NULL;
|
|
332 continue;
|
|
333 }
|
|
334 if (e == EXP_BREAK_INTERPRET)
|
|
335 { e = NULL;
|
|
336 break;
|
|
337 }
|
|
338 if (e)
|
|
339 break;
|
|
340 }
|
|
341 }
|
|
342 return e;
|
|
343 }
|
|
344
|
|
345 Expression *IfStatement::interpret(InterState *istate)
|
|
346 {
|
|
347 #if LOG
|
|
348 printf("IfStatement::interpret(%s)\n", condition->toChars());
|
|
349 #endif
|
|
350
|
|
351 if (istate->start == this)
|
|
352 istate->start = NULL;
|
|
353 if (istate->start)
|
|
354 {
|
|
355 Expression *e = NULL;
|
|
356 if (ifbody)
|
|
357 e = ifbody->interpret(istate);
|
|
358 if (istate->start && elsebody)
|
|
359 e = elsebody->interpret(istate);
|
|
360 return e;
|
|
361 }
|
|
362
|
|
363 Expression *e = condition->interpret(istate);
|
|
364 assert(e);
|
|
365 //if (e == EXP_CANT_INTERPRET) printf("cannot interpret\n");
|
|
366 if (e != EXP_CANT_INTERPRET)
|
|
367 {
|
19
|
368 if (e->isBool(TRUE))
|
|
369 e = ifbody ? ifbody->interpret(istate) : NULL;
|
|
370 else if (e->isBool(FALSE))
|
|
371 e = elsebody ? elsebody->interpret(istate) : NULL;
|
1
|
372 else
|
|
373 {
|
19
|
374 e = EXP_CANT_INTERPRET;
|
1
|
375 }
|
|
376 }
|
|
377 return e;
|
|
378 }
|
|
379
|
|
380 Expression *ScopeStatement::interpret(InterState *istate)
|
|
381 {
|
|
382 #if LOG
|
|
383 printf("ScopeStatement::interpret()\n");
|
|
384 #endif
|
|
385 if (istate->start == this)
|
|
386 istate->start = NULL;
|
|
387 return statement ? statement->interpret(istate) : NULL;
|
|
388 }
|
|
389
|
|
390 Expression *ReturnStatement::interpret(InterState *istate)
|
|
391 {
|
|
392 #if LOG
|
|
393 printf("ReturnStatement::interpret(%s)\n", exp ? exp->toChars() : "");
|
|
394 #endif
|
|
395 START()
|
|
396 if (!exp)
|
|
397 return EXP_VOID_INTERPRET;
|
|
398 #if LOG
|
|
399 Expression *e = exp->interpret(istate);
|
|
400 printf("e = %p\n", e);
|
|
401 return e;
|
|
402 #else
|
|
403 return exp->interpret(istate);
|
|
404 #endif
|
|
405 }
|
|
406
|
|
407 Expression *BreakStatement::interpret(InterState *istate)
|
|
408 {
|
|
409 #if LOG
|
|
410 printf("BreakStatement::interpret()\n");
|
|
411 #endif
|
|
412 START()
|
|
413 if (ident)
|
|
414 return EXP_CANT_INTERPRET;
|
|
415 else
|
|
416 return EXP_BREAK_INTERPRET;
|
|
417 }
|
|
418
|
|
419 Expression *ContinueStatement::interpret(InterState *istate)
|
|
420 {
|
|
421 #if LOG
|
|
422 printf("ContinueStatement::interpret()\n");
|
|
423 #endif
|
|
424 START()
|
|
425 if (ident)
|
|
426 return EXP_CANT_INTERPRET;
|
|
427 else
|
|
428 return EXP_CONTINUE_INTERPRET;
|
|
429 }
|
|
430
|
|
431 Expression *WhileStatement::interpret(InterState *istate)
|
|
432 {
|
|
433 #if LOG
|
|
434 printf("WhileStatement::interpret()\n");
|
|
435 #endif
|
|
436 if (istate->start == this)
|
|
437 istate->start = NULL;
|
|
438 Expression *e;
|
|
439
|
|
440 if (istate->start)
|
|
441 {
|
|
442 e = body ? body->interpret(istate) : NULL;
|
|
443 if (istate->start)
|
|
444 return NULL;
|
|
445 if (e == EXP_CANT_INTERPRET)
|
|
446 return e;
|
|
447 if (e == EXP_BREAK_INTERPRET)
|
|
448 return NULL;
|
|
449 if (e != EXP_CONTINUE_INTERPRET)
|
|
450 return e;
|
|
451 }
|
|
452
|
|
453 while (1)
|
|
454 {
|
|
455 e = condition->interpret(istate);
|
|
456 if (e == EXP_CANT_INTERPRET)
|
|
457 break;
|
|
458 if (!e->isConst())
|
|
459 { e = EXP_CANT_INTERPRET;
|
|
460 break;
|
|
461 }
|
|
462 if (e->isBool(TRUE))
|
|
463 { e = body ? body->interpret(istate) : NULL;
|
|
464 if (e == EXP_CANT_INTERPRET)
|
|
465 break;
|
|
466 if (e == EXP_CONTINUE_INTERPRET)
|
|
467 continue;
|
|
468 if (e == EXP_BREAK_INTERPRET)
|
|
469 { e = NULL;
|
|
470 break;
|
|
471 }
|
|
472 if (e)
|
|
473 break;
|
|
474 }
|
|
475 else if (e->isBool(FALSE))
|
|
476 { e = NULL;
|
|
477 break;
|
|
478 }
|
|
479 else
|
|
480 assert(0);
|
|
481 }
|
|
482 return e;
|
|
483 }
|
|
484
|
|
485 Expression *DoStatement::interpret(InterState *istate)
|
|
486 {
|
|
487 #if LOG
|
|
488 printf("DoStatement::interpret()\n");
|
|
489 #endif
|
|
490 if (istate->start == this)
|
|
491 istate->start = NULL;
|
|
492 Expression *e;
|
|
493
|
|
494 if (istate->start)
|
|
495 {
|
|
496 e = body ? body->interpret(istate) : NULL;
|
|
497 if (istate->start)
|
|
498 return NULL;
|
|
499 if (e == EXP_CANT_INTERPRET)
|
|
500 return e;
|
|
501 if (e == EXP_BREAK_INTERPRET)
|
|
502 return NULL;
|
|
503 if (e == EXP_CONTINUE_INTERPRET)
|
|
504 goto Lcontinue;
|
|
505 if (e)
|
|
506 return e;
|
|
507 }
|
|
508
|
|
509 while (1)
|
|
510 {
|
|
511 e = body ? body->interpret(istate) : NULL;
|
|
512 if (e == EXP_CANT_INTERPRET)
|
|
513 break;
|
|
514 if (e == EXP_BREAK_INTERPRET)
|
|
515 { e = NULL;
|
|
516 break;
|
|
517 }
|
|
518 if (e && e != EXP_CONTINUE_INTERPRET)
|
|
519 break;
|
|
520
|
|
521 Lcontinue:
|
|
522 e = condition->interpret(istate);
|
|
523 if (e == EXP_CANT_INTERPRET)
|
|
524 break;
|
|
525 if (!e->isConst())
|
|
526 { e = EXP_CANT_INTERPRET;
|
|
527 break;
|
|
528 }
|
|
529 if (e->isBool(TRUE))
|
|
530 {
|
|
531 }
|
|
532 else if (e->isBool(FALSE))
|
|
533 { e = NULL;
|
|
534 break;
|
|
535 }
|
|
536 else
|
|
537 assert(0);
|
|
538 }
|
|
539 return e;
|
|
540 }
|
|
541
|
|
542 Expression *ForStatement::interpret(InterState *istate)
|
|
543 {
|
|
544 #if LOG
|
|
545 printf("ForStatement::interpret()\n");
|
|
546 #endif
|
|
547 if (istate->start == this)
|
|
548 istate->start = NULL;
|
|
549 Expression *e;
|
|
550
|
|
551 if (init)
|
|
552 {
|
|
553 e = init->interpret(istate);
|
|
554 if (e == EXP_CANT_INTERPRET)
|
|
555 return e;
|
|
556 assert(!e);
|
|
557 }
|
|
558
|
|
559 if (istate->start)
|
|
560 {
|
|
561 e = body ? body->interpret(istate) : NULL;
|
|
562 if (istate->start)
|
|
563 return NULL;
|
|
564 if (e == EXP_CANT_INTERPRET)
|
|
565 return e;
|
|
566 if (e == EXP_BREAK_INTERPRET)
|
|
567 return NULL;
|
|
568 if (e == EXP_CONTINUE_INTERPRET)
|
|
569 goto Lcontinue;
|
|
570 if (e)
|
|
571 return e;
|
|
572 }
|
|
573
|
|
574 while (1)
|
|
575 {
|
|
576 e = condition->interpret(istate);
|
|
577 if (e == EXP_CANT_INTERPRET)
|
|
578 break;
|
|
579 if (!e->isConst())
|
|
580 { e = EXP_CANT_INTERPRET;
|
|
581 break;
|
|
582 }
|
|
583 if (e->isBool(TRUE))
|
|
584 { e = body ? body->interpret(istate) : NULL;
|
|
585 if (e == EXP_CANT_INTERPRET)
|
|
586 break;
|
|
587 if (e == EXP_BREAK_INTERPRET)
|
|
588 { e = NULL;
|
|
589 break;
|
|
590 }
|
|
591 if (e && e != EXP_CONTINUE_INTERPRET)
|
|
592 break;
|
|
593 Lcontinue:
|
|
594 e = increment->interpret(istate);
|
|
595 if (e == EXP_CANT_INTERPRET)
|
|
596 break;
|
|
597 }
|
|
598 else if (e->isBool(FALSE))
|
|
599 { e = NULL;
|
|
600 break;
|
|
601 }
|
|
602 else
|
|
603 assert(0);
|
|
604 }
|
|
605 return e;
|
|
606 }
|
|
607
|
|
608 Expression *ForeachStatement::interpret(InterState *istate)
|
|
609 {
|
|
610 #if LOG
|
|
611 printf("ForeachStatement::interpret()\n");
|
|
612 #endif
|
|
613 if (istate->start == this)
|
|
614 istate->start = NULL;
|
|
615 if (istate->start)
|
|
616 return NULL;
|
|
617
|
|
618 Expression *e = NULL;
|
|
619 Expression *eaggr;
|
|
620
|
|
621 if (value->isOut() || value->isRef())
|
|
622 return EXP_CANT_INTERPRET;
|
|
623
|
|
624 eaggr = aggr->interpret(istate);
|
|
625 if (eaggr == EXP_CANT_INTERPRET)
|
|
626 return EXP_CANT_INTERPRET;
|
|
627
|
|
628 Expression *dim = ArrayLength(Type::tsize_t, eaggr);
|
|
629 if (dim == EXP_CANT_INTERPRET)
|
|
630 return EXP_CANT_INTERPRET;
|
|
631
|
|
632 Expression *keysave = key ? key->value : NULL;
|
|
633 Expression *valuesave = value->value;
|
|
634
|
|
635 uinteger_t d = dim->toUInteger();
|
|
636 uinteger_t index;
|
|
637
|
|
638 if (op == TOKforeach)
|
|
639 {
|
|
640 for (index = 0; index < d; index++)
|
|
641 {
|
|
642 Expression *ekey = new IntegerExp(loc, index, Type::tsize_t);
|
|
643 if (key)
|
|
644 key->value = ekey;
|
|
645 e = Index(value->type, eaggr, ekey);
|
|
646 if (e == EXP_CANT_INTERPRET)
|
|
647 break;
|
|
648 value->value = e;
|
|
649
|
|
650 e = body ? body->interpret(istate) : NULL;
|
|
651 if (e == EXP_CANT_INTERPRET)
|
|
652 break;
|
|
653 if (e == EXP_BREAK_INTERPRET)
|
|
654 { e = NULL;
|
|
655 break;
|
|
656 }
|
|
657 if (e == EXP_CONTINUE_INTERPRET)
|
|
658 e = NULL;
|
|
659 else if (e)
|
|
660 break;
|
|
661 }
|
|
662 }
|
|
663 else // TOKforeach_reverse
|
|
664 {
|
|
665 for (index = d; index-- != 0;)
|
|
666 {
|
|
667 Expression *ekey = new IntegerExp(loc, index, Type::tsize_t);
|
|
668 if (key)
|
|
669 key->value = ekey;
|
|
670 e = Index(value->type, eaggr, ekey);
|
|
671 if (e == EXP_CANT_INTERPRET)
|
|
672 break;
|
|
673 value->value = e;
|
|
674
|
|
675 e = body ? body->interpret(istate) : NULL;
|
|
676 if (e == EXP_CANT_INTERPRET)
|
|
677 break;
|
|
678 if (e == EXP_BREAK_INTERPRET)
|
|
679 { e = NULL;
|
|
680 break;
|
|
681 }
|
|
682 if (e == EXP_CONTINUE_INTERPRET)
|
|
683 e = NULL;
|
|
684 else if (e)
|
|
685 break;
|
|
686 }
|
|
687 }
|
|
688 value->value = valuesave;
|
|
689 if (key)
|
|
690 key->value = keysave;
|
|
691 return e;
|
|
692 }
|
|
693
|
|
694 #if V2
|
|
695 Expression *ForeachRangeStatement::interpret(InterState *istate)
|
|
696 {
|
|
697 #if LOG
|
|
698 printf("ForeachRangeStatement::interpret()\n");
|
|
699 #endif
|
|
700 if (istate->start == this)
|
|
701 istate->start = NULL;
|
|
702 if (istate->start)
|
|
703 return NULL;
|
|
704
|
|
705 Expression *e = NULL;
|
|
706 Expression *elwr = lwr->interpret(istate);
|
|
707 if (elwr == EXP_CANT_INTERPRET)
|
|
708 return EXP_CANT_INTERPRET;
|
|
709
|
|
710 Expression *eupr = upr->interpret(istate);
|
|
711 if (eupr == EXP_CANT_INTERPRET)
|
|
712 return EXP_CANT_INTERPRET;
|
|
713
|
|
714 Expression *keysave = key->value;
|
|
715
|
|
716 if (op == TOKforeach)
|
|
717 {
|
|
718 key->value = elwr;
|
|
719
|
|
720 while (1)
|
|
721 {
|
|
722 e = Cmp(TOKlt, key->value->type, key->value, upr);
|
|
723 if (e == EXP_CANT_INTERPRET)
|
|
724 break;
|
|
725 if (e->isBool(TRUE) == FALSE)
|
|
726 { e = NULL;
|
|
727 break;
|
|
728 }
|
|
729
|
|
730 e = body ? body->interpret(istate) : NULL;
|
|
731 if (e == EXP_CANT_INTERPRET)
|
|
732 break;
|
|
733 if (e == EXP_BREAK_INTERPRET)
|
|
734 { e = NULL;
|
|
735 break;
|
|
736 }
|
|
737 e = Add(key->value->type, key->value, new IntegerExp(loc, 1, key->value->type));
|
|
738 if (e == EXP_CANT_INTERPRET)
|
|
739 break;
|
|
740 key->value = e;
|
|
741 }
|
|
742 }
|
|
743 else // TOKforeach_reverse
|
|
744 {
|
|
745 key->value = eupr;
|
|
746
|
|
747 while (1)
|
|
748 {
|
|
749 e = Cmp(TOKgt, key->value->type, key->value, lwr);
|
|
750 if (e == EXP_CANT_INTERPRET)
|
|
751 break;
|
|
752 if (e->isBool(TRUE) == FALSE)
|
|
753 { e = NULL;
|
|
754 break;
|
|
755 }
|
|
756
|
|
757 e = Min(key->value->type, key->value, new IntegerExp(loc, 1, key->value->type));
|
|
758 if (e == EXP_CANT_INTERPRET)
|
|
759 break;
|
|
760 key->value = e;
|
|
761
|
|
762 e = body ? body->interpret(istate) : NULL;
|
|
763 if (e == EXP_CANT_INTERPRET)
|
|
764 break;
|
|
765 if (e == EXP_BREAK_INTERPRET)
|
|
766 { e = NULL;
|
|
767 break;
|
|
768 }
|
|
769 }
|
|
770 }
|
|
771 key->value = keysave;
|
|
772 return e;
|
|
773 }
|
|
774 #endif
|
|
775
|
|
776 Expression *SwitchStatement::interpret(InterState *istate)
|
|
777 {
|
|
778 #if LOG
|
|
779 printf("SwitchStatement::interpret()\n");
|
|
780 #endif
|
|
781 if (istate->start == this)
|
|
782 istate->start = NULL;
|
|
783 Expression *e = NULL;
|
|
784
|
|
785 if (istate->start)
|
|
786 {
|
|
787 e = body ? body->interpret(istate) : NULL;
|
|
788 if (istate->start)
|
|
789 return NULL;
|
|
790 if (e == EXP_CANT_INTERPRET)
|
|
791 return e;
|
|
792 if (e == EXP_BREAK_INTERPRET)
|
|
793 return NULL;
|
|
794 return e;
|
|
795 }
|
|
796
|
|
797
|
|
798 Expression *econdition = condition->interpret(istate);
|
|
799 if (econdition == EXP_CANT_INTERPRET)
|
|
800 return EXP_CANT_INTERPRET;
|
|
801
|
|
802 Statement *s = NULL;
|
|
803 if (cases)
|
|
804 {
|
|
805 for (size_t i = 0; i < cases->dim; i++)
|
|
806 {
|
|
807 CaseStatement *cs = (CaseStatement *)cases->data[i];
|
|
808 e = Equal(TOKequal, Type::tint32, econdition, cs->exp);
|
|
809 if (e == EXP_CANT_INTERPRET)
|
|
810 return EXP_CANT_INTERPRET;
|
|
811 if (e->isBool(TRUE))
|
|
812 { s = cs;
|
|
813 break;
|
|
814 }
|
|
815 }
|
|
816 }
|
|
817 if (!s)
|
|
818 { if (hasNoDefault)
|
|
819 error("no default or case for %s in switch statement", econdition->toChars());
|
|
820 s = sdefault;
|
|
821 }
|
|
822
|
|
823 assert(s);
|
|
824 istate->start = s;
|
|
825 e = body ? body->interpret(istate) : NULL;
|
|
826 assert(!istate->start);
|
|
827 if (e == EXP_BREAK_INTERPRET)
|
|
828 return NULL;
|
|
829 return e;
|
|
830 }
|
|
831
|
|
832 Expression *CaseStatement::interpret(InterState *istate)
|
|
833 {
|
|
834 #if LOG
|
|
835 printf("CaseStatement::interpret(%s) this = %p\n", exp->toChars(), this);
|
|
836 #endif
|
|
837 if (istate->start == this)
|
|
838 istate->start = NULL;
|
|
839 if (statement)
|
|
840 return statement->interpret(istate);
|
|
841 else
|
|
842 return NULL;
|
|
843 }
|
|
844
|
|
845 Expression *DefaultStatement::interpret(InterState *istate)
|
|
846 {
|
|
847 #if LOG
|
|
848 printf("DefaultStatement::interpret()\n");
|
|
849 #endif
|
|
850 if (istate->start == this)
|
|
851 istate->start = NULL;
|
|
852 if (statement)
|
|
853 return statement->interpret(istate);
|
|
854 else
|
|
855 return NULL;
|
|
856 }
|
|
857
|
|
858 Expression *GotoStatement::interpret(InterState *istate)
|
|
859 {
|
|
860 #if LOG
|
|
861 printf("GotoStatement::interpret()\n");
|
|
862 #endif
|
|
863 START()
|
|
864 assert(label && label->statement);
|
|
865 istate->gotoTarget = label->statement;
|
|
866 return EXP_GOTO_INTERPRET;
|
|
867 }
|
|
868
|
|
869 Expression *GotoCaseStatement::interpret(InterState *istate)
|
|
870 {
|
|
871 #if LOG
|
|
872 printf("GotoCaseStatement::interpret()\n");
|
|
873 #endif
|
|
874 START()
|
|
875 assert(cs);
|
|
876 istate->gotoTarget = cs;
|
|
877 return EXP_GOTO_INTERPRET;
|
|
878 }
|
|
879
|
|
880 Expression *GotoDefaultStatement::interpret(InterState *istate)
|
|
881 {
|
|
882 #if LOG
|
|
883 printf("GotoDefaultStatement::interpret()\n");
|
|
884 #endif
|
|
885 START()
|
|
886 assert(sw && sw->sdefault);
|
|
887 istate->gotoTarget = sw->sdefault;
|
|
888 return EXP_GOTO_INTERPRET;
|
|
889 }
|
|
890
|
|
891 Expression *LabelStatement::interpret(InterState *istate)
|
|
892 {
|
|
893 #if LOG
|
|
894 printf("LabelStatement::interpret()\n");
|
|
895 #endif
|
|
896 if (istate->start == this)
|
|
897 istate->start = NULL;
|
|
898 return statement ? statement->interpret(istate) : NULL;
|
|
899 }
|
|
900
|
|
901 /******************************** Expression ***************************/
|
|
902
|
|
903 Expression *Expression::interpret(InterState *istate)
|
|
904 {
|
|
905 #if LOG
|
|
906 printf("Expression::interpret() %s\n", toChars());
|
|
907 #endif
|
|
908 return EXP_CANT_INTERPRET;
|
|
909 }
|
|
910
|
|
911 Expression *NullExp::interpret(InterState *istate)
|
|
912 {
|
|
913 return this;
|
|
914 }
|
|
915
|
|
916 Expression *IntegerExp::interpret(InterState *istate)
|
|
917 {
|
|
918 #if LOG
|
|
919 printf("IntegerExp::interpret() %s\n", toChars());
|
|
920 #endif
|
|
921 return this;
|
|
922 }
|
|
923
|
|
924 Expression *RealExp::interpret(InterState *istate)
|
|
925 {
|
|
926 #if LOG
|
|
927 printf("RealExp::interpret() %s\n", toChars());
|
|
928 #endif
|
|
929 return this;
|
|
930 }
|
|
931
|
|
932 Expression *ComplexExp::interpret(InterState *istate)
|
|
933 {
|
|
934 return this;
|
|
935 }
|
|
936
|
|
937 Expression *StringExp::interpret(InterState *istate)
|
|
938 {
|
|
939 #if LOG
|
|
940 printf("StringExp::interpret() %s\n", toChars());
|
|
941 #endif
|
|
942 return this;
|
|
943 }
|
|
944
|
|
945 Expression *getVarExp(Loc loc, InterState *istate, Declaration *d)
|
|
946 {
|
|
947 Expression *e = EXP_CANT_INTERPRET;
|
|
948 VarDeclaration *v = d->isVarDeclaration();
|
|
949 SymbolDeclaration *s = d->isSymbolDeclaration();
|
|
950 if (v)
|
|
951 {
|
|
952 if (v->isConst() && v->init)
|
|
953 { e = v->init->toExpression();
|
19
|
954 if (e && !e->type)
|
1
|
955 e->type = v->type;
|
|
956 }
|
|
957 else
|
|
958 { e = v->value;
|
|
959 if (!e)
|
|
960 error(loc, "variable %s is used before initialization", v->toChars());
|
|
961 else if (e != EXP_CANT_INTERPRET)
|
|
962 e = e->interpret(istate);
|
|
963 }
|
|
964 if (!e)
|
|
965 e = EXP_CANT_INTERPRET;
|
|
966 }
|
|
967 else if (s)
|
|
968 {
|
|
969 if (s->dsym->toInitializer() == s->sym)
|
|
970 { Expressions *exps = new Expressions();
|
|
971 e = new StructLiteralExp(0, s->dsym, exps);
|
|
972 e = e->semantic(NULL);
|
|
973 }
|
|
974 }
|
|
975 return e;
|
|
976 }
|
|
977
|
|
978 Expression *VarExp::interpret(InterState *istate)
|
|
979 {
|
|
980 #if LOG
|
|
981 printf("VarExp::interpret() %s\n", toChars());
|
|
982 #endif
|
|
983 return getVarExp(loc, istate, var);
|
|
984 }
|
|
985
|
|
986 Expression *DeclarationExp::interpret(InterState *istate)
|
|
987 {
|
|
988 #if LOG
|
|
989 printf("DeclarationExp::interpret() %s\n", toChars());
|
|
990 #endif
|
19
|
991 Expression *e;
|
1
|
992 VarDeclaration *v = declaration->isVarDeclaration();
|
|
993 if (v)
|
|
994 {
|
|
995 Dsymbol *s = v->toAlias();
|
|
996 if (s == v && !v->isStatic() && v->init)
|
|
997 {
|
|
998 ExpInitializer *ie = v->init->isExpInitializer();
|
|
999 if (ie)
|
|
1000 e = ie->exp->interpret(istate);
|
|
1001 else if (v->init->isVoidInitializer())
|
|
1002 e = NULL;
|
|
1003 }
|
|
1004 else if (s == v && v->isConst() && v->init)
|
|
1005 { e = v->init->toExpression();
|
|
1006 if (!e)
|
|
1007 e = EXP_CANT_INTERPRET;
|
|
1008 else if (!e->type)
|
|
1009 e->type = v->type;
|
|
1010 }
|
|
1011 }
|
19
|
1012 else if (declaration->isAttribDeclaration() ||
|
|
1013 declaration->isTemplateMixin() ||
|
|
1014 declaration->isTupleDeclaration())
|
|
1015 { // These can be made to work, too lazy now
|
|
1016 e = EXP_CANT_INTERPRET;
|
|
1017 }
|
|
1018 else
|
|
1019 { // Others should not contain executable code, so are trivial to evaluate
|
|
1020 e = NULL;
|
|
1021 }
|
|
1022 #if LOG
|
|
1023 printf("-DeclarationExp::interpret(): %p\n", e);
|
|
1024 #endif
|
1
|
1025 return e;
|
|
1026 }
|
|
1027
|
|
1028 Expression *TupleExp::interpret(InterState *istate)
|
|
1029 {
|
|
1030 #if LOG
|
|
1031 printf("TupleExp::interpret() %s\n", toChars());
|
|
1032 #endif
|
|
1033 Expressions *expsx = NULL;
|
|
1034
|
|
1035 for (size_t i = 0; i < exps->dim; i++)
|
|
1036 { Expression *e = (Expression *)exps->data[i];
|
|
1037 Expression *ex;
|
|
1038
|
|
1039 ex = e->interpret(istate);
|
|
1040 if (ex == EXP_CANT_INTERPRET)
|
|
1041 { delete expsx;
|
|
1042 return ex;
|
|
1043 }
|
|
1044
|
|
1045 /* If any changes, do Copy On Write
|
|
1046 */
|
|
1047 if (ex != e)
|
|
1048 {
|
|
1049 if (!expsx)
|
|
1050 { expsx = new Expressions();
|
|
1051 expsx->setDim(exps->dim);
|
|
1052 for (size_t j = 0; j < i; j++)
|
|
1053 {
|
|
1054 expsx->data[j] = exps->data[j];
|
|
1055 }
|
|
1056 }
|
|
1057 expsx->data[i] = (void *)ex;
|
|
1058 }
|
|
1059 }
|
|
1060 if (expsx)
|
|
1061 { TupleExp *te = new TupleExp(loc, expsx);
|
|
1062 expandTuples(te->exps);
|
|
1063 te->type = new TypeTuple(te->exps);
|
|
1064 return te;
|
|
1065 }
|
|
1066 return this;
|
|
1067 }
|
|
1068
|
|
1069 Expression *ArrayLiteralExp::interpret(InterState *istate)
|
|
1070 { Expressions *expsx = NULL;
|
|
1071
|
|
1072 #if LOG
|
|
1073 printf("ArrayLiteralExp::interpret() %s\n", toChars());
|
|
1074 #endif
|
|
1075 if (elements)
|
|
1076 {
|
|
1077 for (size_t i = 0; i < elements->dim; i++)
|
|
1078 { Expression *e = (Expression *)elements->data[i];
|
|
1079 Expression *ex;
|
|
1080
|
|
1081 ex = e->interpret(istate);
|
|
1082 if (ex == EXP_CANT_INTERPRET)
|
|
1083 { delete expsx;
|
|
1084 return EXP_CANT_INTERPRET;
|
|
1085 }
|
|
1086
|
|
1087 /* If any changes, do Copy On Write
|
|
1088 */
|
|
1089 if (ex != e)
|
|
1090 {
|
|
1091 if (!expsx)
|
|
1092 { expsx = new Expressions();
|
|
1093 expsx->setDim(elements->dim);
|
19
|
1094 for (size_t j = 0; j < elements->dim; j++)
|
1
|
1095 {
|
|
1096 expsx->data[j] = elements->data[j];
|
|
1097 }
|
|
1098 }
|
|
1099 expsx->data[i] = (void *)ex;
|
|
1100 }
|
|
1101 }
|
|
1102 }
|
|
1103 if (elements && expsx)
|
|
1104 {
|
|
1105 expandTuples(expsx);
|
|
1106 if (expsx->dim != elements->dim)
|
|
1107 { delete expsx;
|
|
1108 return EXP_CANT_INTERPRET;
|
|
1109 }
|
|
1110 ArrayLiteralExp *ae = new ArrayLiteralExp(loc, expsx);
|
|
1111 ae->type = type;
|
|
1112 return ae;
|
|
1113 }
|
|
1114 return this;
|
|
1115 }
|
|
1116
|
|
1117 Expression *AssocArrayLiteralExp::interpret(InterState *istate)
|
|
1118 { Expressions *keysx = keys;
|
|
1119 Expressions *valuesx = values;
|
|
1120
|
|
1121 #if LOG
|
|
1122 printf("AssocArrayLiteralExp::interpret() %s\n", toChars());
|
|
1123 #endif
|
|
1124 for (size_t i = 0; i < keys->dim; i++)
|
|
1125 { Expression *ekey = (Expression *)keys->data[i];
|
|
1126 Expression *evalue = (Expression *)values->data[i];
|
|
1127 Expression *ex;
|
|
1128
|
|
1129 ex = ekey->interpret(istate);
|
|
1130 if (ex == EXP_CANT_INTERPRET)
|
|
1131 goto Lerr;
|
|
1132
|
|
1133 /* If any changes, do Copy On Write
|
|
1134 */
|
|
1135 if (ex != ekey)
|
|
1136 {
|
|
1137 if (keysx == keys)
|
|
1138 keysx = (Expressions *)keys->copy();
|
|
1139 keysx->data[i] = (void *)ex;
|
|
1140 }
|
|
1141
|
|
1142 ex = evalue->interpret(istate);
|
|
1143 if (ex == EXP_CANT_INTERPRET)
|
|
1144 goto Lerr;
|
|
1145
|
|
1146 /* If any changes, do Copy On Write
|
|
1147 */
|
|
1148 if (ex != evalue)
|
|
1149 {
|
|
1150 if (valuesx == values)
|
|
1151 valuesx = (Expressions *)values->copy();
|
|
1152 valuesx->data[i] = (void *)ex;
|
|
1153 }
|
|
1154 }
|
|
1155 if (keysx != keys)
|
|
1156 expandTuples(keysx);
|
|
1157 if (valuesx != values)
|
|
1158 expandTuples(valuesx);
|
|
1159 if (keysx->dim != valuesx->dim)
|
|
1160 goto Lerr;
|
|
1161
|
|
1162 /* Remove duplicate keys
|
|
1163 */
|
|
1164 for (size_t i = 1; i < keysx->dim; i++)
|
|
1165 { Expression *ekey = (Expression *)keysx->data[i - 1];
|
|
1166
|
|
1167 for (size_t j = i; j < keysx->dim; j++)
|
|
1168 { Expression *ekey2 = (Expression *)keysx->data[j];
|
|
1169 Expression *ex = Equal(TOKequal, Type::tbool, ekey, ekey2);
|
|
1170 if (ex == EXP_CANT_INTERPRET)
|
|
1171 goto Lerr;
|
|
1172 if (ex->isBool(TRUE)) // if a match
|
|
1173 {
|
|
1174 // Remove ekey
|
|
1175 if (keysx == keys)
|
|
1176 keysx = (Expressions *)keys->copy();
|
|
1177 if (valuesx == values)
|
|
1178 valuesx = (Expressions *)values->copy();
|
|
1179 keysx->remove(i - 1);
|
|
1180 valuesx->remove(i - 1);
|
|
1181 i -= 1; // redo the i'th iteration
|
|
1182 break;
|
|
1183 }
|
|
1184 }
|
|
1185 }
|
|
1186
|
|
1187 if (keysx != keys || valuesx != values)
|
|
1188 {
|
|
1189 AssocArrayLiteralExp *ae;
|
|
1190 ae = new AssocArrayLiteralExp(loc, keysx, valuesx);
|
|
1191 ae->type = type;
|
|
1192 return ae;
|
|
1193 }
|
|
1194 return this;
|
|
1195
|
|
1196 Lerr:
|
|
1197 if (keysx != keys)
|
|
1198 delete keysx;
|
|
1199 if (valuesx != values)
|
|
1200 delete values;
|
|
1201 return EXP_CANT_INTERPRET;
|
|
1202 }
|
|
1203
|
|
1204 Expression *StructLiteralExp::interpret(InterState *istate)
|
|
1205 { Expressions *expsx = NULL;
|
|
1206
|
|
1207 #if LOG
|
|
1208 printf("StructLiteralExp::interpret() %s\n", toChars());
|
|
1209 #endif
|
|
1210 /* We don't know how to deal with overlapping fields
|
|
1211 */
|
|
1212 if (sd->hasUnions)
|
|
1213 return EXP_CANT_INTERPRET;
|
|
1214
|
|
1215 if (elements)
|
|
1216 {
|
|
1217 for (size_t i = 0; i < elements->dim; i++)
|
|
1218 { Expression *e = (Expression *)elements->data[i];
|
|
1219 if (!e)
|
|
1220 continue;
|
|
1221
|
|
1222 Expression *ex = e->interpret(istate);
|
|
1223 if (ex == EXP_CANT_INTERPRET)
|
|
1224 { delete expsx;
|
|
1225 return EXP_CANT_INTERPRET;
|
|
1226 }
|
|
1227
|
|
1228 /* If any changes, do Copy On Write
|
|
1229 */
|
|
1230 if (ex != e)
|
|
1231 {
|
|
1232 if (!expsx)
|
|
1233 { expsx = new Expressions();
|
|
1234 expsx->setDim(elements->dim);
|
|
1235 for (size_t j = 0; j < elements->dim; j++)
|
|
1236 {
|
|
1237 expsx->data[j] = elements->data[j];
|
|
1238 }
|
|
1239 }
|
|
1240 expsx->data[i] = (void *)ex;
|
|
1241 }
|
|
1242 }
|
|
1243 }
|
|
1244 if (elements && expsx)
|
|
1245 {
|
|
1246 expandTuples(expsx);
|
|
1247 if (expsx->dim != elements->dim)
|
|
1248 { delete expsx;
|
|
1249 return EXP_CANT_INTERPRET;
|
|
1250 }
|
|
1251 StructLiteralExp *se = new StructLiteralExp(loc, sd, expsx);
|
|
1252 se->type = type;
|
|
1253 return se;
|
|
1254 }
|
|
1255 return this;
|
|
1256 }
|
|
1257
|
|
1258 Expression *UnaExp::interpretCommon(InterState *istate, Expression *(*fp)(Type *, Expression *))
|
|
1259 { Expression *e;
|
|
1260 Expression *e1;
|
|
1261
|
|
1262 #if LOG
|
|
1263 printf("UnaExp::interpretCommon() %s\n", toChars());
|
|
1264 #endif
|
|
1265 e1 = this->e1->interpret(istate);
|
|
1266 if (e1 == EXP_CANT_INTERPRET)
|
|
1267 goto Lcant;
|
|
1268 if (e1->isConst() != 1)
|
|
1269 goto Lcant;
|
|
1270
|
|
1271 e = (*fp)(type, e1);
|
|
1272 return e;
|
|
1273
|
|
1274 Lcant:
|
|
1275 return EXP_CANT_INTERPRET;
|
|
1276 }
|
|
1277
|
|
1278 #define UNA_INTERPRET(op) \
|
|
1279 Expression *op##Exp::interpret(InterState *istate) \
|
|
1280 { \
|
|
1281 return interpretCommon(istate, &op); \
|
|
1282 }
|
|
1283
|
|
1284 UNA_INTERPRET(Neg)
|
|
1285 UNA_INTERPRET(Com)
|
|
1286 UNA_INTERPRET(Not)
|
|
1287 UNA_INTERPRET(Bool)
|
|
1288
|
|
1289
|
|
1290 typedef Expression *(*fp_t)(Type *, Expression *, Expression *);
|
|
1291
|
|
1292 Expression *BinExp::interpretCommon(InterState *istate, fp_t fp)
|
|
1293 { Expression *e;
|
|
1294 Expression *e1;
|
|
1295 Expression *e2;
|
|
1296
|
|
1297 #if LOG
|
|
1298 printf("BinExp::interpretCommon() %s\n", toChars());
|
|
1299 #endif
|
|
1300 e1 = this->e1->interpret(istate);
|
|
1301 if (e1 == EXP_CANT_INTERPRET)
|
|
1302 goto Lcant;
|
|
1303 if (e1->isConst() != 1)
|
|
1304 goto Lcant;
|
|
1305
|
|
1306 e2 = this->e2->interpret(istate);
|
|
1307 if (e2 == EXP_CANT_INTERPRET)
|
|
1308 goto Lcant;
|
|
1309 if (e2->isConst() != 1)
|
|
1310 goto Lcant;
|
|
1311
|
|
1312 e = (*fp)(type, e1, e2);
|
|
1313 return e;
|
|
1314
|
|
1315 Lcant:
|
|
1316 return EXP_CANT_INTERPRET;
|
|
1317 }
|
|
1318
|
|
1319 #define BIN_INTERPRET(op) \
|
|
1320 Expression *op##Exp::interpret(InterState *istate) \
|
|
1321 { \
|
|
1322 return interpretCommon(istate, &op); \
|
|
1323 }
|
|
1324
|
|
1325 BIN_INTERPRET(Add)
|
|
1326 BIN_INTERPRET(Min)
|
|
1327 BIN_INTERPRET(Mul)
|
|
1328 BIN_INTERPRET(Div)
|
|
1329 BIN_INTERPRET(Mod)
|
|
1330 BIN_INTERPRET(Shl)
|
|
1331 BIN_INTERPRET(Shr)
|
|
1332 BIN_INTERPRET(Ushr)
|
|
1333 BIN_INTERPRET(And)
|
|
1334 BIN_INTERPRET(Or)
|
|
1335 BIN_INTERPRET(Xor)
|
|
1336
|
|
1337
|
|
1338 typedef Expression *(*fp2_t)(enum TOK, Type *, Expression *, Expression *);
|
|
1339
|
|
1340 Expression *BinExp::interpretCommon2(InterState *istate, fp2_t fp)
|
|
1341 { Expression *e;
|
|
1342 Expression *e1;
|
|
1343 Expression *e2;
|
|
1344
|
|
1345 #if LOG
|
|
1346 printf("BinExp::interpretCommon2() %s\n", toChars());
|
|
1347 #endif
|
|
1348 e1 = this->e1->interpret(istate);
|
|
1349 if (e1 == EXP_CANT_INTERPRET)
|
|
1350 goto Lcant;
|
|
1351 if (e1->isConst() != 1 &&
|
19
|
1352 e1->op != TOKnull &&
|
1
|
1353 e1->op != TOKstring &&
|
|
1354 e1->op != TOKarrayliteral &&
|
|
1355 e1->op != TOKstructliteral)
|
|
1356 goto Lcant;
|
|
1357
|
|
1358 e2 = this->e2->interpret(istate);
|
|
1359 if (e2 == EXP_CANT_INTERPRET)
|
|
1360 goto Lcant;
|
|
1361 if (e2->isConst() != 1 &&
|
19
|
1362 e2->op != TOKnull &&
|
1
|
1363 e2->op != TOKstring &&
|
|
1364 e2->op != TOKarrayliteral &&
|
|
1365 e2->op != TOKstructliteral)
|
|
1366 goto Lcant;
|
|
1367
|
|
1368 e = (*fp)(op, type, e1, e2);
|
|
1369 return e;
|
|
1370
|
|
1371 Lcant:
|
|
1372 return EXP_CANT_INTERPRET;
|
|
1373 }
|
|
1374
|
|
1375 #define BIN_INTERPRET2(op) \
|
|
1376 Expression *op##Exp::interpret(InterState *istate) \
|
|
1377 { \
|
|
1378 return interpretCommon2(istate, &op); \
|
|
1379 }
|
|
1380
|
|
1381 BIN_INTERPRET2(Equal)
|
|
1382 BIN_INTERPRET2(Identity)
|
|
1383 BIN_INTERPRET2(Cmp)
|
|
1384
|
|
1385 Expression *BinExp::interpretAssignCommon(InterState *istate, fp_t fp, int post)
|
|
1386 {
|
|
1387 #if LOG
|
|
1388 printf("BinExp::interpretAssignCommon() %s\n", toChars());
|
|
1389 #endif
|
|
1390 Expression *e = EXP_CANT_INTERPRET;
|
|
1391 Expression *e1 = this->e1;
|
|
1392
|
|
1393 if (fp)
|
|
1394 {
|
|
1395 if (e1->op == TOKcast)
|
|
1396 { CastExp *ce = (CastExp *)e1;
|
|
1397 e1 = ce->e1;
|
|
1398 }
|
|
1399 }
|
|
1400 if (e1 == EXP_CANT_INTERPRET)
|
|
1401 return e1;
|
|
1402 Expression *e2 = this->e2->interpret(istate);
|
|
1403 if (e2 == EXP_CANT_INTERPRET)
|
|
1404 return e2;
|
|
1405
|
|
1406 /* Assignment to variable of the form:
|
|
1407 * v = e2
|
|
1408 */
|
|
1409 if (e1->op == TOKvar)
|
|
1410 {
|
|
1411 VarExp *ve = (VarExp *)e1;
|
|
1412 VarDeclaration *v = ve->var->isVarDeclaration();
|
|
1413 if (v && !v->isDataseg())
|
|
1414 {
|
|
1415 /* Chase down rebinding of out and ref
|
|
1416 */
|
|
1417 if (v->value && v->value->op == TOKvar)
|
|
1418 {
|
|
1419 ve = (VarExp *)v->value;
|
|
1420 v = ve->var->isVarDeclaration();
|
|
1421 assert(v);
|
|
1422 }
|
|
1423
|
|
1424 Expression *ev = v->value;
|
|
1425 if (fp && !ev)
|
|
1426 { error("variable %s is used before initialization", v->toChars());
|
|
1427 return e;
|
|
1428 }
|
|
1429 if (fp)
|
|
1430 e2 = (*fp)(v->type, ev, e2);
|
|
1431 else
|
19
|
1432 { /* Look for special case of struct being initialized with 0.
|
|
1433 */
|
|
1434 if (v->type->toBasetype()->ty == Tstruct && e2->op == TOKint64)
|
|
1435 {
|
|
1436 e2 = v->type->defaultInit();
|
|
1437 }
|
1
|
1438 e2 = Cast(v->type, v->type, e2);
|
19
|
1439 }
|
1
|
1440 if (e2 != EXP_CANT_INTERPRET)
|
|
1441 {
|
|
1442 if (!v->isParameter())
|
|
1443 {
|
|
1444 for (size_t i = 0; 1; i++)
|
|
1445 {
|
|
1446 if (i == istate->vars.dim)
|
|
1447 { istate->vars.push(v);
|
|
1448 break;
|
|
1449 }
|
|
1450 if (v == (VarDeclaration *)istate->vars.data[i])
|
|
1451 break;
|
|
1452 }
|
|
1453 }
|
|
1454 v->value = e2;
|
|
1455 e = Cast(type, type, post ? ev : e2);
|
|
1456 }
|
|
1457 }
|
|
1458 }
|
|
1459 /* Assignment to struct member of the form:
|
|
1460 * *(symoffexp) = e2
|
|
1461 */
|
|
1462 else if (e1->op == TOKstar && ((PtrExp *)e1)->e1->op == TOKsymoff)
|
|
1463 { SymOffExp *soe = (SymOffExp *)((PtrExp *)e1)->e1;
|
|
1464 VarDeclaration *v = soe->var->isVarDeclaration();
|
|
1465
|
|
1466 if (v->isDataseg())
|
|
1467 return EXP_CANT_INTERPRET;
|
|
1468 if (fp && !v->value)
|
|
1469 { error("variable %s is used before initialization", v->toChars());
|
|
1470 return e;
|
|
1471 }
|
19
|
1472 Expression *vie = v->value;
|
|
1473 if (vie->op == TOKvar)
|
|
1474 {
|
|
1475 Declaration *d = ((VarExp *)vie)->var;
|
|
1476 vie = getVarExp(e1->loc, istate, d);
|
|
1477 }
|
|
1478 if (vie->op != TOKstructliteral)
|
1
|
1479 return EXP_CANT_INTERPRET;
|
19
|
1480 StructLiteralExp *se = (StructLiteralExp *)vie;
|
1
|
1481 int fieldi = se->getFieldIndex(type, soe->offset);
|
|
1482 if (fieldi == -1)
|
|
1483 return EXP_CANT_INTERPRET;
|
|
1484 Expression *ev = se->getField(type, soe->offset);
|
|
1485 if (fp)
|
|
1486 e2 = (*fp)(type, ev, e2);
|
|
1487 else
|
|
1488 e2 = Cast(type, type, e2);
|
|
1489 if (e2 == EXP_CANT_INTERPRET)
|
|
1490 return e2;
|
|
1491
|
|
1492 if (!v->isParameter())
|
|
1493 {
|
|
1494 for (size_t i = 0; 1; i++)
|
|
1495 {
|
|
1496 if (i == istate->vars.dim)
|
|
1497 { istate->vars.push(v);
|
|
1498 break;
|
|
1499 }
|
|
1500 if (v == (VarDeclaration *)istate->vars.data[i])
|
|
1501 break;
|
|
1502 }
|
|
1503 }
|
|
1504
|
|
1505 /* Create new struct literal reflecting updated fieldi
|
|
1506 */
|
|
1507 Expressions *expsx = new Expressions();
|
|
1508 expsx->setDim(se->elements->dim);
|
|
1509 for (size_t j = 0; j < expsx->dim; j++)
|
|
1510 {
|
|
1511 if (j == fieldi)
|
|
1512 expsx->data[j] = (void *)e2;
|
|
1513 else
|
|
1514 expsx->data[j] = se->elements->data[j];
|
|
1515 }
|
|
1516 v->value = new StructLiteralExp(se->loc, se->sd, expsx);
|
|
1517 v->value->type = se->type;
|
|
1518
|
|
1519 e = Cast(type, type, post ? ev : e2);
|
|
1520 }
|
|
1521 /* Assignment to array element of the form:
|
|
1522 * a[i] = e2
|
|
1523 */
|
|
1524 else if (e1->op == TOKindex && ((IndexExp *)e1)->e1->op == TOKvar)
|
|
1525 { IndexExp *ie = (IndexExp *)e1;
|
|
1526 VarExp *ve = (VarExp *)ie->e1;
|
|
1527 VarDeclaration *v = ve->var->isVarDeclaration();
|
|
1528
|
|
1529 if (!v || v->isDataseg())
|
|
1530 return EXP_CANT_INTERPRET;
|
|
1531 if (!v->value)
|
|
1532 {
|
|
1533 if (fp)
|
|
1534 { error("variable %s is used before initialization", v->toChars());
|
|
1535 return e;
|
|
1536 }
|
|
1537
|
|
1538 Type *t = v->type->toBasetype();
|
|
1539 if (t->ty == Tsarray)
|
|
1540 {
|
|
1541 /* This array was void initialized. Create a
|
|
1542 * default initializer for it.
|
|
1543 * What we should do is fill the array literal with
|
|
1544 * NULL data, so use-before-initialized can be detected.
|
|
1545 * But we're too lazy at the moment to do it, as that
|
|
1546 * involves redoing Index() and whoever calls it.
|
|
1547 */
|
|
1548 Expression *ev = v->type->defaultInit();
|
|
1549 size_t dim = ((TypeSArray *)t)->dim->toInteger();
|
|
1550 Expressions *elements = new Expressions();
|
|
1551 elements->setDim(dim);
|
|
1552 for (size_t i = 0; i < dim; i++)
|
|
1553 elements->data[i] = (void *)ev;
|
|
1554 ArrayLiteralExp *ae = new ArrayLiteralExp(0, elements);
|
|
1555 ae->type = v->type;
|
|
1556 v->value = ae;
|
|
1557 }
|
|
1558 else
|
|
1559 return EXP_CANT_INTERPRET;
|
|
1560 }
|
|
1561
|
|
1562 ArrayLiteralExp *ae = NULL;
|
|
1563 AssocArrayLiteralExp *aae = NULL;
|
|
1564 StringExp *se = NULL;
|
|
1565 if (v->value->op == TOKarrayliteral)
|
|
1566 ae = (ArrayLiteralExp *)v->value;
|
|
1567 else if (v->value->op == TOKassocarrayliteral)
|
|
1568 aae = (AssocArrayLiteralExp *)v->value;
|
|
1569 else if (v->value->op == TOKstring)
|
|
1570 se = (StringExp *)v->value;
|
|
1571 else
|
|
1572 return EXP_CANT_INTERPRET;
|
|
1573
|
|
1574 Expression *index = ie->e2->interpret(istate);
|
|
1575 if (index == EXP_CANT_INTERPRET)
|
|
1576 return EXP_CANT_INTERPRET;
|
|
1577 Expression *ev;
|
|
1578 if (fp || ae || se) // not for aae, because key might not be there
|
|
1579 {
|
|
1580 ev = Index(type, v->value, index);
|
|
1581 if (ev == EXP_CANT_INTERPRET)
|
|
1582 return EXP_CANT_INTERPRET;
|
|
1583 }
|
|
1584
|
|
1585 if (fp)
|
|
1586 e2 = (*fp)(type, ev, e2);
|
|
1587 else
|
|
1588 e2 = Cast(type, type, e2);
|
|
1589 if (e2 == EXP_CANT_INTERPRET)
|
|
1590 return e2;
|
|
1591
|
|
1592 if (!v->isParameter())
|
|
1593 {
|
|
1594 for (size_t i = 0; 1; i++)
|
|
1595 {
|
|
1596 if (i == istate->vars.dim)
|
|
1597 { istate->vars.push(v);
|
|
1598 break;
|
|
1599 }
|
|
1600 if (v == (VarDeclaration *)istate->vars.data[i])
|
|
1601 break;
|
|
1602 }
|
|
1603 }
|
|
1604
|
|
1605 if (ae)
|
|
1606 {
|
|
1607 /* Create new array literal reflecting updated elem
|
|
1608 */
|
|
1609 int elemi = index->toInteger();
|
|
1610 Expressions *expsx = new Expressions();
|
|
1611 expsx->setDim(ae->elements->dim);
|
|
1612 for (size_t j = 0; j < expsx->dim; j++)
|
|
1613 {
|
|
1614 if (j == elemi)
|
|
1615 expsx->data[j] = (void *)e2;
|
|
1616 else
|
|
1617 expsx->data[j] = ae->elements->data[j];
|
|
1618 }
|
|
1619 v->value = new ArrayLiteralExp(ae->loc, expsx);
|
|
1620 v->value->type = ae->type;
|
|
1621 }
|
|
1622 else if (aae)
|
|
1623 {
|
|
1624 /* Create new associative array literal reflecting updated key/value
|
|
1625 */
|
|
1626 Expressions *keysx = aae->keys;
|
|
1627 Expressions *valuesx = new Expressions();
|
|
1628 valuesx->setDim(aae->values->dim);
|
|
1629 int updated = 0;
|
|
1630 for (size_t j = valuesx->dim; j; )
|
|
1631 { j--;
|
|
1632 Expression *ekey = (Expression *)aae->keys->data[j];
|
|
1633 Expression *ex = Equal(TOKequal, Type::tbool, ekey, index);
|
|
1634 if (ex == EXP_CANT_INTERPRET)
|
|
1635 return EXP_CANT_INTERPRET;
|
|
1636 if (ex->isBool(TRUE))
|
|
1637 { valuesx->data[j] = (void *)e2;
|
|
1638 updated = 1;
|
|
1639 }
|
|
1640 else
|
|
1641 valuesx->data[j] = aae->values->data[j];
|
|
1642 }
|
|
1643 if (!updated)
|
|
1644 { // Append index/e2 to keysx[]/valuesx[]
|
|
1645 valuesx->push(e2);
|
|
1646 keysx = (Expressions *)keysx->copy();
|
|
1647 keysx->push(index);
|
|
1648 }
|
|
1649 v->value = new AssocArrayLiteralExp(aae->loc, keysx, valuesx);
|
|
1650 v->value->type = aae->type;
|
|
1651 }
|
|
1652 else if (se)
|
|
1653 {
|
|
1654 /* Create new string literal reflecting updated elem
|
|
1655 */
|
|
1656 int elemi = index->toInteger();
|
|
1657 unsigned char *s;
|
|
1658 s = (unsigned char *)mem.calloc(se->len + 1, se->sz);
|
|
1659 memcpy(s, se->string, se->len * se->sz);
|
|
1660 unsigned value = e2->toInteger();
|
|
1661 switch (se->sz)
|
|
1662 {
|
|
1663 case 1: s[elemi] = value; break;
|
|
1664 case 2: ((unsigned short *)s)[elemi] = value; break;
|
|
1665 case 4: ((unsigned *)s)[elemi] = value; break;
|
|
1666 default:
|
|
1667 assert(0);
|
|
1668 break;
|
|
1669 }
|
|
1670 StringExp *se2 = new StringExp(se->loc, s, se->len);
|
|
1671 se2->committed = se->committed;
|
|
1672 se2->postfix = se->postfix;
|
|
1673 se2->type = se->type;
|
|
1674 v->value = se2;
|
|
1675 }
|
|
1676 else
|
|
1677 assert(0);
|
|
1678
|
|
1679 e = Cast(type, type, post ? ev : e2);
|
|
1680 }
|
|
1681 else
|
|
1682 {
|
|
1683 #ifdef DEBUG
|
|
1684 dump(0);
|
|
1685 #endif
|
|
1686 }
|
|
1687 return e;
|
|
1688 }
|
|
1689
|
|
1690 Expression *AssignExp::interpret(InterState *istate)
|
|
1691 {
|
|
1692 return interpretAssignCommon(istate, NULL);
|
|
1693 }
|
|
1694
|
|
1695 #define BIN_ASSIGN_INTERPRET(op) \
|
|
1696 Expression *op##AssignExp::interpret(InterState *istate) \
|
|
1697 { \
|
|
1698 return interpretAssignCommon(istate, &op); \
|
|
1699 }
|
|
1700
|
|
1701 BIN_ASSIGN_INTERPRET(Add)
|
|
1702 BIN_ASSIGN_INTERPRET(Min)
|
|
1703 BIN_ASSIGN_INTERPRET(Cat)
|
|
1704 BIN_ASSIGN_INTERPRET(Mul)
|
|
1705 BIN_ASSIGN_INTERPRET(Div)
|
|
1706 BIN_ASSIGN_INTERPRET(Mod)
|
|
1707 BIN_ASSIGN_INTERPRET(Shl)
|
|
1708 BIN_ASSIGN_INTERPRET(Shr)
|
|
1709 BIN_ASSIGN_INTERPRET(Ushr)
|
|
1710 BIN_ASSIGN_INTERPRET(And)
|
|
1711 BIN_ASSIGN_INTERPRET(Or)
|
|
1712 BIN_ASSIGN_INTERPRET(Xor)
|
|
1713
|
|
1714 Expression *PostExp::interpret(InterState *istate)
|
|
1715 {
|
|
1716 #if LOG
|
|
1717 printf("PostExp::interpret() %s\n", toChars());
|
|
1718 #endif
|
|
1719 Expression *e;
|
|
1720 if (op == TOKplusplus)
|
|
1721 e = interpretAssignCommon(istate, &Add, 1);
|
|
1722 else
|
|
1723 e = interpretAssignCommon(istate, &Min, 1);
|
|
1724 #if LOG
|
|
1725 if (e == EXP_CANT_INTERPRET)
|
|
1726 printf("PostExp::interpret() CANT\n");
|
|
1727 #endif
|
|
1728 return e;
|
|
1729 }
|
|
1730
|
|
1731 Expression *AndAndExp::interpret(InterState *istate)
|
|
1732 {
|
|
1733 #if LOG
|
|
1734 printf("AndAndExp::interpret() %s\n", toChars());
|
|
1735 #endif
|
|
1736 Expression *e = e1->interpret(istate);
|
|
1737 if (e != EXP_CANT_INTERPRET)
|
|
1738 {
|
|
1739 if (e->isBool(FALSE))
|
|
1740 e = new IntegerExp(e1->loc, 0, type);
|
|
1741 else if (e->isBool(TRUE))
|
|
1742 {
|
|
1743 e = e2->interpret(istate);
|
|
1744 if (e != EXP_CANT_INTERPRET)
|
|
1745 {
|
|
1746 if (e->isBool(FALSE))
|
|
1747 e = new IntegerExp(e1->loc, 0, type);
|
|
1748 else if (e->isBool(TRUE))
|
|
1749 e = new IntegerExp(e1->loc, 1, type);
|
|
1750 else
|
|
1751 e = EXP_CANT_INTERPRET;
|
|
1752 }
|
|
1753 }
|
|
1754 else
|
|
1755 e = EXP_CANT_INTERPRET;
|
|
1756 }
|
|
1757 return e;
|
|
1758 }
|
|
1759
|
|
1760 Expression *OrOrExp::interpret(InterState *istate)
|
|
1761 {
|
|
1762 #if LOG
|
|
1763 printf("OrOrExp::interpret() %s\n", toChars());
|
|
1764 #endif
|
|
1765 Expression *e = e1->interpret(istate);
|
|
1766 if (e != EXP_CANT_INTERPRET)
|
|
1767 {
|
|
1768 if (e->isBool(TRUE))
|
|
1769 e = new IntegerExp(e1->loc, 1, type);
|
|
1770 else if (e->isBool(FALSE))
|
|
1771 {
|
|
1772 e = e2->interpret(istate);
|
|
1773 if (e != EXP_CANT_INTERPRET)
|
|
1774 {
|
|
1775 if (e->isBool(FALSE))
|
|
1776 e = new IntegerExp(e1->loc, 0, type);
|
|
1777 else if (e->isBool(TRUE))
|
|
1778 e = new IntegerExp(e1->loc, 1, type);
|
|
1779 else
|
|
1780 e = EXP_CANT_INTERPRET;
|
|
1781 }
|
|
1782 }
|
|
1783 else
|
|
1784 e = EXP_CANT_INTERPRET;
|
|
1785 }
|
|
1786 return e;
|
|
1787 }
|
|
1788
|
|
1789
|
|
1790 Expression *CallExp::interpret(InterState *istate)
|
|
1791 { Expression *e = EXP_CANT_INTERPRET;
|
|
1792
|
|
1793 #if LOG
|
|
1794 printf("CallExp::interpret() %s\n", toChars());
|
|
1795 #endif
|
|
1796 if (e1->op == TOKvar)
|
|
1797 {
|
|
1798 FuncDeclaration *fd = ((VarExp *)e1)->var->isFuncDeclaration();
|
|
1799 if (fd)
|
|
1800 { // Inline .dup
|
|
1801 if (fd->ident == Id::adDup && arguments && arguments->dim == 2)
|
|
1802 {
|
|
1803 e = (Expression *)arguments->data[1];
|
|
1804 e = e->interpret(istate);
|
|
1805 if (e != EXP_CANT_INTERPRET)
|
|
1806 {
|
|
1807 e = expType(type, e);
|
|
1808 }
|
|
1809 }
|
|
1810 else
|
|
1811 {
|
|
1812 Expression *eresult = fd->interpret(istate, arguments);
|
|
1813 if (eresult)
|
|
1814 e = eresult;
|
|
1815 else if (fd->type->toBasetype()->nextOf()->ty == Tvoid)
|
|
1816 e = EXP_VOID_INTERPRET;
|
|
1817 else
|
|
1818 error("cannot evaluate %s at compile time", toChars());
|
|
1819 }
|
|
1820 }
|
|
1821 }
|
|
1822 return e;
|
|
1823 }
|
|
1824
|
|
1825 Expression *CommaExp::interpret(InterState *istate)
|
|
1826 {
|
|
1827 #if LOG
|
|
1828 printf("CommaExp::interpret() %s\n", toChars());
|
|
1829 #endif
|
|
1830 Expression *e = e1->interpret(istate);
|
|
1831 if (e != EXP_CANT_INTERPRET)
|
|
1832 e = e2->interpret(istate);
|
|
1833 return e;
|
|
1834 }
|
|
1835
|
|
1836 Expression *CondExp::interpret(InterState *istate)
|
|
1837 {
|
|
1838 #if LOG
|
|
1839 printf("CondExp::interpret() %s\n", toChars());
|
|
1840 #endif
|
|
1841 Expression *e = econd->interpret(istate);
|
|
1842 if (e != EXP_CANT_INTERPRET)
|
|
1843 {
|
|
1844 if (e->isBool(TRUE))
|
|
1845 e = e1->interpret(istate);
|
|
1846 else if (e->isBool(FALSE))
|
|
1847 e = e2->interpret(istate);
|
|
1848 else
|
|
1849 e = EXP_CANT_INTERPRET;
|
|
1850 }
|
|
1851 return e;
|
|
1852 }
|
|
1853
|
|
1854 Expression *ArrayLengthExp::interpret(InterState *istate)
|
|
1855 { Expression *e;
|
|
1856 Expression *e1;
|
|
1857
|
|
1858 #if LOG
|
|
1859 printf("ArrayLengthExp::interpret() %s\n", toChars());
|
|
1860 #endif
|
|
1861 e1 = this->e1->interpret(istate);
|
|
1862 if (e1 == EXP_CANT_INTERPRET)
|
|
1863 goto Lcant;
|
|
1864 if (e1->op == TOKstring || e1->op == TOKarrayliteral || e1->op == TOKassocarrayliteral)
|
|
1865 {
|
|
1866 e = ArrayLength(type, e1);
|
|
1867 }
|
19
|
1868 else if (e1->op == TOKnull)
|
|
1869 {
|
|
1870 e = new IntegerExp(loc, 0, type);
|
|
1871 }
|
1
|
1872 else
|
|
1873 goto Lcant;
|
|
1874 return e;
|
|
1875
|
|
1876 Lcant:
|
|
1877 return EXP_CANT_INTERPRET;
|
|
1878 }
|
|
1879
|
|
1880 Expression *IndexExp::interpret(InterState *istate)
|
|
1881 { Expression *e;
|
|
1882 Expression *e1;
|
|
1883 Expression *e2;
|
|
1884
|
|
1885 #if LOG
|
|
1886 printf("IndexExp::interpret() %s\n", toChars());
|
|
1887 #endif
|
|
1888 e1 = this->e1->interpret(istate);
|
|
1889 if (e1 == EXP_CANT_INTERPRET)
|
|
1890 goto Lcant;
|
|
1891
|
|
1892 if (e1->op == TOKstring || e1->op == TOKarrayliteral)
|
|
1893 {
|
|
1894 /* Set the $ variable
|
|
1895 */
|
|
1896 e = ArrayLength(Type::tsize_t, e1);
|
|
1897 if (e == EXP_CANT_INTERPRET)
|
|
1898 goto Lcant;
|
|
1899 if (lengthVar)
|
|
1900 lengthVar->value = e;
|
|
1901 }
|
|
1902
|
|
1903 e2 = this->e2->interpret(istate);
|
|
1904 if (e2 == EXP_CANT_INTERPRET)
|
|
1905 goto Lcant;
|
|
1906 return Index(type, e1, e2);
|
|
1907
|
|
1908 Lcant:
|
|
1909 return EXP_CANT_INTERPRET;
|
|
1910 }
|
|
1911
|
|
1912
|
|
1913 Expression *SliceExp::interpret(InterState *istate)
|
|
1914 { Expression *e;
|
|
1915 Expression *e1;
|
|
1916 Expression *lwr;
|
|
1917 Expression *upr;
|
|
1918
|
|
1919 #if LOG
|
|
1920 printf("SliceExp::interpret() %s\n", toChars());
|
|
1921 #endif
|
|
1922 e1 = this->e1->interpret(istate);
|
|
1923 if (e1 == EXP_CANT_INTERPRET)
|
|
1924 goto Lcant;
|
|
1925 if (!this->lwr)
|
|
1926 {
|
|
1927 e = e1->castTo(NULL, type);
|
|
1928 return e->interpret(istate);
|
|
1929 }
|
|
1930
|
|
1931 /* Set the $ variable
|
|
1932 */
|
|
1933 e = ArrayLength(Type::tsize_t, e1);
|
|
1934 if (e == EXP_CANT_INTERPRET)
|
|
1935 goto Lcant;
|
|
1936 if (lengthVar)
|
|
1937 lengthVar->value = e;
|
|
1938
|
|
1939 /* Evaluate lower and upper bounds of slice
|
|
1940 */
|
|
1941 lwr = this->lwr->interpret(istate);
|
|
1942 if (lwr == EXP_CANT_INTERPRET)
|
|
1943 goto Lcant;
|
|
1944 upr = this->upr->interpret(istate);
|
|
1945 if (upr == EXP_CANT_INTERPRET)
|
|
1946 goto Lcant;
|
|
1947
|
|
1948 return Slice(type, e1, lwr, upr);
|
|
1949
|
|
1950 Lcant:
|
|
1951 return EXP_CANT_INTERPRET;
|
|
1952 }
|
|
1953
|
|
1954
|
|
1955 Expression *CatExp::interpret(InterState *istate)
|
|
1956 { Expression *e;
|
|
1957 Expression *e1;
|
|
1958 Expression *e2;
|
|
1959
|
|
1960 #if LOG
|
|
1961 printf("CatExp::interpret() %s\n", toChars());
|
|
1962 #endif
|
|
1963 e1 = this->e1->interpret(istate);
|
|
1964 if (e1 == EXP_CANT_INTERPRET)
|
|
1965 {
|
|
1966 goto Lcant;
|
|
1967 }
|
|
1968 e2 = this->e2->interpret(istate);
|
|
1969 if (e2 == EXP_CANT_INTERPRET)
|
|
1970 goto Lcant;
|
|
1971 return Cat(type, e1, e2);
|
|
1972
|
|
1973 Lcant:
|
|
1974 #if LOG
|
|
1975 printf("CatExp::interpret() %s CANT\n", toChars());
|
|
1976 #endif
|
|
1977 return EXP_CANT_INTERPRET;
|
|
1978 }
|
|
1979
|
|
1980
|
|
1981 Expression *CastExp::interpret(InterState *istate)
|
|
1982 { Expression *e;
|
|
1983 Expression *e1;
|
|
1984
|
|
1985 #if LOG
|
|
1986 printf("CastExp::interpret() %s\n", toChars());
|
|
1987 #endif
|
|
1988 e1 = this->e1->interpret(istate);
|
|
1989 if (e1 == EXP_CANT_INTERPRET)
|
|
1990 goto Lcant;
|
|
1991 return Cast(type, to, e1);
|
|
1992
|
|
1993 Lcant:
|
|
1994 #if LOG
|
|
1995 printf("CastExp::interpret() %s CANT\n", toChars());
|
|
1996 #endif
|
|
1997 return EXP_CANT_INTERPRET;
|
|
1998 }
|
|
1999
|
|
2000
|
|
2001 Expression *AssertExp::interpret(InterState *istate)
|
|
2002 { Expression *e;
|
|
2003 Expression *e1;
|
|
2004
|
|
2005 #if LOG
|
|
2006 printf("AssertExp::interpret() %s\n", toChars());
|
|
2007 #endif
|
|
2008 e1 = this->e1->interpret(istate);
|
|
2009 if (e1 == EXP_CANT_INTERPRET)
|
|
2010 goto Lcant;
|
|
2011 if (e1->isBool(TRUE))
|
|
2012 {
|
|
2013 }
|
|
2014 else if (e1->isBool(FALSE))
|
|
2015 {
|
|
2016 if (msg)
|
|
2017 {
|
|
2018 e = msg->interpret(istate);
|
|
2019 if (e == EXP_CANT_INTERPRET)
|
|
2020 goto Lcant;
|
|
2021 error("%s", e->toChars());
|
|
2022 }
|
|
2023 else
|
|
2024 error("%s failed", toChars());
|
|
2025 goto Lcant;
|
|
2026 }
|
|
2027 else
|
|
2028 goto Lcant;
|
|
2029 return e1;
|
|
2030
|
|
2031 Lcant:
|
|
2032 return EXP_CANT_INTERPRET;
|
|
2033 }
|
|
2034
|
|
2035 Expression *PtrExp::interpret(InterState *istate)
|
|
2036 { Expression *e = EXP_CANT_INTERPRET;
|
|
2037
|
|
2038 #if LOG
|
|
2039 printf("PtrExp::interpret() %s\n", toChars());
|
|
2040 #endif
|
|
2041
|
|
2042 // Constant fold *(&structliteral + offset)
|
|
2043 if (e1->op == TOKadd)
|
|
2044 { AddExp *ae = (AddExp *)e1;
|
|
2045 if (ae->e1->op == TOKaddress && ae->e2->op == TOKint64)
|
|
2046 { AddrExp *ade = (AddrExp *)ae->e1;
|
|
2047 Expression *ex = ade->e1;
|
|
2048 ex = ex->interpret(istate);
|
|
2049 if (ex != EXP_CANT_INTERPRET)
|
|
2050 {
|
|
2051 if (ex->op == TOKstructliteral)
|
|
2052 { StructLiteralExp *se = (StructLiteralExp *)ex;
|
|
2053 unsigned offset = ae->e2->toInteger();
|
|
2054 e = se->getField(type, offset);
|
|
2055 if (!e)
|
|
2056 e = EXP_CANT_INTERPRET;
|
|
2057 return e;
|
|
2058 }
|
|
2059 }
|
|
2060 }
|
|
2061 e = Ptr(type, e1);
|
|
2062 }
|
|
2063 else if (e1->op == TOKsymoff)
|
|
2064 { SymOffExp *soe = (SymOffExp *)e1;
|
|
2065 VarDeclaration *v = soe->var->isVarDeclaration();
|
|
2066 if (v)
|
|
2067 { Expression *ev = getVarExp(loc, istate, v);
|
|
2068 if (ev != EXP_CANT_INTERPRET && ev->op == TOKstructliteral)
|
|
2069 { StructLiteralExp *se = (StructLiteralExp *)ev;
|
|
2070 e = se->getField(type, soe->offset);
|
|
2071 if (!e)
|
|
2072 e = EXP_CANT_INTERPRET;
|
|
2073 }
|
|
2074 }
|
|
2075 }
|
|
2076 #if LOG
|
|
2077 if (e == EXP_CANT_INTERPRET)
|
|
2078 printf("PtrExp::interpret() %s = EXP_CANT_INTERPRET\n", toChars());
|
|
2079 #endif
|
|
2080 return e;
|
|
2081 }
|
|
2082
|
|
2083 /******************************* Special Functions ***************************/
|
|
2084
|
|
2085 Expression *interpret_aaLen(InterState *istate, Expressions *arguments)
|
|
2086 {
|
|
2087 if (!arguments || arguments->dim != 1)
|
|
2088 return NULL;
|
|
2089 Expression *earg = (Expression *)arguments->data[0];
|
|
2090 earg = earg->interpret(istate);
|
|
2091 if (earg == EXP_CANT_INTERPRET)
|
|
2092 return NULL;
|
|
2093 if (earg->op != TOKassocarrayliteral)
|
|
2094 return NULL;
|
|
2095 AssocArrayLiteralExp *aae = (AssocArrayLiteralExp *)earg;
|
|
2096 Expression *e = new IntegerExp(aae->loc, aae->keys->dim, Type::tsize_t);
|
|
2097 return e;
|
|
2098 }
|
|
2099
|
|
2100 Expression *interpret_aaKeys(InterState *istate, Expressions *arguments)
|
|
2101 {
|
|
2102 //printf("interpret_aaKeys()\n");
|
|
2103 if (!arguments || arguments->dim != 2)
|
|
2104 return NULL;
|
|
2105 Expression *earg = (Expression *)arguments->data[0];
|
|
2106 earg = earg->interpret(istate);
|
|
2107 if (earg == EXP_CANT_INTERPRET)
|
|
2108 return NULL;
|
|
2109 if (earg->op != TOKassocarrayliteral)
|
|
2110 return NULL;
|
|
2111 AssocArrayLiteralExp *aae = (AssocArrayLiteralExp *)earg;
|
|
2112 Expression *e = new ArrayLiteralExp(aae->loc, aae->keys);
|
|
2113 return e;
|
|
2114 }
|
|
2115
|
|
2116 Expression *interpret_aaValues(InterState *istate, Expressions *arguments)
|
|
2117 {
|
|
2118 //printf("interpret_aaValues()\n");
|
|
2119 if (!arguments || arguments->dim != 3)
|
|
2120 return NULL;
|
|
2121 Expression *earg = (Expression *)arguments->data[0];
|
|
2122 earg = earg->interpret(istate);
|
|
2123 if (earg == EXP_CANT_INTERPRET)
|
|
2124 return NULL;
|
|
2125 if (earg->op != TOKassocarrayliteral)
|
|
2126 return NULL;
|
|
2127 AssocArrayLiteralExp *aae = (AssocArrayLiteralExp *)earg;
|
|
2128 Expression *e = new ArrayLiteralExp(aae->loc, aae->values);
|
|
2129 //printf("result is %s\n", e->toChars());
|
|
2130 return e;
|
|
2131 }
|
|
2132
|