Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type_of metafunction should dealias the type #93

Open
wants to merge 12 commits into
base: p2996
Choose a base branch
from
29 changes: 21 additions & 8 deletions clang/lib/AST/ExprConstantMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2075,14 +2075,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 @@ -2100,20 +2105,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 @@ -5741,12 +5747,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