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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Ast/TypeMembers/OperatorDeclaration.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
ļ»æ// 
 
2
// OperatorDeclaration.cs
 
3
//
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.ComponentModel;
 
29
using ICSharpCode.NRefactory.TypeSystem;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp
 
32
{
 
33
        public enum OperatorType
 
34
        {
 
35
                // Values must correspond to Mono.CSharp.Operator.OpType
 
36
                // due to the casts used in OperatorDeclaration.
 
37
                
 
38
                // Unary operators
 
39
                LogicalNot = Mono.CSharp.Operator.OpType.LogicalNot,
 
40
                OnesComplement = Mono.CSharp.Operator.OpType.OnesComplement,
 
41
                Increment = Mono.CSharp.Operator.OpType.Increment,
 
42
                Decrement = Mono.CSharp.Operator.OpType.Decrement,
 
43
                True = Mono.CSharp.Operator.OpType.True,
 
44
                False = Mono.CSharp.Operator.OpType.False,
 
45
 
 
46
                // Unary and Binary operators
 
47
                Addition = Mono.CSharp.Operator.OpType.Addition,
 
48
                Subtraction = Mono.CSharp.Operator.OpType.Subtraction,
 
49
 
 
50
                UnaryPlus = Mono.CSharp.Operator.OpType.UnaryPlus,
 
51
                UnaryNegation = Mono.CSharp.Operator.OpType.UnaryNegation,
 
52
                
 
53
                // Binary operators
 
54
                Multiply = Mono.CSharp.Operator.OpType.Multiply,
 
55
                Division = Mono.CSharp.Operator.OpType.Division,
 
56
                Modulus = Mono.CSharp.Operator.OpType.Modulus,
 
57
                BitwiseAnd = Mono.CSharp.Operator.OpType.BitwiseAnd,
 
58
                BitwiseOr = Mono.CSharp.Operator.OpType.BitwiseOr,
 
59
                ExclusiveOr = Mono.CSharp.Operator.OpType.ExclusiveOr,
 
60
                LeftShift = Mono.CSharp.Operator.OpType.LeftShift,
 
61
                RightShift = Mono.CSharp.Operator.OpType.RightShift,
 
62
                Equality = Mono.CSharp.Operator.OpType.Equality,
 
63
                Inequality = Mono.CSharp.Operator.OpType.Inequality,
 
64
                GreaterThan = Mono.CSharp.Operator.OpType.GreaterThan,
 
65
                LessThan = Mono.CSharp.Operator.OpType.LessThan,
 
66
                GreaterThanOrEqual = Mono.CSharp.Operator.OpType.GreaterThanOrEqual,
 
67
                LessThanOrEqual = Mono.CSharp.Operator.OpType.LessThanOrEqual,
 
68
 
 
69
                // Implicit and Explicit
 
70
                Implicit = Mono.CSharp.Operator.OpType.Implicit,
 
71
                Explicit = Mono.CSharp.Operator.OpType.Explicit
 
72
        }
 
73
        
 
74
        public class OperatorDeclaration : EntityDeclaration
 
75
        {
 
76
                public static readonly TokenRole OperatorKeywordRole = new TokenRole ("operator");
 
77
                
 
78
                // Unary operators
 
79
                public static readonly TokenRole LogicalNotRole = new TokenRole ("!");
 
80
                public static readonly TokenRole OnesComplementRole = new TokenRole ("~");
 
81
                public static readonly TokenRole IncrementRole = new TokenRole ("++");
 
82
                public static readonly TokenRole DecrementRole = new TokenRole ("--");
 
83
                public static readonly TokenRole TrueRole = new TokenRole ("true");
 
84
                public static readonly TokenRole FalseRole = new TokenRole ("false");
 
85
 
 
86
                // Unary and Binary operators
 
87
                public static readonly TokenRole AdditionRole = new TokenRole ("+");
 
88
                public static readonly TokenRole SubtractionRole = new TokenRole ("-");
 
89
 
 
90
                // Binary operators
 
91
                public static readonly TokenRole MultiplyRole = new TokenRole ("*");
 
92
                public static readonly TokenRole DivisionRole = new TokenRole ("/");
 
93
                public static readonly TokenRole ModulusRole = new TokenRole ("%");
 
94
                public static readonly TokenRole BitwiseAndRole = new TokenRole ("&");
 
95
                public static readonly TokenRole BitwiseOrRole = new TokenRole ("|");
 
96
                public static readonly TokenRole ExclusiveOrRole = new TokenRole ("^");
 
97
                public static readonly TokenRole LeftShiftRole = new TokenRole ("<<");
 
98
                public static readonly TokenRole RightShiftRole = new TokenRole (">>");
 
99
                public static readonly TokenRole EqualityRole = new TokenRole ("==");
 
100
                public static readonly TokenRole InequalityRole = new TokenRole ("!=");
 
101
                public static readonly TokenRole GreaterThanRole = new TokenRole (">");
 
102
                public static readonly TokenRole LessThanRole = new TokenRole ("<");
 
103
                public static readonly TokenRole GreaterThanOrEqualRole = new TokenRole (">=");
 
104
                public static readonly TokenRole LessThanOrEqualRole = new TokenRole ("<=");
 
105
                
 
106
                public static readonly TokenRole ExplicitRole = new TokenRole ("explicit");
 
107
                public static readonly TokenRole ImplicitRole = new TokenRole ("implicit");
 
108
                
 
109
                public override EntityType EntityType {
 
110
                        get { return EntityType.Operator; }
 
111
                }
 
112
                
 
113
                OperatorType operatorType;
 
114
                
 
115
                public OperatorType OperatorType {
 
116
                        get { return operatorType; }
 
117
                        set {
 
118
                                ThrowIfFrozen();
 
119
                                operatorType = value;
 
120
                        }
 
121
                }
 
122
                
 
123
                public CSharpTokenNode OperatorToken {
 
124
                        get { return GetChildByRole (OperatorKeywordRole); }
 
125
                }
 
126
                
 
127
                public CSharpTokenNode OperatorTypeToken {
 
128
                        get { return GetChildByRole (GetRole (OperatorType)); }
 
129
                }
 
130
                
 
131
                public CSharpTokenNode LParToken {
 
132
                        get { return GetChildByRole (Roles.LPar); }
 
133
                }
 
134
                
 
135
                public AstNodeCollection<ParameterDeclaration> Parameters {
 
136
                        get { return GetChildrenByRole (Roles.Parameter); }
 
137
                }
 
138
                
 
139
                public CSharpTokenNode RParToken {
 
140
                        get { return GetChildByRole (Roles.RPar); }
 
141
                }
 
142
                
 
143
                public BlockStatement Body {
 
144
                        get { return GetChildByRole (Roles.Body); }
 
145
                        set { SetChildByRole (Roles.Body, value); }
 
146
                }
 
147
                
 
148
                /// <summary>
 
149
                /// Gets the operator type from the method name, or null, if the method does not represent one of the known operator types.
 
150
                /// </summary>
 
151
                public static OperatorType? GetOperatorType(string methodName)
 
152
                {
 
153
                        return (OperatorType?)Mono.CSharp.Operator.GetType(methodName);
 
154
                }
 
155
                
 
156
                public static TokenRole GetRole (OperatorType type)
 
157
                {
 
158
                        switch (type) {
 
159
                        case OperatorType.LogicalNot:
 
160
                                return LogicalNotRole;
 
161
                        case OperatorType.OnesComplement:
 
162
                                return OnesComplementRole;
 
163
                        case OperatorType.Increment:
 
164
                                return IncrementRole;
 
165
                        case OperatorType.Decrement:
 
166
                                return DecrementRole;
 
167
                        case OperatorType.True:
 
168
                                return TrueRole;
 
169
                        case OperatorType.False:
 
170
                                return FalseRole;
 
171
                        
 
172
                        case OperatorType.Addition:
 
173
                        case OperatorType.UnaryPlus:
 
174
                                return AdditionRole;
 
175
                        case OperatorType.Subtraction:
 
176
                        case OperatorType.UnaryNegation:
 
177
                                return SubtractionRole;
 
178
                        
 
179
                        case OperatorType.Multiply:
 
180
                                return MultiplyRole;
 
181
                        case OperatorType.Division:
 
182
                                return DivisionRole;
 
183
                        case OperatorType.Modulus:
 
184
                                return ModulusRole;
 
185
                        case OperatorType.BitwiseAnd:
 
186
                                return BitwiseAndRole;
 
187
                        case OperatorType.BitwiseOr:
 
188
                                return BitwiseOrRole;
 
189
                        case OperatorType.ExclusiveOr:
 
190
                                return ExclusiveOrRole;
 
191
                        case OperatorType.LeftShift:
 
192
                                return LeftShiftRole;
 
193
                        case OperatorType.RightShift:
 
194
                                return RightShiftRole;
 
195
                        case OperatorType.Equality:
 
196
                                return EqualityRole;
 
197
                        case OperatorType.Inequality:
 
198
                                return InequalityRole;
 
199
                        case OperatorType.GreaterThan:
 
200
                                return GreaterThanRole;
 
201
                        case OperatorType.LessThan:
 
202
                                return LessThanRole;
 
203
                        case OperatorType.GreaterThanOrEqual:
 
204
                                return GreaterThanOrEqualRole;
 
205
                        case OperatorType.LessThanOrEqual:
 
206
                                return LessThanOrEqualRole;
 
207
                        
 
208
                        case OperatorType.Implicit:
 
209
                                return ImplicitRole;
 
210
                        case OperatorType.Explicit:
 
211
                                return ExplicitRole;
 
212
                        
 
213
                        default:
 
214
                                throw new System.ArgumentOutOfRangeException ();
 
215
                        }
 
216
                }
 
217
                
 
218
                /// <summary>
 
219
                /// Gets the method name for the operator type. ("op_Addition", "op_Implicit", etc.)
 
220
                /// </summary>
 
221
                public static string GetName (OperatorType type)
 
222
                {
 
223
                        return Mono.CSharp.Operator.GetMetadataName ((Mono.CSharp.Operator.OpType)type);
 
224
                }
 
225
                
 
226
                /// <summary>
 
227
                /// Gets the token for the operator type ("+", "implicit", etc.)
 
228
                /// </summary>
 
229
                public static string GetToken (OperatorType type)
 
230
                {
 
231
                        return Mono.CSharp.Operator.GetName ((Mono.CSharp.Operator.OpType)type);
 
232
                }
 
233
                
 
234
                public override void AcceptVisitor (IAstVisitor visitor)
 
235
                {
 
236
                        visitor.VisitOperatorDeclaration (this);
 
237
                }
 
238
                        
 
239
                public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 
240
                {
 
241
                        return visitor.VisitOperatorDeclaration (this);
 
242
                }
 
243
                
 
244
                public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
 
245
                {
 
246
                        return visitor.VisitOperatorDeclaration (this, data);
 
247
                }
 
248
                
 
249
                public override string Name {
 
250
                        get { return GetName (this.OperatorType); }
 
251
                        set { throw new NotSupportedException(); }
 
252
                }
 
253
                
 
254
                [EditorBrowsable(EditorBrowsableState.Never)]
 
255
                public override Identifier NameToken {
 
256
                        get { return Identifier.Null; }
 
257
                        set { throw new NotSupportedException(); }
 
258
                }
 
259
                
 
260
                protected internal override bool DoMatch (AstNode other, PatternMatching.Match match)
 
261
                {
 
262
                        OperatorDeclaration o = other as OperatorDeclaration;
 
263
                        return o != null && this.MatchAttributesAndModifiers (o, match) && this.OperatorType == o.OperatorType
 
264
                                && this.ReturnType.DoMatch (o.ReturnType, match)
 
265
                                && this.Parameters.DoMatch (o.Parameters, match) && this.Body.DoMatch (o.Body, match);
 
266
                }
 
267
        }
 
268
}