mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-01-11 18:33:14 +01:00
Merged dmd 2.059beta
This commit is contained in:
129
dmd2/init.c
129
dmd2/init.c
@@ -1,6 +1,6 @@
|
||||
|
||||
// Compiler implementation of the D programming language
|
||||
// Copyright (c) 1999-2011 by Digital Mars
|
||||
// Copyright (c) 1999-2012 by Digital Mars
|
||||
// All Rights Reserved
|
||||
// written by Walter Bright
|
||||
// http://www.digitalmars.com
|
||||
@@ -53,10 +53,10 @@ Initializers *Initializer::arraySyntaxCopy(Initializers *ai)
|
||||
a = new Initializers();
|
||||
a->setDim(ai->dim);
|
||||
for (size_t i = 0; i < a->dim; i++)
|
||||
{ Initializer *e = ai->tdata()[i];
|
||||
{ Initializer *e = (*ai)[i];
|
||||
|
||||
e = e->syntaxCopy();
|
||||
a->tdata()[i] = e;
|
||||
(*a)[i] = e;
|
||||
}
|
||||
}
|
||||
return a;
|
||||
@@ -128,11 +128,11 @@ Initializer *StructInitializer::syntaxCopy()
|
||||
ai->value.setDim(value.dim);
|
||||
for (size_t i = 0; i < field.dim; i++)
|
||||
{
|
||||
ai->field.tdata()[i] = field.tdata()[i];
|
||||
ai->field[i] = field[i];
|
||||
|
||||
Initializer *init = value.tdata()[i];
|
||||
Initializer *init = value[i];
|
||||
init = init->syntaxCopy();
|
||||
ai->value.tdata()[i] = init;
|
||||
ai->value[i] = init;
|
||||
}
|
||||
return ai;
|
||||
}
|
||||
@@ -164,8 +164,8 @@ Initializer *StructInitializer::semantic(Scope *sc, Type *t, int needInterpret)
|
||||
if (((StructDeclaration *)ad)->isnested) nfields--;
|
||||
for (size_t i = 0; i < field.dim; i++)
|
||||
{
|
||||
Identifier *id = field.tdata()[i];
|
||||
Initializer *val = value.tdata()[i];
|
||||
Identifier *id = field[i];
|
||||
Initializer *val = value[i];
|
||||
Dsymbol *s;
|
||||
VarDeclaration *v;
|
||||
|
||||
@@ -180,7 +180,7 @@ Initializer *StructInitializer::semantic(Scope *sc, Type *t, int needInterpret)
|
||||
}
|
||||
else
|
||||
{
|
||||
s = ad->fields.tdata()[fieldi];
|
||||
s = ad->fields[fieldi];
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -189,7 +189,12 @@ Initializer *StructInitializer::semantic(Scope *sc, Type *t, int needInterpret)
|
||||
s = ad->search(loc, id, 0);
|
||||
if (!s)
|
||||
{
|
||||
error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
|
||||
s = ad->search_correct(id);
|
||||
if (s)
|
||||
error(loc, "'%s' is not a member of '%s', did you mean '%s %s'?",
|
||||
id->toChars(), t->toChars(), s->kind(), s->toChars());
|
||||
else
|
||||
error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
|
||||
errors = 1;
|
||||
continue;
|
||||
}
|
||||
@@ -205,15 +210,15 @@ Initializer *StructInitializer::semantic(Scope *sc, Type *t, int needInterpret)
|
||||
errors = 1;
|
||||
break;
|
||||
}
|
||||
if (s == ad->fields.tdata()[fieldi])
|
||||
if (s == ad->fields[fieldi])
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (s && (v = s->isVarDeclaration()) != NULL)
|
||||
{
|
||||
val = val->semantic(sc, v->type, needInterpret);
|
||||
value.tdata()[i] = val;
|
||||
vars.tdata()[i] = v;
|
||||
value[i] = val;
|
||||
vars[i] = v;
|
||||
}
|
||||
else
|
||||
{ error(loc, "%s is not a field of %s", id ? id->toChars() : s->toChars(), ad->toChars());
|
||||
@@ -274,12 +279,12 @@ Expression *StructInitializer::toExpression()
|
||||
elements->setDim(nfields);
|
||||
for (size_t i = 0; i < elements->dim; i++)
|
||||
{
|
||||
elements->tdata()[i] = NULL;
|
||||
(*elements)[i] = NULL;
|
||||
}
|
||||
unsigned fieldi = 0;
|
||||
for (size_t i = 0; i < value.dim; i++)
|
||||
{
|
||||
Identifier *id = field.tdata()[i];
|
||||
Identifier *id = field[i];
|
||||
if (id)
|
||||
{
|
||||
Dsymbol * s = ad->search(loc, id, 0);
|
||||
@@ -298,7 +303,7 @@ Expression *StructInitializer::toExpression()
|
||||
s->error("is not a per-instance initializable field");
|
||||
goto Lno;
|
||||
}
|
||||
if (s == ad->fields.tdata()[fieldi])
|
||||
if (s == ad->fields[fieldi])
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -306,18 +311,18 @@ Expression *StructInitializer::toExpression()
|
||||
{ error(loc, "too many initializers for '%s'", ad->toChars());
|
||||
goto Lno;
|
||||
}
|
||||
Initializer *iz = value.tdata()[i];
|
||||
Initializer *iz = value[i];
|
||||
if (!iz)
|
||||
goto Lno;
|
||||
Expression *ex = iz->toExpression();
|
||||
if (!ex)
|
||||
goto Lno;
|
||||
if (elements->tdata()[fieldi])
|
||||
if ((*elements)[fieldi])
|
||||
{ error(loc, "duplicate initializer for field '%s'",
|
||||
ad->fields.tdata()[fieldi]->toChars());
|
||||
ad->fields[fieldi]->toChars());
|
||||
goto Lno;
|
||||
}
|
||||
elements->tdata()[fieldi] = ex;
|
||||
(*elements)[fieldi] = ex;
|
||||
++fieldi;
|
||||
}
|
||||
// Now, fill in any missing elements with default initializers.
|
||||
@@ -325,20 +330,20 @@ Expression *StructInitializer::toExpression()
|
||||
offset = 0;
|
||||
for (size_t i = 0; i < elements->dim; )
|
||||
{
|
||||
VarDeclaration * vd = ad->fields.tdata()[i]->isVarDeclaration();
|
||||
VarDeclaration * vd = ad->fields[i]->isVarDeclaration();
|
||||
|
||||
//printf("test2 [%d] : %s %d %d\n", i, vd->toChars(), (int)offset, (int)vd->offset);
|
||||
if (vd->offset < offset)
|
||||
{
|
||||
// Only the first field of a union can have an initializer
|
||||
if (elements->tdata()[i])
|
||||
if ((*elements)[i])
|
||||
goto Lno;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!elements->tdata()[i])
|
||||
if (!(*elements)[i])
|
||||
// Default initialize
|
||||
elements->tdata()[i] = vd->type->defaultInit();
|
||||
(*elements)[i] = vd->type->defaultInit();
|
||||
}
|
||||
offset = vd->offset + vd->type->size();
|
||||
i++;
|
||||
@@ -346,15 +351,15 @@ Expression *StructInitializer::toExpression()
|
||||
int unionSize = ad->numFieldsInUnion(i);
|
||||
if (unionSize == 1)
|
||||
{ // Not a union -- default initialize if missing
|
||||
if (!elements->tdata()[i])
|
||||
elements->tdata()[i] = vd->type->defaultInit();
|
||||
if (!(*elements)[i])
|
||||
(*elements)[i] = vd->type->defaultInit();
|
||||
}
|
||||
else
|
||||
{ // anonymous union -- check for errors
|
||||
int found = -1; // index of the first field with an initializer
|
||||
for (int j = i; j < i + unionSize; ++j)
|
||||
for (size_t j = i; j < i + unionSize; ++j)
|
||||
{
|
||||
if (!elements->tdata()[j])
|
||||
if (!(*elements)[j])
|
||||
continue;
|
||||
if (found >= 0)
|
||||
{
|
||||
@@ -395,13 +400,13 @@ void StructInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
||||
{
|
||||
if (i > 0)
|
||||
buf->writebyte(',');
|
||||
Identifier *id = field.tdata()[i];
|
||||
Identifier *id = field[i];
|
||||
if (id)
|
||||
{
|
||||
buf->writestring(id->toChars());
|
||||
buf->writebyte(':');
|
||||
}
|
||||
Initializer *iz = value.tdata()[i];
|
||||
Initializer *iz = value[i];
|
||||
if (iz)
|
||||
iz->toCBuffer(buf, hgs);
|
||||
}
|
||||
@@ -428,14 +433,14 @@ Initializer *ArrayInitializer::syntaxCopy()
|
||||
ai->index.setDim(index.dim);
|
||||
ai->value.setDim(value.dim);
|
||||
for (size_t i = 0; i < ai->value.dim; i++)
|
||||
{ Expression *e = index.tdata()[i];
|
||||
{ Expression *e = index[i];
|
||||
if (e)
|
||||
e = e->syntaxCopy();
|
||||
ai->index.tdata()[i] = e;
|
||||
ai->index[i] = e;
|
||||
|
||||
Initializer *init = value.tdata()[i];
|
||||
Initializer *init = value[i];
|
||||
init = init->syntaxCopy();
|
||||
ai->value.tdata()[i] = init;
|
||||
ai->value[i] = init;
|
||||
}
|
||||
return ai;
|
||||
}
|
||||
@@ -474,17 +479,17 @@ Initializer *ArrayInitializer::semantic(Scope *sc, Type *t, int needInterpret)
|
||||
length = 0;
|
||||
for (i = 0; i < index.dim; i++)
|
||||
{
|
||||
Expression *idx = index.tdata()[i];
|
||||
Expression *idx = index[i];
|
||||
if (idx)
|
||||
{ idx = idx->semantic(sc);
|
||||
idx = idx->optimize(WANTvalue | WANTinterpret);
|
||||
index.tdata()[i] = idx;
|
||||
index[i] = idx;
|
||||
length = idx->toInteger();
|
||||
}
|
||||
|
||||
Initializer *val = value.tdata()[i];
|
||||
Initializer *val = value[i];
|
||||
val = val->semantic(sc, t->nextOf(), needInterpret);
|
||||
value.tdata()[i] = val;
|
||||
value[i] = val;
|
||||
length++;
|
||||
if (length == 0)
|
||||
{ error(loc, "array dimension overflow");
|
||||
@@ -498,7 +503,7 @@ Initializer *ArrayInitializer::semantic(Scope *sc, Type *t, int needInterpret)
|
||||
dinteger_t edim = ((TypeSArray *)t)->dim->toInteger();
|
||||
if (dim > edim)
|
||||
{
|
||||
error(loc, "array initializer has %u elements, but array length is %jd", dim, edim);
|
||||
error(loc, "array initializer has %u elements, but array length is %lld", dim, edim);
|
||||
goto Lerr;
|
||||
}
|
||||
}
|
||||
@@ -552,8 +557,8 @@ Expression *ArrayInitializer::toExpression()
|
||||
edim = value.dim;
|
||||
for (size_t i = 0, j = 0; i < value.dim; i++, j++)
|
||||
{
|
||||
if (index.tdata()[i])
|
||||
j = index.tdata()[i]->toInteger();
|
||||
if (index[i])
|
||||
j = index[i]->toInteger();
|
||||
if (j >= edim)
|
||||
edim = j + 1;
|
||||
}
|
||||
@@ -564,10 +569,10 @@ Expression *ArrayInitializer::toExpression()
|
||||
elements->zero();
|
||||
for (size_t i = 0, j = 0; i < value.dim; i++, j++)
|
||||
{
|
||||
if (index.tdata()[i])
|
||||
j = (index.tdata()[i])->toInteger();
|
||||
if (index[i])
|
||||
j = (index[i])->toInteger();
|
||||
assert(j < edim);
|
||||
Initializer *iz = value.tdata()[i];
|
||||
Initializer *iz = value[i];
|
||||
if (!iz)
|
||||
goto Lno;
|
||||
Expression *ex = iz->toExpression();
|
||||
@@ -575,7 +580,7 @@ Expression *ArrayInitializer::toExpression()
|
||||
{
|
||||
goto Lno;
|
||||
}
|
||||
elements->tdata()[j] = ex;
|
||||
(*elements)[j] = ex;
|
||||
}
|
||||
|
||||
/* Fill in any missing elements with the default initializer
|
||||
@@ -584,13 +589,13 @@ Expression *ArrayInitializer::toExpression()
|
||||
Expression *init = NULL;
|
||||
for (size_t i = 0; i < edim; i++)
|
||||
{
|
||||
if (!elements->tdata()[i])
|
||||
if (!(*elements)[i])
|
||||
{
|
||||
if (!type)
|
||||
goto Lno;
|
||||
if (!init)
|
||||
init = ((TypeNext *)t)->next->defaultInit();
|
||||
elements->tdata()[i] = init;
|
||||
(*elements)[i] = init;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,18 +626,18 @@ Expression *ArrayInitializer::toAssocArrayLiteral()
|
||||
|
||||
for (size_t i = 0; i < value.dim; i++)
|
||||
{
|
||||
e = index.tdata()[i];
|
||||
e = index[i];
|
||||
if (!e)
|
||||
goto Lno;
|
||||
keys->tdata()[i] = e;
|
||||
(*keys)[i] = e;
|
||||
|
||||
Initializer *iz = value.tdata()[i];
|
||||
Initializer *iz = value[i];
|
||||
if (!iz)
|
||||
goto Lno;
|
||||
e = iz->toExpression();
|
||||
if (!e)
|
||||
goto Lno;
|
||||
values->tdata()[i] = e;
|
||||
(*values)[i] = e;
|
||||
}
|
||||
e = new AssocArrayLiteralExp(loc, keys, values);
|
||||
return e;
|
||||
@@ -648,7 +653,7 @@ int ArrayInitializer::isAssociativeArray()
|
||||
{
|
||||
for (size_t i = 0; i < value.dim; i++)
|
||||
{
|
||||
if (index.tdata()[i])
|
||||
if (index[i])
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -710,13 +715,13 @@ void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
||||
{
|
||||
if (i > 0)
|
||||
buf->writebyte(',');
|
||||
Expression *ex = index.tdata()[i];
|
||||
Expression *ex = index[i];
|
||||
if (ex)
|
||||
{
|
||||
ex->toCBuffer(buf, hgs);
|
||||
buf->writebyte(':');
|
||||
}
|
||||
Initializer *iz = value.tdata()[i];
|
||||
Initializer *iz = value[i];
|
||||
if (iz)
|
||||
iz->toCBuffer(buf, hgs);
|
||||
}
|
||||
@@ -792,10 +797,8 @@ bool hasNonConstPointers(Expression *e)
|
||||
bool arrayHasNonConstPointers(Expressions *elems)
|
||||
{
|
||||
for (size_t i = 0; i < elems->dim; i++)
|
||||
{
|
||||
if (!elems->tdata()[i])
|
||||
continue;
|
||||
if (hasNonConstPointers(elems->tdata()[i]))
|
||||
{ Expression *e = (*elems)[i];
|
||||
if (e && hasNonConstPointers(e))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -816,7 +819,7 @@ Initializer *ExpInitializer::semantic(Scope *sc, Type *t, int needInterpret)
|
||||
return this; // Failed, suppress duplicate error messages
|
||||
|
||||
if (exp->op == TOKtype)
|
||||
error("initializer must be an expression, not '%s'", exp->toChars());
|
||||
exp->error("initializer must be an expression, not '%s'", exp->toChars());
|
||||
|
||||
// Make sure all pointers are constants
|
||||
if (needInterpret && hasNonConstPointers(exp))
|
||||
@@ -865,12 +868,6 @@ L1:
|
||||
Type *ExpInitializer::inferType(Scope *sc)
|
||||
{
|
||||
//printf("ExpInitializer::inferType() %s\n", toChars());
|
||||
if (exp->op == TOKfunction && ((FuncExp *)exp)->td)
|
||||
{
|
||||
exp->error("cannot infer type from ambiguous function literal %s", exp->toChars());
|
||||
return Type::terror;
|
||||
}
|
||||
|
||||
exp = exp->semantic(sc);
|
||||
exp = resolveProperties(sc, exp);
|
||||
|
||||
@@ -884,7 +881,7 @@ Type *ExpInitializer::inferType(Scope *sc)
|
||||
// Give error for overloaded function addresses
|
||||
if (exp->op == TOKdelegate)
|
||||
{ DelegateExp *se = (DelegateExp *)exp;
|
||||
if (
|
||||
if (se->hasOverloads &&
|
||||
se->func->isFuncDeclaration() &&
|
||||
!se->func->isFuncDeclaration()->isUnique())
|
||||
exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
|
||||
|
||||
Reference in New Issue
Block a user