mirror of https://github.com/icsharpcode/ILSpy.git
62 changed files with 949 additions and 325 deletions
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty |
||||
{ |
||||
internal class Issue1918 |
||||
{ |
||||
public static Guid[] NullVal; |
||||
|
||||
public unsafe void ProblemFunction(Guid[] A_0, int A_1) |
||||
{ |
||||
fixed (Guid* ptr = A_0) { |
||||
void* ptr2 = ptr; |
||||
UIntPtr* ptr3 = (UIntPtr*)((byte*)ptr2 - sizeof(UIntPtr)); |
||||
UIntPtr uIntPtr = *ptr3; |
||||
try { |
||||
*ptr3 = (UIntPtr)(ulong)A_1; |
||||
} finally { |
||||
*ptr3 = uIntPtr; |
||||
} |
||||
} |
||||
fixed (Guid[] ptr = NullVal) { |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,96 @@
@@ -0,0 +1,96 @@
|
||||
// Metadata version: v4.0.30319 |
||||
.assembly extern mscorlib |
||||
{ |
||||
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. |
||||
.ver 4:0:0:0 |
||||
} |
||||
.assembly Issue1918 |
||||
{ |
||||
.ver 1:0:0:0 |
||||
} |
||||
.module Issue1918.exe |
||||
.imagebase 0x00400000 |
||||
.file alignment 0x00000200 |
||||
.stackreserve 0x00100000 |
||||
.subsystem 0x0003 // WINDOWS_CUI |
||||
.corflags 0x00020003 // ILONLY 32BITPREFERRED |
||||
|
||||
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue1918 |
||||
extends [mscorlib]System.Object |
||||
{ |
||||
|
||||
.method public hidebysig |
||||
instance void ProblemFunction (valuetype [mscorlib]System.Guid[] '', int32 '' |
||||
) cil managed |
||||
{ |
||||
.maxstack 2 |
||||
.locals init ( |
||||
[0] valuetype [mscorlib]System.Guid[], |
||||
[1] int32, |
||||
[2] void*, |
||||
[3] valuetype [mscorlib]System.Guid[] pinned, |
||||
[4] native uint*, |
||||
[5] native uint |
||||
) |
||||
|
||||
IL_0000: ldarg.1 |
||||
stloc.0 |
||||
|
||||
IL_0010: |
||||
ldarg.2 |
||||
stloc.1 |
||||
ldloc.0 |
||||
dup |
||||
stloc.3 |
||||
brfalse.s IL_0026 |
||||
|
||||
ldloc.3 |
||||
ldlen |
||||
conv.i4 |
||||
brtrue.s IL_002b |
||||
|
||||
IL_0026: ldc.i4.0 |
||||
conv.u |
||||
stloc.2 |
||||
br.s IL_0034 |
||||
|
||||
IL_002b: ldloc.3 |
||||
ldc.i4.0 |
||||
ldelema [mscorlib]System.Guid |
||||
conv.u |
||||
stloc.2 |
||||
|
||||
IL_0034: ldloc.2 |
||||
sizeof [mscorlib]System.UIntPtr |
||||
sub |
||||
stloc.s 4 |
||||
ldloc.s 4 |
||||
ldind.i |
||||
stloc.s 5 |
||||
.try |
||||
{ |
||||
ldloc.s 4 |
||||
ldloc.1 |
||||
conv.i8 |
||||
call native uint [mscorlib]System.UIntPtr::op_Explicit(uint64) |
||||
stind.i |
||||
ldarg.1 |
||||
|
||||
leave.s IL_005c |
||||
} // end .try |
||||
finally |
||||
{ |
||||
ldloc.s 4 |
||||
ldloc.s 5 |
||||
stind.i |
||||
endfinally |
||||
} // end handler |
||||
|
||||
IL_005c: ldsfld valuetype [mscorlib]System.Guid[] ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue1918::NullVal |
||||
stloc.3 |
||||
ret |
||||
} |
||||
|
||||
.field public static valuetype [mscorlib]System.Guid[] NullVal |
||||
|
||||
} |
@ -0,0 +1,120 @@
@@ -0,0 +1,120 @@
|
||||
// Copyright (c) 2020 Daniel Grunwald
|
||||
//
|
||||
// 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.Diagnostics; |
||||
using System.Linq; |
||||
using ICSharpCode.Decompiler.CSharp.Syntax; |
||||
|
||||
namespace ICSharpCode.Decompiler.CSharp.OutputVisitor |
||||
{ |
||||
/// <summary>
|
||||
/// Used to test for the "F(G<A,B>(7));" grammar ambiguity.
|
||||
/// </summary>
|
||||
class GenericGrammarAmbiguityVisitor : DepthFirstAstVisitor<bool> |
||||
{ |
||||
/// <summary>
|
||||
/// Resolves ambiguities in the specified syntax tree.
|
||||
/// This method must be called after the InsertParenthesesVisitor, because the ambiguity depends on whether the
|
||||
/// final `>` in the possible-type-argument is followed by an opening parenthesis.
|
||||
/// </summary>
|
||||
public static void ResolveAmbiguities(AstNode rootNode) |
||||
{ |
||||
foreach (var node in rootNode.Descendants.OfType<BinaryOperatorExpression>()) { |
||||
if (CausesAmbiguityWithGenerics(node)) { |
||||
node.ReplaceWith(n => new ParenthesizedExpression(n)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static bool CausesAmbiguityWithGenerics(BinaryOperatorExpression binaryOperatorExpression) |
||||
{ |
||||
if (binaryOperatorExpression.Operator != BinaryOperatorType.LessThan) |
||||
return false; |
||||
|
||||
var v = new GenericGrammarAmbiguityVisitor(); |
||||
v.genericNestingLevel = 1; |
||||
|
||||
for (AstNode node = binaryOperatorExpression.Right; node != null; node = node.GetNextNode()) { |
||||
if (node.AcceptVisitor(v)) |
||||
return v.ambiguityFound; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
int genericNestingLevel; |
||||
bool ambiguityFound; |
||||
|
||||
protected override bool VisitChildren(AstNode node) |
||||
{ |
||||
// unhandled node: probably not syntactically valid in a typename
|
||||
|
||||
// These are preconditions for all recursive Visit() calls.
|
||||
Debug.Assert(genericNestingLevel > 0); |
||||
Debug.Assert(!ambiguityFound); |
||||
|
||||
// The return value merely indicates whether to stop visiting.
|
||||
return true; // stop visiting, no ambiguity found
|
||||
} |
||||
|
||||
public override bool VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression) |
||||
{ |
||||
if (binaryOperatorExpression.Left.AcceptVisitor(this)) |
||||
return true; |
||||
Debug.Assert(genericNestingLevel > 0); |
||||
switch (binaryOperatorExpression.Operator) { |
||||
case BinaryOperatorType.LessThan: |
||||
genericNestingLevel += 1; |
||||
break; |
||||
case BinaryOperatorType.GreaterThan: |
||||
genericNestingLevel--; |
||||
break; |
||||
case BinaryOperatorType.ShiftRight when genericNestingLevel >= 2: |
||||
genericNestingLevel -= 2; |
||||
break; |
||||
default: |
||||
return true; // stop visiting, no ambiguity found
|
||||
} |
||||
if (genericNestingLevel == 0) { |
||||
// Of the all tokens that might follow `>` and trigger the ambiguity to be resolved in favor of generics,
|
||||
// `(` is the only one that might start an expression.
|
||||
ambiguityFound = binaryOperatorExpression.Right is ParenthesizedExpression; |
||||
return true; // stop visiting
|
||||
} |
||||
return binaryOperatorExpression.Right.AcceptVisitor(this); |
||||
} |
||||
|
||||
public override bool VisitIdentifierExpression(IdentifierExpression identifierExpression) |
||||
{ |
||||
// identifier could also be valid in a type argument
|
||||
return false; // keep visiting
|
||||
} |
||||
|
||||
public override bool VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) |
||||
{ |
||||
return false; // keep visiting
|
||||
} |
||||
|
||||
public override bool VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression) |
||||
{ |
||||
// MRE could also be valid in a type argument
|
||||
return memberReferenceExpression.Target.AcceptVisitor(this); |
||||
} |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue