Skip to content

Commit fec4487

Browse files
committed
[clang] NFC: introduce UnsignedOrNone replacement for std::optional<unsigned>
This introduces a new class 'UnsignedOrNone', which models a lite version of std::optional<unsigned>, but has the same size as 'unsigned'. This replaces most uses of std::optional<unsigned> and of similar schemes utilizing 'int' and '-1' as sentinel.
1 parent 4c3fe79 commit fec4487

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+511
-512
lines changed

clang/include/clang/AST/ASTConcept.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/AST/NestedNameSpecifier.h"
1919
#include "clang/AST/TemplateBase.h"
2020
#include "clang/Basic/SourceLocation.h"
21+
#include "clang/Basic/UnsignedOrNone.h"
2122
#include "llvm/ADT/FoldingSet.h"
2223
#include "llvm/ADT/PointerUnion.h"
2324
#include "llvm/ADT/SmallVector.h"
@@ -229,15 +230,14 @@ class TypeConstraint {
229230
/// type-constraint.
230231
Expr *ImmediatelyDeclaredConstraint = nullptr;
231232
ConceptReference *ConceptRef;
232-
int ArgumentPackSubstitutionIndex;
233+
UnsignedOrNone ArgPackSubstIndex;
233234

234235
public:
235236
TypeConstraint(ConceptReference *ConceptRef,
236237
Expr *ImmediatelyDeclaredConstraint,
237-
int ArgumentPackSubstitutionIndex)
238+
UnsignedOrNone ArgPackSubstIndex)
238239
: ImmediatelyDeclaredConstraint(ImmediatelyDeclaredConstraint),
239-
ConceptRef(ConceptRef),
240-
ArgumentPackSubstitutionIndex(ArgumentPackSubstitutionIndex) {}
240+
ConceptRef(ConceptRef), ArgPackSubstIndex(ArgPackSubstIndex) {}
241241

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

249249
ConceptReference *getConceptReference() const { return ConceptRef; }
250250

251-
int getArgumentPackSubstitutionIndex() const {
252-
return ArgumentPackSubstitutionIndex;
253-
}
251+
UnsignedOrNone getArgPackSubstIndex() const { return ArgPackSubstIndex; }
254252

255253
// FIXME: Instead of using these concept related functions the callers should
256254
// directly work with the corresponding ConceptReference.

clang/include/clang/AST/ASTContext.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,10 +1795,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
17951795
QualType Wrapped, QualType Contained,
17961796
const HLSLAttributedResourceType::Attributes &Attrs);
17971797

1798-
QualType
1799-
getSubstTemplateTypeParmType(QualType Replacement, Decl *AssociatedDecl,
1800-
unsigned Index,
1801-
std::optional<unsigned> PackIndex) const;
1798+
QualType getSubstTemplateTypeParmType(QualType Replacement,
1799+
Decl *AssociatedDecl, unsigned Index,
1800+
UnsignedOrNone PackIndex) const;
18021801
QualType getSubstTemplateTypeParmPackType(Decl *AssociatedDecl,
18031802
unsigned Index, bool Final,
18041803
const TemplateArgument &ArgPack);
@@ -1853,8 +1852,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
18531852
/// expansion is used in a context where the arity is inferred from
18541853
/// elsewhere, such as if the pattern contains a placeholder type or
18551854
/// if this is the canonical type of another pack expansion type.
1856-
QualType getPackExpansionType(QualType Pattern,
1857-
std::optional<unsigned> NumExpansions,
1855+
QualType getPackExpansionType(QualType Pattern, UnsignedOrNone NumExpansions,
18581856
bool ExpectPackInType = true) const;
18591857

18601858
QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
@@ -1898,7 +1896,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
18981896
QualType getPackIndexingType(QualType Pattern, Expr *IndexExpr,
18991897
bool FullySubstituted = false,
19001898
ArrayRef<QualType> Expansions = {},
1901-
int Index = -1) const;
1899+
UnsignedOrNone Index = std::nullopt) const;
19021900

19031901
/// Unary type transforms
19041902
QualType getUnaryTransformType(QualType BaseType, QualType UnderlyingType,
@@ -2393,10 +2391,10 @@ class ASTContext : public RefCountedBase<ASTContext> {
23932391
TemplateName
23942392
getDependentTemplateName(const DependentTemplateStorage &Name) const;
23952393

2396-
TemplateName
2397-
getSubstTemplateTemplateParm(TemplateName replacement, Decl *AssociatedDecl,
2398-
unsigned Index,
2399-
std::optional<unsigned> PackIndex) const;
2394+
TemplateName getSubstTemplateTemplateParm(TemplateName replacement,
2395+
Decl *AssociatedDecl,
2396+
unsigned Index,
2397+
UnsignedOrNone PackIndex) const;
24002398
TemplateName getSubstTemplateTemplateParmPack(const TemplateArgument &ArgPack,
24012399
Decl *AssociatedDecl,
24022400
unsigned Index,

clang/include/clang/AST/ASTImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ class TypeSourceInfo;
592592
/// F should be a field (or indirect field) declaration.
593593
/// \returns The index of the field in its parent context (starting from 0).
594594
/// On error `std::nullopt` is returned (parent context is non-record).
595-
static std::optional<unsigned> getFieldIndex(Decl *F);
595+
static UnsignedOrNone getFieldIndex(Decl *F);
596596
};
597597

598598
} // namespace clang

clang/include/clang/AST/ASTStructuralEquivalence.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ struct StructuralEquivalenceContext {
123123
///
124124
/// FIXME: This is needed by ASTImporter and ASTStructureEquivalence. It
125125
/// probably makes more sense in some other common place then here.
126-
static std::optional<unsigned>
127-
findUntaggedStructOrUnionIndex(RecordDecl *Anon);
126+
static UnsignedOrNone findUntaggedStructOrUnionIndex(RecordDecl *Anon);
128127

129128
// If ErrorOnTagTypeMismatch is set, return the error, otherwise get the
130129
// relevant warning for the input error diagnostic.

clang/include/clang/AST/Decl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "clang/Basic/PragmaKinds.h"
3434
#include "clang/Basic/SourceLocation.h"
3535
#include "clang/Basic/Specifiers.h"
36+
#include "clang/Basic/UnsignedOrNone.h"
3637
#include "clang/Basic/Visibility.h"
3738
#include "llvm/ADT/APSInt.h"
3839
#include "llvm/ADT/ArrayRef.h"
@@ -82,14 +83,13 @@ enum class ImplicitParamKind;
8283
// expanded.
8384
struct AssociatedConstraint {
8485
const Expr *ConstraintExpr = nullptr;
85-
int ArgumentPackSubstitutionIndex = -1;
86+
UnsignedOrNone ArgPackSubstIndex = std::nullopt;
8687

8788
constexpr AssociatedConstraint() = default;
8889

8990
explicit AssociatedConstraint(const Expr *ConstraintExpr,
90-
int ArgumentPackSubstitutionIndex = -1)
91-
: ConstraintExpr(ConstraintExpr),
92-
ArgumentPackSubstitutionIndex(ArgumentPackSubstitutionIndex) {}
91+
UnsignedOrNone ArgPackSubstIndex = std::nullopt)
92+
: ConstraintExpr(ConstraintExpr), ArgPackSubstIndex(ArgPackSubstIndex) {}
9393

9494
explicit operator bool() const { return ConstraintExpr != nullptr; }
9595
};
@@ -2538,7 +2538,7 @@ class FunctionDecl : public DeclaratorDecl,
25382538
/// If this function is an allocation/deallocation function that takes
25392539
/// the `std::nothrow_t` tag, return true through IsNothrow,
25402540
bool isReplaceableGlobalAllocationFunction(
2541-
std::optional<unsigned> *AlignmentParam = nullptr,
2541+
UnsignedOrNone *AlignmentParam = nullptr,
25422542
bool *IsNothrow = nullptr) const;
25432543

25442544
/// Determine if this function provides an inline implementation of a builtin.

clang/include/clang/AST/DeclTemplate.h

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,13 +1198,8 @@ class TemplateTypeParmDecl final : public TypeDecl,
11981198
/// type constraint.
11991199
bool TypeConstraintInitialized : 1;
12001200

1201-
/// Whether this type template parameter is an "expanded"
1202-
/// parameter pack, meaning that its type is a pack expansion and we
1203-
/// already know the set of types that expansion expands to.
1204-
bool ExpandedParameterPack : 1;
1205-
1206-
/// The number of type parameters in an expanded parameter pack.
1207-
unsigned NumExpanded = 0;
1201+
/// The number of type parameters in an expanded parameter pack, if any.
1202+
UnsignedOrNone NumExpanded = std::nullopt;
12081203

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

12141209
TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
12151210
SourceLocation IdLoc, IdentifierInfo *Id, bool Typename,
1216-
bool HasTypeConstraint,
1217-
std::optional<unsigned> NumExpanded)
1211+
bool HasTypeConstraint, UnsignedOrNone NumExpanded)
12181212
: TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
12191213
HasTypeConstraint(HasTypeConstraint), TypeConstraintInitialized(false),
1220-
ExpandedParameterPack(NumExpanded),
1221-
NumExpanded(NumExpanded.value_or(0)) {}
1214+
NumExpanded(NumExpanded) {}
12221215

12231216
public:
12241217
static TemplateTypeParmDecl *
12251218
Create(const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc,
12261219
SourceLocation NameLoc, unsigned D, unsigned P, IdentifierInfo *Id,
12271220
bool Typename, bool ParameterPack, bool HasTypeConstraint = false,
1228-
std::optional<unsigned> NumExpanded = std::nullopt);
1221+
UnsignedOrNone NumExpanded = std::nullopt);
12291222
static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
12301223
GlobalDeclID ID);
12311224
static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
@@ -1327,13 +1320,8 @@ class TemplateTypeParmDecl final : public TypeDecl,
13271320
/// expanded parameter pack. For example, instantiating
13281321
/// \c X<int, unsigned int> results in \c Convertibles being an expanded
13291322
/// parameter pack of size 2 (use getNumExpansionTypes() to get this number).
1330-
bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1331-
1332-
/// Retrieves the number of parameters in an expanded parameter pack.
1333-
unsigned getNumExpansionParameters() const {
1334-
assert(ExpandedParameterPack && "Not an expansion parameter pack");
1335-
return NumExpanded;
1336-
}
1323+
/// Retrieves the number of parameters in an expanded parameter pack, if any.
1324+
UnsignedOrNone getNumExpansionParameters() const { return NumExpanded; }
13371325

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

13451333
void setTypeConstraint(ConceptReference *CR,
13461334
Expr *ImmediatelyDeclaredConstraint,
1347-
int ArgumentPackSubstitutionIndex);
1335+
UnsignedOrNone ArgPackSubstIndex);
13481336

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

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

33883376
if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {

clang/include/clang/AST/ExprCXX.h

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,7 +4210,7 @@ class PackExpansionExpr : public Expr {
42104210

42114211
public:
42124212
PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
4213-
std::optional<unsigned> NumExpansions)
4213+
UnsignedOrNone NumExpansions)
42144214
: Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
42154215
Pattern->getObjectKind()),
42164216
EllipsisLoc(EllipsisLoc),
@@ -4233,7 +4233,7 @@ class PackExpansionExpr : public Expr {
42334233

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

@@ -4304,8 +4304,7 @@ class SizeOfPackExpr final
43044304
/// the given parameter pack.
43054305
SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack,
43064306
SourceLocation PackLoc, SourceLocation RParenLoc,
4307-
std::optional<unsigned> Length,
4308-
ArrayRef<TemplateArgument> PartialArgs)
4307+
UnsignedOrNone Length, ArrayRef<TemplateArgument> PartialArgs)
43094308
: Expr(SizeOfPackExprClass, SizeType, VK_PRValue, OK_Ordinary),
43104309
OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc),
43114310
Length(Length ? *Length : PartialArgs.size()), Pack(Pack) {
@@ -4325,7 +4324,7 @@ class SizeOfPackExpr final
43254324
static SizeOfPackExpr *Create(ASTContext &Context, SourceLocation OperatorLoc,
43264325
NamedDecl *Pack, SourceLocation PackLoc,
43274326
SourceLocation RParenLoc,
4328-
std::optional<unsigned> Length = std::nullopt,
4327+
UnsignedOrNone Length = std::nullopt,
43294328
ArrayRef<TemplateArgument> PartialArgs = {});
43304329
static SizeOfPackExpr *CreateDeserialized(ASTContext &Context,
43314330
unsigned NumPartialArgs);
@@ -4467,7 +4466,7 @@ class PackIndexingExpr final
44674466

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

4470-
std::optional<unsigned> getSelectedIndex() const {
4469+
UnsignedOrNone getSelectedIndex() const {
44714470
if (isInstantiationDependent())
44724471
return std::nullopt;
44734472
ConstantExpr *CE = cast<ConstantExpr>(getIndexExpr());
@@ -4477,7 +4476,7 @@ class PackIndexingExpr final
44774476
}
44784477

44794478
Expr *getSelectedExpr() const {
4480-
std::optional<unsigned> Index = getSelectedIndex();
4479+
UnsignedOrNone Index = getSelectedIndex();
44814480
assert(Index && "extracting the indexed expression of a dependant pack");
44824481
return getTrailingObjects<Expr *>()[*Index];
44834482
}
@@ -4523,11 +4522,11 @@ class SubstNonTypeTemplateParmExpr : public Expr {
45234522
SubstNonTypeTemplateParmExpr(QualType Ty, ExprValueKind ValueKind,
45244523
SourceLocation Loc, Expr *Replacement,
45254524
Decl *AssociatedDecl, unsigned Index,
4526-
std::optional<unsigned> PackIndex, bool RefParam)
4525+
UnsignedOrNone PackIndex, bool RefParam)
45274526
: Expr(SubstNonTypeTemplateParmExprClass, Ty, ValueKind, OK_Ordinary),
45284527
Replacement(Replacement),
45294528
AssociatedDeclAndRef(AssociatedDecl, RefParam), Index(Index),
4530-
PackIndex(PackIndex ? *PackIndex + 1 : 0) {
4529+
PackIndex(PackIndex.toInternalRepresentation()) {
45314530
assert(AssociatedDecl != nullptr);
45324531
SubstNonTypeTemplateParmExprBits.NameLoc = Loc;
45334532
setDependence(computeDependence(this));
@@ -4549,10 +4548,8 @@ class SubstNonTypeTemplateParmExpr : public Expr {
45494548
/// This should match the result of `getParameter()->getIndex()`.
45504549
unsigned getIndex() const { return Index; }
45514550

4552-
std::optional<unsigned> getPackIndex() const {
4553-
if (PackIndex == 0)
4554-
return std::nullopt;
4555-
return PackIndex - 1;
4551+
UnsignedOrNone getPackIndex() const {
4552+
return UnsignedOrNone::fromInternalRepresentation(PackIndex);
45564553
}
45574554

45584555
NonTypeTemplateParmDecl *getParameter() const;
@@ -4867,15 +4864,15 @@ class CXXFoldExpr : public Expr {
48674864
SourceLocation RParenLoc;
48684865
// When 0, the number of expansions is not known. Otherwise, this is one more
48694866
// than the number of expansions.
4870-
unsigned NumExpansions;
4867+
UnsignedOrNone NumExpansions = std::nullopt;
48714868
Stmt *SubExprs[SubExpr::Count];
48724869
BinaryOperatorKind Opcode;
48734870

48744871
public:
48754872
CXXFoldExpr(QualType T, UnresolvedLookupExpr *Callee,
48764873
SourceLocation LParenLoc, Expr *LHS, BinaryOperatorKind Opcode,
48774874
SourceLocation EllipsisLoc, Expr *RHS, SourceLocation RParenLoc,
4878-
std::optional<unsigned> NumExpansions);
4875+
UnsignedOrNone NumExpansions);
48794876

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

@@ -4904,11 +4901,7 @@ class CXXFoldExpr : public Expr {
49044901
SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
49054902
BinaryOperatorKind getOperator() const { return Opcode; }
49064903

4907-
std::optional<unsigned> getNumExpansions() const {
4908-
if (NumExpansions)
4909-
return NumExpansions - 1;
4910-
return std::nullopt;
4911-
}
4904+
UnsignedOrNone getNumExpansions() const { return NumExpansions; }
49124905

49134906
SourceLocation getBeginLoc() const LLVM_READONLY {
49144907
if (LParenLoc.isValid())

clang/include/clang/AST/ExprObjC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ struct ObjCDictionaryElement {
271271

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

276276
/// Determines whether this dictionary element is a pack expansion.
277277
bool isPackExpansion() const { return EllipsisLoc.isValid(); }

clang/include/clang/AST/Mangle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ class MangleContext {
182182

183183
class ItaniumMangleContext : public MangleContext {
184184
public:
185-
using DiscriminatorOverrideTy =
186-
std::optional<unsigned> (*)(ASTContext &, const NamedDecl *);
185+
using DiscriminatorOverrideTy = UnsignedOrNone (*)(ASTContext &,
186+
const NamedDecl *);
187187
explicit ItaniumMangleContext(ASTContext &C, DiagnosticsEngine &D,
188188
bool IsAux = false)
189189
: MangleContext(C, D, MK_Itanium, IsAux) {}

0 commit comments

Comments
 (0)