Exemple #1
0
        public static bool SetType(ref Expression expr, IType dstType, IContext context)
        {
            IType type = expr.EvalType;

            if (!type.CanAssignTo(dstType))
            {
                if (type.CanPromoteTo(dstType))
                {
                    expr = new TypePromotionExpression(expr, new ResolvedType(dstType));
                    expr = expr.ResolveRValue(context);
                }

                /*
                 * else if (type.CanImplicitlyCastTo(dstType))
                 * {
                 * }
                 */
                else if ((dstType.IsEqualTo(Types.GetType("Object")) || dstType.IsEqualTo(Types.GetType("ValueType"))) && (type is ValueTypeSymbol))
                {
                    expr = new BoxExpression(expr);
                    expr = expr.ResolveRValue(context);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        public override ICodeNode VisitBoxExpression(BoxExpression node)
        {
            base.VisitBoxExpression(node);
            if (node.BoxedExpression.CodeNodeType == CodeNodeType.LiteralExpression && node.BoxedAs.FullName == currentTypeSystem.Boolean.FullName)
            {
                FixBooleanLiteral(node.BoxedExpression as LiteralExpression);
                return(node.BoxedExpression.CloneAndAttachInstructions(node.MappedInstructions));
            }

            if (node.BoxedExpression.CodeNodeType == CodeNodeType.ExplicitCastExpression && ((ExplicitCastExpression)node.BoxedExpression).Expression.CodeNodeType == CodeNodeType.ExplicitCastExpression)
            {
                // double cast in a boxed expression;
                ExplicitCastExpression outerCast = node.BoxedExpression as ExplicitCastExpression;
                ExplicitCastExpression innerCast = outerCast.Expression as ExplicitCastExpression;
                if (outerCast.TargetType.FullName == currentTypeSystem.Char.FullName &&
                    innerCast.TargetType.FullName == currentTypeSystem.UInt16.FullName)
                {
                    // Remove the outer cast, as it is produced by the box expression and doesn't have any instructions mapped.
                    // The inner cast contains the instruction, converting the stack value to 2-byte integer (which at this point is known to be char, not ushort).
                    innerCast.TargetType = currentTypeSystem.Char;
                    node.BoxedExpression = innerCast;
                }
            }
            return(node);
        }
 public override ICodeNode VisitBoxExpression(BoxExpression node)
 {
     node = (BoxExpression)this.VisitBoxExpression(node);
     V_0  = node.get_BoxedAs().Resolve();
     if (V_0 != null && V_0.get_IsEnum() && !node.get_BoxedAs().get_IsArray() && node.get_BoxedExpression() as LiteralExpression != null)
     {
         node.set_BoxedExpression(EnumHelper.GetEnumExpression(V_0, node.get_BoxedExpression() as LiteralExpression, this.typeSystem));
     }
     return(node);
 }
Exemple #4
0
        public override ICodeNode VisitBoxExpression(BoxExpression node)
        {
            node = (BoxExpression)base.VisitBoxExpression(node);

            TypeDefinition boxedAs = node.BoxedAs.Resolve();

            if (boxedAs != null && boxedAs.IsEnum && !node.BoxedAs.IsArray)
            {
                if (node.BoxedExpression is LiteralExpression)
                {
                    node.BoxedExpression = EnumHelper.GetEnumExpression(boxedAs, node.BoxedExpression as LiteralExpression, typeSystem);
                }
            }

            return(node);
        }
Exemple #5
0
        private ICodeNode ConvertConstant(MethodInvocationExpression invocation)
        {
            if (invocation.Arguments.Count < 1 || invocation.Arguments.Count > 2)
            {
                return(null);
            }

            Expression result = (Expression)Visit(invocation.Arguments[0]);

            if (invocation.Arguments.Count == 2)
            {
                //This is done in order to mark the Type argument variable
                Visit(invocation.Arguments[1]);
            }

            BoxExpression boxExpression = result as BoxExpression;

            return(boxExpression != null && boxExpression.IsAutoBox ? boxExpression.BoxedExpression : result);
        }
 public override ICodeNode VisitBoxExpression(BoxExpression node)
 {
     dummyVar0 = this.VisitBoxExpression(node);
     if (node.get_BoxedExpression().get_CodeNodeType() == 22 && String.op_Equality(node.get_BoxedAs().get_FullName(), this.currentTypeSystem.get_Boolean().get_FullName()))
     {
         this.FixBooleanLiteral(node.get_BoxedExpression() as LiteralExpression);
         return(node.get_BoxedExpression().CloneAndAttachInstructions(node.get_MappedInstructions()));
     }
     if (node.get_BoxedExpression().get_CodeNodeType() == 31 && ((ExplicitCastExpression)node.get_BoxedExpression()).get_Expression().get_CodeNodeType() == 31)
     {
         stackVariable20 = node.get_BoxedExpression() as ExplicitCastExpression;
         V_0             = stackVariable20.get_Expression() as ExplicitCastExpression;
         if (String.op_Equality(stackVariable20.get_TargetType().get_FullName(), this.currentTypeSystem.get_Char().get_FullName()) && String.op_Equality(V_0.get_TargetType().get_FullName(), this.currentTypeSystem.get_UInt16().get_FullName()))
         {
             V_0.set_TargetType(this.currentTypeSystem.get_Char());
             node.set_BoxedExpression(V_0);
         }
     }
     return(node);
 }
 public override void VisitExpressionStatement(ExpressionStatement node)
 {
     if (!foundWhile)
     {
         if (IsEnumeratorAssignment(node.Expression))
         {
             foundEnumeratorAssignment     = true;
             enumeratorAssignmentStatement = node;
             return;
         }
     }
     else
     {
         if (node.Expression is BinaryExpression)
         {
             if (!IsForeachVariableAssignment(node.Expression as BinaryExpression))
             {
                 if (shouldAdd)
                 {
                     foreachBody.AddStatement(node);
                 }
             }
         }
         else if (node.Expression is MethodInvocationExpression || node.Expression is PropertyReferenceExpression)
         {
             if (IsGetCurrent(node.Expression))
             {
                 //covers the case where the foreach variable is not used
                 this.foreachVariableType = node.Expression.ExpressionType;
             }
             else
             {
                 if (shouldAdd)
                 {
                     foreachBody.AddStatement(node);
                 }
             }
         }
         else if (node.Expression is ExplicitCastExpression)
         {
             ExplicitCastExpression theCast = node.Expression as ExplicitCastExpression;
             if (IsGetCurrent(theCast.Expression))
             {
                 this.foreachVariableType = theCast.ExpressionType;
             }
             else
             {
                 if (shouldAdd)
                 {
                     foreachBody.AddStatement(node);
                 }
             }
         }
         else if (node.Expression is BoxExpression)
         {
             BoxExpression theBox = node.Expression as BoxExpression;
             if (IsGetCurrent(theBox.BoxedExpression))
             {
                 this.foreachVariableType = theBox.BoxedAs;
             }
             else
             {
                 if (shouldAdd)
                 {
                     foreachBody.AddStatement(node);
                 }
             }
         }
         else
         {
             if (shouldAdd)
             {
                 foreachBody.AddStatement(node);
             }
         }
     }
 }
Exemple #8
0
 public virtual void VisitBoxExpression(BoxExpression node)
 {
     Visit(node.BoxedExpression);
 }
Exemple #9
0
 public virtual ICodeNode VisitBoxExpression(BoxExpression node)
 {
     node.BoxedExpression = (Expression)Visit(node.BoxedExpression);
     return(node);
 }
        public override void VisitBoxExpression(BoxExpression node)
        {
            Stack <Expression> removedExpressions = new Stack <Expression>();

            removedExpressions.Push(parentExpressions.Pop());            // pop the box expression
            if (parentExpressions.Count > 0)
            {
                Expression containingExpression = parentExpressions.Peek();
                if (containingExpression is MemberReferenceExpresion)
                {
                    MemberReferenceExpresion memberExpression = parentExpressions.Pop() as MemberReferenceExpresion;
                    removedExpressions.Push(memberExpression);
                    if (memberExpression.Member is MethodReference)
                    {
                        //The box expression is the method target.
                        // This is automatically handled by C#/VB compilers.
                        node.IsAutoBox = true;
                    }
                }
                else if (containingExpression is MethodInvocationExpression ||
                         containingExpression is FieldReferenceExpression ||
                         containingExpression is PropertyReferenceExpression)
                {
                    // The boxed expression is an argument to a method call.
                    // This is automatically handled by C#/VB compilers.
                    node.IsAutoBox = true;
                }
                else if (containingExpression is BinaryExpression)
                {
                    BinaryExpression binary = containingExpression as BinaryExpression;

                    // Check for expressions of the type:
                    //		a = B;
                    //	where a is of type object and B is the boxed expression.
                    if (binary.IsAssignmentExpression && binary.Right.Equals(node))
                    {
                        node.IsAutoBox = true;
                    }

                    // check for expressions of the type:
                    //		X == null
                    //		X != null
                    //		null == X
                    //		null != X
                    //	where X is the boxed expression and X is of some generic parameter type T
                    if (binary.IsComparisonExpression && node.BoxedAs.IsGenericParameter)
                    {
                        if (binary.Left == node)
                        {
                            if (binary.Right is LiteralExpression && (binary.Right as LiteralExpression).Value == null)
                            {
                                node.IsAutoBox = true;
                            }
                        }
                        if (binary.Right == node)
                        {
                            if (binary.Left is LiteralExpression && (binary.Left as LiteralExpression).Value == null)
                            {
                                node.IsAutoBox = true;
                            }
                        }
                    }
                }
                else if (containingExpression is ReturnExpression)
                {
                    if (context.Method.ReturnType.FullName == "System.Object")
                    {
                        node.IsAutoBox = true;
                    }
                    else
                    {
                        TypeDefinition type = node.BoxedExpression.ExpressionType.Resolve();
                        if (type != null && type.IsValueType)
                        {
                            node.IsAutoBox = true;
                        }
                    }
                }
                else if (containingExpression is YieldReturnExpression)
                {
                    YieldReturnExpression yieldReturn = containingExpression as YieldReturnExpression;
                    if (yieldReturn.Expression.Equals(node))
                    {
                        node.IsAutoBox = true;
                    }
                }
            }

            // push back the popped expressions
            while (removedExpressions.Count > 0)
            {
                parentExpressions.Push(removedExpressions.Pop());
            }

            base.VisitBoxExpression(node);
        }
Exemple #11
0
 public virtual void VisitBoxExpression(BoxExpression node)
 {
     this.Visit(node.get_BoxedExpression());
     return;
 }
		AstNode TransformByteCode(ILExpression byteCode)
		{
			object operand = byteCode.Operand;
			AstType operandAsTypeRef = AstBuilder.ConvertType(operand as Cecil.TypeReference);

			List<Ast.Expression> args = new List<Expression>();
			foreach(ILExpression arg in byteCode.Arguments) {
				args.Add((Ast.Expression)TransformExpression(arg));
			}
			Ast.Expression arg1 = args.Count >= 1 ? args[0] : null;
			Ast.Expression arg2 = args.Count >= 2 ? args[1] : null;
			Ast.Expression arg3 = args.Count >= 3 ? args[2] : null;
			
			switch (byteCode.Code) {
					#region Arithmetic
				case ILCode.Add:
				case ILCode.Add_Ovf:
				case ILCode.Add_Ovf_Un:
					{
						BinaryOperatorExpression boe;
						if (byteCode.InferredType is PointerType) {
							if (byteCode.Arguments[0].ExpectedType is PointerType) {
								arg2 = DivideBySize(arg2, ((PointerType)byteCode.InferredType).ElementType);
								boe = new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Add, arg2);
								boe.AddAnnotation(IntroduceUnsafeModifier.PointerArithmeticAnnotation);
							} else if (byteCode.Arguments[1].ExpectedType is PointerType) {
								arg1 = DivideBySize(arg1, ((PointerType)byteCode.InferredType).ElementType);
								boe = new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Add, arg2);
								boe.AddAnnotation(IntroduceUnsafeModifier.PointerArithmeticAnnotation);
							} else {
								boe = new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Add, arg2);
							}
						} else {
							boe = new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Add, arg2);
						}
						boe.AddAnnotation(byteCode.Code == ILCode.Add ? AddCheckedBlocks.UncheckedAnnotation : AddCheckedBlocks.CheckedAnnotation);
						return boe;
					}
				case ILCode.Sub:
				case ILCode.Sub_Ovf:
				case ILCode.Sub_Ovf_Un:
					{
						BinaryOperatorExpression boe;
						if (byteCode.InferredType is PointerType) {
							if (byteCode.Arguments[0].ExpectedType is PointerType) {
								arg2 = DivideBySize(arg2, ((PointerType)byteCode.InferredType).ElementType);
								boe = new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Subtract, arg2);
								boe.WithAnnotation(IntroduceUnsafeModifier.PointerArithmeticAnnotation);
							} else {
								boe = new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Subtract, arg2);
							}
						} else {
							boe = new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Subtract, arg2);
						}
						boe.AddAnnotation(byteCode.Code == ILCode.Sub ? AddCheckedBlocks.UncheckedAnnotation : AddCheckedBlocks.CheckedAnnotation);
						return boe;
					}
					case ILCode.Div:        return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Divide, arg2);
					case ILCode.Div_Un:     return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Divide, arg2);
					case ILCode.Mul:        return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Multiply, arg2).WithAnnotation(AddCheckedBlocks.UncheckedAnnotation);
					case ILCode.Mul_Ovf:    return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Multiply, arg2).WithAnnotation(AddCheckedBlocks.CheckedAnnotation);
					case ILCode.Mul_Ovf_Un: return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Multiply, arg2).WithAnnotation(AddCheckedBlocks.CheckedAnnotation);
					case ILCode.Rem:        return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Modulus, arg2);
					case ILCode.Rem_Un:     return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Modulus, arg2);
					case ILCode.And:        return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.BitwiseAnd, arg2);
					case ILCode.Or:         return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.BitwiseOr, arg2);
					case ILCode.Xor:        return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.ExclusiveOr, arg2);
					case ILCode.Shl:        return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.ShiftLeft, arg2);
					case ILCode.Shr:        return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.ShiftRight, arg2);
					case ILCode.Shr_Un:     return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.ShiftRight, arg2);
					case ILCode.Neg:        return new Ast.UnaryOperatorExpression(UnaryOperatorType.Minus, arg1).WithAnnotation(AddCheckedBlocks.UncheckedAnnotation);
					case ILCode.Not:        return new Ast.UnaryOperatorExpression(UnaryOperatorType.BitNot, arg1);
				case ILCode.PostIncrement:
				case ILCode.PostIncrement_Ovf:
				case ILCode.PostIncrement_Ovf_Un:
					{
						if (arg1 is DirectionExpression)
							arg1 = ((DirectionExpression)arg1).Expression.Detach();
						var uoe = new Ast.UnaryOperatorExpression(
							(int)byteCode.Operand > 0 ? UnaryOperatorType.PostIncrement : UnaryOperatorType.PostDecrement, arg1);
						uoe.AddAnnotation((byteCode.Code == ILCode.PostIncrement) ? AddCheckedBlocks.UncheckedAnnotation : AddCheckedBlocks.CheckedAnnotation);
						return uoe;
					}
					#endregion
					#region Arrays
					case ILCode.Newarr: {
						var ace = new Ast.ArrayCreateExpression();
						ace.Type = operandAsTypeRef;
						ComposedType ct = operandAsTypeRef as ComposedType;
						if (ct != null) {
							// change "new (int[,])[10] to new int[10][,]"
							ct.ArraySpecifiers.MoveTo(ace.AdditionalArraySpecifiers);
						}
						if (byteCode.Code == ILCode.InitArray) {
							ace.Initializer = new ArrayInitializerExpression();
							ace.Initializer.Elements.AddRange(args);
						} else {
							ace.Arguments.Add(arg1);
						}
						return ace;
					}
					case ILCode.InitArray: {
						var ace = new Ast.ArrayCreateExpression();
						ace.Type = operandAsTypeRef;
						ComposedType ct = operandAsTypeRef as ComposedType;
						var arrayType = (ArrayType) operand;
						if (ct != null)
						{
							// change "new (int[,])[10] to new int[10][,]"
							ct.ArraySpecifiers.MoveTo(ace.AdditionalArraySpecifiers);
							ace.Initializer = new ArrayInitializerExpression();
						}
						var newArgs = new List<Expression>();
						foreach (var arrayDimension in arrayType.Dimensions.Skip(1).Reverse())
						{
							int length = (int)arrayDimension.UpperBound - (int)arrayDimension.LowerBound;
							for (int j = 0; j < args.Count; j += length)
							{
								var child = new ArrayInitializerExpression();
								child.Elements.AddRange(args.GetRange(j, length));
								newArgs.Add(child);
							}
							var temp = args;
							args = newArgs;
							newArgs = temp;
							newArgs.Clear();
						}
						ace.Initializer.Elements.AddRange(args);
						return ace;
					}
					case ILCode.Ldlen: return arg1.Member("Length");
				case ILCode.Ldelem_I:
				case ILCode.Ldelem_I1:
				case ILCode.Ldelem_I2:
				case ILCode.Ldelem_I4:
				case ILCode.Ldelem_I8:
				case ILCode.Ldelem_U1:
				case ILCode.Ldelem_U2:
				case ILCode.Ldelem_U4:
				case ILCode.Ldelem_R4:
				case ILCode.Ldelem_R8:
				case ILCode.Ldelem_Ref:
				case ILCode.Ldelem_Any:
					return arg1.Indexer(arg2);
				case ILCode.Ldelema:
					return MakeRef(arg1.Indexer(arg2));
				case ILCode.Stelem_I:
				case ILCode.Stelem_I1:
				case ILCode.Stelem_I2:
				case ILCode.Stelem_I4:
				case ILCode.Stelem_I8:
				case ILCode.Stelem_R4:
				case ILCode.Stelem_R8:
				case ILCode.Stelem_Ref:
				case ILCode.Stelem_Any:
					return new Ast.AssignmentExpression(arg1.Indexer(arg2), arg3);
				case ILCode.CompoundAssignment:
					{
						CastExpression cast = arg1 as CastExpression;
						var boe = cast != null ? (BinaryOperatorExpression)cast.Expression : arg1 as BinaryOperatorExpression;
						// AssignmentExpression doesn't support overloaded operators so they have to be processed to BinaryOperatorExpression
						if (boe == null) {
							var tmp = new ParenthesizedExpression(arg1);
							ReplaceMethodCallsWithOperators.ProcessInvocationExpression((InvocationExpression)arg1);
							boe = (BinaryOperatorExpression)tmp.Expression;
						}
						var assignment = new Ast.AssignmentExpression {
							Left = boe.Left.Detach(),
							Operator = ReplaceMethodCallsWithOperators.GetAssignmentOperatorForBinaryOperator(boe.Operator),
							Right = boe.Right.Detach()
						}.CopyAnnotationsFrom(boe);
						// We do not mark the resulting assignment as RestoreOriginalAssignOperatorAnnotation, because
						// the operator cannot be translated back to the expanded form (as the left-hand expression
						// would be evaluated twice, and might have side-effects)
						if (cast != null) {
							cast.Expression = assignment;
							return cast;
						} else {
							return assignment;
						}
					}
					#endregion
					#region Comparison
					case ILCode.Ceq: return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.Equality, arg2);
					case ILCode.Cne: return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.InEquality, arg2);
					case ILCode.Cgt: return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.GreaterThan, arg2);
					case ILCode.Cgt_Un: {
						// can also mean Inequality, when used with object references
						TypeReference arg1Type = byteCode.Arguments[0].InferredType;
						if (arg1Type != null && !arg1Type.IsValueType) goto case ILCode.Cne;
						goto case ILCode.Cgt;
					}
					case ILCode.Cle_Un: {
						// can also mean Equality, when used with object references
						TypeReference arg1Type = byteCode.Arguments[0].InferredType;
						if (arg1Type != null && !arg1Type.IsValueType) goto case ILCode.Ceq;
						goto case ILCode.Cle;
					}
					case ILCode.Cle: return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.LessThanOrEqual, arg2);
				case ILCode.Cge_Un:
					case ILCode.Cge: return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.GreaterThanOrEqual, arg2);
				case ILCode.Clt_Un:
					case ILCode.Clt:    return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.LessThan, arg2);
					#endregion
					#region Logical
					case ILCode.LogicNot:   return new Ast.UnaryOperatorExpression(UnaryOperatorType.Not, arg1);
					case ILCode.LogicAnd:   return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.ConditionalAnd, arg2);
					case ILCode.LogicOr:    return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.ConditionalOr, arg2);
					case ILCode.TernaryOp:  return new Ast.ConditionalExpression() { Condition = arg1, TrueExpression = arg2, FalseExpression = arg3 };
					case ILCode.NullCoalescing: 	return new Ast.BinaryOperatorExpression(arg1, BinaryOperatorType.NullCoalescing, arg2);
					#endregion
					#region Branch
					case ILCode.Br:         return new Ast.GotoStatement(((ILLabel)byteCode.Operand).Name);
				case ILCode.Brtrue:
					return new Ast.IfElseStatement() {
						Condition = arg1,
						TrueStatement = new BlockStatement() {
							new Ast.GotoStatement(((ILLabel)byteCode.Operand).Name)
						}
					};
					case ILCode.LoopOrSwitchBreak: return new Ast.BreakStatement();
					case ILCode.LoopContinue:      return new Ast.ContinueStatement();
					#endregion
					#region Conversions
				case ILCode.Conv_I1:
				case ILCode.Conv_I2:
				case ILCode.Conv_I4:
				case ILCode.Conv_I8:
				case ILCode.Conv_U1:
				case ILCode.Conv_U2:
				case ILCode.Conv_U4:
				case ILCode.Conv_U8:
				case ILCode.Conv_I:
				case ILCode.Conv_U:
					{
						// conversion was handled by Convert() function using the info from type analysis
						CastExpression cast = arg1 as CastExpression;
						if (cast != null) {
							cast.AddAnnotation(AddCheckedBlocks.UncheckedAnnotation);
						}
						return arg1;
					}
				case ILCode.Conv_R4:
				case ILCode.Conv_R8:
				case ILCode.Conv_R_Un: // TODO
					return arg1;
				case ILCode.Conv_Ovf_I1:
				case ILCode.Conv_Ovf_I2:
				case ILCode.Conv_Ovf_I4:
				case ILCode.Conv_Ovf_I8:
				case ILCode.Conv_Ovf_U1:
				case ILCode.Conv_Ovf_U2:
				case ILCode.Conv_Ovf_U4:
				case ILCode.Conv_Ovf_U8:
				case ILCode.Conv_Ovf_I1_Un:
				case ILCode.Conv_Ovf_I2_Un:
				case ILCode.Conv_Ovf_I4_Un:
				case ILCode.Conv_Ovf_I8_Un:
				case ILCode.Conv_Ovf_U1_Un:
				case ILCode.Conv_Ovf_U2_Un:
				case ILCode.Conv_Ovf_U4_Un:
				case ILCode.Conv_Ovf_U8_Un:
				case ILCode.Conv_Ovf_I:
				case ILCode.Conv_Ovf_U:
				case ILCode.Conv_Ovf_I_Un:
				case ILCode.Conv_Ovf_U_Un:
					{
						// conversion was handled by Convert() function using the info from type analysis
						CastExpression cast = arg1 as CastExpression;
						if (cast != null) {
							cast.AddAnnotation(AddCheckedBlocks.CheckedAnnotation);
						}
						return arg1;
					}
				case ILCode.Unbox_Any:
                    var unboxa = new UnBoxExpression();
                    unboxa.Expression = arg1;
                    unboxa.type = operandAsTypeRef.Clone();
					// unboxing does not require a cast if the argument was an isinst instruction
                    if (arg1 is AsExpression && byteCode.Arguments[0].Code == ILCode.Isinst && TypeAnalysis.IsSameType(operand as TypeReference, byteCode.Arguments[0].Operand as TypeReference))
                        return unboxa;
                    else
                        return unboxa.CastTo(operandAsTypeRef);
						//goto case ILCode.Castclass;
				case ILCode.Castclass:
					if ((byteCode.Arguments[0].InferredType != null && byteCode.Arguments[0].InferredType.IsGenericParameter) || ((Cecil.TypeReference)operand).IsGenericParameter)
						return arg1.CastTo(new PrimitiveType("object")).CastTo(operandAsTypeRef);
					else
						return arg1.CastTo(operandAsTypeRef);
				case ILCode.Isinst:
					return arg1.CastAs(operandAsTypeRef);
				case ILCode.Box:
                    var box = new BoxExpression(); 		
                    box.Expression = arg1; 		
                    box.type = operandAsTypeRef.Clone(); 	
                    return box;
                    //return arg1;
				case ILCode.Unbox:
                    var unbox = new UnBoxExpression();	
                    unbox.Expression = arg1;
                    unbox.type = operandAsTypeRef.Clone();
                    return MakeRef(unbox.CastTo(operandAsTypeRef));
					//return MakeRef(arg1.CastTo(operandAsTypeRef));
					#endregion
					#region Indirect
				case ILCode.Ldind_Ref:
				case ILCode.Ldobj:
					if (arg1 is DirectionExpression)
						return ((DirectionExpression)arg1).Expression.Detach();
					else
						return new UnaryOperatorExpression(UnaryOperatorType.Dereference, arg1);
				case ILCode.Stind_Ref:
				case ILCode.Stobj:
					if (arg1 is DirectionExpression)
						return new AssignmentExpression(((DirectionExpression)arg1).Expression.Detach(), arg2);
					else
						return new AssignmentExpression(new UnaryOperatorExpression(UnaryOperatorType.Dereference, arg1), arg2);
					#endregion
				case ILCode.Arglist:
					return new UndocumentedExpression { UndocumentedExpressionType = UndocumentedExpressionType.ArgListAccess };
					case ILCode.Break:    return InlineAssembly(byteCode, args);
				case ILCode.Call:
				case ILCode.CallGetter:
				case ILCode.CallSetter:
					return TransformCall(false, byteCode, args);
				case ILCode.Callvirt:
				case ILCode.CallvirtGetter:
				case ILCode.CallvirtSetter:
					return TransformCall(true, byteCode,  args);
					case ILCode.Ldftn: {
						Cecil.MethodReference cecilMethod = ((MethodReference)operand);
						var expr = new Ast.IdentifierExpression(cecilMethod.Name);
						expr.TypeArguments.AddRange(ConvertTypeArguments(cecilMethod));
						expr.AddAnnotation(cecilMethod);
						return new IdentifierExpression("ldftn").Invoke(expr)
							.WithAnnotation(new Transforms.DelegateConstruction.Annotation(false));
					}
					case ILCode.Ldvirtftn: {
						Cecil.MethodReference cecilMethod = ((MethodReference)operand);
						var expr = new Ast.IdentifierExpression(cecilMethod.Name);
						expr.TypeArguments.AddRange(ConvertTypeArguments(cecilMethod));
						expr.AddAnnotation(cecilMethod);
						return new IdentifierExpression("ldvirtftn").Invoke(expr)
							.WithAnnotation(new Transforms.DelegateConstruction.Annotation(true));
					}
					case ILCode.Calli:       return InlineAssembly(byteCode, args);
					case ILCode.Ckfinite:    return InlineAssembly(byteCode, args);
					case ILCode.Constrained: return InlineAssembly(byteCode, args);
					case ILCode.Cpblk:       return InlineAssembly(byteCode, args);
					case ILCode.Cpobj:       return InlineAssembly(byteCode, args);
					case ILCode.Dup:         return arg1;
					case ILCode.Endfilter:   return InlineAssembly(byteCode, args);
					case ILCode.Endfinally:  return null;
					case ILCode.Initblk:     return InlineAssembly(byteCode, args);
					case ILCode.Initobj:      return InlineAssembly(byteCode, args);
				case ILCode.DefaultValue:
					return MakeDefaultValue((TypeReference)operand);
					case ILCode.Jmp: return InlineAssembly(byteCode, args);
				case ILCode.Ldc_I4:
					return AstBuilder.MakePrimitive((int)operand, byteCode.InferredType);
				case ILCode.Ldc_I8:
					return AstBuilder.MakePrimitive((long)operand, byteCode.InferredType);
				case ILCode.Ldc_R4:
				case ILCode.Ldc_R8:
				case ILCode.Ldc_Decimal:
					return new Ast.PrimitiveExpression(operand);
				case ILCode.Ldfld:
					if (arg1 is DirectionExpression)
						arg1 = ((DirectionExpression)arg1).Expression.Detach();
					return arg1.Member(((FieldReference) operand).Name).WithAnnotation(operand);
				case ILCode.Ldsfld:
					return AstBuilder.ConvertType(((FieldReference)operand).DeclaringType)
						.Member(((FieldReference)operand).Name).WithAnnotation(operand);
				case ILCode.Stfld:
					if (arg1 is DirectionExpression)
						arg1 = ((DirectionExpression)arg1).Expression.Detach();
					return new AssignmentExpression(arg1.Member(((FieldReference) operand).Name).WithAnnotation(operand), arg2);
				case ILCode.Stsfld:
					return new AssignmentExpression(
						AstBuilder.ConvertType(((FieldReference)operand).DeclaringType)
						.Member(((FieldReference)operand).Name).WithAnnotation(operand),
						arg1);
				case ILCode.Ldflda:
					if (arg1 is DirectionExpression)
						arg1 = ((DirectionExpression)arg1).Expression.Detach();
					return MakeRef(arg1.Member(((FieldReference) operand).Name).WithAnnotation(operand));
				case ILCode.Ldsflda:
					return MakeRef(
						AstBuilder.ConvertType(((FieldReference)operand).DeclaringType)
						.Member(((FieldReference)operand).Name).WithAnnotation(operand));
					case ILCode.Ldloc: {
						ILVariable v = (ILVariable)operand;
						if (!v.IsParameter)
							localVariablesToDefine.Add((ILVariable)operand);
						Expression expr;
						if (v.IsParameter && v.OriginalParameter.Index < 0)
							expr = new ThisReferenceExpression();
						else
							expr = new Ast.IdentifierExpression(((ILVariable)operand).Name).WithAnnotation(operand);
						return v.IsParameter && v.Type is ByReferenceType ? MakeRef(expr) : expr;
					}
					case ILCode.Ldloca: {
						ILVariable v = (ILVariable)operand;
						if (v.IsParameter && v.OriginalParameter.Index < 0)
							return MakeRef(new ThisReferenceExpression());
						if (!v.IsParameter)
							localVariablesToDefine.Add((ILVariable)operand);
						return MakeRef(new Ast.IdentifierExpression(((ILVariable)operand).Name).WithAnnotation(operand));
					}
					case ILCode.Ldnull: return new Ast.NullReferenceExpression();
					case ILCode.Ldstr:  return new Ast.PrimitiveExpression(operand);
				case ILCode.Ldtoken:
					if (operand is Cecil.TypeReference) {
						return AstBuilder.CreateTypeOfExpression((TypeReference)operand).Member("TypeHandle");
					} else {
						Expression referencedEntity;
						string loadName;
						string handleName;
						if (operand is Cecil.FieldReference) {
							loadName = "fieldof";
							handleName = "FieldHandle";
							FieldReference fr = (FieldReference)operand;
							referencedEntity = AstBuilder.ConvertType(fr.DeclaringType).Member(fr.Name).WithAnnotation(fr);
						} else if (operand is Cecil.MethodReference) {
							loadName = "methodof";
							handleName = "MethodHandle";
							MethodReference mr = (MethodReference)operand;
							var methodParameters = mr.Parameters.Select(p => new TypeReferenceExpression(AstBuilder.ConvertType(p.ParameterType)));
							referencedEntity = AstBuilder.ConvertType(mr.DeclaringType).Invoke(mr.Name, methodParameters).WithAnnotation(mr);
						} else {
							loadName = "ldtoken";
							handleName = "Handle";
							referencedEntity = new IdentifierExpression(FormatByteCodeOperand(byteCode.Operand));
						}
						return new IdentifierExpression(loadName).Invoke(referencedEntity).WithAnnotation(new LdTokenAnnotation()).Member(handleName);
					}
					case ILCode.Leave:    return new GotoStatement() { Label = ((ILLabel)operand).Name };
				case ILCode.Localloc:
					{
						PointerType ptrType = byteCode.InferredType as PointerType;
						TypeReference type;
						if (ptrType != null) {
							type = ptrType.ElementType;
						} else {
							type = typeSystem.Byte;
						}
						return new StackAllocExpression {
							Type = AstBuilder.ConvertType(type),
							CountExpression = DivideBySize(arg1, type)
						};
					}
				case ILCode.Mkrefany:
					{
						DirectionExpression dir = arg1 as DirectionExpression;
						if (dir != null) {
							return new UndocumentedExpression {
								UndocumentedExpressionType = UndocumentedExpressionType.MakeRef,
								Arguments = { dir.Expression.Detach() }
							};
						} else {
							return InlineAssembly(byteCode, args);
						}
					}
				case ILCode.Refanytype:
					return new UndocumentedExpression {
						UndocumentedExpressionType = UndocumentedExpressionType.RefType,
						Arguments = { arg1 }
					}.Member("TypeHandle");
				case ILCode.Refanyval:
					return MakeRef(
						new UndocumentedExpression {
							UndocumentedExpressionType = UndocumentedExpressionType.RefValue,
							Arguments = { arg1, new TypeReferenceExpression(operandAsTypeRef) }
						});
					case ILCode.Newobj: {
						Cecil.TypeReference declaringType = ((MethodReference)operand).DeclaringType;
						if (declaringType is ArrayType) {
							ComposedType ct = AstBuilder.ConvertType((ArrayType)declaringType) as ComposedType;
							if (ct != null && ct.ArraySpecifiers.Count >= 1) {
								var ace = new Ast.ArrayCreateExpression();
								ct.ArraySpecifiers.First().Remove();
								ct.ArraySpecifiers.MoveTo(ace.AdditionalArraySpecifiers);
								ace.Type = ct;
								ace.Arguments.AddRange(args);
								return ace;
							}
						}
						if (declaringType.IsAnonymousType()) {
							MethodDefinition ctor = ((MethodReference)operand).Resolve();
							if (methodDef != null) {
								AnonymousTypeCreateExpression atce = new AnonymousTypeCreateExpression();
								if (CanInferAnonymousTypePropertyNamesFromArguments(args, ctor.Parameters)) {
									atce.Initializers.AddRange(args);
								} else {
									for (int i = 0; i < args.Count; i++) {
										atce.Initializers.Add(
											new NamedExpression {
												Identifier = ctor.Parameters[i].Name,
												Expression = args[i]
											});
									}
								}
								return atce;
							}
						}
						var oce = new Ast.ObjectCreateExpression();
						oce.Type = AstBuilder.ConvertType(declaringType);
						oce.Arguments.AddRange(args);
						return oce.WithAnnotation(operand);
					}
					case ILCode.No: return InlineAssembly(byteCode, args);
					case ILCode.Nop: return null;
					case ILCode.Pop: return arg1;
					case ILCode.Readonly: return InlineAssembly(byteCode, args);
				case ILCode.Ret:
					if (methodDef.ReturnType.FullName != "System.Void") {
						return new Ast.ReturnStatement { Expression = arg1 };
					} else {
						return new Ast.ReturnStatement();
					}
					case ILCode.Rethrow: return new Ast.ThrowStatement();
					case ILCode.Sizeof:  return new Ast.SizeOfExpression { Type = operandAsTypeRef };
					case ILCode.Stloc: {
						ILVariable locVar = (ILVariable)operand;
						if (!locVar.IsParameter)
							localVariablesToDefine.Add(locVar);
						return new Ast.AssignmentExpression(new Ast.IdentifierExpression(locVar.Name).WithAnnotation(locVar), arg1);
					}
					case ILCode.Switch: return InlineAssembly(byteCode, args);
					case ILCode.Tail: return InlineAssembly(byteCode, args);
					case ILCode.Throw: return new Ast.ThrowStatement { Expression = arg1 };
					case ILCode.Unaligned: return InlineAssembly(byteCode, args);
					case ILCode.Volatile: return InlineAssembly(byteCode, args);
				case ILCode.YieldBreak:
					return new Ast.YieldBreakStatement();
				case ILCode.YieldReturn:
					return new Ast.YieldReturnStatement { Expression = arg1 };
				case ILCode.InitObject:
				case ILCode.InitCollection:
					{
						ArrayInitializerExpression initializer = new ArrayInitializerExpression();
						for (int i = 1; i < args.Count; i++) {
							Match m = objectInitializerPattern.Match(args[i]);
							if (m.Success) {
								MemberReferenceExpression mre = m.Get<MemberReferenceExpression>("left").Single();
								initializer.Elements.Add(
									new NamedExpression {
										Identifier = mre.MemberName,
										Expression = m.Get<Expression>("right").Single().Detach()
									}.CopyAnnotationsFrom(mre));
							} else {
								m = collectionInitializerPattern.Match(args[i]);
								if (m.Success) {
									if (m.Get("arg").Count() == 1) {
										initializer.Elements.Add(m.Get<Expression>("arg").Single().Detach());
									} else {
										ArrayInitializerExpression argList = new ArrayInitializerExpression();
										foreach (var expr in m.Get<Expression>("arg")) {
											argList.Elements.Add(expr.Detach());
										}
										initializer.Elements.Add(argList);
									}
								} else {
									initializer.Elements.Add(args[i]);
								}
							}
						}
						ObjectCreateExpression oce = arg1 as ObjectCreateExpression;
						DefaultValueExpression dve = arg1 as DefaultValueExpression;
						if (oce != null) {
							oce.Initializer = initializer;
							return oce;
						} else if (dve != null) {
							oce = new ObjectCreateExpression(dve.Type.Detach());
							oce.CopyAnnotationsFrom(dve);
							oce.Initializer = initializer;
							return oce;
						} else {
							return new AssignmentExpression(arg1, initializer);
						}
					}
				case ILCode.InitializedObject:
					return new InitializedObjectExpression();
				case ILCode.Wrap:
					return arg1.WithAnnotation(PushNegation.LiftedOperatorAnnotation);
				case ILCode.AddressOf:
					return MakeRef(arg1);
				case ILCode.ExpressionTreeParameterDeclarations:
					args[args.Count - 1].AddAnnotation(new ParameterDeclarationAnnotation(byteCode));
					return args[args.Count - 1];
				case ILCode.Await:
					return new UnaryOperatorExpression(UnaryOperatorType.Await, arg1);
				case ILCode.NullableOf:
				case ILCode.ValueOf: 
					return arg1;
				default:
					throw new Exception("Unknown OpCode: " + byteCode.Code);
			}
		}
 public void VisitBoxExpression(BoxExpression boxExpression)
 {
     StartNode(boxExpression);
     boxExpression.Expression.AcceptVisitor(this);
     EndNode(boxExpression);
 }
 public virtual ICodeNode VisitBoxExpression(BoxExpression node)
 {
     node.set_BoxedExpression((Expression)this.Visit(node.get_BoxedExpression()));
     return(node);
 }
 public void VisitBoxExpression(BoxExpression boxExpression)
 {
     VisitChildren(boxExpression);
 }
 public override void VisitBoxExpression(BoxExpression node)
 {
     V_0 = new Stack <Expression>();
     V_0.Push(this.parentExpressions.Pop());
     if (this.parentExpressions.get_Count() > 0)
     {
         V_1 = this.parentExpressions.Peek();
         if (V_1 as MemberReferenceExpresion == null)
         {
             if (V_1 as MethodInvocationExpression != null || V_1 as FieldReferenceExpression != null || V_1 as PropertyReferenceExpression != null)
             {
                 node.set_IsAutoBox(true);
             }
             else
             {
                 if (V_1 as BinaryExpression == null)
                 {
                     if (V_1 as ReturnExpression == null)
                     {
                         if (V_1 as YieldReturnExpression != null && (V_1 as YieldReturnExpression).get_Expression().Equals(node))
                         {
                             node.set_IsAutoBox(true);
                         }
                     }
                     else
                     {
                         if (!String.op_Equality(this.context.get_Method().get_ReturnType().get_FullName(), "System.Object"))
                         {
                             V_4 = node.get_BoxedExpression().get_ExpressionType().Resolve();
                             if (V_4 != null && V_4.get_IsValueType())
                             {
                                 node.set_IsAutoBox(true);
                             }
                         }
                         else
                         {
                             node.set_IsAutoBox(true);
                         }
                     }
                 }
                 else
                 {
                     V_3 = V_1 as BinaryExpression;
                     if (V_3.get_IsAssignmentExpression() && V_3.get_Right().Equals(node))
                     {
                         node.set_IsAutoBox(true);
                     }
                     if (V_3.get_IsComparisonExpression() && node.get_BoxedAs().get_IsGenericParameter())
                     {
                         if (V_3.get_Left() == node && V_3.get_Right() as LiteralExpression != null && (V_3.get_Right() as LiteralExpression).get_Value() == null)
                         {
                             node.set_IsAutoBox(true);
                         }
                         if (V_3.get_Right() == node && V_3.get_Left() as LiteralExpression != null && (V_3.get_Left() as LiteralExpression).get_Value() == null)
                         {
                             node.set_IsAutoBox(true);
                         }
                     }
                 }
             }
         }
         else
         {
             V_2 = this.parentExpressions.Pop() as MemberReferenceExpresion;
             V_0.Push(V_2);
             if (V_2.get_Member() as MethodReference != null)
             {
                 node.set_IsAutoBox(true);
             }
         }
     }
     while (V_0.get_Count() > 0)
     {
         this.parentExpressions.Push(V_0.Pop());
     }
     this.VisitBoxExpression(node);
     return;
 }