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
switch (this.OutputType) { switch (this.OutputType) {
case OutputType.WinExe: case OutputType.WinExe:
SetProperty(e.Configuration, e.Platform, SetProperty(e.Configuration, e.Platform,
"MyType", "WindowsForms", e.Location, true); "MyType", "WindowsForms", e.NewLocation, true);
break; break;
case OutputType.Exe: case OutputType.Exe:
SetProperty(e.Configuration, e.Platform, SetProperty(e.Configuration, e.Platform,
"MyType", "Console", e.Location, true); "MyType", "Console", e.NewLocation, true);
break; break;
default: default:
SetProperty(e.Configuration, e.Platform, SetProperty(e.Configuration, e.Platform,
"MyType", "Windows", e.Location, true); "MyType", "Windows", e.NewLocation, true);
break; break;
} }
} }

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

@ -223,28 +223,31 @@ namespace ICSharpCode.NRefactory.Visitors
methodDeclaration.Body.Children.RemoveAt(methodDeclaration.Body.Children.Count - 1); methodDeclaration.Body.Children.RemoveAt(methodDeclaration.Body.Children.Count - 1);
methodDeclaration.Body.AddChild(rs); methodDeclaration.Body.AddChild(rs);
} else { } else {
methodDeclaration.Body.AcceptVisitor(new ReturnStatementForFunctionAssignment(methodDeclaration.Name), null); ReturnStatementForFunctionAssignment visitor = new ReturnStatementForFunctionAssignment(methodDeclaration.Name);
Expression init; methodDeclaration.Body.AcceptVisitor(visitor, null);
switch (methodDeclaration.TypeReference.SystemType) { if (visitor.replacementCount > 0) {
case "System.Int16": Expression init;
case "System.Int32": switch (methodDeclaration.TypeReference.SystemType) {
case "System.Int64": case "System.Int16":
case "System.Byte": case "System.Int32":
case "System.UInt16": case "System.Int64":
case "System.UInt32": case "System.Byte":
case "System.UInt64": case "System.UInt16":
init = new PrimitiveExpression(0, "0"); case "System.UInt32":
break; case "System.UInt64":
case "System.Boolean": init = new PrimitiveExpression(0, "0");
init = new PrimitiveExpression(false, "false"); break;
break; case "System.Boolean":
default: init = new PrimitiveExpression(false, "false");
init = new PrimitiveExpression(null, "null"); break;
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
class ReturnStatementForFunctionAssignment : AbstractAstTransformer class ReturnStatementForFunctionAssignment : AbstractAstTransformer
{ {
string functionName; string functionName;
internal int replacementCount = 0;
public ReturnStatementForFunctionAssignment(string functionName) public ReturnStatementForFunctionAssignment(string functionName)
{ {
this.functionName = 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 (identifierExpression.Identifier.Equals(functionName, StringComparison.InvariantCultureIgnoreCase)) {
if (ident != null) { if (!(identifierExpression.Parent is AddressOfExpression) && !(identifierExpression.Parent is InvocationExpression)) {
if (ident.Identifier.Equals(functionName, StringComparison.InvariantCultureIgnoreCase)) { identifierExpression.Identifier = FunctionReturnValueName;
ident.Identifier = FunctionReturnValueName; replacementCount++;
} }
} }
return base.VisitAssignmentExpression(assignmentExpression, data); return base.VisitIdentifierExpression(identifierExpression, data);
} }
} }
#endregion #endregion

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

@ -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] [Test]
public void FunctionWithImplicitReturn() public void FunctionWithImplicitReturn()
{ {
@ -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] [Test]
public void FunctionWithImplicitReturn3() public void FunctionWithImplicitReturn3()
{ {

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

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

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

@ -440,7 +440,9 @@ namespace ICSharpCode.SharpDevelop.Project
lock (SyncRoot) { lock (SyncRoot) {
args = SetPropertyInternal(configuration, platform, propertyName, newValue, location, treatPropertyValueAsLiteral); 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, ProjectPropertyChangedEventArgs SetPropertyInternal(string configuration, string platform,
@ -558,7 +560,11 @@ namespace ICSharpCode.SharpDevelop.Project
args = new ProjectPropertyChangedEventArgs(propertyName); args = new ProjectPropertyChangedEventArgs(propertyName);
args.Configuration = configuration; args.Configuration = configuration;
args.Platform = platform; 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 (newValue == null) {
if (existingPropertyGroup != null && existingProperty != null) { if (existingPropertyGroup != null && existingProperty != null) {

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

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