-
Notifications
You must be signed in to change notification settings - Fork 564
Expand file tree
/
Copy pathDerivedLinkContext.cs
More file actions
290 lines (251 loc) · 9.57 KB
/
DerivedLinkContext.cs
File metadata and controls
290 lines (251 loc) · 9.57 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Linker;
using Mono.Collections.Generic;
using Registrar;
using Mono.Tuner;
using Xamarin.Bundler;
using Xamarin.Linker;
#if !LEGACY_TOOLS
using LinkContext = Xamarin.Bundler.DotNetLinkContext;
#endif
#nullable enable
namespace Xamarin.Tuner {
public class DerivedLinkContext : LinkContext {
#if !LEGACY_TOOLS
internal StaticRegistrar StaticRegistrar => App.StaticRegistrar;
#endif
internal Application App;
Symbols? required_symbols;
// Any errors or warnings during the link process that won't prevent linking from continuing can be stored here.
// This is typically used to show as many problems as possible per build (so that the user doesn't have to fix one thing, rebuild, fix another, rebuild, fix another, etc).
// Obviously if the problem is such that cascading errors may end up reported, this should not be used.
// The errors/warnings will be shown when the linker is done.
public List<Exception> Exceptions = new List<Exception> ();
// SDK candidates - they will be preserved only if the application (not the SDK) uses it
List<ICustomAttributeProvider> srs_data_contract = new List<ICustomAttributeProvider> ();
List<ICustomAttributeProvider> xml_serialization = new List<ICustomAttributeProvider> ();
HashSet<TypeDefinition>? cached_isnsobject;
// Tristate:
// null = don't know, must check at runtime (can't inline)
// true/false = corresponding constant value
Dictionary<TypeDefinition, bool?>? isdirectbinding_value;
// Store interfaces the linker has linked away so that the static registrar can access them.
public Dictionary<TypeDefinition, List<TypeDefinition>> ProtocolImplementations { get; private set; } = new Dictionary<TypeDefinition, List<TypeDefinition>> ();
// Store types the linker has linked away so that the static registrar can access them.
Dictionary<string, LinkedAwayTypeReference> LinkedAwayTypes = new Dictionary<string, LinkedAwayTypeReference> ();
// The linked away TypeDefinitions lacks some information (it can't even find itself in the LinkedAwayTypes dictionary)
// so we need a second dictionary
Dictionary<TypeDefinition, LinkedAwayTypeReference> LinkedAwayTypeMap = new Dictionary<TypeDefinition, LinkedAwayTypeReference> ();
public bool DidRunApplyPreserveAttributeStep { get; set; }
public DerivedLinkContext (LinkerConfiguration configuration, Application app)
#if !LEGACY_TOOLS
: base (configuration)
#endif
{
this.App = app;
}
AssemblyDefinition? corlib;
public AssemblyDefinition Corlib {
get {
if (corlib is null) {
var name = Driver.CorlibName;
corlib = this.GetAssembly (name);
if (corlib is null)
throw ErrorHelper.CreateError (2111, Errors.MX2111 /* Can not find the corlib assembly '{0}' in the list of loaded assemblies. */, name);
}
return corlib;
}
}
public HashSet<TypeDefinition>? CachedIsNSObject {
get { return cached_isnsobject; }
set { cached_isnsobject = value; }
}
public Dictionary<TypeDefinition, bool?>? IsDirectBindingValue {
get { return isdirectbinding_value; }
set { isdirectbinding_value = value; }
}
public IList<ICustomAttributeProvider> DataContract {
get {
return srs_data_contract;
}
}
public IList<ICustomAttributeProvider> XmlSerialization {
get {
return xml_serialization;
}
}
public Symbols RequiredSymbols {
get {
if (required_symbols is null)
required_symbols = new Symbols ();
return required_symbols;
}
}
public bool RequireMonoNative {
get; set;
}
public Dictionary<IMetadataTokenProvider, object>? GetAllCustomAttributes (string storage_name)
{
#if LEGACY_TOOLS
throw new NotImplementedException ();
#else
return Annotations?.GetCustomAnnotations (storage_name);
#endif
}
public List<ICustomAttribute>? GetCustomAttributes (ICustomAttributeProvider? provider, string storage_name)
{
#if LEGACY_TOOLS
throw new NotImplementedException ();
#else
if (provider is null)
return null;
var annotations = Annotations?.GetCustomAnnotations (storage_name);
if (annotations is null)
return null;
if (annotations.TryGetValue (provider, out var storage))
return (List<ICustomAttribute>) storage;
return null;
#endif
}
// Stores custom attributes in the link context, so that the attribute can be retrieved and
// inspected even if it's linked away.
public void StoreCustomAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, string storage_name)
{
#if LEGACY_TOOLS
throw new NotImplementedException ();
#else
var dict = Annotations.GetCustomAnnotations (storage_name);
List<ICustomAttribute> attribs;
if (!dict.TryGetValue (provider, out var attribObjects)) {
attribs = new List<ICustomAttribute> ();
dict [provider] = attribs;
} else {
attribs = (List<ICustomAttribute>) attribObjects;
}
// Make sure the attribute is resolved, since after removing the attribute
// it won't be able to do it. The 'CustomAttribute.Resolve' method is private, but fetching
// any property will cause it to be called.
// We also need to store the constructor's DeclaringType separately, because it may
// be nulled out from the constructor by the linker if the attribute type itself is linked away.
var dummy = attribute.HasConstructorArguments;
attribs.Add (new AttributeStorage (attribute, attribute.Constructor.DeclaringType));
#endif
}
public List<ICustomAttribute>? GetCustomAttributes (ICustomAttributeProvider? provider, string @namespace, string name)
{
// The equivalent StoreCustomAttribute method below ignores the namespace (it's not needed so far since all attribute names we care about are unique),
// so we need to retrieve the attributes the same way (using the name only).
return GetCustomAttributes (provider, name);
}
public void StoreCustomAttribute (ICustomAttributeProvider provider, CustomAttribute attribute)
{
StoreCustomAttribute (provider, attribute, attribute.AttributeType.Name);
}
public void StoreProtocolMethods (TypeDefinition type)
{
#if LEGACY_TOOLS
throw new NotImplementedException ();
#else
var attribs = Annotations.GetCustomAnnotations ("ProtocolMethods");
if (!attribs.TryGetValue (type, out var value))
attribs [type] = type.Methods.ToArray (); // Make a copy of the collection, since the linker may remove methods from it.
#endif
}
public IList<MethodDefinition>? GetProtocolMethods (TypeDefinition type)
{
#if LEGACY_TOOLS
throw new NotImplementedException ();
#else
var attribs = Annotations.GetCustomAnnotations ("ProtocolMethods");
if (attribs.TryGetValue (type, out var value))
return (MethodDefinition []) value;
return null;
#endif // !LEGACY_TOOLS
}
public void AddLinkedAwayType (TypeDefinition td)
{
var latr = new LinkedAwayTypeReference (td);
LinkedAwayTypes.Add (td.Module.Assembly.Name.Name + ": " + td.FullName, latr);
LinkedAwayTypeMap.Add (td, latr);
}
// Returns a TypeDefinition for a type that was linked away.
// The Module property is null for the returned TypeDefinition (unfortunately TypeDefinition is
// sealed, so I can't provide a custom TypeDefinition subclass), so we need to return
// the module as an out parameter instead.
public TypeDefinition? GetLinkedAwayType (TypeReference tr, out ModuleDefinition? module)
{
module = null;
if (tr is TypeDefinition td) {
// We might get called again for a TypeDefinition we already returned,
// in which case tr.Scope will be null, so we can't use the code below.
// Instead look in our second dictionary, of TypeDefinition -> LinkedAwayTypeReference
// to find the module.
if (LinkedAwayTypeMap.TryGetValue (td, out var latd))
module = latd.Module;
return td;
}
string name;
switch (tr.Scope.MetadataScopeType) {
case MetadataScopeType.ModuleDefinition:
var md = (ModuleDefinition) tr.Scope;
name = md.Assembly.Name.Name;
break;
default:
name = tr.Scope.Name;
break;
}
if (LinkedAwayTypes.TryGetValue (name + ": " + tr.FullName, out var latr)) {
module = latr.Module;
return latr.Resolve ();
}
return null;
}
#if !LEGACY_TOOLS
class AttributeStorage : ICustomAttribute {
public CustomAttribute Attribute { get; }
public TypeReference AttributeType { get; set; }
public AttributeStorage (CustomAttribute attribute, TypeReference attributeType)
{
Attribute = attribute;
AttributeType = attributeType;
}
public bool HasFields => Attribute.HasFields;
public bool HasProperties => Attribute.HasProperties;
public bool HasConstructorArguments => Attribute.HasConstructorArguments;
public Collection<CustomAttributeNamedArgument> Fields => Attribute.Fields;
public Collection<CustomAttributeNamedArgument> Properties => Attribute.Properties;
public Collection<CustomAttributeArgument> ConstructorArguments => Attribute.ConstructorArguments;
}
#endif
class LinkedAwayTypeReference : TypeReference {
// When a type is linked away, its Module and Scope properties
// return null.
// This class keeps those values around.
TypeDefinition type;
ModuleDefinition module;
IMetadataScope scope;
public LinkedAwayTypeReference (TypeDefinition type)
: base (type.Namespace, type.Name)
{
this.type = type;
this.module = type.Module;
this.scope = type.Scope;
}
public override TypeDefinition Resolve ()
{
return type;
}
public override ModuleDefinition Module {
get {
return module;
}
}
public override IMetadataScope Scope {
get { return scope; }
set { scope = value; }
}
}
}
}