Skip to content

Commit

Permalink
type_of metafunction should dealias the type (#93)
Browse files Browse the repository at this point in the history
Fixes issue #91.
  • Loading branch information
changkhothuychung authored Oct 31, 2024
1 parent 5a5e1a4 commit cfdc5e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
29 changes: 21 additions & 8 deletions clang/lib/AST/ExprConstantMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2069,14 +2069,19 @@ bool type_of(APValue &Result, ASTContext &C, MetaActions &Meta,

switch (RV.getReflectionKind()) {
case ReflectionKind::Null:
case ReflectionKind::Type:
case ReflectionKind::Type: {
QualType QT = desugarType(RV.getTypeOfReflectedResult(C),
/*UnwrapAliases=*/ true, /*DropCV=*/false,
/*DropRefs=*/false);
return SetAndSucceed(Result, makeReflection(QT));
}
case ReflectionKind::Template:
case ReflectionKind::Namespace:
return Diagnoser(Range.getBegin(), diag::metafn_no_associated_property)
<< DescriptionOf(RV) << 0 << Range;
case ReflectionKind::Object: {
QualType QT = desugarType(RV.getTypeOfReflectedResult(C),
/*UnwrapAliases=*/false, /*DropCV=*/false,
/*UnwrapAliases=*/ true, /*DropCV=*/false,
/*DropRefs=*/false);
return SetAndSucceed(Result, makeReflection(QT));
}
Expand All @@ -2094,20 +2099,21 @@ bool type_of(APValue &Result, ASTContext &C, MetaActions &Meta,

bool UnwrapAliases = isa<ParmVarDecl>(VD) || isa<BindingDecl>(VD);
bool DropCV = isa<ParmVarDecl>(VD);
QualType QT = desugarType(VD->getType(), UnwrapAliases, DropCV,
QualType QT = desugarType(VD->getType(),
/*UnwrapAliases=*/ true, DropCV,
/*DropRefs=*/false);
return SetAndSucceed(Result, makeReflection(QT));
}
case ReflectionKind::BaseSpecifier: {
QualType QT = RV.getReflectedBaseSpecifier()->getType();
QT = desugarType(QT, /*UnwrapAliases=*/false, /*DropCV=*/false,
QT = desugarType(QT, /*UnwrapAliases=*/true, /*DropCV=*/false,
/*DropRefs=*/false);
return SetAndSucceed(Result, makeReflection(QT));
}
case ReflectionKind::DataMemberSpec:
{
QualType QT = RV.getReflectedDataMemberSpec()->Ty;
QT = desugarType(QT, /*UnwrapAliases=*/false, /*DropCV=*/false,
QT = desugarType(QT, /*UnwrapAliases=*/true, /*DropCV=*/false,
/*DropRefs=*/false);
return SetAndSucceed(Result, makeReflection(QT));
}
Expand Down Expand Up @@ -5618,12 +5624,19 @@ bool return_type_of(APValue &Result, ASTContext &C, MetaActions &Meta,
return true;

switch (RV.getReflectionKind()) {
case ReflectionKind::Type:
if (auto *FPT = dyn_cast<FunctionProtoType>(RV.getReflectedType()))
return SetAndSucceed(Result, makeReflection(FPT->getReturnType()));
case ReflectionKind::Type: {
if (auto *FPT = dyn_cast<FunctionProtoType>(RV.getReflectedType())) {
QualType QT =
desugarType(FPT->getReturnType(), /*UnwrapAliases=*/ true, /*DropCV=*/false,
/*DropRefs=*/false);
return SetAndSucceed(Result, makeReflection(QT));
}

return Diagnoser(Range.getBegin(), diag::metafn_cannot_introspect_type)
<< 3 << 2 << Range;
}


case ReflectionKind::Declaration:
if (auto *FD = dyn_cast<FunctionDecl>(RV.getReflectedDecl());
FD && !isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ extern int_alias ia;
extern decltype(int_alias{}) dtia;

static_assert(type_of(^^i) == ^^int);
static_assert(type_of(^^ia) == ^^int_alias);
static_assert(type_of(^^ia) != ^^int);
static_assert(type_of(^^ia) != ^^int_alias);
static_assert(type_of(^^ia) == ^^int);
static_assert(type_of(^^dtia) == ^^int);
static_assert(type_of(^^dtia) != ^^int_alias);
static_assert(^^int_alias != ^^int);
Expand All @@ -43,7 +43,7 @@ constexpr auto a = data_member_spec(^^int, {});
constexpr auto b = data_member_spec(^^int_alias, {});

static_assert(type_of(a) == ^^int);
static_assert(type_of(b) == ^^int_alias);
static_assert(type_of(b) != ^^int_alias);
} // namespace expression_type

// ================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ struct S{};

using Alias1 = int;
constexpr Alias1 a1 = 3;
static_assert(type_of(^^a1) == ^^const Alias1);
static_assert(type_of(^^a1) != ^^const Alias1);
static_assert(type_of(^^a1) == ^^const int);
static_assert(type_of(value_of(^^a1)) == ^^int);

using Alias2 = S;
constexpr Alias2 a2 {};
static_assert(type_of(^^a2) == ^^const Alias2);
static_assert(type_of(^^a2) != ^^const Alias2);
static_assert(type_of(^^a2) == ^^const S);
static_assert(type_of(value_of(^^a2)) == ^^const S);

constexpr const int &ref = a1;
Expand Down

0 comments on commit cfdc5e2

Please sign in to comment.