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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Ast/Expressions/UnaryOperatorExpression.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
 
// UnaryOperatorExpression.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.Linq.Expressions;
29
 
 
30
 
namespace ICSharpCode.NRefactory.CSharp
31
 
{
32
 
        /// <summary>
33
 
        /// Operator Expression
34
 
        /// </summary>
35
 
        public class UnaryOperatorExpression : Expression
36
 
        {
37
 
                public readonly static TokenRole NotRole = new TokenRole ("!");
38
 
                public readonly static TokenRole BitNotRole = new TokenRole ("~");
39
 
                public readonly static TokenRole MinusRole = new TokenRole ("-");
40
 
                public readonly static TokenRole PlusRole = new TokenRole ("+");
41
 
                public readonly static TokenRole IncrementRole = new TokenRole ("++");
42
 
                public readonly static TokenRole DecrementRole = new TokenRole ("--");
43
 
                public readonly static TokenRole DereferenceRole = new TokenRole ("*");
44
 
                public readonly static TokenRole AddressOfRole = new TokenRole ("&");
45
 
                public readonly static TokenRole AwaitRole = new TokenRole ("await");
46
 
                
47
 
                public UnaryOperatorExpression()
48
 
                {
49
 
                }
50
 
                
51
 
                public UnaryOperatorExpression(UnaryOperatorType op, Expression expression)
52
 
                {
53
 
                        this.Operator = op;
54
 
                        this.Expression = expression;
55
 
                }
56
 
                
57
 
                public UnaryOperatorType Operator {
58
 
                        get;
59
 
                        set;
60
 
                }
61
 
                
62
 
                public CSharpTokenNode OperatorToken {
63
 
                        get { return GetChildByRole (GetOperatorRole (Operator)); }
64
 
                }
65
 
                
66
 
                public Expression Expression {
67
 
                        get { return GetChildByRole (Roles.Expression); }
68
 
                        set { SetChildByRole (Roles.Expression, value); }
69
 
                }
70
 
                
71
 
                public override void AcceptVisitor (IAstVisitor visitor)
72
 
                {
73
 
                        visitor.VisitUnaryOperatorExpression (this);
74
 
                }
75
 
                        
76
 
                public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
77
 
                {
78
 
                        return visitor.VisitUnaryOperatorExpression (this);
79
 
                }
80
 
                
81
 
                public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
82
 
                {
83
 
                        return visitor.VisitUnaryOperatorExpression (this, data);
84
 
                }
85
 
                
86
 
                protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
87
 
                {
88
 
                        UnaryOperatorExpression o = other as UnaryOperatorExpression;
89
 
                        return o != null && (this.Operator == UnaryOperatorType.Any || this.Operator == o.Operator)
90
 
                                && this.Expression.DoMatch(o.Expression, match);
91
 
                }
92
 
                
93
 
                public static TokenRole GetOperatorRole(UnaryOperatorType op)
94
 
                {
95
 
                        switch (op) {
96
 
                                case UnaryOperatorType.Not:
97
 
                                        return NotRole;
98
 
                                case UnaryOperatorType.BitNot:
99
 
                                        return BitNotRole;
100
 
                                case UnaryOperatorType.Minus:
101
 
                                        return MinusRole;
102
 
                                case UnaryOperatorType.Plus:
103
 
                                        return PlusRole;
104
 
                                case UnaryOperatorType.Increment:
105
 
                                case UnaryOperatorType.PostIncrement:
106
 
                                        return IncrementRole;
107
 
                                case UnaryOperatorType.PostDecrement:
108
 
                                case UnaryOperatorType.Decrement:
109
 
                                        return DecrementRole;
110
 
                                case UnaryOperatorType.Dereference:
111
 
                                        return DereferenceRole;
112
 
                                case UnaryOperatorType.AddressOf:
113
 
                                        return AddressOfRole;
114
 
                                case UnaryOperatorType.Await:
115
 
                                        return AwaitRole;
116
 
                                default:
117
 
                                        throw new NotSupportedException("Invalid value for UnaryOperatorType");
118
 
                        }
119
 
                }
120
 
                
121
 
                public static ExpressionType GetLinqNodeType(UnaryOperatorType op, bool checkForOverflow)
122
 
                {
123
 
                        switch (op) {
124
 
                                case UnaryOperatorType.Not:
125
 
                                        return ExpressionType.Not;
126
 
                                case UnaryOperatorType.BitNot:
127
 
                                        return ExpressionType.OnesComplement;
128
 
                                case UnaryOperatorType.Minus:
129
 
                                        return checkForOverflow ? ExpressionType.NegateChecked : ExpressionType.Negate;
130
 
                                case UnaryOperatorType.Plus:
131
 
                                        return ExpressionType.UnaryPlus;
132
 
                                case UnaryOperatorType.Increment:
133
 
                                        return ExpressionType.PreIncrementAssign;
134
 
                                case UnaryOperatorType.Decrement:
135
 
                                        return ExpressionType.PreDecrementAssign;
136
 
                                case UnaryOperatorType.PostIncrement:
137
 
                                        return ExpressionType.PostIncrementAssign;
138
 
                                case UnaryOperatorType.PostDecrement:
139
 
                                        return ExpressionType.PostDecrementAssign;
140
 
                                case UnaryOperatorType.Dereference:
141
 
                                case UnaryOperatorType.AddressOf:
142
 
                                case UnaryOperatorType.Await:
143
 
                                        return ExpressionType.Extension;
144
 
                                default:
145
 
                                        throw new NotSupportedException("Invalid value for UnaryOperatorType");
146
 
                        }
147
 
                }
148
 
        }
149
 
        
150
 
        public enum UnaryOperatorType
151
 
        {
152
 
                /// <summary>
153
 
                /// Any unary operator (used in pattern matching)
154
 
                /// </summary>
155
 
                Any,
156
 
                
157
 
                /// <summary>Logical not (!a)</summary>
158
 
                Not,
159
 
                /// <summary>Bitwise not (~a)</summary>
160
 
                BitNot,
161
 
                /// <summary>Unary minus (-a)</summary>
162
 
                Minus,
163
 
                /// <summary>Unary plus (+a)</summary>
164
 
                Plus,
165
 
                /// <summary>Pre increment (++a)</summary>
166
 
                Increment,
167
 
                /// <summary>Pre decrement (--a)</summary>
168
 
                Decrement,
169
 
                /// <summary>Post increment (a++)</summary>
170
 
                PostIncrement,
171
 
                /// <summary>Post decrement (a--)</summary>
172
 
                PostDecrement,
173
 
                /// <summary>Dereferencing (*a)</summary>
174
 
                Dereference,
175
 
                /// <summary>Get address (&a)</summary>
176
 
                AddressOf,
177
 
                /// <summary>C# 5.0 await</summary>
178
 
                Await
179
 
        }
180
 
}