Browse Source

Revive the transform from "*(ptr + offset)" to "ptr[offset]"

pull/924/head
Daniel Grunwald 8 years ago
parent
commit
9975c722d5
  1. 13
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUnsafeModifier.cs

13
ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUnsafeModifier.cs

@ -16,17 +16,15 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System.Linq;
using ICSharpCode.Decompiler.CSharp.Syntax; using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler.CSharp.Transforms namespace ICSharpCode.Decompiler.CSharp.Transforms
{ {
public class IntroduceUnsafeModifier : DepthFirstAstVisitor<bool>, IAstTransform public class IntroduceUnsafeModifier : DepthFirstAstVisitor<bool>, IAstTransform
{ {
public static readonly object PointerArithmeticAnnotation = new PointerArithmetic();
sealed class PointerArithmetic {}
public void Run(AstNode compilationUnit, TransformContext context) public void Run(AstNode compilationUnit, TransformContext context)
{ {
compilationUnit.AcceptVisitor(this); compilationUnit.AcceptVisitor(this);
@ -67,8 +65,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{ {
bool result = base.VisitUnaryOperatorExpression(unaryOperatorExpression); bool result = base.VisitUnaryOperatorExpression(unaryOperatorExpression);
if (unaryOperatorExpression.Operator == UnaryOperatorType.Dereference) { if (unaryOperatorExpression.Operator == UnaryOperatorType.Dereference) {
BinaryOperatorExpression bop = unaryOperatorExpression.Expression as BinaryOperatorExpression; var bop = unaryOperatorExpression.Expression as BinaryOperatorExpression;
if (bop != null && bop.Operator == BinaryOperatorType.Add && bop.Annotation<PointerArithmetic>() != null) { if (bop != null && bop.Operator == BinaryOperatorType.Add
&& bop.GetResolveResult() is OperatorResolveResult orr
&& orr.Operands.FirstOrDefault()?.Type.Kind == TypeKind.Pointer)
{
// transform "*(ptr + int)" to "ptr[int]" // transform "*(ptr + int)" to "ptr[int]"
IndexerExpression indexer = new IndexerExpression(); IndexerExpression indexer = new IndexerExpression();
indexer.Target = bop.Left.Detach(); indexer.Target = bop.Left.Detach();

Loading…
Cancel
Save