提交 df3d2ae9 编写于 作者: T Tomas Matousek

Sync C# and VB implementations of AssemblyData subclasses

上级 e4a7cca2
......@@ -368,7 +368,7 @@ private bool CreateAndSetSourceAssemblyFullBind(CSharpCompilation compilation)
{
if ((object)bindingResult[i].AssemblySymbol == null)
{
// symbols hasn't been found in the cache, create a new one
// symbol hasn't been found in the cache, create a new one
var compilationData = (AssemblyDataForMetadataOrCompilation)allAssemblyData[i];
bindingResult[i].AssemblySymbol = compilationData.CreateAssemblySymbol();
newSymbols.Add(i);
......
......@@ -2175,31 +2175,6 @@ public void AsymmetricUnification()
Assert.Equal("System.Numerics.Vectors, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", ((IAssemblySymbol)a1).Identity.GetDisplayName());
}
private class TestMissingMetadataReferenceResolver : MetadataReferenceResolver
{
private readonly Dictionary<string, MetadataReference> _map;
public readonly List<AssemblyIdentity> ResolutionAttempts = new List<AssemblyIdentity>();
public TestMissingMetadataReferenceResolver(Dictionary<string, MetadataReference> map)
{
_map = map;
}
public override PortableExecutableReference ResolveMissingAssembly(AssemblyIdentity identity)
{
ResolutionAttempts.Add(identity);
MetadataReference reference;
string nameAndVersion = identity.Name + (identity.Version != AssemblyIdentity.NullVersion ? $", {identity.Version}" : "");
return _map.TryGetValue(nameAndVersion, out reference) ? (PortableExecutableReference)reference : null;
}
public override bool ResolveMissingAssemblies => true;
public override bool Equals(object other) => true;
public override int GetHashCode() => 1;
public override ImmutableArray<PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties) => default(ImmutableArray<PortableExecutableReference>);
}
[Fact]
public void MissingAssemblyResolution1()
{
......
......@@ -41,5 +41,29 @@ public void SelectDistinct1()
builder = new ArrayBuilder<int> { 1, 2, 3, 2, 4, 5, 1 };
AssertEx.Equal(new byte[] { 1, 2, 3, 4, 5 }, builder.SelectDistinct(n => (byte)n));
}
[Fact]
public void AddRange()
{
var builder = new ArrayBuilder<int>();
builder.AddRange(new int[0], 0, 0);
AssertEx.Equal(new int[0], builder.ToArray());
builder.AddRange(new[] { 1, 2, 3 }, 0, 3);
AssertEx.Equal(new[] { 1, 2, 3 }, builder.ToArray());
builder.AddRange(new[] { 1, 2, 3 }, 2, 0);
AssertEx.Equal(new[] { 1, 2, 3 }, builder.ToArray());
builder.AddRange(new[] { 1, 2, 3 }, 1, 1);
AssertEx.Equal(new[] { 1, 2, 3, 2 }, builder.ToArray());
builder.AddRange(new[] { 1, 2, 3 }, 1, 2);
AssertEx.Equal(new[] { 1, 2, 3, 2, 2, 3 }, builder.ToArray());
builder.AddRange(new[] { 1, 2, 3 }, 2, 1);
AssertEx.Equal(new[] { 1, 2, 3, 2, 2, 3, 3 }, builder.ToArray());
}
}
}
......@@ -385,9 +385,9 @@ public void AddRange(ImmutableArray<T> items, int length)
public void AddRange(T[] items, int start, int length)
{
for (int i = 0; i < length; i++)
for (int i = start, end = start + length; i < end; i++)
{
Add(items[start + i]);
Add(items[i]);
}
}
......
......@@ -4,6 +4,7 @@ Imports System.Collections.Immutable
Imports Microsoft.CodeAnalysis.VisualBasic.Emit
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols.Retargeting
Imports MetadataOrDiagnostic = System.Object
Namespace Microsoft.CodeAnalysis.VisualBasic
......@@ -277,8 +278,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim explicitAssemblyData = referencedAssemblies.Insert(0, assemblyBeingBuiltData)
' Let's bind all the references and resolve missing one (if resolver is available)
Dim corLibraryIndex As Integer
Dim hasCircularReference As Boolean
Dim corLibraryIndex As Integer
Dim implicitlyResolvedReferences As ImmutableArray(Of MetadataReference) = Nothing
Dim implicitlyResolvedReferenceMap As ImmutableArray(Of ResolvedReference) = Nothing
Dim allAssemblyData As ImmutableArray(Of AssemblyData) = Nothing
......@@ -295,6 +296,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Debug.Assert(bindingResult.Length = allAssemblyData.Length)
references = references.AddRange(implicitlyResolvedReferences)
referenceMap = referenceMap.AddRange(implicitlyResolvedReferenceMap)
Dim referencedAssembliesMap As Dictionary(Of MetadataReference, Integer) = Nothing
......@@ -314,21 +316,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
For i As Integer = 1 To bindingResult.Length - 1 Step 1
If bindingResult(i).AssemblySymbol Is Nothing Then
Dim compilationData = TryCast(allAssemblyData(i), AssemblyDataForCompilation)
If compilationData IsNot Nothing Then
bindingResult(i).AssemblySymbol = New Retargeting.RetargetingAssemblySymbol(compilationData.Compilation.SourceAssembly,
compilationData.IsLinked)
Else
Dim fileData = DirectCast(allAssemblyData(i), AssemblyDataForFile)
bindingResult(i).AssemblySymbol = New PEAssemblySymbol(fileData.Assembly,
fileData.DocumentationProvider,
fileData.IsLinked,
fileData.EffectiveImportOptions)
End If
' symbol hasn't been found in the cache, create a new one
Dim compilationData = DirectCast(allAssemblyData(i), AssemblyDataForMetadataOrCompilation)
bindingResult(i).AssemblySymbol = compilationData.CreateAssemblySymbol()
newSymbols.Add(i)
End If
......@@ -353,11 +343,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
' This should be done after we created/found all AssemblySymbols
Dim missingAssemblies As Dictionary(Of AssemblyIdentity, MissingAssemblySymbol) = Nothing
' -1 for assembly being built
Dim totalReferencedAssemblyCount = allAssemblyData.Length - 1
' Setup bound references for newly created SourceAssemblySymbol
Dim moduleReferences As ImmutableArray(Of ModuleReferences(Of AssemblySymbol)) = Nothing
SetupReferencesForSourceAssembly(allAssemblyData,
assemblySymbol,
SetupReferencesForSourceAssembly(assemblySymbol,
modules,
totalReferencedAssemblyCount,
bindingResult,
missingAssemblies,
moduleReferences)
......@@ -596,9 +589,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Sub
Private Shared Sub SetupReferencesForSourceAssembly(
allAssemblyData As ImmutableArray(Of AssemblyData),
sourceAssembly As SourceAssemblySymbol,
modules As ImmutableArray(Of PEModule),
totalReferencedAssemblyCount As Integer,
bindingResult() As BoundInputAssembly,
ByRef missingAssemblies As Dictionary(Of AssemblyIdentity, MissingAssemblySymbol),
ByRef moduleReferences As ImmutableArray(Of ModuleReferences(Of AssemblySymbol))
......@@ -610,8 +603,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim refsUsed As Integer = 0
For moduleIndex As Integer = 0 To moduleSymbols.Length - 1 Step 1
' -1 for assembly being built
Dim refsCount As Integer = If(moduleIndex = 0, allAssemblyData.Length - 1, modules(moduleIndex - 1).ReferencedAssemblies.Length)
Dim refsCount As Integer = If(moduleIndex = 0, totalReferencedAssemblyCount, modules(moduleIndex - 1).ReferencedAssemblies.Length)
Dim identities(refsCount - 1) As AssemblyIdentity
Dim symbols(refsCount - 1) As AssemblySymbol
......@@ -629,7 +621,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
identities(k) = boundReference.ReferenceIdentity
Next
Dim references = New ModuleReferences(Of AssemblySymbol)(identities.AsImmutableOrNull(), symbols.AsImmutableOrNull(), unifiedAssemblies.AsImmutableOrEmpty())
Dim references = New ModuleReferences(Of AssemblySymbol)(identities.AsImmutableOrNull(),
symbols.AsImmutableOrNull(),
unifiedAssemblies.AsImmutableOrEmpty())
If moduleIndex > 0 Then
moduleReferencesBuilder.Add(references)
......@@ -640,7 +634,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
refsUsed += refsCount
Next
moduleReferences = moduleReferencesBuilder.AsImmutableOrEmpty()
moduleReferences = moduleReferencesBuilder.ToImmutableOrEmptyAndFree()
End Sub
Private Shared Function GetAssemblyDefinitionSymbol(bindingResult As BoundInputAssembly(),
......@@ -687,16 +681,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Private ReadOnly m_ReferencedAssemblies As ImmutableArray(Of AssemblyIdentity)
Private ReadOnly m_EmbedInteropTypes As Boolean
'This is the name of the compilation that is being built.
'This should be the assembly name w/o the extension. It is
'used to compute whether or not it is possible that this
'assembly will give friend access to the compilation.
Protected ReadOnly m_CompilationName As String
Protected Sub New(identity As AssemblyIdentity,
referencedAssemblies As ImmutableArray(Of AssemblyIdentity),
embedInteropTypes As Boolean,
compilationName As String)
embedInteropTypes As Boolean)
Debug.Assert(identity IsNot Nothing)
Debug.Assert(Not referencedAssemblies.IsDefault)
......@@ -704,9 +691,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
m_EmbedInteropTypes = embedInteropTypes
m_Identity = identity
m_ReferencedAssemblies = referencedAssemblies
m_CompilationName = compilationName
End Sub
Friend MustOverride Function CreateAssemblySymbol() As AssemblySymbol
Public Overrides ReadOnly Property Identity As AssemblyIdentity
Get
Return m_Identity
......@@ -750,61 +738,60 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Private NotInheritable Class AssemblyDataForFile
Inherits AssemblyDataForMetadataOrCompilation
Private ReadOnly _assembly As PEAssembly
Private ReadOnly _cachedSymbols As WeakList(Of IAssemblySymbol)
Private ReadOnly _documentationProvider As DocumentationProvider
Private ReadOnly _compilationImportOptions As MetadataImportOptions
Public ReadOnly Property Assembly As PEAssembly
Get
Return _assembly
End Get
End Property
Public ReadOnly Assembly As PEAssembly
''' <summary>
''' Guarded by <see cref="CommonReferenceManager.SymbolCacheAndReferenceManagerStateGuard"/>.
''' </summary>
Public ReadOnly Property CachedSymbols As WeakList(Of IAssemblySymbol)
Get
Return _cachedSymbols
End Get
End Property
Public ReadOnly CachedSymbols As WeakList(Of IAssemblySymbol)
Public ReadOnly Property DocumentationProvider As DocumentationProvider
Get
Return _documentationProvider
End Get
End Property
Public ReadOnly DocumentationProvider As DocumentationProvider
''' <summary>
''' Import options of the compilation being built.
''' </summary>
Private ReadOnly _compilationImportOptions As MetadataImportOptions
' This is the name of the compilation that is being built.
' This should be the assembly name w/o the extension. It is
' used to compute whether or not it is possible that this
' assembly will give friend access to the compilation.
Private ReadOnly _sourceAssemblySimpleName As String
Private _internalsVisibleComputed As Boolean = False
Private _internalsPotentiallyVisibleToCompilation As Boolean = False
Public Sub New(assembly As PEAssembly,
cachedSymbols As WeakList(Of IAssemblySymbol),
embedInteropTypes As Boolean,
documentationProvider As DocumentationProvider,
compilationName As String,
sourceAssemblySimpleName As String,
compilationImportOptions As MetadataImportOptions)
MyBase.New(assembly.Identity, assembly.AssemblyReferences, embedInteropTypes, compilationName)
MyBase.New(assembly.Identity, assembly.AssemblyReferences, embedInteropTypes)
Debug.Assert(documentationProvider IsNot Nothing)
Debug.Assert(cachedSymbols IsNot Nothing)
_cachedSymbols = cachedSymbols
_assembly = assembly
_documentationProvider = If(documentationProvider, DocumentationProvider.Default)
Me.CachedSymbols = cachedSymbols
Me.Assembly = assembly
Me.DocumentationProvider = documentationProvider
_compilationImportOptions = compilationImportOptions
_sourceAssemblySimpleName = sourceAssemblySimpleName
End Sub
Private _internalsVisibleComputed As Boolean = False
Private _internalsVisibleToCompilation As Boolean = False
Friend Overrides Function CreateAssemblySymbol() As AssemblySymbol
Return New PEAssemblySymbol(Assembly, DocumentationProvider, IsLinked, EffectiveImportOptions)
End Function
Friend ReadOnly Property InternalsMayBeVisibleToCompilation As Boolean
Get
If Not _internalsVisibleComputed Then
_internalsVisibleToCompilation = InternalsMayBeVisibleToAssemblyBeingCompiled(m_CompilationName, _assembly)
_internalsPotentiallyVisibleToCompilation = InternalsMayBeVisibleToAssemblyBeingCompiled(_sourceAssemblySimpleName, Assembly)
_internalsVisibleComputed = True
End If
Return _internalsVisibleToCompilation
Return _internalsPotentiallyVisibleToCompilation
End Get
End Property
......@@ -823,7 +810,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
' accessing cached symbols requires a lock
SyncLock SymbolCacheAndReferenceManagerStateGuard
For Each assemblySymbol In _cachedSymbols
For Each assemblySymbol In CachedSymbols
Dim peAssembly = TryCast(assemblySymbol, PEAssemblySymbol)
If IsMatchingAssembly(peAssembly) Then
assemblies.Add(peAssembly)
......@@ -862,13 +849,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Public Overrides ReadOnly Property ContainsNoPiaLocalTypes() As Boolean
Get
Return _assembly.ContainsNoPiaLocalTypes()
Return Assembly.ContainsNoPiaLocalTypes()
End Get
End Property
Public Overrides ReadOnly Property DeclaresTheObjectClass As Boolean
Get
Return _assembly.DeclaresTheObjectClass
Return Assembly.DeclaresTheObjectClass
End Get
End Property
......@@ -885,7 +872,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Public ReadOnly Compilation As VisualBasicCompilation
Public Sub New(compilation As VisualBasicCompilation, embedInteropTypes As Boolean)
MyBase.New(compilation.Assembly.Identity, GetReferencedAssemblies(compilation), embedInteropTypes, compilation.AssemblyName)
MyBase.New(compilation.Assembly.Identity, GetReferencedAssemblies(compilation), embedInteropTypes)
Debug.Assert(compilation IsNot Nothing)
Me.Compilation = compilation
......@@ -916,6 +903,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return refs.ToImmutableAndFree()
End Function
Friend Overrides Function CreateAssemblySymbol() As AssemblySymbol
Return New RetargetingAssemblySymbol(Compilation.SourceAssembly, IsLinked)
End Function
Protected Overrides Sub AddAvailableSymbols(assemblies As List(Of AssemblySymbol))
assemblies.Add(Compilation.Assembly)
......@@ -926,7 +917,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Sub
Public Overrides Function IsMatchingAssembly(candidateAssembly As AssemblySymbol) As Boolean
Dim retargeting = TryCast(candidateAssembly, Retargeting.RetargetingAssemblySymbol)
Dim retargeting = TryCast(candidateAssembly, RetargetingAssemblySymbol)
Dim asm As AssemblySymbol
If retargeting IsNot Nothing Then
......@@ -935,7 +926,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
asm = TryCast(candidateAssembly, SourceAssemblySymbol)
End If
Debug.Assert(Not (TypeOf asm Is Retargeting.RetargetingAssemblySymbol))
Debug.Assert(TypeOf asm IsNot RetargetingAssemblySymbol)
Return asm Is Compilation.Assembly
End Function
......
// 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.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
namespace Roslyn.Test.Utilities
{
internal class TestMissingMetadataReferenceResolver : MetadataReferenceResolver
{
private readonly Dictionary<string, MetadataReference> _map;
public readonly List<AssemblyIdentity> ResolutionAttempts = new List<AssemblyIdentity>();
public TestMissingMetadataReferenceResolver(Dictionary<string, MetadataReference> map)
{
_map = map;
}
public override PortableExecutableReference ResolveMissingAssembly(AssemblyIdentity identity)
{
ResolutionAttempts.Add(identity);
MetadataReference reference;
string nameAndVersion = identity.Name + (identity.Version != AssemblyIdentity.NullVersion ? $", {identity.Version}" : "");
return _map.TryGetValue(nameAndVersion, out reference) ? (PortableExecutableReference)reference : null;
}
public override bool ResolveMissingAssemblies => true;
public override bool Equals(object other) => true;
public override int GetHashCode() => 1;
public override ImmutableArray<PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties) => default(ImmutableArray<PortableExecutableReference>);
}
}
......@@ -52,6 +52,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Metadata\TypeAttributesMissing.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Metadata\PEModuleTestHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\TestDocumentationCommentProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\TestMissingMetadataReferenceResolver.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\VirtualizedRelativePathResolver.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Pdb\MockSymWriter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Pdb\PdbTestUtilities.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册