/
CommonAttributeData.cs
579 lines (491 loc) · 25.5 KB
/
CommonAttributeData.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis.Symbols;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
public abstract class AttributeData
{
protected AttributeData()
{
}
/// <summary>
/// The attribute class.
/// </summary>
public INamedTypeSymbol? AttributeClass { get { return CommonAttributeClass; } }
protected abstract INamedTypeSymbol? CommonAttributeClass { get; }
/// <summary>
/// The constructor on the attribute class.
/// </summary>
public IMethodSymbol? AttributeConstructor { get { return CommonAttributeConstructor; } }
protected abstract IMethodSymbol? CommonAttributeConstructor { get; }
public SyntaxReference? ApplicationSyntaxReference { get { return CommonApplicationSyntaxReference; } }
protected abstract SyntaxReference? CommonApplicationSyntaxReference { get; }
/// <summary>
/// Constructor arguments on the attribute.
/// </summary>
public ImmutableArray<TypedConstant> ConstructorArguments { get { return CommonConstructorArguments; } }
protected internal abstract ImmutableArray<TypedConstant> CommonConstructorArguments { get; }
/// <summary>
/// Named (property value) arguments on the attribute.
/// </summary>
public ImmutableArray<KeyValuePair<string, TypedConstant>> NamedArguments { get { return CommonNamedArguments; } }
protected internal abstract ImmutableArray<KeyValuePair<string, TypedConstant>> CommonNamedArguments { get; }
/// <summary>
/// Attribute is conditionally omitted if it is a source attribute and both the following are true:
/// (a) It has at least one applied conditional attribute AND
/// (b) None of conditional symbols are true at the attribute source location.
/// </summary>
internal virtual bool IsConditionallyOmitted
{
get { return false; }
}
// Uncommenting portion of the attribute is tracked by https://github.com/dotnet/roslyn/issues/70592
[MemberNotNullWhen(false, nameof(AttributeClass)/*, nameof(AttributeConstructor)*/)]
internal virtual bool HasErrors
{
get
{
Debug.Assert(AttributeClass is not null);
return false;
}
}
/// <summary>
/// Checks if an applied attribute with the given attributeType matches the namespace name and type name of the given early attribute's description
/// and the attribute description has a signature with parameter count equal to the given attributeArgCount.
/// NOTE: We don't allow early decoded attributes to have optional parameters.
/// </summary>
internal static bool IsTargetEarlyAttribute(INamedTypeSymbolInternal attributeType, int attributeArgCount, AttributeDescription description)
{
if (attributeType.ContainingSymbol?.Kind != SymbolKind.Namespace)
{
return false;
}
int attributeCtorsCount = description.Signatures.Length;
for (int i = 0; i < attributeCtorsCount; i++)
{
int parameterCount = description.GetParameterCount(signatureIndex: i);
// NOTE: Below assumption disallows early decoding well-known attributes with optional parameters.
if (attributeArgCount == parameterCount)
{
StringComparison options = description.MatchIgnoringCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
return attributeType.Name.Equals(description.Name, options) && namespaceMatch(attributeType.ContainingNamespace, description.Namespace, options);
}
}
return false;
static bool namespaceMatch(INamespaceSymbolInternal container, string namespaceName, StringComparison options)
{
int index = namespaceName.Length;
bool expectDot = false;
while (true)
{
if (container.IsGlobalNamespace)
{
return index == 0;
}
if (expectDot)
{
index--;
if (index < 0 || namespaceName[index] != '.')
{
return false;
}
}
else
{
expectDot = true;
}
string name = container.Name;
int nameLength = name.Length;
index -= nameLength;
if (index < 0 || string.Compare(namespaceName, index, name, 0, nameLength, options) != 0)
{
return false;
}
container = container.ContainingNamespace;
if (container is null)
{
return false;
}
}
}
}
/// <summary>
/// Returns the value of a constructor argument as type <typeparamref name="T"/>.
/// Throws if no constructor argument exists or the argument cannot be converted to the type.
/// </summary>
internal T? GetConstructorArgument<T>(int i, SpecialType specialType)
{
var constructorArgs = this.CommonConstructorArguments;
return constructorArgs[i].DecodeValue<T>(specialType);
}
/// <summary>
/// Returns named attribute argument with the given <paramref name="name"/> as type <typeparamref name="T"/>.
/// If there is more than one named argument with this name, it returns the last one.
/// If no named argument is found then the <paramref name="defaultValue"/> is returned.
/// </summary>
/// <param name="name">The metadata property or field name. This name is case sensitive (both VB and C#).</param>
/// <param name="specialType">SpecialType of the named argument.</param>
/// <param name="defaultValue">Default value for the named argument.</param>
/// <remarks>
/// For user defined attributes VB allows duplicate named arguments and uses the last value.
/// Dev11 reports an error for pseudo-custom attributes when emitting metadata. We don't.
/// </remarks>
internal T? DecodeNamedArgument<T>(string name, SpecialType specialType, T? defaultValue = default)
{
return DecodeNamedArgument<T>(CommonNamedArguments, name, specialType, defaultValue);
}
private static T? DecodeNamedArgument<T>(ImmutableArray<KeyValuePair<string, TypedConstant>> namedArguments, string name, SpecialType specialType, T? defaultValue = default)
{
int index = IndexOfNamedArgument(namedArguments, name);
return index >= 0 ? namedArguments[index].Value.DecodeValue<T>(specialType) : defaultValue;
}
private static int IndexOfNamedArgument(ImmutableArray<KeyValuePair<string, TypedConstant>> namedArguments, string name)
{
// For user defined attributes VB allows duplicate named arguments and uses the last value.
// Dev11 reports an error for pseudo-custom attributes when emitting metadata. We don't.
for (int i = namedArguments.Length - 1; i >= 0; i--)
{
// even for VB this is case sensitive comparison:
if (string.Equals(namedArguments[i].Key, name, StringComparison.Ordinal))
{
return i;
}
}
return -1;
}
#region Decimal and DateTime Constant Decoding
internal ConstantValue DecodeDecimalConstantValue()
{
// There are two decimal constant attribute ctors:
// (byte scale, byte sign, uint high, uint mid, uint low) and
// (byte scale, byte sign, int high, int mid, int low)
// The dev10 compiler only honors the first; Roslyn honours both.
// We should not end up in this code path unless we know we have one of them.
Debug.Assert(AttributeConstructor is object);
var parameters = AttributeConstructor.Parameters;
ImmutableArray<TypedConstant> args = this.CommonConstructorArguments;
Debug.Assert(parameters.Length == 5);
Debug.Assert(parameters[0].Type.SpecialType == SpecialType.System_Byte);
Debug.Assert(parameters[1].Type.SpecialType == SpecialType.System_Byte);
int low, mid, high;
byte scale = args[0].DecodeValue<byte>(SpecialType.System_Byte);
bool isNegative = args[1].DecodeValue<byte>(SpecialType.System_Byte) != 0;
if (parameters[2].Type.SpecialType == SpecialType.System_Int32)
{
Debug.Assert(parameters[2].Type.SpecialType == SpecialType.System_Int32);
Debug.Assert(parameters[3].Type.SpecialType == SpecialType.System_Int32);
Debug.Assert(parameters[4].Type.SpecialType == SpecialType.System_Int32);
high = args[2].DecodeValue<int>(SpecialType.System_Int32);
mid = args[3].DecodeValue<int>(SpecialType.System_Int32);
low = args[4].DecodeValue<int>(SpecialType.System_Int32);
}
else
{
Debug.Assert(parameters[2].Type.SpecialType == SpecialType.System_UInt32);
Debug.Assert(parameters[3].Type.SpecialType == SpecialType.System_UInt32);
Debug.Assert(parameters[4].Type.SpecialType == SpecialType.System_UInt32);
high = unchecked((int)args[2].DecodeValue<uint>(SpecialType.System_UInt32));
mid = unchecked((int)args[3].DecodeValue<uint>(SpecialType.System_UInt32));
low = unchecked((int)args[4].DecodeValue<uint>(SpecialType.System_UInt32));
}
return ConstantValue.Create(new decimal(low, mid, high, isNegative, scale));
}
internal ConstantValue DecodeDateTimeConstantValue()
{
long value = this.CommonConstructorArguments[0].DecodeValue<long>(SpecialType.System_Int64);
// if value is outside this range, DateTime would throw when constructed
if (value < DateTime.MinValue.Ticks || value > DateTime.MaxValue.Ticks)
{
return ConstantValue.Bad;
}
return ConstantValue.Create(new DateTime(value));
}
#endregion
internal ObsoleteAttributeData DecodeObsoleteAttribute(ObsoleteAttributeKind kind)
{
switch (kind)
{
case ObsoleteAttributeKind.Obsolete:
return DecodeObsoleteAttribute();
case ObsoleteAttributeKind.Deprecated:
return DecodeDeprecatedAttribute();
case ObsoleteAttributeKind.WindowsExperimental:
return DecodeWindowsExperimentalAttribute();
case ObsoleteAttributeKind.Experimental:
return DecodeExperimentalAttribute();
default:
throw ExceptionUtilities.UnexpectedValue(kind);
}
}
internal ObsoleteAttributeData DecodeExperimentalAttribute()
{
// ExperimentalAttribute(string diagnosticId)
Debug.Assert(this.CommonConstructorArguments.Length == 1);
string? diagnosticId = this.CommonConstructorArguments[0].ValueInternal as string;
if (string.IsNullOrWhiteSpace(diagnosticId))
{
diagnosticId = null;
}
string? urlFormat = null;
foreach (var (name, value) in this.CommonNamedArguments)
{
if (urlFormat is null && name == ObsoleteAttributeData.UrlFormatPropertyName && IsStringProperty(ObsoleteAttributeData.UrlFormatPropertyName))
{
urlFormat = value.ValueInternal as string;
}
if (urlFormat is not null)
{
break;
}
}
return new ObsoleteAttributeData(ObsoleteAttributeKind.Experimental, message: null, isError: false, diagnosticId, urlFormat);
}
/// <summary>
/// Decode the arguments to ObsoleteAttribute. ObsoleteAttribute can have 0, 1 or 2 arguments.
/// </summary>
private ObsoleteAttributeData DecodeObsoleteAttribute()
{
ImmutableArray<TypedConstant> args = this.CommonConstructorArguments;
// ObsoleteAttribute()
string? message = null;
bool isError = false;
if (args.Length > 0)
{
// ObsoleteAttribute(string)
// ObsoleteAttribute(string, bool)
Debug.Assert(args.Length <= 2);
message = (string?)args[0].ValueInternal;
if (args.Length == 2)
{
Debug.Assert(args[1].ValueInternal is object);
isError = (bool)args[1].ValueInternal!;
}
}
string? diagnosticId = null;
string? urlFormat = null;
foreach (var (name, value) in this.CommonNamedArguments)
{
if (diagnosticId is null && name == ObsoleteAttributeData.DiagnosticIdPropertyName && IsStringProperty(ObsoleteAttributeData.DiagnosticIdPropertyName))
{
diagnosticId = value.ValueInternal as string;
}
else if (urlFormat is null && name == ObsoleteAttributeData.UrlFormatPropertyName && IsStringProperty(ObsoleteAttributeData.UrlFormatPropertyName))
{
urlFormat = value.ValueInternal as string;
}
if (diagnosticId is object && urlFormat is object)
{
break;
}
}
return new ObsoleteAttributeData(ObsoleteAttributeKind.Obsolete, message, isError, diagnosticId, urlFormat);
}
// Note: it is disallowed to declare a property and a field
// with the same name in C# or VB source, even if it is allowed in IL.
//
// We use a virtual method and override to prevent having to realize the public symbols just to decode obsolete attributes.
// Ideally we would use an abstract method, but that would require making the method visible to
// public consumers who inherit from this class, which we don't want to do.
// Therefore we just make it a 'private protected virtual' method instead.
private protected virtual bool IsStringProperty(string memberName) => throw ExceptionUtilities.Unreachable();
/// <summary>
/// Decode the arguments to DeprecatedAttribute. DeprecatedAttribute can have 3 or 4 arguments.
/// </summary>
private ObsoleteAttributeData DecodeDeprecatedAttribute()
{
var args = this.CommonConstructorArguments;
// DeprecatedAttribute()
string? message = null;
bool isError = false;
if (args.Length == 3 || args.Length == 4)
{
// DeprecatedAttribute(String, DeprecationType, UInt32)
// DeprecatedAttribute(String, DeprecationType, UInt32, Platform)
// DeprecatedAttribute(String, DeprecationType, UInt32, String)
Debug.Assert(args[1].ValueInternal is object);
message = (string?)args[0].ValueInternal;
isError = ((int)args[1].ValueInternal! == 1);
}
return new ObsoleteAttributeData(ObsoleteAttributeKind.Deprecated, message, isError, diagnosticId: null, urlFormat: null);
}
/// <summary>
/// Decode the arguments to ExperimentalAttribute. ExperimentalAttribute has 0 arguments.
/// </summary>
private ObsoleteAttributeData DecodeWindowsExperimentalAttribute()
{
// ExperimentalAttribute()
Debug.Assert(this.CommonConstructorArguments.Length == 0);
return ObsoleteAttributeData.WindowsExperimental;
}
internal static void DecodeMethodImplAttribute<T, TAttributeSyntaxNode, TAttributeData, TAttributeLocation>(
ref DecodeWellKnownAttributeArguments<TAttributeSyntaxNode, TAttributeData, TAttributeLocation> arguments,
CommonMessageProvider messageProvider)
where T : CommonMethodWellKnownAttributeData, new()
where TAttributeSyntaxNode : SyntaxNode
where TAttributeData : AttributeData
{
Debug.Assert(arguments.AttributeSyntaxOpt is object);
MethodImplOptions options;
var attribute = arguments.Attribute;
if (attribute.CommonConstructorArguments.Length == 1)
{
Debug.Assert(attribute.AttributeConstructor is object);
if (attribute.AttributeConstructor.Parameters[0].Type.SpecialType == SpecialType.System_Int16)
{
options = (MethodImplOptions)attribute.CommonConstructorArguments[0].DecodeValue<short>(SpecialType.System_Int16);
}
else
{
options = attribute.CommonConstructorArguments[0].DecodeValue<MethodImplOptions>(SpecialType.System_Enum);
}
// low two bits should only be set via MethodCodeType property
if (((int)options & 3) != 0)
{
messageProvider.ReportInvalidAttributeArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, 0, attribute);
options = options & ~(MethodImplOptions)3;
}
}
else
{
options = default;
}
MethodImplAttributes codeType = MethodImplAttributes.IL;
int position = attribute.CommonConstructorArguments.Length;
foreach (var namedArg in attribute.CommonNamedArguments)
{
if (namedArg.Key == "MethodCodeType")
{
var value = (MethodImplAttributes)namedArg.Value.DecodeValue<int>(SpecialType.System_Enum);
if (value < 0 || value > MethodImplAttributes.Runtime)
{
Debug.Assert(attribute.AttributeClass is object);
messageProvider.ReportInvalidNamedArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position, attribute.AttributeClass, "MethodCodeType");
}
else
{
codeType = value;
}
}
position++;
}
arguments.GetOrCreateData<T>().SetMethodImplementation(arguments.Index, (MethodImplAttributes)options | codeType);
}
internal static void DecodeStructLayoutAttribute<TTypeWellKnownAttributeData, TAttributeSyntaxNode, TAttributeData, TAttributeLocation>(
ref DecodeWellKnownAttributeArguments<TAttributeSyntaxNode, TAttributeData, TAttributeLocation> arguments,
CharSet defaultCharSet,
int defaultAutoLayoutSize,
CommonMessageProvider messageProvider)
where TTypeWellKnownAttributeData : CommonTypeWellKnownAttributeData, new()
where TAttributeSyntaxNode : SyntaxNode
where TAttributeData : AttributeData
{
Debug.Assert(arguments.AttributeSyntaxOpt is object);
var attribute = arguments.Attribute;
Debug.Assert(attribute.AttributeClass is object);
CharSet charSet = (defaultCharSet != Cci.Constants.CharSet_None) ? defaultCharSet : CharSet.Ansi;
int? size = null;
int? alignment = null;
bool hasErrors = false;
LayoutKind kind = attribute.CommonConstructorArguments[0].DecodeValue<LayoutKind>(SpecialType.System_Enum);
switch (kind)
{
case LayoutKind.Auto:
case LayoutKind.Explicit:
case LayoutKind.Sequential:
break;
default:
messageProvider.ReportInvalidAttributeArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, 0, attribute);
hasErrors = true;
break;
}
int position = attribute.CommonConstructorArguments.Length;
foreach (var namedArg in attribute.CommonNamedArguments)
{
switch (namedArg.Key)
{
case "CharSet":
charSet = namedArg.Value.DecodeValue<CharSet>(SpecialType.System_Enum);
switch (charSet)
{
case Cci.Constants.CharSet_None:
charSet = CharSet.Ansi;
break;
case CharSet.Ansi:
case Cci.Constants.CharSet_Auto:
case CharSet.Unicode:
break;
default:
messageProvider.ReportInvalidNamedArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position, attribute.AttributeClass, namedArg.Key);
hasErrors = true;
break;
}
break;
case "Pack":
alignment = namedArg.Value.DecodeValue<int>(SpecialType.System_Int32);
// only powers of 2 less or equal to 128 are allowed:
if (alignment > 128 || (alignment & (alignment - 1)) != 0)
{
messageProvider.ReportInvalidNamedArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position, attribute.AttributeClass, namedArg.Key);
hasErrors = true;
}
break;
case "Size":
size = namedArg.Value.DecodeValue<int>(Microsoft.CodeAnalysis.SpecialType.System_Int32);
if (size < 0)
{
messageProvider.ReportInvalidNamedArgument(arguments.Diagnostics, arguments.AttributeSyntaxOpt, position, attribute.AttributeClass, namedArg.Key);
hasErrors = true;
}
break;
}
position++;
}
if (!hasErrors)
{
if (kind == LayoutKind.Auto && size == null && alignment != null)
{
// If size is unspecified
// C# emits size=0
// VB emits size=1 if the type is a struct, auto-layout and has alignment specified; 0 otherwise
size = defaultAutoLayoutSize;
}
arguments.GetOrCreateData<TTypeWellKnownAttributeData>().SetStructLayout(new TypeLayout(kind, size ?? 0, (byte)(alignment ?? 0)), charSet);
}
}
internal AttributeUsageInfo DecodeAttributeUsageAttribute()
{
return DecodeAttributeUsageAttribute(this.CommonConstructorArguments[0], this.CommonNamedArguments);
}
internal static AttributeUsageInfo DecodeAttributeUsageAttribute(TypedConstant positionalArg, ImmutableArray<KeyValuePair<string, TypedConstant>> namedArgs)
{
// BREAKING CHANGE (C#):
// If the well known attribute class System.AttributeUsage is overridden in source,
// we will use the overriding AttributeUsage type for attribute usage validation,
// i.e. we try to find a constructor in that type with signature AttributeUsage(AttributeTargets)
// and public bool properties Inherited and AllowMultiple.
// If we are unable to find any of these, we use their default values.
//
// This behavior matches the approach chosen by native VB and Roslyn VB compilers,
// but breaks compatibility with native C# compiler.
// Native C# compiler preloads all the well known attribute types from mscorlib prior to binding,
// hence it uses the AttributeUsage type defined in mscorlib for attribute usage validation.
//
// See Roslyn Bug 8603: ETA crashes with InvalidOperationException on duplicate attributes for details.
Debug.Assert(positionalArg.ValueInternal is object);
var validOn = (AttributeTargets)positionalArg.ValueInternal;
bool allowMultiple = DecodeNamedArgument(namedArgs, "AllowMultiple", SpecialType.System_Boolean, false);
bool inherited = DecodeNamedArgument(namedArgs, "Inherited", SpecialType.System_Boolean, true);
return new AttributeUsageInfo(validOn, allowMultiple, inherited);
}
}
}