solidity/AST.cpp

509 lines
19 KiB
C++
Raw Normal View History

/*
2014-10-16 12:08:54 +00:00
This file is part of cpp-ethereum.
2014-10-16 12:08:54 +00:00
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2014-10-16 12:08:54 +00:00
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-10-16 12:08:54 +00:00
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Solidity abstract syntax tree.
*/
2014-10-13 16:22:15 +00:00
#include <algorithm>
#include <libsolidity/Utils.h>
#include <libsolidity/AST.h>
#include <libsolidity/ASTVisitor.h>
2014-10-15 12:45:51 +00:00
#include <libsolidity/Exceptions.h>
#include <libsolidity/AST_accept.h>
#include <libdevcrypto/SHA3.h>
2014-10-24 17:06:30 +00:00
using namespace std;
2014-10-16 12:08:54 +00:00
namespace dev
{
namespace solidity
{
TypeError ASTNode::createTypeError(string const& _description) const
2014-10-23 17:22:30 +00:00
{
return TypeError() << errinfo_sourceLocation(getLocation()) << errinfo_comment(_description);
}
TypePointer ContractDefinition::getType(ContractDefinition const* _currentContract) const
{
return make_shared<TypeType>(make_shared<ContractType>(*this), _currentContract);
}
void ContractDefinition::checkTypeRequirements()
{
for (ASTPointer<InheritanceSpecifier> const& baseSpecifier: getBaseContracts())
baseSpecifier->checkTypeRequirements();
checkIllegalOverrides();
FunctionDefinition const* constructor = getConstructor();
if (constructor && !constructor->getReturnParameters().empty())
BOOST_THROW_EXCEPTION(constructor->getReturnParameterList()->createTypeError(
"Non-empty \"returns\" directive for constructor."));
for (ASTPointer<ModifierDefinition> const& modifier: getFunctionModifiers())
modifier->checkTypeRequirements();
for (ASTPointer<FunctionDefinition> const& function: getDefinedFunctions())
function->checkTypeRequirements();
// check for hash collisions in function signatures
set<FixedHash<4>> hashes;
for (auto const& hashAndFunction: getInterfaceFunctionList())
{
FixedHash<4> const& hash = hashAndFunction.first;
if (hashes.count(hash))
BOOST_THROW_EXCEPTION(createTypeError("Function signature hash collision for " +
hashAndFunction.second->getCanonicalSignature()));
hashes.insert(hash);
}
}
map<FixedHash<4>, FunctionDefinition const*> ContractDefinition::getInterfaceFunctions() const
{
vector<pair<FixedHash<4>, FunctionDefinition const*>> exportedFunctionList = getInterfaceFunctionList();
map<FixedHash<4>, FunctionDefinition const*> exportedFunctions(exportedFunctionList.begin(),
exportedFunctionList.end());
solAssert(exportedFunctionList.size() == exportedFunctions.size(),
"Hash collision at Function Definition Hash calculation");
return exportedFunctions;
}
2014-12-12 15:49:26 +00:00
FunctionDefinition const* ContractDefinition::getConstructor() const
{
for (ASTPointer<FunctionDefinition> const& f: m_definedFunctions)
if (f->isConstructor())
2014-12-12 15:49:26 +00:00
return f.get();
return nullptr;
}
void ContractDefinition::checkIllegalOverrides() const
{
// TODO unify this at a later point. for this we need to put the constness and the access specifier
// into the types
map<string, FunctionDefinition const*> functions;
map<string, ModifierDefinition const*> modifiers;
// We search from derived to base, so the stored item causes the error.
for (ContractDefinition const* contract: getLinearizedBaseContracts())
{
for (ASTPointer<FunctionDefinition> const& function: contract->getDefinedFunctions())
{
if (function->isConstructor())
continue; // constructors can neither be overridden nor override anything
string const& name = function->getName();
if (modifiers.count(name))
BOOST_THROW_EXCEPTION(modifiers[name]->createTypeError("Override changes function to modifier."));
FunctionDefinition const*& override = functions[name];
if (!override)
override = function.get();
else if (override->isPublic() != function->isPublic() ||
override->isDeclaredConst() != function->isDeclaredConst() ||
FunctionType(*override) != FunctionType(*function))
BOOST_THROW_EXCEPTION(override->createTypeError("Override changes extended function signature."));
}
for (ASTPointer<ModifierDefinition> const& modifier: contract->getFunctionModifiers())
{
string const& name = modifier->getName();
if (functions.count(name))
BOOST_THROW_EXCEPTION(functions[name]->createTypeError("Override changes modifier to function."));
ModifierDefinition const*& override = modifiers[name];
if (!override)
override = modifier.get();
else if (ModifierType(*override) != ModifierType(*modifier))
BOOST_THROW_EXCEPTION(override->createTypeError("Override changes modifier signature."));
}
}
}
vector<pair<FixedHash<4>, FunctionDefinition const*>> const& ContractDefinition::getInterfaceFunctionList() const
{
if (!m_interfaceFunctionList)
{
set<string> functionsSeen;
m_interfaceFunctionList.reset(new vector<pair<FixedHash<4>, FunctionDefinition const*>>());
for (ContractDefinition const* contract: getLinearizedBaseContracts())
for (ASTPointer<FunctionDefinition> const& f: contract->getDefinedFunctions())
if (f->isPublic() && !f->isConstructor() && functionsSeen.count(f->getName()) == 0)
{
functionsSeen.insert(f->getName());
FixedHash<4> hash(dev::sha3(f->getCanonicalSignature()));
m_interfaceFunctionList->push_back(make_pair(hash, f.get()));
}
}
return *m_interfaceFunctionList;
}
void InheritanceSpecifier::checkTypeRequirements()
{
m_baseName->checkTypeRequirements();
for (ASTPointer<Expression> const& argument: m_arguments)
argument->checkTypeRequirements();
ContractDefinition const* base = dynamic_cast<ContractDefinition const*>(m_baseName->getReferencedDeclaration());
solAssert(base, "Base contract not available.");
TypePointers parameterTypes = ContractType(*base).getConstructorType()->getParameterTypes();
if (parameterTypes.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for constructor call."));
for (size_t i = 0; i < m_arguments.size(); ++i)
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in constructer call."));
}
TypePointer StructDefinition::getType(ContractDefinition const*) const
{
return make_shared<TypeType>(make_shared<StructType>(*this));
}
void StructDefinition::checkMemberTypes() const
2014-12-03 06:46:55 +00:00
{
for (ASTPointer<VariableDeclaration> const& member: getMembers())
if (!member->getType()->canBeStored())
BOOST_THROW_EXCEPTION(member->createTypeError("Type cannot be used in struct."));
}
void StructDefinition::checkRecursion() const
2014-12-03 06:46:55 +00:00
{
set<StructDefinition const*> definitionsSeen;
vector<StructDefinition const*> queue = {this};
while (!queue.empty())
{
StructDefinition const* def = queue.back();
queue.pop_back();
if (definitionsSeen.count(def))
BOOST_THROW_EXCEPTION(ParserError() << errinfo_sourceLocation(def->getLocation())
<< errinfo_comment("Recursive struct definition."));
definitionsSeen.insert(def);
for (ASTPointer<VariableDeclaration> const& member: def->getMembers())
if (member->getType()->getCategory() == Type::Category::STRUCT)
{
UserDefinedTypeName const& typeName = dynamic_cast<UserDefinedTypeName const&>(*member->getTypeName());
2014-12-03 06:46:55 +00:00
queue.push_back(&dynamic_cast<StructDefinition const&>(*typeName.getReferencedDeclaration()));
}
}
}
TypePointer FunctionDefinition::getType(ContractDefinition const*) const
{
return make_shared<FunctionType>(*this);
}
2014-11-10 16:31:09 +00:00
void FunctionDefinition::checkTypeRequirements()
{
for (ASTPointer<VariableDeclaration> const& var: getParameters() + getReturnParameters())
if (!var->getType()->canLiveOutsideStorage())
BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
for (ASTPointer<ModifierInvocation> const& modifier: m_functionModifiers)
modifier->checkTypeRequirements();
2014-11-10 16:31:09 +00:00
m_body->checkTypeRequirements();
}
string FunctionDefinition::getCanonicalSignature() const
{
return getName() + FunctionType(*this).getCanonicalSignature();
}
Declaration::LValueType VariableDeclaration::getLValueType() const
{
2015-01-23 01:35:27 +00:00
if (dynamic_cast<FunctionDefinition const*>(getScope()) || dynamic_cast<ModifierDefinition const*>(getScope()))
return Declaration::LValueType::LOCAL;
else
return Declaration::LValueType::STORAGE;
}
TypePointer ModifierDefinition::getType(ContractDefinition const*) const
{
return make_shared<ModifierType>(*this);
}
2015-01-21 10:16:18 +00:00
void ModifierDefinition::checkTypeRequirements()
{
m_body->checkTypeRequirements();
}
void ModifierInvocation::checkTypeRequirements()
{
m_modifierName->checkTypeRequirements();
for (ASTPointer<Expression> const& argument: m_arguments)
argument->checkTypeRequirements();
ModifierDefinition const* modifier = dynamic_cast<ModifierDefinition const*>(m_modifierName->getReferencedDeclaration());
solAssert(modifier, "Function modifier not found.");
vector<ASTPointer<VariableDeclaration>> const& parameters = modifier->getParameters();
if (parameters.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for modifier invocation."));
for (size_t i = 0; i < m_arguments.size(); ++i)
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameters[i]->getType()))
BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in modifier invocation."));
}
void Block::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
2014-10-24 17:06:30 +00:00
for (shared_ptr<Statement> const& statement: m_statements)
2014-10-13 16:22:15 +00:00
statement->checkTypeRequirements();
}
void IfStatement::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
m_condition->expectType(BoolType());
2014-10-13 16:22:15 +00:00
m_trueBody->checkTypeRequirements();
2014-10-16 21:49:45 +00:00
if (m_falseBody)
m_falseBody->checkTypeRequirements();
2014-10-13 16:22:15 +00:00
}
void WhileStatement::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
m_condition->expectType(BoolType());
2014-10-13 16:22:15 +00:00
m_body->checkTypeRequirements();
}
void ForStatement::checkTypeRequirements()
{
if (m_initExpression)
m_initExpression->checkTypeRequirements();
if (m_condExpression)
m_condExpression->expectType(BoolType());
if (m_loopExpression)
m_loopExpression->checkTypeRequirements();
m_body->checkTypeRequirements();
}
void Return::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
if (!m_expression)
return;
2015-01-23 01:35:27 +00:00
if (!m_returnParameters)
BOOST_THROW_EXCEPTION(createTypeError("Return arguments not allowed."));
2014-10-13 16:22:15 +00:00
if (m_returnParameters->getParameters().size() != 1)
2014-10-23 17:22:30 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Different number of arguments in return statement "
"than in returns declaration."));
2014-10-13 16:22:15 +00:00
// this could later be changed such that the paramaters type is an anonymous struct type,
// but for now, we only allow one return parameter
m_expression->expectType(*m_returnParameters->getParameters().front()->getType());
2014-10-13 16:22:15 +00:00
}
void VariableDefinition::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
// Variables can be declared without type (with "var"), in which case the first assignment
2014-11-10 16:31:09 +00:00
// sets the type.
2014-10-13 16:22:15 +00:00
// Note that assignments before the first declaration are legal because of the special scoping
// rules inherited from JavaScript.
2014-10-16 12:08:54 +00:00
if (m_value)
{
if (m_variable->getType())
m_value->expectType(*m_variable->getType());
2014-10-16 12:08:54 +00:00
else
{
2014-10-13 16:22:15 +00:00
// no type declared and no previous assignment, infer the type
m_value->checkTypeRequirements();
2014-12-19 10:31:17 +00:00
TypePointer type = m_value->getType();
if (type->getCategory() == Type::Category::INTEGER_CONSTANT)
{
auto intType = dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType();
if (!intType)
BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString()));
type = intType;
}
m_variable->setType(type);
}
2014-10-13 16:22:15 +00:00
}
}
void Assignment::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
m_leftHandSide->checkTypeRequirements();
2014-11-10 16:31:09 +00:00
m_leftHandSide->requireLValue();
//@todo later, assignments to structs might be possible, but not to mappings
if (!m_leftHandSide->getType()->isValueType() && !m_leftHandSide->isLocalLValue())
BOOST_THROW_EXCEPTION(createTypeError("Assignment to non-local non-value lvalue."));
2014-10-13 16:22:15 +00:00
m_type = m_leftHandSide->getType();
if (m_assigmentOperator == Token::ASSIGN)
m_rightHandSide->expectType(*m_type);
else
{
2014-10-20 10:41:56 +00:00
// compound assignment
m_rightHandSide->checkTypeRequirements();
TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
m_rightHandSide->getType());
if (!resultType || *resultType != *m_type)
2014-12-19 10:31:17 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_assigmentOperator)) +
" not compatible with types " +
m_type->toString() + " and " +
m_rightHandSide->getType()->toString()));
}
2014-10-13 16:22:15 +00:00
}
void ExpressionStatement::checkTypeRequirements()
{
m_expression->checkTypeRequirements();
2014-12-19 10:31:17 +00:00
if (m_expression->getType()->getCategory() == Type::Category::INTEGER_CONSTANT)
if (!dynamic_pointer_cast<IntegerConstantType const>(m_expression->getType())->getIntegerType())
BOOST_THROW_EXCEPTION(m_expression->createTypeError("Invalid integer constant."));
}
2014-11-05 14:04:33 +00:00
void Expression::expectType(Type const& _expectedType)
{
checkTypeRequirements();
2014-11-06 21:04:10 +00:00
Type const& type = *getType();
if (!type.isImplicitlyConvertibleTo(_expectedType))
BOOST_THROW_EXCEPTION(createTypeError("Type " + type.toString() +
" not implicitly convertible to expected type "
+ _expectedType.toString() + "."));
}
2014-11-10 16:31:09 +00:00
void Expression::requireLValue()
{
if (!isLValue())
2014-11-10 16:31:09 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Expression has to be an lvalue."));
m_lvalueRequested = true;
}
void UnaryOperation::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
2014-10-25 14:52:22 +00:00
// INC, DEC, ADD, SUB, NOT, BIT_NOT, DELETE
m_subExpression->checkTypeRequirements();
2014-10-25 14:52:22 +00:00
if (m_operator == Token::Value::INC || m_operator == Token::Value::DEC || m_operator == Token::Value::DELETE)
2014-11-10 16:31:09 +00:00
m_subExpression->requireLValue();
m_type = m_subExpression->getType()->unaryOperatorResult(m_operator);
if (!m_type)
2014-10-23 17:22:30 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
2014-10-13 16:22:15 +00:00
}
void BinaryOperation::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
m_left->checkTypeRequirements();
2014-11-05 22:35:00 +00:00
m_right->checkTypeRequirements();
m_commonType = m_left->getType()->binaryOperatorResult(m_operator, m_right->getType());
if (!m_commonType)
BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_operator)) +
" not compatible with types " +
m_left->getType()->toString() + " and " +
m_right->getType()->toString()));
m_type = Token::isCompareOp(m_operator) ? make_shared<BoolType>() : m_commonType;
2014-10-13 16:22:15 +00:00
}
void FunctionCall::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
m_expression->checkTypeRequirements();
for (ASTPointer<Expression> const& argument: m_arguments)
2014-10-13 16:22:15 +00:00
argument->checkTypeRequirements();
2014-10-20 10:41:56 +00:00
Type const* expressionType = m_expression->getType().get();
if (isTypeConversion())
2014-10-16 12:08:54 +00:00
{
2014-11-05 13:20:56 +00:00
TypeType const& type = dynamic_cast<TypeType const&>(*expressionType);
2014-10-13 16:22:15 +00:00
//@todo for structs, we have to check the number of arguments to be equal to the
// number of non-mapping members
if (m_arguments.size() != 1)
BOOST_THROW_EXCEPTION(createTypeError("More than one argument for explicit type conversion."));
2014-11-05 13:20:56 +00:00
if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type.getActualType()))
2014-10-23 17:22:30 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
2014-11-05 13:20:56 +00:00
m_type = type.getActualType();
2014-10-16 12:08:54 +00:00
}
2014-12-12 15:49:26 +00:00
else if (FunctionType const* functionType = dynamic_cast<FunctionType const*>(expressionType))
2014-10-16 12:08:54 +00:00
{
2014-10-13 16:22:15 +00:00
//@todo would be nice to create a struct type from the arguments
// and then ask if that is implicitly convertible to the struct represented by the
// function parameters
2014-12-12 15:49:26 +00:00
TypePointers const& parameterTypes = functionType->getParameterTypes();
if (parameterTypes.size() != m_arguments.size())
2014-10-23 17:22:30 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
2014-10-16 12:08:54 +00:00
for (size_t i = 0; i < m_arguments.size(); ++i)
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
2014-10-23 17:22:30 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
2014-10-13 16:22:15 +00:00
// @todo actually the return type should be an anonymous struct,
// but we change it to the type of the first return value until we have structs
2014-12-12 15:49:26 +00:00
if (functionType->getReturnParameterTypes().empty())
2014-10-24 17:06:30 +00:00
m_type = make_shared<VoidType>();
2014-10-13 16:22:15 +00:00
else
2014-12-12 15:49:26 +00:00
m_type = functionType->getReturnParameterTypes().front();
2014-10-16 12:08:54 +00:00
}
2014-12-12 15:49:26 +00:00
else
BOOST_THROW_EXCEPTION(createTypeError("Type is not callable."));
2014-10-20 10:41:56 +00:00
}
bool FunctionCall::isTypeConversion() const
{
return m_expression->getType()->getCategory() == Type::Category::TYPE;
2014-10-13 16:22:15 +00:00
}
2014-12-12 15:49:26 +00:00
void NewExpression::checkTypeRequirements()
{
m_contractName->checkTypeRequirements();
m_contract = dynamic_cast<ContractDefinition const*>(m_contractName->getReferencedDeclaration());
if (!m_contract)
BOOST_THROW_EXCEPTION(createTypeError("Identifier is not a contract."));
2015-01-13 17:12:19 +00:00
shared_ptr<ContractType const> contractType = make_shared<ContractType>(*m_contract);
TypePointers const& parameterTypes = contractType->getConstructorType()->getParameterTypes();
m_type = make_shared<FunctionType>(parameterTypes, TypePointers{contractType},
FunctionType::Location::CREATION);
2014-12-12 15:49:26 +00:00
}
void MemberAccess::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
2014-11-13 00:12:57 +00:00
m_expression->checkTypeRequirements();
2014-11-20 09:19:43 +00:00
Type const& type = *m_expression->getType();
m_type = type.getMemberType(*m_memberName);
if (!m_type)
2015-01-19 18:18:34 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found or not "
"visible in " + type.toString()));
//@todo later, this will not always be STORAGE
m_lvalue = type.getCategory() == Type::Category::STRUCT ? Declaration::LValueType::STORAGE : Declaration::LValueType::NONE;
2014-10-13 16:22:15 +00:00
}
void IndexAccess::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
2014-11-10 16:31:09 +00:00
m_base->checkTypeRequirements();
if (m_base->getType()->getCategory() != Type::Category::MAPPING)
BOOST_THROW_EXCEPTION(m_base->createTypeError("Indexed expression has to be a mapping (is " +
m_base->getType()->toString() + ")"));
MappingType const& type = dynamic_cast<MappingType const&>(*m_base->getType());
m_index->expectType(*type.getKeyType());
m_type = type.getValueType();
m_lvalue = Declaration::LValueType::STORAGE;
2014-10-13 16:22:15 +00:00
}
void Identifier::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
solAssert(m_referencedDeclaration, "Identifier not resolved.");
2014-11-05 13:20:56 +00:00
m_lvalue = m_referencedDeclaration->getLValueType();
m_type = m_referencedDeclaration->getType(m_currentContract);
if (!m_type)
BOOST_THROW_EXCEPTION(createTypeError("Declaration referenced before type could be determined."));
2014-10-13 16:22:15 +00:00
}
void ElementaryTypeNameExpression::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
m_type = make_shared<TypeType>(Type::fromElementaryTypeName(m_typeToken));
2014-10-13 16:22:15 +00:00
}
void Literal::checkTypeRequirements()
2014-10-13 16:22:15 +00:00
{
m_type = Type::forLiteral(*this);
if (!m_type)
2014-12-19 10:31:17 +00:00
BOOST_THROW_EXCEPTION(createTypeError("Invalid literal value."));
2014-10-13 16:22:15 +00:00
}
2014-10-16 12:08:54 +00:00
}
}