Skip to content

Commit ab8b4f6

Browse files
authored
[clang][bytecode][NFC] Replace std::optional<unsigned> with UnsignedO… (#154286)
…rNone
1 parent a7df02f commit ab8b4f6

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
256256

257257
// Prepare storage for the result.
258258
if (!Initializing && !SubExprT) {
259-
std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
259+
UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
260260
if (!LocalIndex)
261261
return false;
262262
if (!this->emitGetPtrLocal(*LocalIndex, CE))
@@ -609,7 +609,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
609609
// We're creating a complex value here, so we need to
610610
// allocate storage for it.
611611
if (!Initializing) {
612-
std::optional<unsigned> LocalIndex = allocateTemporary(CE);
612+
UnsignedOrNone LocalIndex = allocateTemporary(CE);
613613
if (!LocalIndex)
614614
return false;
615615
if (!this->emitGetPtrLocal(*LocalIndex, CE))
@@ -633,7 +633,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
633633
assert(CE->getType()->isAnyComplexType());
634634
assert(SubExpr->getType()->isAnyComplexType());
635635
if (!Initializing) {
636-
std::optional<unsigned> LocalIndex = allocateLocal(CE);
636+
UnsignedOrNone LocalIndex = allocateLocal(CE);
637637
if (!LocalIndex)
638638
return false;
639639
if (!this->emitGetPtrLocal(*LocalIndex, CE))
@@ -678,7 +678,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
678678
assert(CE->getType()->isVectorType());
679679

680680
if (!Initializing) {
681-
std::optional<unsigned> LocalIndex = allocateLocal(CE);
681+
UnsignedOrNone LocalIndex = allocateLocal(CE);
682682
if (!LocalIndex)
683683
return false;
684684
if (!this->emitGetPtrLocal(*LocalIndex, CE))
@@ -722,7 +722,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
722722
assert(CE->getType()->isVectorType());
723723

724724
if (!Initializing) {
725-
std::optional<unsigned> LocalIndex = allocateTemporary(CE);
725+
UnsignedOrNone LocalIndex = allocateTemporary(CE);
726726
if (!LocalIndex)
727727
return false;
728728
if (!this->emitGetPtrLocal(*LocalIndex, CE))
@@ -810,7 +810,7 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
810810
return true;
811811

812812
if (!Initializing) {
813-
std::optional<unsigned> LocalIndex = allocateTemporary(E);
813+
UnsignedOrNone LocalIndex = allocateTemporary(E);
814814
if (!LocalIndex)
815815
return false;
816816
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -911,7 +911,7 @@ bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
911911

912912
// We need a temporary variable holding our return value.
913913
if (!Initializing) {
914-
std::optional<unsigned> ResultIndex = this->allocateLocal(BO);
914+
UnsignedOrNone ResultIndex = this->allocateLocal(BO);
915915
if (!this->emitGetPtrLocal(*ResultIndex, BO))
916916
return false;
917917
}
@@ -1151,7 +1151,7 @@ template <class Emitter>
11511151
bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11521152
// Prepare storage for result.
11531153
if (!Initializing) {
1154-
std::optional<unsigned> LocalIndex = allocateTemporary(E);
1154+
UnsignedOrNone LocalIndex = allocateTemporary(E);
11551155
if (!LocalIndex)
11561156
return false;
11571157
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -1210,7 +1210,7 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
12101210

12111211
if (!LHSIsComplex) {
12121212
// This is using the RHS type for the fake-complex LHS.
1213-
std::optional<unsigned> LocalIndex = allocateTemporary(RHS);
1213+
UnsignedOrNone LocalIndex = allocateTemporary(RHS);
12141214
if (!LocalIndex)
12151215
return false;
12161216
LHSOffset = *LocalIndex;
@@ -1385,7 +1385,7 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
13851385

13861386
// Prepare storage for result.
13871387
if (!Initializing && !E->isCompoundAssignmentOp()) {
1388-
std::optional<unsigned> LocalIndex = allocateTemporary(E);
1388+
UnsignedOrNone LocalIndex = allocateTemporary(E);
13891389
if (!LocalIndex)
13901390
return false;
13911391
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -2099,7 +2099,7 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
20992099
ExplicitMemberFn);
21002100
}
21012101

2102-
std::optional<unsigned> LocalIndex =
2102+
UnsignedOrNone LocalIndex =
21032103
allocateLocal(std::move(Source), Arg->getType(),
21042104
/*ExtendingDecl=*/nullptr, ScopeKind::Call);
21052105
if (!LocalIndex)
@@ -2942,7 +2942,7 @@ bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
29422942
return false;
29432943

29442944
const Expr *Inner = E->getSubExpr()->skipRValueSubobjectAdjustments();
2945-
if (std::optional<unsigned> LocalIndex =
2945+
if (UnsignedOrNone LocalIndex =
29462946
allocateLocal(E, Inner->getType(), E->getExtendingDecl())) {
29472947
InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
29482948
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -3019,7 +3019,7 @@ bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
30193019
unsigned LocalIndex;
30203020
if (T)
30213021
LocalIndex = this->allocateLocalPrimitive(Init, *T, /*IsConst=*/false);
3022-
else if (std::optional<unsigned> MaybeIndex = this->allocateLocal(Init))
3022+
else if (UnsignedOrNone MaybeIndex = this->allocateLocal(Init))
30233023
LocalIndex = *MaybeIndex;
30243024
else
30253025
return false;
@@ -3196,7 +3196,7 @@ bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
31963196
if (Ctor->isTrivial())
31973197
return true;
31983198
assert(!Initializing);
3199-
std::optional<unsigned> LocalIndex = allocateLocal(E);
3199+
UnsignedOrNone LocalIndex = allocateLocal(E);
32003200

32013201
if (!LocalIndex)
32023202
return false;
@@ -3406,7 +3406,7 @@ bool Compiler<Emitter>::VisitCXXScalarValueInitExpr(
34063406

34073407
if (const auto *CT = Ty->getAs<ComplexType>()) {
34083408
if (!Initializing) {
3409-
std::optional<unsigned> LocalIndex = allocateLocal(E);
3409+
UnsignedOrNone LocalIndex = allocateLocal(E);
34103410
if (!LocalIndex)
34113411
return false;
34123412
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -3429,7 +3429,7 @@ bool Compiler<Emitter>::VisitCXXScalarValueInitExpr(
34293429
if (const auto *VT = Ty->getAs<VectorType>()) {
34303430
// FIXME: Code duplication with the _Complex case above.
34313431
if (!Initializing) {
3432-
std::optional<unsigned> LocalIndex = allocateLocal(E);
3432+
UnsignedOrNone LocalIndex = allocateLocal(E);
34333433
if (!LocalIndex)
34343434
return false;
34353435
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -4055,8 +4055,7 @@ bool Compiler<Emitter>::VisitExtVectorElementExpr(
40554055

40564056
// Now the vector variable for the return value.
40574057
if (!Initializing) {
4058-
std::optional<unsigned> ResultIndex;
4059-
ResultIndex = allocateLocal(E);
4058+
UnsignedOrNone ResultIndex = allocateLocal(E);
40604059
if (!ResultIndex)
40614060
return false;
40624061
if (!this->emitGetPtrLocal(*ResultIndex, E))
@@ -4180,7 +4179,7 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
41804179
// Create local variable to hold the return value.
41814180
if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
41824181
!canClassify(E->getType())) {
4183-
std::optional<unsigned> LocalIndex = allocateLocal(E);
4182+
UnsignedOrNone LocalIndex = allocateLocal(E);
41844183
if (!LocalIndex)
41854184
return false;
41864185

@@ -4547,10 +4546,10 @@ unsigned Compiler<Emitter>::allocateLocalPrimitive(
45474546
}
45484547

45494548
template <class Emitter>
4550-
std::optional<unsigned>
4551-
Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
4552-
const ValueDecl *ExtendingDecl, ScopeKind SC,
4553-
bool IsConstexprUnknown) {
4549+
UnsignedOrNone Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
4550+
const ValueDecl *ExtendingDecl,
4551+
ScopeKind SC,
4552+
bool IsConstexprUnknown) {
45544553
const ValueDecl *Key = nullptr;
45554554
const Expr *Init = nullptr;
45564555
bool IsTemporary = false;
@@ -4584,7 +4583,7 @@ Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
45844583
}
45854584

45864585
template <class Emitter>
4587-
std::optional<unsigned> Compiler<Emitter>::allocateTemporary(const Expr *E) {
4586+
UnsignedOrNone Compiler<Emitter>::allocateTemporary(const Expr *E) {
45884587
QualType Ty = E->getType();
45894588
assert(!Ty->isRecordType());
45904589

@@ -4663,7 +4662,7 @@ bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
46634662
// Expressions with a composite return type.
46644663
// For us, that means everything we don't
46654664
// have a PrimType for.
4666-
if (std::optional<unsigned> LocalOffset = this->allocateLocal(E)) {
4665+
if (UnsignedOrNone LocalOffset = this->allocateLocal(E)) {
46674666
InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalOffset));
46684667
if (!this->emitGetPtrLocal(*LocalOffset, E))
46694668
return false;
@@ -4860,7 +4859,7 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD,
48604859
return this->emitSetLocal(*VarT, Offset, VD);
48614860
}
48624861
} else {
4863-
if (std::optional<unsigned> Offset = this->allocateLocal(
4862+
if (UnsignedOrNone Offset = this->allocateLocal(
48644863
VD, VD->getType(), nullptr, ScopeKind::Block, IsConstexprUnknown)) {
48654864
if (!Init)
48664865
return true;
@@ -5012,7 +5011,7 @@ bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E,
50125011

50135012
// Non-primitive return type. Prepare storage.
50145013
if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
5015-
std::optional<unsigned> LocalIndex = allocateLocal(E);
5014+
UnsignedOrNone LocalIndex = allocateLocal(E);
50165015
if (!LocalIndex)
50175016
return false;
50185017
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -5108,15 +5107,15 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
51085107
// If we need to discard the return value but the function returns its
51095108
// value via an RVO pointer, we need to create one such pointer just
51105109
// for this call.
5111-
if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
5110+
if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
51125111
if (!this->emitGetPtrLocal(*LocalIndex, E))
51135112
return false;
51145113
}
51155114
} else {
51165115
// We need the result. Prepare a pointer to return or
51175116
// dup the current one.
51185117
if (!Initializing) {
5119-
if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
5118+
if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
51205119
if (!this->emitGetPtrLocal(*LocalIndex, E))
51215120
return false;
51225121
}
@@ -5152,7 +5151,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
51525151
}
51535152

51545153
bool Devirtualized = false;
5155-
std::optional<unsigned> CalleeOffset;
5154+
UnsignedOrNone CalleeOffset = std::nullopt;
51565155
// Add the (optional, implicit) This pointer.
51575156
if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
51585157
if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
@@ -6560,7 +6559,7 @@ bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) {
65606559
OptPrimType ResT = classify(E);
65616560
auto prepareResult = [=]() -> bool {
65626561
if (!ResT && !Initializing) {
6563-
std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
6562+
UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
65646563
if (!LocalIndex)
65656564
return false;
65666565
return this->emitGetPtrLocal(*LocalIndex, E);
@@ -6678,7 +6677,7 @@ bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) {
66786677
return this->delegate(SubExpr);
66796678

66806679
if (!Initializing) {
6681-
std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
6680+
UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
66826681
if (!LocalIndex)
66836682
return false;
66846683
if (!this->emitGetPtrLocal(*LocalIndex, E))
@@ -7282,7 +7281,7 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
72827281

72837282
// Prepare storage for the result in case we discard.
72847283
if (DiscardResult && !Initializing && !ToT) {
7285-
std::optional<unsigned> LocalIndex = allocateLocal(E);
7284+
UnsignedOrNone LocalIndex = allocateLocal(E);
72867285
if (!LocalIndex)
72877286
return false;
72887287
if (!this->emitGetPtrLocal(*LocalIndex, E))

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
316316
bool IsConstexprUnknown = false);
317317

318318
/// Allocates a space storing a local given its type.
319-
std::optional<unsigned>
320-
allocateLocal(DeclTy &&Decl, QualType Ty = QualType(),
321-
const ValueDecl *ExtendingDecl = nullptr,
322-
ScopeKind = ScopeKind::Block, bool IsConstexprUnknown = false);
323-
std::optional<unsigned> allocateTemporary(const Expr *E);
319+
UnsignedOrNone allocateLocal(DeclTy &&Decl, QualType Ty = QualType(),
320+
const ValueDecl *ExtendingDecl = nullptr,
321+
ScopeKind = ScopeKind::Block,
322+
bool IsConstexprUnknown = false);
323+
UnsignedOrNone allocateTemporary(const Expr *E);
324324

325325
private:
326326
friend class VariableScope<Emitter>;
@@ -568,7 +568,7 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
568568

569569
void addLocal(const Scope::Local &Local) override {
570570
if (!Idx) {
571-
Idx = this->Ctx->Descriptors.size();
571+
Idx = static_cast<unsigned>(this->Ctx->Descriptors.size());
572572
this->Ctx->Descriptors.emplace_back();
573573
this->Ctx->emitInitScope(*Idx, {});
574574
}
@@ -616,7 +616,7 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
616616
}
617617

618618
/// Index of the scope in the chain.
619-
std::optional<unsigned> Idx;
619+
UnsignedOrNone Idx = std::nullopt;
620620
};
621621

622622
/// Scope for storage declared in a compound statement.

0 commit comments

Comments
 (0)