|
| 1 | +//===--- TypeExpansionContext.h - Swift Type Expansion Context --*- 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 | +// This file defines the TypeExpansionContext class. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_TYPEEXPANSIONCONTEXT_H |
| 18 | +#define SWIFT_TYPEEXPANSIONCONTEXT_H |
| 19 | + |
| 20 | +#include "swift/AST/ResilienceExpansion.h" |
| 21 | + |
| 22 | +namespace swift { |
| 23 | + class DeclContext; |
| 24 | + |
| 25 | +/// Describes the context in which SIL types should eventually be expanded. |
| 26 | +/// Required for lowering resilient types and deciding whether to look through |
| 27 | +/// opaque result types to their underlying type. |
| 28 | +class TypeExpansionContext { |
| 29 | + ResilienceExpansion expansion; |
| 30 | + // The context (module, function, ...) we are expanding the type in. |
| 31 | + const DeclContext *inContext; |
| 32 | + // Is the context in which we are expanding in the whole module. |
| 33 | + bool isContextWholeModule; |
| 34 | + |
| 35 | + // The minimal expansion. |
| 36 | + TypeExpansionContext() { |
| 37 | + inContext = nullptr; |
| 38 | + expansion = ResilienceExpansion::Minimal; |
| 39 | + isContextWholeModule = false; |
| 40 | + } |
| 41 | + |
| 42 | +public: |
| 43 | + |
| 44 | + // Infer the expansion for the SIL function. |
| 45 | + TypeExpansionContext(const SILFunction &f); |
| 46 | + |
| 47 | + TypeExpansionContext(ResilienceExpansion expansion, |
| 48 | + const DeclContext *inContext, bool isWholeModuleContext) |
| 49 | + : expansion(expansion), inContext(inContext), |
| 50 | + isContextWholeModule(isWholeModuleContext) {} |
| 51 | + |
| 52 | + ResilienceExpansion getResilienceExpansion() const { return expansion; } |
| 53 | + |
| 54 | + const DeclContext *getContext() const { return inContext; } |
| 55 | + |
| 56 | + bool isWholeModuleContext() const { return isContextWholeModule; } |
| 57 | + |
| 58 | + bool shouldLookThroughOpaqueTypeArchetypes() const { |
| 59 | + return inContext != nullptr; |
| 60 | + } |
| 61 | + |
| 62 | + bool isMinimal() const { return *this == TypeExpansionContext(); } |
| 63 | + |
| 64 | + static TypeExpansionContext minimal() { |
| 65 | + return TypeExpansionContext(); |
| 66 | + } |
| 67 | + |
| 68 | + static TypeExpansionContext maximal(const DeclContext *inContext, |
| 69 | + bool isContextWholeModule) { |
| 70 | + return TypeExpansionContext(ResilienceExpansion::Maximal, inContext, |
| 71 | + isContextWholeModule); |
| 72 | + } |
| 73 | + |
| 74 | + static TypeExpansionContext maximalResilienceExpansionOnly() { |
| 75 | + return maximal(nullptr, false); |
| 76 | + } |
| 77 | + |
| 78 | + static TypeExpansionContext |
| 79 | + noOpaqueTypeArchetypesSubstitution(ResilienceExpansion expansion) { |
| 80 | + return TypeExpansionContext(expansion, nullptr, false); |
| 81 | + } |
| 82 | + |
| 83 | + bool operator==(const TypeExpansionContext &other) const { |
| 84 | + assert(other.inContext != this->inContext || |
| 85 | + other.isContextWholeModule == this->isContextWholeModule); |
| 86 | + return other.inContext == this->inContext && |
| 87 | + other.expansion == this->expansion; |
| 88 | + } |
| 89 | + |
| 90 | + bool operator<(const TypeExpansionContext other) const { |
| 91 | + assert(other.inContext != this->inContext || |
| 92 | + other.isContextWholeModule == this->isContextWholeModule); |
| 93 | + if (this->expansion == other.expansion) |
| 94 | + return this->inContext < other.inContext; |
| 95 | + return this->expansion < other.expansion; |
| 96 | + } |
| 97 | + |
| 98 | + bool operator>(const TypeExpansionContext other) const { |
| 99 | + assert(other.inContext != this->inContext || |
| 100 | + other.isContextWholeModule == this->isContextWholeModule); |
| 101 | + if (this->expansion == other.expansion) |
| 102 | + return this->inContext > other.inContext; |
| 103 | + return this->expansion > other.expansion; |
| 104 | + } |
| 105 | + |
| 106 | + uintptr_t getHashKey() const { |
| 107 | + uintptr_t key = (uintptr_t)inContext | (uintptr_t)expansion; |
| 108 | + return key; |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +} // namespace swift |
| 113 | + |
| 114 | +#endif |
0 commit comments