Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#include "PrimType.h"
#include "Program.h"
#include "clang/AST/Attr.h"
#include "clang/Basic/UnsignedOrNone.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Support/Casting.h"

#define DEBUG_TYPE "exprconstant"

using namespace clang;
using namespace clang::interp;
Expand Down Expand Up @@ -1942,9 +1947,9 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,

const ConstantArrayType *CAT =
Ctx.getASTContext().getAsConstantArrayType(QT);
uint64_t NumElems = CAT->getZExtSize();
uint64_t Capacity = CAT->getZExtSize();

if (!this->emitCheckArraySize(NumElems, E))
if (!this->emitCheckArraySize(Capacity, E))
return false;

OptPrimType InitT = classify(CAT->getElementType());
Expand Down Expand Up @@ -1977,15 +1982,59 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
}
}

// Expand the filler expression.
// FIXME: This should go away.
if (ArrayFiller) {
for (; ElementIndex != NumElems; ++ElementIndex) {
if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
auto isInit = [&](const Expr *E) -> bool {
if (const auto *VD = dyn_cast_if_present<VarDecl>(this->InitializingDecl))
return E == VD->getInit();
return false;
};

LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
<< ElementIndex << ".\n");

if (isInit(E) && ArraySize::shouldUseFiller(ElementIndex, Capacity) &&
Context::shouldBeGloballyIndexed(InitializingDecl)) {
assert(ArrayFiller);

unsigned TempOffset = 0;
if (InitT) {
TempOffset = this->allocateLocalPrimitive(ArrayFiller, *InitT,
/*IsConst=*/false);
if (!this->visit(ArrayFiller))
return false;
if (!this->emitSetLocal(*InitT, TempOffset, ArrayFiller))
return false;
} else {
if (UnsignedOrNone LocalOffset = this->allocateLocal(ArrayFiller)) {
TempOffset = *LocalOffset;
if (!this->emitGetPtrLocal(*LocalOffset, E))
return false;

if (!this->visitInitializer(ArrayFiller))
return false;

if (!this->emitFinishInitPop(ArrayFiller))
return false;
} else {
return false;
}
}

// Now copy the array filler from the local to the array.
if (!this->emitGetPtrLocal(TempOffset, E))
return false;
if (!this->emitSetArrayFillerPtr(TempOffset, E))
return false;
} else {
// Expand the filler expression.
if (ArrayFiller) {
for (; ElementIndex != Capacity; ++ElementIndex) {
if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
return false;
}
}
}

// assert(false);
return this->emitFinishInit(E);
}

Expand Down Expand Up @@ -4884,6 +4933,8 @@ Compiler<Emitter>::visitVarDecl(const VarDecl *VD, const Expr *Init,
}
// Local variables.
InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));
this->InitializingDecl = VD;
// DeclScope<Emitter> LocalScope(this, VD);

if (VarT) {
unsigned Offset = this->allocateLocalPrimitive(
Expand Down
Loading