|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 - 2023 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 | +#ifndef SWIFT_IDE_IDEBRIDGING |
| 14 | +#define SWIFT_IDE_IDEBRIDGING |
| 15 | + |
| 16 | +#include "swift/Basic/BasicBridging.h" |
| 17 | + |
| 18 | +#ifdef USED_IN_CPP_SOURCE |
| 19 | +#include "swift/Basic/SourceLoc.h" |
| 20 | +#include "llvm/ADT/Optional.h" |
| 21 | +#include "llvm/CAS/CASReference.h" |
| 22 | +#include <vector> |
| 23 | +#endif |
| 24 | + |
| 25 | +enum class LabelRangeType { |
| 26 | + None, |
| 27 | + |
| 28 | + /// `foo([a: ]2) or .foo([a: ]String)` |
| 29 | + CallArg, |
| 30 | + |
| 31 | + /// `func([a b]: Int)` |
| 32 | + Param, |
| 33 | + |
| 34 | + /// `subscript([a a]: Int)` |
| 35 | + NoncollapsibleParam, |
| 36 | + |
| 37 | + /// `#selector(foo.func([a]:))` |
| 38 | + Selector, |
| 39 | +}; |
| 40 | + |
| 41 | +enum class ResolvedLocContext { Default, Selector, Comment, StringLiteral }; |
| 42 | + |
| 43 | +#ifdef USED_IN_CPP_SOURCE |
| 44 | +struct ResolvedLoc { |
| 45 | + /// The range of the call's base name. |
| 46 | + swift::CharSourceRange range; |
| 47 | + |
| 48 | + // FIXME: (NameMatcher) We should agree on whether `labelRanges` contains the |
| 49 | + // colon or not |
| 50 | + /// The range of the labels. |
| 51 | + /// |
| 52 | + /// What the label range contains depends on the `labelRangeType`: |
| 53 | + /// - Labels of calls span from the label name (excluding trivia) to the end |
| 54 | + /// of the colon's trivia. |
| 55 | + /// - Declaration labels contain the first name and the second name, excluding |
| 56 | + /// the trivia on their sides |
| 57 | + /// - For function arguments that don't have a label, this is an empty range |
| 58 | + /// that points to the start of the argument (exculding trivia). |
| 59 | + std::vector<swift::CharSourceRange> labelRanges; |
| 60 | + |
| 61 | + /// The in index in `labelRanges` that belongs to the first trailing closure |
| 62 | + /// or `llvm::None` if there is no trailing closure. |
| 63 | + llvm::Optional<unsigned> firstTrailingLabel; |
| 64 | + |
| 65 | + LabelRangeType labelType; |
| 66 | + |
| 67 | + /// Whether the location is in an active `#if` region or not. |
| 68 | + bool isActive; |
| 69 | + |
| 70 | + ResolvedLocContext context; |
| 71 | + |
| 72 | + ResolvedLoc(swift::CharSourceRange range, |
| 73 | + std::vector<swift::CharSourceRange> labelRanges, |
| 74 | + llvm::Optional<unsigned> firstTrailingLabel, |
| 75 | + LabelRangeType labelType, bool isActive, |
| 76 | + ResolvedLocContext context); |
| 77 | + |
| 78 | + ResolvedLoc(); |
| 79 | +}; |
| 80 | + |
| 81 | +#endif // USED_IN_CPP_SOURCE |
| 82 | + |
| 83 | +/// An opaque, heap-allocated `ResolvedLoc`. |
| 84 | +/// |
| 85 | +/// This type is manually memory managed. The creator of the object needs to |
| 86 | +/// ensure that `takeUnbridged` is called to free the memory. |
| 87 | +struct BridgedResolvedLoc { |
| 88 | + /// Opaque pointer to `ResolvedLoc`. |
| 89 | + void *resolvedLoc; |
| 90 | + |
| 91 | + /// This consumes `labelRanges` by calling `takeUnbridged` on it. |
| 92 | + SWIFT_NAME( |
| 93 | + "init(range:labelRanges:firstTrailingLabel:labelType:isActive:context:)") |
| 94 | + BridgedResolvedLoc(BridgedCharSourceRange range, |
| 95 | + BridgedCharSourceRangeVector labelRanges, |
| 96 | + unsigned firstTrailingLabel, LabelRangeType labelType, |
| 97 | + bool isActive, ResolvedLocContext context); |
| 98 | + |
| 99 | +#ifdef USED_IN_CPP_SOURCE |
| 100 | + ResolvedLoc takeUnbridged() { |
| 101 | + ResolvedLoc *resolvedLocPtr = static_cast<ResolvedLoc *>(resolvedLoc); |
| 102 | + ResolvedLoc unbridged = *resolvedLocPtr; |
| 103 | + delete resolvedLocPtr; |
| 104 | + return unbridged; |
| 105 | + } |
| 106 | +#endif |
| 107 | +}; |
| 108 | + |
| 109 | +/// A heap-allocated `std::vector<ResoledLoc>` that can be represented by an |
| 110 | +/// opaque pointer value. |
| 111 | +/// |
| 112 | +/// This type is manually memory managed. The creator of the object needs to |
| 113 | +/// ensure that `takeUnbridged` is called to free the memory. |
| 114 | +class BridgedResolvedLocVector { |
| 115 | + /// Opaque pointer to `std::vector<ResolvedLoc>` |
| 116 | + void *vector; |
| 117 | + |
| 118 | +public: |
| 119 | + BridgedResolvedLocVector(); |
| 120 | + |
| 121 | + /// Create a `BridgedResolvedLocVector` from an opaque value obtained from |
| 122 | + /// `getOpaqueValue`. |
| 123 | + BridgedResolvedLocVector(void *opaqueValue); |
| 124 | + |
| 125 | + /// This consumes `Loc`, calling `takeUnbridged` on it. |
| 126 | + SWIFT_NAME("append(_:)") |
| 127 | + void push_back(BridgedResolvedLoc Loc); |
| 128 | + |
| 129 | +#ifdef USED_IN_CPP_SOURCE |
| 130 | + std::vector<ResolvedLoc> takeUnbridged() { |
| 131 | + std::vector<ResolvedLoc> *vectorPtr = |
| 132 | + static_cast<std::vector<ResolvedLoc> *>(vector); |
| 133 | + std::vector<ResolvedLoc> unbridged = *vectorPtr; |
| 134 | + delete vectorPtr; |
| 135 | + return unbridged; |
| 136 | + } |
| 137 | +#endif |
| 138 | + |
| 139 | + SWIFT_IMPORT_UNSAFE |
| 140 | + void *getOpaqueValue() const; |
| 141 | +}; |
| 142 | + |
| 143 | +#ifdef __cplusplus |
| 144 | +extern "C" { |
| 145 | +#endif |
| 146 | + |
| 147 | +/// Entry point to run the NameMatcher written in swift-syntax. |
| 148 | +/// |
| 149 | +/// - Parameters: |
| 150 | +/// - sourceFilePtr: A pointer to an `ExportedSourceFile`, used to access the |
| 151 | +/// syntax tree |
| 152 | +/// - locations: Pointer to a buffer of `BridgedSourceLoc` that should be |
| 153 | +/// resolved by the name matcher. |
| 154 | +/// - locationsCount: Number of elements in `locations`. |
| 155 | +/// - Returns: The opaque value of a `BridgedResolvedLocVector`. |
| 156 | +void *swift_SwiftIDEUtilsBridging_runNameMatcher(const void *sourceFilePtr, |
| 157 | + BridgedSourceLoc *locations, |
| 158 | + size_t locationsCount); |
| 159 | +#ifdef __cplusplus |
| 160 | +} |
| 161 | +#endif |
| 162 | + |
| 163 | +#endif |
0 commit comments