コード例 #1
0
        string InsertRequired(Expression expr)
        {
            expr = expr.Clone();
            expr.AcceptVisitor(new InsertParenthesesVisitor {
                InsertParenthesesForReadability = false
            });
            StringWriter w = new StringWriter();

            w.NewLine = " ";
            expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w)
            {
                IndentationString = ""
            }, policy));
            return(w.ToString());
        }
コード例 #2
0
            Expression VisitNested(Expression node, ParameterDeclaration transparentParameter)
            {
                var oldRangeVariableSubstitutions = activeRangeVariableSubstitutions;

                try {
                    if (transparentParameter != null && currentTransparentType.Count > 1)
                    {
                        activeRangeVariableSubstitutions = new Dictionary <string, Expression>(activeRangeVariableSubstitutions);
                        foreach (var t in currentTransparentType)
                        {
                            activeRangeVariableSubstitutions[t.Item1.Name] = MakeNestedMemberAccess(new IdentifierExpression(transparentParameter.Name), t.Item2);
                        }
                    }
                    var result = node.AcceptVisitor(this);
                    return((Expression)(result ?? node.Clone()));
                }
                finally {
                    activeRangeVariableSubstitutions = oldRangeVariableSubstitutions;
                }
            }
コード例 #3
0
 public static Expression InvertCondition(Expression condition)
 {
     return(InvertConditionInternal(condition.Clone()));
 }
コード例 #4
0
 static Expression ParenthesizeIfNeeded(Expression expr)
 {
     return(NeedsToBeParenthesized(expr) ? new ParenthesizedExpression(expr.Clone()) : expr);
 }
コード例 #5
0
        static Expression InvertConditionInternal(Expression condition)
        {
            if (condition is ParenthesizedExpression)
            {
                return(new ParenthesizedExpression(InvertCondition(((ParenthesizedExpression)condition).Expression)));
            }

            if (condition is UnaryOperatorExpression)
            {
                var uOp = (UnaryOperatorExpression)condition;
                if (uOp.Operator == UnaryOperatorType.Not)
                {
                    if (!(uOp.Parent is Expression))
                    {
                        return(GetInnerMostExpression(uOp.Expression).Clone());
                    }
                    return(uOp.Expression.Clone());
                }
                return(new UnaryOperatorExpression(UnaryOperatorType.Not, uOp.Clone()));
            }

            if (condition is BinaryOperatorExpression)
            {
                var bOp = (BinaryOperatorExpression)condition;

                if ((bOp.Operator == BinaryOperatorType.ConditionalAnd) || (bOp.Operator == BinaryOperatorType.ConditionalOr))
                {
                    return(new BinaryOperatorExpression(InvertCondition(bOp.Left), NegateConditionOperator(bOp.Operator), InvertCondition(bOp.Right)));
                }
                else if ((bOp.Operator == BinaryOperatorType.Equality) || (bOp.Operator == BinaryOperatorType.InEquality) || (bOp.Operator == BinaryOperatorType.GreaterThan) ||
                         (bOp.Operator == BinaryOperatorType.GreaterThanOrEqual) || (bOp.Operator == BinaryOperatorType.LessThan) ||
                         (bOp.Operator == BinaryOperatorType.LessThanOrEqual))
                {
                    return(new BinaryOperatorExpression(bOp.Left.Clone(), NegateRelationalOperator(bOp.Operator), bOp.Right.Clone()));
                }
                else
                {
                    var negatedOp = NegateRelationalOperator(bOp.Operator);
                    if (negatedOp == BinaryOperatorType.Any)
                    {
                        return(new UnaryOperatorExpression(UnaryOperatorType.Not, new ParenthesizedExpression(condition.Clone())));
                    }
                    bOp          = (BinaryOperatorExpression)bOp.Clone();
                    bOp.Operator = negatedOp;
                    return(bOp);
                }
            }
            if (condition is ConditionalExpression)
            {
                var cEx = condition.Clone() as ConditionalExpression;
                cEx.Condition = InvertCondition(cEx.Condition);
                return(cEx);
            }
            if (condition is PrimitiveExpression)
            {
                var pex = condition as PrimitiveExpression;
                if (pex.Value is bool)
                {
                    return(new PrimitiveExpression(!((bool)pex.Value)));
                }
            }

            return(new UnaryOperatorExpression(UnaryOperatorType.Not, AddParensForUnaryExpressionIfRequired(condition.Clone())));
        }
コード例 #6
0
        private Expression WidenType <T>(Expression source)
        {
            var tref = ShaderDefinition.ToCecil(typeof(T));
            var n    = new ObjectCreateExpression(AstBuilder.ConvertType(tref), new[] { source.Clone() });

            return(n);
        }
コード例 #7
0
        public override AstNode VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression)
        {
            if (this.Rules.Integer == IntegerRule.Managed && (unaryOperatorExpression.Operator == UnaryOperatorType.Increment ||
                                                              unaryOperatorExpression.Operator == UnaryOperatorType.PostIncrement ||
                                                              unaryOperatorExpression.Operator == UnaryOperatorType.Decrement ||
                                                              unaryOperatorExpression.Operator == UnaryOperatorType.PostDecrement))
            {
                var rr            = this.Resolver.ResolveNode(unaryOperatorExpression, null);
                var expression_rr = this.Resolver.ResolveNode(unaryOperatorExpression.Expression, null);

                if (rr is ErrorResolveResult)
                {
                    UnaryOperatorExpression clonUnaryOperatorExpression = (UnaryOperatorExpression)base.VisitUnaryOperatorExpression(unaryOperatorExpression);
                    if (clonUnaryOperatorExpression == null)
                    {
                        clonUnaryOperatorExpression = (UnaryOperatorExpression)unaryOperatorExpression.Clone();
                    }

                    bool isPost = clonUnaryOperatorExpression.Operator == UnaryOperatorType.PostDecrement ||
                                  clonUnaryOperatorExpression.Operator == UnaryOperatorType.PostIncrement;

                    var isStatement = unaryOperatorExpression.Parent is ExpressionStatement;
                    var isIncr      = clonUnaryOperatorExpression.Operator == UnaryOperatorType.Increment || clonUnaryOperatorExpression.Operator == UnaryOperatorType.PostIncrement;
                    AssignmentExpression ae;

                    ae = new AssignmentExpression(clonUnaryOperatorExpression.Expression.Clone(), new BinaryOperatorExpression(clonUnaryOperatorExpression.Expression.Clone(), isIncr ? BinaryOperatorType.Add : BinaryOperatorType.Subtract, new PrimitiveExpression(1)));

                    if (isPost && !isStatement)
                    {
                        return(new InvocationExpression(new MemberReferenceExpression(new MemberReferenceExpression(new TypeReferenceExpression(new MemberType(new SimpleType("global"), CS.NS.BRIDGE)
                        {
                            IsDoubleColon = true
                        }), "Script"), "Identity"), clonUnaryOperatorExpression.Expression.Clone(), ae));
                    }
                    else
                    {
                        if (isStatement)
                        {
                            return(ae);
                        }

                        return(new ParenthesizedExpression(ae));
                    }
                }
                else
                {
                    var orr = (OperatorResolveResult)rr;

                    if (Helpers.IsFloatType(orr.Type, this.Resolver) || Helpers.Is64Type(orr.Type, this.Resolver))
                    {
                        return(base.VisitUnaryOperatorExpression(unaryOperatorExpression));
                    }

                    UnaryOperatorExpression clonUnaryOperatorExpression = (UnaryOperatorExpression)base.VisitUnaryOperatorExpression(unaryOperatorExpression);
                    if (clonUnaryOperatorExpression == null)
                    {
                        clonUnaryOperatorExpression = (UnaryOperatorExpression)unaryOperatorExpression.Clone();
                    }

                    bool isPost = clonUnaryOperatorExpression.Operator == UnaryOperatorType.PostDecrement ||
                                  clonUnaryOperatorExpression.Operator == UnaryOperatorType.PostIncrement;

                    var isStatement        = unaryOperatorExpression.Parent is ExpressionStatement;
                    var isIncr             = clonUnaryOperatorExpression.Operator == UnaryOperatorType.Increment || clonUnaryOperatorExpression.Operator == UnaryOperatorType.PostIncrement;
                    var needReturnOriginal = isPost && !isStatement;

                    Expression expression      = clonUnaryOperatorExpression.Expression.Clone();
                    Expression expressionAfter = clonUnaryOperatorExpression.Expression.Clone();

                    AssignmentExpression ae = null;

                    if (orr.UserDefinedOperatorMethod != null)
                    {
                        ae = new AssignmentExpression(expressionAfter.Clone(), clonUnaryOperatorExpression);
                    }
                    else if (clonUnaryOperatorExpression.Expression is MemberReferenceExpression mre && expression_rr is MemberResolveResult member_rr)
                    {
                        if (needReturnOriginal)
                        {
                            bool isSimple = (member_rr != null &&
                                             (member_rr.TargetResult is ThisResolveResult ||
                                              member_rr.TargetResult is LocalResolveResult ||
                                              member_rr.TargetResult is TypeResolveResult ||
                                              member_rr.TargetResult is ConstantResolveResult));

                            expression = isSimple ? mre.Clone() : new MemberReferenceExpression(new InvocationExpression(new MemberReferenceExpression(new MemberReferenceExpression(new TypeReferenceExpression(new MemberType(new SimpleType("global"), CS.NS.BRIDGE)
                            {
                                IsDoubleColon = true
                            }), "Script"), "ToTemp"), new PrimitiveExpression("idx" + tempKey), mre.Target.Clone()), mre.MemberName);
                            expressionAfter = new InvocationExpression(new MemberReferenceExpression(new MemberReferenceExpression(new TypeReferenceExpression(new MemberType(new SimpleType("global"), CS.NS.BRIDGE)
                            {
                                IsDoubleColon = true
                            }), "Script"), "FromTemp"), new PrimitiveExpression("idx" + tempKey++), mre.Target.Clone());
                        }
                        else
                        {
                            expressionAfter = null;
                        }

                        ae = BuildMemberReferenceReplacement(isIncr ? BinaryOperatorType.Add : BinaryOperatorType.Subtract, mre, member_rr, expressionAfter);
                    }
                    else if (clonUnaryOperatorExpression.Expression is IndexerExpression ie && expression_rr is ArrayAccessResolveResult array_rr)
                    {
                        IndexerExpression cacheIndexer = null;
                        if (needReturnOriginal)
                        {
                            var  array_target_rr = array_rr.Array as MemberResolveResult;
                            bool isSimpleTarget  = (array_target_rr != null && array_target_rr.Member is IField &&
                                                    (array_target_rr.TargetResult is ThisResolveResult ||
                                                     array_target_rr.TargetResult is LocalResolveResult ||
                                                     array_target_rr.TargetResult is TypeResolveResult ||
                                                     array_target_rr.TargetResult is ConstantResolveResult)) ||
                                                   (array_rr.Array is ThisResolveResult ||
                                                    array_rr.Array is LocalResolveResult ||
                                                    array_rr.Array is ConstantResolveResult);

                            bool simpleIndex = true;

                            foreach (var index in array_rr.Indexes)
                            {
                                var  indexMemberTargetrr = index as MemberResolveResult;
                                bool isIndexSimple       = (indexMemberTargetrr != null && indexMemberTargetrr.Member is IField &&
                                                            (indexMemberTargetrr.TargetResult is ThisResolveResult ||
                                                             indexMemberTargetrr.TargetResult is LocalResolveResult)) || index is ThisResolveResult || index is LocalResolveResult || index is ConstantResolveResult;

                                if (!isIndexSimple)
                                {
                                    simpleIndex = false;
                                    break;
                                }
                            }
                            var leftIndexerArgs  = new List <Expression>();
                            var rightIndexerArgs = new List <Expression>();

                            foreach (var index in ie.Arguments)
                            {
                                var expr = simpleIndex ? index.Clone() : new InvocationExpression(new MemberReferenceExpression(new MemberReferenceExpression(new TypeReferenceExpression(new MemberType(new SimpleType("global"), CS.NS.BRIDGE)
                                {
                                    IsDoubleColon = true
                                }), "Script"), "ToTemp"), new PrimitiveExpression("idx" + tempKey), index.Clone());
                                leftIndexerArgs.Add(expr);

                                expr = simpleIndex ? index.Clone() : new InvocationExpression(new MemberReferenceExpression(new MemberReferenceExpression(new TypeReferenceExpression(new MemberType(new SimpleType("global"), CS.NS.BRIDGE)
                                {
                                    IsDoubleColon = true
                                }), "Script"), "FromTemp"), new PrimitiveExpression("idx" + tempKey++), index.Clone());
                                rightIndexerArgs.Add(expr);
                            }

                            var leftExpr = new InvocationExpression(new MemberReferenceExpression(new MemberReferenceExpression(new TypeReferenceExpression(new MemberType(new SimpleType("global"), CS.NS.BRIDGE)
                            {
                                IsDoubleColon = true
                            }), "Script"), "ToTemp"), new PrimitiveExpression("idx" + tempKey), ie.Target.Clone());
                            var rightExpr = new InvocationExpression(new MemberReferenceExpression(new MemberReferenceExpression(new TypeReferenceExpression(new MemberType(new SimpleType("global"), CS.NS.BRIDGE)
                            {
                                IsDoubleColon = true
                            }), "Script"), "FromTemp"), new PrimitiveExpression("idx" + tempKey++), ie.Target.Clone());

                            var leftIndexer  = new IndexerExpression(isSimpleTarget ? ie.Target.Clone() : leftExpr, leftIndexerArgs);
                            var rightIndexer = new IndexerExpression(isSimpleTarget ? ie.Target.Clone() : rightExpr, rightIndexerArgs);

                            expression   = leftIndexer;
                            cacheIndexer = rightIndexer;
                        }

                        ae = BuildIndexerReplacement(isStatement, isIncr ? BinaryOperatorType.Add : BinaryOperatorType.Subtract, ie, array_rr, cacheIndexer);
                    }