Register functions.

This commit is contained in:
chriseth 2017-02-17 12:03:55 +01:00 committed by chriseth
parent c3b839ca75
commit 72fdf755c9
3 changed files with 90 additions and 33 deletions

View File

@ -62,12 +62,22 @@ bool Scope::registerFunction(string const& _name, size_t _arguments, size_t _ret
Scope::Identifier* Scope::lookup(string const& _name) Scope::Identifier* Scope::lookup(string const& _name)
{ {
if (identifiers.count(_name)) bool crossedFunctionBoundary = false;
return &identifiers[_name]; for (Scope* s = this; s; s = s->superScope)
else if (superScope && !closedScope) {
return superScope->lookup(_name); auto id = identifiers.find(_name);
else if (id != identifiers.end())
return nullptr; {
if (crossedFunctionBoundary && id->second.type() == typeid(Scope::Variable))
return nullptr;
else
return &id->second;
}
if (s->functionScope)
crossedFunctionBoundary = true;
}
return nullptr;
} }
bool Scope::exists(string const& _name) bool Scope::exists(string const& _name)
@ -84,11 +94,10 @@ AsmAnalyzer::AsmAnalyzer(AsmAnalyzer::Scopes& _scopes, ErrorList& _errors):
m_scopes(_scopes), m_errors(_errors) m_scopes(_scopes), m_errors(_errors)
{ {
// Make the Solidity ErrorTag available to inline assembly // Make the Solidity ErrorTag available to inline assembly
m_scopes[nullptr] = make_shared<Scope>();
Scope::Label errorLabel; Scope::Label errorLabel;
errorLabel.id = Scope::Label::errorLabelId; errorLabel.id = Scope::Label::errorLabelId;
m_scopes[nullptr]->identifiers["invalidJumpLabel"] = errorLabel; scope(nullptr).identifiers["invalidJumpLabel"] = errorLabel;
m_currentScope = m_scopes[nullptr].get(); m_currentScope = &scope(nullptr);
} }
bool AsmAnalyzer::operator()(assembly::Literal const& _literal) bool AsmAnalyzer::operator()(assembly::Literal const& _literal)
@ -138,38 +147,52 @@ bool AsmAnalyzer::operator()(FunctionalAssignment const& _assignment)
bool AsmAnalyzer::operator()(assembly::VariableDeclaration const& _varDecl) bool AsmAnalyzer::operator()(assembly::VariableDeclaration const& _varDecl)
{ {
bool success = boost::apply_visitor(*this, *_varDecl.value); bool success = boost::apply_visitor(*this, *_varDecl.value);
if (!m_currentScope->registerVariable(_varDecl.name)) if (!registerVariable(_varDecl.name, _varDecl.location, *m_currentScope))
success = false;
return success;
}
bool AsmAnalyzer::operator()(assembly::FunctionDefinition const& _funDef)
{
bool success = true;
if (!m_currentScope->registerFunction(_funDef.name, _funDef.arguments.size(), _funDef.returns.size()))
{ {
//@TODO secondary location //@TODO secondary location
m_errors.push_back(make_shared<Error>( m_errors.push_back(make_shared<Error>(
Error::Type::DeclarationError, Error::Type::DeclarationError,
"Variable name " + _varDecl.name + " already taken in this scope.", "Function name " + _funDef.name + " already taken in this scope.",
_varDecl.location _funDef.location
)); ));
success = false; success = false;
} }
Scope& body = scope(&_funDef.body);
body.superScope = m_currentScope;
body.functionScope = true;
for (auto const& var: _funDef.arguments + _funDef.returns)
if (!registerVariable(var, _funDef.location, body))
success = false;
(*this)(_funDef.body);
return success; return success;
} }
bool AsmAnalyzer::operator()(assembly::FunctionDefinition const&) bool AsmAnalyzer::operator()(assembly::FunctionCall const& _funCall)
{ {
// TODO - we cannot throw an exception here because of some tests. bool success = true;
return true; for (auto const& arg: _funCall.arguments | boost::adaptors::reversed)
} if (!boost::apply_visitor(*this, arg))
success = false;
bool AsmAnalyzer::operator()(assembly::FunctionCall const&) // TODO actually look up the function (can only be done in a second pass)
{ // and check that the number of arguments and of returns matches the context
// TODO - we cannot throw an exception here because of some tests. return success;
return true;
} }
bool AsmAnalyzer::operator()(Block const& _block) bool AsmAnalyzer::operator()(Block const& _block)
{ {
bool success = true; bool success = true;
auto scope = make_shared<Scope>(); scope(&_block).superScope = m_currentScope;
scope->superScope = m_currentScope; m_currentScope = &scope(&_block);
m_scopes[&_block] = scope;
m_currentScope = scope.get();
for (auto const& s: _block.statements) for (auto const& s: _block.statements)
if (!boost::apply_visitor(*this, s)) if (!boost::apply_visitor(*this, s))
@ -178,3 +201,26 @@ bool AsmAnalyzer::operator()(Block const& _block)
m_currentScope = m_currentScope->superScope; m_currentScope = m_currentScope->superScope;
return success; return success;
} }
bool AsmAnalyzer::registerVariable(string const& _name, SourceLocation const& _location, Scope& _scope)
{
if (!_scope.registerVariable(_name))
{
//@TODO secondary location
m_errors.push_back(make_shared<Error>(
Error::Type::DeclarationError,
"Variable name " + _name + " already taken in this scope.",
_location
));
return false;
}
return true;
}
Scope& AsmAnalyzer::scope(Block const* _block)
{
auto& scope = m_scopes[_block];
if (!scope)
scope = make_shared<Scope>();
return *scope;
}

View File

@ -101,12 +101,15 @@ struct Scope
bool registerLabel(std::string const& _name); bool registerLabel(std::string const& _name);
bool registerFunction(std::string const& _name, size_t _arguments, size_t _returns); bool registerFunction(std::string const& _name, size_t _arguments, size_t _returns);
/// Looks up the identifier in this or super scopes (stops and function and assembly boundaries) /// Looks up the identifier in this or super scopes and returns a valid pointer if found
/// and returns a valid pointer if found or a nullptr if not found. /// or a nullptr if not found. Variable lookups up across function boundaries will fail, as
/// will any lookups across assembly boundaries.
/// The pointer will be invalidated if the scope is modified. /// The pointer will be invalidated if the scope is modified.
/// @param _crossedFunction if true, we already crossed a function boundary during recursive lookup
Identifier* lookup(std::string const& _name); Identifier* lookup(std::string const& _name);
/// Looks up the identifier in this and super scopes (stops and function and assembly boundaries) /// Looks up the identifier in this and super scopes (will not find variables across function
/// and calls the visitor, returns false if not found. /// boundaries and generally stops at 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)
{ {
@ -122,9 +125,9 @@ struct Scope
/// across function and assembly boundaries). /// across function and assembly boundaries).
bool exists(std::string const& _name); 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 /// If true, variables from the super scope are not visible here (other identifiers are),
/// taken into account to prevent shadowing. /// but they are still taken into account to prevent shadowing.
bool closedScope = false; bool functionScope = false;
std::map<std::string, Identifier> identifiers; std::map<std::string, Identifier> identifiers;
}; };
@ -148,6 +151,14 @@ public:
bool operator()(assembly::Block const& _block); bool operator()(assembly::Block const& _block);
private: private:
bool registerVariable(
std::string const& _name,
SourceLocation const& _location,
Scope& _scope
);
Scope& scope(assembly::Block const* _block);
Scope* m_currentScope = nullptr; Scope* m_currentScope = nullptr;
Scopes& m_scopes; Scopes& m_scopes;
ErrorList& m_errors; ErrorList& m_errors;

View File

@ -66,7 +66,7 @@ struct GeneratorState
return size_t(id); return size_t(id);
} }
std::map<assembly::Block const*, shared_ptr<Scope>> scopes; AsmAnalyzer::Scopes scopes;
ErrorList& errors; ErrorList& errors;
eth::Assembly& assembly; eth::Assembly& assembly;
}; };