aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaDecl.cpp
diff options
context:
space:
mode:
authorMatheus Izvekov <mizvekov@gmail.com>2025-08-25 20:18:56 -0300
committerGitHub <noreply@github.com>2025-08-25 20:18:56 -0300
commitdc8596d5485a52abee2967cec72f81ef4872270c (patch)
tree5d0b5cbc1fed95f3b9aae2b39387c0f2b4d0e47c /clang/lib/Sema/SemaDecl.cpp
parent1ba8b36fef84bedb0a657b570076ec1a47e9061d (diff)
downloadllvm-dc8596d5485a52abee2967cec72f81ef4872270c.zip
llvm-dc8596d5485a52abee2967cec72f81ef4872270c.tar.gz
llvm-dc8596d5485a52abee2967cec72f81ef4872270c.tar.bz2
[clang] NFC: change more places to use Type::getAsTagDecl and friends (#155313)
This changes a bunch of places which use getAs<TagType>, including derived types, just to obtain the tag definition. This is preparation for #155028, offloading all the changes that PR used to introduce which don't depend on any new helpers.
Diffstat (limited to 'clang/lib/Sema/SemaDecl.cpp')
-rw-r--r--clang/lib/Sema/SemaDecl.cpp160
1 files changed, 69 insertions, 91 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 160d735..8a5b9fc 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -5291,14 +5291,9 @@ Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS,
// UNION_TYPE; <- where UNION_TYPE is a typedef union.
if ((Tag && Tag->getDeclName()) ||
DS.getTypeSpecType() == DeclSpec::TST_typename) {
- RecordDecl *Record = nullptr;
- if (Tag)
- Record = dyn_cast<RecordDecl>(Tag);
- else if (const RecordType *RT =
- DS.getRepAsType().get()->getAsStructureType())
- Record = RT->getOriginalDecl()->getDefinitionOrSelf();
- else if (const RecordType *UT = DS.getRepAsType().get()->getAsUnionType())
- Record = UT->getOriginalDecl()->getDefinitionOrSelf();
+ RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag);
+ if (!Record)
+ Record = DS.getRepAsType().get()->getAsRecordDecl();
if (Record && getLangOpts().MicrosoftExt) {
Diag(DS.getBeginLoc(), diag::ext_ms_anonymous_record)
@@ -14480,11 +14475,8 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
// version of one of these types, or an array of one of the preceding
// types and is declared without an initializer.
if (getLangOpts().CPlusPlus && Var->hasLocalStorage()) {
- if (const RecordType *Record
- = Context.getBaseElementType(Type)->getAs<RecordType>()) {
- CXXRecordDecl *CXXRecord =
- cast<CXXRecordDecl>(Record->getOriginalDecl())
- ->getDefinitionOrSelf();
+ if (const auto *CXXRecord =
+ Context.getBaseElementType(Type)->getAsCXXRecordDecl()) {
// Mark the function (if we're in one) for further checking even if the
// looser rules of C++11 do not require such checks, so that we can
// diagnose incompatibilities with C++98.
@@ -14947,8 +14939,8 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
// Require the destructor.
if (!type->isDependentType())
- if (const RecordType *recordType = baseType->getAs<RecordType>())
- FinalizeVarWithDestructor(var, recordType);
+ if (auto *RD = baseType->getAsCXXRecordDecl())
+ FinalizeVarWithDestructor(var, RD);
// If this variable must be emitted, add it as an initializer for the current
// module.
@@ -19119,17 +19111,16 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T,
if (!InvalidDecl && getLangOpts().CPlusPlus) {
if (Record->isUnion()) {
- if (const RecordType *RT = EltTy->getAs<RecordType>()) {
- CXXRecordDecl *RDecl = cast<CXXRecordDecl>(RT->getOriginalDecl());
- if (RDecl->getDefinition()) {
- // C++ [class.union]p1: An object of a class with a non-trivial
- // constructor, a non-trivial copy constructor, a non-trivial
- // destructor, or a non-trivial copy assignment operator
- // cannot be a member of a union, nor can an array of such
- // objects.
- if (CheckNontrivialField(NewFD))
- NewFD->setInvalidDecl();
- }
+ if (const auto *RD = EltTy->getAsCXXRecordDecl();
+ RD && (RD->isBeingDefined() || RD->isCompleteDefinition())) {
+
+ // C++ [class.union]p1: An object of a class with a non-trivial
+ // constructor, a non-trivial copy constructor, a non-trivial
+ // destructor, or a non-trivial copy assignment operator
+ // cannot be a member of a union, nor can an array of such
+ // objects.
+ if (CheckNontrivialField(NewFD))
+ NewFD->setInvalidDecl();
}
// C++ [class.union]p1: If a union contains a member of reference type,
@@ -19185,55 +19176,51 @@ bool Sema::CheckNontrivialField(FieldDecl *FD) {
return false;
QualType EltTy = Context.getBaseElementType(FD->getType());
- if (const RecordType *RT = EltTy->getAs<RecordType>()) {
- CXXRecordDecl *RDecl =
- cast<CXXRecordDecl>(RT->getOriginalDecl())->getDefinitionOrSelf();
- if (RDecl->getDefinition()) {
- // We check for copy constructors before constructors
- // because otherwise we'll never get complaints about
- // copy constructors.
-
- CXXSpecialMemberKind member = CXXSpecialMemberKind::Invalid;
- // We're required to check for any non-trivial constructors. Since the
- // implicit default constructor is suppressed if there are any
- // user-declared constructors, we just need to check that there is a
- // trivial default constructor and a trivial copy constructor. (We don't
- // worry about move constructors here, since this is a C++98 check.)
- if (RDecl->hasNonTrivialCopyConstructor())
- member = CXXSpecialMemberKind::CopyConstructor;
- else if (!RDecl->hasTrivialDefaultConstructor())
- member = CXXSpecialMemberKind::DefaultConstructor;
- else if (RDecl->hasNonTrivialCopyAssignment())
- member = CXXSpecialMemberKind::CopyAssignment;
- else if (RDecl->hasNonTrivialDestructor())
- member = CXXSpecialMemberKind::Destructor;
-
- if (member != CXXSpecialMemberKind::Invalid) {
- if (!getLangOpts().CPlusPlus11 &&
- getLangOpts().ObjCAutoRefCount && RDecl->hasObjectMember()) {
- // Objective-C++ ARC: it is an error to have a non-trivial field of
- // a union. However, system headers in Objective-C programs
- // occasionally have Objective-C lifetime objects within unions,
- // and rather than cause the program to fail, we make those
- // members unavailable.
- SourceLocation Loc = FD->getLocation();
- if (getSourceManager().isInSystemHeader(Loc)) {
- if (!FD->hasAttr<UnavailableAttr>())
- FD->addAttr(UnavailableAttr::CreateImplicit(Context, "",
- UnavailableAttr::IR_ARCFieldWithOwnership, Loc));
- return false;
- }
+ if (const auto *RDecl = EltTy->getAsCXXRecordDecl();
+ RDecl && (RDecl->isBeingDefined() || RDecl->isCompleteDefinition())) {
+ // We check for copy constructors before constructors
+ // because otherwise we'll never get complaints about
+ // copy constructors.
+
+ CXXSpecialMemberKind member = CXXSpecialMemberKind::Invalid;
+ // We're required to check for any non-trivial constructors. Since the
+ // implicit default constructor is suppressed if there are any
+ // user-declared constructors, we just need to check that there is a
+ // trivial default constructor and a trivial copy constructor. (We don't
+ // worry about move constructors here, since this is a C++98 check.)
+ if (RDecl->hasNonTrivialCopyConstructor())
+ member = CXXSpecialMemberKind::CopyConstructor;
+ else if (!RDecl->hasTrivialDefaultConstructor())
+ member = CXXSpecialMemberKind::DefaultConstructor;
+ else if (RDecl->hasNonTrivialCopyAssignment())
+ member = CXXSpecialMemberKind::CopyAssignment;
+ else if (RDecl->hasNonTrivialDestructor())
+ member = CXXSpecialMemberKind::Destructor;
+
+ if (member != CXXSpecialMemberKind::Invalid) {
+ if (!getLangOpts().CPlusPlus11 && getLangOpts().ObjCAutoRefCount &&
+ RDecl->hasObjectMember()) {
+ // Objective-C++ ARC: it is an error to have a non-trivial field of
+ // a union. However, system headers in Objective-C programs
+ // occasionally have Objective-C lifetime objects within unions,
+ // and rather than cause the program to fail, we make those
+ // members unavailable.
+ SourceLocation Loc = FD->getLocation();
+ if (getSourceManager().isInSystemHeader(Loc)) {
+ if (!FD->hasAttr<UnavailableAttr>())
+ FD->addAttr(UnavailableAttr::CreateImplicit(
+ Context, "", UnavailableAttr::IR_ARCFieldWithOwnership, Loc));
+ return false;
}
-
- Diag(
- FD->getLocation(),
- getLangOpts().CPlusPlus11
- ? diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member
- : diag::err_illegal_union_or_anon_struct_member)
- << FD->getParent()->isUnion() << FD->getDeclName() << member;
- DiagnoseNontrivial(RDecl, member);
- return !getLangOpts().CPlusPlus11;
}
+
+ Diag(FD->getLocation(),
+ getLangOpts().CPlusPlus11
+ ? diag::warn_cxx98_compat_nontrivial_union_or_anon_struct_member
+ : diag::err_illegal_union_or_anon_struct_member)
+ << FD->getParent()->isUnion() << FD->getDeclName() << member;
+ DiagnoseNontrivial(RDecl, member);
+ return !getLangOpts().CPlusPlus11;
}
}
@@ -19509,11 +19496,9 @@ bool Sema::EntirelyFunctionPointers(const RecordDecl *Record) {
return PointeeType.getDesugaredType(Context)->isFunctionType();
}
// If a member is a struct entirely of function pointers, that counts too.
- if (const RecordType *RT = FieldType->getAs<RecordType>()) {
- const RecordDecl *Record = RT->getOriginalDecl()->getDefinitionOrSelf();
- if (Record->isStruct() && EntirelyFunctionPointers(Record))
- return true;
- }
+ if (const auto *Record = FieldType->getAsRecordDecl();
+ Record && Record->isStruct() && EntirelyFunctionPointers(Record))
+ return true;
return false;
};
@@ -19667,10 +19652,8 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
FD->setInvalidDecl();
EnclosingDecl->setInvalidDecl();
continue;
- } else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) {
- if (Record && FDTTy->getOriginalDecl()
- ->getDefinitionOrSelf()
- ->hasFlexibleArrayMember()) {
+ } else if (const auto *RD = FDTy->getAsRecordDecl()) {
+ if (Record && RD->hasFlexibleArrayMember()) {
// A type which contains a flexible array member is considered to be a
// flexible array member.
Record->setHasFlexibleArrayMember(true);
@@ -19696,7 +19679,6 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
// Ivars can not have abstract class types
FD->setInvalidDecl();
}
- const RecordDecl *RD = FDTTy->getOriginalDecl()->getDefinitionOrSelf();
if (Record && RD->hasObjectMember())
Record->setHasObjectMember(true);
if (Record && RD->hasVolatileMember())
@@ -19730,10 +19712,8 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
Record->setHasObjectMember(true);
else if (Context.getAsArrayType(FD->getType())) {
QualType BaseType = Context.getBaseElementType(FD->getType());
- if (BaseType->isRecordType() && BaseType->castAs<RecordType>()
- ->getOriginalDecl()
- ->getDefinitionOrSelf()
- ->hasObjectMember())
+ if (const auto *RD = BaseType->getAsRecordDecl();
+ RD && RD->hasObjectMember())
Record->setHasObjectMember(true);
else if (BaseType->isObjCObjectPointerType() ||
BaseType.isObjCGCStrong())
@@ -19765,10 +19745,8 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
Record->setHasNonTrivialToPrimitiveDestructCUnion(true);
}
- if (const auto *RT = FT->getAs<RecordType>()) {
- if (RT->getOriginalDecl()
- ->getDefinitionOrSelf()
- ->getArgPassingRestrictions() ==
+ if (const auto *RD = FT->getAsRecordDecl()) {
+ if (RD->getArgPassingRestrictions() ==
RecordArgPassingKind::CanNeverPassInRegs)
Record->setArgPassingRestrictions(
RecordArgPassingKind::CanNeverPassInRegs);