提交 7b0f1790 编写于 作者: C Cyrus Najmabadi

Expose ModuleMetadata off of IModuleSymbols.

上级 c8e219cb
...@@ -682,5 +682,7 @@ internal IEnumerable<NamedTypeSymbol> GetForwardedTypes() ...@@ -682,5 +682,7 @@ internal IEnumerable<NamedTypeSymbol> GetForwardedTypes()
yield return assemblySymbol.LookupTopLevelMetadataType(ref name, digThroughForwardedTypes: true); yield return assemblySymbol.LookupTopLevelMetadataType(ref name, digThroughForwardedTypes: true);
} }
} }
public override ModuleMetadata GetMetadata() => _module.GetMetadataCopy();
} }
} }
...@@ -183,6 +183,8 @@ internal override bool HasAssemblyRuntimeCompatibilityAttribute ...@@ -183,6 +183,8 @@ internal override bool HasAssemblyRuntimeCompatibilityAttribute
{ {
get { return null; } get { return null; }
} }
public override ModuleMetadata GetMetadata() => null;
} }
internal class MissingModuleSymbolWithName : MissingModuleSymbol internal class MissingModuleSymbolWithName : MissingModuleSymbol
......
...@@ -377,6 +377,8 @@ public override TResult Accept<TResult>(SymbolVisitor<TResult> visitor) ...@@ -377,6 +377,8 @@ public override TResult Accept<TResult>(SymbolVisitor<TResult> visitor)
return visitor.VisitModule(this); return visitor.VisitModule(this);
} }
public abstract ModuleMetadata GetMetadata();
#endregion #endregion
} }
} }
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
...@@ -282,5 +283,7 @@ internal override bool HasAssemblyRuntimeCompatibilityAttribute ...@@ -282,5 +283,7 @@ internal override bool HasAssemblyRuntimeCompatibilityAttribute
{ {
get { return null; } get { return null; }
} }
public override ModuleMetadata GetMetadata() => _underlyingModule.GetMetadata();
} }
} }
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
...@@ -555,5 +556,7 @@ internal override bool HasAssemblyRuntimeCompatibilityAttribute ...@@ -555,5 +556,7 @@ internal override bool HasAssemblyRuntimeCompatibilityAttribute
return data != null && data.HasDefaultCharSetAttribute ? data.DefaultCharacterSet : (CharSet?)null; return data != null && data.HasDefaultCharSetAttribute ? data.DefaultCharacterSet : (CharSet?)null;
} }
} }
public override ModuleMetadata GetMetadata() => null;
} }
} }
...@@ -24,6 +24,9 @@ namespace Microsoft.CodeAnalysis ...@@ -24,6 +24,9 @@ namespace Microsoft.CodeAnalysis
/// </summary> /// </summary>
internal sealed class PEModule : IDisposable internal sealed class PEModule : IDisposable
{ {
// We need to store reference for to keep the metadata alive while symbols have reference to PEModule.
private readonly ModuleMetadata _owner;
// Either we have PEReader or we have pointer and size of the metadata blob: // Either we have PEReader or we have pointer and size of the metadata blob:
private readonly PEReader _peReaderOpt; private readonly PEReader _peReaderOpt;
private readonly IntPtr _metadataPointerOpt; private readonly IntPtr _metadataPointerOpt;
...@@ -76,13 +79,14 @@ internal sealed class PEModule : IDisposable ...@@ -76,13 +79,14 @@ internal sealed class PEModule : IDisposable
private static readonly AttributeValueExtractor<ObsoleteAttributeData> s_attributeObsoleteDataExtractor = CrackObsoleteAttributeData; private static readonly AttributeValueExtractor<ObsoleteAttributeData> s_attributeObsoleteDataExtractor = CrackObsoleteAttributeData;
private static readonly AttributeValueExtractor<ObsoleteAttributeData> s_attributeDeprecatedDataExtractor = CrackDeprecatedAttributeData; private static readonly AttributeValueExtractor<ObsoleteAttributeData> s_attributeDeprecatedDataExtractor = CrackDeprecatedAttributeData;
internal PEModule(PEReader peReader, IntPtr metadataOpt, int metadataSizeOpt, bool includeEmbeddedInteropTypes = false) internal PEModule(ModuleMetadata owner, PEReader peReader, IntPtr metadataOpt, int metadataSizeOpt, bool includeEmbeddedInteropTypes = false)
{ {
// shall not throw // shall not throw
Debug.Assert((peReader == null) ^ (metadataOpt == IntPtr.Zero && metadataSizeOpt == 0)); Debug.Assert((peReader == null) ^ (metadataOpt == IntPtr.Zero && metadataSizeOpt == 0));
Debug.Assert(metadataOpt == IntPtr.Zero || metadataSizeOpt > 0); Debug.Assert(metadataOpt == IntPtr.Zero || metadataSizeOpt > 0);
_owner = owner;
_peReaderOpt = peReader; _peReaderOpt = peReader;
_metadataPointerOpt = metadataOpt; _metadataPointerOpt = metadataOpt;
_metadataSizeOpt = metadataSizeOpt; _metadataSizeOpt = metadataSizeOpt;
...@@ -2966,5 +2970,7 @@ public unsafe override string GetString(byte* bytes, int byteCount) ...@@ -2966,5 +2970,7 @@ public unsafe override string GetString(byte* bytes, int byteCount)
return StringTable.AddSharedUTF8(bytes, byteCount); return StringTable.AddSharedUTF8(bytes, byteCount);
} }
} }
public ModuleMetadata GetMetadataCopy() => _owner.Copy();
} }
} }
...@@ -24,13 +24,13 @@ public sealed partial class ModuleMetadata : Metadata ...@@ -24,13 +24,13 @@ public sealed partial class ModuleMetadata : Metadata
private ModuleMetadata(PEReader peReader) private ModuleMetadata(PEReader peReader)
: base(isImageOwner: true, id: new MetadataId()) : base(isImageOwner: true, id: new MetadataId())
{ {
_module = new PEModule(peReader: peReader, metadataOpt: IntPtr.Zero, metadataSizeOpt: 0); _module = new PEModule(this, peReader: peReader, metadataOpt: IntPtr.Zero, metadataSizeOpt: 0);
} }
private ModuleMetadata(IntPtr metadata, int size, bool includeEmbeddedInteropTypes) private ModuleMetadata(IntPtr metadata, int size, bool includeEmbeddedInteropTypes)
: base(isImageOwner: true, id: new MetadataId()) : base(isImageOwner: true, id: new MetadataId())
{ {
_module = new PEModule(peReader: null, metadataOpt: metadata, metadataSizeOpt: size, includeEmbeddedInteropTypes: includeEmbeddedInteropTypes); _module = new PEModule(this, peReader: null, metadataOpt: metadata, metadataSizeOpt: size, includeEmbeddedInteropTypes: includeEmbeddedInteropTypes);
} }
// creates a copy // creates a copy
......
...@@ -87,6 +87,7 @@ Microsoft.CodeAnalysis.Emit.DebugInformationFormat.PortablePdb = 2 -> Microsoft. ...@@ -87,6 +87,7 @@ Microsoft.CodeAnalysis.Emit.DebugInformationFormat.PortablePdb = 2 -> Microsoft.
Microsoft.CodeAnalysis.IAssemblySymbol.GetMetadata() -> Microsoft.CodeAnalysis.AssemblyMetadata Microsoft.CodeAnalysis.IAssemblySymbol.GetMetadata() -> Microsoft.CodeAnalysis.AssemblyMetadata
Microsoft.CodeAnalysis.ICompilationUnitSyntax Microsoft.CodeAnalysis.ICompilationUnitSyntax
Microsoft.CodeAnalysis.ICompilationUnitSyntax.EndOfFileToken.get -> Microsoft.CodeAnalysis.SyntaxToken Microsoft.CodeAnalysis.ICompilationUnitSyntax.EndOfFileToken.get -> Microsoft.CodeAnalysis.SyntaxToken
Microsoft.CodeAnalysis.IModuleSymbol.GetMetadata() -> Microsoft.CodeAnalysis.ModuleMetadata
Microsoft.CodeAnalysis.Metadata.Id.get -> Microsoft.CodeAnalysis.MetadataId Microsoft.CodeAnalysis.Metadata.Id.get -> Microsoft.CodeAnalysis.MetadataId
Microsoft.CodeAnalysis.MetadataId Microsoft.CodeAnalysis.MetadataId
Microsoft.CodeAnalysis.MetadataReference.MetadataReference(Microsoft.CodeAnalysis.MetadataReferenceProperties properties) -> void Microsoft.CodeAnalysis.MetadataReference.MetadataReference(Microsoft.CodeAnalysis.MetadataReferenceProperties properties) -> void
......
...@@ -38,5 +38,13 @@ public interface IModuleSymbol : ISymbol ...@@ -38,5 +38,13 @@ public interface IModuleSymbol : ISymbol
/// from ReferencedAssemblySymbols correspond to each other. /// from ReferencedAssemblySymbols correspond to each other.
/// </summary> /// </summary>
ImmutableArray<IAssemblySymbol> ReferencedAssemblySymbols { get; } ImmutableArray<IAssemblySymbol> ReferencedAssemblySymbols { get; }
/// <summary>
/// If this is an <see cref="IModuleSymbol"/> for an <see cref="ModuleMetadata"/>,
/// this returns that <see cref="ModuleMetadata"/>.
///
/// Otherwise, this returns <code>null</code>.
/// </summary>
ModuleMetadata GetMetadata();
} }
} }
...@@ -706,6 +706,10 @@ Friend Class MockModuleSymbol ...@@ -706,6 +706,10 @@ Friend Class MockModuleSymbol
Return Nothing Return Nothing
End Get End Get
End Property End Property
Public Overrides Function GetMetadata() As ModuleMetadata
Return Nothing
End Function
End Class End Class
Friend Class MockAssemblySymbol Friend Class MockAssemblySymbol
......
...@@ -457,5 +457,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE ...@@ -457,5 +457,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
Next Next
End Function End Function
Public Overrides Function GetMetadata() As ModuleMetadata
Return _module.GetMetadataCopy()
End Function
End Class End Class
End Namespace End Namespace
...@@ -165,6 +165,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ...@@ -165,6 +165,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Return Nothing Return Nothing
End Get End Get
End Property End Property
Public Overrides Function GetMetadata() As ModuleMetadata
Return Nothing
End Function
End Class End Class
Friend Class MissingModuleSymbolWithName Friend Class MissingModuleSymbolWithName
...@@ -200,5 +204,4 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ...@@ -200,5 +204,4 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Return other IsNot Nothing AndAlso m_Assembly.Equals(other.m_Assembly) AndAlso String.Equals(_name, other._name, StringComparison.OrdinalIgnoreCase) Return other IsNot Nothing AndAlso m_Assembly.Equals(other.m_Assembly) AndAlso String.Equals(_name, other._name, StringComparison.OrdinalIgnoreCase)
End Function End Function
End Class End Class
End Namespace
End Namespace \ No newline at end of file
...@@ -123,6 +123,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ...@@ -123,6 +123,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
End Get End Get
End Property End Property
Public MustOverride Function GetMetadata() As ModuleMetadata Implements IModuleSymbol.GetMetadata
''' <summary> ''' <summary>
''' Returns an array of assembly identities for assemblies referenced by this module. ''' Returns an array of assembly identities for assemblies referenced by this module.
''' Items at the same position from GetReferencedAssemblies and from GetReferencedAssemblySymbols ''' Items at the same position from GetReferencedAssemblies and from GetReferencedAssemblySymbols
......
...@@ -273,5 +273,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Retargeting ...@@ -273,5 +273,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Retargeting
Public Overrides Function GetDocumentationCommentXml(Optional preferredCulture As CultureInfo = Nothing, Optional expandIncludes As Boolean = False, Optional cancellationToken As CancellationToken = Nothing) As String Public Overrides Function GetDocumentationCommentXml(Optional preferredCulture As CultureInfo = Nothing, Optional expandIncludes As Boolean = False, Optional cancellationToken As CancellationToken = Nothing) As String
Return _underlyingModule.GetDocumentationCommentXml(preferredCulture, expandIncludes, cancellationToken) Return _underlyingModule.GetDocumentationCommentXml(preferredCulture, expandIncludes, cancellationToken)
End Function End Function
Public Overrides Function GetMetadata() As ModuleMetadata
Return _underlyingModule.GetMetadata()
End Function
End Class End Class
End Namespace End Namespace
...@@ -1163,5 +1163,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ...@@ -1163,5 +1163,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
End Select End Select
Next Next
End Sub End Sub
Public Overrides Function GetMetadata() As ModuleMetadata
Return Nothing
End Function
End Class End Class
End Namespace End Namespace
...@@ -249,8 +249,9 @@ private static ImmutableArray<ImmutableArray<string>> GetGroupedImportStrings(Co ...@@ -249,8 +249,9 @@ private static ImmutableArray<ImmutableArray<string>> GetGroupedImportStrings(Co
compilation.Emit(exebits, pdbbits); compilation.Emit(exebits, pdbbits);
exebits.Position = 0; exebits.Position = 0;
using (var module = new PEModule(new PEReader(exebits, PEStreamOptions.LeaveOpen), metadataOpt: IntPtr.Zero, metadataSizeOpt: 0)) using (var metadata = ModuleMetadata.CreateFromStream(exebits, leaveOpen: true))
{ {
var module = metadata.Module;
var metadataReader = module.MetadataReader; var metadataReader = module.MetadataReader;
MethodDefinitionHandle methodHandle = metadataReader.MethodDefinitions.Single(mh => metadataReader.GetString(metadataReader.GetMethodDefinition(mh).Name) == methodName); MethodDefinitionHandle methodHandle = metadataReader.MethodDefinitions.Single(mh => metadataReader.GetString(metadataReader.GetMethodDefinition(mh).Name) == methodName);
int methodToken = metadataReader.GetToken(methodHandle); int methodToken = metadataReader.GetToken(methodHandle);
......
...@@ -376,8 +376,9 @@ internal static ImmutableArray<string> GetLocalNames(this ISymUnmanagedReader sy ...@@ -376,8 +376,9 @@ internal static ImmutableArray<string> GetLocalNames(this ISymUnmanagedReader sy
throw new NotImplementedException(); throw new NotImplementedException();
} }
using (var module = new PEModule(new PEReader(assembly), metadataOpt: IntPtr.Zero, metadataSizeOpt: 0)) using (var metadata = ModuleMetadata.CreateFromImage(assembly))
{ {
var module = metadata.Module;
var reader = module.MetadataReader; var reader = module.MetadataReader;
var typeDef = reader.GetTypeDef(parts[0]); var typeDef = reader.GetTypeDef(parts[0]);
var methodName = parts[1]; var methodName = parts[1];
......
...@@ -97,10 +97,10 @@ internal int GetLocalSignatureToken(MethodDefinitionHandle methodHandle) ...@@ -97,10 +97,10 @@ internal int GetLocalSignatureToken(MethodDefinitionHandle methodHandle)
return 0; return 0;
} }
using (var module = new PEModule(new PEReader(ImmutableArray.CreateRange(this.FullImage)), metadataOpt: IntPtr.Zero, metadataSizeOpt: 0)) using (var metadata = ModuleMetadata.CreateFromImage(this.FullImage))
{ {
var reader = module.MetadataReader; var reader = metadata.MetadataReader;
var methodIL = module.GetMethodBodyOrThrow(methodHandle); var methodIL = metadata.Module.GetMethodBodyOrThrow(methodHandle);
var localSignatureHandle = methodIL.LocalSignature; var localSignatureHandle = methodIL.LocalSignature;
return reader.GetToken(localSignatureHandle); return reader.GetToken(localSignatureHandle);
} }
......
...@@ -124,7 +124,8 @@ End Namespace ...@@ -124,7 +124,8 @@ End Namespace
compilation.Emit(exebits, pdbbits) compilation.Emit(exebits, pdbbits)
exebits.Position = 0 exebits.Position = 0
Using [module] As New PEModule(New PEReader(exebits, PEStreamOptions.LeaveOpen), metadataOpt:=Nothing, metadataSizeOpt:=0) Using metadata = modulemetadata.CreateFromStream(exebits, leaveOpen:=True)
Dim [module] = metadata.module
Dim metadataReader = [module].MetadataReader Dim metadataReader = [module].MetadataReader
Dim methodHandle = metadataReader.MethodDefinitions.Single(Function(mh) metadataReader.GetString(metadataReader.GetMethodDefinition(mh).Name) = methodName) Dim methodHandle = metadataReader.MethodDefinitions.Single(Function(mh) metadataReader.GetString(metadataReader.GetMethodDefinition(mh).Name) = methodName)
Dim methodToken = metadataReader.GetToken(methodHandle) Dim methodToken = metadataReader.GetToken(methodHandle)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册