Browse Source

Fixed conversion of VB 'static' variables to C#.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@5721 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Daniel Grunwald 16 years ago
parent
commit
d6067bfc4c
  1. 3
      src/AddIns/Misc/SharpReport/ICSharpCode.Reports.Addin/ICSharpCode.ReportDesigner.addin
  2. 29
      src/Libraries/NRefactory/Project/Src/Visitors/ConvertVisitorBase.cs
  3. 85
      src/Libraries/NRefactory/Project/Src/Visitors/ToCSharpConvertVisitor.cs
  4. 2
      src/Libraries/NRefactory/Project/Src/Visitors/VBNetConstructsConvertVisitor.cs
  5. 8
      src/Libraries/NRefactory/Test/NRefactoryTests.csproj
  6. 95
      src/Libraries/NRefactory/Test/Output/CSharp/VBNetToCSharpConverterTest.cs

3
src/AddIns/Misc/SharpReport/ICSharpCode.Reports.Addin/ICSharpCode.ReportDesigner.addin

@ -1,7 +1,8 @@ @@ -1,7 +1,8 @@
<AddIn name = "SharpDevelopReports"
author = "Forstmeier Peter"
url = "http://www.sharpdevelopreports.net/"
description = "Reporting Tool For SharpDevelop">
description = "Reporting Tool For SharpDevelop"
addInManagerHidden = "preinstalled">
<Manifest>
<Identity name="ICSharpCode.Reports.Addin" version="@ICSharpCode.Reports.Addin.dll"/>

29
src/Libraries/NRefactory/Project/Src/Visitors/ConvertVisitorBase.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.NRefactory.Visitors
@ -24,5 +25,33 @@ namespace ICSharpCode.NRefactory.Visitors @@ -24,5 +25,33 @@ namespace ICSharpCode.NRefactory.Visitors
sibling.Parent.Children.Insert(index + 1, newNode);
newNode.Parent = sibling.Parent;
}
List<KeyValuePair<INode, INode>> insertions = new List<KeyValuePair<INode, INode>>();
protected void InsertBeforeSibling(INode sibling, INode newNode)
{
if (sibling == null) return;
if (!(sibling.Parent is TypeDeclaration))
throw new NotSupportedException();
insertions.Add(new KeyValuePair<INode, INode>(sibling, newNode));
}
public override object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
{
object result = base.VisitTypeDeclaration(typeDeclaration, data);
for (int i = 0; i < insertions.Count; i++) {
if (insertions[i].Key.Parent == typeDeclaration) {
INode sibling = insertions[i].Key;
INode newNode = insertions[i].Value;
int index = sibling.Parent.Children.IndexOf(sibling);
sibling.Parent.Children.Insert(index, newNode);
newNode.Parent = sibling.Parent;
insertions.RemoveAt(i--);
}
}
return result;
}
}
}

85
src/Libraries/NRefactory/Project/Src/Visitors/ToCSharpConvertVisitor.cs

@ -5,9 +5,10 @@ @@ -5,9 +5,10 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.NRefactory.AstBuilder;
using System;
using System.Linq;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.AstBuilder;
namespace ICSharpCode.NRefactory.Visitors
{
@ -147,24 +148,81 @@ namespace ICSharpCode.NRefactory.Visitors @@ -147,24 +148,81 @@ namespace ICSharpCode.NRefactory.Visitors
parent = parent.Parent;
}
if (parent != null) {
INode type = parent.Parent;
if (type != null) {
int pos = type.Children.IndexOf(parent);
if (pos >= 0) {
FieldDeclaration field = new FieldDeclaration(null);
field.TypeReference = localVariableDeclaration.TypeReference;
field.Modifier = Modifiers.Static;
field.Fields = localVariableDeclaration.Variables;
new PrefixFieldsVisitor(field.Fields, "static_" + GetTypeLevelEntityName(parent) + "_").Run(parent);
type.Children.Insert(pos + 1, field);
RemoveCurrentNode();
string fieldPrefix = "static_" + GetTypeLevelEntityName(parent) + "_";
foreach (VariableDeclaration v in localVariableDeclaration.Variables) {
if (!v.Initializer.IsNull) {
string initFieldName = fieldPrefix + v.Name + "_Init";
FieldDeclaration initField = new FieldDeclaration(null);
initField.TypeReference = new TypeReference("Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag");
initField.Modifier = (((AttributedNode)parent).Modifier & Modifiers.Static) | Modifiers.ReadOnly;
Expression initializer = initField.TypeReference.New();
initField.Fields.Add(new VariableDeclaration(initFieldName, initializer));
InsertBeforeSibling(parent, initField);
InsertAfterSibling(localVariableDeclaration, InitStaticVariable(initFieldName, v.Name, v.Initializer, parent.Parent as TypeDeclaration));
}
FieldDeclaration field = new FieldDeclaration(null);
field.TypeReference = localVariableDeclaration.TypeReference;
field.Modifier = ((AttributedNode)parent).Modifier & Modifiers.Static;
field.Fields.Add(new VariableDeclaration(fieldPrefix + v.Name) { TypeReference = v.TypeReference });
InsertBeforeSibling(parent, field);
}
new PrefixFieldsVisitor(localVariableDeclaration.Variables, fieldPrefix).Run(parent);
RemoveCurrentNode();
}
}
return null;
}
INode InitStaticVariable(string initFieldName, string variableName, Expression initializer, TypeDeclaration typeDeclaration)
{
const string helperMethodName = "InitStaticVariableHelper";
if (typeDeclaration != null) {
if (!typeDeclaration.Children.OfType<MethodDeclaration>().Any(m => m.Name == helperMethodName)) {
// add helper method
var helperMethod = new MethodDeclaration {
Name = helperMethodName,
Modifier = Modifiers.Static,
TypeReference = new TypeReference("System.Boolean", true),
Parameters = {
new ParameterDeclarationExpression(new TypeReference("Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag"), "flag")
},
Body = new BlockStatement()
};
BlockStatement trueBlock = new BlockStatement();
BlockStatement elseIfBlock = new BlockStatement();
BlockStatement falseBlock = new BlockStatement();
helperMethod.Body.AddStatement(
new IfElseStatement(ExpressionBuilder.Identifier("flag").Member("State").Operator(BinaryOperatorType.Equality, new PrimitiveExpression(0))) {
TrueStatement = { trueBlock },
ElseIfSections = {
new ElseIfSection(ExpressionBuilder.Identifier("flag").Member("State").Operator(BinaryOperatorType.Equality, new PrimitiveExpression(2)), elseIfBlock)
},
FalseStatement = { falseBlock }
});
trueBlock.Assign(ExpressionBuilder.Identifier("flag").Member("State"), new PrimitiveExpression(2));
trueBlock.Return(new PrimitiveExpression(true));
elseIfBlock.Throw(new TypeReference("Microsoft.VisualBasic.CompilerServices.IncompleteInitialization").New());
falseBlock.Return(new PrimitiveExpression(false));
typeDeclaration.AddChild(helperMethod);
}
}
BlockStatement tryBlock = new BlockStatement();
BlockStatement ifTrueBlock = new BlockStatement();
tryBlock.AddStatement(new IfElseStatement(ExpressionBuilder.Identifier(helperMethodName).Call(ExpressionBuilder.Identifier(initFieldName)), ifTrueBlock));
ifTrueBlock.Assign(ExpressionBuilder.Identifier(variableName), initializer);
BlockStatement finallyBlock = new BlockStatement();
finallyBlock.Assign(ExpressionBuilder.Identifier(initFieldName).Member("State"), new PrimitiveExpression(1));
BlockStatement lockBlock = new BlockStatement();
lockBlock.AddStatement(new TryCatchStatement(tryBlock, null, finallyBlock));
return new LockStatement(ExpressionBuilder.Identifier(initFieldName), lockBlock);
}
public override object VisitWithStatement(WithStatement withStatement, object data)
{
withStatement.Body.AcceptVisitor(new ReplaceWithAccessTransformer(withStatement.Expression), data);
@ -201,8 +259,7 @@ namespace ICSharpCode.NRefactory.Visitors @@ -201,8 +259,7 @@ namespace ICSharpCode.NRefactory.Visitors
static bool IsTypeLevel(INode node)
{
return node is MethodDeclaration || node is PropertyDeclaration || node is EventDeclaration
|| node is OperatorDeclaration || node is FieldDeclaration;
return node.Parent is TypeDeclaration;
}
static string GetTypeLevelEntityName(INode node)

2
src/Libraries/NRefactory/Project/Src/Visitors/VBNetConstructsConvertVisitor.cs

@ -492,7 +492,7 @@ namespace ICSharpCode.NRefactory.Visitors @@ -492,7 +492,7 @@ namespace ICSharpCode.NRefactory.Visitors
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
{
if (AddDefaultValueInitializerToLocalVariableDeclarations) {
if (AddDefaultValueInitializerToLocalVariableDeclarations && (localVariableDeclaration.Modifier & Modifiers.Static) == 0) {
for (int i = 0; i < localVariableDeclaration.Variables.Count; i++) {
VariableDeclaration decl = localVariableDeclaration.Variables[i];
if (decl.FixedArrayInitialization.IsNull && decl.Initializer.IsNull) {

8
src/Libraries/NRefactory/Test/NRefactoryTests.csproj

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
<OutputType>Library</OutputType>
<OutputPath>..\..\..\..\bin\UnitTests\</OutputPath>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
@ -37,6 +37,12 @@ @@ -37,6 +37,12 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>False</DebugSymbols>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="nunit.framework">

95
src/Libraries/NRefactory/Test/Output/CSharp/VBNetToCSharpConverterTest.cs

@ -507,37 +507,112 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -507,37 +507,112 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
public void StaticMethodVariable()
{
TestMember(@"Private Sub Test
Static j As Integer = 0
Static j As Integer
j += 1
End Sub
Private Shared Sub Test2
Static j As Integer
j += 2
End Sub",
@"private void Test()
@"int static_Test_j;
private void Test()
{
static_Test_j += 1;
}
static int static_Test_j = 0;");
static int static_Test2_j;
private static void Test2()
{
static_Test2_j += 2;
}");
}
[Test]
public void StaticMethodVariable2()
public void StaticMethodVariableWithInitialization()
{
TestMember(@"Private Sub Test
Static j As Integer = 0
j += 1
End Sub",
@"readonly Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag static_Test_j_Init = new Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag();
int static_Test_j;
private void Test()
{
lock (static_Test_j_Init) {
try {
if (InitStaticVariableHelper(static_Test_j_Init)) {
static_Test_j = 0;
}
} finally {
static_Test_j_Init.State = 1;
}
}
static_Test_j += 1;
}
static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag flag)
{
if (flag.State == 0) {
flag.State = 2;
return true;
} else if (flag.State == 2) {
throw new Microsoft.VisualBasic.CompilerServices.IncompleteInitialization();
} else {
return false;
}
}");
}
[Test]
public void StaticMethodVariableWithInitialization2()
{
TestMember(@"Private Sub Test
Static j As Integer = 10
j += 1
End Sub
Private Sub Test2
Static j As Integer = 0
Private Shared Sub Test2
Static j As Integer = 20
j += 2
End Sub",
@"private void Test()
@"readonly Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag static_Test_j_Init = new Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag();
int static_Test_j;
private void Test()
{
lock (static_Test_j_Init) {
try {
if (InitStaticVariableHelper(static_Test_j_Init)) {
static_Test_j = 10;
}
} finally {
static_Test_j_Init.State = 1;
}
}
static_Test_j += 1;
}
static int static_Test_j = 0;
private void Test2()
static readonly Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag static_Test2_j_Init = new Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag();
static int static_Test2_j;
private static void Test2()
{
lock (static_Test2_j_Init) {
try {
if (InitStaticVariableHelper(static_Test2_j_Init)) {
static_Test2_j = 20;
}
} finally {
static_Test2_j_Init.State = 1;
}
}
static_Test2_j += 2;
}
static int static_Test2_j = 0;");
static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag flag)
{
if (flag.State == 0) {
flag.State = 2;
return true;
} else if (flag.State == 2) {
throw new Microsoft.VisualBasic.CompilerServices.IncompleteInitialization();
} else {
return false;
}
}");
}
[Test]

Loading…
Cancel
Save