forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NullableContextAttribute (dotnet#36152)
- Loading branch information
Showing
53 changed files
with
3,232 additions
and
770 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/Compilers/CSharp/Portable/Emitter/Model/MostCommonNullableValueBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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 Microsoft.CodeAnalysis.CSharp.Symbols; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp | ||
{ | ||
internal struct MostCommonNullableValueBuilder | ||
{ | ||
private int _value0; | ||
private int _value1; | ||
private int _value2; | ||
|
||
internal byte? MostCommonValue | ||
{ | ||
get | ||
{ | ||
int max; | ||
byte b; | ||
if (_value1 > _value0) | ||
{ | ||
max = _value1; | ||
b = 1; | ||
} | ||
else | ||
{ | ||
max = _value0; | ||
b = 0; | ||
} | ||
if (_value2 > max) | ||
{ | ||
return 2; | ||
} | ||
return max == 0 ? (byte?)null : b; | ||
} | ||
} | ||
|
||
internal void AddValue(byte value) | ||
{ | ||
switch (value) | ||
{ | ||
case 0: | ||
_value0++; | ||
break; | ||
case 1: | ||
_value1++; | ||
break; | ||
case 2: | ||
_value2++; | ||
break; | ||
default: | ||
throw ExceptionUtilities.UnexpectedValue(value); | ||
} | ||
} | ||
|
||
internal void AddValue(byte? value) | ||
{ | ||
if (value != null) | ||
{ | ||
AddValue(value.GetValueOrDefault()); | ||
} | ||
} | ||
|
||
internal void AddValue(TypeWithAnnotations type) | ||
{ | ||
var builder = ArrayBuilder<byte>.GetInstance(); | ||
type.AddNullableTransforms(builder); | ||
AddValue(GetCommonValue(builder)); | ||
builder.Free(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the common value if all bytes are the same value. | ||
/// Otherwise returns null. | ||
/// </summary> | ||
internal static byte? GetCommonValue(ArrayBuilder<byte> builder) | ||
{ | ||
int n = builder.Count; | ||
if (n == 0) | ||
{ | ||
return null; | ||
} | ||
byte b = builder[0]; | ||
for (int i = 1; i < n; i++) | ||
{ | ||
if (builder[i] != b) | ||
{ | ||
return null; | ||
} | ||
} | ||
return b; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.