comparison dmd/interpret.c @ 19:788401029ecf trunk

[svn r23] * Updated to DMD 1.021
author lindquist
date Thu, 04 Oct 2007 03:42:56 +0200
parents c53b6e3fe49a
children 5825d48b27d1
comparison
equal deleted inserted replaced
18:c05ef76f1c20 19:788401029ecf
75 if (needThis() || isNested() || !fbody) 75 if (needThis() || isNested() || !fbody)
76 { cantInterpret = 1; 76 { cantInterpret = 1;
77 return NULL; 77 return NULL;
78 } 78 }
79 79
80 //printf("test2 %d, %p\n", semanticRun, scope);
80 if (semanticRun == 0 && scope) 81 if (semanticRun == 0 && scope)
81 { 82 {
82 semantic3(scope); 83 semantic3(scope);
83 } 84 }
84 if (semanticRun < 2) 85 if (semanticRun < 2)
274 if (exp) 275 if (exp)
275 { 276 {
276 Expression *e = exp->interpret(istate); 277 Expression *e = exp->interpret(istate);
277 if (e == EXP_CANT_INTERPRET) 278 if (e == EXP_CANT_INTERPRET)
278 { 279 {
279 //printf("cannot interpret %s\n", exp->toChars()); 280 //printf("-ExpStatement::interpret(): %p\n", e);
280 return EXP_CANT_INTERPRET; 281 return EXP_CANT_INTERPRET;
281 } 282 }
282 } 283 }
283 return NULL; 284 return NULL;
284 } 285 }
362 Expression *e = condition->interpret(istate); 363 Expression *e = condition->interpret(istate);
363 assert(e); 364 assert(e);
364 //if (e == EXP_CANT_INTERPRET) printf("cannot interpret\n"); 365 //if (e == EXP_CANT_INTERPRET) printf("cannot interpret\n");
365 if (e != EXP_CANT_INTERPRET) 366 if (e != EXP_CANT_INTERPRET)
366 { 367 {
367 if (!e->isConst()) 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;
372 else
368 { 373 {
369 e = EXP_CANT_INTERPRET; 374 e = EXP_CANT_INTERPRET;
370 }
371 else
372 {
373 if (e->isBool(TRUE))
374 e = ifbody ? ifbody->interpret(istate) : NULL;
375 else if (e->isBool(FALSE))
376 e = elsebody ? elsebody->interpret(istate) : NULL;
377 else
378 {
379 e = EXP_CANT_INTERPRET;
380 }
381 } 375 }
382 } 376 }
383 return e; 377 return e;
384 } 378 }
385 379
955 SymbolDeclaration *s = d->isSymbolDeclaration(); 949 SymbolDeclaration *s = d->isSymbolDeclaration();
956 if (v) 950 if (v)
957 { 951 {
958 if (v->isConst() && v->init) 952 if (v->isConst() && v->init)
959 { e = v->init->toExpression(); 953 { e = v->init->toExpression();
960 if (!e->type) 954 if (e && !e->type)
961 e->type = v->type; 955 e->type = v->type;
962 } 956 }
963 else 957 else
964 { e = v->value; 958 { e = v->value;
965 if (!e) 959 if (!e)
992 Expression *DeclarationExp::interpret(InterState *istate) 986 Expression *DeclarationExp::interpret(InterState *istate)
993 { 987 {
994 #if LOG 988 #if LOG
995 printf("DeclarationExp::interpret() %s\n", toChars()); 989 printf("DeclarationExp::interpret() %s\n", toChars());
996 #endif 990 #endif
997 Expression *e = EXP_CANT_INTERPRET; 991 Expression *e;
998 VarDeclaration *v = declaration->isVarDeclaration(); 992 VarDeclaration *v = declaration->isVarDeclaration();
999 if (v) 993 if (v)
1000 { 994 {
1001 Dsymbol *s = v->toAlias(); 995 Dsymbol *s = v->toAlias();
1002 if (s == v && !v->isStatic() && v->init) 996 if (s == v && !v->isStatic() && v->init)
1013 e = EXP_CANT_INTERPRET; 1007 e = EXP_CANT_INTERPRET;
1014 else if (!e->type) 1008 else if (!e->type)
1015 e->type = v->type; 1009 e->type = v->type;
1016 } 1010 }
1017 } 1011 }
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
1018 return e; 1025 return e;
1019 } 1026 }
1020 1027
1021 Expression *TupleExp::interpret(InterState *istate) 1028 Expression *TupleExp::interpret(InterState *istate)
1022 { 1029 {
1082 if (ex != e) 1089 if (ex != e)
1083 { 1090 {
1084 if (!expsx) 1091 if (!expsx)
1085 { expsx = new Expressions(); 1092 { expsx = new Expressions();
1086 expsx->setDim(elements->dim); 1093 expsx->setDim(elements->dim);
1087 for (size_t j = 0; j < i; j++) 1094 for (size_t j = 0; j < elements->dim; j++)
1088 { 1095 {
1089 expsx->data[j] = elements->data[j]; 1096 expsx->data[j] = elements->data[j];
1090 } 1097 }
1091 } 1098 }
1092 expsx->data[i] = (void *)ex; 1099 expsx->data[i] = (void *)ex;
1340 #endif 1347 #endif
1341 e1 = this->e1->interpret(istate); 1348 e1 = this->e1->interpret(istate);
1342 if (e1 == EXP_CANT_INTERPRET) 1349 if (e1 == EXP_CANT_INTERPRET)
1343 goto Lcant; 1350 goto Lcant;
1344 if (e1->isConst() != 1 && 1351 if (e1->isConst() != 1 &&
1352 e1->op != TOKnull &&
1345 e1->op != TOKstring && 1353 e1->op != TOKstring &&
1346 e1->op != TOKarrayliteral && 1354 e1->op != TOKarrayliteral &&
1347 e1->op != TOKstructliteral) 1355 e1->op != TOKstructliteral)
1348 goto Lcant; 1356 goto Lcant;
1349 1357
1350 e2 = this->e2->interpret(istate); 1358 e2 = this->e2->interpret(istate);
1351 if (e2 == EXP_CANT_INTERPRET) 1359 if (e2 == EXP_CANT_INTERPRET)
1352 goto Lcant; 1360 goto Lcant;
1353 if (e2->isConst() != 1 && 1361 if (e2->isConst() != 1 &&
1362 e2->op != TOKnull &&
1354 e2->op != TOKstring && 1363 e2->op != TOKstring &&
1355 e2->op != TOKarrayliteral && 1364 e2->op != TOKarrayliteral &&
1356 e2->op != TOKstructliteral) 1365 e2->op != TOKstructliteral)
1357 goto Lcant; 1366 goto Lcant;
1358 1367
1418 return e; 1427 return e;
1419 } 1428 }
1420 if (fp) 1429 if (fp)
1421 e2 = (*fp)(v->type, ev, e2); 1430 e2 = (*fp)(v->type, ev, e2);
1422 else 1431 else
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 }
1423 e2 = Cast(v->type, v->type, e2); 1438 e2 = Cast(v->type, v->type, e2);
1439 }
1424 if (e2 != EXP_CANT_INTERPRET) 1440 if (e2 != EXP_CANT_INTERPRET)
1425 { 1441 {
1426 if (!v->isParameter()) 1442 if (!v->isParameter())
1427 { 1443 {
1428 for (size_t i = 0; 1; i++) 1444 for (size_t i = 0; 1; i++)
1451 return EXP_CANT_INTERPRET; 1467 return EXP_CANT_INTERPRET;
1452 if (fp && !v->value) 1468 if (fp && !v->value)
1453 { error("variable %s is used before initialization", v->toChars()); 1469 { error("variable %s is used before initialization", v->toChars());
1454 return e; 1470 return e;
1455 } 1471 }
1456 if (v->value->op != TOKstructliteral) 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)
1457 return EXP_CANT_INTERPRET; 1479 return EXP_CANT_INTERPRET;
1458 StructLiteralExp *se = (StructLiteralExp *)v->value; 1480 StructLiteralExp *se = (StructLiteralExp *)vie;
1459 int fieldi = se->getFieldIndex(type, soe->offset); 1481 int fieldi = se->getFieldIndex(type, soe->offset);
1460 if (fieldi == -1) 1482 if (fieldi == -1)
1461 return EXP_CANT_INTERPRET; 1483 return EXP_CANT_INTERPRET;
1462 Expression *ev = se->getField(type, soe->offset); 1484 Expression *ev = se->getField(type, soe->offset);
1463 if (fp) 1485 if (fp)
1841 goto Lcant; 1863 goto Lcant;
1842 if (e1->op == TOKstring || e1->op == TOKarrayliteral || e1->op == TOKassocarrayliteral) 1864 if (e1->op == TOKstring || e1->op == TOKarrayliteral || e1->op == TOKassocarrayliteral)
1843 { 1865 {
1844 e = ArrayLength(type, e1); 1866 e = ArrayLength(type, e1);
1845 } 1867 }
1868 else if (e1->op == TOKnull)
1869 {
1870 e = new IntegerExp(loc, 0, type);
1871 }
1846 else 1872 else
1847 goto Lcant; 1873 goto Lcant;
1848 return e; 1874 return e;
1849 1875
1850 Lcant: 1876 Lcant: