1
// Copyright (c) 2010-2013 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
public Expression ReplaceWith(Func<Expression, Expression> replaceFunction)
124
if (replaceFunction == null)
125
throw new ArgumentNullException("replaceFunction");
126
return (Expression)base.ReplaceWith(node => replaceFunction((Expression)node));
129
#region Builder methods
131
/// Builds an member reference expression using this expression as target.
133
public MemberReferenceExpression Member(string memberName)
135
return new MemberReferenceExpression { Target = this, MemberName = memberName };
139
/// Builds an indexer expression using this expression as target.
141
public IndexerExpression Indexer(IEnumerable<Expression> arguments)
143
IndexerExpression expr = new IndexerExpression();
145
expr.Arguments.AddRange(arguments);
150
/// Builds an indexer expression using this expression as target.
152
public IndexerExpression Indexer(params Expression[] arguments)
154
IndexerExpression expr = new IndexerExpression();
156
expr.Arguments.AddRange(arguments);
161
/// Builds an invocation expression using this expression as target.
163
public InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments)
165
return Invoke(methodName, null, arguments);
169
/// Builds an invocation expression using this expression as target.
171
public InvocationExpression Invoke(string methodName, params Expression[] arguments)
173
return Invoke(methodName, null, arguments);
177
/// Builds an invocation expression using this expression as target.
179
public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
181
InvocationExpression ie = new InvocationExpression();
182
MemberReferenceExpression mre = new MemberReferenceExpression();
184
mre.MemberName = methodName;
185
mre.TypeArguments.AddRange(typeArguments);
187
ie.Arguments.AddRange(arguments);
192
/// Builds an invocation expression using this expression as target.
194
public InvocationExpression Invoke(IEnumerable<Expression> arguments)
196
InvocationExpression ie = new InvocationExpression();
198
ie.Arguments.AddRange(arguments);
203
/// Builds an invocation expression using this expression as target.
205
public InvocationExpression Invoke(params Expression[] arguments)
207
InvocationExpression ie = new InvocationExpression();
209
ie.Arguments.AddRange(arguments);
213
public CastExpression CastTo(AstType type)
215
return new CastExpression { Type = type, Expression = this };
218
public AsExpression CastAs(AstType type)
220
return new AsExpression { Type = type, Expression = this };
223
public IsExpression IsType(AstType type)
225
return new IsExpression { Type = type, Expression = this };