Browse Source

Move ConvertedExpression into a separate file

pull/728/head
Daniel Grunwald 11 years ago
parent
commit
30faef021b
  1. 78
      ICSharpCode.Decompiler/CSharp/ConvertedExpression.cs
  2. 74
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

78
ICSharpCode.Decompiler/CSharp/ConvertedExpression.cs

@ -0,0 +1,78 @@ @@ -0,0 +1,78 @@
// Copyright (c) 2014 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 ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.Decompiler.CSharp
{
/// <summary>
/// Output of C# ExpressionBuilder -- a pair of decompiled C# expression and its type
/// </summary>
struct ConvertedExpression
{
public readonly Expression Expression;
public readonly IType Type;
public ConvertedExpression(Expression expression, IType type)
{
this.Expression = expression;
this.Type = type;
}
public Expression ConvertTo(IType targetType, ExpressionBuilder expressionBuilder)
{
if (targetType.IsKnownType(KnownTypeCode.Boolean))
return ConvertToBoolean();
if (Type.Equals(targetType))
return Expression;
if (Type.Kind == TypeKind.ByReference && targetType.Kind == TypeKind.Pointer && Expression is DirectionExpression) {
// convert from reference to pointer
Expression arg = ((DirectionExpression)Expression).Expression.Detach();
var pointerExpr = new ConvertedExpression(
new UnaryOperatorExpression(UnaryOperatorType.AddressOf, arg),
new PointerType(((ByReferenceType)Type).ElementType));
// perform remaining pointer cast, if necessary
return pointerExpr.ConvertTo(targetType, expressionBuilder);
}
if (Expression is PrimitiveExpression) {
object value = ((PrimitiveExpression)Expression).Value;
var rr = expressionBuilder.resolver.ResolveCast(targetType, new ConstantResolveResult(Type, value));
if (rr.IsCompileTimeConstant && !rr.IsError)
return expressionBuilder.astBuilder.ConvertConstantValue(rr);
}
return new CastExpression(
expressionBuilder.astBuilder.ConvertType(targetType),
Expression);
}
public Expression ConvertToBoolean()
{
if (Type.IsKnownType(KnownTypeCode.Boolean) || Type.Kind == TypeKind.Unknown)
return Expression;
else if (Expression is PrimitiveExpression && Type.IsKnownType(KnownTypeCode.Int32))
return new PrimitiveExpression((int)((PrimitiveExpression)Expression).Value != 0);
else if (Type.Kind == TypeKind.Pointer)
return new BinaryOperatorExpression(Expression, BinaryOperatorType.InEquality, new NullReferenceExpression());
else
return new BinaryOperatorExpression(Expression, BinaryOperatorType.InEquality, new PrimitiveExpression(0));
}
}
}

74
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -34,12 +34,12 @@ namespace ICSharpCode.Decompiler.CSharp @@ -34,12 +34,12 @@ namespace ICSharpCode.Decompiler.CSharp
/// <summary>
/// Translates from ILAst to C# expressions.
/// </summary>
class ExpressionBuilder : ILVisitor<ExpressionBuilder.ConvertedExpression>
class ExpressionBuilder : ILVisitor<ConvertedExpression>
{
private readonly ICompilation compilation;
readonly NRefactoryCecilMapper cecilMapper;
readonly CSharpResolver resolver;
readonly TypeSystemAstBuilder astBuilder;
internal readonly CSharpResolver resolver;
internal readonly TypeSystemAstBuilder astBuilder;
public ExpressionBuilder(ICompilation compilation, NRefactoryCecilMapper cecilMapper)
{
@ -48,57 +48,14 @@ namespace ICSharpCode.Decompiler.CSharp @@ -48,57 +48,14 @@ namespace ICSharpCode.Decompiler.CSharp
this.resolver = new CSharpResolver(compilation);
this.astBuilder = new TypeSystemAstBuilder(resolver);
}
internal struct ConvertedExpression
public AstType ConvertType(Mono.Cecil.TypeReference type)
{
public readonly Expression Expression;
public readonly IType Type;
public ConvertedExpression(Expression expression, IType type)
{
this.Expression = expression;
this.Type = type;
}
public Expression ConvertTo(IType targetType, ExpressionBuilder expressionBuilder)
{
if (targetType.IsKnownType(KnownTypeCode.Boolean))
return ConvertToBoolean();
if (Type.Equals(targetType))
return Expression;
if (Type.Kind == TypeKind.ByReference && targetType.Kind == TypeKind.Pointer && Expression is DirectionExpression) {
// convert from reference to pointer
Expression arg = ((DirectionExpression)Expression).Expression.Detach();
var pointerExpr = new ConvertedExpression(
new UnaryOperatorExpression(UnaryOperatorType.AddressOf, arg),
new PointerType(((ByReferenceType)Type).ElementType));
// perform remaining pointer cast, if necessary
return pointerExpr.ConvertTo(targetType, expressionBuilder);
}
if (Expression is PrimitiveExpression) {
object value = ((PrimitiveExpression)Expression).Value;
var rr = expressionBuilder.resolver.ResolveCast(targetType, new ConstantResolveResult(Type, value));
if (rr.IsCompileTimeConstant && !rr.IsError)
return expressionBuilder.astBuilder.ConvertConstantValue(rr);
}
return new CastExpression(
expressionBuilder.astBuilder.ConvertType(targetType),
Expression);
}
public Expression ConvertToBoolean()
{
if (Type.IsKnownType(KnownTypeCode.Boolean) || Type.Kind == TypeKind.Unknown)
return Expression;
else if (Expression is PrimitiveExpression && Type.IsKnownType(KnownTypeCode.Int32))
return new PrimitiveExpression((int)((PrimitiveExpression)Expression).Value != 0);
else if (Type.Kind == TypeKind.Pointer)
return new BinaryOperatorExpression(Expression, BinaryOperatorType.InEquality, new NullReferenceExpression());
else
return new BinaryOperatorExpression(Expression, BinaryOperatorType.InEquality, new PrimitiveExpression(0));
}
if (type == null)
return null;
return astBuilder.ConvertType(cecilMapper.GetType(type));
}
public Expression Convert(ILInstruction inst)
{
return ConvertArgument(inst).Expression;
@ -109,11 +66,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -109,11 +66,10 @@ namespace ICSharpCode.Decompiler.CSharp
return ConvertArgument(inst).ConvertTo(expectedType, this);
}
public AstType ConvertType(Mono.Cecil.TypeReference type)
public Expression ConvertCondition(ILInstruction condition)
{
if (type == null)
return null;
return astBuilder.ConvertType(cecilMapper.GetType(type));
var expr = ConvertArgument(condition);
return expr.ConvertToBoolean();
}
ConvertedExpression ConvertVariable(ILVariable variable)
@ -436,11 +392,5 @@ namespace ICSharpCode.Decompiler.CSharp @@ -436,11 +392,5 @@ namespace ICSharpCode.Decompiler.CSharp
e.AddChild(new Comment(message, CommentType.MultiLine), Roles.Comment);
return new ConvertedExpression(e, SpecialType.UnknownType);
}
public Expression ConvertCondition(ILInstruction condition)
{
var expr = ConvertArgument(condition);
return expr.ConvertToBoolean();
}
}
}

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -62,6 +62,7 @@ @@ -62,6 +62,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CSharp\ConvertedExpression.cs" />
<Compile Include="CSharp\NRefactoryCecilMapper.cs" />
<Compile Include="CSharp\CSharpDecompiler.cs" />
<Compile Include="CSharp\ExpressionBuilder.cs" />

Loading…
Cancel
Save