2
// OperatorDeclaration.cs
5
// Mike KrĆ¼ger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.ComponentModel;
29
using ICSharpCode.NRefactory.TypeSystem;
31
namespace ICSharpCode.NRefactory.CSharp
33
public enum OperatorType
35
// Values must correspond to Mono.CSharp.Operator.OpType
36
// due to the casts used in OperatorDeclaration.
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,
46
// Unary and Binary operators
47
Addition = Mono.CSharp.Operator.OpType.Addition,
48
Subtraction = Mono.CSharp.Operator.OpType.Subtraction,
50
UnaryPlus = Mono.CSharp.Operator.OpType.UnaryPlus,
51
UnaryNegation = Mono.CSharp.Operator.OpType.UnaryNegation,
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,
69
// Implicit and Explicit
70
Implicit = Mono.CSharp.Operator.OpType.Implicit,
71
Explicit = Mono.CSharp.Operator.OpType.Explicit
74
public class OperatorDeclaration : EntityDeclaration
76
public static readonly TokenRole OperatorKeywordRole = new TokenRole ("operator");
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");
86
// Unary and Binary operators
87
public static readonly TokenRole AdditionRole = new TokenRole ("+");
88
public static readonly TokenRole SubtractionRole = new TokenRole ("-");
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 ("<=");
106
public static readonly TokenRole ExplicitRole = new TokenRole ("explicit");
107
public static readonly TokenRole ImplicitRole = new TokenRole ("implicit");
109
public override EntityType EntityType {
110
get { return EntityType.Operator; }
113
OperatorType operatorType;
115
public OperatorType OperatorType {
116
get { return operatorType; }
119
operatorType = value;
123
public CSharpTokenNode OperatorToken {
124
get { return GetChildByRole (OperatorKeywordRole); }
127
public CSharpTokenNode OperatorTypeToken {
128
get { return GetChildByRole (GetRole (OperatorType)); }
131
public CSharpTokenNode LParToken {
132
get { return GetChildByRole (Roles.LPar); }
135
public AstNodeCollection<ParameterDeclaration> Parameters {
136
get { return GetChildrenByRole (Roles.Parameter); }
139
public CSharpTokenNode RParToken {
140
get { return GetChildByRole (Roles.RPar); }
143
public BlockStatement Body {
144
get { return GetChildByRole (Roles.Body); }
145
set { SetChildByRole (Roles.Body, value); }
149
/// Gets the operator type from the method name, or null, if the method does not represent one of the known operator types.
151
public static OperatorType? GetOperatorType(string methodName)
153
return (OperatorType?)Mono.CSharp.Operator.GetType(methodName);
156
public static TokenRole GetRole (OperatorType 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:
169
case OperatorType.False:
172
case OperatorType.Addition:
173
case OperatorType.UnaryPlus:
175
case OperatorType.Subtraction:
176
case OperatorType.UnaryNegation:
177
return SubtractionRole;
179
case OperatorType.Multiply:
181
case OperatorType.Division:
183
case OperatorType.Modulus:
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:
197
case OperatorType.Inequality:
198
return InequalityRole;
199
case OperatorType.GreaterThan:
200
return GreaterThanRole;
201
case OperatorType.LessThan:
203
case OperatorType.GreaterThanOrEqual:
204
return GreaterThanOrEqualRole;
205
case OperatorType.LessThanOrEqual:
206
return LessThanOrEqualRole;
208
case OperatorType.Implicit:
210
case OperatorType.Explicit:
214
throw new System.ArgumentOutOfRangeException ();
219
/// Gets the method name for the operator type. ("op_Addition", "op_Implicit", etc.)
221
public static string GetName (OperatorType type)
223
return Mono.CSharp.Operator.GetMetadataName ((Mono.CSharp.Operator.OpType)type);
227
/// Gets the token for the operator type ("+", "implicit", etc.)
229
public static string GetToken (OperatorType type)
231
return Mono.CSharp.Operator.GetName ((Mono.CSharp.Operator.OpType)type);
234
public override void AcceptVisitor (IAstVisitor visitor)
236
visitor.VisitOperatorDeclaration (this);
239
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
241
return visitor.VisitOperatorDeclaration (this);
244
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
246
return visitor.VisitOperatorDeclaration (this, data);
249
public override string Name {
250
get { return GetName (this.OperatorType); }
251
set { throw new NotSupportedException(); }
254
[EditorBrowsable(EditorBrowsableState.Never)]
255
public override Identifier NameToken {
256
get { return Identifier.Null; }
257
set { throw new NotSupportedException(); }
260
protected internal override bool DoMatch (AstNode other, PatternMatching.Match match)
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);