Пример #1
0
 private static bool MatchConditional(ConditionalExpression e1,
                                      ConditionalExpression e2,
                                      Substitution subst)
 {
     return(Match(e1.Test, e2.Test, subst) &&
            Match(e1.IfTrue, e2.IfTrue, subst) &&
            Match(e1.IfFalse, e2.IfFalse, subst));
 }
Пример #2
0
 private static bool MatchBinary(BinaryExpression e1,
                                 BinaryExpression e2,
                                 Substitution subst)
 {
     return(e1.NodeType == e2.NodeType &&
            Match(e1.Left, e2.Left, subst) &&
            Match(e1.Right, e2.Right, subst));
 }
Пример #3
0
 public static bool MatchTypeBinary(TypeBinaryExpression e1,
                                    TypeBinaryExpression e2,
                                    Substitution subst)
 {
     return(e1.NodeType == ExpressionType.TypeIs &&
            e2.NodeType == ExpressionType.TypeIs &&
            e1.TypeOperand.Equals(e2.TypeOperand) &&
            Match(e1.Expression, e2.Expression, subst));
 }
Пример #4
0
        internal override PartitionInfo Rewrite(LambdaExpression resultSel, ParameterExpression param)
        {
            ParameterExpression a         = this.m_keySelector.Parameters[0];
            Substitution        pSubst    = Substitution.Empty.Cons(a, param);
            LambdaExpression    newKeySel = HpcLinqExpression.Rewrite(this.m_keySelector, resultSel, pSubst);

            if (newKeySel == null)
            {
                return(new RandomPartition(this.m_count));
            }
            return(this.Create(newKeySel));
        }
Пример #5
0
        public static bool MatchParameter(ParameterExpression e1,
                                          ParameterExpression e2,
                                          Substitution subst)
        {
            if (e1.Equals(e2))
            {
                return(true);
            }
            ParameterExpression e = subst.Find(e1);

            return(e != null && e.Equals(e2));
        }
Пример #6
0
 private static bool MatchConstant(ConstantExpression e1,
                                   ConstantExpression e2,
                                   Substitution subst)
 {
     if (e1.Value == null)
     {
         return(e2.Value == null);
     }
     else
     {
         return(e1.Value.Equals(e2.Value));
     }
 }
Пример #7
0
        public ParameterExpression Find(ParameterExpression a)
        {
            Substitution curSubst = this;

            while (curSubst != Empty)
            {
                if (curSubst.x.Equals(a))
                {
                    return(y);
                }
                curSubst = curSubst.next;
            }
            return(null);
        }
Пример #8
0
 private static bool MatchNew(NewExpression e1, NewExpression e2, Substitution subst)
 {
     if (e1.Arguments.Count != e2.Arguments.Count)
     {
         return(false);
     }
     for (int i = 0, n = e1.Arguments.Count; i < n; i++)
     {
         if (!Match(e1.Arguments[i], e2.Arguments[i], subst))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #9
0
 private static bool MatchMemberBinding(MemberBinding b1, MemberBinding b2, Substitution subst)
 {
     if (b1.BindingType != b2.BindingType ||
         !b1.Member.Equals(b2.Member))
     {
         return(false);
     }
     if (b1 is MemberAssignment)
     {
         return(Match(((MemberAssignment)b1).Expression,
                      ((MemberAssignment)b2).Expression,
                      subst));
     }
     else if (b1 is MemberMemberBinding)
     {
         MemberMemberBinding mmb1 = (MemberMemberBinding)b1;
         MemberMemberBinding mmb2 = (MemberMemberBinding)b2;
         if (mmb1.Bindings.Count != mmb2.Bindings.Count)
         {
             return(false);
         }
         for (int i = 0, n = mmb1.Bindings.Count; i < n; i++)
         {
             if (!MatchMemberBinding(mmb1.Bindings[i], mmb2.Bindings[i], subst))
             {
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         MemberListBinding mlb1 = (MemberListBinding)b1;
         MemberListBinding mlb2 = (MemberListBinding)b2;
         if (mlb1.Initializers.Count != mlb2.Initializers.Count)
         {
             return(false);
         }
         for (int i = 0, n = mlb1.Initializers.Count; i < n; i++)
         {
             if (!MatchElementInit(mlb1.Initializers[i], mlb2.Initializers[i], subst))
             {
                 return(false);
             }
         }
         return(true);
     }
 }
Пример #10
0
 public static bool MatchNewArray(NewArrayExpression e1,
                                  NewArrayExpression e2,
                                  Substitution subst)
 {
     if (e1.NodeType != e2.NodeType ||
         e1.Expressions.Count != e2.Expressions.Count)
     {
         return(false);
     }
     for (int i = 0, n = e1.Expressions.Count; i < n; i++)
     {
         if (!Match(e1.Expressions[i], e2.Expressions[i], subst))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #11
0
 public static bool MatchMemberInit(MemberInitExpression e1,
                                    MemberInitExpression e2,
                                    Substitution subst)
 {
     if (!Match(e1.NewExpression, e2.NewExpression, subst) ||
         e1.Bindings.Count != e2.Bindings.Count)
     {
         return(false);
     }
     for (int i = 0, n = e1.Bindings.Count; i < n; i++)
     {
         if (!MatchMemberBinding(e1.Bindings[i], e2.Bindings[i], subst))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #12
0
 private static bool MatchElementInit(ElementInit init1,
                                      ElementInit init2,
                                      Substitution subst)
 {
     if (init1.AddMethod != init2.AddMethod ||
         init1.Arguments.Count != init2.Arguments.Count)
     {
         return(false);
     }
     for (int i = 0; i < init1.Arguments.Count; i++)
     {
         if (!Match(init1.Arguments[i], init2.Arguments[i], subst))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #13
0
        private static bool MatchLambda(LambdaExpression e1,
                                        LambdaExpression e2,
                                        Substitution subst)
        {
            if (e1.Parameters.Count != e2.Parameters.Count)
            {
                return(false);
            }
            Substitution subst1 = subst;

            for (int i = 0, n = e1.Parameters.Count; i < n; i++)
            {
                if (!e1.Parameters[i].Equals(e2.Parameters[i]))
                {
                    subst1 = subst1.Cons(e1.Parameters[i], e2.Parameters[i]);
                }
            }
            return(Match(e1.Body, e2.Body, subst1));
        }
Пример #14
0
        private static bool MatchInvocation(InvocationExpression e1,
                                            InvocationExpression e2,
                                            Substitution subst)
        {
            ReadOnlyCollection <Expression> args1 = e1.Arguments;
            ReadOnlyCollection <Expression> args2 = e2.Arguments;

            if (!Match(e1.Expression, e2.Expression, subst) || args1.Count != args2.Count)
            {
                return(false);
            }
            for (int i = 0; i < args1.Count; i++)
            {
                if (!Match(args1[i], args2[i], subst))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #15
0
 private static bool MatchMember(MemberExpression e1,
                                 MemberExpression e2,
                                 Substitution subst)
 {
     if (e1.Expression == null)
     {
         if (e2.Expression != null)
         {
             return(false);
         }
     }
     else
     {
         if (e2.Expression == null ||
             !Match(e1.Expression, e2.Expression, subst))
         {
             return(false);
         }
     }
     return(e1.Member.Equals(e2.Member));
 }
Пример #16
0
 public static bool MatchListInit(ListInitExpression e1,
                                  ListInitExpression e2,
                                  Substitution subst)
 {
     if (!Match(e1.NewExpression, e2.NewExpression, subst))
     {
         return(false);
     }
     if (e1.Initializers.Count != e2.Initializers.Count)
     {
         return(false);
     }
     for (int i = 0, n = e1.Initializers.Count; i < n; i++)
     {
         ElementInit init1 = e1.Initializers[i];
         ElementInit init2 = e2.Initializers[i];
         if (!MatchElementInit(init1, init2, subst))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #17
0
 private static bool MatchLambda(LambdaExpression e1,
                                 LambdaExpression e2,
                                 Substitution subst)
 {
     if (e1.Parameters.Count != e2.Parameters.Count)
     {
         return false;
     }
     Substitution subst1 = subst;
     for (int i = 0, n = e1.Parameters.Count; i < n; i++)
     {
         if (!e1.Parameters[i].Equals(e2.Parameters[i]))
         {
             subst1 = subst1.Cons(e1.Parameters[i], e2.Parameters[i]);
         }
     }
     return Match(e1.Body, e2.Body, subst1);
 }
Пример #18
0
 private static bool MatchMember(MemberExpression e1,
                                 MemberExpression e2,
                                 Substitution subst)
 {
     if (e1.Expression == null)
     {
         if (e2.Expression != null) return false;
     }
     else
     {
         if (e2.Expression == null ||
             !Match(e1.Expression, e2.Expression, subst))
         {
             return false;
         }
     }
     return e1.Member.Equals(e2.Member);
 }
Пример #19
0
        private static bool MatchMethodCall(MethodCallExpression e1,
                                            MethodCallExpression e2,
                                            Substitution subst)
        {
            if (e1.Method != e2.Method) return false;

            if (e1.Object == null || e2.Object == null)
            {
                if (e1.Object != e2.Object) return false;
            }
            else if (!Match(e1.Object, e2.Object, subst) ||
                     e1.Arguments.Count != e2.Arguments.Count)
            {
                return false;
            }
            for (int i = 0, n = e1.Arguments.Count; i < n; i++)
            {
                if (!Match(e1.Arguments[i], e2.Arguments[i], subst))
                {
                    return false;
                }
            }
            return true;
        }
Пример #20
0
 public static bool MatchUnary(UnaryExpression e1, UnaryExpression e2, Substitution subst)
 {
     return(e1.NodeType == e2.NodeType &&
            Match(e1.Operand, e2.Operand, subst));
 }
Пример #21
0
 private static bool MatchElementInit(ElementInit init1,
                                      ElementInit init2,
                                      Substitution subst)
 {
     if (init1.AddMethod != init2.AddMethod ||
         init1.Arguments.Count != init2.Arguments.Count)
     {
         return false;
     }
     for (int i = 0; i < init1.Arguments.Count; i++)
     {
         if (!Match(init1.Arguments[i], init2.Arguments[i], subst))
         {
             return false;
         }
     }
     return true;
 }
Пример #22
0
        public static bool MatchListInit(ListInitExpression e1,
                                         ListInitExpression e2,
                                         Substitution subst)
        {
            if (!Match(e1.NewExpression, e2.NewExpression, subst))
            {
                return false;
            }
            if (e1.Initializers.Count != e2.Initializers.Count)
            {
                return false;
            }
            for (int i = 0, n = e1.Initializers.Count; i < n; i++)
	    {
                ElementInit init1 = e1.Initializers[i];
                ElementInit init2 = e2.Initializers[i];
                if (!MatchElementInit(init1, init2, subst))
                {
                    return false;
                }
            }
            return true;
        }
Пример #23
0
        public static bool MatchMemberInit(MemberInitExpression e1,
                                           MemberInitExpression e2,
                                           Substitution subst)
        {
            if (!Match(e1.NewExpression, e2.NewExpression, subst) ||
                e1.Bindings.Count != e2.Bindings.Count)
            {
                return false;
            }
            for (int i = 0, n = e1.Bindings.Count; i < n; i++)
	    {
                if (!MatchMemberBinding(e1.Bindings[i], e2.Bindings[i], subst))
                {
                    return false;
                }
            }
            return true;
        }
Пример #24
0
 public static bool MatchParameter(ParameterExpression e1,
                                   ParameterExpression e2,
                                   Substitution subst)
 {
     if (e1.Equals(e2)) return true;
     ParameterExpression e = subst.Find(e1);
     return (e != null && e.Equals(e2));
 }
Пример #25
0
 private static bool MatchBinary(BinaryExpression e1,
                                 BinaryExpression e2,
                                 Substitution subst)
 {
     return (e1.NodeType == e2.NodeType &&
             Match(e1.Left, e2.Left, subst) &&
             Match(e1.Right, e2.Right, subst));
 }
Пример #26
0
 public static bool MatchTypeBinary(TypeBinaryExpression e1,
                                    TypeBinaryExpression e2,
                                    Substitution subst)
 {
     return (e1.NodeType == ExpressionType.TypeIs &&
             e2.NodeType == ExpressionType.TypeIs &&
             e1.TypeOperand.Equals(e2.TypeOperand) &&
             Match(e1.Expression, e2.Expression, subst));
 }
Пример #27
0
 public static bool MatchUnary(UnaryExpression e1, UnaryExpression e2, Substitution subst)
 {
     return (e1.NodeType == e2.NodeType &&
             Match(e1.Operand, e2.Operand, subst));
 }
Пример #28
0
 private Substitution(ParameterExpression x, ParameterExpression y, Substitution s)
 {
     this.x = x;
     this.y = y;
     this.next = s;
 }
Пример #29
0
 private static bool MatchConstant(ConstantExpression e1,
                                   ConstantExpression e2,
                                   Substitution subst)
 {
     if (e1.Value == null)
     {
         return (e2.Value == null);
     }
     else
     {
         return e1.Value.Equals(e2.Value);
     }
 }
Пример #30
0
 public static bool MatchNewArray(NewArrayExpression e1,
                                  NewArrayExpression e2,
                                  Substitution subst)
 {
     if (e1.NodeType != e2.NodeType ||
         e1.Expressions.Count != e2.Expressions.Count)
     {
         return false;
     }
     for (int i = 0, n = e1.Expressions.Count; i < n; i++)
     {
         if (!Match(e1.Expressions[i], e2.Expressions[i], subst))
         {
             return false;
         }
     }
     return true;
 }
Пример #31
0
 private static bool MatchConditional(ConditionalExpression e1,
                                      ConditionalExpression e2,
                                      Substitution subst)
 {
     return (Match(e1.Test, e2.Test, subst) &&
             Match(e1.IfTrue, e2.IfTrue, subst) &&
             Match(e1.IfFalse, e2.IfFalse, subst));
 }
Пример #32
0
 private static bool MatchNew(NewExpression e1, NewExpression e2, Substitution subst)
 {
     if (e1.Arguments.Count != e2.Arguments.Count)
     {
         return false;
     }
     for (int i = 0, n = e1.Arguments.Count; i < n; i++)
     {
         if (!Match(e1.Arguments[i], e2.Arguments[i], subst))
         {
             return false;
         }
     }
     return true;
 }
Пример #33
0
        internal static bool Match(Expression e1, Expression e2, Substitution subst)
        {
            if (!e1.Type.Equals(e2.Type)) return false;
            
            if (e1 is BinaryExpression)
            {
                return ((e2 is BinaryExpression) &&
                        MatchBinary((BinaryExpression)e1, (BinaryExpression)e2, subst));
            }
            else if (e1 is ConditionalExpression)
            {
                return ((e2 is ConditionalExpression) &&
                        MatchConditional((ConditionalExpression)e1, (ConditionalExpression)e2, subst));
            }
            else if (e1 is ConstantExpression)
            {
                return ((e2 is ConstantExpression) &&
                        MatchConstant((ConstantExpression)e1, (ConstantExpression)e2, subst));
            }
            else if (e1 is InvocationExpression)
            {
                return ((e2 is InvocationExpression) &&
                        MatchInvocation((InvocationExpression)e1, (InvocationExpression)e2, subst));
            }
            else if (e1 is LambdaExpression)
            {
                return ((e2 is LambdaExpression) &&
                        MatchLambda((LambdaExpression)e1, (LambdaExpression)e2, subst));
            }
            else if (e1 is MemberExpression)
            {
                return ((e2 is MemberExpression) &&
                        MatchMember((MemberExpression)e1, (MemberExpression)e2, subst));
            }
            else if (e1 is MethodCallExpression)
            {
                return ((e2 is MethodCallExpression) &&
                        MatchMethodCall((MethodCallExpression)e1, (MethodCallExpression)e2, subst));
            }
            else if (e1 is NewExpression)
            {
                return ((e2 is NewExpression) &&
                        MatchNew((NewExpression)e1, (NewExpression)e2, subst));
            }
            else if (e1 is NewArrayExpression)
            {
                return ((e2 is NewArrayExpression) &&
                        MatchNewArray((NewArrayExpression)e1, (NewArrayExpression)e2, subst));
            }
            else if (e1 is MemberInitExpression)
            {
                return ((e2 is MemberInitExpression) &&
                        MatchMemberInit((MemberInitExpression)e1, (MemberInitExpression)e2, subst));
            }
            else if (e1 is ListInitExpression)
            {
                return ((e2 is ListInitExpression) &&
                        MatchListInit((ListInitExpression)e1, (ListInitExpression)e2, subst));
            }
            else if (e1 is ParameterExpression)
            {
                return ((e2 is ParameterExpression) &&
                        MatchParameter((ParameterExpression)e1, (ParameterExpression)e2, subst));
            }
            else if (e1 is TypeBinaryExpression)
            {
                return ((e2 is TypeBinaryExpression) &&
                        MatchTypeBinary((TypeBinaryExpression)e1, (TypeBinaryExpression)e2, subst));
            }
            else if (e1 is UnaryExpression)
            {
                return ((e2 is UnaryExpression) &&
                        MatchUnary((UnaryExpression)e1, (UnaryExpression)e2, subst));
            }

            throw new DryadLinqException(DryadLinqErrorCode.ExpressionTypeNotHandled,
                                         String.Format(SR.ExpressionTypeNotHandled,
                                                       "ExpressionMatcher", e1.NodeType));
        }
Пример #34
0
 private static bool MatchMemberBinding(MemberBinding b1, MemberBinding b2, Substitution subst)
 {
     if (b1.BindingType != b2.BindingType ||
         !b1.Member.Equals(b2.Member))
     {
         return false;
     }
     if (b1 is MemberAssignment)
     {
         return Match(((MemberAssignment)b1).Expression,
                      ((MemberAssignment)b2).Expression,
                      subst);
     }
     else if (b1 is MemberMemberBinding)
     {
         MemberMemberBinding mmb1 = (MemberMemberBinding)b1;
         MemberMemberBinding mmb2 = (MemberMemberBinding)b2;
         if (mmb1.Bindings.Count != mmb2.Bindings.Count)
         {
             return false;
         }
         for (int i = 0, n = mmb1.Bindings.Count; i < n; i++)
         {
             if (!MatchMemberBinding(mmb1.Bindings[i], mmb2.Bindings[i], subst))
             {
                 return false;
             }
         }
         return true;
     }
     else
     {
         MemberListBinding mlb1 = (MemberListBinding)b1;
         MemberListBinding mlb2 = (MemberListBinding)b2;
         if (mlb1.Initializers.Count != mlb2.Initializers.Count)
         {
             return false;
         }
         for (int i = 0, n = mlb1.Initializers.Count; i < n; i++)
         {
             if (!MatchElementInit(mlb1.Initializers[i], mlb2.Initializers[i], subst))
             {
                 return false;
             }
         }
         return true;
     }
 }
Пример #35
0
 private Substitution(ParameterExpression x, ParameterExpression y, Substitution s)
 {
     this.x    = x;
     this.y    = y;
     this.next = s;
 }
Пример #36
0
 internal ExpressionSubst(Substitution paramSubst)
 {
     this.m_paramSubst = paramSubst;
     this.m_leftExprList = new List<Expression>(2);
     this.m_rightExprList = new List<Expression>(2);
 }
Пример #37
0
        internal static bool Match(Expression e1, Expression e2, Substitution subst)
        {
            if (!e1.Type.Equals(e2.Type))
            {
                return(false);
            }

            if (e1 is BinaryExpression)
            {
                return((e2 is BinaryExpression) &&
                       MatchBinary((BinaryExpression)e1, (BinaryExpression)e2, subst));
            }
            else if (e1 is ConditionalExpression)
            {
                return((e2 is ConditionalExpression) &&
                       MatchConditional((ConditionalExpression)e1, (ConditionalExpression)e2, subst));
            }
            else if (e1 is ConstantExpression)
            {
                return((e2 is ConstantExpression) &&
                       MatchConstant((ConstantExpression)e1, (ConstantExpression)e2, subst));
            }
            else if (e1 is InvocationExpression)
            {
                return((e2 is InvocationExpression) &&
                       MatchInvocation((InvocationExpression)e1, (InvocationExpression)e2, subst));
            }
            else if (e1 is LambdaExpression)
            {
                return((e2 is LambdaExpression) &&
                       MatchLambda((LambdaExpression)e1, (LambdaExpression)e2, subst));
            }
            else if (e1 is MemberExpression)
            {
                return((e2 is MemberExpression) &&
                       MatchMember((MemberExpression)e1, (MemberExpression)e2, subst));
            }
            else if (e1 is MethodCallExpression)
            {
                return((e2 is MethodCallExpression) &&
                       MatchMethodCall((MethodCallExpression)e1, (MethodCallExpression)e2, subst));
            }
            else if (e1 is NewExpression)
            {
                return((e2 is NewExpression) &&
                       MatchNew((NewExpression)e1, (NewExpression)e2, subst));
            }
            else if (e1 is NewArrayExpression)
            {
                return((e2 is NewArrayExpression) &&
                       MatchNewArray((NewArrayExpression)e1, (NewArrayExpression)e2, subst));
            }
            else if (e1 is MemberInitExpression)
            {
                return((e2 is MemberInitExpression) &&
                       MatchMemberInit((MemberInitExpression)e1, (MemberInitExpression)e2, subst));
            }
            else if (e1 is ListInitExpression)
            {
                return((e2 is ListInitExpression) &&
                       MatchListInit((ListInitExpression)e1, (ListInitExpression)e2, subst));
            }
            else if (e1 is ParameterExpression)
            {
                return((e2 is ParameterExpression) &&
                       MatchParameter((ParameterExpression)e1, (ParameterExpression)e2, subst));
            }
            else if (e1 is TypeBinaryExpression)
            {
                return((e2 is TypeBinaryExpression) &&
                       MatchTypeBinary((TypeBinaryExpression)e1, (TypeBinaryExpression)e2, subst));
            }
            else if (e1 is UnaryExpression)
            {
                return((e2 is UnaryExpression) &&
                       MatchUnary((UnaryExpression)e1, (UnaryExpression)e2, subst));
            }

            throw new DryadLinqException(HpcLinqErrorCode.ExpressionTypeNotHandled,
                                         String.Format(SR.ExpressionTypeNotHandled,
                                                       "ExpressionMatcher", e1.NodeType));
        }
Пример #38
0
        public static LambdaExpression Rewrite(LambdaExpression expr, LambdaExpression selector, Substitution pSubst)
        {
            if (expr != null)
            {
                Type resultType = selector.Body.Type;
                ParameterExpression resultParam = Expression.Parameter(resultType, "key_1");

                // Perform substitutions
                ExpressionSubst subst = new ExpressionSubst(pSubst);
                subst.AddSubst(selector.Body, resultParam);
                if (selector.Body is NewExpression)
                {
                    NewExpression newBody = (NewExpression)selector.Body;
                    if (newBody.Constructor != null)
                    {
                        resultType = newBody.Constructor.DeclaringType;
                    }
                    if (TypeSystem.IsAnonymousType(resultType))
                    {
                        PropertyInfo[] props = resultType.GetProperties();

                        //the following test is never expected to occur, and an assert would most likely suffice.
                        if (props.Length != newBody.Arguments.Count)
                        {
                            throw new DryadLinqException(DryadLinqErrorCode.Internal, SR.BugInHandlingAnonymousClass);
                        }

                        for (int i = 0; i < props.Length; i++)
                        {
                            Expression leftExpr = newBody.Arguments[i];
                            Expression rightExpr = CreateMemberAccess(resultParam, props[i].Name);
                            subst.AddSubst(leftExpr, rightExpr);
                        }
                    }
                }
                if (selector.Body is MemberInitExpression)
                {
                    ReadOnlyCollection<MemberBinding> bindings = ((MemberInitExpression)selector.Body).Bindings;
                    for (int i = 0; i < bindings.Count; i++)
                    {
                        if (bindings[i] is MemberAssignment)
                        {
                            Expression leftExpr = ((MemberAssignment)bindings[i]).Expression;
                            Expression rightExpr = CreateMemberAccess(resultParam, ((MemberAssignment)bindings[i]).Member.Name);
                            subst.AddSubst(leftExpr, rightExpr);
                        }
                    }
                }
                else
                {
                    FieldMappingAttribute[] attribs = AttributeSystem.GetFieldMappingAttribs(selector);
                    if (attribs != null)
                    {
                        foreach (FieldMappingAttribute attrib in attribs)
                        {
                            string[] srcFieldNames = attrib.Source.Split('.');
                            string paramName = srcFieldNames[0];

                            ParameterInfo[] paramInfos = null;
                            if (selector.Body is MethodCallExpression)
                            {
                                paramInfos = ((MethodCallExpression)selector.Body).Method.GetParameters();
                            }
                            else if (selector.Body is NewExpression)
                            {
                                paramInfos = ((NewExpression)selector.Body).Constructor.GetParameters();
                            }

                            if (paramInfos != null)
                            {
                                int argIdx = -1;
                                for (int i = 0; i < paramInfos.Length; i++)
                                {
                                    if (paramInfos[i].Name == paramName)
                                    {
                                        argIdx = i;
                                        break;
                                    }
                                }

                                Expression leftExpr = null;
                                if (argIdx != -1)
                                {
                                    if (selector.Body is MethodCallExpression)
                                    {
                                        leftExpr = ((MethodCallExpression)selector.Body).Arguments[argIdx];
                                    }
                                    else if (selector.Body is NewExpression)
                                    {
                                        leftExpr = ((NewExpression)selector.Body).Arguments[argIdx];
                                    }
                                }
                                if (leftExpr == null)
                                {
                                    throw new DryadLinqException(DryadLinqErrorCode.Internal,
                                                               "The source of the FieldMapping annotation was wrong. " +
                                                               paramName + " is not a formal parameter.");
                                }

                                string[] fieldNames = new string[srcFieldNames.Length - 1];
                                for (int i = 1; i < srcFieldNames.Length; i++)
                                {
                                    fieldNames[i] = srcFieldNames[i-1];
                                }
                                leftExpr = CreateMemberAccess(leftExpr, fieldNames);
                                Expression rightExpr = CreateMemberAccess(resultParam, attrib.Destination.Split('.'));
                                subst.AddSubst(leftExpr, rightExpr);
                            }
                        }
                    }
                }
                Expression resultBody = subst.Visit(expr.Body);

                // Check if the substitutions are complete
                FreeParameters freeParams = new FreeParameters();
                freeParams.Visit(resultBody);
                if (freeParams.Parameters.Count == 1 && freeParams.Parameters.Contains(resultParam))
                {
                    Type funcType = typeof(Func<,>).MakeGenericType(resultType, expr.Body.Type);
                    return Expression.Lambda(funcType, resultBody, resultParam);
                }
            }
            return null;
        }
Пример #39
0
 internal ExpressionSubst(Substitution paramSubst)
 {
     this.m_paramSubst    = paramSubst;
     this.m_leftExprList  = new List <Expression>(2);
     this.m_rightExprList = new List <Expression>(2);
 }
Пример #40
0
 private static bool MatchInvocation(InvocationExpression e1,
                                     InvocationExpression e2,
                                     Substitution subst)
 {
     ReadOnlyCollection<Expression> args1 = e1.Arguments;
     ReadOnlyCollection<Expression> args2 = e2.Arguments;
     if (!Match(e1.Expression, e2.Expression, subst) || args1.Count != args2.Count)
     {
         return false;
     }
     for (int i = 0; i < args1.Count; i++)
     {
         if (!Match(args1[i], args2[i], subst))
         {
             return false;
         }
     }
     return true;
 }