2005-07-14 Sureshkumar T <tsureshkumar@novell.com>
[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);
122                                                 else
123                                                         right = new UIntConstant ((uint) ic.Value);
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                                 //
145                                 // If either operand is an enum constant, the other one must
146                                 // be implicitly convertable to that enum's underlying type.
147                                 //
148                                 EnumConstant match;
149                                 Constant other;
150                                 if (left is EnumConstant){
151                                         other = right;
152                                         match = (EnumConstant) left;
153                                 } else {
154                                         other = left;
155                                         match = (EnumConstant) right;
156                                 }
157
158                                 bool need_check = (other is EnumConstant) ||
159                                         !(oper == Binary.Operator.Addition || 
160                                           oper == Binary.Operator.Subtraction ||
161                                           (other.IsZeroInteger && other is IntConstant));
162
163                                 if (need_check &&
164                                     !Convert.ImplicitConversionExists (ec, match, other.Type)) {
165                                         Convert.Error_CannotImplicitConversion (loc, match.Type, other.Type);
166                                         left = null;
167                                         right = null;
168                                         return;
169                                 }
170
171                                 if (left is EnumConstant)
172                                         left = ((EnumConstant) left).Child;
173                                 if (right is EnumConstant)
174                                         right = ((EnumConstant) right).Child;
175
176                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
177                                 return;
178
179                         } else {
180                                 //
181                                 // Force conversions to int32
182                                 //
183                                 if (!(left is IntConstant))
184                                         left = left.ToInt (loc);
185                                 if (!(right is IntConstant))
186                                         right = right.ToInt (loc);
187                         }
188                         return;
189                 }
190
191                 static void Error_CompileTimeOverflow (Location loc)
192                 {
193                         Report.Error (220, loc, "The operation overflows at compile time in checked mode");
194                 }
195                 
196                 /// <summary>
197                 ///   Constant expression folder for binary operations.
198                 ///
199                 ///   Returns null if the expression can not be folded.
200                 /// </summary>
201                 static public Expression BinaryFold (EmitContext ec, Binary.Operator oper,
202                                                      Constant left, Constant right, Location loc)
203                 {
204                         Type lt = left.Type;
205                         Type rt = right.Type;
206                         Type result_type = null;
207                         bool bool_res;
208
209                         //
210                         // Enumerator folding
211                         //
212                         if (rt == lt && left is EnumConstant)
213                                 result_type = lt;
214
215                         //
216                         // During an enum evaluation, we need to unwrap enumerations
217                         //
218                         if (ec.InEnumContext){
219                                 if (left is EnumConstant)
220                                         left = ((EnumConstant) left).Child;
221                                 
222                                 if (right is EnumConstant)
223                                         right = ((EnumConstant) right).Child;
224                         }
225
226                         Type wrap_as;
227                         Constant result = null;
228                         switch (oper){
229                         case Binary.Operator.BitwiseOr:
230                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
231                                 if (left == null || right == null)
232                                         return null;
233
234                                 if (left is IntConstant){
235                                         IntConstant v;
236                                         int res = ((IntConstant) left).Value | ((IntConstant) right).Value;
237                                         
238                                         v = new IntConstant (res);
239                                         if (result_type == null)
240                                                 return v;
241                                         else
242                                                 return new EnumConstant (v, result_type);
243                                 } else if (left is UIntConstant){
244                                         UIntConstant v;
245                                         uint res = ((UIntConstant)left).Value | ((UIntConstant)right).Value;
246                                         
247                                         v = new UIntConstant (res);
248                                         if (result_type == null)
249                                                 return v;
250                                         else
251                                                 return new EnumConstant (v, result_type);
252                                 } else if (left is LongConstant){
253                                         LongConstant v;
254                                         long res = ((LongConstant)left).Value | ((LongConstant)right).Value;
255                                         
256                                         v = new LongConstant (res);
257                                         if (result_type == null)
258                                                 return v;
259                                         else
260                                                 return new EnumConstant (v, result_type);
261                                 } else if (left is ULongConstant){
262                                         ULongConstant v;
263                                         ulong res = ((ULongConstant)left).Value |
264                                                 ((ULongConstant)right).Value;
265                                         
266                                         v = new ULongConstant (res);
267                                         if (result_type == null)
268                                                 return v;
269                                         else
270                                                 return new EnumConstant (v, result_type);
271                                 } else if (left is UShortConstant){
272                                         UShortConstant v;
273                                         ushort res = (ushort) (((UShortConstant)left).Value |
274                                                                ((UShortConstant)right).Value);
275                                         
276                                         v = new UShortConstant (res);
277                                         if (result_type == null)
278                                                 return v;
279                                         else
280                                                 return new EnumConstant (v, result_type);
281                                 } else if (left is ShortConstant){
282                                         ShortConstant v;
283                                         short res = (short) (((ShortConstant)left).Value |
284                                                              ((ShortConstant)right).Value);
285                                         
286                                         v = new ShortConstant (res);
287                                         if (result_type == null)
288                                                 return v;
289                                         else
290                                                 return new EnumConstant (v, result_type);
291                                 }
292                                 break;
293                                 
294                         case Binary.Operator.BitwiseAnd:
295                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
296                                 if (left == null || right == null)
297                                         return null;
298                                 
299                                 if (left is IntConstant){
300                                         IntConstant v;
301                                         int res = ((IntConstant) left).Value & ((IntConstant) right).Value;
302                                         
303                                         v = new IntConstant (res);
304                                         if (result_type == null)
305                                                 return v;
306                                         else
307                                                 return new EnumConstant (v, result_type);
308                                 } else if (left is UIntConstant){
309                                         UIntConstant v;
310                                         uint res = ((UIntConstant)left).Value & ((UIntConstant)right).Value;
311                                         
312                                         v = new UIntConstant (res);
313                                         if (result_type == null)
314                                                 return v;
315                                         else
316                                                 return new EnumConstant (v, result_type);
317                                 } else if (left is LongConstant){
318                                         LongConstant v;
319                                         long res = ((LongConstant)left).Value & ((LongConstant)right).Value;
320                                         
321                                         v = new LongConstant (res);
322                                         if (result_type == null)
323                                                 return v;
324                                         else
325                                                 return new EnumConstant (v, result_type);
326                                 } else if (left is ULongConstant){
327                                         ULongConstant v;
328                                         ulong res = ((ULongConstant)left).Value &
329                                                 ((ULongConstant)right).Value;
330                                         
331                                         v = new ULongConstant (res);
332                                         if (result_type == null)
333                                                 return v;
334                                         else
335                                                 return new EnumConstant (v, result_type);
336                                 } else if (left is UShortConstant){
337                                         UShortConstant v;
338                                         ushort res = (ushort) (((UShortConstant)left).Value &
339                                                                ((UShortConstant)right).Value);
340                                         
341                                         v = new UShortConstant (res);
342                                         if (result_type == null)
343                                                 return v;
344                                         else
345                                                 return new EnumConstant (v, result_type);
346                                 } else if (left is ShortConstant){
347                                         ShortConstant v;
348                                         short res = (short) (((ShortConstant)left).Value &
349                                                              ((ShortConstant)right).Value);
350                                         
351                                         v = new ShortConstant (res);
352                                         if (result_type == null)
353                                                 return v;
354                                         else
355                                                 return new EnumConstant (v, result_type);
356                                 }
357                                 break;
358
359                         case Binary.Operator.ExclusiveOr:
360                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
361                                 if (left == null || right == null)
362                                         return null;
363                                 
364                                 if (left is IntConstant){
365                                         IntConstant v;
366                                         int res = ((IntConstant) left).Value ^ ((IntConstant) right).Value;
367                                         
368                                         v = new IntConstant (res);
369                                         if (result_type == null)
370                                                 return v;
371                                         else
372                                                 return new EnumConstant (v, result_type);
373                                 } else if (left is UIntConstant){
374                                         UIntConstant v;
375                                         uint res = ((UIntConstant)left).Value ^ ((UIntConstant)right).Value;
376                                         
377                                         v = new UIntConstant (res);
378                                         if (result_type == null)
379                                                 return v;
380                                         else
381                                                 return new EnumConstant (v, result_type);
382                                 } else if (left is LongConstant){
383                                         LongConstant v;
384                                         long res = ((LongConstant)left).Value ^ ((LongConstant)right).Value;
385                                         
386                                         v = new LongConstant (res);
387                                         if (result_type == null)
388                                                 return v;
389                                         else
390                                                 return new EnumConstant (v, result_type);
391                                 } else if (left is ULongConstant){
392                                         ULongConstant v;
393                                         ulong res = ((ULongConstant)left).Value ^
394                                                 ((ULongConstant)right).Value;
395                                         
396                                         v = new ULongConstant (res);
397                                         if (result_type == null)
398                                                 return v;
399                                         else
400                                                 return new EnumConstant (v, result_type);
401                                 } else if (left is UShortConstant){
402                                         UShortConstant v;
403                                         ushort res = (ushort) (((UShortConstant)left).Value ^
404                                                                ((UShortConstant)right).Value);
405                                         
406                                         v = new UShortConstant (res);
407                                         if (result_type == null)
408                                                 return v;
409                                         else
410                                                 return new EnumConstant (v, result_type);
411                                 } else if (left is ShortConstant){
412                                         ShortConstant v;
413                                         short res = (short)(((ShortConstant)left).Value ^
414                                                             ((ShortConstant)right).Value);
415                                         
416                                         v = new ShortConstant (res);
417                                         if (result_type == null)
418                                                 return v;
419                                         else
420                                                 return new EnumConstant (v, result_type);
421                                 }
422                                 break;
423
424                         case Binary.Operator.Addition:
425                                 bool left_is_string = left is StringConstant;
426                                 bool right_is_string = right is StringConstant;
427
428                                 //
429                                 // If both sides are strings, then concatenate, if
430                                 // one is a string, and the other is not, then defer
431                                 // to runtime concatenation
432                                 //
433                                 wrap_as = null;
434                                 if (left_is_string || right_is_string){
435                                         if (left_is_string && right_is_string)
436                                                 return new StringConstant (
437                                                         ((StringConstant) left).Value +
438                                                         ((StringConstant) right).Value);
439                                         
440                                         return null;
441                                 }
442
443                                 //
444                                 // handle "E operator + (E x, U y)"
445                                 // handle "E operator + (Y y, E x)"
446                                 //
447                                 // note that E operator + (E x, E y) is invalid
448                                 //
449                                 if (left is EnumConstant){
450                                         if (right is EnumConstant){
451                                                 return null;
452                                         }
453                                         if (((EnumConstant) left).Child.Type != right.Type)
454                                                 return null;
455
456                                         wrap_as = left.Type;
457                                 } else if (right is EnumConstant){
458                                         if (((EnumConstant) right).Child.Type != left.Type)
459                                                 return null;
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);
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);
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);
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);
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);
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);
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);
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 new EnumConstant (result, wrap_as);
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                                                         wrap_as = TypeManager.EnumToUnderlying (left.Type);
569                                                 else
570                                                         return null;
571                                         }
572                                         if (((EnumConstant) left).Child.Type != right.Type)
573                                                 return null;
574
575                                         wrap_as = left.Type;
576                                 } else if (right is EnumConstant){
577                                         if (((EnumConstant) right).Child.Type != left.Type)
578                                                 return null;
579                                         wrap_as = right.Type;
580                                 }
581
582                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
583                                 if (left == null || right == null)
584                                         return null;
585
586                                 try {
587                                         if (left is DoubleConstant){
588                                                 double res;
589                                                 
590                                                 if (ec.ConstantCheckState)
591                                                         res = checked (((DoubleConstant) left).Value -
592                                                                        ((DoubleConstant) right).Value);
593                                                 else
594                                                         res = unchecked (((DoubleConstant) left).Value -
595                                                                          ((DoubleConstant) right).Value);
596                                                 
597                                                 result = new DoubleConstant (res);
598                                         } else if (left is FloatConstant){
599                                                 float res;
600                                                 
601                                                 if (ec.ConstantCheckState)
602                                                         res = checked (((FloatConstant) left).Value -
603                                                                        ((FloatConstant) right).Value);
604                                                 else
605                                                         res = unchecked (((FloatConstant) left).Value -
606                                                                          ((FloatConstant) right).Value);
607                                                 
608                                                 result = new FloatConstant (res);
609                                         } else if (left is ULongConstant){
610                                                 ulong res;
611                                                 
612                                                 if (ec.ConstantCheckState)
613                                                         res = checked (((ULongConstant) left).Value -
614                                                                        ((ULongConstant) right).Value);
615                                                 else
616                                                         res = unchecked (((ULongConstant) left).Value -
617                                                                          ((ULongConstant) right).Value);
618                                                 
619                                                 result = new ULongConstant (res);
620                                         } else if (left is LongConstant){
621                                                 long res;
622                                                 
623                                                 if (ec.ConstantCheckState)
624                                                         res = checked (((LongConstant) left).Value -
625                                                                        ((LongConstant) right).Value);
626                                                 else
627                                                         res = unchecked (((LongConstant) left).Value -
628                                                                          ((LongConstant) right).Value);
629                                                 
630                                                 result = new LongConstant (res);
631                                         } else if (left is UIntConstant){
632                                                 uint res;
633                                                 
634                                                 if (ec.ConstantCheckState)
635                                                         res = checked (((UIntConstant) left).Value -
636                                                                        ((UIntConstant) right).Value);
637                                                 else
638                                                         res = unchecked (((UIntConstant) left).Value -
639                                                                          ((UIntConstant) right).Value);
640                                                 
641                                                 result = new UIntConstant (res);
642                                         } else if (left is IntConstant){
643                                                 int res;
644
645                                                 if (ec.ConstantCheckState)
646                                                         res = checked (((IntConstant) left).Value -
647                                                                        ((IntConstant) right).Value);
648                                                 else
649                                                         res = unchecked (((IntConstant) left).Value -
650                                                                          ((IntConstant) right).Value);
651
652                                                 result = new IntConstant (res);
653                                         } else if (left is DecimalConstant) {
654                                                 decimal res;
655
656                                                 if (ec.ConstantCheckState)
657                                                         res = checked (((DecimalConstant) left).Value -
658                                                                 ((DecimalConstant) right).Value);
659                                                 else
660                                                         res = unchecked (((DecimalConstant) left).Value -
661                                                                 ((DecimalConstant) right).Value);
662
663                                                 return new DecimalConstant (res);
664                                         } else {
665                                                 throw new Exception ( "Unexepected subtraction input: " + left);
666                                         }
667                                 } catch (OverflowException){
668                                         Error_CompileTimeOverflow (loc);
669                                 }
670                                 if (wrap_as != null)
671                                         return new EnumConstant (result, wrap_as);
672                                 else
673                                         return result;
674                                 
675                         case Binary.Operator.Multiply:
676                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
677                                 if (left == null || right == null)
678                                         return null;
679
680                                 try {
681                                         if (left is DoubleConstant){
682                                                 double res;
683                                                 
684                                                 if (ec.ConstantCheckState)
685                                                         res = checked (((DoubleConstant) left).Value *
686                                                                 ((DoubleConstant) right).Value);
687                                                 else
688                                                         res = unchecked (((DoubleConstant) left).Value *
689                                                                 ((DoubleConstant) right).Value);
690                                                 
691                                                 return new DoubleConstant (res);
692                                         } else if (left is FloatConstant){
693                                                 float res;
694                                                 
695                                                 if (ec.ConstantCheckState)
696                                                         res = checked (((FloatConstant) left).Value *
697                                                                 ((FloatConstant) right).Value);
698                                                 else
699                                                         res = unchecked (((FloatConstant) left).Value *
700                                                                 ((FloatConstant) right).Value);
701                                                 
702                                                 return new FloatConstant (res);
703                                         } else if (left is ULongConstant){
704                                                 ulong res;
705                                                 
706                                                 if (ec.ConstantCheckState)
707                                                         res = checked (((ULongConstant) left).Value *
708                                                                 ((ULongConstant) right).Value);
709                                                 else
710                                                         res = unchecked (((ULongConstant) left).Value *
711                                                                 ((ULongConstant) right).Value);
712                                                 
713                                                 return new ULongConstant (res);
714                                         } else if (left is LongConstant){
715                                                 long res;
716                                                 
717                                                 if (ec.ConstantCheckState)
718                                                         res = checked (((LongConstant) left).Value *
719                                                                 ((LongConstant) right).Value);
720                                                 else
721                                                         res = unchecked (((LongConstant) left).Value *
722                                                                 ((LongConstant) right).Value);
723                                                 
724                                                 return new LongConstant (res);
725                                         } else if (left is UIntConstant){
726                                                 uint res;
727                                                 
728                                                 if (ec.ConstantCheckState)
729                                                         res = checked (((UIntConstant) left).Value *
730                                                                 ((UIntConstant) right).Value);
731                                                 else
732                                                         res = unchecked (((UIntConstant) left).Value *
733                                                                 ((UIntConstant) right).Value);
734                                                 
735                                                 return new UIntConstant (res);
736                                         } else if (left is IntConstant){
737                                                 int res;
738
739                                                 if (ec.ConstantCheckState)
740                                                         res = checked (((IntConstant) left).Value *
741                                                                 ((IntConstant) right).Value);
742                                                 else
743                                                         res = unchecked (((IntConstant) left).Value *
744                                                                 ((IntConstant) right).Value);
745
746                                                 return new IntConstant (res);
747                                         } else if (left is DecimalConstant) {
748                                                 decimal res;
749
750                                                 if (ec.ConstantCheckState)
751                                                         res = checked (((DecimalConstant) left).Value *
752                                                                 ((DecimalConstant) right).Value);
753                                                 else
754                                                         res = unchecked (((DecimalConstant) left).Value *
755                                                                 ((DecimalConstant) right).Value);
756
757                                                 return new DecimalConstant (res);
758                                         } else {
759                                                 throw new Exception ( "Unexepected multiply input: " + left);
760                                         }
761                                 } catch (OverflowException){
762                                         Error_CompileTimeOverflow (loc);
763                                 }
764                                 break;
765
766                         case Binary.Operator.Division:
767                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
768                                 if (left == null || right == null)
769                                         return null;
770
771                                 try {
772                                         if (left is DoubleConstant){
773                                                 double res;
774                                                 
775                                                 if (ec.ConstantCheckState)
776                                                         res = checked (((DoubleConstant) left).Value /
777                                                                 ((DoubleConstant) right).Value);
778                                                 else
779                                                         res = unchecked (((DoubleConstant) left).Value /
780                                                                 ((DoubleConstant) right).Value);
781                                                 
782                                                 return new DoubleConstant (res);
783                                         } else if (left is FloatConstant){
784                                                 float res;
785                                                 
786                                                 if (ec.ConstantCheckState)
787                                                         res = checked (((FloatConstant) left).Value /
788                                                                 ((FloatConstant) right).Value);
789                                                 else
790                                                         res = unchecked (((FloatConstant) left).Value /
791                                                                 ((FloatConstant) right).Value);
792                                                 
793                                                 return new FloatConstant (res);
794                                         } else if (left is ULongConstant){
795                                                 ulong res;
796                                                 
797                                                 if (ec.ConstantCheckState)
798                                                         res = checked (((ULongConstant) left).Value /
799                                                                 ((ULongConstant) right).Value);
800                                                 else
801                                                         res = unchecked (((ULongConstant) left).Value /
802                                                                 ((ULongConstant) right).Value);
803                                                 
804                                                 return new ULongConstant (res);
805                                         } else if (left is LongConstant){
806                                                 long res;
807                                                 
808                                                 if (ec.ConstantCheckState)
809                                                         res = checked (((LongConstant) left).Value /
810                                                                 ((LongConstant) right).Value);
811                                                 else
812                                                         res = unchecked (((LongConstant) left).Value /
813                                                                 ((LongConstant) right).Value);
814                                                 
815                                                 return new LongConstant (res);
816                                         } else if (left is UIntConstant){
817                                                 uint res;
818                                                 
819                                                 if (ec.ConstantCheckState)
820                                                         res = checked (((UIntConstant) left).Value /
821                                                                 ((UIntConstant) right).Value);
822                                                 else
823                                                         res = unchecked (((UIntConstant) left).Value /
824                                                                 ((UIntConstant) right).Value);
825                                                 
826                                                 return new UIntConstant (res);
827                                         } else if (left is IntConstant){
828                                                 int res;
829
830                                                 if (ec.ConstantCheckState)
831                                                         res = checked (((IntConstant) left).Value /
832                                                                 ((IntConstant) right).Value);
833                                                 else
834                                                         res = unchecked (((IntConstant) left).Value /
835                                                                 ((IntConstant) right).Value);
836
837                                                 return new IntConstant (res);
838                                         } else if (left is DecimalConstant) {
839                                                 decimal res;
840
841                                                 if (ec.ConstantCheckState)
842                                                         res = checked (((DecimalConstant) left).Value /
843                                                                 ((DecimalConstant) right).Value);
844                                                 else
845                                                         res = unchecked (((DecimalConstant) left).Value /
846                                                                 ((DecimalConstant) right).Value);
847
848                                                 return new DecimalConstant (res);
849                                         } else {
850                                                 throw new Exception ( "Unexepected division input: " + left);
851                                         }
852                                 } catch (OverflowException){
853                                         Error_CompileTimeOverflow (loc);
854
855                                 } catch (DivideByZeroException) {
856                                         Report.Error (020, loc, "Division by constant zero");
857                                 }
858                                 
859                                 break;
860                                 
861                         case Binary.Operator.Modulus:
862                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
863                                 if (left == null || right == null)
864                                         return null;
865
866                                 try {
867                                         if (left is DoubleConstant){
868                                                 double res;
869                                                 
870                                                 if (ec.ConstantCheckState)
871                                                         res = checked (((DoubleConstant) left).Value %
872                                                                        ((DoubleConstant) right).Value);
873                                                 else
874                                                         res = unchecked (((DoubleConstant) left).Value %
875                                                                          ((DoubleConstant) right).Value);
876                                                 
877                                                 return new DoubleConstant (res);
878                                         } else if (left is FloatConstant){
879                                                 float res;
880                                                 
881                                                 if (ec.ConstantCheckState)
882                                                         res = checked (((FloatConstant) left).Value %
883                                                                        ((FloatConstant) right).Value);
884                                                 else
885                                                         res = unchecked (((FloatConstant) left).Value %
886                                                                          ((FloatConstant) right).Value);
887                                                 
888                                                 return new FloatConstant (res);
889                                         } else if (left is ULongConstant){
890                                                 ulong res;
891                                                 
892                                                 if (ec.ConstantCheckState)
893                                                         res = checked (((ULongConstant) left).Value %
894                                                                        ((ULongConstant) right).Value);
895                                                 else
896                                                         res = unchecked (((ULongConstant) left).Value %
897                                                                          ((ULongConstant) right).Value);
898                                                 
899                                                 return new ULongConstant (res);
900                                         } else if (left is LongConstant){
901                                                 long res;
902                                                 
903                                                 if (ec.ConstantCheckState)
904                                                         res = checked (((LongConstant) left).Value %
905                                                                        ((LongConstant) right).Value);
906                                                 else
907                                                         res = unchecked (((LongConstant) left).Value %
908                                                                          ((LongConstant) right).Value);
909                                                 
910                                                 return new LongConstant (res);
911                                         } else if (left is UIntConstant){
912                                                 uint res;
913                                                 
914                                                 if (ec.ConstantCheckState)
915                                                         res = checked (((UIntConstant) left).Value %
916                                                                        ((UIntConstant) right).Value);
917                                                 else
918                                                         res = unchecked (((UIntConstant) left).Value %
919                                                                          ((UIntConstant) right).Value);
920                                                 
921                                                 return new UIntConstant (res);
922                                         } else if (left is IntConstant){
923                                                 int res;
924
925                                                 if (ec.ConstantCheckState)
926                                                         res = checked (((IntConstant) left).Value %
927                                                                        ((IntConstant) right).Value);
928                                                 else
929                                                         res = unchecked (((IntConstant) left).Value %
930                                                                          ((IntConstant) right).Value);
931
932                                                 return new IntConstant (res);
933                                         } else {
934                                                 throw new Exception ( "Unexepected modulus input: " + left);
935                                         }
936                                 } catch (DivideByZeroException){
937                                         Report.Error (020, loc, "Division by constant zero");
938                                 } catch (OverflowException){
939                                         Error_CompileTimeOverflow (loc);
940                                 }
941                                 break;
942
943                                 //
944                                 // There is no overflow checking on left shift
945                                 //
946                         case Binary.Operator.LeftShift:
947                                 IntConstant ic = right.ToInt (loc);
948                                 if (ic == null){
949                                         Binary.Error_OperatorCannotBeApplied (loc, "<<", lt, rt);
950                                         return null;
951                                 }
952                                 int lshift_val = ic.Value;
953
954                                 IntConstant lic;
955                                 if ((lic = left.ConvertToInt ()) != null)
956                                         return new IntConstant (lic.Value << lshift_val);
957
958                                 UIntConstant luic;
959                                 if ((luic = left.ConvertToUInt ()) != null)
960                                         return new UIntConstant (luic.Value << lshift_val);
961
962                                 LongConstant llc;
963                                 if ((llc = left.ConvertToLong ()) != null)
964                                         return new LongConstant (llc.Value << lshift_val);
965
966                                 ULongConstant lulc;
967                                 if ((lulc = left.ConvertToULong ()) != null)
968                                         return new ULongConstant (lulc.Value << lshift_val);
969
970                                 Binary.Error_OperatorCannotBeApplied (loc, "<<", lt, rt);
971                                 break;
972
973                                 //
974                                 // There is no overflow checking on right shift
975                                 //
976                         case Binary.Operator.RightShift:
977                                 IntConstant sic = right.ToInt (loc);
978                                 if (sic == null){
979                                         Binary.Error_OperatorCannotBeApplied (loc, ">>", lt, rt);
980                                         return null;
981                                 }
982                                 int rshift_val = sic.Value;
983
984                                 IntConstant ric;
985                                 if ((ric = left.ConvertToInt ()) != null)
986                                         return new IntConstant (ric.Value >> rshift_val);
987
988                                 UIntConstant ruic;
989                                 if ((ruic = left.ConvertToUInt ()) != null)
990                                         return new UIntConstant (ruic.Value >> rshift_val);
991
992                                 LongConstant rlc;
993                                 if ((rlc = left.ConvertToLong ()) != null)
994                                         return new LongConstant (rlc.Value >> rshift_val);
995
996                                 ULongConstant rulc;
997                                 if ((rulc = left.ConvertToULong ()) != null)
998                                         return new ULongConstant (rulc.Value >> rshift_val);
999
1000                                 Binary.Error_OperatorCannotBeApplied (loc, ">>", lt, rt);
1001                                 break;
1002
1003                         case Binary.Operator.LogicalAnd:
1004                                 if (left is BoolConstant && right is BoolConstant){
1005                                         return new BoolConstant (
1006                                                 ((BoolConstant) left).Value &&
1007                                                 ((BoolConstant) right).Value);
1008                                 }
1009                                 break;
1010
1011                         case Binary.Operator.LogicalOr:
1012                                 if (left is BoolConstant && right is BoolConstant){
1013                                         return new BoolConstant (
1014                                                 ((BoolConstant) left).Value ||
1015                                                 ((BoolConstant) right).Value);
1016                                 }
1017                                 break;
1018                                 
1019                         case Binary.Operator.Equality:
1020                                 if (left is BoolConstant && right is BoolConstant){
1021                                         return new BoolConstant (
1022                                                 ((BoolConstant) left).Value ==
1023                                                 ((BoolConstant) right).Value);
1024                                 
1025                                 }
1026                                 if (left is NullLiteral){
1027                                         if (right is NullLiteral)
1028                                                 return new BoolConstant (true);
1029                                         else if (right is StringConstant)
1030                                                 return new BoolConstant (
1031                                                         ((StringConstant) right).Value == null);
1032                                 } else if (right is NullLiteral){
1033                                         if (left is NullLiteral)
1034                                                 return new BoolConstant (true);
1035                                         else if (left is StringConstant)
1036                                                 return new BoolConstant (
1037                                                         ((StringConstant) left).Value == null);
1038                                 }
1039                                 if (left is StringConstant && right is StringConstant){
1040                                         return new BoolConstant (
1041                                                 ((StringConstant) left).Value ==
1042                                                 ((StringConstant) right).Value);
1043                                         
1044                                 }
1045
1046                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1047                                 if (left == null || right == null)
1048                                         return null;
1049
1050                                 bool_res = false;
1051                                 if (left is DoubleConstant)
1052                                         bool_res = ((DoubleConstant) left).Value ==
1053                                                 ((DoubleConstant) right).Value;
1054                                 else if (left is FloatConstant)
1055                                         bool_res = ((FloatConstant) left).Value ==
1056                                                 ((FloatConstant) right).Value;
1057                                 else if (left is ULongConstant)
1058                                         bool_res = ((ULongConstant) left).Value ==
1059                                                 ((ULongConstant) right).Value;
1060                                 else if (left is LongConstant)
1061                                         bool_res = ((LongConstant) left).Value ==
1062                                                 ((LongConstant) right).Value;
1063                                 else if (left is UIntConstant)
1064                                         bool_res = ((UIntConstant) left).Value ==
1065                                                 ((UIntConstant) right).Value;
1066                                 else if (left is IntConstant)
1067                                         bool_res = ((IntConstant) left).Value ==
1068                                                 ((IntConstant) right).Value;
1069                                 else
1070                                         return null;
1071
1072                                 return new BoolConstant (bool_res);
1073
1074                         case Binary.Operator.Inequality:
1075                                 if (left is BoolConstant && right is BoolConstant){
1076                                         return new BoolConstant (
1077                                                 ((BoolConstant) left).Value !=
1078                                                 ((BoolConstant) right).Value);
1079                                 }
1080                                 if (left is NullLiteral){
1081                                         if (right is NullLiteral)
1082                                                 return new BoolConstant (false);
1083                                         else if (right is StringConstant)
1084                                                 return new BoolConstant (
1085                                                         ((StringConstant) right).Value != null);
1086                                 } else if (right is NullLiteral){
1087                                         if (left is NullLiteral)
1088                                                 return new BoolConstant (false);
1089                                         else if (left is StringConstant)
1090                                                 return new BoolConstant (
1091                                                         ((StringConstant) left).Value != null);
1092                                 }
1093                                 if (left is StringConstant && right is StringConstant){
1094                                         return new BoolConstant (
1095                                                 ((StringConstant) left).Value !=
1096                                                 ((StringConstant) right).Value);
1097                                         
1098                                 }
1099                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1100                                 if (left == null || right == null)
1101                                         return null;
1102
1103                                 bool_res = false;
1104                                 if (left is DoubleConstant)
1105                                         bool_res = ((DoubleConstant) left).Value !=
1106                                                 ((DoubleConstant) right).Value;
1107                                 else if (left is FloatConstant)
1108                                         bool_res = ((FloatConstant) left).Value !=
1109                                                 ((FloatConstant) right).Value;
1110                                 else if (left is ULongConstant)
1111                                         bool_res = ((ULongConstant) left).Value !=
1112                                                 ((ULongConstant) right).Value;
1113                                 else if (left is LongConstant)
1114                                         bool_res = ((LongConstant) left).Value !=
1115                                                 ((LongConstant) right).Value;
1116                                 else if (left is UIntConstant)
1117                                         bool_res = ((UIntConstant) left).Value !=
1118                                                 ((UIntConstant) right).Value;
1119                                 else if (left is IntConstant)
1120                                         bool_res = ((IntConstant) left).Value !=
1121                                                 ((IntConstant) right).Value;
1122                                 else
1123                                         return null;
1124
1125                                 return new BoolConstant (bool_res);
1126
1127                         case Binary.Operator.LessThan:
1128                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1129                                 if (left == null || right == null)
1130                                         return null;
1131
1132                                 bool_res = false;
1133                                 if (left is DoubleConstant)
1134                                         bool_res = ((DoubleConstant) left).Value <
1135                                                 ((DoubleConstant) right).Value;
1136                                 else if (left is FloatConstant)
1137                                         bool_res = ((FloatConstant) left).Value <
1138                                                 ((FloatConstant) right).Value;
1139                                 else if (left is ULongConstant)
1140                                         bool_res = ((ULongConstant) left).Value <
1141                                                 ((ULongConstant) right).Value;
1142                                 else if (left is LongConstant)
1143                                         bool_res = ((LongConstant) left).Value <
1144                                                 ((LongConstant) right).Value;
1145                                 else if (left is UIntConstant)
1146                                         bool_res = ((UIntConstant) left).Value <
1147                                                 ((UIntConstant) right).Value;
1148                                 else if (left is IntConstant)
1149                                         bool_res = ((IntConstant) left).Value <
1150                                                 ((IntConstant) right).Value;
1151                                 else
1152                                         return null;
1153
1154                                 return new BoolConstant (bool_res);
1155                                 
1156                         case Binary.Operator.GreaterThan:
1157                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1158                                 if (left == null || right == null)
1159                                         return null;
1160
1161                                 bool_res = false;
1162                                 if (left is DoubleConstant)
1163                                         bool_res = ((DoubleConstant) left).Value >
1164                                                 ((DoubleConstant) right).Value;
1165                                 else if (left is FloatConstant)
1166                                         bool_res = ((FloatConstant) left).Value >
1167                                                 ((FloatConstant) right).Value;
1168                                 else if (left is ULongConstant)
1169                                         bool_res = ((ULongConstant) left).Value >
1170                                                 ((ULongConstant) right).Value;
1171                                 else if (left is LongConstant)
1172                                         bool_res = ((LongConstant) left).Value >
1173                                                 ((LongConstant) right).Value;
1174                                 else if (left is UIntConstant)
1175                                         bool_res = ((UIntConstant) left).Value >
1176                                                 ((UIntConstant) right).Value;
1177                                 else if (left is IntConstant)
1178                                         bool_res = ((IntConstant) left).Value >
1179                                                 ((IntConstant) right).Value;
1180                                 else
1181                                         return null;
1182
1183                                 return new BoolConstant (bool_res);
1184
1185                         case Binary.Operator.GreaterThanOrEqual:
1186                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1187                                 if (left == null || right == null)
1188                                         return null;
1189
1190                                 bool_res = false;
1191                                 if (left is DoubleConstant)
1192                                         bool_res = ((DoubleConstant) left).Value >=
1193                                                 ((DoubleConstant) right).Value;
1194                                 else if (left is FloatConstant)
1195                                         bool_res = ((FloatConstant) left).Value >=
1196                                                 ((FloatConstant) right).Value;
1197                                 else if (left is ULongConstant)
1198                                         bool_res = ((ULongConstant) left).Value >=
1199                                                 ((ULongConstant) right).Value;
1200                                 else if (left is LongConstant)
1201                                         bool_res = ((LongConstant) left).Value >=
1202                                                 ((LongConstant) right).Value;
1203                                 else if (left is UIntConstant)
1204                                         bool_res = ((UIntConstant) left).Value >=
1205                                                 ((UIntConstant) right).Value;
1206                                 else if (left is IntConstant)
1207                                         bool_res = ((IntConstant) left).Value >=
1208                                                 ((IntConstant) right).Value;
1209                                 else
1210                                         return null;
1211
1212                                 return new BoolConstant (bool_res);
1213
1214                         case Binary.Operator.LessThanOrEqual:
1215                                 DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
1216                                 if (left == null || right == null)
1217                                         return null;
1218
1219                                 bool_res = false;
1220                                 if (left is DoubleConstant)
1221                                         bool_res = ((DoubleConstant) left).Value <=
1222                                                 ((DoubleConstant) right).Value;
1223                                 else if (left is FloatConstant)
1224                                         bool_res = ((FloatConstant) left).Value <=
1225                                                 ((FloatConstant) right).Value;
1226                                 else if (left is ULongConstant)
1227                                         bool_res = ((ULongConstant) left).Value <=
1228                                                 ((ULongConstant) right).Value;
1229                                 else if (left is LongConstant)
1230                                         bool_res = ((LongConstant) left).Value <=
1231                                                 ((LongConstant) right).Value;
1232                                 else if (left is UIntConstant)
1233                                         bool_res = ((UIntConstant) left).Value <=
1234                                                 ((UIntConstant) right).Value;
1235                                 else if (left is IntConstant)
1236                                         bool_res = ((IntConstant) left).Value <=
1237                                                 ((IntConstant) right).Value;
1238                                 else
1239                                         return null;
1240
1241                                 return new BoolConstant (bool_res);
1242                         }
1243                                         
1244                         return null;
1245                 }
1246         }
1247 }