-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmetadata_cache.h
177 lines (145 loc) · 6.96 KB
/
metadata_cache.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
171
172
173
174
175
176
177
#pragma once
#include <map>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
#include "winmd_reader.h"
#include "task_group.h"
#include "types.h"
namespace swiftwinrt
{
struct api_contract
{
winmd::reader::TypeDef type;
std::uint32_t current_version;
};
struct category_compare
{
bool operator()(typedef_base const& lhs, typedef_base const& rhs) const
{
using namespace winmd::reader;
auto leftCat = get_category(lhs.type());
auto rightCat = get_category(rhs.type());
if (leftCat == rightCat)
{
return lhs.swift_full_name() < rhs.swift_full_name();
}
auto category_power = [](category cat)
{
switch (cat)
{
case category::enum_type: return 0;
case category::struct_type: return 1;
case category::delegate_type: return 2;
case category::interface_type: return 3;
case category::class_type: return 4;
default: return 100;
}
};
return category_power(leftCat) < category_power(rightCat);
}
};
struct metadata_cache;
struct type_cache
{
metadata_cache const* cache;
// Definitions
std::vector<std::reference_wrapper<enum_type const>> enums;
std::vector<std::reference_wrapper<struct_type const>> structs;
std::vector<std::reference_wrapper<delegate_type const>> delegates;
std::vector<std::reference_wrapper<interface_type const>> interfaces;
std::vector<std::reference_wrapper<class_type const>> classes;
// Dependencies
std::set<std::string_view> dependent_namespaces;
std::map<std::string_view, std::reference_wrapper<generic_inst const>> generic_instantiations;
std::set<std::reference_wrapper<typedef_base const>> external_dependencies;
std::set<std::reference_wrapper<typedef_base const>, category_compare> internal_dependencies;
std::map<std::string_view, metadata_type const*> implementable_event_types;
};
struct namespace_cache
{
// Definitions
std::vector<enum_type> enums;
std::vector<struct_type> structs;
std::vector<delegate_type> delegates;
std::vector<interface_type> interfaces;
std::vector<class_type> classes;
std::vector<api_contract> contracts;
// Dependencies
std::set<std::string_view> dependent_namespaces;
std::map<std::string_view, generic_inst> generic_instantiations;
std::set<std::reference_wrapper<typedef_base const>> type_dependencies;
};
struct metadata_filter;
struct metadata_cache
{
std::map<std::string_view, namespace_cache> namespaces;
metadata_cache(winmd::reader::cache const& c);
type_cache compile_namespaces(std::vector<std::string_view> const& targetNamespaces, metadata_filter const& f);
std::set<std::string_view> get_dependent_namespaces(std::vector<std::string_view> const& targetNamespaces, metadata_filter const& f);
bool has_namespace(std::string_view typeNamespace) const
{
return m_typeTable.find(typeNamespace) != m_typeTable.end();
}
metadata_type const* try_find(std::string_view typeNamespace, std::string_view typeName) const
{
if (typeNamespace == system_namespace)
{
return &system_type::from_name(typeName);
}
auto nsItr = m_typeTable.find(typeNamespace);
if (nsItr != m_typeTable.end())
{
auto nameItr = nsItr->second.find(typeName);
if (nameItr != nsItr->second.end())
{
return &nameItr->second;
}
}
return nullptr;
}
metadata_type const& find(std::string_view typeNamespace, std::string_view typeName) const
{
if (auto ptr = try_find(typeNamespace, typeName))
{
return *ptr;
}
swiftwinrt::throw_invalid("Could not find type '", typeName, "' in namespace '", typeNamespace, "'");
}
private:
void process_namespace_types(
winmd::reader::cache::namespace_members const& members,
namespace_cache& target,
std::map<std::string_view, metadata_type const&>& table);
struct init_state
{
namespace_cache* target;
generic_inst const* parent_generic_inst = nullptr;
typedef_base const* parent_generic_iface_or_delegate = nullptr;
};
void process_namespace_dependencies(namespace_cache& target);
void process_enum_dependencies(init_state& state, enum_type& type);
void process_struct_dependencies(init_state& state, struct_type& type);
void process_delegate_dependencies(init_state& state, delegate_type& type);
void process_interface_dependencies(init_state& state, interface_type& type);
void process_class_dependencies(init_state& state, class_type& type);
using relative_version = std::pair<std::size_t, std::uint32_t>;
using relative_version_map = std::unordered_map<interface_type const*, relative_version>;
void process_fastabi_required_interfaces(init_state& state, interface_type const* currentInterface, relative_version rank, relative_version_map& interfaceMap);
function_def process_function(init_state& state, winmd::reader::MethodDef const& def);
property_def process_property(init_state& state, winmd::reader::Property const& def);
event_def process_event(init_state& state, Event const& def);
metadata_type const& find_dependent_type(init_state& state, winmd::reader::TypeSig const& type);
metadata_type const& find_dependent_type(init_state& state, winmd::reader::coded_index<winmd::reader::TypeDefOrRef> const& type);
metadata_type const& find_dependent_type(init_state& state, winmd::reader::GenericTypeInstSig const& type);
using get_interfaces_t = std::vector<named_interface_info>;
get_interfaces_t get_interfaces(init_state& state, winmd::reader::TypeDef const& type);
void get_interfaces_impl(init_state& state, writer& w, get_interfaces_t& result, bool defaulted, bool overridable, bool base, std::pair<InterfaceImpl, InterfaceImpl>&& children);
interface_info* find(get_interfaces_t& interfaces, std::string_view const& name);
void insert_or_assign(get_interfaces_t& interfaces, std::string_view const& name, interface_info&& info);
std::map<std::string, attributed_type> get_attributed_types(winmd::reader::TypeDef const& type) const;
std::map<std::string_view, std::map<std::string_view, metadata_type const&>> m_typeTable;
void try_insert_buffer_byte_access(winmd::reader::TypeDef const& type, get_interfaces_t& result, bool defaulted);
};
}