Browse Source

Fix #1828, Fix #2290: Ensure base references have the correct type and are turned into this references and casted if necessary

pull/2315/head
Siegfried Pammer 4 years ago
parent
commit
280a94ce5d
  1. 4
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  2. 8
      ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

4
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -2487,9 +2487,11 @@ namespace ICSharpCode.Decompiler.CSharp
{ {
if (ShouldUseBaseReference()) if (ShouldUseBaseReference())
{ {
var baseReferenceType = resolver.CurrentTypeDefinition.DirectBaseTypes
.FirstOrDefault(t => t.Kind != TypeKind.Interface);
return new BaseReferenceExpression() return new BaseReferenceExpression()
.WithILInstruction(target) .WithILInstruction(target)
.WithRR(new ThisResolveResult(memberDeclaringType, nonVirtualInvocation)); .WithRR(new ThisResolveResult(baseReferenceType ?? memberDeclaringType, nonVirtualInvocation));
} }
else else
{ {

8
ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

@ -452,6 +452,7 @@ namespace ICSharpCode.Decompiler.CSharp
// perform remaining pointer cast, if necessary // perform remaining pointer cast, if necessary
return pointerExpr.ConvertTo(targetType, expressionBuilder); return pointerExpr.ConvertTo(targetType, expressionBuilder);
} }
Expression expr;
if (targetType.Kind == TypeKind.ByReference) if (targetType.Kind == TypeKind.ByReference)
{ {
if (NormalizeTypeVisitor.TypeErasure.EquivalentTypes(targetType, this.Type)) if (NormalizeTypeVisitor.TypeErasure.EquivalentTypes(targetType, this.Type))
@ -481,7 +482,6 @@ namespace ICSharpCode.Decompiler.CSharp
// Convert from integer/pointer to reference. // Convert from integer/pointer to reference.
// First, convert to the corresponding pointer type: // First, convert to the corresponding pointer type:
var arg = this.ConvertTo(new PointerType(elementType), expressionBuilder, checkForOverflow); var arg = this.ConvertTo(new PointerType(elementType), expressionBuilder, checkForOverflow);
Expression expr;
ResolveResult elementRR; ResolveResult elementRR;
if (arg.Expression is UnaryOperatorExpression unary && unary.Operator == UnaryOperatorType.AddressOf) if (arg.Expression is UnaryOperatorExpression unary && unary.Operator == UnaryOperatorType.AddressOf)
{ {
@ -561,7 +561,11 @@ namespace ICSharpCode.Decompiler.CSharp
return this; return this;
} }
} }
var castExpr = new CastExpression(expressionBuilder.ConvertType(targetType), Expression); // BaseReferenceExpression must not be used with CastExpressions
expr = Expression is BaseReferenceExpression
? new ThisReferenceExpression().WithILInstruction(this.ILInstructions)
: Expression;
var castExpr = new CastExpression(expressionBuilder.ConvertType(targetType), expr);
bool needsCheckAnnotation = targetUType.GetStackType().IsIntegerType(); bool needsCheckAnnotation = targetUType.GetStackType().IsIntegerType();
if (needsCheckAnnotation) if (needsCheckAnnotation)
{ {

Loading…
Cancel
Save