-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathConstantEvaluator.h
91 lines (72 loc) · 2.88 KB
/
ConstantEvaluator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* @author Christian <[email protected]>
* @date 2015
* Evaluator for types of constant expressions.
*/
#pragma once
#include <libsolidity/ast/ASTVisitor.h>
#include <utility>
namespace solidity::langutil
{
class ErrorReporter;
}
namespace solidity::frontend
{
class TypeChecker;
/**
* Small drop-in replacement for TypeChecker to evaluate simple expressions of integer constants.
*
* Note: This always use "checked arithmetic" in the sense that any over- or underflow
* results in "unknown" value.
*/
class ConstantEvaluator: private ASTConstVisitor
{
public:
struct TypedRational
{
Type const* type;
rational value;
};
static std::optional<TypedRational> evaluate(
langutil::ErrorReporter& _errorReporter,
Expression const& _expr
);
/// Works the same as `evaluate` but swallows any errors that might occur in the evaluation and simply returns
/// `std::nullopt` instead.
static std::optional<TypedRational> tryEvaluate(Expression const& _expr);
/// Performs arbitrary-precision evaluation of a binary operator. Returns nullopt on cases like
/// division by zero or e.g. bit operators applied to fractional values.
static std::optional<rational> evaluateBinaryOperator(Token _operator, rational const& _left, rational const& _right);
/// Performs arbitrary-precision evaluation of a unary operator. Returns nullopt on cases like
/// bit operators applied to fractional values.
static std::optional<rational> evaluateUnaryOperator(Token _operator, rational const& _input);
private:
explicit ConstantEvaluator(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) {}
std::optional<TypedRational> evaluate(ASTNode const& _node);
void endVisit(BinaryOperation const& _operation) override;
void endVisit(UnaryOperation const& _operation) override;
void endVisit(Literal const& _literal) override;
void endVisit(Identifier const& _identifier) override;
void endVisit(TupleExpression const& _tuple) override;
void endVisit(FunctionCall const& _functionCall) override;
langutil::ErrorReporter& m_errorReporter;
/// Current recursion depth.
size_t m_depth = 0;
/// Values of sub-expressions and variable declarations.
std::map<ASTNode const*, std::optional<TypedRational>> m_values;
};
}