11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4- using System . Reflection ;
54using System . Runtime . CompilerServices ;
65using System . Runtime . InteropServices ;
76using System . Threading . Tasks ;
87using ICSharpCode . CodeConverter . Shared ;
98using ICSharpCode . CodeConverter . Util ;
10- using ICSharpCode . CodeConverter . Util . FromRoslyn ;
119using Microsoft . CodeAnalysis ;
1210using Microsoft . CodeAnalysis . CSharp ;
13- using Microsoft . CodeAnalysis . CSharp . Syntax ;
1411using Microsoft . CodeAnalysis . Editing ;
1512using Microsoft . CodeAnalysis . FindSymbols ;
1613using Microsoft . CodeAnalysis . Operations ;
@@ -64,11 +61,11 @@ public CommonConversions(Document document, SemanticModel semanticModel,
6461 WinformsConversions = new WinformsConversions ( typeContext ) ;
6562 }
6663
67- public async Task < ( IReadOnlyCollection < ( VariableDeclarationSyntax Decl , ITypeSymbol Type ) > Variables , IReadOnlyCollection < CSharpSyntaxNode > Methods ) > SplitVariableDeclarationsAsync (
64+ public async Task < ( IReadOnlyCollection < ( CSSyntax . VariableDeclarationSyntax Decl , ITypeSymbol Type ) > Variables , IReadOnlyCollection < CSharpSyntaxNode > Methods ) > SplitVariableDeclarationsAsync (
6865 VariableDeclaratorSyntax declarator , HashSet < ILocalSymbol > symbolsToSkip = null , bool preferExplicitType = false )
6966 {
7067 var vbInitValue = GetInitializerToConvert ( declarator ) ;
71- var initializerOrMethodDecl = await vbInitValue . AcceptAsync ( TriviaConvertingExpressionVisitor ) ;
68+ var initializerOrMethodDecl = await vbInitValue . AcceptAsync < CSharpSyntaxNode > ( TriviaConvertingExpressionVisitor ) ;
7269 var vbInitializerTypeInfo = vbInitValue != null ? _semanticModel . GetTypeInfo ( vbInitValue ) : default ( TypeInfo ? ) ;
7370 var vbInitializerType = vbInitValue != null ? vbInitializerTypeInfo . Value . Type : default ( ITypeSymbol ) ;
7471
@@ -82,7 +79,7 @@ public CommonConversions(Document document, SemanticModel semanticModel,
8279 requireExplicitTypeForAll |= vbInitIsNothingLiteral || isAnonymousFunction ;
8380 }
8481
85- var csVars = new Dictionary < string , ( VariableDeclarationSyntax Decl , ITypeSymbol Type ) > ( ) ;
82+ var csVars = new Dictionary < string , ( CSSyntax . VariableDeclarationSyntax Decl , ITypeSymbol Type ) > ( ) ;
8683 var csMethods = new List < CSharpSyntaxNode > ( ) ;
8784
8885 foreach ( var name in declarator . Names ) {
@@ -124,7 +121,7 @@ public bool ShouldPreferExplicitType(VBSyntax.ExpressionSyntax exp,
124121 return shouldPreferExplicitType ;
125122 }
126123
127- private async Task < EqualsValueClauseSyntax > ConvertEqualsValueClauseSyntaxAsync (
124+ private async Task < CSSyntax . EqualsValueClauseSyntax > ConvertEqualsValueClauseSyntaxAsync (
128125 VariableDeclaratorSyntax vbDeclarator , VBSyntax . ModifiedIdentifierSyntax vbName ,
129126 VBSyntax . ExpressionSyntax vbInitValue ,
130127 ITypeSymbol declaredSymbolType ,
@@ -136,7 +133,7 @@ private async Task<EqualsValueClauseSyntax> ConvertEqualsValueClauseSyntaxAsync(
136133 bool declaredConst = declaredSymbol is IFieldSymbol fieldSymbol && fieldSymbol . IsConst ||
137134 declaredSymbol is ILocalSymbol localSymbol && localSymbol . IsConst ;
138135
139- EqualsValueClauseSyntax equalsValueClauseSyntax ;
136+ CSSyntax . EqualsValueClauseSyntax equalsValueClauseSyntax ;
140137 if ( await GetInitializerFromNameAndTypeAsync ( declaredSymbolType , vbName , initializerOrMethodDecl ) is ExpressionSyntax
141138 adjustedInitializerExpr )
142139 {
@@ -180,9 +177,9 @@ public static string GetInitialValueFunctionName(VBSyntax.ModifiedIdentifierSynt
180177 return "initial" + vbName . Identifier . ValueText . ToPascalCase ( ) ;
181178 }
182179
183- private VariableDeclarationSyntax CreateVariableDeclaration ( VariableDeclaratorSyntax vbDeclarator , bool preferExplicitType ,
180+ private CSSyntax . VariableDeclarationSyntax CreateVariableDeclaration ( VariableDeclaratorSyntax vbDeclarator , bool preferExplicitType ,
184181 bool requireExplicitTypeForAll , ITypeSymbol vbInitializerType , ITypeSymbol declaredSymbolType ,
185- EqualsValueClauseSyntax equalsValueClauseSyntax , IMethodSymbol initSymbol , CSSyntax . VariableDeclaratorSyntax v )
182+ CSSyntax . EqualsValueClauseSyntax equalsValueClauseSyntax , IMethodSymbol initSymbol , CSSyntax . VariableDeclaratorSyntax v )
186183 {
187184 var requireExplicitType = requireExplicitTypeForAll ||
188185 vbInitializerType != null && ! Equals ( declaredSymbolType , vbInitializerType ) ;
@@ -257,7 +254,7 @@ private async Task<CSharpSyntaxNode> GetInitializerFromNameAndTypeAsync(ITypeSym
257254 var arrayTypeSyntax = ( ( ArrayTypeSyntax ) GetTypeSyntax ( typeSymbol ) ) . WithRankSpecifiers ( rankSpecifiersWithSizes ) ;
258255 if ( rankSpecifiersWithSizes . SelectMany ( ars => ars . Sizes ) . Any ( e => ! e . IsKind ( CSSyntaxKind . OmittedArraySizeExpression ) ) ) {
259256 initializer = SyntaxFactory . ArrayCreationExpression ( arrayTypeSyntax ) ;
260- } else if ( initializer is ImplicitArrayCreationExpressionSyntax iaces && iaces . Initializer != null ) {
257+ } else if ( initializer is CSSyntax . ImplicitArrayCreationExpressionSyntax iaces && iaces . Initializer != null ) {
261258 initializer = SyntaxFactory . ArrayCreationExpression ( arrayTypeSyntax , iaces . Initializer ) ;
262259 }
263260 }
@@ -274,7 +271,7 @@ public SyntaxToken ConvertIdentifier(SyntaxToken id, bool isAttribute = false, S
274271 if ( id . SyntaxTree == _semanticModel . SyntaxTree ) {
275272 var idSymbol = _semanticModel . GetSymbolInfo ( id . Parent ) . Symbol ?? _semanticModel . GetDeclaredSymbol ( id . Parent ) ;
276273 if ( idSymbol != null && ! String . IsNullOrWhiteSpace ( idSymbol . Name ) ) {
277- text = WithDeclarationName ( id , idSymbol , text ) ;
274+ text = WithDeclarationName ( id , idSymbol , text , _typeContext . AssembliesBeingConverted ) ;
278275 var normalizedText = text . WithHalfWidthLatinCharacters ( ) ;
279276 if ( idSymbol . IsConstructor ( ) && isAttribute ) {
280277 text = idSymbol . ContainingType . Name ;
@@ -307,10 +304,32 @@ public SyntaxToken ConvertIdentifier(SyntaxToken id, bool isAttribute = false, S
307304 /// <seealso cref="DeclarationNodeVisitor.WithDeclarationNameCasingAsync(VBSyntax.NamespaceBlockSyntax, ISymbol)"/>
308305 /// <seealso cref="CommonConversions.WithDeclarationNameCasing(TypeSyntax, ITypeSymbol)"/>
309306 /// </summary>
310- private static string WithDeclarationName ( SyntaxToken id , ISymbol idSymbol , string text )
307+ private static string WithDeclarationName ( SyntaxToken id , ISymbol idSymbol , string text , IEnumerable < IAssemblySymbol > assembliesBeingConverted )
311308 {
312309 //This also covers the case when the name is different (in VB you can have method X implements IFoo.Y), but doesn't resolve any resulting name clashes
313- var baseSymbol = idSymbol . IsKind ( SymbolKind . Method ) || idSymbol . IsKind ( SymbolKind . Property ) ? idSymbol . FollowProperty ( s => s . BaseMember ( ) ) . Last ( ) : idSymbol ;
310+ var assemblyIdentities = assembliesBeingConverted . Select ( t => t . Identity ) ;
311+ ISymbol baseSymbol = default ;
312+ var containingType = idSymbol . ContainingType ;
313+
314+ if ( idSymbol . IsKind ( SymbolKind . Method ) || idSymbol . IsKind ( SymbolKind . Property ) )
315+ {
316+ var possibleSymbols = idSymbol . FollowProperty ( s => s . BaseMember ( ) ) ;
317+ foreach ( var possibleSymbol in possibleSymbols )
318+ {
319+ if ( ! assemblyIdentities . Contains ( possibleSymbol . ContainingAssembly . Identity ) && possibleSymbol . ContainingType . Equals ( containingType ) )
320+ {
321+ baseSymbol = possibleSymbol ;
322+ break ;
323+ }
324+
325+ baseSymbol = possibleSymbol ;
326+ }
327+ }
328+ else
329+ {
330+ baseSymbol = idSymbol ;
331+ }
332+
314333 bool isDeclaration = baseSymbol . Locations . Any ( l => l . SourceSpan == id . Span ) ;
315334 bool isPartial = baseSymbol . IsPartialClassDefinition ( ) || baseSymbol . IsPartialMethodDefinition ( ) ||
316335 baseSymbol . IsPartialMethodImplementation ( ) ;
@@ -349,11 +368,13 @@ public SyntaxTokenList ConvertModifiers(SyntaxNode node, IReadOnlyCollection<Syn
349368
350369 private static bool ? RequiresNewKeyword ( ISymbol declaredSymbol )
351370 {
352- if ( ! ( declaredSymbol is IMethodSymbol methodSymbol ) ) return null ;
353- if ( declaredSymbol . IsOverride ) return false ;
354- var methodSignature = methodSymbol . GetUnqualifiedMethodSignature ( true ) ;
355- return declaredSymbol . ContainingType . FollowProperty ( s => s . BaseType ) . Skip ( 1 ) . Any ( t => t . GetMembers ( )
356- . Any ( s => s . Name == declaredSymbol . Name && s is IMethodSymbol m && m . GetUnqualifiedMethodSignature ( true ) == methodSignature ) ) ;
371+ if ( declaredSymbol . IsOverride ) return false ;
372+ if ( declaredSymbol is IPropertySymbol propertySymbol || declaredSymbol is IMethodSymbol methodSymbol ) {
373+ var methodSignature = declaredSymbol . GetUnqualifiedMethodOrPropertySignature ( true ) ;
374+ return declaredSymbol . ContainingType . FollowProperty ( s => s . BaseType ) . Skip ( 1 ) . Any ( t => t . GetMembers ( )
375+ . Any ( s => s . Name == declaredSymbol . Name && ( s is IPropertySymbol || s is IMethodSymbol ) && s . GetUnqualifiedMethodOrPropertySignature ( true ) == methodSignature ) ) ;
376+ }
377+ return null ;
357378 }
358379
359380 private static bool ContextHasIdenticalDefaults ( TokenContext context , TokenContext [ ] contextsWithIdenticalDefaults , ISymbol declaredSymbol )
@@ -511,10 +532,10 @@ private async Task<ExpressionSyntax> IncreaseArrayUpperBoundExpressionAsync(VBSy
511532 {
512533 // These attributes' semantic effects are expressed differently in CSharp.
513534 return await attributeList . Attributes . Where ( a => ! IsExtensionAttribute ( a ) && ! IsOutAttribute ( a ) )
514- . SelectAsync ( async a => ( CSSyntax . AttributeListSyntax ) await a . AcceptAsync ( TriviaConvertingExpressionVisitor ) ) ;
535+ . SelectAsync ( async a => await a . AcceptAsync < CSSyntax . AttributeListSyntax > ( TriviaConvertingExpressionVisitor ) ) ;
515536 }
516537
517- public static AttributeArgumentListSyntax CreateAttributeArgumentList ( params AttributeArgumentSyntax [ ] attributeArgumentSyntaxs )
538+ public static CSSyntax . AttributeArgumentListSyntax CreateAttributeArgumentList ( params CSSyntax . AttributeArgumentSyntax [ ] attributeArgumentSyntaxs )
518539 {
519540 return SyntaxFactory . AttributeArgumentList ( SyntaxFactory . SeparatedList ( attributeArgumentSyntaxs ) ) ;
520541 }
@@ -524,7 +545,7 @@ public static CSSyntax.LocalDeclarationStatementSyntax CreateLocalVariableDeclar
524545 return SyntaxFactory . LocalDeclarationStatement ( CreateVariableDeclarationAndAssignment ( variableName , initValue ) ) ;
525546 }
526547
527- public static VariableDeclarationSyntax CreateVariableDeclarationAndAssignment ( string variableName ,
548+ public static CSSyntax . VariableDeclarationSyntax CreateVariableDeclarationAndAssignment ( string variableName ,
528549 ExpressionSyntax initValue , TypeSyntax explicitType = null )
529550 {
530551 CSSyntax . VariableDeclaratorSyntax variableDeclaratorSyntax = CreateVariableDeclarator ( variableName , initValue ) ;
0 commit comments