mirror of https://github.com/icsharpcode/ILSpy.git
16 changed files with 556 additions and 59 deletions
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
{ |
||||
"profiles": { |
||||
"ILSpy.BamlDecompiler": { |
||||
"commandName": "Executable", |
||||
"executablePath": "C:\\Users\\sie_p\\Projects\\ILSpy\\ILSpy\\bin\\Debug\\net472\\ILSpy.exe", |
||||
"commandLineArgs": "/separate" |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
{ |
||||
"profiles": { |
||||
"ILSpy.ReadyToRun": { |
||||
"commandName": "Executable", |
||||
"executablePath": "C:\\Users\\sie_p\\Projects\\ILSpy\\ILSpy\\bin\\Debug\\net472\\ILSpy.exe", |
||||
"commandLineArgs": "/separate /language:ReadyToRun" |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,99 @@
@@ -0,0 +1,99 @@
|
||||
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.ComponentModel.Composition; |
||||
using System.IO; |
||||
using System.Threading.Tasks; |
||||
|
||||
using ICSharpCode.AvalonEdit.Highlighting; |
||||
using ICSharpCode.Decompiler.Metadata; |
||||
using ICSharpCode.ILSpy.TextView; |
||||
using ICSharpCode.ILSpy.TreeNodes; |
||||
using ICSharpCode.ILSpy.ViewModels; |
||||
|
||||
namespace ICSharpCode.ILSpy.TreeNodes |
||||
{ |
||||
[Export(typeof(IResourceNodeFactory))] |
||||
sealed class JsonResourceNodeFactory : IResourceNodeFactory |
||||
{ |
||||
private readonly static string[] jsonFileExtensions = { ".json" }; |
||||
|
||||
public ILSpyTreeNode CreateNode(Resource resource) |
||||
{ |
||||
Stream stream = resource.TryOpenStream(); |
||||
if (stream == null) |
||||
return null; |
||||
return CreateNode(resource.Name, stream); |
||||
} |
||||
|
||||
public ILSpyTreeNode CreateNode(string key, object data) |
||||
{ |
||||
if (!(data is Stream)) |
||||
return null; |
||||
foreach (string fileExt in jsonFileExtensions) |
||||
{ |
||||
if (key.EndsWith(fileExt, StringComparison.OrdinalIgnoreCase)) |
||||
return new JsonResourceEntryNode(key, (Stream)data); |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
sealed class JsonResourceEntryNode : ResourceEntryNode |
||||
{ |
||||
string json; |
||||
|
||||
public JsonResourceEntryNode(string key, Stream data) |
||||
: base(key, data) |
||||
{ |
||||
} |
||||
|
||||
// TODO : add Json Icon
|
||||
public override object Icon => Images.Resource; |
||||
|
||||
public override bool View(TabPageModel tabPage) |
||||
{ |
||||
AvalonEditTextOutput output = new AvalonEditTextOutput(); |
||||
IHighlightingDefinition highlighting = null; |
||||
|
||||
tabPage.ShowTextView(textView => textView.RunWithCancellation( |
||||
token => Task.Factory.StartNew( |
||||
() => { |
||||
try { |
||||
// cache read XAML because stream will be closed after first read
|
||||
if (json == null) { |
||||
using (var reader = new StreamReader(Data)) { |
||||
json = reader.ReadToEnd(); |
||||
} |
||||
} |
||||
output.Write(json); |
||||
highlighting = HighlightingManager.Instance.GetDefinitionByExtension(".json"); |
||||
} |
||||
catch (Exception ex) { |
||||
output.Write(ex.ToString()); |
||||
} |
||||
return output; |
||||
}, token) |
||||
).Then(t => textView.ShowNode(t, this, highlighting)) |
||||
.HandleExceptions()); |
||||
tabPage.SupportsLanguageSwitching = false; |
||||
return true; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,98 @@
@@ -0,0 +1,98 @@
|
||||
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.ComponentModel.Composition; |
||||
using System.IO; |
||||
using System.Threading.Tasks; |
||||
|
||||
using ICSharpCode.AvalonEdit.Highlighting; |
||||
using ICSharpCode.Decompiler.Metadata; |
||||
using ICSharpCode.ILSpy.TextView; |
||||
using ICSharpCode.ILSpy.TreeNodes; |
||||
using ICSharpCode.ILSpy.ViewModels; |
||||
|
||||
namespace ICSharpCode.ILSpy.TreeNodes |
||||
{ |
||||
[Export(typeof(IResourceNodeFactory))] |
||||
sealed class TextResourceNodeFactory : IResourceNodeFactory |
||||
{ |
||||
private readonly static string[] txtFileExtensions = { ".txt", ".md" }; |
||||
|
||||
public ILSpyTreeNode CreateNode(Resource resource) |
||||
{ |
||||
Stream stream = resource.TryOpenStream(); |
||||
if (stream == null) |
||||
return null; |
||||
return CreateNode(resource.Name, stream); |
||||
} |
||||
|
||||
public ILSpyTreeNode CreateNode(string key, object data) |
||||
{ |
||||
if (!(data is Stream)) |
||||
return null; |
||||
foreach (string fileExt in txtFileExtensions) |
||||
{ |
||||
if (key.EndsWith(fileExt, StringComparison.OrdinalIgnoreCase)) |
||||
return new TextResourceEntryNode(key, (Stream)data); |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
sealed class TextResourceEntryNode : ResourceEntryNode |
||||
{ |
||||
string text; |
||||
|
||||
public TextResourceEntryNode(string key, Stream data) |
||||
: base(key, data) |
||||
{ |
||||
} |
||||
|
||||
public override object Icon => Images.Resource; |
||||
|
||||
public override bool View(TabPageModel tabPage) |
||||
{ |
||||
AvalonEditTextOutput output = new AvalonEditTextOutput(); |
||||
IHighlightingDefinition highlighting = null; |
||||
|
||||
tabPage.ShowTextView(textView => textView.RunWithCancellation( |
||||
token => Task.Factory.StartNew( |
||||
() => { |
||||
try { |
||||
// cache read text because stream will be closed after first read
|
||||
if (text == null) { |
||||
using (var reader = new StreamReader(Data)) { |
||||
text = reader.ReadToEnd(); |
||||
} |
||||
} |
||||
output.Write(text); |
||||
highlighting = null; |
||||
} |
||||
catch (Exception ex) { |
||||
output.Write(ex.ToString()); |
||||
} |
||||
return output; |
||||
}, token) |
||||
).Then(t => textView.ShowNode(t, this, highlighting)) |
||||
.HandleExceptions()); |
||||
tabPage.SupportsLanguageSwitching = false; |
||||
return true; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,152 @@
@@ -0,0 +1,152 @@
|
||||
ILAst Pattern Matching |
||||
================= |
||||
|
||||
Some IL instructions are classified as "patterns". |
||||
```c# |
||||
abstract class PatternMatchILInstruction : IStoreInstruction { |
||||
public ILInstruction TestedOperand { get; } |
||||
public ILVariable Variable { get; } // variable that receives the match result; may also be a temporary |
||||
} |
||||
|
||||
public bool IsPattern(this ILInstruction inst, out ILInstruction testedOperand) => inst switch { |
||||
PatternMatchILInstruction pm => testedOperand = pm.TestedOperand; return true; |
||||
LogicNot logicNot => IsPattern(logicNot.Operand, out testedOperand), |
||||
Comp comp => testedOperand = comp.Left; return IsConstant(comp.Right); |
||||
} |
||||
``` |
||||
Every `match.*` instruction has the following properties: |
||||
* The `TestedOperand` specifies what gets matched against the pattern. |
||||
* The `Variable` stores the value of `TestedOperand` (after converting to the matched type, if appropriate). |
||||
* If this variable is also used outside the `match.*` node, it corresponds to the C# `single_variable_designation`. |
||||
* Otherwise it's a temporary used for pattern matching. |
||||
* I think in both cases it should have VariableKind.PatternVar |
||||
* The match instruction evaluates to `StackType.I4`: 0 if the pattern was matched, 1 otherwise. |
||||
|
||||
Some `match` instructions have a body with `List<ILInstruction> nestedPatterns`. Here every nested pattern must be a pattern according to `IsPattern()`, and the `testedOperand` of each must be a member of the `Variable` of the parent pattern. (members are: field, property, or deconstruction.result). |
||||
(exception: `match.and`/`match.or`, these instead require the `testedOperand` to be exactly the `Variable` of the parent pattern) |
||||
|
||||
|
||||
Examples |
||||
-------- |
||||
1) `expr is var x` |
||||
=> |
||||
`match.var(x = expr)` |
||||
=> |
||||
``` |
||||
Block (VarPattern) { |
||||
stloc x(expr) // single eval expr |
||||
final: ldc.i4 1 // match always |
||||
} |
||||
``` |
||||
2) `expr is T x` |
||||
=> |
||||
`match.type T(x = expr) {}` |
||||
=> |
||||
``` |
||||
Block (TypePattern) { |
||||
stloc x(isinst T(expr)) |
||||
final: x != null |
||||
} |
||||
``` |
||||
3) `expr is C { A: var x } z` |
||||
=> |
||||
``` |
||||
match.type C(z = expr) { |
||||
match.var(x = z.A) |
||||
} |
||||
``` |
||||
=> |
||||
``` |
||||
Block (TypePattern) { |
||||
stloc z(isinst T(expr)) |
||||
final: (z != null) |
||||
&& Block(VarPattern) { |
||||
stloc x(z.A) |
||||
final: ldc.i4 1 |
||||
} |
||||
} |
||||
``` |
||||
4) `expr is C { A: var x, B: 42, C: { A: 4 } } z` |
||||
=> |
||||
``` |
||||
match.type C(z = expr) { |
||||
match.var (x = z.A), |
||||
comp (z.B == 42), |
||||
match.recursive (temp2 = z.C) { |
||||
comp (temp2.A == 4) |
||||
} |
||||
} |
||||
``` |
||||
=> |
||||
``` |
||||
Block (TypePattern) { |
||||
stloc z(isinst C(expr)) |
||||
final: (z != null) |
||||
&& Block(VarPattern) { |
||||
stloc x(z.A) |
||||
final: ldc.i4 1 |
||||
} |
||||
&& comp (z.B == 42) |
||||
&& Block(RecursivePattern) { |
||||
stloc temp2(z.C) |
||||
final: (temp2 != null) |
||||
&& comp (temp2.A == 4) |
||||
} |
||||
} |
||||
``` |
||||
5) `expr is C(var x, var y, <4) { ... }` |
||||
=> |
||||
``` |
||||
match.recursive.type.deconstruct(C tmp1 = expr) { |
||||
match.var(x = deconstruct.result0(tmp1)), |
||||
match.var(y = deconstruct.result1(tmp1)), |
||||
comp(deconstruct.result2(tmp1) < 4), |
||||
} |
||||
``` |
||||
|
||||
6) `expr is C(1, D(2, 3))` |
||||
=> |
||||
``` |
||||
match.type.deconstruct(C c = expr) { |
||||
comp(deconstruct.result0(c) == 1), |
||||
match.type.deconstruct(D d = deconstruct.result1(c)) { |
||||
comp(deconstruct.result0(d) == 2), |
||||
comp(deconstruct.result1(d) == 2), |
||||
} |
||||
} |
||||
``` |
||||
|
||||
7) `x is >= 0 and var y and <= 100` |
||||
``` |
||||
match.and(tmp1 = x) { |
||||
comp(tmp1 >= 0), |
||||
match.var(y = tmp1), |
||||
comp(tmp1 <= 100) |
||||
} |
||||
``` |
||||
|
||||
8) `x is not C _` |
||||
=> |
||||
``` |
||||
logic.not( |
||||
match.type(C tmp1 = x) {} |
||||
) |
||||
``` |
||||
|
||||
9) `expr is (var a, var b)` (when expr is object) |
||||
=> |
||||
``` |
||||
match.type.deconstruct(ITuple tmp = expr) { |
||||
match.var(a = deconstruct.result0(tmp)), |
||||
match.var(b = deconstruct.result1(tmp)), |
||||
} |
||||
``` |
||||
|
||||
10) `expr is (var a, var b)` (when expr is ValueTuple<int, int>) |
||||
=> |
||||
``` |
||||
match.recursive(tmp = expr) { |
||||
match.var(a = tmp.Item1), |
||||
match.var(b = tmp.Item2), |
||||
} |
||||
``` |
Binary file not shown.
Loading…
Reference in new issue