Browse Source

NRefactory: remove collection setters from AST; expose AstNodeCollection<T> instead.

pull/37/head
Daniel Grunwald 14 years ago
parent
commit
9912340414
  1. 4
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 71
      ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
  3. 4
      ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs
  4. 18
      ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs
  5. 4
      ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs
  6. 10
      ICSharpCode.Decompiler/Ast/Transforms/RestoreLoop.cs
  7. 6
      ILSpy/CSharpLanguage.cs
  8. 31
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/AstNode.cs
  9. 156
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/AstNodeCollection.cs
  10. 3
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs
  11. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/AnonymousMethodExpression.cs
  12. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ArgListExpression.cs
  13. 6
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ArrayCreateExpression.cs
  14. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ArrayInitializerExpression.cs
  15. 39
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/Expression.cs
  16. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/IdentifierExpression.cs
  17. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/IndexerExpression.cs
  18. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/InvocationExpression.cs
  19. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/LambdaExpression.cs
  20. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/MemberReferenceExpression.cs
  21. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ObjectCreateExpression.cs
  22. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/PointerReferenceExpression.cs
  23. 6
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/QueryExpression.cs
  24. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/Attribute.cs
  25. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/AttributeSection.cs
  26. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/Constraint.cs
  27. 11
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/DelegateDeclaration.cs
  28. 10
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/NamespaceDeclaration.cs
  29. 14
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/TypeDeclaration.cs
  30. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/MemberType.cs
  31. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/SimpleType.cs
  32. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/BlockStatement.cs
  33. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/FixedStatement.cs
  34. 8
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/ForStatement.cs
  35. 11
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/SwitchStatement.cs
  36. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/TryCatchStatement.cs
  37. 15
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/VariableDeclarationStatement.cs
  38. 3
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/AttributedNode.cs
  39. 8
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/ConstructorDeclaration.cs
  40. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/EventDeclaration.cs
  41. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/FieldDeclaration.cs
  42. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/IndexerDeclaration.cs
  43. 11
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/MethodDeclaration.cs
  44. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/OperatorDeclaration.cs
  45. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/ParameterDeclaration.cs
  46. 4
      NRefactory/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  47. 3
      NRefactory/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

4
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -420,7 +420,7 @@ namespace Decompiler
astMethod.AddAnnotation(methodDef); astMethod.AddAnnotation(methodDef);
astMethod.Name = methodDef.Name; astMethod.Name = methodDef.Name;
astMethod.ReturnType = ConvertType(methodDef.ReturnType, methodDef.MethodReturnType); astMethod.ReturnType = ConvertType(methodDef.ReturnType, methodDef.MethodReturnType);
astMethod.Parameters = MakeParameters(methodDef.Parameters); astMethod.Parameters.AddRange(MakeParameters(methodDef.Parameters));
if (!methodDef.DeclaringType.IsInterface) { if (!methodDef.DeclaringType.IsInterface) {
astMethod.Modifiers = ConvertModifiers(methodDef); astMethod.Modifiers = ConvertModifiers(methodDef);
astMethod.Body = AstMethodBodyBuilder.CreateMethodBody(methodDef, context); astMethod.Body = AstMethodBodyBuilder.CreateMethodBody(methodDef, context);
@ -437,7 +437,7 @@ namespace Decompiler
// don't show visibility for static ctors // don't show visibility for static ctors
astMethod.Modifiers &= ~Modifiers.VisibilityMask; astMethod.Modifiers &= ~Modifiers.VisibilityMask;
} }
astMethod.Parameters = MakeParameters(methodDef.Parameters); astMethod.Parameters.AddRange(MakeParameters(methodDef.Parameters));
astMethod.Body = AstMethodBodyBuilder.CreateMethodBody(methodDef, context); astMethod.Body = AstMethodBodyBuilder.CreateMethodBody(methodDef, context);
return astMethod; return astMethod;
} }

71
ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs

@ -193,27 +193,27 @@ namespace Decompiler
ILSwitch ilSwitch = (ILSwitch)node; ILSwitch ilSwitch = (ILSwitch)node;
SwitchStatement switchStmt = new SwitchStatement() { Expression = (Expression)TransformExpression(ilSwitch.Condition.Arguments[0]) }; SwitchStatement switchStmt = new SwitchStatement() { Expression = (Expression)TransformExpression(ilSwitch.Condition.Arguments[0]) };
for (int i = 0; i < ilSwitch.CaseBlocks.Count; i++) { for (int i = 0; i < ilSwitch.CaseBlocks.Count; i++) {
switchStmt.AddChild(new SwitchSection() { SwitchSection section = new SwitchSection();
CaseLabels = new[] { new CaseLabel() { Expression = new PrimitiveExpression(i) } }, section.CaseLabels.Add(new CaseLabel() { Expression = new PrimitiveExpression(i) });
Statements = new[] { TransformBlock(ilSwitch.CaseBlocks[i]) } section.Statements.Add(TransformBlock(ilSwitch.CaseBlocks[i]));
}, SwitchStatement.SwitchSectionRole); switchStmt.SwitchSections.Add(section);
} }
yield return switchStmt; yield return switchStmt;
} else if (node is ILTryCatchBlock) { } else if (node is ILTryCatchBlock) {
ILTryCatchBlock tryCatchNode = ((ILTryCatchBlock)node); ILTryCatchBlock tryCatchNode = ((ILTryCatchBlock)node);
List<Ast.CatchClause> catchClauses = new List<CatchClause>(); var tryCatchStmt = new Ast.TryCatchStatement();
tryCatchStmt.TryBlock = TransformBlock(tryCatchNode.TryBlock);
foreach (var catchClause in tryCatchNode.CatchBlocks) { foreach (var catchClause in tryCatchNode.CatchBlocks) {
catchClauses.Add(new Ast.CatchClause { tryCatchStmt.CatchClauses.Add(
Type = AstBuilder.ConvertType(catchClause.ExceptionType), new Ast.CatchClause {
VariableName = "exception", Type = AstBuilder.ConvertType(catchClause.ExceptionType),
Body = TransformBlock(catchClause) VariableName = "exception",
}); Body = TransformBlock(catchClause)
});
} }
yield return new Ast.TryCatchStatement { if (tryCatchNode.FinallyBlock != null)
TryBlock = TransformBlock(tryCatchNode.TryBlock), tryCatchStmt.FinallyBlock = TransformBlock(tryCatchNode.FinallyBlock);
CatchClauses = catchClauses, yield return tryCatchStmt;
FinallyBlock = tryCatchNode.FinallyBlock != null ? TransformBlock(tryCatchNode.FinallyBlock) : null
};
} else if (node is ILBlock) { } else if (node is ILBlock) {
yield return TransformBlock((ILBlock)node); yield return TransformBlock((ILBlock)node);
} else { } else {
@ -402,12 +402,12 @@ namespace Decompiler
#endregion #endregion
#region Arrays #region Arrays
case Code.Newarr: case Code.Newarr:
operandAsTypeRef = operandAsTypeRef.MakeArrayType(0); {
return new Ast.ArrayCreateExpression { var ace = new Ast.ArrayCreateExpression();
Type = operandAsTypeRef, ace.Type = operandAsTypeRef;
Arguments = new Expression[] {arg1} ace.Arguments.Add(arg1);
}; return ace;
}
case Code.Ldlen: case Code.Ldlen:
return arg1.Member("Length"); return arg1.Member("Length");
@ -532,7 +532,7 @@ namespace Decompiler
{ {
Cecil.MethodReference cecilMethod = ((MethodReference)operand); Cecil.MethodReference cecilMethod = ((MethodReference)operand);
var expr = new Ast.IdentifierExpression(cecilMethod.Name); var expr = new Ast.IdentifierExpression(cecilMethod.Name);
expr.TypeArguments = ConvertTypeArguments(cecilMethod); expr.TypeArguments.AddRange(ConvertTypeArguments(cecilMethod));
expr.AddAnnotation(cecilMethod); expr.AddAnnotation(cecilMethod);
return new IdentifierExpression("ldftn").Invoke(expr) return new IdentifierExpression("ldftn").Invoke(expr)
.WithAnnotation(new Transforms.DelegateConstruction.Annotation(false)); .WithAnnotation(new Transforms.DelegateConstruction.Annotation(false));
@ -541,7 +541,7 @@ namespace Decompiler
{ {
Cecil.MethodReference cecilMethod = ((MethodReference)operand); Cecil.MethodReference cecilMethod = ((MethodReference)operand);
var expr = new Ast.IdentifierExpression(cecilMethod.Name); var expr = new Ast.IdentifierExpression(cecilMethod.Name);
expr.TypeArguments = ConvertTypeArguments(cecilMethod); expr.TypeArguments.AddRange(ConvertTypeArguments(cecilMethod));
expr.AddAnnotation(cecilMethod); expr.AddAnnotation(cecilMethod);
return new IdentifierExpression("ldvirtftn").Invoke(expr) return new IdentifierExpression("ldvirtftn").Invoke(expr)
.WithAnnotation(new Transforms.DelegateConstruction.Annotation(true)); .WithAnnotation(new Transforms.DelegateConstruction.Annotation(true));
@ -634,18 +634,21 @@ namespace Decompiler
case Code.Localloc: throw new NotImplementedException(); case Code.Localloc: throw new NotImplementedException();
case Code.Mkrefany: throw new NotImplementedException(); case Code.Mkrefany: throw new NotImplementedException();
case Code.Newobj: case Code.Newobj:
Cecil.TypeReference declaringType = ((MethodReference)operand).DeclaringType; {
// TODO: Ensure that the corrent overloaded constructor is called Cecil.TypeReference declaringType = ((MethodReference)operand).DeclaringType;
if (declaringType is ArrayType) { // TODO: Ensure that the corrent overloaded constructor is called
/*if (declaringType is ArrayType) { shouldn't this be newarr?
return new Ast.ArrayCreateExpression { return new Ast.ArrayCreateExpression {
Type = AstBuilder.ConvertType((ArrayType)declaringType), Type = AstBuilder.ConvertType((ArrayType)declaringType),
Arguments = args Arguments = args
}; };
}*/
var oce = new Ast.ObjectCreateExpression();
oce.Type = AstBuilder.ConvertType(declaringType);
oce.Arguments.AddRange(args);
return oce.WithAnnotation(operand);
} }
return new Ast.ObjectCreateExpression {
Type = AstBuilder.ConvertType(declaringType),
Arguments = args
};
case Code.No: throw new NotImplementedException(); case Code.No: throw new NotImplementedException();
case Code.Nop: return null; case Code.Nop: return null;
case Code.Pop: return arg1; case Code.Pop: return arg1;
@ -667,10 +670,10 @@ namespace Decompiler
ILVariable locVar = (ILVariable)operand; ILVariable locVar = (ILVariable)operand;
if (!definedLocalVars.Contains(locVar)) { if (!definedLocalVars.Contains(locVar)) {
definedLocalVars.Add(locVar); definedLocalVars.Add(locVar);
return new Ast.VariableDeclarationStatement() { return new Ast.VariableDeclarationStatement(
Type = locVar.Type != null ? AstBuilder.ConvertType(locVar.Type) : new Ast.PrimitiveType("var"), locVar.Type != null ? AstBuilder.ConvertType(locVar.Type) : new Ast.PrimitiveType("var"),
Variables = new[] { new Ast.VariableInitializer(locVar.Name, arg1) } locVar.Name,
}; arg1);
} else { } else {
return new Ast.AssignmentExpression(new Ast.IdentifierExpression(locVar.Name), arg1); return new Ast.AssignmentExpression(new Ast.IdentifierExpression(locVar.Name), arg1);
} }

4
ICSharpCode.Decompiler/Ast/Transforms/ConvertConstructorCallIntoInitializer.cs

@ -28,9 +28,7 @@ namespace Decompiler.Transforms
else else
return null; return null;
// Move arguments from invocation to initializer: // Move arguments from invocation to initializer:
var arguments = invocation.Arguments.ToArray(); invocation.Arguments.MoveTo(ci.Arguments);
invocation.Arguments = null;
ci.Arguments = arguments;
// Add the initializer: // Add the initializer:
constructorDeclaration.Initializer = ci.WithAnnotation(invocation.Annotation<MethodReference>()); constructorDeclaration.Initializer = ci.WithAnnotation(invocation.Annotation<MethodReference>());
// Remove the statement: // Remove the statement:

18
ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs

@ -74,15 +74,13 @@ namespace Decompiler.Transforms
} }
} }
// now transform the identifier into a member reference // now transform the identifier into a member reference
var typeArguments = methodIdent.TypeArguments.ToArray(); MemberReferenceExpression mre = new MemberReferenceExpression();
methodIdent.TypeArguments = null; mre.Target = obj;
MemberReferenceExpression mre = new MemberReferenceExpression { mre.MemberName = methodIdent.Identifier;
Target = obj, methodIdent.TypeArguments.MoveTo(mre.TypeArguments);
MemberName = methodIdent.Identifier,
TypeArguments = typeArguments
};
mre.AddAnnotation(method); mre.AddAnnotation(method);
objectCreateExpression.Arguments = new [] { mre }; objectCreateExpression.Arguments.Clear();
objectCreateExpression.Arguments.Add(mre);
return null; return null;
} }
} }
@ -112,7 +110,7 @@ namespace Decompiler.Transforms
ame.HasParameterList = false; ame.HasParameterList = false;
} else { } else {
ame.HasParameterList = true; ame.HasParameterList = true;
ame.Parameters = AstBuilder.MakeParameters(method.Parameters); ame.Parameters.AddRange(AstBuilder.MakeParameters(method.Parameters));
} }
ame.Body = body; ame.Body = body;
// Replace all occurrences of 'this' in the method body with the delegate's target: // Replace all occurrences of 'this' in the method body with the delegate's target:
@ -193,7 +191,7 @@ namespace Decompiler.Transforms
continue; continue;
VariableDeclarationStatement newVarDecl = new VariableDeclarationStatement(); VariableDeclarationStatement newVarDecl = new VariableDeclarationStatement();
newVarDecl.Type = AstBuilder.ConvertType(field.FieldType, field); newVarDecl.Type = AstBuilder.ConvertType(field.FieldType, field);
newVarDecl.Variables = new [] { new VariableInitializer(field.Name) }; newVarDecl.Variables.Add(new VariableInitializer(field.Name));
blockStatement.InsertChildBefore(cur, newVarDecl, BlockStatement.StatementRole); blockStatement.InsertChildBefore(cur, newVarDecl, BlockStatement.StatementRole);
dict[field.Name] = new IdentifierExpression(field.Name); dict[field.Name] = new IdentifierExpression(field.Name);
} }

4
ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs

@ -25,7 +25,7 @@ namespace Decompiler.Transforms
// Reduce "String.Concat(a, b)" to "a + b" // Reduce "String.Concat(a, b)" to "a + b"
if (methodRef != null && methodRef.Name == "Concat" && methodRef.DeclaringType.FullName == "System.String" && arguments.Length >= 2) if (methodRef != null && methodRef.Name == "Concat" && methodRef.DeclaringType.FullName == "System.String" && arguments.Length >= 2)
{ {
invocationExpression.Arguments = null; // detach arguments from invocationExpression invocationExpression.Arguments.Clear(); // detach arguments from invocationExpression
Expression expr = arguments[0]; Expression expr = arguments[0];
for (int i = 1; i < arguments.Length; i++) { for (int i = 1; i < arguments.Length; i++) {
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Add, arguments[i]); expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Add, arguments[i]);
@ -48,7 +48,7 @@ namespace Decompiler.Transforms
BinaryOperatorType? bop = GetBinaryOperatorTypeFromMetadataName(methodRef.Name); BinaryOperatorType? bop = GetBinaryOperatorTypeFromMetadataName(methodRef.Name);
if (bop != null && arguments.Length == 2) { if (bop != null && arguments.Length == 2) {
invocationExpression.Arguments = null; // detach arguments from invocationExpression invocationExpression.Arguments.Clear(); // detach arguments from invocationExpression
invocationExpression.ReplaceWith( invocationExpression.ReplaceWith(
new BinaryOperatorExpression(arguments[0], bop.Value, arguments[1]).WithAnnotation(methodRef) new BinaryOperatorExpression(arguments[0], bop.Value, arguments[1]).WithAnnotation(methodRef)
); );

10
ICSharpCode.Decompiler/Ast/Transforms/RestoreLoop.cs

@ -1,6 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using ICSharpCode.NRefactory.CSharp; using ICSharpCode.NRefactory.CSharp;
namespace Decompiler.Transforms.Ast namespace Decompiler.Transforms.Ast
{ {
@ -15,7 +15,7 @@ namespace Decompiler.Transforms.Ast
VariableDeclarationStatement varDeclr = forStatement.PrevSibling as VariableDeclarationStatement; VariableDeclarationStatement varDeclr = forStatement.PrevSibling as VariableDeclarationStatement;
if (varDeclr != null) { if (varDeclr != null) {
varDeclr.ReplaceWith(Statement.Null); varDeclr.ReplaceWith(Statement.Null);
forStatement.Initializers = new Statement[] { varDeclr }; forStatement.Initializers.Add(varDeclr);
} }
} }
@ -63,7 +63,7 @@ namespace Decompiler.Transforms.Ast
if (lastStmt != null && if (lastStmt != null &&
(lastStmt.Expression is AssignmentExpression || lastStmt.Expression is UnaryOperatorExpression)) { (lastStmt.Expression is AssignmentExpression || lastStmt.Expression is UnaryOperatorExpression)) {
lastStmt.Remove(); lastStmt.Remove();
forStatement.Iterators = new Statement[] { lastStmt }; forStatement.Iterators.Add(lastStmt);
} }
} }

6
ILSpy/CSharpLanguage.cs

@ -157,9 +157,9 @@ namespace ICSharpCode.ILSpy
base.VisitMemberType(memberType, data); base.VisitMemberType(memberType, data);
SimpleType st = memberType.Target as SimpleType; SimpleType st = memberType.Target as SimpleType;
if (st != null && !st.TypeArguments.Any()) { if (st != null && !st.TypeArguments.Any()) {
var ta = memberType.TypeArguments.ToArray(); SimpleType newSt = new SimpleType(memberType.MemberName);
memberType.TypeArguments = null; memberType.TypeArguments.MoveTo(newSt.TypeArguments);
memberType.ReplaceWith(new SimpleType { Identifier = memberType.MemberName, TypeArguments = ta }); memberType.ReplaceWith(newSt);
} }
return null; return null;
} }

31
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/AstNode.cs

@ -1,4 +1,4 @@
// //
// AstNode.cs // AstNode.cs
// //
// Author: // Author:
@ -164,16 +164,9 @@ namespace ICSharpCode.NRefactory.CSharp
return role.NullObject; return role.NullObject;
} }
public IEnumerable<T> GetChildrenByRole<T>(Role<T> role) where T : AstNode public AstNodeCollection<T> GetChildrenByRole<T>(Role<T> role) where T : AstNode
{ {
AstNode next; return new AstNodeCollection<T>(this, role);
for (AstNode cur = firstChild; cur != null; cur = next) {
// Remember next before yielding cur.
// This allows removing/replacing nodes while iterating through the list.
next = cur.nextSibling;
if (cur.role == role)
yield return (T)cur;
}
} }
protected void SetChildByRole<T>(Role<T> role, T newChild) where T : AstNode protected void SetChildByRole<T>(Role<T> role, T newChild) where T : AstNode
@ -185,24 +178,6 @@ namespace ICSharpCode.NRefactory.CSharp
oldChild.ReplaceWith(newChild); oldChild.ReplaceWith(newChild);
} }
protected void SetChildrenByRole<T>(Role<T> role, IEnumerable<T> newChildren) where T : AstNode
{
// Evaluate 'newChildren' first, since it might change when we remove the old children
// Example: SetChildren(role, GetChildrenByRole(role));
if (newChildren != null)
newChildren = newChildren.ToList();
// remove old children
foreach (AstNode node in GetChildrenByRole(role))
node.Remove();
// add new children
if (newChildren != null) {
foreach (T node in newChildren) {
AddChild(node, role);
}
}
}
public void AddChild<T>(T child, Role<T> role) where T : AstNode public void AddChild<T>(T child, Role<T> role) where T : AstNode
{ {
if (role == null) if (role == null)

156
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/AstNodeCollection.cs

@ -0,0 +1,156 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// Represents the children of an AstNode that have a specific role.
/// </summary>
public struct AstNodeCollection<T> : ICollection<T> where T : AstNode
{
readonly AstNode node;
readonly Role<T> role;
public AstNodeCollection(AstNode node, Role<T> role)
{
if (node == null)
throw new ArgumentNullException("node");
if (role == null)
throw new ArgumentNullException("role");
this.node = node;
this.role = role;
}
public int Count {
get {
var e = GetEnumerator();
int count = 0;
while (e.MoveNext())
count++;
return count;
}
}
public void Add(T element)
{
node.AddChild(element, role);
}
public void AddRange(IEnumerable<T> nodes)
{
// Evaluate 'nodes' first, since it might change when we add the new children
// Example: collection.AddRange(collection);
if (nodes != null) {
foreach (T node in nodes.ToList())
Add(node);
}
}
public void AddRange(T[] nodes)
{
// Fast overload for arrays - we don't need to create a copy
if (nodes != null) {
foreach (T node in nodes)
Add(node);
}
}
public void ReplaceWith(IEnumerable<T> nodes)
{
// Evaluate 'nodes' first, since it might change when we call Clear()
// Example: collection.ReplaceWith(collection);
if (nodes != null)
nodes = nodes.ToList();
Clear();
foreach (T node in nodes)
Add(node);
}
public void MoveTo(ICollection<T> targetCollection)
{
foreach (T node in this) {
node.Remove();
targetCollection.Add(node);
}
}
public bool Contains(T element)
{
return element != null && element.Parent == node && element.Role == role;
}
public bool Remove(T element)
{
if (Contains(element)) {
element.Remove();
return true;
} else {
return false;
}
}
public void CopyTo(T[] array, int arrayIndex)
{
foreach (T item in this)
array[arrayIndex++] = item;
}
public void Clear()
{
foreach (T item in this)
item.Remove();
}
bool ICollection<T>.IsReadOnly {
get { return false; }
}
public IEnumerator<T> GetEnumerator()
{
AstNode next;
for (AstNode cur = node.FirstChild; cur != null; cur = next) {
// Remember next before yielding cur.
// This allows removing/replacing nodes while iterating through the list.
next = cur.NextSibling;
if (cur.Role == role)
yield return (T)cur;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#region Equals and GetHashCode implementation
public override bool Equals(object obj)
{
if (obj is AstNodeCollection<T>) {
return ((AstNodeCollection<T>)obj) == this;
} else {
return false;
}
}
public override int GetHashCode()
{
return node.GetHashCode() ^ role.GetHashCode();
}
public static bool operator ==(AstNodeCollection<T> left, AstNodeCollection<T> right)
{
return left.role == right.role && left.node == right.node;
}
public static bool operator !=(AstNodeCollection<T> left, AstNodeCollection<T> right)
{
return !(left.role == right.role && left.node == right.node);
}
#endregion
}
}

3
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs

@ -67,9 +67,8 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<ArraySpecifier> ArraySpecifiers { public AstNodeCollection<ArraySpecifier> ArraySpecifiers {
get { return GetChildrenByRole (ArraySpecifierRole); } get { return GetChildrenByRole (ArraySpecifierRole); }
set { SetChildrenByRole (ArraySpecifierRole, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/AnonymousMethodExpression.cs

@ -1,4 +1,4 @@
// //
// AnonymousMethodExpression.cs // AnonymousMethodExpression.cs
// //
// Author: // Author:
@ -47,9 +47,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<ParameterDeclaration> Parameters { public AstNodeCollection<ParameterDeclaration> Parameters {
get { return GetChildrenByRole (Roles.Parameter); } get { return GetChildrenByRole (Roles.Parameter); }
set { SetChildrenByRole (Roles.Parameter, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ArgListExpression.cs

@ -1,4 +1,4 @@
// //
// ArgListExpression.cs // ArgListExpression.cs
// //
// Author: // Author:
@ -45,9 +45,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<Expression> Arguments { public AstNodeCollection<Expression> Arguments {
get { return GetChildrenByRole(Roles.Argument); } get { return GetChildrenByRole(Roles.Argument); }
set { SetChildrenByRole(Roles.Argument, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {

6
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ArrayCreateExpression.cs

@ -16,18 +16,16 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (Roles.Type, value); } set { SetChildByRole (Roles.Type, value); }
} }
public IEnumerable<Expression> Arguments { public AstNodeCollection<Expression> Arguments {
get { return GetChildrenByRole (Roles.Argument); } get { return GetChildrenByRole (Roles.Argument); }
set { SetChildrenByRole (Roles.Argument, value); }
} }
/// <summary> /// <summary>
/// Gets additional array ranks (those without size info). /// Gets additional array ranks (those without size info).
/// Empty for "new int[5,1]"; will contain a single element for "new int[5][]". /// Empty for "new int[5,1]"; will contain a single element for "new int[5][]".
/// </summary> /// </summary>
public IEnumerable<ArraySpecifier> AdditionalArraySpecifiers { public AstNodeCollection<ArraySpecifier> AdditionalArraySpecifiers {
get { return GetChildrenByRole(AdditionalArraySpecifierRole); } get { return GetChildrenByRole(AdditionalArraySpecifierRole); }
set { SetChildrenByRole (AdditionalArraySpecifierRole, value); }
} }
public ArrayInitializerExpression Initializer { public ArrayInitializerExpression Initializer {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ArrayInitializerExpression.cs

@ -1,4 +1,4 @@
// //
// ArrayInitializerExpression.cs // ArrayInitializerExpression.cs
// //
// Author: // Author:
@ -55,9 +55,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LBrace); } get { return GetChildByRole (Roles.LBrace); }
} }
public IEnumerable<Expression> Elements { public AstNodeCollection<Expression> Elements {
get { return GetChildrenByRole(Roles.Expression); } get { return GetChildrenByRole(Roles.Expression); }
set { SetChildrenByRole(Roles.Expression, value); }
} }
public CSharpTokenNode RBraceToken { public CSharpTokenNode RBraceToken {

39
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/Expression.cs

@ -66,7 +66,10 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary> /// </summary>
public IndexerExpression Indexer(IEnumerable<Expression> arguments) public IndexerExpression Indexer(IEnumerable<Expression> arguments)
{ {
return new IndexerExpression { Target = this, Arguments = arguments }; IndexerExpression expr = new IndexerExpression();
expr.Target = this;
expr.Arguments.AddRange(arguments);
return expr;
} }
/// <summary> /// <summary>
@ -74,7 +77,10 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary> /// </summary>
public IndexerExpression Indexer(params Expression[] arguments) public IndexerExpression Indexer(params Expression[] arguments)
{ {
return new IndexerExpression { Target = this, Arguments = arguments }; IndexerExpression expr = new IndexerExpression();
expr.Target = this;
expr.Arguments.AddRange(arguments);
return expr;
} }
/// <summary> /// <summary>
@ -98,14 +104,14 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary> /// </summary>
public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments) public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
{ {
return new InvocationExpression { InvocationExpression ie = new InvocationExpression();
Target = new MemberReferenceExpression { MemberReferenceExpression mre = new MemberReferenceExpression();
Target = this, mre.Target = this;
MemberName = methodName, mre.MemberName = methodName;
TypeArguments = typeArguments mre.TypeArguments.AddRange(typeArguments);
}, ie.Target = mre;
Arguments = arguments ie.Arguments.AddRange(arguments);
}; return ie;
} }
/// <summary> /// <summary>
@ -113,10 +119,10 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary> /// </summary>
public InvocationExpression Invoke(IEnumerable<Expression> arguments) public InvocationExpression Invoke(IEnumerable<Expression> arguments)
{ {
return new InvocationExpression { InvocationExpression ie = new InvocationExpression();
Target = this, ie.Target = this;
Arguments = arguments ie.Arguments.AddRange(arguments);
}; return ie;
} }
/// <summary> /// <summary>
@ -124,7 +130,10 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary> /// </summary>
public InvocationExpression Invoke(params Expression[] arguments) public InvocationExpression Invoke(params Expression[] arguments)
{ {
return Invoke(arguments.AsEnumerable()); InvocationExpression ie = new InvocationExpression();
ie.Target = this;
ie.Arguments.AddRange(arguments);
return ie;
} }
public CastExpression CastTo(AstType type) public CastExpression CastTo(AstType type)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/IdentifierExpression.cs

@ -1,4 +1,4 @@
// //
// IdentifierExpression.cs // IdentifierExpression.cs
// //
// Author: // Author:
@ -56,9 +56,8 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<AstType> TypeArguments { public AstNodeCollection<AstType> TypeArguments {
get { return GetChildrenByRole (Roles.TypeArgument); } get { return GetChildrenByRole (Roles.TypeArgument); }
set { SetChildrenByRole (Roles.TypeArgument, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/IndexerExpression.cs

@ -1,4 +1,4 @@
// //
// IndexerExpression.cs // IndexerExpression.cs
// //
// Author: // Author:
@ -42,9 +42,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LBracket); } get { return GetChildByRole (Roles.LBracket); }
} }
public IEnumerable<Expression> Arguments { public AstNodeCollection<Expression> Arguments {
get { return GetChildrenByRole<Expression>(Roles.Argument); } get { return GetChildrenByRole<Expression>(Roles.Argument); }
set { SetChildrenByRole(Roles.Argument, value); }
} }
public CSharpTokenNode RBracketToken { public CSharpTokenNode RBracketToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/InvocationExpression.cs

@ -1,4 +1,4 @@
// //
// InvocationExpression.cs // InvocationExpression.cs
// //
// Author: // Author:
@ -42,9 +42,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<Expression> Arguments { public AstNodeCollection<Expression> Arguments {
get { return GetChildrenByRole<Expression>(Roles.Argument); } get { return GetChildrenByRole<Expression>(Roles.Argument); }
set { SetChildrenByRole(Roles.Argument, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/LambdaExpression.cs

@ -1,4 +1,4 @@
// //
// LambdaExpression.cs // LambdaExpression.cs
// //
// Author: // Author:
@ -36,9 +36,8 @@ namespace ICSharpCode.NRefactory.CSharp
public readonly static Role<CSharpTokenNode> ArrowRole = new Role<CSharpTokenNode>("Arrow", CSharpTokenNode.Null); public readonly static Role<CSharpTokenNode> ArrowRole = new Role<CSharpTokenNode>("Arrow", CSharpTokenNode.Null);
public static readonly Role<AstNode> BodyRole = new Role<AstNode>("Body", AstNode.Null); public static readonly Role<AstNode> BodyRole = new Role<AstNode>("Body", AstNode.Null);
public IEnumerable<ParameterDeclaration> Parameters { public AstNodeCollection<ParameterDeclaration> Parameters {
get { return GetChildrenByRole (Roles.Parameter); } get { return GetChildrenByRole (Roles.Parameter); }
set { SetChildrenByRole (Roles.Parameter, value); }
} }
public CSharpTokenNode ArrowToken { public CSharpTokenNode ArrowToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/MemberReferenceExpression.cs

@ -1,4 +1,4 @@
// //
// MemberReferenceExpression.cs // MemberReferenceExpression.cs
// //
// Author: // Author:
@ -51,9 +51,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LChevron); } get { return GetChildByRole (Roles.LChevron); }
} }
public IEnumerable<AstType> TypeArguments { public AstNodeCollection<AstType> TypeArguments {
get { return GetChildrenByRole (Roles.TypeArgument); } get { return GetChildrenByRole (Roles.TypeArgument); }
set { SetChildrenByRole (Roles.TypeArgument, value); }
} }
public CSharpTokenNode RChevronToken { public CSharpTokenNode RChevronToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/ObjectCreateExpression.cs

@ -1,4 +1,4 @@
// //
// ObjectCreateExpression.cs // ObjectCreateExpression.cs
// //
// Author: // Author:
@ -48,9 +48,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<Expression> Arguments { public AstNodeCollection<Expression> Arguments {
get { return GetChildrenByRole (Roles.Argument); } get { return GetChildrenByRole (Roles.Argument); }
set { SetChildrenByRole (Roles.Argument, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/PointerReferenceExpression.cs

@ -1,4 +1,4 @@
// //
// PointerReferenceExpression.cs // PointerReferenceExpression.cs
// //
// Author: // Author:
@ -49,9 +49,8 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<AstType> TypeArguments { public AstNodeCollection<AstType> TypeArguments {
get { return GetChildrenByRole (Roles.TypeArgument); } get { return GetChildrenByRole (Roles.TypeArgument); }
set { SetChildrenByRole (Roles.TypeArgument, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

6
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Expressions/QueryExpression.cs

@ -28,9 +28,8 @@ namespace ICSharpCode.NRefactory.CSharp
} }
#endregion #endregion
public IEnumerable<QueryClause> Clauses { public AstNodeCollection<QueryClause> Clauses {
get { return GetChildrenByRole(ClauseRole); } get { return GetChildrenByRole(ClauseRole); }
set { SetChildrenByRole(ClauseRole, value); }
} }
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
@ -260,9 +259,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.Keyword); } get { return GetChildByRole (Roles.Keyword); }
} }
public IEnumerable<QueryOrdering> Orderings { public AstNodeCollection<QueryOrdering> Orderings {
get { return GetChildrenByRole (OrderingRole); } get { return GetChildrenByRole (OrderingRole); }
set { SetChildrenByRole (OrderingRole, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/Attribute.cs

@ -1,4 +1,4 @@
// //
// Attribute.cs // Attribute.cs
// //
// Author: // Author:
@ -44,9 +44,8 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (Roles.Type, value); } set { SetChildByRole (Roles.Type, value); }
} }
public IEnumerable<Expression> Arguments { public AstNodeCollection<Expression> Arguments {
get { return base.GetChildrenByRole (Roles.Argument); } get { return base.GetChildrenByRole (Roles.Argument); }
set { SetChildrenByRole (Roles.Argument, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/AttributeSection.cs

@ -1,4 +1,4 @@
// //
// AttributeSection.cs // AttributeSection.cs
// //
// Author: // Author:
@ -49,9 +49,8 @@ namespace ICSharpCode.NRefactory.CSharp
set; set;
} }
public IEnumerable<Attribute> Attributes { public AstNodeCollection<Attribute> Attributes {
get { return base.GetChildrenByRole (AttributeRole); } get { return base.GetChildrenByRole (AttributeRole); }
set { SetChildrenByRole (AttributeRole, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/Constraint.cs

@ -1,4 +1,4 @@
// //
// Constraint.cs // Constraint.cs
// //
// Author: // Author:
@ -53,9 +53,8 @@ namespace ICSharpCode.NRefactory.CSharp
// TODO: what about new(), struct and class constraints? // TODO: what about new(), struct and class constraints?
public IEnumerable<AstType> BaseTypes { public AstNodeCollection<AstType> BaseTypes {
get { return GetChildrenByRole (BaseTypeRole); } get { return GetChildrenByRole (BaseTypeRole); }
set { SetChildrenByRole (BaseTypeRole, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

11
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/DelegateDeclaration.cs

@ -1,4 +1,4 @@
// //
// DelegateDeclaration.cs // DelegateDeclaration.cs
// //
// Author: // Author:
@ -54,27 +54,24 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (Roles.Type, value); } set { SetChildByRole (Roles.Type, value); }
} }
public IEnumerable<TypeParameterDeclaration> TypeParameters { public AstNodeCollection<TypeParameterDeclaration> TypeParameters {
get { return GetChildrenByRole (Roles.TypeParameter); } get { return GetChildrenByRole (Roles.TypeParameter); }
set { SetChildrenByRole (Roles.TypeParameter, value); }
} }
public CSharpTokenNode LParToken { public CSharpTokenNode LParToken {
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<ParameterDeclaration> Parameters { public AstNodeCollection<ParameterDeclaration> Parameters {
get { return GetChildrenByRole (Roles.Parameter); } get { return GetChildrenByRole (Roles.Parameter); }
set { SetChildrenByRole (Roles.Parameter, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {
get { return GetChildByRole (Roles.RPar); } get { return GetChildByRole (Roles.RPar); }
} }
public IEnumerable<Constraint> Constraints { public AstNodeCollection<Constraint> Constraints {
get { return GetChildrenByRole (Roles.Constraint); } get { return GetChildrenByRole (Roles.Constraint); }
set { SetChildrenByRole (Roles.Constraint, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

10
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/NamespaceDeclaration.cs

@ -1,4 +1,4 @@
// //
// NamespaceDeclaration.cs // NamespaceDeclaration.cs
// //
// Author: // Author:
@ -54,13 +54,12 @@ namespace ICSharpCode.NRefactory.CSharp
return builder.ToString (); return builder.ToString ();
} }
set { set {
SetChildrenByRole (Roles.Identifier, value.Split('.').Select(ident => new Identifier(ident, AstLocation.Empty))); GetChildrenByRole(Roles.Identifier).ReplaceWith(value.Split('.').Select(ident => new Identifier(ident, AstLocation.Empty)));
} }
} }
public IEnumerable<Identifier> Identifiers { public AstNodeCollection<Identifier> Identifiers {
get { return GetChildrenByRole (Roles.Identifier); } get { return GetChildrenByRole (Roles.Identifier); }
set { SetChildrenByRole (Roles.Identifier, value); }
} }
/// <summary> /// <summary>
@ -79,9 +78,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LBrace); } get { return GetChildByRole (Roles.LBrace); }
} }
public IEnumerable<AstNode> Members { public AstNodeCollection<AstNode> Members {
get { return GetChildrenByRole(MemberRole); } get { return GetChildrenByRole(MemberRole); }
set { SetChildrenByRole(MemberRole, value); }
} }
public CSharpTokenNode RBraceToken { public CSharpTokenNode RBraceToken {

14
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/TypeDeclaration.cs

@ -1,4 +1,4 @@
// //
// TypeDeclaration.cs // TypeDeclaration.cs
// //
// Author: // Author:
@ -59,28 +59,24 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<TypeParameterDeclaration> TypeParameters { public AstNodeCollection<TypeParameterDeclaration> TypeParameters {
get { return GetChildrenByRole (Roles.TypeParameter); } get { return GetChildrenByRole (Roles.TypeParameter); }
set { SetChildrenByRole (Roles.TypeParameter, value); }
} }
public IEnumerable<AstType> BaseTypes { public AstNodeCollection<AstType> BaseTypes {
get { return GetChildrenByRole (BaseTypeRole); } get { return GetChildrenByRole (BaseTypeRole); }
set { SetChildrenByRole (BaseTypeRole, value); }
} }
public IEnumerable<Constraint> Constraints { public AstNodeCollection<Constraint> Constraints {
get { return GetChildrenByRole (Roles.Constraint); } get { return GetChildrenByRole (Roles.Constraint); }
set { SetChildrenByRole (Roles.Constraint, value); }
} }
public CSharpTokenNode LBraceToken { public CSharpTokenNode LBraceToken {
get { return GetChildByRole (Roles.LBrace); } get { return GetChildByRole (Roles.LBrace); }
} }
public IEnumerable<AttributedNode> Members { public AstNodeCollection<AttributedNode> Members {
get { return GetChildrenByRole (MemberRole); } get { return GetChildrenByRole (MemberRole); }
set { SetChildrenByRole (MemberRole, value); }
} }
public CSharpTokenNode RBraceToken { public CSharpTokenNode RBraceToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/MemberType.cs

@ -1,4 +1,4 @@
// //
// FullTypeName.cs // FullTypeName.cs
// //
// Author: // Author:
@ -51,9 +51,8 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<AstType> TypeArguments { public AstNodeCollection<AstType> TypeArguments {
get { return GetChildrenByRole (Roles.TypeArgument); } get { return GetChildrenByRole (Roles.TypeArgument); }
set { SetChildrenByRole (Roles.TypeArgument, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/SimpleType.cs

@ -1,4 +1,4 @@
// //
// FullTypeName.cs // FullTypeName.cs
// //
// Author: // Author:
@ -56,9 +56,8 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<AstType> TypeArguments { public AstNodeCollection<AstType> TypeArguments {
get { return GetChildrenByRole (Roles.TypeArgument); } get { return GetChildrenByRole (Roles.TypeArgument); }
set { SetChildrenByRole (Roles.TypeArgument, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/BlockStatement.cs

@ -1,4 +1,4 @@
// //
// BlockStatement.cs // BlockStatement.cs
// //
// Author: // Author:
@ -56,9 +56,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LBrace); } get { return GetChildByRole (Roles.LBrace); }
} }
public IEnumerable<Statement> Statements { public AstNodeCollection<Statement> Statements {
get { return GetChildrenByRole (StatementRole); } get { return GetChildrenByRole (StatementRole); }
set { SetChildrenByRole (StatementRole, value); }
} }
public CSharpTokenNode RBraceToken { public CSharpTokenNode RBraceToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/FixedStatement.cs

@ -1,4 +1,4 @@
// //
// FixedStatement.cs // FixedStatement.cs
// //
// Author: // Author:
@ -46,9 +46,8 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (Roles.Type, value); } set { SetChildByRole (Roles.Type, value); }
} }
public IEnumerable<VariableInitializer> Variables { public AstNodeCollection<VariableInitializer> Variables {
get { return GetChildrenByRole (Roles.Variable); } get { return GetChildrenByRole (Roles.Variable); }
set { SetChildrenByRole (Roles.Variable, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {

8
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/ForStatement.cs

@ -1,4 +1,4 @@
// //
// ForStatement.cs // ForStatement.cs
// //
// Author: // Author:
@ -49,9 +49,8 @@ namespace ICSharpCode.NRefactory.CSharp
/// Note: this contains multiple statements for "for (a = 2, b = 1; a > b; a--)", but contains /// Note: this contains multiple statements for "for (a = 2, b = 1; a > b; a--)", but contains
/// only a single statement for "for (int a = 2, b = 1; a > b; a--)" (a single VariableDeclarationStatement with two variables) /// only a single statement for "for (int a = 2, b = 1; a > b; a--)" (a single VariableDeclarationStatement with two variables)
/// </summary> /// </summary>
public IEnumerable<Statement> Initializers { public AstNodeCollection<Statement> Initializers {
get { return GetChildrenByRole (InitializerRole); } get { return GetChildrenByRole (InitializerRole); }
set { SetChildrenByRole (InitializerRole, value); }
} }
public Expression Condition { public Expression Condition {
@ -59,9 +58,8 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (Roles.Condition, value); } set { SetChildByRole (Roles.Condition, value); }
} }
public IEnumerable<Statement> Iterators { public AstNodeCollection<Statement> Iterators {
get { return GetChildrenByRole (IteratorRole); } get { return GetChildrenByRole (IteratorRole); }
set { SetChildrenByRole (IteratorRole, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {

11
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/SwitchStatement.cs

@ -1,4 +1,4 @@
// //
// SwitchStatement.cs // SwitchStatement.cs
// //
// Author: // Author:
@ -57,9 +57,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LBrace); } get { return GetChildByRole (Roles.LBrace); }
} }
public IEnumerable<SwitchSection> SwitchSections { public AstNodeCollection<SwitchSection> SwitchSections {
get { return GetChildrenByRole (SwitchSectionRole); } get { return GetChildrenByRole (SwitchSectionRole); }
set { SetChildrenByRole (SwitchSectionRole, value); }
} }
public CSharpTokenNode RBraceToken { public CSharpTokenNode RBraceToken {
@ -82,14 +81,12 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<CaseLabel> CaseLabels { public AstNodeCollection<CaseLabel> CaseLabels {
get { return GetChildrenByRole (CaseLabelRole); } get { return GetChildrenByRole (CaseLabelRole); }
set { SetChildrenByRole (CaseLabelRole, value); }
} }
public IEnumerable<Statement> Statements { public AstNodeCollection<Statement> Statements {
get { return GetChildrenByRole (Roles.EmbeddedStatement); } get { return GetChildrenByRole (Roles.EmbeddedStatement); }
set { SetChildrenByRole (Roles.EmbeddedStatement, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/TryCatchStatement.cs

@ -1,4 +1,4 @@
// //
// TryCatchStatement.cs // TryCatchStatement.cs
// //
// Author: // Author:
@ -49,9 +49,8 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (TryBlockRole, value); } set { SetChildByRole (TryBlockRole, value); }
} }
public IEnumerable<CatchClause> CatchClauses { public AstNodeCollection<CatchClause> CatchClauses {
get { return GetChildrenByRole (CatchClauseRole); } get { return GetChildrenByRole (CatchClauseRole); }
set { SetChildrenByRole (CatchClauseRole, value); }
} }
public CSharpTokenNode FinallyToken { public CSharpTokenNode FinallyToken {

15
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Statements/VariableDeclarationStatement.cs

@ -1,4 +1,4 @@
// //
// VariableDeclarationStatement.cs // VariableDeclarationStatement.cs
// //
// Author: // Author:
@ -33,6 +33,16 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
public static readonly Role<CSharpModifierToken> ModifierRole = AttributedNode.ModifierRole; public static readonly Role<CSharpModifierToken> ModifierRole = AttributedNode.ModifierRole;
public VariableDeclarationStatement()
{
}
public VariableDeclarationStatement(AstType type, string name, Expression initializer = null)
{
this.Type = type;
this.Variables.Add(new VariableInitializer(name, initializer));
}
public Modifiers Modifiers { public Modifiers Modifiers {
get { return AttributedNode.GetModifiers(this); } get { return AttributedNode.GetModifiers(this); }
set { AttributedNode.SetModifiers(this, value); } set { AttributedNode.SetModifiers(this, value); }
@ -43,9 +53,8 @@ namespace ICSharpCode.NRefactory.CSharp
set { SetChildByRole (Roles.Type, value); } set { SetChildByRole (Roles.Type, value); }
} }
public IEnumerable<VariableInitializer> Variables { public AstNodeCollection<VariableInitializer> Variables {
get { return GetChildrenByRole (Roles.Variable); } get { return GetChildrenByRole (Roles.Variable); }
set { SetChildrenByRole (Roles.Variable, value); }
} }
public CSharpTokenNode SemicolonToken { public CSharpTokenNode SemicolonToken {

3
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/AttributedNode.cs

@ -11,9 +11,8 @@ namespace ICSharpCode.NRefactory.CSharp
public static readonly Role<AttributeSection> AttributeRole = new Role<AttributeSection>("Attribute"); public static readonly Role<AttributeSection> AttributeRole = new Role<AttributeSection>("Attribute");
public static readonly Role<CSharpModifierToken> ModifierRole = new Role<CSharpModifierToken>("Modifier"); public static readonly Role<CSharpModifierToken> ModifierRole = new Role<CSharpModifierToken>("Modifier");
public IEnumerable<AttributeSection> Attributes { public AstNodeCollection<AttributeSection> Attributes {
get { return base.GetChildrenByRole (AttributeRole); } get { return base.GetChildrenByRole (AttributeRole); }
set { SetChildrenByRole (AttributeRole, value); }
} }
public Modifiers Modifiers { public Modifiers Modifiers {

8
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/ConstructorDeclaration.cs

@ -1,4 +1,4 @@
// //
// ConstructorDeclaration.cs // ConstructorDeclaration.cs
// //
// Author: // Author:
@ -37,9 +37,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<ParameterDeclaration> Parameters { public AstNodeCollection<ParameterDeclaration> Parameters {
get { return GetChildrenByRole (Roles.Parameter); } get { return GetChildrenByRole (Roles.Parameter); }
set { SetChildrenByRole (Roles.Parameter, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {
@ -105,9 +104,8 @@ namespace ICSharpCode.NRefactory.CSharp
set; set;
} }
public IEnumerable<Expression> Arguments { public AstNodeCollection<Expression> Arguments {
get { return GetChildrenByRole (Roles.Argument); } get { return GetChildrenByRole (Roles.Argument); }
set { SetChildrenByRole (Roles.Argument, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/EventDeclaration.cs

@ -1,4 +1,4 @@
// //
// EventDeclaration.cs // EventDeclaration.cs
// //
// Author: // Author:
@ -30,9 +30,8 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
public class EventDeclaration : MemberDeclaration public class EventDeclaration : MemberDeclaration
{ {
public IEnumerable<VariableInitializer> Variables { public AstNodeCollection<VariableInitializer> Variables {
get { return GetChildrenByRole (Roles.Variable); } get { return GetChildrenByRole (Roles.Variable); }
set { SetChildrenByRole (Roles.Variable, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/FieldDeclaration.cs

@ -1,4 +1,4 @@
// //
// FieldDeclaration.cs // FieldDeclaration.cs
// //
// Author: // Author:
@ -31,9 +31,8 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
public class FieldDeclaration : MemberDeclaration public class FieldDeclaration : MemberDeclaration
{ {
public IEnumerable<VariableInitializer> Variables { public AstNodeCollection<VariableInitializer> Variables {
get { return GetChildrenByRole (Roles.Variable); } get { return GetChildrenByRole (Roles.Variable); }
set { SetChildrenByRole (Roles.Variable, value); }
} }
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/IndexerDeclaration.cs

@ -1,4 +1,4 @@
// //
// IndexerDeclaration.cs // IndexerDeclaration.cs
// //
// Author: // Author:
@ -35,9 +35,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LBracket); } get { return GetChildByRole (Roles.LBracket); }
} }
public IEnumerable<ParameterDeclaration> Parameters { public AstNodeCollection<ParameterDeclaration> Parameters {
get { return GetChildrenByRole (Roles.Parameter); } get { return GetChildrenByRole (Roles.Parameter); }
set { SetChildrenByRole (Roles.Parameter, value); }
} }
public CSharpTokenNode RBracketToken { public CSharpTokenNode RBracketToken {

11
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/MethodDeclaration.cs

@ -1,4 +1,4 @@
// //
// MethodDeclaration.cs // MethodDeclaration.cs
// //
// Author: // Author:
@ -31,27 +31,24 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
public class MethodDeclaration : MemberDeclaration public class MethodDeclaration : MemberDeclaration
{ {
public IEnumerable<TypeParameterDeclaration> TypeParameters { public AstNodeCollection<TypeParameterDeclaration> TypeParameters {
get { return GetChildrenByRole (Roles.TypeParameter); } get { return GetChildrenByRole (Roles.TypeParameter); }
set { SetChildrenByRole (Roles.TypeParameter, value); }
} }
public CSharpTokenNode LParToken { public CSharpTokenNode LParToken {
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<ParameterDeclaration> Parameters { public AstNodeCollection<ParameterDeclaration> Parameters {
get { return GetChildrenByRole (Roles.Parameter); } get { return GetChildrenByRole (Roles.Parameter); }
set { SetChildrenByRole (Roles.Parameter, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {
get { return GetChildByRole (Roles.RPar); } get { return GetChildByRole (Roles.RPar); }
} }
public IEnumerable<Constraint> Constraints { public AstNodeCollection<Constraint> Constraints {
get { return GetChildrenByRole (Roles.Constraint); } get { return GetChildrenByRole (Roles.Constraint); }
set { SetChildrenByRole (Roles.Constraint, value); }
} }
public BlockStatement Body { public BlockStatement Body {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/OperatorDeclaration.cs

@ -1,4 +1,4 @@
// //
// OperatorDeclaration.cs // OperatorDeclaration.cs
// //
// Author: // Author:
@ -80,9 +80,8 @@ namespace ICSharpCode.NRefactory.CSharp
get { return GetChildByRole (Roles.LPar); } get { return GetChildByRole (Roles.LPar); }
} }
public IEnumerable<ParameterDeclaration> Parameters { public AstNodeCollection<ParameterDeclaration> Parameters {
get { return GetChildrenByRole (Roles.Parameter); } get { return GetChildrenByRole (Roles.Parameter); }
set { SetChildrenByRole (Roles.Parameter, value); }
} }
public CSharpTokenNode RParToken { public CSharpTokenNode RParToken {

5
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/ParameterDeclaration.cs

@ -1,4 +1,4 @@
// //
// ParameterDeclarationExpression.cs // ParameterDeclarationExpression.cs
// //
// Author: // Author:
@ -49,9 +49,8 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<AttributeSection> Attributes { public AstNodeCollection<AttributeSection> Attributes {
get { return GetChildrenByRole (AttributeRole); } get { return GetChildrenByRole (AttributeRole); }
set { SetChildrenByRole (AttributeRole, value); }
} }
public ParameterModifier ParameterModifier { public ParameterModifier ParameterModifier {

4
NRefactory/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

@ -122,9 +122,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null) if (location != null)
spec.AddChild (new CSharpTokenNode (Convert (location[0]), 1), FieldDeclaration.Roles.RBracket); spec.AddChild (new CSharpTokenNode (Convert (location[0]), 1), FieldDeclaration.Roles.RBracket);
result.ArraySpecifiers = new ArraySpecifier[] { result.ArraySpecifiers.Add(spec);
spec
};
} }
return result; return result;
} }

3
NRefactory/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup> <PropertyGroup>
<ProjectGuid>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</ProjectGuid> <ProjectGuid>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</ProjectGuid>
@ -56,6 +56,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CSharp\Ast\AstComparer.cs" /> <Compile Include="CSharp\Ast\AstComparer.cs" />
<Compile Include="CSharp\Ast\AstNodeCollection.cs" />
<Compile Include="CSharp\Ast\Expressions\TypeReferenceExpression.cs" /> <Compile Include="CSharp\Ast\Expressions\TypeReferenceExpression.cs" />
<Compile Include="CSharp\Ast\IAstVisitor.cs" /> <Compile Include="CSharp\Ast\IAstVisitor.cs" />
<Compile Include="CSharp\Ast\CompilationUnit.cs" /> <Compile Include="CSharp\Ast\CompilationUnit.cs" />

Loading…
Cancel
Save