1 | // |
---|
2 | // ConditionalExpression.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 | using System.Collections.Generic; |
---|
27 | |
---|
28 | namespace ICSharpCode.NRefactory.CSharp |
---|
29 | { |
---|
30 | /// <summary> |
---|
31 | /// Condition ? TrueExpression : FalseExpression |
---|
32 | /// </summary> |
---|
33 | public class ConditionalExpression : Expression |
---|
34 | { |
---|
35 | public readonly static Role<Expression> ConditionRole = Roles.Condition; |
---|
36 | public readonly static TokenRole QuestionMarkRole = new TokenRole("?"); |
---|
37 | public readonly static Role<Expression> TrueRole = new Role<Expression>("True", Expression.Null); |
---|
38 | public readonly static TokenRole ColonRole = Roles.Colon; |
---|
39 | public readonly static Role<Expression> FalseRole = new Role<Expression>("False", Expression.Null); |
---|
40 | |
---|
41 | public Expression Condition { |
---|
42 | get { return GetChildByRole(ConditionRole); } |
---|
43 | set { SetChildByRole(ConditionRole, value); } |
---|
44 | } |
---|
45 | |
---|
46 | public CSharpTokenNode QuestionMarkToken { |
---|
47 | get { return GetChildByRole (QuestionMarkRole); } |
---|
48 | } |
---|
49 | |
---|
50 | public Expression TrueExpression { |
---|
51 | get { return GetChildByRole(TrueRole); } |
---|
52 | set { SetChildByRole(TrueRole, value); } |
---|
53 | } |
---|
54 | |
---|
55 | public CSharpTokenNode ColonToken { |
---|
56 | get { return GetChildByRole (ColonRole); } |
---|
57 | } |
---|
58 | |
---|
59 | public Expression FalseExpression { |
---|
60 | get { return GetChildByRole(FalseRole); } |
---|
61 | set { SetChildByRole(FalseRole, value); } |
---|
62 | } |
---|
63 | |
---|
64 | public ConditionalExpression () |
---|
65 | { |
---|
66 | } |
---|
67 | |
---|
68 | public ConditionalExpression (Expression condition, Expression trueExpression, Expression falseExpression) |
---|
69 | { |
---|
70 | AddChild (condition, ConditionRole); |
---|
71 | AddChild (trueExpression, TrueRole); |
---|
72 | AddChild (falseExpression, FalseRole); |
---|
73 | } |
---|
74 | |
---|
75 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
76 | { |
---|
77 | visitor.VisitConditionalExpression (this); |
---|
78 | } |
---|
79 | |
---|
80 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
81 | { |
---|
82 | return visitor.VisitConditionalExpression (this); |
---|
83 | } |
---|
84 | |
---|
85 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
86 | { |
---|
87 | return visitor.VisitConditionalExpression (this, data); |
---|
88 | } |
---|
89 | |
---|
90 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
91 | { |
---|
92 | ConditionalExpression o = other as ConditionalExpression; |
---|
93 | return o != null && this.Condition.DoMatch(o.Condition, match) && this.TrueExpression.DoMatch(o.TrueExpression, match) && this.FalseExpression.DoMatch(o.FalseExpression, match); |
---|
94 | } |
---|
95 | |
---|
96 | #region Builder methods |
---|
97 | public override MemberReferenceExpression Member(string memberName) |
---|
98 | { |
---|
99 | return new MemberReferenceExpression { Target = this, MemberName = memberName }; |
---|
100 | } |
---|
101 | |
---|
102 | public override IndexerExpression Indexer(IEnumerable<Expression> arguments) |
---|
103 | { |
---|
104 | IndexerExpression expr = new IndexerExpression(); |
---|
105 | expr.Target = new ParenthesizedExpression(this); |
---|
106 | expr.Arguments.AddRange(arguments); |
---|
107 | return expr; |
---|
108 | } |
---|
109 | |
---|
110 | public override IndexerExpression Indexer(params Expression[] arguments) |
---|
111 | { |
---|
112 | IndexerExpression expr = new IndexerExpression(); |
---|
113 | expr.Target = new ParenthesizedExpression(this); |
---|
114 | expr.Arguments.AddRange(arguments); |
---|
115 | return expr; |
---|
116 | } |
---|
117 | |
---|
118 | public override InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments) |
---|
119 | { |
---|
120 | InvocationExpression ie = new InvocationExpression(); |
---|
121 | MemberReferenceExpression mre = new MemberReferenceExpression(); |
---|
122 | mre.Target = new ParenthesizedExpression(this); |
---|
123 | mre.MemberName = methodName; |
---|
124 | mre.TypeArguments.AddRange(typeArguments); |
---|
125 | ie.Target = mre; |
---|
126 | ie.Arguments.AddRange(arguments); |
---|
127 | return ie; |
---|
128 | } |
---|
129 | |
---|
130 | public override InvocationExpression Invoke(IEnumerable<Expression> arguments) |
---|
131 | { |
---|
132 | InvocationExpression ie = new InvocationExpression(); |
---|
133 | ie.Target = new ParenthesizedExpression(this); |
---|
134 | ie.Arguments.AddRange(arguments); |
---|
135 | return ie; |
---|
136 | } |
---|
137 | |
---|
138 | public override InvocationExpression Invoke(params Expression[] arguments) |
---|
139 | { |
---|
140 | InvocationExpression ie = new InvocationExpression(); |
---|
141 | ie.Target = new ParenthesizedExpression(this); |
---|
142 | ie.Arguments.AddRange(arguments); |
---|
143 | return ie; |
---|
144 | } |
---|
145 | |
---|
146 | public override CastExpression CastTo(AstType type) |
---|
147 | { |
---|
148 | return new CastExpression { Type = type, Expression = new ParenthesizedExpression(this) }; |
---|
149 | } |
---|
150 | |
---|
151 | public override AsExpression CastAs(AstType type) |
---|
152 | { |
---|
153 | return new AsExpression { Type = type, Expression = new ParenthesizedExpression(this) }; |
---|
154 | } |
---|
155 | |
---|
156 | public override IsExpression IsType(AstType type) |
---|
157 | { |
---|
158 | return new IsExpression { Type = type, Expression = new ParenthesizedExpression(this) }; |
---|
159 | } |
---|
160 | #endregion |
---|
161 | } |
---|
162 | } |
---|