1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
22
namespace ICSharpCode.NRefactory.CSharp
25
/// Base class for expressions.
28
/// This class is useful even though it doesn't provide any additional functionality:
29
/// It can be used to communicate more information in APIs, e.g. "this subnode will always be an expression"
31
public abstract class Expression : AstNode
34
public new static readonly Expression Null = new NullExpression ();
36
sealed class NullExpression : Expression
38
public override bool IsNull {
44
public override void AcceptVisitor (IAstVisitor visitor)
48
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
53
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
58
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
60
return other == null || other.IsNull;
65
#region PatternPlaceholder
66
public static implicit operator Expression(PatternMatching.Pattern pattern)
68
return pattern != null ? new PatternPlaceholder(pattern) : null;
71
sealed class PatternPlaceholder : Expression, PatternMatching.INode
73
readonly PatternMatching.Pattern child;
75
public PatternPlaceholder(PatternMatching.Pattern child)
80
public override NodeType NodeType {
81
get { return NodeType.Pattern; }
84
public override void AcceptVisitor (IAstVisitor visitor)
86
visitor.VisitPatternPlaceholder(this, child);
89
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
91
return visitor.VisitPatternPlaceholder(this, child);
94
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
96
return visitor.VisitPatternPlaceholder(this, child, data);
99
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
101
return child.DoMatch(other, match);
104
bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
106
return child.DoMatchCollection(role, pos, match, backtrackingInfo);
111
public override NodeType NodeType {
113
return NodeType.Expression;
117
public new Expression Clone()
119
return (Expression)base.Clone();
122
// Make debugging easier by giving Expressions a ToString() implementation
123
public override string ToString()
125
return DebugToString();
128
public Expression ReplaceWith(Func<Expression, Expression> replaceFunction)
130
if (replaceFunction == null)
131
throw new ArgumentNullException("replaceFunction");
132
return (Expression)base.ReplaceWith(node => replaceFunction((Expression)node));
135
#region Builder methods
137
/// Builds an member reference expression using this expression as target.
139
public MemberReferenceExpression Member(string memberName)
141
return new MemberReferenceExpression { Target = this, MemberName = memberName };
145
/// Builds an indexer expression using this expression as target.
147
public IndexerExpression Indexer(IEnumerable<Expression> arguments)
149
IndexerExpression expr = new IndexerExpression();
151
expr.Arguments.AddRange(arguments);
156
/// Builds an indexer expression using this expression as target.
158
public IndexerExpression Indexer(params Expression[] arguments)
160
IndexerExpression expr = new IndexerExpression();
162
expr.Arguments.AddRange(arguments);
167
/// Builds an invocation expression using this expression as target.
169
public InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments)
171
return Invoke(methodName, null, arguments);
175
/// Builds an invocation expression using this expression as target.
177
public InvocationExpression Invoke(string methodName, params Expression[] arguments)
179
return Invoke(methodName, null, arguments);
183
/// Builds an invocation expression using this expression as target.
185
public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
187
InvocationExpression ie = new InvocationExpression();
188
MemberReferenceExpression mre = new MemberReferenceExpression();
190
mre.MemberName = methodName;
191
mre.TypeArguments.AddRange(typeArguments);
193
ie.Arguments.AddRange(arguments);
198
/// Builds an invocation expression using this expression as target.
200
public InvocationExpression Invoke(IEnumerable<Expression> arguments)
202
InvocationExpression ie = new InvocationExpression();
204
ie.Arguments.AddRange(arguments);
209
/// Builds an invocation expression using this expression as target.
211
public InvocationExpression Invoke(params Expression[] arguments)
213
InvocationExpression ie = new InvocationExpression();
215
ie.Arguments.AddRange(arguments);
219
public CastExpression CastTo(AstType type)
221
return new CastExpression { Type = type, Expression = this };
224
public AsExpression CastAs(AstType type)
226
return new AsExpression { Type = type, Expression = this };
229
public IsExpression IsType(AstType type)
231
return new IsExpression { Type = type, Expression = this };