Skip to content

Commit aa46657

Browse files
authored
[clang] NFC: Provide inline definitions for {get,cast}TagDecl and friends (#155051)
This is a small performance improvement: This helps recover the performance lost in #155028, reversing it into a small positive instead. <img width="1464" height="20" alt="image" src="https://github.com/user-attachments/assets/3378789e-109d-4211-846e-0d38d6cb190a" />
1 parent a8680be commit aa46657

File tree

3 files changed

+57
-57
lines changed

3 files changed

+57
-57
lines changed

clang/include/clang/AST/Type.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,55 @@
2323

2424
namespace clang {
2525

26+
inline CXXRecordDecl *Type::getAsCXXRecordDecl() const {
27+
const auto *TT = dyn_cast<TagType>(CanonicalType);
28+
if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
29+
return nullptr;
30+
auto *TD = TT->getOriginalDecl();
31+
if (isa<RecordType>(TT) && !isa<CXXRecordDecl>(TD))
32+
return nullptr;
33+
return cast<CXXRecordDecl>(TD)->getDefinitionOrSelf();
34+
}
35+
36+
inline CXXRecordDecl *Type::castAsCXXRecordDecl() const {
37+
const auto *TT = cast<TagType>(CanonicalType);
38+
return cast<CXXRecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
39+
}
40+
41+
inline RecordDecl *Type::getAsRecordDecl() const {
42+
const auto *TT = dyn_cast<TagType>(CanonicalType);
43+
if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
44+
return nullptr;
45+
return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
46+
}
47+
48+
inline RecordDecl *Type::castAsRecordDecl() const {
49+
const auto *TT = cast<TagType>(CanonicalType);
50+
return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
51+
}
52+
53+
inline EnumDecl *Type::getAsEnumDecl() const {
54+
if (const auto *TT = dyn_cast<EnumType>(CanonicalType))
55+
return TT->getOriginalDecl()->getDefinitionOrSelf();
56+
return nullptr;
57+
}
58+
59+
inline EnumDecl *Type::castAsEnumDecl() const {
60+
return cast<EnumType>(CanonicalType)
61+
->getOriginalDecl()
62+
->getDefinitionOrSelf();
63+
}
64+
65+
inline TagDecl *Type::getAsTagDecl() const {
66+
if (const auto *TT = dyn_cast<TagType>(CanonicalType))
67+
return TT->getOriginalDecl()->getDefinitionOrSelf();
68+
return nullptr;
69+
}
70+
71+
inline TagDecl *Type::castAsTagDecl() const {
72+
return cast<TagType>(CanonicalType)->getOriginalDecl()->getDefinitionOrSelf();
73+
}
74+
2675
inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const {
2776
if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
2877
return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD);

clang/include/clang/AST/TypeBase.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,22 +2882,22 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
28822882
/// Retrieves the CXXRecordDecl that this type refers to, either
28832883
/// because the type is a RecordType or because it is the injected-class-name
28842884
/// type of a class template or class template partial specialization.
2885-
CXXRecordDecl *getAsCXXRecordDecl() const;
2886-
CXXRecordDecl *castAsCXXRecordDecl() const;
2885+
inline CXXRecordDecl *getAsCXXRecordDecl() const;
2886+
inline CXXRecordDecl *castAsCXXRecordDecl() const;
28872887

28882888
/// Retrieves the RecordDecl this type refers to.
2889-
RecordDecl *getAsRecordDecl() const;
2890-
RecordDecl *castAsRecordDecl() const;
2889+
inline RecordDecl *getAsRecordDecl() const;
2890+
inline RecordDecl *castAsRecordDecl() const;
28912891

28922892
/// Retrieves the EnumDecl this type refers to.
2893-
EnumDecl *getAsEnumDecl() const;
2894-
EnumDecl *castAsEnumDecl() const;
2893+
inline EnumDecl *getAsEnumDecl() const;
2894+
inline EnumDecl *castAsEnumDecl() const;
28952895

28962896
/// Retrieves the TagDecl that this type refers to, either
28972897
/// because the type is a TagType or because it is the injected-class-name
28982898
/// type of a class template or class template partial specialization.
2899-
TagDecl *getAsTagDecl() const;
2900-
TagDecl *castAsTagDecl() const;
2899+
inline TagDecl *getAsTagDecl() const;
2900+
inline TagDecl *castAsTagDecl() const;
29012901

29022902
/// If this is a pointer or reference to a RecordType, return the
29032903
/// CXXRecordDecl that the type refers to.

clang/lib/AST/Type.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,55 +1917,6 @@ const CXXRecordDecl *Type::getPointeeCXXRecordDecl() const {
19171917
return PointeeType->getAsCXXRecordDecl();
19181918
}
19191919

1920-
CXXRecordDecl *Type::getAsCXXRecordDecl() const {
1921-
const auto *TT = dyn_cast<TagType>(CanonicalType);
1922-
if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
1923-
return nullptr;
1924-
auto *TD = TT->getOriginalDecl();
1925-
if (!isa<InjectedClassNameType>(TT) && !isa<CXXRecordDecl>(TD))
1926-
return nullptr;
1927-
return cast<CXXRecordDecl>(TD)->getDefinitionOrSelf();
1928-
}
1929-
1930-
CXXRecordDecl *Type::castAsCXXRecordDecl() const {
1931-
const auto *TT = cast<TagType>(CanonicalType);
1932-
return cast<CXXRecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
1933-
}
1934-
1935-
RecordDecl *Type::getAsRecordDecl() const {
1936-
const auto *TT = dyn_cast<TagType>(CanonicalType);
1937-
if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
1938-
return nullptr;
1939-
return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
1940-
}
1941-
1942-
RecordDecl *Type::castAsRecordDecl() const {
1943-
const auto *TT = cast<TagType>(CanonicalType);
1944-
return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
1945-
}
1946-
1947-
EnumDecl *Type::getAsEnumDecl() const {
1948-
if (const auto *TT = dyn_cast<EnumType>(CanonicalType))
1949-
return TT->getOriginalDecl()->getDefinitionOrSelf();
1950-
return nullptr;
1951-
}
1952-
1953-
EnumDecl *Type::castAsEnumDecl() const {
1954-
return cast<EnumType>(CanonicalType)
1955-
->getOriginalDecl()
1956-
->getDefinitionOrSelf();
1957-
}
1958-
1959-
TagDecl *Type::getAsTagDecl() const {
1960-
if (const auto *TT = dyn_cast<TagType>(CanonicalType))
1961-
return TT->getOriginalDecl()->getDefinitionOrSelf();
1962-
return nullptr;
1963-
}
1964-
1965-
TagDecl *Type::castAsTagDecl() const {
1966-
return cast<TagType>(CanonicalType)->getOriginalDecl()->getDefinitionOrSelf();
1967-
}
1968-
19691920
const TemplateSpecializationType *
19701921
Type::getAsNonAliasTemplateSpecializationType() const {
19711922
const auto *TST = getAs<TemplateSpecializationType>();

0 commit comments

Comments
 (0)