/
BindableTypeProvidersGenerationTask.cs
601 lines (493 loc) · 23.4 KB
/
BindableTypeProvidersGenerationTask.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
using Uno.Logging;
using Uno.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Diagnostics;
using Uno.SourceGeneration;
using Microsoft.CodeAnalysis;
using Microsoft.Build.Execution;
using Uno.UI.SourceGenerators.XamlGenerator;
using Microsoft.CodeAnalysis.FindSymbols;
namespace Uno.UI.SourceGenerators.BindableTypeProviders
{
[GenerateAfter("Uno.ImmutableGenerator")]
public class BindableTypeProvidersSourceGenerator : SourceGenerator
{
private const string TypeMetadataConfigFile = "TypeMetadataConfig.xml";
private string _defaultNamespace;
private Dictionary<INamedTypeSymbol, GeneratedTypeInfo> _typeMap = new Dictionary<INamedTypeSymbol, GeneratedTypeInfo>();
private INamedTypeSymbol[] _bindableAttributeSymbol;
private ITypeSymbol _dependencyPropertySymbol;
private INamedTypeSymbol _objectSymbol;
private INamedTypeSymbol _javaObjectSymbol;
private INamedTypeSymbol _nsObjectSymbol;
private INamedTypeSymbol _nonBindableSymbol;
private IModuleSymbol _currentModule;
public string[] AnalyzerSuppressions { get; set; }
public INamedTypeSymbol _stringSymbol { get; private set; }
public override void Execute(SourceGeneratorContext context)
{
try
{
if (PlatformHelper.IsValidPlatform(context))
{
var project = context.GetProjectInstance();
if (IsApplication(project))
{
_defaultNamespace = project.GetPropertyValue("RootNamespace");
_bindableAttributeSymbol = FindBindableAttributes(context);
_dependencyPropertySymbol = context.Compilation.GetTypeByMetadataName(XamlConstants.Types.DependencyProperty);
_objectSymbol = context.Compilation.GetTypeByMetadataName("System.Object");
_javaObjectSymbol = context.Compilation.GetTypeByMetadataName("Java.Lang.Object");
_nsObjectSymbol = context.Compilation.GetTypeByMetadataName("Foundation.NSObject");
_stringSymbol = context.Compilation.GetTypeByMetadataName("System.String");
_nonBindableSymbol = context.Compilation.GetTypeByMetadataName("Windows.UI.Xaml.Data.NonBindableAttribute");
_currentModule = context.Compilation.SourceModule;
AnalyzerSuppressions = new string[0];
var modules = from ext in context.Compilation.ExternalReferences
let sym = context.Compilation.GetAssemblyOrModuleSymbol(ext) as IAssemblySymbol
where sym != null
from module in sym.Modules
select module;
modules = modules.Concat(context.Compilation.SourceModule);
context.AddCompilationUnit("BindableMetadata", GenerateTypeProviders(modules));
}
}
}
catch(Exception e)
{
var message = e.Message + e.StackTrace;
if (e is AggregateException)
{
message = (e as AggregateException).InnerExceptions.Select(ex => ex.Message + e.StackTrace).JoinBy("\r\n");
}
this.Log().Error("Failed to generate type providers.", new Exception("Failed to generate type providers." + message, e));
}
}
private static INamedTypeSymbol[] FindBindableAttributes(SourceGeneratorContext context) =>
SymbolFinder.FindDeclarationsAsync(context.Project, "BindableAttribute", false).Result.OfType<INamedTypeSymbol>().ToArray();
private bool IsApplication(ProjectInstance projectInstance)
{
var isAndroidApp = projectInstance.GetPropertyValue("AndroidApplication")?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false;
var isiOSApp = projectInstance.GetPropertyValue("ProjectTypeGuids")?.Equals("{FEACFBD2-3405-455C-9665-78FE426C6842};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}", StringComparison.OrdinalIgnoreCase) ?? false;
var isExe = projectInstance.GetPropertyValue("OutputType")?.Equals("Exe", StringComparison.OrdinalIgnoreCase) ?? false;
var isWasm = projectInstance.GetPropertyValue("WasmHead")?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false;
return isAndroidApp || (isiOSApp && isExe) || isWasm;
}
private string GenerateTypeProviders(IEnumerable<IModuleSymbol> modules)
{
var q = from module in modules
from type in module.GlobalNamespace.GetNamespaceTypes()
where (
_bindableAttributeSymbol.Any(s => type.FindAttributeFlattened(s) != null)
&& !type.IsGenericType
&& !type.IsAbstract
&& IsValidProvider(type)
)
select type;
q = q.ToArray();
var writer = new IndentedStringBuilder();
writer.AppendLineInvariant("// <auto-generated>");
writer.AppendLineInvariant("// *****************************************************************************");
writer.AppendLineInvariant("// This file has been generated by Uno.UI (BindableTypeProvidersSourceGenerator)");
writer.AppendLineInvariant("// *****************************************************************************");
writer.AppendLineInvariant("// </auto-generated>");
writer.AppendLine();
writer.AppendLineInvariant("#pragma warning disable 618 // Ignore obsolete members warnings");
writer.AppendLineInvariant("using System;");
writer.AppendLineInvariant("using System.Linq;");
writer.AppendLineInvariant("using System.Diagnostics;");
using (writer.BlockInvariant("namespace {0}", _defaultNamespace))
{
GenerateMetadata(writer, q);
GenerateProviderTable(q, writer);
}
return writer.ToString();
}
private bool IsValidProvider(INamedTypeSymbol type) => type.IsLocallyPublic(_currentModule);
private void GenerateProviderTable(IEnumerable<INamedTypeSymbol> q, IndentedStringBuilder writer)
{
writer.AppendLineInvariant("[System.Runtime.CompilerServices.CompilerGeneratedAttribute]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1502:AvoidExcessiveComplexity\", Justification=\"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1506:AvoidExcessiveClassCoupling\", Justification = \"Must be ignored even if generated code is checked.\")]");
AnalyzerSuppressionsGenerator.Generate(writer, AnalyzerSuppressions);
using (writer.BlockInvariant("public class BindableMetadataProvider : global::Uno.UI.DataBinding.IBindableMetadataProvider"))
{
writer.AppendLineInvariant(@"static global::System.Collections.Hashtable _bindableTypeCacheByFullName = new global::System.Collections.Hashtable({0});", q.Count());
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1502:AvoidExcessiveComplexity\", Justification=\"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1506:AvoidExcessiveClassCoupling\", Justification = \"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1505:AvoidUnmaintainableCode\", Justification = \"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("private delegate global::Uno.UI.DataBinding.IBindableType TypeBuilderDelegate();");
using (writer.BlockInvariant("private static TypeBuilderDelegate CreateMemoized(TypeBuilderDelegate builder)"))
{
writer.AppendLineInvariant(@"global::Uno.UI.DataBinding.IBindableType value = null;
return () => {{
if (value == null)
{{
value = builder();
}}
return value;
}};"
);
}
using (writer.BlockInvariant("static BindableMetadataProvider()"))
{
GenerateTypeTable(writer, q);
}
writer.AppendLineInvariant(@"#if DEBUG");
writer.AppendLineInvariant(@"private global::System.Collections.Generic.List<global::System.Type> _knownMissingTypes = new global::System.Collections.Generic.List<global::System.Type>();");
writer.AppendLineInvariant(@"#endif");
using (writer.BlockInvariant("public void ForceInitialize()"))
{
using (writer.BlockInvariant(@"foreach(TypeBuilderDelegate item in _bindableTypeCacheByFullName.Values)"))
{
writer.AppendLineInvariant(@"item();");
}
}
using (writer.BlockInvariant("public global::Uno.UI.DataBinding.IBindableType GetBindableTypeByFullName(string fullName)"))
{
writer.AppendLineInvariant(@"var selector = _bindableTypeCacheByFullName[fullName] as TypeBuilderDelegate;");
using (writer.BlockInvariant(@"if(selector != null)"))
{
writer.AppendLineInvariant(@"return selector();");
}
using (writer.BlockInvariant(@"else"))
{
writer.AppendLineInvariant(@"return null;");
}
}
using (writer.BlockInvariant("public global::Uno.UI.DataBinding.IBindableType GetBindableTypeByType(Type type)"))
{
writer.AppendLineInvariant(@"var selector = _bindableTypeCacheByFullName[type.FullName] as TypeBuilderDelegate;");
using (writer.BlockInvariant(@"if(selector != null)"))
{
writer.AppendLineInvariant(@"return selector();");
}
writer.AppendLineInvariant(@"#if DEBUG");
using (writer.BlockInvariant(@"else"))
{
using (writer.BlockInvariant(@"lock(_knownMissingTypes)"))
{
using (writer.BlockInvariant(@"if(!_knownMissingTypes.Contains(type))"))
{
writer.AppendLineInvariant(@"_knownMissingTypes.Add(type);");
writer.AppendLineInvariant(@"Debug.WriteLine($""The type [{{type.FullName}}] is not known by the MetadataProvider, either add the Bindable attribute or add it to the TypeMetadataConfig.xml"");");
}
}
}
writer.AppendLineInvariant(@"#endif");
writer.AppendLineInvariant(@"return null;");
}
}
}
private class PropertyNameEqualityComparer : IEqualityComparer<IPropertySymbol>
{
public bool Equals(IPropertySymbol x, IPropertySymbol y)
{
return x.Name == y.Name;
}
public int GetHashCode(IPropertySymbol obj)
{
return obj.Name.GetHashCode();
}
}
private void GenerateMetadata(IndentedStringBuilder writer, IEnumerable<INamedTypeSymbol> types)
{
foreach (var type in types)
{
GenerateType(writer, type);
}
}
class GeneratedTypeInfo
{
public GeneratedTypeInfo(int index, bool hasProperties)
{
Index = index;
HasProperties = hasProperties;
}
public int Index { get; private set; }
public bool HasProperties { get; private set; }
}
private void GenerateType(IndentedStringBuilder writer, INamedTypeSymbol ownerType)
{
if (_typeMap.ContainsKey(ownerType))
{
return;
}
var ownerTypeName = GetGlobalQualifier(ownerType) + ownerType.ToString();
var flattenedProperties =
from property in ownerType.GetAllProperties()
where !property.IsStatic
&& !IsIgnoredType(property.ContainingSymbol as INamedTypeSymbol)
&& !IsNonBindable(property)
&& !IsOverride(property.GetMethod)
select property;
var properties =
from property in ownerType.GetProperties()
where !property.IsStatic
&& !IsNonBindable(property)
&& HasPublicGetter(property)
&& !IsOverride(property.GetMethod)
select property;
var propertyDependencyProperties =
from property in ownerType.GetProperties()
where property.IsStatic
&& property.Type == _dependencyPropertySymbol
select property.Name;
var fieldDependencyProperties =
from field in ownerType.GetFields()
where field.IsStatic
&& field.Type == _dependencyPropertySymbol
select field.Name;
var dependencyProperties = fieldDependencyProperties
.Concat(propertyDependencyProperties)
.ToArray();
properties = from prop in properties.Distinct(new PropertyNameEqualityComparer())
where !dependencyProperties.Contains(prop.Name + "Property")
select prop;
properties = properties
.ToArray();
var typeInfo = new GeneratedTypeInfo(
index: _typeMap.Count,
hasProperties: properties.Any() || dependencyProperties.Any()
);
_typeMap.Add(ownerType, typeInfo);
var baseType = GetBaseType(ownerType);
// Call the builders for the base types
if (baseType != null)
{
GenerateType(writer, baseType);
}
writer.AppendLineInvariant("/// <summary>");
writer.AppendLineInvariant("/// Builder for {0}", ownerType.GetFullName());
writer.AppendLineInvariant("/// </summary>");
writer.AppendLineInvariant("[System.Runtime.CompilerServices.CompilerGeneratedAttribute]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1502:AvoidExcessiveComplexity\", Justification=\"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1506:AvoidExcessiveClassCoupling\", Justification = \"Must be ignored even if generated code is checked.\")]");
AnalyzerSuppressionsGenerator.Generate(writer, AnalyzerSuppressions);
using (writer.BlockInvariant("static class MetadataBuilder_{0:000}", typeInfo.Index))
{
var postWriter = new IndentedStringBuilder();
postWriter.Indent(writer.CurrentLevel);
// Generate a parameter-less build to avoid generating a lambda during registration (avoids creating a caching backing field)
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1502:AvoidExcessiveComplexity\", Justification=\"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1506:AvoidExcessiveClassCoupling\", Justification = \"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1505:AvoidUnmaintainableCode\", Justification = \"Must be ignored even if generated code is checked.\")]");
using (writer.BlockInvariant("internal static global::Uno.UI.DataBinding.IBindableType Build()"))
{
writer.AppendLineInvariant("return Build(null);");
}
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1502:AvoidExcessiveComplexity\", Justification=\"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1506:AvoidExcessiveClassCoupling\", Justification = \"Must be ignored even if generated code is checked.\")]");
writer.AppendLineInvariant("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Maintainability\", \"CA1505:AvoidUnmaintainableCode\", Justification = \"Must be ignored even if generated code is checked.\")]");
using (writer.BlockInvariant("internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent)"))
{
writer.AppendLineInvariant(
@"var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType({0}, typeof(global::{1}));",
flattenedProperties
.Where(p => !IsStringIndexer(p) && HasPublicGetter(p))
.Count()
, ExpandType(ownerType)
);
// Call the builders for the base types
if (baseType != null)
{
var baseTypeMapped = _typeMap.UnoGetValueOrDefault(baseType);
writer.AppendLineInvariant(@"MetadataBuilder_{0:000}.Build(bindableType); // {1}", baseTypeMapped.Index, baseType.GetFullName());
}
var ctor = ownerType.GetMethods().FirstOrDefault(m => m.MethodKind == MethodKind.Constructor && !m.Parameters.Any() && m.IsLocallyPublic(_currentModule));
if (ctor != null && IsCreateable(ownerType))
{
using (writer.BlockInvariant("if(parent == null)"))
{
writer.AppendLineInvariant(@"bindableType.AddActivator(CreateInstance);");
postWriter.AppendLineInvariant($@"private static object CreateInstance() => new {ownerTypeName}();");
}
}
foreach (var property in properties)
{
var propertyTypeName = GetGlobalQualifier(property.Type) + SanitizeTypeName(property.Type.ToString());
var propertyName = property.Name;
if (IsStringIndexer(property))
{
writer.AppendLineInvariant("bindableType.AddIndexer(GetIndexer, SetIndexer);");
postWriter.AppendLineInvariant($@"private static object GetIndexer(object instance, string name) => (({ownerTypeName})instance)[name];");
if (property.SetMethod != null)
{
postWriter.AppendLineInvariant($@"private static void SetIndexer(object instance, string name, object value) => (({ownerTypeName})instance)[name] = ({propertyTypeName})value;");
}
else
{
postWriter.AppendLineInvariant("private static void SetIndexer(object instance, string name, object value) {{}}");
}
continue;
}
if (property.IsIndexer)
{
// Other types of indexers are currently not supported.
continue;
}
if (
property.SetMethod != null
&& property.SetMethod != null
&& property.SetMethod.IsLocallyPublic(_currentModule)
)
{
if (property.Type.IsValueType)
{
writer.AppendLineInvariant($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyTypeName}), Get{propertyName}, Set{propertyName});");
postWriter.AppendLineInvariant($@"private static object Get{propertyName}(object instance, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName};");
using (postWriter.BlockInvariant($@"private static void Set{propertyName}(object instance, object value, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence)"))
{
using (postWriter.BlockInvariant($"if(value != null)"))
{
postWriter.AppendLineInvariant($"(({ownerTypeName})instance).{propertyName} = ({propertyTypeName})value;");
}
}
}
else
{
writer.AppendLineInvariant($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyTypeName}), Get{propertyName}, Set{propertyName});");
postWriter.AppendLineInvariant($@"private static object Get{propertyName}(object instance, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName};");
postWriter.AppendLineInvariant($@"private static void Set{propertyName}(object instance, object value, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName} = ({propertyTypeName})value;");
}
}
else if (HasPublicGetter(property))
{
writer.AppendLineInvariant($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyTypeName}), Get{propertyName});");
postWriter.AppendLineInvariant($@"private static object Get{propertyName}(object instance, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => (({ownerTypeName})instance).{propertyName};");
}
}
foreach (var dependencyProperty in dependencyProperties)
{
var propertyName = dependencyProperty.TrimEnd("Property");
var getMethod = ownerType.GetMethods().FirstOrDefault(m => m.Name == "Get" + propertyName && m.Parameters.Length == 1 && m.IsLocallyPublic(_currentModule));
var setMethod = ownerType.GetMethods().FirstOrDefault(m => m.Name == "Set" + propertyName && m.Parameters.Length == 2 && m.IsLocallyPublic(_currentModule));
if (getMethod == null)
{
getMethod = ownerType
.GetProperties()
.FirstOrDefault(p => p.Name == propertyName && (p.GetMethod?.IsLocallyPublic(_currentModule) ?? false))
?.GetMethod;
}
if (getMethod != null)
{
var getter = $"{XamlConstants.Types.DependencyObjectExtensions}.GetValue(instance, {ownerTypeName}.{propertyName}Property, precedence)";
var setter = $"{XamlConstants.Types.DependencyObjectExtensions}.SetValue(instance, {ownerTypeName}.{propertyName}Property, value, precedence)";
var propertyType = SanitizeTypeName(getMethod.ReturnType.ToString());
writer.AppendLineInvariant($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyType}), Get{propertyName}, Set{propertyName});");
postWriter.AppendLineInvariant($@"private static object Get{propertyName}(object instance, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => {getter};");
postWriter.AppendLineInvariant($@"private static void Set{propertyName}(object instance, object value, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => {setter};");
}
}
writer.AppendLineInvariant(@"return bindableType;");
}
writer.Append(postWriter.ToString());
}
writer.AppendLine();
}
private static string ExpandType(INamedTypeSymbol ownerType)
{
if(ownerType.TypeKind == TypeKind.Error)
{
return ownerType.ToString() + "/* Type is error */";
}
else
{
return ownerType.GetFullName();
}
}
private static string GetGlobalQualifier(ITypeSymbol ownerType)
{
var arrayType = ownerType as IArrayTypeSymbol;
if (arrayType != null)
{
return GetGlobalQualifier(arrayType.ElementType);
}
ITypeSymbol nullType;
if (ownerType.IsNullable(out nullType))
{
return GetGlobalQualifier(nullType);
}
var needsGlobal = ownerType.SpecialType == SpecialType.None && !ownerType.IsTupleType;
return (needsGlobal ? "global::" : "");
}
private bool IsCreateable(INamedTypeSymbol type)
{
return !type.IsAbstract
&& type
.GetMethods()
.Safe()
.Any(m =>
m.MethodKind == MethodKind.Constructor
&& m.IsLocallyPublic(_currentModule)
&& m.Parameters.Safe().None()
);
}
private INamedTypeSymbol GetBaseType(INamedTypeSymbol type)
{
if (type.BaseType != null)
{
var ignoredByConfig = IsIgnoredType(type.BaseType);
// These types are know to not be bindable, so ignore them by default.
var isKnownBaseType = type.BaseType == _objectSymbol
|| type.BaseType == _javaObjectSymbol
|| type.BaseType == _nsObjectSymbol;
if(!ignoredByConfig && !isKnownBaseType)
{
return type.BaseType;
}
else
{
return GetBaseType(type.BaseType);
}
}
return null;
}
private bool IsIgnoredType(INamedTypeSymbol typeSymbol)
{
return typeSymbol.IsGenericType;
}
private bool HasPublicGetter(IPropertySymbol property) => property.GetMethod?.IsLocallyPublic(_currentModule) ?? false;
private bool IsStringIndexer(IPropertySymbol property)
{
return property.IsIndexer
&& property.GetMethod.IsLocallyPublic(_currentModule)
&& property.Parameters.Length == 1
&& property.Parameters.Any(p => p.Type == _stringSymbol);
}
private bool IsNonBindable(IPropertySymbol property) => property.FindAttributeFlattened(_nonBindableSymbol) != null;
private bool IsOverride(IMethodSymbol methodDefinition)
{
return methodDefinition!=null
&& methodDefinition.IsOverride
&& !methodDefinition.IsVirtual;
}
private string SanitizeTypeName(string name)
{
for (int i = 0; i < 10; i++)
{
name = name.Replace("`" + i, "");
}
name = name.Replace("/", ".");
return name;
}
private void GenerateTypeTable(IndentedStringBuilder writer, IEnumerable<INamedTypeSymbol> types)
{
foreach (var type in _typeMap.Where(k => !k.Key.IsGenericType))
{
writer.AppendLineInvariant(
"_bindableTypeCacheByFullName[\"{0}\"] = CreateMemoized(MetadataBuilder_{1:000}.Build);",
type.Key,
type.Value.Index
);
}
}
}
}