PageRenderTime 59ms CodeModel.GetById 44ms app.highlight 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/IronPython/IronPython/Compiler/Ast/BinaryExpression.Generated.cs

https://github.com/dsblank/main
C# | 154 lines | 121 code | 14 blank | 19 comment | 48 complexity | 208b7a74b66d6406e83a99a31b0ef2fc MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16using System;
 17using System.Collections.Generic;
 18using System.Text;
 19
 20using Microsoft.Scripting.Runtime;
 21
 22using IronPython.Runtime.Operations;
 23
 24#if CLR2
 25using Microsoft.Scripting.Math;
 26using Complex = Microsoft.Scripting.Math.Complex64;
 27#else
 28using System.Numerics;
 29#endif
 30
 31namespace IronPython.Compiler.Ast {
 32    public partial class BinaryExpression {
 33        const int MaximumInlineStringLength = 1024 * 1024;
 34
 35        internal override ConstantExpression ConstantFold() {
 36            Expression left = _left.ConstantFold();
 37            Expression right = _right.ConstantFold();
 38
 39            var constLeft = left as ConstantExpression;
 40            var constRight = right as ConstantExpression;
 41            try {
 42                if (constLeft != null && constRight != null &&
 43                    constLeft.Value != null && constRight.Value != null &&
 44                    constLeft.Value.GetType() == constRight.Value.GetType()) {
 45                    #region Generated Python Constant Folding
 46
 47                    // *** BEGIN GENERATED CODE ***
 48                    // generated by function: gen_constant_folding from: generate_ops.py
 49
 50                    if (constLeft.Value.GetType() == typeof(Int32)) {
 51                        switch (_op) {
 52                            case PythonOperator.Add: return new ConstantExpression(Int32Ops.Add((Int32)constLeft.Value, (Int32)constRight.Value));
 53                            case PythonOperator.Subtract: return new ConstantExpression(Int32Ops.Subtract((Int32)constLeft.Value, (Int32)constRight.Value));
 54                            case PythonOperator.Power: return new ConstantExpression(Int32Ops.Power((Int32)constLeft.Value, (Int32)constRight.Value));
 55                            case PythonOperator.Multiply: return new ConstantExpression(Int32Ops.Multiply((Int32)constLeft.Value, (Int32)constRight.Value));
 56                            case PythonOperator.FloorDivide: return new ConstantExpression(Int32Ops.FloorDivide((Int32)constLeft.Value, (Int32)constRight.Value));
 57                            case PythonOperator.Divide: return new ConstantExpression(Int32Ops.Divide((Int32)constLeft.Value, (Int32)constRight.Value));
 58                            case PythonOperator.TrueDivide: return new ConstantExpression(Int32Ops.TrueDivide((Int32)constLeft.Value, (Int32)constRight.Value));
 59                            case PythonOperator.Mod: return new ConstantExpression(Int32Ops.Mod((Int32)constLeft.Value, (Int32)constRight.Value));
 60                            case PythonOperator.LeftShift: return new ConstantExpression(Int32Ops.LeftShift((Int32)constLeft.Value, (Int32)constRight.Value));
 61                            case PythonOperator.RightShift: return new ConstantExpression(Int32Ops.RightShift((Int32)constLeft.Value, (Int32)constRight.Value));
 62                            case PythonOperator.BitwiseAnd: return new ConstantExpression(Int32Ops.BitwiseAnd((Int32)constLeft.Value, (Int32)constRight.Value));
 63                            case PythonOperator.BitwiseOr: return new ConstantExpression(Int32Ops.BitwiseOr((Int32)constLeft.Value, (Int32)constRight.Value));
 64                            case PythonOperator.ExclusiveOr: return new ConstantExpression(Int32Ops.ExclusiveOr((Int32)constLeft.Value, (Int32)constRight.Value));
 65                            case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) < 0));
 66                            case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) > 0));
 67                            case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) <= 0));
 68                            case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) >= 0));
 69                            case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) == 0));
 70                            case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) != 0));
 71                        }
 72                    }
 73                    if (constLeft.Value.GetType() == typeof(Double)) {
 74                        switch (_op) {
 75                            case PythonOperator.Add: return new ConstantExpression(DoubleOps.Add((Double)constLeft.Value, (Double)constRight.Value));
 76                            case PythonOperator.Subtract: return new ConstantExpression(DoubleOps.Subtract((Double)constLeft.Value, (Double)constRight.Value));
 77                            case PythonOperator.Power: return new ConstantExpression(DoubleOps.Power((Double)constLeft.Value, (Double)constRight.Value));
 78                            case PythonOperator.Multiply: return new ConstantExpression(DoubleOps.Multiply((Double)constLeft.Value, (Double)constRight.Value));
 79                            case PythonOperator.FloorDivide: return new ConstantExpression(DoubleOps.FloorDivide((Double)constLeft.Value, (Double)constRight.Value));
 80                            case PythonOperator.Divide: return new ConstantExpression(DoubleOps.Divide((Double)constLeft.Value, (Double)constRight.Value));
 81                            case PythonOperator.TrueDivide: return new ConstantExpression(DoubleOps.TrueDivide((Double)constLeft.Value, (Double)constRight.Value));
 82                            case PythonOperator.Mod: return new ConstantExpression(DoubleOps.Mod((Double)constLeft.Value, (Double)constRight.Value));
 83                            case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) < 0));
 84                            case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) > 0));
 85                            case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) <= 0));
 86                            case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) >= 0));
 87                            case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) == 0));
 88                            case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) != 0));
 89                        }
 90                    }
 91                    if (constLeft.Value.GetType() == typeof(BigInteger)) {
 92                        switch (_op) {
 93                            case PythonOperator.Add: return new ConstantExpression(BigIntegerOps.Add((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
 94                            case PythonOperator.Subtract: return new ConstantExpression(BigIntegerOps.Subtract((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
 95                            case PythonOperator.Power: return new ConstantExpression(BigIntegerOps.Power((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
 96                            case PythonOperator.Multiply: return new ConstantExpression(BigIntegerOps.Multiply((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
 97                            case PythonOperator.FloorDivide: return new ConstantExpression(BigIntegerOps.FloorDivide((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
 98                            case PythonOperator.Divide: return new ConstantExpression(BigIntegerOps.Divide((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
 99                            case PythonOperator.TrueDivide: return new ConstantExpression(BigIntegerOps.TrueDivide((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
100                            case PythonOperator.Mod: return new ConstantExpression(BigIntegerOps.Mod((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
101                            case PythonOperator.LeftShift: return new ConstantExpression(BigIntegerOps.LeftShift((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
102                            case PythonOperator.RightShift: return new ConstantExpression(BigIntegerOps.RightShift((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
103                            case PythonOperator.BitwiseAnd: return new ConstantExpression(BigIntegerOps.BitwiseAnd((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
104                            case PythonOperator.BitwiseOr: return new ConstantExpression(BigIntegerOps.BitwiseOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
105                            case PythonOperator.ExclusiveOr: return new ConstantExpression(BigIntegerOps.ExclusiveOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
106                            case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) < 0));
107                            case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) > 0));
108                            case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) <= 0));
109                            case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) >= 0));
110                            case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) == 0));
111                            case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) != 0));
112                        }
113                    }
114                    if (constLeft.Value.GetType() == typeof(Complex)) {
115                        switch (_op) {
116                            case PythonOperator.Add: return new ConstantExpression(ComplexOps.Add((Complex)constLeft.Value, (Complex)constRight.Value));
117                            case PythonOperator.Subtract: return new ConstantExpression(ComplexOps.Subtract((Complex)constLeft.Value, (Complex)constRight.Value));
118                            case PythonOperator.Power: return new ConstantExpression(ComplexOps.Power((Complex)constLeft.Value, (Complex)constRight.Value));
119                            case PythonOperator.Multiply: return new ConstantExpression(ComplexOps.Multiply((Complex)constLeft.Value, (Complex)constRight.Value));
120                            case PythonOperator.Divide: return new ConstantExpression(ComplexOps.Divide((Complex)constLeft.Value, (Complex)constRight.Value));
121                            case PythonOperator.TrueDivide: return new ConstantExpression(ComplexOps.TrueDivide((Complex)constLeft.Value, (Complex)constRight.Value));
122                        }
123                    }
124
125                    // *** END GENERATED CODE ***
126
127                    #endregion
128
129                    if (constLeft.Value.GetType() == typeof(string) && _op == PythonOperator.Add) {
130                        return new ConstantExpression((string)constLeft.Value + (string)constRight.Value);
131                    }
132                } else if (_op == PythonOperator.Multiply && constLeft != null && constRight != null) {
133                    // 10000 check is to avoid creating massive strings in IL - there's a limit to the number of
134                    // bytes of strings we get to have so we don't want to use them all up.
135                    if (constLeft.Value.GetType() == typeof(string) && constRight.Value.GetType() == typeof(int)) {
136                        var res = StringOps.Multiply((string)constLeft.Value, (int)constRight.Value);
137                        if (res.Length < MaximumInlineStringLength) {
138                            return new ConstantExpression(res);
139                        }
140                    } else if (constLeft.Value.GetType() == typeof(int) && constRight.Value.GetType() == typeof(string)) {
141                        var res = StringOps.Multiply((string)constRight.Value, (int)constLeft.Value);
142                        if (res.Length < MaximumInlineStringLength) {
143                            return new ConstantExpression(res);
144                        }
145                    }
146                }   
147            } catch (ArithmeticException) {
148            }
149
150            return null;
151        }
152
153    }
154}