1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
//===--- ProtocolInfo.h - Abstract protocol witness layout ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines types for representing the abstract layout of a
// protocol.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_PROTOCOLINFO_H
#define SWIFT_IRGEN_PROTOCOLINFO_H
#include "swift/AST/Decl.h"
#include "swift/AST/ProtocolAssociations.h"
#include "swift/IRGen/ValueWitness.h"
#include "WitnessIndex.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/TrailingObjects.h"
namespace swift {
class CanType;
class ProtocolConformance;
namespace irgen {
class IRGenModule;
class TypeInfo;
/// A witness to a specific element of a protocol. Every
/// ProtocolTypeInfo stores one of these for each requirement
/// introduced by the protocol.
class WitnessTableEntry {
enum WitnessKind {
PlaceholderKind,
OutOfLineBaseKind,
MethodKind,
AssociatedTypeKind,
AssociatedConformanceKind
};
struct OutOfLineBaseWitness {
ProtocolDecl *Protocol;
};
struct MethodWitness {
SILDeclRef Witness;
};
struct AssociatedTypeWitness {
AssociatedTypeDecl *Association;
};
struct AssociatedConformanceWitness {
TypeBase *AssociatedType;
ProtocolDecl *Protocol;
};
WitnessKind Kind;
union {
OutOfLineBaseWitness OutOfLineBaseEntry;
MethodWitness MethodEntry;
AssociatedTypeWitness AssociatedTypeEntry;
AssociatedConformanceWitness AssociatedConformanceEntry;
};
WitnessTableEntry(WitnessKind Kind) : Kind(Kind) {}
public:
static WitnessTableEntry forPlaceholder() {
return WitnessTableEntry(WitnessKind::PlaceholderKind);
}
static WitnessTableEntry forOutOfLineBase(ProtocolDecl *proto) {
assert(proto != nullptr);
WitnessTableEntry entry(WitnessKind::OutOfLineBaseKind);
entry.OutOfLineBaseEntry = {proto};
return entry;
}
/// Is this a base-protocol entry?
bool isBase() const { return Kind == WitnessKind::OutOfLineBaseKind; }
bool matchesBase(ProtocolDecl *proto) const {
assert(proto != nullptr);
return isBase() && OutOfLineBaseEntry.Protocol == proto;
}
/// Given that this is a base-protocol entry, is the table
/// "out of line"?
bool isOutOfLineBase() const {
assert(isBase());
return true;
}
ProtocolDecl *getBase() const {
assert(isBase());
return OutOfLineBaseEntry.Protocol;
}
static WitnessTableEntry forFunction(SILDeclRef declRef) {
assert(!declRef.isNull());
WitnessTableEntry entry(WitnessKind::MethodKind);
entry.MethodEntry = {declRef};
return entry;
}
bool isFunction() const { return Kind == WitnessKind::MethodKind; }
bool matchesFunction(SILDeclRef declRef) const {
return isFunction() && MethodEntry.Witness == declRef;
}
SILDeclRef getFunction() const {
assert(isFunction());
return MethodEntry.Witness;
}
static WitnessTableEntry forAssociatedType(AssociatedType ty) {
WitnessTableEntry entry(WitnessKind::AssociatedTypeKind);
entry.AssociatedTypeEntry = {ty.getAssociation()};
return entry;
}
bool isAssociatedType() const {
return Kind == WitnessKind::AssociatedTypeKind;
}
bool matchesAssociatedType(AssociatedType assocType) const {
return isAssociatedType() &&
AssociatedTypeEntry.Association == assocType.getAssociation();
}
AssociatedTypeDecl *getAssociatedType() const {
assert(isAssociatedType());
return AssociatedTypeEntry.Association;
}
static WitnessTableEntry
forAssociatedConformance(AssociatedConformance conf) {
WitnessTableEntry entry(WitnessKind::AssociatedConformanceKind);
entry.AssociatedConformanceEntry = {conf.getAssociation().getPointer(),
conf.getAssociatedRequirement()};
return entry;
}
bool isAssociatedConformance() const {
return Kind == WitnessKind::AssociatedConformanceKind;
}
bool matchesAssociatedConformance(const AssociatedConformance &conf) const {
return isAssociatedConformance() &&
AssociatedConformanceEntry.AssociatedType ==
conf.getAssociation().getPointer() &&
AssociatedConformanceEntry.Protocol ==
conf.getAssociatedRequirement();
}
CanType getAssociatedConformancePath() const {
assert(isAssociatedConformance());
return CanType(AssociatedConformanceEntry.AssociatedType);
}
ProtocolDecl *getAssociatedConformanceRequirement() const {
assert(isAssociatedConformance());
return AssociatedConformanceEntry.Protocol;
}
friend bool operator==(WitnessTableEntry left, WitnessTableEntry right) {
if (left.Kind != right.Kind)
return false;
switch (left.Kind) {
case WitnessKind::PlaceholderKind:
return true;
case WitnessKind::OutOfLineBaseKind:
return left.OutOfLineBaseEntry.Protocol ==
right.OutOfLineBaseEntry.Protocol;
case WitnessKind::MethodKind:
return left.MethodEntry.Witness == right.MethodEntry.Witness;
case WitnessKind::AssociatedTypeKind:
return left.AssociatedTypeEntry.Association ==
right.AssociatedTypeEntry.Association;
case WitnessKind::AssociatedConformanceKind:
return left.AssociatedConformanceEntry.AssociatedType ==
right.AssociatedConformanceEntry.AssociatedType &&
left.AssociatedConformanceEntry.Protocol ==
right.AssociatedConformanceEntry.Protocol;
}
llvm_unreachable("invalid witness kind");
}
};
/// Describes the information available in a ProtocolInfo.
///
/// Each kind includes the information of the kinds before it.
enum class ProtocolInfoKind : uint8_t {
RequirementSignature,
Full
};
/// An abstract description of a protocol.
class ProtocolInfo final :
private llvm::TrailingObjects<ProtocolInfo, WitnessTableEntry> {
friend TrailingObjects;
friend class TypeConverter;
/// The number of table entries in this protocol layout.
unsigned NumTableEntries;
ProtocolInfoKind Kind;
ProtocolInfoKind getKind() const {
return Kind;
}
ProtocolInfo(ArrayRef<WitnessTableEntry> table, ProtocolInfoKind kind)
: NumTableEntries(table.size()), Kind(kind) {
std::uninitialized_copy(table.begin(), table.end(),
getTrailingObjects<WitnessTableEntry>());
}
static std::unique_ptr<ProtocolInfo> create(ArrayRef<WitnessTableEntry> table,
ProtocolInfoKind kind);
public:
/// The number of witness slots in a conformance to this protocol;
/// in other words, the size of the table in words.
unsigned getNumWitnesses() const {
assert(getKind() == ProtocolInfoKind::Full);
return NumTableEntries;
}
/// Return all of the entries in this protocol witness table.
///
/// The addresses of the entries in this array can be passed to
/// getBaseWitnessIndex/getNonBaseWitnessIndex, below.
ArrayRef<WitnessTableEntry> getWitnessEntries() const {
return {getTrailingObjects<WitnessTableEntry>(), NumTableEntries};
}
/// Given the address of a witness entry from this PI for a base protocol
/// conformance, return its witness index.
WitnessIndex getBaseWitnessIndex(const WitnessTableEntry *witness) const {
assert(witness && witness->isBase());
auto entries = getWitnessEntries();
assert(entries.begin() <= witness && witness < entries.end() &&
"argument witness entry does not belong to this ProtocolInfo");
if (witness->isOutOfLineBase()) {
return WitnessIndex(witness - entries.begin(), false);
} else {
return WitnessIndex(0, true);
}
}
/// Given the address of a witness entry from this PI for a non-base
/// witness, return its witness index.
WitnessIndex getNonBaseWitnessIndex(const WitnessTableEntry *witness) const {
assert(witness && !witness->isBase());
auto entries = getWitnessEntries();
assert(entries.begin() <= witness && witness < entries.end());
return WitnessIndex(witness - entries.begin(), false);
}
/// Return the witness index for the protocol conformance pointer
/// for the given base protocol requirement.
WitnessIndex getBaseIndex(ProtocolDecl *protocol) const {
for (auto &witness : getWitnessEntries()) {
if (witness.matchesBase(protocol))
return getBaseWitnessIndex(&witness);
}
llvm_unreachable("didn't find entry for base");
}
/// Return the witness index for the witness function for the given
/// function requirement.
WitnessIndex getFunctionIndex(SILDeclRef declRef) const {
assert(getKind() >= ProtocolInfoKind::Full);
for (auto &witness : getWitnessEntries()) {
if (witness.matchesFunction(declRef))
return getNonBaseWitnessIndex(&witness);
}
llvm_unreachable("didn't find entry for function");
}
/// Return the witness index for the type metadata access function
/// for the given associated type.
WitnessIndex getAssociatedTypeIndex(IRGenModule &IGM,
AssociatedType assocType) const;
/// Return the witness index for the protocol witness table access
/// function for the given associated protocol conformance.
WitnessIndex
getAssociatedConformanceIndex(const AssociatedConformance &conf) const {
for (auto &witness : getWitnessEntries()) {
if (witness.matchesAssociatedConformance(conf))
return getNonBaseWitnessIndex(&witness);
}
llvm_unreachable("didn't find entry for associated conformance");
}
};
/// Detail about how an object conforms to a protocol.
class ConformanceInfo {
virtual void anchor();
public:
virtual ~ConformanceInfo() = default;
virtual llvm::Value *getTable(IRGenFunction &IGF,
llvm::Value **conformingMetadataCache) const = 0;
/// Try to get this table as a constant pointer. This might just
/// not be supportable at all.
virtual llvm::Constant *tryGetConstantTable(IRGenModule &IGM,
CanType conformingType) const = 0;
};
} // end namespace irgen
} // end namespace swift
#endif
|