From 4ea7ebc517fff9e094535540e6a791566822d157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Tue, 13 Mar 2012 12:36:19 +0100 Subject: [PATCH] Moved more roles to the Roles class/Changed TypeDeclaration representation (saves some memory and class type is now always defined). --- .../Ast/CompilationUnit.cs | 13 ++- .../Ast/GeneralScope/DelegateDeclaration.cs | 6 +- .../GeneralScope/ExternAliasDeclaration.cs | 7 +- .../Ast/GeneralScope/NamespaceDeclaration.cs | 4 +- .../Ast/GeneralScope/TypeDeclaration.cs | 108 +++++++++++++++--- ICSharpCode.NRefactory.CSharp/Ast/Roles.cs | 15 ++- .../Completion/CSharpCompletionEngine.cs | 4 +- .../OutputVisitor/CSharpOutputVisitor.cs | 24 ++-- .../Parser/CSharpParser.cs | 69 ++++++----- .../Refactoring/TypeSystemAstBuilder.cs | 8 +- .../CSharp/AstStructureTests.cs | 5 +- .../CSharp/CSharpOutputVisitorTests.cs | 5 +- .../GeneralScope/AttributeSectionTests.cs | 11 +- .../PreprocessorDirectiveTests.cs | 8 +- .../GeneralScope/TypeDeclarationTests.cs | 36 +++--- .../TypeMembers/MethodDeclarationTests.cs | 13 +-- .../TypeMembers/PropertyDeclarationTests.cs | 4 +- 17 files changed, 206 insertions(+), 134 deletions(-) diff --git a/ICSharpCode.NRefactory.CSharp/Ast/CompilationUnit.cs b/ICSharpCode.NRefactory.CSharp/Ast/CompilationUnit.cs index 4e9845945b..a58eb16212 100644 --- a/ICSharpCode.NRefactory.CSharp/Ast/CompilationUnit.cs +++ b/ICSharpCode.NRefactory.CSharp/Ast/CompilationUnit.cs @@ -1,4 +1,4 @@ -// +// // CompilationUnit.cs // // Author: @@ -72,17 +72,18 @@ namespace ICSharpCode.NRefactory.CSharp { } - public IEnumerable GetTypes (bool includeInnerTypes = false) + public IEnumerable GetTypes(bool includeInnerTypes = false) { Stack nodeStack = new Stack (); - nodeStack.Push (this); + nodeStack.Push(this); while (nodeStack.Count > 0) { - var curNode = nodeStack.Pop (); - if (curNode is TypeDeclaration) + var curNode = nodeStack.Pop(); + if (curNode is TypeDeclaration) { yield return (TypeDeclaration)curNode; + } foreach (var child in curNode.Children) { if (!(child is Statement || child is Expression) && - (child.Role != TypeDeclaration.MemberRole || (child is TypeDeclaration && includeInnerTypes))) + (child.Role != Roles.TypeMemberRole || (child is TypeDeclaration && includeInnerTypes))) nodeStack.Push (child); } } diff --git a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/DelegateDeclaration.cs b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/DelegateDeclaration.cs index c397920549..088afa90b3 100644 --- a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/DelegateDeclaration.cs +++ b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/DelegateDeclaration.cs @@ -33,8 +33,6 @@ namespace ICSharpCode.NRefactory.CSharp /// public class DelegateDeclaration : EntityDeclaration { - public readonly static TokenRole DelegateKeywordRole = new TokenRole ("delegate"); - public override NodeType NodeType { get { return NodeType.TypeDeclaration; } } @@ -44,9 +42,9 @@ namespace ICSharpCode.NRefactory.CSharp } public CSharpTokenNode DelegateToken { - get { return GetChildByRole (DelegateKeywordRole); } + get { return GetChildByRole(Roles.DelegateKeyword); } } - + public AstNodeCollection TypeParameters { get { return GetChildrenByRole (Roles.TypeParameter); } } diff --git a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/ExternAliasDeclaration.cs b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/ExternAliasDeclaration.cs index f2310d5a53..9404d417f4 100644 --- a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/ExternAliasDeclaration.cs +++ b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/ExternAliasDeclaration.cs @@ -31,9 +31,6 @@ namespace ICSharpCode.NRefactory.CSharp /// public class ExternAliasDeclaration : AstNode { - public static readonly TokenRole ExternKeywordRole = new TokenRole ("extern"); - public static readonly TokenRole AliasKeywordRole = new TokenRole ("alias"); - public override NodeType NodeType { get { return NodeType.Unknown; @@ -41,11 +38,11 @@ namespace ICSharpCode.NRefactory.CSharp } public CSharpTokenNode ExternToken { - get { return GetChildByRole (ExternKeywordRole); } + get { return GetChildByRole (Roles.ExternKeyword); } } public CSharpTokenNode AliasToken { - get { return GetChildByRole (AliasKeywordRole); } + get { return GetChildByRole (Roles.AliasKeyword); } } public string Name { diff --git a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/NamespaceDeclaration.cs b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/NamespaceDeclaration.cs index c5052b54b4..01198222b4 100644 --- a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/NamespaceDeclaration.cs +++ b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/NamespaceDeclaration.cs @@ -35,8 +35,6 @@ namespace ICSharpCode.NRefactory.CSharp /// public class NamespaceDeclaration : AstNode { - public static readonly TokenRole NamespaceKeywordRole = new TokenRole ("namespace"); - public static readonly Role MemberRole = CompilationUnit.MemberRole; public override NodeType NodeType { @@ -46,7 +44,7 @@ namespace ICSharpCode.NRefactory.CSharp } public CSharpTokenNode NamespaceToken { - get { return GetChildByRole (NamespaceKeywordRole); } + get { return GetChildByRole (Roles.NamespaceKeyword); } } public string Name { diff --git a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/TypeDeclaration.cs b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/TypeDeclaration.cs index 8c4f40fcb8..73a203d88a 100644 --- a/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/TypeDeclaration.cs +++ b/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/TypeDeclaration.cs @@ -41,15 +41,8 @@ namespace ICSharpCode.NRefactory.CSharp /// /// class Name<TypeParameters> : BaseTypes where Constraints; /// - public class TypeDeclaration : EntityDeclaration + public abstract class TypeDeclaration : EntityDeclaration { - public static readonly TokenRole EnumKeywordRole = new TokenRole ("enum"); - public static readonly TokenRole InterfaceKeywordRole = new TokenRole ("interface"); - public static readonly TokenRole StructKeywordRole = new TokenRole ("struct"); - public static readonly TokenRole ClassKeywordRole = new TokenRole ("class"); - - public readonly static Role MemberRole = new Role("Member"); - public override NodeType NodeType { get { return NodeType.TypeDeclaration; } } @@ -58,22 +51,25 @@ namespace ICSharpCode.NRefactory.CSharp get { return EntityType.TypeDefinition; } } - public ClassType ClassType { + + public abstract CSharpTokenNode TypeKeyword { get; - set; } - + + public abstract ClassType ClassType { + get; + } + public AstNodeCollection TypeParameters { get { return GetChildrenByRole (Roles.TypeParameter); } } public AstNodeCollection BaseTypes { - get { - return GetChildrenByRole(Roles.BaseType); } + get { return GetChildrenByRole(Roles.BaseType); } } public AstNodeCollection Constraints { - get { return GetChildrenByRole (Roles.Constraint); } + get { return GetChildrenByRole(Roles.Constraint); } } public CSharpTokenNode LBraceToken { @@ -81,7 +77,7 @@ namespace ICSharpCode.NRefactory.CSharp } public AstNodeCollection Members { - get { return GetChildrenByRole (MemberRole); } + get { return GetChildrenByRole (Roles.TypeMemberRole); } } public CSharpTokenNode RBraceToken { @@ -111,5 +107,87 @@ namespace ICSharpCode.NRefactory.CSharp && this.BaseTypes.DoMatch(o.BaseTypes, match) && this.Constraints.DoMatch(o.Constraints, match) && this.Members.DoMatch(o.Members, match); } + + protected TypeDeclaration () + { + } + + public static TypeDeclaration Create(ClassType type) + { + switch (type) { + case ICSharpCode.NRefactory.CSharp.ClassType.Class: + return new Class (); + case ICSharpCode.NRefactory.CSharp.ClassType.Struct: + return new Struct (); + case ICSharpCode.NRefactory.CSharp.ClassType.Interface: + return new Interface (); + case ICSharpCode.NRefactory.CSharp.ClassType.Enum: + return new Enum (); + default: + throw new System.ArgumentOutOfRangeException(); + } + } + + #region Concrete Types + public class Class : TypeDeclaration + { + public override ClassType ClassType { + get { + return ClassType.Class; + } + } + + public override CSharpTokenNode TypeKeyword { + get { + return GetChildByRole(Roles.ClassKeyword); + } + } + } + + public class Struct : TypeDeclaration + { + public override ClassType ClassType { + get { + return ClassType.Struct; + } + } + + public override CSharpTokenNode TypeKeyword { + get { + return GetChildByRole(Roles.StructKeyword); + } + } + } + + public class Interface : TypeDeclaration + { + public override ClassType ClassType { + get { + return ClassType.Interface; + } + } + + public override CSharpTokenNode TypeKeyword { + get { + return GetChildByRole(Roles.InterfaceKeyword); + } + } + } + + public class Enum : TypeDeclaration + { + public override ClassType ClassType { + get { + return ClassType.Enum; + } + } + + public override CSharpTokenNode TypeKeyword { + get { + return GetChildByRole(Roles.EnumKeyword); + } + } + } + #endregion } } diff --git a/ICSharpCode.NRefactory.CSharp/Ast/Roles.cs b/ICSharpCode.NRefactory.CSharp/Ast/Roles.cs index 64b6f4d997..8f3562ccad 100644 --- a/ICSharpCode.NRefactory.CSharp/Ast/Roles.cs +++ b/ICSharpCode.NRefactory.CSharp/Ast/Roles.cs @@ -49,7 +49,10 @@ namespace ICSharpCode.NRefactory.CSharp public readonly static Role Constraint = new Role ("Constraint"); public static readonly Role Variable = new Role ("Variable", VariableInitializer.Null); public static readonly Role EmbeddedStatement = new Role ("EmbeddedStatement", CSharp.Statement.Null); -// public static readonly TokenRole Keyword = new TokenRole ("Keyword", CSharpTokenNode.Null); + public readonly static Role TypeMemberRole = new Role ("TypeMember"); + + + // public static readonly TokenRole Keyword = new TokenRole ("Keyword", CSharpTokenNode.Null); // public static readonly TokenRole InKeyword = new TokenRole ("InKeyword", CSharpTokenNode.Null); // some pre defined constants for most used punctuation @@ -78,6 +81,16 @@ namespace ICSharpCode.NRefactory.CSharp public readonly static TokenRole WhereKeyword = new TokenRole ("where"); public readonly static Role ConstraintTypeParameter = new Role ("TypeParameter", SimpleType.Null); + public readonly static TokenRole DelegateKeyword = new TokenRole ("delegate"); + public static readonly TokenRole ExternKeyword = new TokenRole ("extern"); + public static readonly TokenRole AliasKeyword = new TokenRole ("alias"); + public static readonly TokenRole NamespaceKeyword = new TokenRole ("namespace"); + + public static readonly TokenRole EnumKeyword = new TokenRole ("enum"); + public static readonly TokenRole InterfaceKeyword = new TokenRole ("interface"); + public static readonly TokenRole StructKeyword = new TokenRole ("struct"); + public static readonly TokenRole ClassKeyword = new TokenRole ("class"); + } } diff --git a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs index f72cc23d02..22219ff31d 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs @@ -1,4 +1,4 @@ -// +// // CSharpCompletionEngine.cs // // Author: @@ -2238,7 +2238,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion member2.Remove (); if (member is TypeDeclaration) { - member.AddChild (member2, TypeDeclaration.MemberRole); + member.AddChild (member2, Roles.TypeMemberRole); } else { member.ReplaceWith (member2); } diff --git a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs index 2513e53d0a..1cf0728d31 100644 --- a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs +++ b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs @@ -1382,7 +1382,7 @@ namespace ICSharpCode.NRefactory.CSharp StartNode (delegateDeclaration); WriteAttributes (delegateDeclaration.Attributes); WriteModifiers (delegateDeclaration.ModifierTokens); - WriteKeyword (DelegateDeclaration.DelegateKeywordRole); + WriteKeyword (Roles.DelegateKeyword); delegateDeclaration.ReturnType.AcceptVisitor (this); Space (); WriteIdentifier (delegateDeclaration.Name); @@ -1396,10 +1396,10 @@ namespace ICSharpCode.NRefactory.CSharp EndNode (delegateDeclaration); } - public void VisitNamespaceDeclaration (NamespaceDeclaration namespaceDeclaration) + public void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration) { - StartNode (namespaceDeclaration); - WriteKeyword (NamespaceDeclaration.NamespaceKeywordRole); + StartNode(namespaceDeclaration); + WriteKeyword(Roles.NamespaceKeyword); WriteQualifiedIdentifier (namespaceDeclaration.Identifiers); OpenBrace (policy.NamespaceBraceStyle); foreach (var member in namespaceDeclaration.Members) @@ -1418,19 +1418,19 @@ namespace ICSharpCode.NRefactory.CSharp BraceStyle braceStyle; switch (typeDeclaration.ClassType) { case ClassType.Enum: - WriteKeyword(TypeDeclaration.EnumKeywordRole); + WriteKeyword(Roles.EnumKeyword); braceStyle = policy.EnumBraceStyle; break; case ClassType.Interface: - WriteKeyword(TypeDeclaration.InterfaceKeywordRole); + WriteKeyword(Roles.InterfaceKeyword); braceStyle = policy.InterfaceBraceStyle; break; case ClassType.Struct: - WriteKeyword(TypeDeclaration.StructKeywordRole); + WriteKeyword(Roles.StructKeyword); braceStyle = policy.StructBraceStyle; break; default: - WriteKeyword(TypeDeclaration.ClassKeywordRole); + WriteKeyword(Roles.ClassKeyword); braceStyle = policy.ClassBraceStyle; break; } @@ -1492,12 +1492,12 @@ namespace ICSharpCode.NRefactory.CSharp EndNode (usingDeclaration); } - public void VisitExternAliasDeclaration (ExternAliasDeclaration externAliasDeclaration) + public void VisitExternAliasDeclaration(ExternAliasDeclaration externAliasDeclaration) { - StartNode (externAliasDeclaration); - WriteKeyword (ExternAliasDeclaration.ExternKeywordRole); + StartNode(externAliasDeclaration); + WriteKeyword(Roles.ExternKeyword); Space (); - WriteKeyword (ExternAliasDeclaration.AliasKeywordRole); + WriteKeyword (Roles.AliasKeyword); Space (); externAliasDeclaration.NameToken.AcceptVisitor (this); Semicolon (); diff --git a/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs b/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs index f920fda3d6..3177968ef0 100644 --- a/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs +++ b/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs @@ -81,7 +81,7 @@ namespace ICSharpCode.NRefactory.CSharp if (nspace.NS != null && !string.IsNullOrEmpty (nspace.NS.Name)) { nDecl = new NamespaceDeclaration (); if (loc != null) - nDecl.AddChild (new CSharpTokenNode (Convert (loc [0])), NamespaceDeclaration.NamespaceKeywordRole); + nDecl.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.NamespaceKeyword); ConvertNamespaceName (nspace.RealMemberName, nDecl); if (loc != null && loc.Count > 1) nDecl.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.LBrace); @@ -352,7 +352,7 @@ namespace ICSharpCode.NRefactory.CSharp if (nspace.NS != null && !string.IsNullOrEmpty (nspace.NS.Name)) { nDecl = new NamespaceDeclaration (); if (loc != null) - nDecl.AddChild (new CSharpTokenNode (Convert (loc [0])), NamespaceDeclaration.NamespaceKeywordRole); + nDecl.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.NamespaceKeyword); ConvertNamespaceName (nspace.RealMemberName, nDecl); if (loc != null && loc.Count > 1) nDecl.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.LBrace); @@ -442,9 +442,9 @@ namespace ICSharpCode.NRefactory.CSharp { var ud = new ExternAliasDeclaration (); var loc = LocationsBag.GetLocations (uea); - ud.AddChild (new CSharpTokenNode (Convert (uea.Location)), ExternAliasDeclaration.ExternKeywordRole); + ud.AddChild (new CSharpTokenNode (Convert (uea.Location)), Roles.ExternKeyword); if (loc != null) - ud.AddChild (new CSharpTokenNode (Convert (loc [0])), ExternAliasDeclaration.AliasKeywordRole); + ud.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.AliasKeyword); ud.AddChild (Identifier.Create (uea.Alias.Value, Convert (uea.Alias.Location)), Roles.Identifier); if (loc != null && loc.Count > 1) ud.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Semicolon); @@ -483,15 +483,14 @@ namespace ICSharpCode.NRefactory.CSharp public override void Visit(Class c) { - TypeDeclaration newType = new TypeDeclaration (); - newType.ClassType = ClassType.Class; + var newType = new TypeDeclaration.Class (); AddAttributeSection(newType, c); var location = LocationsBag.GetMemberLocation(c); AddModifiers(newType, location); int curLoc = 0; if (location != null) - newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), TypeDeclaration.ClassKeywordRole); + newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.ClassKeyword); newType.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier); AddTypeParameters (newType, c.MemberName); @@ -534,14 +533,13 @@ namespace ICSharpCode.NRefactory.CSharp public override void Visit(Struct s) { - TypeDeclaration newType = new TypeDeclaration (); - newType.ClassType = ClassType.Struct; + var newType = new TypeDeclaration.Struct(); AddAttributeSection(newType, s); var location = LocationsBag.GetMemberLocation(s); AddModifiers(newType, location); int curLoc = 0; if (location != null) - newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), TypeDeclaration.StructKeywordRole); + newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.StructKeyword); newType.AddChild (Identifier.Create (s.MemberName.Name, Convert (s.MemberName.Location)), Roles.Identifier); AddTypeParameters (newType, s.MemberName); @@ -579,14 +577,13 @@ namespace ICSharpCode.NRefactory.CSharp public override void Visit(Interface i) { - TypeDeclaration newType = new TypeDeclaration (); - newType.ClassType = ClassType.Interface; + var newType = new TypeDeclaration.Interface (); AddAttributeSection(newType, i); var location = LocationsBag.GetMemberLocation(i); AddModifiers(newType, location); int curLoc = 0; if (location != null) - newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), TypeDeclaration.InterfaceKeywordRole); + newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.InterfaceKeyword); newType.AddChild (Identifier.Create (i.MemberName.Name, Convert (i.MemberName.Location)), Roles.Identifier); AddTypeParameters (newType, i.MemberName); @@ -622,14 +619,15 @@ namespace ICSharpCode.NRefactory.CSharp AddType (newType); } - public override void Visit (Mono.CSharp.Delegate d) + public override void Visit(Mono.CSharp.Delegate d) { DelegateDeclaration newDelegate = new DelegateDeclaration (); - var location = LocationsBag.GetMemberLocation (d); - AddAttributeSection (newDelegate, d); - AddModifiers (newDelegate, location); - if (location != null) - newDelegate.AddChild (new CSharpTokenNode (Convert (location [0])), DelegateDeclaration.DelegateKeywordRole); + var location = LocationsBag.GetMemberLocation(d); + AddAttributeSection(newDelegate, d); + AddModifiers(newDelegate, location); + if (location != null) { + newDelegate.AddChild(new CSharpTokenNode (Convert(location [0])), Roles.DelegateKeyword); + } newDelegate.AddChild (ConvertToType (d.ReturnType), Roles.Type); newDelegate.AddChild (Identifier.Create (d.MemberName.Name, Convert (d.MemberName.Location)), Roles.Identifier); AddTypeParameters (newDelegate, d.MemberName); @@ -651,7 +649,7 @@ namespace ICSharpCode.NRefactory.CSharp void AddType (EntityDeclaration child) { if (typeStack.Count > 0) { - typeStack.Peek ().AddChild (child, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (child, Roles.TypeMemberRole); } else { AddToNamespace (child); } @@ -668,15 +666,14 @@ namespace ICSharpCode.NRefactory.CSharp public override void Visit(Mono.CSharp.Enum e) { - TypeDeclaration newType = new TypeDeclaration (); + var newType = new TypeDeclaration.Enum (); AddAttributeSection(newType, e); - newType.ClassType = ClassType.Enum; var location = LocationsBag.GetMemberLocation(e); AddModifiers(newType, location); int curLoc = 0; if (location != null) - newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), TypeDeclaration.EnumKeywordRole); + newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.EnumKeyword); newType.AddChild (Identifier.Create (e.MemberName.Name, Convert (e.MemberName.Location)), Roles.Identifier); if (e.BaseTypeExpression != null) { @@ -719,7 +716,7 @@ namespace ICSharpCode.NRefactory.CSharp } //Console.WriteLine (newField.StartLocation +"-" + newField.EndLocation); - typeStack.Peek ().AddChild (newField, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole); } #endregion @@ -771,7 +768,7 @@ namespace ICSharpCode.NRefactory.CSharp } if (location != null) newField.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon); - typeStack.Peek ().AddChild (newField, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole); } @@ -812,7 +809,7 @@ namespace ICSharpCode.NRefactory.CSharp if (location != null) newField.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1])), Roles.Semicolon); - typeStack.Peek ().AddChild (newField, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole); } public override void Visit (Const f) @@ -852,7 +849,7 @@ namespace ICSharpCode.NRefactory.CSharp if (location != null) newField.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon); - typeStack.Peek ().AddChild (newField, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole); } @@ -902,7 +899,7 @@ namespace ICSharpCode.NRefactory.CSharp if (location != null && location.Count >= 5) newOperator.AddChild (new CSharpTokenNode (Convert (location [4])), Roles.Semicolon); } - typeStack.Peek ().AddChild (newOperator, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newOperator, Roles.TypeMemberRole); } public void AddAttributeSection (AstNode parent, Attributable a) @@ -985,7 +982,7 @@ namespace ICSharpCode.NRefactory.CSharp // parser error, set end node to max value. newIndexer.AddChild (new ErrorNode (), Roles.Error); } - typeStack.Peek ().AddChild (newIndexer, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newIndexer, Roles.TypeMemberRole); } public override void Visit (Method m) @@ -1024,7 +1021,7 @@ namespace ICSharpCode.NRefactory.CSharp newMethod.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.Semicolon); } } - typeStack.Peek ().AddChild (newMethod, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newMethod, Roles.TypeMemberRole); } static Dictionary modifierTable = new Dictionary (); @@ -1151,7 +1148,7 @@ namespace ICSharpCode.NRefactory.CSharp newProperty.AddChild (new ErrorNode (), Roles.Error); } - typeStack.Peek ().AddChild (newProperty, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newProperty, Roles.TypeMemberRole); } public override void Visit (Constructor c) @@ -1188,7 +1185,7 @@ namespace ICSharpCode.NRefactory.CSharp if (c.Block != null) newConstructor.AddChild ((BlockStatement)c.Block.Accept (this), Roles.Body); - typeStack.Peek ().AddChild (newConstructor, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newConstructor, Roles.TypeMemberRole); } public override void Visit (Destructor d) @@ -1211,7 +1208,7 @@ namespace ICSharpCode.NRefactory.CSharp if (d.Block != null) newDestructor.AddChild ((BlockStatement)d.Block.Accept (this), Roles.Body); - typeStack.Peek ().AddChild (newDestructor, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newDestructor, Roles.TypeMemberRole); } public override void Visit (EventField e) @@ -1255,9 +1252,9 @@ namespace ICSharpCode.NRefactory.CSharp if (location != null && location.Count > 1) newEvent.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon); - typeStack.Peek ().AddChild (newEvent, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole); } - + void AddExplicitInterface (AstNode parent, MemberName memberName) { if (memberName == null || memberName.ExplicitInterface == null) @@ -1316,7 +1313,7 @@ namespace ICSharpCode.NRefactory.CSharp newEvent.AddChild (new ErrorNode (), Roles.Error); } - typeStack.Peek ().AddChild (newEvent, TypeDeclaration.MemberRole); + typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole); } #endregion diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/TypeSystemAstBuilder.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/TypeSystemAstBuilder.cs index 56e4066403..a3682bdc8c 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/TypeSystemAstBuilder.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/TypeSystemAstBuilder.cs @@ -466,18 +466,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring break; case TypeKind.Delegate: IMethod invoke = typeDefinition.GetDelegateInvokeMethod(); - if (invoke != null) + if (invoke != null) { return ConvertDelegate(invoke, modifiers); - else + } else { goto default; + } default: classType = ClassType.Class; break; } - TypeDeclaration decl = new TypeDeclaration(); + var decl = TypeDeclaration.Create(classType); decl.Modifiers = modifiers; - decl.ClassType = classType; decl.Name = typeDefinition.Name; int outerTypeParameterCount = (typeDefinition.DeclaringTypeDefinition == null) ? 0 : typeDefinition.DeclaringTypeDefinition.TypeParameterCount; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/AstStructureTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/AstStructureTests.cs index 777f2e8697..8ac0654313 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/AstStructureTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/AstStructureTests.cs @@ -26,11 +26,12 @@ namespace ICSharpCode.NRefactory.CSharp public class AstStructureTests { [Test] - public void RolesAreStaticReadOnly() + public void RolesAreStaticReadOnly () { foreach (Type type in typeof(AstNode).Assembly.GetExportedTypes()) { - if (type.IsSubclassOf(typeof(AstNode))) { + if (type.IsSubclassOf (typeof(AstNode))) { foreach (FieldInfo field in type.GetFields()) { + Console.WriteLine (field); if (field.FieldType.IsSubclassOf(typeof(Role))) { Assert.IsTrue(field.IsPublic); Assert.IsTrue(field.IsStatic); diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs index 47f8c56c32..24c44bc3e8 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs @@ -51,10 +51,9 @@ namespace ICSharpCode.NRefactory.CSharp } [Test] - public void EnumDeclarationWithInitializers() + public void EnumDeclarationWithInitializers () { - TypeDeclaration type = new TypeDeclaration { - ClassType = ClassType.Enum, + TypeDeclaration type = new TypeDeclaration.Enum { Name = "DisplayFlags", Members = { new EnumMemberDeclaration { diff --git a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs index a48a5ef090..8dd47d6b91 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs @@ -97,7 +97,7 @@ public class Form1 { { ParseUtilCSharp.AssertGlobal( @"[A, B] class Test {}", - new TypeDeclaration { + new TypeDeclaration.Class { Name = "Test", Attributes = { new AttributeSection { @@ -113,8 +113,7 @@ public class Form1 { { ParseUtilCSharp.AssertGlobal( "class Test<[A,B]C> {}", - new TypeDeclaration { - ClassType = ClassType.Class, + new TypeDeclaration.Class { Name = "Test", TypeParameters = { new TypeParameterDeclaration { @@ -169,11 +168,11 @@ public class Form1 { // TODO: Tests for other contexts where attributes can appear [Test] - public void AttributeWithNamedArguments() + public void AttributeWithNamedArguments () { - ParseUtilCSharp.AssertTypeMember( + ParseUtilCSharp.AssertTypeMember ( @"[A(0, a:1, b=2)] class Test {}", - new TypeDeclaration { + new TypeDeclaration.Class { Name = "Test", Attributes = { new AttributeSection { diff --git a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/PreprocessorDirectiveTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/PreprocessorDirectiveTests.cs index f3505ceab1..21074279f7 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/PreprocessorDirectiveTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/PreprocessorDirectiveTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // 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 @@ -37,7 +37,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope Assert.AreEqual(0, ns.Members.Count); Assert.AreEqual(new Role[] { - NamespaceDeclaration.NamespaceKeywordRole, + Roles.NamespaceKeyword, Roles.Identifier, Roles.LBrace, Roles.PreProcessorDirective, @@ -83,7 +83,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope Assert.AreEqual(0, ns.Members.Count); Assert.AreEqual(new Role[] { - NamespaceDeclaration.NamespaceKeywordRole, + Roles.NamespaceKeyword, Roles.Identifier, Roles.LBrace, Roles.PreProcessorDirective, @@ -110,7 +110,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope Assert.AreEqual(0, ns.Members.Count); Assert.AreEqual(new Role[] { - NamespaceDeclaration.NamespaceKeywordRole, + Roles.NamespaceKeyword, Roles.Identifier, Roles.LBrace, Roles.PreProcessorDirective, diff --git a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs index caffedb205..7c40d5f78c 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // 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 @@ -89,9 +89,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope { ParseUtilCSharp.AssertGlobal( "public class G {}", - new TypeDeclaration { + new TypeDeclaration.Class { Modifiers = Modifiers.Public, - ClassType = ClassType.Class, Name = "G", TypeParameters = { new TypeParameterDeclaration { Name = "T" } } }); @@ -102,9 +101,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope { ParseUtilCSharp.AssertGlobal( @"public class Test where T : IMyInterface { }", - new TypeDeclaration { + new TypeDeclaration.Class { Modifiers = Modifiers.Public, - ClassType = ClassType.Class, Name = "Test", TypeParameters = { new TypeParameterDeclaration { Name = "T" } }, Constraints = { @@ -120,9 +118,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope { ParseUtilCSharp.AssertGlobal( "public interface Generic : System.IComparable where S : G, new() where T : MyNamespace.IMyInterface {}", - new TypeDeclaration { + new TypeDeclaration.Interface { Modifiers = Modifiers.Public, - ClassType = ClassType.Interface, Name = "Generic", TypeParameters = { new TypeParameterDeclaration { Variance = VarianceModifier.Contravariant, Name = "T" }, @@ -167,7 +164,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 { }", - new TypeDeclaration { + new TypeDeclaration.Class { Attributes = { new AttributeSection { Attributes = { @@ -176,7 +173,6 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 } }, Modifiers = Modifiers.Public | Modifiers.Abstract, - ClassType = ClassType.Class, Name = "MyClass", BaseTypes = { new SimpleType("MyBase"), @@ -223,9 +219,8 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 { ParseUtilCSharp.AssertGlobal( "partial class partial<[partial: where] where> where where : partial { }", - new TypeDeclaration { + new TypeDeclaration.Class { Modifiers = Modifiers.Partial, - ClassType = ClassType.Class, Name = "partial", TypeParameters = { new TypeParameterDeclaration { @@ -305,12 +300,11 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 } [Test, Ignore("Mono parser crash")] - public void EnumWithIncorrectNewlineAfterIntegerLiteral() + public void EnumWithIncorrectNewlineAfterIntegerLiteral () { - ParseUtilCSharp.AssertGlobal( + ParseUtilCSharp.AssertGlobal ( "enum DisplayFlags { D = 4\r\r\n}", - new TypeDeclaration { - ClassType = ClassType.Enum, + new TypeDeclaration.Enum { Name = "DisplayFlags", Members = { new EnumMemberDeclaration { @@ -326,10 +320,10 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 TypeDeclaration td = ParseUtilCSharp.ParseGlobal("enum MyEnum { A, }"); Assert.AreEqual( new Role[] { - TypeDeclaration.EnumKeywordRole, + Roles.EnumKeyword, Roles.Identifier, Roles.LBrace, - TypeDeclaration.MemberRole, + Roles.TypeMemberRole, Roles.Comma, Roles.RBrace }, td.Children.Select(c => c.Role).ToArray()); @@ -341,10 +335,10 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 TypeDeclaration td = ParseUtilCSharp.ParseGlobal("enum MyEnum { A, };"); Assert.AreEqual( new Role[] { - TypeDeclaration.EnumKeywordRole, + Roles.EnumKeyword, Roles.Identifier, Roles.LBrace, - TypeDeclaration.MemberRole, + Roles.TypeMemberRole, Roles.Comma, Roles.RBrace, Roles.Semicolon @@ -357,10 +351,10 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 TypeDeclaration td = ParseUtilCSharp.ParseGlobal("enum MyEnum { A };"); Assert.AreEqual( new Role[] { - TypeDeclaration.EnumKeywordRole, + Roles.EnumKeyword, Roles.Identifier, Roles.LBrace, - TypeDeclaration.MemberRole, + Roles.TypeMemberRole, Roles.RBrace, Roles.Semicolon }, td.Children.Select(c => c.Role).ToArray()); diff --git a/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.cs index e3558fd00f..b126ac1081 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.cs @@ -165,8 +165,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers T MyMethod(T a) where T : ISomeInterface; } ", - new TypeDeclaration { - ClassType = ClassType.Interface, + new TypeDeclaration.Interface { Name = "MyInterface", Members = { new MethodDeclaration { @@ -191,8 +190,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers void MyMethod(T a) where T : ISomeInterface; } ", - new TypeDeclaration { - ClassType = ClassType.Interface, + new TypeDeclaration.Interface { Name = "MyInterface", Members = { new MethodDeclaration { @@ -210,15 +208,14 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers } [Test] - public void ShadowingMethodInInterface() + public void ShadowingMethodInInterface () { - ParseUtilCSharp.AssertGlobal( + ParseUtilCSharp.AssertGlobal ( @"interface MyInterface : IDisposable { new void Dispose(); } ", - new TypeDeclaration { - ClassType = ClassType.Interface, + new TypeDeclaration.Interface { Name = "MyInterface", BaseTypes = { new SimpleType("IDisposable") }, Members = { diff --git a/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/PropertyDeclarationTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/PropertyDeclarationTests.cs index e9fa3567cb..cdce97c4f7 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/PropertyDeclarationTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/PropertyDeclarationTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // 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 @@ -73,7 +73,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers CSharpParser parser = new CSharpParser(); CompilationUnit cu = parser.Parse(new StringReader(code), "parsed.cs"); - PropertyDeclaration pd = (PropertyDeclaration)cu.Children.Single().GetChildByRole(TypeDeclaration.MemberRole); + PropertyDeclaration pd = (PropertyDeclaration)cu.Children.Single().GetChildByRole(Roles.TypeMemberRole); Assert.AreEqual(new TextLocation(2, code.IndexOf("{\n\t\tget") - line2Pos + 1), pd.GetChildByRole(Roles.LBrace).StartLocation); Assert.AreEqual(new TextLocation(5, 3), pd.EndLocation); Assert.AreEqual(new TextLocation(3, code.IndexOf("{ return") - line3Pos + 1), pd.Getter.Body.StartLocation);