Skip to content

Commit 44099ad

Browse files
committed
[Evaluator] Simplify the definition of cycle diagnostics for a SimpleRequest.
The arithmetic evaluator cannot really make use of the new diagnostics machinery, but put it in place for other request kinds.
1 parent 6822b79 commit 44099ad

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

include/swift/Basic/SimpleRequest.h renamed to include/swift/AST/SimpleRequest.h

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
// to define new request kinds.
1515
//
1616
//===----------------------------------------------------------------------===//
17-
#ifndef SWIFT_BASIC_SIMPLEREQUEST_H
18-
#define SWIFT_BASIC_SIMPLEREQUEST_H
17+
#ifndef SWIFT_AST_SIMPLEREQUEST_H
18+
#define SWIFT_AST_SIMPLEREQUEST_H
1919

20+
#include "swift/AST/DiagnosticEngine.h"
21+
#include "swift/AST/DiagnosticsCommon.h"
2022
#include "swift/Basic/SimpleDisplay.h"
2123
#include "swift/Basic/TypeID.h"
2224
#include "llvm/ADT/Hashing.h"
@@ -30,6 +32,41 @@ class Evaluator;
3032
/// CRTP base class that describes a request operation that takes values
3133
/// with the given input types (\c Inputs...) and produces an output of
3234
/// the given type.
35+
///
36+
/// \tparam Derived The final, derived class type for the request.
37+
/// \tparam Output The type of the result produced by evaluating this request.
38+
/// \tparam Inputs The types of the inputs to this request, i.e., the values
39+
/// that comprise the request itself. These will determine the uniqueness of
40+
/// the request.
41+
///
42+
/// The Derived class needs to implement several operations. The most important
43+
/// one takes an evaluator and the input values, then computes the final
44+
/// result:
45+
/// \code
46+
/// Output operator()(Evaluator &evaluator, Inputs...) const;
47+
/// \endcode
48+
///
49+
/// The Derived class will also need to implement an operation to break a
50+
/// cycle if one is found, i.e.,
51+
/// \code
52+
/// OutputType breakCycle() const;
53+
/// \endcode
54+
///
55+
/// Cycle diagnostics can be handled in one of two ways. Either the Derived
56+
/// class can implement the two cycle-diagnosing operations directly:
57+
/// \code
58+
/// void diagnoseCycle(DiagnosticEngine &diags) const;
59+
/// void noteCycleStep(DiagnosticEngine &diags) const;
60+
/// \endcode
61+
///
62+
/// Or the Derived class can provide a "diagnostic location" operation and
63+
/// diagnostic values for the main cycle diagnostic and a "note" describing a
64+
/// step within the chain of diagnostics:
65+
/// \code
66+
/// T getCycleDiagnosticLoc(Inputs...) const;
67+
/// static constexpr Diag<Inputs...> cycleDiagnostic = ...;
68+
/// static constexpr Diag<Inputs...> cycleStepDiagnostic = ...;
69+
/// \endcode
3370
template<typename Derived, typename Output, typename ...Inputs>
3471
class SimpleRequest {
3572
std::tuple<Inputs...> storage;
@@ -49,6 +86,14 @@ class SimpleRequest {
4986
return asDerived()(evaluator, std::get<Indices>(storage)...);
5087
}
5188

89+
template<size_t ...Indices>
90+
void diagnoseImpl(DiagnosticEngine &diags, Diag<Inputs...> diag,
91+
llvm::index_sequence<Indices...>) const {
92+
diags.diagnose(
93+
asDerived().getCycleDiagnosticLoc(std::get<Indices>(storage)...),
94+
diag, std::get<Indices>(storage)...);
95+
}
96+
5297
protected:
5398
/// Retrieve the storage value directly.
5499
const std::tuple<Inputs...> &getStorage() const { return storage; }
@@ -62,7 +107,17 @@ class SimpleRequest {
62107
OutputType operator()(Evaluator &evaluator) const {
63108
return callDerived(evaluator, llvm::index_sequence_for<Inputs...>());
64109
}
65-
110+
111+
void diagnoseCycle(DiagnosticEngine &diags) const {
112+
diagnoseImpl(diags, Derived::cycleDiagnostic,
113+
llvm::index_sequence_for<Inputs...>());
114+
}
115+
116+
void noteCycleStep(DiagnosticEngine &diags) const {
117+
diagnoseImpl(diags, Derived::cycleStepDiagnostic,
118+
llvm::index_sequence_for<Inputs...>());
119+
}
120+
66121
friend bool operator==(const SimpleRequest &lhs, const SimpleRequest &rhs) {
67122
return lhs.storage == rhs.storage;
68123
}

unittests/AST/ArithmeticEvaluator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616
#include "swift/AST/DiagnosticEngine.h"
1717
#include "swift/AST/Evaluator.h"
18-
#include "swift/Basic/SimpleRequest.h"
18+
#include "swift/AST/SimpleRequest.h"
1919
#include "swift/Basic/SourceManager.h"
2020
#include "gtest/gtest.h"
2121
#include <cmath>

0 commit comments

Comments
 (0)