Browse Source

Improved C# <-> VB.Net conversion.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@188 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 21 years ago
parent
commit
ef076ee060
  1. 2
      src/Libraries/NRefactory/NRefactory.sln
  2. 3
      src/Libraries/NRefactory/Project/NRefactory.csproj
  3. 50
      src/Libraries/NRefactory/Project/Src/Output/CSharp/CSharpOutputVisitor.cs
  4. 82
      src/Libraries/NRefactory/Project/Src/Output/CodeDOM/CodeDOMOutputVisitor.cs
  5. 24
      src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputVisitor.cs
  6. 42
      src/Libraries/NRefactory/Project/Src/Parser/AST/General/Expressions/BinaryOperatorExpression.cs
  7. 6
      src/Libraries/NRefactory/Project/Src/Parser/AST/General/Statements/LocalVariableDeclaration.cs
  8. 10
      src/Libraries/NRefactory/Project/Src/Parser/AST/General/TypeLevel/EventDeclaration.cs
  9. 6
      src/Libraries/NRefactory/Project/Src/Parser/AST/General/TypeLevel/FieldDeclaration.cs
  10. 3
      src/Libraries/NRefactory/Project/Src/Parser/AST/General/TypeLevel/PropertyGetSetRegion.cs
  11. 8
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  12. 8
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  13. 4608
      src/Libraries/NRefactory/Project/Src/Parser/Frames/trace.txt
  14. 2131
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs
  15. 13
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG
  16. 88
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpToVBNetConvertVisitor.cs
  17. 170
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/PrefixFieldsVisitor.cs
  18. 58
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/VBNetToCSharpConvertVisitor.cs
  19. 1
      src/Main/Base/Project/Src/Commands/VBConverter/CSharpConvertBuffer.cs
  20. 1
      src/Main/Base/Project/Src/Commands/VBConverter/ConvertBuffer.cs

2
src/Libraries/NRefactory/NRefactory.sln

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
# SharpDevelop 2.0.0.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NRefactory", "Project\NRefactory.csproj", "{3A9AE6AA-BC07-4A2F-972C-581E3AE2F195}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NRefactoryTests", "Test\NRefactoryTests.csproj", "{870115DD-960A-4406-A6B9-600BCDC36A03}"

3
src/Libraries/NRefactory/Project/NRefactory.csproj

@ -185,6 +185,9 @@ @@ -185,6 +185,9 @@
<Compile Include="Src\Parser\AST\CSharp\Expressions\AnonymousMethodExpression.cs" />
<Compile Include="Src\Lexer\Special\ISpecial.cs" />
<Compile Include="Src\Output\SpecialNodesInserter.cs" />
<Compile Include="Src\Parser\Visitors\CSharpToVBNetConvertVisitor.cs" />
<Compile Include="Src\Parser\Visitors\PrefixFieldsVisitor.cs" />
<Compile Include="Src\Parser\Visitors\VBNetToCSharpConvertVisitor.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Src\Lexer\CSharp\CSharpKeywordList.txt" />

50
src/Libraries/NRefactory/Project/Src/Output/CSharp/CSharpOutputVisitor.cs

@ -824,7 +824,16 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -824,7 +824,16 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public object Visit(EraseStatement eraseStatement, object data)
{
errors.Error(-1, -1, String.Format("EraseStatement is unsupported"));
foreach (Expression expr in eraseStatement.Expressions) {
outputFormatter.Indent();
expr.AcceptVisitor(this, data);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Null);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
}
return null;
}
@ -1108,7 +1117,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1108,7 +1117,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public object Visit(StopStatement stopStatement, object data)
{
outputFormatter.PrintIdentifier("Debugger.Break()");
outputFormatter.PrintIdentifier("System.Diagnostics.Debugger.Break()");
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
@ -1441,8 +1450,38 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1441,8 +1450,38 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
return null;
}
bool IsNullLiteralExpression(Expression expr)
{
PrimitiveExpression pe = expr as PrimitiveExpression;
if (pe == null) return false;
return pe.Value == null;
}
public object Visit(BinaryOperatorExpression binaryOperatorExpression, object data)
{
// VB-operators that require special representation:
switch (binaryOperatorExpression.Op) {
case BinaryOperatorType.ReferenceEquality:
case BinaryOperatorType.ReferenceInequality:
if (IsNullLiteralExpression(binaryOperatorExpression.Left) || IsNullLiteralExpression(binaryOperatorExpression.Right)) {
// prefer a == null to object.ReferenceEquals(a, null)
break;
}
if (binaryOperatorExpression.Op == BinaryOperatorType.ReferenceInequality)
outputFormatter.PrintToken(Tokens.Not);
outputFormatter.PrintIdentifier("object.ReferenceEquals");
if (prettyPrintOptions.BeforeMethodCallParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(binaryOperatorExpression.Left, data);
PrintFormattedComma();
nodeTracker.TrackedVisit(binaryOperatorExpression.Right, data);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
return null;
}
nodeTracker.TrackedVisit(binaryOperatorExpression.Left, data);
switch (binaryOperatorExpression.Op) {
case BinaryOperatorType.Add:
@ -1476,6 +1515,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1476,6 +1515,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
break;
case BinaryOperatorType.Divide:
case BinaryOperatorType.DivideInteger:
if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
outputFormatter.Space();
}
@ -1563,19 +1603,20 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1563,19 +1603,20 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
}
break;
case BinaryOperatorType.AS:
case BinaryOperatorType.AsCast:
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.As);
outputFormatter.Space();
break;
case BinaryOperatorType.IS:
case BinaryOperatorType.TypeCheck:
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Is);
outputFormatter.Space();
break;
case BinaryOperatorType.Equality:
case BinaryOperatorType.ReferenceEquality:
if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
outputFormatter.Space();
}
@ -1603,6 +1644,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1603,6 +1644,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
}
break;
case BinaryOperatorType.InEquality:
case BinaryOperatorType.ReferenceInequality:
if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
outputFormatter.Space();
}

82
src/Libraries/NRefactory/Project/Src/Output/CodeDOM/CodeDOMOutputVisitor.cs

@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.Parser
}
}
string ConvType(string type)
string ConvType(string type)
{
if (type == null) {
return null;
@ -121,7 +121,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -121,7 +121,7 @@ namespace ICSharpCode.NRefactory.Parser
}
// FIXME: map all modifiers correctly
MemberAttributes ConvMemberAttributes(Modifier modifier)
MemberAttributes ConvMemberAttributes(Modifier modifier)
{
MemberAttributes attr = (MemberAttributes)0;
@ -161,7 +161,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -161,7 +161,7 @@ namespace ICSharpCode.NRefactory.Parser
return attr;
}
#region ICSharpCode.SharpRefactory.Parser.IASTVisitor interface implementation
#region ICSharpCode.SharpRefactory.Parser.IASTVisitor interface implementation
public override object Visit(CompilationUnit compilationUnit, object data)
{
if (compilationUnit == null) {
@ -236,7 +236,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -236,7 +236,7 @@ namespace ICSharpCode.NRefactory.Parser
{
// CodeTypeDelegate codeTypeDelegate = new CodeTypeDelegate(delegateDeclaration.Name);
// codeTypeDelegate.Parameters
//
//
// ((CodeNamespace)namespaceDeclarations.Peek()).Types.Add(codeTypeDelegate);
return null;
}
@ -349,11 +349,11 @@ namespace ICSharpCode.NRefactory.Parser @@ -349,11 +349,11 @@ namespace ICSharpCode.NRefactory.Parser
VariableDeclaration var = (VariableDeclaration)localVariableDeclaration.Variables[i];
if (!var.Initializer.IsNull) {
declStmt = new CodeVariableDeclarationStatement(type,
var.Name,
(CodeExpression)((INode)var.Initializer).AcceptVisitor(this, data));
var.Name,
(CodeExpression)((INode)var.Initializer).AcceptVisitor(this, data));
} else {
declStmt = new CodeVariableDeclarationStatement(type,
var.Name);
var.Name);
}
}
@ -499,7 +499,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -499,7 +499,7 @@ namespace ICSharpCode.NRefactory.Parser
codeStack.Pop();
}
foreach (CatchClause clause in tryCatchStatement.CatchClauses)
foreach (CatchClause clause in tryCatchStatement.CatchClauses)
{
CodeCatchClause catchClause = new CodeCatchClause(clause.VariableName);
catchClause.CatchExceptionType = new CodeTypeReference(clause.TypeReference.Type);
@ -558,6 +558,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -558,6 +558,7 @@ namespace ICSharpCode.NRefactory.Parser
op = CodeBinaryOperatorType.BooleanOr;
break;
case BinaryOperatorType.Divide:
case BinaryOperatorType.DivideInteger:
op = CodeBinaryOperatorType.Divide;
break;
case BinaryOperatorType.GreaterThan:
@ -587,23 +588,26 @@ namespace ICSharpCode.NRefactory.Parser @@ -587,23 +588,26 @@ namespace ICSharpCode.NRefactory.Parser
case BinaryOperatorType.Subtract:
op = CodeBinaryOperatorType.Subtract;
break;
case BinaryOperatorType.ValueEquality:
op = CodeBinaryOperatorType.ValueEquality;
break;
//case BinaryOperatorType.ValueEquality:
// op = CodeBinaryOperatorType.ValueEquality;
// break;
case BinaryOperatorType.ShiftLeft:
// CodeDOM suxx
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.ShiftRight:
// CodeDOM suxx
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.IS:
case BinaryOperatorType.ReferenceEquality:
op = CodeBinaryOperatorType.IdentityEquality;
break;
case BinaryOperatorType.AS:
op = CodeBinaryOperatorType.IdentityEquality;
case BinaryOperatorType.ReferenceInequality:
op = CodeBinaryOperatorType.IdentityInequality;
break;
case BinaryOperatorType.AsCast:
case BinaryOperatorType.TypeCheck:
// CodeDOM suxx
op = CodeBinaryOperatorType.Add;
break;
case BinaryOperatorType.ExclusiveOr:
// TODO ExclusiveOr
op = CodeBinaryOperatorType.BitwiseAnd;
@ -633,7 +637,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -633,7 +637,7 @@ namespace ICSharpCode.NRefactory.Parser
FieldReferenceExpression fRef2 = (FieldReferenceExpression)fRef.TargetObject;
if (fRef2.FieldName != null && Char.IsUpper(fRef2.FieldName[0])) {
// an exception is thrown if it doesn't end in an indentifier exception
// for example for : this.MyObject.MyMethod() leads to an exception, which
// for example for : this.MyObject.MyMethod() leads to an exception, which
// is correct in this case ... I know this is really HACKY :)
try {
CodeExpression tExpr = ConvertToIdentifier(fRef2);
@ -691,10 +695,10 @@ namespace ICSharpCode.NRefactory.Parser @@ -691,10 +695,10 @@ namespace ICSharpCode.NRefactory.Parser
return new CodePrimitiveExpression(- (float)expression.Value);
}
}
}
return new CodeBinaryOperatorExpression(new CodePrimitiveExpression(0),
CodeBinaryOperatorType.Subtract,
(CodeExpression)unaryOperatorExpression.Expression.AcceptVisitor(this, data));
CodeBinaryOperatorType.Subtract,
(CodeExpression)unaryOperatorExpression.Expression.AcceptVisitor(this, data));
case UnaryOperatorType.Plus:
return unaryOperatorExpression.Expression.AcceptVisitor(this, data);
@ -703,36 +707,36 @@ namespace ICSharpCode.NRefactory.Parser @@ -703,36 +707,36 @@ namespace ICSharpCode.NRefactory.Parser
var = (CodeExpression)unaryOperatorExpression.Expression.AcceptVisitor(this, data);
return new CodeAssignStatement(var,
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Add,
new CodePrimitiveExpression(1)));
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Add,
new CodePrimitiveExpression(1)));
case UnaryOperatorType.PostDecrement:
// emulate i--, with i = i - 1
var = (CodeExpression)unaryOperatorExpression.Expression.AcceptVisitor(this, data);
return new CodeAssignStatement(var,
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Subtract,
new CodePrimitiveExpression(1)));
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Subtract,
new CodePrimitiveExpression(1)));
case UnaryOperatorType.Decrement:
// emulate --i, with i = i - 1
var = (CodeExpression)unaryOperatorExpression.Expression.AcceptVisitor(this, data);
return new CodeAssignStatement(var,
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Subtract,
new CodePrimitiveExpression(1)));
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Subtract,
new CodePrimitiveExpression(1)));
case UnaryOperatorType.Increment:
// emulate ++i, with i = i + 1
var = (CodeExpression)unaryOperatorExpression.Expression.AcceptVisitor(this, data);
return new CodeAssignStatement(var,
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Add,
new CodePrimitiveExpression(1)));
new CodeBinaryOperatorExpression(var,
CodeBinaryOperatorType.Add,
new CodePrimitiveExpression(1)));
}
return null;
@ -745,15 +749,15 @@ namespace ICSharpCode.NRefactory.Parser @@ -745,15 +749,15 @@ namespace ICSharpCode.NRefactory.Parser
methodReference = true;
CodeExpression methodInvoker = (CodeExpression)assignmentExpression.Right.AcceptVisitor(this, null);
methodReference = false;
if (assignmentExpression.Left is IdentifierExpression) {
AddStmt(new CodeAttachEventStatement(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), ((IdentifierExpression)assignmentExpression.Left).Identifier),
methodInvoker));
methodInvoker));
} else {
FieldReferenceExpression fr = (FieldReferenceExpression)assignmentExpression.Left;
AddStmt(new CodeAttachEventStatement(new CodeEventReferenceExpression((CodeExpression)fr.TargetObject.AcceptVisitor(this, data), fr.FieldName),
methodInvoker));
methodInvoker));
}
} else {
if (assignmentExpression.Left is IdentifierExpression) {
@ -765,7 +769,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -765,7 +769,7 @@ namespace ICSharpCode.NRefactory.Parser
return null;
}
public override object Visit(TypeOfExpression typeOfExpression, object data)
{
return new CodeTypeOfExpression(ConvType(typeOfExpression.TypeReference.Type));
@ -957,10 +961,10 @@ namespace ICSharpCode.NRefactory.Parser @@ -957,10 +961,10 @@ namespace ICSharpCode.NRefactory.Parser
Type GetType(string typeName)
{
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
Type type = asm.GetType(typeName);
if (type != null)
if (type != null)
{
return type;
}

24
src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputVisitor.cs

@ -499,7 +499,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -499,7 +499,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
{
VisitAttributes(propertySetRegion.Attributes, data);
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Get);
outputFormatter.PrintToken(Tokens.Set);
outputFormatter.NewLine();
++outputFormatter.IndentationLevel;
@ -508,7 +508,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -508,7 +508,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.End);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Get);
outputFormatter.PrintToken(Tokens.Set);
outputFormatter.NewLine();
return null;
}
@ -1729,8 +1729,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1729,8 +1729,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
case BinaryOperatorType.LogicalOr:
op = Tokens.OrElse;
break;
case BinaryOperatorType.ReferenceEquality:
op = Tokens.Is;
break;
case BinaryOperatorType.ReferenceInequality:
op = Tokens.IsNot;
break;
case BinaryOperatorType.AS:
case BinaryOperatorType.AsCast:
outputFormatter.PrintIdentifier("CType(Microsoft.VisualBasic.IIf(TypeOf ");
nodeTracker.TrackedVisit(binaryOperatorExpression.Left, data);
outputFormatter.PrintIdentifier(" Is ");
@ -1741,21 +1747,15 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1741,21 +1747,15 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
nodeTracker.TrackedVisit(binaryOperatorExpression.Right, data);
outputFormatter.PrintIdentifier(")");
return null;
case BinaryOperatorType.IS:
outputFormatter.PrintToken(Tokens.TypeOf);
outputFormatter.Space();
case BinaryOperatorType.TypeCheck:
outputFormatter.PrintIdentifier("TypeOf ");
nodeTracker.TrackedVisit(binaryOperatorExpression.Left, data);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Is);
outputFormatter.Space();
outputFormatter.PrintIdentifier(" Is ");
nodeTracker.TrackedVisit(binaryOperatorExpression.Right, data);
return null;
case BinaryOperatorType.Equality:
op = Tokens.Assign;
if (binaryOperatorExpression.Right is PrimitiveExpression && ((PrimitiveExpression)binaryOperatorExpression.Right).Value == null) {
op = Tokens.Is;
}
break;
case BinaryOperatorType.GreaterThan:
op = Tokens.GreaterThan;

42
src/Libraries/NRefactory/Project/Src/Parser/AST/General/Expressions/BinaryOperatorExpression.cs

@ -8,37 +8,61 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -8,37 +8,61 @@ namespace ICSharpCode.NRefactory.Parser.AST
{
None,
/// <summary>'&amp;' in C#, 'And' in VB.</summary>
BitwiseAnd,
/// <summary>'|' in C#, 'Or' in VB.</summary>
BitwiseOr,
LogicalAnd, // Lazy operator
LogicalOr, // Lazy operator
/// <summary>'&amp;&amp;' in C#, 'AndAlso' in VB.</summary>
LogicalAnd,
/// <summary>'||' in C#, 'OrElse' in VB.</summary>
LogicalOr,
/// <summary>'^' in C#, 'Xor' in VB.</summary>
ExclusiveOr,
/// <summary>&gt;</summary>
GreaterThan,
/// <summary>&gt;=</summary>
GreaterThanOrEqual,
/// <summary>'==' in C#, '=' in VB.</summary>
Equality,
/// <summary>'!=' in C#, '&lt;&gt;' in VB.</summary>
InEquality,
/// <summary>&lt;</summary>
LessThan,
/// <summary>&lt;=</summary>
LessThanOrEqual,
/// <summary>+</summary>
Add,
/// <summary>-</summary>
Subtract,
/// <summary>*</summary>
Multiply,
/// <summary>/</summary>
Divide,
/// <summary>'%' in C#, 'Mod' in VB.</summary>
Modulus,
ValueEquality, // What was this for ?
// VB specific operators
/// <summary>VB-only: \</summary>
DivideInteger,
/// <summary>VB-only: ^</summary>
Power,
/// <summary>VB-only: &</summary>
Concat,
// additional
/// <summary>C#: &lt;&lt;</summary>
ShiftLeft,
/// <summary>C#: &gt;&gt;</summary>
ShiftRight,
IS,
IsNot,
AS,
Like, // What was this for ?
/// <summary>VB-only: Is</summary>
ReferenceEquality,
/// <summary>VB-only: IsNot</summary>
ReferenceInequality,
/// <summary>C#: Is</summary>
TypeCheck,
/// <summary>C#: as-cast</summary>
AsCast,
/// <summary>VB-only: Like</summary>
Like,
}
public class BinaryOperatorExpression : Expression

6
src/Libraries/NRefactory/Project/Src/Parser/AST/General/Statements/LocalVariableDeclaration.cs

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.Parser.AST
{
@ -10,8 +11,7 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -10,8 +11,7 @@ namespace ICSharpCode.NRefactory.Parser.AST
{
TypeReference typeReference;
Modifier modifier = Modifier.None;
// List<VariableDeclaration> variables = new List<VariableDeclaration>(1);
ArrayList variables = new ArrayList(1);
List<VariableDeclaration> variables = new List<VariableDeclaration>(1);
BlockStatement block = BlockStatement.Null; // the block in witch the variable is declared; needed for the LookupTable
public TypeReference TypeReference {
@ -32,7 +32,7 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -32,7 +32,7 @@ namespace ICSharpCode.NRefactory.Parser.AST
}
}
public ArrayList Variables {
public List<VariableDeclaration> Variables {
get {
return variables;
}

10
src/Libraries/NRefactory/Project/Src/Parser/AST/General/TypeLevel/EventDeclaration.cs

@ -19,6 +19,7 @@ using System; @@ -19,6 +19,7 @@ using System;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace ICSharpCode.NRefactory.Parser.AST
@ -26,9 +27,8 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -26,9 +27,8 @@ namespace ICSharpCode.NRefactory.Parser.AST
public class EventDeclaration : ParametrizedNode
{
TypeReference typeReference = TypeReference.Null;
// List<VariableDeclaration> variableDeclarators = new List<VariableDeclaration>(1);
List<VariableDeclaration> variableDeclarators = new List<VariableDeclaration>(1);
// ArrayList implementsClause = new ArrayList(); // VB only
ArrayList variableDeclarators = new ArrayList(1);
ArrayList implementsClause = new ArrayList(); // VB only
EventAddRegion addRegion = EventAddRegion.Null; // only for C#
EventRemoveRegion removeRegion = EventRemoveRegion.Null; // only for C#
@ -43,12 +43,12 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -43,12 +43,12 @@ namespace ICSharpCode.NRefactory.Parser.AST
typeReference = TypeReference.CheckNull(value);
}
}
public ArrayList VariableDeclarators {
public List<VariableDeclaration> VariableDeclarators {
get {
return variableDeclarators;
}
set {
variableDeclarators = value == null ? new ArrayList(1) : value;
variableDeclarators = value == null ? new List<VariableDeclaration>(1) : value;
}
}
@ -110,7 +110,7 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -110,7 +110,7 @@ namespace ICSharpCode.NRefactory.Parser.AST
{
}
public EventDeclaration(TypeReference typeReference, ArrayList variableDeclarators, Modifier modifier, ArrayList attributes) : base(modifier, attributes)
public EventDeclaration(TypeReference typeReference, List<VariableDeclaration> variableDeclarators, Modifier modifier, ArrayList attributes) : base(modifier, attributes)
{
this.TypeReference = typeReference;
this.VariableDeclarators = variableDeclarators;

6
src/Libraries/NRefactory/Project/Src/Parser/AST/General/TypeLevel/FieldDeclaration.cs

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.Parser.AST
{
@ -27,8 +28,7 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -27,8 +28,7 @@ namespace ICSharpCode.NRefactory.Parser.AST
ArrayList attributes;
TypeReference typeReference = TypeReference.Null;
Modifier modifier = Modifier.None;
// List<VariableDeclaration> fields = new List<VariableDeclaration>(1); // [VariableDeclaration]
ArrayList fields = new ArrayList(1); // [VariableDeclaration]
List<VariableDeclaration> fields = new List<VariableDeclaration>(1);
public ArrayList Attributes {
get {
@ -55,7 +55,7 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -55,7 +55,7 @@ namespace ICSharpCode.NRefactory.Parser.AST
modifier = value;
}
}
public ArrayList Fields {
public List<VariableDeclaration> Fields {
get {
return fields;
}

3
src/Libraries/NRefactory/Project/Src/Parser/AST/General/TypeLevel/PropertyGetSetRegion.cs

@ -21,6 +21,9 @@ using System.Collections; @@ -21,6 +21,9 @@ using System.Collections;
namespace ICSharpCode.NRefactory.Parser.AST
{
/// <summary>
/// Base class for PropertyGetRegion and PropertySetRegion.
/// </summary>
public abstract class PropertyGetSetRegion : AttributedNode, INullable
{
BlockStatement block = BlockStatement.Null; // can be null if only the definition is there (interface declaration)

8
src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs

@ -1798,7 +1798,7 @@ Modifiers m, ArrayList attributes) { @@ -1798,7 +1798,7 @@ Modifiers m, ArrayList attributes) {
Expression expr;
ArrayList p = new ArrayList();
Statement stmt = null;
ArrayList variableDeclarators = new ArrayList();
List<VariableDeclaration> variableDeclarators = new List<VariableDeclaration>();
List<TemplateDefinition> templates = new List<TemplateDefinition>();
if (la.kind == 58) {
@ -2664,7 +2664,7 @@ out Statement stmt) { @@ -2664,7 +2664,7 @@ out Statement stmt) {
void VariableDeclarator(
#line 1462 "cs.ATG"
ArrayList fieldDeclaration) {
List<VariableDeclaration> fieldDeclaration) {
#line 1463 "cs.ATG"
Expression expr = null;
@ -5068,12 +5068,12 @@ ref expr); @@ -5068,12 +5068,12 @@ ref expr);
lexer.NextToken();
#line 2136 "cs.ATG"
op = BinaryOperatorType.IS;
op = BinaryOperatorType.TypeCheck;
} else if (la.kind == 48) {
lexer.NextToken();
#line 2137 "cs.ATG"
op = BinaryOperatorType.AS;
op = BinaryOperatorType.AsCast;
} else SynErr(183);
Type(
#line 2139 "cs.ATG"

8
src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG

@ -1052,7 +1052,7 @@ StructMemberDecl<Modifiers m, ArrayList attributes> @@ -1052,7 +1052,7 @@ StructMemberDecl<Modifiers m, ArrayList attributes>
Expression expr;
ArrayList p = new ArrayList();
Statement stmt = null;
ArrayList variableDeclarators = new ArrayList();
List<VariableDeclaration> variableDeclarators = new List<VariableDeclaration>();
List<TemplateDefinition> templates = new List<TemplateDefinition>();
.)
=
@ -1459,7 +1459,7 @@ InterfaceAccessors<out PropertyGetRegion getBlock, out PropertySetRegion setBloc @@ -1459,7 +1459,7 @@ InterfaceAccessors<out PropertyGetRegion getBlock, out PropertySetRegion setBloc
]
.
VariableDeclarator<ArrayList fieldDeclaration>
VariableDeclarator<List<VariableDeclaration> fieldDeclaration>
(. Expression expr = null; .)
=
ident (. VariableDeclaration f = new VariableDeclaration(t.val); .)
@ -2133,8 +2133,8 @@ RelationalExpr<ref Expression outExpr> @@ -2133,8 +2133,8 @@ RelationalExpr<ref Expression outExpr>
UnaryExpr<out expr> ShiftExpr<ref expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
|
(
"is" (. op = BinaryOperatorType.IS; .)
| "as" (. op = BinaryOperatorType.AS; .)
"is" (. op = BinaryOperatorType.TypeCheck; .)
| "as" (. op = BinaryOperatorType.AsCast; .)
)
Type<out type> (. outExpr = new BinaryOperatorExpression(outExpr, op, new TypeReferenceExpression(type)); .)
}

4608
src/Libraries/NRefactory/Project/Src/Parser/Frames/trace.txt

File diff suppressed because it is too large Load Diff

2131
src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs

File diff suppressed because it is too large Load Diff

13
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using ICSharpCode.NRefactory.Parser.AST;
@ -832,7 +833,7 @@ ClassBaseType<out string name> @@ -832,7 +833,7 @@ ClassBaseType<out string name>
StructureMemberDecl<Modifiers m, ArrayList attributes>
(.
TypeReference type = null; ArrayList p = null;
Statement stmt = null; ArrayList variableDeclarators = new ArrayList();
Statement stmt = null; List<VariableDeclaration> variableDeclarators = new List<VariableDeclaration>();
.)=
NonModuleDeclaration<m, attributes>
| /* 9.2.1 */
@ -1056,7 +1057,7 @@ StructureMemberDecl<Modifiers m, ArrayList attributes> @@ -1056,7 +1057,7 @@ StructureMemberDecl<Modifiers m, ArrayList attributes>
"Const" (. m.Add(Modifier.Const); .)
(.
FieldDeclaration fd = new FieldDeclaration(attributes, type, m.Modifier);
fd.StartLocation = t.Location;ArrayList constantDeclarators = new ArrayList();
fd.StartLocation = t.Location;List<VariableDeclaration> constantDeclarators = new List<VariableDeclaration>();
.)
ConstantDeclarator<constantDeclarators>
{ "," ConstantDeclarator<constantDeclarators> }
@ -1177,7 +1178,7 @@ SetAccessorDecl<out PropertySetRegion setBlock,ArrayList attributes> @@ -1177,7 +1178,7 @@ SetAccessorDecl<out PropertySetRegion setBlock,ArrayList attributes>
.
/* 9.5 */
ConstantDeclarator<ArrayList constantDeclaration>
ConstantDeclarator<List<VariableDeclaration> constantDeclaration>
(.
Expression expr = null;
TypeReference type = null;
@ -1194,7 +1195,7 @@ ConstantDeclarator<ArrayList constantDeclaration> @@ -1194,7 +1195,7 @@ ConstantDeclarator<ArrayList constantDeclaration>
.
/* 9.6 */
VariableDeclarator<ArrayList fieldDeclaration>
VariableDeclarator<List<VariableDeclaration> fieldDeclaration>
(.
Expression expr = null;
TypeReference type = null;ArrayList rank = null;ArrayList dimension = null;
@ -1547,8 +1548,8 @@ RelationalExpr<out Expression outExpr> @@ -1547,8 +1548,8 @@ RelationalExpr<out Expression outExpr>
ShiftExpr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
|
/* 11.5.3 */
("Is" (. op = BinaryOperatorType.IS; .) |
"IsNot" (. op = BinaryOperatorType.IsNot; .) )
("Is" (. op = BinaryOperatorType.ReferenceEquality; .) |
"IsNot" (. op = BinaryOperatorType.ReferenceInequality; .) )
Expr<out expr> (. outExpr = new BinaryOperatorExpression(outExpr, op, expr); .)
}
.

88
src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpToVBNetConvertVisitor.cs

@ -0,0 +1,88 @@ @@ -0,0 +1,88 @@
/*
* Created by SharpDevelop.
* User: Daniel Grunwald
* Date: 16.07.2005
* Time: 16:32
*/
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.VB;
using ICSharpCode.NRefactory.Parser.AST;
namespace ICSharpCode.NRefactory.Parser
{
/// <summary>
/// This class converts C# constructs to their VB.NET equivalents.
/// </summary>
public class CSharpToVBNetConvertVisitor : AbstractASTVisitor
{
// The following conversions are implemented:
// Conflicting field/property names -> m_field
// a == null -> a Is Nothing
// a != null -> a Is Not Nothing
// The following conversions should be implemented in the future:
// ForStatement -> ForNextStatement when for-loop is simple
// if (Event != null) Event(this, bla); -> RaiseEvent Event(this, bla)
public override object Visit(BinaryOperatorExpression binaryOperatorExpression, object data)
{
if (binaryOperatorExpression.Op == BinaryOperatorType.Equality || binaryOperatorExpression.Op == BinaryOperatorType.InEquality) {
if (IsNullLiteralExpression(binaryOperatorExpression.Left)) {
Expression tmp = binaryOperatorExpression.Left;
binaryOperatorExpression.Left = binaryOperatorExpression.Right;
binaryOperatorExpression.Right = tmp;
}
if (IsNullLiteralExpression(binaryOperatorExpression.Right)) {
if (binaryOperatorExpression.Op == BinaryOperatorType.Equality) {
binaryOperatorExpression.Op = BinaryOperatorType.ReferenceEquality;
} else {
binaryOperatorExpression.Op = BinaryOperatorType.ReferenceInequality;
}
}
}
return base.Visit(binaryOperatorExpression, data);
}
bool IsNullLiteralExpression(Expression expr)
{
PrimitiveExpression pe = expr as PrimitiveExpression;
if (pe == null) return false;
return pe.Value == null;
}
public override object Visit(TypeDeclaration td, object data)
{
// Conflicting field/property names -> m_field
List<string> properties = new List<string>();
foreach (object o in td.Children) {
PropertyDeclaration pd = o as PropertyDeclaration;
if (pd != null) {
properties.Add(pd.Name);
}
}
List<VariableDeclaration> conflicts = new List<VariableDeclaration>();
foreach (object o in td.Children) {
FieldDeclaration fd = o as FieldDeclaration;
if (fd != null) {
foreach (VariableDeclaration var in fd.Fields) {
string name = var.Name;
foreach (string propertyName in properties) {
if (name.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase)) {
conflicts.Add(var);
}
}
}
}
}
new PrefixFieldsVisitor(conflicts, "m_").Run(td);
return base.Visit(td, data);
}
}
}

170
src/Libraries/NRefactory/Project/Src/Parser/Visitors/PrefixFieldsVisitor.cs

@ -0,0 +1,170 @@ @@ -0,0 +1,170 @@
/*
* Created by SharpDevelop.
* User: Daniel Grunwald
* Date: 16.07.2005
* Time: 16:33
*/
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Parser.AST;
namespace ICSharpCode.NRefactory.Parser
{
/// <summary>
/// Prefixes the names of the specified fields with the prefix and replaces the use.
/// </summary>
public class PrefixFieldsVisitor : AbstractASTVisitor
{
List<VariableDeclaration> fields;
List<string> curBlock = new List<string>();
Stack<List<string>> blocks = new Stack<List<string>>();
string prefix;
public PrefixFieldsVisitor(List<VariableDeclaration> fields, string prefix)
{
this.fields = fields;
this.prefix = prefix;
}
public void Run(TypeDeclaration typeDeclaration)
{
base.Visit(typeDeclaration, null);
foreach (VariableDeclaration decl in fields) {
decl.Name = prefix + decl.Name;
}
}
public override object Visit(TypeDeclaration typeDeclaration, object data)
{
// TODO: treat fields of base types like locals
return base.Visit(typeDeclaration, data);
}
public override object Visit(BlockStatement blockStatement, object data)
{
Push();
object result = base.Visit(blockStatement, data);
Pop();
return result;
}
public override object Visit(MethodDeclaration methodDeclaration, object data)
{
Push();
object result = base.Visit(methodDeclaration, data);
Pop();
return result;
}
public override object Visit(PropertyGetRegion p, object data)
{
Push();
object result = base.Visit(p, data);
Pop();
return result;
}
public override object Visit(PropertySetRegion p, object data)
{
Push();
object result = base.Visit(p, data);
Pop();
return result;
}
public override object Visit(ConstructorDeclaration constructorDeclaration, object data)
{
Push();
object result = base.Visit(constructorDeclaration, data);
Pop();
return result;
}
private void Push()
{
blocks.Push(curBlock);
curBlock = new List<string>();
}
private void Pop()
{
curBlock = blocks.Pop();
}
public override object Visit(LocalVariableDeclaration localVariableDeclaration, object data)
{
foreach (VariableDeclaration decl in localVariableDeclaration.Variables) {
//print("add variable ${decl.Name} to block")
curBlock.Add(decl.Name);
}
return base.Visit(localVariableDeclaration, data);
}
public override object Visit(ParameterDeclarationExpression parameterDeclarationExpression, object data)
{
curBlock.Add(parameterDeclarationExpression.ParameterName);
//print("add parameter ${parameterDeclarationExpression.ParameterName} to block")
return base.Visit(parameterDeclarationExpression, data);
}
public override object Visit(IdentifierExpression identifierExpression, object data)
{
string name = identifierExpression.Identifier;
foreach (VariableDeclaration var in fields) {
if (var.Name == name && !IsLocal(name)) {
identifierExpression.Identifier = prefix + name;
break;
}
}
return base.Visit(identifierExpression, data);
}
public override object Visit(FieldReferenceExpression fieldReferenceExpression, object data)
{
if (fieldReferenceExpression.TargetObject is ThisReferenceExpression) {
string name = fieldReferenceExpression.FieldName;
foreach (VariableDeclaration var in fields) {
if (var.Name == name) {
fieldReferenceExpression.FieldName = prefix + name;
break;
}
}
}
return base.Visit(fieldReferenceExpression, data);
}
bool IsLocal(string name)
{
foreach (List<string> block in blocks) {
if (block.Contains(name))
return true;
}
return curBlock.Contains(name);
}
/*
public override object Visit(invocationExpression as InvocationExpression, object data)
{
// this method is a workaround for a bug in SharpRefactory
result = data
if invocationExpression.TargetObject != null:
result = invocationExpression.TargetObject.AcceptVisitor(self, data)
if invocationExpression.Parameters != null:
for n as INode in invocationExpression.Parameters:
n.AcceptVisitor(self, data)
return result
}
public override object Visit(indexerExpression as IndexerExpression, object data)
{
// this method is a workaround for a bug in SharpRefactory
result = indexerExpression.TargetObject.AcceptVisitor(self, data)
for n as INode in indexerExpression.Indices {
n.AcceptVisitor(self, data)
}
return result
}
*/
}
}

58
src/Libraries/NRefactory/Project/Src/Parser/Visitors/VBNetToCSharpConvertVisitor.cs

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
/*
* Created by SharpDevelop.
* User: Daniel Grunwald
* Date: 16.07.2005
* Time: 18:24
*/
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.VB;
using ICSharpCode.NRefactory.Parser.AST;
namespace ICSharpCode.NRefactory.Parser
{
/// <summary>
/// This class converts VB.NET constructs to their C# equivalents.
/// </summary>
public class VBNetToCSharpConvertVisitor : AbstractASTVisitor
{
// The following conversions are implemented:
// MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()
// The following conversions should be implemented in the future:
// Public Event EventName(param As String) -> automatic delegate declaration
public override object Visit(ConstructorDeclaration constructorDeclaration, object data)
{
// MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()
BlockStatement body = constructorDeclaration.Body;
if (body != null && body.Children.Count > 0) {
StatementExpression se = body.Children[0] as StatementExpression;
if (se != null) {
InvocationExpression ie = se.Expression as InvocationExpression;
if (ie != null) {
FieldReferenceExpression fre = ie.TargetObject as FieldReferenceExpression;
if (fre != null && "New".Equals(fre.FieldName, StringComparison.InvariantCultureIgnoreCase)) {
if (fre.TargetObject is BaseReferenceExpression || fre.TargetObject is ClassReferenceExpression) {
body.Children.RemoveAt(0);
ConstructorInitializer ci = constructorDeclaration.ConstructorInitializer;
ci.Arguments = ie.Parameters;
if (fre.TargetObject is BaseReferenceExpression)
ci.ConstructorInitializerType = ConstructorInitializerType.Base;
else
ci.ConstructorInitializerType = ConstructorInitializerType.This;
}
}
}
}
}
return base.Visit(constructorDeclaration, data);
}
}
}

1
src/Main/Base/Project/Src/Commands/VBConverter/CSharpConvertBuffer.cs

@ -43,6 +43,7 @@ namespace ICSharpCode.SharpDevelop.Commands @@ -43,6 +43,7 @@ namespace ICSharpCode.SharpDevelop.Commands
ICSharpCode.NRefactory.PrettyPrinter.CSharpOutputVisitor vbv = new ICSharpCode.NRefactory.PrettyPrinter.CSharpOutputVisitor();
List<ISpecial> specials = p.Lexer.SpecialTracker.CurrentSpecials;
PreProcessingDirective.VBToCSharp(specials);
new VBNetToCSharpConvertVisitor().Visit(p.CompilationUnit, null);
SpecialNodesInserter sni = new SpecialNodesInserter(specials,
new SpecialOutputVisitor(vbv.OutputFormatter));
vbv.NodeTracker.NodeVisiting += sni.AcceptNodeStart;

1
src/Main/Base/Project/Src/Commands/VBConverter/ConvertBuffer.cs

@ -45,6 +45,7 @@ namespace ICSharpCode.SharpDevelop.Commands @@ -45,6 +45,7 @@ namespace ICSharpCode.SharpDevelop.Commands
List<ISpecial> specials = p.Lexer.SpecialTracker.CurrentSpecials;
PreProcessingDirective.CSharpToVB(specials);
new CSharpToVBNetConvertVisitor().Visit(p.CompilationUnit, null);
SpecialNodesInserter sni = new SpecialNodesInserter(specials,
new SpecialOutputVisitor(vbv.OutputFormatter));
vbv.NodeTracker.NodeVisiting += sni.AcceptNodeStart;

Loading…
Cancel
Save