Skip to content

Commit 833f5e2

Browse files
committed
Cleanup
1 parent 51de45f commit 833f5e2

File tree

6 files changed

+107
-151
lines changed

6 files changed

+107
-151
lines changed

src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,8 +3598,19 @@ internal override bool CompileMethods(
35983598
return true;
35993599
}
36003600

3601-
private protected override EmitBaseline MapToCompilation(CommonPEModuleBuilder moduleBeingBuilt)
3602-
=> EmitHelpers.MapToCompilation(this, (PEDeltaAssemblyBuilder)moduleBeingBuilt);
3601+
private protected override SymbolMatcher CreatePreviousToCurrentSourceAssemblyMatcher(
3602+
EmitBaseline previousGeneration,
3603+
SynthesizedTypeMaps otherSynthesizedTypes,
3604+
IReadOnlyDictionary<ISymbolInternal, ImmutableArray<ISymbolInternal>> otherSynthesizedMembers,
3605+
IReadOnlyDictionary<ISymbolInternal, ImmutableArray<ISymbolInternal>> otherDeletedMembers)
3606+
{
3607+
return new CSharpSymbolMatcher(
3608+
sourceAssembly: ((CSharpCompilation)previousGeneration.Compilation).SourceAssembly,
3609+
SourceAssembly,
3610+
otherSynthesizedTypes,
3611+
otherSynthesizedMembers,
3612+
otherDeletedMembers);
3613+
}
36033614

36043615
private class DuplicateFilePathsVisitor : CSharpSymbolVisitor
36053616
{

src/Compilers/CSharp/Portable/Emitter/EditAndContinue/EmitHelpers.cs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using System.Collections.Immutable;
87
using System.Diagnostics;
98
using System.IO;
109
using System.Reflection.Metadata;
@@ -15,7 +14,6 @@
1514
using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE;
1615
using Microsoft.CodeAnalysis.Emit;
1716
using Microsoft.CodeAnalysis.PooledObjects;
18-
using Roslyn.Utilities;
1917

2018
namespace Microsoft.CodeAnalysis.CSharp.Emit
2119
{
@@ -178,69 +176,5 @@ private static bool GetPredefinedHotReloadExceptionTypeConstructor(CSharpCompila
178176

179177
return false;
180178
}
181-
182-
/// <summary>
183-
/// Return a version of the baseline with all definitions mapped to this compilation.
184-
/// Definitions from the initial generation, from metadata, are not mapped since
185-
/// the initial generation is always included as metadata. That is, the symbols from
186-
/// types, methods, ... in the TypesAdded, MethodsAdded, ... collections are replaced
187-
/// by the corresponding symbols from the current compilation.
188-
/// </summary>
189-
internal static EmitBaseline MapToCompilation(
190-
CSharpCompilation compilation,
191-
PEDeltaAssemblyBuilder moduleBeingBuilt)
192-
{
193-
var previousGeneration = moduleBeingBuilt.PreviousGeneration;
194-
RoslynDebug.Assert(previousGeneration.Compilation != compilation);
195-
196-
if (previousGeneration.Ordinal == 0)
197-
{
198-
// Initial generation, nothing to map. (Since the initial generation
199-
// is always loaded from metadata in the context of the current
200-
// compilation, there's no separate mapping step.)
201-
return previousGeneration;
202-
}
203-
204-
RoslynDebug.AssertNotNull(previousGeneration.Compilation);
205-
RoslynDebug.AssertNotNull(previousGeneration.PEModuleBuilder);
206-
RoslynDebug.AssertNotNull(moduleBeingBuilt.EncSymbolChanges);
207-
208-
var currentSynthesizedTypes = moduleBeingBuilt.GetAllSynthesizedTypes();
209-
var currentSynthesizedMembers = moduleBeingBuilt.GetAllSynthesizedMembers();
210-
var currentDeletedMembers = moduleBeingBuilt.EncSymbolChanges.DeletedMembers;
211-
212-
// Mapping from previous compilation to the current.
213-
var previousSourceAssembly = ((CSharpCompilation)previousGeneration.Compilation).SourceAssembly;
214-
215-
var matcher = new CSharpSymbolMatcher(
216-
sourceAssembly: previousSourceAssembly,
217-
otherAssembly: compilation.SourceAssembly,
218-
otherSynthesizedTypes: currentSynthesizedTypes,
219-
otherSynthesizedMembers: currentSynthesizedMembers,
220-
otherDeletedMembers: currentDeletedMembers);
221-
222-
var mappedSynthesizedTypes = matcher.MapSynthesizedTypes(previousGeneration.SynthesizedTypes, currentSynthesizedTypes);
223-
224-
var mappedSynthesizedMembers = matcher.MapSynthesizedOrDeletedMembers(previousGeneration.SynthesizedMembers, currentSynthesizedMembers, isDeletedMemberMapping: false);
225-
226-
// Deleted members are mapped the same way as synthesized members, so we can just call the same method.
227-
var mappedDeletedMembers = matcher.MapSynthesizedOrDeletedMembers(previousGeneration.DeletedMembers, currentDeletedMembers, isDeletedMemberMapping: true);
228-
229-
// TODO: can we reuse some data from the previous matcher?
230-
var matcherWithAllSynthesizedTypesAndMembers = new CSharpSymbolMatcher(
231-
sourceAssembly: previousSourceAssembly,
232-
otherAssembly: compilation.SourceAssembly,
233-
otherSynthesizedTypes: mappedSynthesizedTypes,
234-
otherSynthesizedMembers: mappedSynthesizedMembers,
235-
otherDeletedMembers: mappedDeletedMembers);
236-
237-
return matcherWithAllSynthesizedTypesAndMembers.MapBaselineToCompilation(
238-
previousGeneration,
239-
compilation,
240-
moduleBeingBuilt,
241-
mappedSynthesizedTypes,
242-
mappedSynthesizedMembers,
243-
mappedDeletedMembers);
244-
}
245179
}
246180
}

src/Compilers/Core/Portable/Compilation/Compilation.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3435,7 +3435,70 @@ internal static bool SerializePeToStream(
34353435
return true;
34363436
}
34373437

3438-
private protected abstract EmitBaseline MapToCompilation(CommonPEModuleBuilder moduleBeingBuilt);
3438+
/// <summary>
3439+
/// Return a version of the baseline with all definitions mapped to this compilation.
3440+
/// Definitions from the initial generation, from metadata, are not mapped since
3441+
/// the initial generation is always included as metadata. That is, the symbols from
3442+
/// types, methods, ... in the TypesAdded, MethodsAdded, ... collections are replaced
3443+
/// by the corresponding symbols from the current compilation.
3444+
/// </summary>
3445+
internal EmitBaseline MapToCompilation(CommonPEModuleBuilder moduleBeingBuilt)
3446+
{
3447+
var previousGeneration = moduleBeingBuilt.PreviousGeneration;
3448+
Debug.Assert(previousGeneration != null);
3449+
Debug.Assert(previousGeneration.Compilation != this);
3450+
3451+
if (previousGeneration.Ordinal == 0)
3452+
{
3453+
// Initial generation, nothing to map. (Since the initial generation
3454+
// is always loaded from metadata in the context of the current
3455+
// compilation, there's no separate mapping step.)
3456+
return previousGeneration;
3457+
}
3458+
3459+
Debug.Assert(previousGeneration.Compilation != null);
3460+
Debug.Assert(previousGeneration.PEModuleBuilder != null);
3461+
Debug.Assert(moduleBeingBuilt.EncSymbolChanges != null);
3462+
3463+
var currentSynthesizedTypes = moduleBeingBuilt.GetAllSynthesizedTypes();
3464+
var currentSynthesizedMembers = moduleBeingBuilt.GetAllSynthesizedMembers();
3465+
var currentDeletedMembers = moduleBeingBuilt.EncSymbolChanges.DeletedMembers;
3466+
3467+
// Mapping from previous compilation to the current.
3468+
var matcher = CreatePreviousToCurrentSourceAssemblyMatcher(
3469+
previousGeneration,
3470+
currentSynthesizedTypes,
3471+
currentSynthesizedMembers,
3472+
currentDeletedMembers);
3473+
3474+
var mappedSynthesizedTypes = matcher.MapSynthesizedTypes(previousGeneration.SynthesizedTypes, currentSynthesizedTypes);
3475+
3476+
var mappedSynthesizedMembers = matcher.MapSynthesizedOrDeletedMembers(previousGeneration.SynthesizedMembers, currentSynthesizedMembers, isDeletedMemberMapping: false);
3477+
3478+
// Deleted members are mapped the same way as synthesized members, so we can just call the same method.
3479+
var mappedDeletedMembers = matcher.MapSynthesizedOrDeletedMembers(previousGeneration.DeletedMembers, currentDeletedMembers, isDeletedMemberMapping: true);
3480+
3481+
// TODO: can we reuse some data from the previous matcher?
3482+
var matcherWithAllSynthesizedTypesAndMembers = CreatePreviousToCurrentSourceAssemblyMatcher(
3483+
previousGeneration,
3484+
mappedSynthesizedTypes,
3485+
mappedSynthesizedMembers,
3486+
mappedDeletedMembers);
3487+
3488+
return matcherWithAllSynthesizedTypesAndMembers.MapBaselineToCompilation(
3489+
previousGeneration,
3490+
this,
3491+
moduleBeingBuilt,
3492+
mappedSynthesizedTypes,
3493+
mappedSynthesizedMembers,
3494+
mappedDeletedMembers);
3495+
}
3496+
3497+
private protected abstract SymbolMatcher CreatePreviousToCurrentSourceAssemblyMatcher(
3498+
EmitBaseline previousGeneration,
3499+
SynthesizedTypeMaps otherSynthesizedTypes,
3500+
IReadOnlyDictionary<ISymbolInternal, ImmutableArray<ISymbolInternal>> otherSynthesizedMembers,
3501+
IReadOnlyDictionary<ISymbolInternal, ImmutableArray<ISymbolInternal>> otherDeletedMembers);
34393502

34403503
internal EmitBaseline? SerializeToDeltaStreams(
34413504
CommonPEModuleBuilder moduleBeingBuilt,

src/Compilers/Core/Portable/Emit/EditAndContinue/SymbolMatcher.cs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.Collections.Immutable;
78
using System.Diagnostics;
@@ -103,16 +104,21 @@ private IReadOnlyDictionary<int, AddedOrChangedMethodInfo> MapAddedOrChangedMeth
103104
return result;
104105
}
105106

106-
private static ImmutableSegmentedDictionary<AnonymousTypeKey, AnonymousTypeValue> MapAnonymousTypes(
107-
ImmutableSegmentedDictionary<AnonymousTypeKey, AnonymousTypeValue> previousTypes,
108-
ImmutableSegmentedDictionary<AnonymousTypeKey, AnonymousTypeValue> newTypes)
107+
/// <summary>
108+
/// Merges anonymous types/delegates generated during lowering, or emit, of the current compilation with aggregate
109+
/// types/delegates from all previous source generations (gen >= 1) similarly to <see cref="MapSynthesizedOrDeletedMembers"/>
110+
/// </summary>
111+
private static ImmutableSegmentedDictionary<TKey, TValue> MapAnonymousTypesAndDelegatesWithUniqueKey<TKey, TValue>(
112+
ImmutableSegmentedDictionary<TKey, TValue> previousTypes,
113+
ImmutableSegmentedDictionary<TKey, TValue> newTypes)
114+
where TKey : IEquatable<TKey>
109115
{
110116
if (previousTypes.Count == 0)
111117
{
112118
return newTypes;
113119
}
114120

115-
var builder = ImmutableSegmentedDictionary.CreateBuilder<AnonymousTypeKey, AnonymousTypeValue>();
121+
var builder = ImmutableSegmentedDictionary.CreateBuilder<TKey, TValue>();
116122
builder.AddRange(newTypes);
117123

118124
foreach (var (key, previousValue) in previousTypes)
@@ -128,31 +134,10 @@ private static ImmutableSegmentedDictionary<AnonymousTypeKey, AnonymousTypeValue
128134
return builder.ToImmutable();
129135
}
130136

131-
private static ImmutableSegmentedDictionary<SynthesizedDelegateKey, SynthesizedDelegateValue> MapAnonymousDelegates(
132-
ImmutableSegmentedDictionary<SynthesizedDelegateKey, SynthesizedDelegateValue> previousDelegates,
133-
ImmutableSegmentedDictionary<SynthesizedDelegateKey, SynthesizedDelegateValue> newDelegates)
134-
{
135-
if (previousDelegates.Count == 0)
136-
{
137-
return newDelegates;
138-
}
139-
140-
var builder = ImmutableSegmentedDictionary.CreateBuilder<SynthesizedDelegateKey, SynthesizedDelegateValue>();
141-
builder.AddRange(newDelegates);
142-
143-
foreach (var (key, previousValue) in previousDelegates)
144-
{
145-
if (newDelegates.ContainsKey(key))
146-
{
147-
continue;
148-
}
149-
150-
builder.Add(key, previousValue);
151-
}
152-
153-
return builder.ToImmutable();
154-
}
155-
137+
/// <summary>
138+
/// Merges anonymous delegates with indexed names generated during lowering, or emit, of the current compilation with aggregate
139+
/// delegates from all previous source generations (gen >= 1) similarly to <see cref="MapSynthesizedOrDeletedMembers"/>
140+
/// </summary>
156141
private ImmutableSegmentedDictionary<AnonymousDelegateWithIndexedNamePartialKey, ImmutableArray<AnonymousTypeValue>> MapAnonymousDelegatesWithIndexedNames(
157142
ImmutableSegmentedDictionary<AnonymousDelegateWithIndexedNamePartialKey, ImmutableArray<AnonymousTypeValue>> previousDelegates,
158143
ImmutableSegmentedDictionary<AnonymousDelegateWithIndexedNamePartialKey, ImmutableArray<AnonymousTypeValue>> newDelegates)
@@ -201,8 +186,8 @@ private ImmutableSegmentedDictionary<AnonymousDelegateWithIndexedNamePartialKey,
201186

202187
internal SynthesizedTypeMaps MapSynthesizedTypes(SynthesizedTypeMaps previousTypes, SynthesizedTypeMaps newTypes)
203188
=> new SynthesizedTypeMaps(
204-
MapAnonymousTypes(previousTypes.AnonymousTypes, newTypes.AnonymousTypes),
205-
MapAnonymousDelegates(previousTypes.AnonymousDelegates, newTypes.AnonymousDelegates),
189+
MapAnonymousTypesAndDelegatesWithUniqueKey(previousTypes.AnonymousTypes, newTypes.AnonymousTypes),
190+
MapAnonymousTypesAndDelegatesWithUniqueKey(previousTypes.AnonymousDelegates, newTypes.AnonymousDelegates),
206191
MapAnonymousDelegatesWithIndexedNames(previousTypes.AnonymousDelegatesWithIndexedNames, newTypes.AnonymousDelegatesWithIndexedNames));
207192

208193
/// <summary>
@@ -218,6 +203,8 @@ internal SynthesizedTypeMaps MapSynthesizedTypes(SynthesizedTypeMaps previousTyp
218203
///
219204
/// Then the resulting collection shall have the following entries:
220205
/// {S' -> {A', B', C, D}, U -> {G, H}, T -> {E, F}}
206+
///
207+
/// Note that the results may include symbols declared in different compilations (previous generations).
221208
/// </remarks>
222209
internal IReadOnlyDictionary<ISymbolInternal, ImmutableArray<ISymbolInternal>> MapSynthesizedOrDeletedMembers(
223210
IReadOnlyDictionary<ISymbolInternal, ImmutableArray<ISymbolInternal>> previousMembers,

src/Compilers/VisualBasic/Portable/Compilation/VisualBasicCompilation.vb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,8 +2534,18 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
25342534
Return True
25352535
End Function
25362536

2537-
Private Protected Overrides Function MapToCompilation(moduleBeingBuilt As CommonPEModuleBuilder) As EmitBaseline
2538-
Return EmitHelpers.MapToCompilation(Me, DirectCast(moduleBeingBuilt, PEDeltaAssemblyBuilder))
2537+
Private Protected Overrides Function CreatePreviousToCurrentSourceAssemblyMatcher(
2538+
previousGeneration As EmitBaseline,
2539+
otherSynthesizedTypes As SynthesizedTypeMaps,
2540+
otherSynthesizedMembers As IReadOnlyDictionary(Of ISymbolInternal, ImmutableArray(Of ISymbolInternal)),
2541+
otherDeletedMembers As IReadOnlyDictionary(Of ISymbolInternal, ImmutableArray(Of ISymbolInternal))) As SymbolMatcher
2542+
2543+
Return New VisualBasicSymbolMatcher(
2544+
sourceAssembly:=DirectCast(previousGeneration.Compilation, VisualBasicCompilation).SourceAssembly,
2545+
otherAssembly:=SourceAssembly,
2546+
otherSynthesizedTypes,
2547+
otherSynthesizedMembers,
2548+
otherDeletedMembers)
25392549
End Function
25402550

25412551
Friend Overrides Function GenerateResources(

src/Compilers/VisualBasic/Portable/Emit/EditAndContinue/EmitHelpers.vb

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -166,54 +166,5 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Emit
166166

167167
Return False
168168
End Function
169-
170-
Friend Function MapToCompilation(
171-
compilation As VisualBasicCompilation,
172-
moduleBeingBuilt As PEDeltaAssemblyBuilder) As EmitBaseline
173-
174-
Dim previousGeneration = moduleBeingBuilt.PreviousGeneration
175-
Debug.Assert(previousGeneration.Compilation IsNot compilation)
176-
177-
If previousGeneration.Ordinal = 0 Then
178-
' Initial generation, nothing to map. (Since the initial generation
179-
' is always loaded from metadata in the context of the current
180-
' compilation, there's no separate mapping step.)
181-
Return previousGeneration
182-
End If
183-
184-
Dim currentSynthesizedTypes = moduleBeingBuilt.GetAllSynthesizedTypes()
185-
Dim currentSynthesizedMembers = moduleBeingBuilt.GetAllSynthesizedMembers()
186-
Dim currentDeletedMembers = moduleBeingBuilt.EncSymbolChanges.DeletedMembers
187-
188-
' Mapping from previous compilation to the current.
189-
Dim previousSourceAssembly = DirectCast(previousGeneration.Compilation, VisualBasicCompilation).SourceAssembly
190-
191-
Dim matcher = New VisualBasicSymbolMatcher(
192-
sourceAssembly:=previousSourceAssembly,
193-
otherAssembly:=compilation.SourceAssembly,
194-
otherSynthesizedTypes:=currentSynthesizedTypes,
195-
otherSynthesizedMembers:=currentSynthesizedMembers,
196-
otherDeletedMembers:=currentDeletedMembers)
197-
198-
Dim mappedSynthesizedTypes = matcher.MapSynthesizedTypes(previousGeneration.SynthesizedTypes, currentSynthesizedTypes)
199-
Dim mappedSynthesizedMembers = matcher.MapSynthesizedOrDeletedMembers(previousGeneration.SynthesizedMembers, currentSynthesizedMembers, isDeletedMemberMapping:=False)
200-
Dim mappedDeletedMembers = matcher.MapSynthesizedOrDeletedMembers(previousGeneration.DeletedMembers, currentDeletedMembers, isDeletedMemberMapping:=True)
201-
202-
' TODO can we reuse some data from the previous matcher?
203-
Dim matcherWithAllSynthesizedTypesAndMembers = New VisualBasicSymbolMatcher(
204-
sourceAssembly:=previousSourceAssembly,
205-
otherAssembly:=compilation.SourceAssembly,
206-
otherSynthesizedTypes:=mappedSynthesizedTypes,
207-
otherSynthesizedMembers:=mappedSynthesizedMembers,
208-
otherDeletedMembers:=mappedDeletedMembers)
209-
210-
Return matcherWithAllSynthesizedTypesAndMembers.MapBaselineToCompilation(
211-
previousGeneration,
212-
compilation,
213-
moduleBeingBuilt,
214-
mappedSynthesizedTypes,
215-
mappedSynthesizedMembers,
216-
mappedDeletedMembers)
217-
End Function
218169
End Module
219170
End Namespace

0 commit comments

Comments
 (0)