|
| 1 | +//===--- UnsafeUse.h - A use of an unsafe construct -------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 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_AST_UNSAFEUSE_H |
| 14 | +#define SWIFT_AST_UNSAFEUSE_H |
| 15 | + |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/AST/ProtocolConformance.h" |
| 18 | +#include "swift/AST/Type.h" |
| 19 | +#include "swift/Basic/SourceLoc.h" |
| 20 | +#include "swift/Basic/Unreachable.h" |
| 21 | + |
| 22 | +namespace swift { |
| 23 | + |
| 24 | +class NormalProtocolConformance; |
| 25 | + |
| 26 | +/// Describes a use of an unsafe construct. |
| 27 | +/// |
| 28 | +/// Every use of an unsafe construct that should be diagnosed will be captured |
| 29 | +/// in an instance of this type. |
| 30 | +class UnsafeUse { |
| 31 | +public: |
| 32 | + enum Kind { |
| 33 | + /// An unsafe declaration overriding a safe one. |
| 34 | + Override, |
| 35 | + /// An unsafe declaration witnessing a safe one. |
| 36 | + Witness, |
| 37 | + /// An unsafe type witnesses a safe associated type. |
| 38 | + TypeWitness, |
| 39 | + /// An @unsafe or @unchecked conformance (e.g., Sendable). |
| 40 | + UnsafeConformance, |
| 41 | + /// A reference to an unowned(unsafe) entity. |
| 42 | + UnownedUnsafe, |
| 43 | + /// A reference to a nonisolated(unsafe) entity. |
| 44 | + NonisolatedUnsafe, |
| 45 | + /// A reference to an unsafe declaration. |
| 46 | + ReferenceToUnsafe, |
| 47 | + /// A call to an unsafe declaration. |
| 48 | + CallToUnsafe, |
| 49 | + }; |
| 50 | + |
| 51 | +private: |
| 52 | + Kind kind; |
| 53 | + |
| 54 | + union { |
| 55 | + struct { |
| 56 | + const Decl *decl; |
| 57 | + const Decl *original; |
| 58 | + NormalProtocolConformance *conformance; |
| 59 | + } polymorphic; |
| 60 | + |
| 61 | + struct { |
| 62 | + const AssociatedTypeDecl *assocType; |
| 63 | + TypeBase *type; |
| 64 | + NormalProtocolConformance *conformance; |
| 65 | + const void *location; |
| 66 | + } typeWitness; |
| 67 | + |
| 68 | + struct { |
| 69 | + NormalProtocolConformance *conformance; |
| 70 | + const void *location; |
| 71 | + } conformance; |
| 72 | + |
| 73 | + struct { |
| 74 | + const Decl *decl; |
| 75 | + DeclContext *declContext; |
| 76 | + TypeBase *type; |
| 77 | + const void *location; |
| 78 | + } entity; |
| 79 | + } storage; |
| 80 | + |
| 81 | + UnsafeUse(Kind kind) : kind(kind) { } |
| 82 | + |
| 83 | + static UnsafeUse forPolymorphic( |
| 84 | + Kind kind, |
| 85 | + const Decl *decl, |
| 86 | + const Decl *original, |
| 87 | + NormalProtocolConformance *conformance |
| 88 | + ) { |
| 89 | + assert(kind == Override || kind == Witness); |
| 90 | + |
| 91 | + UnsafeUse result(kind); |
| 92 | + result.storage.polymorphic.decl = decl; |
| 93 | + result.storage.polymorphic.original = original; |
| 94 | + result.storage.polymorphic.conformance = conformance; |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + static UnsafeUse forReference( |
| 99 | + Kind kind, |
| 100 | + DeclContext *dc, |
| 101 | + const Decl *decl, |
| 102 | + Type type, |
| 103 | + SourceLoc location |
| 104 | + ) { |
| 105 | + assert(kind == UnownedUnsafe || |
| 106 | + kind == NonisolatedUnsafe || |
| 107 | + kind == ReferenceToUnsafe || |
| 108 | + kind == CallToUnsafe); |
| 109 | + |
| 110 | + UnsafeUse result(kind); |
| 111 | + result.storage.entity.decl = decl; |
| 112 | + result.storage.entity.declContext = dc; |
| 113 | + result.storage.entity.type = type.getPointer(); |
| 114 | + result.storage.entity.location = location.getOpaquePointerValue(); |
| 115 | + return result; |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +public: |
| 120 | + static UnsafeUse forOverride(const Decl *decl, const Decl *overridden) { |
| 121 | + return forPolymorphic(Override, decl, overridden, nullptr); |
| 122 | + } |
| 123 | + |
| 124 | + static UnsafeUse forWitness(const Decl *witness, const Decl *requirement, |
| 125 | + NormalProtocolConformance *conformance) { |
| 126 | + return forPolymorphic(Witness, witness, requirement, conformance); |
| 127 | + } |
| 128 | + |
| 129 | + static UnsafeUse forTypeWitness(const AssociatedTypeDecl *assocType, |
| 130 | + Type typeWitness, |
| 131 | + NormalProtocolConformance *conformance, |
| 132 | + SourceLoc location) { |
| 133 | + UnsafeUse result(TypeWitness); |
| 134 | + result.storage.typeWitness.assocType = assocType; |
| 135 | + result.storage.typeWitness.type = typeWitness.getPointer(); |
| 136 | + result.storage.typeWitness.conformance = conformance; |
| 137 | + result.storage.typeWitness.location = location.getOpaquePointerValue(); |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + static UnsafeUse forConformance(NormalProtocolConformance *conformance, |
| 142 | + SourceLoc location) { |
| 143 | + UnsafeUse result(UnsafeConformance); |
| 144 | + result.storage.conformance.conformance = conformance; |
| 145 | + result.storage.conformance.location = location.getOpaquePointerValue(); |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + static UnsafeUse forUnownedUnsafe(const Decl *decl, |
| 150 | + SourceLoc location, |
| 151 | + DeclContext *dc) { |
| 152 | + return forReference(UnownedUnsafe, dc, decl, Type(), location); |
| 153 | + } |
| 154 | + |
| 155 | + static UnsafeUse forNonisolatedUnsafe(const Decl *decl, |
| 156 | + SourceLoc location, |
| 157 | + DeclContext *dc) { |
| 158 | + return forReference(NonisolatedUnsafe, dc, decl, Type(), location); |
| 159 | + } |
| 160 | + |
| 161 | + static UnsafeUse forReferenceToUnsafe(const Decl *decl, |
| 162 | + bool isCall, |
| 163 | + DeclContext *dc, |
| 164 | + Type type, |
| 165 | + SourceLoc location) { |
| 166 | + return forReference(isCall ? CallToUnsafe: ReferenceToUnsafe, dc, |
| 167 | + decl, type, location); |
| 168 | + } |
| 169 | + |
| 170 | + Kind getKind() const { return kind; } |
| 171 | + |
| 172 | + /// The location at which this use will be diagnosed. |
| 173 | + SourceLoc getLocation() const { |
| 174 | + switch (getKind()) { |
| 175 | + case Override: |
| 176 | + case Witness: |
| 177 | + return getDecl()->getLoc(); |
| 178 | + |
| 179 | + case UnsafeConformance: |
| 180 | + return getConformance()->getLoc(); |
| 181 | + |
| 182 | + case TypeWitness: |
| 183 | + return SourceLoc( |
| 184 | + llvm::SMLoc::getFromPointer( |
| 185 | + (const char *)storage.typeWitness.location)); |
| 186 | + |
| 187 | + case UnownedUnsafe: |
| 188 | + case NonisolatedUnsafe: |
| 189 | + case ReferenceToUnsafe: |
| 190 | + case CallToUnsafe: |
| 191 | + return SourceLoc( |
| 192 | + llvm::SMLoc::getFromPointer((const char *)storage.entity.location)); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /// Get the main declaration, when there is one. |
| 197 | + const Decl *getDecl() const { |
| 198 | + switch (getKind()) { |
| 199 | + case Override: |
| 200 | + case Witness: |
| 201 | + return storage.polymorphic.decl; |
| 202 | + |
| 203 | + case TypeWitness: |
| 204 | + return storage.typeWitness.assocType; |
| 205 | + |
| 206 | + case UnownedUnsafe: |
| 207 | + case NonisolatedUnsafe: |
| 208 | + case ReferenceToUnsafe: |
| 209 | + case CallToUnsafe: |
| 210 | + return storage.entity.decl; |
| 211 | + |
| 212 | + case UnsafeConformance: |
| 213 | + return nullptr; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + /// Get the associated type declaration for a type witness. |
| 218 | + const AssociatedTypeDecl *getAssociatedType() const { |
| 219 | + assert(getKind() == TypeWitness); |
| 220 | + return storage.typeWitness.assocType; |
| 221 | + } |
| 222 | + |
| 223 | + /// Retrieve the declaration context in which the reference occurs. |
| 224 | + DeclContext *getDeclContext() const { |
| 225 | + switch (getKind()) { |
| 226 | + case UnownedUnsafe: |
| 227 | + case NonisolatedUnsafe: |
| 228 | + case ReferenceToUnsafe: |
| 229 | + case CallToUnsafe: |
| 230 | + return storage.entity.declContext; |
| 231 | + |
| 232 | + case Override: |
| 233 | + case Witness: |
| 234 | + case TypeWitness: |
| 235 | + case UnsafeConformance: |
| 236 | + return nullptr; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + /// Get the original declaration for an issue with a polymorphic |
| 241 | + /// implementation, e.g., an overridden declaration or a protocol |
| 242 | + /// requirement. |
| 243 | + const Decl *getOriginalDecl() const { |
| 244 | + switch (getKind()) { |
| 245 | + case Override: |
| 246 | + case Witness: |
| 247 | + return storage.polymorphic.original; |
| 248 | + |
| 249 | + case TypeWitness: |
| 250 | + case UnownedUnsafe: |
| 251 | + case NonisolatedUnsafe: |
| 252 | + case ReferenceToUnsafe: |
| 253 | + case CallToUnsafe: |
| 254 | + case UnsafeConformance: |
| 255 | + return nullptr; |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + /// Get the type of the reference, if there is one. |
| 260 | + Type getType() const { |
| 261 | + switch (getKind()) { |
| 262 | + case Override: |
| 263 | + case Witness: |
| 264 | + case UnsafeConformance: |
| 265 | + return nullptr; |
| 266 | + |
| 267 | + case TypeWitness: |
| 268 | + return storage.typeWitness.type; |
| 269 | + |
| 270 | + case UnownedUnsafe: |
| 271 | + case NonisolatedUnsafe: |
| 272 | + case ReferenceToUnsafe: |
| 273 | + case CallToUnsafe: |
| 274 | + return storage.entity.type; |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + /// Get the protocol conformance, if there is one. |
| 279 | + NormalProtocolConformance *getConformance() const { |
| 280 | + switch (getKind()) { |
| 281 | + case UnsafeConformance: |
| 282 | + return storage.conformance.conformance; |
| 283 | + |
| 284 | + case Witness: |
| 285 | + return storage.polymorphic.conformance; |
| 286 | + |
| 287 | + case TypeWitness: |
| 288 | + return storage.typeWitness.conformance; |
| 289 | + |
| 290 | + case Override: |
| 291 | + case UnownedUnsafe: |
| 292 | + case NonisolatedUnsafe: |
| 293 | + case ReferenceToUnsafe: |
| 294 | + case CallToUnsafe: |
| 295 | + return nullptr; |
| 296 | + } |
| 297 | + } |
| 298 | +}; |
| 299 | + |
| 300 | +} // end namespace swift |
| 301 | + |
| 302 | +#endif // SWIFT_AST_UNSAFEUSE_H |
0 commit comments