Fix #77200.
[mono.git] / mcs / mcs / cfold.cs
1 //
2 // cfold.cs: Constant Folding
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2002, 2003 Ximian, Inc.
8 //
9
10 using System;
11
12 namespace Mono.CSharp {
13
14         public class ConstantFold {
15
16                 //
17                 // Performs the numeric promotions on the left and right expresions
18                 // and desposits the results on `lc' and `rc'.
19                 //
20                 // On success, the types of `lc' and `rc' on output will always match,
21                 // and the pair will be one of:
22                 //
23                 //   (double, double)
24                 //   (float, float)
25                 //   (ulong, ulong)
26                 //   (long, long)
27                 //   (uint, uint)
28                 //   (int, int)
29                 //   (short, short)   (Happens with enumerations with underlying short type)
30                 //   (ushort, ushort) (Happens with enumerations with underlying short type)
31                 //
32                 static void DoConstantNumericPromotions (EmitContext ec, Binary.Operator oper,
33                                                          ref Constant left, ref Constant right,
34                                                          Location loc)
35                 {
36                         if (left is DoubleConstant || right is DoubleConstant){
37                                 //
38                                 // If either side is a double, convert the other to a double
39                                 //
40                                 if (!(left is DoubleConstant))
41                                         left = left.ToDouble (loc);
42
43                                 if (!(right is DoubleConstant))
44                                         right = right.ToDouble (loc);
45                                 return;
46                         } else if (left is FloatConstant || right is FloatConstant) {
47                                 //
48                                 // If either side is a float, convert the other to a float
49                                 //
50                                 if (!(left is FloatConstant))
51                                         left = left.ToFloat (loc);
52
53                                 if (!(right is FloatConstant))
54                                         right = right.ToFloat (loc);
55 ;                               return;
56                         } else if (left is ULongConstant || right is ULongConstant){
57                                 //
58                                 // If either operand is of type ulong, the other operand is
59                                 // converted to type ulong.  or an error ocurrs if the other
60                                 // operand is of type sbyte, short, int or long
61                                 //
62 #if WRONG
63                                 Constant match, other;
64 #endif
65                                         
66                                 if (left is ULongConstant){
67 #if WRONG
68                                         other = right;
69                                         match = left;
70 #endif
71                                         if (!(right is ULongConstant))
72                                                 right = right.ToULong (loc);
73                                 } else {
74 #if WRONG
75                                         other = left;
76                                         match = right;
77 #endif
78                                         left = left.ToULong (loc);
79                                 }
80
81 #if WRONG
82                                 if (other is SByteConstant || other is ShortConstant ||
83                                     other is IntConstant || other is LongConstant){
84                                         Binary.Error_OperatorAmbiguous
85                                                 (loc, oper, other.Type, match.Type);
86                                         left = null;
87                                         right = null;
88                                 }
89 #endif
90                                 return;
91                         } else if (left is LongConstant || right is LongConstant){
92                                 //
93                                 // If either operand is of type long, the other operand is converted
94                                 // to type long.
95                                 //
96                                 if (!(left is LongConstant))
97                                         left = left.ToLong (loc);
98                                 else if (!(right is LongConstant))
99                                         right = right.ToLong (loc);
100                                 return;
101                         } else if (left is UIntConstant || right is UIntConstant){
102                                 //
103                                 // If either operand is of type uint, and the other
104                                 // operand is of type sbyte, short or int, the operands are
105                                 // converted to type long.
106                                 //
107                                 Constant other;
108                                 if (left is UIntConstant)
109                                         other = right;
110                                 else
111                                         other = left;
112
113                                 // Nothing to do.
114                                 if (other is UIntConstant)
115                                         return;
116
117                                 IntConstant ic = other as IntConstant;
118                                 if (ic != null){
119                                         if (ic.Value >= 0){
120                                                 if (left == other)
121                                                         left = new UIntConstant ((uint) ic.Value, ic.Location);
122                                                 else
123                                                         right = new UIntConstant ((uint) ic.Value, ic.Location);
124                                                 return;
125                                         }
126                                 }
127                                 
128                                 if (other is SByteConstant || other is ShortConstant || ic != null){
129                                         left = left.ToLong (loc);
130                                         right = right.ToLong (loc);
131                                 } else {
132                                         left = left.ToUInt (loc);
133                                         right = left.ToUInt (loc);
134                                 }
135
136                                 return;
137                         } else if (left is DecimalConstant || right is DecimalConstant) {
138                                 if (!(left is DecimalConstant))
139                                         left = left.ToDecimal (loc);
140                                 else if (!(right is DecimalConstant))
141                                         right = right.ToDecimal (loc);
142                                 return;
143                         } else if (left is EnumConstant || right is EnumConstant){
144                                 if (left is EnumConstant)
145                                         left = ((EnumConstant) left).Child;
146                                 if (right is EnumConstant)
147                                         right = ((EnumConstant) right).Child;
148
149                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
150                                 return;
151
152                         } else {
153                                 //
154                                 // Force conversions to int32
155                                 //
156                                 if (!(left is IntConstant))
157                                         left = left.ToInt (loc);
158                                 if (!(right is IntConstant))
159                                         right = right.ToInt (loc);
160                         }
161                         return;
162                 }
163
164                 static void Error_CompileTimeOverflow (Location loc)
165                 {
166                         Report.Error (220, loc, "The operation overflows at compile time in checked mode");
167                 }
168                 
169                 /// <summary>
170                 ///   Constant expression folder for binary operations.
171                 ///
172                 ///   Returns null if the expression can not be folded.
173                 /// </summary>
174                 static public Expression BinaryFold (EmitContext ec, Binary.Operator oper,
175                                                      Constant left, Constant right, Location loc)
176                 {
177                         if (left is NullCast)
178                                 return BinaryFold (ec, oper, ((NullCast)left).child, right, loc);
179
180                         if (right is NullCast)
181                                 return BinaryFold (ec, oper, left, ((NullCast)right).child, loc);
182
183                         Type lt = left.Type;
184                         Type rt = right.Type;
185                         Type result_type = null;
186                         bool bool_res;
187
188                         //
189                         // Enumerator folding
190                         //
191                         if (rt == lt && left is EnumConstant)
192                                 result_type = lt;
193
194                         //
195                         // During an enum evaluation, we need to unwrap enumerations
196                         //
197                         if (ec.InEnumContext){
198                                 if (left is EnumConstant)
199                                         left = ((EnumConstant) left).Child;
200                                 
201                                 if (right is EnumConstant)
202                                         right = ((EnumConstant) right).Child;
203                         }
204
205                         if (left is BoolConstant && right is BoolConstant) {
206                                 bool lv = ((BoolConstant) left ).Value;
207                                 bool rv = ((BoolConstant) right).Value;
208                                 switch (oper) {
209                                 case Binary.Operator.BitwiseAnd:
210                                 case Binary.Operator.LogicalAnd:
211                                         return new BoolConstant (lv && rv, left.Location);
212                                 case Binary.Operator.BitwiseOr:
213                                 case Binary.Operator.LogicalOr:
214                                         return new BoolConstant (lv || rv, left.Location);
215                                 case Binary.Operator.ExclusiveOr:
216                                         return new BoolConstant (lv ^ rv, left.Location);
217                                 default:
218                                         throw new InternalErrorException ("Invalid operator on booleans: " + oper);
219                                 }
220                         }
221
222                         Type wrap_as;
223                         Constant result = null;
224                         switch (oper){
225                         case Binary.Operator.BitwiseOr:
226                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
227                                 if (left == null || right == null)
228                                         return null;
229
230                                 if (left is IntConstant){
231                                         IntConstant v;
232                                         int res = ((IntConstant) left).Value | ((IntConstant) right).Value;
233                                         
234                                         v = new IntConstant (res, left.Location);
235                                         if (result_type == null)
236                                                 return v;
237                                         else
238                                                 return new EnumConstant (v, result_type);
239                                 } else if (left is UIntConstant){
240                                         UIntConstant v;
241                                         uint res = ((UIntConstant)left).Value | ((UIntConstant)right).Value;
242                                         
243                                         v = new UIntConstant (res, left.Location);
244                                         if (result_type == null)
245                                                 return v;
246                                         else
247                                                 return new EnumConstant (v, result_type);
248                                 } else if (left is LongConstant){
249                                         LongConstant v;
250                                         long res = ((LongConstant)left).Value | ((LongConstant)right).Value;
251                                         
252                                         v = new LongConstant (res, left.Location);
253                                         if (result_type == null)
254                                                 return v;
255                                         else
256                                                 return new EnumConstant (v, result_type);
257                                 } else if (left is ULongConstant){
258                                         ULongConstant v;
259                                         ulong res = ((ULongConstant)left).Value |
260                                                 ((ULongConstant)right).Value;
261                                         
262                                         v = new ULongConstant (res, left.Location);
263                                         if (result_type == null)
264                                                 return v;
265                                         else
266                                                 return new EnumConstant (v, result_type);
267                                 } else if (left is UShortConstant){
268                                         UShortConstant v;
269                                         ushort res = (ushort) (((UShortConstant)left).Value |
270                                                                ((UShortConstant)right).Value);
271                                         
272                                         v = new UShortConstant (res, left.Location);
273                                         if (result_type == null)
274                                                 return v;
275                                         else
276                                                 return new EnumConstant (v, result_type);
277                                 } else if (left is ShortConstant){
278                                         ShortConstant v;
279                                         short res = (short) (((ShortConstant)left).Value |
280                                                              ((ShortConstant)right).Value);
281                                         
282                                         v = new ShortConstant (res, left.Location);
283                                         if (result_type == null)
284                                                 return v;
285                                         else
286                                                 return new EnumConstant (v, result_type);
287                                 }
288                                 break;
289                                 
290                         case Binary.Operator.BitwiseAnd:
291                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
292                                 if (left == null || right == null)
293                                         return null;
294                                 
295                                 if (left is IntConstant){
296                                         IntConstant v;
297                                         int res = ((IntConstant) left).Value & ((IntConstant) right).Value;
298                                         
299                                         v = new IntConstant (res, left.Location);
300                                         if (result_type == null)
301                                                 return v;
302                                         else
303                                                 return new EnumConstant (v, result_type);
304                                 } else if (left is UIntConstant){
305                                         UIntConstant v;
306                                         uint res = ((UIntConstant)left).Value & ((UIntConstant)right).Value;
307                                         
308                                         v = new UIntConstant (res, left.Location);
309                                         if (result_type == null)
310                                                 return v;
311                                         else
312                                                 return new EnumConstant (v, result_type);
313                                 } else if (left is LongConstant){
314                                         LongConstant v;
315                                         long res = ((LongConstant)left).Value & ((LongConstant)right).Value;
316                                         
317                                         v = new LongConstant (res, left.Location);
318                                         if (result_type == null)
319                                                 return v;
320                                         else
321                                                 return new EnumConstant (v, result_type);
322                                 } else if (left is ULongConstant){
323                                         ULongConstant v;
324                                         ulong res = ((ULongConstant)left).Value &
325                                                 ((ULongConstant)right).Value;
326                                         
327                                         v = new ULongConstant (res, left.Location);
328                                         if (result_type == null)
329                                                 return v;
330                                         else
331                                                 return new EnumConstant (v, result_type);
332                                 } else if (left is UShortConstant){
333                                         UShortConstant v;
334                                         ushort res = (ushort) (((UShortConstant)left).Value &
335                                                                ((UShortConstant)right).Value);
336                                         
337                                         v = new UShortConstant (res, left.Location);
338                                         if (result_type == null)
339                                                 return v;
340                                         else
341                                                 return new EnumConstant (v, result_type);
342                                 } else if (left is ShortConstant){
343                                         ShortConstant v;
344                                         short res = (short) (((ShortConstant)left).Value &
345                                                              ((ShortConstant)right).Value);
346                                         
347                                         v = new ShortConstant (res, left.Location);
348                                         if (result_type == null)
349                                                 return v;
350                                         else
351                                                 return new EnumConstant (v, result_type);
352                                 }
353                                 break;
354
355                         case Binary.Operator.ExclusiveOr:
356                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
357                                 if (left == null || right == null)
358                                         return null;
359                                 
360                                 if (left is IntConstant){
361                                         IntConstant v;
362                                         int res = ((IntConstant) left).Value ^ ((IntConstant) right).Value;
363                                         
364                                         v = new IntConstant (res, left.Location);
365                                         if (result_type == null)
366                                                 return v;
367                                         else
368                                                 return new EnumConstant (v, result_type);
369                                 } else if (left is UIntConstant){
370                                         UIntConstant v;
371                                         uint res = ((UIntConstant)left).Value ^ ((UIntConstant)right).Value;
372                                         
373                                         v = new UIntConstant (res, left.Location);
374                                         if (result_type == null)
375                                                 return v;
376                                         else
377                                                 return new EnumConstant (v, result_type);
378                                 } else if (left is LongConstant){
379                                         LongConstant v;
380                                         long res = ((LongConstant)left).Value ^ ((LongConstant)right).Value;
381                                         
382                                         v = new LongConstant (res, left.Location);
383                                         if (result_type == null)
384                                                 return v;
385                                         else
386                                                 return new EnumConstant (v, result_type);
387                                 } else if (left is ULongConstant){
388                                         ULongConstant v;
389                                         ulong res = ((ULongConstant)left).Value ^
390                                                 ((ULongConstant)right).Value;
391                                         
392                                         v = new ULongConstant (res, left.Location);
393                                         if (result_type == null)
394                                                 return v;
395                                         else
396                                                 return new EnumConstant (v, result_type);
397                                 } else if (left is UShortConstant){
398                                         UShortConstant v;
399                                         ushort res = (ushort) (((UShortConstant)left).Value ^
400                                                                ((UShortConstant)right).Value);
401                                         
402                                         v = new UShortConstant (res, left.Location);
403                                         if (result_type == null)
404                                                 return v;
405                                         else
406                                                 return new EnumConstant (v, result_type);
407                                 } else if (left is ShortConstant){
408                                         ShortConstant v;
409                                         short res = (short)(((ShortConstant)left).Value ^
410                                                             ((ShortConstant)right).Value);
411                                         
412                                         v = new ShortConstant (res, left.Location);
413                                         if (result_type == null)
414                                                 return v;
415                                         else
416                                                 return new EnumConstant (v, result_type);
417                                 }
418                                 break;
419
420                         case Binary.Operator.Addition:
421                                 bool left_is_string = left is StringConstant;
422                                 bool right_is_string = right is StringConstant;
423
424                                 //
425                                 // If both sides are strings, then concatenate, if
426                                 // one is a string, and the other is not, then defer
427                                 // to runtime concatenation
428                                 //
429                                 wrap_as = null;
430                                 if (left_is_string || right_is_string){
431                                         if (left_is_string && right_is_string)
432                                                 return new StringConstant (
433                                                         ((StringConstant) left).Value +
434                                                         ((StringConstant) right).Value, left.Location);
435                                         
436                                         return null;
437                                 }
438
439                                 //
440                                 // handle "E operator + (E x, U y)"
441                                 // handle "E operator + (Y y, E x)"
442                                 //
443                                 // note that E operator + (E x, E y) is invalid
444                                 //
445                                 if (left is EnumConstant){
446                                         if (right is EnumConstant){
447                                                 return null;
448                                         }
449
450                                         right = right.ToType (((EnumConstant) left).Child.Type, loc);
451                                         if (right == null)
452                                                 return null;
453
454                                         wrap_as = left.Type;
455                                 } else if (right is EnumConstant){
456                                         left = left.ToType (((EnumConstant) right).Child.Type, loc);
457                                         if (left == null)
458                                                 return null;
459
460                                         wrap_as = right.Type;
461                                 }
462
463                                 result = null;
464                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
465                                 if (left == null || right == null)
466                                         return null;
467
468                                 try {
469                                         if (left is DoubleConstant){
470                                                 double res;
471                                                 
472                                                 if (ec.ConstantCheckState)
473                                                         res = checked (((DoubleConstant) left).Value +
474                                                                        ((DoubleConstant) right).Value);
475                                                 else
476                                                         res = unchecked (((DoubleConstant) left).Value +
477                                                                          ((DoubleConstant) right).Value);
478                                                 
479                                                 result = new DoubleConstant (res, left.Location);
480                                         } else if (left is FloatConstant){
481                                                 float res;
482                                                 
483                                                 if (ec.ConstantCheckState)
484                                                         res = checked (((FloatConstant) left).Value +
485                                                                        ((FloatConstant) right).Value);
486                                                 else
487                                                         res = unchecked (((FloatConstant) left).Value +
488                                                                          ((FloatConstant) right).Value);
489                                                 
490                                                 result = new FloatConstant (res, left.Location);
491                                         } else if (left is ULongConstant){
492                                                 ulong res;
493                                                 
494                                                 if (ec.ConstantCheckState)
495                                                         res = checked (((ULongConstant) left).Value +
496                                                                        ((ULongConstant) right).Value);
497                                                 else
498                                                         res = unchecked (((ULongConstant) left).Value +
499                                                                          ((ULongConstant) right).Value);
500
501                                                 result = new ULongConstant (res, left.Location);
502                                         } else if (left is LongConstant){
503                                                 long res;
504                                                 
505                                                 if (ec.ConstantCheckState)
506                                                         res = checked (((LongConstant) left).Value +
507                                                                        ((LongConstant) right).Value);
508                                                 else
509                                                         res = unchecked (((LongConstant) left).Value +
510                                                                          ((LongConstant) right).Value);
511                                                 
512                                                 result = new LongConstant (res, left.Location);
513                                         } else if (left is UIntConstant){
514                                                 uint res;
515                                                 
516                                                 if (ec.ConstantCheckState)
517                                                         res = checked (((UIntConstant) left).Value +
518                                                                        ((UIntConstant) right).Value);
519                                                 else
520                                                         res = unchecked (((UIntConstant) left).Value +
521                                                                          ((UIntConstant) right).Value);
522                                                 
523                                                 result = new UIntConstant (res, left.Location);
524                                         } else if (left is IntConstant){
525                                                 int res;
526
527                                                 if (ec.ConstantCheckState)
528                                                         res = checked (((IntConstant) left).Value +
529                                                                        ((IntConstant) right).Value);
530                                                 else
531                                                         res = unchecked (((IntConstant) left).Value +
532                                                                          ((IntConstant) right).Value);
533
534                                                 result = new IntConstant (res, left.Location);
535                                         } else if (left is DecimalConstant) {
536                                                 decimal res;
537
538                                                 if (ec.ConstantCheckState)
539                                                         res = checked (((DecimalConstant) left).Value +
540                                                                 ((DecimalConstant) right).Value);
541                                                 else
542                                                         res = unchecked (((DecimalConstant) left).Value +
543                                                                 ((DecimalConstant) right).Value);
544
545                                                 result = new DecimalConstant (res, left.Location);
546                                         } else {
547                                                 throw new Exception ( "Unexepected addition input: " + left);
548                                         }
549                                 } catch (OverflowException){
550                                         Error_CompileTimeOverflow (loc);
551                                 }
552
553                                 if (wrap_as != null)
554                                         return result.TryReduce (ec, wrap_as, loc);
555                                 else
556                                         return result;
557
558                         case Binary.Operator.Subtraction:
559                                 //
560                                 // handle "E operator - (E x, U y)"
561                                 // handle "E operator - (Y y, E x)"
562                                 // handle "U operator - (E x, E y)"
563                                 //
564                                 wrap_as = null;
565                                 if (left is EnumConstant){
566                                         if (right is EnumConstant){
567                                                 if (left.Type != right.Type) {
568                                                         Binary.Error_OperatorCannotBeApplied (loc, "-", left.Type, right.Type);
569                                                         return null;
570                                                 }
571
572                                                 wrap_as = TypeManager.EnumToUnderlying (left.Type);
573                                                 right = ((EnumConstant) right).Child.ToType (wrap_as, loc);
574                                                 if (right == null)
575                                                         return null;
576
577                                                 left = ((EnumConstant) left).Child.ToType (wrap_as, loc);
578                                                 if (left == null)
579                                                         return null;
580                                         }
581                                         else {
582                                                 right = right.ToType (((EnumConstant) left).Child.Type, loc);
583                                                 if (right == null)
584                                                         return null;
585
586                                                 wrap_as = left.Type;
587                                         }
588                                 } else if (right is EnumConstant){
589                                         left = left.ToType (((EnumConstant) right).Child.Type, loc);
590                                         if (left == null)
591                                                 return null;
592
593                                         wrap_as = right.Type;
594                                 }
595
596                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
597                                 if (left == null || right == null)
598                                         return null;
599
600                                 try {
601                                         if (left is DoubleConstant){
602                                                 double res;
603                                                 
604                                                 if (ec.ConstantCheckState)
605                                                         res = checked (((DoubleConstant) left).Value -
606                                                                        ((DoubleConstant) right).Value);
607                                                 else
608                                                         res = unchecked (((DoubleConstant) left).Value -
609                                                                          ((DoubleConstant) right).Value);
610                                                 
611                                                 result = new DoubleConstant (res, left.Location);
612                                         } else if (left is FloatConstant){
613                                                 float res;
614                                                 
615                                                 if (ec.ConstantCheckState)
616                                                         res = checked (((FloatConstant) left).Value -
617                                                                        ((FloatConstant) right).Value);
618                                                 else
619                                                         res = unchecked (((FloatConstant) left).Value -
620                                                                          ((FloatConstant) right).Value);
621                                                 
622                                                 result = new FloatConstant (res, left.Location);
623                                         } else if (left is ULongConstant){
624                                                 ulong res;
625                                                 
626                                                 if (ec.ConstantCheckState)
627                                                         res = checked (((ULongConstant) left).Value -
628                                                                        ((ULongConstant) right).Value);
629                                                 else
630                                                         res = unchecked (((ULongConstant) left).Value -
631                                                                          ((ULongConstant) right).Value);
632                                                 
633                                                 result = new ULongConstant (res, left.Location);
634                                         } else if (left is LongConstant){
635                                                 long res;
636                                                 
637                                                 if (ec.ConstantCheckState)
638                                                         res = checked (((LongConstant) left).Value -
639                                                                        ((LongConstant) right).Value);
640                                                 else
641                                                         res = unchecked (((LongConstant) left).Value -
642                                                                          ((LongConstant) right).Value);
643                                                 
644                                                 result = new LongConstant (res, left.Location);
645                                         } else if (left is UIntConstant){
646                                                 uint res;
647                                                 
648                                                 if (ec.ConstantCheckState)
649                                                         res = checked (((UIntConstant) left).Value -
650                                                                        ((UIntConstant) right).Value);
651                                                 else
652                                                         res = unchecked (((UIntConstant) left).Value -
653                                                                          ((UIntConstant) right).Value);
654                                                 
655                                                 result = new UIntConstant (res, left.Location);
656                                         } else if (left is IntConstant){
657                                                 int res;
658
659                                                 if (ec.ConstantCheckState)
660                                                         res = checked (((IntConstant) left).Value -
661                                                                        ((IntConstant) right).Value);
662                                                 else
663                                                         res = unchecked (((IntConstant) left).Value -
664                                                                          ((IntConstant) right).Value);
665
666                                                 result = new IntConstant (res, left.Location);
667                                         } else if (left is DecimalConstant) {
668                                                 decimal res;
669
670                                                 if (ec.ConstantCheckState)
671                                                         res = checked (((DecimalConstant) left).Value -
672                                                                 ((DecimalConstant) right).Value);
673                                                 else
674                                                         res = unchecked (((DecimalConstant) left).Value -
675                                                                 ((DecimalConstant) right).Value);
676
677                                                 return new DecimalConstant (res, left.Location);
678                                         } else {
679                                                 throw new Exception ( "Unexepected subtraction input: " + left);
680                                         }
681                                 } catch (OverflowException){
682                                         Error_CompileTimeOverflow (loc);
683                                 }
684
685                                 if (wrap_as != null)
686                                         return result.TryReduce (ec, wrap_as, loc);
687
688                                 return result;
689                                 
690                         case Binary.Operator.Multiply:
691                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
692                                 if (left == null || right == null)
693                                         return null;
694
695                                 try {
696                                         if (left is DoubleConstant){
697                                                 double res;
698                                                 
699                                                 if (ec.ConstantCheckState)
700                                                         res = checked (((DoubleConstant) left).Value *
701                                                                 ((DoubleConstant) right).Value);
702                                                 else
703                                                         res = unchecked (((DoubleConstant) left).Value *
704                                                                 ((DoubleConstant) right).Value);
705                                                 
706                                                 return new DoubleConstant (res, left.Location);
707                                         } else if (left is FloatConstant){
708                                                 float res;
709                                                 
710                                                 if (ec.ConstantCheckState)
711                                                         res = checked (((FloatConstant) left).Value *
712                                                                 ((FloatConstant) right).Value);
713                                                 else
714                                                         res = unchecked (((FloatConstant) left).Value *
715                                                                 ((FloatConstant) right).Value);
716                                                 
717                                                 return new FloatConstant (res, left.Location);
718                                         } else if (left is ULongConstant){
719                                                 ulong res;
720                                                 
721                                                 if (ec.ConstantCheckState)
722                                                         res = checked (((ULongConstant) left).Value *
723                                                                 ((ULongConstant) right).Value);
724                                                 else
725                                                         res = unchecked (((ULongConstant) left).Value *
726                                                                 ((ULongConstant) right).Value);
727                                                 
728                                                 return new ULongConstant (res, left.Location);
729                                         } else if (left is LongConstant){
730                                                 long res;
731                                                 
732                                                 if (ec.ConstantCheckState)
733                                                         res = checked (((LongConstant) left).Value *
734                                                                 ((LongConstant) right).Value);
735                                                 else
736                                                         res = unchecked (((LongConstant) left).Value *
737                                                                 ((LongConstant) right).Value);
738                                                 
739                                                 return new LongConstant (res, left.Location);
740                                         } else if (left is UIntConstant){
741                                                 uint res;
742                                                 
743                                                 if (ec.ConstantCheckState)
744                                                         res = checked (((UIntConstant) left).Value *
745                                                                 ((UIntConstant) right).Value);
746                                                 else
747                                                         res = unchecked (((UIntConstant) left).Value *
748                                                                 ((UIntConstant) right).Value);
749                                                 
750                                                 return new UIntConstant (res, left.Location);
751                                         } else if (left is IntConstant){
752                                                 int res;
753
754                                                 if (ec.ConstantCheckState)
755                                                         res = checked (((IntConstant) left).Value *
756                                                                 ((IntConstant) right).Value);
757                                                 else
758                                                         res = unchecked (((IntConstant) left).Value *
759                                                                 ((IntConstant) right).Value);
760
761                                                 return new IntConstant (res, left.Location);
762                                         } else if (left is DecimalConstant) {
763                                                 decimal res;
764
765                                                 if (ec.ConstantCheckState)
766                                                         res = checked (((DecimalConstant) left).Value *
767                                                                 ((DecimalConstant) right).Value);
768                                                 else
769                                                         res = unchecked (((DecimalConstant) left).Value *
770                                                                 ((DecimalConstant) right).Value);
771
772                                                 return new DecimalConstant (res, left.Location);
773                                         } else {
774                                                 throw new Exception ( "Unexepected multiply input: " + left);
775                                         }
776                                 } catch (OverflowException){
777                                         Error_CompileTimeOverflow (loc);
778                                 }
779                                 break;
780
781                         case Binary.Operator.Division:
782                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
783                                 if (left == null || right == null)
784                                         return null;
785
786                                 try {
787                                         if (left is DoubleConstant){
788                                                 double res;
789                                                 
790                                                 if (ec.ConstantCheckState)
791                                                         res = checked (((DoubleConstant) left).Value /
792                                                                 ((DoubleConstant) right).Value);
793                                                 else
794                                                         res = unchecked (((DoubleConstant) left).Value /
795                                                                 ((DoubleConstant) right).Value);
796                                                 
797                                                 return new DoubleConstant (res, left.Location);
798                                         } else if (left is FloatConstant){
799                                                 float res;
800                                                 
801                                                 if (ec.ConstantCheckState)
802                                                         res = checked (((FloatConstant) left).Value /
803                                                                 ((FloatConstant) right).Value);
804                                                 else
805                                                         res = unchecked (((FloatConstant) left).Value /
806                                                                 ((FloatConstant) right).Value);
807                                                 
808                                                 return new FloatConstant (res, left.Location);
809                                         } else if (left is ULongConstant){
810                                                 ulong res;
811                                                 
812                                                 if (ec.ConstantCheckState)
813                                                         res = checked (((ULongConstant) left).Value /
814                                                                 ((ULongConstant) right).Value);
815                                                 else
816                                                         res = unchecked (((ULongConstant) left).Value /
817                                                                 ((ULongConstant) right).Value);
818                                                 
819                                                 return new ULongConstant (res, left.Location);
820                                         } else if (left is LongConstant){
821                                                 long res;
822                                                 
823                                                 if (ec.ConstantCheckState)
824                                                         res = checked (((LongConstant) left).Value /
825                                                                 ((LongConstant) right).Value);
826                                                 else
827                                                         res = unchecked (((LongConstant) left).Value /
828                                                                 ((LongConstant) right).Value);
829                                                 
830                                                 return new LongConstant (res, left.Location);
831                                         } else if (left is UIntConstant){
832                                                 uint res;
833                                                 
834                                                 if (ec.ConstantCheckState)
835                                                         res = checked (((UIntConstant) left).Value /
836                                                                 ((UIntConstant) right).Value);
837                                                 else
838                                                         res = unchecked (((UIntConstant) left).Value /
839                                                                 ((UIntConstant) right).Value);
840                                                 
841                                                 return new UIntConstant (res, left.Location);
842                                         } else if (left is IntConstant){
843                                                 int res;
844
845                                                 if (ec.ConstantCheckState)
846                                                         res = checked (((IntConstant) left).Value /
847                                                                 ((IntConstant) right).Value);
848                                                 else
849                                                         res = unchecked (((IntConstant) left).Value /
850                                                                 ((IntConstant) right).Value);
851
852                                                 return new IntConstant (res, left.Location);
853                                         } else if (left is DecimalConstant) {
854                                                 decimal res;
855
856                                                 if (ec.ConstantCheckState)
857                                                         res = checked (((DecimalConstant) left).Value /
858                                                                 ((DecimalConstant) right).Value);
859                                                 else
860                                                         res = unchecked (((DecimalConstant) left).Value /
861                                                                 ((DecimalConstant) right).Value);
862
863                                                 return new DecimalConstant (res, left.Location);
864                                         } else {
865                                                 throw new Exception ( "Unexepected division input: " + left);
866                                         }
867                                 } catch (OverflowException){
868                                         Error_CompileTimeOverflow (loc);
869
870                                 } catch (DivideByZeroException) {
871                                         Report.Error (020, loc, "Division by constant zero");
872                                 }
873                                 
874                                 break;
875                                 
876                         case Binary.Operator.Modulus:
877                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
878                                 if (left == null || right == null)
879                                         return null;
880
881                                 try {
882                                         if (left is DoubleConstant){
883                                                 double res;
884                                                 
885                                                 if (ec.ConstantCheckState)
886                                                         res = checked (((DoubleConstant) left).Value %
887                                                                        ((DoubleConstant) right).Value);
888                                                 else
889                                                         res = unchecked (((DoubleConstant) left).Value %
890                                                                          ((DoubleConstant) right).Value);
891                                                 
892                                                 return new DoubleConstant (res, left.Location);
893                                         } else if (left is FloatConstant){
894                                                 float res;
895                                                 
896                                                 if (ec.ConstantCheckState)
897                                                         res = checked (((FloatConstant) left).Value %
898                                                                        ((FloatConstant) right).Value);
899                                                 else
900                                                         res = unchecked (((FloatConstant) left).Value %
901                                                                          ((FloatConstant) right).Value);
902                                                 
903                                                 return new FloatConstant (res, left.Location);
904                                         } else if (left is ULongConstant){
905                                                 ulong res;
906                                                 
907                                                 if (ec.ConstantCheckState)
908                                                         res = checked (((ULongConstant) left).Value %
909                                                                        ((ULongConstant) right).Value);
910                                                 else
911                                                         res = unchecked (((ULongConstant) left).Value %
912                                                                          ((ULongConstant) right).Value);
913                                                 
914                                                 return new ULongConstant (res, left.Location);
915                                         } else if (left is LongConstant){
916                                                 long res;
917                                                 
918                                                 if (ec.ConstantCheckState)
919                                                         res = checked (((LongConstant) left).Value %
920                                                                        ((LongConstant) right).Value);
921                                                 else
922                                                         res = unchecked (((LongConstant) left).Value %
923                                                                          ((LongConstant) right).Value);
924                                                 
925                                                 return new LongConstant (res, left.Location);
926                                         } else if (left is UIntConstant){
927                                                 uint res;
928                                                 
929                                                 if (ec.ConstantCheckState)
930                                                         res = checked (((UIntConstant) left).Value %
931                                                                        ((UIntConstant) right).Value);
932                                                 else
933                                                         res = unchecked (((UIntConstant) left).Value %
934                                                                          ((UIntConstant) right).Value);
935                                                 
936                                                 return new UIntConstant (res, left.Location);
937                                         } else if (left is IntConstant){
938                                                 int res;
939
940                                                 if (ec.ConstantCheckState)
941                                                         res = checked (((IntConstant) left).Value %
942                                                                        ((IntConstant) right).Value);
943                                                 else
944                                                         res = unchecked (((IntConstant) left).Value %
945                                                                          ((IntConstant) right).Value);
946
947                                                 return new IntConstant (res, left.Location);
948                                         } else {
949                                                 throw new Exception ( "Unexepected modulus input: " + left);
950                                         }
951                                 } catch (DivideByZeroException){
952                                         Report.Error (020, loc, "Division by constant zero");
953                                 } catch (OverflowException){
954                                         Error_CompileTimeOverflow (loc);
955                                 }
956                                 break;
957
958                                 //
959                                 // There is no overflow checking on left shift
960                                 //
961                         case Binary.Operator.LeftShift:
962                                 IntConstant ic = right.ToInt (loc);
963                                 if (ic == null){
964                                         Binary.Error_OperatorCannotBeApplied (loc, "<<", lt, rt);
965                                         return null;
966                                 }
967                                 int lshift_val = ic.Value;
968
969                                 IntConstant lic;
970                                 if ((lic = left.ConvertToInt ()) != null)
971                                         return new IntConstant (lic.Value << lshift_val, left.Location);
972
973                                 UIntConstant luic;
974                                 if ((luic = left.ConvertToUInt ()) != null)
975                                         return new UIntConstant (luic.Value << lshift_val, left.Location);
976
977                                 LongConstant llc;
978                                 if ((llc = left.ConvertToLong ()) != null)
979                                         return new LongConstant (llc.Value << lshift_val, left.Location);
980
981                                 ULongConstant lulc;
982                                 if ((lulc = left.ConvertToULong ()) != null)
983                                         return new ULongConstant (lulc.Value << lshift_val, left.Location);
984
985                                 Binary.Error_OperatorCannotBeApplied (loc, "<<", lt, rt);
986                                 break;
987
988                                 //
989                                 // There is no overflow checking on right shift
990                                 //
991                         case Binary.Operator.RightShift:
992                                 IntConstant sic = right.ToInt (loc);
993                                 if (sic == null){
994                                         Binary.Error_OperatorCannotBeApplied (loc, ">>", lt, rt);
995                                         return null;
996                                 }
997                                 int rshift_val = sic.Value;
998
999                                 IntConstant ric;
1000                                 if ((ric = left.ConvertToInt ()) != null)
1001                                         return new IntConstant (ric.Value >> rshift_val, left.Location);
1002
1003                                 UIntConstant ruic;
1004                                 if ((ruic = left.ConvertToUInt ()) != null)
1005                                         return new UIntConstant (ruic.Value >> rshift_val, left.Location);
1006
1007                                 LongConstant rlc;
1008                                 if ((rlc = left.ConvertToLong ()) != null)
1009                                         return new LongConstant (rlc.Value >> rshift_val, left.Location);
1010
1011                                 ULongConstant rulc;
1012                                 if ((rulc = left.ConvertToULong ()) != null)
1013                                         return new ULongConstant (rulc.Value >> rshift_val, left.Location);
1014
1015                                 Binary.Error_OperatorCannotBeApplied (loc, ">>", lt, rt);
1016                                 break;
1017
1018                         case Binary.Operator.Equality:
1019                                 if (left is BoolConstant && right is BoolConstant){
1020                                         return new BoolConstant (
1021                                                 ((BoolConstant) left).Value ==
1022                                                 ((BoolConstant) right).Value, left.Location);
1023                                 
1024                                 }
1025                                 if (left is NullLiteral){
1026                                         if (right is NullLiteral)
1027                                                 return new BoolConstant (true, left.Location);
1028                                         else if (right is StringConstant)
1029                                                 return new BoolConstant (
1030                                                         ((StringConstant) right).Value == null, left.Location);
1031                                 } else if (right is NullLiteral){
1032                                         if (left is NullLiteral)
1033                                                 return new BoolConstant (true, left.Location);
1034                                         else if (left is StringConstant)
1035                                                 return new BoolConstant (
1036                                                         ((StringConstant) left).Value == null, left.Location);
1037                                 }
1038                                 if (left is StringConstant && right is StringConstant){
1039                                         return new BoolConstant (
1040                                                 ((StringConstant) left).Value ==
1041                                                 ((StringConstant) right).Value, left.Location);
1042                                         
1043                                 }
1044
1045                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1046                                 if (left == null || right == null)
1047                                         return null;
1048
1049                                 bool_res = false;
1050                                 if (left is DoubleConstant)
1051                                         bool_res = ((DoubleConstant) left).Value ==
1052                                                 ((DoubleConstant) right).Value;
1053                                 else if (left is FloatConstant)
1054                                         bool_res = ((FloatConstant) left).Value ==
1055                                                 ((FloatConstant) right).Value;
1056                                 else if (left is ULongConstant)
1057                                         bool_res = ((ULongConstant) left).Value ==
1058                                                 ((ULongConstant) right).Value;
1059                                 else if (left is LongConstant)
1060                                         bool_res = ((LongConstant) left).Value ==
1061                                                 ((LongConstant) right).Value;
1062                                 else if (left is UIntConstant)
1063                                         bool_res = ((UIntConstant) left).Value ==
1064                                                 ((UIntConstant) right).Value;
1065                                 else if (left is IntConstant)
1066                                         bool_res = ((IntConstant) left).Value ==
1067                                                 ((IntConstant) right).Value;
1068                                 else
1069                                         return null;
1070
1071                                 return new BoolConstant (bool_res, left.Location);
1072
1073                         case Binary.Operator.Inequality:
1074                                 if (left is BoolConstant && right is BoolConstant){
1075                                         return new BoolConstant (
1076                                                 ((BoolConstant) left).Value !=
1077                                                 ((BoolConstant) right).Value, left.Location);
1078                                 }
1079                                 if (left is NullLiteral){
1080                                         if (right is NullLiteral)
1081                                                 return new BoolConstant (false, left.Location);
1082                                         else if (right is StringConstant)
1083                                                 return new BoolConstant (
1084                                                         ((StringConstant) right).Value != null, left.Location);
1085                                 } else if (right is NullLiteral){
1086                                         if (left is NullLiteral)
1087                                                 return new BoolConstant (false, left.Location);
1088                                         else if (left is StringConstant)
1089                                                 return new BoolConstant (
1090                                                         ((StringConstant) left).Value != null, left.Location);
1091                                 }
1092                                 if (left is StringConstant && right is StringConstant){
1093                                         return new BoolConstant (
1094                                                 ((StringConstant) left).Value !=
1095                                                 ((StringConstant) right).Value, left.Location);
1096                                         
1097                                 }
1098                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1099                                 if (left == null || right == null)
1100                                         return null;
1101
1102                                 bool_res = false;
1103                                 if (left is DoubleConstant)
1104                                         bool_res = ((DoubleConstant) left).Value !=
1105                                                 ((DoubleConstant) right).Value;
1106                                 else if (left is FloatConstant)
1107                                         bool_res = ((FloatConstant) left).Value !=
1108                                                 ((FloatConstant) right).Value;
1109                                 else if (left is ULongConstant)
1110                                         bool_res = ((ULongConstant) left).Value !=
1111                                                 ((ULongConstant) right).Value;
1112                                 else if (left is LongConstant)
1113                                         bool_res = ((LongConstant) left).Value !=
1114                                                 ((LongConstant) right).Value;
1115                                 else if (left is UIntConstant)
1116                                         bool_res = ((UIntConstant) left).Value !=
1117                                                 ((UIntConstant) right).Value;
1118                                 else if (left is IntConstant)
1119                                         bool_res = ((IntConstant) left).Value !=
1120                                                 ((IntConstant) right).Value;
1121                                 else
1122                                         return null;
1123
1124                                 return new BoolConstant (bool_res, left.Location);
1125
1126                         case Binary.Operator.LessThan:
1127                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1128                                 if (left == null || right == null)
1129                                         return null;
1130
1131                                 bool_res = false;
1132                                 if (left is DoubleConstant)
1133                                         bool_res = ((DoubleConstant) left).Value <
1134                                                 ((DoubleConstant) right).Value;
1135                                 else if (left is FloatConstant)
1136                                         bool_res = ((FloatConstant) left).Value <
1137                                                 ((FloatConstant) right).Value;
1138                                 else if (left is ULongConstant)
1139                                         bool_res = ((ULongConstant) left).Value <
1140                                                 ((ULongConstant) right).Value;
1141                                 else if (left is LongConstant)
1142                                         bool_res = ((LongConstant) left).Value <
1143                                                 ((LongConstant) right).Value;
1144                                 else if (left is UIntConstant)
1145                                         bool_res = ((UIntConstant) left).Value <
1146                                                 ((UIntConstant) right).Value;
1147                                 else if (left is IntConstant)
1148                                         bool_res = ((IntConstant) left).Value <
1149                                                 ((IntConstant) right).Value;
1150                                 else
1151                                         return null;
1152
1153                                 return new BoolConstant (bool_res, left.Location);
1154                                 
1155                         case Binary.Operator.GreaterThan:
1156                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1157                                 if (left == null || right == null)
1158                                         return null;
1159
1160                                 bool_res = false;
1161                                 if (left is DoubleConstant)
1162                                         bool_res = ((DoubleConstant) left).Value >
1163                                                 ((DoubleConstant) right).Value;
1164                                 else if (left is FloatConstant)
1165                                         bool_res = ((FloatConstant) left).Value >
1166                                                 ((FloatConstant) right).Value;
1167                                 else if (left is ULongConstant)
1168                                         bool_res = ((ULongConstant) left).Value >
1169                                                 ((ULongConstant) right).Value;
1170                                 else if (left is LongConstant)
1171                                         bool_res = ((LongConstant) left).Value >
1172                                                 ((LongConstant) right).Value;
1173                                 else if (left is UIntConstant)
1174                                         bool_res = ((UIntConstant) left).Value >
1175                                                 ((UIntConstant) right).Value;
1176                                 else if (left is IntConstant)
1177                                         bool_res = ((IntConstant) left).Value >
1178                                                 ((IntConstant) right).Value;
1179                                 else
1180                                         return null;
1181
1182                                 return new BoolConstant (bool_res, left.Location);
1183
1184                         case Binary.Operator.GreaterThanOrEqual:
1185                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1186                                 if (left == null || right == null)
1187                                         return null;
1188
1189                                 bool_res = false;
1190                                 if (left is DoubleConstant)
1191                                         bool_res = ((DoubleConstant) left).Value >=
1192                                                 ((DoubleConstant) right).Value;
1193                                 else if (left is FloatConstant)
1194                                         bool_res = ((FloatConstant) left).Value >=
1195                                                 ((FloatConstant) right).Value;
1196                                 else if (left is ULongConstant)
1197                                         bool_res = ((ULongConstant) left).Value >=
1198                                                 ((ULongConstant) right).Value;
1199                                 else if (left is LongConstant)
1200                                         bool_res = ((LongConstant) left).Value >=
1201                                                 ((LongConstant) right).Value;
1202                                 else if (left is UIntConstant)
1203                                         bool_res = ((UIntConstant) left).Value >=
1204                                                 ((UIntConstant) right).Value;
1205                                 else if (left is IntConstant)
1206                                         bool_res = ((IntConstant) left).Value >=
1207                                                 ((IntConstant) right).Value;
1208                                 else
1209                                         return null;
1210
1211                                 return new BoolConstant (bool_res, left.Location);
1212
1213                         case Binary.Operator.LessThanOrEqual:
1214                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1215                                 if (left == null || right == null)
1216                                         return null;
1217
1218                                 bool_res = false;
1219                                 if (left is DoubleConstant)
1220                                         bool_res = ((DoubleConstant) left).Value <=
1221                                                 ((DoubleConstant) right).Value;
1222                                 else if (left is FloatConstant)
1223                                         bool_res = ((FloatConstant) left).Value <=
1224                                                 ((FloatConstant) right).Value;
1225                                 else if (left is ULongConstant)
1226                                         bool_res = ((ULongConstant) left).Value <=
1227                                                 ((ULongConstant) right).Value;
1228                                 else if (left is LongConstant)
1229                                         bool_res = ((LongConstant) left).Value <=
1230                                                 ((LongConstant) right).Value;
1231                                 else if (left is UIntConstant)
1232                                         bool_res = ((UIntConstant) left).Value <=
1233                                                 ((UIntConstant) right).Value;
1234                                 else if (left is IntConstant)
1235                                         bool_res = ((IntConstant) left).Value <=
1236                                                 ((IntConstant) right).Value;
1237                                 else
1238                                         return null;
1239
1240                                 return new BoolConstant (bool_res, left.Location);
1241                         }
1242                                         
1243                         return null;
1244                 }
1245         }
1246 }