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