public static void InsertNumericUnboxOrPrimitiveTypeCoercion(ILGenerator gen, TypeDescriptor stackDescriptor, TypeDescriptor targetDescriptor) { if (stackDescriptor.IsBoxedNumber) { gen.Emit(OpCodes.Unbox_Any, stackDescriptor.Value); CodeFlow.InsertAnyNecessaryTypeConversionBytecodes(gen, targetDescriptor, stackDescriptor.UnBox()); } else { CodeFlow.InsertAnyNecessaryTypeConversionBytecodes(gen, targetDescriptor, stackDescriptor); } }
public static void InsertAnyNecessaryTypeConversionBytecodes(ILGenerator gen, TypeDescriptor targetDescriptor, TypeDescriptor stackDescriptor) { if (CodeFlow.IsValueType(stackDescriptor)) { if (stackDescriptor == TypeDescriptor.I || stackDescriptor == TypeDescriptor.B || stackDescriptor == TypeDescriptor.S || stackDescriptor == TypeDescriptor.C) { if (targetDescriptor == TypeDescriptor.D) { gen.Emit(OpCodes.Conv_R8); } else if (targetDescriptor == TypeDescriptor.F) { gen.Emit(OpCodes.Conv_R4); } else if (targetDescriptor == TypeDescriptor.J) { if (stackDescriptor == TypeDescriptor.I || stackDescriptor == TypeDescriptor.S) { gen.Emit(OpCodes.Conv_I8); } else { gen.Emit(OpCodes.Conv_U8); } } else if (targetDescriptor == TypeDescriptor.I) { gen.Emit(OpCodes.Conv_I4); } else { throw new InvalidOperationException("Cannot get from " + stackDescriptor + " to " + targetDescriptor); } } else if (stackDescriptor == TypeDescriptor.J) { if (targetDescriptor == TypeDescriptor.D) { gen.Emit(OpCodes.Conv_R8); } else if (targetDescriptor == TypeDescriptor.F) { gen.Emit(OpCodes.Conv_R4); } else if (targetDescriptor == TypeDescriptor.J) { // nop } else if (targetDescriptor == TypeDescriptor.I) { gen.Emit(OpCodes.Conv_I4); } else { throw new InvalidOperationException("Cannot get from " + stackDescriptor + " to " + targetDescriptor); } } else if (stackDescriptor == TypeDescriptor.F) { if (targetDescriptor == TypeDescriptor.D) { gen.Emit(OpCodes.Conv_R8); } else if (targetDescriptor == TypeDescriptor.F) { // nop } else if (targetDescriptor == TypeDescriptor.J) { gen.Emit(OpCodes.Conv_I8); } else if (targetDescriptor == TypeDescriptor.I) { gen.Emit(OpCodes.Conv_I4); } else { throw new InvalidOperationException("Cannot get from " + stackDescriptor + " to " + targetDescriptor); } } else if (stackDescriptor == TypeDescriptor.D) { if (targetDescriptor == TypeDescriptor.D) { // nop } else if (targetDescriptor == TypeDescriptor.F) { gen.Emit(OpCodes.Conv_R4); } else if (targetDescriptor == TypeDescriptor.J) { gen.Emit(OpCodes.Conv_I8); } else if (targetDescriptor == TypeDescriptor.I) { gen.Emit(OpCodes.Conv_I4); } else { throw new InvalidOperationException("Cannot get from " + stackDescriptor + " to " + targetDescriptor); } } } }