|
| 1 | +//===--- TypeWrapper.cpp - Type Traversal ---------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 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 implements functionality related to type wrapper feature. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "swift/AST/ASTContext.h" |
| 18 | +#include "swift/AST/Decl.h" |
| 19 | +#include "swift/AST/TypeCheckRequests.h" |
| 20 | +#include "swift/AST/TypeResolutionStage.h" |
| 21 | + |
| 22 | +using namespace swift; |
| 23 | + |
| 24 | +NominalTypeDecl *NominalTypeDecl::getTypeWrapper() const { |
| 25 | + auto *mutableSelf = const_cast<NominalTypeDecl *>(this); |
| 26 | + return evaluateOrDefault(getASTContext().evaluator, |
| 27 | + GetTypeWrapper{mutableSelf}, nullptr); |
| 28 | +} |
| 29 | + |
| 30 | +bool UsesTypeWrapperFeature::evaluate(Evaluator &evaluator, |
| 31 | + NominalTypeDecl *decl) const { |
| 32 | + // This is a type wrapper type. |
| 33 | + if (decl->getAttrs().hasAttribute<TypeWrapperAttr>()) |
| 34 | + return true; |
| 35 | + |
| 36 | + // This is a type wrapped type. |
| 37 | + if (decl->hasTypeWrapper()) |
| 38 | + return true; |
| 39 | + |
| 40 | + // This type could be depending on a type wrapper feature |
| 41 | + // indirectly by conforming to a protocol with a type |
| 42 | + // wrapper attribute. To determine that we need to walk |
| 43 | + // protocol dependency chains and check each one. |
| 44 | + |
| 45 | + auto &ctx = decl->getASTContext(); |
| 46 | + |
| 47 | + auto usesTypeWrapperFeature = [&](ProtocolDecl *protocol) { |
| 48 | + return evaluateOrDefault(ctx.evaluator, UsesTypeWrapperFeature{protocol}, |
| 49 | + false); |
| 50 | + }; |
| 51 | + |
| 52 | + for (unsigned i : indices(decl->getInherited())) { |
| 53 | + auto inheritedType = evaluateOrDefault( |
| 54 | + ctx.evaluator, |
| 55 | + InheritedTypeRequest{decl, i, TypeResolutionStage::Interface}, Type()); |
| 56 | + |
| 57 | + if (!(inheritedType && inheritedType->isConstraintType())) |
| 58 | + continue; |
| 59 | + |
| 60 | + if (auto *protocol = |
| 61 | + dyn_cast_or_null<ProtocolDecl>(inheritedType->getAnyNominal())) { |
| 62 | + if (usesTypeWrapperFeature(protocol)) |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + if (auto composition = inheritedType->getAs<ProtocolCompositionType>()) { |
| 67 | + for (auto member : composition->getMembers()) { |
| 68 | + if (auto *protocol = |
| 69 | + dyn_cast_or_null<ProtocolDecl>(member->getAnyNominal())) { |
| 70 | + if (usesTypeWrapperFeature(protocol)) |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | +} |
0 commit comments