-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSolutionResult.h
148 lines (117 loc) · 4.13 KB
/
SolutionResult.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
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
//===--- SolutionResult.h - Constraint System Solution ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the SolutionResult class.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_TYPECHECK_SOLUTION_RESULT_H
#define SWIFT_TYPECHECK_SOLUTION_RESULT_H
#include "llvm/ADT/ArrayRef.h"
namespace swift {
using llvm::ArrayRef;
using llvm::makeArrayRef;
namespace constraints {
class Solution;
/// Describes the result of solving a constraint system, after
/// potentially taking various corrective actions.
class SolutionResult {
public:
enum Kind : unsigned char {
/// The constraint system was successfully solved, and one can
/// retrieve the resulting solution.
Success,
/// The constraint system had multiple solutions, none of which
/// was better than the others.
Ambiguous,
/// The constraint system had no solution, and a diagnostic has
/// already been emitted.
Error,
/// The constraint system had no solution, but no diagnostic has
/// been emitted yet.
UndiagnosedError,
/// The constraint system was too complex to solve, but no
/// diagnostic has been emitted yet.
TooComplex,
};
private:
/// The kind of solution result.
Kind kind;
/// Whether the client has emitted a diagnostic.
unsigned emittedDiagnostic : 1;
/// The number of solutions owned by this result.
unsigned numSolutions = 0;
/// A pointer to the set of solutions, of which there are
/// \c numSolutions entries.
Solution *solutions = nullptr;
/// General constructor for the named constructors.
SolutionResult(Kind kind) : kind(kind) {
emittedDiagnostic = false;
}
public:
SolutionResult(const SolutionResult &other) = delete;
SolutionResult(SolutionResult &&other)
: kind(other.kind), numSolutions(other.numSolutions),
solutions(other.solutions) {
emittedDiagnostic = false;
other.kind = Error;
other.numSolutions = 0;
other.solutions = nullptr;
}
SolutionResult &operator=(const SolutionResult &other) = delete;
SolutionResult &operator=(SolutionResult &&other) = delete;
~SolutionResult();
/// Produce a "solved" result, embedding the given solution.
static SolutionResult forSolved(Solution &&solution);
/// Produce an "ambiguous" result, providing the set of
/// potential solutions.
static SolutionResult forAmbiguous(MutableArrayRef<Solution> solutions);
/// Produce a "too complex" failure, which was not yet been
/// diagnosed.
static SolutionResult forTooComplex() {
return SolutionResult(TooComplex);
}
/// Produce a failure that has already been diagnosed.
static SolutionResult forError() {
return SolutionResult(Error);
}
/// Produce a failure that has not yet been diagnosed.
static SolutionResult forUndiagnosedError() {
return SolutionResult(UndiagnosedError);
}
Kind getKind() const{ return kind; }
/// Retrieve the solution, where there is one.
const Solution &getSolution() const;
/// Retrieve the solution, where there is one.
Solution &&takeSolution() &&;
/// Retrieve the set of solutions when there is an ambiguity.
ArrayRef<Solution> getAmbiguousSolutions() const;
/// Take the set of solutions when there is an ambiguity.
MutableArrayRef<Solution> takeAmbiguousSolutions() &&;
/// Whether this solution requires the client to produce a diagnostic.
bool requiresDiagnostic() const {
switch (kind) {
case Success:
case Ambiguous:
case Error:
return false;
case UndiagnosedError:
case TooComplex:
return true;
}
}
/// Note that the failure has been diagnosed.
void markAsDiagnosed() {
emittedDiagnostic = true;
}
};
} }
#endif /* SWIFT_TYPECHECK_SOLUTION_RESULT_H */