-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathGenericRequirement.h
150 lines (126 loc) · 5.33 KB
/
GenericRequirement.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
//===--- GenericRequirement.h - Generic requirements ------------*- 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 class describes types for working with requirements of generic
// signatures and the layout of the generic arguments section of
// generic type metadata.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_GENERICREQUIREMENT_H
#define SWIFT_IRGEN_GENERICREQUIREMENT_H
#include "swift/AST/GenericRequirement.h"
#include "swift/AST/Type.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
namespace llvm {
class Value;
}
namespace swift {
class CanGenericSignature;
enum class MetadataState : size_t;
class ModuleDecl;
class NominalTypeDecl;
class ProtocolDecl;
class SubstitutionMap;
namespace irgen {
class Address;
class IRGenFunction;
class IRGenModule;
using RequirementCallback =
llvm::function_ref<void(GenericRequirement requirement)>;
/// Enumerate the generic requirements imposed by a generic signature.
void enumerateGenericSignatureRequirements(CanGenericSignature signature,
const RequirementCallback &callback);
/// Given an array of substitutions that parallel the dependent
/// signature for which a requirement was emitted, emit the required
/// value.
llvm::Value *
emitGenericRequirementFromSubstitutions(IRGenFunction &IGF,
CanGenericSignature signature,
GenericRequirement requirement,
SubstitutionMap subs);
using EmitGenericRequirementFn =
llvm::function_ref<llvm::Value*(GenericRequirement reqt)>;
void emitInitOfGenericRequirementsBuffer(IRGenFunction &IGF,
ArrayRef<GenericRequirement> reqts,
Address buffer,
EmitGenericRequirementFn emitRequirement);
using GetTypeParameterInContextFn =
llvm::function_ref<CanType(CanType type)>;
/// Given a required value, map the requirement into the given
/// context and bind the value.
void bindGenericRequirement(IRGenFunction &IGF,
GenericRequirement requirement,
llvm::Value *requiredValue,
MetadataState metadataState,
GetTypeParameterInContextFn getInContext);
void bindFromGenericRequirementsBuffer(IRGenFunction &IGF,
ArrayRef<GenericRequirement> reqts,
Address buffer,
MetadataState metadataState,
GetTypeParameterInContextFn getInContext);
/// A class describing the layout of the generic requirements of a
/// nominal type metadata.
///
/// The generic requirements are always laid out as a sequence of type
/// metadata (corresponding to the type parameters of the context established
/// by the type, minus anything fulfillable from its parent type metadata)
/// followed by a sequence of protocol witness tables (corresponding to the
/// root conformances of the context established by the type, again minus
/// anything fulfillable from its parent type metadata).
class GenericTypeRequirements {
llvm::SmallVector<GenericRequirement, 4> Requirements;
CanGenericSignature Generics;
public:
GenericTypeRequirements(IRGenModule &IGM, NominalTypeDecl *decl);
GenericTypeRequirements(IRGenModule &IGM, GenericSignature sig);
/// Return the layout chunks.
ArrayRef<GenericRequirement> getRequirements() const {
return Requirements;
}
/// Return the number of entries required in order to store this data.
unsigned getStorageSizeInWords() const {
return Requirements.size();
}
/// Return the number of type metadata requirements.
unsigned getNumTypeRequirements() const {
unsigned count = 0;
for (auto i = Requirements.begin(), e = Requirements.end(); i != e; ++i) {
if (!i->Protocol) {
count++;
} else {
#ifndef NDEBUG
// Assert that the rest of the requirements are conformance
// requirements.
for (++i; i != e; ++i) {
assert(i->Protocol && "type requirement followed conformance!");
}
#endif
break;
}
}
return count;
}
bool empty() const { return Requirements.empty(); }
using FulfillmentCallback = llvm::function_ref<void(
unsigned requirementIndex, CanType type, ProtocolConformanceRef conf)>;
void enumerateFulfillments(IRGenModule &IGM, SubstitutionMap subs,
FulfillmentCallback callback);
void emitInitOfBuffer(IRGenFunction &IGF, SubstitutionMap subs,
Address buffer);
void bindFromBuffer(IRGenFunction &IGF, Address buffer, MetadataState state,
GetTypeParameterInContextFn getInContext);
};
} // end namespace irgen
} // end namespace swift
#endif