Skip to content

Commit 82ebef0

Browse files
committed
Correct behavior for default mem-initializers used by inherited ctors.
1 parent cba0278 commit 82ebef0

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

clang/lib/AST/ExprConstantMeta.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,9 @@ static bool getParameterName(ParmVarDecl *PVD, std::string &Out) {
989989
StringRef FirstNameSeen = PVD->getName();
990990
unsigned ParamIdx = PVD->getFunctionScopeIndex();
991991

992+
// TODO(P2996): This will crash if we're in the trailing requires-clause of
993+
// a function declaration, since the DeclContext is not the function but the
994+
// TranslationUnitDecl.
992995
FunctionDecl *FD = cast<FunctionDecl>(PVD->getDeclContext());
993996
FD = FD->getMostRecentDecl();
994997

@@ -1019,6 +1022,9 @@ static bool getParameterName(ParmVarDecl *PVD, std::string &Out) {
10191022
}
10201023

10211024
static ParmVarDecl *getMostRecentParmVarDecl(ParmVarDecl *PVD) {
1025+
// TODO(P2996): This will crash if we're in the trailing requires-clause of
1026+
// a function declaration, since the DeclContext is not the function but the
1027+
// TranslationUnitDecl.
10221028
FunctionDecl *FD = cast<FunctionDecl>(PVD->getDeclContext());
10231029
FD = FD->getMostRecentDecl();
10241030
return FD->getParamDecl(PVD->getFunctionScopeIndex());
@@ -5570,14 +5576,22 @@ bool current_access_context(APValue &Result, ASTContext &C, MetaActions &Meta,
55705576
SourceRange Range, ArrayRef<Expr *> Args,
55715577
Decl *ContainingDecl) {
55725578
assert(ResultTy == C.MetaInfoTy);
5579+
Decl *Ctx = nullptr;
55735580

55745581
StackLocationExpr *SLE = StackLocationExpr::Create(C, SourceRange(), 1);
5575-
if (!Evaluator(Result, SLE, true) || !Result.isReflection())
5582+
if (!Evaluator(Result, SLE, true) || !Result.isReflectedDecl())
55765583
return true;
5577-
else if (Result.getReflectedDecl() != nullptr)
5578-
return false;
5584+
else if (Ctx = Result.getReflectedDecl(); !Ctx)
5585+
Ctx = Meta.CurrentCtx();
5586+
5587+
if (auto *Ctor = dyn_cast<CXXConstructorDecl>(Ctx);
5588+
Ctor && Ctor->isInheritingConstructor())
5589+
Ctx = cast<Decl>(Ctor->getDeclContext());
55795590

5580-
return SetAndSucceed(Result, makeReflection(Meta.CurrentCtx()));
5591+
if (auto *RD = dyn_cast<CXXRecordDecl>(Ctx))
5592+
return SetAndSucceed(Result,
5593+
makeReflection(QualType(RD->getTypeForDecl(), 0)));
5594+
return SetAndSucceed(Result, makeReflection(Ctx));
55815595
}
55825596

55835597
bool is_accessible(APValue &Result, ASTContext &C, MetaActions &Meta,

clang/lib/Sema/SemaReflect.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/Sema/Template.h"
2727
#include "clang/Sema/TemplateDeduction.h"
2828
#include "llvm/Support/raw_ostream.h"
29+
#include <iostream>
2930

3031
using namespace clang;
3132
using namespace sema;

libcxx/test/std/experimental/reflection/member-visibility.pass.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,22 @@ static_assert(is_accessible(^^a,
339339
std::meta::access_context::unprivileged()));
340340
} // namespace anonymous_structs_unions
341341

342+
// ========================
343+
// default_mem_initializers
344+
// ========================
345+
346+
namespace default_mem_initializers {
347+
struct A {
348+
int a;
349+
consteval A(int p) : a(p) {}
350+
};
351+
struct B : A {
352+
using A::A;
353+
consteval B(int p, int q) : A(p * q) {}
354+
std::meta::info s = std::meta::access_context::current().scope();
355+
};
356+
static_assert(B(1).s == ^^B);
357+
static_assert(is_constructor(B{1, 2}.s) && parent_of(B{1, 2}.s) == ^^B);
358+
} // namespace default_mem_initializers
359+
342360
int main() { }

0 commit comments

Comments
 (0)