PageRenderTime 66ms CodeModel.GetById 23ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/icsharpcode/ILSpy
C# | 230 lines | 152 code | 30 blank | 48 comment | 5 complexity | 0ca2d5b11068be7810b7fc37c92bf092 MD5 | raw file
  1// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
  2// 
  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:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 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.
 18
 19using System;
 20using System.Collections.Generic;
 21
 22namespace ICSharpCode.NRefactory.CSharp
 23{
 24	/// <summary>
 25	/// Base class for expressions.
 26	/// </summary>
 27	/// <remarks>
 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"
 30	/// </remarks>
 31	public abstract class Expression : AstNode
 32	{
 33		#region Null
 34		public new static readonly Expression Null = new NullExpression ();
 35		
 36		sealed class NullExpression : Expression
 37		{
 38			public override bool IsNull {
 39				get {
 40					return true;
 41				}
 42			}
 43			
 44			public override void AcceptVisitor (IAstVisitor visitor)
 45			{
 46				visitor.VisitNullNode(this);
 47			}
 48			
 49			public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 50			{
 51				return visitor.VisitNullNode(this);
 52			}
 53			
 54			public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
 55			{
 56				return visitor.VisitNullNode(this, data);
 57			}
 58			
 59			protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 60			{
 61				return other == null || other.IsNull;
 62			}
 63		}
 64		#endregion
 65		
 66		#region PatternPlaceholder
 67		public static implicit operator Expression(PatternMatching.Pattern pattern)
 68		{
 69			return pattern != null ? new PatternPlaceholder(pattern) : null;
 70		}
 71		
 72		sealed class PatternPlaceholder : Expression, PatternMatching.INode
 73		{
 74			readonly PatternMatching.Pattern child;
 75			
 76			public PatternPlaceholder(PatternMatching.Pattern child)
 77			{
 78				this.child = child;
 79			}
 80			
 81			public override NodeType NodeType {
 82				get { return NodeType.Pattern; }
 83			}
 84			
 85			public override void AcceptVisitor (IAstVisitor visitor)
 86			{
 87				visitor.VisitPatternPlaceholder(this, child);
 88			}
 89				
 90			public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 91			{
 92				return visitor.VisitPatternPlaceholder(this, child);
 93			}
 94			
 95			public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
 96			{
 97				return visitor.VisitPatternPlaceholder(this, child, data);
 98			}
 99			
100			protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
101			{
102				return child.DoMatch(other, match);
103			}
104			
105			bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
106			{
107				return child.DoMatchCollection(role, pos, match, backtrackingInfo);
108			}
109		}
110		#endregion
111		
112		public override NodeType NodeType {
113			get {
114				return NodeType.Expression;
115			}
116		}
117		
118		public new Expression Clone()
119		{
120			return (Expression)base.Clone();
121		}
122
123		public Expression ReplaceWith(Func<Expression, Expression> replaceFunction)
124		{
125			if (replaceFunction == null)
126				throw new ArgumentNullException("replaceFunction");
127			return (Expression)base.ReplaceWith(node => replaceFunction((Expression)node));
128		}
129		
130		#region Builder methods
131		/// <summary>
132		/// Builds an member reference expression using this expression as target.
133		/// </summary>
134		public virtual MemberReferenceExpression Member(string memberName)
135		{
136			return new MemberReferenceExpression { Target = this, MemberName = memberName };
137		}
138		
139		/// <summary>
140		/// Builds an indexer expression using this expression as target.
141		/// </summary>
142		public virtual IndexerExpression Indexer(IEnumerable<Expression> arguments)
143		{
144			IndexerExpression expr = new IndexerExpression();
145			expr.Target = this;
146			expr.Arguments.AddRange(arguments);
147			return expr;
148		}
149		
150		/// <summary>
151		/// Builds an indexer expression using this expression as target.
152		/// </summary>
153		public virtual IndexerExpression Indexer(params Expression[] arguments)
154		{
155			IndexerExpression expr = new IndexerExpression();
156			expr.Target = this;
157			expr.Arguments.AddRange(arguments);
158			return expr;
159		}
160		
161		/// <summary>
162		/// Builds an invocation expression using this expression as target.
163		/// </summary>
164		public virtual InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments)
165		{
166			return Invoke(methodName, null, arguments);
167		}
168		
169		/// <summary>
170		/// Builds an invocation expression using this expression as target.
171		/// </summary>
172		public virtual InvocationExpression Invoke(string methodName, params Expression[] arguments)
173		{
174			return Invoke(methodName, null, arguments);
175		}
176		
177		/// <summary>
178		/// Builds an invocation expression using this expression as target.
179		/// </summary>
180		public virtual InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
181		{
182			InvocationExpression ie = new InvocationExpression();
183			MemberReferenceExpression mre = new MemberReferenceExpression();
184			mre.Target = this;
185			mre.MemberName = methodName;
186			mre.TypeArguments.AddRange(typeArguments);
187			ie.Target = mre;
188			ie.Arguments.AddRange(arguments);
189			return ie;
190		}
191		
192		/// <summary>
193		/// Builds an invocation expression using this expression as target.
194		/// </summary>
195		public virtual InvocationExpression Invoke(IEnumerable<Expression> arguments)
196		{
197			InvocationExpression ie = new InvocationExpression();
198			ie.Target = this;
199			ie.Arguments.AddRange(arguments);
200			return ie;
201		}
202		
203		/// <summary>
204		/// Builds an invocation expression using this expression as target.
205		/// </summary>
206		public virtual InvocationExpression Invoke(params Expression[] arguments)
207		{
208			InvocationExpression ie = new InvocationExpression();
209			ie.Target = this;
210			ie.Arguments.AddRange(arguments);
211			return ie;
212		}
213		
214		public virtual CastExpression CastTo(AstType type)
215		{
216			return new CastExpression { Type = type,  Expression = this };
217		}
218		
219		public virtual AsExpression CastAs(AstType type)
220		{
221			return new AsExpression { Type = type,  Expression = this };
222		}
223		
224		public virtual IsExpression IsType(AstType type)
225		{
226			return new IsExpression { Type = type,  Expression = this };
227		}
228		#endregion
229	}
230}