Merge pull request #2311 from ethereum/julia-types

Validate types for Julia (part 1)
This commit is contained in:
chriseth 2017-05-29 14:12:38 +02:00 committed by GitHub
commit 2c4a107f43
6 changed files with 63 additions and 15 deletions

View File

@ -194,7 +194,7 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
// Will be re-generated later with correct information
assembly::AsmAnalysisInfo analysisInfo;
assembly::AsmAnalyzer(analysisInfo, errorsIgnored, resolver).analyze(_inlineAssembly.operations());
assembly::AsmAnalyzer(analysisInfo, errorsIgnored, false, resolver).analyze(_inlineAssembly.operations());
return false;
}

View File

@ -711,6 +711,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
assembly::AsmAnalyzer analyzer(
*_inlineAssembly.annotation().analysisInfo,
m_errors,
false,
identifierAccess
);
if (!analyzer.analyze(_inlineAssembly.operations()))

View File

@ -38,13 +38,10 @@ using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::assembly;
AsmAnalyzer::AsmAnalyzer(
AsmAnalysisInfo& _analysisInfo,
ErrorList& _errors,
julia::ExternalIdentifierAccess::Resolver const& _resolver
):
m_resolver(_resolver), m_info(_analysisInfo), m_errors(_errors)
{
namespace {
set<string> const builtinTypes{"bool", "u8", "s8", "u32", "s32", "u64", "s64", "u128", "s128", "u256", "s256"};
}
bool AsmAnalyzer::analyze(Block const& _block)
@ -57,12 +54,14 @@ bool AsmAnalyzer::analyze(Block const& _block)
bool AsmAnalyzer::operator()(Label const& _label)
{
solAssert(!m_julia, "");
m_info.stackHeightInfo[&_label] = m_stackHeight;
return true;
}
bool AsmAnalyzer::operator()(assembly::Instruction const& _instruction)
{
solAssert(!m_julia, "");
auto const& info = instructionInfo(_instruction.instruction);
m_stackHeight += info.ret - info.args;
m_info.stackHeightInfo[&_instruction] = m_stackHeight;
@ -71,6 +70,7 @@ bool AsmAnalyzer::operator()(assembly::Instruction const& _instruction)
bool AsmAnalyzer::operator()(assembly::Literal const& _literal)
{
expectValidType(_literal.type, _literal.location);
++m_stackHeight;
if (_literal.kind == assembly::LiteralKind::String && _literal.value.size() > 32)
{
@ -143,6 +143,7 @@ bool AsmAnalyzer::operator()(assembly::Identifier const& _identifier)
bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
{
solAssert(!m_julia, "");
bool success = true;
for (auto const& arg: _instr.arguments | boost::adaptors::reversed)
{
@ -162,12 +163,12 @@ bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
bool AsmAnalyzer::operator()(assembly::StackAssignment const& _assignment)
{
solAssert(!m_julia, "");
bool success = checkAssignment(_assignment.variableName, size_t(-1));
m_info.stackHeightInfo[&_assignment] = m_stackHeight;
return success;
}
bool AsmAnalyzer::operator()(assembly::Assignment const& _assignment)
{
int const stackHeight = m_stackHeight;
@ -195,7 +196,10 @@ bool AsmAnalyzer::operator()(assembly::VariableDeclaration const& _varDecl)
}
for (auto const& variable: _varDecl.variables)
{
expectValidType(variable.type, variable.location);
boost::get<Scope::Variable>(m_currentScope->identifiers.at(variable.name)).active = true;
}
m_info.stackHeightInfo[&_varDecl] = m_stackHeight;
return success;
}
@ -204,7 +208,10 @@ bool AsmAnalyzer::operator()(assembly::FunctionDefinition const& _funDef)
{
Scope& bodyScope = scope(&_funDef.body);
for (auto const& var: _funDef.arguments + _funDef.returns)
{
expectValidType(var.type, var.location);
boost::get<Scope::Variable>(bodyScope.identifiers.at(var.name)).active = true;
}
int const stackHeight = m_stackHeight;
m_stackHeight = _funDef.arguments.size() + _funDef.returns.size();
@ -453,3 +460,16 @@ Scope& AsmAnalyzer::scope(Block const* _block)
solAssert(scopePtr, "Scope requested but not present.");
return *scopePtr;
}
void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
{
if (!m_julia)
return;
if (!builtinTypes.count(type))
m_errors.push_back(make_shared<Error>(
Error::Type::TypeError,
"\"" + type + "\" is not a valid type (user defined types are not yet supported).",
_location
));
}

View File

@ -61,11 +61,12 @@ struct AsmAnalysisInfo;
class AsmAnalyzer: public boost::static_visitor<bool>
{
public:
AsmAnalyzer(
explicit AsmAnalyzer(
AsmAnalysisInfo& _analysisInfo,
ErrorList& _errors,
bool _julia = false,
julia::ExternalIdentifierAccess::Resolver const& _resolver = julia::ExternalIdentifierAccess::Resolver()
);
): m_resolver(_resolver), m_info(_analysisInfo), m_errors(_errors), m_julia(_julia) {}
bool analyze(assembly::Block const& _block);
@ -88,6 +89,7 @@ private:
bool checkAssignment(assembly::Identifier const& _assignment, size_t _valueSize = size_t(-1));
bool expectDeposit(int _deposit, int _oldHeight, SourceLocation const& _location);
Scope& scope(assembly::Block const* _block);
void expectValidType(std::string const& type, SourceLocation const& _location);
/// This is used when we enter the body of a function definition. There, the parameters
/// and return parameters appear as variables which are already on the stack before
@ -98,6 +100,7 @@ private:
Scope* m_currentScope = nullptr;
AsmAnalysisInfo& m_info;
ErrorList& m_errors;
bool m_julia = false;
};
}

View File

@ -53,7 +53,7 @@ bool InlineAssemblyStack::parse(
*m_parserResult = std::move(*result);
AsmAnalysisInfo analysisInfo;
return (AsmAnalyzer(analysisInfo, m_errors, _resolver)).analyze(*m_parserResult);
return (AsmAnalyzer(analysisInfo, m_errors, false, _resolver)).analyze(*m_parserResult);
}
string InlineAssemblyStack::toString()
@ -84,7 +84,7 @@ bool InlineAssemblyStack::parseAndAssemble(
solAssert(parserResult, "");
AsmAnalysisInfo analysisInfo;
AsmAnalyzer analyzer(analysisInfo, errors, _identifierAccess.resolve);
AsmAnalyzer analyzer(analysisInfo, errors, false, _identifierAccess.resolve);
solAssert(analyzer.analyze(*parserResult), "");
CodeGenerator(errors).assemble(*parserResult, analysisInfo, _assembly, _identifierAccess);

View File

@ -54,7 +54,7 @@ bool parse(string const& _source, ErrorList& errors)
if (parserResult)
{
assembly::AsmAnalysisInfo analysisInfo;
return (assembly::AsmAnalyzer(analysisInfo, errors)).analyze(*parserResult);
return (assembly::AsmAnalyzer(analysisInfo, errors, true)).analyze(*parserResult);
}
}
catch (FatalError const&)
@ -110,7 +110,6 @@ do \
BOOST_CHECK(searchErrorMessage(err, (substring))); \
} while(0)
BOOST_AUTO_TEST_SUITE(JuliaParser)
BOOST_AUTO_TEST_CASE(smoke_test)
@ -197,6 +196,31 @@ BOOST_AUTO_TEST_CASE(lacking_types)
CHECK_ERROR("{ function f(a:u256) -> b {} }", ParserError, "Expected token Colon got 'LBrace'");
}
BOOST_AUTO_TEST_CASE(invalid_types)
{
/// testing invalid literal
/// NOTE: these will need to change when types are compared
CHECK_ERROR("{ let x:bool := 1:invalid }", TypeError, "\"invalid\" is not a valid type (user defined types are not yet supported).");
/// testing invalid variable declaration
CHECK_ERROR("{ let x:invalid := 1:bool }", TypeError, "\"invalid\" is not a valid type (user defined types are not yet supported).");
CHECK_ERROR("{ function f(a:invalid) {} }", TypeError, "\"invalid\" is not a valid type (user defined types are not yet supported).");
}
BOOST_AUTO_TEST_CASE(builtin_types)
{
BOOST_CHECK(successParse("{ let x:bool := true:bool }"));
BOOST_CHECK(successParse("{ let x:u8 := 1:u8 }"));
BOOST_CHECK(successParse("{ let x:s8 := 1:u8 }"));
BOOST_CHECK(successParse("{ let x:u32 := 1:u32 }"));
BOOST_CHECK(successParse("{ let x:s32 := 1:s32 }"));
BOOST_CHECK(successParse("{ let x:u64 := 1:u64 }"));
BOOST_CHECK(successParse("{ let x:s64 := 1:s64 }"));
BOOST_CHECK(successParse("{ let x:u128 := 1:u128 }"));
BOOST_CHECK(successParse("{ let x:s128 := 1:s128 }"));
BOOST_CHECK(successParse("{ let x:u256 := 1:u256 }"));
BOOST_CHECK(successParse("{ let x:s256 := 1:s256 }"));
}
BOOST_AUTO_TEST_SUITE_END()
}