예제 #1
0
 public string Generated_GenerateExpression(Expression expr)
 {
     if (expr.Parent != null && expr.Parent.NodeType > expr.NodeType && expr.NodeType != ExpressionType.Variable && expr.NodeType != ExpressionType.Constant)
         {
             return "(" + Generated_GenerateExpressionWB(expr) + ")";
         }
         else
         {
             return Generated_GenerateExpressionWB(expr);
         }
 }
예제 #2
0
 private void Validate_Expression(Expression expression)
 {
     if (expression.NodeType == ExpressionType.Constant) return;
     if (TypeHelpers.IsAssignableFrom(expression.ExpectedType, expression.Type))
     {
         foreach (Expression child in expression.Children)
         {
             this.Validate_Expression(child);
         }
     }
     else
     {
         errorReporter.Error(expression.GetMetaInfo<SourceLocationInfo>(), "Type mismatch, expected \"{0}\", found \"{1}\"", (expression.ExpectedType != null) ? expression.ExpectedType.Name : "?", (expression.Type != null) ? expression.Type.Name : "?");
     }
 }
예제 #3
0
 private void GetReferencedNamespaces(HashSet<Namespace> imports, Expression expression)
 {
     switch (expression.NodeType)
     {
         case ExpressionType.Convert:
         case ExpressionType.TypeAs:
             imports.Add(((UnaryExpression)expression).Type.Namespace);
             break;
         case ExpressionType.New:
             imports.Add(((NewExpression)expression).Type.Namespace);
             break;
         case ExpressionType.NewArrayInit:
         case ExpressionType.NewArrayBounds:
             imports.Add(((NewArrayExpression)expression).ItemType.Namespace);
             break;
         case ExpressionType.TypeIs:
             imports.Add(((TypeBinaryExpression)expression).TypeOperand.Namespace);
             break;
         //case ExpressionType.Parameter:
         case ExpressionType.Default:
             imports.Add(((DefaultExpression)expression).Type.Namespace);
             break;
         default:
             break;
     }
     foreach (Expression child in expression.Children)
     {
         GetReferencedNamespaces(imports, child);
     }
 }
예제 #4
0
 public string Generated_GenerateExpressionWB(Expression expr)
 {
     if (expr.NodeType == ExpressionType.Negate)
         {
             return "-" + Generated_GenerateExpression(((UnaryExpression)expr).Operand);
         }
         else if (expr.NodeType == ExpressionType.UnaryPlus)
         {
             return Generated_GenerateExpression(((UnaryExpression)expr).Operand);
         }
         else if (expr.NodeType == ExpressionType.OnesComplement)
         {
             return "~" + Generated_GenerateExpression(((UnaryExpression)expr).Operand);
         }
         else if (expr.NodeType == ExpressionType.Not)
         {
             return "!" + Generated_GenerateExpression(((UnaryExpression)expr).Operand);
         }
         else if (expr.NodeType == ExpressionType.Convert)
         {
             return "(" + Generated_PrintType((Type)((UnaryExpression)expr).Type) + ")" + Generated_GenerateExpression(((UnaryExpression)expr).Operand);
         }
         else if (expr.NodeType == ExpressionType.TypeAs)
         {
             return Generated_GenerateExpression(((UnaryExpression)expr).Operand) + " as " + Generated_PrintType((Type)((UnaryExpression)expr).Type);
         }
         else if (expr.NodeType == ExpressionType.New)
         {
             if (((NewExpression)expr).Members.Count == 0)
             {
                 return "new " + Generated_PrintType((Type)((NewExpression)expr).Type) + "()";
             }
             else
             {
                 StringBuilder s = new StringBuilder("new " + Generated_PrintType((Type)((NewExpression)expr).Type) + "() {");
                 s.Append("}");
                 return s.ToString();
             }
         }
         else if (expr.NodeType == ExpressionType.NewArrayInit)
         {
             StringBuilder s = new StringBuilder("new " + Generated_PrintType((Type)((NewArrayExpression)expr).Type) + "[] {");
             string comma = "";
             int __loop1_iteration = 0;
             var __loop1_result =
                 (from __loop1_tmp_item___noname1 in EnumerableExtensions.Enumerate((((NewArrayExpression)expr).Expressions).GetEnumerator())
                 from __loop1_tmp_item_ex in EnumerableExtensions.Enumerate((__loop1_tmp_item___noname1).GetEnumerator()).OfType<Expression>()
                 select
                     new
                     {
                         __loop1_item___noname1 = __loop1_tmp_item___noname1,
                         __loop1_item_ex = __loop1_tmp_item_ex,
                     }).ToArray();
             foreach (var __loop1_item in __loop1_result)
             {
                 var __noname1 = __loop1_item.__loop1_item___noname1;
                 var ex = __loop1_item.__loop1_item_ex;
                 ++__loop1_iteration;
                 s.Append(comma + Generated_GenerateExpression(ex));
                 comma = ", ";
             }
             s.Append("}");
             return s.ToString();
         }
         else if (expr.NodeType == ExpressionType.NewArrayBounds)
         {
             return "new " + Generated_PrintType((Type)((NewArrayExpression)expr).Type) + "[" + Generated_GenerateExpression(((NewArrayExpression)expr).Expressions.ElementAt(0)) + "]";
         }
         else if (expr.NodeType == ExpressionType.LeftShift)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " << " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.RightShift)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " >> " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Modulo)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " % " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Add)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " + " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Divide)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " / " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Multiply)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " * " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Subtract)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " - " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.And)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " & " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.AndAlso)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " && " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Equal)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " == " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.ExclusiveOr)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " ^ " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.GreaterThan)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " > " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.GreaterThanOrEqual)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " >= " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.LessThan)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " < " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.LessThanOrEqual)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " <= " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.NotEqual)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " != " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Or)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " | " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.OrElse)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " || " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.Coalesce)
         {
             return Generated_GenerateExpression(((BinaryExpression)expr).Left) + " ?? " + Generated_GenerateExpression(((BinaryExpression)expr).Right);
         }
         else if (expr.NodeType == ExpressionType.TypeIs)
         {
             return Generated_GenerateExpression(((TypeBinaryExpression)expr).Expression) + " is " + Generated_PrintType(((TypeBinaryExpression)expr).TypeOperand);
         }
         else if (expr.NodeType == ExpressionType.Conditional)
         {
             return "(" + Generated_GenerateExpression(((ConditionalExpression)expr).Test) + ") ? " + Generated_GenerateExpression(((ConditionalExpression)expr).IfThen) + " : " + Generated_GenerateExpression(((ConditionalExpression)expr).IfElse);
         }
         else if (expr.NodeType == ExpressionType.Lambda)
         {
             return "";
         }
         else if (expr.NodeType == ExpressionType.Constant)
         {
             if (((ConstantExpression)expr).Type == PseudoType.Object)
             {
                 return "null";
             }
             else if (((ConstantExpression)expr).Type == BuiltInType.Bool)
             {
                 return ((ConstantExpression)expr).Value.ToString().ToLower();
             }
             else if (((ConstantExpression)expr).Type == BuiltInType.Long || ((ConstantExpression)expr).Type == BuiltInType.Double)
             {
                 return ((ConstantExpression)expr).Value.ToString();
             }
             else if (((ConstantExpression)expr).Type == BuiltInType.String || ((ConstantExpression)expr).Type == BuiltInType.Guid)
             {
                 return "\"" + ((ConstantExpression)expr).Value.ToString() + "\"";
             }
             else if (((ConstantExpression)expr).Type == BuiltInType.Date || ((ConstantExpression)expr).Type == BuiltInType.Time || ((ConstantExpression)expr).Type == BuiltInType.DateTime || ((ConstantExpression)expr).Type == BuiltInType.TimeSpan)
             {
                 return "";
             }
         }
         else if (expr.NodeType == ExpressionType.Default)
         {
             return "";
         }
         else if (expr.NodeType == ExpressionType.Old)
         {
             return "";
         }
         else if (expr.NodeType == ExpressionType.Variable)
         {
             if (((IdentifierExpression)expr).Object is Reference)
             {
                 return ((Reference)(((IdentifierExpression)expr).Object)).Name;
             }
             else
             {
                 return ((Declaration)(((IdentifierExpression)expr).Object)).Name;
             }
         }
         else if (expr.NodeType == ExpressionType.ArrayIndex)
         {
             return Generated_GenerateExpression(((IndexExpression)expr).Object) + "[" + Generated_GenerateExpression(((IndexExpression)expr).Argument) + "]";
         }
         else if (expr.NodeType == ExpressionType.MemberAccess)
         {
             if (((MemberExpression)expr).Member is Declaration)
             {
                 return Generated_GenerateExpression(((MemberExpression)expr).Object) + "." + ((Declaration)((MemberExpression)expr).Member).Name;
             }
             else if (((MemberExpression)expr).Member is ClaimField)
             {
                 return Generated_GenerateExpression(((MemberExpression)expr).Object) + "." + ((ClaimField)((MemberExpression)expr).Member).Name;
             }
             else if (((MemberExpression)expr).Member is EnumValue)
             {
                 return Generated_GenerateExpression(((MemberExpression)expr).Object) + "." + ((EnumValue)((MemberExpression)expr).Member).Name.ToUpper();
             }
         }
         else if (expr.NodeType == ExpressionType.Call)
         {
             StringBuilder s = new StringBuilder(Generated_GenerateExpression(((MethodCallExpression)expr).Object) + "." + ((MethodCallExpression)expr).Operation.Name + "(");
             string comma = "";
             int __loop2_iteration = 0;
             var __loop2_result =
                 (from __loop2_tmp_item___noname2 in EnumerableExtensions.Enumerate((((MethodCallExpression)expr).Arguments).GetEnumerator())
                 from __loop2_tmp_item_ex in EnumerableExtensions.Enumerate((__loop2_tmp_item___noname2).GetEnumerator()).OfType<Expression>()
                 select
                     new
                     {
                         __loop2_item___noname2 = __loop2_tmp_item___noname2,
                         __loop2_item_ex = __loop2_tmp_item_ex,
                     }).ToArray();
             foreach (var __loop2_item in __loop2_result)
             {
                 var __noname2 = __loop2_item.__loop2_item___noname2;
                 var ex = __loop2_item.__loop2_item_ex;
                 ++__loop2_iteration;
                 s.Append(comma + Generated_GenerateExpression(ex));
                 comma = ", ";
             }
             s.Append(")");
             return s.ToString();
         }
         return "";
 }
예제 #5
0
        public MethodCallExpression(Expression obj, string operationName, IEnumerable<Expression> arguments)
            : base(ExpressionType.Call)
        {
            this.Object = obj;
            this.Arguments = new List<Expression>(arguments);

            ModelClass.LazyInit(this, OperationProperty, () => this.CalculateOperation(operationName));
            ModelClass.LazyInit(this, TypeProperty, () => this.Operation.ReturnType);

            for(int i = 0; i < this.Arguments.Count; i++)
            {
                ModelClass.LazyInit(this.Arguments[i], ExpectedTypeProperty, () => this.Operation.Parameters[i].Type);
            }
            ModelClass.LazyInit(this, ValueProperty, () => { throw new EvaluationException(this); });

            this.Children.Add(this.Object);
            this.Children.AddRange(this.Arguments);
        }
예제 #6
0
        public MemberExpression(Expression @object, string memberName)
            : base(ExpressionType.MemberAccess)
        {
            this.Object = @object;

            ModelClass.LazyInit(this, MemberProperty, () => this.CalculateMember(memberName));
            ModelClass.LazyInit(this, TypeProperty, () => this.CalculateType());
            ModelClass.LazyInit(this, ValueProperty, () => this.CalculateValue());
            ModelClass.LazyInit(this.Object, ExpectedTypeProperty, () => PseudoType.Object);

            this.Children.Add(this.Object);
        }
예제 #7
0
        public MemberInitExpression(string memberName, Expression value)
            : base(ExpressionType.MemberInit)
        {
            this.ValueExpression = value;

            ModelClass.LazyInit(this, PropertyProperty, () => NameContext.ResolveName(this.NewExpression.Type, memberName, typeof(StructField), typeof(ExceptionField)));
            ModelClass.LazyInit(this, ExpectedTypeProperty, () => this.Property.Type);
            ModelClass.LazyInit(this, TypeProperty, () => this.ValueExpression.Type);
            ModelClass.LazyInit(this, ValueProperty, () => this.ValueExpression.Value);
            ModelClass.LazyInit(this.ValueExpression, ExpectedTypeProperty, () => this.ExpectedType);

            this.Children.Add(this.ValueExpression);
        }
예제 #8
0
        public IndexExpression(Expression obj, Expression argument)
            : base(ExpressionType.ArrayIndex)
        {
            this.Object = obj;
            this.Argument = argument;

            ModelClass.LazyInit(this, TypeProperty, () => ((ArrayType)this.Object.Type).ItemType);
            ModelClass.LazyInit(this.Argument, ExpectedTypeProperty, () => BuiltInType.Int);
            ModelClass.LazyInit(this, ValueProperty, () => { throw new EvaluationException(this); });

            this.Children.Add(this.Object);
            this.Children.Add(this.Argument);
        }
예제 #9
0
        public ConditionalExpression(Expression test, Expression ifThen, Expression ifElse)
            : base(ExpressionType.Conditional)
        {
            this.Test = test;
            this.IfThen = ifThen;
            this.IfElse = ifElse;

            ModelClass.LazyInit(this.Test, ExpectedTypeProperty, () => BuiltInType.Bool);
            ModelClass.LazyInit(this.IfThen, ExpectedTypeProperty, () => this.ExpectedType);
            ModelClass.LazyInit(this.IfElse, ExpectedTypeProperty, () => this.ExpectedType);
            ModelClass.LazyInit(this, TypeProperty, () => TypeHelpers.GetCommonType(this.IfThen.Type, this.IfElse.Type));
            ModelClass.LazyInit(this, ValueProperty, () => { throw new EvaluationException(this); });

            this.Children.Add(this.Test);
            this.Children.Add(this.IfThen);
            this.Children.Add(this.IfElse);
        }
예제 #10
0
        public BinaryExpression(ExpressionType nodeType, Expression left, Expression right)
            : base(nodeType)
        {
            this.Left = left;
            this.Right = right;

            switch (nodeType)
            {
                case ExpressionType.LeftShift:
                case ExpressionType.RightShift:
                case ExpressionType.Modulo:
                    ModelClass.LazyInit(this, TypeProperty, () => TypeHelpers.GetCommonType(this.Left.Type, this.Right.Type));
                    ModelClass.LazyInit(this.Left, ExpectedTypeProperty, () => BuiltInType.Long);
                    ModelClass.LazyInit(this.Right, ExpectedTypeProperty, () => BuiltInType.Long);
                    break;
                case ExpressionType.Add:
                case ExpressionType.Divide:
                case ExpressionType.Multiply:
                case ExpressionType.Subtract:
                    ModelClass.LazyInit(this, TypeProperty, () => TypeHelpers.GetCommonType(this.Left.Type, this.Right.Type));
                    ModelClass.LazyInit(this.Left, ExpectedTypeProperty, () => this.ExpectedType);
                    ModelClass.LazyInit(this.Right, ExpectedTypeProperty, () => this.ExpectedType);
                    break;
                case ExpressionType.Equal:
                case ExpressionType.GreaterThan:
                case ExpressionType.GreaterThanOrEqual:
                case ExpressionType.LessThan:
                case ExpressionType.LessThanOrEqual:
                case ExpressionType.NotEqual:
                    ModelClass.LazyInit(this, TypeProperty, () => BuiltInType.Bool);
                    ModelClass.LazyInit(this.Left, ExpectedTypeProperty, () => this.Right.Type);
                    ModelClass.LazyInit(this.Right, ExpectedTypeProperty, () => this.Left.Type);
                    break;
                case ExpressionType.And:
                case ExpressionType.AndAlso:
                case ExpressionType.ExclusiveOr:
                case ExpressionType.Or:
                case ExpressionType.OrElse:
                    ModelClass.LazyInit(this, TypeProperty, () => BuiltInType.Bool);
                    ModelClass.LazyInit(this.Left, ExpectedTypeProperty, () => BuiltInType.Bool);
                    ModelClass.LazyInit(this.Right, ExpectedTypeProperty, () => BuiltInType.Bool);
                    break;
                case ExpressionType.Coalesce:
                    ModelClass.LazyInit(this, TypeProperty, () => TypeHelpers.GetCommonType(((NullableType)this.Left.Type).InnerType, this.Right.Type));
                    ModelClass.LazyInit(this.Left, ExpectedTypeProperty, () => NullableType.CreateFrom((Type)this.ExpectedType));
                    ModelClass.LazyInit(this.Right, ExpectedTypeProperty, () => this.ExpectedType);
                    break;
                default:
                    throw new SoaModelException("Invalid binary expression type: " + nodeType.ToString());
            }
            ModelClass.LazyInit(this, ValueProperty, () => { throw new EvaluationException(this); });

            this.Children.Add(this.Left);
            this.Children.Add(this.Right);
        }
예제 #11
0
        public UnaryExpression(ExpressionType nodeType, Expression operand, Type type = null)
            : base(nodeType)
        {
            this.Operand = operand;

            switch (nodeType)
            {
                case ExpressionType.Negate:
                case ExpressionType.UnaryPlus:
                case ExpressionType.OnesComplement:
                    ModelClass.LazyInit(this, TypeProperty, () => this.Operand.Type);
                    ModelClass.LazyInit(this.Operand, ExpectedTypeProperty, () => this.ExpectedType);
                    break;
                case ExpressionType.Not:
                    ModelClass.LazyInit(this, TypeProperty, () => BuiltInType.Bool);
                    ModelClass.LazyInit(this.Operand, ExpectedTypeProperty, () => BuiltInType.Bool);
                    break;
                case ExpressionType.Convert:
                case ExpressionType.TypeAs:
                    ModelClass.LazyInit(this, TypeProperty, () => type);
                    ModelClass.LazyInit(this.Operand, ExpectedTypeProperty, () => PseudoType.Object);
                    break;
                default:
                    throw new SoaModelException("Invalid unary expression type: " + nodeType.ToString());
            }
            ModelClass.LazyInit(this, ValueProperty, () => { throw new EvaluationException(this); });

            this.Children.Add(this.Operand);
        }
예제 #12
0
        public TypeBinaryExpression(Expression expression, Type typeOperand)
            : base(ExpressionType.TypeIs)
        {
            this.Expression = expression;
            this.TypeOperand = typeOperand;

            ModelClass.LazyInit(this, TypeProperty, () => BuiltInType.Bool);
            ModelClass.LazyInit(this.Expression, ExpectedTypeProperty, () => PseudoType.Object);
            ModelClass.LazyInit(this, ValueProperty, () => { throw new EvaluationException(this); });

            this.Children.Add(this.Expression);
        }