Add reservedIdentifier to dialect

This commit is contained in:
Alex Beregszaszi 2020-07-29 18:42:15 +01:00 committed by chriseth
parent d0551b67d7
commit 3a687c34e4
3 changed files with 35 additions and 1 deletions

View File

@ -68,6 +68,9 @@ struct Dialect: boost::noncopyable
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
virtual BuiltinFunction const* builtin(YulString /*_name*/) const { return nullptr; }
/// @returns true if the identifier is reserved. This includes the builtins too.
virtual bool reservedIdentifier(YulString _name) const { return builtin(_name) != nullptr; }
virtual BuiltinFunction const* discardFunction(YulString /* _type */) const { return nullptr; }
virtual BuiltinFunction const* equalityFunction(YulString /* _type */) const { return nullptr; }
virtual BuiltinFunction const* booleanNegationFunction() const { return nullptr; }

View File

@ -109,6 +109,26 @@ pair<YulString, BuiltinFunctionForEVM> createFunction(
return {name, f};
}
set<YulString> createReservedIdentifiers()
{
set<YulString> reserved;
for (auto const& instr: evmasm::c_instructions)
{
string name = instr.first;
transform(name.begin(), name.end(), name.begin(), [](unsigned char _c) { return tolower(_c); });
reserved.emplace(name);
}
reserved += vector<YulString>{
"linkersymbol"_yulstring,
"datasize"_yulstring,
"dataoffset"_yulstring,
"datacopy"_yulstring,
"setimmutable"_yulstring,
"loadimmutable"_yulstring,
};
return reserved;
}
map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVersion, bool _objectAccess)
{
map<YulString, BuiltinFunctionForEVM> builtins;
@ -266,7 +286,8 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
EVMDialect::EVMDialect(langutil::EVMVersion _evmVersion, bool _objectAccess):
m_objectAccess(_objectAccess),
m_evmVersion(_evmVersion),
m_functions(createBuiltins(_evmVersion, _objectAccess))
m_functions(createBuiltins(_evmVersion, _objectAccess)),
m_reserved(createReservedIdentifiers())
{
}
@ -279,6 +300,11 @@ BuiltinFunctionForEVM const* EVMDialect::builtin(YulString _name) const
return nullptr;
}
bool EVMDialect::reservedIdentifier(YulString _name) const
{
return m_reserved.count(_name) != 0;
}
EVMDialect const& EVMDialect::strictAssemblyForEVM(langutil::EVMVersion _version)
{
static map<langutil::EVMVersion, unique_ptr<EVMDialect const>> dialects;

View File

@ -28,6 +28,7 @@
#include <liblangutil/EVMVersion.h>
#include <map>
#include <set>
namespace solidity::yul
{
@ -70,6 +71,9 @@ struct EVMDialect: public Dialect
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
BuiltinFunctionForEVM const* builtin(YulString _name) const override;
/// @returns true if the identifier is reserved. This includes the builtins too.
bool reservedIdentifier(YulString _name) const override;
BuiltinFunctionForEVM const* discardFunction(YulString /*_type*/) const override { return builtin("pop"_yulstring); }
BuiltinFunctionForEVM const* equalityFunction(YulString /*_type*/) const override { return builtin("eq"_yulstring); }
BuiltinFunctionForEVM const* booleanNegationFunction() const override { return builtin("iszero"_yulstring); }
@ -91,6 +95,7 @@ protected:
bool const m_objectAccess;
langutil::EVMVersion const m_evmVersion;
std::map<YulString, BuiltinFunctionForEVM> m_functions;
std::set<YulString> m_reserved;
};
/**