diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2017-11-22 10:34:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-22 10:34:17 +0800 |
commit | 4e0723ce27b343b9dd7c423542b9261d7c48927b (patch) | |
tree | c9f709e54f729ed76df401b380ae4e245eb793df /libsolidity | |
parent | f22ac8fc9647fbbef172f7c0a1f6799a5a0b38a8 (diff) | |
parent | 455e51a608910d60348adb4b464590d976ce7fd7 (diff) | |
download | dexon-solidity-4e0723ce27b343b9dd7c423542b9261d7c48927b.tar dexon-solidity-4e0723ce27b343b9dd7c423542b9261d7c48927b.tar.gz dexon-solidity-4e0723ce27b343b9dd7c423542b9261d7c48927b.tar.bz2 dexon-solidity-4e0723ce27b343b9dd7c423542b9261d7c48927b.tar.lz dexon-solidity-4e0723ce27b343b9dd7c423542b9261d7c48927b.tar.xz dexon-solidity-4e0723ce27b343b9dd7c423542b9261d7c48927b.tar.zst dexon-solidity-4e0723ce27b343b9dd7c423542b9261d7c48927b.zip |
Merge pull request #3142 from Balajiganapathi/allow_constant_array_length
Allow constant integer variables as array lengths.
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/analysis/ConstantEvaluator.cpp | 22 | ||||
-rw-r--r-- | libsolidity/analysis/ConstantEvaluator.h | 8 |
2 files changed, 28 insertions, 2 deletions
diff --git a/libsolidity/analysis/ConstantEvaluator.cpp b/libsolidity/analysis/ConstantEvaluator.cpp index bc3b7cf1..4d546e68 100644 --- a/libsolidity/analysis/ConstantEvaluator.cpp +++ b/libsolidity/analysis/ConstantEvaluator.cpp @@ -74,3 +74,25 @@ void ConstantEvaluator::endVisit(Literal const& _literal) if (!_literal.annotation().type) m_errorReporter.fatalTypeError(_literal.location(), "Invalid literal value."); } + +void ConstantEvaluator::endVisit(Identifier const& _identifier) +{ + VariableDeclaration const* variableDeclaration = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration); + if (!variableDeclaration) + return; + if (!variableDeclaration->isConstant()) + m_errorReporter.fatalTypeError(_identifier.location(), "Identifier must be declared constant."); + + ASTPointer<Expression> value = variableDeclaration->value(); + if (!value) + m_errorReporter.fatalTypeError(_identifier.location(), "Constant identifier declaration must have a constant value."); + + if (!value->annotation().type) + { + if (m_depth > 32) + m_errorReporter.fatalTypeError(_identifier.location(), "Cyclic constant definition (or maximum recursion depth exhausted)."); + ConstantEvaluator e(*value, m_errorReporter, m_depth + 1); + } + + _identifier.annotation().type = value->annotation().type; +} diff --git a/libsolidity/analysis/ConstantEvaluator.h b/libsolidity/analysis/ConstantEvaluator.h index 90bceb5d..6725d610 100644 --- a/libsolidity/analysis/ConstantEvaluator.h +++ b/libsolidity/analysis/ConstantEvaluator.h @@ -38,8 +38,9 @@ class TypeChecker; class ConstantEvaluator: private ASTConstVisitor { public: - ConstantEvaluator(Expression const& _expr, ErrorReporter& _errorReporter): - m_errorReporter(_errorReporter) + ConstantEvaluator(Expression const& _expr, ErrorReporter& _errorReporter, size_t _newDepth = 0): + m_errorReporter(_errorReporter), + m_depth(_newDepth) { _expr.accept(*this); } @@ -48,8 +49,11 @@ private: virtual void endVisit(BinaryOperation const& _operation); virtual void endVisit(UnaryOperation const& _operation); virtual void endVisit(Literal const& _literal); + virtual void endVisit(Identifier const& _identifier); ErrorReporter& m_errorReporter; + /// Current recursion depth. + size_t m_depth; }; } |