NullableFlowStateExtensions.cs 2.1 KB
Newer Older
1 2
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

F
Fredric Silberberg 已提交
3
using Roslyn.Utilities;
4

5
namespace Microsoft.CodeAnalysis.CSharp
6 7 8
{
    internal static class NullableFlowStateExtensions
    {
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
        public static bool MayBeNull(this NullableFlowState state) => state == NullableFlowState.MaybeNull;

        public static bool IsNotNull(this NullableFlowState state) => state == NullableFlowState.NotNull;

        /// <summary>
        /// Join nullable flow states from distinct branches during flow analysis.
        /// The result is <see cref="NullableFlowState.MaybeNull"/> if either operand is that.
        /// </summary>
        public static NullableFlowState Join(this NullableFlowState a, NullableFlowState b) => (a > b) ? a : b;

        /// <summary>
        /// Meet two nullable flow states from distinct states for the meet (union) operation in flow analysis.
        /// The result is <see cref="NullableFlowState.NotNull"/> if either operand is that.
        /// </summary>
        public static NullableFlowState Meet(this NullableFlowState a, NullableFlowState b) => (a < b) ? a : b;
24

F
Fredric Silberberg 已提交
25
        internal static CodeAnalysis.NullableFlowState ToPublicFlowState(this NullableFlowState nullableFlowState) => nullableFlowState switch
26
        {
F
Fredric Silberberg 已提交
27 28 29 30
            NullableFlowState.NotNull => CodeAnalysis.NullableFlowState.NotNull,
            NullableFlowState.MaybeNull => CodeAnalysis.NullableFlowState.MaybeNull,
            _ => throw ExceptionUtilities.UnexpectedValue(nullableFlowState)
        };
F
Fredric Silberberg 已提交
31 32

        // PROTOTYPE(nullable-api): remove if possible
F
Fredric Silberberg 已提交
33
        public static NullableFlowState ToInternalFlowState(this CodeAnalysis.NullableFlowState flowState) => flowState switch
F
Fredric Silberberg 已提交
34
        {
F
Fredric Silberberg 已提交
35 36 37 38 39
            CodeAnalysis.NullableFlowState.NotApplicable => NullableFlowState.NotNull,
            CodeAnalysis.NullableFlowState.NotNull => NullableFlowState.NotNull,
            CodeAnalysis.NullableFlowState.MaybeNull => NullableFlowState.MaybeNull,
            _ => throw ExceptionUtilities.UnexpectedValue(flowState)
        };
40 41
    }
}