From 960ce1d1993532ef7227da0b82786ee05c909bb4 Mon Sep 17 00:00:00 2001 From: jbevain Date: Wed, 19 May 2010 13:36:21 +0000 Subject: [PATCH] 2010-05-19 Jb Evain * ConstantExpression.cs: fix emission of nullable constants. git-svn-id: svn+ssh://mono-cvs.ximian.com/source/trunk/mcs@157550 e3ebcda4-bce8-0310-ba0a-eca2169e7518 --- .../System.Core/System.Linq.Expressions/ChangeLog | 4 +++ .../System.Linq.Expressions/ConstantExpression.cs | 40 +++++++++++++++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/class/System.Core/System.Linq.Expressions/ChangeLog b/class/System.Core/System.Linq.Expressions/ChangeLog index 8c1d3dd217..4ceca36025 100644 --- a/class/System.Core/System.Linq.Expressions/ChangeLog +++ b/class/System.Core/System.Linq.Expressions/ChangeLog @@ -1,3 +1,7 @@ +2010-05-19 Jb Evain + + * ConstantExpression.cs: fix emission of nullable constants. + 2010-01-08 Jb Evain * Expression.cs (Call): properly deal with zero length array diff --git a/class/System.Core/System.Linq.Expressions/ConstantExpression.cs b/class/System.Core/System.Linq.Expressions/ConstantExpression.cs index 0bf985cbb4..1a8c4d7b7a 100644 --- a/class/System.Core/System.Linq.Expressions/ConstantExpression.cs +++ b/class/System.Core/System.Linq.Expressions/ConstantExpression.cs @@ -52,9 +52,33 @@ namespace System.Linq.Expressions { internal override void Emit (EmitContext ec) { - ILGenerator ig = ec.ig; + if (Type.IsNullable ()) { + EmitNullableConstant (ec, Type, value); + return; + } + + EmitConstant (ec, Type, value); + } + + void EmitNullableConstant (EmitContext ec, Type type, object value) + { + if (value == null) { + var ig = ec.ig; + var local = ig.DeclareLocal (type); + ig.Emit (OpCodes.Ldloca, local); + ig.Emit (OpCodes.Initobj, type); + ig.Emit (OpCodes.Ldloc, local); + } else { + EmitConstant (ec, type.GetFirstGenericArgument (), value); + ec.EmitNullableNew (type); + } + } + + void EmitConstant (EmitContext ec, Type type, object value) + { + var ig = ec.ig; - switch (Type.GetTypeCode (Type)){ + switch (Type.GetTypeCode (type)){ case TypeCode.Byte: ig.Emit (OpCodes.Ldc_I4, (int) ((byte)value)); return; @@ -163,17 +187,7 @@ namespace System.Linq.Expressions { void EmitIfNotNull (EmitContext ec, Action emit) { if (value == null) { - var ig = ec.ig; - - if (Type.IsValueType) { // happens for nullable types - var local = ig.DeclareLocal (Type); - ig.Emit (OpCodes.Ldloca, local); - ig.Emit (OpCodes.Initobj, Type); - ig.Emit (OpCodes.Ldloc, local); - } else { - ec.ig.Emit (OpCodes.Ldnull); - } - + ec.ig.Emit (OpCodes.Ldnull); return; } -- 2.11.4.GIT