From 5ac7620045012c2ed565b81978dc35f5f83f7eea Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Mon, 10 Dec 2018 04:25:51 +0100 Subject: Use C++ user-defined literals for creating YulString constants. --- libyul/AsmAnalysis.cpp | 2 +- libyul/AsmParser.cpp | 6 +++--- libyul/AsmPrinter.cpp | 4 ++-- libyul/ObjectParser.cpp | 2 +- libyul/YulString.h | 5 +++++ libyul/backends/evm/EVMCodeTransform.cpp | 2 +- libyul/optimiser/MainFunction.cpp | 4 ++-- 7 files changed, 15 insertions(+), 10 deletions(-) (limited to 'libyul') diff --git a/libyul/AsmAnalysis.cpp b/libyul/AsmAnalysis.cpp index 821da005..0ecc5a30 100644 --- a/libyul/AsmAnalysis.cpp +++ b/libyul/AsmAnalysis.cpp @@ -102,7 +102,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal) else if (_literal.kind == LiteralKind::Boolean) { solAssert(m_dialect->flavour == AsmFlavour::Yul, ""); - solAssert(_literal.value == YulString{string("true")} || _literal.value == YulString{string("false")}, ""); + solAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, ""); } m_info.stackHeightInfo[&_literal] = m_stackHeight; return true; diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index c7302063..f3ca6cd0 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -354,11 +354,11 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() { YulString literal; if (currentToken() == Token::Return) - literal = YulString{"return"}; + literal = "return"_yulstring; else if (currentToken() == Token::Byte) - literal = YulString{"byte"}; + literal = "byte"_yulstring; else if (currentToken() == Token::Address) - literal = YulString{"address"}; + literal = "address"_yulstring; else literal = YulString{currentLiteral()}; // first search the set of builtins, then the instructions. diff --git a/libyul/AsmPrinter.cpp b/libyul/AsmPrinter.cpp index eaaba9f3..4bea1f36 100644 --- a/libyul/AsmPrinter.cpp +++ b/libyul/AsmPrinter.cpp @@ -55,8 +55,8 @@ string AsmPrinter::operator()(Literal const& _literal) solAssert(isValidDecimal(_literal.value.str()) || isValidHex(_literal.value.str()), "Invalid number literal"); return _literal.value.str() + appendTypeName(_literal.type); case LiteralKind::Boolean: - solAssert(_literal.value.str() == "true" || _literal.value.str() == "false", "Invalid bool literal."); - return ((_literal.value.str() == "true") ? "true" : "false") + appendTypeName(_literal.type); + solAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal."); + return ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type); case LiteralKind::String: break; } diff --git a/libyul/ObjectParser.cpp b/libyul/ObjectParser.cpp index ba19e099..5f1eadef 100644 --- a/libyul/ObjectParser.cpp +++ b/libyul/ObjectParser.cpp @@ -42,7 +42,7 @@ shared_ptr ObjectParser::parse(shared_ptr const& _scanner, bool { // Special case: Code-only form. object = make_shared(); - object->name = YulString{"object"}; + object->name = "object"_yulstring; object->code = parseBlock(); if (!object->code) return nullptr; diff --git a/libyul/YulString.h b/libyul/YulString.h index 2179c23b..35c1d92d 100644 --- a/libyul/YulString.h +++ b/libyul/YulString.h @@ -127,4 +127,9 @@ private: YulStringRepository::Handle m_handle{ 0, YulStringRepository::emptyHash() }; }; +inline YulString operator "" _yulstring(const char *_string, std::size_t _size) +{ + return YulString(std::string(_string, _size)); +} + } diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp index bd18985c..04dc5040 100644 --- a/libyul/backends/evm/EVMCodeTransform.cpp +++ b/libyul/backends/evm/EVMCodeTransform.cpp @@ -387,7 +387,7 @@ void CodeTransform::operator()(Literal const& _literal) m_assembly.appendConstant(u256(_literal.value.str())); else if (_literal.kind == LiteralKind::Boolean) { - if (_literal.value.str() == "true") + if (_literal.value == "true"_yulstring) m_assembly.appendConstant(u256(1)); else m_assembly.appendConstant(u256(0)); diff --git a/libyul/optimiser/MainFunction.cpp b/libyul/optimiser/MainFunction.cpp index 63eea2db..fabbf66f 100644 --- a/libyul/optimiser/MainFunction.cpp +++ b/libyul/optimiser/MainFunction.cpp @@ -40,12 +40,12 @@ void MainFunction::operator()(Block& _block) for (size_t i = 1; i < _block.statements.size(); ++i) assertThrow(_block.statements.at(i).type() == typeid(FunctionDefinition), OptimizerException, ""); /// @todo this should handle scopes properly and instead of an assertion it should rename the conflicting function - assertThrow(NameCollector(_block).names().count(YulString{"main"}) == 0, OptimizerException, ""); + assertThrow(NameCollector(_block).names().count("main"_yulstring) == 0, OptimizerException, ""); Block& block = boost::get(_block.statements[0]); FunctionDefinition main{ block.location, - YulString{"main"}, + "main"_yulstring, {}, {}, std::move(block) -- cgit