MLIR  14.0.0git
LogicalResult.h
Go to the documentation of this file.
1 //===- LogicalResult.h - Utilities for handling success/failure -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef MLIR_SUPPORT_LOGICAL_RESULT_H
10 #define MLIR_SUPPORT_LOGICAL_RESULT_H
11 
12 #include "mlir/Support/LLVM.h"
13 #include "llvm/ADT/Optional.h"
14 
15 namespace mlir {
16 
17 /// This class represents an efficient way to signal success or failure. It
18 /// should be preferred over the use of `bool` when appropriate, as it avoids
19 /// all of the ambiguity that arises in interpreting a boolean result. This
20 /// class is marked as NODISCARD to ensure that the result is processed. Users
21 /// may explicitly discard a result by using `(void)`, e.g.
22 /// `(void)functionThatReturnsALogicalResult();`. Given the intended nature of
23 /// this class, it generally shouldn't be used as the result of functions that
24 /// very frequently have the result ignored. This class is intended to be used
25 /// in conjunction with the utility functions below.
26 struct LLVM_NODISCARD LogicalResult {
27 public:
28  /// If isSuccess is true a `success` result is generated, otherwise a
29  /// 'failure' result is generated.
30  static LogicalResult success(bool isSuccess = true) {
31  return LogicalResult(isSuccess);
32  }
33 
34  /// If isFailure is true a `failure` result is generated, otherwise a
35  /// 'success' result is generated.
36  static LogicalResult failure(bool isFailure = true) {
37  return success(!isFailure);
38  }
39 
40  /// Returns true if the provided LogicalResult corresponds to a success value.
41  bool succeeded() const { return isSuccess; }
42 
43  /// Returns true if the provided LogicalResult corresponds to a failure value.
44  bool failed() const { return !succeeded(); }
45 
46 private:
47  LogicalResult(bool isSuccess) : isSuccess(isSuccess) {}
48 
49  /// Boolean indicating if this is a success result, if false this is a
50  /// failure result.
51  bool isSuccess;
52 };
53 
54 /// Utility function to generate a LogicalResult. If isSuccess is true a
55 /// `success` result is generated, otherwise a 'failure' result is generated.
56 inline LogicalResult success(bool isSuccess = true) {
57  return LogicalResult::success(isSuccess);
58 }
59 
60 /// Utility function to generate a LogicalResult. If isFailure is true a
61 /// `failure` result is generated, otherwise a 'success' result is generated.
62 inline LogicalResult failure(bool isFailure = true) {
63  return LogicalResult::failure(isFailure);
64 }
65 
66 /// Utility function that returns true if the provided LogicalResult corresponds
67 /// to a success value.
68 inline bool succeeded(LogicalResult result) { return result.succeeded(); }
69 
70 /// Utility function that returns true if the provided LogicalResult corresponds
71 /// to a failure value.
72 inline bool failed(LogicalResult result) { return result.failed(); }
73 
74 /// This class provides support for representing a failure result, or a valid
75 /// value of type `T`. This allows for integrating with LogicalResult, while
76 /// also providing a value on the success path.
77 template <typename T> class LLVM_NODISCARD FailureOr : public Optional<T> {
78 public:
79  /// Allow constructing from a LogicalResult. The result *must* be a failure.
80  /// Success results should use a proper instance of type `T`.
82  assert(failed(result) &&
83  "success should be constructed with an instance of 'T'");
84  }
86  FailureOr(T &&y) : Optional<T>(std::forward<T>(y)) {}
87  FailureOr(const T &y) : Optional<T>(y) {}
88  template <typename U,
90  FailureOr(const FailureOr<U> &other)
91  : Optional<T>(failed(other) ? Optional<T>() : Optional<T>(*other)) {}
92 
93  operator LogicalResult() const { return success(this->hasValue()); }
94 
95 private:
96  /// Hide the bool conversion as it easily creates confusion.
97  using Optional<T>::operator bool;
99 };
100 
101 } // namespace mlir
102 
103 #endif // MLIR_SUPPORT_LOGICAL_RESULT_H
Include the generated interface declarations.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
Definition: LogicalResult.h:72
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value...
Definition: LogicalResult.h:68
static constexpr const bool value
bool failed() const
Returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:44
static LogicalResult success(bool isSuccess=true)
If isSuccess is true a success result is generated, otherwise a &#39;failure&#39; result is generated...
Definition: LogicalResult.h:30
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
This class provides support for representing a failure result, or a valid value of type T...
Definition: LogicalResult.h:77
FailureOr(LogicalResult result)
Allow constructing from a LogicalResult.
Definition: LogicalResult.h:81
FailureOr(const T &y)
Definition: LogicalResult.h:87
bool succeeded() const
Returns true if the provided LogicalResult corresponds to a success value.
Definition: LogicalResult.h:41
FailureOr(const FailureOr< U > &other)
Definition: LogicalResult.h:90
static LogicalResult failure(bool isFailure=true)
If isFailure is true a failure result is generated, otherwise a &#39;success&#39; result is generated...
Definition: LogicalResult.h:36