aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis
diff options
context:
space:
mode:
Diffstat (limited to 'libsolidity/analysis')
-rw-r--r--libsolidity/analysis/ReferencesResolver.cpp22
-rw-r--r--libsolidity/analysis/ReferencesResolver.h1
-rw-r--r--libsolidity/analysis/TypeChecker.cpp57
-rw-r--r--libsolidity/analysis/TypeChecker.h1
4 files changed, 81 insertions, 0 deletions
diff --git a/libsolidity/analysis/ReferencesResolver.cpp b/libsolidity/analysis/ReferencesResolver.cpp
index ca002f58..d7542bf3 100644
--- a/libsolidity/analysis/ReferencesResolver.cpp
+++ b/libsolidity/analysis/ReferencesResolver.cpp
@@ -25,6 +25,8 @@
#include <libsolidity/analysis/NameAndTypeResolver.h>
#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/analysis/ConstantEvaluator.h>
+#include <libsolidity/inlineasm/AsmCodeGen.h>
+#include <libsolidity/inlineasm/AsmData.h>
using namespace std;
using namespace dev;
@@ -112,6 +114,26 @@ void ReferencesResolver::endVisit(ArrayTypeName const& _typeName)
_typeName.annotation().type = make_shared<ArrayType>(DataLocation::Storage, baseType);
}
+bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
+{
+ // We need to perform a full code generation pass here as inline assembly does not distinguish
+ // reference resolution and code generation.
+ // Errors created in this stage are completely ignored because we do not yet know
+ // the type and size of external identifiers, which would result in false errors.
+ ErrorList errorsIgnored;
+ assembly::CodeGenerator codeGen(_inlineAssembly.operations(), errorsIgnored);
+ codeGen.typeCheck([&](assembly::Identifier const& _identifier, eth::Assembly&, assembly::CodeGenerator::IdentifierContext) {
+ auto declarations = m_resolver.nameFromCurrentScope(_identifier.name);
+ if (declarations.size() != 1)
+ return false;
+ _inlineAssembly.annotation().externalReferences[&_identifier] = declarations.front();
+ // At this stage we neither know the code to generate nor the stack size of the identifier,
+ // so we do not modify assembly.
+ return true;
+ });
+ return false;
+}
+
bool ReferencesResolver::visit(Return const& _return)
{
_return.annotation().functionReturnParameters = m_returnParameters;
diff --git a/libsolidity/analysis/ReferencesResolver.h b/libsolidity/analysis/ReferencesResolver.h
index a2d71dc3..1986b2bb 100644
--- a/libsolidity/analysis/ReferencesResolver.h
+++ b/libsolidity/analysis/ReferencesResolver.h
@@ -64,6 +64,7 @@ private:
virtual void endVisit(UserDefinedTypeName const& _typeName) override;
virtual void endVisit(Mapping const& _typeName) override;
virtual void endVisit(ArrayTypeName const& _typeName) override;
+ virtual bool visit(InlineAssembly const& _inlineAssembly) override;
virtual bool visit(Return const& _return) override;
virtual void endVisit(VariableDeclaration const& _variable) override;
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp
index 44f4629b..c63b6b5a 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -24,6 +24,8 @@
#include <memory>
#include <boost/range/adaptor/reversed.hpp>
#include <libsolidity/ast/AST.h>
+#include <libevmasm/Assembly.h> // needed for inline assembly
+#include <libsolidity/inlineasm/AsmCodeGen.h>
using namespace std;
using namespace dev;
@@ -547,6 +549,61 @@ bool TypeChecker::visit(EventDefinition const& _eventDef)
return false;
}
+bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
+{
+ // Inline assembly does not have its own type-checking phase, so we just run the
+ // code-generator and see whether it produces any errors.
+ // External references have already been resolved in a prior stage and stored in the annotation.
+ assembly::CodeGenerator codeGen(_inlineAssembly.operations(), m_errors);
+ codeGen.typeCheck([&](assembly::Identifier const& _identifier, eth::Assembly& _assembly, assembly::CodeGenerator::IdentifierContext _context) {
+ auto ref = _inlineAssembly.annotation().externalReferences.find(&_identifier);
+ if (ref == _inlineAssembly.annotation().externalReferences.end())
+ return false;
+ Declaration const* declaration = ref->second;
+ solAssert(!!declaration, "");
+ if (_context == assembly::CodeGenerator::IdentifierContext::RValue)
+ {
+ solAssert(!!declaration->type(), "Type of declaration required but not yet determined.");
+ unsigned pushes = 0;
+ if (dynamic_cast<FunctionDefinition const*>(declaration))
+ pushes = 1;
+ else if (auto var = dynamic_cast<VariableDeclaration const*>(declaration))
+ {
+ if (var->isConstant())
+ fatalTypeError(SourceLocation(), "Constant variables not yet implemented for inline assembly.");
+ if (var->isLocalVariable())
+ pushes = var->type()->sizeOnStack();
+ else if (var->type()->isValueType())
+ pushes = 1;
+ else
+ pushes = 2; // slot number, intra slot offset
+ }
+ else if (auto contract = dynamic_cast<ContractDefinition const*>(declaration))
+ {
+ if (!contract->isLibrary())
+ return false;
+ pushes = 1;
+ }
+ for (unsigned i = 0; i < pushes; ++i)
+ _assembly.append(u256(0)); // just to verify the stack height
+ }
+ else
+ {
+ // lvalue context
+ if (auto varDecl = dynamic_cast<VariableDeclaration const*>(declaration))
+ {
+ if (!varDecl->isLocalVariable())
+ return false; // only local variables are inline-assemlby lvalues
+ for (unsigned i = 0; i < declaration->type()->sizeOnStack(); ++i)
+ _assembly.append(eth::Instruction::POP); // remove value just to verify the stack height
+ }
+ else
+ return false;
+ }
+ return true;
+ });
+ return false;
+}
bool TypeChecker::visit(IfStatement const& _ifStatement)
{
diff --git a/libsolidity/analysis/TypeChecker.h b/libsolidity/analysis/TypeChecker.h
index b884db49..48f8285a 100644
--- a/libsolidity/analysis/TypeChecker.h
+++ b/libsolidity/analysis/TypeChecker.h
@@ -84,6 +84,7 @@ private:
/// case this is a base constructor call.
void visitManually(ModifierInvocation const& _modifier, std::vector<ContractDefinition const*> const& _bases);
virtual bool visit(EventDefinition const& _eventDef) override;
+ virtual bool visit(InlineAssembly const& _inlineAssembly) override;
virtual bool visit(IfStatement const& _ifStatement) override;
virtual bool visit(WhileStatement const& _whileStatement) override;
virtual bool visit(ForStatement const& _forStatement) override;