aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity
diff options
context:
space:
mode:
Diffstat (limited to 'libsolidity')
-rw-r--r--libsolidity/analysis/SyntaxChecker.cpp21
-rw-r--r--libsolidity/analysis/SyntaxChecker.h9
-rw-r--r--libsolidity/codegen/ContractCompiler.cpp73
-rw-r--r--libsolidity/codegen/ContractCompiler.h7
4 files changed, 74 insertions, 36 deletions
diff --git a/libsolidity/analysis/SyntaxChecker.cpp b/libsolidity/analysis/SyntaxChecker.cpp
index e94ce9fe..593f2f69 100644
--- a/libsolidity/analysis/SyntaxChecker.cpp
+++ b/libsolidity/analysis/SyntaxChecker.cpp
@@ -40,13 +40,26 @@ void SyntaxChecker::syntaxError(SourceLocation const& _location, std::string con
m_errors.push_back(err);
}
+bool SyntaxChecker::visit(ModifierDefinition const&)
+{
+ m_placeholderFound = false;
+ return true;
+}
+
+void SyntaxChecker::endVisit(ModifierDefinition const& _modifier)
+{
+ if (!m_placeholderFound)
+ syntaxError(_modifier.body().location(), "Modifier body does not contain '_'.");
+ m_placeholderFound = false;
+}
+
bool SyntaxChecker::visit(WhileStatement const&)
{
m_inLoopDepth++;
return true;
}
-void SyntaxChecker::endVisit(WhileStatement const&)
+void SyntaxChecker::endVisit(WhileStatement const& )
{
m_inLoopDepth--;
}
@@ -78,3 +91,9 @@ bool SyntaxChecker::visit(Break const& _breakStatement)
return true;
}
+bool SyntaxChecker::visit(const PlaceholderStatement&)
+{
+ m_placeholderFound = true;
+ return true;
+}
+
diff --git a/libsolidity/analysis/SyntaxChecker.h b/libsolidity/analysis/SyntaxChecker.h
index c836d49f..3198ffd0 100644
--- a/libsolidity/analysis/SyntaxChecker.h
+++ b/libsolidity/analysis/SyntaxChecker.h
@@ -31,6 +31,7 @@ namespace solidity
/**
* The module that performs syntax analysis on the AST:
* - whether continue/break is in a for/while loop.
+ * - whether a modifier contains at least one '_'
*/
class SyntaxChecker: private ASTConstVisitor
{
@@ -44,6 +45,9 @@ private:
/// Adds a new error to the list of errors.
void syntaxError(SourceLocation const& _location, std::string const& _description);
+ virtual bool visit(ModifierDefinition const& _modifier) override;
+ virtual void endVisit(ModifierDefinition const& _modifier) override;
+
virtual bool visit(WhileStatement const& _whileStatement) override;
virtual void endVisit(WhileStatement const& _whileStatement) override;
virtual bool visit(ForStatement const& _forStatement) override;
@@ -52,8 +56,13 @@ private:
virtual bool visit(Continue const& _continueStatement) override;
virtual bool visit(Break const& _breakStatement) override;
+ virtual bool visit(PlaceholderStatement const& _placeholderStatement) override;
+
ErrorList& m_errors;
+ /// Flag that indicates whether a function modifier actually contains '_'.
+ bool m_placeholderFound = false;
+
int m_inLoopDepth = 0;
};
diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp
index bcfd33f2..715852be 100644
--- a/libsolidity/codegen/ContractCompiler.cpp
+++ b/libsolidity/codegen/ContractCompiler.cpp
@@ -431,16 +431,16 @@ bool ContractCompiler::visit(FunctionDefinition const& _function)
if (auto c = m_context.nextConstructor(dynamic_cast<ContractDefinition const&>(*_function.scope())))
appendBaseConstructor(*c);
- m_returnTag = m_context.newTag();
+ solAssert(m_returnTags.empty(), "");
m_breakTags.clear();
m_continueTags.clear();
m_stackCleanupForReturn = 0;
m_currentFunction = &_function;
- m_modifierDepth = 0;
+ m_modifierDepth = -1;
appendModifierOrFunctionCode();
- m_context << m_returnTag;
+ solAssert(m_returnTags.empty(), "");
// Now we need to re-shuffle the stack. For this we keep a record of the stack layout
// that shows the target positions of the elements, where "-1" denotes that this element needs
@@ -695,7 +695,7 @@ bool ContractCompiler::visit(Return const& _return)
}
for (unsigned i = 0; i < m_stackCleanupForReturn; ++i)
m_context << Instruction::POP;
- m_context.appendJumpTo(m_returnTag);
+ m_context.appendJumpTo(m_returnTags.back());
m_context.adjustStackOffset(m_stackCleanupForReturn);
return false;
}
@@ -755,9 +755,7 @@ bool ContractCompiler::visit(PlaceholderStatement const& _placeholderStatement)
{
StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, _placeholderStatement);
- ++m_modifierDepth;
appendModifierOrFunctionCode();
- --m_modifierDepth;
checker.check();
return true;
}
@@ -775,10 +773,15 @@ void ContractCompiler::appendMissingFunctions()
void ContractCompiler::appendModifierOrFunctionCode()
{
solAssert(m_currentFunction, "");
+ unsigned stackSurplus = 0;
+ Block const* codeBlock = nullptr;
+
+ m_modifierDepth++;
+
if (m_modifierDepth >= m_currentFunction->modifiers().size())
{
solAssert(m_currentFunction->isImplemented(), "");
- m_currentFunction->body().accept(*this);
+ codeBlock = &m_currentFunction->body();
}
else
{
@@ -786,37 +789,45 @@ void ContractCompiler::appendModifierOrFunctionCode()
// constructor call should be excluded
if (dynamic_cast<ContractDefinition const*>(modifierInvocation->name()->annotation().referencedDeclaration))
- {
- ++m_modifierDepth;
appendModifierOrFunctionCode();
- --m_modifierDepth;
- return;
- }
-
- ModifierDefinition const& modifier = m_context.functionModifier(modifierInvocation->name()->name());
- CompilerContext::LocationSetter locationSetter(m_context, modifier);
- solAssert(modifier.parameters().size() == modifierInvocation->arguments().size(), "");
- for (unsigned i = 0; i < modifier.parameters().size(); ++i)
+ else
{
- m_context.addVariable(*modifier.parameters()[i]);
- compileExpression(
- *modifierInvocation->arguments()[i],
- modifier.parameters()[i]->annotation().type
- );
+ ModifierDefinition const& modifier = m_context.functionModifier(modifierInvocation->name()->name());
+ CompilerContext::LocationSetter locationSetter(m_context, modifier);
+ solAssert(modifier.parameters().size() == modifierInvocation->arguments().size(), "");
+ for (unsigned i = 0; i < modifier.parameters().size(); ++i)
+ {
+ m_context.addVariable(*modifier.parameters()[i]);
+ compileExpression(
+ *modifierInvocation->arguments()[i],
+ modifier.parameters()[i]->annotation().type
+ );
+ }
+ for (VariableDeclaration const* localVariable: modifier.localVariables())
+ appendStackVariableInitialisation(*localVariable);
+
+ stackSurplus =
+ CompilerUtils::sizeOnStack(modifier.parameters()) +
+ CompilerUtils::sizeOnStack(modifier.localVariables());
+ codeBlock = &modifier.body();
+
+ codeBlock = &modifier.body();
}
- for (VariableDeclaration const* localVariable: modifier.localVariables())
- appendStackVariableInitialisation(*localVariable);
+ }
+
+ if (codeBlock)
+ {
+ m_returnTags.push_back(m_context.newTag());
- unsigned const c_stackSurplus = CompilerUtils::sizeOnStack(modifier.parameters()) +
- CompilerUtils::sizeOnStack(modifier.localVariables());
- m_stackCleanupForReturn += c_stackSurplus;
+ codeBlock->accept(*this);
- modifier.body().accept(*this);
+ solAssert(!m_returnTags.empty(), "");
+ m_context << m_returnTags.back();
+ m_returnTags.pop_back();
- for (unsigned i = 0; i < c_stackSurplus; ++i)
- m_context << Instruction::POP;
- m_stackCleanupForReturn -= c_stackSurplus;
+ CompilerUtils(m_context).popStackSlots(stackSurplus);
}
+ m_modifierDepth--;
}
void ContractCompiler::appendStackVariableInitialisation(VariableDeclaration const& _variable)
diff --git a/libsolidity/codegen/ContractCompiler.h b/libsolidity/codegen/ContractCompiler.h
index d1517e88..0799a543 100644
--- a/libsolidity/codegen/ContractCompiler.h
+++ b/libsolidity/codegen/ContractCompiler.h
@@ -40,11 +40,9 @@ class ContractCompiler: private ASTConstVisitor
public:
explicit ContractCompiler(CompilerContext& _context, bool _optimise):
m_optimise(_optimise),
- m_context(_context),
- m_returnTag(eth::Tag, u256(-1))
+ m_context(_context)
{
m_context = CompilerContext();
- m_returnTag = m_context.newTag();
}
void compileContract(
@@ -122,7 +120,8 @@ private:
CompilerContext& m_context;
std::vector<eth::AssemblyItem> m_breakTags; ///< tag to jump to for a "break" statement
std::vector<eth::AssemblyItem> m_continueTags; ///< tag to jump to for a "continue" statement
- eth::AssemblyItem m_returnTag; ///< tag to jump to for a "return" statement
+ /// Tag to jump to for a "return" statement, needs to be stacked because of modifiers.
+ std::vector<eth::AssemblyItem> m_returnTags;
unsigned m_modifierDepth = 0;
FunctionDefinition const* m_currentFunction = nullptr;
unsigned m_stackCleanupForReturn = 0; ///< this number of stack elements need to be removed before jump to m_returnTag