|
| 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 | +#include "swift/AST/TypeWrappers.h" |
| 22 | + |
| 23 | +using namespace swift; |
| 24 | + |
| 25 | +Optional<TypeWrapperInfo> NominalTypeDecl::getTypeWrapper() const { |
| 26 | + auto *mutableSelf = const_cast<NominalTypeDecl *>(this); |
| 27 | + return evaluateOrDefault(getASTContext().evaluator, |
| 28 | + GetTypeWrapper{mutableSelf}, |
| 29 | + Optional<TypeWrapperInfo>()); |
| 30 | +} |
| 31 | + |
| 32 | +VarDecl *ConstructorDecl::getLocalTypeWrapperStorageVar() const { |
| 33 | + auto &ctx = getASTContext(); |
| 34 | + auto *mutableSelf = const_cast<ConstructorDecl *>(this); |
| 35 | + return evaluateOrDefault( |
| 36 | + ctx.evaluator, SynthesizeLocalVariableForTypeWrapperStorage{mutableSelf}, |
| 37 | + nullptr); |
| 38 | +} |
| 39 | + |
| 40 | +bool VarDecl::isTypeWrapperLocalStorageForInitializer() const { |
| 41 | + if (auto *ctor = |
| 42 | + dyn_cast_or_null<ConstructorDecl>(getDeclContext()->getAsDecl())) { |
| 43 | + return this == ctor->getLocalTypeWrapperStorageVar(); |
| 44 | + } |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +bool UsesTypeWrapperFeature::evaluate(Evaluator &evaluator, |
| 49 | + NominalTypeDecl *decl) const { |
| 50 | + // This is a type wrapper type. |
| 51 | + if (decl->getAttrs().hasAttribute<TypeWrapperAttr>()) |
| 52 | + return true; |
| 53 | + |
| 54 | + // This is a type wrapped type. |
| 55 | + if (decl->hasTypeWrapper()) |
| 56 | + return true; |
| 57 | + |
| 58 | + // This type could be depending on a type wrapper feature |
| 59 | + // indirectly by conforming to a protocol with a type |
| 60 | + // wrapper attribute. To determine that we need to walk |
| 61 | + // protocol dependency chains and check each one. |
| 62 | + |
| 63 | + auto &ctx = decl->getASTContext(); |
| 64 | + |
| 65 | + auto usesTypeWrapperFeature = [&](ProtocolDecl *protocol) { |
| 66 | + return evaluateOrDefault(ctx.evaluator, UsesTypeWrapperFeature{protocol}, |
| 67 | + false); |
| 68 | + }; |
| 69 | + |
| 70 | + for (unsigned i : indices(decl->getInherited())) { |
| 71 | + auto inheritedType = evaluateOrDefault( |
| 72 | + ctx.evaluator, |
| 73 | + InheritedTypeRequest{decl, i, TypeResolutionStage::Interface}, Type()); |
| 74 | + |
| 75 | + if (!(inheritedType && inheritedType->isConstraintType())) |
| 76 | + continue; |
| 77 | + |
| 78 | + if (auto *protocol = |
| 79 | + dyn_cast_or_null<ProtocolDecl>(inheritedType->getAnyNominal())) { |
| 80 | + if (usesTypeWrapperFeature(protocol)) |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + if (auto composition = inheritedType->getAs<ProtocolCompositionType>()) { |
| 85 | + for (auto member : composition->getMembers()) { |
| 86 | + if (auto *protocol = |
| 87 | + dyn_cast_or_null<ProtocolDecl>(member->getAnyNominal())) { |
| 88 | + if (usesTypeWrapperFeature(protocol)) |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return false; |
| 96 | +} |
0 commit comments