Esempio n. 1
0
        protected override void RunTest(Program program, string outputFile)
        {
            FileUnitTester fut = null;

            try
            {
                fut = new FileUnitTester(outputFile);
                var factory = program.TypeFactory;
                var store   = program.TypeStore;
                var aen     = new ExpressionNormalizer(program.Platform.PointerType);
                var eqb     = new EquivalenceClassBuilder(factory, store, eventListener);
                var tyco    = new TypeCollector(factory, store, program, eventListener);

                aen.Transform(program);
                eqb.Build(program);
                tyco.CollectTypes();
                if (buildEquivalenceClasses)
                {
                    store.BuildEquivalenceClassDataTypes(factory);
                    new TypeVariableReplacer(store).ReplaceTypeVariables();
                }
            }
            catch (Exception ex)
            {
                fut.TextWriter.WriteLine(ex.Message);
                fut.TextWriter.WriteLine(ex.StackTrace);
                throw;
            }
            finally
            {
                DumpProgAndStore(program, fut.TextWriter);
                fut.AssertFilesEqual();
                fut.Dispose();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// QueryableResourceExpression visit method.
        /// </summary>
        /// <param name="rse">QueryableResourceExpression expression to visit</param>
        /// <returns>Visited QueryableResourceExpression expression</returns>
        internal override Expression VisitQueryableResourceExpression(QueryableResourceExpression rse)
        {
            if ((ResourceExpressionType)rse.NodeType == ResourceExpressionType.ResourceNavigationProperty)
            {
                if (rse.IsOperationInvocation && !(rse.Source is QueryableResourceExpression))
                {
                    var normalizerRewrites = new Dictionary <Expression, Expression>(ReferenceEqualityComparer <Expression> .Instance);
                    var e = Evaluator.PartialEval(rse.Source);
                    e = ExpressionNormalizer.Normalize(e, normalizerRewrites);
                    e = ResourceBinder.Bind(e, this.context);
                    this.Visit(e);
                }
                else
                {
                    this.Visit(rse.Source);
                }

                this.uriBuilder.Append(UriHelper.FORWARDSLASH).Append(this.ExpressionToString(rse.MemberExpression, /*inPath*/ true));
            }
            else if (rse.MemberExpression != null)
            {
                // this is a resource set expression
                // we should be at the very beginning of
                // the URI
                Debug.Assert(this.uriBuilder.Length == 0, "The builder is not empty while we are adding a resourceset");
                string entitySetName = (String)((ConstantExpression)rse.MemberExpression).Value;
                this.uriBuilder.Append(this.context.BaseUriResolver.GetEntitySetUri(entitySetName));
            }
            else
            {
                this.uriBuilder.Append(this.context.BaseUriResolver.BaseUriOrNull);
            }

            WebUtil.RaiseVersion(ref this.uriVersion, rse.UriVersion);

            if (rse.ResourceTypeAs != null)
            {
                this.uriBuilder.Append(UriHelper.FORWARDSLASH);
                UriHelper.AppendTypeSegment(this.uriBuilder, rse.ResourceTypeAs, this.context, /*inPath*/ true, ref this.uriVersion);
            }

            if (rse.KeyPredicateConjuncts.Count > 0)
            {
                this.context.UrlKeyDelimiter.AppendKeyExpression(rse.GetKeyProperties(), kvp => ClientTypeUtil.GetServerDefinedName(kvp.Key), kvp => kvp.Value.Value, this.uriBuilder);
            }

            if (rse.IsOperationInvocation)
            {
                this.VisitOperationInvocation(rse);
            }

            if (rse.CountOption == CountOption.CountSegment)
            {
                // append $count segment: /$count
                this.uriBuilder.Append(UriHelper.FORWARDSLASH).Append(UriHelper.DOLLARSIGN).Append(UriHelper.COUNT);
            }

            this.VisitQueryOptions(rse);
            return(rse);
        }
Esempio n. 3
0
 private TraitCollector CreateCollector(Program prog)
 {
     en      = new ExpressionNormalizer(prog.Architecture.PointerType);
     eqb     = new EquivalenceClassBuilder(prog.TypeFactory, prog.TypeStore);
     handler = new TestTraitHandler(prog.TypeStore);
     return(new TraitCollector(prog.TypeFactory, prog.TypeStore, handler, prog));
 }
Esempio n. 4
0
 private TraitCollector CreateCollector(Program program)
 {
     en      = new ExpressionNormalizer(program.Architecture.PointerType);
     eqb     = new EquivalenceClassBuilder(program.TypeFactory, program.TypeStore, new FakeDecompilerEventListener());
     handler = new TestTraitHandler(program.TypeStore);
     return(new TraitCollector(program.TypeFactory, program.TypeStore, handler, program));
 }
        /// <summary>Builds the Uri for the expression passed in.</summary>
        /// <param name="e">The expression to translate into a Uri</param>
        /// <returns>Query components</returns>
        internal QueryComponents Translate(Expression e)
        {
            Uri     uri;
            Version version;
            bool    addTrailingParens = false;
            Dictionary <Expression, Expression> normalizerRewrites = null;

            // short cut analysis if just a resource set or singleton resource.
            // note - to be backwards compatible with V1, will only append trailing () for queries
            // that include more then just a resource set.
            if (!(e is QueryableResourceExpression))
            {
                normalizerRewrites = new Dictionary <Expression, Expression>(ReferenceEqualityComparer <Expression> .Instance);
                e = Evaluator.PartialEval(e);
                e = ExpressionNormalizer.Normalize(e, normalizerRewrites);
                e = ResourceBinder.Bind(e, this.Context);
                addTrailingParens = true;
            }

            UriWriter.Translate(this.Context, addTrailingParens, e, out uri, out version);
            ResourceExpression re            = e as ResourceExpression;
            Type             lastSegmentType = re.Projection == null ? re.ResourceType : re.Projection.Selector.Parameters[0].Type;
            LambdaExpression selector        = re.Projection == null ? null : re.Projection.Selector;

            return(new QueryComponents(uri, version, lastSegmentType, selector, normalizerRewrites));
        }
Esempio n. 6
0
        protected void RunStringTest(Program program, string expectedOutput)
        {
            var sw = new StringWriter();

            var factory = program.TypeFactory;
            var store   = program.TypeStore;
            var aen     = new ExpressionNormalizer(program.Platform.PointerType);
            var eqb     = new EquivalenceClassBuilder(factory, store, eventListener);
            var tyco    = new TypeCollector(factory, store, program, eventListener);

            aen.Transform(program);
            eqb.Build(program);
            tyco.CollectTypes();

            aen.Transform(program);
            eqb.Build(program);
            var coll = new TypeCollector(program.TypeFactory, program.TypeStore, program, eventListener);

            coll.CollectTypes();
            program.TypeStore.Dump();

            program.TypeStore.BuildEquivalenceClassDataTypes(program.TypeFactory);
            DumpProgAndStore(program, sw);
            Assert.AreEqual(expectedOutput, sw.ToString());
        }
Esempio n. 7
0
        protected override void RunTest(Program program, string outputFileName)
        {
            ExpressionNormalizer aen = new ExpressionNormalizer(program.Architecture.PointerType);
            aen.Transform(program);
            EquivalenceClassBuilder eq = new EquivalenceClassBuilder(factory, store);
            eq.Build(program);
#if OLD
            			DataTypeBuilder dtb = new DataTypeBuilder(factory, store, program.Architecture);
			TraitCollector coll = new TraitCollector(factory, store, dtb, program);
			coll.CollectProgramTraits(program);
			dtb.BuildEquivalenceClassDataTypes();
#else
            TypeCollector coll = new TypeCollector(factory, store, program);
            coll.CollectTypes();
            store.BuildEquivalenceClassDataTypes(factory);
#endif

            TypeVariableReplacer tvr = new TypeVariableReplacer(store);
            tvr.ReplaceTypeVariables();

            TypeTransformer trans = new TypeTransformer(factory, store, program);
            trans.Transform();
            using (FileUnitTester fut = new FileUnitTester(outputFileName))
            {
                foreach (Procedure proc in program.Procedures.Values)
                {
                    proc.Write(false, fut.TextWriter);
                    fut.TextWriter.WriteLine();
                }
                store.Write(fut.TextWriter);
                fut.AssertFilesEqual();
            }
        }
 public void SetupPreStages(Program prog)
 {
     aen   = new ExpressionNormalizer(prog.Platform.PointerType);
     eqb   = new EquivalenceClassBuilder(prog.TypeFactory, prog.TypeStore);
     dtb   = new DataTypeBuilder(prog.TypeFactory, prog.TypeStore, prog.Platform);
     tvr   = new TypeVariableReplacer(prog.TypeStore);
     trans = new TypeTransformer(prog.TypeFactory, prog.TypeStore, prog);
     ctn   = new ComplexTypeNamer();
 }
        /// <summary>
        /// Applies normalization rewrites to the specified
        /// <paramref name="expression"/>, recording them in the
        /// <paramref name="rewrites"/> dictionary.
        /// </summary>
        /// <param name="expression">Expression to normalize.</param>
        /// <param name="rewrites">Dictionary in which to record rewrites.</param>
        /// <returns>The normalized expression.</returns>
        internal static Expression Normalize(Expression expression, Dictionary <Expression, Expression> rewrites)
        {
            Debug.Assert(expression != null, "expression != null");
            Debug.Assert(rewrites != null, "rewrites != null");

            ExpressionNormalizer normalizer = new ExpressionNormalizer(rewrites);
            Expression           result     = normalizer.Visit(expression);

            return(result);
        }
 public void NormalizeComplexWithExponentaInput()
 {
     var input = "4*x + 5*y^3 -6*x = 4*x+ 10*x*y^4";
                 var normalizer = new ExpressionNormalizer(
                         new СoefficientParser(),
                         new VariablesParser(),
                         new NormalizeExpressionFormatter());
                 var result = normalizer.NormalizeExpression(input);
                 Assert.IsTrue(result.Length > 0);
 }
Esempio n. 11
0
 public void SetUp()
 {
     store   = new TypeStore();
     factory = new TypeFactory();
     aen     = new ExpressionNormalizer(PrimitiveType.Pointer32);
     eqb     = new EquivalenceClassBuilder(factory, store);
     arch    = new FakeArchitecture();
     program = new Program();
     program.Architecture = arch;
     program.Platform     = new DefaultPlatform(null, arch);
     dtb = new DataTypeBuilder(factory, store, program.Platform);
 }
Esempio n. 12
0
 public void SetupPreStages(Program program)
 {
     foreach (var f in userDefinedGlobals)
     {
         program.GlobalFields.Fields.Add(f);
     }
     aen   = new ExpressionNormalizer(program.Platform.PointerType);
     eqb   = new EquivalenceClassBuilder(program.TypeFactory, program.TypeStore);
     dtb   = new DataTypeBuilder(program.TypeFactory, program.TypeStore, program.Platform);
     tvr   = new TypeVariableReplacer(program.TypeStore);
     trans = new TypeTransformer(program.TypeFactory, program.TypeStore, program);
     ctn   = new ComplexTypeNamer();
 }
Esempio n. 13
0
 public void SetUp()
 {
     store                = new TypeStore();
     factory              = new TypeFactory();
     listener             = new FakeDecompilerEventListener();
     aen                  = new ExpressionNormalizer(PrimitiveType.Ptr32);
     eqb                  = new EquivalenceClassBuilder(factory, store, listener);
     arch                 = new FakeArchitecture(new ServiceContainer());
     program              = new Program();
     program.Architecture = arch;
     program.Platform     = new DefaultPlatform(null, arch);
     dtb                  = new DataTypeBuilder(factory, store, program.Platform);
 }
Esempio n. 14
0
        protected override void RunTest(Program program, string outputFileName)
        {
            var listener             = new FakeDecompilerEventListener();
            ExpressionNormalizer aen = new ExpressionNormalizer(program.Architecture.PointerType);

            aen.Transform(program);
            EquivalenceClassBuilder eq = new EquivalenceClassBuilder(factory, store, listener);

            eq.Build(program);
#if OLD
            DataTypeBuilder dtb  = new DataTypeBuilder(factory, store, program.Architecture);
            TraitCollector  coll = new TraitCollector(factory, store, dtb, program);
            coll.CollectProgramTraits(program);
            sktore.BuildEquivalenceClassDataTypes(factory);
#else
            TypeCollector coll = new TypeCollector(factory, store, program, listener);
            coll.CollectTypes();

            store.BuildEquivalenceClassDataTypes(factory);
#endif

            TypeVariableReplacer tvr = new TypeVariableReplacer(store);
            tvr.ReplaceTypeVariables();

            Exception theEx = null;
            try
            {
                TypeTransformer trans = new TypeTransformer(factory, store, program);
                trans.Transform();
            } catch (Exception ex)
            {
                theEx = ex;
            }
            using (FileUnitTester fut = new FileUnitTester(outputFileName))
            {
                if (theEx != null)
                {
                    fut.TextWriter.WriteLine(theEx.Message);
                    fut.TextWriter.WriteLine(theEx.StackTrace);
                }
                foreach (Procedure proc in program.Procedures.Values)
                {
                    proc.Write(false, fut.TextWriter);
                    fut.TextWriter.WriteLine();
                }
                store.Write(fut.TextWriter);
                fut.AssertFilesEqual();
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Converts expression to Uri
        /// </summary>
        /// <param name="e">The expression</param>
        /// <returns>Converted uri</returns>
        public virtual Uri Convert(Expression e)
        {
            Uri uri;

            Dictionary <Expression, Expression> rewrites = null;

            if (!(e is ResourceSetExpression))
            {
                rewrites = new Dictionary <Expression, Expression>(ReferenceEqualityComparer <Expression> .Instance);
                e        = Evaluator.PartialEval(e);
                e        = ExpressionNormalizer.Normalize(e, rewrites);
                e        = ResourceBinder.Bind(e);
            }

            UriWriter.Translate(false, e, out uri);

            return(uri);
        }
Esempio n. 16
0
        public void SetupPreStages(Program program)
        {
            var listener = new FakeDecompilerEventListener();

            foreach (var f in userDefinedGlobals)
            {
                program.GlobalFields.Fields.Add(f);
            }
            foreach (var s in imageSegments.Values)
            {
                program.SegmentMap.Segments.Add(s.Address, s);
            }
            aen   = new ExpressionNormalizer(program.Platform.PointerType);
            eqb   = new EquivalenceClassBuilder(program.TypeFactory, program.TypeStore, listener);
            dtb   = new DataTypeBuilder(program.TypeFactory, program.TypeStore, program.Platform);
            tvr   = new TypeVariableReplacer(program.TypeStore);
            trans = new TypeTransformer(program.TypeFactory, program.TypeStore, program);
            ctn   = new ComplexTypeNamer();
        }
        internal string Bind(Expression expression)
        {
            Argument.AssertNotNull(expression, nameof(expression));

            Dictionary <Expression, Expression> normalizerRewrites = new Dictionary <Expression, Expression>(ReferenceEqualityComparer <Expression> .Instance);

            // Evaluate any local evaluatable expressions ( lambdas etc)
            Expression partialEvaluatedExpression = Evaluator.PartialEval(expression);

            // Normalize expression, replace String Comparisons etc.
            Expression normalizedExpression = ExpressionNormalizer.Normalize(partialEvaluatedExpression, normalizerRewrites);

            // Parse the Bound expression into sub components, i.e. take count, filter, select columns, request options, opcontext, etc.
            ExpressionParser parser = GetExpressionParser();

            parser.Translate(normalizedExpression);

            // Return the FilterString.
            return(parser.FilterString == "true" ? null : parser.FilterString);
        }
Esempio n. 18
0
        private void RunTestCore(Program program)
        {
            var listener = new FakeDecompilerEventListener();
            var aen      = new ExpressionNormalizer(program.Architecture.PointerType);

            aen.Transform(program);
            var eq = new EquivalenceClassBuilder(factory, store, listener);

            eq.Build(program);
            var coll = new TypeCollector(factory, store, program, listener);

            coll.CollectTypes();
            store.BuildEquivalenceClassDataTypes(factory);

            var tvr = new TypeVariableReplacer(store);

            tvr.ReplaceTypeVariables();

            var trans = new TypeTransformer(factory, store, program);

            trans.Transform();
        }
Esempio n. 19
0
        internal ExecutionInfo Bind()
        {
            ExecutionInfo retVal = new ExecutionInfo();

            // IQueryable impl
            if (this.Expression != null)
            {
                Dictionary <Expression, Expression> normalizerRewrites = new Dictionary <Expression, Expression>(ReferenceEqualityComparer <Expression> .Instance);

                // Step 1. Evaluate any local evaluatable expressions ( lambdas etc)
                Expression partialEvaluatedExpression = Evaluator.PartialEval(this.Expression);

                // Step 2. Normalize expression, replace String Comparisons etc.
                Expression normalizedExpression = ExpressionNormalizer.Normalize(partialEvaluatedExpression, normalizerRewrites);

                // Step 3. Bind Expression, Analyze predicates and create query option expressions. End result is a single ResourceSetExpression
                Expression boundExpression = ResourceBinder.Bind(normalizedExpression);

                // Step 4. Parse the Bound expression into sub components, i.e. take count, filter, select columns, request options, opcontext, etc.
                ExpressionParser parser = new ExpressionParser();
                parser.Translate(boundExpression);

                // Step 5. Store query components & params
                this.TakeCount          = parser.TakeCount;
                this.FilterString       = parser.FilterString;
                this.SelectColumns      = parser.SelectColumns;
                retVal.RequestOptions   = parser.RequestOptions;
                retVal.OperationContext = parser.OperationContext;

                // Step 6. If projection & no resolver then generate a resolver to perform the projection
                if (parser.Resolver == null)
                {
                    if (parser.Projection != null && parser.Projection.Selector != ProjectionQueryOptionExpression.DefaultLambda)
                    {
                        Type intermediateType = parser.Projection.Selector.Parameters[0].Type;

                        // Convert Expression to take type object as input to allow for direct invocation.
                        ParameterExpression paramExpr = Expression.Parameter(typeof(object));

                        Func <object, TElement> projectorFunc = Expression.Lambda <Func <object, TElement> >(
                            Expression.Invoke(parser.Projection.Selector, Expression.Convert(paramExpr, intermediateType)), paramExpr).Compile();

                        // Generate a resolver to do the projection.
                        retVal.Resolver = (pk, rk, ts, props, etag) =>
                        {
                            // Parse to intermediate type
                            ITableEntity intermediateObject = (ITableEntity)EntityUtilities.InstantiateEntityFromType(intermediateType);
                            intermediateObject.PartitionKey = pk;
                            intermediateObject.RowKey       = rk;
                            intermediateObject.Timestamp    = ts;
                            intermediateObject.ReadEntity(props, parser.OperationContext);
                            intermediateObject.ETag = etag;

                            // Invoke lambda expression
                            return(projectorFunc(intermediateObject));
                        };
                    }
                    else
                    {
                        // No op - No resolver or projection specified.
                    }
                }
                else
                {
                    retVal.Resolver = (EntityResolver <TElement>)parser.Resolver.Value;
                }
            }

            retVal.RequestOptions   = TableRequestOptions.ApplyDefaults(retVal.RequestOptions, this.queryProvider.Table.ServiceClient);
            retVal.OperationContext = retVal.OperationContext ?? new OperationContext();
            return(retVal);
        }
Esempio n. 20
0
 public void Setup()
 {
     m   = new ProcedureBuilder();
     aen = new ExpressionNormalizer(PrimitiveType.Pointer32);
 }
Esempio n. 21
0
 public void SetUp()
 {
     normalizer = new ExpressionNormalizer();
 }