More convenient function type construction.

This commit is contained in:
Christian 2015-01-12 12:46:52 +01:00
parent 94cff9684f
commit 307a83e1de
7 changed files with 53 additions and 68 deletions

View File

@ -250,7 +250,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
appendTypeConversion(*arguments.front()->getType(), appendTypeConversion(*arguments.front()->getType(),
*function.getParameterTypes().front(), true); *function.getParameterTypes().front(), true);
}; };
appendExternalFunctionCall(FunctionType({}, {}, Location::EXTERNAL), {}, options); appendExternalFunctionCall(FunctionType(TypePointers{}, TypePointers{}, Location::EXTERNAL), {}, options);
break; break;
} }
case Location::SUICIDE: case Location::SUICIDE:

View File

@ -34,54 +34,29 @@ namespace solidity
{ {
GlobalContext::GlobalContext(): GlobalContext::GlobalContext():
// TODO: make this cleaner.
m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::BLOCK)), m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::BLOCK)),
make_shared<MagicVariableDeclaration>("msg", make_shared<MagicType>(MagicType::Kind::MSG)), make_shared<MagicVariableDeclaration>("msg", make_shared<MagicType>(MagicType::Kind::MSG)),
make_shared<MagicVariableDeclaration>("tx", make_shared<MagicType>(MagicType::Kind::TX)), make_shared<MagicVariableDeclaration>("tx", make_shared<MagicType>(MagicType::Kind::TX)),
make_shared<MagicVariableDeclaration>("suicide", make_shared<MagicVariableDeclaration>("suicide",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(0, make_shared<FunctionType>(vector<string>{"address"}, vector<string>{}, FunctionType::Location::SUICIDE)),
IntegerType::Modifier::ADDRESS)}),
TypePointers(),
FunctionType::Location::SUICIDE)),
make_shared<MagicVariableDeclaration>("sha3", make_shared<MagicVariableDeclaration>("sha3",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash"}, vector<string>{"hash"}, FunctionType::Location::SHA3)),
TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
FunctionType::Location::SHA3)),
make_shared<MagicVariableDeclaration>("log0", make_shared<MagicVariableDeclaration>("log0",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash"},vector<string>{}, FunctionType::Location::LOG0)),
TypePointers(),
FunctionType::Location::LOG0)),
make_shared<MagicVariableDeclaration>("log1", make_shared<MagicVariableDeclaration>("log1",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash", "hash"},vector<string>{}, FunctionType::Location::LOG1)),
TypePointers(),
FunctionType::Location::LOG1)),
make_shared<MagicVariableDeclaration>("log2", make_shared<MagicVariableDeclaration>("log2",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash", "hash", "hash"},vector<string>{}, FunctionType::Location::LOG2)),
TypePointers(),
FunctionType::Location::LOG2)),
make_shared<MagicVariableDeclaration>("log3", make_shared<MagicVariableDeclaration>("log3",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash", "hash", "hash", "hash"},vector<string>{}, FunctionType::Location::LOG3)),
TypePointers(),
FunctionType::Location::LOG3)),
make_shared<MagicVariableDeclaration>("log4", make_shared<MagicVariableDeclaration>("log4",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash", "hash", "hash", "hash", "hash"},vector<string>{}, FunctionType::Location::LOG4)),
TypePointers(),
FunctionType::Location::LOG4)),
make_shared<MagicVariableDeclaration>("sha256", make_shared<MagicVariableDeclaration>("sha256",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash"}, vector<string>{"hash"}, FunctionType::Location::SHA256)),
TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
FunctionType::Location::SHA256)),
make_shared<MagicVariableDeclaration>("ecrecover", make_shared<MagicVariableDeclaration>("ecrecover",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), make_shared<FunctionType>(vector<string>{"hash", "hash8", "hash", "hash"}, vector<string>{"address"}, FunctionType::Location::ECRECOVER)),
std::make_shared<IntegerType>(8, IntegerType::Modifier::HASH),
std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH),
std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers({std::make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)}),
FunctionType::Location::ECRECOVER)),
make_shared<MagicVariableDeclaration>("ripemd160", make_shared<MagicVariableDeclaration>("ripemd160",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}), make_shared<FunctionType>(vector<string>{"hash"}, vector<string>{"hash160"}, FunctionType::Location::RIPEMD160))})
TypePointers({std::make_shared<IntegerType>(160, IntegerType::Modifier::HASH)}),
FunctionType::Location::RIPEMD160))})
{ {
} }

View File

@ -700,24 +700,6 @@ Token::Value Scanner::scanNumber(char _charSeen)
return Token::NUMBER; 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<string, Token::Value> 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() Token::Value Scanner::scanIdentifierOrKeyword()
{ {
solAssert(isIdentifierStart(m_char), ""); solAssert(isIdentifierStart(m_char), "");
@ -727,7 +709,7 @@ Token::Value Scanner::scanIdentifierOrKeyword()
while (isIdentifierPart(m_char)) while (isIdentifierPart(m_char))
addLiteralCharAndAdvance(); addLiteralCharAndAdvance();
literal.complete(); literal.complete();
return keywordOrIdentifierToken(m_nextToken.literal); return Token::fromIdentifierOrKeyword(m_nextToken.literal);
} }
char CharStream::advanceAndGet(size_t _chars) char CharStream::advanceAndGet(size_t _chars)

View File

@ -40,8 +40,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. // along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
#include <map>
#include <libsolidity/Token.h> #include <libsolidity/Token.h>
using namespace std;
namespace dev namespace dev
{ {
namespace solidity namespace solidity
@ -77,6 +80,20 @@ char const Token::m_tokenType[] =
{ {
TOKEN_LIST(KT, KK) 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<string, Token::Value> 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 KT
#undef KK #undef KK

View File

@ -386,6 +386,8 @@ public:
return m_precedence[tok]; return m_precedence[tok];
} }
static Token::Value fromIdentifierOrKeyword(std::string const& _name);
private: private:
static char const* const m_name[NUM_TOKENS]; static char const* const m_name[NUM_TOKENS];
static char const* const m_string[NUM_TOKENS]; static char const* const m_string[NUM_TOKENS];

View File

@ -35,7 +35,7 @@ namespace solidity
shared_ptr<Type const> Type::fromElementaryTypeName(Token::Value _typeToken) shared_ptr<Type const> Type::fromElementaryTypeName(Token::Value _typeToken)
{ {
solAssert(Token::isElementaryTypeName(_typeToken), ""); solAssert(Token::isElementaryTypeName(_typeToken), "Elementary type name expected.");
if (Token::INT <= _typeToken && _typeToken <= Token::HASH256) if (Token::INT <= _typeToken && _typeToken <= Token::HASH256)
{ {
@ -204,18 +204,12 @@ TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointe
} }
const MemberList IntegerType::AddressMemberList = const MemberList IntegerType::AddressMemberList =
MemberList({{"balance", MemberList({{"balance", make_shared<IntegerType >(256)},
make_shared<IntegerType >(256)}, {"callstring32", make_shared<FunctionType>(vector<string>{"string32"},
{"callstring32", vector<string>{}, FunctionType::Location::BARE)},
make_shared<FunctionType>(TypePointers({make_shared<StaticStringType>(32)}), {"callstring32string32", make_shared<FunctionType>(vector<string>{"string32", "string32"},
TypePointers(), FunctionType::Location::BARE)}, vector<string>{}, FunctionType::Location::BARE)},
{"callstring32string32", {"send", make_shared<FunctionType>(vector<string>{"uint"}, vector<string>{}, FunctionType::Location::SEND)}});
make_shared<FunctionType>(TypePointers({make_shared<StaticStringType>(32),
make_shared<StaticStringType>(32)}),
TypePointers(), FunctionType::Location::BARE)},
{"send",
make_shared<FunctionType>(TypePointers({make_shared<IntegerType>(256)}),
TypePointers(), FunctionType::Location::SEND)}});
shared_ptr<IntegerConstantType const> IntegerConstantType::fromLiteral(string const& _literal) shared_ptr<IntegerConstantType const> IntegerConstantType::fromLiteral(string const& _literal)
{ {
@ -625,6 +619,15 @@ string FunctionType::getCanonicalSignature() const
return ret + ")"; return ret + ")";
} }
TypePointers FunctionType::parseElementaryTypeVector(vector<string> 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 bool MappingType::operator==(Type const& _other) const
{ {
if (_other.getCategory() != getCategory()) if (_other.getCategory() != getCategory())

View File

@ -352,6 +352,10 @@ public:
virtual Category getCategory() const override { return Category::FUNCTION; } virtual Category getCategory() const override { return Category::FUNCTION; }
explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true); explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true);
FunctionType(std::vector<std::string> const& _parameterTypes,
std::vector<std::string> const& _returnParameterTypes,
Location _location = Location::INTERNAL):
FunctionType(parseElementaryTypeVector(_parameterTypes), parseElementaryTypeVector(_returnParameterTypes), _location) {}
FunctionType(TypePointers const& _parameterTypes, TypePointers const& _returnParameterTypes, FunctionType(TypePointers const& _parameterTypes, TypePointers const& _returnParameterTypes,
Location _location = Location::INTERNAL): Location _location = Location::INTERNAL):
m_parameterTypes(_parameterTypes), m_returnParameterTypes(_returnParameterTypes), m_parameterTypes(_parameterTypes), m_returnParameterTypes(_returnParameterTypes),
@ -371,6 +375,8 @@ public:
std::string getCanonicalSignature() const; std::string getCanonicalSignature() const;
private: private:
static TypePointers parseElementaryTypeVector(std::vector<std::string> const& _types);
TypePointers m_parameterTypes; TypePointers m_parameterTypes;
TypePointers m_returnParameterTypes; TypePointers m_returnParameterTypes;
Location m_location; Location m_location;