|
| 1 | +//===--- ForeignAsyncConvention.h - Async conventions -----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines the ForeignAsyncConvention structure, which |
| 14 | +// describes the rules for how to detect that a foreign API is asynchronous. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_FOREIGN_ASYNC_CONVENTION_H |
| 19 | +#define SWIFT_FOREIGN_ASYNC_CONVENTION_H |
| 20 | + |
| 21 | +#include "swift/AST/Type.h" |
| 22 | + |
| 23 | +namespace swift { |
| 24 | + |
| 25 | +/// A small structure describing the async convention of a foreign declaration. |
| 26 | +class ForeignAsyncConvention { |
| 27 | + /// The index of the completion handler parameters. |
| 28 | + unsigned CompletionHandlerParamIndex; |
| 29 | + |
| 30 | + /// When non-zero, indicates which parameter to the completion handler is the |
| 31 | + /// Error? parameter (minus one) that makes this async function also throwing. |
| 32 | + unsigned CompletionHandlerErrorParamIndex; |
| 33 | +public: |
| 34 | + ForeignAsyncConvention() |
| 35 | + : CompletionHandlerParamIndex(0), CompletionHandlerErrorParamIndex(0) { } |
| 36 | + |
| 37 | + ForeignAsyncConvention(unsigned completionHandlerParamIndex, |
| 38 | + Optional<unsigned> completionHandlerErrorParamIndex) |
| 39 | + : CompletionHandlerParamIndex(completionHandlerParamIndex), |
| 40 | + CompletionHandlerErrorParamIndex( |
| 41 | + completionHandlerErrorParamIndex |
| 42 | + ? *completionHandlerErrorParamIndex + 1 |
| 43 | + : 0) {} |
| 44 | + |
| 45 | + /// Retrieve the index of the completion handler parameter, which will be |
| 46 | + /// erased from the Swift signature of the imported async function. |
| 47 | + unsigned completionHandlerParamIndex() const { |
| 48 | + return CompletionHandlerParamIndex; |
| 49 | + } |
| 50 | + |
| 51 | + /// Retrieve the index of the \c Error? parameter in the completion handler's |
| 52 | + /// parameter list. When argument passed to this parameter is non-null, the |
| 53 | + /// provided error will be thrown by the async function. |
| 54 | + Optional<unsigned> completionHandlerErrorParamIndex() const { |
| 55 | + if (CompletionHandlerErrorParamIndex == 0) |
| 56 | + return None; |
| 57 | + |
| 58 | + return CompletionHandlerErrorParamIndex - 1; |
| 59 | + } |
| 60 | + |
| 61 | + /// Whether the async function is throwing due to the completion handler |
| 62 | + /// having an \c Error? parameter. |
| 63 | + /// |
| 64 | + /// Equivalent to \c static_cast<bool>(completionHandlerErrorParamIndex()). |
| 65 | + bool isThrowing() const { |
| 66 | + return CompletionHandlerErrorParamIndex != 0; |
| 67 | + } |
| 68 | + |
| 69 | + bool operator==(ForeignAsyncConvention other) const { |
| 70 | + return CompletionHandlerParamIndex == other.CompletionHandlerParamIndex |
| 71 | + && CompletionHandlerErrorParamIndex == |
| 72 | + other.CompletionHandlerErrorParamIndex; |
| 73 | + } |
| 74 | + bool operator!=(ForeignAsyncConvention other) const { |
| 75 | + return !(*this == other); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +#endif |
0 commit comments