Browse Source

Improve type inference for ?: operators

pull/728/merge
Siegfried Pammer 9 years ago
parent
commit
5e79cf228c
  1. 25
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

25
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1467,11 +1467,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1467,11 +1467,10 @@ namespace ICSharpCode.Decompiler.CSharp
var condition = TranslateCondition(inst.Condition);
var trueBranch = Translate(inst.TrueInst);
var falseBranch = Translate(inst.FalseInst);
ILInstruction lhsInst, rhsInst;
BinaryOperatorType op = BinaryOperatorType.Any;
TranslatedExpression rhs = default(TranslatedExpression);
if (inst.MatchLogicAnd(out lhsInst, out rhsInst)) {
if (inst.MatchLogicAnd(out var lhsInst, out var rhsInst)) {
op = BinaryOperatorType.ConditionalAnd;
Debug.Assert(rhsInst == inst.TrueInst);
rhs = trueBranch;
@ -1489,15 +1488,21 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1489,15 +1488,21 @@ namespace ICSharpCode.Decompiler.CSharp
.WithRR(new ResolveResult(compilation.FindType(KnownTypeCode.Boolean)));
}
IType targetType;
if (!trueBranch.Type.Equals(SpecialType.NullType) && !falseBranch.Type.Equals(SpecialType.NullType) && !trueBranch.Type.Equals(falseBranch.Type)) {
targetType = compilation.FindType(inst.ResultType.ToKnownTypeCode());
} else {
targetType = trueBranch.Type.Equals(SpecialType.NullType) ? falseBranch.Type : trueBranch.Type;
var rr = resolver.ResolveConditional(condition.ResolveResult, trueBranch.ResolveResult, falseBranch.ResolveResult);
if (rr.IsError) {
IType targetType;
if (!trueBranch.Type.Equals(SpecialType.NullType) && !falseBranch.Type.Equals(SpecialType.NullType) && !trueBranch.Type.Equals(falseBranch.Type)) {
targetType = compilation.FindType(inst.ResultType.ToKnownTypeCode());
} else {
targetType = trueBranch.Type.Equals(SpecialType.NullType) ? falseBranch.Type : trueBranch.Type;
}
trueBranch = trueBranch.ConvertTo(targetType, this);
falseBranch = falseBranch.ConvertTo(targetType, this);
rr = new ResolveResult(targetType);
}
return new ConditionalExpression(condition.Expression, trueBranch.ConvertTo(targetType, this).Expression, falseBranch.ConvertTo(targetType, this).Expression)
return new ConditionalExpression(condition.Expression, trueBranch.Expression, falseBranch.Expression)
.WithILInstruction(inst)
.WithRR(new ResolveResult(targetType));
.WithRR(rr);
}
protected internal override TranslatedExpression VisitAddressOf(AddressOf inst, TranslationContext context)

Loading…
Cancel
Save