-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Compilation_WellKnownMembers.cs
767 lines (655 loc) · 33.2 KB
/
Compilation_WellKnownMembers.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.RuntimeMembers;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp
{
// TODO: (tomat) translated 1:1 from VB, might need adjustments
// TODO: (tomat) can we share more with VB?
public partial class CSharpCompilation
{
private readonly WellKnownMembersSignatureComparer _wellKnownMemberSignatureComparer;
/// <summary>
/// An array of cached well known types available for use in this Compilation.
/// Lazily filled by GetWellKnownType method.
/// </summary>
private NamedTypeSymbol[] _lazyWellKnownTypes;
/// <summary>
/// Lazy cache of well known members.
/// Not yet known value is represented by ErrorTypeSymbol.UnknownResultType
/// </summary>
private Symbol[] _lazyWellKnownTypeMembers;
/// <summary>
/// Lookup member declaration in well known type used by this Compilation.
/// </summary>
/// <remarks>
/// If a well-known member of a generic type instantiation is needed use this method to get the corresponding generic definition and
/// <see cref="MethodSymbol.AsMember"/> to construct an instantiation.
/// </remarks>
internal Symbol GetWellKnownTypeMember(WellKnownMember member)
{
Debug.Assert(member >= 0 && member < WellKnownMember.Count);
// Test hook: if a member is marked missing, then return null.
if (IsMemberMissing(member)) return null;
if (_lazyWellKnownTypeMembers == null || ReferenceEquals(_lazyWellKnownTypeMembers[(int)member], ErrorTypeSymbol.UnknownResultType))
{
if (_lazyWellKnownTypeMembers == null)
{
var wellKnownTypeMembers = new Symbol[(int)WellKnownMember.Count];
for (int i = 0; i < wellKnownTypeMembers.Length; i++)
{
wellKnownTypeMembers[i] = ErrorTypeSymbol.UnknownResultType;
}
Interlocked.CompareExchange(ref _lazyWellKnownTypeMembers, wellKnownTypeMembers, null);
}
MemberDescriptor descriptor = WellKnownMembers.GetDescriptor(member);
NamedTypeSymbol type = descriptor.DeclaringTypeId <= (int)SpecialType.Count
? this.GetSpecialType((SpecialType)descriptor.DeclaringTypeId)
: this.GetWellKnownType((WellKnownType)descriptor.DeclaringTypeId);
Symbol result = null;
if (!type.IsErrorType())
{
result = GetRuntimeMember(type, ref descriptor, _wellKnownMemberSignatureComparer, accessWithinOpt: this.Assembly);
}
Interlocked.CompareExchange(ref _lazyWellKnownTypeMembers[(int)member], result, ErrorTypeSymbol.UnknownResultType);
}
return _lazyWellKnownTypeMembers[(int)member];
}
internal NamedTypeSymbol GetWellKnownType(WellKnownType type)
{
Debug.Assert(type >= WellKnownType.First && type <= WellKnownType.Last);
int index = (int)type - (int)WellKnownType.First;
if (_lazyWellKnownTypes == null || (object)_lazyWellKnownTypes[index] == null)
{
if (_lazyWellKnownTypes == null)
{
Interlocked.CompareExchange(ref _lazyWellKnownTypes, new NamedTypeSymbol[(int)WellKnownTypes.Count], null);
}
string mdName = type.GetMetadataName();
var warnings = DiagnosticBag.GetInstance();
NamedTypeSymbol result;
if (IsTypeMissing(type))
{
result = null;
}
else
{
result = this.Assembly.GetTypeByMetadataName(
mdName, includeReferences: true, useCLSCompliantNameArityEncoding: true, isWellKnownType: true, warnings: warnings);
}
if ((object)result == null)
{
// TODO: should GetTypeByMetadataName rather return a missing symbol?
MetadataTypeName emittedName = MetadataTypeName.FromFullName(mdName, useCLSCompliantNameArityEncoding: true);
result = new MissingMetadataTypeSymbol.TopLevel(this.Assembly.Modules[0], ref emittedName, type);
}
if ((object)Interlocked.CompareExchange(ref _lazyWellKnownTypes[index], result, null) != null)
{
Debug.Assert(
result == _lazyWellKnownTypes[index] || (_lazyWellKnownTypes[index].IsErrorType() && result.IsErrorType())
);
}
else
{
AdditionalCodegenWarnings.AddRange(warnings);
}
warnings.Free();
}
return _lazyWellKnownTypes[index];
}
internal bool IsAttributeType(TypeSymbol type)
{
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
return IsEqualOrDerivedFromWellKnownClass(type, WellKnownType.System_Attribute, ref useSiteDiagnostics);
}
internal override bool IsAttributeType(ITypeSymbol type)
{
return IsAttributeType((TypeSymbol)type);
}
internal bool IsExceptionType(TypeSymbol type, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
{
return IsEqualOrDerivedFromWellKnownClass(type, WellKnownType.System_Exception, ref useSiteDiagnostics);
}
internal bool IsEqualOrDerivedFromWellKnownClass(TypeSymbol type, WellKnownType wellKnownType, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
{
Debug.Assert(wellKnownType == WellKnownType.System_Attribute ||
wellKnownType == WellKnownType.System_Exception);
if (type.Kind != SymbolKind.NamedType || type.TypeKind != TypeKind.Class)
{
return false;
}
var wkType = GetWellKnownType(wellKnownType);
return type.Equals(wkType, ignoreDynamic: false) || type.IsDerivedFrom(wkType, ignoreDynamic: false, useSiteDiagnostics: ref useSiteDiagnostics);
}
internal override bool IsSystemTypeReference(ITypeSymbol type)
{
return (TypeSymbol)type == GetWellKnownType(WellKnownType.System_Type);
}
internal override ISymbol CommonGetWellKnownTypeMember(WellKnownMember member)
{
return GetWellKnownTypeMember(member);
}
internal static Symbol GetRuntimeMember(NamedTypeSymbol declaringType, ref MemberDescriptor descriptor, SignatureComparer<MethodSymbol, FieldSymbol, PropertySymbol, TypeSymbol, ParameterSymbol> comparer, AssemblySymbol accessWithinOpt)
{
Symbol result = null;
SymbolKind targetSymbolKind;
MethodKind targetMethodKind = MethodKind.Ordinary;
bool isStatic = (descriptor.Flags & MemberFlags.Static) != 0;
switch (descriptor.Flags & MemberFlags.KindMask)
{
case MemberFlags.Constructor:
targetSymbolKind = SymbolKind.Method;
targetMethodKind = MethodKind.Constructor;
// static constructors are never called explicitly
Debug.Assert(!isStatic);
break;
case MemberFlags.Method:
targetSymbolKind = SymbolKind.Method;
break;
case MemberFlags.PropertyGet:
targetSymbolKind = SymbolKind.Method;
targetMethodKind = MethodKind.PropertyGet;
break;
case MemberFlags.Field:
targetSymbolKind = SymbolKind.Field;
break;
case MemberFlags.Property:
targetSymbolKind = SymbolKind.Property;
break;
default:
throw ExceptionUtilities.UnexpectedValue(descriptor.Flags);
}
foreach (var member in declaringType.GetMembers(descriptor.Name))
{
Debug.Assert(member.Name.Equals(descriptor.Name));
if (member.Kind != targetSymbolKind || member.IsStatic != isStatic ||
!(member.DeclaredAccessibility == Accessibility.Public || ((object)accessWithinOpt != null && Symbol.IsSymbolAccessible(member, accessWithinOpt))))
{
continue;
}
switch (targetSymbolKind)
{
case SymbolKind.Method:
{
MethodSymbol method = (MethodSymbol)member;
MethodKind methodKind = method.MethodKind;
// Treat user-defined conversions and operators as ordinary methods for the purpose
// of matching them here.
if (methodKind == MethodKind.Conversion || methodKind == MethodKind.UserDefinedOperator)
{
methodKind = MethodKind.Ordinary;
}
if (method.Arity != descriptor.Arity || methodKind != targetMethodKind ||
((descriptor.Flags & MemberFlags.Virtual) != 0) != (method.IsVirtual || method.IsOverride || method.IsAbstract))
{
continue;
}
if (!comparer.MatchMethodSignature(method, descriptor.Signature))
{
continue;
}
}
break;
case SymbolKind.Property:
{
PropertySymbol property = (PropertySymbol)member;
if (((descriptor.Flags & MemberFlags.Virtual) != 0) != (property.IsVirtual || property.IsOverride || property.IsAbstract))
{
continue;
}
if (!comparer.MatchPropertySignature(property, descriptor.Signature))
{
continue;
}
}
break;
case SymbolKind.Field:
if (!comparer.MatchFieldSignature((FieldSymbol)member, descriptor.Signature))
{
continue;
}
break;
default:
throw ExceptionUtilities.UnexpectedValue(targetSymbolKind);
}
// ambiguity
if ((object)result != null)
{
result = null;
break;
}
result = member;
}
return result;
}
/// <summary>
/// Synthesizes a custom attribute.
/// Returns null if the <paramref name="constructor"/> symbol is missing,
/// or any of the members in <paramref name="namedArguments" /> are missing.
/// The attribute is synthesized only if present.
/// </summary>
/// <param name="constructor">
/// Constructor of the attribute. If it doesn't exist, the attribute is not created.
/// </param>
/// <param name="arguments">Arguments to the attribute constructor.</param>
/// <param name="namedArguments">
/// Takes a list of pairs of well-known members and constants. The constants
/// will be passed to the field/property referenced by the well-known member.
/// If the well-known member does not exist in the compilation then no attribute
/// will be synthesized.
/// </param>
internal SynthesizedAttributeData TrySynthesizeAttribute(
WellKnownMember constructor,
ImmutableArray<TypedConstant> arguments = default(ImmutableArray<TypedConstant>),
ImmutableArray<KeyValuePair<WellKnownMember, TypedConstant>> namedArguments = default(ImmutableArray<KeyValuePair<WellKnownMember, TypedConstant>>))
{
var ctorSymbol = (MethodSymbol)GetWellKnownTypeMember(constructor);
if ((object)ctorSymbol == null)
{
// if this assert fails, UseSiteErrors for "member" have not been checked before emitting ...
Debug.Assert(WellKnownMembers.IsSynthesizedAttributeOptional(constructor));
return null;
}
if (arguments.IsDefault)
{
arguments = ImmutableArray<TypedConstant>.Empty;
}
ImmutableArray<KeyValuePair<string, TypedConstant>> namedStringArguments;
if (namedArguments.IsDefault)
{
namedStringArguments = ImmutableArray<KeyValuePair<string, TypedConstant>>.Empty;
}
else
{
var builder = new ArrayBuilder<KeyValuePair<string, TypedConstant>>(namedArguments.Length);
foreach (var arg in namedArguments)
{
var wellKnownMember = GetWellKnownTypeMember(arg.Key);
if (wellKnownMember == null || wellKnownMember is ErrorTypeSymbol)
{
// if this assert fails, UseSiteErrors for "member" have not been checked before emitting ...
Debug.Assert(WellKnownMembers.IsSynthesizedAttributeOptional(constructor));
return null;
}
else
{
builder.Add(new KeyValuePair<string, TypedConstant>(
wellKnownMember.Name, arg.Value));
}
}
namedStringArguments = builder.ToImmutableAndFree();
}
return new SynthesizedAttributeData(ctorSymbol, arguments, namedStringArguments);
}
internal SynthesizedAttributeData SynthesizeDecimalConstantAttribute(decimal value)
{
bool isNegative;
byte scale;
uint low, mid, high;
value.GetBits(out isNegative, out scale, out low, out mid, out high);
var systemByte = GetSpecialType(SpecialType.System_Byte);
Debug.Assert(!systemByte.HasUseSiteError);
var systemUnit32 = GetSpecialType(SpecialType.System_UInt32);
Debug.Assert(!systemUnit32.HasUseSiteError);
return TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_DecimalConstantAttribute__ctor,
ImmutableArray.Create(
new TypedConstant(systemByte, TypedConstantKind.Primitive, scale),
new TypedConstant(systemByte, TypedConstantKind.Primitive, (byte)(isNegative ? 128 : 0)),
new TypedConstant(systemUnit32, TypedConstantKind.Primitive, high),
new TypedConstant(systemUnit32, TypedConstantKind.Primitive, mid),
new TypedConstant(systemUnit32, TypedConstantKind.Primitive, low)
));
}
internal SynthesizedAttributeData SynthesizeDebuggerBrowsableNeverAttribute()
{
if (Options.OptimizationLevel != OptimizationLevel.Debug)
{
return null;
}
return TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerBrowsableAttribute__ctor,
ImmutableArray.Create(new TypedConstant(
GetWellKnownType(WellKnownType.System_Diagnostics_DebuggerBrowsableState),
TypedConstantKind.Enum,
DebuggerBrowsableState.Never)));
}
internal SynthesizedAttributeData SynthesizeDebuggerStepThroughAttribute()
{
if (Options.OptimizationLevel != OptimizationLevel.Debug)
{
return null;
}
return TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerStepThroughAttribute__ctor);
}
internal SynthesizedAttributeData SynthesizeDebuggableAttribute()
{
TypeSymbol debuggableAttribute = GetWellKnownType(WellKnownType.System_Diagnostics_DebuggableAttribute);
Debug.Assert((object)debuggableAttribute != null, "GetWellKnownType unexpectedly returned null");
if (debuggableAttribute is MissingMetadataTypeSymbol)
{
return null;
}
TypeSymbol debuggingModesType = GetWellKnownType(WellKnownType.System_Diagnostics_DebuggableAttribute__DebuggingModes);
Debug.Assert((object)debuggingModesType != null, "GetWellKnownType unexpectedly returned null");
if (debuggingModesType is MissingMetadataTypeSymbol)
{
return null;
}
// IgnoreSymbolStoreDebuggingMode flag is checked by the CLR, it is not referred to by the debugger.
// It tells the JIT that it doesn't need to load the PDB at the time it generates jitted code.
// The PDB would still be used by a debugger, or even by the runtime for putting source line information
// on exception stack traces. We always set this flag to avoid overhead of JIT loading the PDB.
// The theoretical scenario for not setting it would be a language compiler that wants their sequence points
// at specific places, but those places don't match what CLR's heuristics calculate when scanning the IL.
var ignoreSymbolStoreDebuggingMode = (FieldSymbol)GetWellKnownTypeMember(WellKnownMember.System_Diagnostics_DebuggableAttribute_DebuggingModes__IgnoreSymbolStoreSequencePoints);
if ((object)ignoreSymbolStoreDebuggingMode == null || !ignoreSymbolStoreDebuggingMode.HasConstantValue)
{
return null;
}
int constantVal = ignoreSymbolStoreDebuggingMode.GetConstantValue(ConstantFieldsInProgress.Empty, earlyDecodingWellKnownAttributes: false).Int32Value;
// Since .NET 2.0 the combinations of None, Default and DisableOptimizations have the following effect:
//
// None JIT optimizations enabled
// Default JIT optimizations enabled
// DisableOptimizations JIT optimizations enabled
// Default | DisableOptimizations JIT optimizations disabled
if (_options.OptimizationLevel == OptimizationLevel.Debug)
{
var defaultDebuggingMode = (FieldSymbol)GetWellKnownTypeMember(WellKnownMember.System_Diagnostics_DebuggableAttribute_DebuggingModes__Default);
if ((object)defaultDebuggingMode == null || !defaultDebuggingMode.HasConstantValue)
{
return null;
}
var disableOptimizationsDebuggingMode = (FieldSymbol)GetWellKnownTypeMember(WellKnownMember.System_Diagnostics_DebuggableAttribute_DebuggingModes__DisableOptimizations);
if ((object)disableOptimizationsDebuggingMode == null || !disableOptimizationsDebuggingMode.HasConstantValue)
{
return null;
}
constantVal |= defaultDebuggingMode.GetConstantValue(ConstantFieldsInProgress.Empty, earlyDecodingWellKnownAttributes: false).Int32Value;
constantVal |= disableOptimizationsDebuggingMode.GetConstantValue(ConstantFieldsInProgress.Empty, earlyDecodingWellKnownAttributes: false).Int32Value;
}
if (_options.EnableEditAndContinue)
{
var enableEncDebuggingMode = (FieldSymbol)GetWellKnownTypeMember(WellKnownMember.System_Diagnostics_DebuggableAttribute_DebuggingModes__EnableEditAndContinue);
if ((object)enableEncDebuggingMode == null || !enableEncDebuggingMode.HasConstantValue)
{
return null;
}
constantVal |= enableEncDebuggingMode.GetConstantValue(ConstantFieldsInProgress.Empty, earlyDecodingWellKnownAttributes: false).Int32Value;
}
var typedConstantDebugMode = new TypedConstant(debuggingModesType, TypedConstantKind.Enum, constantVal);
return TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggableAttribute__ctorDebuggingModes,
ImmutableArray.Create(typedConstantDebugMode));
}
/// <summary>
/// Given a type <paramref name="type"/>, which is either dynamic type OR is a constructed type with dynamic type present in it's type argument tree,
/// returns a synthesized DynamicAttribute with encoded dynamic transforms array.
/// </summary>
/// <remarks>This method is port of AttrBind::CompileDynamicAttr from the native C# compiler.</remarks>
internal SynthesizedAttributeData SynthesizeDynamicAttribute(TypeSymbol type, int customModifiersCount, RefKind refKindOpt = RefKind.None)
{
Debug.Assert((object)type != null);
Debug.Assert(type.ContainsDynamic());
if (type.IsDynamic() && refKindOpt == RefKind.None && customModifiersCount == 0)
{
return TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_DynamicAttribute__ctor);
}
else
{
NamedTypeSymbol booleanType = GetSpecialType(SpecialType.System_Boolean);
Debug.Assert((object)booleanType != null);
var transformFlags = DynamicTransformsEncoder.Encode(type, booleanType, customModifiersCount, refKindOpt);
var boolArray = new ArrayTypeSymbol(booleanType.ContainingAssembly, booleanType, customModifiers: ImmutableArray<CustomModifier>.Empty);
var arguments = ImmutableArray.Create<TypedConstant>(new TypedConstant(boolArray, transformFlags));
return TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_DynamicAttribute__ctorTransformFlags, arguments);
}
}
/// <summary>
/// Used to generate the dynamic attributes for the required typesymbol.
/// </summary>
internal static class DynamicTransformsEncoder
{
internal static ImmutableArray<TypedConstant> Encode(TypeSymbol type, TypeSymbol booleanType, int customModifiersCount, RefKind refKind)
{
var flagsBuilder = ArrayBuilder<bool>.GetInstance();
EncodeInternal(type, customModifiersCount, refKind, flagsBuilder);
Debug.Assert(flagsBuilder.Any());
Debug.Assert(flagsBuilder.Contains(true));
var constantsBuilder = ArrayBuilder<TypedConstant>.GetInstance(flagsBuilder.Count);
foreach (bool flag in flagsBuilder)
{
constantsBuilder.Add(new TypedConstant(booleanType, TypedConstantKind.Primitive, flag));
}
flagsBuilder.Free();
return constantsBuilder.ToImmutableAndFree();
}
internal static ImmutableArray<bool> Encode(TypeSymbol type, int customModifiersCount, RefKind refKind)
{
var transformFlagsBuilder = ArrayBuilder<bool>.GetInstance();
EncodeInternal(type, customModifiersCount, refKind, transformFlagsBuilder);
return transformFlagsBuilder.ToImmutableAndFree();
}
internal static void EncodeInternal(TypeSymbol type, int customModifiersCount, RefKind refKind, ArrayBuilder<bool> transformFlagsBuilder)
{
Debug.Assert(!transformFlagsBuilder.Any());
if (refKind != RefKind.None)
{
// Native compiler encodes an extra transform flag, always false, for ref/out parameters.
transformFlagsBuilder.Add(false);
}
// Native compiler encodes an extra transform flag, always false, for each custom modifier.
HandleCustomModifiers(customModifiersCount, transformFlagsBuilder);
type.VisitType(s_encodeDynamicTransform, transformFlagsBuilder);
}
private static readonly Func<TypeSymbol, ArrayBuilder<bool>, bool, bool> s_encodeDynamicTransform = (type, transformFlagsBuilder, isNestedNamedType) =>
{
// Encode transforms flag for this type and it's custom modifiers (if any).
switch (type.TypeKind)
{
case TypeKind.Dynamic:
transformFlagsBuilder.Add(true);
break;
case TypeKind.Array:
HandleCustomModifiers(((ArrayTypeSymbol)type).CustomModifiers.Length, transformFlagsBuilder);
transformFlagsBuilder.Add(false);
break;
case TypeKind.Pointer:
HandleCustomModifiers(((PointerTypeSymbol)type).CustomModifiers.Length, transformFlagsBuilder);
transformFlagsBuilder.Add(false);
break;
default:
// Encode transforms flag for this type.
// For nested named types, a single flag (false) is encoded for the entire type name, followed by flags for all of the type arguments.
// For example, for type "A<T>.B<dynamic>", encoded transform flags are:
// {
// false, // Type "A.B"
// false, // Type parameter "T"
// true, // Type parameter "dynamic"
// }
if (!isNestedNamedType)
{
transformFlagsBuilder.Add(false);
}
break;
}
// Continue walking types
return false;
};
private static void HandleCustomModifiers(int customModifiersCount, ArrayBuilder<bool> transformFlagsBuilder)
{
for (int i = 0; i < customModifiersCount; i++)
{
// Native compiler encodes an extra transforms flag, always false, for each custom modifier.
transformFlagsBuilder.Add(false);
}
}
}
internal class SpecialMembersSignatureComparer : SignatureComparer<MethodSymbol, FieldSymbol, PropertySymbol, TypeSymbol, ParameterSymbol>
{
// Fields
public static readonly SpecialMembersSignatureComparer Instance = new SpecialMembersSignatureComparer();
// Methods
protected SpecialMembersSignatureComparer()
{
}
protected override TypeSymbol GetArrayElementType(TypeSymbol type)
{
if (type.Kind != SymbolKind.ArrayType)
{
return null;
}
ArrayTypeSymbol array = (ArrayTypeSymbol)type;
if (array.Rank < 2)
{
return null;
}
return array.ElementType;
}
protected override TypeSymbol GetFieldType(FieldSymbol field)
{
return field.Type;
}
protected override TypeSymbol GetPropertyType(PropertySymbol property)
{
return property.Type;
}
protected override TypeSymbol GetGenericTypeArgument(TypeSymbol type, int argumentIndex)
{
if (type.Kind != SymbolKind.NamedType)
{
return null;
}
NamedTypeSymbol named = (NamedTypeSymbol)type;
if (named.Arity <= argumentIndex)
{
return null;
}
if ((object)named.ContainingType != null)
{
return null;
}
return named.TypeArgumentsNoUseSiteDiagnostics[argumentIndex];
}
protected override TypeSymbol GetGenericTypeDefinition(TypeSymbol type)
{
if (type.Kind != SymbolKind.NamedType)
{
return null;
}
NamedTypeSymbol named = (NamedTypeSymbol)type;
if ((object)named.ContainingType != null)
{
return null;
}
if (named.Arity == 0)
{
return null;
}
return (NamedTypeSymbol)named.OriginalDefinition;
}
protected override ImmutableArray<ParameterSymbol> GetParameters(MethodSymbol method)
{
return method.Parameters;
}
protected override ImmutableArray<ParameterSymbol> GetParameters(PropertySymbol property)
{
return property.Parameters;
}
protected override TypeSymbol GetParamType(ParameterSymbol parameter)
{
return parameter.Type;
}
protected override TypeSymbol GetPointedToType(TypeSymbol type)
{
return type.Kind == SymbolKind.PointerType ? ((PointerTypeSymbol)type).PointedAtType : null;
}
protected override TypeSymbol GetReturnType(MethodSymbol method)
{
return method.ReturnType;
}
protected override TypeSymbol GetSZArrayElementType(TypeSymbol type)
{
if (type.Kind != SymbolKind.ArrayType)
{
return null;
}
ArrayTypeSymbol array = (ArrayTypeSymbol)type;
if (array.Rank != 1)
{
return null;
}
return array.ElementType;
}
protected override bool IsByRefParam(ParameterSymbol parameter)
{
return parameter.RefKind != RefKind.None;
}
protected override bool IsGenericMethodTypeParam(TypeSymbol type, int paramPosition)
{
if (type.Kind != SymbolKind.TypeParameter)
{
return false;
}
TypeParameterSymbol typeParam = (TypeParameterSymbol)type;
if (typeParam.ContainingSymbol.Kind != SymbolKind.Method)
{
return false;
}
return (typeParam.Ordinal == paramPosition);
}
protected override bool IsGenericTypeParam(TypeSymbol type, int paramPosition)
{
if (type.Kind != SymbolKind.TypeParameter)
{
return false;
}
TypeParameterSymbol typeParam = (TypeParameterSymbol)type;
if (typeParam.ContainingSymbol.Kind != SymbolKind.NamedType)
{
return false;
}
return (typeParam.Ordinal == paramPosition);
}
protected override bool MatchArrayRank(TypeSymbol type, int countOfDimensions)
{
if (countOfDimensions == 1)
{
return false;
}
if (type.Kind != SymbolKind.ArrayType)
{
return false;
}
ArrayTypeSymbol array = (ArrayTypeSymbol)type;
return (array.Rank == countOfDimensions);
}
protected override bool MatchTypeToTypeId(TypeSymbol type, int typeId)
{
return (int)type.SpecialType == typeId;
}
}
private class WellKnownMembersSignatureComparer : SpecialMembersSignatureComparer
{
private readonly CSharpCompilation _compilation;
public WellKnownMembersSignatureComparer(CSharpCompilation compilation)
{
_compilation = compilation;
}
protected override bool MatchTypeToTypeId(TypeSymbol type, int typeId)
{
WellKnownType wellKnownId = (WellKnownType)typeId;
if (wellKnownId >= WellKnownType.First && wellKnownId <= WellKnownType.Last)
{
return (type == _compilation.GetWellKnownType(wellKnownId));
}
return base.MatchTypeToTypeId(type, typeId);
}
}
}
}