-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmetadata_filter.cpp
226 lines (209 loc) · 7.74 KB
/
metadata_filter.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
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
#include "pch.h"
#include <queue>
#include "types.h"
#include "common.h"
#include "type_helpers.h"
#include "types.h"
#include "metadata_cache.h"
#include "metadata_filter.h"
#include "attributes.h"
#include "helpers.h"
namespace swiftwinrt
{
using processing_queue = std::queue<metadata_type const*>;
inline void add_struct_dependencies_to_queue(processing_queue& to_process, struct_type const& type)
{
for (auto& member : type.members)
{
to_process.emplace(member.type);
}
}
inline void add_class_dependencies_to_queue(processing_queue& to_process, class_type const& type)
{
if (type.base_class)
{
to_process.emplace(type.base_class);
}
for (auto& required : type.required_interfaces)
{
to_process.emplace(required.second.type);
}
}
inline void add_interface_dependencies_to_queue(processing_queue& to_process, interface_type const& type)
{
for (const auto& member : type.functions)
{
if (member.return_type)
{
to_process.emplace(member.return_type.value().type);
}
for (auto&& param : member.params)
{
to_process.emplace(param.type);
}
}
for (const auto& prop : type.properties)
{
to_process.emplace(prop.type);
}
for (const auto& event : type.events)
{
to_process.emplace(event.type);
}
for (auto& [_, required] : type.required_interfaces)
{
to_process.emplace(required.type);
}
}
inline void add_delegate_dependencies_to_queue(processing_queue& to_process, delegate_type const& type)
{
if (type.functions.empty()) return;
auto invoke_func = type.functions[0];
if (invoke_func.return_type)
{
to_process.emplace(invoke_func.return_type.value().type);
}
for (auto&& param : invoke_func.params)
{
to_process.emplace(param.type);
}
}
inline auto add_generic_dependencies_to_queue(processing_queue& to_process, generic_inst const& generic)
{
for (auto& param : generic.generic_params())
{
to_process.emplace(param);
}
for (auto& dependency : generic.dependencies)
{
to_process.emplace(dependency);
}
for (auto& func : generic.functions)
{
if (func.return_type)
{
to_process.emplace(func.return_type.value().type);
}
for (auto&& param : func.params)
{
to_process.emplace(param.type);
}
}
to_process.emplace(generic.generic_type());
}
template<typename T>
void add_ns_types_to_queue(processing_queue& to_process, std::vector<T> const& types)
{
for (auto& type : types)
{
to_process.emplace(&type);
}
}
include_only_used_filter::include_only_used_filter(metadata_cache const& cache, std::vector<std::string> const& includes)
{
processing_queue to_process;
for (auto& include : includes)
{
auto nsIter = cache.namespaces.find(include);
// If this is a namespace, then grab all types and add to queue for processing
if (nsIter != cache.namespaces.end()) {
add_ns_types_to_queue(to_process, nsIter->second.classes);
add_ns_types_to_queue(to_process, nsIter->second.interfaces);
add_ns_types_to_queue(to_process, nsIter->second.delegates);
add_ns_types_to_queue(to_process, nsIter->second.enums);
add_ns_types_to_queue(to_process, nsIter->second.structs);
for (const auto [name, inst] : nsIter->second.generic_instantiations)
{
to_process.push(&inst);
}
}
else {
auto last_ns_index = include.find_last_of('.');
if (last_ns_index == include.npos){
swiftwinrt::throw_invalid("Namespace '", include, "' not found");
}
auto ns = include.substr(0, last_ns_index);
auto name = include.substr(last_ns_index + 1);
auto type = &cache.find(ns, name);
to_process.push(type);
}
}
namespaces.emplace("Windows.Foundation");
while (!to_process.empty())
{
auto processing = to_process.front();
type_name processing_name(processing);
auto [iter, added] = types.emplace(std::piecewise_construct,
std::forward_as_tuple(processing_name.name_space),
std::forward_as_tuple());
auto& full_type_names = iter->second;
to_process.pop();
auto processing_full_name = processing_name.name;
if (processing_full_name.empty()) continue;
if (full_type_names.find(processing_full_name) == full_type_names.end())
{
if (auto s = dynamic_cast<const struct_type*>(processing))
{
add_struct_dependencies_to_queue(to_process, *s);
}
else if (auto c = dynamic_cast<const class_type*>(processing))
{
add_class_dependencies_to_queue(to_process, *c);
for (auto& [name, attributed] : c->factories)
{
if (attributed.type)
{
to_process.emplace(attributed.type);
}
}
}
else if (auto i = dynamic_cast<const interface_type*>(processing))
{
add_interface_dependencies_to_queue(to_process, *i);
}
else if (auto d = dynamic_cast<const delegate_type*>(processing))
{
add_delegate_dependencies_to_queue(to_process, *d);
}
else if (auto g = dynamic_cast<const generic_inst*>(processing))
{
add_generic_dependencies_to_queue(to_process, *g);
generics.emplace(g->swift_full_name());
}
// enums and contracts will be added to processed queue, but they don't have any dependencies
full_type_names.insert(processing_full_name);
auto ns = processing->swift_logical_namespace();
if (!ns.empty())
{
namespaces.emplace(ns);
}
}
}
}
bool include_only_used_filter::includes(winmd::reader::TypeDef const& type) const
{
if (settings.exclude.contains(std::string(type.TypeNamespace())))
{
return false;
}
else if (settings.exclude.contains(get_full_type_name(type)))
{
return false;
}
auto ns = types.find(type.TypeNamespace());
if (ns != types.end())
{
return ns->second.find(type.TypeName()) != ns->second.end();
}
return false;
}
// we want to make sure the cache (which contains all possible types) has **something** that is
// projected - this ensures a simple namespace with just an API contract (i.e. Microsoft.Foundation)
// isn't included. we won't generate a header for this type, and so we can get in a world where we
// try to include a non-existent header
bool include_all_filter::includes_ns(std::string_view const& ns) const
{
auto members = m_cache.namespaces();
return has_projected_types(members[ns]);
}
}