-
Notifications
You must be signed in to change notification settings - Fork 1
/
Locals.tt
48 lines (44 loc) · 2.15 KB
/
Locals.tt
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
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".autogen.cs" #>
using System;
using System.Reflection.Emit;
using JetBrains.Annotations;
namespace ILGeneratorExtensions
{
[PublicAPI]
public static partial class Locals
{
<# foreach (var type in new [] { typeof(bool), typeof(char), typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double) }) { #>
/// <summary>
/// Stores the given value in the given local
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="local">The local to store value in</param>
/// <param name="value">The value to store in the local</param>
/// <exception cref="ArgumentException">Thrown if the local is not of type <see cref="<#= type.Name #>" /></exception>
[PublicAPI]
public static ILGenerator OverwriteLocalWith(this ILGenerator generator, LocalBuilder local, <#= type.Name #> value)
{
if (local.LocalType != typeof(<#= type.Name #>))
{
throw new ArgumentException("Cannot store a <#= type.Name #> value in a local of type " + local.LocalType.Name);
}
return generator.LoadConstant(value)
.StoreInLocal(local);
}
/// <summary>
/// Stores the given value in the given local
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="localName">The name of the fluently-specified local</param>
/// <param name="value">The value to store in the local</param>
/// <exception cref="ArgumentException">Thrown if the local is not of type <see cref="<#= type.Name #>" /></exception>
public static ILGenerator OverwriteLocalWith(this ILGenerator generator, string localName, <#= type.Name #> value)
=> generator.OverwriteLocalWith(generator.GetLocal(localName), value);
<# } #>
}
}