mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-01-11 18:33:14 +01:00
Merged dmdfe 2.031.
This commit is contained in:
71
dmd2/init.c
71
dmd2/init.c
@@ -396,24 +396,70 @@ Expression *ArrayInitializer::toExpression()
|
||||
{ Expressions *elements;
|
||||
Expression *e;
|
||||
|
||||
//printf("ArrayInitializer::toExpression()\n");
|
||||
//printf("ArrayInitializer::toExpression(), dim = %d\n", dim);
|
||||
//static int i; if (++i == 2) halt();
|
||||
|
||||
size_t edim;
|
||||
Type *t = NULL;
|
||||
if (type)
|
||||
{
|
||||
t = type->toBasetype();
|
||||
switch (t->ty)
|
||||
{
|
||||
case Tsarray:
|
||||
edim = ((TypeSArray *)t)->dim->toInteger();
|
||||
break;
|
||||
|
||||
case Tpointer:
|
||||
case Tarray:
|
||||
edim = dim;
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
edim = value.dim;
|
||||
|
||||
elements = new Expressions();
|
||||
for (size_t i = 0; i < value.dim; i++)
|
||||
elements->setDim(edim);
|
||||
for (size_t i = 0, j = 0; i < value.dim; i++, j++)
|
||||
{
|
||||
if (index.data[i])
|
||||
goto Lno;
|
||||
j = ((Expression *)index.data[i])->toInteger();
|
||||
assert(j < edim);
|
||||
Initializer *iz = (Initializer *)value.data[i];
|
||||
if (!iz)
|
||||
goto Lno;
|
||||
Expression *ex = iz->toExpression();
|
||||
if (!ex)
|
||||
{
|
||||
goto Lno;
|
||||
elements->push(ex);
|
||||
}
|
||||
elements->data[j] = ex;
|
||||
}
|
||||
e = new ArrayLiteralExp(loc, elements);
|
||||
|
||||
/* Fill in any missing elements with the default initializer
|
||||
*/
|
||||
{
|
||||
Expression *init = NULL;
|
||||
for (size_t i = 0; i < edim; i++)
|
||||
{
|
||||
if (!elements->data[i])
|
||||
{
|
||||
if (!type)
|
||||
goto Lno;
|
||||
if (!init)
|
||||
init = ((TypeNext *)t)->next->defaultInit();
|
||||
elements->data[i] = init;
|
||||
}
|
||||
}
|
||||
|
||||
Expression *e = new ArrayLiteralExp(loc, elements);
|
||||
e->type = type;
|
||||
return e;
|
||||
}
|
||||
|
||||
Lno:
|
||||
delete elements;
|
||||
@@ -466,21 +512,26 @@ Lno:
|
||||
|
||||
Type *ArrayInitializer::inferType(Scope *sc)
|
||||
{
|
||||
//printf("ArrayInitializer::inferType() %s\n", toChars());
|
||||
type = Type::terror;
|
||||
for (size_t i = 0; i < value.dim; i++)
|
||||
{
|
||||
if (index.data[i])
|
||||
goto Lno;
|
||||
}
|
||||
if (value.dim)
|
||||
for (size_t i = 0; i < value.dim; i++)
|
||||
{
|
||||
Initializer *iz = (Initializer *)value.data[0];
|
||||
Initializer *iz = (Initializer *)value.data[i];
|
||||
if (iz)
|
||||
{ Type *t = iz->inferType(sc);
|
||||
t = new TypeSArray(t, new IntegerExp(value.dim));
|
||||
t = t->semantic(loc, sc);
|
||||
return t;
|
||||
if (i == 0)
|
||||
{ t = new TypeSArray(t, new IntegerExp(value.dim));
|
||||
t = t->semantic(loc, sc);
|
||||
type = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return type;
|
||||
|
||||
Lno:
|
||||
error(loc, "cannot infer type from this array initializer");
|
||||
|
||||
Reference in New Issue
Block a user