/// <summary> /// Checks whether the specified <see cref="MethodCallExpression"/> refers /// to a Select method call that works on the results of another Select call /// </summary> /// <param name="call">Method call expression to check.</param> /// <param name="type">Type of the projection</param> internal static void CheckChainedSequence(MethodCallExpression call, Type type) { if (ReflectionUtil.IsSequenceMethod(call.Method, SequenceMethod.Select)) { // Chained Selects are not allowed // c.Orders.Select(...).Select(...) MethodCallExpression insideCall = ResourceBinder.StripTo <MethodCallExpression>(call.Arguments[0]); if (insideCall != null && ReflectionUtil.IsSequenceMethod(insideCall.Method, SequenceMethod.Select)) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(type, call.ToString())); } } }
internal override Expression VisitParameter(ParameterExpression p) { if (ClientTypeUtil.TypeOrElementTypeIsEntity(p.Type)) { if (p != box.ParamExpressionInScope) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, p.ToString())); } this.box.StartNewPath(); } return(p); }
internal override Expression VisitUnary(UnaryExpression u) { Debug.Assert(u != null, "u != null"); if (!ResourceBinder.PatternRules.MatchConvertToAssignable(u)) { if (ClientType.CheckElementTypeIsEntity(u.Operand.Type)) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, u.ToString())); } } return(base.VisitUnary(u)); }
internal static void Analyze(MemberInitExpression mie, PathBox pb) { Debug.Assert(mie != null, "mie != null"); var epa = new EntityProjectionAnalyzer(pb, mie.Type); MemberAssignmentAnalysis targetEntityPath = null; foreach (MemberBinding mb in mie.Bindings) { MemberAssignment ma = mb as MemberAssignment; epa.Visit(ma.Expression); if (ma != null) { var analysis = MemberAssignmentAnalysis.Analyze(pb.ParamExpressionInScope, ma.Expression); if (analysis.IncompatibleAssignmentsException != null) { throw analysis.IncompatibleAssignmentsException; } Type targetType = GetMemberType(ma.Member); Expression[] lastExpressions = analysis.GetExpressionsBeyondTargetEntity(); if (lastExpressions.Length == 0) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(targetType, ma.Expression)); } MemberExpression lastExpression = lastExpressions[lastExpressions.Length - 1] as MemberExpression; Debug.Assert( !analysis.MultiplePathsFound, "!analysis.MultiplePathsFound -- the initilizer has been visited, and cannot be empty, and expressions that can combine paths should have thrown exception during initializer analysis"); Debug.Assert( lastExpression != null, "lastExpression != null -- the initilizer has been visited, and cannot be empty, and the only expressions that are allowed can be formed off the parameter, so this is always correlatd"); if (lastExpression != null && (lastExpression.Member.Name != ma.Member.Name)) { throw new NotSupportedException(Strings.ALinq_PropertyNamesMustMatchInProjections(lastExpression.Member.Name, ma.Member.Name)); } analysis.CheckCompatibleAssignments(mie.Type, ref targetEntityPath); bool targetIsEntity = ClientType.CheckElementTypeIsEntity(targetType); bool sourceIsEntity = ClientType.CheckElementTypeIsEntity(lastExpression.Type); if (sourceIsEntity && !targetIsEntity) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(targetType, ma.Expression)); } } } }
internal override Expression VisitMethodCall(MethodCallExpression m) { if ((m.Object != null && IsDisallowedExpressionForMethodCall(m.Object)) || m.Arguments.Any(a => IsDisallowedExpressionForMethodCall(a))) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, m.ToString())); } if (ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m)) { CheckChainedSequence(m, this.type); return(base.VisitMethodCall(m)); } throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(this.type, m.ToString())); }
/// <summary> /// Visits a member access expression in non-entity projections, validating that /// it's correct and recording the path visit to include in a projection if necessary. /// </summary> /// <param name="m">Expression to visit.</param> /// <returns>The same expression.</returns> /// <remarks> /// The projection analyzer runs after funcletization, so a member expression /// rather than a constant expression implies that this is correlated to /// a parameter, by dotting through the argument in valid cases, and possibly /// more complex cases in others like new DSC(p.Orders)*.Foo* <- .Foo is invalid. /// </remarks> internal override Expression VisitMemberAccess(MemberExpression m) { Debug.Assert(m != null, "m != null"); Type expressionType = m.Expression.Type; this.leafExpressionIsMemberAccess = true; // if primitive or nullable primitive, allow member access... i.e. calling Value on nullable<int> if (PrimitiveType.IsKnownNullableType(expressionType)) { this.leafExpressionIsMemberAccess = false; return(base.VisitMemberAccess(m)); } // Only allowed to project entities, also it is ok to do client side projections on complex types. // Details on the fix for the Dev11 bug 350541 "Inconsistency between Count() method call and Count property projection on clr type collections": // Relax check to only throw if IsCollectionProducingExpression returns true. // This enables client side projections (for example "Count") on Clr type collections, like ReadOnlyCollection (which is used in spatial types), ICollection, IList, etc. // We already allow client side method calls (like Linq extension method "Count()") on clr type collections, so it makes client side projections consistent. // Note: it will still throw for List<T> (because IsCollectionProducingExpression returns true for List<T>), // however this is consistent with how we handle MethodCallExpression on clr type collections // and changing IsCollectionProducingExpression seems risky at this point as it's used in a lot of places. if (IsCollectionProducingExpression(m.Expression)) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, m.ToString())); } PropertyInfo pi; Expression boundTarget; if (ResourceBinder.PatternRules.MatchNonPrivateReadableProperty(m, out pi, out boundTarget)) { Expression e = base.VisitMemberAccess(m); if (ClientTypeUtil.TypeOrElementTypeIsEntity(expressionType)) { Type convertedType; ResourceBinder.StripTo <Expression>(m.Expression, out convertedType); box.AppendPropertyToPath(pi, convertedType, this.context); this.leafExpressionIsMemberAccess = false; } return(e); } throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, m.ToString())); }
internal override Expression VisitConditional(ConditionalExpression c) { var nullCheck = ResourceBinder.PatternRules.MatchNullCheck(this.box.ParamExpressionInScope, c); if (nullCheck.Match) { this.Visit(nullCheck.AssignExpression); return(c); } if (ClientType.CheckElementTypeIsEntity(c.Test.Type) || ClientType.CheckElementTypeIsEntity(c.IfTrue.Type) || ClientType.CheckElementTypeIsEntity(c.IfFalse.Type) || IsCollectionProducingExpression(c.Test) || IsCollectionProducingExpression(c.IfTrue) || IsCollectionProducingExpression(c.IfFalse)) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, c.ToString())); } return(base.VisitConditional(c)); }
internal override Expression VisitMethodCall(MethodCallExpression m) { if ((m.Object != null && IsDisallowedExpressionForMethodCall(m.Object)) || m.Arguments.Any(a => IsDisallowedExpressionForMethodCall(a))) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, m.ToString())); } if (ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m)) { CheckChainedSequence(m, this.type); // allow selects for following pattern: // Orders = c.Orders.Select(o=> new NarrowOrder {...}).ToList(); return(base.VisitMethodCall(m)); } throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(this.type, m.ToString())); }
/// <summary>Visits a unary expression while initializing a non-entity type structure.</summary> /// <param name="u">Expression to visit.</param> /// <returns>The visited expression.</returns> internal override Expression VisitUnary(UnaryExpression u) { Debug.Assert(u != null, "u != null"); if (!ResourceBinder.PatternRules.MatchConvertToAssignable(u)) { // In V3 while we support TypeAs conversions, we only support TypeAs before a MemberAccess and not TypeAs as the last operation // i.e. we support "Manager = (p as Employee).Manager" (see VisitMemberAccess for detail), but we dont support "Manager = (p as Manager)" // Note that the server also doesn't support a property path which ends with a type identifier. if (u.NodeType == ExpressionType.TypeAs && this.leafExpressionIsMemberAccess) { return(base.VisitUnary(u)); } if (ClientTypeUtil.TypeOrElementTypeIsEntity(u.Operand.Type)) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, u.ToString())); } } return(base.VisitUnary(u)); }
internal override Expression VisitMethodCall(MethodCallExpression m) { // We throw NotSupportedException when IsDisallowedExceptionForMethodCall() is true // or we have a method call on a non-entity type, for example c.MyCollectionComplexProperty.Select(...) if ((m.Object != null && (IsDisallowedExpressionForMethodCall(m.Object, this.context.Model) || !ClientTypeUtil.TypeOrElementTypeIsEntity(m.Object.Type))) || m.Arguments.Any(a => IsDisallowedExpressionForMethodCall(a, this.context.Model)) || (m.Object == null && !ClientTypeUtil.TypeOrElementTypeIsEntity(m.Arguments[0].Type))) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, m.ToString())); } if (ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m)) { CheckChainedSequence(m, this.type); // allow selects for following pattern: // Orders = c.Orders.Select(o=> new NarrowOrder {...}).ToList(); return(base.VisitMethodCall(m)); } throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(this.type, m.ToString())); }
internal override Expression VisitMethodCall(MethodCallExpression m) { if ((m.Object != null && IsDisallowedExpressionForMethodCall(m.Object, this.context.Model)) || m.Arguments.Any(a => IsDisallowedExpressionForMethodCall(a, this.context.Model))) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, m.ToString())); } CheckChainedSequence(m, this.type); if (ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m)) { // allow IEnum.Select and IEnum.ToList even if entity type. return(base.VisitMethodCall(m)); } if ((m.Object != null ? ClientTypeUtil.TypeOrElementTypeIsEntity(m.Object.Type) : false) || m.Arguments.Any(a => ClientTypeUtil.TypeOrElementTypeIsEntity(a.Type))) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(this.type, m.ToString())); } return(base.VisitMethodCall(m)); }
/// <summary>Analyzes the specified member-init expression.</summary> /// <param name="mie">Expression to analyze.</param> /// <param name="pb">Path-tracking object to store analysis in.</param> internal static void Analyze(MemberInitExpression mie, PathBox pb) { Debug.Assert(mie != null, "mie != null"); var epa = new EntityProjectionAnalyzer(pb, mie.Type); MemberAssignmentAnalysis targetEntityPath = null; foreach (MemberBinding mb in mie.Bindings) { MemberAssignment ma = mb as MemberAssignment; epa.Visit(ma.Expression); if (ma != null) { var analysis = MemberAssignmentAnalysis.Analyze(pb.ParamExpressionInScope, ma.Expression); if (analysis.IncompatibleAssignmentsException != null) { throw analysis.IncompatibleAssignmentsException; } // Note that an "empty" assignment on the binding is not checked/handled, // because the funcletizer would have turned that into a constant // in the tree, the visit earlier in this method would have thrown // an exception at finding a constant in an entity projection. // // We do account however for failing to find a reference off the // parameter entry to detect errors like this: new ET() { Ref = e } // Here it looks like the new ET should be the parent of 'e', but // there is nothing in scope that represents that. // // This also explains while error messages might be a bit misleading // in this case (because they reference a constant when the user // hasn't included any). Type targetType = GetMemberType(ma.Member); Expression[] lastExpressions = analysis.GetExpressionsBeyondTargetEntity(); if (lastExpressions.Length == 0) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(targetType, ma.Expression)); } MemberExpression lastExpression = lastExpressions[lastExpressions.Length - 1] as MemberExpression; Debug.Assert( !analysis.MultiplePathsFound, "!analysis.MultiplePathsFound -- the initilizer has been visited, and cannot be empty, and expressions that can combine paths should have thrown exception during initializer analysis"); Debug.Assert( lastExpression != null, "lastExpression != null -- the initilizer has been visited, and cannot be empty, and the only expressions that are allowed can be formed off the parameter, so this is always correlatd"); if (lastExpression != null && (lastExpression.Member.Name != ma.Member.Name)) { throw new NotSupportedException(Strings.ALinq_PropertyNamesMustMatchInProjections(lastExpression.Member.Name, ma.Member.Name)); } analysis.CheckCompatibleAssignments(mie.Type, ref targetEntityPath); // Unless we're initializing an entity, we should not traverse into the parameter in scope. bool targetIsEntity = ClientType.CheckElementTypeIsEntity(targetType); bool sourceIsEntity = ClientType.CheckElementTypeIsEntity(lastExpression.Type); if (sourceIsEntity && !targetIsEntity) { throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(targetType, ma.Expression)); } } } }