-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathDerivedLinkContext.cs
265 lines (228 loc) · 8.97 KB
/
DerivedLinkContext.cs
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
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Linker;
using Mono.Collections.Generic;
using Registrar;
using Mono.Tuner;
using Xamarin.Bundler;
#if NET
using LinkContext = Xamarin.Bundler.DotNetLinkContext;
#endif
namespace Xamarin.Tuner
{
public class DerivedLinkContext : LinkContext
{
internal StaticRegistrar StaticRegistrar => Target.StaticRegistrar;
internal Target Target;
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 Application App {
get {
return Target.App;
}
}
AssemblyDefinition corlib;
public AssemblyDefinition Corlib {
get {
if (corlib == null) {
var name = Driver.CorlibName;
corlib = this.GetAssembly (name);
if (corlib == 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 == null)
required_symbols = new Symbols ();
return required_symbols;
}
}
public bool RequireMonoNative {
get; set;
}
#if !NET
public DerivedLinkContext (Pipeline pipeline, AssemblyResolver resolver)
: base (pipeline, resolver)
{
UserAction = AssemblyAction.Link;
}
#endif
public Dictionary<IMetadataTokenProvider, object> GetAllCustomAttributes (string storage_name)
{
return Annotations?.GetCustomAnnotations (storage_name);
}
public List<ICustomAttribute> GetCustomAttributes (ICustomAttributeProvider provider, string storage_name)
{
var annotations = Annotations?.GetCustomAnnotations (storage_name);
object storage = null;
if (annotations?.TryGetValue (provider, out storage) != true)
return null;
return (List<ICustomAttribute>) storage;
}
// 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)
{
var dict = Annotations.GetCustomAnnotations (storage_name);
List<ICustomAttribute> attribs;
object attribObjects;
if (!dict.TryGetValue (provider, out 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, AttributeType = attribute.Constructor.DeclaringType });
}
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)
{
var attribs = Annotations.GetCustomAnnotations ("ProtocolMethods");
object value;
if (!attribs.TryGetValue (type, out value))
attribs [type] = type.Methods.ToArray (); // Make a copy of the collection, since the linker may remove methods from it.
}
public IList<MethodDefinition> GetProtocolMethods (TypeDefinition type)
{
var attribs = Annotations.GetCustomAnnotations ("ProtocolMethods");
object value;
if (attribs.TryGetValue (type, out value))
return (MethodDefinition []) value;
return null;
}
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;
}
class AttributeStorage : ICustomAttribute
{
public CustomAttribute Attribute;
public TypeReference AttributeType { get; set; }
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;
}
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; }
}
}
}
}