Inheritance: Telerik.JustDecompiler.Ast.Expressions.Expression
Exemplo n.º 1
0
        private void VisitAddressOfExpression(UnaryExpression node)
        {
            var variableExpression = node.Operand as VariableReferenceExpression;

            if (variableExpression != null)
            {
                this.VisitVariableReferenceExpression(variableExpression);
            }
        }
Exemplo n.º 2
0
 public override void VisitUnaryExpression(UnaryExpression node)
 {
     if (node.Operator == UnaryOperator.AddressOf)
     {
         VisitAddressOfExpression(node);
         return;
     }
     base.VisitUnaryExpression(node);
 }
Exemplo n.º 3
0
        public static Expression Negate(Expression expression, TypeSystem typeSystem)
        {
			if (expression == null)
			{
				return null;
			}

            switch (expression.CodeNodeType)
            {
                case CodeNodeType.BinaryExpression:
                    {
						BinaryExpression binaryExpression = (BinaryExpression)expression;
						if (IsMathOperator(binaryExpression.Operator))
                        {
                            if (binaryExpression.ExpressionType.FullName == "System.Boolean")
                            {
                                return new UnaryExpression(UnaryOperator.LogicalNot, expression, null);
                            }
                            BinaryExpression result =
                                new BinaryExpression(BinaryOperator.ValueEquality, expression, new LiteralExpression(0, typeSystem, null), typeSystem, null);
                            return result;
                        }
                        return NegateBinaryExpression(expression, typeSystem);
                    }
                case CodeNodeType.UnaryExpression:
                    {
                        return NegateUnaryExpression(expression, typeSystem);
                    }
                case CodeNodeType.ConditionExpression:
                    {
                        return NegateConditionExpression(expression, typeSystem);
                    }
            }

            if (expression.CodeNodeType == CodeNodeType.LiteralExpression && expression.ExpressionType.FullName == typeSystem.Boolean.FullName)
            {
                return new LiteralExpression(!(bool)(expression as LiteralExpression).Value, typeSystem, expression.UnderlyingSameMethodInstructions);
            }

            LiteralExpression literalExpression = expression.ExpressionType.GetDefaultValueExpression(typeSystem) as LiteralExpression;
			if (literalExpression != null)
            {
				if (literalExpression.ExpressionType.FullName == typeSystem.Boolean.FullName)
				{
					UnaryExpression unaryResult = new UnaryExpression(UnaryOperator.LogicalNot, expression, null);
					return unaryResult;
				}
                BinaryExpression result = new BinaryExpression(BinaryOperator.ValueEquality, expression, literalExpression, typeSystem, null);
                return result;
            }
            else
            {
                return new UnaryExpression(UnaryOperator.LogicalNot, expression, null);
            }
        }
			public override void  VisitUnaryExpression(UnaryExpression node)
			{
				if ((this.methodInvocationsStackCount == 0 && !(node.Operand is ArgumentReferenceExpression)) && 
					(node.Operator == UnaryOperator.AddressDereference || node.Operator == UnaryOperator.AddressReference || node.Operator == UnaryOperator.AddressOf))
				{
					this.IsAddressUnaryOperatorFound = true;
					this.FoundUnaryOperator = node.Operator;
				}

				base.VisitUnaryExpression(node);
			}
 public override void VisitUnaryExpression(UnaryExpression node)
 {
     if ((node.Operator == UnaryOperator.AddressOf || node.Operator == UnaryOperator.AddressReference) &&
         node.Operand.CodeNodeType == CodeNodeType.VariableReferenceExpression)
     {
         RemoveVariable((node.Operand as VariableReferenceExpression).Variable.Resolve());
     }
     else
     {
         base.VisitUnaryExpression(node);
     }
 }
        private void InsertTopLevelParameterAssignments(BlockStatement block)
        {
            for (int i = 0; i < this.context.MethodContext.OutParametersToAssign.Count; i++)
            {
                ParameterDefinition parameter = this.context.MethodContext.OutParametersToAssign[i];
                TypeReference nonPointerType = parameter.ParameterType.IsByReference ? parameter.ParameterType.GetElementType() : parameter.ParameterType;
                UnaryExpression parameterDereference =
                    new UnaryExpression(UnaryOperator.AddressDereference, new ArgumentReferenceExpression(parameter, null), null);

                BinaryExpression assignExpression = new BinaryExpression(BinaryOperator.Assign, parameterDereference,
                    nonPointerType.GetDefaultValueExpression(typeSystem), nonPointerType, typeSystem, null);

                block.AddStatementAt(i, new ExpressionStatement(assignExpression));
            }
        }
		private void TryProcessUnaryExpression(UnaryExpression node)
		{
			if (processStep == ProcessStep.Replace)
			{
				Expression expression;
				if (TryGetVariableExpression(node.Operand, out expression))
				{
					node.Operand = expression;
				}
			}
		}
        public override void VisitUnaryExpression(UnaryExpression node)
        {
            if (node.Operator == UnaryOperator.Negate ||
                node.Operator == UnaryOperator.LogicalNot ||
                node.Operator == UnaryOperator.BitwiseNot ||
                node.Operator == UnaryOperator.UnaryPlus)
            {
                Write(ToString(node.Operator));
                base.VisitUnaryExpression(node);
                return;
            }

            if (node.Operator == UnaryOperator.AddressOf)
            {
                VisitAddressOfExpression(node);
                return;
            }
            if (node.Operator == UnaryOperator.AddressDereference)
            {
                base.VisitUnaryExpression(node);
                return;
            }
            bool isPostOp = IsPostUnaryOperator(node.Operator);

            bool shouldClone = false;
            if ((node.Operator == UnaryOperator.PostDecrement) ||
                (node.Operator == UnaryOperator.PostIncrement) ||
                (node.Operator == UnaryOperator.PreDecrement) ||
                (node.Operator == UnaryOperator.PreIncrement))
            {
                shouldClone = true;
                Visit(node.Operand);

                WriteSpace();
                WriteToken("=");
                WriteSpace();
            }

            if (!isPostOp)
                Write(ToString(node.Operator));

            Visit(shouldClone ? node.Operand.CloneExpressionOnly() : node.Operand);

            if (isPostOp)
                Write(ToString(node.Operator));
        }
        /// <summary>
        /// Performs a move out operation on the goto statement. For more information see
        /// Chapter 2.2.1 "Outward-movement Transformations"
        /// </summary>
        /// <param name="gotoStatement"></param>
        /// <param name="label"></param>
        private void MoveOut(IfStatement gotoStatement, string label)
        {
            /// Preprocessing.
            BlockStatement containingBlock = gotoStatement.Parent as BlockStatement;
            BlockStatement outerBlock = GetOuterBlock(containingBlock);
            VariableReferenceExpression conditionVar = new VariableReferenceExpression(GetLabelVariable(label), null);

            ExtractConditionIntoVariable(conditionVar.CloneExpressionOnly() as VariableReferenceExpression, gotoStatement, containingBlock);

            Statement oldEnclosingStatement = containingBlock.Parent;
            if (oldEnclosingStatement is SwitchCase)
            {
                oldEnclosingStatement = oldEnclosingStatement.Parent;
            }
            if (containingBlock.Parent is SwitchCase || containingBlock.Parent is WhileStatement || containingBlock.Parent is DoWhileStatement || containingBlock.Parent is ForStatement
                || containingBlock.Parent is ForEachStatement)
            {
                /// Then we can exit using break.
                /// Create the if - break.
                BlockStatement thenBlock = new BlockStatement();
                thenBlock.AddStatement(new BreakStatement(null));  //might have to keep track of brakes
                IfStatement breakIf = new IfStatement(conditionVar.CloneExpressionOnly(), thenBlock, null);

                /// Replace the original goto
                int gotoIndex = containingBlock.Statements.IndexOf(gotoStatement);
                containingBlock.Statements.Remove(gotoStatement);
                containingBlock.AddStatementAt(gotoIndex, breakIf);
            }
            else if (containingBlock.Parent is IfStatement || containingBlock.Parent is TryStatement || containingBlock.Parent is IfElseIfStatement)
            {
                /// Then we can exit via introducing another condition.
                int gotoIndex = containingBlock.Statements.IndexOf(gotoStatement);
                int index = gotoIndex + 1;
                BlockStatement thenBlock = new BlockStatement();
                while (index < containingBlock.Statements.Count)
                {
                    thenBlock.AddStatement(containingBlock.Statements[index]);
                    containingBlock.Statements.RemoveAt(index);
                }
                UnaryExpression condition = new UnaryExpression(UnaryOperator.LogicalNot, conditionVar.CloneExpressionOnly(), null);
                IfStatement innerIf = new IfStatement(condition, thenBlock, null);

                /// At this point the goto statement should be the last one in the block.
                /// Simply replace it with the new if.
                containingBlock.Statements.Remove(gotoStatement);
                /// If the then block is empty, the if should not be added.
                if (innerIf.Then.Statements.Count != 0)
                {
                    containingBlock.AddStatement(innerIf);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("Goto statement can not leave this parent construct.");
            }

            /// Add the goto statement after the construct.
            /// The goto statement shoud be detached from the AST at this point.
            outerBlock.AddStatementAt(outerBlock.Statements.IndexOf(oldEnclosingStatement) + 1, gotoStatement);
        }
		public override void VisitUnaryExpression(UnaryExpression node)
		{
            //Added during refactoring and removal of AddressOfExpression class
            if (node.Operator == UnaryOperator.AddressOf)
            {
                return;
            }

			TryProcessUnaryExpression(node);

			states.Push(Step.Expression);
			base.VisitUnaryExpression(node);
			states.Pop();
		}
        public override ICodeNode VisitUnaryExpression(UnaryExpression node)
        {
            if (status == InliningResult.Abort)
            {
                //sanity check
                throw new Exception("Invalid state");
            }

            Expression originalOperand = node.Operand;
            node.Operand = (Expression)Visit(node.Operand);
            if (node.Operator == UnaryOperator.LogicalNot && originalOperand != node.Operand)
            {
                return Negator.Negate(node.Operand, typeSystem);
            }
            return node;
        }
        public override void VisitUnaryExpression(UnaryExpression node)
        {
            if (node.Operator == UnaryOperator.Negate ||
                node.Operator == UnaryOperator.LogicalNot ||
                node.Operator == UnaryOperator.BitwiseNot ||
                node.Operator == UnaryOperator.UnaryPlus)
            {
                Write(ToString(node.Operator));

                if (node.Operand is SafeCastExpression || node.Operand is CanCastExpression)
                {
                    WriteToken("(");
                }

                base.VisitUnaryExpression(node);

                if (node.Operand is SafeCastExpression || node.Operand is CanCastExpression)
                {
                    WriteToken(")");
                }

                return;
            }

            if (node.Operator == UnaryOperator.AddressOf)
            {
                VisitAddressOfExpression(node);
                return;
            }

            if (node.Operator == UnaryOperator.AddressDereference)
            {
                base.VisitUnaryExpression(node);
                return;
            }

            bool isPostOp = IsPostUnaryOperator(node.Operator);

            if (!isPostOp)
                Write(ToString(node.Operator));

            Visit(node.Operand);

            if (isPostOp)
                Write(ToString(node.Operator));
        }
            public override ICodeNode VisitUnaryExpression(UnaryExpression node)
            {
                if (node.Operator == UnaryOperator.AddressDereference)
                {
					if (node.Operand is UnaryExpression)
					{
						UnaryExpression unaryOperand = node.Operand as UnaryExpression;
						if (unaryOperand.Operator == UnaryOperator.AddressOf || unaryOperand.Operator == UnaryOperator.AddressReference)
						{
							return Visit(unaryOperand.Operand);
						}
					}
					if (node.Operand is CastExpression && node.Operand.ExpressionType.IsByReference)
					{
						CastExpression theCast = node.Operand as CastExpression;
						TypeReference targetType = (theCast.ExpressionType as ByReferenceType).ElementType;
						CastExpression result = new CastExpression(theCast.Expression, targetType, theCast.MappedInstructions);
						return Visit(result);
					}
                }
                return base.VisitUnaryExpression(node);
            }
        public override Expression Clone()
        {
            UnaryExpression result = new UnaryExpression(Operator, Operand.Clone(), instructions);
            result.expressionType = this.expressionType;
			return result;
        }
			public override ICodeNode VisitUnaryExpression(UnaryExpression node)
			{
				if (node.Operator == UnaryOperator.AddressDereference)
				{
					if (!(node.Operand.CodeNodeType == CodeNodeType.VariableReferenceExpression ||
						  node.Operand.CodeNodeType == CodeNodeType.ArgumentReferenceExpression ||
						  node.Operand.CodeNodeType == CodeNodeType.CastExpression))
					{
						node.Operand = new ParenthesesExpression(node.Operand);
					}
				}
				else if (node.Operator != UnaryOperator.None)
				{
					if (node.Operand.CodeNodeType == CodeNodeType.BinaryExpression)
					{
						node.Operand = new ParenthesesExpression(node.Operand);
					}
				}

				return base.VisitUnaryExpression(node);
			}
		private void VisitAddressDereferenceExpression(UnaryExpression node)
		{
			if (node.Operand.CodeNodeType == CodeNodeType.ArgumentReferenceExpression && node.Operand.ExpressionType.IsByReference)
			{
				// out/ref parameters are automatically dereferenced by the compiler.
				// adding the dereference symbol will result in illegal code in C#/VB
				Visit(node.Operand);
				return;
			}
			WriteKeyword(KeyWordWriter.Dereference);
			Visit(node.Operand);
		}
Exemplo n.º 17
0
 public virtual void VisitUnaryExpression(UnaryExpression node)
 {
     Visit(node.Operand);
 }
        public override ICodeNode VisitUnaryExpression(UnaryExpression node)
        {
            node.Operand = (Expression)Visit(node.Operand);
            if (node.Operand == null)
            {
                return null;
            }

            return node;
        }
 public override void VisitUnaryExpression(UnaryExpression node)
 {
     if ((node.Operator == UnaryOperator.AddressOf || node.Operator == UnaryOperator.AddressReference) && node.Operand.CodeNodeType == CodeNodeType.VariableReferenceExpression)
     {
         VariableDefinition variableDefinition = (node.Operand as VariableReferenceExpression).Variable.Resolve();
         if (bannedVariables.Add(variableDefinition))
         {
             singleDefinitionVariables.Remove(variableDefinition);
             singleUsageVariables.Remove(variableDefinition);
         }
         return;
     }
     else if (node.Operator == UnaryOperator.AddressDereference && node.Operand.CodeNodeType == CodeNodeType.UnaryExpression)
     {
         UnaryExpression unaryOperand = node.Operand as UnaryExpression;
         if (unaryOperand.Operator == UnaryOperator.AddressOf || unaryOperand.Operator == UnaryOperator.AddressReference)
         {
             base.Visit(unaryOperand.Operand);
             return;
         }
     }
     base.VisitUnaryExpression(node);
 }
 private void VisitAddressOfExpression(UnaryExpression node)
 {
     if (MethodReferences.Count == 0)
     {
         WriteKeyword(KeyWordWriter.AddressOf);
         WriteSpace();
     }
     Visit(node.Operand);
 }
            public override ICodeNode VisitUnaryExpression(UnaryExpression node)
            {
                if (node.Operator == UnaryOperator.AddressDereference)
                {
                    if (node.Operand.CodeNodeType == CodeNodeType.ThisReferenceExpression)
                    {
                        return node.Operand;
                    }

                    UnaryExpression unaryOperand = node.Operand as UnaryExpression;
                    if (unaryOperand != null && (unaryOperand.Operator == UnaryOperator.AddressReference || unaryOperand.Operator == UnaryOperator.AddressOf))
                    {
                        return Visit(unaryOperand.Operand);
                    }

                    CastExpression castOperand = node.Operand as CastExpression;
                    if (castOperand != null && castOperand.TargetType.IsByReference)
                    {
                        TypeReference targetType = (castOperand.TargetType as ByReferenceType).ElementType;
                        return new CastExpression((Expression)Visit(castOperand.Expression), targetType, null);
                    }
                }
                return base.VisitUnaryExpression(node);
            }
        private ICodeNode VisitCeqExpression(BinaryExpression node)
        {
            //removes <boolean_expression> == true/false
            //and replaces them with (!)<boolean_expression> as appropriate
            if (node.Right is LiteralExpression && node.Left.CodeNodeType != CodeNodeType.LiteralExpression)
            {
				if (node.Left.HasType)
				{
					TypeReference leftType = node.Left.ExpressionType;
					if (leftType.FullName == "System.Boolean")
					{
						LiteralExpression rightSide = node.Right as LiteralExpression;
						bool shouldNegate = false;
						if (rightSide.Value.Equals(false) && node.Operator == BinaryOperator.ValueEquality ||
							rightSide.Value.Equals(true) && node.Operator == BinaryOperator.ValueInequality)
						{
							shouldNegate = true;
						}
						if (shouldNegate)
						{
							UnaryExpression result = new UnaryExpression(UnaryOperator.LogicalNot, node.Left, null);
							return Visit(result);
						}
						return Visit(node.Left);
					}
				}
            }
            else if (node.Left is LiteralExpression && node.Right.CodeNodeType != CodeNodeType.LiteralExpression)
            {
				if (node.Right.HasType)
				{
					TypeReference rightType = node.Right.ExpressionType;
					if (rightType.FullName == "System.Boolean")
					{
						{
							LiteralExpression leftSide = node.Left as LiteralExpression;
							bool shouldNegate = false;
							if (leftSide.Value.Equals(false) && node.Operator == BinaryOperator.ValueEquality ||
								leftSide.Value.Equals(true) && node.Operator == BinaryOperator.ValueInequality)
							{
								shouldNegate = true;
							}
							if (shouldNegate)
							{
								UnaryExpression result = new UnaryExpression(UnaryOperator.LogicalNot, node.Right, null);
								return Visit(result);
							}
							return Visit(node.Left);
						}
					}
				}
            }
            node.Left = (Expression)Visit(node.Left);
            node.Right = (Expression)Visit(node.Right);
            return node;
        }
 public override Expression CloneExpressionOnly()
 {
     UnaryExpression result = new UnaryExpression(Operator, Operand.CloneExpressionOnly(), new Instruction[0]);
     result.expressionType = this.expressionType;
     return result;
 }
        /// <summary>
        /// Fixes expressions, generated for brtrue and brfalse. As 0,false and null are all represented as 0 in IL, this step adds the correct constant
        /// to the expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="isBrTrue">The type of jump.</param>
        /// <returns>Returns the fixed expression.</returns>
        private ICodeNode FixBranchingExpression(Expression expression, Instruction branch)
        {
            /// example:
            /// before the step:
            /// ...
            /// a //a is int32, opperation is brfalse
            /// After the step:
            /// ...
            /// a == 0

            bool isBrTrue = branch.OpCode.Code == Code.Brtrue || branch.OpCode.Code == Code.Brtrue_S;

            TypeReference expressionType = expression.ExpressionType;
            BinaryExpression newCondition;
            BinaryOperator operation = BinaryOperator.ValueEquality;
            Instruction[] instructions = new Instruction[] { branch };
            if (isBrTrue)
            {
                operation = BinaryOperator.ValueInequality;
            }
            if (expressionType.Name == "Boolean" || expressionType.Name.Contains("Boolean "))
            {
                Expression operand;
                if (!isBrTrue)
                {
                    operand = Negator.Negate(expression, typeSystem);
                }
                else
                {
                    operand = expression;
                }

                Expression result;
                if (expression is SafeCastExpression)
                {
                    result = new BinaryExpression(operation, expression, GetLiteralExpression(false, null), typeSystem, instructions);
                }
                else
                {
                    result = new UnaryExpression(UnaryOperator.None, operand, instructions);
                }

                return result;
            }
            if (expressionType.Name == "Char")
            {
                newCondition = new BinaryExpression(operation, expression, GetLiteralExpression('\u0000', null), typeSystem, instructions);
                newCondition.ExpressionType = typeSystem.Boolean;
            }
            if (!expressionType.IsPrimitive)
            {
                TypeDefinition expressionTypeDefinition = expressionType.Resolve();

                /// expressionTypeDefinition can resolve to null when dealing with generics
                if (expressionTypeDefinition != null && expressionTypeDefinition.IsEnum && !expressionType.IsArray)
                {
                    ///Find the field that corresponds to 0
                    FieldDefinition field = null;
                    foreach (FieldDefinition enumField in expressionTypeDefinition.Fields)
                    {
                        if (enumField.Constant != null && enumField.Constant.Value != null && enumField.Constant.Value.Equals(0))
                        {
                            field = enumField;
                            break;
                        }
                    }

                    if (field == null)
                    {
                        newCondition = new BinaryExpression(operation, expression, GetLiteralExpression(0, null), typeSystem, instructions);
                        newCondition.ExpressionType = typeSystem.Boolean;
                    }
                    else
                    {
                        newCondition = new BinaryExpression(operation, expression, new EnumExpression(field, null), typeSystem, instructions);
                        newCondition.ExpressionType = typeSystem.Boolean;
                    }
                }
                else
                {
                    /// If it is not primitive class, then the check should be against null
                    newCondition = new BinaryExpression(operation, expression, GetLiteralExpression(null, null), typeSystem, instructions);
                    newCondition.ExpressionType = typeSystem.Boolean;
                }
            }
            else
            {
                /// This is primitive type, the check should be against 0
                newCondition = new BinaryExpression(operation, expression, GetLiteralExpression(0, null), typeSystem, instructions);
                newCondition.ExpressionType = typeSystem.Boolean;
            }
            return newCondition;
        }
 public override void VisitUnaryExpression(UnaryExpression node)
 {
     if (node.Operator != UnaryOperator.AddressDereference || node.Operand.CodeNodeType != CodeNodeType.VariableReferenceExpression ||
         !variableToAssignExpression.ContainsKey((node.Operand as VariableReferenceExpression).Variable.Resolve()))
     {
         base.VisitUnaryExpression(node);
     }
 }
		public override void VisitUnaryExpression(UnaryExpression node)
		{
			if (node.Operator == UnaryOperator.AddressDereference)
			{
				VisitAddressDereferenceExpression(node);
			}
			else
			{
				Visit(node.Operand);
			}
		}
		public override void VisitUnaryExpression(UnaryExpression node)
		{
			base.VisitUnaryExpression(node);

			if (node.Operator == UnaryOperator.Negate || node.Operator == UnaryOperator.UnaryPlus)
			{
				if (node.Operand.HasType)
				{
					TypeDefinition supposedEnum = node.Operand.ExpressionType.Resolve();
					if (supposedEnum != null && supposedEnum.IsEnum)
					{
						node.Operand = new CastExpression(node.Operand, GetEnumUnderlyingType(supposedEnum), null);
						node.DecideExpressionType();
					}
				}
			}
		}