-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathDllImportStubContext.cs
292 lines (251 loc) · 13 KB
/
DllImportStubContext.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Interop
{
internal record StubEnvironment(
Compilation Compilation,
TargetFramework TargetFramework,
Version TargetFrameworkVersion,
bool ModuleSkipLocalsInit,
DllImportGeneratorOptions Options)
{
/// <summary>
/// Override for determining if two StubEnvironment instances are
/// equal. This intentionally excludes the Compilation instance
/// since that represents the actual compilation and not just the settings.
/// </summary>
/// <param name="env1">The first StubEnvironment</param>
/// <param name="env2">The second StubEnvironment</param>
/// <returns>True if the settings are equal, otherwise false.</returns>
public static bool AreCompilationSettingsEqual(StubEnvironment env1, StubEnvironment env2)
{
return env1.TargetFramework == env2.TargetFramework
&& env1.TargetFrameworkVersion == env2.TargetFrameworkVersion
&& env1.ModuleSkipLocalsInit == env2.ModuleSkipLocalsInit
&& env1.Options.Equals(env2.Options);
}
}
internal sealed class DllImportStubContext : IEquatable<DllImportStubContext>
{
// We don't need the warnings around not setting the various
// non-nullable fields/properties on this type in the constructor
// since we always use a property initializer.
#pragma warning disable 8618
private DllImportStubContext()
{
}
#pragma warning restore
public ImmutableArray<TypePositionInfo> ElementTypeInformation { get; init; }
public string? StubTypeNamespace { get; init; }
public ImmutableArray<TypeDeclarationSyntax> StubContainingTypes { get; init; }
public TypeSyntax StubReturnType { get; init; }
public IEnumerable<ParameterSyntax> StubParameters
{
get
{
foreach (TypePositionInfo typeInfo in ElementTypeInformation)
{
if (typeInfo.ManagedIndex != TypePositionInfo.UnsetIndex
&& typeInfo.ManagedIndex != TypePositionInfo.ReturnIndex)
{
yield return Parameter(Identifier(typeInfo.InstanceIdentifier))
.WithType(typeInfo.ManagedType.Syntax)
.WithModifiers(TokenList(Token(typeInfo.RefKindSyntax)));
}
}
}
}
public ImmutableArray<AttributeListSyntax> AdditionalAttributes { get; init; }
public DllImportGeneratorOptions Options { get; init; }
public IMarshallingGeneratorFactory GeneratorFactory { get; init; }
public static DllImportStubContext Create(
IMethodSymbol method,
GeneratedDllImportData dllImportData,
StubEnvironment env,
GeneratorDiagnostics diagnostics,
CancellationToken token)
{
// Cancel early if requested
token.ThrowIfCancellationRequested();
// Determine the namespace
string? stubTypeNamespace = null;
if (!(method.ContainingNamespace is null)
&& !method.ContainingNamespace.IsGlobalNamespace)
{
stubTypeNamespace = method.ContainingNamespace.ToString();
}
// Determine containing type(s)
ImmutableArray<TypeDeclarationSyntax>.Builder containingTypes = ImmutableArray.CreateBuilder<TypeDeclarationSyntax>();
INamedTypeSymbol currType = method.ContainingType;
while (!(currType is null))
{
// Use the declaring syntax as a basis for this type declaration.
// Since we're generating source for the method, we know that the current type
// has to be declared in source.
TypeDeclarationSyntax typeDecl = (TypeDeclarationSyntax)currType.DeclaringSyntaxReferences[0].GetSyntax(token);
// Remove current members, attributes, and base list so we don't double declare them.
typeDecl = typeDecl.WithMembers(List<MemberDeclarationSyntax>())
.WithAttributeLists(List<AttributeListSyntax>())
.WithBaseList(null);
containingTypes.Add(typeDecl);
currType = currType.ContainingType;
}
(ImmutableArray<TypePositionInfo> typeInfos, IMarshallingGeneratorFactory generatorFactory) = GenerateTypeInformation(method, dllImportData, diagnostics, env);
ImmutableArray<AttributeListSyntax>.Builder additionalAttrs = ImmutableArray.CreateBuilder<AttributeListSyntax>();
// Define additional attributes for the stub definition.
if (env.TargetFrameworkVersion >= new Version(5, 0) && !MethodIsSkipLocalsInit(env, method))
{
additionalAttrs.Add(
AttributeList(
SeparatedList(new[]
{
// Adding the skip locals init indiscriminately since the source generator is
// targeted at non-blittable method signatures which typically will contain locals
// in the generated code.
Attribute(ParseName(TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute))
})));
}
return new DllImportStubContext()
{
StubReturnType = method.ReturnType.AsTypeSyntax(),
ElementTypeInformation = typeInfos,
StubTypeNamespace = stubTypeNamespace,
StubContainingTypes = containingTypes.ToImmutable(),
AdditionalAttributes = additionalAttrs.ToImmutable(),
Options = env.Options,
GeneratorFactory = generatorFactory
};
}
private static (ImmutableArray<TypePositionInfo>, IMarshallingGeneratorFactory) GenerateTypeInformation(IMethodSymbol method, GeneratedDllImportData dllImportData, GeneratorDiagnostics diagnostics, StubEnvironment env)
{
// Compute the current default string encoding value.
CharEncoding defaultEncoding = CharEncoding.Undefined;
if (dllImportData.IsUserDefined.HasFlag(DllImportMember.CharSet))
{
defaultEncoding = dllImportData.CharSet switch
{
CharSet.Unicode => CharEncoding.Utf16,
CharSet.Auto => CharEncoding.PlatformDefined,
CharSet.Ansi => CharEncoding.Ansi,
_ => CharEncoding.Undefined, // [Compat] Do not assume a specific value for None
};
}
var defaultInfo = new DefaultMarshallingInfo(defaultEncoding);
var marshallingAttributeParser = new MarshallingAttributeInfoParser(env.Compilation, diagnostics, defaultInfo, method);
// Determine parameter and return types
ImmutableArray<TypePositionInfo>.Builder typeInfos = ImmutableArray.CreateBuilder<TypePositionInfo>();
for (int i = 0; i < method.Parameters.Length; i++)
{
IParameterSymbol param = method.Parameters[i];
MarshallingInfo marshallingInfo = marshallingAttributeParser.ParseMarshallingInfo(param.Type, param.GetAttributes());
var typeInfo = TypePositionInfo.CreateForParameter(param, marshallingInfo, env.Compilation);
typeInfo = typeInfo with
{
ManagedIndex = i,
NativeIndex = typeInfos.Count
};
typeInfos.Add(typeInfo);
}
TypePositionInfo retTypeInfo = new(ManagedTypeInfo.CreateTypeInfoForTypeSymbol(method.ReturnType), marshallingAttributeParser.ParseMarshallingInfo(method.ReturnType, method.GetReturnTypeAttributes()));
retTypeInfo = retTypeInfo with
{
ManagedIndex = TypePositionInfo.ReturnIndex,
NativeIndex = TypePositionInfo.ReturnIndex
};
InteropGenerationOptions options = new(env.Options.UseMarshalType, env.Options.UseInternalUnsafeType);
IMarshallingGeneratorFactory generatorFactory;
if (env.Options.GenerateForwarders)
{
generatorFactory = new ForwarderMarshallingGeneratorFactory();
}
else
{
generatorFactory = new DefaultMarshallingGeneratorFactory(options);
AttributedMarshallingModelGeneratorFactory attributedMarshallingFactory = new(generatorFactory, options);
generatorFactory = attributedMarshallingFactory;
if (!dllImportData.PreserveSig)
{
// Create type info for native out param
if (!method.ReturnsVoid)
{
// Transform the managed return type info into an out parameter and add it as the last param
TypePositionInfo nativeOutInfo = retTypeInfo with
{
InstanceIdentifier = PInvokeStubCodeGenerator.ReturnIdentifier,
RefKind = RefKind.Out,
RefKindSyntax = SyntaxKind.OutKeyword,
ManagedIndex = TypePositionInfo.ReturnIndex,
NativeIndex = typeInfos.Count
};
typeInfos.Add(nativeOutInfo);
}
// Use a marshalling generator that supports the HRESULT return->exception marshalling.
generatorFactory = new NoPreserveSigMarshallingGeneratorFactory(generatorFactory);
// Create type info for native HRESULT return
retTypeInfo = new TypePositionInfo(SpecialTypeInfo.Int32, NoMarshallingInfo.Instance);
retTypeInfo = retTypeInfo with
{
NativeIndex = TypePositionInfo.ReturnIndex
};
}
generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory);
attributedMarshallingFactory.ElementMarshallingGeneratorFactory = generatorFactory;
}
typeInfos.Add(retTypeInfo);
return (typeInfos.ToImmutable(), generatorFactory);
}
public override bool Equals(object obj)
{
return obj is DllImportStubContext other && Equals(other);
}
public bool Equals(DllImportStubContext other)
{
// We don't check if the generator factories are equal since
// the generator factory is deterministically created based on the ElementTypeInformation and Options.
return other is not null
&& StubTypeNamespace == other.StubTypeNamespace
&& ElementTypeInformation.SequenceEqual(other.ElementTypeInformation)
&& StubContainingTypes.SequenceEqual(other.StubContainingTypes, (IEqualityComparer<TypeDeclarationSyntax>)SyntaxEquivalentComparer.Instance)
&& StubReturnType.IsEquivalentTo(other.StubReturnType)
&& AdditionalAttributes.SequenceEqual(other.AdditionalAttributes, (IEqualityComparer<AttributeListSyntax>)SyntaxEquivalentComparer.Instance)
&& Options.Equals(other.Options);
}
public override int GetHashCode()
{
throw new UnreachableException();
}
private static bool MethodIsSkipLocalsInit(StubEnvironment env, IMethodSymbol method)
{
if (env.ModuleSkipLocalsInit)
{
return true;
}
if (method.GetAttributes().Any(a => IsSkipLocalsInitAttribute(a)))
{
return true;
}
for (INamedTypeSymbol type = method.ContainingType; type is not null; type = type.ContainingType)
{
if (type.GetAttributes().Any(a => IsSkipLocalsInitAttribute(a)))
{
return true;
}
}
// We check the module case earlier, so we don't need to do it here.
return false;
static bool IsSkipLocalsInitAttribute(AttributeData a)
=> a.AttributeClass?.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute;
}
}
}