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

« back to all changes in this revision

Viewing changes to contrib/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) 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
 
                // Make debugging easier by giving Expressions a ToString() implementation
123
 
                public override string ToString()
124
 
                {
125
 
                        return DebugToString();
126
 
                }
127
 
                
128
 
                public Expression ReplaceWith(Func<Expression, Expression> replaceFunction)
129
 
                {
130
 
                        if (replaceFunction == null)
131
 
                                throw new ArgumentNullException("replaceFunction");
132
 
                        return (Expression)base.ReplaceWith(node => replaceFunction((Expression)node));
133
 
                }
134
 
                
135
 
                #region Builder methods
136
 
                /// <summary>
137
 
                /// Builds an member reference expression using this expression as target.
138
 
                /// </summary>
139
 
                public MemberReferenceExpression Member(string memberName)
140
 
                {
141
 
                        return new MemberReferenceExpression { Target = this, MemberName = memberName };
142
 
                }
143
 
                
144
 
                /// <summary>
145
 
                /// Builds an indexer expression using this expression as target.
146
 
                /// </summary>
147
 
                public IndexerExpression Indexer(IEnumerable<Expression> arguments)
148
 
                {
149
 
                        IndexerExpression expr = new IndexerExpression();
150
 
                        expr.Target = this;
151
 
                        expr.Arguments.AddRange(arguments);
152
 
                        return expr;
153
 
                }
154
 
                
155
 
                /// <summary>
156
 
                /// Builds an indexer expression using this expression as target.
157
 
                /// </summary>
158
 
                public IndexerExpression Indexer(params Expression[] arguments)
159
 
                {
160
 
                        IndexerExpression expr = new IndexerExpression();
161
 
                        expr.Target = this;
162
 
                        expr.Arguments.AddRange(arguments);
163
 
                        return expr;
164
 
                }
165
 
                
166
 
                /// <summary>
167
 
                /// Builds an invocation expression using this expression as target.
168
 
                /// </summary>
169
 
                public InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments)
170
 
                {
171
 
                        return Invoke(methodName, null, arguments);
172
 
                }
173
 
                
174
 
                /// <summary>
175
 
                /// Builds an invocation expression using this expression as target.
176
 
                /// </summary>
177
 
                public InvocationExpression Invoke(string methodName, params Expression[] arguments)
178
 
                {
179
 
                        return Invoke(methodName, null, arguments);
180
 
                }
181
 
                
182
 
                /// <summary>
183
 
                /// Builds an invocation expression using this expression as target.
184
 
                /// </summary>
185
 
                public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
186
 
                {
187
 
                        InvocationExpression ie = new InvocationExpression();
188
 
                        MemberReferenceExpression mre = new MemberReferenceExpression();
189
 
                        mre.Target = this;
190
 
                        mre.MemberName = methodName;
191
 
                        mre.TypeArguments.AddRange(typeArguments);
192
 
                        ie.Target = mre;
193
 
                        ie.Arguments.AddRange(arguments);
194
 
                        return ie;
195
 
                }
196
 
                
197
 
                /// <summary>
198
 
                /// Builds an invocation expression using this expression as target.
199
 
                /// </summary>
200
 
                public InvocationExpression Invoke(IEnumerable<Expression> arguments)
201
 
                {
202
 
                        InvocationExpression ie = new InvocationExpression();
203
 
                        ie.Target = this;
204
 
                        ie.Arguments.AddRange(arguments);
205
 
                        return ie;
206
 
                }
207
 
                
208
 
                /// <summary>
209
 
                /// Builds an invocation expression using this expression as target.
210
 
                /// </summary>
211
 
                public InvocationExpression Invoke(params Expression[] arguments)
212
 
                {
213
 
                        InvocationExpression ie = new InvocationExpression();
214
 
                        ie.Target = this;
215
 
                        ie.Arguments.AddRange(arguments);
216
 
                        return ie;
217
 
                }
218
 
                
219
 
                public CastExpression CastTo(AstType type)
220
 
                {
221
 
                        return new CastExpression { Type = type,  Expression = this };
222
 
                }
223
 
                
224
 
                public AsExpression CastAs(AstType type)
225
 
                {
226
 
                        return new AsExpression { Type = type,  Expression = this };
227
 
                }
228
 
                
229
 
                public IsExpression IsType(AstType type)
230
 
                {
231
 
                        return new IsExpression { Type = type,  Expression = this };
232
 
                }
233
 
                #endregion
234
 
        }
235
 
}