Exemplo n.º 1
0
        private TypeSymbol GetTernaryOperationType(TypeManagerContext context, TernaryOperationSyntax syntax)
        {
            var errors = new List <ErrorDiagnostic>();

            // ternary operator requires the condition to be of bool type
            var conditionType = this.GetTypeInfoInternal(context, syntax.ConditionExpression);

            CollectErrors(errors, conditionType);

            var trueType = this.GetTypeInfoInternal(context, syntax.TrueExpression);

            CollectErrors(errors, trueType);

            var falseType = this.GetTypeInfoInternal(context, syntax.FalseExpression);

            CollectErrors(errors, falseType);

            if (errors.Any())
            {
                return(new ErrorTypeSymbol(errors));
            }

            var expectedConditionType = LanguageConstants.Bool;

            if (TypeValidator.AreTypesAssignable(conditionType, expectedConditionType) != true)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.ConditionExpression).ValueTypeMismatch(expectedConditionType.Name)));
            }

            // the return type is the union of true and false expression types
            return(UnionType.Create(trueType, falseType));
        }
Exemplo n.º 2
0
        public override void VisitTernaryOperationSyntax(TernaryOperationSyntax syntax)
        => AssignTypeWithCaching(syntax, () => {
            var errors = new List <ErrorDiagnostic>();

            // ternary operator requires the condition to be of bool type
            var conditionType = VisitAndReturnType(syntax.ConditionExpression);
            CollectErrors(errors, conditionType);

            var trueType = VisitAndReturnType(syntax.TrueExpression);
            CollectErrors(errors, trueType);

            var falseType = VisitAndReturnType(syntax.FalseExpression);
            CollectErrors(errors, falseType);

            if (errors.Any())
            {
                return(new ErrorTypeSymbol(errors));
            }

            var expectedConditionType = LanguageConstants.Bool;
            if (TypeValidator.AreTypesAssignable(conditionType, expectedConditionType) != true)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.ConditionExpression).ValueTypeMismatch(expectedConditionType.Name)));
            }

            // the return type is the union of true and false expression types
            return(UnionType.Create(trueType, falseType));
        });
Exemplo n.º 3
0
 public override void VisitTernaryOperationSyntax(TernaryOperationSyntax syntax) =>
 this.BuildWithSpread(() => base.VisitTernaryOperationSyntax(syntax));
Exemplo n.º 4
0
 public override void VisitTernaryOperationSyntax(TernaryOperationSyntax syntax)
 {
     this.AppendError(syntax);
 }
Exemplo n.º 5
0
 public override void VisitTernaryOperationSyntax(TernaryOperationSyntax syntax)
 {
     this.buffer.Append('(');
     base.VisitTernaryOperationSyntax(syntax);
     this.buffer.Append(')');
 }