Modify the qualified/unqualified getter for TypeOfType; NFC

Post-commit feedback observed that returning the TypeOfKind from the
type instead of a Boolean cleans up code using that interface.
This commit is contained in:
Aaron Ballman 2022-10-04 12:12:49 -04:00
parent 4409a83c29
commit 42ad305bdb
7 changed files with 35 additions and 33 deletions

View File

@ -4608,8 +4608,11 @@ protected:
public:
Expr *getUnderlyingExpr() const { return TOExpr; }
/// Returns true if this is a typeof_unqual type.
bool isUnqual() const { return TypeOfBits.IsUnqual; }
/// Returns the kind of 'typeof' type this is.
TypeOfKind getKind() const {
return TypeOfBits.IsUnqual ? TypeOfKind::Unqualified
: TypeOfKind::Qualified;
}
/// Remove a single level of sugar.
QualType desugar() const;
@ -4635,7 +4638,8 @@ public:
: TypeOfExprType(E, Kind), Context(Context) {}
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Context, getUnderlyingExpr(), isUnqual());
Profile(ID, Context, getUnderlyingExpr(),
getKind() == TypeOfKind::Unqualified);
}
static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
@ -4664,14 +4668,17 @@ public:
/// Remove a single level of sugar.
QualType desugar() const {
QualType QT = getUnmodifiedType();
return isUnqual() ? QT.getAtomicUnqualifiedType() : QT;
return TypeOfBits.IsUnqual ? QT.getAtomicUnqualifiedType() : QT;
}
/// Returns whether this type directly provides sugar.
bool isSugared() const { return true; }
/// Returns true if this is a typeof_unqual type.
bool isUnqual() const { return TypeOfBits.IsUnqual; }
/// Returns the kind of 'typeof' type this is.
TypeOfKind getKind() const {
return TypeOfBits.IsUnqual ? TypeOfKind::Unqualified
: TypeOfKind::Qualified;
}
static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
};

View File

@ -394,8 +394,7 @@ let Class = TypeOfExprType in {
}
def : Property<"kind", TypeOfKind> {
let Read = [{ node->isUnqual() ? TypeOfKind::Unqualified
: TypeOfKind::Qualified }];
let Read = [{ node->getKind() }];
}
def : Creator<[{
@ -409,8 +408,7 @@ let Class = TypeOfType in {
}
def : Property<"kind", TypeOfKind> {
let Read = [{ node->isUnqual() ? TypeOfKind::Unqualified
: TypeOfKind::Qualified }];
let Read = [{ node->getKind() }];
}
def : Creator<[{

View File

@ -12963,16 +12963,17 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
return QualType();
return Ctx.getTypedefType(CD, Ctx.getQualifiedType(Underlying));
}
case Type::TypeOf:
case Type::TypeOf: {
// The common sugar between two typeof expressions, where one is
// potentially a typeof_unqual and the other is not, we unify to the
// qualified type as that retains the most information along with the type.
// We only return a typeof_unqual type when both types are unqual types.
return Ctx.getTypeOfType(Ctx.getQualifiedType(Underlying),
cast<TypeOfType>(X)->isUnqual() &&
cast<TypeOfType>(Y)->isUnqual()
? TypeOfKind::Unqualified
: TypeOfKind::Qualified);
TypeOfKind Kind = TypeOfKind::Qualified;
if (cast<TypeOfType>(X)->getKind() == cast<TypeOfType>(Y)->getKind() &&
cast<TypeOfType>(X)->getKind() == TypeOfKind::Unqualified)
Kind = TypeOfKind::Unqualified;
return Ctx.getTypeOfType(Ctx.getQualifiedType(Underlying), Kind);
}
case Type::TypeOfExpr:
return QualType();

View File

@ -1374,9 +1374,7 @@ ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
if (!ToExprOrErr)
return ToExprOrErr.takeError();
return Importer.getToContext().getTypeOfExprType(
*ToExprOrErr,
T->isUnqual() ? TypeOfKind::Unqualified : TypeOfKind::Qualified);
return Importer.getToContext().getTypeOfExprType(*ToExprOrErr, T->getKind());
}
ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
@ -1384,7 +1382,7 @@ ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
if (!ToUnderlyingTypeOrErr)
return ToUnderlyingTypeOrErr.takeError();
return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr,
T->isUnqual() ? TypeOfKind::Unqualified : TypeOfKind::Qualified);
T->getKind());
}
ExpectedType ASTNodeImporter::VisitUsingType(const UsingType *T) {

View File

@ -3499,7 +3499,7 @@ bool TypeOfExprType::isSugared() const {
QualType TypeOfExprType::desugar() const {
if (isSugared()) {
QualType QT = getUnderlyingExpr()->getType();
return isUnqual() ? QT.getAtomicUnqualifiedType() : QT;
return TypeOfBits.IsUnqual ? QT.getAtomicUnqualifiedType() : QT;
}
return QualType(this, 0);
}

View File

@ -1110,7 +1110,8 @@ void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) {}
void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
raw_ostream &OS) {
OS << (T->isUnqual() ? "typeof_unqual " : "typeof ");
OS << (T->getKind() == TypeOfKind::Unqualified ? "typeof_unqual "
: "typeof ");
if (T->getUnderlyingExpr())
T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
spaceBeforePlaceHolder(OS);
@ -1120,7 +1121,8 @@ void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
raw_ostream &OS) {}
void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
OS << (T->isUnqual() ? "typeof_unqual(" : "typeof(");
OS << (T->getKind() == TypeOfKind::Unqualified ? "typeof_unqual("
: "typeof(");
print(T->getUnmodifiedType(), OS, StringRef());
OS << ')';
spaceBeforePlaceHolder(OS);

View File

@ -6200,12 +6200,10 @@ QualType TreeTransform<Derived>::TransformTypeOfExprType(TypeLocBuilder &TLB,
return QualType();
QualType Result = TL.getType();
bool IsUnqual = Result->getAs<TypeOfExprType>()->isUnqual();
if (getDerived().AlwaysRebuild() ||
E.get() != TL.getUnderlyingExpr()) {
Result = getDerived().RebuildTypeOfExprType(
E.get(), TL.getTypeofLoc(),
IsUnqual ? TypeOfKind::Unqualified : TypeOfKind::Qualified);
TypeOfKind Kind = Result->getAs<TypeOfExprType>()->getKind();
if (getDerived().AlwaysRebuild() || E.get() != TL.getUnderlyingExpr()) {
Result =
getDerived().RebuildTypeOfExprType(E.get(), TL.getTypeofLoc(), Kind);
if (Result.isNull())
return QualType();
}
@ -6227,11 +6225,9 @@ QualType TreeTransform<Derived>::TransformTypeOfType(TypeLocBuilder &TLB,
return QualType();
QualType Result = TL.getType();
bool IsUnqual = Result->getAs<TypeOfType>()->isUnqual();
TypeOfKind Kind = Result->getAs<TypeOfType>()->getKind();
if (getDerived().AlwaysRebuild() || New_Under_TI != Old_Under_TI) {
Result = getDerived().RebuildTypeOfType(New_Under_TI->getType(),
IsUnqual ? TypeOfKind::Unqualified
: TypeOfKind::Qualified);
Result = getDerived().RebuildTypeOfType(New_Under_TI->getType(), Kind);
if (Result.isNull())
return QualType();
}