|
| 1 | +//===--- ProtocolAssociations.h - Associated types/conformances -*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 types for representing types and conformances |
| 14 | +// associated with a protocol. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_AST_PROTOCOLASSOCIATIONS_H |
| 19 | +#define SWIFT_AST_PROTOCOLASSOCIATIONS_H |
| 20 | + |
| 21 | +#include "swift/AST/Decl.h" |
| 22 | +#include "llvm/ADT/DenseMapInfo.h" |
| 23 | + |
| 24 | +namespace swift { |
| 25 | + |
| 26 | +/// A type associated with a protocol. |
| 27 | +/// |
| 28 | +/// This struct exists largely so that we can maybe eventually |
| 29 | +/// generalize it to an arbitrary path. |
| 30 | +class AssociatedType { |
| 31 | + AssociatedTypeDecl *Association; |
| 32 | + using AssociationInfo = llvm::DenseMapInfo<AssociatedTypeDecl*>; |
| 33 | + |
| 34 | + struct SpecialValue {}; |
| 35 | + explicit AssociatedType(SpecialValue _, AssociatedTypeDecl *specialValue) |
| 36 | + : Association(specialValue) {} |
| 37 | + |
| 38 | +public: |
| 39 | + explicit AssociatedType(AssociatedTypeDecl *association) |
| 40 | + : Association(association) { |
| 41 | + assert(association); |
| 42 | + } |
| 43 | + |
| 44 | + ProtocolDecl *getSourceProtocol() const { |
| 45 | + return Association->getProtocol(); |
| 46 | + } |
| 47 | + |
| 48 | + AssociatedTypeDecl *getAssociation() const { |
| 49 | + return Association; |
| 50 | + } |
| 51 | + |
| 52 | + friend bool operator==(AssociatedType lhs, AssociatedType rhs) { |
| 53 | + return lhs.Association == rhs.Association; |
| 54 | + } |
| 55 | + friend bool operator!=(AssociatedType lhs, AssociatedType rhs) { |
| 56 | + return !(lhs == rhs); |
| 57 | + } |
| 58 | + |
| 59 | + unsigned getHashValue() const { |
| 60 | + return llvm::hash_value(Association); |
| 61 | + } |
| 62 | + |
| 63 | + static AssociatedType getEmptyKey() { |
| 64 | + return AssociatedType(SpecialValue(), AssociationInfo::getEmptyKey()); |
| 65 | + } |
| 66 | + static AssociatedType getTombstoneKey() { |
| 67 | + return AssociatedType(SpecialValue(), AssociationInfo::getTombstoneKey()); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +/// A conformance associated with a protocol. |
| 72 | +class AssociatedConformance { |
| 73 | + ProtocolDecl *Source; |
| 74 | + CanType Association; |
| 75 | + ProtocolDecl *Requirement; |
| 76 | + |
| 77 | + using SourceInfo = llvm::DenseMapInfo<ProtocolDecl*>; |
| 78 | + |
| 79 | + explicit AssociatedConformance(ProtocolDecl *specialValue) |
| 80 | + : Source(specialValue), Association(CanType()), Requirement(nullptr) {} |
| 81 | + |
| 82 | +public: |
| 83 | + explicit AssociatedConformance(ProtocolDecl *source, CanType association, |
| 84 | + ProtocolDecl *requirement) |
| 85 | + : Source(source), Association(association), Requirement(requirement) { |
| 86 | + assert(source && association && requirement); |
| 87 | + } |
| 88 | + |
| 89 | + ProtocolDecl *getSourceProtocol() const { |
| 90 | + return Source; |
| 91 | + } |
| 92 | + |
| 93 | + CanType getAssociation() const { |
| 94 | + return Association; |
| 95 | + } |
| 96 | + |
| 97 | + ProtocolDecl *getAssociatedRequirement() const { |
| 98 | + return Requirement; |
| 99 | + } |
| 100 | + |
| 101 | + friend bool operator==(const AssociatedConformance &lhs, |
| 102 | + const AssociatedConformance &rhs) { |
| 103 | + return lhs.Source == rhs.Source && |
| 104 | + lhs.Association == rhs.Association && |
| 105 | + lhs.Requirement == rhs.Requirement; |
| 106 | + } |
| 107 | + friend bool operator!=(const AssociatedConformance &lhs, |
| 108 | + const AssociatedConformance &rhs) { |
| 109 | + return !(lhs == rhs); |
| 110 | + } |
| 111 | + |
| 112 | + unsigned getHashValue() const { |
| 113 | + return hash_value(llvm::hash_combine(Source, |
| 114 | + Association.getPointer(), |
| 115 | + Requirement)); |
| 116 | + } |
| 117 | + |
| 118 | + static AssociatedConformance getEmptyKey() { |
| 119 | + return AssociatedConformance(SourceInfo::getEmptyKey()); |
| 120 | + } |
| 121 | + static AssociatedConformance getTombstoneKey() { |
| 122 | + return AssociatedConformance(SourceInfo::getTombstoneKey()); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +} // end namespace swift |
| 127 | + |
| 128 | +namespace llvm { |
| 129 | + template <> struct DenseMapInfo<swift::AssociatedType> { |
| 130 | + static inline swift::AssociatedType getEmptyKey() { |
| 131 | + return swift::AssociatedType::getEmptyKey(); |
| 132 | + } |
| 133 | + |
| 134 | + static inline swift::AssociatedType getTombstoneKey() { |
| 135 | + return swift::AssociatedType::getTombstoneKey(); |
| 136 | + } |
| 137 | + |
| 138 | + static unsigned getHashValue(swift::AssociatedType val) { |
| 139 | + return val.getHashValue(); |
| 140 | + } |
| 141 | + |
| 142 | + static bool isEqual(swift::AssociatedType lhs, swift::AssociatedType rhs) { |
| 143 | + return lhs == rhs; |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + template <> struct DenseMapInfo<swift::AssociatedConformance> { |
| 148 | + static inline swift::AssociatedConformance getEmptyKey() { |
| 149 | + return swift::AssociatedConformance::getEmptyKey(); |
| 150 | + } |
| 151 | + |
| 152 | + static inline swift::AssociatedConformance getTombstoneKey() { |
| 153 | + return swift::AssociatedConformance::getTombstoneKey(); |
| 154 | + } |
| 155 | + |
| 156 | + static unsigned getHashValue(swift::AssociatedConformance val) { |
| 157 | + return val.getHashValue(); |
| 158 | + } |
| 159 | + |
| 160 | + static bool isEqual(swift::AssociatedConformance lhs, |
| 161 | + swift::AssociatedConformance rhs) { |
| 162 | + return lhs == rhs; |
| 163 | + } |
| 164 | + }; |
| 165 | +} |
| 166 | + |
| 167 | +#endif |
0 commit comments