Index: clang/lib/AST/Interp/ByteCodeExprGen.h =================================================================== --- clang/lib/AST/Interp/ByteCodeExprGen.h +++ clang/lib/AST/Interp/ByteCodeExprGen.h @@ -253,6 +253,15 @@ return (*InitFn)(); } + /// Returns the CXXRecordDecl for the type of the given expression, + /// or nullptr if no such decl exists. + const CXXRecordDecl * getRecordDecl(const Expr *E) const { + QualType T = E->getType(); + if (const auto *RD = T->getPointeeCXXRecordDecl()) + return RD; + return T->getAsCXXRecordDecl(); + } + protected: /// Variable to storage mapping. llvm::DenseMap Locals; Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp =================================================================== --- clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -107,6 +107,21 @@ }); } + case CK_UncheckedDerivedToBase: { + if (!this->visit(SubExpr)) + return false; + + const CXXRecordDecl *FromDecl = getRecordDecl(SubExpr); + assert(FromDecl); + const CXXRecordDecl *ToDecl = getRecordDecl(CE); + assert(ToDecl); + const Record *R = getRecord(FromDecl); + const Record::Base *ToBase = R->getBase(ToDecl); + assert(ToBase); + + return this->emitGetPtrBase(ToBase->Offset, CE); + } + case CK_ArrayToPointerDecay: case CK_AtomicToNonAtomic: case CK_ConstructorConversion: Index: clang/lib/AST/Interp/ByteCodeStmtGen.cpp =================================================================== --- clang/lib/AST/Interp/ByteCodeStmtGen.cpp +++ clang/lib/AST/Interp/ByteCodeStmtGen.cpp @@ -100,31 +100,45 @@ const Record *R = this->getRecord(RD); for (const auto *Init : Ctor->inits()) { - const FieldDecl *Member = Init->getMember(); const Expr *InitExpr = Init->getInit(); - const Record::Field *F = R->getField(Member); - - if (Optional T = this->classify(InitExpr->getType())) { - if (!this->emitThis(InitExpr)) - return false; - - if (!this->visit(InitExpr)) - return false; - - if (!this->emitInitField(*T, F->Offset, InitExpr)) + if (const FieldDecl *Member = Init->getMember()) { + const Record::Field *F = R->getField(Member); + + if (Optional T = this->classify(InitExpr->getType())) { + if (!this->emitThis(InitExpr)) + return false; + + if (!this->visit(InitExpr)) + return false; + + if (!this->emitInitField(*T, F->Offset, InitExpr)) + return false; + } else { + // Non-primitive case. Get a pointer to the field-to-initialize + // on the stack and call visitInitialzer() for it. + if (!this->emitThis(InitExpr)) + return false; + + if (!this->emitGetPtrField(F->Offset, InitExpr)) + return false; + + if (!this->visitInitializer(InitExpr)) + return false; + + if (!this->emitPopPtr(InitExpr)) + return false; + } + } else if (const Type *Base = Init->getBaseClass()) { + // Base class initializer. + // Get This Base and call initializer on it. + auto *BaseDecl = Base->getAsCXXRecordDecl(); + assert(BaseDecl); + const Record::Base *B = R->getBase(BaseDecl); + assert(B); + if (!this->emitGetPtrThisBase(B->Offset, InitExpr)) return false; - } else { - // Non-primitive case. Get a pointer to the field-to-initialize - // on the stack and call visitInitialzer() for it. - if (!this->emitThis(InitExpr)) - return false; - - if (!this->emitGetPtrField(F->Offset, InitExpr)) - return false; - if (!this->visitInitializer(InitExpr)) return false; - if (!this->emitPopPtr(InitExpr)) return false; } Index: clang/test/AST/Interp/records.cpp =================================================================== --- clang/test/AST/Interp/records.cpp +++ clang/test/AST/Interp/records.cpp @@ -1,9 +1,6 @@ // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s // RUN: %clang_cc1 -verify=ref %s -// ref-no-diagnostics -// expected-no-diagnostics - struct BoolPair { bool first; bool second; @@ -157,3 +154,57 @@ static_assert(LT2.v[0].second == false, ""); static_assert(LT2.v[2].first == true, ""); static_assert(LT2.v[2].second == false, ""); + +class Base { +public: + int i; + constexpr Base() : i(10) {} + constexpr Base(int i) : i(i) {} +}; + +class A : public Base { +public: + constexpr A() : Base(100) {} + constexpr A(int a) : Base(a) {} +}; +constexpr A a{}; +static_assert(a.i == 100, ""); +constexpr A a2{12}; +static_assert(a2.i == 12, ""); +static_assert(a2.i == 200, ""); // ref-error {{static assertion failed}} \ + // ref-note {{evaluates to '12 == 200'}} \ + // expected-error {{static assertion failed}} \ + // expected-note {{evaluates to '12 == 200'}} + +namespace MI { + class A { + public: + int a; + constexpr A(int a) : a(a) {} + }; + + class B { + public: + int b; + constexpr B(int b) : b(b) {} + }; + + class C : public A, public B { + public: + constexpr C() : A(10), B(20) {} + }; + constexpr C c = {}; + static_assert(c.a == 10); + static_assert(c.b == 20); + + + class D : private A, private B { + public: + constexpr D() : A(20), B(30) {} + constexpr int getA() const { return a; } + constexpr int getB() const { return b; } + }; + constexpr D d = {}; + static_assert(d.getA() == 20); + static_assert(d.getB() == 30); +};