blob: 04bac3b6dc8dd6c559f46a098563c05eb589ec49 [file] [log] [blame]
//===--- SolutionResult.h - Constraint System Solution ----------*- C++ -*-===//
// This source file is part of the open source project
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
// See for license information
// See for the list of Swift project authors
// This file defines the SolutionResult class.
#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 {
enum Kind : unsigned char {
/// The constraint system was successfully solved, and one can
/// retrieve the resulting solution.
/// The constraint system had multiple solutions, none of which
/// was better than the others.
/// The constraint system had no solution, and a diagnostic has
/// already been emitted.
/// The constraint system had no solution, but no diagnostic has
/// been emitted yet.
/// The constraint system was too complex to solve, but no
/// diagnostic has been emitted yet.
/// 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;
SolutionResult(const SolutionResult &other) = delete;
SolutionResult(SolutionResult &&other)
: kind(other.kind), numSolutions(other.numSolutions),
solutions( {
emittedDiagnostic = false;
other.kind = Error;
other.numSolutions = 0; = nullptr;
SolutionResult &operator=(const SolutionResult &other) = delete;
SolutionResult &operator=(SolutionResult &&other) = delete;
/// 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;
llvm_unreachable("invalid diagnostic kind");
/// Note that the failure has been diagnosed.
void markAsDiagnosed() {
emittedDiagnostic = true;
} }