-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emission and reading of custom modifiers #85504
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 109 additions & 6 deletions
115
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/ModifiedType.NativeAot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,123 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection.Runtime.General; | ||
|
||
using Internal.Metadata.NativeFormat; | ||
|
||
using Debug = System.Diagnostics.Debug; | ||
|
||
namespace System.Reflection | ||
{ | ||
internal partial class ModifiedType | ||
{ | ||
internal struct TypeSignature | ||
internal readonly struct TypeSignature | ||
{ | ||
internal readonly MetadataReader Reader; | ||
internal readonly Handle Handle; | ||
public TypeSignature(MetadataReader reader, Handle handle) | ||
=> (Reader, Handle) = (reader, handle); | ||
} | ||
|
||
internal Type GetTypeParameter(Type unmodifiedType, int index) | ||
{ | ||
MetadataReader reader = _typeSignature.Reader; | ||
Handle handle = _typeSignature.Handle; | ||
|
||
while (handle.HandleType == HandleType.ModifiedType) | ||
handle = reader.GetModifiedType(handle.ToModifiedTypeHandle(reader)).Type; | ||
|
||
if (handle.HandleType == HandleType.TypeSpecification) | ||
handle = reader.GetTypeSpecification(handle.ToTypeSpecificationHandle(reader)).Signature; | ||
|
||
switch (handle.HandleType) | ||
{ | ||
case HandleType.SZArraySignature: | ||
Debug.Assert(index == 0); | ||
return Create(unmodifiedType, new TypeSignature(reader, reader.GetSZArraySignature(handle.ToSZArraySignatureHandle(reader)).ElementType)); | ||
case HandleType.ArraySignature: | ||
Debug.Assert(index == 0); | ||
return Create(unmodifiedType, new TypeSignature(reader, reader.GetArraySignature(handle.ToArraySignatureHandle(reader)).ElementType)); | ||
case HandleType.PointerSignature: | ||
Debug.Assert(index == 0); | ||
return Create(unmodifiedType, new TypeSignature(reader, reader.GetPointerSignature(handle.ToPointerSignatureHandle(reader)).Type)); | ||
case HandleType.ByReferenceSignature: | ||
Debug.Assert(index == 0); | ||
return Create(unmodifiedType, new TypeSignature(reader, reader.GetByReferenceSignature(handle.ToByReferenceSignatureHandle(reader)).Type)); | ||
case HandleType.FunctionPointerSignature: | ||
{ | ||
MethodSignature functionSig = reader.GetMethodSignature( | ||
reader.GetFunctionPointerSignature(handle.ToFunctionPointerSignatureHandle(reader)).Signature); | ||
if (index-- == 0) | ||
return Create(unmodifiedType, new TypeSignature(reader, functionSig.ReturnType)); | ||
|
||
Debug.Assert(index <= functionSig.Parameters.Count); | ||
foreach (Handle paramHandle in functionSig.Parameters) | ||
if (index-- == 0) | ||
return Create(unmodifiedType, new TypeSignature(reader, paramHandle)); | ||
} | ||
break; | ||
case HandleType.TypeInstantiationSignature: | ||
{ | ||
TypeInstantiationSignature typeInst = | ||
reader.GetTypeInstantiationSignature(handle.ToTypeInstantiationSignatureHandle(reader)); | ||
Debug.Assert(index < typeInst.GenericTypeArguments.Count); | ||
foreach (Handle paramHandle in typeInst.GenericTypeArguments) | ||
if (index-- == 0) | ||
return Create(unmodifiedType, new TypeSignature(reader, paramHandle)); | ||
} | ||
break; | ||
} | ||
|
||
Debug.Fail(handle.HandleType.ToString()); | ||
return null; | ||
} | ||
|
||
internal SignatureCallingConvention GetCallingConventionFromFunctionPointer() | ||
{ | ||
MetadataReader reader = _typeSignature.Reader; | ||
Handle fnPtrTypeSigHandle = reader.GetTypeSpecification( | ||
_typeSignature.Handle.ToTypeSpecificationHandle(reader)).Signature; | ||
MethodSignatureHandle methodSigHandle = reader.GetFunctionPointerSignature( | ||
fnPtrTypeSigHandle.ToFunctionPointerSignatureHandle(reader)).Signature; | ||
|
||
Debug.Assert((int)Internal.Metadata.NativeFormat.SignatureCallingConvention.StdCall == (int)SignatureCallingConvention.StdCall); | ||
Debug.Assert((int)Internal.Metadata.NativeFormat.SignatureCallingConvention.Unmanaged == (int)SignatureCallingConvention.Unmanaged); | ||
return (SignatureCallingConvention)(reader.GetMethodSignature(methodSigHandle).CallingConvention | ||
& Internal.Metadata.NativeFormat.SignatureCallingConvention.UnmanagedCallingConventionMask); | ||
} | ||
|
||
#pragma warning disable IDE0060 | ||
internal Type GetTypeParameter(Type unmodifiedType, int index) => throw new NotSupportedException(); | ||
private Type[] GetCustomModifiers(bool required) | ||
{ | ||
ArrayBuilder<Type> builder = default; | ||
|
||
MetadataReader reader = _typeSignature.Reader; | ||
Handle handle = _typeSignature.Handle; | ||
|
||
while (handle.HandleType == HandleType.ModifiedType) | ||
{ | ||
var modifiedType = reader.GetModifiedType(handle.ToModifiedTypeHandle(reader)); | ||
|
||
internal SignatureCallingConvention GetCallingConventionFromFunctionPointer() => throw new NotSupportedException(); | ||
handle = modifiedType.Type; | ||
|
||
if (modifiedType.IsOptional == required) | ||
continue; | ||
|
||
builder.Add(modifiedType.ModifierType.Resolve(reader, new TypeContext(null, null))); | ||
} | ||
|
||
Type[] result = builder.ToArray(); | ||
|
||
// We call Reverse for compat with CoreCLR that also reverses these. | ||
// ILDasm also reverses these but don't be fooled: you can go to | ||
// View -> MetaInfo -> Show to see the file format order in ILDasm. | ||
Array.Reverse(result); | ||
|
||
return result; | ||
} | ||
|
||
private Type[] GetCustomModifiers(bool required) => throw new NotSupportedException(); | ||
#pragma warning restore IDE0060 | ||
public static Type Create(Type unmodifiedType, MetadataReader reader, Handle typeSignature) | ||
=> ModifiedType.Create(unmodifiedType, new TypeSignature(reader, typeSignature)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weird.