Added support for implicitly-sized initialized arrays.

Also fixed dynamic arrays not being cleared before initializing.
This commit is contained in:
Chronos Ouroboros 2019-01-06 23:13:51 -02:00 committed by Christoph Oelckers
commit 4fdcc47edc
4 changed files with 62 additions and 8 deletions

View file

@ -11406,6 +11406,7 @@ FxLocalArrayDeclaration::FxLocalArrayDeclaration(PType *type, FName name, FArgum
{
ExprType = EFX_LocalArrayDeclaration;
values = std::move(args);
clearExpr = nullptr;
}
FxExpression *FxLocalArrayDeclaration::Resolve(FCompileContext &ctx)
@ -11421,6 +11422,16 @@ FxExpression *FxLocalArrayDeclaration::Resolve(FCompileContext &ctx)
auto elementType = (static_cast<PArray *> (ValueType))->ElementType;
auto elementCount = (static_cast<PArray *> (ValueType))->ElementCount;
// We HAVE to clear dynamic arrays before initializing them
if (IsDynamicArray())
{
FArgumentList argsList;
argsList.Clear();
clearExpr = new FxMemberFunctionCall(stackVar, "Clear", argsList, (const FScriptPosition) ScriptPosition);
SAFE_RESOLVE(clearExpr, ctx);
}
if (values.Size() > elementCount)
{
ScriptPosition.Message(MSG_ERROR, "Initializer contains more elements than the array can contain");
@ -11477,6 +11488,11 @@ ExpEmit FxLocalArrayDeclaration::Emit(VMFunctionBuilder *build)
{
assert(!(VarFlags & VARF_Out)); // 'out' variables should never be initialized, they can only exist as function parameters.
if (IsDynamicArray() && clearExpr != nullptr)
{
clearExpr->Emit(build);
}
auto elementSizeConst = build->GetConstantInt(static_cast<PArray *>(ValueType)->ElementSize);
auto arrOffsetReg = build->Registers[REGT_INT].Get(1);
build->Emit(OP_LK, arrOffsetReg, build->GetConstantInt(StackOffset));