From 42ad305bdbb87d567d4d365ec5ede8be4d2a4210 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Tue, 4 Oct 2022 12:12:49 -0400 Subject: [PATCH] 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. --- clang/include/clang/AST/Type.h | 19 +++++++++++++------ clang/include/clang/AST/TypeProperties.td | 6 ++---- clang/lib/AST/ASTContext.cpp | 13 +++++++------ clang/lib/AST/ASTImporter.cpp | 6 ++---- clang/lib/AST/Type.cpp | 2 +- clang/lib/AST/TypePrinter.cpp | 6 ++++-- clang/lib/Sema/TreeTransform.h | 16 ++++++---------- 7 files changed, 35 insertions(+), 33 deletions(-) diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index b7ef891..fbf65e5 100644 --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -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; } }; diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td index 9634d2b..ef7ed6e 100644 --- a/clang/include/clang/AST/TypeProperties.td +++ b/clang/include/clang/AST/TypeProperties.td @@ -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<[{ diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index fa7d526..d609fe8 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -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(X)->isUnqual() && - cast(Y)->isUnqual() - ? TypeOfKind::Unqualified - : TypeOfKind::Qualified); + TypeOfKind Kind = TypeOfKind::Qualified; + if (cast(X)->getKind() == cast(Y)->getKind() && + cast(X)->getKind() == TypeOfKind::Unqualified) + Kind = TypeOfKind::Unqualified; + return Ctx.getTypeOfType(Ctx.getQualifiedType(Underlying), Kind); + } case Type::TypeOfExpr: return QualType(); diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index 63b258c..988fa63 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -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) { diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index ccad2f3..bd5fe3d 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -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); } diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index c033b35..8b0159b 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -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); diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 0ae3c77..eb7be2c 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6200,12 +6200,10 @@ QualType TreeTransform::TransformTypeOfExprType(TypeLocBuilder &TLB, return QualType(); QualType Result = TL.getType(); - bool IsUnqual = Result->getAs()->isUnqual(); - if (getDerived().AlwaysRebuild() || - E.get() != TL.getUnderlyingExpr()) { - Result = getDerived().RebuildTypeOfExprType( - E.get(), TL.getTypeofLoc(), - IsUnqual ? TypeOfKind::Unqualified : TypeOfKind::Qualified); + TypeOfKind Kind = Result->getAs()->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::TransformTypeOfType(TypeLocBuilder &TLB, return QualType(); QualType Result = TL.getType(); - bool IsUnqual = Result->getAs()->isUnqual(); + TypeOfKind Kind = Result->getAs()->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(); } -- 2.7.4