Browse Source

On VB -> C# conversion, do not generate "functionReturnValue" for functions that don't assign to their name.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2071 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
ecf6c52e82
  1. 6
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/Project/VBNetProject.cs
  2. 58
      src/Libraries/NRefactory/Project/Src/Visitors/VBNetConstructsConvertVisitor.cs
  3. 31
      src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs
  4. 2
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  5. 10
      src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs
  6. 29
      src/Main/Base/Project/Src/Project/ProjectPropertyChangedEventArgs.cs

6
src/AddIns/BackendBindings/VBNetBinding/Project/Src/Project/VBNetProject.cs

@ -26,15 +26,15 @@ namespace VBNetBinding @@ -26,15 +26,15 @@ namespace VBNetBinding
switch (this.OutputType) {
case OutputType.WinExe:
SetProperty(e.Configuration, e.Platform,
"MyType", "WindowsForms", e.Location, true);
"MyType", "WindowsForms", e.NewLocation, true);
break;
case OutputType.Exe:
SetProperty(e.Configuration, e.Platform,
"MyType", "Console", e.Location, true);
"MyType", "Console", e.NewLocation, true);
break;
default:
SetProperty(e.Configuration, e.Platform,
"MyType", "Windows", e.Location, true);
"MyType", "Windows", e.NewLocation, true);
break;
}
}

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

@ -223,28 +223,31 @@ namespace ICSharpCode.NRefactory.Visitors @@ -223,28 +223,31 @@ namespace ICSharpCode.NRefactory.Visitors
methodDeclaration.Body.Children.RemoveAt(methodDeclaration.Body.Children.Count - 1);
methodDeclaration.Body.AddChild(rs);
} else {
methodDeclaration.Body.AcceptVisitor(new ReturnStatementForFunctionAssignment(methodDeclaration.Name), null);
Expression init;
switch (methodDeclaration.TypeReference.SystemType) {
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Byte":
case "System.UInt16":
case "System.UInt32":
case "System.UInt64":
init = new PrimitiveExpression(0, "0");
break;
case "System.Boolean":
init = new PrimitiveExpression(false, "false");
break;
default:
init = new PrimitiveExpression(null, "null");
break;
ReturnStatementForFunctionAssignment visitor = new ReturnStatementForFunctionAssignment(methodDeclaration.Name);
methodDeclaration.Body.AcceptVisitor(visitor, null);
if (visitor.replacementCount > 0) {
Expression init;
switch (methodDeclaration.TypeReference.SystemType) {
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Byte":
case "System.UInt16":
case "System.UInt32":
case "System.UInt64":
init = new PrimitiveExpression(0, "0");
break;
case "System.Boolean":
init = new PrimitiveExpression(false, "false");
break;
default:
init = new PrimitiveExpression(null, "null");
break;
}
methodDeclaration.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(FunctionReturnValueName, init, methodDeclaration.TypeReference)));
methodDeclaration.Body.Children[0].Parent = methodDeclaration.Body;
methodDeclaration.Body.AddChild(new ReturnStatement(new IdentifierExpression(FunctionReturnValueName)));
}
methodDeclaration.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(FunctionReturnValueName, init, methodDeclaration.TypeReference)));
methodDeclaration.Body.Children[0].Parent = methodDeclaration.Body;
methodDeclaration.Body.AddChild(new ReturnStatement(new IdentifierExpression(FunctionReturnValueName)));
}
}
@ -273,21 +276,22 @@ namespace ICSharpCode.NRefactory.Visitors @@ -273,21 +276,22 @@ namespace ICSharpCode.NRefactory.Visitors
class ReturnStatementForFunctionAssignment : AbstractAstTransformer
{
string functionName;
internal int replacementCount = 0;
public ReturnStatementForFunctionAssignment(string functionName)
{
this.functionName = functionName;
}
public override object VisitAssignmentExpression(AssignmentExpression assignmentExpression, object data)
public override object VisitIdentifierExpression(IdentifierExpression identifierExpression, object data)
{
IdentifierExpression ident = assignmentExpression.Left as IdentifierExpression;
if (ident != null) {
if (ident.Identifier.Equals(functionName, StringComparison.InvariantCultureIgnoreCase)) {
ident.Identifier = FunctionReturnValueName;
if (identifierExpression.Identifier.Equals(functionName, StringComparison.InvariantCultureIgnoreCase)) {
if (!(identifierExpression.Parent is AddressOfExpression) && !(identifierExpression.Parent is InvocationExpression)) {
identifierExpression.Identifier = FunctionReturnValueName;
replacementCount++;
}
}
return base.VisitAssignmentExpression(assignmentExpression, data);
return base.VisitIdentifierExpression(identifierExpression, data);
}
}
#endregion

31
src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs

@ -391,6 +391,18 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -391,6 +391,18 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
"}");
}
[Test]
public void FunctionWithoutImplicitReturn()
{
TestMember("Public Function run(i As Integer) As Integer\n" +
" Return 0\n" +
"End Function",
"public int run(int i)\n" +
"{\n" +
"\treturn 0;\n" +
"}");
}
[Test]
public void FunctionWithImplicitReturn()
{
@ -421,6 +433,25 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -421,6 +433,25 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
"}");
}
[Test]
public void FunctionWithImplicitReturn2b()
{
const string ReturnValueName = VBNetConstructsConvertVisitor.FunctionReturnValueName;
TestMember("Public Function run(i As Integer) As Integer\n" +
" While something\n" +
" run = run + run(i - 1)\n" +
" End While\n" +
"End Function",
"public int run(int i)\n" +
"{\n" +
"\tint " + ReturnValueName + " = 0;\n" +
"\twhile (something) {\n" +
"\t\t" + ReturnValueName + " = " + ReturnValueName + " + run(i - 1);\n" +
"\t}\n" +
"\treturn " + ReturnValueName + ";\n" +
"}");
}
[Test]
public void FunctionWithImplicitReturn3()
{

2
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Optimize>False</Optimize>
<DefineConstants>DEBUG</DefineConstants>
<DefineConstants>DEBUG,TRACE</DefineConstants>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<OutputPath>..\..\..\..\bin\</OutputPath>
<WarningLevel>4</WarningLevel>

10
src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs

@ -440,7 +440,9 @@ namespace ICSharpCode.SharpDevelop.Project @@ -440,7 +440,9 @@ namespace ICSharpCode.SharpDevelop.Project
lock (SyncRoot) {
args = SetPropertyInternal(configuration, platform, propertyName, newValue, location, treatPropertyValueAsLiteral);
}
OnPropertyChanged(args);
if (args.NewValue != args.OldValue || args.NewLocation != args.OldLocation) {
OnPropertyChanged(args);
}
}
ProjectPropertyChangedEventArgs SetPropertyInternal(string configuration, string platform,
@ -558,7 +560,11 @@ namespace ICSharpCode.SharpDevelop.Project @@ -558,7 +560,11 @@ namespace ICSharpCode.SharpDevelop.Project
args = new ProjectPropertyChangedEventArgs(propertyName);
args.Configuration = configuration;
args.Platform = platform;
args.Location = location;
args.NewLocation = location;
args.OldLocation = oldLocation;
if (newValue != null) {
args.NewValue = treatPropertyValueAsLiteral ? MSBuildInternals.Escape(newValue) : newValue;
}
if (newValue == null) {
if (existingPropertyGroup != null && existingProperty != null) {

29
src/Main/Base/Project/Src/Project/ProjectPropertyChangedEventArgs.cs

@ -15,8 +15,9 @@ namespace ICSharpCode.SharpDevelop.Project @@ -15,8 +15,9 @@ namespace ICSharpCode.SharpDevelop.Project
{
string propertyName;
string configuration, platform;
string oldValue;
PropertyStorageLocations location;
string oldValue, newValue;
PropertyStorageLocations newLocation;
PropertyStorageLocations oldLocation;
public ProjectPropertyChangedEventArgs(string propertyName)
{
@ -49,7 +50,7 @@ namespace ICSharpCode.SharpDevelop.Project @@ -49,7 +50,7 @@ namespace ICSharpCode.SharpDevelop.Project
}
/// <summary>
/// Gets the old value before the property was changed. This value might not
/// Gets the (unevaluated) old value before the property was changed. This value might not
/// be available if the property changed location.
/// </summary>
public string OldValue {
@ -57,12 +58,28 @@ namespace ICSharpCode.SharpDevelop.Project @@ -57,12 +58,28 @@ namespace ICSharpCode.SharpDevelop.Project
set { oldValue = value; }
}
/// <summary>
/// The new (escaped) value of the property.
/// </summary>
public string NewValue {
get { return newValue; }
set { newValue = value; }
}
/// <summary>
/// The location where the changed property was saved to.
/// </summary>
public PropertyStorageLocations Location {
get { return location; }
set { location = value; }
public PropertyStorageLocations NewLocation {
get { return newLocation; }
set { newLocation = value; }
}
/// <summary>
/// The location where the property was previously saved to.
/// </summary>
public PropertyStorageLocations OldLocation {
get { return oldLocation; }
set { oldLocation = value; }
}
}
}

Loading…
Cancel
Save