-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCallBinder.cs
145 lines (117 loc) · 5.14 KB
/
CallBinder.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Reflection;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Semantics;
namespace Cometary.Macros
{
/// <summary>
/// Provides access to the <see cref="IOperation"/> that called
/// the currently executing method.
/// </summary>
public static class CallBinder
{
#region Internal
private struct Disposable : IDisposable
{
public void Dispose()
{
inContext = false;
invocationSymbol = null;
statementSymbol = null;
expressionSyntax = null;
statementSyntax = null;
callerInfo = null;
callerSymbol = null;
methodInfo = null;
methodSymbol = null;
Monitor.Exit(syncObject);
}
}
private static bool inContext;
private static readonly object syncObject = new object();
internal static IDisposable EnterContext(IInvocationExpression invocation, Lazy<IOperation> statement,
InvocationExpressionSyntax invocationSyntax, StatementSyntax stmtSyntax,
MethodInfo mi, IMethodSymbol ms, Lazy<MethodInfo> callerMi, Lazy<IMethodSymbol> callerMs)
{
inContext = true;
invocationSymbol = invocation;
statementSymbol = statement;
expressionSyntax = invocationSyntax;
statementSyntax = stmtSyntax;
callerInfo = callerMi;
callerSymbol = callerMs;
methodInfo = mi;
methodSymbol = ms;
Monitor.Enter(syncObject);
return new Disposable();
}
internal static (ExpressionSyntax Expr, StatementSyntax Stmt) Result => (InvocationSyntax, StatementSyntax);
#endregion
private static Lazy<IOperation> statementSymbol;
private static Lazy<MethodInfo> callerInfo;
private static Lazy<IMethodSymbol> callerSymbol;
private static IInvocationExpression invocationSymbol;
private static ExpressionSyntax expressionSyntax;
private static StatementSyntax statementSyntax;
private static MethodInfo methodInfo;
private static IMethodSymbol methodSymbol;
private static InvalidOperationException NotInContext => new InvalidOperationException("Cannot call CallBinder methods outside of an Expand method during compilation.");
/// <summary>
/// Gets the expression representing the call to the currently
/// executing method.
/// </summary>
public static IInvocationExpression InvocationSymbol => inContext ? invocationSymbol : throw NotInContext;
/// <summary>
/// Gets the statement in which the <see cref="InvocationSymbol"/> is.
/// </summary>
public static IOperation StatementSymbol => inContext ? statementSymbol.Value : throw NotInContext;
/// <summary>
/// Gets the <see cref="MethodInfo"/> describing the target of the call.
/// </summary>
public static MethodInfo TargetInfo => inContext ? methodInfo : throw NotInContext;
/// <summary>
/// Gets the <see cref="IMethodSymbol"/> that represents the target of the call.
/// </summary>
public static IMethodSymbol TargetSymbol => inContext ? methodSymbol : throw NotInContext;
/// <summary>
/// Gets the <see cref="MethodInfo"/> describing the method in which the call to the current method was made.
/// </summary>
public static MethodInfo CallerInfo => inContext ? callerInfo.Value : throw NotInContext;
/// <summary>
/// Gets the <see cref="IMethodSymbol"/> that represents the method in which the call of the current method was made.
/// </summary>
public static IMethodSymbol CallerSymbol => inContext ? callerSymbol.Value : throw NotInContext;
/// <summary>
/// Gets or sets the <see cref="ExpressionSyntax"/> representing the call to the current method.
/// </summary>
public static ExpressionSyntax InvocationSyntax
{
get => inContext ? expressionSyntax : throw NotInContext;
set
{
if (!inContext) throw NotInContext;
expressionSyntax = value ?? throw new ArgumentNullException(nameof(value));
}
}
/// <summary>
/// <para>
/// Gets or sets the syntax of the statement in which the <see cref="InvocationSyntax"/> is.
/// </para>
/// <para>
/// If this value is changed, then the value of the <see cref="InvocationSyntax"/> property will
/// be ignored, and the whole statement replaced.
/// </para>
/// </summary>
public static StatementSyntax StatementSyntax
{
get => inContext ? statementSyntax : throw NotInContext;
set
{
if (!inContext) throw NotInContext;
statementSyntax = value ?? throw new ArgumentNullException(nameof(value));
}
}
}
}