Skip to content

[clang] NFC: introduce UnsignedOrNone as a replacement for std::optional<unsigned> #134142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions clang/include/clang/AST/ASTConcept.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "clang/AST/NestedNameSpecifier.h"
#include "clang/AST/TemplateBase.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/UnsignedOrNone.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -229,15 +230,14 @@ class TypeConstraint {
/// type-constraint.
Expr *ImmediatelyDeclaredConstraint = nullptr;
ConceptReference *ConceptRef;
int ArgumentPackSubstitutionIndex;
UnsignedOrNone ArgPackSubstIndex;

public:
TypeConstraint(ConceptReference *ConceptRef,
Expr *ImmediatelyDeclaredConstraint,
int ArgumentPackSubstitutionIndex)
UnsignedOrNone ArgPackSubstIndex)
: ImmediatelyDeclaredConstraint(ImmediatelyDeclaredConstraint),
ConceptRef(ConceptRef),
ArgumentPackSubstitutionIndex(ArgumentPackSubstitutionIndex) {}
ConceptRef(ConceptRef), ArgPackSubstIndex(ArgPackSubstIndex) {}

/// \brief Get the immediately-declared constraint expression introduced by
/// this type-constraint, that is - the constraint expression that is added to
Expand All @@ -248,9 +248,7 @@ class TypeConstraint {

ConceptReference *getConceptReference() const { return ConceptRef; }

int getArgumentPackSubstitutionIndex() const {
return ArgumentPackSubstitutionIndex;
}
UnsignedOrNone getArgPackSubstIndex() const { return ArgPackSubstIndex; }

// FIXME: Instead of using these concept related functions the callers should
// directly work with the corresponding ConceptReference.
Expand Down
9 changes: 4 additions & 5 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ class ASTContext : public RefCountedBase<ASTContext> {

QualType getSubstTemplateTypeParmType(QualType Replacement,
Decl *AssociatedDecl, unsigned Index,
std::optional<unsigned> PackIndex,
UnsignedOrNone PackIndex,
bool Final) const;
QualType getSubstTemplateTypeParmPackType(Decl *AssociatedDecl,
unsigned Index, bool Final,
Expand Down Expand Up @@ -1853,8 +1853,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// expansion is used in a context where the arity is inferred from
/// elsewhere, such as if the pattern contains a placeholder type or
/// if this is the canonical type of another pack expansion type.
QualType getPackExpansionType(QualType Pattern,
std::optional<unsigned> NumExpansions,
QualType getPackExpansionType(QualType Pattern, UnsignedOrNone NumExpansions,
bool ExpectPackInType = true) const;

QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Expand Down Expand Up @@ -1898,7 +1897,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
QualType getPackIndexingType(QualType Pattern, Expr *IndexExpr,
bool FullySubstituted = false,
ArrayRef<QualType> Expansions = {},
int Index = -1) const;
UnsignedOrNone Index = std::nullopt) const;

/// Unary type transforms
QualType getUnaryTransformType(QualType BaseType, QualType UnderlyingType,
Expand Down Expand Up @@ -2396,7 +2395,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
TemplateName getSubstTemplateTemplateParm(TemplateName replacement,
Decl *AssociatedDecl,
unsigned Index,
std::optional<unsigned> PackIndex,
UnsignedOrNone PackIndex,
bool Final) const;
TemplateName getSubstTemplateTemplateParmPack(const TemplateArgument &ArgPack,
Decl *AssociatedDecl,
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/AST/ASTImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ class TypeSourceInfo;
/// F should be a field (or indirect field) declaration.
/// \returns The index of the field in its parent context (starting from 0).
/// On error `std::nullopt` is returned (parent context is non-record).
static std::optional<unsigned> getFieldIndex(Decl *F);
static UnsignedOrNone getFieldIndex(Decl *F);
};

} // namespace clang
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/AST/ASTStructuralEquivalence.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ struct StructuralEquivalenceContext {
///
/// FIXME: This is needed by ASTImporter and ASTStructureEquivalence. It
/// probably makes more sense in some other common place then here.
static std::optional<unsigned>
findUntaggedStructOrUnionIndex(RecordDecl *Anon);
static UnsignedOrNone findUntaggedStructOrUnionIndex(RecordDecl *Anon);

// If ErrorOnTagTypeMismatch is set, return the error, otherwise get the
// relevant warning for the input error diagnostic.
Expand Down
10 changes: 5 additions & 5 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "clang/Basic/PragmaKinds.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Basic/UnsignedOrNone.h"
#include "clang/Basic/Visibility.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/ArrayRef.h"
Expand Down Expand Up @@ -82,14 +83,13 @@ enum class ImplicitParamKind;
// expanded.
struct AssociatedConstraint {
const Expr *ConstraintExpr = nullptr;
int ArgumentPackSubstitutionIndex = -1;
UnsignedOrNone ArgPackSubstIndex = std::nullopt;

constexpr AssociatedConstraint() = default;

explicit AssociatedConstraint(const Expr *ConstraintExpr,
int ArgumentPackSubstitutionIndex = -1)
: ConstraintExpr(ConstraintExpr),
ArgumentPackSubstitutionIndex(ArgumentPackSubstitutionIndex) {}
UnsignedOrNone ArgPackSubstIndex = std::nullopt)
: ConstraintExpr(ConstraintExpr), ArgPackSubstIndex(ArgPackSubstIndex) {}

explicit operator bool() const { return ConstraintExpr != nullptr; }

Expand Down Expand Up @@ -2540,7 +2540,7 @@ class FunctionDecl : public DeclaratorDecl,
/// If this function is an allocation/deallocation function that takes
/// the `std::nothrow_t` tag, return true through IsNothrow,
bool isReplaceableGlobalAllocationFunction(
std::optional<unsigned> *AlignmentParam = nullptr,
UnsignedOrNone *AlignmentParam = nullptr,
bool *IsNothrow = nullptr) const;

/// Determine if this function provides an inline implementation of a builtin.
Expand Down
36 changes: 12 additions & 24 deletions clang/include/clang/AST/DeclTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,13 +1198,8 @@ class TemplateTypeParmDecl final : public TypeDecl,
/// type constraint.
bool TypeConstraintInitialized : 1;

/// Whether this type template parameter is an "expanded"
/// parameter pack, meaning that its type is a pack expansion and we
/// already know the set of types that expansion expands to.
bool ExpandedParameterPack : 1;

/// The number of type parameters in an expanded parameter pack.
unsigned NumExpanded = 0;
/// The number of type parameters in an expanded parameter pack, if any.
UnsignedOrNone NumExpanded = std::nullopt;

/// The default template argument, if any.
using DefArgStorage =
Expand All @@ -1213,19 +1208,17 @@ class TemplateTypeParmDecl final : public TypeDecl,

TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
SourceLocation IdLoc, IdentifierInfo *Id, bool Typename,
bool HasTypeConstraint,
std::optional<unsigned> NumExpanded)
bool HasTypeConstraint, UnsignedOrNone NumExpanded)
: TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
HasTypeConstraint(HasTypeConstraint), TypeConstraintInitialized(false),
ExpandedParameterPack(NumExpanded),
NumExpanded(NumExpanded.value_or(0)) {}
NumExpanded(NumExpanded) {}

public:
static TemplateTypeParmDecl *
Create(const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc,
SourceLocation NameLoc, unsigned D, unsigned P, IdentifierInfo *Id,
bool Typename, bool ParameterPack, bool HasTypeConstraint = false,
std::optional<unsigned> NumExpanded = std::nullopt);
UnsignedOrNone NumExpanded = std::nullopt);
static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
GlobalDeclID ID);
static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
Expand Down Expand Up @@ -1327,13 +1320,8 @@ class TemplateTypeParmDecl final : public TypeDecl,
/// expanded parameter pack. For example, instantiating
/// \c X<int, unsigned int> results in \c Convertibles being an expanded
/// parameter pack of size 2 (use getNumExpansionTypes() to get this number).
bool isExpandedParameterPack() const { return ExpandedParameterPack; }

/// Retrieves the number of parameters in an expanded parameter pack.
unsigned getNumExpansionParameters() const {
assert(ExpandedParameterPack && "Not an expansion parameter pack");
return NumExpanded;
}
/// Retrieves the number of parameters in an expanded parameter pack, if any.
UnsignedOrNone getNumExpansionParameters() const { return NumExpanded; }

/// Returns the type constraint associated with this template parameter (if
/// any).
Expand All @@ -1344,7 +1332,7 @@ class TemplateTypeParmDecl final : public TypeDecl,

void setTypeConstraint(ConceptReference *CR,
Expr *ImmediatelyDeclaredConstraint,
int ArgumentPackSubstitutionIndex);
UnsignedOrNone ArgPackSubstIndex);

/// Determine whether this template parameter has a type-constraint.
bool hasTypeConstraint() const {
Expand All @@ -1360,7 +1348,7 @@ class TemplateTypeParmDecl final : public TypeDecl,
llvm::SmallVectorImpl<AssociatedConstraint> &AC) const {
if (HasTypeConstraint)
AC.emplace_back(getTypeConstraint()->getImmediatelyDeclaredConstraint(),
getTypeConstraint()->getArgumentPackSubstitutionIndex());
getTypeConstraint()->getArgPackSubstIndex());
}

SourceRange getSourceRange() const override LLVM_READONLY;
Expand Down Expand Up @@ -3379,10 +3367,10 @@ inline TemplateDecl *getAsTypeTemplateDecl(Decl *D) {
///
/// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us
/// is not a pack expansion, so returns an empty Optional.
inline std::optional<unsigned> getExpandedPackSize(const NamedDecl *Param) {
inline UnsignedOrNone getExpandedPackSize(const NamedDecl *Param) {
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
if (TTP->isExpandedParameterPack())
return TTP->getNumExpansionParameters();
if (UnsignedOrNone Num = TTP->getNumExpansionParameters())
return Num;
}

if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Expand Down
33 changes: 13 additions & 20 deletions clang/include/clang/AST/ExprCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -4210,7 +4210,7 @@ class PackExpansionExpr : public Expr {

public:
PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
std::optional<unsigned> NumExpansions)
UnsignedOrNone NumExpansions)
: Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
Pattern->getObjectKind()),
EllipsisLoc(EllipsisLoc),
Expand All @@ -4233,7 +4233,7 @@ class PackExpansionExpr : public Expr {

/// Determine the number of expansions that will be produced when
/// this pack expansion is instantiated, if already known.
std::optional<unsigned> getNumExpansions() const {
UnsignedOrNone getNumExpansions() const {
if (NumExpansions)
return NumExpansions - 1;

Expand Down Expand Up @@ -4304,8 +4304,7 @@ class SizeOfPackExpr final
/// the given parameter pack.
SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
SourceLocation PackLoc, SourceLocation RParenLoc,
std::optional<unsigned> Length,
ArrayRef<TemplateArgument> PartialArgs)
UnsignedOrNone Length, ArrayRef<TemplateArgument> PartialArgs)
: Expr(SizeOfPackExprClass, SizeType, VK_PRValue, OK_Ordinary),
OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
Expand All @@ -4325,7 +4324,7 @@ class SizeOfPackExpr final
static SizeOfPackExpr *Create(ASTContext &Context, SourceLocation OperatorLoc,
NamedDecl *Pack, SourceLocation PackLoc,
SourceLocation RParenLoc,
std::optional<unsigned> Length = std::nullopt,
UnsignedOrNone Length = std::nullopt,
ArrayRef<TemplateArgument> PartialArgs = {});
static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
unsigned NumPartialArgs);
Expand Down Expand Up @@ -4467,7 +4466,7 @@ class PackIndexingExpr final

Expr *getIndexExpr() const { return cast<Expr>(SubExprs[1]); }

std::optional<unsigned> getSelectedIndex() const {
UnsignedOrNone getSelectedIndex() const {
if (isInstantiationDependent())
return std::nullopt;
ConstantExpr *CE = cast<ConstantExpr>(getIndexExpr());
Expand All @@ -4477,7 +4476,7 @@ class PackIndexingExpr final
}

Expr *getSelectedExpr() const {
std::optional<unsigned> Index = getSelectedIndex();
UnsignedOrNone Index = getSelectedIndex();
assert(Index && "extracting the indexed expression of a dependant pack");
return getTrailingObjects<Expr *>()[*Index];
}
Expand Down Expand Up @@ -4525,12 +4524,12 @@ class SubstNonTypeTemplateParmExpr : public Expr {
SubstNonTypeTemplateParmExpr(QualType Ty, ExprValueKind ValueKind,
SourceLocation Loc, Expr *Replacement,
Decl *AssociatedDecl, unsigned Index,
std::optional<unsigned> PackIndex, bool RefParam,
UnsignedOrNone PackIndex, bool RefParam,
bool Final)
: Expr(SubstNonTypeTemplateParmExprClass, Ty, ValueKind, OK_Ordinary),
Replacement(Replacement),
AssociatedDeclAndRef(AssociatedDecl, RefParam), Index(Index),
PackIndex(PackIndex ? *PackIndex + 1 : 0), Final(Final) {
PackIndex(PackIndex.toInternalRepresentation()), Final(Final) {
assert(AssociatedDecl != nullptr);
SubstNonTypeTemplateParmExprBits.NameLoc = Loc;
setDependence(computeDependence(this));
Expand All @@ -4552,10 +4551,8 @@ class SubstNonTypeTemplateParmExpr : public Expr {
/// This should match the result of `getParameter()->getIndex()`.
unsigned getIndex() const { return Index; }

std::optional<unsigned> getPackIndex() const {
if (PackIndex == 0)
return std::nullopt;
return PackIndex - 1;
UnsignedOrNone getPackIndex() const {
return UnsignedOrNone::fromInternalRepresentation(PackIndex);
}

// This substitution is Final, which means the substitution is fully
Expand Down Expand Up @@ -4882,15 +4879,15 @@ class CXXFoldExpr : public Expr {
SourceLocation RParenLoc;
// When 0, the number of expansions is not known. Otherwise, this is one more
// than the number of expansions.
unsigned NumExpansions;
UnsignedOrNone NumExpansions = std::nullopt;
Stmt *SubExprs[SubExpr::Count];
BinaryOperatorKind Opcode;

public:
CXXFoldExpr(QualType T, UnresolvedLookupExpr *Callee,
SourceLocation LParenLoc, Expr *LHS, BinaryOperatorKind Opcode,
SourceLocation EllipsisLoc, Expr *RHS, SourceLocation RParenLoc,
std::optional<unsigned> NumExpansions);
UnsignedOrNone NumExpansions);

CXXFoldExpr(EmptyShell Empty) : Expr(CXXFoldExprClass, Empty) {}

Expand Down Expand Up @@ -4919,11 +4916,7 @@ class CXXFoldExpr : public Expr {
SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
BinaryOperatorKind getOperator() const { return Opcode; }

std::optional<unsigned> getNumExpansions() const {
if (NumExpansions)
return NumExpansions - 1;
return std::nullopt;
}
UnsignedOrNone getNumExpansions() const { return NumExpansions; }

SourceLocation getBeginLoc() const LLVM_READONLY {
if (LParenLoc.isValid())
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/AST/ExprObjC.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ struct ObjCDictionaryElement {

/// The number of elements this pack expansion will expand to, if
/// this is a pack expansion and is known.
std::optional<unsigned> NumExpansions;
UnsignedOrNone NumExpansions;

/// Determines whether this dictionary element is a pack expansion.
bool isPackExpansion() const { return EllipsisLoc.isValid(); }
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/AST/Mangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ class MangleContext {

class ItaniumMangleContext : public MangleContext {
public:
using DiscriminatorOverrideTy =
std::optional<unsigned> (*)(ASTContext &, const NamedDecl *);
using DiscriminatorOverrideTy = UnsignedOrNone (*)(ASTContext &,
const NamedDecl *);
explicit ItaniumMangleContext(ASTContext &C, DiagnosticsEngine &D,
bool IsAux = false)
: MangleContext(C, D, MK_Itanium, IsAux) {}
Expand Down
14 changes: 5 additions & 9 deletions clang/include/clang/AST/PropertiesBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def TemplateNameKind : EnumPropertyType<"TemplateName::NameKind">;
def TypeOfKind : EnumPropertyType<"TypeOfKind">;
def UInt32 : CountPropertyType<"uint32_t">;
def UInt64 : CountPropertyType<"uint64_t">;
def UnsignedOrNone : PropertyType;
def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">;
def VectorKind : EnumPropertyType<"VectorKind">;
def TypeCoupledDeclRefInfo : PropertyType;
Expand Down Expand Up @@ -727,7 +728,7 @@ let Class = PropertyTypeCase<TemplateName, "SubstTemplateTemplateParm"> in {
def : Property<"index", UInt32> {
let Read = [{ parm->getIndex() }];
}
def : Property<"packIndex", Optional<UInt32>> {
def : Property<"packIndex", UnsignedOrNone> {
let Read = [{ parm->getPackIndex() }];
}
def : Property<"final", Bool> { let Read = [{ parm->getFinal() }]; }
Expand Down Expand Up @@ -860,21 +861,16 @@ let Class = PropertyTypeCase<TemplateArgument, "TemplateExpansion"> in {
def : Property<"name", TemplateName> {
let Read = [{ node.getAsTemplateOrTemplatePattern() }];
}
def : Property<"numExpansions", Optional<UInt32>> {
def : Property<"numExpansions", UnsignedOrNone> {
let Read = [{
// Translate unsigned -> uint32_t just in case.
llvm::transformOptional(node.getNumTemplateExpansions(),
[](unsigned i) { return uint32_t(i); })
node.getNumTemplateExpansions()
}];
}
def : Property<"isDefaulted", Bool> {
let Read = [{ node.getIsDefaulted() }];
}
def : Creator<[{
auto numExpansionsUnsigned = llvm::transformOptional(
numExpansions, [](uint32_t i) { return unsigned(i); });

return TemplateArgument(name, numExpansionsUnsigned, isDefaulted);
return TemplateArgument(name, numExpansions, isDefaulted);
}]>;
}
let Class = PropertyTypeCase<TemplateArgument, "Expression"> in {
Expand Down
Loading