diff --git a/src/Libraries/NRefactory/Project/NRefactory.csproj b/src/Libraries/NRefactory/Project/NRefactory.csproj index f355c70f7f..1819cabcdd 100644 --- a/src/Libraries/NRefactory/Project/NRefactory.csproj +++ b/src/Libraries/NRefactory/Project/NRefactory.csproj @@ -23,19 +23,19 @@ 4096 False -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 - v2.0 + v3.5 False True - TEST;DEBUG + TEST;DEBUG;NET35 ..\..\..\..\bin\ false True False - TEST + TEST;NET35 ..\..\..\..\bin\ false @@ -49,11 +49,16 @@ + + 3.5 + + + @@ -152,6 +157,9 @@ + + + ..\src\Tools\UpdateAssemblyInfo\bin\Debug\UpdateAssemblyInfo.exe diff --git a/src/Libraries/NRefactory/Project/Src/AstBuilder/ExpressionBuilder.cs b/src/Libraries/NRefactory/Project/Src/AstBuilder/ExpressionBuilder.cs new file mode 100644 index 0000000000..373c84e98d --- /dev/null +++ b/src/Libraries/NRefactory/Project/Src/AstBuilder/ExpressionBuilder.cs @@ -0,0 +1,58 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Ast; + +namespace ICSharpCode.NRefactory.AstBuilder +{ + #if NET35 + /// + /// Extension methods for NRefactory.Ast.Expression. + /// + 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(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(arguments)); + } + } + #endif +} diff --git a/src/Libraries/NRefactory/Project/Src/AstBuilder/StatementBuilder.cs b/src/Libraries/NRefactory/Project/Src/AstBuilder/StatementBuilder.cs new file mode 100644 index 0000000000..28117b9537 --- /dev/null +++ b/src/Libraries/NRefactory/Project/Src/AstBuilder/StatementBuilder.cs @@ -0,0 +1,61 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Ast; + +namespace ICSharpCode.NRefactory.AstBuilder +{ + #if NET35 + /// + /// Extension methods for NRefactory.Ast.Expression. + /// + 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 +} diff --git a/src/Main/Base/Test/CodeConverterTests.cs b/src/Main/Base/Test/CodeConverterTests.cs index 42a13b6554..1e42ead7c3 100644 --- a/src/Main/Base/Test/CodeConverterTests.cs +++ b/src/Main/Base/Test/CodeConverterTests.cs @@ -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 diff --git a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/CSharpToVBNetConvertVisitor.cs b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/CSharpToVBNetConvertVisitor.cs index 97eb48bbd0..ae6bbbc289 100644 --- a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/CSharpToVBNetConvertVisitor.cs +++ b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/CSharpToVBNetConvertVisitor.cs @@ -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 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 } 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 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 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 { targetType }); - ReplaceCurrentNode(new ObjectCreateExpression(pointerType, new List { unaryOperatorExpression.Expression }) { - StartLocation = unaryOperatorExpression.StartLocation, - EndLocation = unaryOperatorExpression.EndLocation - }); + ReplaceCurrentNode(pointerType.New(unaryOperatorExpression.Expression)); } break; } diff --git a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/VBNetToCSharpConvertVisitor.cs b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/VBNetToCSharpConvertVisitor.cs index 4c4b86de2f..465d5af1c6 100644 --- a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/VBNetToCSharpConvertVisitor.cs +++ b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/VBNetToCSharpConvertVisitor.cs @@ -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 // 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; } diff --git a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/CodeGenerator.cs b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/CodeGenerator.cs index d48da2afdf..95cc86ee3a 100644 --- a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/CodeGenerator.cs +++ b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Refactoring/CodeGenerator.cs @@ -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 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 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 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 } 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")); } } }