/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Compilers/CSharp/Portable/Utilities/ValueSetFactory.BoolValueSetFactory.cs
65 строк
2 KB
AlekseyTs
Pattern matching: Implement value set for a union of types
07 дек 2025, 00:51
07 дек 2025, 00:51
3b33619
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Diagnostics; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp { using static BinaryOperatorKind; internal static partial class ValueSetFactory { /// <summary> /// A value set factory for boolean values. /// </summary> private sealed class BoolValueSetFactory : IConstantValueSetFactory<bool> { public static readonly BoolValueSetFactory Instance = new BoolValueSetFactory(); private BoolValueSetFactory() { } IConstantValueSet IConstantValueSetFactory.AllValues => BoolValueSet.AllValues; IConstantValueSet IConstantValueSetFactory.NoValues => BoolValueSet.None; public IConstantValueSet<bool> Related(BinaryOperatorKind relation, bool value) { switch (relation, value) { case (Equal, true): return BoolValueSet.OnlyTrue; case (Equal, false): return BoolValueSet.OnlyFalse; default: // for error recovery return BoolValueSet.AllValues; } } IConstantValueSet IConstantValueSetFactory.Random(int expectedSize, Random random) => random.Next(4) switch { 0 => BoolValueSet.None, 1 => BoolValueSet.OnlyFalse, 2 => BoolValueSet.OnlyTrue, 3 => BoolValueSet.AllValues, _ => throw ExceptionUtilities.UnexpectedValue("random"), }; ConstantValue IConstantValueSetFactory.RandomValue(Random random) => ConstantValue.Create(random.NextDouble() < 0.5); IConstantValueSet IConstantValueSetFactory.Related(BinaryOperatorKind relation, ConstantValue value) { return value.IsBad ? BoolValueSet.AllValues : Related(relation, value.BooleanValue); } bool IConstantValueSetFactory.Related(BinaryOperatorKind relation, ConstantValue left, ConstantValue right) { Debug.Assert(relation == BinaryOperatorKind.Equal); return left.IsBad || right.IsBad || left.BooleanValue == right.BooleanValue; } } } }