private StatementSyntax GetFinalStatement(
                ExpressionSyntax switchExpression,
                SyntaxTriviaList leadingTrivia,
                ITypeSymbol declaratorToRemoveTypeOpt,
                SyntaxKind nodeToGenerate,
                bool generateDeclaration)
            {
                switch (nodeToGenerate)
                {
                case SyntaxKind.ReturnStatement:
                    return(ReturnStatement(
                               Token(leadingTrivia, SyntaxKind.ReturnKeyword, trailing: default),
                               switchExpression,
                               Token(SyntaxKind.SemicolonToken)));

                case SyntaxKind.ThrowStatement:
                    return(ThrowStatement(
                               Token(leadingTrivia, SyntaxKind.ThrowKeyword, trailing: default),
                               switchExpression,
                               Token(SyntaxKind.SemicolonToken)));
                }

                Debug.Assert(SyntaxFacts.IsAssignmentExpression(nodeToGenerate));
                Debug.Assert(_assignmentTargetOpt != null);

                return(generateDeclaration
                    ? GenerateVariableDeclaration(switchExpression, leadingTrivia, declaratorToRemoveTypeOpt)
                    : GenerateAssignment(switchExpression, nodeToGenerate, leadingTrivia));
            }
Exemple #2
0
        private static ExpressionSyntax ConvertInvocation(InvocationExpressionSyntax invocation)
        {
            var arguments = invocation.ArgumentList.Arguments;

            if (arguments.Count == 1)
            {
                // Assignment expressions in a collection initializer will cause the compiler to
                // report an error.  This is because { a = b } is the form for an object initializer,
                // and the two forms are not allowed to mix/match.  Parenthesize the assignment to
                // avoid the ambiguity.
                var expression = arguments[0].Expression;
                return(SyntaxFacts.IsAssignmentExpression(expression.Kind())
                  ? SyntaxFactory.ParenthesizedExpression(expression)
                  : expression);
            }

            return(SyntaxFactory.InitializerExpression(
                       SyntaxKind.ComplexElementInitializerExpression,
                       SyntaxFactory.Token(SyntaxKind.OpenBraceToken).WithoutTrivia(),
                       SyntaxFactory.SeparatedList(
                           arguments.Select(a => a.Expression),
                           arguments.GetSeparators()
                           ),
                       SyntaxFactory.Token(SyntaxKind.CloseBraceToken).WithoutTrivia()
                       ));
        }
Exemple #3
0
        internal static bool CanBeInitializerExpressionInForStatement(SyntaxKind kind)
        {
            switch (kind)
            {
            case SyntaxKind.InvocationExpression:
            case SyntaxKind.PreIncrementExpression:
            case SyntaxKind.PreDecrementExpression:
            case SyntaxKind.PostIncrementExpression:
            case SyntaxKind.PostDecrementExpression:
            case SyntaxKind.ObjectCreationExpression:
            case SyntaxKind.AwaitExpression:
                return(true);
            }

            return(SyntaxFacts.IsAssignmentExpression(kind));
        }
        public override string VisitBinaryExpression(BinaryExpressionSyntax node)
        {
            string token = node.OperatorToken.ToFullString().Trim();

            if (token == "%")
            {
                throw new ShaderGenerationException(
                          "Modulus operator not supported in shader functions. Use ShaderBuiltins.Mod instead.");
            }

            string leftExpr      = Visit(node.Left);
            string leftExprType  = Utilities.GetFullTypeName(GetModel(node), node.Left);
            string operatorToken = node.OperatorToken.ToString();
            string rightExpr     = Visit(node.Right);
            string rightExprType = Utilities.GetFullTypeName(GetModel(node), node.Right);

            if (SyntaxFacts.IsAssignmentExpression(node.Kind()))
            {
                MarkWritten(node.Left);
            }

            return(_backend.CorrectBinaryExpression(leftExpr, leftExprType, operatorToken, rightExpr, rightExprType));
        }
Exemple #5
0
        private bool IsPossibleExpression()
        {
            var tk = Current.Kind;

            switch (tk)
            {
            case SyntaxKind.FalseKeyword:
            case SyntaxKind.TrueKeyword:
            case SyntaxKind.OpenParenToken:
            case SyntaxKind.IntegerLiteralToken:
            case SyntaxKind.FloatLiteralToken:
            case SyntaxKind.StringLiteralToken:
            case SyntaxKind.IdentifierToken:
            case SyntaxKind.CompileKeyword:
                return(true);

            default:
                return(SyntaxFacts.IsPrefixUnaryExpression(tk) ||
                       (SyntaxFacts.IsPredefinedType(Current) && tk != SyntaxKind.VoidKeyword && Lookahead.Kind == SyntaxKind.OpenParenToken) ||
                       SyntaxFacts.IsAnyUnaryExpression(tk) ||
                       SyntaxFacts.IsBinaryExpression(tk) ||
                       SyntaxFacts.IsAssignmentExpression(tk));
            }
        }
 public static bool IsAnyAssignExpression(this SyntaxNode node)
 => SyntaxFacts.IsAssignmentExpression(node.Kind());
        private ExpressionSyntax ParseSubExpression(uint precedence)
        {
            if (Current.Kind == SyntaxKind.CompileKeyword)
            {
                var compile            = Match(SyntaxKind.CompileKeyword);
                var shaderTarget       = Match(SyntaxKind.IdentifierToken);
                var shaderFunctionName = ParseIdentifier();
                var shaderFunction     = new FunctionInvocationExpressionSyntax(shaderFunctionName, ParseParenthesizedArgumentList(false));
                return(new CompileExpressionSyntax(compile, shaderTarget, shaderFunction));
            }

            ExpressionSyntax leftOperand;
            SyntaxKind       opKind;

            // No left operand, so we need to parse one -- possibly preceded by a
            // unary operator.
            var tk = Current.Kind;

            if (SyntaxFacts.IsPrefixUnaryExpression(tk))
            {
                opKind      = SyntaxFacts.GetPrefixUnaryExpression(tk);
                leftOperand = ParsePrefixUnaryExpression(opKind);
            }
            else
            {
                // Not a unary operator - get a primary expression.
                leftOperand = ParseTerm();
            }

            while (true)
            {
                // We either have a binary or assignment or compound operator here, or we're finished.
                tk = Current.Kind;

                ExpressionOperatorType operatorType;
                if (SyntaxFacts.IsBinaryExpression(tk) &&
                    (!_greaterThanTokenIsNotOperator || tk != SyntaxKind.GreaterThanToken) &&
                    (tk != SyntaxKind.GreaterThanToken || !_allowGreaterThanTokenAroundRhsExpression || Lookahead.Kind != SyntaxKind.SemiToken))
                {
                    operatorType = ExpressionOperatorType.BinaryExpression;
                    opKind       = SyntaxFacts.GetBinaryExpression(tk);
                }
                else if (SyntaxFacts.IsAssignmentExpression(tk))
                {
                    operatorType = ExpressionOperatorType.AssignmentExpression;
                    opKind       = SyntaxFacts.GetAssignmentExpression(tk);
                }
                else if (tk == SyntaxKind.CommaToken && CommaIsSeparatorStack.Peek() == false)
                {
                    operatorType = ExpressionOperatorType.CompoundExpression;
                    opKind       = SyntaxKind.CompoundExpression;
                }
                else
                {
                    break;
                }

                var newPrecedence = SyntaxFacts.GetOperatorPrecedence(opKind);

                Debug.Assert(newPrecedence > 0); // All binary operators must have precedence > 0!

                // Check the precedence to see if we should "take" this operator
                if (newPrecedence < precedence)
                {
                    break;
                }

                // Same precedence, but not right-associative -- deal with this "later"
                if (newPrecedence == precedence && !SyntaxFacts.IsRightAssociative(opKind))
                {
                    break;
                }

                // Precedence is okay, so we'll "take" this operator.
                var opToken = NextToken();

                SyntaxToken lessThanToken = null;
                if (operatorType == ExpressionOperatorType.AssignmentExpression && _allowGreaterThanTokenAroundRhsExpression)
                {
                    lessThanToken = NextTokenIf(SyntaxKind.LessThanToken);
                }

                var rightOperand = ParseSubExpression(newPrecedence);

                SyntaxToken greaterThanToken = null;
                if (lessThanToken != null)
                {
                    greaterThanToken = NextTokenIf(SyntaxKind.GreaterThanToken);
                }

                switch (operatorType)
                {
                case ExpressionOperatorType.BinaryExpression:
                    leftOperand = new BinaryExpressionSyntax(opKind, leftOperand, opToken, rightOperand);
                    break;

                case ExpressionOperatorType.AssignmentExpression:
                    leftOperand = new AssignmentExpressionSyntax(opKind, leftOperand, opToken, lessThanToken, rightOperand, greaterThanToken);
                    break;

                case ExpressionOperatorType.CompoundExpression:
                    leftOperand = new CompoundExpressionSyntax(opKind, leftOperand, opToken, rightOperand);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            var conditionalPrecedence = SyntaxFacts.GetOperatorPrecedence(SyntaxKind.ConditionalExpression);

            if (tk == SyntaxKind.QuestionToken && precedence <= conditionalPrecedence)
            {
                var questionToken = NextToken();

                var colonLeft = ParseSubExpression(conditionalPrecedence);
                var colon     = Match(SyntaxKind.ColonToken);

                var colonRight = ParseSubExpression(conditionalPrecedence);
                leftOperand = new ConditionalExpressionSyntax(leftOperand, questionToken, colonLeft, colon, colonRight);
            }

            return(leftOperand);
        }
 public static bool IsAnyAssignExpression(this SyntaxNode node)
 {
     return(SyntaxFacts.IsAssignmentExpression(node.CSharpKind()));
 }