Browse Source

C# <-> VB converter now converts P/Invoke declarations.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1018 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
1538282335
  1. 2
      src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitor.cs
  2. 10
      src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/MemberTests.cs
  3. 6
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs
  4. 11
      src/Libraries/ICSharpCode.TextEditor/Project/Resources/VBNET-Mode.xshd
  5. 2
      src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Lexer.cs
  6. 39
      src/Libraries/NRefactory/Project/Src/Output/CSharp/CSharpOutputVisitor.cs
  7. 28
      src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputVisitor.cs
  8. 33
      src/Libraries/NRefactory/Project/Src/Parser/AST/Enums.cs
  9. 8
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs
  10. 8
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG
  11. 119
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/ToVBNetConvertVisitor.cs
  12. 112
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/VBNetConstructsConvertVisitor.cs
  13. 39
      src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs
  14. 31
      src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs
  15. 4
      src/Libraries/NRefactory/Test/Parser/TypeLevel/DeclareDeclarationTests.cs
  16. 1
      src/Libraries/NRefactory/Test/Parser/TypeLevel/MethodDeclarationTests.cs
  17. 2
      src/Main/Base/Project/Src/Dom/CodeCompletionOptions.cs
  18. 1
      src/Main/Base/Project/Src/Gui/Dialogs/SharpDevelopAboutPanels.cs
  19. 4
      src/Main/Base/Project/Src/Project/MSBuildEngine.cs

2
src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitor.cs

@ -129,7 +129,7 @@ namespace NRefactoryToBooConverter @@ -129,7 +129,7 @@ namespace NRefactoryToBooConverter
r |= B.TypeMemberModifiers.Partial;
}
if ((m & Modifier.Extern) != 0) {
AddError(node, "Extern modifier is not supported");
// not necessary in Boo
}
if ((m & Modifier.Volatile) != 0) {
AddError(node, "Volatile modifier is not supported");

10
src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/MemberTests.cs

@ -250,5 +250,15 @@ namespace NRefactoryToBooConverter.Tests @@ -250,5 +250,15 @@ namespace NRefactoryToBooConverter.Tests
{
TestInClass("[LookHere] event EventHandler Closed;", "[LookHere]\nprivate event Closed as EventHandler");
}
[Test]
public void PInvoke()
{
TestInClass("[DllImport(\"User32.dll\", CharSet = CharSet.Auto)]\n" +
"static extern IntPtr MessageBox(int etc);",
"[DllImport('User32.dll', CharSet: CharSet.Auto)]\n" +
"private static def MessageBox(etc as System.Int32) as IntPtr:\n" +
"\tpass");
}
}
}

6
src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs

@ -46,9 +46,9 @@ namespace VBNetBinding.FormattingStrategy @@ -46,9 +46,9 @@ namespace VBNetBinding.FormattingStrategy
statements.Add(new VBStatement("\\bstructure \\w+$", "^end structure$", "End Structure", 1));
statements.Add(new VBStatement("^while ", "^end while$", "End While", 1));
statements.Add(new VBStatement("^select case", "^end select$", "End Select", 2));
statements.Add(new VBStatement("(?<!\\bmustoverride )\\bsub \\w+", "^end sub$", "End Sub", 1));
statements.Add(new VBStatement("(?<!\\bmustoverride (readonly |writeonly )?)\\bproperty \\w+", "^end property$", "End Property", 1));
statements.Add(new VBStatement("(?<!\\bmustoverride )\\bfunction \\w+", "^end function$", "End Function", 1));
statements.Add(new VBStatement(@"(?<!\b(mustoverride|declare(\s+(unicode|ansi|auto))?)\s+)\bsub \w+", @"^end\s+sub$", "End Sub", 1));
statements.Add(new VBStatement(@"(?<!\bmustoverride (readonly |writeonly )?)\bproperty \w+", @"^end\s+property$", "End Property", 1));
statements.Add(new VBStatement(@"(?<!\b(mustoverride|declare(\s+(unicode|ansi|auto))?)\s+)\bfunction \w+", @"^end\s+function$", "End Function", 1));
statements.Add(new VBStatement("\\bfor .*?$", "^next( \\w+)?$", "Next", 1));
statements.Add(new VBStatement("^synclock .*?$", "^end synclock$", "End SyncLock", 1));
statements.Add(new VBStatement("^get$", "^end get$", "End Get", 1));

11
src/Libraries/ICSharpCode.TextEditor/Project/Resources/VBNET-Mode.xshd

@ -170,7 +170,6 @@ @@ -170,7 +170,6 @@
<Key word = "Continue" />
<Key word = "Dim" />
<Key word = "ReDim" />
<Key word = "Preserve" />
<Key word = "Erase" />
<Key word = "On" />
<Key word = "Error" />
@ -188,7 +187,6 @@ @@ -188,7 +187,6 @@
<Key word = "Module" />
<Key word = "Structure" />
<Key word = "Enum" />
<Key word = "" />
</KeyWords>
<KeyWords name = "TypeLevelConstructs" bold="false" italic="false" color="Blue">
@ -212,7 +210,6 @@ @@ -212,7 +210,6 @@
<Key word = "Do" />
<Key word = "While" />
<Key word = "Loop" />
<Key word = "Until" />
<Key word = "Wend" />
<Key word = "Try" />
@ -236,6 +233,14 @@ @@ -236,6 +233,14 @@
<Key word = "Select" />
<Key word = "Case" />
</KeyWords>
<KeyWords name = "ContextKeywords" bold="false" italic="false" color="Blue">
<Key word = "Ansi" />
<Key word = "Auto" />
<Key word = "Unicode" />
<Key word = "Preserve" />
<Key word = "Until" />
</KeyWords>
</RuleSet>
<RuleSet name = "PreprocessorSet" ignorecase = "true">

2
src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Lexer.cs

@ -54,6 +54,7 @@ namespace ICSharpCode.NRefactory.Parser.VB @@ -54,6 +54,7 @@ namespace ICSharpCode.NRefactory.Parser.VB
protected override Token Next()
{
unchecked {
int nextChar;
while ((nextChar = ReaderRead()) != -1) {
char ch = (char)nextChar;
@ -220,6 +221,7 @@ namespace ICSharpCode.NRefactory.Parser.VB @@ -220,6 +221,7 @@ namespace ICSharpCode.NRefactory.Parser.VB
return new Token(Tokens.EOF);
}
}
string ReadIdent(char ch)
{

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

@ -794,44 +794,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -794,44 +794,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public object Visit(DeclareDeclaration declareDeclaration, object data)
{
VisitAttributes(declareDeclaration.Attributes, data);
outputFormatter.Indent();
outputFormatter.PrintText(String.Format("[System.Runtime.InteropServices.DllImport({0}", declareDeclaration.Library));
if (declareDeclaration.Alias != null && declareDeclaration.Alias.Length >0) {
outputFormatter.PrintText(String.Format(", EntryPoint={0}", declareDeclaration.Alias));
}
switch (declareDeclaration.Charset) {
case CharsetModifier.ANSI:
outputFormatter.PrintText(", CharSet=System.Runtime.InteropServices.CharSet.Ansi");
break;
case CharsetModifier.Unicode:
outputFormatter.PrintText(", CharSet=System.Runtime.InteropServices.CharSet.Unicode");
break;
case CharsetModifier.Auto:
outputFormatter.PrintText(", CharSet=System.Runtime.InteropServices.CharSet.Auto");
break;
}
outputFormatter.PrintText(")]");
outputFormatter.NewLine();
outputFormatter.Indent();
OutputModifier(declareDeclaration.Modifier);
outputFormatter.PrintToken(Tokens.Static);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Extern);
outputFormatter.Space();
nodeTracker.TrackedVisit(declareDeclaration.TypeReference, data);
outputFormatter.Space();
outputFormatter.PrintIdentifier(declareDeclaration.Name);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(declareDeclaration.Parameters);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
errors.Error(-1, -1, "DeclareDeclaration is unsupported");
return null;
}
#endregion

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

@ -189,12 +189,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -189,12 +189,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Space();
}
Debug.Assert(attributeSection.Attributes != null);
for (int j = 0; j < attributeSection.Attributes.Count; ++j) {
nodeTracker.TrackedVisit((INode)attributeSection.Attributes[j], data);
if (j + 1 < attributeSection.Attributes.Count) {
outputFormatter.PrintToken(Tokens.Comma);
}
}
AppendCommaSeparatedList(attributeSection.Attributes);
if ("assembly".Equals(attributeSection.AttributeTarget, StringComparison.InvariantCultureIgnoreCase)
|| "module".Equals(attributeSection.AttributeTarget, StringComparison.InvariantCultureIgnoreCase)) {
outputFormatter.PrintText(">");
@ -209,18 +205,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -209,18 +205,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
{
outputFormatter.PrintIdentifier(attribute.Name);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
this.AppendCommaSeparatedList(attribute.PositionalArguments);
AppendCommaSeparatedList(attribute.PositionalArguments);
if (attribute.NamedArguments != null && attribute.NamedArguments.Count > 0) {
if (attribute.PositionalArguments.Count > 0) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
for (int i = 0; i < attribute.NamedArguments.Count; ++i) {
nodeTracker.TrackedVisit((INode)attribute.NamedArguments[i], data);
if (i + 1 < attribute.NamedArguments.Count) {
outputFormatter.PrintToken(Tokens.Comma);
}
}
AppendCommaSeparatedList(attribute.NamedArguments);
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
return null;
@ -1103,17 +1095,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1103,17 +1095,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
switch (declareDeclaration.Charset) {
case CharsetModifier.Auto:
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Auto);
outputFormatter.Space();
break;
case CharsetModifier.Unicode:
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Unicode);
outputFormatter.Space();
break;
case CharsetModifier.ANSI:
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Ansi);
outputFormatter.Space();
break;
@ -1131,13 +1120,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1131,13 +1120,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Lib);
outputFormatter.Space();
outputFormatter.PrintText('"' + declareDeclaration.Library + '"');
outputFormatter.PrintText('"' + ConvertString(declareDeclaration.Library) + '"');
outputFormatter.Space();
if (declareDeclaration.Alias.Length > 0) {
outputFormatter.PrintToken(Tokens.Alias);
outputFormatter.Space();
outputFormatter.PrintText('"' + declareDeclaration.Alias + '"');
outputFormatter.PrintText('"' + ConvertString(declareDeclaration.Alias) + '"');
outputFormatter.Space();
}
@ -2556,9 +2545,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -2556,9 +2545,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Space();
}
// TODO : Extern
if ((modifier & Modifier.Extern) == Modifier.Extern) {
errors.Error(-1, -1, String.Format("'Extern' modifier not convertable"));
// not required in VB
}
// TODO : Volatile

33
src/Libraries/NRefactory/Project/Src/Parser/AST/Enums.cs

@ -49,17 +49,18 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -49,17 +49,18 @@ namespace ICSharpCode.NRefactory.Parser.AST
/// <summary>Only for VB properties.</summary>
WriteOnly = 0x400000, // VB specific
Classes = New | Public | Protected | Internal | Private | Abstract | Sealed | Partial | Static,
VBModules = Private | Public | Protected | Internal,
VBStructures = Private | Public | Protected | Internal | New,
VBEnums = Private | Public | Protected | Internal | New,
VBInterfacs = Private | Public | Protected | Internal | New,
VBDelegates = Private | Public | Protected | Internal | New,
VBMethods = Private | Public | Protected | Internal | New | Static | Virtual | Sealed | Abstract | Override | Overloads,
VBExternalMethods = Private | Public | Protected | Internal | New | Overloads,
VBEvents = Private | Public | Protected | Internal | New | Overloads,
Visibility = Private | Public | Protected | Internal,
Classes = New | Visibility | Abstract | Sealed | Partial | Static,
VBModules = Visibility,
VBStructures = Visibility | New,
VBEnums = Visibility | New,
VBInterfacs = Visibility | New,
VBDelegates = Visibility | New,
VBMethods = Visibility | New | Static | Virtual | Sealed | Abstract | Override | Overloads,
VBExternalMethods = Visibility | New | Overloads,
VBEvents = Visibility | New | Overloads,
VBProperties = VBMethods | Default | ReadOnly | WriteOnly,
VBCustomEvents = Private | Public | Protected | Internal | New | Overloads,
VBCustomEvents = Visibility | New | Overloads,
VBOperators = Public | Static | Overloads | New | Widening | Narrowing,
@ -69,15 +70,15 @@ namespace ICSharpCode.NRefactory.Parser.AST @@ -69,15 +70,15 @@ namespace ICSharpCode.NRefactory.Parser.AST
VBInterfaceProperties = New | Overloads | ReadOnly | WriteOnly | Default,
VBInterfaceEnums = New,
Fields = New | Public | Protected | Internal | Private | Static | ReadOnly | Volatile,
PropertysEventsMethods = New | Public | Protected | Internal | Private | Static | Virtual | Sealed | Override | Abstract | Extern,
Indexers = New | Public | Protected | Internal | Private | Virtual | Sealed | Override | Abstract | Extern,
Fields = New | Visibility | Static | ReadOnly | Volatile,
PropertysEventsMethods = New | Visibility | Static | Virtual | Sealed | Override | Abstract | Extern,
Indexers = New | Visibility | Virtual | Sealed | Override | Abstract | Extern,
Operators = Public | Static | Extern,
Constants = New | Public | Protected | Internal | Private,
StructsInterfacesEnumsDelegates = New | Public | Protected | Internal | Private | Partial,
Constants = New | Visibility,
StructsInterfacesEnumsDelegates = New | Visibility | Partial,
StaticConstructors = Extern | Static | Unsafe,
Destructors = Extern | Unsafe,
Constructors = Public | Protected | Internal | Private | Extern,
Constructors = Visibility | Extern,
}
public enum ClassType

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

@ -1913,13 +1913,13 @@ out charsetModifer); @@ -1913,13 +1913,13 @@ out charsetModifer);
Expect(3);
#line 1123 "VBNET.ATG"
library = t.val.ToString();
library = t.literalValue.ToString();
if (la.kind == 44) {
lexer.NextToken();
Expect(3);
#line 1124 "VBNET.ATG"
alias = t.val.ToString();
alias = t.literalValue.ToString();
}
if (la.kind == 24) {
lexer.NextToken();
@ -1948,13 +1948,13 @@ p); @@ -1948,13 +1948,13 @@ p);
Expect(3);
#line 1136 "VBNET.ATG"
library = t.val;
library = t.literalValue.ToString();
if (la.kind == 44) {
lexer.NextToken();
Expect(3);
#line 1137 "VBNET.ATG"
alias = t.val;
alias = t.literalValue.ToString();
}
if (la.kind == 24) {
lexer.NextToken();

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

@ -1120,8 +1120,8 @@ StructureMemberDecl<Modifiers m, List<AttributeSection> attributes> @@ -1120,8 +1120,8 @@ StructureMemberDecl<Modifiers m, List<AttributeSection> attributes>
(
"Sub"
Identifier (. name = t.val; .)
"Lib" LiteralString (. library = t.val.ToString(); .)
["Alias" LiteralString (. alias = t.val.ToString(); .)]
"Lib" LiteralString (. library = t.literalValue.ToString(); .)
["Alias" LiteralString (. alias = t.literalValue.ToString(); .)]
[ "(" [ FormalParameterList<p> ] ")" ]
EOL
(.
@ -1133,8 +1133,8 @@ StructureMemberDecl<Modifiers m, List<AttributeSection> attributes> @@ -1133,8 +1133,8 @@ StructureMemberDecl<Modifiers m, List<AttributeSection> attributes>
|
"Function"
Identifier (. name = t.val; .)
"Lib" LiteralString (. library = t.val; .)
["Alias" LiteralString (. alias = t.val; .)]
"Lib" LiteralString (. library = t.literalValue.ToString(); .)
["Alias" LiteralString (. alias = t.literalValue.ToString(); .)]
[ "(" [ FormalParameterList<p> ] ")" ]
["As" TypeName<out type> ]
EOL

119
src/Libraries/NRefactory/Project/Src/Parser/Visitors/ToVBNetConvertVisitor.cs

@ -13,6 +13,7 @@ using System.Diagnostics; @@ -13,6 +13,7 @@ using System.Diagnostics;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.AST;
using Attribute = ICSharpCode.NRefactory.Parser.AST.Attribute;
namespace ICSharpCode.NRefactory.Parser
{
@ -146,5 +147,123 @@ namespace ICSharpCode.NRefactory.Parser @@ -146,5 +147,123 @@ namespace ICSharpCode.NRefactory.Parser
}
return base.Visit(assignmentExpression, data);
}
public override object Visit(MethodDeclaration methodDeclaration, object data)
{
if ((methodDeclaration.Modifier & Modifier.Visibility) == 0)
methodDeclaration.Modifier |= Modifier.Private;
base.Visit(methodDeclaration, data);
const Modifier externStatic = Modifier.Static | Modifier.Extern;
if ((methodDeclaration.Modifier & externStatic) == externStatic
&& methodDeclaration.Body.IsNull)
{
foreach (AttributeSection sec in methodDeclaration.Attributes) {
foreach (Attribute att in sec.Attributes) {
if ("DllImport".Equals(att.Name, StringComparison.InvariantCultureIgnoreCase)) {
if (ConvertPInvoke(methodDeclaration, att)) {
sec.Attributes.Remove(att);
break;
}
}
}
if (sec.Attributes.Count == 0) {
methodDeclaration.Attributes.Remove(sec);
break;
}
}
}
return null;
}
bool ConvertPInvoke(MethodDeclaration method, ICSharpCode.NRefactory.Parser.AST.Attribute att)
{
if (att.PositionalArguments.Count != 1)
return false;
PrimitiveExpression pe = att.PositionalArguments[0] as PrimitiveExpression;
if (pe == null || !(pe.Value is string))
return false;
string libraryName = (string)pe.Value;
string alias = null;
bool setLastError = false;
bool exactSpelling = false;
CharsetModifier charSet = CharsetModifier.Auto;
foreach (NamedArgumentExpression arg in att.NamedArguments) {
switch (arg.Name) {
case "SetLastError":
pe = arg.Expression as PrimitiveExpression;
if (pe != null && pe.Value is bool)
setLastError = (bool)pe.Value;
else
return false;
break;
case "ExactSpelling":
pe = arg.Expression as PrimitiveExpression;
if (pe != null && pe.Value is bool)
exactSpelling = (bool)pe.Value;
else
return false;
break;
case "CharSet":
{
FieldReferenceExpression fre = arg.Expression as FieldReferenceExpression;
if (fre == null || !(fre.TargetObject is IdentifierExpression))
return false;
if ((fre.TargetObject as IdentifierExpression).Identifier != "CharSet")
return false;
switch (fre.FieldName) {
case "Unicode":
charSet = CharsetModifier.Unicode;
break;
case "Auto":
charSet = CharsetModifier.Auto;
break;
case "Ansi":
charSet = CharsetModifier.ANSI;
break;
default:
return false;
}
}
break;
case "EntryPoint":
pe = arg.Expression as PrimitiveExpression;
if (pe != null)
alias = pe.Value as string;
break;
default:
return false;
}
}
if (setLastError && exactSpelling) {
// Only P/Invokes with SetLastError and ExactSpelling can be converted to a DeclareDeclaration
const Modifier removeModifiers = Modifier.Static | Modifier.Extern;
DeclareDeclaration decl = new DeclareDeclaration(method.Name, method.Modifier &~ removeModifiers,
method.TypeReference,
method.Parameters,
method.Attributes,
libraryName, alias, charSet);
ReplaceCurrentNode(decl);
base.Visit(decl, null);
return true;
} else {
return false;
}
}
public override object Visit(PropertyDeclaration propertyDeclaration, object data)
{
if ((propertyDeclaration.Modifier & Modifier.Visibility) == 0)
propertyDeclaration.Modifier |= Modifier.Private;
return base.Visit(propertyDeclaration, data);
}
public override object Visit(ConstructorDeclaration constructorDeclaration, object data)
{
if ((constructorDeclaration.Modifier & Modifier.Visibility) == 0)
constructorDeclaration.Modifier |= Modifier.Private;
return base.Visit(constructorDeclaration, data);
}
}
}

112
src/Libraries/NRefactory/Project/Src/Parser/Visitors/VBNetConstructsConvertVisitor.cs

@ -14,6 +14,7 @@ using System.Diagnostics; @@ -14,6 +14,7 @@ using System.Diagnostics;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.VB;
using ICSharpCode.NRefactory.Parser.AST;
using Attribute = ICSharpCode.NRefactory.Parser.AST.Attribute;
namespace ICSharpCode.NRefactory.Parser
{
@ -24,12 +25,46 @@ namespace ICSharpCode.NRefactory.Parser @@ -24,12 +25,46 @@ namespace ICSharpCode.NRefactory.Parser
{
// The following conversions are implemented:
// MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()
// Add Public Modifier to methods and properties
// The following conversions should be implemented in the future:
// Function A() \n A = SomeValue \n End Function -> convert to return statement
Dictionary<string, string> usings;
List<UsingDeclaration> addedUsings;
public override object Visit(CompilationUnit compilationUnit, object data)
{
usings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
addedUsings = new List<UsingDeclaration>();
base.Visit(compilationUnit, data);
int i;
for (i = 0; i < compilationUnit.Children.Count; i++) {
if (!(compilationUnit.Children[i] is UsingDeclaration))
break;
}
foreach (UsingDeclaration decl in addedUsings) {
decl.Parent = compilationUnit;
compilationUnit.Children.Insert(i++, decl);
}
usings = null;
addedUsings = null;
return null;
}
public override object Visit(Using @using, object data)
{
if (!@using.IsAlias) {
usings[@using.Name] = @using.Name;
}
return base.Visit(@using, data);
}
public override object Visit(ConstructorDeclaration constructorDeclaration, object data)
{
if ((constructorDeclaration.Modifier & Modifier.Visibility) == 0)
constructorDeclaration.Modifier = Modifier.Public;
// 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) {
@ -55,5 +90,82 @@ namespace ICSharpCode.NRefactory.Parser @@ -55,5 +90,82 @@ namespace ICSharpCode.NRefactory.Parser
}
return base.Visit(constructorDeclaration, data);
}
public override object Visit(DeclareDeclaration declareDeclaration, object data)
{
if (!usings.ContainsKey("System.Runtime.InteropServices")) {
UsingDeclaration @using = new UsingDeclaration("System.Runtime.InteropServices");
addedUsings.Add(@using);
base.Visit(@using, data);
}
MethodDeclaration method = new MethodDeclaration(declareDeclaration.Name, declareDeclaration.Modifier,
declareDeclaration.TypeReference, declareDeclaration.Parameters,
declareDeclaration.Attributes);
method.Modifier |= Modifier.Extern | Modifier.Static;
ICSharpCode.NRefactory.Parser.AST.Attribute att = new Attribute("DllImport", null, null);
att.PositionalArguments.Add(CreateStringLiteral(declareDeclaration.Library));
if (declareDeclaration.Alias.Length > 0) {
att.NamedArguments.Add(new NamedArgumentExpression("EntryPoint", CreateStringLiteral(declareDeclaration.Alias)));
}
switch (declareDeclaration.Charset) {
case CharsetModifier.Auto:
att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
new FieldReferenceExpression(new IdentifierExpression("CharSet"),
"Auto")));
break;
case CharsetModifier.Unicode:
att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
new FieldReferenceExpression(new IdentifierExpression("CharSet"),
"Unicode")));
break;
default:
att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
new FieldReferenceExpression(new IdentifierExpression("CharSet"),
"Ansi")));
break;
}
att.NamedArguments.Add(new NamedArgumentExpression("SetLastError", new PrimitiveExpression(true, true.ToString())));
att.NamedArguments.Add(new NamedArgumentExpression("ExactSpelling", new PrimitiveExpression(true, true.ToString())));
AttributeSection sec = new AttributeSection(null, null);
sec.Attributes.Add(att);
method.Attributes.Add(sec);
ReplaceCurrentNode(method);
return base.Visit(method, data);
}
static PrimitiveExpression CreateStringLiteral(string text)
{
return new PrimitiveExpression(text, text);
}
public override object Visit(MethodDeclaration methodDeclaration, object data)
{
if ((methodDeclaration.Modifier & Modifier.Visibility) == 0)
methodDeclaration.Modifier |= Modifier.Public;
if ((methodDeclaration.Modifier & (Modifier.Static | Modifier.Extern)) == Modifier.Static
&& methodDeclaration.Body.Children.Count == 0)
{
foreach (AttributeSection sec in methodDeclaration.Attributes) {
foreach (Attribute att in sec.Attributes) {
if ("DllImport".Equals(att.Name, StringComparison.InvariantCultureIgnoreCase)) {
methodDeclaration.Modifier |= Modifier.Extern;
methodDeclaration.Body = null;
}
}
}
}
return base.Visit(methodDeclaration, data);
}
public override object Visit(PropertyDeclaration propertyDeclaration, object data)
{
if ((propertyDeclaration.Modifier & Modifier.Visibility) == 0)
propertyDeclaration.Modifier = Modifier.Public;
return base.Visit(propertyDeclaration, data);
}
}
}

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

@ -31,8 +31,18 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -31,8 +31,18 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
}
public void TestMember(string input, string expectedOutput)
{
TestMember(input, expectedOutput, null);
}
public void TestMember(string input, string expectedOutput, string expectedAutomaticImport)
{
StringBuilder b = new StringBuilder();
if (expectedAutomaticImport != null) {
b.Append("using ");
b.Append(expectedAutomaticImport);
b.AppendLine(";");
}
b.AppendLine("class tmp1");
b.AppendLine("{");
using (StringReader r = new StringReader(expectedOutput)) {
@ -51,7 +61,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -51,7 +61,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
StringBuilder b = new StringBuilder();
b.AppendLine("class tmp1");
b.AppendLine("{");
b.AppendLine("\tvoid tmp2()");
b.AppendLine("\tpublic void tmp2()");
b.AppendLine("\t{");
using (StringReader r = new StringReader(expectedOutput)) {
string line;
@ -89,5 +99,32 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -89,5 +99,32 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
TestStatement("RaiseEvent someEvent(Me, EventArgs.Empty)",
"if (someEvent != null) {\n\tsomeEvent(this, EventArgs.Empty);\n}");
}
[Test]
public void StaticMethod()
{
TestMember("Shared Sub A()\nEnd Sub",
"public static void A()\n{\n}");
}
[Test]
public void PInvoke()
{
TestMember("Declare Function SendMessage Lib \"user32.dll\" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr",
"[DllImport(\"user32.dll\", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]\n" +
"static extern IntPtr SendMessage(IntPtr hWnd, int Msg, UIntPtr wParam, IntPtr lParam);",
"System.Runtime.InteropServices");
TestMember("Declare Unicode Function SendMessage Lib \"user32.dll\" Alias \"SendMessageW\" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr",
"[DllImport(\"user32.dll\", EntryPoint = \"SendMessageW\", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]\n" +
"static extern IntPtr SendMessage(IntPtr hWnd, int Msg, UIntPtr wParam, IntPtr lParam);",
"System.Runtime.InteropServices");
TestMember("<DllImport(\"user32.dll\", CharSet:=CharSet.Auto)> _\n" +
"Shared Function MessageBox(ByVal hwnd As IntPtr, ByVal t As String, ByVal caption As String, ByVal t2 As UInt32) As Integer\n" +
"End Function",
"[DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n" +
"public static extern int MessageBox(IntPtr hwnd, string t, string caption, UInt32 t2);");
}
}
}

31
src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs

@ -49,7 +49,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -49,7 +49,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
{
StringBuilder b = new StringBuilder();
b.AppendLine("Class tmp1");
b.AppendLine("\tSub tmp2()");
b.AppendLine("\tPrivate Sub tmp2()");
using (StringReader r = new StringReader(expectedOutput)) {
string line;
while ((line = r.ReadLine()) != null) {
@ -158,7 +158,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -158,7 +158,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
public void AnonymousMethod()
{
TestMember("void A() { someEvent += delegate(int argument) { return argument * 2; }; }",
"Sub A()\n" +
"Private Sub A()\n" +
"\tAddHandler someEvent, AddressOf ConvertedAnonymousMethod1\n" +
"End Sub\n" +
"Private Sub ConvertedAnonymousMethod1(ByVal argument As Integer)\n" +
@ -170,7 +170,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -170,7 +170,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
public void AnonymousMethodInVarDeclaration()
{
TestMember("void A() { SomeDelegate i = delegate(int argument) { return argument * 2; }; }",
"Sub A()\n" +
"Private Sub A()\n" +
"\tDim i As SomeDelegate = AddressOf ConvertedAnonymousMethod1\n" +
"End Sub\n" +
"Private Sub ConvertedAnonymousMethod1(ByVal argument As Integer)\n" +
@ -190,5 +190,30 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -190,5 +190,30 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
TestStatement("someEvent += new SomeDelegate(this.tmp2);",
"AddHandler someEvent, AddressOf tmp2");
}
[Test]
public void StaticMethod()
{
TestMember("static void A() {}",
"Private Shared Sub A()\nEnd Sub");
}
[Test]
public void PInvoke()
{
TestMember("[DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n" +
"public static extern int MessageBox(IntPtr hwnd, string t, string caption, UInt32 t2);",
"<DllImport(\"user32.dll\", CharSet := CharSet.Auto)> _\n" +
"Public Shared Function MessageBox(ByVal hwnd As IntPtr, ByVal t As String, ByVal caption As String, ByVal t2 As UInt32) As Integer\n" +
"End Function");
TestMember("[DllImport(\"user32.dll\", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]\n" +
"public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, UIntPtr wParam, IntPtr lParam);",
"Public Declare Ansi Function SendMessage Lib \"user32.dll\" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr");
TestMember("[DllImport(\"user32.dll\", SetLastError = true, ExactSpelling = true, EntryPoint = \"SendMessageW\")]\n" +
"public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, UIntPtr wParam, IntPtr lParam);",
"Public Declare Auto Function SendMessage Lib \"user32.dll\" Alias \"SendMessageW\" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr");
}
}
}

4
src/Libraries/NRefactory/Test/Parser/TypeLevel/DeclareDeclarationTests.cs

@ -31,8 +31,8 @@ namespace ICSharpCode.NRefactory.Tests.AST @@ -31,8 +31,8 @@ namespace ICSharpCode.NRefactory.Tests.AST
DeclareDeclaration dd = ParseUtilVBNet.ParseTypeMember<DeclareDeclaration>(program);
Assert.AreEqual("System.Int32", dd.TypeReference.SystemType);
Assert.AreEqual("GetUserName", dd.Name);
Assert.AreEqual("\"advapi32.dll\"", dd.Library);
Assert.AreEqual("\"GetUserNameA\"", dd.Alias);
Assert.AreEqual("advapi32.dll", dd.Library);
Assert.AreEqual("GetUserNameA", dd.Alias);
Assert.AreEqual(CharsetModifier.ANSI, dd.Charset);
}
#endregion

1
src/Libraries/NRefactory/Test/Parser/TypeLevel/MethodDeclarationTests.cs

@ -227,6 +227,7 @@ namespace ICSharpCode.NRefactory.Tests.AST @@ -227,6 +227,7 @@ namespace ICSharpCode.NRefactory.Tests.AST
end sub";
MethodDeclaration md = ParseUtilVBNet.ParseTypeMember<MethodDeclaration>(program);
Assert.AreEqual(Modifier.Public | Modifier.Static, md.Modifier);
Assert.AreEqual(2, md.StartLocation.Y, "StartLocation.Y");
Assert.AreEqual(2, md.EndLocation.Y, "EndLocation.Y");
Assert.AreEqual(2, md.StartLocation.X, "StartLocation.X");

2
src/Main/Base/Project/Src/Dom/CodeCompletionOptions.cs

@ -43,7 +43,7 @@ namespace ICSharpCode.SharpDevelop.Dom @@ -43,7 +43,7 @@ namespace ICSharpCode.SharpDevelop.Dom
public static int DataUsageCacheItemCount {
get {
return properties.Get("DataUsageCacheItemCount", 200);
return properties.Get("DataUsageCacheItemCount", 500);
}
set {
properties.Set("DataUsageCacheItemCount", value);

1
src/Main/Base/Project/Src/Gui/Dialogs/SharpDevelopAboutPanels.cs

@ -71,6 +71,7 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -71,6 +71,7 @@ namespace ICSharpCode.SharpDevelop.Gui
Dock = DockStyle.Fill;
throwExceptionButton.Location = new System.Drawing.Point(8, sponsorLabel.Bounds.Bottom + 1);
throwExceptionButton.AutoSize = true;
throwExceptionButton.Text = ResourceService.GetString("Dialog.About.ThrowExceptionButton");
throwExceptionButton.Size = new System.Drawing.Size(96, 24);
throwExceptionButton.Click += new EventHandler(ThrowExceptionButtonClick);

4
src/Main/Base/Project/Src/Project/MSBuildEngine.cs

@ -301,9 +301,13 @@ namespace ICSharpCode.SharpDevelop.Project @@ -301,9 +301,13 @@ namespace ICSharpCode.SharpDevelop.Project
if (string.Equals(file, activeTaskName, StringComparison.InvariantCultureIgnoreCase)) {
file = "";
} else {
bool isShortFileName = file == Path.GetFileNameWithoutExtension(file);
if (projectFiles.Count > 0) {
file = Path.Combine(Path.GetDirectoryName(projectFiles.Peek()), file);
}
if (isShortFileName && !File.Exists(file)) {
file = "";
}
}
CompilerError error = new CompilerError(file, lineNumber, columnNumber, code, message);
error.IsWarning = isWarning;

Loading…
Cancel
Save