Skip to content

Commit

Permalink
ImportC add simple array initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored and dlang-bot committed Jun 10, 2021
1 parent f1692f5 commit a9efb98
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
41 changes: 38 additions & 3 deletions src/dmd/initsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,46 @@ extern(C++) Initializer initializerSemantic(Initializer init, Scope* sc, ref Typ
return i;
}

Initializer visitC(CInitializer i)
Initializer visitC(CInitializer ci)
{
//printf("CInitializer::semantic()\n");
error(i.loc, "C initializers not supported yet");
return err();
t = t.toBasetype();
auto tsa = t.isTypeSArray();
if (!tsa)
{
error(ci.loc, "C non-static-array initializers not supported yet");
return err();
}

const uint amax = 0x8000_0000;
bool errors;
auto tn = tsa.nextOf();
auto dil = ci.initializerList[];
foreach (di; dil)
{
if (di.designatorList)
{
error(ci.loc, "C designator-list not supported yet");
return err();
}
di.initializer = di.initializer.initializerSemantic(sc, tn, needInterpret);
if (di.initializer.isErrorInitializer())
errors = true;
}

if (errors)
return err();

const sz = tn.size();
bool overflow;
const max = mulu(dil.length, sz, overflow);
if (overflow || max >= amax)
{
error(ci.loc, "array dimension %llu exceeds max of %llu", ulong(dil.length), ulong(amax / sz));
return err();
}

return ci;
}

final switch (init.kind)
Expand Down
16 changes: 15 additions & 1 deletion src/dmd/todt.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ extern (C++) void Initializer_toDt(Initializer init, ref DtBuilder dtb)

void visitStruct(StructInitializer si)
{
/* The StructInitializer was converted to a StructLiteralExp,
* which is converted to dtb by membersToDt()
*/
//printf("StructInitializer.toDt('%s')\n", si.toChars());
assert(0);
}
Expand Down Expand Up @@ -197,14 +200,25 @@ extern (C++) void Initializer_toDt(Initializer init, ref DtBuilder dtb)
Expression_toDt(ei.exp, dtb);
}

void visitC(CInitializer ci)
{
//printf("CInitializer::semantic()\n");
auto dil = ci.initializerList[];
foreach (di; dil)
{
assert(!di.designatorList);
Initializer_toDt(di.initializer, dtb);
}
}

final switch (init.kind)
{
case InitKind.void_: return visitVoid (cast( VoidInitializer)init);
case InitKind.error: return visitError (cast( ErrorInitializer)init);
case InitKind.struct_: return visitStruct(cast(StructInitializer)init);
case InitKind.array: return visitArray (cast( ArrayInitializer)init);
case InitKind.exp: return visitExp (cast( ExpInitializer)init);
case InitKind.C_: assert(0);
case InitKind.C_: return visitC (cast( CInitializer)init);
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/runnable/cstuff2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

int printf(const char *, ...);
void exit(int);

void test1()
{
static int a[3] = {1, 2, 3};
if (a[0] != 1 ||
a[1] != 2 ||
a[2] != 3)
{
printf("error 1\n");
exit(1);
}
}

int main()
{
test1();
return 0;
}

0 comments on commit a9efb98

Please sign in to comment.