Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp =================================================================== --- clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -328,7 +328,10 @@ template bool ByteCodeExprGen::VisitArrayInitIndexExpr( const ArrayInitIndexExpr *E) { - assert(ArrayIndex); + // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated + // stand-alone, e.g. via EvaluateAsInt(). + if (!ArrayIndex) + return false; return this->emitConstUint64(*ArrayIndex, E); } @@ -604,9 +607,15 @@ if (const auto *InitList = dyn_cast(Initializer)) { unsigned ElementIndex = 0; for (const Expr *Init : InitList->inits()) { - QualType InitType = Init->getType(); - - if (InitType->isArrayType()) { + if (Optional T = classify(Init->getType())) { + // Visit the primitive element like normal. + if (!this->emitDupPtr(Init)) + return false; + if (!this->visit(Init)) + return false; + if (!this->emitInitElem(*T, ElementIndex, Init)) + return false; + } else { // Advance the pointer currently on the stack to the given // dimension and narrow(). if (!this->emitDupPtr(Init)) @@ -617,21 +626,12 @@ return false; if (!this->emitNarrowPtr(Init)) return false; - if (!visitArrayInitializer(Init)) + + if (!visitInitializer(Init)) return false; + } if (!this->emitPopPtr(Init)) return false; - } else if (Optional T = classify(InitType)) { - // Visit the primitive element like normal. - if (!this->emitDupPtr(Init)) - return false; - if (!this->visit(Init)) - return false; - if (!this->emitInitElemPop(*T, ElementIndex, Init)) - return false; - } else { - assert(false && "Unhandled type in array initializer initlist"); - } ++ElementIndex; } @@ -646,19 +646,34 @@ size_t Size = AILE->getArraySize().getZExtValue(); Optional ElemT = classify(SubExpr->getType()); - if (!ElemT) - return false; - + // So, every iteration, we execute an assignment here + // where the LHS is on the stack (the target array) + // and the RHS is our SubExpr. for (size_t I = 0; I != Size; ++I) { ArrayIndex = I; - if (!this->emitDupPtr(SubExpr)) - return false; - if (!this->visit(SubExpr)) + if (!this->emitDupPtr(SubExpr)) // LHS return false; - if (!this->emitInitElem(*ElemT, I, Initializer)) - return false; + if (ElemT) { + if (!this->visit(SubExpr)) + return false; + if (!this->emitInitElem(*ElemT, I, Initializer)) + return false; + } else { + // Narrow to our array element and recurse into visitInitializer() + if (!this->emitConstSint32(I, SubExpr)) + return false; + + if (!this->emitAddOffsetSint32(SubExpr)) + return false; + + if (!this->emitNarrowPtr(SubExpr)) + return false; + + if (!visitInitializer(SubExpr)) + return false; + } if (!this->emitPopPtr(Initializer)) return false; Index: clang/lib/AST/Interp/Interp.h =================================================================== --- clang/lib/AST/Interp/Interp.h +++ clang/lib/AST/Interp/Interp.h @@ -494,7 +494,7 @@ /// 1) Pops the value from the stack /// 2) Pops a pointer from the stack -/// 3) Writes the value to field I of the pointer +/// 3) Pushes the value to field I of the pointer on the stack template ::T> bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) { const T &Value = S.Stk.pop(); @@ -548,6 +548,8 @@ return true; } +/// 1) Pops a Pointer from the stack +/// 2) Pushes Pointer.atField(Off) on the stack inline bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) { const Pointer &Ptr = S.Stk.pop(); if (!CheckNull(S, OpPC, Ptr, CSK_Field)) Index: clang/lib/AST/Interp/Pointer.cpp =================================================================== --- clang/lib/AST/Interp/Pointer.cpp +++ clang/lib/AST/Interp/Pointer.cpp @@ -37,8 +37,11 @@ Pointer::Pointer(Block *Pointee, unsigned Base, unsigned Offset) : Pointee(Pointee), Base(Base), Offset(Offset) { assert((Base == RootPtrMark || Base % alignof(void *) == 0) && "wrong base"); - if (Pointee) + if (Pointee) { Pointee->addPointer(this); + assert(Base < Pointee->getSize()); + assert(Offset < Pointee->getSize()); + } } Pointer::~Pointer() { @@ -151,6 +154,7 @@ bool Pointer::isInitialized() const { assert(Pointee && "Cannot check if null pointer was initialized"); Descriptor *Desc = getFieldDesc(); + assert(Desc); if (Desc->isPrimitiveArray()) { if (Pointee->IsStatic) return true; @@ -171,6 +175,7 @@ assert(Pointee && "Cannot initialize null pointer"); Descriptor *Desc = getFieldDesc(); + assert(Desc); if (Desc->isArray()) { if (Desc->isPrimitiveArray() && !Pointee->IsStatic) { // Primitive array initializer. Index: clang/test/AST/Interp/records.cpp =================================================================== --- clang/test/AST/Interp/records.cpp +++ clang/test/AST/Interp/records.cpp @@ -140,3 +140,24 @@ return c.a; } static_assert(locals() == 10, ""); + +struct FourBoolPairs { + BoolPair v[4] = { + {false, false}, + {false, true}, + {true, false}, + {true, true }, + }; +}; +// Init +constexpr FourBoolPairs LT; +// Copy ctor +constexpr FourBoolPairs LT2 = LT; +// FIXME: The copy constructor call above +// works, but APValue we generate for it is +// not sufficiently correct, so the lvalue-to-rvalue +// conversion in ExprConstant.c runs into an assertion. +//static_assert(LT2.v[0].first == false, ""); +//static_assert(LT2.v[0].second == false, ""); +//static_assert(LT2.v[2].first == true, ""); +//static_assert(LT2.v[2].second == false, "");