-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes Nullable<T> initialization with null literal (#323)
such intialization (for instance 'int? i=null;' was generating invalid code
- Loading branch information
Showing
4 changed files
with
127 additions
and
5 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
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,56 @@ | ||
using System.Threading; | ||
using Cecilifier.Core.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace Cecilifier.Core.AST; | ||
|
||
/// <summary> | ||
/// A `Disposable` type used to emit code to handle passing null as arguments to parameters typed as Nullable{T} | ||
/// </summary> | ||
/// <remarks> | ||
/// Since Nullable{T} is a struct passing `null` as an argument the code needs to: | ||
/// 1. Add a synthetic local variable | ||
/// 2. Loads it address to stack | ||
/// 3. Execute InitObj instruction | ||
/// 4. Load the local variable to stack (which will be used as the argument value) | ||
/// | ||
/// This type handles 1, 2 & 4. | ||
/// </remarks> | ||
internal ref struct NullLiteralArgumentDecorator | ||
{ | ||
private string? _localVariableName; | ||
private readonly IVisitorContext? _context; | ||
private readonly string? _ilVar; | ||
|
||
public NullLiteralArgumentDecorator(IVisitorContext context, ArgumentSyntax node, string ilVar) | ||
{ | ||
if (node.Expression is not LiteralExpressionSyntax { RawKind: (int) SyntaxKind.NullLiteralExpression } nullLiteralExpression) | ||
return; | ||
|
||
var argType = context.SemanticModel.GetTypeInfo(node.Expression).ConvertedType.EnsureNotNull(); | ||
if (!SymbolEqualityComparer.Default.Equals(argType.OriginalDefinition, context.RoslynTypeSystem.SystemNullableOfT)) | ||
return; | ||
|
||
// we have a `null` being passed to a Nullable<T> parameter so we need to emit code | ||
// for steps 1 & 2 as outlined in the remarks section above. | ||
var local = context.AddLocalVariableToCurrentMethod("tmpNull", context.TypeResolver.Resolve(argType)); | ||
context.EmitCilInstruction(ilVar, OpCodes.Ldloca_S, local.VariableName); | ||
|
||
_localVariableName = local.VariableName; | ||
_context = context; | ||
_ilVar = ilVar; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// Step 4 (see remarks in the method description) | ||
string localVariable = Interlocked.Exchange(ref _localVariableName, null); | ||
if ( localVariable != null) | ||
{ | ||
_context!.EmitCilInstruction(_ilVar, OpCodes.Ldloc_S, localVariable); | ||
} | ||
} | ||
} |
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