[Support] Move ParseResult from OpDefinition.h to LogicalResult.h

This class is a helper for 'parser-like' use cases of LogicalResult
where the implicit conversion to bool is tolerable.  It is used by the
operation asmparsers, but is more generic functionality that is closely
aligned with LogicalResult.  Hoist it up to LogicalResult.h to make it
more accessible.  This is part of Issue #54884

Differential Revision: https://reviews.llvm.org/D123760
This commit is contained in:
Chris Lattner 2022-04-13 22:01:15 -07:00
parent b27430f9f4
commit 81b2dc548b
3 changed files with 26 additions and 15 deletions

View file

@ -265,6 +265,9 @@ public:
/// Allow a diagnostic to be converted to 'failure'.
operator LogicalResult() const;
/// Allow a diagnostic to be converted to 'failure'.
operator ParseResult() const { return ParseResult(LogicalResult(*this)); }
/// Allow a diagnostic to be converted to FailureOr<T>. Always results in
/// 'failure' because this cast cannot possibly return an object of 'T'.
template <typename T>
@ -354,6 +357,10 @@ public:
/// 'success' if this is an empty diagnostic.
operator LogicalResult() const;
/// Allow an inflight diagnostic to be converted to 'failure', otherwise
/// 'success' if this is an empty diagnostic.
operator ParseResult() const { return ParseResult(LogicalResult(*this)); }
/// Allow an inflight diagnostic to be converted to FailureOr<T>. Always
/// results in 'failure' because this cast cannot possibly return an object of
/// 'T'.

View file

@ -29,21 +29,6 @@ namespace mlir {
class Builder;
class OpBuilder;
/// This class represents success/failure for operation parsing. It is
/// essentially a simple wrapper class around LogicalResult that allows for
/// explicit conversion to bool. This allows for the parser to chain together
/// parse rules without the clutter of "failed/succeeded".
class ParseResult : public LogicalResult {
public:
ParseResult(LogicalResult result = success()) : LogicalResult(result) {}
// Allow diagnostics emitted during parsing to be converted to failure.
ParseResult(const InFlightDiagnostic &) : LogicalResult(failure()) {}
ParseResult(const Diagnostic &) : LogicalResult(failure()) {}
/// Failure is true in a boolean context.
explicit operator bool() const { return failed(); }
};
/// This class implements `Optional` functionality for ParseResult. We don't
/// directly use Optional here, because it provides an implicit conversion
/// to 'bool' which we want to avoid. This class is used to implement tri-state

View file

@ -71,6 +71,25 @@ inline bool succeeded(LogicalResult result) { return result.succeeded(); }
/// to a failure value.
inline bool failed(LogicalResult result) { return result.failed(); }
/// This class represents success/failure for parsing-like operations that find
/// it important to chain together failable operations with `||`. This is an
/// extended version of `LogicalResult` that allows for explicit conversion to
/// bool.
///
/// This class should not be used for general error handling cases - we prefer
/// to keep the logic explicit with the `succeeded`/`failed` predicates.
/// However, traditional monadic-style parsering logic can sometimes get
/// swallowed up in boilerplate without this, so we provide this for narrow
/// cases where it is important.
///
class ParseResult : public LogicalResult {
public:
ParseResult(LogicalResult result = success()) : LogicalResult(result) {}
/// Failure is true in a boolean context.
explicit operator bool() const { return failed(); }
};
/// This class provides support for representing a failure result, or a valid
/// value of type `T`. This allows for integrating with LogicalResult, while
/// also providing a value on the success path.