-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathFulfillment.h
172 lines (140 loc) · 6.04 KB
/
Fulfillment.h
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
//===--- Fulfillment.h - Deriving type/conformance metadata -----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines interfaces for deriving type metadata and protocol
// witness tables from various sources.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_FULFILLMENT_H
#define SWIFT_IRGEN_FULFILLMENT_H
#include "llvm/ADT/DenseMap.h"
#include "swift/AST/Types.h"
#include "swift/AST/GenericSignature.h"
#include "MetadataPath.h"
namespace swift {
namespace irgen {
class IRGenModule;
enum IsExact_t : bool;
/// The metadata value can be fulfilled by following the given metadata
/// path from the given source.
struct Fulfillment {
Fulfillment() = default;
Fulfillment(unsigned sourceIndex, MetadataPath &&path)
: SourceIndex(sourceIndex), Path(std::move(path)) {}
/// The source index.
unsigned SourceIndex;
/// The path from the source metadata.
MetadataPath Path;
};
class FulfillmentMap {
using FulfillmentKey = std::pair<Type, ProtocolDecl*>;
llvm::DenseMap<FulfillmentKey, Fulfillment> Fulfillments;
public:
struct InterestingKeysCallback {
/// Is the given type something that we should add fulfillments for?
virtual bool isInterestingType(CanType type) const = 0;
/// Is the given type expressed in terms of types that we should add
/// fulfillments for?
///
/// It's okay to conservatively return true here.
virtual bool hasInterestingType(CanType type) const = 0;
/// Are we only interested in a subset of the conformances for a
/// given type?
virtual bool hasLimitedInterestingConformances(CanType type) const = 0;
/// Return the limited interesting conformances for an interesting type.
virtual GenericSignature::ConformsToArray
getInterestingConformances(CanType type) const = 0;
virtual ~InterestingKeysCallback() = default;
};
/// An implementation of InterestingKeysCallback that returns everything
/// fulfillable.
struct Everything : InterestingKeysCallback {
bool isInterestingType(CanType type) const override;
bool hasInterestingType(CanType type) const override;
bool hasLimitedInterestingConformances(CanType type) const override;
GenericSignature::ConformsToArray
getInterestingConformances(CanType type) const override;
};
FulfillmentMap() = default;
using iterator = decltype(Fulfillments)::iterator;
iterator begin() { return Fulfillments.begin(); }
iterator end() { return Fulfillments.end(); }
/// Is it even theoretically possible that we might find a fulfillment
/// in the given type?
static bool isInterestingTypeForFulfillments(CanType type) {
// Some day, if we ever record fulfillments for concrete types, this
// optimization will probably no longer be useful.
return type->hasTypeParameter();
}
/// Search the given type metadata for useful fulfillments.
///
/// \return true if any fulfillments were added by this search.
bool searchTypeMetadata(IRGenModule &IGM, CanType type, IsExact_t isExact,
unsigned sourceIndex, MetadataPath &&path,
const InterestingKeysCallback &interestingKeys);
/// Search the given witness table for useful fulfillments.
///
/// \return true if any fulfillments were added by this search.
bool searchWitnessTable(IRGenModule &IGM, CanType type, ProtocolDecl *protocol,
unsigned sourceIndex, MetadataPath &&path,
const InterestingKeysCallback &interestingKeys);
/// Register a fulfillment for the given key.
///
/// \return true if the fulfillment was added, which won't happen if there's
/// already a fulfillment that was at least as good
bool addFulfillment(FulfillmentKey key, unsigned source, MetadataPath &&path);
const Fulfillment *getTypeMetadata(CanType type) const {
auto it = Fulfillments.find({type, nullptr});
if (it != Fulfillments.end()) {
return &it->second;
} else {
return nullptr;
}
}
const Fulfillment *getWitnessTable(CanType type, ProtocolDecl *proto) const {
auto it = Fulfillments.find({type, proto});
if (it != Fulfillments.end()) {
return &it->second;
} else {
return nullptr;
}
}
void dump() const;
void print(llvm::raw_ostream &out) const;
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out,
const FulfillmentMap &map) {
map.print(out);
return out;
}
private:
bool searchParentTypeMetadata(IRGenModule &IGM, NominalTypeDecl *typeDecl,
CanType parent,
unsigned source, MetadataPath &&path,
const InterestingKeysCallback &keys);
bool searchNominalTypeMetadata(IRGenModule &IGM, CanNominalType type,
unsigned source, MetadataPath &&path,
const InterestingKeysCallback &keys);
bool searchBoundGenericTypeMetadata(IRGenModule &IGM, CanBoundGenericType type,
unsigned source, MetadataPath &&path,
const InterestingKeysCallback &keys);
/// Search the given witness table for useful fulfillments.
///
/// \return true if any fulfillments were added by this search.
bool searchWitnessTable(IRGenModule &IGM, CanType type, ProtocolDecl *protocol,
unsigned sourceIndex, MetadataPath &&path,
const InterestingKeysCallback &interestingKeys,
const llvm::SmallPtrSetImpl<ProtocolDecl*> *
interestingConformances);
};
}
}
#endif