-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathSynthesizedMethodBaseSymbol.cs
More file actions
222 lines (184 loc) · 9.79 KB
/
SynthesizedMethodBaseSymbol.cs
File metadata and controls
222 lines (184 loc) · 9.79 KB
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
// 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.
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Microsoft.Cci;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.CSharp.Emit;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
/// <summary>
/// A base method symbol used as a base class for lambda method symbol and base method wrapper symbol.
/// </summary>
internal abstract class SynthesizedMethodBaseSymbol : SourceMemberMethodSymbol
{
protected readonly MethodSymbol BaseMethod;
internal TypeMap TypeMap { get; private set; }
private readonly string _name;
private ImmutableArray<TypeParameterSymbol> _typeParameters;
private ImmutableArray<ParameterSymbol> _parameters;
protected SynthesizedMethodBaseSymbol(NamedTypeSymbol containingType,
MethodSymbol baseMethod,
SyntaxReference syntaxReference,
Location location,
string name,
DeclarationModifiers declarationModifiers,
bool isIterator)
: base(containingType, syntaxReference, location, isIterator,
(declarationModifiers, MakeFlags(
methodKind: MethodKind.Ordinary,
refKind: baseMethod.RefKind,
declarationModifiers,
returnsVoid: baseMethod.ReturnsVoid,
returnsVoidIsSet: true,
isExtensionMethod: false,
isNullableAnalysisEnabled: false,
isVarArg: baseMethod.IsVararg,
isExpressionBodied: false,
isExplicitInterfaceImplementation: false,
hasThisInitializer: false)))
{
Debug.Assert((object)containingType != null);
Debug.Assert((object)baseMethod != null);
this.BaseMethod = baseMethod;
_name = name;
}
protected void AssignTypeMapAndTypeParameters(TypeMap typeMap, ImmutableArray<TypeParameterSymbol> typeParameters)
{
Debug.Assert(typeMap != null);
Debug.Assert(this.TypeMap == null);
Debug.Assert(!typeParameters.IsDefault);
Debug.Assert(_typeParameters.IsDefault);
this.TypeMap = typeMap;
_typeParameters = typeParameters;
}
protected override void MethodChecks(BindingDiagnosticBag diagnostics)
{
// TODO: move more functionality into here, making these symbols more lazy
}
public sealed override ImmutableArray<TypeParameterSymbol> TypeParameters
{
get { return _typeParameters; }
}
public sealed override ImmutableArray<ImmutableArray<TypeWithAnnotations>> GetTypeParameterConstraintTypes()
=> ImmutableArray<ImmutableArray<TypeWithAnnotations>>.Empty;
public sealed override ImmutableArray<TypeParameterConstraintKind> GetTypeParameterConstraintKinds()
=> ImmutableArray<TypeParameterConstraintKind>.Empty;
internal override int ParameterCount
{
get { return this.Parameters.Length; }
}
public sealed override ImmutableArray<ParameterSymbol> Parameters
{
get
{
if (_parameters.IsDefault)
{
ImmutableInterlocked.InterlockedInitialize(ref _parameters, MakeParameters());
}
return _parameters;
}
}
protected virtual ImmutableArray<TypeSymbol> ExtraSynthesizedRefParameters
{
get { return default(ImmutableArray<TypeSymbol>); }
}
protected virtual ImmutableArray<ParameterSymbol> BaseMethodParameters
{
get { return this.BaseMethod.Parameters; }
}
private ImmutableArray<ParameterSymbol> MakeParameters()
{
int ordinal = 0;
var builder = ArrayBuilder<ParameterSymbol>.GetInstance();
var parameters = this.BaseMethodParameters;
var inheritAttributes = InheritsBaseMethodAttributes;
foreach (var p in parameters)
{
builder.Add(SynthesizedParameterSymbol.Create(
this,
this.TypeMap.SubstituteType(p.OriginalDefinition.TypeWithAnnotations),
ordinal++,
p.RefKind,
p.Name,
p.EffectiveScope,
p.ExplicitDefaultConstantValue,
// the synthesized parameter doesn't need to have the same ref custom modifiers as the base
refCustomModifiers: default,
baseParameterForAttributes: inheritAttributes ? p : null,
isParams: this is SynthesizedClosureMethod && p.IsParams));
}
var extraSynthed = ExtraSynthesizedRefParameters;
if (!extraSynthed.IsDefaultOrEmpty)
{
foreach (var extra in extraSynthed)
{
builder.Add(SynthesizedParameterSymbol.Create(this, this.TypeMap.SubstituteType(extra), ordinal++, RefKind.Ref));
}
}
return builder.ToImmutableAndFree();
}
/// <summary>
/// Indicates that this method inherits attributes from the base method, its parameters, return type, and type parameters.
/// </summary>
internal virtual bool InheritsBaseMethodAttributes => false;
public sealed override ImmutableArray<CSharpAttributeData> GetAttributes()
{
Debug.Assert(base.GetAttributes().IsEmpty);
return InheritsBaseMethodAttributes
? BaseMethod.GetAttributes()
: ImmutableArray<CSharpAttributeData>.Empty;
}
public sealed override ImmutableArray<CSharpAttributeData> GetReturnTypeAttributes()
{
Debug.Assert(base.GetReturnTypeAttributes().IsEmpty);
return InheritsBaseMethodAttributes ? BaseMethod.GetReturnTypeAttributes() : ImmutableArray<CSharpAttributeData>.Empty;
}
#nullable enable
public sealed override DllImportData? GetDllImportData() => InheritsBaseMethodAttributes ? BaseMethod.GetDllImportData() : null;
internal sealed override MethodImplAttributes ImplementationAttributes => InheritsBaseMethodAttributes ? BaseMethod.ImplementationAttributes : default;
internal sealed override MarshalPseudoCustomAttributeData? ReturnValueMarshallingInformation => InheritsBaseMethodAttributes ? BaseMethod.ReturnValueMarshallingInformation : null;
internal sealed override bool HasSpecialName => InheritsBaseMethodAttributes && BaseMethod.HasSpecialName;
// Synthesized methods created from a base method with [SkipLocalsInitAttribute] will also
// skip locals init where applicable, even if the synthesized method does not inherit attributes.
// Note that this doesn't affect BaseMethodWrapperSymbol for example because the implementation has no locals.
public sealed override bool AreLocalsZeroed => !(BaseMethod is SourceMethodSymbol sourceMethod) || sourceMethod.AreLocalsZeroed;
internal sealed override bool RequiresSecurityObject => InheritsBaseMethodAttributes && BaseMethod.RequiresSecurityObject;
internal sealed override bool HasDeclarativeSecurity => InheritsBaseMethodAttributes && BaseMethod.HasDeclarativeSecurity;
internal sealed override IEnumerable<SecurityAttribute> GetSecurityInformation() => InheritsBaseMethodAttributes
? BaseMethod.GetSecurityInformation()
: SpecializedCollections.EmptyEnumerable<SecurityAttribute>();
#nullable disable
public sealed override TypeWithAnnotations ReturnTypeWithAnnotations
{
get { return this.TypeMap.SubstituteType(this.BaseMethod.OriginalDefinition.ReturnTypeWithAnnotations); }
}
public sealed override ImmutableArray<CustomModifier> RefCustomModifiers
{
get { return this.TypeMap.SubstituteCustomModifiers(this.BaseMethod.OriginalDefinition.RefCustomModifiers); }
}
public sealed override FlowAnalysisAnnotations ReturnTypeFlowAnalysisAnnotations => BaseMethod.ReturnTypeFlowAnalysisAnnotations;
public sealed override ImmutableHashSet<string> ReturnNotNullIfParameterNotNull => BaseMethod.ReturnNotNullIfParameterNotNull;
public sealed override FlowAnalysisAnnotations FlowAnalysisAnnotations => FlowAnalysisAnnotations.None;
public sealed override string Name
{
get { return _name; }
}
public sealed override bool IsImplicitlyDeclared
{
get { return true; }
}
internal sealed override ThreeState RuntimeAsyncMethodGenerationAttributeSetting =>
InheritsBaseMethodAttributes
? BaseMethod.RuntimeAsyncMethodGenerationAttributeSetting
: ThreeState.Unknown;
}
}