Skip to content

Commit

Permalink
Merge pull request #2554 from ethereum/minMaxValue
Browse files Browse the repository at this point in the history
Some helper functions.
  • Loading branch information
axic committed Jul 11, 2017
2 parents 6fa5d47 + 01a1296 commit 0b17ff1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libsolidity/ast/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,23 @@ bool VariableDeclaration::isCallableParameter() const
return false;
}

bool VariableDeclaration::isLocalOrReturn() const
{
return isReturnParameter() || (isLocalVariable() && !isCallableParameter());
}

bool VariableDeclaration::isReturnParameter() const
{
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
if (!callable)
return false;
if (callable->returnParameterList())
for (auto const& variable: callable->returnParameterList()->parameters())
if (variable.get() == this)
return true;
return false;
}

bool VariableDeclaration::isExternalCallableParameter() const
{
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
Expand Down
4 changes: 4 additions & 0 deletions libsolidity/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ class VariableDeclaration: public Declaration
bool isLocalVariable() const { return !!dynamic_cast<CallableDeclaration const*>(scope()); }
/// @returns true if this variable is a parameter or return parameter of a function.
bool isCallableParameter() const;
/// @returns true if this variable is a return parameter of a function.
bool isReturnParameter() const;
/// @returns true if this variable is a local variable or return parameter.
bool isLocalOrReturn() const;
/// @returns true if this variable is a parameter (not return parameter) of an external function.
bool isExternalCallableParameter() const;
/// @returns true if the type of the variable does not need to be specified, i.e. it is declared
Expand Down
16 changes: 16 additions & 0 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,22 @@ u256 IntegerType::literalValue(Literal const* _literal) const
return u256(_literal->value());
}

bigint IntegerType::minValue() const
{
if (isSigned())
return -(bigint(1) << (m_bits - 1));
else
return bigint(0);
}

bigint IntegerType::maxValue() const
{
if (isSigned())
return (bigint(1) << (m_bits - 1)) - 1;
else
return (bigint(1) << m_bits) - 1;
}

TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
{
if (
Expand Down
3 changes: 3 additions & 0 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ class IntegerType: public Type
bool isAddress() const { return m_modifier == Modifier::Address; }
bool isSigned() const { return m_modifier == Modifier::Signed; }

bigint minValue() const;
bigint maxValue() const;

private:
int m_bits;
Modifier m_modifier;
Expand Down

0 comments on commit 0b17ff1

Please sign in to comment.