forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceConstructorSymbol.cs
334 lines (278 loc) · 15.7 KB
/
SourceConstructorSymbol.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
// 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.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal sealed class SourceConstructorSymbol : SourceConstructorSymbolBase
{
private SourceConstructorSymbol? _otherPartOfPartial;
#nullable disable
public static SourceConstructorSymbol CreateConstructorSymbol(
SourceMemberContainerTypeSymbol containingType,
ConstructorDeclarationSyntax syntax,
bool isNullableAnalysisEnabled,
BindingDiagnosticBag diagnostics)
{
var methodKind = syntax.Modifiers.Any(SyntaxKind.StaticKeyword) ? MethodKind.StaticConstructor : MethodKind.Constructor;
return new SourceConstructorSymbol(containingType, syntax.Identifier.GetLocation(), syntax, methodKind, isNullableAnalysisEnabled, diagnostics);
}
private SourceConstructorSymbol(
SourceMemberContainerTypeSymbol containingType,
Location location,
ConstructorDeclarationSyntax syntax,
MethodKind methodKind,
bool isNullableAnalysisEnabled,
BindingDiagnosticBag diagnostics) :
base(containingType, location, syntax, SyntaxFacts.HasYieldOperations(syntax),
MakeModifiersAndFlags(
containingType, syntax, methodKind, isNullableAnalysisEnabled, syntax.Initializer?.Kind() == SyntaxKind.ThisConstructorInitializer, location, diagnostics, out bool modifierErrors, out bool report_ERR_StaticConstructorWithAccessModifiers))
{
this.CheckUnsafeModifier(DeclarationModifiers, diagnostics);
if (report_ERR_StaticConstructorWithAccessModifiers)
{
diagnostics.Add(ErrorCode.ERR_StaticConstructorWithAccessModifiers, location, this);
}
if (syntax.Identifier.ValueText != containingType.Name)
{
// This is probably a method declaration with the type missing.
diagnostics.Add(ErrorCode.ERR_MemberNeedsType, location);
}
bool hasAnyBody = syntax.HasAnyBody();
if (IsExtern)
{
if (methodKind == MethodKind.Constructor && syntax.Initializer != null)
{
diagnostics.Add(ErrorCode.ERR_ExternHasConstructorInitializer, location, this);
}
if (hasAnyBody)
{
diagnostics.Add(ErrorCode.ERR_ExternHasBody, location, this);
}
}
if (IsPartialDefinition && syntax.Initializer is { } initializer)
{
diagnostics.Add(ErrorCode.ERR_PartialConstructorInitializer, initializer, this);
}
if (methodKind == MethodKind.StaticConstructor)
{
CheckFeatureAvailabilityAndRuntimeSupport(syntax, location, hasAnyBody, diagnostics);
}
ModifierUtils.CheckAccessibility(this.DeclarationModifiers, this, isExplicitInterfaceImplementation: false, diagnostics, location);
if (!modifierErrors)
{
this.CheckModifiers(methodKind, hasAnyBody, location, diagnostics);
}
CheckForBlockAndExpressionBody(
syntax.Body, syntax.ExpressionBody, syntax, diagnostics);
}
private static (DeclarationModifiers, Flags) MakeModifiersAndFlags(
NamedTypeSymbol containingType,
ConstructorDeclarationSyntax syntax,
MethodKind methodKind,
bool isNullableAnalysisEnabled,
bool hasThisInitializer,
Location location,
BindingDiagnosticBag diagnostics,
out bool modifierErrors,
out bool report_ERR_StaticConstructorWithAccessModifiers)
{
bool hasAnyBody = syntax.HasAnyBody();
DeclarationModifiers declarationModifiers = MakeModifiers(containingType, syntax, methodKind, hasAnyBody, location, diagnostics, out modifierErrors, out bool hasExplicitAccessModifier, out report_ERR_StaticConstructorWithAccessModifiers);
Flags flags = new Flags(
methodKind, RefKind.None, declarationModifiers, returnsVoid: true, returnsVoidIsSet: true, hasAnyBody: hasAnyBody,
isExpressionBodied: syntax.IsExpressionBodied(), isExtensionMethod: false, isVararg: syntax.IsVarArg(),
isNullableAnalysisEnabled: isNullableAnalysisEnabled, isExplicitInterfaceImplementation: false,
hasThisInitializer: hasThisInitializer, hasExplicitAccessModifier: hasExplicitAccessModifier);
return (declarationModifiers, flags);
}
internal ConstructorDeclarationSyntax GetSyntax()
{
Debug.Assert(syntaxReferenceOpt != null);
return (ConstructorDeclarationSyntax)syntaxReferenceOpt.GetSyntax();
}
internal override ExecutableCodeBinder TryGetBodyBinder(BinderFactory binderFactoryOpt = null, bool ignoreAccessibility = false)
{
return TryGetBodyBinderFromSyntax(binderFactoryOpt, ignoreAccessibility);
}
protected override ParameterListSyntax GetParameterList()
{
return GetSyntax().ParameterList;
}
protected override CSharpSyntaxNode GetInitializer()
{
return GetSyntax().Initializer;
}
private static DeclarationModifiers MakeModifiers(
NamedTypeSymbol containingType, ConstructorDeclarationSyntax syntax, MethodKind methodKind, bool hasBody, Location location, BindingDiagnosticBag diagnostics,
out bool modifierErrors, out bool hasExplicitAccessModifier, out bool report_ERR_StaticConstructorWithAccessModifiers)
{
var defaultAccess = (methodKind == MethodKind.StaticConstructor) ? DeclarationModifiers.None : DeclarationModifiers.Private;
// Check that the set of modifiers is allowed
DeclarationModifiers allowedModifiers =
DeclarationModifiers.AccessibilityMask |
DeclarationModifiers.Static |
DeclarationModifiers.Extern |
DeclarationModifiers.Unsafe;
if (methodKind == MethodKind.Constructor)
{
allowedModifiers |= DeclarationModifiers.Partial;
}
bool isInterface = containingType.IsInterface;
var mods = ModifierUtils.MakeAndCheckNonTypeMemberModifiers(isOrdinaryMethod: false, isForInterfaceMember: isInterface, syntax.Modifiers, defaultAccess, allowedModifiers, location, diagnostics, out modifierErrors, out hasExplicitAccessModifier);
report_ERR_StaticConstructorWithAccessModifiers = false;
if (methodKind == MethodKind.StaticConstructor)
{
// Don't report ERR_StaticConstructorWithAccessModifiers if the ctor symbol name doesn't match the containing type name.
// This avoids extra unnecessary errors.
// There will already be a diagnostic saying Method must have a return type.
if ((mods & DeclarationModifiers.AccessibilityMask) != 0 &&
containingType.Name == syntax.Identifier.ValueText)
{
mods = mods & ~DeclarationModifiers.AccessibilityMask;
report_ERR_StaticConstructorWithAccessModifiers = true;
modifierErrors = true;
}
mods |= DeclarationModifiers.Private; // we mark static constructors private in the symbol table
if (isInterface)
{
ModifierUtils.ReportDefaultInterfaceImplementationModifiers(hasBody, mods,
DeclarationModifiers.Extern,
location, diagnostics);
}
}
return mods;
}
private void CheckModifiers(MethodKind methodKind, bool hasBody, Location location, BindingDiagnosticBag diagnostics)
{
if (!hasBody && !IsExtern && !IsPartial)
{
diagnostics.Add(ErrorCode.ERR_ConcreteMissingBody, location, this);
}
else if (ContainingType.IsSealed && this.DeclaredAccessibility.HasProtected() && !this.IsOverride)
{
diagnostics.Add(AccessCheck.GetProtectedMemberInSealedTypeError(ContainingType), location, this);
}
else if (ContainingType.IsStatic && methodKind == MethodKind.Constructor)
{
diagnostics.Add(ErrorCode.ERR_ConstructorInStaticClass, location);
}
else if (IsPartial && !ContainingType.IsPartial())
{
diagnostics.Add(ErrorCode.ERR_PartialMemberOnlyInPartialClass, location);
}
}
internal override OneOrMany<SyntaxList<AttributeListSyntax>> GetAttributeDeclarations()
{
return OneOrMany.Create(((ConstructorDeclarationSyntax)this.SyntaxNode).AttributeLists);
}
internal override bool IsNullableAnalysisEnabled()
=> flags.HasThisInitializer
? flags.IsNullableAnalysisEnabled
: ((SourceMemberContainerTypeSymbol)ContainingType).IsNullableEnabledForConstructorsAndInitializers(IsStatic);
protected override bool AllowRefOrOut
{
get
{
return true;
}
}
protected override bool IsWithinExpressionOrBlockBody(int position, out int offset)
{
ConstructorDeclarationSyntax ctorSyntax = GetSyntax();
if (ctorSyntax.Body?.Span.Contains(position) == true)
{
offset = position - ctorSyntax.Body.Span.Start;
return true;
}
else if (ctorSyntax.ExpressionBody?.Span.Contains(position) == true)
{
offset = position - ctorSyntax.ExpressionBody.Span.Start;
return true;
}
offset = -1;
return false;
}
#nullable enable
internal sealed override void ForceComplete(SourceLocation? locationOpt, Predicate<Symbol>? filter, CancellationToken cancellationToken)
{
SourcePartialImplementationPart?.ForceComplete(locationOpt, filter, cancellationToken);
base.ForceComplete(locationOpt, filter, cancellationToken);
}
protected override void PartialConstructorChecks(BindingDiagnosticBag diagnostics)
{
if (SourcePartialImplementationPart is { } implementation)
{
PartialConstructorChecks(implementation, diagnostics);
}
}
private void PartialConstructorChecks(SourceConstructorSymbol implementation, BindingDiagnosticBag diagnostics)
{
Debug.Assert(this.IsPartialDefinition);
Debug.Assert(!ReferenceEquals(this, implementation));
Debug.Assert(ReferenceEquals(this.OtherPartOfPartial, implementation));
if (MemberSignatureComparer.ConsideringTupleNamesCreatesDifference(this, implementation))
{
diagnostics.Add(ErrorCode.ERR_PartialMemberInconsistentTupleNames, implementation.GetFirstLocation(), this, implementation);
}
else if (!MemberSignatureComparer.PartialMethodsStrictComparer.Equals(this, implementation)
|| !Parameters.SequenceEqual(implementation.Parameters, static (a, b) => a.Name == b.Name))
{
diagnostics.Add(ErrorCode.WRN_PartialMemberSignatureDifference, implementation.GetFirstLocation(),
new FormattedSymbol(this, SymbolDisplayFormat.MinimallyQualifiedFormat),
new FormattedSymbol(implementation, SymbolDisplayFormat.MinimallyQualifiedFormat));
}
if (IsUnsafe != implementation.IsUnsafe && this.CompilationAllowsUnsafe())
{
diagnostics.Add(ErrorCode.ERR_PartialMemberUnsafeDifference, implementation.GetFirstLocation());
}
if (this.IsParams() != implementation.IsParams())
{
diagnostics.Add(ErrorCode.ERR_PartialMemberParamsDifference, implementation.GetFirstLocation());
}
if (DeclaredAccessibility != implementation.DeclaredAccessibility
|| HasExplicitAccessModifier != implementation.HasExplicitAccessModifier)
{
diagnostics.Add(ErrorCode.ERR_PartialMemberAccessibilityDifference, implementation.GetFirstLocation());
}
Debug.Assert(this.ParameterCount == implementation.ParameterCount);
for (var i = 0; i < this.ParameterCount; i++)
{
// An error is only reported for a modifier difference here, regardless of whether the difference is safe or not.
// Presence of UnscopedRefAttribute is also not considered when checking partial signatures, because when the attribute is used, it will affect both parts the same way.
var definitionParameter = (SourceParameterSymbol)this.Parameters[i];
var implementationParameter = (SourceParameterSymbol)implementation.Parameters[i];
if (definitionParameter.DeclaredScope != implementationParameter.DeclaredScope)
{
diagnostics.Add(ErrorCode.ERR_ScopedMismatchInParameterOfPartial, implementation.GetFirstLocation(), new FormattedSymbol(implementation.Parameters[i], SymbolDisplayFormat.ShortFormat));
}
}
}
public sealed override bool IsExtern => PartialImplementationPart is { } implementation ? implementation.IsExtern : HasExternModifier;
private bool HasAnyBody => flags.HasAnyBody;
private bool HasExplicitAccessModifier => flags.HasExplicitAccessModifier;
internal bool IsPartialDefinition => IsPartial && !HasAnyBody && !HasExternModifier;
internal bool IsPartialImplementation => IsPartial && (HasAnyBody || HasExternModifier);
internal SourceConstructorSymbol? OtherPartOfPartial => _otherPartOfPartial;
internal SourceConstructorSymbol? SourcePartialDefinitionPart => IsPartialImplementation ? OtherPartOfPartial : null;
internal SourceConstructorSymbol? SourcePartialImplementationPart => IsPartialDefinition ? OtherPartOfPartial : null;
public override MethodSymbol? PartialDefinitionPart => SourcePartialDefinitionPart;
public override MethodSymbol? PartialImplementationPart => SourcePartialImplementationPart;
internal static void InitializePartialConstructorParts(SourceConstructorSymbol definition, SourceConstructorSymbol implementation)
{
Debug.Assert(definition.IsPartialDefinition);
Debug.Assert(implementation.IsPartialImplementation);
Debug.Assert(definition._otherPartOfPartial is not { } alreadySetImplPart || ReferenceEquals(alreadySetImplPart, implementation));
Debug.Assert(implementation._otherPartOfPartial is not { } alreadySetDefPart || ReferenceEquals(alreadySetDefPart, definition));
definition._otherPartOfPartial = implementation;
implementation._otherPartOfPartial = definition;
Debug.Assert(ReferenceEquals(definition._otherPartOfPartial, implementation));
Debug.Assert(ReferenceEquals(implementation._otherPartOfPartial, definition));
}
}
}