Skip to content

Commit df729b3

Browse files
author
Christian Parpart
committed
Make use of C++17 std::optional<> instead of boost::optional<>.
1 parent 302a51a commit df729b3

Some content is hidden

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

67 files changed

+173
-168
lines changed

CODING_STYLE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Use `solAssert` and `solUnimplementedAssert` generously to check assumptions tha
119119
4. Favour declarations close to use; don't habitually declare at top of scope ala C.
120120
5. Pass non-trivial parameters as const reference, unless the data is to be copied into the function, then either pass by const reference or by value and use std::move.
121121
6. If a function returns multiple values, use std::tuple (std::pair acceptable) or better introduce a struct type. Do not use */& arguments.
122-
7. Use parameters of pointer type only if ``nullptr`` is a valid argument, use references otherwise. Often, ``boost::optional`` is better suited than a raw pointer.
122+
7. Use parameters of pointer type only if ``nullptr`` is a valid argument, use references otherwise. Often, ``std::optional`` is better suited than a raw pointer.
123123
8. Never use a macro where adequate non-preprocessor C++ can be written.
124124
9. Only use ``auto`` if the type is very long and rather irrelevant.
125125
10. Do not pass bools: prefer enumerations instead.

libdevcore/CommonData.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525

2626
#include <libdevcore/Common.h>
2727

28-
#include <boost/optional.hpp>
29-
3028
#include <vector>
3129
#include <type_traits>
3230
#include <cstring>
31+
#include <optional>
3332
#include <string>
3433
#include <set>
3534
#include <functional>
@@ -277,7 +276,7 @@ void iterateReplacing(std::vector<T>& _vector, F const& _f)
277276
std::vector<T> modifiedVector;
278277
for (size_t i = 0; i < _vector.size(); ++i)
279278
{
280-
if (boost::optional<std::vector<T>> r = _f(_vector[i]))
279+
if (std::optional<std::vector<T>> r = _f(_vector[i]))
281280
{
282281
if (!useModified)
283282
{
@@ -305,7 +304,7 @@ void iterateReplacingWindow(std::vector<T>& _vector, F const& _f, std::index_seq
305304
size_t i = 0;
306305
for (; i + sizeof...(I) <= _vector.size(); ++i)
307306
{
308-
if (boost::optional<std::vector<T>> r = _f(_vector[i + I]...))
307+
if (std::optional<std::vector<T>> r = _f(_vector[i + I]...))
309308
{
310309
if (!useModified)
311310
{

liblangutil/EVMVersion.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
#include <libevmasm/Instruction.h>
2424

25+
#include <optional>
2526
#include <string>
2627

27-
#include <boost/optional.hpp>
2828
#include <boost/operators.hpp>
2929

3030

@@ -51,12 +51,12 @@ class EVMVersion:
5151
static EVMVersion istanbul() { return {Version::Istanbul}; }
5252
static EVMVersion berlin() { return {Version::Berlin}; }
5353

54-
static boost::optional<EVMVersion> fromString(std::string const& _version)
54+
static std::optional<EVMVersion> fromString(std::string const& _version)
5555
{
5656
for (auto const& v: {homestead(), tangerineWhistle(), spuriousDragon(), byzantium(), constantinople(), petersburg(), istanbul(), berlin()})
5757
if (_version == v.name())
5858
return v;
59-
return {};
59+
return std::nullopt;
6060
}
6161

6262
bool operator==(EVMVersion const& _other) const { return m_version == _other.m_version; }

liblangutil/Scanner.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
#include <liblangutil/Common.h>
5454
#include <liblangutil/Exceptions.h>
5555
#include <liblangutil/Scanner.h>
56-
#include <boost/optional.hpp>
56+
5757
#include <algorithm>
58+
#include <optional>
5859
#include <ostream>
5960
#include <tuple>
6061

@@ -187,7 +188,7 @@ bool Scanner::scanHexByte(char& o_scannedByte)
187188
return true;
188189
}
189190

190-
boost::optional<unsigned> Scanner::scanUnicode()
191+
std::optional<unsigned> Scanner::scanUnicode()
191192
{
192193
unsigned x = 0;
193194
for (int i = 0; i < 4; i++)
@@ -718,7 +719,7 @@ bool Scanner::scanEscape()
718719
break;
719720
case 'u':
720721
{
721-
if (boost::optional<unsigned> codepoint = scanUnicode())
722+
if (auto const codepoint = scanUnicode(); codepoint.has_value())
722723
addUnicodeAsUTF8(*codepoint);
723724
else
724725
return false;

liblangutil/Scanner.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include <liblangutil/SourceLocation.h>
5858
#include <libdevcore/Common.h>
5959
#include <libdevcore/CommonData.h>
60+
61+
#include <optional>
6062
#include <iosfwd>
6163

6264
namespace langutil
@@ -207,7 +209,7 @@ class Scanner
207209
inline Token selectToken(char _next, Token _then, Token _else);
208210

209211
bool scanHexByte(char& o_scannedByte);
210-
boost::optional<unsigned> scanUnicode();
212+
std::optional<unsigned> scanUnicode();
211213

212214
/// Scans a single Solidity token.
213215
void scanToken();

libsolidity/analysis/ReferencesResolver.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ bool ReferencesResolver::visit(ElementaryTypeName const& _typeName)
122122
if (!_typeName.annotation().type)
123123
{
124124
_typeName.annotation().type = TypeProvider::fromElementaryTypeName(_typeName.typeName());
125-
if (_typeName.stateMutability().is_initialized())
125+
if (_typeName.stateMutability().has_value())
126126
{
127127
// for non-address types this was already caught by the parser
128128
solAssert(_typeName.annotation().type->category() == Type::Category::Address, "");
@@ -323,7 +323,7 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
323323
// Will be re-generated later with correct information
324324
// We use the latest EVM version because we will re-run it anyway.
325325
yul::AsmAnalysisInfo analysisInfo;
326-
boost::optional<Error::Type> errorTypeForLoose = Error::Type::SyntaxError;
326+
std::optional<Error::Type> errorTypeForLoose = Error::Type::SyntaxError;
327327
yul::AsmAnalyzer(
328328
analysisInfo,
329329
errorsIgnored,

libsolidity/analysis/ViewPureChecker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void ViewPureChecker::endVisit(InlineAssembly const& _inlineAssembly)
241241
void ViewPureChecker::reportMutability(
242242
StateMutability _mutability,
243243
SourceLocation const& _location,
244-
boost::optional<SourceLocation> const& _nestedLocation
244+
std::optional<SourceLocation> const& _nestedLocation
245245
)
246246
{
247247
if (_mutability > m_bestMutabilityAndLocation.mutability)

libsolidity/analysis/ViewPureChecker.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <map>
2525
#include <memory>
26+
#include <optional>
2627

2728
namespace langutil
2829
{
@@ -67,7 +68,7 @@ class ViewPureChecker: private ASTConstVisitor
6768
void reportMutability(
6869
StateMutability _mutability,
6970
langutil::SourceLocation const& _location,
70-
boost::optional<langutil::SourceLocation> const& _nestedLocation = {}
71+
std::optional<langutil::SourceLocation> const& _nestedLocation = {}
7172
);
7273

7374
std::vector<std::shared_ptr<ASTNode>> const& m_ast;

libsolidity/ast/AST.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <json/json.h>
3737

3838
#include <memory>
39+
#include <optional>
3940
#include <string>
4041
#include <vector>
4142

@@ -912,22 +913,22 @@ class ElementaryTypeName: public TypeName
912913
ElementaryTypeName(
913914
SourceLocation const& _location,
914915
ElementaryTypeNameToken const& _elem,
915-
boost::optional<StateMutability> _stateMutability = {}
916+
std::optional<StateMutability> _stateMutability = {}
916917
): TypeName(_location), m_type(_elem), m_stateMutability(_stateMutability)
917918
{
918-
solAssert(!_stateMutability.is_initialized() || _elem.token() == Token::Address, "");
919+
solAssert(!_stateMutability.has_value() || _elem.token() == Token::Address, "");
919920
}
920921

921922
void accept(ASTVisitor& _visitor) override;
922923
void accept(ASTConstVisitor& _visitor) const override;
923924

924925
ElementaryTypeNameToken const& typeName() const { return m_type; }
925926

926-
boost::optional<StateMutability> const& stateMutability() const { return m_stateMutability; }
927+
std::optional<StateMutability> const& stateMutability() const { return m_stateMutability; }
927928

928929
private:
929930
ElementaryTypeNameToken m_type;
930-
boost::optional<StateMutability> m_stateMutability; ///< state mutability for address type
931+
std::optional<StateMutability> m_stateMutability; ///< state mutability for address type
931932
};
932933

933934
/**

libsolidity/ast/ASTAnnotations.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
#include <libsolidity/ast/ASTEnums.h>
2727
#include <libsolidity/ast/ExperimentalFeatures.h>
2828

29-
#include <boost/optional.hpp>
30-
3129
#include <map>
3230
#include <memory>
31+
#include <optional>
3332
#include <set>
3433
#include <vector>
3534

@@ -183,7 +182,7 @@ struct ExpressionAnnotation: ASTAnnotation
183182

184183
/// Types and - if given - names of arguments if the expr. is a function
185184
/// that is called, used for overload resoultion
186-
boost::optional<FuncCallArguments> arguments;
185+
std::optional<FuncCallArguments> arguments;
187186
};
188187

189188
struct IdentifierAnnotation: ExpressionAnnotation

libsolidity/ast/ASTJsonConverter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Json::Value ASTJsonConverter::typePointerToJson(TypePointer _tp, bool _short)
148148
return typeDescriptions;
149149

150150
}
151-
Json::Value ASTJsonConverter::typePointerToJson(boost::optional<FuncCallArguments> const& _tps)
151+
Json::Value ASTJsonConverter::typePointerToJson(std::optional<FuncCallArguments> const& _tps)
152152
{
153153
if (_tps)
154154
{

libsolidity/ast/ASTJsonConverter.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
#include <liblangutil/Exceptions.h>
2828

2929
#include <json/json.h>
30+
31+
#include <algorithm>
32+
#include <optional>
3033
#include <ostream>
3134
#include <stack>
3235
#include <vector>
33-
#include <algorithm>
3436

3537
namespace langutil
3638
{
@@ -169,7 +171,7 @@ class ASTJsonConverter: public ASTConstVisitor
169171
return json;
170172
}
171173
static Json::Value typePointerToJson(TypePointer _tp, bool _short = false);
172-
static Json::Value typePointerToJson(boost::optional<FuncCallArguments> const& _tps);
174+
static Json::Value typePointerToJson(std::optional<FuncCallArguments> const& _tps);
173175
void appendExpressionAttributes(
174176
std::vector<std::pair<std::string, Json::Value>> &_attributes,
175177
ExpressionAnnotation const& _annotation

libsolidity/ast/TypeProvider.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <array>
2323
#include <map>
2424
#include <memory>
25+
#include <optional>
2526
#include <utility>
2627

2728
namespace dev

libsolidity/ast/Types.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1812,10 +1812,10 @@ TypePointer ArrayType::decodingType() const
18121812

18131813
TypeResult ArrayType::interfaceType(bool _inLibrary) const
18141814
{
1815-
if (_inLibrary && m_interfaceType_library.is_initialized())
1815+
if (_inLibrary && m_interfaceType_library.has_value())
18161816
return *m_interfaceType_library;
18171817

1818-
if (!_inLibrary && m_interfaceType.is_initialized())
1818+
if (!_inLibrary && m_interfaceType.has_value())
18191819
return *m_interfaceType;
18201820

18211821
TypeResult result{TypePointer{}};
@@ -2106,10 +2106,10 @@ MemberList::MemberMap StructType::nativeMembers(ContractDefinition const*) const
21062106

21072107
TypeResult StructType::interfaceType(bool _inLibrary) const
21082108
{
2109-
if (_inLibrary && m_interfaceType_library.is_initialized())
2109+
if (_inLibrary && m_interfaceType_library.has_value())
21102110
return *m_interfaceType_library;
21112111

2112-
if (!_inLibrary && m_interfaceType.is_initialized())
2112+
if (!_inLibrary && m_interfaceType.has_value())
21132113
return *m_interfaceType;
21142114

21152115
TypeResult result{TypePointer{}};
@@ -2166,7 +2166,7 @@ TypeResult StructType::interfaceType(bool _inLibrary) const
21662166
}
21672167
};
21682168

2169-
m_recursive = m_recursive.get() || (CycleDetector<StructDefinition>(visitor).run(structDefinition()) != nullptr);
2169+
m_recursive = m_recursive.value() || (CycleDetector<StructDefinition>(visitor).run(structDefinition()) != nullptr);
21702170

21712171
std::string const recursiveErrMsg = "Recursive type not allowed for public or external contract functions.";
21722172

@@ -2179,13 +2179,13 @@ TypeResult StructType::interfaceType(bool _inLibrary) const
21792179
else
21802180
m_interfaceType_library = TypeProvider::withLocation(this, DataLocation::Memory, true);
21812181

2182-
if (m_recursive.get())
2182+
if (m_recursive.value())
21832183
m_interfaceType = TypeResult::err(recursiveErrMsg);
21842184

21852185
return *m_interfaceType_library;
21862186
}
21872187

2188-
if (m_recursive.get())
2188+
if (m_recursive.value())
21892189
m_interfaceType = TypeResult::err(recursiveErrMsg);
21902190
else if (!result.message().empty())
21912191
m_interfaceType = result;

libsolidity/ast/Types.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
#include <libdevcore/CommonIO.h>
3232
#include <libdevcore/Result.h>
3333

34-
#include <boost/optional.hpp>
3534
#include <boost/rational.hpp>
3635

3736
#include <map>
3837
#include <memory>
38+
#include <optional>
3939
#include <set>
4040
#include <string>
4141

@@ -769,8 +769,8 @@ class ArrayType: public ReferenceType
769769
Type const* m_baseType;
770770
bool m_hasDynamicLength = true;
771771
u256 m_length;
772-
mutable boost::optional<TypeResult> m_interfaceType;
773-
mutable boost::optional<TypeResult> m_interfaceType_library;
772+
mutable std::optional<TypeResult> m_interfaceType;
773+
mutable std::optional<TypeResult> m_interfaceType_library;
774774
};
775775

776776
/**
@@ -865,12 +865,12 @@ class StructType: public ReferenceType
865865

866866
bool recursive() const
867867
{
868-
if (m_recursive.is_initialized())
869-
return m_recursive.get();
868+
if (m_recursive.has_value())
869+
return m_recursive.value();
870870

871871
interfaceType(false);
872872

873-
return m_recursive.get();
873+
return m_recursive.value();
874874
}
875875

876876
std::unique_ptr<ReferenceType> copyForLocation(DataLocation _location, bool _isPointer) const override;
@@ -898,9 +898,9 @@ class StructType: public ReferenceType
898898
private:
899899
StructDefinition const& m_struct;
900900
// Caches for interfaceType(bool)
901-
mutable boost::optional<TypeResult> m_interfaceType;
902-
mutable boost::optional<TypeResult> m_interfaceType_library;
903-
mutable boost::optional<bool> m_recursive;
901+
mutable std::optional<TypeResult> m_interfaceType;
902+
mutable std::optional<TypeResult> m_interfaceType_library;
903+
mutable std::optional<bool> m_recursive;
904904
};
905905

906906
/**

libsolidity/codegen/CompilerContext.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void CompilerContext::appendInlineAssembly(
411411
analyzerResult = yul::AsmAnalyzer(
412412
analysisInfo,
413413
errorReporter,
414-
boost::none,
414+
std::nullopt,
415415
dialect,
416416
identifierAccess.resolve
417417
).analyze(*parserResult);

libsolidity/codegen/YulUtilFunctions.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -962,11 +962,11 @@ string YulUtilFunctions::readFromCalldata(Type const& _type)
962962
return readFromMemoryOrCalldata(_type, true);
963963
}
964964

965-
string YulUtilFunctions::updateStorageValueFunction(Type const& _type, boost::optional<unsigned> const& _offset)
965+
string YulUtilFunctions::updateStorageValueFunction(Type const& _type, std::optional<unsigned> const& _offset)
966966
{
967967
string const functionName =
968968
"update_storage_value_" +
969-
(_offset.is_initialized() ? ("offset_" + to_string(*_offset)) : "") +
969+
(_offset.has_value() ? ("offset_" + to_string(*_offset)) : "") +
970970
_type.identifier();
971971

972972
return m_functionCollector->createFunction(functionName, [&] {
@@ -983,11 +983,11 @@ string YulUtilFunctions::updateStorageValueFunction(Type const& _type, boost::op
983983
)")
984984
("functionName", functionName)
985985
("update",
986-
_offset.is_initialized() ?
986+
_offset.has_value() ?
987987
updateByteSliceFunction(_type.storageBytes(), *_offset) :
988988
updateByteSliceFunctionDynamic(_type.storageBytes())
989989
)
990-
("offset", _offset.is_initialized() ? "" : "offset, ")
990+
("offset", _offset.has_value() ? "" : "offset, ")
991991
("prepare", prepareStoreFunction(_type))
992992
.render();
993993
}

0 commit comments

Comments
 (0)