~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/Expression.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
 
 
22
namespace 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
                        }
 
47
                        
 
48
                        public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 
49
                        {
 
50
                                return default (T);
 
51
                        }
 
52
                        
 
53
                        public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
 
54
                        {
 
55
                                return default (S);
 
56
                        }
 
57
                        
 
58
                        protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 
59
                        {
 
60
                                return other == null || other.IsNull;
 
61
                        }
 
62
                }
 
63
                #endregion
 
64
                
 
65
                #region PatternPlaceholder
 
66
                public static implicit operator Expression(PatternMatching.Pattern pattern)
 
67
                {
 
68
                        return pattern != null ? new PatternPlaceholder(pattern) : null;
 
69
                }
 
70
                
 
71
                sealed class PatternPlaceholder : Expression, PatternMatching.INode
 
72
                {
 
73
                        readonly PatternMatching.Pattern child;
 
74
                        
 
75
                        public PatternPlaceholder(PatternMatching.Pattern child)
 
76
                        {
 
77
                                this.child = child;
 
78
                        }
 
79
                        
 
80
                        public override NodeType NodeType {
 
81
                                get { return NodeType.Pattern; }
 
82
                        }
 
83
                        
 
84
                        public override void AcceptVisitor (IAstVisitor visitor)
 
85
                        {
 
86
                                visitor.VisitPatternPlaceholder(this, child);
 
87
                        }
 
88
                                
 
89
                        public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 
90
                        {
 
91
                                return visitor.VisitPatternPlaceholder(this, child);
 
92
                        }
 
93
                        
 
94
                        public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
 
95
                        {
 
96
                                return visitor.VisitPatternPlaceholder(this, child, data);
 
97
                        }
 
98
                        
 
99
                        protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 
100
                        {
 
101
                                return child.DoMatch(other, match);
 
102
                        }
 
103
                        
 
104
                        bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
 
105
                        {
 
106
                                return child.DoMatchCollection(role, pos, match, backtrackingInfo);
 
107
                        }
 
108
                }
 
109
                #endregion
 
110
                
 
111
                public override NodeType NodeType {
 
112
                        get {
 
113
                                return NodeType.Expression;
 
114
                        }
 
115
                }
 
116
                
 
117
                public new Expression Clone()
 
118
                {
 
119
                        return (Expression)base.Clone();
 
120
                }
 
121
 
 
122
                public Expression ReplaceWith(Func<Expression, Expression> replaceFunction)
 
123
                {
 
124
                        if (replaceFunction == null)
 
125
                                throw new ArgumentNullException("replaceFunction");
 
126
                        return (Expression)base.ReplaceWith(node => replaceFunction((Expression)node));
 
127
                }
 
128
                
 
129
                #region Builder methods
 
130
                /// <summary>
 
131
                /// Builds an member reference expression using this expression as target.
 
132
                /// </summary>
 
133
                public MemberReferenceExpression Member(string memberName)
 
134
                {
 
135
                        return new MemberReferenceExpression { Target = this, MemberName = memberName };
 
136
                }
 
137
                
 
138
                /// <summary>
 
139
                /// Builds an indexer expression using this expression as target.
 
140
                /// </summary>
 
141
                public IndexerExpression Indexer(IEnumerable<Expression> arguments)
 
142
                {
 
143
                        IndexerExpression expr = new IndexerExpression();
 
144
                        expr.Target = this;
 
145
                        expr.Arguments.AddRange(arguments);
 
146
                        return expr;
 
147
                }
 
148
                
 
149
                /// <summary>
 
150
                /// Builds an indexer expression using this expression as target.
 
151
                /// </summary>
 
152
                public IndexerExpression Indexer(params Expression[] arguments)
 
153
                {
 
154
                        IndexerExpression expr = new IndexerExpression();
 
155
                        expr.Target = this;
 
156
                        expr.Arguments.AddRange(arguments);
 
157
                        return expr;
 
158
                }
 
159
                
 
160
                /// <summary>
 
161
                /// Builds an invocation expression using this expression as target.
 
162
                /// </summary>
 
163
                public InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments)
 
164
                {
 
165
                        return Invoke(methodName, null, arguments);
 
166
                }
 
167
                
 
168
                /// <summary>
 
169
                /// Builds an invocation expression using this expression as target.
 
170
                /// </summary>
 
171
                public InvocationExpression Invoke(string methodName, params Expression[] arguments)
 
172
                {
 
173
                        return Invoke(methodName, null, arguments);
 
174
                }
 
175
                
 
176
                /// <summary>
 
177
                /// Builds an invocation expression using this expression as target.
 
178
                /// </summary>
 
179
                public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
 
180
                {
 
181
                        InvocationExpression ie = new InvocationExpression();
 
182
                        MemberReferenceExpression mre = new MemberReferenceExpression();
 
183
                        mre.Target = this;
 
184
                        mre.MemberName = methodName;
 
185
                        mre.TypeArguments.AddRange(typeArguments);
 
186
                        ie.Target = mre;
 
187
                        ie.Arguments.AddRange(arguments);
 
188
                        return ie;
 
189
                }
 
190
                
 
191
                /// <summary>
 
192
                /// Builds an invocation expression using this expression as target.
 
193
                /// </summary>
 
194
                public InvocationExpression Invoke(IEnumerable<Expression> arguments)
 
195
                {
 
196
                        InvocationExpression ie = new InvocationExpression();
 
197
                        ie.Target = this;
 
198
                        ie.Arguments.AddRange(arguments);
 
199
                        return ie;
 
200
                }
 
201
                
 
202
                /// <summary>
 
203
                /// Builds an invocation expression using this expression as target.
 
204
                /// </summary>
 
205
                public InvocationExpression Invoke(params Expression[] arguments)
 
206
                {
 
207
                        InvocationExpression ie = new InvocationExpression();
 
208
                        ie.Target = this;
 
209
                        ie.Arguments.AddRange(arguments);
 
210
                        return ie;
 
211
                }
 
212
                
 
213
                public CastExpression CastTo(AstType type)
 
214
                {
 
215
                        return new CastExpression { Type = type,  Expression = this };
 
216
                }
 
217
                
 
218
                public AsExpression CastAs(AstType type)
 
219
                {
 
220
                        return new AsExpression { Type = type,  Expression = this };
 
221
                }
 
222
                
 
223
                public IsExpression IsType(AstType type)
 
224
                {
 
225
                        return new IsExpression { Type = type,  Expression = this };
 
226
                }
 
227
                #endregion
 
228
        }
 
229
}