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(),
*function.getParameterTypes().front(), true);
};
appendExternalFunctionCall(FunctionType({}, {}, Location::EXTERNAL), {}, options);
appendExternalFunctionCall(FunctionType(TypePointers{}, TypePointers{}, Location::EXTERNAL), {}, options);
break;
}
case Location::SUICIDE:

View File

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

View File

@ -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<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()
{
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)

View File

@ -40,8 +40,11 @@
// You should have received a copy of the GNU General Public License
// along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
#include <map>
#include <libsolidity/Token.h>
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<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 KK

View File

@ -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];

View File

@ -35,7 +35,7 @@ namespace solidity
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)
{
@ -204,18 +204,12 @@ TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointe
}
const MemberList IntegerType::AddressMemberList =
MemberList({{"balance",
make_shared<IntegerType >(256)},
{"callstring32",
make_shared<FunctionType>(TypePointers({make_shared<StaticStringType>(32)}),
TypePointers(), FunctionType::Location::BARE)},
{"callstring32string32",
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)}});
MemberList({{"balance", make_shared<IntegerType >(256)},
{"callstring32", make_shared<FunctionType>(vector<string>{"string32"},
vector<string>{}, FunctionType::Location::BARE)},
{"callstring32string32", make_shared<FunctionType>(vector<string>{"string32", "string32"},
vector<string>{}, FunctionType::Location::BARE)},
{"send", make_shared<FunctionType>(vector<string>{"uint"}, vector<string>{}, FunctionType::Location::SEND)}});
shared_ptr<IntegerConstantType const> IntegerConstantType::fromLiteral(string const& _literal)
{
@ -625,6 +619,15 @@ string FunctionType::getCanonicalSignature() const
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
{
if (_other.getCategory() != getCategory())

View File

@ -352,6 +352,10 @@ public:
virtual Category getCategory() const override { return Category::FUNCTION; }
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,
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<std::string> const& _types);
TypePointers m_parameterTypes;
TypePointers m_returnParameterTypes;
Location m_location;