comparison dmd/interpret.c @ 875:330f999ade44

Merged DMD 1.038
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 06 Jan 2009 16:33:51 +0100
parents 50383e476c7e
children 29c0d1194033
comparison
equal deleted inserted replaced
874:2ddee23bd70e 875:330f999ade44
1511 } 1511 }
1512 v->value = e2; 1512 v->value = e2;
1513 e = Cast(type, type, post ? ev : e2); 1513 e = Cast(type, type, post ? ev : e2);
1514 } 1514 }
1515 } 1515 }
1516 }
1517 /* Assignment to struct member of the form:
1518 * v.var = e2
1519 */
1520 else if (e1->op == TOKdotvar && ((DotVarExp *)e1)->e1->op == TOKvar)
1521 { VarExp *ve = (VarExp *)((DotVarExp *)e1)->e1;
1522 VarDeclaration *v = ve->var->isVarDeclaration();
1523
1524 if (v->isDataseg())
1525 return EXP_CANT_INTERPRET;
1526 if (fp && !v->value)
1527 { error("variable %s is used before initialization", v->toChars());
1528 return e;
1529 }
1530 Expression *vie = v->value;
1531 if (vie->op == TOKvar)
1532 {
1533 Declaration *d = ((VarExp *)vie)->var;
1534 vie = getVarExp(e1->loc, istate, d);
1535 }
1536 if (vie->op != TOKstructliteral)
1537 return EXP_CANT_INTERPRET;
1538 StructLiteralExp *se = (StructLiteralExp *)vie;
1539 VarDeclaration *vf = ((DotVarExp *)e1)->var->isVarDeclaration();
1540 if (!vf)
1541 return EXP_CANT_INTERPRET;
1542 int fieldi = se->getFieldIndex(type, vf->offset);
1543 if (fieldi == -1)
1544 return EXP_CANT_INTERPRET;
1545 Expression *ev = se->getField(type, vf->offset);
1546 if (fp)
1547 e2 = (*fp)(type, ev, e2);
1548 else
1549 e2 = Cast(type, type, e2);
1550 if (e2 == EXP_CANT_INTERPRET)
1551 return e2;
1552
1553 if (!v->isParameter())
1554 {
1555 for (size_t i = 0; 1; i++)
1556 {
1557 if (i == istate->vars.dim)
1558 { istate->vars.push(v);
1559 break;
1560 }
1561 if (v == (VarDeclaration *)istate->vars.data[i])
1562 break;
1563 }
1564 }
1565
1566 /* Create new struct literal reflecting updated fieldi
1567 */
1568 Expressions *expsx = new Expressions();
1569 expsx->setDim(se->elements->dim);
1570 for (size_t j = 0; j < expsx->dim; j++)
1571 {
1572 if (j == fieldi)
1573 expsx->data[j] = (void *)e2;
1574 else
1575 expsx->data[j] = se->elements->data[j];
1576 }
1577 v->value = new StructLiteralExp(se->loc, se->sd, expsx);
1578 v->value->type = se->type;
1579
1580 e = Cast(type, type, post ? ev : e2);
1516 } 1581 }
1517 /* Assignment to struct member of the form: 1582 /* Assignment to struct member of the form:
1518 * *(symoffexp) = e2 1583 * *(symoffexp) = e2
1519 */ 1584 */
1520 else if (e1->op == TOKstar && ((PtrExp *)e1)->e1->op == TOKsymoff) 1585 else if (e1->op == TOKstar && ((PtrExp *)e1)->e1->op == TOKsymoff)
2159 } 2224 }
2160 2225
2161 Expression *DotVarExp::interpret(InterState *istate) 2226 Expression *DotVarExp::interpret(InterState *istate)
2162 { Expression *e = EXP_CANT_INTERPRET; 2227 { Expression *e = EXP_CANT_INTERPRET;
2163 2228
2229 #if LOG
2230 printf("DotVarExp::interpret() %s\n", toChars());
2231 #endif
2232
2164 Expression *ex = e1->interpret(istate); 2233 Expression *ex = e1->interpret(istate);
2165 2234 if (ex != EXP_CANT_INTERPRET)
2166 // Constant fold structliteral.member 2235 {
2167 if (ex != EXP_CANT_INTERPRET && ex->op == TOKstructliteral) 2236 if (ex->op == TOKstructliteral)
2168 { StructLiteralExp *se = (StructLiteralExp *)ex; 2237 { StructLiteralExp *se = (StructLiteralExp *)ex;
2169 2238 VarDeclaration *v = var->isVarDeclaration();
2170 VarDeclaration* v; 2239 if (v)
2171 if (v = var->isVarDeclaration()) 2240 { e = se->getField(type, v->offset);
2172 { 2241 if (!e)
2173 e = se->getField(type, v->offset); 2242 e = EXP_CANT_INTERPRET;
2174 if (!e) 2243 return e;
2175 e = EXP_CANT_INTERPRET; 2244 }
2176 } 2245 }
2177 } 2246 }
2178 2247
2248 #if LOG
2249 if (e == EXP_CANT_INTERPRET)
2250 printf("DotVarExp::interpret() %s = EXP_CANT_INTERPRET\n", toChars());
2251 #endif
2179 return e; 2252 return e;
2180 } 2253 }
2181 2254
2182 /******************************* Special Functions ***************************/ 2255 /******************************* Special Functions ***************************/
2183 2256