Skip to content

Commit 4ad2ada

Browse files
committed
[clang][NFC] Refactor ElaboratedTypeKeyword
This patch moves ElaboratedTypeKeyword before `Type` definition so that the enum is complete where bit-field for it is declared. It also converts it to scoped enum and removes `ETK_` prefix.
1 parent df4cf0b commit 4ad2ada

22 files changed

+193
-145
lines changed

clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ matchEnableIfSpecializationImplTypename(TypeLoc TheType) {
5353
if (const auto Dep = TheType.getAs<DependentNameTypeLoc>()) {
5454
const IdentifierInfo *Identifier = Dep.getTypePtr()->getIdentifier();
5555
if (!Identifier || Identifier->getName() != "type" ||
56-
Dep.getTypePtr()->getKeyword() != ETK_Typename) {
56+
Dep.getTypePtr()->getKeyword() != ElaboratedTypeKeyword::Typename) {
5757
return std::nullopt;
5858
}
5959
TheType = Dep.getQualifierLoc().getTypeLoc();
@@ -105,7 +105,7 @@ matchEnableIfSpecializationImplTrait(TypeLoc TheType) {
105105
if (const auto *AliasedType =
106106
dyn_cast<DependentNameType>(Specialization->getAliasedType())) {
107107
if (AliasedType->getIdentifier()->getName() != "type" ||
108-
AliasedType->getKeyword() != ETK_Typename) {
108+
AliasedType->getKeyword() != ElaboratedTypeKeyword::Typename) {
109109
return std::nullopt;
110110
}
111111
} else {

clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
9898
return false;
9999
const auto *T = TL.getTypePtr();
100100
return TraverseTypeLoc(TL.getNamedTypeLoc(),
101-
T->getKeyword() != ETK_None || T->getQualifier());
101+
T->getKeyword() != ElaboratedTypeKeyword::None ||
102+
T->getQualifier());
102103
}
103104

104105
bool VisitDeclRefExpr(DeclRefExpr *S) {

clang/include/clang/AST/Type.h

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,32 @@ enum class AutoTypeKeyword {
15751575
/// 'static' is only allowed on function parameters.
15761576
enum class ArraySizeModifier { Normal, Static, Star };
15771577

1578+
/// The elaboration keyword that precedes a qualified type name or
1579+
/// introduces an elaborated-type-specifier.
1580+
enum class ElaboratedTypeKeyword {
1581+
/// The "struct" keyword introduces the elaborated-type-specifier.
1582+
Struct,
1583+
1584+
/// The "__interface" keyword introduces the elaborated-type-specifier.
1585+
Interface,
1586+
1587+
/// The "union" keyword introduces the elaborated-type-specifier.
1588+
Union,
1589+
1590+
/// The "class" keyword introduces the elaborated-type-specifier.
1591+
Class,
1592+
1593+
/// The "enum" keyword introduces the elaborated-type-specifier.
1594+
Enum,
1595+
1596+
/// The "typename" keyword precedes the qualified type name, e.g.,
1597+
/// \c typename T::type.
1598+
Typename,
1599+
1600+
/// No keyword precedes the qualified type name.
1601+
None
1602+
};
1603+
15781604
/// The base class of the type hierarchy.
15791605
///
15801606
/// A central concept with types is that each type always has a canonical
@@ -5659,32 +5685,6 @@ enum TagTypeKind {
56595685
TTK_Enum
56605686
};
56615687

5662-
/// The elaboration keyword that precedes a qualified type name or
5663-
/// introduces an elaborated-type-specifier.
5664-
enum ElaboratedTypeKeyword {
5665-
/// The "struct" keyword introduces the elaborated-type-specifier.
5666-
ETK_Struct,
5667-
5668-
/// The "__interface" keyword introduces the elaborated-type-specifier.
5669-
ETK_Interface,
5670-
5671-
/// The "union" keyword introduces the elaborated-type-specifier.
5672-
ETK_Union,
5673-
5674-
/// The "class" keyword introduces the elaborated-type-specifier.
5675-
ETK_Class,
5676-
5677-
/// The "enum" keyword introduces the elaborated-type-specifier.
5678-
ETK_Enum,
5679-
5680-
/// The "typename" keyword precedes the qualified type name, e.g.,
5681-
/// \c typename T::type.
5682-
ETK_Typename,
5683-
5684-
/// No keyword precedes the qualified type name.
5685-
ETK_None
5686-
};
5687-
56885688
/// A helper class for Type nodes having an ElaboratedTypeKeyword.
56895689
/// The keyword in stored in the free bits of the base class.
56905690
/// Also provides a few static helpers for converting and printing
@@ -5694,7 +5694,7 @@ class TypeWithKeyword : public Type {
56945694
TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc,
56955695
QualType Canonical, TypeDependence Dependence)
56965696
: Type(tc, Canonical, Dependence) {
5697-
TypeWithKeywordBits.Keyword = Keyword;
5697+
TypeWithKeywordBits.Keyword = llvm::to_underlying(Keyword);
56985698
}
56995699

57005700
public:
@@ -5800,7 +5800,7 @@ class ElaboratedType final
58005800
static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
58015801
NestedNameSpecifier *NNS, QualType NamedType,
58025802
TagDecl *OwnedTagDecl) {
5803-
ID.AddInteger(Keyword);
5803+
ID.AddInteger(llvm::to_underlying(Keyword));
58045804
ID.AddPointer(NNS);
58055805
NamedType.Profile(ID);
58065806
ID.AddPointer(OwnedTagDecl);
@@ -5859,7 +5859,7 @@ class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode {
58595859

58605860
static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
58615861
NestedNameSpecifier *NNS, const IdentifierInfo *Name) {
5862-
ID.AddInteger(Keyword);
5862+
ID.AddInteger(llvm::to_underlying(Keyword));
58635863
ID.AddPointer(NNS);
58645864
ID.AddPointer(Name);
58655865
}

clang/include/clang/AST/TypeLoc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ class ElaboratedTypeLoc : public ConcreteTypeLoc<UnqualTypeLoc,
22942294
QualType getInnerType() const { return getTypePtr()->getNamedType(); }
22952295

22962296
bool isEmpty() const {
2297-
return getTypePtr()->getKeyword() == ElaboratedTypeKeyword::ETK_None &&
2297+
return getTypePtr()->getKeyword() == ElaboratedTypeKeyword::None &&
22982298
!getTypePtr()->getQualifier();
22992299
}
23002300

clang/lib/AST/ASTContext.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5122,7 +5122,8 @@ ASTContext::getDependentTemplateSpecializationType(
51225122
NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
51235123

51245124
ElaboratedTypeKeyword CanonKeyword = Keyword;
5125-
if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
5125+
if (Keyword == ElaboratedTypeKeyword::None)
5126+
CanonKeyword = ElaboratedTypeKeyword::Typename;
51265127

51275128
bool AnyNonCanonArgs = false;
51285129
auto CanonArgs =
@@ -12502,7 +12503,7 @@ static auto getCommonTemplateArguments(ASTContext &Ctx,
1250212503
template <class T>
1250312504
static ElaboratedTypeKeyword getCommonTypeKeyword(const T *X, const T *Y) {
1250412505
return X->getKeyword() == Y->getKeyword() ? X->getKeyword()
12505-
: ElaboratedTypeKeyword::ETK_None;
12506+
: ElaboratedTypeKeyword::None;
1250612507
}
1250712508

1250812509
template <class T>

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,9 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
11731173
case Type::Elaborated: {
11741174
const auto *Elab1 = cast<ElaboratedType>(T1);
11751175
const auto *Elab2 = cast<ElaboratedType>(T2);
1176-
// CHECKME: what if a keyword is ETK_None or ETK_typename ?
1176+
// CHECKME: what if a keyword is ElaboratedTypeKeyword::None or
1177+
// ElaboratedTypeKeyword::Typename
1178+
// ?
11771179
if (Elab1->getKeyword() != Elab2->getKeyword())
11781180
return false;
11791181
if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(),

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4209,20 +4209,20 @@ void CXXNameMangler::mangleType(const DependentNameType *T) {
42094209
// ::= Te <name> # dependent elaborated type specifier using
42104210
// # 'enum'
42114211
switch (T->getKeyword()) {
4212-
case ETK_None:
4213-
case ETK_Typename:
4214-
break;
4215-
case ETK_Struct:
4216-
case ETK_Class:
4217-
case ETK_Interface:
4218-
Out << "Ts";
4219-
break;
4220-
case ETK_Union:
4221-
Out << "Tu";
4222-
break;
4223-
case ETK_Enum:
4224-
Out << "Te";
4225-
break;
4212+
case ElaboratedTypeKeyword::None:
4213+
case ElaboratedTypeKeyword::Typename:
4214+
break;
4215+
case ElaboratedTypeKeyword::Struct:
4216+
case ElaboratedTypeKeyword::Class:
4217+
case ElaboratedTypeKeyword::Interface:
4218+
Out << "Ts";
4219+
break;
4220+
case ElaboratedTypeKeyword::Union:
4221+
Out << "Tu";
4222+
break;
4223+
case ElaboratedTypeKeyword::Enum:
4224+
Out << "Te";
4225+
break;
42264226
}
42274227
// Typename types are always nested
42284228
Out << 'N';

clang/lib/AST/ODRHash.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
11811181
}
11821182

11831183
void VisitTypeWithKeyword(const TypeWithKeyword *T) {
1184-
ID.AddInteger(T->getKeyword());
1184+
ID.AddInteger(llvm::to_underlying(T->getKeyword()));
11851185
VisitType(T);
11861186
};
11871187

clang/lib/AST/QualTypeNames.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
440440
// elaborated type.
441441
Qualifiers PrefixQualifiers = QT.getLocalQualifiers();
442442
QT = QualType(QT.getTypePtr(), 0);
443-
ElaboratedTypeKeyword Keyword = ETK_None;
443+
ElaboratedTypeKeyword Keyword = ElaboratedTypeKeyword::None;
444444
if (const auto *ETypeInput = dyn_cast<ElaboratedType>(QT.getTypePtr())) {
445445
QT = ETypeInput->getNamedType();
446446
assert(!QT.hasLocalQualifiers());
@@ -471,7 +471,7 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
471471
Ctx, QT.getTypePtr(), WithGlobalNsPrefix);
472472
QT = QualType(TypePtr, 0);
473473
}
474-
if (Prefix || Keyword != ETK_None) {
474+
if (Prefix || Keyword != ElaboratedTypeKeyword::None) {
475475
QT = Ctx.getElaboratedType(Keyword, Prefix, QT);
476476
}
477477
QT = Ctx.getQualifiedType(QT, PrefixQualifiers);

clang/lib/AST/Type.cpp

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,13 +3016,20 @@ bool Type::isSpecifierType() const {
30163016
ElaboratedTypeKeyword
30173017
TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
30183018
switch (TypeSpec) {
3019-
default: return ETK_None;
3020-
case TST_typename: return ETK_Typename;
3021-
case TST_class: return ETK_Class;
3022-
case TST_struct: return ETK_Struct;
3023-
case TST_interface: return ETK_Interface;
3024-
case TST_union: return ETK_Union;
3025-
case TST_enum: return ETK_Enum;
3019+
default:
3020+
return ElaboratedTypeKeyword::None;
3021+
case TST_typename:
3022+
return ElaboratedTypeKeyword::Typename;
3023+
case TST_class:
3024+
return ElaboratedTypeKeyword::Class;
3025+
case TST_struct:
3026+
return ElaboratedTypeKeyword::Struct;
3027+
case TST_interface:
3028+
return ElaboratedTypeKeyword::Interface;
3029+
case TST_union:
3030+
return ElaboratedTypeKeyword::Union;
3031+
case TST_enum:
3032+
return ElaboratedTypeKeyword::Enum;
30263033
}
30273034
}
30283035

@@ -3042,25 +3049,35 @@ TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
30423049
ElaboratedTypeKeyword
30433050
TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
30443051
switch (Kind) {
3045-
case TTK_Class: return ETK_Class;
3046-
case TTK_Struct: return ETK_Struct;
3047-
case TTK_Interface: return ETK_Interface;
3048-
case TTK_Union: return ETK_Union;
3049-
case TTK_Enum: return ETK_Enum;
3052+
case TTK_Class:
3053+
return ElaboratedTypeKeyword::Class;
3054+
case TTK_Struct:
3055+
return ElaboratedTypeKeyword::Struct;
3056+
case TTK_Interface:
3057+
return ElaboratedTypeKeyword::Interface;
3058+
case TTK_Union:
3059+
return ElaboratedTypeKeyword::Union;
3060+
case TTK_Enum:
3061+
return ElaboratedTypeKeyword::Enum;
30503062
}
30513063
llvm_unreachable("Unknown tag type kind.");
30523064
}
30533065

30543066
TagTypeKind
30553067
TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
30563068
switch (Keyword) {
3057-
case ETK_Class: return TTK_Class;
3058-
case ETK_Struct: return TTK_Struct;
3059-
case ETK_Interface: return TTK_Interface;
3060-
case ETK_Union: return TTK_Union;
3061-
case ETK_Enum: return TTK_Enum;
3062-
case ETK_None: // Fall through.
3063-
case ETK_Typename:
3069+
case ElaboratedTypeKeyword::Class:
3070+
return TTK_Class;
3071+
case ElaboratedTypeKeyword::Struct:
3072+
return TTK_Struct;
3073+
case ElaboratedTypeKeyword::Interface:
3074+
return TTK_Interface;
3075+
case ElaboratedTypeKeyword::Union:
3076+
return TTK_Union;
3077+
case ElaboratedTypeKeyword::Enum:
3078+
return TTK_Enum;
3079+
case ElaboratedTypeKeyword::None: // Fall through.
3080+
case ElaboratedTypeKeyword::Typename:
30643081
llvm_unreachable("Elaborated type keyword is not a tag type kind.");
30653082
}
30663083
llvm_unreachable("Unknown elaborated type keyword.");
@@ -3069,28 +3086,35 @@ TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
30693086
bool
30703087
TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
30713088
switch (Keyword) {
3072-
case ETK_None:
3073-
case ETK_Typename:
3089+
case ElaboratedTypeKeyword::None:
3090+
case ElaboratedTypeKeyword::Typename:
30743091
return false;
3075-
case ETK_Class:
3076-
case ETK_Struct:
3077-
case ETK_Interface:
3078-
case ETK_Union:
3079-
case ETK_Enum:
3092+
case ElaboratedTypeKeyword::Class:
3093+
case ElaboratedTypeKeyword::Struct:
3094+
case ElaboratedTypeKeyword::Interface:
3095+
case ElaboratedTypeKeyword::Union:
3096+
case ElaboratedTypeKeyword::Enum:
30803097
return true;
30813098
}
30823099
llvm_unreachable("Unknown elaborated type keyword.");
30833100
}
30843101

30853102
StringRef TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
30863103
switch (Keyword) {
3087-
case ETK_None: return {};
3088-
case ETK_Typename: return "typename";
3089-
case ETK_Class: return "class";
3090-
case ETK_Struct: return "struct";
3091-
case ETK_Interface: return "__interface";
3092-
case ETK_Union: return "union";
3093-
case ETK_Enum: return "enum";
3104+
case ElaboratedTypeKeyword::None:
3105+
return {};
3106+
case ElaboratedTypeKeyword::Typename:
3107+
return "typename";
3108+
case ElaboratedTypeKeyword::Class:
3109+
return "class";
3110+
case ElaboratedTypeKeyword::Struct:
3111+
return "struct";
3112+
case ElaboratedTypeKeyword::Interface:
3113+
return "__interface";
3114+
case ElaboratedTypeKeyword::Union:
3115+
return "union";
3116+
case ElaboratedTypeKeyword::Enum:
3117+
return "enum";
30943118
}
30953119

30963120
llvm_unreachable("Unknown elaborated type keyword.");
@@ -3123,7 +3147,7 @@ DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
31233147
NestedNameSpecifier *Qualifier,
31243148
const IdentifierInfo *Name,
31253149
ArrayRef<TemplateArgument> Args) {
3126-
ID.AddInteger(Keyword);
3150+
ID.AddInteger(llvm::to_underlying(Keyword));
31273151
ID.AddPointer(Qualifier);
31283152
ID.AddPointer(Name);
31293153
for (const TemplateArgument &Arg : Args)
@@ -4741,7 +4765,7 @@ AutoType::AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword,
47414765
ConceptDecl *TypeConstraintConcept,
47424766
ArrayRef<TemplateArgument> TypeConstraintArgs)
47434767
: DeducedType(Auto, DeducedAsType, ExtraDependence, Canon) {
4744-
AutoTypeBits.Keyword = (unsigned)Keyword;
4768+
AutoTypeBits.Keyword = llvm::to_underlying(Keyword);
47454769
AutoTypeBits.NumArgs = TypeConstraintArgs.size();
47464770
this->TypeConstraintConcept = TypeConstraintConcept;
47474771
assert(TypeConstraintConcept || AutoTypeBits.NumArgs == 0);

clang/lib/AST/TypePrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,7 @@ void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
15971597
if (!Policy.IncludeTagDefinition)
15981598
{
15991599
OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1600-
if (T->getKeyword() != ETK_None)
1600+
if (T->getKeyword() != ElaboratedTypeKeyword::None)
16011601
OS << " ";
16021602
NestedNameSpecifier *Qualifier = T->getQualifier();
16031603
if (Qualifier)
@@ -1641,7 +1641,7 @@ void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
16411641
void TypePrinter::printDependentNameBefore(const DependentNameType *T,
16421642
raw_ostream &OS) {
16431643
OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1644-
if (T->getKeyword() != ETK_None)
1644+
if (T->getKeyword() != ElaboratedTypeKeyword::None)
16451645
OS << " ";
16461646

16471647
T->getQualifier()->print(OS, Policy);
@@ -1658,7 +1658,7 @@ void TypePrinter::printDependentTemplateSpecializationBefore(
16581658
IncludeStrongLifetimeRAII Strong(Policy);
16591659

16601660
OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1661-
if (T->getKeyword() != ETK_None)
1661+
if (T->getKeyword() != ElaboratedTypeKeyword::None)
16621662
OS << " ";
16631663

16641664
if (T->getQualifier())

0 commit comments

Comments
 (0)