Files
swift-mirror/include/swift/Sema/SolutionResult.h
Evan Wilde f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00

156 lines
4.5 KiB
C++

//===--- 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"
#include "llvm/ADT/Optional.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;
/// A source range that was too complex to solve.
llvm::Optional<SourceRange> TooComplexAt = llvm::None;
/// 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(llvm::Optional<SourceRange> affected);
/// 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() &&;
/// Retrieve a range of source that has been determined to be too
/// complex to solve in a reasonable time.
llvm::Optional<SourceRange> getTooComplexAt() const { return TooComplexAt; }
/// 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;
}
llvm_unreachable("invalid diagnostic kind");
}
/// Note that the failure has been diagnosed.
void markAsDiagnosed() {
emittedDiagnostic = true;
}
};
} }
#endif /* SWIFT_TYPECHECK_SOLUTION_RESULT_H */