From b9dc346330f981e2e542de24e276fec21bcfd77a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 8 May 2011 21:32:56 +0200 Subject: [PATCH] implement Attribute conversion --- .../OutputVisitor/OutputVisitor.cs | 78 +++++++++++++++++-- .../Visitors/CSharpToVBConverterVisitor.cs | 58 +++++++++++++- 2 files changed, 129 insertions(+), 7 deletions(-) diff --git a/ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs b/ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs index 907e1a6178..e16957fe8a 100644 --- a/ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs +++ b/ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; - using ICSharpCode.NRefactory.PatternMatching; using ICSharpCode.NRefactory.VB.Ast; @@ -95,12 +94,28 @@ namespace ICSharpCode.NRefactory.VB 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) { - 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) @@ -136,6 +151,17 @@ namespace ICSharpCode.NRefactory.VB node.AcceptVisitor(this, null); } NewLine(); + Indent(); + isFirst = true; + foreach (var member in namespaceDeclaration.Members) { + if (isFirst) { + isFirst = false; + } else { + NewLine(); + } + member.AcceptVisitor(this, data); + } + Unindent(); WriteKeyword("End"); WriteKeyword("Namespace"); NewLine(); @@ -149,7 +175,40 @@ namespace ICSharpCode.NRefactory.VB 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) @@ -362,7 +421,6 @@ namespace ICSharpCode.NRefactory.VB void Comma(AstNode nextNode, bool noSpaceAfterComma = false) { WriteSpecialsUpToRole(AstNode.Roles.Comma, nextNode); - Space(); // TODO: Comma policy has changed. formatter.WriteToken(","); lastWritten = LastWritten.Other; Space(!noSpaceAfterComma); // TODO: Comma policy has changed. @@ -577,6 +635,16 @@ namespace ICSharpCode.NRefactory.VB formatter.NewLine(); lastWritten = LastWritten.Whitespace; } + + void Indent() + { + formatter.Indent(); + } + + void Unindent() + { + formatter.Unindent(); + } #endregion #region IsKeyword Test diff --git a/ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs b/ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs index 0077ab687a..4a3cc0609b 100644 --- a/ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs +++ b/ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs @@ -3,15 +3,31 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ICSharpCode.NRefactory.TypeSystem; using ICSharpCode.NRefactory.VB.Ast; namespace ICSharpCode.NRefactory.VB.Visitors { + public interface IEnvironmentProvider + { + string RootNamespace { get; } + string GetTypeNameForAttribute(CSharp.Attribute attribute); + } + /// /// Description of CSharpToVBConverterVisitor. /// public class CSharpToVBConverterVisitor : CSharp.IAstVisitor { + IEnvironmentProvider provider; + + public CSharpToVBConverterVisitor(IEnvironmentProvider provider) + { + this.provider = provider; + } + public AstNode VisitAnonymousMethodExpression(CSharp.AnonymousMethodExpression anonymousMethodExpression, object data) { throw new NotImplementedException(); @@ -234,12 +250,21 @@ namespace ICSharpCode.NRefactory.VB.Visitors 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) { - throw new NotImplementedException(); + AttributeBlock block = new AttributeBlock(); + ConvertNodes(attributeSection.Attributes, block.Attributes); + return EndNode(attributeSection, block); } public AstNode VisitDelegateDeclaration(CSharp.DelegateDeclaration delegateDeclaration, object data) @@ -261,6 +286,23 @@ namespace ICSharpCode.NRefactory.VB.Visitors { 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); } @@ -620,5 +662,17 @@ namespace ICSharpCode.NRefactory.VB.Visitors { return result; } + + bool HasAttribute(CSharp.AstNodeCollection 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; + } } }