forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSILWitnessTable.cpp
193 lines (164 loc) · 6.89 KB
/
SILWitnessTable.cpp
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
//===--- SILWitnessTable.cpp - Defines the SILWitnessTable class ----------===//
//
// 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 the SILWitnessTable class, which is used to map a protocol
// conformance for a type to its implementing SILFunctions. This information is
// (FIXME will be) used by IRGen to create witness tables for protocol dispatch.
// It can also be used by generic specialization and existential
// devirtualization passes to promote witness_method and protocol_method
// instructions to static function_refs.
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILWitnessTable.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/Module.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/SIL/SILModule.h"
#include "llvm/ADT/SmallString.h"
using namespace swift;
static std::string mangleConstant(NormalProtocolConformance *C) {
Mangle::ASTMangler Mangler;
return Mangler.mangleWitnessTable(C);
}
void SILWitnessTable::addWitnessTable() {
// Make sure we have not seen this witness table yet.
assert(Mod.WitnessTableMap.find(Conformance) ==
Mod.WitnessTableMap.end() && "Attempting to create duplicate "
"witness table.");
Mod.WitnessTableMap[Conformance] = this;
Mod.witnessTables.push_back(this);
}
SILWitnessTable *SILWitnessTable::create(
SILModule &M, SILLinkage Linkage, IsSerialized_t Serialized,
NormalProtocolConformance *Conformance,
ArrayRef<SILWitnessTable::Entry> entries,
ArrayRef<ConditionalConformance> conditionalConformances) {
assert(Conformance && "Cannot create a witness table for a null "
"conformance.");
// Create the mangled name of our witness table...
Identifier Name = M.getASTContext().getIdentifier(mangleConstant(Conformance));
// Allocate the witness table and initialize it.
void *buf = M.allocate(sizeof(SILWitnessTable), alignof(SILWitnessTable));
SILWitnessTable *wt = ::new (buf)
SILWitnessTable(M, Linkage, Serialized, Name.str(), Conformance, entries,
conditionalConformances);
wt->addWitnessTable();
// Return the resulting witness table.
return wt;
}
SILWitnessTable *
SILWitnessTable::create(SILModule &M, SILLinkage Linkage,
NormalProtocolConformance *Conformance) {
assert(Conformance && "Cannot create a witness table for a null "
"conformance.");
// Create the mangled name of our witness table...
Identifier Name = M.getASTContext().getIdentifier(mangleConstant(Conformance));
// Allocate the witness table and initialize it.
void *buf = M.allocate(sizeof(SILWitnessTable), alignof(SILWitnessTable));
SILWitnessTable *wt = ::new (buf) SILWitnessTable(M, Linkage, Name.str(),
Conformance);
wt->addWitnessTable();
// Return the resulting witness table.
return wt;
}
SILWitnessTable::SILWitnessTable(
SILModule &M, SILLinkage Linkage, IsSerialized_t Serialized, StringRef N,
NormalProtocolConformance *Conformance, ArrayRef<Entry> entries,
ArrayRef<ConditionalConformance> conditionalConformances)
: Mod(M), Name(N), Linkage(Linkage), Conformance(Conformance), Entries(),
ConditionalConformances(), IsDeclaration(true), Serialized(false) {
convertToDefinition(entries, conditionalConformances, Serialized);
}
SILWitnessTable::SILWitnessTable(SILModule &M, SILLinkage Linkage, StringRef N,
NormalProtocolConformance *Conformance)
: Mod(M), Name(N), Linkage(Linkage), Conformance(Conformance), Entries(),
ConditionalConformances(), IsDeclaration(true), Serialized(false) {}
SILWitnessTable::~SILWitnessTable() {
if (isDeclaration())
return;
// Drop the reference count of witness functions referenced by this table.
for (auto entry : getEntries()) {
switch (entry.getKind()) {
case Method:
if (entry.getMethodWitness().Witness) {
entry.getMethodWitness().Witness->decrementRefCount();
}
break;
case AssociatedType:
case AssociatedTypeProtocol:
case BaseProtocol:
case Invalid:
break;
}
}
}
void SILWitnessTable::convertToDefinition(
ArrayRef<Entry> entries,
ArrayRef<ConditionalConformance> conditionalConformances,
IsSerialized_t isSerialized) {
assert(isDeclaration() && "Definitions should never call this method.");
IsDeclaration = false;
assert(isSerialized != IsSerializable);
Serialized = (isSerialized == IsSerialized);
Entries = Mod.allocateCopy(entries);
ConditionalConformances = Mod.allocateCopy(conditionalConformances);
// Bump the reference count of witness functions referenced by this table.
for (auto entry : getEntries()) {
switch (entry.getKind()) {
case Method:
if (entry.getMethodWitness().Witness) {
entry.getMethodWitness().Witness->incrementRefCount();
}
break;
case AssociatedType:
case AssociatedTypeProtocol:
case BaseProtocol:
case Invalid:
break;
}
}
}
Identifier SILWitnessTable::getIdentifier() const {
return Mod.getASTContext().getIdentifier(Name);
}
bool SILWitnessTable::conformanceIsSerialized(ProtocolConformance *conformance) {
// Serialize witness tables for conformances synthesized by
// the ClangImporter.
if (isa<ClangModuleUnit>(conformance->getDeclContext()->getModuleScopeContext()))
return true;
auto *nominal = conformance->getType()->getAnyNominal();
// Only serialize witness tables for fixed layout types.
//
// FIXME: This is not the right long term solution. We need an explicit
// mechanism for declaring conformances as 'fragile'.
auto protocolIsPublic =
conformance->getProtocol()->getEffectiveAccess() >= AccessLevel::Public;
auto typeIsPublic = nominal->getEffectiveAccess() >= AccessLevel::Public;
return !nominal->isResilient() && protocolIsPublic && typeIsPublic;
}
bool SILWitnessTable::enumerateWitnessTableConditionalConformances(
const ProtocolConformance *conformance,
llvm::function_ref<bool(unsigned, CanType, ProtocolDecl *)> fn) {
unsigned conformanceIndex = 0;
for (auto req : conformance->getConditionalRequirements()) {
if (req.getKind() != RequirementKind::Conformance)
continue;
auto proto = req.getSecondType()->castTo<ProtocolType>()->getDecl();
if (Lowering::TypeConverter::protocolRequiresWitnessTable(proto)) {
if (fn(conformanceIndex, req.getFirstType()->getCanonicalType(), proto))
return true;
conformanceIndex++;
}
}
return false;
}