-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
SynthesizedSimpleProgramEntryPointSymbol.cs
300 lines (252 loc) · 12.5 KB
/
SynthesizedSimpleProgramEntryPointSymbol.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
// 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.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal sealed class SynthesizedSimpleProgramEntryPointSymbol : SourceMemberMethodSymbol
{
/// <summary>
/// The corresponding <see cref="SingleTypeDeclaration"/>.
/// </summary>
private readonly SingleTypeDeclaration _declaration;
private readonly TypeSymbol _returnType;
private readonly ImmutableArray<ParameterSymbol> _parameters;
private WeakReference<ExecutableCodeBinder>? _weakBodyBinder;
private WeakReference<ExecutableCodeBinder>? _weakIgnoreAccessibilityBodyBinder;
internal SynthesizedSimpleProgramEntryPointSymbol(SourceMemberContainerTypeSymbol containingType, SingleTypeDeclaration declaration, BindingDiagnosticBag diagnostics)
: base(containingType, syntaxReferenceOpt: declaration.SyntaxReference, declaration.SyntaxReference.GetLocation(), isIterator: declaration.IsIterator,
MakeModifiersAndFlags(containingType, declaration))
{
Debug.Assert(declaration.SyntaxReference.GetSyntax() is CompilationUnitSyntax);
_declaration = declaration;
bool hasAwait = declaration.HasAwaitExpressions;
bool hasReturnWithExpression = declaration.HasReturnWithExpression;
CSharpCompilation compilation = containingType.DeclaringCompilation;
switch (hasAwait, hasReturnWithExpression)
{
case (true, false):
_returnType = Binder.GetWellKnownType(compilation, WellKnownType.System_Threading_Tasks_Task, diagnostics, NoLocation.Singleton);
break;
case (false, false):
_returnType = Binder.GetSpecialType(compilation, SpecialType.System_Void, NoLocation.Singleton, diagnostics);
break;
case (true, true):
_returnType = Binder.GetWellKnownType(compilation, WellKnownType.System_Threading_Tasks_Task_T, diagnostics, NoLocation.Singleton).
Construct(Binder.GetSpecialType(compilation, SpecialType.System_Int32, NoLocation.Singleton, diagnostics));
break;
case (false, true):
_returnType = Binder.GetSpecialType(compilation, SpecialType.System_Int32, NoLocation.Singleton, diagnostics);
break;
}
_parameters = ImmutableArray.Create(SynthesizedParameterSymbol.Create(this,
TypeWithAnnotations.Create(
ArrayTypeSymbol.CreateCSharpArray(compilation.Assembly,
TypeWithAnnotations.Create(Binder.GetSpecialType(compilation, SpecialType.System_String, NoLocation.Singleton, diagnostics)))), 0, RefKind.None, "args"));
}
private static (DeclarationModifiers, Flags) MakeModifiersAndFlags(SourceMemberContainerTypeSymbol containingType, SingleTypeDeclaration declaration)
{
bool hasAwait = declaration.HasAwaitExpressions;
bool hasReturnWithExpression = declaration.HasReturnWithExpression;
DeclarationModifiers declarationModifiers = DeclarationModifiers.Static | DeclarationModifiers.Private | (hasAwait ? DeclarationModifiers.Async : DeclarationModifiers.None);
CSharpCompilation compilation = containingType.DeclaringCompilation;
var compilationUnit = (CompilationUnitSyntax)declaration.SyntaxReference.GetSyntax();
bool isNullableAnalysisEnabled = IsNullableAnalysisEnabled(compilation, compilationUnit);
Flags flags = MakeFlags(
MethodKind.Ordinary,
RefKind.None,
declarationModifiers,
returnsVoid: !hasAwait && !hasReturnWithExpression,
returnsVoidIsSet: true,
isExpressionBodied: false,
isExtensionMethod: false,
isNullableAnalysisEnabled: isNullableAnalysisEnabled,
isVarArg: false,
isExplicitInterfaceImplementation: false,
hasThisInitializer: false);
return (declarationModifiers, flags);
}
internal static SynthesizedSimpleProgramEntryPointSymbol? GetSimpleProgramEntryPoint(CSharpCompilation compilation, CompilationUnitSyntax compilationUnit, bool fallbackToMainEntryPoint)
{
var type = GetSimpleProgramNamedTypeSymbol(compilation);
if (type is null)
{
return null;
}
ImmutableArray<SynthesizedSimpleProgramEntryPointSymbol> entryPoints = type.GetSimpleProgramEntryPoints();
foreach (var entryPoint in entryPoints)
{
if (entryPoint.SyntaxTree == compilationUnit.SyntaxTree && entryPoint.SyntaxNode == compilationUnit)
{
return entryPoint;
}
}
return fallbackToMainEntryPoint ? entryPoints[0] : null;
}
internal static SynthesizedSimpleProgramEntryPointSymbol? GetSimpleProgramEntryPoint(CSharpCompilation compilation)
{
return GetSimpleProgramNamedTypeSymbol(compilation)?.GetSimpleProgramEntryPoints().First();
}
private static SourceNamedTypeSymbol? GetSimpleProgramNamedTypeSymbol(CSharpCompilation compilation)
{
return compilation.SourceModule.GlobalNamespace.GetTypeMembers(WellKnownMemberNames.TopLevelStatementsEntryPointTypeName).OfType<SourceNamedTypeSymbol>().SingleOrDefault(s => s.IsSimpleProgram);
}
public override string Name
{
get
{
return WellKnownMemberNames.TopLevelStatementsEntryPointMethodName;
}
}
internal override System.Reflection.MethodImplAttributes ImplementationAttributes
{
get { return default(System.Reflection.MethodImplAttributes); }
}
public override ImmutableArray<TypeParameterSymbol> TypeParameters
{
get
{
return ImmutableArray<TypeParameterSymbol>.Empty;
}
}
internal override int ParameterCount
{
get
{
return 1;
}
}
public override ImmutableArray<ParameterSymbol> Parameters
{
get
{
return _parameters;
}
}
public override TypeWithAnnotations ReturnTypeWithAnnotations
{
get
{
return TypeWithAnnotations.Create(_returnType);
}
}
public override FlowAnalysisAnnotations ReturnTypeFlowAnalysisAnnotations => FlowAnalysisAnnotations.None;
public override ImmutableHashSet<string> ReturnNotNullIfParameterNotNull => ImmutableHashSet<string>.Empty;
public override FlowAnalysisAnnotations FlowAnalysisAnnotations => FlowAnalysisAnnotations.None;
public override ImmutableArray<CustomModifier> RefCustomModifiers
{
get
{
return ImmutableArray<CustomModifier>.Empty;
}
}
public sealed override bool IsImplicitlyDeclared
{
get
{
return false;
}
}
internal sealed override bool GenerateDebugInfo
{
get
{
return true;
}
}
internal override int CalculateLocalSyntaxOffset(int localPosition, SyntaxTree localTree)
{
return localPosition;
}
protected override void MethodChecks(BindingDiagnosticBag diagnostics)
{
}
public override ImmutableArray<ImmutableArray<TypeWithAnnotations>> GetTypeParameterConstraintTypes()
=> ImmutableArray<ImmutableArray<TypeWithAnnotations>>.Empty;
public override ImmutableArray<TypeParameterConstraintKind> GetTypeParameterConstraintKinds()
=> ImmutableArray<TypeParameterConstraintKind>.Empty;
protected override object MethodChecksLockObject => _declaration;
internal CompilationUnitSyntax CompilationUnit => (CompilationUnitSyntax)SyntaxNode;
internal override ExecutableCodeBinder TryGetBodyBinder(BinderFactory? binderFactoryOpt = null, bool ignoreAccessibility = false)
{
return GetBodyBinder(ignoreAccessibility);
}
private ExecutableCodeBinder CreateBodyBinder(bool ignoreAccessibility)
{
CSharpCompilation compilation = DeclaringCompilation;
var syntaxNode = SyntaxNode;
Binder result = new BuckStopsHereBinder(compilation, FileIdentifier.Create(syntaxNode.SyntaxTree, compilation.Options.SourceReferenceResolver));
var globalNamespace = compilation.GlobalNamespace;
var declaringSymbol = (SourceNamespaceSymbol)compilation.SourceModule.GlobalNamespace;
result = WithExternAndUsingAliasesBinder.Create(declaringSymbol, syntaxNode, WithUsingNamespacesAndTypesBinder.Create(declaringSymbol, syntaxNode, result));
result = new InContainerBinder(globalNamespace, result);
result = new InContainerBinder(ContainingType, result);
result = new InMethodBinder(this, result);
result = result.WithAdditionalFlags(ignoreAccessibility ? BinderFlags.IgnoreAccessibility : BinderFlags.None);
return new ExecutableCodeBinder(syntaxNode, this, result);
}
internal ExecutableCodeBinder GetBodyBinder(bool ignoreAccessibility)
{
ref WeakReference<ExecutableCodeBinder>? weakBinder = ref ignoreAccessibility ? ref _weakIgnoreAccessibilityBodyBinder : ref _weakBodyBinder;
while (true)
{
var previousWeakReference = weakBinder;
if (previousWeakReference != null && previousWeakReference.TryGetTarget(out ExecutableCodeBinder? previousBinder))
{
return previousBinder;
}
ExecutableCodeBinder newBinder = CreateBodyBinder(ignoreAccessibility);
if (Interlocked.CompareExchange(ref weakBinder, new WeakReference<ExecutableCodeBinder>(newBinder), previousWeakReference) == previousWeakReference)
{
return newBinder;
}
}
}
public override bool IsDefinedInSourceTree(SyntaxTree tree, TextSpan? definedWithinSpan, CancellationToken cancellationToken)
{
if (_declaration.SyntaxReference.SyntaxTree == tree)
{
if (!definedWithinSpan.HasValue)
{
return true;
}
else
{
var span = definedWithinSpan.GetValueOrDefault();
foreach (var global in ((CompilationUnitSyntax)tree.GetRoot(cancellationToken)).Members.OfType<GlobalStatementSyntax>())
{
cancellationToken.ThrowIfCancellationRequested();
if (global.Span.IntersectsWith(span))
{
return true;
}
}
}
}
return false;
}
public SyntaxNode ReturnTypeSyntax => CompilationUnit.Members.First(m => m.Kind() == SyntaxKind.GlobalStatement);
private static bool IsNullableAnalysisEnabled(CSharpCompilation compilation, CompilationUnitSyntax syntax)
{
foreach (var member in syntax.Members)
{
if (member.Kind() == SyntaxKind.GlobalStatement && compilation.IsNullableAnalysisEnabledIn(member))
{
return true;
}
}
return false;
}
internal sealed override int TryGetOverloadResolutionPriority()
{
return 0;
}
}
}