solidity/libsolidity/ast/AST.cpp

674 lines
18 KiB
C++
Raw Normal View History

/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
2014-10-16 12:08:54 +00:00
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.
solidity is distributed in the hope that it will be useful,
2014-10-16 12:08:54 +00:00
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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Solidity abstract syntax tree.
*/
2015-10-20 22:21:52 +00:00
#include <libsolidity/ast/AST.h>
2018-12-17 14:33:15 +00:00
2015-10-20 22:21:52 +00:00
#include <libsolidity/ast/ASTVisitor.h>
#include <libsolidity/ast/AST_accept.h>
2018-10-18 11:35:20 +00:00
#include <libdevcore/Keccak256.h>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <functional>
2014-10-24 17:06:30 +00:00
using namespace std;
using namespace dev;
using namespace dev::solidity;
2014-10-24 17:06:30 +00:00
class IDDispenser
{
public:
static size_t next() { return ++instance(); }
static void reset() { instance() = 0; }
private:
static size_t& instance()
{
static IDDispenser dispenser;
return dispenser.id;
}
size_t id = 0;
};
ASTNode::ASTNode(SourceLocation const& _location):
m_id(IDDispenser::next()),
m_location(_location)
2014-10-16 12:08:54 +00:00
{
2014-10-23 17:22:30 +00:00
}
ASTNode::~ASTNode()
{
2015-09-21 16:55:58 +00:00
delete m_annotation;
}
void ASTNode::resetID()
{
IDDispenser::reset();
}
2015-09-21 16:55:58 +00:00
ASTAnnotation& ASTNode::annotation() const
{
if (!m_annotation)
m_annotation = new ASTAnnotation();
return *m_annotation;
}
2015-12-15 14:46:03 +00:00
SourceUnitAnnotation& SourceUnit::annotation() const
{
if (!m_annotation)
m_annotation = new SourceUnitAnnotation();
return dynamic_cast<SourceUnitAnnotation&>(*m_annotation);
2015-12-15 14:46:03 +00:00
}
set<SourceUnit const*> SourceUnit::referencedSourceUnits(bool _recurse, set<SourceUnit const*> _skipList) const
2017-07-12 23:08:28 +00:00
{
set<SourceUnit const*> sourceUnits;
2017-07-12 23:08:28 +00:00
for (ImportDirective const* importDirective: filteredNodes<ImportDirective>(nodes()))
{
auto const& sourceUnit = importDirective->annotation().sourceUnit;
if (!_skipList.count(sourceUnit))
{
_skipList.insert(sourceUnit);
sourceUnits.insert(sourceUnit);
if (_recurse)
2017-07-13 23:17:52 +00:00
sourceUnits += sourceUnit->referencedSourceUnits(true, _skipList);
}
2017-07-12 23:08:28 +00:00
}
return sourceUnits;
}
2015-12-09 16:35:20 +00:00
ImportAnnotation& ImportDirective::annotation() const
{
if (!m_annotation)
m_annotation = new ImportAnnotation();
return dynamic_cast<ImportAnnotation&>(*m_annotation);
2015-12-09 16:35:20 +00:00
}
2015-12-15 14:46:03 +00:00
TypePointer ImportDirective::type() const
{
solAssert(!!annotation().sourceUnit, "");
return make_shared<ModuleType>(*annotation().sourceUnit);
}
2015-08-31 16:44:29 +00:00
map<FixedHash<4>, FunctionTypePointer> ContractDefinition::interfaceFunctions() const
{
2015-08-31 16:44:29 +00:00
auto exportedFunctionList = interfaceFunctionList();
map<FixedHash<4>, FunctionTypePointer> exportedFunctions;
for (auto const& it: exportedFunctionList)
exportedFunctions.insert(it);
2015-09-10 08:17:17 +00:00
solAssert(
exportedFunctionList.size() == exportedFunctions.size(),
"Hash collision at Function Definition Hash calculation"
);
return exportedFunctions;
}
2015-08-31 16:44:29 +00:00
FunctionDefinition const* ContractDefinition::constructor() const
2014-12-12 15:49:26 +00:00
{
for (FunctionDefinition const* f: definedFunctions())
if (f->isConstructor())
return f;
2014-12-12 15:49:26 +00:00
return nullptr;
}
bool ContractDefinition::constructorIsPublic() const
{
FunctionDefinition const* f = constructor();
return !f || f->isPublic();
}
2015-08-31 16:44:29 +00:00
FunctionDefinition const* ContractDefinition::fallbackFunction() const
2015-01-29 21:50:20 +00:00
{
for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
for (FunctionDefinition const* f: contract->definedFunctions())
2017-07-27 19:55:55 +00:00
if (f->isFallback())
return f;
2015-01-29 21:50:20 +00:00
return nullptr;
}
vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() const
{
if (!m_interfaceEvents)
{
set<string> eventsSeen;
m_interfaceEvents.reset(new vector<EventDefinition const*>());
for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
for (EventDefinition const* e: contract->events())
2017-08-25 09:41:48 +00:00
{
/// NOTE: this requires the "internal" version of an Event,
/// though here internal strictly refers to visibility,
/// and not to function encoding (jump vs. call)
auto const& function = e->functionType(true);
solAssert(function, "");
string eventSignature = function->externalSignature();
if (eventsSeen.count(eventSignature) == 0)
{
2017-08-25 09:41:48 +00:00
eventsSeen.insert(eventSignature);
m_interfaceEvents->push_back(e);
}
2017-08-25 09:41:48 +00:00
}
}
return *m_interfaceEvents;
}
2015-08-31 16:44:29 +00:00
vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::interfaceFunctionList() const
{
if (!m_interfaceFunctionList)
{
2015-03-01 03:34:39 +00:00
set<string> signaturesSeen;
2015-01-29 16:28:14 +00:00
m_interfaceFunctionList.reset(new vector<pair<FixedHash<4>, FunctionTypePointer>>());
for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
{
vector<FunctionTypePointer> functions;
for (FunctionDefinition const* f: contract->definedFunctions())
if (f->isPartOfExternalInterface())
functions.push_back(make_shared<FunctionType>(*f, false));
for (VariableDeclaration const* v: contract->stateVariables())
if (v->isPartOfExternalInterface())
functions.push_back(make_shared<FunctionType>(*v));
for (FunctionTypePointer const& fun: functions)
2015-03-01 03:34:39 +00:00
{
if (!fun->interfaceFunctionType())
// Fails hopefully because we already registered the error
continue;
string functionSignature = fun->externalSignature();
if (signaturesSeen.count(functionSignature) == 0)
{
2015-03-01 03:34:39 +00:00
signaturesSeen.insert(functionSignature);
2016-10-05 10:30:28 +00:00
FixedHash<4> hash(dev::keccak256(functionSignature));
m_interfaceFunctionList->emplace_back(hash, fun);
}
2015-03-01 03:34:39 +00:00
}
}
}
return *m_interfaceFunctionList;
}
2015-08-31 16:44:29 +00:00
vector<Declaration const*> const& ContractDefinition::inheritableMembers() const
{
if (!m_inheritableMembers)
{
set<string> memberSeen;
m_inheritableMembers.reset(new vector<Declaration const*>());
auto addInheritableMember = [&](Declaration const* _decl)
{
2016-10-21 10:14:24 +00:00
solAssert(_decl, "addInheritableMember got a nullpointer.");
2015-08-31 16:44:29 +00:00
if (memberSeen.count(_decl->name()) == 0 && _decl->isVisibleInDerivedContracts())
{
2015-08-31 16:44:29 +00:00
memberSeen.insert(_decl->name());
m_inheritableMembers->push_back(_decl);
}
};
for (FunctionDefinition const* f: definedFunctions())
addInheritableMember(f);
for (VariableDeclaration const* v: stateVariables())
addInheritableMember(v);
2015-02-26 11:11:54 +00:00
for (StructDefinition const* s: definedStructs())
addInheritableMember(s);
for (EnumDefinition const* e: definedEnums())
addInheritableMember(e);
for (EventDefinition const* e: events())
addInheritableMember(e);
}
return *m_inheritableMembers;
}
2015-11-19 17:02:04 +00:00
TypePointer ContractDefinition::type() const
2015-02-10 12:40:21 +00:00
{
2015-11-19 17:02:04 +00:00
return make_shared<TypeType>(make_shared<ContractType>(*this));
}
2015-09-21 16:55:58 +00:00
ContractDefinitionAnnotation& ContractDefinition::annotation() const
{
if (!m_annotation)
m_annotation = new ContractDefinitionAnnotation();
return dynamic_cast<ContractDefinitionAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
TypeNameAnnotation& TypeName::annotation() const
{
if (!m_annotation)
m_annotation = new TypeNameAnnotation();
return dynamic_cast<TypeNameAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
2015-11-19 17:02:04 +00:00
TypePointer StructDefinition::type() const
{
return make_shared<TypeType>(make_shared<StructType>(*this));
}
TypeDeclarationAnnotation& StructDefinition::annotation() const
{
if (!m_annotation)
m_annotation = new TypeDeclarationAnnotation();
return dynamic_cast<TypeDeclarationAnnotation&>(*m_annotation);
}
2015-11-19 17:02:04 +00:00
TypePointer EnumValue::type() const
2014-12-03 06:46:55 +00:00
{
auto parentDef = dynamic_cast<EnumDefinition const*>(scope());
solAssert(parentDef, "Enclosing Scope of EnumValue was not set");
return make_shared<EnumType>(*parentDef);
2014-12-03 06:46:55 +00:00
}
2015-11-19 17:02:04 +00:00
TypePointer EnumDefinition::type() const
2015-02-09 17:08:56 +00:00
{
return make_shared<TypeType>(make_shared<EnumType>(*this));
2015-02-09 17:08:56 +00:00
}
TypeDeclarationAnnotation& EnumDefinition::annotation() const
{
if (!m_annotation)
m_annotation = new TypeDeclarationAnnotation();
return dynamic_cast<TypeDeclarationAnnotation&>(*m_annotation);
}
ContractDefinition::ContractKind FunctionDefinition::inContractKind() const
{
auto contractDef = dynamic_cast<ContractDefinition const*>(scope());
solAssert(contractDef, "Enclosing Scope of FunctionDefinition was not set.");
return contractDef->contractKind();
}
FunctionTypePointer FunctionDefinition::functionType(bool _internal) const
2017-01-10 15:26:13 +00:00
{
if (_internal)
{
switch (visibility())
{
case Declaration::Visibility::Default:
solAssert(false, "visibility() should not return Default");
case Declaration::Visibility::Private:
case Declaration::Visibility::Internal:
case Declaration::Visibility::Public:
2017-01-10 17:55:36 +00:00
return make_shared<FunctionType>(*this, _internal);
2017-01-10 15:26:13 +00:00
case Declaration::Visibility::External:
return {};
}
}
else
{
switch (visibility())
{
case Declaration::Visibility::Default:
solAssert(false, "visibility() should not return Default");
case Declaration::Visibility::Private:
case Declaration::Visibility::Internal:
return {};
case Declaration::Visibility::Public:
case Declaration::Visibility::External:
2017-01-10 17:55:36 +00:00
return make_shared<FunctionType>(*this, _internal);
2017-01-10 15:26:13 +00:00
}
}
// To make the compiler happy
return {};
}
2015-11-19 17:02:04 +00:00
TypePointer FunctionDefinition::type() const
{
solAssert(visibility() != Declaration::Visibility::External, "");
return make_shared<FunctionType>(*this);
}
string FunctionDefinition::externalSignature() const
{
return FunctionType(*this).externalSignature();
}
2015-10-26 14:13:36 +00:00
FunctionDefinitionAnnotation& FunctionDefinition::annotation() const
{
if (!m_annotation)
m_annotation = new FunctionDefinitionAnnotation();
return dynamic_cast<FunctionDefinitionAnnotation&>(*m_annotation);
2015-10-26 14:13:36 +00:00
}
2015-11-19 17:02:04 +00:00
TypePointer ModifierDefinition::type() const
{
return make_shared<ModifierType>(*this);
}
2015-10-26 14:13:36 +00:00
ModifierDefinitionAnnotation& ModifierDefinition::annotation() const
{
if (!m_annotation)
m_annotation = new ModifierDefinitionAnnotation();
return dynamic_cast<ModifierDefinitionAnnotation&>(*m_annotation);
2015-10-26 14:13:36 +00:00
}
2015-11-19 17:02:04 +00:00
TypePointer EventDefinition::type() const
{
return make_shared<FunctionType>(*this);
}
2015-03-08 22:26:36 +00:00
FunctionTypePointer EventDefinition::functionType(bool _internal) const
2017-01-10 17:55:36 +00:00
{
if (_internal)
return make_shared<FunctionType>(*this);
else
return {};
}
2015-10-26 14:13:36 +00:00
EventDefinitionAnnotation& EventDefinition::annotation() const
{
if (!m_annotation)
m_annotation = new EventDefinitionAnnotation();
return dynamic_cast<EventDefinitionAnnotation&>(*m_annotation);
2015-10-26 14:13:36 +00:00
}
2015-09-21 16:55:58 +00:00
UserDefinedTypeNameAnnotation& UserDefinedTypeName::annotation() const
{
if (!m_annotation)
m_annotation = new UserDefinedTypeNameAnnotation();
return dynamic_cast<UserDefinedTypeNameAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
SourceUnit const& Scopable::sourceUnit() const
{
ASTNode const* s = scope();
solAssert(s, "");
// will not always be a declaration
while (dynamic_cast<Scopable const*>(s) && dynamic_cast<Scopable const*>(s)->scope())
s = dynamic_cast<Scopable const*>(s)->scope();
return dynamic_cast<SourceUnit const&>(*s);
}
string Scopable::sourceUnitName() const
{
return sourceUnit().annotation().path;
}
bool VariableDeclaration::isLValue() const
{
// External function parameters and constant declared variables are Read-Only
return !isExternalCallableParameter() && !m_isConstant;
}
2018-02-09 15:53:52 +00:00
bool VariableDeclaration::isLocalVariable() const
{
auto s = scope();
return
2018-08-07 13:58:34 +00:00
dynamic_cast<FunctionTypeName const*>(s) ||
2018-02-09 15:53:52 +00:00
dynamic_cast<CallableDeclaration const*>(s) ||
dynamic_cast<Block const*>(s) ||
dynamic_cast<ForStatement const*>(s);
}
bool VariableDeclaration::isCallableParameter() const
{
2018-08-07 13:58:34 +00:00
if (isReturnParameter())
return true;
vector<ASTPointer<VariableDeclaration>> const* parameters = nullptr;
if (auto const* funTypeName = dynamic_cast<FunctionTypeName const*>(scope()))
parameters = &funTypeName->parameterTypes();
else if (auto const* callable = dynamic_cast<CallableDeclaration const*>(scope()))
parameters = &callable->parameters();
if (parameters)
for (auto const& variable: *parameters)
if (variable.get() == this)
return true;
return false;
}
2017-07-06 09:05:05 +00:00
bool VariableDeclaration::isLocalOrReturn() const
{
return isReturnParameter() || (isLocalVariable() && !isCallableParameter());
}
bool VariableDeclaration::isReturnParameter() const
{
2018-08-07 13:58:34 +00:00
vector<ASTPointer<VariableDeclaration>> const* returnParameters = nullptr;
if (auto const* funTypeName = dynamic_cast<FunctionTypeName const*>(scope()))
returnParameters = &funTypeName->returnParameterTypes();
else if (auto const* callable = dynamic_cast<CallableDeclaration const*>(scope()))
if (callable->returnParameterList())
returnParameters = &callable->returnParameterList()->parameters();
if (returnParameters)
for (auto const& variable: *returnParameters)
2017-07-06 09:05:05 +00:00
if (variable.get() == this)
return true;
return false;
}
bool VariableDeclaration::isExternalCallableParameter() const
{
2018-08-07 13:58:34 +00:00
if (!isCallableParameter())
return false;
2018-08-07 13:58:34 +00:00
if (auto const* callable = dynamic_cast<CallableDeclaration const*>(scope()))
if (callable->visibility() == Declaration::Visibility::External)
return !isReturnParameter();
return false;
}
2018-08-07 13:58:34 +00:00
bool VariableDeclaration::isInternalCallableParameter() const
{
if (!isCallableParameter())
return false;
if (auto const* funTypeName = dynamic_cast<FunctionTypeName const*>(scope()))
return funTypeName->visibility() == Declaration::Visibility::Internal;
else if (auto const* callable = dynamic_cast<CallableDeclaration const*>(scope()))
return callable->visibility() <= Declaration::Visibility::Internal;
return false;
}
bool VariableDeclaration::isLibraryFunctionParameter() const
{
if (!isCallableParameter())
return false;
if (auto const* funDef = dynamic_cast<FunctionDefinition const*>(scope()))
return dynamic_cast<ContractDefinition const&>(*funDef->scope()).isLibrary();
else
return false;
}
bool VariableDeclaration::isEventParameter() const
{
return dynamic_cast<EventDefinition const*>(scope()) != nullptr;
}
bool VariableDeclaration::hasReferenceOrMappingType() const
{
solAssert(typeName(), "");
solAssert(typeName()->annotation().type, "Can only be called after reference resolution");
TypePointer const& type = typeName()->annotation().type;
return type->category() == Type::Category::Mapping || dynamic_cast<ReferenceType const*>(type.get());
}
set<VariableDeclaration::Location> VariableDeclaration::allowedDataLocations() const
{
using Location = VariableDeclaration::Location;
if (!hasReferenceOrMappingType() || isStateVariable() || isEventParameter())
return set<Location>{ Location::Unspecified };
2018-08-07 13:58:34 +00:00
else if (isStateVariable() && isConstant())
return set<Location>{ Location::Memory };
else if (isExternalCallableParameter())
{
set<Location> locations{ Location::CallData };
if (isLibraryFunctionParameter())
locations.insert(Location::Storage);
return locations;
}
else if (isCallableParameter())
{
set<Location> locations{ Location::Memory };
if (isInternalCallableParameter() || isLibraryFunctionParameter())
locations.insert(Location::Storage);
return locations;
}
else if (isLocalVariable())
{
solAssert(typeName(), "");
solAssert(typeName()->annotation().type, "Can only be called after reference resolution");
if (typeName()->annotation().type->category() == Type::Category::Mapping)
return set<Location>{ Location::Storage };
else
// TODO: add Location::Calldata once implemented for local variables.
return set<Location>{ Location::Memory, Location::Storage };
}
else
// Struct members etc.
return set<Location>{ Location::Unspecified };
2018-08-07 13:58:34 +00:00
}
2015-11-19 17:02:04 +00:00
TypePointer VariableDeclaration::type() const
2014-10-13 16:22:15 +00:00
{
return annotation().type;
2014-10-16 12:08:54 +00:00
}
2015-09-21 16:55:58 +00:00
FunctionTypePointer VariableDeclaration::functionType(bool _internal) const
2017-01-10 15:26:13 +00:00
{
if (_internal)
return {};
switch (visibility())
{
case Declaration::Visibility::Default:
solAssert(false, "visibility() should not return Default");
case Declaration::Visibility::Private:
case Declaration::Visibility::Internal:
return {};
case Declaration::Visibility::Public:
case Declaration::Visibility::External:
2017-01-10 17:55:36 +00:00
return make_shared<FunctionType>(*this);
2017-01-10 15:26:13 +00:00
}
// To make the compiler happy
return {};
}
2015-09-21 16:55:58 +00:00
VariableDeclarationAnnotation& VariableDeclaration::annotation() const
{
if (!m_annotation)
m_annotation = new VariableDeclarationAnnotation();
return dynamic_cast<VariableDeclarationAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
2015-10-26 16:20:29 +00:00
StatementAnnotation& Statement::annotation() const
{
if (!m_annotation)
m_annotation = new StatementAnnotation();
return dynamic_cast<StatementAnnotation&>(*m_annotation);
2015-10-26 16:20:29 +00:00
}
InlineAssemblyAnnotation& InlineAssembly::annotation() const
{
if (!m_annotation)
m_annotation = new InlineAssemblyAnnotation();
return dynamic_cast<InlineAssemblyAnnotation&>(*m_annotation);
}
2015-09-21 16:55:58 +00:00
ReturnAnnotation& Return::annotation() const
{
if (!m_annotation)
m_annotation = new ReturnAnnotation();
return dynamic_cast<ReturnAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
ExpressionAnnotation& Expression::annotation() const
{
if (!m_annotation)
m_annotation = new ExpressionAnnotation();
return dynamic_cast<ExpressionAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
MemberAccessAnnotation& MemberAccess::annotation() const
{
if (!m_annotation)
m_annotation = new MemberAccessAnnotation();
return dynamic_cast<MemberAccessAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
BinaryOperationAnnotation& BinaryOperation::annotation() const
{
if (!m_annotation)
m_annotation = new BinaryOperationAnnotation();
return dynamic_cast<BinaryOperationAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
FunctionCallAnnotation& FunctionCall::annotation() const
{
if (!m_annotation)
m_annotation = new FunctionCallAnnotation();
return dynamic_cast<FunctionCallAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
IdentifierAnnotation& Identifier::annotation() const
{
if (!m_annotation)
m_annotation = new IdentifierAnnotation();
return dynamic_cast<IdentifierAnnotation&>(*m_annotation);
2015-09-21 16:55:58 +00:00
}
ASTString Literal::valueWithoutUnderscores() const
{
return boost::erase_all_copy(value(), "_");
}
2017-06-28 15:59:34 +00:00
bool Literal::isHexNumber() const
2017-06-26 11:49:05 +00:00
{
if (token() != Token::Number)
return false;
2017-06-28 16:00:14 +00:00
return boost::starts_with(value(), "0x");
2017-06-26 11:49:05 +00:00
}
bool Literal::looksLikeAddress() const
{
if (subDenomination() != SubDenomination::None)
return false;
2017-06-26 11:49:05 +00:00
2017-06-28 15:59:34 +00:00
if (!isHexNumber())
2017-06-22 14:39:42 +00:00
return false;
return abs(int(valueWithoutUnderscores().length()) - 42) <= 1;
}
bool Literal::passesAddressChecksum() const
{
2017-06-28 15:59:34 +00:00
solAssert(isHexNumber(), "Expected hex number");
return dev::passesAddressChecksum(valueWithoutUnderscores(), true);
}
2018-08-07 13:58:34 +00:00
string Literal::getChecksummedAddress() const
{
solAssert(isHexNumber(), "Expected hex number");
/// Pad literal to be a proper hex address.
string address = valueWithoutUnderscores().substr(2);
if (address.length() > 40)
return string();
address.insert(address.begin(), 40 - address.size(), '0');
return dev::getChecksummedAddress(address);
}