aboutsummaryrefslogtreecommitdiffstats
path: root/AST.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'AST.cpp')
-rw-r--r--AST.cpp254
1 files changed, 201 insertions, 53 deletions
diff --git a/AST.cpp b/AST.cpp
index 0abdf781..acf39934 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -52,6 +52,7 @@ void ContractDefinition::checkTypeRequirements()
for (ASTPointer<InheritanceSpecifier> const& baseSpecifier: getBaseContracts())
baseSpecifier->checkTypeRequirements();
+ checkDuplicateFunctions();
checkIllegalOverrides();
checkAbstractFunctions();
@@ -86,6 +87,7 @@ void ContractDefinition::checkTypeRequirements()
for (ASTPointer<VariableDeclaration> const& variable: m_stateVariables)
variable->checkTypeRequirements();
+ checkExternalTypeClashes();
// check for hash collisions in function signatures
set<FixedHash<4>> hashes;
for (auto const& it: getInterfaceFunctionList())
@@ -130,6 +132,33 @@ FunctionDefinition const* ContractDefinition::getFallbackFunction() const
return nullptr;
}
+void ContractDefinition::checkDuplicateFunctions() const
+{
+ /// Checks that two functions with the same name defined in this contract have different
+ /// argument types and that there is at most one constructor.
+ map<string, vector<FunctionDefinition const*>> functions;
+ for (ASTPointer<FunctionDefinition> const& function: getDefinedFunctions())
+ functions[function->getName()].push_back(function.get());
+ if (functions[getName()].size() > 1)
+ BOOST_THROW_EXCEPTION(
+ DeclarationError() <<
+ errinfo_sourceLocation(getLocation()) <<
+ errinfo_comment("More than one constructor defined.")
+ );
+ for (auto const& it: functions)
+ {
+ vector<FunctionDefinition const*> const& overloads = it.second;
+ for (size_t i = 0; i < overloads.size(); ++i)
+ for (size_t j = i + 1; j < overloads.size(); ++j)
+ if (FunctionType(*overloads[i]).hasEqualArgumentTypes(FunctionType(*overloads[j])))
+ BOOST_THROW_EXCEPTION(
+ DeclarationError() <<
+ errinfo_sourceLocation(overloads[j]->getLocation()) <<
+ errinfo_comment("Function with same name and arguments already defined.")
+ );
+ }
+}
+
void ContractDefinition::checkAbstractFunctions()
{
map<string, bool> functions;
@@ -156,7 +185,7 @@ 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, vector<FunctionDefinition const*>> functions;
map<string, ModifierDefinition const*> modifiers;
// We search from derived to base, so the stored item causes the error.
@@ -169,28 +198,67 @@ void ContractDefinition::checkIllegalOverrides() const
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->getVisibility() != function->getVisibility() ||
- override->isDeclaredConst() != function->isDeclaredConst() ||
- FunctionType(*override) != FunctionType(*function))
- BOOST_THROW_EXCEPTION(override->createTypeError("Override changes extended function signature."));
+ FunctionType functionType(*function);
+ // function should not change the return type
+ for (FunctionDefinition const* overriding: functions[name])
+ {
+ FunctionType overridingType(*overriding);
+ if (!overridingType.hasEqualArgumentTypes(functionType))
+ continue;
+ if (
+ overriding->getVisibility() != function->getVisibility() ||
+ overriding->isDeclaredConst() != function->isDeclaredConst() ||
+ overridingType != functionType
+ )
+ BOOST_THROW_EXCEPTION(overriding->createTypeError("Override changes extended function signature."));
+ }
+ functions[name].push_back(function.get());
}
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."));
+ if (!functions[name].empty())
+ BOOST_THROW_EXCEPTION(override->createTypeError("Override changes modifier to function."));
}
}
}
+void ContractDefinition::checkExternalTypeClashes() const
+{
+ map<string, vector<pair<Declaration const*, shared_ptr<FunctionType>>>> externalDeclarations;
+ for (ContractDefinition const* contract: getLinearizedBaseContracts())
+ {
+ for (ASTPointer<FunctionDefinition> const& f: contract->getDefinedFunctions())
+ if (f->isPartOfExternalInterface())
+ {
+ auto functionType = make_shared<FunctionType>(*f);
+ externalDeclarations[functionType->externalSignature(f->getName())].push_back(
+ make_pair(f.get(), functionType)
+ );
+ }
+ for (ASTPointer<VariableDeclaration> const& v: contract->getStateVariables())
+ if (v->isPartOfExternalInterface())
+ {
+ auto functionType = make_shared<FunctionType>(*v);
+ externalDeclarations[functionType->externalSignature(v->getName())].push_back(
+ make_pair(v.get(), functionType)
+ );
+ }
+ }
+ for (auto const& it: externalDeclarations)
+ for (size_t i = 0; i < it.second.size(); ++i)
+ for (size_t j = i + 1; j < it.second.size(); ++j)
+ if (!it.second[i].second->hasEqualArgumentTypes(*it.second[j].second))
+ BOOST_THROW_EXCEPTION(it.second[j].first->createTypeError(
+ "Function overload clash during conversion to external types for arguments."
+ ));
+}
+
std::vector<ASTPointer<EventDefinition>> const& ContractDefinition::getInterfaceEvents() const
{
if (!m_interfaceEvents)
@@ -213,16 +281,21 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getIn
if (!m_interfaceFunctionList)
{
set<string> functionsSeen;
+ set<string> signaturesSeen;
m_interfaceFunctionList.reset(new vector<pair<FixedHash<4>, FunctionTypePointer>>());
for (ContractDefinition const* contract: getLinearizedBaseContracts())
{
for (ASTPointer<FunctionDefinition> const& f: contract->getDefinedFunctions())
- if (functionsSeen.count(f->getName()) == 0 && f->isPartOfExternalInterface())
+ {
+ string functionSignature = f->externalSignature();
+ if (f->isPartOfExternalInterface() && signaturesSeen.count(functionSignature) == 0)
{
functionsSeen.insert(f->getName());
- FixedHash<4> hash(dev::sha3(f->externalSignature()));
+ signaturesSeen.insert(functionSignature);
+ FixedHash<4> hash(dev::sha3(functionSignature));
m_interfaceFunctionList->push_back(make_pair(hash, make_shared<FunctionType>(*f, false)));
}
+ }
for (ASTPointer<VariableDeclaration> const& v: contract->getStateVariables())
if (functionsSeen.count(v->getName()) == 0 && v->isPartOfExternalInterface())
@@ -274,11 +347,11 @@ TypePointer EnumValue::getType(ContractDefinition const*) const
void InheritanceSpecifier::checkTypeRequirements()
{
- m_baseName->checkTypeRequirements();
+ m_baseName->checkTypeRequirements(nullptr);
for (ASTPointer<Expression> const& argument: m_arguments)
- argument->checkTypeRequirements();
+ argument->checkTypeRequirements(nullptr);
- ContractDefinition const* base = dynamic_cast<ContractDefinition const*>(m_baseName->getReferencedDeclaration());
+ 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())
@@ -388,7 +461,8 @@ void VariableDeclaration::checkTypeRequirements()
if (!m_value)
// This feature might be extended in the future.
BOOST_THROW_EXCEPTION(createTypeError("Assignment necessary for type detection."));
- m_value->checkTypeRequirements();
+ m_value->checkTypeRequirements(nullptr);
+
TypePointer type = m_value->getType();
if (type->getCategory() == Type::Category::IntegerConstant)
{
@@ -428,11 +502,15 @@ void ModifierDefinition::checkTypeRequirements()
void ModifierInvocation::checkTypeRequirements(vector<ASTPointer<InheritanceSpecifier>> const& _bases)
{
- m_modifierName->checkTypeRequirements();
+ TypePointers argumentTypes;
for (ASTPointer<Expression> const& argument: m_arguments)
- argument->checkTypeRequirements();
+ {
+ argument->checkTypeRequirements(nullptr);
+ argumentTypes.push_back(argument->getType());
+ }
+ m_modifierName->checkTypeRequirements(&argumentTypes);
- auto declaration = m_modifierName->getReferencedDeclaration();
+ auto const* declaration = &m_modifierName->getReferencedDeclaration();
vector<ASTPointer<VariableDeclaration>> emptyParameterList;
vector<ASTPointer<VariableDeclaration>> const* parameters = nullptr;
if (auto modifier = dynamic_cast<ModifierDefinition const*>(declaration))
@@ -440,7 +518,7 @@ void ModifierInvocation::checkTypeRequirements(vector<ASTPointer<InheritanceSpec
else
// check parameters for Base constructors
for (auto const& base: _bases)
- if (declaration == base->getName()->getReferencedDeclaration())
+ if (declaration == &base->getName()->getReferencedDeclaration())
{
if (auto referencedConstructor = dynamic_cast<ContractDefinition const&>(*declaration).getConstructor())
parameters = &referencedConstructor->getParameters();
@@ -523,9 +601,9 @@ void VariableDeclarationStatement::checkTypeRequirements()
m_variable->checkTypeRequirements();
}
-void Assignment::checkTypeRequirements()
+void Assignment::checkTypeRequirements(TypePointers const*)
{
- m_leftHandSide->checkTypeRequirements();
+ m_leftHandSide->checkTypeRequirements(nullptr);
m_leftHandSide->requireLValue();
if (m_leftHandSide->getType()->getCategory() == Type::Category::Mapping)
BOOST_THROW_EXCEPTION(createTypeError("Mappings cannot be assigned to."));
@@ -535,7 +613,7 @@ void Assignment::checkTypeRequirements()
else
{
// compound assignment
- m_rightHandSide->checkTypeRequirements();
+ m_rightHandSide->checkTypeRequirements(nullptr);
TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
m_rightHandSide->getType());
if (!resultType || *resultType != *m_type)
@@ -548,7 +626,7 @@ void Assignment::checkTypeRequirements()
void ExpressionStatement::checkTypeRequirements()
{
- m_expression->checkTypeRequirements();
+ m_expression->checkTypeRequirements(nullptr);
if (m_expression->getType()->getCategory() == Type::Category::IntegerConstant)
if (!dynamic_pointer_cast<IntegerConstantType const>(m_expression->getType())->getIntegerType())
BOOST_THROW_EXCEPTION(m_expression->createTypeError("Invalid integer constant."));
@@ -556,7 +634,7 @@ void ExpressionStatement::checkTypeRequirements()
void Expression::expectType(Type const& _expectedType)
{
- checkTypeRequirements();
+ checkTypeRequirements(nullptr);
Type const& type = *getType();
if (!type.isImplicitlyConvertibleTo(_expectedType))
BOOST_THROW_EXCEPTION(createTypeError("Type " + type.toString() +
@@ -571,10 +649,10 @@ void Expression::requireLValue()
m_lvalueRequested = true;
}
-void UnaryOperation::checkTypeRequirements()
+void UnaryOperation::checkTypeRequirements(TypePointers const*)
{
// Inc, Dec, Add, Sub, Not, BitNot, Delete
- m_subExpression->checkTypeRequirements();
+ m_subExpression->checkTypeRequirements(nullptr);
if (m_operator == Token::Value::Inc || m_operator == Token::Value::Dec || m_operator == Token::Value::Delete)
m_subExpression->requireLValue();
m_type = m_subExpression->getType()->unaryOperatorResult(m_operator);
@@ -582,10 +660,10 @@ void UnaryOperation::checkTypeRequirements()
BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
}
-void BinaryOperation::checkTypeRequirements()
+void BinaryOperation::checkTypeRequirements(TypePointers const*)
{
- m_left->checkTypeRequirements();
- m_right->checkTypeRequirements();
+ m_left->checkTypeRequirements(nullptr);
+ m_right->checkTypeRequirements(nullptr);
m_commonType = m_left->getType()->binaryOperatorResult(m_operator, m_right->getType());
if (!m_commonType)
BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_operator)) +
@@ -595,11 +673,22 @@ void BinaryOperation::checkTypeRequirements()
m_type = Token::isCompareOp(m_operator) ? make_shared<BoolType>() : m_commonType;
}
-void FunctionCall::checkTypeRequirements()
+void FunctionCall::checkTypeRequirements(TypePointers const*)
{
- m_expression->checkTypeRequirements();
+ bool isPositionalCall = m_names.empty();
+
+ // we need to check arguments' type first as they will be forwarded to
+ // m_expression->checkTypeRequirements
+ TypePointers argumentTypes;
for (ASTPointer<Expression> const& argument: m_arguments)
- argument->checkTypeRequirements();
+ {
+ argument->checkTypeRequirements(nullptr);
+ // only store them for positional calls
+ if (isPositionalCall)
+ argumentTypes.push_back(argument->getType());
+ }
+
+ m_expression->checkTypeRequirements(isPositionalCall ? &argumentTypes : nullptr);
Type const* expressionType = m_expression->getType().get();
if (isTypeConversion())
@@ -609,7 +698,7 @@ void FunctionCall::checkTypeRequirements()
// number of non-mapping members
if (m_arguments.size() != 1)
BOOST_THROW_EXCEPTION(createTypeError("More than one argument for explicit type conversion."));
- if (!m_names.empty())
+ if (!isPositionalCall)
BOOST_THROW_EXCEPTION(createTypeError("Type conversion cannot allow named arguments."));
if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type.getActualType()))
BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
@@ -624,8 +713,9 @@ void FunctionCall::checkTypeRequirements()
if (!functionType->takesArbitraryParameters() && parameterTypes.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
- if (m_names.empty())
+ if (isPositionalCall)
{
+ // call by positional arguments
for (size_t i = 0; i < m_arguments.size(); ++i)
if (!functionType->takesArbitraryParameters() &&
!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
@@ -633,6 +723,7 @@ void FunctionCall::checkTypeRequirements()
}
else
{
+ // call by named arguments
if (functionType->takesArbitraryParameters())
BOOST_THROW_EXCEPTION(createTypeError("Named arguments cannnot be used for functions "
"that take arbitrary parameters."));
@@ -679,10 +770,10 @@ bool FunctionCall::isTypeConversion() const
return m_expression->getType()->getCategory() == Type::Category::TypeType;
}
-void NewExpression::checkTypeRequirements()
+void NewExpression::checkTypeRequirements(TypePointers const*)
{
- m_contractName->checkTypeRequirements();
- m_contract = dynamic_cast<ContractDefinition const*>(m_contractName->getReferencedDeclaration());
+ m_contractName->checkTypeRequirements(nullptr);
+ m_contract = dynamic_cast<ContractDefinition const*>(&m_contractName->getReferencedDeclaration());
if (!m_contract)
BOOST_THROW_EXCEPTION(createTypeError("Identifier is not a contract."));
if (!m_contract->isFullyImplemented())
@@ -693,15 +784,37 @@ void NewExpression::checkTypeRequirements()
FunctionType::Location::Creation);
}
-void MemberAccess::checkTypeRequirements()
+void MemberAccess::checkTypeRequirements(TypePointers const* _argumentTypes)
{
- m_expression->checkTypeRequirements();
+ m_expression->checkTypeRequirements(nullptr);
Type const& type = *m_expression->getType();
- m_type = type.getMemberType(*m_memberName);
- if (!m_type)
- BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found or not "
- "visible in " + type.toString()));
- // This should probably move somewhere else.
+
+ MemberList::MemberMap possibleMembers = type.getMembers().membersByName(*m_memberName);
+ if (possibleMembers.size() > 1 && _argumentTypes)
+ {
+ // do override resolution
+ for (auto it = possibleMembers.begin(); it != possibleMembers.end();)
+ if (
+ it->type->getCategory() == Type::Category::Function &&
+ !dynamic_cast<FunctionType const&>(*it->type).canTakeArguments(*_argumentTypes)
+ )
+ it = possibleMembers.erase(it);
+ else
+ ++it;
+ }
+ if (possibleMembers.size() == 0)
+ BOOST_THROW_EXCEPTION(createTypeError(
+ "Member \"" + *m_memberName + "\" not found or not visible "
+ "after argument-dependent lookup in " + type.toString()
+ ));
+ else if (possibleMembers.size() > 1)
+ BOOST_THROW_EXCEPTION(createTypeError(
+ "Member \"" + *m_memberName + "\" not unique "
+ "after argument-dependent lookup in " + type.toString()
+ ));
+
+ m_referencedDeclaration = possibleMembers.front().declaration;
+ m_type = possibleMembers.front().type;
if (type.getCategory() == Type::Category::Struct)
m_isLValue = true;
else if (type.getCategory() == Type::Category::Array)
@@ -714,9 +827,9 @@ void MemberAccess::checkTypeRequirements()
m_isLValue = false;
}
-void IndexAccess::checkTypeRequirements()
+void IndexAccess::checkTypeRequirements(TypePointers const*)
{
- m_base->checkTypeRequirements();
+ m_base->checkTypeRequirements(nullptr);
switch (m_base->getType()->getCategory())
{
case Type::Category::Array:
@@ -749,7 +862,7 @@ void IndexAccess::checkTypeRequirements()
m_type = make_shared<TypeType>(make_shared<ArrayType>(ArrayType::Location::Memory, type.getActualType()));
else
{
- m_index->checkTypeRequirements();
+ m_index->checkTypeRequirements(nullptr);
auto length = dynamic_cast<IntegerConstantType const*>(m_index->getType().get());
if (!length)
BOOST_THROW_EXCEPTION(m_index->createTypeError("Integer constant expected."));
@@ -764,22 +877,57 @@ void IndexAccess::checkTypeRequirements()
}
}
-void Identifier::checkTypeRequirements()
+void Identifier::checkTypeRequirements(TypePointers const* _argumentTypes)
{
- solAssert(m_referencedDeclaration, "Identifier not resolved.");
-
+ if (!m_referencedDeclaration)
+ {
+ if (!_argumentTypes)
+ BOOST_THROW_EXCEPTION(createTypeError("Unable to determine overloaded type."));
+ overloadResolution(*_argumentTypes);
+ }
+ solAssert(!!m_referencedDeclaration, "Referenced declaration is null after overload resolution.");
m_isLValue = m_referencedDeclaration->isLValue();
m_type = m_referencedDeclaration->getType(m_currentContract);
if (!m_type)
BOOST_THROW_EXCEPTION(createTypeError("Declaration referenced before type could be determined."));
}
-void ElementaryTypeNameExpression::checkTypeRequirements()
+Declaration const& Identifier::getReferencedDeclaration() const
+{
+ solAssert(!!m_referencedDeclaration, "Identifier not resolved.");
+ return *m_referencedDeclaration;
+}
+
+void Identifier::overloadResolution(TypePointers const& _argumentTypes)
+{
+ solAssert(!m_referencedDeclaration, "Referenced declaration should be null before overload resolution.");
+ solAssert(!m_overloadedDeclarations.empty(), "No candidates for overload resolution found.");
+
+ std::vector<Declaration const*> possibles;
+ if (m_overloadedDeclarations.size() == 1)
+ m_referencedDeclaration = *m_overloadedDeclarations.begin();
+
+ for (Declaration const* declaration: m_overloadedDeclarations)
+ {
+ TypePointer const& function = declaration->getType();
+ auto const* functionType = dynamic_cast<FunctionType const*>(function.get());
+ if (functionType && functionType->canTakeArguments(_argumentTypes))
+ possibles.push_back(declaration);
+ }
+ if (possibles.size() == 1)
+ m_referencedDeclaration = possibles.front();
+ else if (possibles.empty())
+ BOOST_THROW_EXCEPTION(createTypeError("No matching declaration found after argument-dependent lookup."));
+ else
+ BOOST_THROW_EXCEPTION(createTypeError("No unique declaration found after argument-dependent lookup."));
+}
+
+void ElementaryTypeNameExpression::checkTypeRequirements(TypePointers const*)
{
m_type = make_shared<TypeType>(Type::fromElementaryTypeName(m_typeToken));
}
-void Literal::checkTypeRequirements()
+void Literal::checkTypeRequirements(TypePointers const*)
{
m_type = Type::forLiteral(*this);
if (!m_type)