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

using System.Diagnostics;
F
Fredric Silberberg 已提交
4
using Roslyn.Utilities;
5

6
namespace Microsoft.CodeAnalysis.CSharp
7 8 9
{
    internal static class NullableFlowStateExtensions
    {
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
        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;
25

26 27 28 29 30
        internal static CodeAnalysis.NullableFlowState ToPublicFlowState(this NullableFlowState nullableFlowState)
        {
            Debug.Assert((CodeAnalysis.NullableFlowState)(NullableFlowState.NotNull + 1) == CodeAnalysis.NullableFlowState.NotNull);
            return (CodeAnalysis.NullableFlowState)nullableFlowState + 1;
        }
F
Fredric Silberberg 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43

        // PROTOTYPE(nullable-api): remove if possible
        public static NullableFlowState ToInternalFlowState(this CodeAnalysis.NullableFlowState flowState)
        {
            Debug.Assert((CodeAnalysis.NullableFlowState)(NullableFlowState.NotNull + 1) == CodeAnalysis.NullableFlowState.NotNull);
            return flowState switch
            {
                CodeAnalysis.NullableFlowState.NotApplicable => NullableFlowState.NotNull,
                CodeAnalysis.NullableFlowState.NotNull => NullableFlowState.NotNull,
                CodeAnalysis.NullableFlowState.MaybeNull => NullableFlowState.MaybeNull,
                _ => throw ExceptionUtilities.UnexpectedValue(flowState)
            };
        }
44 45
    }
}