-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathArithmeticEvaluator.cpp
326 lines (270 loc) · 9.7 KB
/
ArithmeticEvaluator.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//===--- ArithmeticEvaluator.cpp ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Simple arithmetic evaluator using the Evaluator class.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/Evaluator.h"
#include "swift/AST/SimpleRequest.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/SourceManager.h"
#include "gtest/gtest.h"
#include <cmath>
#include <memory>
using namespace swift;
using namespace llvm;
class ArithmeticExpr {
public:
enum class Kind {
Literal,
Binary,
} kind;
// Note: used for internal caching.
std::optional<double> cachedValue;
protected:
ArithmeticExpr(Kind kind) : kind(kind) { }
};
class Literal : public ArithmeticExpr {
public:
const double value;
Literal(double value) : ArithmeticExpr(Kind::Literal), value(value) { }
};
class Binary : public ArithmeticExpr {
public:
enum class OperatorKind {
Sum,
Product,
} operatorKind;
ArithmeticExpr *lhs;
ArithmeticExpr *rhs;
Binary(OperatorKind operatorKind, ArithmeticExpr *lhs, ArithmeticExpr *rhs)
: ArithmeticExpr(Kind::Binary), operatorKind(operatorKind),
lhs(lhs), rhs(rhs) { }
};
void simple_display(llvm::raw_ostream &out, ArithmeticExpr *expr) {
switch (expr->kind) {
case ArithmeticExpr::Kind::Literal:
out << "Literal: " << static_cast<Literal *>(expr)->value;
break;
case ArithmeticExpr::Kind::Binary:
out << "Binary: ";
switch (static_cast<Binary *>(expr)->operatorKind) {
case Binary::OperatorKind::Sum:
out << "sum";
break;
case Binary::OperatorKind::Product:
out << "product";
break;
}
break;
}
}
/// Helper to short-circuit errors to NaN.
template<typename Request>
static double evalOrNaN(Evaluator &evaluator, const Request &request) {
return evaluateOrDefault(evaluator, request, NAN);
}
/// Rule to evaluate the value of the expression.
template<typename Derived, RequestFlags Caching>
struct EvaluationRule
: public SimpleRequest<Derived, double(ArithmeticExpr *), Caching>
{
using SimpleRequest<Derived, double(ArithmeticExpr *), Caching>
::SimpleRequest;
static bool brokeCycle;
double evaluate(Evaluator &evaluator, ArithmeticExpr *expr) const {
switch (expr->kind) {
case ArithmeticExpr::Kind::Literal:
return static_cast<Literal *>(expr)->value;
case ArithmeticExpr::Kind::Binary: {
auto binary = static_cast<Binary *>(expr);
// Evaluate the left- and right-hand sides.
auto lhsValue = evalOrNaN(evaluator, Derived{binary->lhs});
if (std::isnan(lhsValue)) {
brokeCycle = true;
return lhsValue;
}
auto rhsValue = evalOrNaN(evaluator, Derived{binary->rhs});
if (std::isnan(rhsValue)) {
brokeCycle = true;
return rhsValue;
}
switch (binary->operatorKind) {
case Binary::OperatorKind::Sum:
return lhsValue + rhsValue;
case Binary::OperatorKind::Product:
return lhsValue * rhsValue;
}
}
}
}
SourceLoc getNearestLoc() const { return SourceLoc(); }
};
template<typename Derived, RequestFlags Caching>
bool EvaluationRule<Derived, Caching>::brokeCycle = false;
struct InternallyCachedEvaluationRule :
EvaluationRule<InternallyCachedEvaluationRule, RequestFlags::Cached>
{
using EvaluationRule::EvaluationRule;
bool isCached() const {
auto expr = std::get<0>(getStorage());
switch (expr->kind) {
case ArithmeticExpr::Kind::Literal:
return false;
case ArithmeticExpr::Kind::Binary:
return true;
}
}
};
struct UncachedEvaluationRule
: EvaluationRule<UncachedEvaluationRule, RequestFlags::Uncached> {
using EvaluationRule::EvaluationRule;
};
struct ExternallyCachedEvaluationRule
: EvaluationRule<ExternallyCachedEvaluationRule,
RequestFlags::SeparatelyCached> {
using EvaluationRule::EvaluationRule;
bool isCached() const {
auto expr = std::get<0>(getStorage());
switch (expr->kind) {
case ArithmeticExpr::Kind::Literal:
return false;
case ArithmeticExpr::Kind::Binary:
return true;
}
}
std::optional<double> getCachedResult() const {
auto expr = std::get<0>(getStorage());
return expr->cachedValue;
}
void cacheResult(double value) const {
auto expr = std::get<0>(getStorage());
expr->cachedValue = value;
}
};
// Define the arithmetic evaluator's zone.
namespace swift {
#define SWIFT_TYPEID_ZONE ArithmeticEvaluator
#define SWIFT_TYPEID_HEADER "ArithmeticEvaluatorTypeIDZone.def"
#include "swift/Basic/DefineTypeIDZone.h"
#define SWIFT_TYPEID_ZONE ArithmeticEvaluator
#define SWIFT_TYPEID_HEADER "ArithmeticEvaluatorTypeIDZone.def"
#include "swift/Basic/ImplementTypeIDZone.h"
}
/// All of the arithmetic request functions.
static AbstractRequestFunction *arithmeticRequestFunctions[] = {
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \
reinterpret_cast<AbstractRequestFunction *>(&Name::evaluateRequest),
#include "ArithmeticEvaluatorTypeIDZone.def"
#undef SWIFT_REQUEST
};
TEST(ArithmeticEvaluator, Simple) {
// (3.14159 + 2.71828) * 42
ArithmeticExpr *pi = new Literal(3.14159);
ArithmeticExpr *e = new Literal(2.71828);
ArithmeticExpr *sum = new Binary(Binary::OperatorKind::Sum, pi, e);
ArithmeticExpr *lifeUniverseEverything = new Literal(42.0);
ArithmeticExpr *product = new Binary(Binary::OperatorKind::Product, sum,
lifeUniverseEverything);
SourceManager sourceMgr;
DiagnosticEngine diags(sourceMgr);
LangOptions opts;
opts.DebugDumpCycles = false;
Evaluator evaluator(diags, opts);
evaluator.registerRequestFunctions(Zone::ArithmeticEvaluator,
arithmeticRequestFunctions);
const double expectedResult = (3.14159 + 2.71828) * 42.0;
EXPECT_EQ(evalOrNaN(evaluator, InternallyCachedEvaluationRule(product)),
expectedResult);
// Cached response.
EXPECT_EQ(evalOrNaN(evaluator, InternallyCachedEvaluationRule(product)),
expectedResult);
// Uncached
evaluator.clearCache();
EXPECT_EQ(evalOrNaN(evaluator, UncachedEvaluationRule(product)),
expectedResult);
EXPECT_EQ(evalOrNaN(evaluator, UncachedEvaluationRule(product)),
expectedResult);
// External cached query.
evaluator.clearCache();
EXPECT_EQ(evalOrNaN(evaluator, ExternallyCachedEvaluationRule(product)),
expectedResult);
EXPECT_EQ(*sum->cachedValue, 3.14159 + 2.71828);
EXPECT_EQ(*product->cachedValue, expectedResult);
EXPECT_EQ(evalOrNaN(evaluator, ExternallyCachedEvaluationRule(product)),
expectedResult);
EXPECT_EQ(*sum->cachedValue, 3.14159 + 2.71828);
EXPECT_EQ(*product->cachedValue, expectedResult);
// Cleanup
delete pi;
delete e;
delete sum;
delete lifeUniverseEverything;
delete product;
}
TEST(ArithmeticEvaluator, Cycle) {
// (3.14159 + 2.71828) * 42
ArithmeticExpr *pi = new Literal(3.14159);
ArithmeticExpr *e = new Literal(2.71828);
Binary *sum = new Binary(Binary::OperatorKind::Sum, pi, e);
ArithmeticExpr *lifeUniverseEverything = new Literal(42.0);
ArithmeticExpr *product = new Binary(Binary::OperatorKind::Product, sum,
lifeUniverseEverything);
// Introduce a cycle.
sum->rhs = product;
SourceManager sourceMgr;
DiagnosticEngine diags(sourceMgr);
LangOptions opts;
opts.DebugDumpCycles = false;
Evaluator evaluator(diags, opts);
evaluator.registerRequestFunctions(Zone::ArithmeticEvaluator,
arithmeticRequestFunctions);
// Evaluate when there is a cycle.
UncachedEvaluationRule::brokeCycle = false;
EXPECT_TRUE(std::isnan(evalOrNaN(evaluator,
UncachedEvaluationRule(product))));
EXPECT_TRUE(UncachedEvaluationRule::brokeCycle);
// Cycle-breaking result is cached.
EXPECT_TRUE(std::isnan(evalOrNaN(evaluator,
UncachedEvaluationRule(product))));
UncachedEvaluationRule::brokeCycle = false;
EXPECT_FALSE(UncachedEvaluationRule::brokeCycle);
// Evaluate when there is a cycle.
evaluator.clearCache();
InternallyCachedEvaluationRule::brokeCycle = false;
EXPECT_TRUE(std::isnan(evalOrNaN(evaluator,
InternallyCachedEvaluationRule(product))));
EXPECT_TRUE(InternallyCachedEvaluationRule::brokeCycle);
// Cycle-breaking result is cached.
InternallyCachedEvaluationRule::brokeCycle = false;
EXPECT_TRUE(std::isnan(evalOrNaN(evaluator,
InternallyCachedEvaluationRule(product))));
EXPECT_FALSE(InternallyCachedEvaluationRule::brokeCycle);
// Evaluate when there is a cycle.
evaluator.clearCache();
ExternallyCachedEvaluationRule::brokeCycle = false;
EXPECT_TRUE(std::isnan(evalOrNaN(evaluator,
ExternallyCachedEvaluationRule(product))));
EXPECT_TRUE(ExternallyCachedEvaluationRule::brokeCycle);
// Cycle-breaking result is cached.
ExternallyCachedEvaluationRule::brokeCycle = false;
EXPECT_TRUE(std::isnan(evalOrNaN(evaluator,
ExternallyCachedEvaluationRule(product))));
EXPECT_FALSE(ExternallyCachedEvaluationRule::brokeCycle);
// Cleanup
delete pi;
delete e;
delete sum;
delete lifeUniverseEverything;
delete product;
}