Browse Source

implement Attribute conversion

pull/254/head
Siegfried Pammer 15 years ago
parent
commit
fffd3d8e80
  1. 21
      ILSpy/VB/VBLanguage.cs
  2. 19
      ILSpy/VB/VBTextOutputFormatter.cs
  3. 78
      NRefactory/ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs
  4. 58
      NRefactory/ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs

21
ILSpy/VB/VBLanguage.cs

@ -18,6 +18,8 @@
using System; using System;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Linq;
using ICSharpCode.Decompiler; using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Ast; using ICSharpCode.Decompiler.Ast;
using ICSharpCode.Decompiler.Ast.Transforms; using ICSharpCode.Decompiler.Ast.Transforms;
@ -101,7 +103,7 @@ namespace ICSharpCode.ILSpy.VB
astBuilder.RunTransformations(transformAbortCondition); astBuilder.RunTransformations(transformAbortCondition);
if (options.DecompilerSettings.ShowXmlDocumentation) if (options.DecompilerSettings.ShowXmlDocumentation)
AddXmlDocTransform.Run(astBuilder.CompilationUnit); AddXmlDocTransform.Run(astBuilder.CompilationUnit);
var unit = astBuilder.CompilationUnit.AcceptVisitor(new CSharpToVBConverterVisitor(), null); var unit = astBuilder.CompilationUnit.AcceptVisitor(new CSharpToVBConverterVisitor(new ILSpyEnvironmentProvider()), null);
var outputFormatter = new VBTextOutputFormatter(output); var outputFormatter = new VBTextOutputFormatter(output);
var formattingPolicy = new VBFormattingOptions(); var formattingPolicy = new VBFormattingOptions();
unit.AcceptVisitor(new OutputVisitor(outputFormatter, formattingPolicy), null); unit.AcceptVisitor(new OutputVisitor(outputFormatter, formattingPolicy), null);
@ -124,4 +126,21 @@ namespace ICSharpCode.ILSpy.VB
}); });
} }
} }
public class ILSpyEnvironmentProvider : IEnvironmentProvider
{
public string RootNamespace {
get {
return "";
}
}
public string GetTypeNameForAttribute(ICSharpCode.NRefactory.CSharp.Attribute attribute)
{
return attribute.Type.Annotations
.OfType<Mono.Cecil.MemberReference>()
.First()
.FullName;
}
}
} }

19
ILSpy/VB/VBTextOutputFormatter.cs

@ -1,5 +1,20 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) //
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

78
NRefactory/ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using ICSharpCode.NRefactory.PatternMatching; using ICSharpCode.NRefactory.PatternMatching;
using ICSharpCode.NRefactory.VB.Ast; using ICSharpCode.NRefactory.VB.Ast;
@ -95,12 +94,28 @@ namespace ICSharpCode.NRefactory.VB
public object VisitAttribute(ICSharpCode.NRefactory.VB.Ast.Attribute attribute, object data) public object VisitAttribute(ICSharpCode.NRefactory.VB.Ast.Attribute attribute, object data)
{ {
throw new NotImplementedException(); StartNode(attribute);
if (!attribute.Target.IsNull) {
attribute.Target.AcceptVisitor(this, data);
WriteToken(":", VB.Ast.Attribute.TargetRole);
}
attribute.Type.AcceptVisitor(this, data);
WriteCommaSeparatedListInParenthesis(attribute.Arguments, false);
return EndNode(attribute);
} }
public object VisitAttributeBlock(AttributeBlock attributeBlock, object data) public object VisitAttributeBlock(AttributeBlock attributeBlock, object data)
{ {
throw new NotImplementedException(); StartNode(attributeBlock);
WriteToken("<", AttributeBlock.Roles.LChevron);
WriteCommaSeparatedList(attributeBlock.Attributes);
WriteToken(">", AttributeBlock.Roles.RChevron);
NewLine();
return EndNode(attributeBlock);
} }
public object VisitImportsStatement(ImportsStatement importsStatement, object data) public object VisitImportsStatement(ImportsStatement importsStatement, object data)
@ -136,6 +151,17 @@ namespace ICSharpCode.NRefactory.VB
node.AcceptVisitor(this, null); node.AcceptVisitor(this, null);
} }
NewLine(); NewLine();
Indent();
isFirst = true;
foreach (var member in namespaceDeclaration.Members) {
if (isFirst) {
isFirst = false;
} else {
NewLine();
}
member.AcceptVisitor(this, data);
}
Unindent();
WriteKeyword("End"); WriteKeyword("End");
WriteKeyword("Namespace"); WriteKeyword("Namespace");
NewLine(); NewLine();
@ -149,7 +175,40 @@ namespace ICSharpCode.NRefactory.VB
public object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) public object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
{ {
throw new NotImplementedException(); StartNode(typeDeclaration);
WriteAttributes(typeDeclaration.Attributes);
WriteModifiers(typeDeclaration.ModifierTokens);
WriteClassTypeKeyword(typeDeclaration);
WriteIdentifier(typeDeclaration.Name.Name);
NewLine();
WriteKeyword("End");
WriteClassTypeKeyword(typeDeclaration);
NewLine();
return EndNode(typeDeclaration);
}
void WriteClassTypeKeyword(TypeDeclaration typeDeclaration)
{
switch (typeDeclaration.ClassType) {
case ICSharpCode.NRefactory.TypeSystem.ClassType.Class:
WriteKeyword("Class");
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Enum:
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Interface:
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Struct:
WriteKeyword("Structure");
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Delegate:
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Module:
WriteKeyword("Module");
break;
default:
throw new Exception("Invalid value for ClassType");
}
} }
public object VisitXmlNamespaceImportsClause(XmlNamespaceImportsClause xmlNamespaceImportsClause, object data) public object VisitXmlNamespaceImportsClause(XmlNamespaceImportsClause xmlNamespaceImportsClause, object data)
@ -362,7 +421,6 @@ namespace ICSharpCode.NRefactory.VB
void Comma(AstNode nextNode, bool noSpaceAfterComma = false) void Comma(AstNode nextNode, bool noSpaceAfterComma = false)
{ {
WriteSpecialsUpToRole(AstNode.Roles.Comma, nextNode); WriteSpecialsUpToRole(AstNode.Roles.Comma, nextNode);
Space(); // TODO: Comma policy has changed.
formatter.WriteToken(","); formatter.WriteToken(",");
lastWritten = LastWritten.Other; lastWritten = LastWritten.Other;
Space(!noSpaceAfterComma); // TODO: Comma policy has changed. Space(!noSpaceAfterComma); // TODO: Comma policy has changed.
@ -577,6 +635,16 @@ namespace ICSharpCode.NRefactory.VB
formatter.NewLine(); formatter.NewLine();
lastWritten = LastWritten.Whitespace; lastWritten = LastWritten.Whitespace;
} }
void Indent()
{
formatter.Indent();
}
void Unindent()
{
formatter.Unindent();
}
#endregion #endregion
#region IsKeyword Test #region IsKeyword Test

58
NRefactory/ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs

@ -3,15 +3,31 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.VB.Ast; using ICSharpCode.NRefactory.VB.Ast;
namespace ICSharpCode.NRefactory.VB.Visitors namespace ICSharpCode.NRefactory.VB.Visitors
{ {
public interface IEnvironmentProvider
{
string RootNamespace { get; }
string GetTypeNameForAttribute(CSharp.Attribute attribute);
}
/// <summary> /// <summary>
/// Description of CSharpToVBConverterVisitor. /// Description of CSharpToVBConverterVisitor.
/// </summary> /// </summary>
public class CSharpToVBConverterVisitor : CSharp.IAstVisitor<object, VB.AstNode> public class CSharpToVBConverterVisitor : CSharp.IAstVisitor<object, VB.AstNode>
{ {
IEnvironmentProvider provider;
public CSharpToVBConverterVisitor(IEnvironmentProvider provider)
{
this.provider = provider;
}
public AstNode VisitAnonymousMethodExpression(CSharp.AnonymousMethodExpression anonymousMethodExpression, object data) public AstNode VisitAnonymousMethodExpression(CSharp.AnonymousMethodExpression anonymousMethodExpression, object data)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -234,12 +250,21 @@ namespace ICSharpCode.NRefactory.VB.Visitors
public AstNode VisitAttribute(CSharp.Attribute attribute, object data) public AstNode VisitAttribute(CSharp.Attribute attribute, object data)
{ {
throw new NotImplementedException(); var attr = new VB.Ast.Attribute();
// TODO : attribute targets
attr.Type = (AstType)attribute.Type.AcceptVisitor(this, data);
// ConvertNodes(attribute.Arguments, attr.Arguments);
return EndNode(attribute, attr);
} }
public AstNode VisitAttributeSection(CSharp.AttributeSection attributeSection, object data) public AstNode VisitAttributeSection(CSharp.AttributeSection attributeSection, object data)
{ {
throw new NotImplementedException(); AttributeBlock block = new AttributeBlock();
ConvertNodes(attributeSection.Attributes, block.Attributes);
return EndNode(attributeSection, block);
} }
public AstNode VisitDelegateDeclaration(CSharp.DelegateDeclaration delegateDeclaration, object data) public AstNode VisitDelegateDeclaration(CSharp.DelegateDeclaration delegateDeclaration, object data)
@ -261,6 +286,23 @@ namespace ICSharpCode.NRefactory.VB.Visitors
{ {
var type = new TypeDeclaration(); var type = new TypeDeclaration();
CSharp.Attribute stdModAttr;
if (typeDeclaration.ClassType == ClassType.Class && HasAttribute(typeDeclaration.Attributes, "Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute", out stdModAttr)) {
type.ClassType = ClassType.Module;
// remove AttributeSection if only one attribute is present
var attrSec = (CSharp.AttributeSection)stdModAttr.Parent;
if (attrSec.Attributes.Count == 1)
attrSec.Remove();
else
stdModAttr.Remove();
} else
type.ClassType = typeDeclaration.ClassType;
ConvertNodes(typeDeclaration.Attributes, type.Attributes);
type.Name = new Identifier(typeDeclaration.Name, AstLocation.Empty);
return EndNode(typeDeclaration, type); return EndNode(typeDeclaration, type);
} }
@ -620,5 +662,17 @@ namespace ICSharpCode.NRefactory.VB.Visitors
{ {
return result; return result;
} }
bool HasAttribute(CSharp.AstNodeCollection<CSharp.AttributeSection> attributes, string name, out CSharp.Attribute foundAttribute)
{
foreach (var attr in attributes.SelectMany(a => a.Attributes)) {
if (provider.GetTypeNameForAttribute(attr) == name) {
foundAttribute = attr;
return true;
}
}
foundAttribute = null;
return false;
}
} }
} }

Loading…
Cancel
Save