2014-10-07 16:25:04 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
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.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
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-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-07 16:25:04 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Solidity abstract syntax tree.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
|
|
|
#include <libsolidity/ast/AST_accept.h>
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2015-05-19 17:51:38 +00:00
|
|
|
#include <libdevcore/SHA3.h>
|
2015-01-07 15:39:21 +00:00
|
|
|
|
2017-01-24 16:38:06 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
|
2014-10-24 17:06:30 +00:00
|
|
|
using namespace std;
|
2015-09-16 14:56:30 +00:00
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
2014-10-24 17:06:30 +00:00
|
|
|
|
2017-01-20 10:47:20 +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;
|
|
|
|
};
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
ASTNode::ASTNode(SourceLocation const& _location):
|
2017-01-20 10:47:20 +00:00
|
|
|
m_id(IDDispenser::next()),
|
2015-09-16 14:56:30 +00:00
|
|
|
m_location(_location)
|
2014-10-16 12:08:54 +00:00
|
|
|
{
|
2014-10-23 17:22:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
ASTNode::~ASTNode()
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
2015-09-21 16:55:58 +00:00
|
|
|
delete m_annotation;
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:47:20 +00:00
|
|
|
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-01-22 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 12:41:40 +00:00
|
|
|
Error ASTNode::createTypeError(string const& _description) const
|
2014-12-15 15:09:50 +00:00
|
|
|
{
|
2015-10-02 12:41:40 +00:00
|
|
|
return Error(Error::Type::TypeError) << errinfo_sourceLocation(location()) << errinfo_comment(_description);
|
2014-12-15 15:09:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 14:46:03 +00:00
|
|
|
SourceUnitAnnotation& SourceUnit::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new SourceUnitAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<SourceUnitAnnotation&>(*m_annotation);
|
2015-12-15 14:46:03 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 10:46:43 +00:00
|
|
|
string Declaration::sourceUnitName() const
|
|
|
|
{
|
2016-11-24 09:32:52 +00:00
|
|
|
solAssert(!!m_scope, "");
|
2016-11-14 10:46:43 +00:00
|
|
|
ASTNode const* scope = m_scope;
|
|
|
|
while (dynamic_cast<Declaration const*>(scope) && dynamic_cast<Declaration const*>(scope)->m_scope)
|
|
|
|
scope = dynamic_cast<Declaration const*>(scope)->m_scope;
|
|
|
|
return dynamic_cast<SourceUnit const&>(*scope).annotation().path;
|
|
|
|
}
|
|
|
|
|
2015-12-09 16:35:20 +00:00
|
|
|
ImportAnnotation& ImportDirective::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new ImportAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
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
|
2014-11-11 16:41:48 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto exportedFunctionList = interfaceFunctionList();
|
2015-01-23 15:37:06 +00:00
|
|
|
|
2015-01-29 15:48:39 +00:00
|
|
|
map<FixedHash<4>, FunctionTypePointer> exportedFunctions;
|
2015-01-23 15:37:06 +00:00
|
|
|
for (auto const& it: exportedFunctionList)
|
2015-01-29 15:39:30 +00:00
|
|
|
exportedFunctions.insert(it);
|
2015-01-22 16:40:22 +00:00
|
|
|
|
2015-09-10 08:17:17 +00:00
|
|
|
solAssert(
|
|
|
|
exportedFunctionList.size() == exportedFunctions.size(),
|
2015-09-08 12:30:21 +00:00
|
|
|
"Hash collision at Function Definition Hash calculation"
|
|
|
|
);
|
2014-11-11 16:41:48 +00:00
|
|
|
|
|
|
|
return exportedFunctions;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
FunctionDefinition const* ContractDefinition::constructor() const
|
2014-12-12 15:49:26 +00:00
|
|
|
{
|
2015-11-23 22:57:17 +00:00
|
|
|
for (FunctionDefinition const* f: definedFunctions())
|
2015-01-20 14:58:04 +00:00
|
|
|
if (f->isConstructor())
|
2015-11-23 22:57:17 +00:00
|
|
|
return f;
|
2014-12-12 15:49:26 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:12:20 +00:00
|
|
|
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
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
|
2015-11-23 22:57:17 +00:00
|
|
|
for (FunctionDefinition const* f: contract->definedFunctions())
|
2015-08-31 16:44:29 +00:00
|
|
|
if (f->name().empty())
|
2015-11-23 22:57:17 +00:00
|
|
|
return f;
|
2015-01-29 21:50:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-23 22:57:17 +00:00
|
|
|
vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() const
|
2015-01-31 13:41:11 +00:00
|
|
|
{
|
|
|
|
if (!m_interfaceEvents)
|
|
|
|
{
|
|
|
|
set<string> eventsSeen;
|
2015-11-23 22:57:17 +00:00
|
|
|
m_interfaceEvents.reset(new vector<EventDefinition const*>());
|
2015-09-16 14:56:30 +00:00
|
|
|
for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
|
2015-11-23 22:57:17 +00:00
|
|
|
for (EventDefinition const* e: contract->events())
|
2015-08-31 16:44:29 +00:00
|
|
|
if (eventsSeen.count(e->name()) == 0)
|
2015-01-31 13:41:11 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
eventsSeen.insert(e->name());
|
2015-01-31 13:41:11 +00:00
|
|
|
m_interfaceEvents->push_back(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *m_interfaceEvents;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::interfaceFunctionList() const
|
2015-01-16 16:50:10 +00:00
|
|
|
{
|
|
|
|
if (!m_interfaceFunctionList)
|
|
|
|
{
|
|
|
|
set<string> functionsSeen;
|
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>>());
|
2015-09-16 14:56:30 +00:00
|
|
|
for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
|
2015-01-22 16:40:22 +00:00
|
|
|
{
|
2015-10-16 11:11:16 +00:00
|
|
|
vector<FunctionTypePointer> functions;
|
2015-11-23 22:57:17 +00:00
|
|
|
for (FunctionDefinition const* f: contract->definedFunctions())
|
2015-10-16 11:11:16 +00:00
|
|
|
if (f->isPartOfExternalInterface())
|
|
|
|
functions.push_back(make_shared<FunctionType>(*f, false));
|
2015-11-23 22:57:17 +00:00
|
|
|
for (VariableDeclaration const* v: contract->stateVariables())
|
2015-10-16 11:11:16 +00:00
|
|
|
if (v->isPartOfExternalInterface())
|
|
|
|
functions.push_back(make_shared<FunctionType>(*v));
|
|
|
|
for (FunctionTypePointer const& fun: functions)
|
2015-03-01 03:34:39 +00:00
|
|
|
{
|
2015-10-16 11:11:16 +00:00
|
|
|
if (!fun->interfaceFunctionType())
|
|
|
|
// Fails hopefully because we already registered the error
|
2015-06-26 14:52:30 +00:00
|
|
|
continue;
|
2015-10-16 11:11:16 +00:00
|
|
|
string functionSignature = fun->externalSignature();
|
2015-06-26 14:52:30 +00:00
|
|
|
if (signaturesSeen.count(functionSignature) == 0)
|
2015-01-16 16:50:10 +00:00
|
|
|
{
|
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));
|
2015-10-16 11:11:16 +00:00
|
|
|
m_interfaceFunctionList->push_back(make_pair(hash, fun));
|
2015-01-16 16:50:10 +00:00
|
|
|
}
|
2015-03-01 03:34:39 +00:00
|
|
|
}
|
2015-01-22 16:40:22 +00:00
|
|
|
}
|
2015-01-16 16:50:10 +00:00
|
|
|
}
|
|
|
|
return *m_interfaceFunctionList;
|
2015-01-14 09:16:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 01:04:00 +00:00
|
|
|
Json::Value const& ContractDefinition::devDocumentation() const
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
2015-06-19 13:40:09 +00:00
|
|
|
return m_devDocumentation;
|
2015-06-15 13:21:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 01:04:00 +00:00
|
|
|
Json::Value const& ContractDefinition::userDocumentation() const
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
2015-06-19 13:40:09 +00:00
|
|
|
return m_userDocumentation;
|
2015-06-15 13:21:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 01:04:00 +00:00
|
|
|
void ContractDefinition::setDevDocumentation(Json::Value const& _devDocumentation)
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
2015-06-19 13:40:09 +00:00
|
|
|
m_devDocumentation = _devDocumentation;
|
2015-06-15 13:21:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 01:04:00 +00:00
|
|
|
void ContractDefinition::setUserDocumentation(Json::Value const& _userDocumentation)
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
|
|
|
m_userDocumentation = _userDocumentation;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<Declaration const*> const& ContractDefinition::inheritableMembers() const
|
2015-02-25 19:35:55 +00:00
|
|
|
{
|
|
|
|
if (!m_inheritableMembers)
|
|
|
|
{
|
|
|
|
set<string> memberSeen;
|
2015-03-02 11:08:32 +00:00
|
|
|
m_inheritableMembers.reset(new vector<Declaration const*>());
|
|
|
|
auto addInheritableMember = [&](Declaration const* _decl)
|
2015-02-25 19:35:55 +00:00
|
|
|
{
|
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-02-27 10:35:25 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
memberSeen.insert(_decl->name());
|
2015-02-28 17:30:33 +00:00
|
|
|
m_inheritableMembers->push_back(_decl);
|
|
|
|
}
|
|
|
|
};
|
2015-02-27 10:35:25 +00:00
|
|
|
|
2015-11-23 22:57:17 +00:00
|
|
|
for (FunctionDefinition const* f: definedFunctions())
|
|
|
|
addInheritableMember(f);
|
2015-02-25 19:35:55 +00:00
|
|
|
|
2015-11-23 22:57:17 +00:00
|
|
|
for (VariableDeclaration const* v: stateVariables())
|
|
|
|
addInheritableMember(v);
|
2015-02-26 11:11:54 +00:00
|
|
|
|
2015-11-23 22:57:17 +00:00
|
|
|
for (StructDefinition const* s: definedStructs())
|
|
|
|
addInheritableMember(s);
|
2016-10-18 11:01:22 +00:00
|
|
|
|
|
|
|
for (EnumDefinition const* e: definedEnums())
|
|
|
|
addInheritableMember(e);
|
2016-10-26 11:08:00 +00:00
|
|
|
|
|
|
|
for (EventDefinition const* e: events())
|
|
|
|
addInheritableMember(e);
|
2015-02-25 19:35:55 +00:00
|
|
|
}
|
|
|
|
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-01-19 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 16:55:58 +00:00
|
|
|
ContractDefinitionAnnotation& ContractDefinition::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new ContractDefinitionAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
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
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
|
|
|
return make_shared<TypeType>(make_shared<StructType>(*this));
|
|
|
|
}
|
|
|
|
|
2015-10-05 15:19:23 +00:00
|
|
|
TypeDeclarationAnnotation& StructDefinition::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new TypeDeclarationAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<TypeDeclarationAnnotation&>(*m_annotation);
|
2015-10-05 15:19:23 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 17:02:04 +00:00
|
|
|
TypePointer EnumValue::type() const
|
2014-12-03 06:46:55 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +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
|
|
|
{
|
2015-02-11 15:37:46 +00:00
|
|
|
return make_shared<TypeType>(make_shared<EnumType>(*this));
|
2015-02-09 17:08:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 15:19:23 +00:00
|
|
|
TypeDeclarationAnnotation& EnumDefinition::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new TypeDeclarationAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<TypeDeclarationAnnotation&>(*m_annotation);
|
2015-10-05 15:19:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 17:55:36 +00:00
|
|
|
shared_ptr<FunctionType> 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 {};
|
|
|
|
default:
|
|
|
|
solAssert(false, "visibility() should not return a Visibility");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
default:
|
|
|
|
solAssert(false, "visibility() should not return a Visibility");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// To make the compiler happy
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2015-11-19 17:02:04 +00:00
|
|
|
TypePointer FunctionDefinition::type() const
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
|
|
|
return make_shared<FunctionType>(*this);
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:28:32 +00:00
|
|
|
string FunctionDefinition::externalSignature() const
|
2015-01-06 16:42:38 +00:00
|
|
|
{
|
2015-10-05 15:19:23 +00:00
|
|
|
return FunctionType(*this).externalSignature();
|
2015-01-06 16:42:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:13:36 +00:00
|
|
|
FunctionDefinitionAnnotation& FunctionDefinition::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new FunctionDefinitionAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
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
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
return make_shared<ModifierType>(*this);
|
2015-02-14 00:22:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:13:36 +00:00
|
|
|
ModifierDefinitionAnnotation& ModifierDefinition::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new ModifierDefinitionAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
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
|
2015-02-17 15:21:38 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
return make_shared<FunctionType>(*this);
|
|
|
|
}
|
2015-03-08 22:26:36 +00:00
|
|
|
|
2017-01-10 17:55:36 +00:00
|
|
|
std::shared_ptr<FunctionType> EventDefinition::functionType(bool _internal) const
|
|
|
|
{
|
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<UserDefinedTypeNameAnnotation&>(*m_annotation);
|
2015-09-21 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
bool VariableDeclaration::isLValue() const
|
|
|
|
{
|
|
|
|
// External function parameters and constant declared variables are Read-Only
|
|
|
|
return !isExternalCallableParameter() && !m_isConstant;
|
2015-02-17 15:21:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:26:08 +00:00
|
|
|
bool VariableDeclaration::isCallableParameter() const
|
2015-06-05 09:07:50 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
|
2015-06-09 12:26:08 +00:00
|
|
|
if (!callable)
|
2015-06-05 09:07:50 +00:00
|
|
|
return false;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (auto const& variable: callable->parameters())
|
2015-06-05 09:07:50 +00:00
|
|
|
if (variable.get() == this)
|
|
|
|
return true;
|
2015-08-31 16:44:29 +00:00
|
|
|
if (callable->returnParameterList())
|
|
|
|
for (auto const& variable: callable->returnParameterList()->parameters())
|
2015-06-09 12:26:08 +00:00
|
|
|
if (variable.get() == this)
|
|
|
|
return true;
|
2015-06-05 09:07:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:26:08 +00:00
|
|
|
bool VariableDeclaration::isExternalCallableParameter() const
|
2015-02-14 00:22:44 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
|
|
|
|
if (!callable || callable->visibility() != Declaration::Visibility::External)
|
2015-02-14 00:22:44 +00:00
|
|
|
return false;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (auto const& variable: callable->parameters())
|
2015-02-14 00:22:44 +00:00
|
|
|
if (variable.get() == this)
|
|
|
|
return true;
|
|
|
|
return false;
|
2015-01-22 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
bool VariableDeclaration::canHaveAutoType() const
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
|
|
|
|
return (!!callable && !isCallableParameter());
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 17:02:04 +00:00
|
|
|
TypePointer VariableDeclaration::type() const
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
return annotation().type;
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
2015-09-21 16:55:58 +00:00
|
|
|
|
2017-01-10 17:55:36 +00:00
|
|
|
shared_ptr<FunctionType> 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
|
|
|
default:
|
|
|
|
solAssert(false, "visibility() should not return a Visibility");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
2017-03-06 13:00:46 +00:00
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<StatementAnnotation&>(*m_annotation);
|
2015-10-26 16:20:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
InlineAssemblyAnnotation& InlineAssembly::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new InlineAssemblyAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<InlineAssemblyAnnotation&>(*m_annotation);
|
2016-03-01 21:56:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 16:55:58 +00:00
|
|
|
ReturnAnnotation& Return::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new ReturnAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<ReturnAnnotation&>(*m_annotation);
|
2015-09-21 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 18:44:56 +00:00
|
|
|
VariableDeclarationStatementAnnotation& VariableDeclarationStatement::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new VariableDeclarationStatementAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<VariableDeclarationStatementAnnotation&>(*m_annotation);
|
2015-10-09 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 16:55:58 +00:00
|
|
|
ExpressionAnnotation& Expression::annotation() const
|
|
|
|
{
|
|
|
|
if (!m_annotation)
|
|
|
|
m_annotation = new ExpressionAnnotation();
|
2017-03-06 13:00:46 +00:00
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
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();
|
2017-03-06 13:00:46 +00:00
|
|
|
return dynamic_cast<IdentifierAnnotation&>(*m_annotation);
|
2015-09-21 16:55:58 +00:00
|
|
|
}
|
2017-01-24 16:38:06 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-24 16:38:06 +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;
|
2017-01-24 16:38:06 +00:00
|
|
|
|
2017-06-26 11:49:05 +00:00
|
|
|
return abs(int(value().length()) - 42) <= 1;
|
2017-01-24 16:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Literal::passesAddressChecksum() const
|
|
|
|
{
|
2017-06-28 15:59:34 +00:00
|
|
|
solAssert(isHexNumber(), "Expected hex number");
|
2017-06-26 11:49:05 +00:00
|
|
|
return dev::passesAddressChecksum(value(), true);
|
2017-01-24 16:38:06 +00:00
|
|
|
}
|