Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / System.ServiceModel / Mono.CodeGeneration / CodeBinaryComparison.cs
1 //
2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
9 // 
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
12 // 
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 // Copyright (C) Lluis Sanchez Gual, 2004
22 //
23
24 #if !MONOTOUCH
25 using System;
26 using System.Reflection;
27 using System.Reflection.Emit;
28
29 namespace Mono.CodeGeneration
30 {
31         public abstract class CodeBinaryComparison: CodeConditionExpression
32         {
33                 protected CodeExpression exp1;
34                 protected CodeExpression exp2;
35                 protected Type t1;
36                 protected Type t2;
37                 string symbol;
38                 
39                 public CodeBinaryComparison (CodeExpression exp1, CodeExpression exp2, string symbol)
40                 {
41                         this.symbol = symbol;
42                         this.exp1 = exp1;
43                         this.exp2 = exp2;
44                         
45                         t1 = exp1.GetResultType ();
46                         t2 = exp2.GetResultType ();
47                         
48                         if (!t1.IsPrimitive || !t2.IsPrimitive || (t1 != t2)) {
49                                 throw new InvalidOperationException ("Operator " + GetType().Name + " cannot be applied to operands of type '" + t1.Name + " and " + t2.Name);
50                         }
51                 }
52                 
53                 public override void PrintCode (CodeWriter cp)
54                 {
55                         exp1.PrintCode (cp);
56                         cp.Write (" " + symbol + " ");
57                         exp2.PrintCode (cp);
58                 }
59                 
60                 public override Type GetResultType ()
61                 {
62                         return typeof(bool);
63                 }
64         }
65         
66         public class CodeGreaterThan: CodeBinaryComparison
67         {
68                 public CodeGreaterThan (CodeExpression exp1, CodeExpression exp2)
69                 : base (exp1, exp2, ">")
70                 {
71                 }
72                 
73                 public override void Generate (ILGenerator gen)
74                 {
75                         exp1.Generate (gen);
76                         exp2.Generate (gen);
77                         gen.Emit (OpCodes.Cgt);
78                 }
79                 
80                 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
81                 {
82                         exp1.Generate (gen);
83                         exp2.Generate (gen);
84                         if (branchCase)
85                                 gen.Emit (OpCodes.Bgt, label);
86                         else
87                                 gen.Emit (OpCodes.Ble, label);
88                 }
89         }
90         
91         public class CodeGreaterEqualThan: CodeBinaryComparison
92         {
93                 public CodeGreaterEqualThan (CodeExpression exp1, CodeExpression exp2)
94                 : base (exp1, exp2, ">=")
95                 {
96                 }
97                 
98                 public override void Generate (ILGenerator gen)
99                 {
100                         exp1.Generate (gen);
101                         exp2.Generate (gen);
102                         gen.Emit (OpCodes.Clt);
103                         gen.Emit (OpCodes.Ldc_I4_0);
104                         gen.Emit (OpCodes.Ceq);
105                 }
106                 
107                 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
108                 {
109                         exp1.Generate (gen);
110                         exp2.Generate (gen);
111                         if (branchCase)
112                                 gen.Emit (OpCodes.Bge, label);
113                         else
114                                 gen.Emit (OpCodes.Blt, label);
115                 }
116         }
117         
118         public class CodeLessThan: CodeBinaryComparison
119         {
120                 public CodeLessThan (CodeExpression exp1, CodeExpression exp2)
121                 : base (exp1, exp2, "<")
122                 {
123                 }
124                 
125                 public override void Generate (ILGenerator gen)
126                 {
127                         exp1.Generate (gen);
128                         exp2.Generate (gen);
129                         gen.Emit (OpCodes.Clt);
130                 }
131                 
132                 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
133                 {
134                         exp1.Generate (gen);
135                         exp2.Generate (gen);
136                         if (branchCase)
137                                 gen.Emit (OpCodes.Blt, label);
138                         else
139                                 gen.Emit (OpCodes.Bge, label);
140                 }
141         }
142         
143         public class CodeLessEqualThan: CodeBinaryComparison
144         {
145                 public CodeLessEqualThan (CodeExpression exp1, CodeExpression exp2)
146                 : base (exp1, exp2, "<=")
147                 {
148                 }
149                 
150                 public override void Generate (ILGenerator gen)
151                 {
152                         exp1.Generate (gen);
153                         exp2.Generate (gen);
154                         gen.Emit (OpCodes.Cgt);
155                         gen.Emit (OpCodes.Ldc_I4_0);
156                         gen.Emit (OpCodes.Ceq);
157                 }
158                 
159                 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
160                 {
161                         exp1.Generate (gen);
162                         exp2.Generate (gen);
163                         if (branchCase)
164                                 gen.Emit (OpCodes.Ble, label);
165                         else
166                                 gen.Emit (OpCodes.Bgt, label);
167                 }
168         }
169 }
170 #endif