Mercurial > projects > ldc
comparison dmd/interpret.c @ 1627:e83f0778c260
Merge DMD r321: bugzilla 3575 CTFE: member structs not initialized correctly
---
dmd/expression.c | 4 +---
dmd/interpret.c | 32 +++++++++-----------------------
dmd/mtype.c | 48 +++++++++++++++++++++++++++++++++++++++++++-----
dmd/mtype.h | 2 ++
4 files changed, 55 insertions(+), 31 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:23 -0300 |
parents | fb2e6707ad17 |
children | 44b145be2ef5 |
comparison
equal
deleted
inserted
replaced
1626:8fa4ab3dcc88 | 1627:e83f0778c260 |
---|---|
1514 return ae; | 1514 return ae; |
1515 } | 1515 } |
1516 | 1516 |
1517 | 1517 |
1518 /******************************** | 1518 /******************************** |
1519 * Necessary because defaultInit() for a struct is a VarExp, not a StructLiteralExp. | |
1520 */ | |
1521 StructLiteralExp *createDefaultInitStructLiteral(Loc loc, StructDeclaration *sym) | |
1522 { | |
1523 Expressions *structelems = new Expressions(); | |
1524 structelems->setDim(sym->fields.dim); | |
1525 for (size_t j = 0; j < structelems->dim; j++) | |
1526 { | |
1527 structelems->data[j] = ((VarDeclaration *)(sym->fields.data[j]))->type->defaultInit(); | |
1528 } | |
1529 StructLiteralExp *structinit = new StructLiteralExp(loc, sym, structelems); | |
1530 // Why doesn't the StructLiteralExp constructor do this, when | |
1531 // sym->type != NULL ? | |
1532 structinit->type = sym->type; | |
1533 return structinit; | |
1534 } | |
1535 | |
1536 /******************************** | |
1537 * Add v to the istate list, unless it already exists there. | 1519 * Add v to the istate list, unless it already exists there. |
1538 */ | 1520 */ |
1539 void addVarToInterstate(InterState *istate, VarDeclaration *v) | 1521 void addVarToInterstate(InterState *istate, VarDeclaration *v) |
1540 { | 1522 { |
1541 if (!v->isParameter()) | 1523 if (!v->isParameter()) |
1590 else | 1572 else |
1591 e1 = v->value; | 1573 e1 = v->value; |
1592 } | 1574 } |
1593 else if (v && v->value && (v->value->op==TOKindex || v->value->op == TOKdotvar)) | 1575 else if (v && v->value && (v->value->op==TOKindex || v->value->op == TOKdotvar)) |
1594 { | 1576 { |
1595 // It is no longer be a TOKvar, eg when a[4] is passed by ref. | 1577 // It is no longer a TOKvar, eg when a[4] is passed by ref. |
1596 e1 = v->value; | 1578 e1 = v->value; |
1597 } | 1579 } |
1598 } | 1580 } |
1599 | 1581 |
1600 // To reduce code complexity of handling dotvar expressions, | 1582 // To reduce code complexity of handling dotvar expressions, |
1632 else | 1614 else |
1633 { /* Look for special case of struct being initialized with 0. | 1615 { /* Look for special case of struct being initialized with 0. |
1634 */ | 1616 */ |
1635 if (v->type->toBasetype()->ty == Tstruct && e2->op == TOKint64) | 1617 if (v->type->toBasetype()->ty == Tstruct && e2->op == TOKint64) |
1636 { | 1618 { |
1637 e2 = v->type->defaultInit(); | 1619 e2 = v->type->defaultInitLiteral(); |
1638 } | 1620 } |
1639 e2 = Cast(v->type, v->type, e2); | 1621 e2 = Cast(v->type, v->type, e2); |
1640 } | 1622 } |
1641 if (e2 == EXP_CANT_INTERPRET) | 1623 if (e2 == EXP_CANT_INTERPRET) |
1642 return e2; | 1624 return e2; |
1958 | 1940 |
1959 Type *telem = ((TypeSArray *)t)->nextOf()->toBasetype(); | 1941 Type *telem = ((TypeSArray *)t)->nextOf()->toBasetype(); |
1960 if (telem->ty != Tstruct) { return EXP_CANT_INTERPRET; } | 1942 if (telem->ty != Tstruct) { return EXP_CANT_INTERPRET; } |
1961 | 1943 |
1962 // Create a default struct literal... | 1944 // Create a default struct literal... |
1963 StructDeclaration *sym = ((TypeStruct *)telem)->sym; | 1945 Expression *structinit = telem->defaultInitLiteral(v->loc); |
1964 StructLiteralExp *structinit = createDefaultInitStructLiteral(v->loc, sym); | |
1965 | 1946 |
1966 // ... and use to create a blank array literal | 1947 // ... and use to create a blank array literal |
1967 size_t dim = ((TypeSArray *)t)->dim->toInteger(); | 1948 size_t dim = ((TypeSArray *)t)->dim->toInteger(); |
1968 ae = createBlockDuplicatedArrayLiteral(v->type, structinit, dim); | 1949 ae = createBlockDuplicatedArrayLiteral(v->type, structinit, dim); |
1969 v->value = ae; | 1950 v->value = ae; |
2640 { StructLiteralExp *se = (StructLiteralExp *)ex; | 2621 { StructLiteralExp *se = (StructLiteralExp *)ex; |
2641 VarDeclaration *v = var->isVarDeclaration(); | 2622 VarDeclaration *v = var->isVarDeclaration(); |
2642 if (v) | 2623 if (v) |
2643 { e = se->getField(type, v->offset); | 2624 { e = se->getField(type, v->offset); |
2644 if (!e) | 2625 if (!e) |
2626 { | |
2627 error("couldn't find field %s in %s", v->toChars(), type->toChars()); | |
2645 e = EXP_CANT_INTERPRET; | 2628 e = EXP_CANT_INTERPRET; |
2629 } | |
2646 return e; | 2630 return e; |
2647 } | 2631 } |
2648 } else error("%s.%s is not yet implemented at compile time", ex->toChars(), var->toChars()); | 2632 } |
2633 else | |
2634 error("%s.%s is not yet implemented at compile time", ex->toChars(), var->toChars()); | |
2649 } | 2635 } |
2650 | 2636 |
2651 #if LOG | 2637 #if LOG |
2652 if (e == EXP_CANT_INTERPRET) | 2638 if (e == EXP_CANT_INTERPRET) |
2653 printf("DotVarExp::interpret() %s = EXP_CANT_INTERPRET\n", toChars()); | 2639 printf("DotVarExp::interpret() %s = EXP_CANT_INTERPRET\n", toChars()); |