diff --git a/libsolidity/analysis/PostTypeChecker.cpp b/libsolidity/analysis/PostTypeChecker.cpp index 3361e443639e..2c5dbf5c50f3 100644 --- a/libsolidity/analysis/PostTypeChecker.cpp +++ b/libsolidity/analysis/PostTypeChecker.cpp @@ -473,7 +473,7 @@ class YulLValueChecker : public solidity::yul::ASTWalker if (ranges::any_of( _assignment.variableNames, - [&](auto const& yulIdentifier) { return m_yulNameRepository.labelOf(yulIdentifier.name) == m_identifierName; } + [&](auto const& yulIdentifier) { return m_yulNameRepository.requiredLabelOf(yulIdentifier.name) == m_identifierName; } )) m_willBeWrittenTo = true; } diff --git a/libsolidity/analysis/ReferencesResolver.cpp b/libsolidity/analysis/ReferencesResolver.cpp index 49f688643b56..913df20be2cb 100644 --- a/libsolidity/analysis/ReferencesResolver.cpp +++ b/libsolidity/analysis/ReferencesResolver.cpp @@ -274,11 +274,11 @@ void ReferencesResolver::operator()(yul::FunctionDefinition const& _function) { solAssert(m_yulNameRepository != nullptr); solAssert(nativeLocationOf(_function) == originLocationOf(_function), ""); - validateYulIdentifierName(m_yulNameRepository->labelOf(_function.name), nativeLocationOf(_function)); + validateYulIdentifierName(m_yulNameRepository->requiredLabelOf(_function.name), nativeLocationOf(_function)); for (yul::TypedName const& varName: _function.parameters + _function.returnVariables) { solAssert(nativeLocationOf(varName) == originLocationOf(varName), ""); - validateYulIdentifierName(m_yulNameRepository->labelOf(varName.name), nativeLocationOf(varName)); + validateYulIdentifierName(m_yulNameRepository->requiredLabelOf(varName.name), nativeLocationOf(varName)); } bool wasInsideFunction = m_yulInsideFunction; @@ -291,7 +291,7 @@ void ReferencesResolver::operator()(yul::Identifier const& _identifier) { solAssert(m_yulNameRepository != nullptr); solAssert(nativeLocationOf(_identifier) == originLocationOf(_identifier), ""); - auto const identifierLabel = m_yulNameRepository->labelOf(_identifier.name); + auto const identifierLabel = m_yulNameRepository->requiredLabelOf(_identifier.name); if (m_resolver.experimentalSolidity()) { std::vector splitName; @@ -399,10 +399,10 @@ void ReferencesResolver::operator()(yul::VariableDeclaration const& _varDecl) for (auto const& identifier: _varDecl.variables) { solAssert(nativeLocationOf(identifier) == originLocationOf(identifier), ""); - validateYulIdentifierName(m_yulNameRepository->labelOf(identifier.name), nativeLocationOf(identifier)); + validateYulIdentifierName(m_yulNameRepository->requiredLabelOf(identifier.name), nativeLocationOf(identifier)); if ( - auto declarations = m_resolver.nameFromCurrentScope(std::string(m_yulNameRepository->labelOf(identifier.name))); + auto declarations = m_resolver.nameFromCurrentScope(std::string(m_yulNameRepository->requiredLabelOf(identifier.name))); !declarations.empty() ) { diff --git a/libsolidity/ast/ASTJsonExporter.cpp b/libsolidity/ast/ASTJsonExporter.cpp index 79242773671e..8740b375fa6f 100644 --- a/libsolidity/ast/ASTJsonExporter.cpp +++ b/libsolidity/ast/ASTJsonExporter.cpp @@ -654,7 +654,7 @@ bool ASTJsonExporter::visit(InlineAssembly const& _node) for (auto const& it: _node.annotation().externalReferences) if (it.first) externalReferences.emplace_back(std::make_pair( - _node.operations().nameRepository().labelOf(it.first->name), + _node.operations().nameRepository().requiredLabelOf(it.first->name), inlineAssemblyIdentifierToJson(it) )); diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index 9f9135055400..b56f1f8ef784 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -87,7 +87,7 @@ struct CopyTranslate: public yul::ASTCopier if (m_yulNameRepository.isBuiltinName(_name)) return _name; else - return m_yulNameRepository.defineName("usr$" + std::string(m_yulNameRepository.labelOf(_name))); + return m_yulNameRepository.defineName("usr$" + std::string(m_yulNameRepository.requiredLabelOf(_name))); } yul::Identifier translate(yul::Identifier const& _identifier) override diff --git a/libsolidity/experimental/codegen/IRGeneratorForStatements.cpp b/libsolidity/experimental/codegen/IRGeneratorForStatements.cpp index 64cb101e8014..a40e0e58bea4 100644 --- a/libsolidity/experimental/codegen/IRGeneratorForStatements.cpp +++ b/libsolidity/experimental/codegen/IRGeneratorForStatements.cpp @@ -75,7 +75,7 @@ struct CopyTranslate: public yul::ASTCopier if (m_yulNameRepository.isBuiltinName(_name)) return _name; else - return m_yulNameRepository.defineName("usr$" + std::string(m_yulNameRepository.labelOf(_name))); + return m_yulNameRepository.defineName("usr$" + std::string(m_yulNameRepository.requiredLabelOf(_name))); } yul::Identifier translate(yul::Identifier const& _identifier) override diff --git a/libsolidity/lsp/RenameSymbol.cpp b/libsolidity/lsp/RenameSymbol.cpp index d56316d066ad..4e968d6feb5d 100644 --- a/libsolidity/lsp/RenameSymbol.cpp +++ b/libsolidity/lsp/RenameSymbol.cpp @@ -283,7 +283,7 @@ void RenameSymbol::extractNameAndDeclaration(InlineAssembly const& _inlineAssemb if (location.containsOffset(_cursorBytePosition)) { m_declarationToRename = externalReference.declaration; - m_symbolName = _inlineAssembly.operations().nameRepository().labelOf(identifier->name); + m_symbolName = _inlineAssembly.operations().nameRepository().requiredLabelOf(identifier->name); if (!externalReference.suffix.empty()) m_symbolName = m_symbolName.substr(0, m_symbolName.length() - externalReference.suffix.size() - 1); @@ -296,7 +296,7 @@ void RenameSymbol::Visitor::endVisit(InlineAssembly const& _node) { for (auto&& [identifier, externalReference]: _node.annotation().externalReferences) { - auto identifierName = _node.operations().nameRepository().labelOf(identifier->name); + auto identifierName = _node.operations().nameRepository().requiredLabelOf(identifier->name); if (!externalReference.suffix.empty()) identifierName = identifierName.substr(0, identifierName.length() - externalReference.suffix.size() - 1); diff --git a/libyul/AsmAnalysis.cpp b/libyul/AsmAnalysis.cpp index 9b26abe572e9..8c17f6cebf3b 100644 --- a/libyul/AsmAnalysis.cpp +++ b/libyul/AsmAnalysis.cpp @@ -126,7 +126,7 @@ std::vector AsmAnalyzer::operator()(Literal const& _literal) nativeLocationOf(_literal), fmt::format( R"(Invalid type "{}" for literal "{}".)", - m_yulNameRepository.labelOf(_literal.type), + m_yulNameRepository.requiredLabelOf(_literal.type), formatLiteral(_literal, false) ) ); @@ -151,7 +151,7 @@ std::vector AsmAnalyzer::operator()(Identifier const& _identifier) nativeLocationOf(_identifier), fmt::format( "Variable {} used before it was declared.", - m_yulNameRepository.labelOf(_identifier.name) + m_yulNameRepository.requiredLabelOf(_identifier.name) ) ); type = _var.type; @@ -161,7 +161,7 @@ std::vector AsmAnalyzer::operator()(Identifier const& _identifier) m_errorReporter.typeError( 6041_error, nativeLocationOf(_identifier), - fmt::format("Function {} used without being called.", m_yulNameRepository.labelOf(_identifier.name)) + fmt::format("Function {} used without being called.", m_yulNameRepository.requiredLabelOf(_identifier.name)) ); } })) @@ -186,7 +186,7 @@ std::vector AsmAnalyzer::operator()(Identifier const& _identifier) m_errorReporter.declarationError( 8198_error, nativeLocationOf(_identifier), - fmt::format("Identifier \"{}\" not found.", m_yulNameRepository.labelOf(_identifier.name)) + fmt::format("Identifier \"{}\" not found.", m_yulNameRepository.requiredLabelOf(_identifier.name)) ); } @@ -224,7 +224,7 @@ void AsmAnalyzer::operator()(Assignment const& _assignment) nativeLocationOf(_assignment), fmt::format( "Variable {} occurs multiple times on the left-hand side of the assignment.", - m_yulNameRepository.labelOf(_variableName.name) + m_yulNameRepository.requiredLabelOf(_variableName.name) ) ); @@ -236,7 +236,7 @@ void AsmAnalyzer::operator()(Assignment const& _assignment) nativeLocationOf(_assignment), fmt::format( "Variable count for assignment to \"{}\" does not match number of values ({} vs. {})", - joinHumanReadable(applyMap(_assignment.variableNames, [this](auto const& _identifier){ return m_yulNameRepository.labelOf(_identifier.name); })), + joinHumanReadable(applyMap(_assignment.variableNames, [this](auto const& _identifier){ return m_yulNameRepository.requiredLabelOf(_identifier.name); })), numVariables, types.size() ) @@ -273,7 +273,7 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl) nativeLocationOf(_varDecl), fmt::format( "Variable count mismatch for declaration of \"{}\": {} variables and {} values.", - joinHumanReadable(applyMap(_varDecl.variables, [this](auto const& _identifier){ return m_yulNameRepository.labelOf(_identifier.name); })), + joinHumanReadable(applyMap(_varDecl.variables, [this](auto const& _identifier){ return m_yulNameRepository.requiredLabelOf(_identifier.name); })), numVariables, types.size() ) @@ -291,8 +291,8 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl) nativeLocationOf(variable), fmt::format( R"(Assigning value of type "{}" to variable of type "{}".)", - m_yulNameRepository.labelOf(givenType), - m_yulNameRepository.labelOf(variable.type) + m_yulNameRepository.requiredLabelOf(givenType), + m_yulNameRepository.requiredLabelOf(variable.type) ) ); } @@ -395,7 +395,7 @@ std::vector AsmAnalyzer::operator()(FunctionCall const& _funCall) m_errorReporter.declarationError( 4619_error, nativeLocationOf(_funCall.functionName), - fmt::format("Function \"{}\" not found.", m_yulNameRepository.labelOf(_funCall.functionName.name)) + fmt::format("Function \"{}\" not found.", m_yulNameRepository.requiredLabelOf(_funCall.functionName.name)) ); yulAssert(!watcher.ok(), "Expected a reported error."); } @@ -406,7 +406,7 @@ std::vector AsmAnalyzer::operator()(FunctionCall const& _funCall) nativeLocationOf(_funCall.functionName), fmt::format( "Function \"{}\" expects {} arguments but got {}.", - m_yulNameRepository.labelOf(_funCall.functionName.name), + m_yulNameRepository.requiredLabelOf(_funCall.functionName.name), parameterTypes->size(), _funCall.arguments.size() ) @@ -597,8 +597,8 @@ void AsmAnalyzer::expectBoolExpression(Expression const& _expr) nativeLocationOf(_expr), fmt::format( R"(Expected a value of boolean type "{}" but got "{}")", - m_yulNameRepository.labelOf(m_yulNameRepository.predefined().boolType), - m_yulNameRepository.labelOf(type) + m_yulNameRepository.requiredLabelOf(m_yulNameRepository.predefined().boolType), + m_yulNameRepository.requiredLabelOf(type) ) ); } @@ -625,7 +625,7 @@ void AsmAnalyzer::checkAssignment(Identifier const& _variable, Type _valueType) m_errorReporter.declarationError( 1133_error, nativeLocationOf(_variable), - fmt::format("Variable {} used before it was declared.", m_yulNameRepository.labelOf(_variable.name)) + fmt::format("Variable {} used before it was declared.", m_yulNameRepository.requiredLabelOf(_variable.name)) ); else variableType = &std::get(*var).type; @@ -650,8 +650,8 @@ void AsmAnalyzer::checkAssignment(Identifier const& _variable, Type _valueType) nativeLocationOf(_variable), fmt::format( R"(Assigning a value of type "{}" to a variable of type "{}".)", - m_yulNameRepository.labelOf(_valueType), - m_yulNameRepository.labelOf(*variableType) + m_yulNameRepository.requiredLabelOf(_valueType), + m_yulNameRepository.requiredLabelOf(*variableType) ) ); @@ -668,7 +668,7 @@ Scope& AsmAnalyzer::scope(Block const* _block) void AsmAnalyzer::expectValidIdentifier(YulName _identifier, SourceLocation const& _location) { - auto const label = m_yulNameRepository.labelOf(_identifier); + auto const label = m_yulNameRepository.requiredLabelOf(_identifier); // NOTE: the leading dot case is handled by the parser not allowing it. if (boost::ends_with(label, ".")) m_errorReporter.syntaxError( @@ -698,7 +698,7 @@ void AsmAnalyzer::expectValidType(Type _type, SourceLocation const& _location) m_errorReporter.typeError( 5473_error, _location, - fmt::format("\"{}\" is not a valid type (user defined types are not yet supported).", m_yulNameRepository.labelOf(_type)) + fmt::format("\"{}\" is not a valid type (user defined types are not yet supported).", m_yulNameRepository.requiredLabelOf(_type)) ); } @@ -710,8 +710,8 @@ void AsmAnalyzer::expectType(Type _expectedType, Type _givenType, SourceLocation _location, fmt::format( R"(Expected a value of type "{}" but got "{}".)", - m_yulNameRepository.labelOf(_expectedType), - m_yulNameRepository.labelOf(_givenType) + m_yulNameRepository.requiredLabelOf(_expectedType), + m_yulNameRepository.requiredLabelOf(_givenType) ) ); } @@ -806,5 +806,5 @@ bool AsmAnalyzer::validateInstructions(evmasm::Instruction _instr, SourceLocatio bool AsmAnalyzer::validateInstructions(FunctionCall const& _functionCall) { - return validateInstructions(m_yulNameRepository.labelOf(_functionCall.functionName.name), nativeLocationOf(_functionCall.functionName)); + return validateInstructions(m_yulNameRepository.requiredLabelOf(_functionCall.functionName.name), nativeLocationOf(_functionCall.functionName)); } diff --git a/libyul/AsmJsonConverter.cpp b/libyul/AsmJsonConverter.cpp index 69b6803920d5..51e7e281d55d 100644 --- a/libyul/AsmJsonConverter.cpp +++ b/libyul/AsmJsonConverter.cpp @@ -45,8 +45,8 @@ Json AsmJsonConverter::operator()(TypedName const& _node) const { yulAssert(YulNameRepository::emptyName() != _node.name, "Invalid variable name."); Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulTypedName"); - ret["name"] = m_yulNameRepository.labelOf(_node.name); - ret["type"] = m_yulNameRepository.labelOf(_node.type); + ret["name"] = m_yulNameRepository.requiredLabelOf(_node.name); + ret["type"] = m_yulNameRepository.requiredLabelOf(_node.type); return ret; } @@ -67,7 +67,7 @@ Json AsmJsonConverter::operator()(Literal const& _node) const ret["hexValue"] = util::toHex(util::asBytes(formatLiteral(_node))); break; } - ret["type"] = m_yulNameRepository.labelOf(_node.type); + ret["type"] = m_yulNameRepository.requiredLabelOf(_node.type); { auto const formattedLiteral = formatLiteral(_node); if (util::validateUTF8(formattedLiteral)) @@ -80,7 +80,7 @@ Json AsmJsonConverter::operator()(Identifier const& _node) const { yulAssert(YulNameRepository::emptyName() != _node.name, "Invalid identifier"); Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulIdentifier"); - ret["name"] = m_yulNameRepository.labelOf(_node.name); + ret["name"] = m_yulNameRepository.requiredLabelOf(_node.name); return ret; } @@ -122,7 +122,7 @@ Json AsmJsonConverter::operator()(FunctionDefinition const& _node) const { yulAssert(YulNameRepository::emptyName() != _node.name, "Invalid function name."); Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulFunctionDefinition"); - ret["name"] = m_yulNameRepository.labelOf(_node.name); + ret["name"] = m_yulNameRepository.requiredLabelOf(_node.name); for (auto const& var: _node.parameters) ret["parameters"].emplace_back((*this)(var)); for (auto const& var: _node.returnVariables) diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index fb2667cacca3..feee5aac299a 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -442,7 +442,7 @@ Statement Parser::parseStatement(YulNameRepository& _nameRepository) 6272_error, fmt::format( "Cannot assign to builtin function \"{}\".", - _nameRepository.labelOf(identifier.name) + _nameRepository.requiredLabelOf(identifier.name) ) ); @@ -530,7 +530,7 @@ Expression Parser::parseExpression(YulNameRepository& _nameRepository, bool _unl fatalParserError( 7104_error, nativeLocationOf(_identifier), - fmt::format("Builtin function \"{}\" must be called.", _nameRepository.labelOf(_identifier.name)) + fmt::format("Builtin function \"{}\" must be called.", _nameRepository.requiredLabelOf(_identifier.name)) ); return std::move(_identifier); }, diff --git a/libyul/AsmPrinter.cpp b/libyul/AsmPrinter.cpp index a7f335080c60..547e306f82e9 100644 --- a/libyul/AsmPrinter.cpp +++ b/libyul/AsmPrinter.cpp @@ -66,7 +66,7 @@ std::string AsmPrinter::operator()(Literal const& _literal) std::string AsmPrinter::operator()(Identifier const& _identifier) { yulAssert(_identifier.name != YulNameRepository::emptyName(), "Invalid identifier."); - return formatDebugData(_identifier) + std::string(m_nameRepository.labelOf(_identifier.name)); + return formatDebugData(_identifier) + std::string(m_nameRepository.requiredLabelOf(_identifier.name)); } std::string AsmPrinter::operator()(ExpressionStatement const& _statement) @@ -112,7 +112,7 @@ std::string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition yulAssert(_functionDefinition.name != YulNameRepository::emptyName(), "Invalid function name."); std::string out = formatDebugData(_functionDefinition); - out += "function " + std::string(m_nameRepository.labelOf(_functionDefinition.name)) + "("; + out += "function " + std::string(m_nameRepository.requiredLabelOf(_functionDefinition.name)) + "("; out += boost::algorithm::join( _functionDefinition.parameters | ranges::views::transform( [this](TypedName argument) { return formatTypedName(argument); } @@ -240,7 +240,7 @@ std::string AsmPrinter::operator()(Block const& _block) std::string AsmPrinter::formatTypedName(TypedName const& _variable) { yulAssert(_variable.name != YulNameRepository::emptyName(), "Invalid variable name."); - return formatDebugData(_variable) + std::string(m_nameRepository.labelOf(_variable.name)) + appendTypeName(_variable.type); + return formatDebugData(_variable) + std::string(m_nameRepository.requiredLabelOf(_variable.name)) + appendTypeName(_variable.type); } std::string AsmPrinter::appendTypeName(Type _type, bool const _isBoolLiteral) const @@ -256,7 +256,7 @@ std::string AsmPrinter::appendTypeName(Type _type, bool const _isBoolLiteral) co if (_type == YulNameRepository::emptyName()) return {}; else - return fmt::format(":{}", m_nameRepository.labelOf(_type)); + return fmt::format(":{}", m_nameRepository.requiredLabelOf(_type)); } std::string AsmPrinter::formatSourceLocation( diff --git a/libyul/ScopeFiller.cpp b/libyul/ScopeFiller.cpp index 63dddfcab433..155a138f6b20 100644 --- a/libyul/ScopeFiller.cpp +++ b/libyul/ScopeFiller.cpp @@ -142,7 +142,7 @@ bool ScopeFiller::registerVariable(TypedName const& _name, SourceLocation const& m_errorReporter.declarationError( 1395_error, _location, - fmt::format("Variable name {} already taken in this scope.", m_yulNameRegistry.labelOf(_name.name)) + fmt::format("Variable name {} already taken in this scope.", m_yulNameRegistry.requiredLabelOf(_name.name)) ); return false; } @@ -163,7 +163,7 @@ bool ScopeFiller::registerFunction(FunctionDefinition const& _funDef) m_errorReporter.declarationError( 6052_error, nativeLocationOf(_funDef), - fmt::format("Function name {} already taken in this scope.", m_yulNameRegistry.labelOf(_funDef.name)) + fmt::format("Function name {} already taken in this scope.", m_yulNameRegistry.requiredLabelOf(_funDef.name)) ); return false; } diff --git a/libyul/YulName.cpp b/libyul/YulName.cpp index 32bf777e3ddb..03f5187c1170 100644 --- a/libyul/YulName.cpp +++ b/libyul/YulName.cpp @@ -163,6 +163,13 @@ std::optional YulNameRepository::labelOf(YulName const _name) return std::nullopt; } +std::string_view YulNameRepository::requiredLabelOf(YulName const _name) const +{ + auto const label = labelOf(_name); + yulAssert(label.has_value(), "YulName currently has no defined label in the YulNameRepository."); + return label.value(); +} + YulNameRepository::YulName YulNameRepository::baseNameOf(YulName _name) const { yulAssert(nameWithinBounds(_name), "YulName exceeds repository size, probably stems from another instance."); diff --git a/libyul/YulName.h b/libyul/YulName.h index d83123d5506a..1fd5ac92f0cb 100644 --- a/libyul/YulName.h +++ b/libyul/YulName.h @@ -102,6 +102,10 @@ class YulNameRepository /// generated with ``generateLabels``, or it is a builtin. std::optional labelOf(YulName _name) const; + /// If it can be assumed that the label was already generated, this function will yield it (or fail with an + /// assertion error). + std::string_view requiredLabelOf(YulName _name) const; + /// Yields the name that the provided name was based on - or the name itself, if the name was directly "defined". YulName baseNameOf(YulName _name) const; @@ -157,16 +161,6 @@ class YulNameRepository void generateLabels(Block const& _ast, std::set const& _illegal = {}); private: - bool nameWithinBounds(YulName const _name) const { return _name < m_index; } - - size_t indexOfType(YulName _type) const; - BuiltinFunction convertBuiltinFunction(YulName _name, yul::BuiltinFunction const& _builtin) const; - BuiltinFunction const* fetchTypedPredefinedFunction(YulName _type, std::vector> const& _functions) const; - - Dialect const& m_dialect; - std::vector> m_dialectTypes; - std::map m_builtinFunctions; - struct PredefinedBuiltinFunctions { std::vector> discardFunctions; @@ -186,8 +180,8 @@ class YulNameRepository size_t endBuiltins {}; }; enum class YulNameState { DERIVED, DEFINED }; - bool nameWithinBounds(YulName const _name) const { return _name < m_index; } + bool nameWithinBounds(YulName const _name) const { return _name < m_index; } size_t indexOfType(YulName _type) const; BuiltinFunction convertBuiltinFunction(YulName _name, yul::BuiltinFunction const& _builtin) const; BuiltinFunction const* fetchTypedPredefinedFunction(YulName _type, std::vector> const& _functions) const; diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp index e23b68ec9141..bb85a4fb9f93 100644 --- a/libyul/backends/evm/EVMCodeTransform.cpp +++ b/libyul/backends/evm/EVMCodeTransform.cpp @@ -465,7 +465,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) static_cast(stackLayout.size()) - 17, fmt::format( "The function {} has {} parameters or return variables too many to fit the stack size.", - m_yulNameRepository.labelOf(_function.name), stackLayout.size() - 17 + m_yulNameRepository.requiredLabelOf(_function.name), stackLayout.size() - 17 ) ); stackError(std::move(error), m_assembly.stackHeight() - static_cast(_function.parameters.size())); @@ -605,7 +605,7 @@ void CodeTransform::createFunctionEntryID(FunctionDefinition const& _function) !nameAlreadySeen ) ? m_assembly.namedLabel( - std::string(m_yulNameRepository.labelOf(_function.name)), + std::string(m_yulNameRepository.requiredLabelOf(_function.name)), _function.parameters.size(), _function.returnVariables.size(), astID @@ -787,7 +787,7 @@ size_t CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulName _v heightDiff - limit, fmt::format( "Variable {} is {} slot(s) too deep inside the stack. {}", - m_yulNameRepository.labelOf(_varName), heightDiff - limit, stackTooDeepString + m_yulNameRepository.requiredLabelOf(_varName), heightDiff - limit, stackTooDeepString ) ); m_assembly.markAsInvalid(); diff --git a/libyul/backends/evm/EVMDialect.cpp b/libyul/backends/evm/EVMDialect.cpp index 7aca938fa872..82680acc2c57 100644 --- a/libyul/backends/evm/EVMDialect.cpp +++ b/libyul/backends/evm/EVMDialect.cpp @@ -476,13 +476,6 @@ std::set EVMDialect::builtinNames() const { return { keys.begin(), keys.end() }; } -std::set EVMDialect::builtinNames() const { - static std::array verbatim {YulString("verbatim")}; - auto const keys = ranges::views::concat(ranges::views::keys(m_functions), verbatim) - | ranges::views::transform([](auto const& name) { return name.str(); }); - return { keys.begin(), keys.end() }; -} - EVMDialectTyped::EVMDialectTyped(langutil::EVMVersion _evmVersion, bool _objectAccess): EVMDialect(_evmVersion, _objectAccess) { diff --git a/libyul/backends/evm/OptimizedEVMCodeTransform.cpp b/libyul/backends/evm/OptimizedEVMCodeTransform.cpp index a9726ffbf1e8..651fb226489c 100644 --- a/libyul/backends/evm/OptimizedEVMCodeTransform.cpp +++ b/libyul/backends/evm/OptimizedEVMCodeTransform.cpp @@ -197,7 +197,7 @@ OptimizedEVMCodeTransform::OptimizedEVMCodeTransform( bool useNamedLabel = _useNamedLabelsForFunctions != UseNamedLabels::Never && !nameAlreadySeen; functionLabels[&functionInfo] = useNamedLabel ? m_assembly.namedLabel( - std::string(_yulNameRepository.labelOf(function->name)), + std::string(_yulNameRepository.requiredLabelOf(function->name)), function->arguments.size(), function->returns.size(), functionInfo.debugData ? functionInfo.debugData->astID : std::nullopt @@ -270,8 +270,8 @@ void OptimizedEVMCodeTransform::createStackLayout(langutil::DebugData::ConstPtr YulName varNameDeep = slotVariableName(deepSlot); YulName varNameTop = slotVariableName(m_stack.back()); std::string msg = - "Cannot swap " + (varNameDeep == YulNameRepository::emptyName() ? "Slot " + stackSlotToString(deepSlot, m_yulNameRepository) : "Variable " + std::string(m_yulNameRepository.labelOf(varNameDeep))) + - " with " + (varNameTop == YulNameRepository::emptyName() ? "Slot " + stackSlotToString(m_stack.back(), m_yulNameRepository) : "Variable " + std::string(m_yulNameRepository.labelOf(varNameTop))) + + "Cannot swap " + (varNameDeep == YulNameRepository::emptyName() ? "Slot " + stackSlotToString(deepSlot, m_yulNameRepository) : "Variable " + std::string(m_yulNameRepository.requiredLabelOf(varNameDeep))) + + " with " + (varNameTop == YulNameRepository::emptyName() ? "Slot " + stackSlotToString(m_stack.back(), m_yulNameRepository) : "Variable " + std::string(m_yulNameRepository.requiredLabelOf(varNameTop))) + ": too deep in the stack by " + std::to_string(deficit) + " slots in " + stackToString(m_stack, m_yulNameRepository); m_stackErrors.emplace_back(StackTooDeepError( m_currentFunctionInfo ? m_currentFunctionInfo->function.name : YulNameRepository::emptyName(), @@ -300,7 +300,7 @@ void OptimizedEVMCodeTransform::createStackLayout(langutil::DebugData::ConstPtr int deficit = static_cast(*depth - 15); YulName varName = slotVariableName(_slot); std::string msg = - (varName == YulNameRepository::emptyName() ? "Slot " + stackSlotToString(_slot, m_yulNameRepository) : "Variable " + std::string(m_yulNameRepository.labelOf(varName))) + (varName == YulNameRepository::emptyName() ? "Slot " + stackSlotToString(_slot, m_yulNameRepository) : "Variable " + std::string(m_yulNameRepository.requiredLabelOf(varName))) + " is " + std::to_string(*depth - 15) + " too deep in the stack " + stackToString(m_stack, m_yulNameRepository); m_stackErrors.emplace_back(StackTooDeepError( m_currentFunctionInfo ? m_currentFunctionInfo->function.name : YulNameRepository::emptyName(), diff --git a/libyul/backends/evm/StackHelpers.h b/libyul/backends/evm/StackHelpers.h index 4644511c9488..a0a6387abbf7 100644 --- a/libyul/backends/evm/StackHelpers.h +++ b/libyul/backends/evm/StackHelpers.h @@ -36,11 +36,11 @@ namespace solidity::yul inline std::string stackSlotToString(StackSlot const& _slot, YulNameRepository const& _yulNameRepository) { return std::visit(util::GenericVisitor{ - [&_yulNameRepository](FunctionCallReturnLabelSlot const& _ret) -> std::string { return fmt::format("RET[{}]", _yulNameRepository.labelOf(_ret.call.get().functionName.name)); }, + [&_yulNameRepository](FunctionCallReturnLabelSlot const& _ret) -> std::string { return fmt::format("RET[{}]", _yulNameRepository.requiredLabelOf(_ret.call.get().functionName.name)); }, [](FunctionReturnLabelSlot const&) -> std::string { return "RET"; }, - [&_yulNameRepository](VariableSlot const& _var) { return std::string(_yulNameRepository.labelOf(_var.variable.get().name)); }, + [&_yulNameRepository](VariableSlot const& _var) { return std::string(_yulNameRepository.requiredLabelOf(_var.variable.get().name)); }, [](LiteralSlot const& _lit) { return toCompactHexWithPrefix(_lit.value); }, - [&_yulNameRepository](TemporarySlot const& _tmp) -> std::string { return fmt::format("TMP[{}, {}]", _yulNameRepository.labelOf(_tmp.call.get().functionName.name), _tmp.index); }, + [&_yulNameRepository](TemporarySlot const& _tmp) -> std::string { return fmt::format("TMP[{}, {}]", _yulNameRepository.requiredLabelOf(_tmp.call.get().functionName.name), _tmp.index); }, [](JunkSlot const&) -> std::string { return "JUNK"; } }, _slot); } diff --git a/test/libyul/CompilabilityChecker.cpp b/test/libyul/CompilabilityChecker.cpp index c90474b00140..6052fa81fe16 100644 --- a/test/libyul/CompilabilityChecker.cpp +++ b/test/libyul/CompilabilityChecker.cpp @@ -40,7 +40,7 @@ std::string check(std::string const& _input) // recast into map string -> int s.t. order is predictable std::map labelledFunctions; for(const auto& [k, v] : functions) - labelledFunctions[std::string(obj.code->nameRepository().labelOf(k))] = v; + labelledFunctions[std::string(obj.code->nameRepository().requiredLabelOf(k))] = v; std::string out; for (auto const& function: labelledFunctions) out += function.first + ": " + std::to_string(function.second) + " "; diff --git a/test/libyul/ControlFlowGraphTest.cpp b/test/libyul/ControlFlowGraphTest.cpp index 7423df366110..af80800ab9c6 100644 --- a/test/libyul/ControlFlowGraphTest.cpp +++ b/test/libyul/ControlFlowGraphTest.cpp @@ -53,7 +53,7 @@ namespace { static std::string variableSlotToString(VariableSlot const& _slot, YulNameRepository const& _yulNameRepository) { - return std::string{_yulNameRepository.labelOf(_slot.variable.get().name)}; + return std::string{_yulNameRepository.requiredLabelOf(_slot.variable.get().name)}; } } @@ -83,8 +83,8 @@ class ControlFlowGraphPrinter CFG::FunctionInfo const& _info ) { - m_stream << "FunctionEntry_" << m_yulNameRepository.labelOf(_info.function.name) << "_" << getBlockId(*_info.entry) << " [label=\""; - m_stream << "function " << m_yulNameRepository.labelOf(_info.function.name) << "("; + m_stream << "FunctionEntry_" << m_yulNameRepository.requiredLabelOf(_info.function.name) << "_" << getBlockId(*_info.entry) << " [label=\""; + m_stream << "function " << m_yulNameRepository.requiredLabelOf(_info.function.name) << "("; m_stream << joinHumanReadable(_info.parameters | ranges::views::transform([&](auto const& var) { return variableSlotToString(var, m_yulNameRepository); })); m_stream << ")"; if (!_info.returnVariables.empty()) @@ -93,7 +93,7 @@ class ControlFlowGraphPrinter m_stream << joinHumanReadable(_info.returnVariables | ranges::views::transform([&](auto const& var) { return variableSlotToString(var, m_yulNameRepository); })); } m_stream << "\"];\n"; - m_stream << "FunctionEntry_" << m_yulNameRepository.labelOf(_info.function.name) << "_" << getBlockId(*_info.entry) << " -> Block" << getBlockId(*_info.entry) << ";\n"; + m_stream << "FunctionEntry_" << m_yulNameRepository.requiredLabelOf(_info.function.name) << "_" << getBlockId(*_info.entry) << " -> Block" << getBlockId(*_info.entry) << ";\n"; (*this)(*_info.entry, false); } @@ -126,10 +126,10 @@ class ControlFlowGraphPrinter { std::visit(util::GenericVisitor{ [&](CFG::FunctionCall const& _call) { - m_stream << m_yulNameRepository.labelOf(_call.function.get().name) << ": "; + m_stream << m_yulNameRepository.requiredLabelOf(_call.function.get().name) << ": "; }, [&](CFG::BuiltinCall const& _call) { - m_stream << m_yulNameRepository.labelOf(_call.functionCall.get().functionName.name) << ": "; + m_stream << m_yulNameRepository.requiredLabelOf(_call.functionCall.get().functionName.name) << ": "; }, [&](CFG::Assignment const& _assignment) { @@ -169,7 +169,7 @@ class ControlFlowGraphPrinter }, [&](CFG::BasicBlock::FunctionReturn const& _return) { - m_stream << "Block" << getBlockId(_block) << "Exit [label=\"FunctionReturn[" << m_yulNameRepository.labelOf(_return.info->function.name) << "]\"];\n"; + m_stream << "Block" << getBlockId(_block) << "Exit [label=\"FunctionReturn[" << m_yulNameRepository.requiredLabelOf(_return.info->function.name) << "]\"];\n"; m_stream << "Block" << getBlockId(_block) << " -> Block" << getBlockId(_block) << "Exit;\n"; }, [&](CFG::BasicBlock::Terminated const&) diff --git a/test/libyul/ControlFlowSideEffectsTest.cpp b/test/libyul/ControlFlowSideEffectsTest.cpp index 9d302965db6d..ff69a56eba8b 100644 --- a/test/libyul/ControlFlowSideEffectsTest.cpp +++ b/test/libyul/ControlFlowSideEffectsTest.cpp @@ -69,7 +69,7 @@ TestCase::TestResult ControlFlowSideEffectsTest::run(std::ostream& _stream, std: m_obtainedResult.clear(); forEach(obj.code->block(), [&](FunctionDefinition const& _fun) { std::string effectStr = toString(sideEffects.functionSideEffects().at(&_fun)); - m_obtainedResult += std::string(obj.code->nameRepository().labelOf(_fun.name)) + (effectStr.empty() ? ":" : ": " + effectStr) + "\n"; + m_obtainedResult += std::string(obj.code->nameRepository().requiredLabelOf(_fun.name)) + (effectStr.empty() ? ":" : ": " + effectStr) + "\n"; }); return checkResult(_stream, _linePrefix, _formatted); diff --git a/test/libyul/FunctionSideEffects.cpp b/test/libyul/FunctionSideEffects.cpp index 6d58d7eae6a2..ee0713e0851a 100644 --- a/test/libyul/FunctionSideEffects.cpp +++ b/test/libyul/FunctionSideEffects.cpp @@ -94,7 +94,7 @@ TestCase::TestResult FunctionSideEffects::run(std::ostream& _stream, std::string std::map functionSideEffectsStr; for (auto const& fun: functionSideEffects) - functionSideEffectsStr[obj.code->nameRepository().labelOf(fun.first)] = toString(fun.second); + functionSideEffectsStr[obj.code->nameRepository().requiredLabelOf(fun.first)] = toString(fun.second); m_obtainedResult.clear(); for (auto const& fun: functionSideEffectsStr) diff --git a/test/libyul/StackLayoutGeneratorTest.cpp b/test/libyul/StackLayoutGeneratorTest.cpp index 02847dfdebe9..053ac3601ae0 100644 --- a/test/libyul/StackLayoutGeneratorTest.cpp +++ b/test/libyul/StackLayoutGeneratorTest.cpp @@ -57,7 +57,7 @@ namespace { static std::string variableSlotToString(VariableSlot const& _slot, YulNameRepository const& _yulNameRepository) { - return std::string(_yulNameRepository.labelOf(_slot.variable.get().name)); + return std::string(_yulNameRepository.requiredLabelOf(_slot.variable.get().name)); } } @@ -87,8 +87,8 @@ class StackLayoutPrinter CFG::FunctionInfo const& _info ) { - m_stream << "FunctionEntry_" << m_yulNameRepository.labelOf(_info.function.name) << " [label=\""; - m_stream << "function " << m_yulNameRepository.labelOf(_info.function.name) << "("; + m_stream << "FunctionEntry_" << m_yulNameRepository.requiredLabelOf(_info.function.name) << " [label=\""; + m_stream << "function " << m_yulNameRepository.requiredLabelOf(_info.function.name) << "("; m_stream << joinHumanReadable(_info.parameters | ranges::views::transform([&](auto const& var) { return variableSlotToString(var, m_yulNameRepository); })); m_stream << ")"; if (!_info.returnVariables.empty()) @@ -100,7 +100,7 @@ class StackLayoutPrinter Stack functionEntryStack = {FunctionReturnLabelSlot{_info.function}}; functionEntryStack += _info.parameters | ranges::views::reverse; m_stream << stackToString(functionEntryStack, m_yulNameRepository) << "\"];\n"; - m_stream << "FunctionEntry_" << m_yulNameRepository.labelOf(_info.function.name) << " -> Block" << getBlockId(*_info.entry) << ";\n"; + m_stream << "FunctionEntry_" << m_yulNameRepository.requiredLabelOf(_info.function.name) << " -> Block" << getBlockId(*_info.entry) << ";\n"; (*this)(*_info.entry, false); } @@ -137,10 +137,10 @@ class StackLayoutPrinter m_stream << stackToString(m_stackLayout.operationEntryLayout.at(&operation), m_yulNameRepository) << "\\l\\\n"; std::visit(util::GenericVisitor{ [&](CFG::FunctionCall const& _call) { - m_stream << m_yulNameRepository.labelOf(_call.function.get().name); + m_stream << m_yulNameRepository.requiredLabelOf(_call.function.get().name); }, [&](CFG::BuiltinCall const& _call) { - m_stream << m_yulNameRepository.labelOf(_call.functionCall.get().functionName.name); + m_stream << m_yulNameRepository.requiredLabelOf(_call.functionCall.get().functionName.name); }, [&](CFG::Assignment const& _assignment) { @@ -186,7 +186,7 @@ class StackLayoutPrinter }, [&](CFG::BasicBlock::FunctionReturn const& _return) { - m_stream << "Block" << getBlockId(_block) << "Exit [label=\"FunctionReturn[" << m_yulNameRepository.labelOf(_return.info->function.name) << "]\"];\n"; + m_stream << "Block" << getBlockId(_block) << "Exit [label=\"FunctionReturn[" << m_yulNameRepository.requiredLabelOf(_return.info->function.name) << "]\"];\n"; m_stream << "Block" << getBlockId(_block) << " -> Block" << getBlockId(_block) << "Exit;\n"; }, [&](CFG::BasicBlock::Terminated const&) diff --git a/test/libyul/YulName.cpp b/test/libyul/YulName.cpp index 6c7238799853..8d3c9539e0de 100644 --- a/test/libyul/YulName.cpp +++ b/test/libyul/YulName.cpp @@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(repository_with_evm_dialect) auto const extractEVMFunction = [](auto const* function) { return function ? function->data : nullptr; }; auto const type = YulNameRepository::emptyName(); - YulString typeLabel(std::string(nameRepository.labelOf(type).value())); + std::string typeLabel(nameRepository.labelOf(type).value()); BOOST_CHECK(extractEVMFunction(nameRepository.discardFunction(type)) == dialect.discardFunction(typeLabel)); BOOST_CHECK(extractEVMFunction(nameRepository.equalityFunction(type)) == dialect.equalityFunction(typeLabel));; BOOST_CHECK(extractEVMFunction(nameRepository.booleanNegationFunction()) == dialect.booleanNegationFunction()); @@ -114,7 +114,7 @@ BOOST_AUTO_TEST_CASE(repository_with_evm_dialect) BOOST_CHECK(extractEVMFunction(nameRepository.memoryLoadFunction(type)) == dialect.memoryLoadFunction(typeLabel)); BOOST_CHECK(extractEVMFunction(nameRepository.storageStoreFunction(type)) == dialect.storageStoreFunction(typeLabel)); BOOST_CHECK(extractEVMFunction(nameRepository.storageLoadFunction(type)) == dialect.storageLoadFunction(typeLabel)); - BOOST_CHECK(nameRepository.labelOf(nameRepository.hashFunction(type)) == dialect.hashFunction(typeLabel).str()); + BOOST_CHECK(nameRepository.labelOf(nameRepository.hashFunction(type)) == dialect.hashFunction(typeLabel)); } BOOST_AUTO_TEST_CASE(repository_with_typed_evm_dialect) @@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(repository_with_typed_evm_dialect) for (auto const type : {wordType, boolType}) { - YulString typeLabel(std::string(nameRepository.labelOf(type).value())); + std::string typeLabel(nameRepository.labelOf(type).value()); BOOST_CHECK(extractEVMFunction(nameRepository.discardFunction(type)) == dialect.discardFunction(typeLabel)); BOOST_CHECK(extractEVMFunction(nameRepository.equalityFunction(type)) == dialect.equalityFunction(typeLabel)); BOOST_CHECK(extractEVMFunction(nameRepository.booleanNegationFunction()) == dialect.booleanNegationFunction()); @@ -143,7 +143,7 @@ BOOST_AUTO_TEST_CASE(repository_with_typed_evm_dialect) BOOST_CHECK(extractEVMFunction(nameRepository.memoryLoadFunction(type)) == dialect.memoryLoadFunction(typeLabel)); BOOST_CHECK(extractEVMFunction(nameRepository.storageStoreFunction(type)) == dialect.storageStoreFunction(typeLabel)); BOOST_CHECK(extractEVMFunction(nameRepository.storageLoadFunction(type)) == dialect.storageLoadFunction(typeLabel)); - BOOST_CHECK(nameRepository.labelOf(nameRepository.hashFunction(type)) == dialect.hashFunction(typeLabel).str()); + BOOST_CHECK(nameRepository.labelOf(nameRepository.hashFunction(type)) == dialect.hashFunction(typeLabel)); } } @@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE(verbatim_functions) BOOST_CHECK(nameRepository.isBuiltinName(verbatimName)); BOOST_CHECK(nameRepository.isDerivedName(verbatimName)); BOOST_CHECK(nameRepository.baseNameOf(verbatimName) == nameRepository.predefined().verbatim); - BOOST_CHECK(nameRepository.builtin(verbatimName)->data == dialect.builtin(YulString{"verbatim_5i_3o"})); + BOOST_CHECK(nameRepository.builtin(verbatimName)->data == dialect.builtin("verbatim_5i_3o")); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/tools/yulInterpreter/Inspector.cpp b/test/tools/yulInterpreter/Inspector.cpp index f9d8bcf1d1e4..823c236d1c4c 100644 --- a/test/tools/yulInterpreter/Inspector.cpp +++ b/test/tools/yulInterpreter/Inspector.cpp @@ -99,7 +99,7 @@ Inspector::NodeAction Inspector::queryUser(langutil::DebugData const& _data, std else if (input == "variables" || input == "v") { for (auto &&[yulName, val]: _variables) - printVariable(m_yulNameRepository.labelOf(yulName), val); + printVariable(m_yulNameRepository.requiredLabelOf(yulName), val); std::cout << std::endl; } else if ( @@ -119,7 +119,7 @@ Inspector::NodeAction Inspector::queryUser(langutil::DebugData const& _data, std bool found = false; for (auto &&[yulName, val]: _variables) { - auto const yulStr = m_yulNameRepository.labelOf(yulName); + auto const yulStr = m_yulNameRepository.requiredLabelOf(yulName); if (yulStr == varname) { printVariable(yulStr, val); diff --git a/test/yulPhaser/Program.cpp b/test/yulPhaser/Program.cpp index 97813949b122..f3424a4749e5 100644 --- a/test/yulPhaser/Program.cpp +++ b/test/yulPhaser/Program.cpp @@ -122,8 +122,8 @@ BOOST_AUTO_TEST_CASE(load_should_disambiguate) VariableDeclaration const& declaration2 = get(innerBlock2.statements[0]); BOOST_TEST(declaration1.variables[0].name != declaration2.variables[0].name); - BOOST_TEST(program.nameRepository().labelOf(declaration1.variables[0].name) == "x"); - BOOST_TEST(program.nameRepository().labelOf(declaration2.variables[0].name) != "x"); + BOOST_TEST(program.nameRepository().requiredLabelOf(declaration1.variables[0].name) == "x"); + BOOST_TEST(program.nameRepository().requiredLabelOf(declaration2.variables[0].name) != "x"); } BOOST_AUTO_TEST_CASE(load_should_do_function_grouping_and_hoisting)