Exemplo n.º 1
0
        public override SyntaxNode VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node)
        {
            if (node.Kind() != SyntaxKind.LogicalNotExpression)
            {
                throw new InvalidPreprocessorExpressionException("Expected logical not expression");
            }

            var newExpression = (ExpressionSyntax)node.Operand.Accept(this);

            if (newExpression.Kind() == SyntaxKind.LogicalNotExpression)
            {
                return(((PrefixUnaryExpressionSyntax)newExpression).Operand
                       .WithLeadingTrivia(node.GetLeadingTrivia())
                       .WithTrailingTrivia(node.GetTrailingTrivia()));
            }
            else
            {
                return(node.Operand != newExpression?
                       node.WithOperand(newExpression) : node);
            }
        }
        public override SyntaxNode VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node)
        {
            if (node.Kind() != SyntaxKind.LogicalNotExpression)
            {
                throw new InvalidPreprocessorExpressionException("Expected logical not expression");
            }

            var newExpression = (ExpressionSyntax)node.Operand.Accept(this);
            if (newExpression.Kind() == SyntaxKind.LogicalNotExpression)
            {
                return ((PrefixUnaryExpressionSyntax)newExpression).Operand
                    .WithLeadingTrivia(node.GetLeadingTrivia())
                    .WithTrailingTrivia(node.GetTrailingTrivia());
            }
            else
            {
                return node.Operand != newExpression ?
                    node.WithOperand(newExpression) : node;
            }
        }
        public override SyntaxNode VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node)
        {
            if (node.Kind() != SyntaxKind.UnaryMinusExpression)
            {
                return(base.VisitPrefixUnaryExpression(node));
            }
            var expression = node.Operand;

            if (expression.Kind() != SyntaxKind.NumericLiteralExpression)
            {
                return(base.VisitPrefixUnaryExpression(node));
            }

            CheckCastContex(node);

            string      value    = node.ToString();
            bool        found    = false;
            VirtualData constant = null;

            foreach (var data in _virtualizationContext.data)
            {
                if (value.Equals(data.Name))
                {
                    found    = true;
                    constant = data;
                    break;
                }
            }
            if (!found)
            {
                int              index          = _virtualizationContext.DataIndex;
                string           name           = value;
                SyntaxAnnotation indexMarker    = new SyntaxAnnotation("index", index + "");
                SyntaxAnnotation nameMarker     = new SyntaxAnnotation("name", name);
                SyntaxAnnotation constantMarker = new SyntaxAnnotation("type", "constant");
                SyntaxAnnotation codeMarker     = new SyntaxAnnotation("code", "undefined");
                SyntaxAnnotation uniqueMarker   = new SyntaxAnnotation("unique", "" + VirtualizationContext.UniqueId);

                constant       = new VirtualData();
                constant.Index = index;
                constant.Name  = name;
                var typeInfo = _virtualizationContext.semanticModel.GetTypeInfo(node);
                var info     = typeInfo.Type.ToString();
                constant.Type         = info;
                constant.Node         = node;
                constant.DefaultValue = node;
                constant.Annotations.Add(indexMarker);
                constant.Annotations.Add(nameMarker);
                constant.Annotations.Add(constantMarker);
                constant.Annotations.Add(codeMarker);
                constant.Annotations.Add(uniqueMarker);
                _virtualizationContext.data.Add(constant);
            }

            //            constants.Add(node);



            ExpressionSyntax newNode = SyntaxFactoryExtensions.DataCodeVirtualAccess();

            newNode = newNode.WithAdditionalAnnotations(constant.Annotations);
            ExpressionSyntax newExpression;

            if (CastEnabled)
            {
                newExpression = SyntaxFactory.CastExpression(SyntaxFactory.IdentifierName
                                                             (
                                                                 @"" + constant.Type
                                                             ),
                                                             newNode
                                                             );
            }
            else
            {
                newExpression = newNode;
            }

            //TODO: add annotations + comments
            newExpression = newExpression.WithLeadingTrivia(node.GetLeadingTrivia())
                            .WithTrailingTrivia(node.GetTrailingTrivia())
            ;

            return(newExpression);
        }
Exemplo n.º 4
0
        private static ExpressionSyntax GetNewNode(PrefixUnaryExpressionSyntax logicalNot, Document document)
        {
            ExpressionSyntax operand    = logicalNot.Operand;
            ExpressionSyntax expression = operand.WalkDownParentheses();

            switch (expression.Kind())
            {
            case SyntaxKind.TrueLiteralExpression:
            case SyntaxKind.FalseLiteralExpression:
            {
                LiteralExpressionSyntax newNode = BooleanLiteralExpression(expression.Kind() == SyntaxKind.FalseLiteralExpression);

                newNode = newNode.WithTriviaFrom(expression);

                return(operand.ReplaceNode(expression, newNode));
            }

            case SyntaxKind.LogicalNotExpression:
            {
                return(((PrefixUnaryExpressionSyntax)expression).Operand);
            }

            case SyntaxKind.EqualsExpression:
            case SyntaxKind.NotEqualsExpression:
            case SyntaxKind.LessThanExpression:
            case SyntaxKind.LessThanOrEqualExpression:
            case SyntaxKind.GreaterThanExpression:
            case SyntaxKind.GreaterThanOrEqualExpression:
            {
                BinaryExpressionSyntax newExpression = SyntaxLogicalInverter.GetInstance(document).InvertBinaryExpression((BinaryExpressionSyntax)expression);

                return(operand.ReplaceNode(expression, newExpression));
            }

            case SyntaxKind.InvocationExpression:
            {
                var invocationExpression = (InvocationExpressionSyntax)expression;

                var memberAccessExpression = (MemberAccessExpressionSyntax)invocationExpression.Expression;

                ExpressionSyntax lambdaExpression = invocationExpression.ArgumentList.Arguments[0].Expression.WalkDownParentheses();

                SingleParameterLambdaExpressionInfo lambdaInfo = SyntaxInfo.SingleParameterLambdaExpressionInfo(lambdaExpression);

                var logicalNot2 = (PrefixUnaryExpressionSyntax)SimplifyLogicalNegationAnalyzer.GetReturnExpression(lambdaInfo.Body).WalkDownParentheses();

                InvocationExpressionSyntax newNode = invocationExpression.ReplaceNode(logicalNot2, logicalNot2.Operand.WithTriviaFrom(logicalNot2));

                return(SyntaxRefactorings.ChangeInvokedMethodName(newNode, (memberAccessExpression.Name.Identifier.ValueText == "All") ? "Any" : "All"));
            }

            case SyntaxKind.IsPatternExpression:
            {
                var isPatternExpression = (IsPatternExpressionSyntax)expression;

                var pattern = (ConstantPatternSyntax)isPatternExpression.Pattern;

                UnaryPatternSyntax newPattern = NotPattern(pattern.WithoutTrivia()).WithTriviaFrom(pattern);

                return(isPatternExpression.WithPattern(newPattern)
                       .PrependToLeadingTrivia(logicalNot.GetLeadingTrivia())
                       .AppendToTrailingTrivia(logicalNot.GetTrailingTrivia()));
            }
            }

            return(null);
        }
Exemplo n.º 5
0
        private Method TraversePrefixUnaryExpressions(PrefixUnaryExpressionSyntax pues)
        {
            Method retMethod = new Method();

            if (pues.HasLeadingTrivia)
            {
                SetOuterComments(retMethod, pues.GetLeadingTrivia().ToFullString());
            }

            if (pues.HasTrailingTrivia)
            {
                SetInnerComments(retMethod, pues.GetTrailingTrivia().ToFullString());
            }

            var vars = from aVar in pues.ChildNodes().OfType<IdentifierNameSyntax>() select aVar;
            foreach (IdentifierNameSyntax ins in vars)
            {
                Variables tempVar = new Variables();
                tempVar.Name = ins.Identifier.ValueText;
                retMethod.AccessedVariables.Add(tempVar);
            }
            return retMethod;
        }