/// <summary> /// Visit an <see cref="InputReferenceExpression"/>, producing a new InputReferenceExpression /// based on the visited form of the <see cref="QueryableResourceExpression"/> that is referenced by /// the InputReferenceExpression argument, <paramref name="ire"/>. /// </summary> /// <param name="ire">InputReferenceExpression expression to visit</param> /// <returns>Visited InputReferenceExpression expression</returns> internal virtual Expression VisitInputReferenceExpression(InputReferenceExpression ire) { Debug.Assert(ire != null, "ire != null -- otherwise caller never should have visited here"); ResourceExpression re = (ResourceExpression)this.Visit(ire.Target); return(re.CreateReference()); }
/// <summary> /// Creates an <see cref="InputReferenceExpression"/> that refers to this component of the resource path. /// The returned expression is guaranteed to be reference-equal (object.ReferenceEquals) /// to any other InputReferenceExpression that also refers to this resource path component. /// </summary> /// <returns>The InputReferenceExpression that refers to this resource path component</returns> internal InputReferenceExpression CreateReference() { if (this.inputRef == null) { this.inputRef = new InputReferenceExpression(this); } return(this.inputRef); }
/// <summary> /// Instructs this resource set expression to use the input reference expression from <paramref name="newInput"/> as it's /// own input reference, and to retarget the input reference from <paramref name="newInput"/> to this resource set expression. /// </summary> /// <param name="newInput">The resource set expression from which to take the input reference.</param> /// <remarks>Used exclusively by ResourceBinder.RemoveTransparentScope.</remarks> internal void OverrideInputReference(QueryableResourceExpression newInput) { Debug.Assert(newInput != null, "Original resource set cannot be null"); Debug.Assert(this.inputRef == null, "OverrideInputReference cannot be called if the target has already been referenced"); InputReferenceExpression inputRef = newInput.inputRef; if (inputRef != null) { this.inputRef = inputRef; inputRef.OverrideTarget(this); } }
/// <summary> /// Input resource set references are intentionally omitted from the URL string for the top level /// references to input parameter (i.e. outside of any/all methods). /// For parameter references to input (range variable for Where) inside any/all methods we write "$it". /// </summary> /// <param name="ire">The input reference</param> /// <returns>The same input reference expression</returns> internal override Expression VisitInputReferenceExpression(InputReferenceExpression ire) { // This method intentionally does not write anything to the URI for implicit references to the input parameter ($it). // This is how 'Where(<input>.Id == 5)' becomes '$filter=Id eq 5'. Debug.Assert(ire != null, "ire != null"); if (this.parent == null || (!this.InSubScope && this.parent.NodeType != ExpressionType.MemberAccess && this.parent.NodeType != ExpressionType.TypeAs && this.parent.NodeType != ExpressionType.Call)) { // Ideally we refer to the parent expression as the un-translatable one, // because we cannot reference 'this' as a standalone expression; however // if the parent is null for any reason, we fall back to the expression itself. string expressionText = (this.parent != null) ? this.parent.ToString() : ire.ToString(); throw new NotSupportedException(Strings.ALinq_CantTranslateExpression(expressionText)); } // Write "$it" for input parameter reference inside any/all methods if (this.InSubScope || this.parent.NodeType == ExpressionType.Call) { this.builder.Append(XmlConstants.ImplicitFilterParameter); } return(ire); }
/// <summary> /// Resolves member accesses that represent transparent scope property accesses to the corresponding resource, /// iff the input resource is enclosed in a transparent scope and the specified MemberExpression represents /// such a property access. /// </summary> /// <param name="m">MemberExpression expression to visit</param> /// <returns> /// An InputReferenceExpression if the member access represents a transparent scope property /// access that can be resolved to a resource in the path that produces the input resource; /// otherwise the same MemberExpression is returned. /// </returns> internal override Expression VisitMemberAccess(MemberExpression m) { // If the current input resource is not enclosed in a transparent scope, then this // MemberExpression cannot represent a valid transparent scope access based on the input parameter. if (this.inputResource == null || !this.inputResource.HasTransparentScope) { return(base.VisitMemberAccess(m)); } ParameterExpression innerParamRef = null; Stack <PropertyInfo> nestedAccesses = new Stack <PropertyInfo>(); MemberExpression memberRef = m; while (memberRef != null && PlatformHelper.IsProperty(memberRef.Member) && memberRef.Expression != null) { nestedAccesses.Push((PropertyInfo)memberRef.Member); if (memberRef.Expression.NodeType == ExpressionType.Parameter) { innerParamRef = (ParameterExpression)memberRef.Expression; } memberRef = memberRef.Expression as MemberExpression; } // Only continue if the inner non-MemberExpression is the input reference ParameterExpression and // at least one property reference is present - otherwise this cannot be a transparent scope access. if (innerParamRef != this.inputParameter || nestedAccesses.Count == 0) { return(m); } ResourceExpression target = this.input; QueryableResourceExpression targetResource = this.inputResource; bool transparentScopeTraversed = false; // Process all the traversals through transparent scopes. while (nestedAccesses.Count > 0) { if (targetResource == null || !targetResource.HasTransparentScope) { break; } // Peek the property; pop it once it's consumed // (it could be a non-transparent-identifier access). PropertyInfo currentProp = nestedAccesses.Peek(); // If this is the accessor for the target, then the member // refers to the target itself. if (currentProp.Name.Equals(targetResource.TransparentScope.Accessor, StringComparison.Ordinal)) { target = targetResource; nestedAccesses.Pop(); transparentScopeTraversed = true; continue; } // This member could also be one of the in-scope sources of the target. Expression source; if (!targetResource.TransparentScope.SourceAccessors.TryGetValue(currentProp.Name, out source)) { break; } transparentScopeTraversed = true; nestedAccesses.Pop(); Debug.Assert(source != null, "source != null -- otherwise ResourceBinder created an accessor to nowhere"); InputReferenceExpression sourceReference = source as InputReferenceExpression; if (sourceReference == null) { targetResource = source as QueryableResourceExpression; if (targetResource == null || !targetResource.HasTransparentScope) { target = (ResourceExpression)source; } } else { targetResource = sourceReference.Target as QueryableResourceExpression; target = targetResource; } } // If no traversals were made, the original expression is OK. if (!transparentScopeTraversed) { return(m); } // Process traversals after the transparent scope. Expression result = this.CreateReference(target); while (nestedAccesses.Count > 0) { result = Expression.Property(result, nestedAccesses.Pop()); } return(result); }