aboutsummaryrefslogtreecommitdiffstats
path: root/AST.cpp
diff options
context:
space:
mode:
authorLu Guanqun <guanqun.lu@gmail.com>2015-01-30 01:26:00 +0800
committerLu Guanqun <guanqun.lu@gmail.com>2015-01-30 01:32:55 +0800
commit5c828dc8b25f223f45d85359dbbb5d9e275167c2 (patch)
tree290d64097f64744341cc147391693ef8c16a9055 /AST.cpp
parent77384af827d7d941620552c4f5f740c3b7c576ef (diff)
downloaddexon-solidity-5c828dc8b25f223f45d85359dbbb5d9e275167c2.tar
dexon-solidity-5c828dc8b25f223f45d85359dbbb5d9e275167c2.tar.gz
dexon-solidity-5c828dc8b25f223f45d85359dbbb5d9e275167c2.tar.bz2
dexon-solidity-5c828dc8b25f223f45d85359dbbb5d9e275167c2.tar.lz
dexon-solidity-5c828dc8b25f223f45d85359dbbb5d9e275167c2.tar.xz
dexon-solidity-5c828dc8b25f223f45d85359dbbb5d9e275167c2.tar.zst
dexon-solidity-5c828dc8b25f223f45d85359dbbb5d9e275167c2.zip
implement named arguments
Diffstat (limited to 'AST.cpp')
-rw-r--r--AST.cpp43
1 files changed, 40 insertions, 3 deletions
diff --git a/AST.cpp b/AST.cpp
index d95a254e..bda366ae 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -430,6 +430,8 @@ void FunctionCall::checkTypeRequirements()
// number of non-mapping members
if (m_arguments.size() != 1)
BOOST_THROW_EXCEPTION(createTypeError("More than one argument for explicit type conversion."));
+ if (!m_names.empty())
+ BOOST_THROW_EXCEPTION(createTypeError("Type conversion can't allow named arguments."));
if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type.getActualType()))
BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
m_type = type.getActualType();
@@ -442,9 +444,44 @@ void FunctionCall::checkTypeRequirements()
TypePointers const& parameterTypes = functionType->getParameterTypes();
if (parameterTypes.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
- for (size_t i = 0; i < m_arguments.size(); ++i)
- if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
- BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
+
+ if (m_names.empty())
+ {
+ for (size_t i = 0; i < m_arguments.size(); ++i)
+ if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
+ BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
+ }
+ else
+ {
+ auto const& parameterNames = functionType->getParameterNames();
+ if (parameterNames.size() != m_names.size())
+ BOOST_THROW_EXCEPTION(createTypeError("Some argument names are missing."));
+
+ // check duplicate names
+ for (size_t i = 0; i < m_names.size(); i++) {
+ for (size_t j = i + 1; j < m_names.size(); j++) {
+ if (m_names[i] == m_names[j])
+ BOOST_THROW_EXCEPTION(createTypeError("Duplicate named argument."));
+ }
+ }
+
+ for (size_t i = 0; i < m_names.size(); i++) {
+ bool found = false;
+ for (size_t j = 0; j < parameterNames.size(); j++) {
+ if (parameterNames[j] == m_names[i]) {
+ // check type convertible
+ if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[j]))
+ BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
+
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ BOOST_THROW_EXCEPTION(createTypeError("Named argument doesn't match function declaration."));
+ }
+ }
+
// @todo actually the return type should be an anonymous struct,
// but we change it to the type of the first return value until we have structs
if (functionType->getReturnParameterTypes().empty())