From 307a83e1dea95cd144a955aa0891476e7dd159de Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 12 Jan 2015 12:46:52 +0100 Subject: [PATCH] More convenient function type construction. --- ExpressionCompiler.cpp | 2 +- GlobalContext.cpp | 45 ++++++++++-------------------------------- Scanner.cpp | 20 +------------------ Token.cpp | 17 ++++++++++++++++ Token.h | 2 ++ Types.cpp | 29 +++++++++++++++------------ Types.h | 6 ++++++ 7 files changed, 53 insertions(+), 68 deletions(-) diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 4fdea3326..1c02f4f32 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -250,7 +250,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true); }; - appendExternalFunctionCall(FunctionType({}, {}, Location::EXTERNAL), {}, options); + appendExternalFunctionCall(FunctionType(TypePointers{}, TypePointers{}, Location::EXTERNAL), {}, options); break; } case Location::SUICIDE: diff --git a/GlobalContext.cpp b/GlobalContext.cpp index 633331a90..26a52fd10 100644 --- a/GlobalContext.cpp +++ b/GlobalContext.cpp @@ -34,54 +34,29 @@ namespace solidity { GlobalContext::GlobalContext(): -// TODO: make this cleaner. m_magicVariables(vector>{make_shared("block", make_shared(MagicType::Kind::BLOCK)), make_shared("msg", make_shared(MagicType::Kind::MSG)), make_shared("tx", make_shared(MagicType::Kind::TX)), make_shared("suicide", - make_shared(TypePointers({std::make_shared(0, - IntegerType::Modifier::ADDRESS)}), - TypePointers(), - FunctionType::Location::SUICIDE)), + make_shared(vector{"address"}, vector{}, FunctionType::Location::SUICIDE)), make_shared("sha3", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers({std::make_shared(256, IntegerType::Modifier::HASH)}), - FunctionType::Location::SHA3)), + make_shared(vector{"hash"}, vector{"hash"}, FunctionType::Location::SHA3)), make_shared("log0", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers(), - FunctionType::Location::LOG0)), + make_shared(vector{"hash"},vector{}, FunctionType::Location::LOG0)), make_shared("log1", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers(), - FunctionType::Location::LOG1)), + make_shared(vector{"hash", "hash"},vector{}, FunctionType::Location::LOG1)), make_shared("log2", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers(), - FunctionType::Location::LOG2)), + make_shared(vector{"hash", "hash", "hash"},vector{}, FunctionType::Location::LOG2)), make_shared("log3", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers(), - FunctionType::Location::LOG3)), + make_shared(vector{"hash", "hash", "hash", "hash"},vector{}, FunctionType::Location::LOG3)), make_shared("log4", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH), std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers(), - FunctionType::Location::LOG4)), + make_shared(vector{"hash", "hash", "hash", "hash", "hash"},vector{}, FunctionType::Location::LOG4)), make_shared("sha256", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers({std::make_shared(256, IntegerType::Modifier::HASH)}), - FunctionType::Location::SHA256)), + make_shared(vector{"hash"}, vector{"hash"}, FunctionType::Location::SHA256)), make_shared("ecrecover", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH), - std::make_shared(8, IntegerType::Modifier::HASH), - std::make_shared(256, IntegerType::Modifier::HASH), - std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers({std::make_shared(0, IntegerType::Modifier::ADDRESS)}), - FunctionType::Location::ECRECOVER)), + make_shared(vector{"hash", "hash8", "hash", "hash"}, vector{"address"}, FunctionType::Location::ECRECOVER)), make_shared("ripemd160", - make_shared(TypePointers({std::make_shared(256, IntegerType::Modifier::HASH)}), - TypePointers({std::make_shared(160, IntegerType::Modifier::HASH)}), - FunctionType::Location::RIPEMD160))}) + make_shared(vector{"hash"}, vector{"hash160"}, FunctionType::Location::RIPEMD160))}) { } diff --git a/Scanner.cpp b/Scanner.cpp index 6e3d04bc5..b283ca10e 100644 --- a/Scanner.cpp +++ b/Scanner.cpp @@ -700,24 +700,6 @@ Token::Value Scanner::scanNumber(char _charSeen) return Token::NUMBER; } - -// ---------------------------------------------------------------------------- -// Keyword Matcher - - -static Token::Value keywordOrIdentifierToken(string const& _input) -{ - // The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored - // and keywords to be put inside the keywords variable. -#define KEYWORD(name, string, precedence) {string, Token::name}, -#define TOKEN(name, string, precedence) - static const map keywords({TOKEN_LIST(TOKEN, KEYWORD)}); -#undef KEYWORD -#undef TOKEN - auto it = keywords.find(_input); - return it == keywords.end() ? Token::IDENTIFIER : it->second; -} - Token::Value Scanner::scanIdentifierOrKeyword() { solAssert(isIdentifierStart(m_char), ""); @@ -727,7 +709,7 @@ Token::Value Scanner::scanIdentifierOrKeyword() while (isIdentifierPart(m_char)) addLiteralCharAndAdvance(); literal.complete(); - return keywordOrIdentifierToken(m_nextToken.literal); + return Token::fromIdentifierOrKeyword(m_nextToken.literal); } char CharStream::advanceAndGet(size_t _chars) diff --git a/Token.cpp b/Token.cpp index 093bd9c1d..7dc56c327 100644 --- a/Token.cpp +++ b/Token.cpp @@ -40,8 +40,11 @@ // You should have received a copy of the GNU General Public License // along with cpp-ethereum. If not, see . +#include #include +using namespace std; + namespace dev { namespace solidity @@ -77,6 +80,20 @@ char const Token::m_tokenType[] = { TOKEN_LIST(KT, KK) }; + +Token::Value Token::fromIdentifierOrKeyword(const std::string& _name) +{ + // The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored + // and keywords to be put inside the keywords variable. +#define KEYWORD(name, string, precedence) {string, Token::name}, +#define TOKEN(name, string, precedence) + static const map keywords({TOKEN_LIST(TOKEN, KEYWORD)}); +#undef KEYWORD +#undef TOKEN + auto it = keywords.find(_name); + return it == keywords.end() ? Token::IDENTIFIER : it->second; +} + #undef KT #undef KK diff --git a/Token.h b/Token.h index f2ffd076a..2d4441d08 100644 --- a/Token.h +++ b/Token.h @@ -386,6 +386,8 @@ public: return m_precedence[tok]; } + static Token::Value fromIdentifierOrKeyword(std::string const& _name); + private: static char const* const m_name[NUM_TOKENS]; static char const* const m_string[NUM_TOKENS]; diff --git a/Types.cpp b/Types.cpp index eda022ccb..b3eae2025 100644 --- a/Types.cpp +++ b/Types.cpp @@ -35,7 +35,7 @@ namespace solidity shared_ptr Type::fromElementaryTypeName(Token::Value _typeToken) { - solAssert(Token::isElementaryTypeName(_typeToken), ""); + solAssert(Token::isElementaryTypeName(_typeToken), "Elementary type name expected."); if (Token::INT <= _typeToken && _typeToken <= Token::HASH256) { @@ -204,18 +204,12 @@ TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointe } const MemberList IntegerType::AddressMemberList = - MemberList({{"balance", - make_shared(256)}, - {"callstring32", - make_shared(TypePointers({make_shared(32)}), - TypePointers(), FunctionType::Location::BARE)}, - {"callstring32string32", - make_shared(TypePointers({make_shared(32), - make_shared(32)}), - TypePointers(), FunctionType::Location::BARE)}, - {"send", - make_shared(TypePointers({make_shared(256)}), - TypePointers(), FunctionType::Location::SEND)}}); + MemberList({{"balance", make_shared(256)}, + {"callstring32", make_shared(vector{"string32"}, + vector{}, FunctionType::Location::BARE)}, + {"callstring32string32", make_shared(vector{"string32", "string32"}, + vector{}, FunctionType::Location::BARE)}, + {"send", make_shared(vector{"uint"}, vector{}, FunctionType::Location::SEND)}}); shared_ptr IntegerConstantType::fromLiteral(string const& _literal) { @@ -625,6 +619,15 @@ string FunctionType::getCanonicalSignature() const return ret + ")"; } +TypePointers FunctionType::parseElementaryTypeVector(vector const& _types) +{ + TypePointers pointers; + pointers.reserve(_types.size()); + for (string const& type: _types) + pointers.push_back(Type::fromElementaryTypeName(Token::fromIdentifierOrKeyword(type))); + return pointers; +} + bool MappingType::operator==(Type const& _other) const { if (_other.getCategory() != getCategory()) diff --git a/Types.h b/Types.h index 335c58a31..031d45ea5 100644 --- a/Types.h +++ b/Types.h @@ -352,6 +352,10 @@ public: virtual Category getCategory() const override { return Category::FUNCTION; } explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true); + FunctionType(std::vector const& _parameterTypes, + std::vector const& _returnParameterTypes, + Location _location = Location::INTERNAL): + FunctionType(parseElementaryTypeVector(_parameterTypes), parseElementaryTypeVector(_returnParameterTypes), _location) {} FunctionType(TypePointers const& _parameterTypes, TypePointers const& _returnParameterTypes, Location _location = Location::INTERNAL): m_parameterTypes(_parameterTypes), m_returnParameterTypes(_returnParameterTypes), @@ -371,6 +375,8 @@ public: std::string getCanonicalSignature() const; private: + static TypePointers parseElementaryTypeVector(std::vector const& _types); + TypePointers m_parameterTypes; TypePointers m_returnParameterTypes; Location m_location;