This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathLoadedStep.cs
227 lines (192 loc) · 10.3 KB
/
LoadedStep.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Microsoft.Quantum.QsCompiler.CompilationBuilder;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.Quantum.QsCompiler.DataTypes.Range;
namespace Microsoft.Quantum.QsCompiler
{
/// <summary>
/// Concrete implementation of a rewrite steps with an additional property specifying the dll it was loaded from.
/// </summary>
internal class LoadedStep : IRewriteStep
{
internal Uri Origin { get; }
private readonly IRewriteStep? selfAsStep;
private readonly object selfAsObject;
private readonly MethodInfo[]? interfaceMethods;
private MethodInfo? InterfaceMethod(string name) =>
// This choice of filtering the interface methods may seem a bit particular.
// However, unless you know what you are doing, please don't change it.
// If you are sure you know what you are doing, please make sure the loading via reflection works for rewrite steps
// implemented in both F# or C#, and whether they are compiled against the current compiler version or an older one.
this.interfaceMethods?.FirstOrDefault(method => method.Name.Split("-").Last() == name);
private object? GetViaReflection(string name) =>
this.InterfaceMethod($"get_{name}")?.Invoke(this.selfAsObject, null);
[return: MaybeNull]
private T GetViaReflection<T>(string name) =>
this.InterfaceMethod($"get_{name}")?.Invoke(this.selfAsObject, null) is T result ? result : default;
private void SetViaReflection<T>(string name, T arg) =>
this.InterfaceMethod($"set_{name}")?.Invoke(this.selfAsObject, new object?[] { arg });
[return: MaybeNull]
private T InvokeViaReflection<T>(string name, params object?[] args) =>
this.InterfaceMethod(name)?.Invoke(this.selfAsObject, args) is T result ? result : default;
private LoadedStep()
{
this.Origin = new Uri(this.GetType().Assembly.Location);
this.selfAsObject = new object();
this.Name = "Empty";
}
internal LoadedStep(IRewriteStep rewriteStep, string? outputFolder = null)
{
this.selfAsObject = rewriteStep;
this.Origin = new Uri(rewriteStep.GetType().Assembly.Location);
this.OutputFolder = outputFolder;
this.selfAsStep = rewriteStep;
this.Name = this.selfAsStep.Name;
this.Priority = this.selfAsStep.Priority;
}
/// <summary>
/// Attempts to construct a rewrite step via reflection.
/// Note that the loading via reflection has the consequence that methods may fail on execution.
/// This is e.g. the case if they invoke methods from package references if the corresponding dll
/// has not been copied to output folder of the dll from which the rewrite step is loaded.
/// Throws the corresponding exception if that construction fails.
/// </summary>
internal LoadedStep(object implementation, Type interfaceType, Uri origin, string? outputFolder = null)
{
this.Origin = origin;
this.OutputFolder = outputFolder;
this.selfAsObject = implementation;
// Initializing the _InterfaceMethods even if the implementation implements IRewriteStep
// would result in certain properties being loaded via reflection instead of simply being accessed via _SelfAsStep.
if (this.selfAsObject is IRewriteStep step)
{
this.selfAsStep = step;
}
else
{
this.interfaceMethods = implementation.GetType().GetInterfaceMap(interfaceType).TargetMethods;
}
// The Name and Priority need to be fixed throughout the loading,
// so whatever their value is when loaded that's what these values well be as far at the compiler is concerned.
this.Name = this.selfAsStep?.Name
?? this.GetViaReflection<string>(nameof(IRewriteStep.Name))
?? "(no name)";
this.Priority = this.selfAsStep?.Priority ?? this.GetViaReflection<int>(nameof(IRewriteStep.Priority));
}
public string Name { get; }
public int Priority { get; }
public string? OutputFolder { get; }
internal static Diagnostic ConvertDiagnostic(string stepName, IRewriteStep.Diagnostic diagnostic, Func<DiagnosticSeverity, string?>? getCode = null)
{
var severity =
diagnostic.Severity == CodeAnalysis.DiagnosticSeverity.Error ? DiagnosticSeverity.Error :
diagnostic.Severity == CodeAnalysis.DiagnosticSeverity.Warning ? DiagnosticSeverity.Warning :
diagnostic.Severity == CodeAnalysis.DiagnosticSeverity.Info ? DiagnosticSeverity.Information :
DiagnosticSeverity.Hint;
var addStage =
diagnostic.Stage == IRewriteStep.Stage.PreconditionVerification ||
diagnostic.Stage == IRewriteStep.Stage.PostconditionVerification;
var origin =
string.IsNullOrWhiteSpace(stepName) && !addStage ? $"" :
string.IsNullOrWhiteSpace(stepName) && addStage ? $"[{diagnostic.Stage}] " :
addStage ? $"[{diagnostic.Stage} {stepName}] " : $"[{stepName}] ";
// NOTE: If we change data structure to add or change properties,
// then the cast below in GeneratedDiagnostics needs to be adapted.
return new Diagnostic
{
Code = getCode?.Invoke(severity),
Severity = severity,
Message = $"{origin}{diagnostic.Message}",
Source = diagnostic.Source,
Range = diagnostic.Source is null || diagnostic.Range is null
? new VisualStudio.LanguageServer.Protocol.Range
{
Start = new Position(0, 0),
End = new Position(0, 0),
}
: diagnostic.Range.ToLsp(),
};
}
public IDictionary<string, string?> AssemblyConstants
{
get => this.selfAsStep?.AssemblyConstants
?? this.GetViaReflection<IDictionary<string, string?>>(nameof(IRewriteStep.AssemblyConstants))
?? ImmutableDictionary<string, string?>.Empty;
}
public IEnumerable<IRewriteStep.Diagnostic> GeneratedDiagnostics
{
get
{
if (this.selfAsStep != null)
{
return this.selfAsStep.GeneratedDiagnostics;
}
static bool IEnumerableInterface(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>);
var enumerable = this.GetViaReflection(nameof(IRewriteStep.GeneratedDiagnostics)) as IEnumerable;
var itemType = enumerable?.GetType().GetInterfaces().FirstOrDefault(IEnumerableInterface)?.GetGenericArguments().FirstOrDefault();
if (enumerable is null || itemType is null)
{
return Enumerable.Empty<IRewriteStep.Diagnostic>();
}
var diagnostics = ImmutableArray.CreateBuilder<IRewriteStep.Diagnostic>();
foreach (var obj in enumerable)
{
if (obj == null)
{
continue;
}
diagnostics.Add(new IRewriteStep.Diagnostic
{
Severity = (CodeAnalysis.DiagnosticSeverity)itemType.GetProperty(nameof(IRewriteStep.Diagnostic.Severity)).GetValue(obj, null),
Message = itemType.GetProperty(nameof(IRewriteStep.Diagnostic.Message)).GetValue(obj, null) as string,
Source = itemType.GetProperty(nameof(IRewriteStep.Diagnostic.Source)).GetValue(obj, null) as string,
Range = itemType.GetProperty(nameof(IRewriteStep.Diagnostic.Range)).GetValue(obj, null) as Range,
});
}
return diagnostics.ToImmutable();
}
}
public bool ImplementsTransformation
{
get => this.selfAsStep?.ImplementsTransformation
?? this.GetViaReflection<bool>(nameof(IRewriteStep.ImplementsTransformation));
}
public bool ImplementsPreconditionVerification
{
get => this.selfAsStep?.ImplementsPreconditionVerification
?? this.GetViaReflection<bool>(nameof(IRewriteStep.ImplementsPreconditionVerification));
}
public bool ImplementsPostconditionVerification
{
get => this.selfAsStep?.ImplementsPostconditionVerification
?? this.GetViaReflection<bool>(nameof(IRewriteStep.ImplementsPostconditionVerification));
}
public bool Transformation(QsCompilation compilation, [NotNullWhen(true)] out QsCompilation? transformed)
{
if (this.selfAsStep != null)
{
return this.selfAsStep.Transformation(compilation, out transformed);
}
var args = new object?[] { compilation, null };
var success = this.InvokeViaReflection<bool>(nameof(IRewriteStep.Transformation), args);
transformed = success ? args[1] as QsCompilation ?? compilation : compilation;
return success;
}
public bool PreconditionVerification(QsCompilation compilation) =>
this.selfAsStep?.PreconditionVerification(compilation)
?? this.InvokeViaReflection<bool>(nameof(IRewriteStep.PreconditionVerification), compilation);
public bool PostconditionVerification(QsCompilation? compilation) =>
!(compilation is null)
&& (this.selfAsStep?.PostconditionVerification(compilation)
?? this.InvokeViaReflection<bool>(nameof(IRewriteStep.PostconditionVerification), compilation));
}
}