Skip to content

Commit 6847a13

Browse files
Dedupe logic
1 parent d453053 commit 6847a13

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

CodeConverter/CSharp/NameExpressionNodeVisitor.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public async Task<CSharpSyntaxNode> ConvertMemberAccessExpressionAsync(VBasic.Sy
8787
if (IsSubPartOfConditionalAccess(node)) {
8888
return isDefaultProperty ? SyntaxFactory.ElementBindingExpression()
8989
: await AdjustForImplicitInvocationAsync(node, SyntaxFactory.MemberBindingExpression(simpleNameSyntax));
90-
} else if (node.IsParentKind(Microsoft.CodeAnalysis.VisualBasic.SyntaxKind.NamedFieldInitializer)) {
90+
} else if (node.IsParentKind(VBasic.SyntaxKind.NamedFieldInitializer)) {
9191
return ValidSyntaxFactory.IdentifierName(_tempNameForAnonymousScope[node.Name.Identifier.Text].Peek().TempName);
9292
}
9393
left = _withBlockLhs.Peek();
@@ -292,21 +292,10 @@ private async Task<ExpressionSyntax> ConvertInvocationAsync(VBSyntax.InvocationE
292292
var refKind = CommonConversions.GetCsRefKind(thisParam);
293293

294294
bool requiresHoist = false;
295-
SemanticModelExtensions.RefConversion refConversion = SemanticModelExtensions.RefConversion.Inline;
295+
RefConversion refConversion = RefConversion.Inline;
296296
if (refKind != RefKind.None) {
297-
var symbolInfo = _semanticModel.GetSymbolInfoInDocument<ISymbol>(maes.Expression);
298-
if (symbolInfo is IPropertySymbol { ReturnsByRef: false, ReturnsByRefReadonly: false } propertySymbol) {
299-
refConversion = propertySymbol.IsReadOnly ? SemanticModelExtensions.RefConversion.PreAssigment : SemanticModelExtensions.RefConversion.PreAndPostAssignment;
300-
} else if (symbolInfo is IFieldSymbol { IsConst: true } or ILocalSymbol { IsConst: true }) {
301-
refConversion = SemanticModelExtensions.RefConversion.PreAssigment;
302-
} else if (symbolInfo is IMethodSymbol { ReturnsByRef: false, ReturnsByRefReadonly: false }) {
303-
refConversion = SemanticModelExtensions.RefConversion.PreAssigment;
304-
} else {
305-
var typeInfo = _semanticModel.GetTypeInfo(maes.Expression);
306-
bool isTypeMismatch = typeInfo.Type == null || !typeInfo.Type.Equals(typeInfo.ConvertedType, SymbolEqualityComparer.IncludeNullability);
307-
if (isTypeMismatch) refConversion = SemanticModelExtensions.RefConversion.PreAndPostAssignment;
308-
}
309-
requiresHoist = refConversion != SemanticModelExtensions.RefConversion.Inline;
297+
refConversion = _semanticModel.GetRefConversionForExpression(maes.Expression);
298+
requiresHoist = refConversion != RefConversion.Inline;
310299
}
311300

312301
if (requiresStaticInvocation || requiresHoist) {

CodeConverter/CSharp/SemanticModelExtensions.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ public static RefConversion NeedsVariableForArgument(this SemanticModel semantic
7070
if (!(node is VBSyntax.SimpleArgumentSyntax sas) || sas is { Expression: VBSyntax.ParenthesizedExpressionSyntax }) return RefConversion.PreAssigment;
7171
var expression = sas.Expression;
7272

73-
return GetRefConversion(expression);
73+
return semanticModel.GetRefConversionForExpression(expression);
7474

75-
RefConversion GetRefConversion(VBSyntax.ExpressionSyntax expression)
75+
}
76+
77+
public static RefConversion GetRefConversionForExpression(this SemanticModel semanticModel, VBasic.Syntax.ExpressionSyntax expression)
78+
{
79+
RefConversion GetRefConversion(VBSyntax.ExpressionSyntax expr)
7680
{
77-
var symbolInfo = semanticModel.GetSymbolInfoInDocument<ISymbol>(expression);
81+
var symbolInfo = semanticModel.GetSymbolInfoInDocument<ISymbol>(expr);
7882
if (symbolInfo is IPropertySymbol { ReturnsByRef: false, ReturnsByRefReadonly: false } propertySymbol) {
7983
// a property in VB.NET code can be ReturnsByRef if it's defined in a C# assembly the VB.NET code references
8084
return propertySymbol.IsReadOnly ? RefConversion.PreAssigment : RefConversion.PreAndPostAssignment;
@@ -87,10 +91,10 @@ RefConversion GetRefConversion(VBSyntax.ExpressionSyntax expression)
8791

8892
if (DeclaredInUsing(symbolInfo)) return RefConversion.PreAssigment;
8993

90-
if (expression is VBasic.Syntax.IdentifierNameSyntax || expression is VBSyntax.MemberAccessExpressionSyntax ||
91-
IsRefArrayAcces(expression)) {
94+
if (expr is VBasic.Syntax.IdentifierNameSyntax || expr is VBSyntax.MemberAccessExpressionSyntax ||
95+
IsRefArrayAcces(expr)) {
9296

93-
var typeInfo = semanticModel.GetTypeInfo(expression);
97+
var typeInfo = semanticModel.GetTypeInfo(expr);
9498
bool isTypeMismatch = typeInfo.Type == null || !typeInfo.Type.Equals(typeInfo.ConvertedType, SymbolEqualityComparer.IncludeNullability);
9599

96100
if (isTypeMismatch) {
@@ -103,9 +107,9 @@ RefConversion GetRefConversion(VBSyntax.ExpressionSyntax expression)
103107
return RefConversion.PreAssigment;
104108
}
105109

106-
bool IsRefArrayAcces(VBSyntax.ExpressionSyntax expression)
110+
bool IsRefArrayAcces(VBSyntax.ExpressionSyntax expr)
107111
{
108-
if (!(expression is VBSyntax.InvocationExpressionSyntax ies)) return false;
112+
if (!(expr is VBSyntax.InvocationExpressionSyntax ies)) return false;
109113
var op = semanticModel.GetOperation(ies);
110114
return (op.IsArrayElementAccess() || IsReturnsByRefPropertyElementAccess(op))
111115
&& GetRefConversion(ies.Expression) == RefConversion.Inline;
@@ -117,6 +121,8 @@ static bool IsReturnsByRefPropertyElementAccess(IOperation op)
117121
&& (prop.ReturnsByRef || prop.ReturnsByRefReadonly);
118122
}
119123
}
124+
125+
return GetRefConversion((VBSyntax.ExpressionSyntax)expression);
120126
}
121127

122128
private static bool DeclaredInUsing(ISymbol symbolInfo)

0 commit comments

Comments
 (0)