Beispiel #1
0
 protected override IExpression ConvertCastExpr(ICastExpression ice)
 {
     return(CodeRecognizer.RemoveCast(base.ConvertCastExpr(ice)));
 }
Beispiel #2
0
        protected override IExpression ConvertMethodInvoke(IMethodInvokeExpression imie)
        {
            if (Recognizer.IsStaticGenericMethod(imie, new Func <PlaceHolder, ICompilerAttribute, PlaceHolder>(Attrib.Var)))
            {
                IVariableReferenceExpression ivre   = imie.Arguments[0] as IVariableReferenceExpression;
                IVariableDeclaration         target = ivre.Variable.Resolve();
                IExpression expr = CodeRecognizer.RemoveCast(imie.Arguments[1]);
                AddAttribute(target, expr);
                return(null);
            }
            else if (Recognizer.IsStaticMethod(imie, new Action <object, object>(Attrib.InitialiseTo)))
            {
                IVariableReferenceExpression ivre   = CodeRecognizer.RemoveCast(imie.Arguments[0]) as IVariableReferenceExpression;
                IVariableDeclaration         target = ivre.Variable.Resolve();
                context.OutputAttributes.Set(target, new InitialiseTo(imie.Arguments[1]));
                return(null);
            }
            else if (CodeRecognizer.IsInfer(imie))
            {
                inferCount++;
                object decl = Recognizer.GetDeclaration(imie.Arguments[0]);
                if (decl != null && !context.InputAttributes.Has <IsInferred>(decl))
                {
                    context.InputAttributes.Set(decl, new IsInferred());
                }
                // the arguments must not be substituted for their values, so we don't call ConvertExpression
                List <IExpression> newArgs = new List <IExpression>();
                foreach (var arg in imie.Arguments)
                {
                    newArgs.Add(CodeRecognizer.RemoveCast(arg));
                }
                IMethodInvokeExpression mie = Builder.MethodInvkExpr();
                mie.Method = imie.Method;
                mie.Arguments.AddRange(newArgs);
                context.InputAttributes.CopyObjectAttributesTo(imie, context.OutputAttributes, mie);
                return(mie);
            }
            IExpression converted = base.ConvertMethodInvoke(imie);

            if (converted is IMethodInvokeExpression)
            {
                var  mie   = (IMethodInvokeExpression)converted;
                bool isAnd = Recognizer.IsStaticMethod(converted, new Func <bool, bool, bool>(Factors.Factor.And));
                bool isOr  = Recognizer.IsStaticMethod(converted, new Func <bool, bool, bool>(Factors.Factor.Or));
                bool anyArgumentIsLiteral = mie.Arguments.Any(arg => arg is ILiteralExpression);
                if (anyArgumentIsLiteral)
                {
                    if (isAnd)
                    {
                        if (mie.Arguments.Any(arg => arg is ILiteralExpression && ((ILiteralExpression)arg).Value.Equals(false)))
                        {
                            return(Builder.LiteralExpr(false));
                        }
                        // any remaining literals must be true, and therefore can be ignored.
                        var reducedArguments = mie.Arguments.Where(arg => !(arg is ILiteralExpression));
                        if (reducedArguments.Count() == 1)
                        {
                            return(reducedArguments.First());
                        }
                        else
                        {
                            return(Builder.LiteralExpr(true));
                        }
                    }
                    else if (isOr)
                    {
                        if (mie.Arguments.Any(arg => arg is ILiteralExpression && ((ILiteralExpression)arg).Value.Equals(true)))
                        {
                            return(Builder.LiteralExpr(true));
                        }
                        // any remaining literals must be false, and therefore can be ignored.
                        var reducedArguments = mie.Arguments.Where(arg => !(arg is ILiteralExpression));
                        if (reducedArguments.Count() == 1)
                        {
                            return(reducedArguments.First());
                        }
                        else
                        {
                            return(Builder.LiteralExpr(false));
                        }
                    }
                    else if (Recognizer.IsStaticMethod(converted, new Func <bool, bool>(Factors.Factor.Not)))
                    {
                        bool allArgumentsAreLiteral = mie.Arguments.All(arg => arg is ILiteralExpression);
                        if (allArgumentsAreLiteral)
                        {
                            return(Builder.LiteralExpr(evaluator.Evaluate(mie)));
                        }
                    }
                }
                foreach (IExpression arg in mie.Arguments)
                {
                    if (arg is IAddressOutExpression)
                    {
                        IAddressOutExpression iaoe = (IAddressOutExpression)arg;
                        IVariableDeclaration  ivd  = Recognizer.GetVariableDeclaration(iaoe.Expression);
                        if (ivd != null)
                        {
                            FactorManager.FactorInfo info = CodeRecognizer.GetFactorInfo(context, mie);
                            if (info != null && info.IsDeterministicFactor && !context.InputAttributes.Has <DerivedVariable>(ivd))
                            {
                                context.InputAttributes.Set(ivd, new DerivedVariable());
                            }
                        }
                    }
                }
            }
            return(converted);
        }