diff options
author | Lefteris Karapetsas <lefteris@refu.co> | 2015-01-23 23:37:06 +0800 |
---|---|---|
committer | Lefteris Karapetsas <lefteris@refu.co> | 2015-01-29 04:46:16 +0800 |
commit | 5c7359aa09c46eb7fc27a70e328adde93d4844ab (patch) | |
tree | df6c6146f6d3b417fdc7312faac11df2374487bc /AST.cpp | |
parent | 3cc04923015cc3f40ad285fba5ed71464bd9ff2a (diff) | |
download | dexon-solidity-5c7359aa09c46eb7fc27a70e328adde93d4844ab.tar dexon-solidity-5c7359aa09c46eb7fc27a70e328adde93d4844ab.tar.gz dexon-solidity-5c7359aa09c46eb7fc27a70e328adde93d4844ab.tar.bz2 dexon-solidity-5c7359aa09c46eb7fc27a70e328adde93d4844ab.tar.lz dexon-solidity-5c7359aa09c46eb7fc27a70e328adde93d4844ab.tar.xz dexon-solidity-5c7359aa09c46eb7fc27a70e328adde93d4844ab.tar.zst dexon-solidity-5c7359aa09c46eb7fc27a70e328adde93d4844ab.zip |
State variable accessors code is now more organized
- FunctionDescription is the abstraction of what should describe a
function. It can either be a VariableDeclaration of a
FunctionDefinition.
- ParamDescription is what FunctionDescription uses to describe its
parameters for outside use purposes with a pair of (name, type)
strings
- Modified code around Solidity and especially interface handler to
adapt to this change
Diffstat (limited to 'AST.cpp')
-rw-r--r-- | AST.cpp | 115 |
1 files changed, 102 insertions, 13 deletions
@@ -71,17 +71,19 @@ void ContractDefinition::checkTypeRequirements() FixedHash<4> const& hash = std::get<0>(hashAndFunction); if (hashes.count(hash)) BOOST_THROW_EXCEPTION(createTypeError( - "Function signature hash collision for " + - std::get<1>(hashAndFunction)>->getCanonicalSignature(std::get<2>(hashAndFunction)->getName()))); + std::string("Function signature hash collision for ") + + std::get<1>(hashAndFunction)->getCanonicalSignature(std::get<2>(hashAndFunction)->getName()))); hashes.insert(hash); } } -map<FixedHash<4>, pair<FunctionType const*, FunctionDefinition const*>> ContractDefinition::getInterfaceFunctions() const +map<FixedHash<4>, FunctionDescription> ContractDefinition::getInterfaceFunctions() const { - vector<tuple<FixedHash<4>, FunctionType const*, Declaration const*>>> exportedFunctionList = getInterfaceFunctionList(); - map<FixedHash<4>, pair<FunctionType *, Declaration const*>> exportedFunctions(exportedFunctionList.begin(), - exportedFunctionList.end()); + auto exportedFunctionList = getInterfaceFunctionList(); + + map<FixedHash<4>, FunctionDescription> exportedFunctions; + for (auto const& it: exportedFunctionList) + exportedFunctions[std::get<0>(it)] = std::move(FunctionDescription(std::get<1>(it), std::get<2>(it))); solAssert(exportedFunctionList.size() == exportedFunctions.size(), "Hash collision at Function Definition Hash calculation"); @@ -136,12 +138,12 @@ void ContractDefinition::checkIllegalOverrides() const } } -vector<tuple<FixedHash<4>, FunctionType const*, Declaration const*>> const& ContractDefinition::getInterfaceFunctionList() const +vector<tuple<FixedHash<4>, std::shared_ptr<FunctionType const>, Declaration const*>> const& ContractDefinition::getInterfaceFunctionList() const { if (!m_interfaceFunctionList) { set<string> functionsSeen; - m_interfaceFunctionList.reset(new vector<tuple<FixedHash<4>, FunctionType const*, Declaration const*>>()); + m_interfaceFunctionList.reset(new vector<tuple<FixedHash<4>, std::shared_ptr<FunctionType const>, Declaration const*>>()); for (ContractDefinition const* contract: getLinearizedBaseContracts()) { for (ASTPointer<FunctionDefinition> const& f: contract->getDefinedFunctions()) @@ -149,7 +151,7 @@ vector<tuple<FixedHash<4>, FunctionType const*, Declaration const*>> const& Cont { functionsSeen.insert(f->getName()); FixedHash<4> hash(dev::sha3(f->getCanonicalSignature())); - m_interfaceFunctionList->push_back(make_tuple(hash, FunctionType(*f), f.get())); + m_interfaceFunctionList->push_back(make_tuple(hash, make_shared<FunctionType>(*f), f.get())); } for (ASTPointer<VariableDeclaration> const& v: contract->getStateVariables()) @@ -157,8 +159,8 @@ vector<tuple<FixedHash<4>, FunctionType const*, Declaration const*>> const& Cont { FunctionType ftype(*v); functionsSeen.insert(v->getName()); - FixedHash<4> hash(dev::sha3(ftype.getCanonicalSignature(v->getName())); - m_interfaceFunctionList->push_back(make_tuple(hash, ftype, v.get())); + FixedHash<4> hash(dev::sha3(ftype.getCanonicalSignature(v->getName()))); + m_interfaceFunctionList->push_back(make_tuple(hash, make_shared<FunctionType>(*v), v.get())); } } } @@ -518,17 +520,104 @@ void Literal::checkTypeRequirements() } -ASTPointer<ASTString> FunctionDescription::getDocumentation() +bool ParamDescription::operator!=(ParamDescription const& _other) const +{ + return m_description.first == _other.getName() && m_description.second == _other.getType(); +} + +std::ostream& ParamDescription::operator<<(std::ostream& os) const +{ + return os << m_description.first << ":" << m_description.second; +} + +std::string ParamDescription::getName() const +{ + return m_description.first; +} + +std::string ParamDescription::getType() const +{ + return m_description.second; +} + + +ASTPointer<ASTString> FunctionDescription::getDocumentation() const { auto function = dynamic_cast<FunctionDefinition const*>(m_description.second); if (function) return function->getDocumentation(); + + return ASTPointer<ASTString>(); } -string FunctionDescription::getSignature() +string FunctionDescription::getSignature() const { return m_description.first->getCanonicalSignature(m_description.second->getName()); } +string FunctionDescription::getName() const +{ + return m_description.second->getName(); +} + +bool FunctionDescription::isConstant() const +{ + auto function = dynamic_cast<FunctionDefinition const*>(m_description.second); + if (function) + return function->isDeclaredConst(); + + return true; +} + +vector<ParamDescription> const FunctionDescription::getParameters() const +{ + auto function = dynamic_cast<FunctionDefinition const*>(m_description.second); + if (function) + { + vector<ParamDescription> paramsDescription; + for (auto const& param: function->getParameters()) + paramsDescription.push_back(ParamDescription(param->getName(), param->getType()->toString())); + + return paramsDescription; + } + + // else for now let's assume no parameters to accessors + // LTODO: fix this for mapping types + return {}; +} + +vector<ParamDescription> const FunctionDescription::getReturnParameters() const +{ + auto function = dynamic_cast<FunctionDefinition const*>(m_description.second); + if (function) + { + vector<ParamDescription> paramsDescription; + for (auto const& param: function->getParameters()) + paramsDescription.push_back(ParamDescription(param->getName(), param->getType()->toString())); + + return paramsDescription; + } + + auto vardecl = dynamic_cast<VariableDeclaration const*>(m_description.second); + return {ParamDescription(vardecl->getName(), vardecl->getType()->toString())}; +} + +Declaration const* FunctionDescription::getDeclaration() const +{ + return m_description.second; +} + +shared_ptr<FunctionType const> FunctionDescription::getFunctionTypeShared() const +{ + return m_description.first; +} + + +FunctionType const* FunctionDescription::getFunctionType() const +{ + return m_description.first.get(); +} + + } } |