Skip to content

Commit

Permalink
Add Type::isSingleCapabilityRecord() helper function
Browse files Browse the repository at this point in the history
This is duplicated twice in RISCVABIInfo and I intend to reuse this in
the following commit.
  • Loading branch information
arichardson committed Jan 9, 2025
1 parent a951385 commit 13f4598
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
/// pointers.
bool isCHERICapabilityType(const ASTContext &Context,
bool IncludeIntCap = true) const;
/// Returns true if this is a struct/union type that contains exactly one
/// capability element.
bool isSingleCapabilityRecord(const ASTContext &Context) const;
/// Returns true for __uintcap_t or __intcap_t (and enums/_Atomic with that
/// underlying type)
bool isIntCapType() const;
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,18 @@ bool Type::isCHERICapabilityType(const ASTContext &Context,
return false;
}

bool Type::isSingleCapabilityRecord(const ASTContext &Context) const {
if (auto *RT = getAs<RecordType>())
return Context.containsCapabilities(RT->getDecl()) &&
Context.getTypeSize(this) ==
Context.getTargetInfo().getCHERICapabilityWidth();
if (const AtomicType *AT = getAs<AtomicType>())
return AT->getValueType()->isSingleCapabilityRecord(Context);
if (const AttributedType *AT = getAs<AttributedType>())
return AT->getModifiedType()->isSingleCapabilityRecord(Context);
return false;
}

bool Type::isIntCapType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() == BuiltinType::IntCap ||
Expand Down
14 changes: 2 additions & 12 deletions clang/lib/CodeGen/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,7 @@ ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,

uint64_t Size = getContext().getTypeSize(Ty);

bool IsSingleCapRecord = false;
if (auto *RT = Ty->getAs<RecordType>())
IsSingleCapRecord = Size == getTarget().getCHERICapabilityWidth() &&
getContext().containsCapabilities(RT->getDecl());

bool IsSingleCapRecord = Ty->isSingleCapabilityRecord(getContext());
bool IsCapability = Ty->isCHERICapabilityType(getContext()) ||
IsSingleCapRecord;

Expand Down Expand Up @@ -495,13 +491,7 @@ Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,

auto TInfo = getContext().getTypeInfoInChars(Ty);

bool IsSingleCapRecord = false;
CharUnits CapabilityWidth =
CharUnits::fromQuantity(getTarget().getCHERICapabilityWidth() / 8);
if (const auto *RT = Ty->getAs<RecordType>())
IsSingleCapRecord = TInfo.Width == CapabilityWidth &&
getContext().containsCapabilities(RT->getDecl());

bool IsSingleCapRecord = Ty->isSingleCapabilityRecord(getContext());
bool IsCapability = Ty->isCHERICapabilityType(getContext()) ||
IsSingleCapRecord;

Expand Down

0 comments on commit 13f4598

Please sign in to comment.