This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
forked from dotnet/dotNext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RefAnyValExpression.cs
48 lines (42 loc) · 1.96 KB
/
RefAnyValExpression.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Linq.Expressions;
namespace DotNext.Linq.Expressions
{
using Intrinsics = Runtime.Intrinsics;
/// <summary>
/// Represents expression that is equivalent to <c>__refvalue</c> C# undocumented keyword
/// or <c>refanyval</c> IL instruction.
/// </summary>
public sealed class RefAnyValExpression : CustomExpression
{
/// <summary>
/// Initializes a new expression.
/// </summary>
/// <param name="typedRef">The variable of type <see cref="TypedReference"/>.</param>
/// <param name="referenceType">The type of the managed reference.</param>
/// <exception cref="ArgumentException"><paramref name="typedRef"/> is not of type <see cref="TypedReference"/>.</exception>
public RefAnyValExpression(ParameterExpression typedRef, Type referenceType)
{
TypedReferenceVar = typedRef.Type == typeof(TypedReference) ? typedRef : throw new ArgumentException(ExceptionMessages.TypedReferenceExpected, nameof(typedRef));
ReferenceType = referenceType.IsByRef ? referenceType.GetElementType()! : referenceType;
}
/// <summary>
/// Gets a variable that holds the value of type <see cref="TypedReference"/>.
/// </summary>
public ParameterExpression TypedReferenceVar { get; }
/// <summary>
/// Gets type of the managed reference.
/// </summary>
public Type ReferenceType { get; }
/// <summary>
/// Gets the type of this expression.
/// </summary>
public override Type Type => ReferenceType.MakeByRefType();
/// <summary>
/// Translates this expression into predefined set of expressions
/// using Lowering technique.
/// </summary>
/// <returns>Translated expression.</returns>
public override Expression Reduce() => Call(typeof(Intrinsics), nameof(Intrinsics.AsRef), new[] { ReferenceType }, TypedReferenceVar);
}
}