Browse Source

Fixed SD2-1420: Variables of type 'char' are converted incorrectly from C# to VB

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3123 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 18 years ago
parent
commit
51b80f6f6d
  1. 14
      src/Libraries/NRefactory/Project/NRefactory.csproj
  2. 58
      src/Libraries/NRefactory/Project/Src/AstBuilder/ExpressionBuilder.cs
  3. 61
      src/Libraries/NRefactory/Project/Src/AstBuilder/StatementBuilder.cs
  4. 7
      src/Main/Base/Test/CodeConverterTests.cs
  5. 45
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/CSharpToVBNetConvertVisitor.cs
  6. 4
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/VBNetToCSharpConvertVisitor.cs
  7. 21
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/CodeGenerator.cs

14
src/Libraries/NRefactory/Project/NRefactory.csproj

@ -23,19 +23,19 @@ @@ -23,19 +23,19 @@
<FileAlignment>4096</FileAlignment>
<RunCodeAnalysis>False</RunCodeAnalysis>
<CodeAnalysisRules>-Microsoft.Design#CA1002;-Microsoft.Design#CA1020;-Microsoft.Design#CA1051;-Microsoft.Design#CA1062;-Microsoft.Globalization#CA1303;-Microsoft.Globalization#CA1305;-Microsoft.Naming#CA1704;-Microsoft.Performance#CA1800;-Microsoft.Performance#CA1805;-Microsoft.Usage#CA2211;-Microsoft.Usage#CA2227</CodeAnalysisRules>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>TEST;DEBUG</DefineConstants>
<DefineConstants>TEST;DEBUG;NET35</DefineConstants>
<OutputPath>..\..\..\..\bin\</OutputPath>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TEST</DefineConstants>
<DefineConstants>TEST;NET35</DefineConstants>
<OutputPath>..\..\..\..\bin\</OutputPath>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
@ -49,11 +49,16 @@ @@ -49,11 +49,16 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="Resources\ICSharpCode.NRefactory.snk" />
<None Include="Src\Lexer\BuildKeywords.pl" />
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\AstBuilder\ExpressionBuilder.cs" />
<Compile Include="Src\AstBuilder\StatementBuilder.cs" />
<Compile Include="Src\Lexer\AbstractLexer.cs" />
<Compile Include="Src\Lexer\CSharp\ConditionalCompilation.cs" />
<Compile Include="Src\Lexer\CSharp\Keywords.cs" />
@ -152,6 +157,9 @@ @@ -152,6 +157,9 @@
<Folder Include="Src\Ast" />
<Folder Include="Src\Ast\General" />
</ItemGroup>
<ItemGroup>
<Folder Include="Src\AstBuilder" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>..\src\Tools\UpdateAssemblyInfo\bin\Debug\UpdateAssemblyInfo.exe</PreBuildEvent>

58
src/Libraries/NRefactory/Project/Src/AstBuilder/ExpressionBuilder.cs

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.NRefactory.AstBuilder
{
#if NET35
/// <summary>
/// Extension methods for NRefactory.Ast.Expression.
/// </summary>
public static class ExpressionBuilder
{
public static IdentifierExpression Identifier(string identifier)
{
return new IdentifierExpression(identifier);
}
public static MemberReferenceExpression Member(this Expression targetObject, string memberName)
{
if (targetObject == null)
throw new ArgumentNullException("targetObject");
return new MemberReferenceExpression(targetObject, memberName);
}
public static InvocationExpression Call(this Expression callTarget, string methodName, params Expression[] arguments)
{
if (callTarget == null)
throw new ArgumentNullException("callTarget");
return Call(Member(callTarget, methodName), arguments);
}
public static InvocationExpression Call(this Expression callTarget, params Expression[] arguments)
{
if (callTarget == null)
throw new ArgumentNullException("callTarget");
if (arguments == null)
throw new ArgumentNullException("arguments");
return new InvocationExpression(callTarget, new List<Expression>(arguments));
}
public static ObjectCreateExpression New(this TypeReference createType, params Expression[] arguments)
{
if (createType == null)
throw new ArgumentNullException("createType");
if (arguments == null)
throw new ArgumentNullException("arguments");
return new ObjectCreateExpression(createType, new List<Expression>(arguments));
}
}
#endif
}

61
src/Libraries/NRefactory/Project/Src/AstBuilder/StatementBuilder.cs

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.NRefactory.AstBuilder
{
#if NET35
/// <summary>
/// Extension methods for NRefactory.Ast.Expression.
/// </summary>
public static class StatementBuilder
{
public static void AddStatement(this BlockStatement block, Statement statement)
{
if (block == null)
throw new ArgumentNullException("block");
if (statement == null)
throw new ArgumentNullException("statement");
block.AddChild(statement);
statement.Parent = block;
}
public static void AddStatement(this BlockStatement block, Expression expressionStatement)
{
if (expressionStatement == null)
throw new ArgumentNullException("expressionStatement");
AddStatement(block, new ExpressionStatement(expressionStatement));
}
public static void Throw(this BlockStatement block, Expression expression)
{
if (expression == null)
throw new ArgumentNullException("expression");
AddStatement(block, new ThrowStatement(expression));
}
public static void Return(this BlockStatement block, Expression expression)
{
if (expression == null)
throw new ArgumentNullException("expression");
AddStatement(block, new ReturnStatement(expression));
}
public static void Assign(this BlockStatement block, Expression left, Expression right)
{
if (left == null)
throw new ArgumentNullException("left");
if (right == null)
throw new ArgumentNullException("right");
AddStatement(block, new AssignmentExpression(left, AssignmentOperatorType.Assign, right));
}
}
#endif
}

7
src/Main/Base/Test/CodeConverterTests.cs

@ -517,6 +517,13 @@ namespace ICSharpCode.SharpDevelop.Tests @@ -517,6 +517,13 @@ namespace ICSharpCode.SharpDevelop.Tests
TestStatementsCS2VB("IDisposable ex = (IDisposable)obj;\n",
"Dim ex As IDisposable = DirectCast(obj, IDisposable)\n");
}
[Test]
public void CastIntegerToChar()
{
TestStatementsCS2VB("char c = (char)42;",
"Dim c As Char = ChrW(42)");
}
#endregion
#region MoveUsingOutOfNamespace

45
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/CSharpToVBNetConvertVisitor.cs

@ -9,6 +9,7 @@ using System; @@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.AstBuilder;
using ICSharpCode.NRefactory.Visitors;
using System.Runtime.InteropServices;
@ -351,10 +352,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -351,10 +352,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
Expression CreateExplicitConversionToString(Expression expr)
{
InvocationExpression ie = new InvocationExpression(
new MemberReferenceExpression(new IdentifierExpression("Convert"), "ToString"));
ie.Arguments.Add(expr);
return ie;
return new IdentifierExpression("Convert").Call("ToString", expr);
}
public override object VisitIdentifierExpression(IdentifierExpression identifierExpression, object data)
@ -411,11 +409,10 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -411,11 +409,10 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
} else if (rr != null && rr.ResolvedType != null) {
IClass c = rr.ResolvedType.GetUnderlyingClass();
if (c != null && c.ClassType == ClassType.Delegate) {
InvocationExpression invocation = new InvocationExpression(
new MemberReferenceExpression(
new IdentifierExpression("Delegate"),
assignmentExpression.Op == AssignmentOperatorType.Add ? "Combine" : "Remove"));
invocation.Arguments.Add(assignmentExpression.Left);
InvocationExpression invocation =
new IdentifierExpression("Delegate").Call(
assignmentExpression.Op == AssignmentOperatorType.Add ? "Combine" : "Remove",
assignmentExpression.Left);
invocation.Arguments.Add(assignmentExpression.Right);
assignmentExpression.Op = AssignmentOperatorType.Assign;
@ -434,14 +431,22 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -434,14 +431,22 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
if (resolver.CompilationUnit == null)
return null;
// cast to value type is a conversion
if (castExpression.CastType == CastType.Cast) {
IReturnType rt = ResolveType(castExpression.CastTo);
if (rt != null) {
IClass c = rt.GetUnderlyingClass();
if (c != null && (c.ClassType == ClassType.Struct || c.ClassType == ClassType.Enum)) {
if (castExpression.CastType != CastType.TryCast) {
IReturnType targetType = ResolveType(castExpression.CastTo);
if (targetType != null) {
IClass targetClass = targetType.GetUnderlyingClass();
if (targetClass != null && (targetClass.ClassType == ClassType.Struct || targetClass.ClassType == ClassType.Enum)) {
// cast to value type is a conversion
castExpression.CastType = CastType.Conversion;
}
if (targetClass != null && targetClass.FullyQualifiedName == "System.Char") {
// C# cast to char is done using ChrW function
ResolveResult sourceRR = resolver.ResolveInternal(castExpression.Expression, ExpressionContext.Default);
IReturnType sourceType = sourceRR != null ? sourceRR.ResolvedType : null;
if (IsInteger(sourceType)) {
ReplaceCurrentNode(new IdentifierExpression("ChrW").Call(castExpression.Expression));
}
}
}
}
return null;
@ -452,20 +457,14 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -452,20 +457,14 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
base.VisitUnaryOperatorExpression(unaryOperatorExpression, data);
switch (unaryOperatorExpression.Op) {
case UnaryOperatorType.Dereference:
ReplaceCurrentNode(new MemberReferenceExpression(unaryOperatorExpression.Expression, "Target") {
StartLocation = unaryOperatorExpression.StartLocation,
EndLocation = unaryOperatorExpression.EndLocation
});
ReplaceCurrentNode(unaryOperatorExpression.Expression.Member("Target"));
break;
case UnaryOperatorType.AddressOf:
ResolveResult rr = resolver.ResolveInternal(unaryOperatorExpression.Expression, ExpressionContext.Default);
if (rr != null && rr.ResolvedType != null) {
TypeReference targetType = Refactoring.CodeGenerator.ConvertType(rr.ResolvedType, CreateContext());
TypeReference pointerType = new TypeReference("Pointer", new List<TypeReference> { targetType });
ReplaceCurrentNode(new ObjectCreateExpression(pointerType, new List<Expression> { unaryOperatorExpression.Expression }) {
StartLocation = unaryOperatorExpression.StartLocation,
EndLocation = unaryOperatorExpression.EndLocation
});
ReplaceCurrentNode(pointerType.New(unaryOperatorExpression.Expression));
}
break;
}

4
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/VBNetToCSharpConvertVisitor.cs

@ -9,6 +9,7 @@ using System; @@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.AstBuilder;
using ICSharpCode.NRefactory.Visitors;
namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
@ -169,8 +170,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -169,8 +170,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
// the VB compiler automatically adds the InitializeComponents() call to the
// default constructor, so the converter has to add the call when creating an explicit
// constructor
cd.Body.Children.Add(new ExpressionStatement(
new InvocationExpression(new IdentifierExpression("InitializeComponent"))));
cd.Body.AddStatement(new IdentifierExpression("InitializeComponent").Call());
}
return cd;
}

21
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/CodeGenerator.cs

@ -10,6 +10,7 @@ using System.Collections.Generic; @@ -10,6 +10,7 @@ using System.Collections.Generic;
using System.Text;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.AstBuilder;
using NR = ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.SharpDevelop.Dom.Refactoring
@ -164,7 +165,7 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring @@ -164,7 +165,7 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring
public static BlockStatement CreateNotImplementedBlock()
{
BlockStatement b = new BlockStatement();
b.AddChild(new ThrowStatement(new ObjectCreateExpression(new TypeReference("NotImplementedException"), null)));
b.Throw(new TypeReference("NotImplementedException").New());
return b;
}
@ -358,14 +359,12 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring @@ -358,14 +359,12 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring
property.TypeReference = ConvertType(field.ReturnType, new ClassFinder(field));
if (createGetter) {
BlockStatement block = new BlockStatement();
block.AddChild(new ReturnStatement(new IdentifierExpression(field.Name)));
block.Return(new IdentifierExpression(field.Name));
property.GetRegion = new PropertyGetRegion(block, null);
}
if (createSetter) {
BlockStatement block = new BlockStatement();
Expression left = new IdentifierExpression(field.Name);
Expression right = new IdentifierExpression("value");
block.AddChild(new ExpressionStatement(new AssignmentExpression(left, AssignmentOperatorType.Assign, right)));
block.Assign(new IdentifierExpression(field.Name), new IdentifierExpression("value"));
property.SetRegion = new PropertySetRegion(block, null);
}
@ -391,7 +390,7 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring @@ -391,7 +390,7 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring
arguments.Add(new PrimitiveExpression(null, "null"));
else
arguments.Add(new ThisReferenceExpression());
arguments.Add(new MemberReferenceExpression(new IdentifierExpression("EventArgs"), "Empty"));
arguments.Add(new IdentifierExpression("EventArgs").Member("Empty"));
InsertCodeAtEnd(property.SetterRegion, document,
new RaiseEventStatement(name, arguments));
}
@ -620,18 +619,14 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring @@ -620,18 +619,14 @@ namespace ICSharpCode.SharpDevelop.Dom.Refactoring
}
PropertyDeclaration property = node as PropertyDeclaration;
if (property != null) {
Expression field = new MemberReferenceExpression(new BaseReferenceExpression(),
property.Name);
Expression field = new BaseReferenceExpression().Member(property.Name);
if (!property.GetRegion.Block.IsNull) {
property.GetRegion.Block.Children.Clear();
property.GetRegion.Block.AddChild(new ReturnStatement(field));
property.GetRegion.Block.Return(field);
}
if (!property.SetRegion.Block.IsNull) {
property.SetRegion.Block.Children.Clear();
Expression expr = new AssignmentExpression(field,
AssignmentOperatorType.Assign,
new IdentifierExpression("value"));
property.SetRegion.Block.AddChild(new ExpressionStatement(expr));
property.SetRegion.Block.Assign(field, new IdentifierExpression("value"));
}
}
}

Loading…
Cancel
Save