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