Exemplo n.º 1
0
        public static Expression CompileClause(PqlCompilerState compilerState, ParseTreeNode parseTreeNode, DataContainerDescriptor containerDescriptor, RequestExecutionContextCacheInfo cacheInfo, Type returnType)
        {
            var exprBody = s_expressionRuntime.Analyze(parseTreeNode, compilerState);

            // now add our field references to the expression
            if (compilerState.FieldRefs.Count > 0 || compilerState.ParamRefs.Count > 0)
            {
                // variable declarations
                var localVariables = new ParameterExpression[compilerState.FieldRefs.Count + compilerState.ParamRefs.Count];
                var exprList       = new Expression[1 + localVariables.Length];

                var ix = 0;
                foreach (var pair in compilerState.FieldRefs)
                {
                    localVariables[ix] = pair.Value.Item1;
                    exprList[ix]       = Expression.Assign(pair.Value.Item1, pair.Value.Item2);
                    ix++;
                }
                foreach (var pair in compilerState.ParamRefs)
                {
                    localVariables[ix] = pair.Value.Item1;
                    exprList[ix]       = Expression.Assign(pair.Value.Item1, pair.Value.Item2);
                    ix++;
                }

                // and the expression code itself
                exprList[ix] = exprBody;
                // ready to go
                exprBody = Expression.Block(localVariables, exprList);
            }

            return(s_expressionRuntime.AdjustReturnType(exprBody, returnType));
        }
        /// <summary>
        /// DO NOT REMOVE! Used implicitly, via generic method instantiation.
        /// </summary>
        public static HashSet <T> EnumerateValues <T>(IExpressionEvaluatorRuntime runtime, ParseTreeNode root, CompilerState state)
        {
            if (runtime == null)
            {
                throw new ArgumentNullException("runtime");
            }

            if (root == null)
            {
                throw new ArgumentNullException("root");
            }

            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            var isString = ReferenceEquals(typeof(T), typeof(string));
            var result   = isString ? new HashSet <T>((IEqualityComparer <T>)StringComparer.OrdinalIgnoreCase) : new HashSet <T>();

            foreach (var childNode in root.ChildNodes)
            {
                var item = runtime.Analyze(childNode, state);
                item.RequireNonVoid(childNode);

                try
                {
                    result.Add((T)Convert.ChangeType(((ConstantExpression)item).Value, typeof(T)));
                }
                catch
                {
                    throw new CompilationException(
                              String.Format(
                                  "All items in the IN arguments list must be constants of type compatible with {0}. Actual expression found: {1}, of type {2}",
                                  typeof(T).FullName, item.NodeType, item.Type), childNode);
                }
            }

            return(result);
        }