|
9 | 9 | using Microsoft.CodeAnalysis.Editing; |
10 | 10 | using Microsoft.CodeAnalysis.FindSymbols; |
11 | 11 | using Microsoft.CodeAnalysis.Operations; |
12 | | -using Microsoft.CodeAnalysis.VisualBasic; |
| 12 | +using VBasic = Microsoft.CodeAnalysis.VisualBasic; |
13 | 13 | using Microsoft.VisualBasic; |
14 | 14 | using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax; |
15 | 15 | using Microsoft.VisualBasic.CompilerServices; |
|
24 | 24 | using TypeSyntax = Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax; |
25 | 25 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
26 | 26 | using System.Linq.Expressions; |
| 27 | +using Microsoft.CodeAnalysis.VisualBasic.Syntax; |
27 | 28 |
|
28 | 29 | namespace ICSharpCode.CodeConverter.CSharp |
29 | 30 | { |
@@ -53,29 +54,42 @@ public ExpressionSyntax AddExplicitConversion(Microsoft.CodeAnalysis.VisualBasic |
53 | 54 | csNode = addParenthesisIfNeeded && (conversionKind == TypeConversionKind.DestructiveCast || conversionKind == TypeConversionKind.NonDestructiveCast) |
54 | 55 | ? VbSyntaxNodeExtensions.ParenthesizeIfPrecedenceCouldChange(vbNode, csNode) |
55 | 56 | : csNode; |
56 | | - return AddExplicitConversion(vbNode, csNode, conversionKind, addParenthesisIfNeeded, isConst, forceSourceType: forceSourceType, forceTargetType: forceTargetType); |
| 57 | + return AddExplicitConversion(vbNode, csNode, conversionKind, addParenthesisIfNeeded, isConst, forceSourceType: forceSourceType, forceTargetType: forceTargetType).Expr; |
57 | 58 | } |
58 | 59 |
|
59 | | - public ExpressionSyntax AddExplicitConversion(Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax vbNode, ExpressionSyntax csNode, TypeConversionKind conversionKind, bool addParenthesisIfNeeded = false, bool isConst = false, ITypeSymbol forceSourceType = null, ITypeSymbol forceTargetType = null) |
| 60 | + public (ExpressionSyntax Expr, bool IsConst) AddExplicitConversion(Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax vbNode, ExpressionSyntax csNode, TypeConversionKind conversionKind, bool addParenthesisIfNeeded = false, bool requiresConst = false, ITypeSymbol forceSourceType = null, ITypeSymbol forceTargetType = null) |
60 | 61 | { |
61 | 62 | var typeInfo = ModelExtensions.GetTypeInfo(_semanticModel, vbNode); |
62 | 63 | var vbType = forceSourceType ?? typeInfo.Type; |
63 | 64 | var vbConvertedType = forceTargetType ?? typeInfo.ConvertedType; |
| 65 | + bool resultConst = false; |
64 | 66 |
|
65 | | - if (isConst && _expressionEvaluator.GetConstantOrNull(vbNode, vbConvertedType, csNode) is ExpressionSyntax constLiteral) { |
66 | | - return constLiteral; |
| 67 | + if (requiresConst) { |
| 68 | + var (constExpression, isCorrectType) = _expressionEvaluator.GetConstantOrNull(vbNode, vbConvertedType, conversionKind, csNode); |
| 69 | + if (isCorrectType) { |
| 70 | + return (constExpression, true); |
| 71 | + } |
| 72 | + if (constExpression != null) { |
| 73 | + csNode = constExpression ?? csNode; |
| 74 | + resultConst = true; |
| 75 | + } |
67 | 76 | } |
68 | 77 |
|
69 | | - switch (conversionKind) |
70 | | - { |
| 78 | + var typeConvertedResult = AddTypeConversion(vbNode, csNode, conversionKind, addParenthesisIfNeeded, vbType, vbConvertedType); |
| 79 | + return (typeConvertedResult, resultConst); |
| 80 | + } |
| 81 | + |
| 82 | + private ExpressionSyntax AddTypeConversion(VBSyntax.ExpressionSyntax vbNode, ExpressionSyntax csNode, TypeConversionKind conversionKind, bool addParenthesisIfNeeded, ITypeSymbol vbType, ITypeSymbol vbConvertedType) |
| 83 | + { |
| 84 | + switch (conversionKind) { |
71 | 85 | case TypeConversionKind.Unknown: |
72 | 86 | case TypeConversionKind.Identity: |
73 | 87 | return addParenthesisIfNeeded ? VbSyntaxNodeExtensions.ParenthesizeIfPrecedenceCouldChange(vbNode, csNode) : csNode; |
74 | 88 | case TypeConversionKind.DestructiveCast: |
75 | 89 | case TypeConversionKind.NonDestructiveCast: |
76 | 90 | return CreateCast(csNode, vbConvertedType); |
77 | 91 | case TypeConversionKind.Conversion: |
78 | | - return AddExplicitConvertTo(vbNode, csNode, vbType, vbConvertedType);; |
| 92 | + return AddExplicitConvertTo(vbNode, csNode, vbType, vbConvertedType); ; |
79 | 93 | case TypeConversionKind.NullableBool: |
80 | 94 | return SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, csNode, |
81 | 95 | LiteralConversions.GetLiteralExpression(true)); |
@@ -120,7 +134,7 @@ public TypeConversionKind AnalyzeConversion(Microsoft.CodeAnalysis.VisualBasic.S |
120 | 134 | } |
121 | 135 | } |
122 | 136 |
|
123 | | - var vbCompilation = (VisualBasicCompilation) _semanticModel.Compilation; |
| 137 | + var vbCompilation = (VBasic.VisualBasicCompilation) _semanticModel.Compilation; |
124 | 138 | var vbConversion = vbCompilation.ClassifyConversion(vbType, vbConvertedType); |
125 | 139 | var csType = GetCSType(vbType, vbNode); |
126 | 140 | var csConvertedType = GetCSType(vbConvertedType); |
@@ -149,7 +163,7 @@ private ITypeSymbol GetCSType(ITypeSymbol vbType, VBSyntax.ExpressionSyntax vbNo |
149 | 163 |
|
150 | 164 | private bool TryAnalyzeCsConversion(Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax vbNode, ITypeSymbol csType, |
151 | 165 | ITypeSymbol csConvertedType, Conversion vbConversion, ITypeSymbol vbConvertedType, ITypeSymbol vbType, |
152 | | - VisualBasicCompilation vbCompilation, bool isConst, out TypeConversionKind typeConversionKind) |
| 166 | + VBasic.VisualBasicCompilation vbCompilation, bool isConst, out TypeConversionKind typeConversionKind) |
153 | 167 | { |
154 | 168 | var csConversion = _csCompilation.ClassifyConversion(csType, csConvertedType); |
155 | 169 |
|
|
0 commit comments