Partially add functions.

This commit is contained in:
chriseth 2017-02-16 23:44:22 +01:00
parent 647473cf01
commit e963405a19
3 changed files with 69 additions and 10 deletions

View File

@ -38,7 +38,7 @@ using namespace dev::solidity::assembly;
bool Scope::registerLabel(string const& _name, size_t _id) bool Scope::registerLabel(string const& _name, size_t _id)
{ {
if (lookup(_name)) if (exists(_name))
return false; return false;
identifiers[_name] = Scope::Label(_id); identifiers[_name] = Scope::Label(_id);
return true; return true;
@ -47,22 +47,40 @@ bool Scope::registerLabel(string const& _name, size_t _id)
bool Scope::registerVariable(string const& _name) bool Scope::registerVariable(string const& _name)
{ {
if (lookup(_name)) if (exists(_name))
return false; return false;
identifiers[_name] = Variable(); identifiers[_name] = Variable();
return true; return true;
} }
bool Scope::registerFunction(string const& _name, size_t _arguments, size_t _returns)
{
if (exists(_name))
return false;
identifiers[_name] = Function(_arguments, _returns);
return true;
}
Scope::Identifier* Scope::lookup(string const& _name) Scope::Identifier* Scope::lookup(string const& _name)
{ {
if (identifiers.count(_name)) if (identifiers.count(_name))
return &identifiers[_name]; return &identifiers[_name];
else if (superScope) else if (superScope && !closedScope)
return superScope->lookup(_name); return superScope->lookup(_name);
else else
return nullptr; return nullptr;
} }
bool Scope::exists(string const& _name)
{
if (identifiers.count(_name))
return true;
else if (superScope)
return superScope->exists(_name);
else
return false;
}
AsmAnalyzer::AsmAnalyzer(AsmAnalyzer::Scopes& _scopes, ErrorList& _errors): AsmAnalyzer::AsmAnalyzer(AsmAnalyzer::Scopes& _scopes, ErrorList& _errors):
m_scopes(_scopes), m_errors(_errors) m_scopes(_scopes), m_errors(_errors)
{ {
@ -161,6 +179,18 @@ bool AsmAnalyzer::operator()(assembly::VariableDeclaration const& _varDecl)
return success; return success;
} }
bool AsmAnalyzer::operator()(assembly::FunctionDefinition const&)
{
// TODO - we cannot throw an exception here because of some tests.
return true;
}
bool AsmAnalyzer::operator()(assembly::FunctionCall const&)
{
// TODO - we cannot throw an exception here because of some tests.
return true;
}
bool AsmAnalyzer::operator()(Block const& _block) bool AsmAnalyzer::operator()(Block const& _block)
{ {
bool success = true; bool success = true;

View File

@ -43,6 +43,8 @@ struct VariableDeclaration;
struct Instruction; struct Instruction;
struct Identifier; struct Identifier;
struct Assignment; struct Assignment;
struct FunctionDefinition;
struct FunctionCall;
template <class...> template <class...>
struct GenericVisitor{}; struct GenericVisitor{};
@ -87,18 +89,27 @@ struct Scope
static const size_t unassignedLabelId = 0; static const size_t unassignedLabelId = 0;
}; };
using Identifier = boost::variant<Variable, Label>; struct Function
using Visitor = GenericVisitor<Variable const, Label const>; {
using NonconstVisitor = GenericVisitor<Variable, Label>; Function(size_t _arguments, size_t _returns): arguments(_arguments), returns(_returns) {}
size_t arguments = 0;
size_t returns = 0;
};
using Identifier = boost::variant<Variable, Label, Function>;
using Visitor = GenericVisitor<Variable const, Label const, Function const>;
using NonconstVisitor = GenericVisitor<Variable, Label, Function>;
bool registerVariable(std::string const& _name); bool registerVariable(std::string const& _name);
bool registerLabel(std::string const& _name, size_t _id); bool registerLabel(std::string const& _name, size_t _id);
bool registerFunction(std::string const& _name, size_t _arguments, size_t _returns);
/// Looks up the identifier in this or super scopes and returns a valid pointer if /// Looks up the identifier in this or super scopes (stops and function and assembly boundaries)
/// found or a nullptr if not found. /// and returns a valid pointer if found or a nullptr if not found.
/// The pointer will be invalidated if the scope is modified. /// The pointer will be invalidated if the scope is modified.
Identifier* lookup(std::string const& _name); Identifier* lookup(std::string const& _name);
/// Looks up the identifier in this and super scopes and calls the visitor, returns false if not found. /// Looks up the identifier in this and super scopes (stops and function and assembly boundaries)
/// and calls the visitor, returns false if not found.
template <class V> template <class V>
bool lookup(std::string const& _name, V const& _visitor) bool lookup(std::string const& _name, V const& _visitor)
{ {
@ -110,8 +121,13 @@ struct Scope
else else
return false; return false;
} }
/// @returns true if the name exists in this scope or in super scopes (also searches
/// across function and assembly boundaries).
bool exists(std::string const& _name);
Scope* superScope = nullptr; Scope* superScope = nullptr;
/// If true, identifiers from the super scope are not visible here, but they are still
/// taken into account to prevent shadowing.
bool closedScope = false;
std::map<std::string, Identifier> identifiers; std::map<std::string, Identifier> identifiers;
}; };
@ -130,6 +146,8 @@ public:
bool operator()(assembly::Assignment const&) { return true; } bool operator()(assembly::Assignment const&) { return true; }
bool operator()(assembly::FunctionalAssignment const& _functionalAssignment); bool operator()(assembly::FunctionalAssignment const& _functionalAssignment);
bool operator()(assembly::VariableDeclaration const& _variableDeclaration); bool operator()(assembly::VariableDeclaration const& _variableDeclaration);
bool operator()(assembly::FunctionDefinition const& _functionDefinition);
bool operator()(assembly::FunctionCall const& _functionCall);
bool operator()(assembly::Block const& _block); bool operator()(assembly::Block const& _block);
private: private:

View File

@ -153,6 +153,10 @@ public:
else if (_label.id == Scope::Label::errorLabelId) else if (_label.id == Scope::Label::errorLabelId)
_label.id = size_t(m_state.assembly.errorTag().data()); _label.id = size_t(m_state.assembly.errorTag().data());
m_state.assembly.append(eth::AssemblyItem(eth::PushTag, _label.id)); m_state.assembly.append(eth::AssemblyItem(eth::PushTag, _label.id));
},
[=](Scope::Function&)
{
solAssert(false, "Not yet implemented");
} }
))) )))
{ {
@ -259,6 +263,13 @@ private:
Error::Type::DeclarationError, Error::Type::DeclarationError,
"Label \"" + string(_variableName.name) + "\" used as variable." "Label \"" + string(_variableName.name) + "\" used as variable."
); );
},
[=](Scope::Function const&)
{
m_state.addError(
Error::Type::DeclarationError,
"Function \"" + string(_variableName.name) + "\" used as variable."
);
} }
))) )))
{ {