Beispiel #1
0
        protected ExpressionSyntax CreateConditional(ExpressionSyntax whenTrue, ExpressionSyntax whenFalse, ITypeSymbol targetType)
        {
            Debug.Assert(whenTrue != null);
            Debug.Assert(whenTrue.FirstAncestorOrSelf <CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
            Debug.Assert(whenFalse != null);
            Debug.Assert(whenFalse.FirstAncestorOrSelf <CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
            Debug.Assert(targetType != null);

            // If there is no conversion between when-true and when-false, we need to insert a cast in
            // one or both of the branches.
            if (!ConversionExists(whenTrue, whenFalse))
            {
                var whenTrueConversion  = SemanticModel.ClassifyConversion(whenTrue, targetType);
                var whenFalseConversion = SemanticModel.ClassifyConversion(whenFalse, targetType);

                if (whenTrueConversion.IsExplicit)
                {
                    whenTrue = whenTrue.CastTo(targetType);
                }
                else if (whenFalseConversion.IsExplicit)
                {
                    whenFalse = whenFalse.CastTo(targetType);
                }
                else if (whenTrueConversion.IsImplicit && whenFalseConversion.IsImplicit)
                {
                    whenTrue = whenTrue.CastTo(targetType);
                }
            }

            var condition = IfStatement.Condition.Kind() == SyntaxKind.SimpleAssignmentExpression
                ? IfStatement.Condition.Parenthesize()
                : IfStatement.Condition;

            ExpressionSyntax result = SyntaxFactory.ConditionalExpression(condition, whenTrue, whenFalse);

            // Ensure that the conditional is implicitly convertible to the target type; otherwise,
            // insert a cast. We do this be speculatively determining the conversion classification
            // of the conditional expression to the target type in the same scope as the original
            // if-statement.
            var conversion = SemanticModel.ClassifyConversion(IfStatement.Span.Start, result, targetType);

            if (conversion.IsExplicit)
            {
                result = result.CastTo(targetType);
            }

            return(result);
        }
Beispiel #2
0
        protected ExpressionSyntax CreateConditional(ExpressionSyntax whenTrue, ExpressionSyntax whenFalse, ITypeSymbol targetType)
        {
            Debug.Assert(whenTrue != null);
            Debug.Assert(whenTrue.FirstAncestorOrSelf<CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
            Debug.Assert(whenFalse != null);
            Debug.Assert(whenFalse.FirstAncestorOrSelf<CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
            Debug.Assert(targetType != null);

            // If there is no conversion between when-true and when-false, we need to insert a cast in
            // one or both of the branches.
            if (!ConversionExists(whenTrue, whenFalse))
            {
                var whenTrueConversion = SemanticModel.ClassifyConversion(whenTrue, targetType);
                var whenFalseConversion = SemanticModel.ClassifyConversion(whenFalse, targetType);

                if (whenTrueConversion.IsExplicit)
                {
                    whenTrue = whenTrue.CastTo(targetType);
                }
                else if (whenFalseConversion.IsExplicit)
                {
                    whenFalse = whenFalse.CastTo(targetType);
                }
                else if (whenTrueConversion.IsImplicit && whenFalseConversion.IsImplicit)
                {
                    whenTrue = whenTrue.CastTo(targetType);
                }
            }

            var condition = IfStatement.Condition.Kind() == SyntaxKind.SimpleAssignmentExpression
                ? IfStatement.Condition.Parenthesize()
                : IfStatement.Condition;

            ExpressionSyntax result = SyntaxFactory.ConditionalExpression(condition, whenTrue, whenFalse);

            // Ensure that the conditional is implicitly convertible to the target type; otherwise,
            // insert a cast. We do this be speculatively determining the conversion classification
            // of the conditional expression to the target type in the same scope as the original
            // if-statement.
            var conversion = SemanticModel.ClassifyConversion(IfStatement.Span.Start, result, targetType);
            if (conversion.IsExplicit)
            {
                result = result.CastTo(targetType);
            }

            return result;
        }