ILGenerator like IL stream builder with tracing.
Наследование: IDisposable
Пример #1
0
		/// <summary>
		///		Emits 'for' statement on current IL stream.
		/// </summary>
		/// <param name="il">IL generator to be emitted to.</param>
		/// <param name="count">'count' local variable which is <see cref="Int32"/> type and holds maximum loop count.</param>
		/// <param name="bodyEmitter">Delegate to emit for statement body.</param>
		public static void EmitFor( TracingILGenerator il, LocalBuilder count, Action<TracingILGenerator, LocalBuilder> bodyEmitter )
		{
			Contract.Requires( il != null );
			Contract.Requires( count != null );
			Contract.Requires( bodyEmitter != null );

			var i = il.DeclareLocal( typeof( int ), "i" );
			il.EmitLdc_I4_0();
			il.EmitAnyStloc( i );
			var forCond = il.DefineLabel( "FOR_COND" );
			il.EmitBr( forCond );
			var body = il.DefineLabel( "BODY" );
			il.MarkLabel( body );
			bodyEmitter( il, i );
			// increment
			il.EmitAnyLdloc( i );
			il.EmitLdc_I4_1();
			il.EmitAdd();
			il.EmitAnyStloc( i );
			// cond
			il.MarkLabel( forCond );
			il.EmitAnyLdloc( i );
			il.EmitAnyLdloc( count );
			il.EmitBlt( body );
		}
Пример #2
0
		public override void LoadValue( TracingILGenerator il, bool shouldBeAddress )
		{
			this.Evaluate( il );
			il.TraceWriteLine( "// Load->: {0}", this );
			if ( this.ContextType.ResolveRuntimeType().GetIsValueType() && shouldBeAddress )
			{
				if ( this._isLocal )
				{
					il.EmitAnyLdloca( this._index );
				}
				else
				{
					il.EmitAnyLdarga( this._index );
				}
			}
			else
			{
				if ( this._isLocal )
				{
					il.EmitAnyLdloc( this._index );
				}
				else
				{
					il.EmitAnyLdarg( this._index );
				}
			}
			il.TraceWriteLine( "// ->Load: {0}", this );
		}
Пример #3
0
		private void DoConditionalInstruction(
			TracingILGenerator il, Action onThen, Action onElse
			)
		{
			if ( this._elseExpression != null )
			{
				var @else = il.DefineLabel( "ELSE" );
				var endIf = il.DefineLabel( "END_IF" );
				this._condition.Branch( il, @else );
				onThen();
				if ( !this._thenExpression.IsTerminating )
				{
					il.EmitBr( endIf );
				}

				il.MarkLabel( @else );
				onElse();
				il.MarkLabel( endIf );
			}
			else
			{
				var endIf = il.DefineLabel( "END_IF" );
				this._condition.Branch( il, endIf );
				onThen();
				il.MarkLabel( endIf );
			}
		}
Пример #4
0
		public override void Evaluate( TracingILGenerator il )
		{
			if ( this._value != null )
			{
				this._value.LoadValue( il, false );
			}

			this._variable.StoreValue( il );
		}
Пример #5
0
		public override void StoreValue( TracingILGenerator il )
		{
			il.TraceWriteLine( "// Stor->: {0}", this );
			this.DoConditionalInstruction(
				il,
				() => this._thenExpression.StoreValue( il ),
				() => this._elseExpression.StoreValue( il )
				);
			il.TraceWriteLine( "// ->Stor: {0}", this );
		}
Пример #6
0
		public override void LoadValue( TracingILGenerator il, bool shouldBeAddress )
		{
			il.TraceWriteLine( "// Load->: {0}", this );
			this.DoConditionalInstruction(
				il,
				() => this._thenExpression.LoadValue( il, shouldBeAddress ),
				() => this._elseExpression.LoadValue( il, shouldBeAddress )
				);
			il.TraceWriteLine( "// ->Load: {0}", this );
		}
Пример #7
0
		public override void Evaluate( TracingILGenerator il )
		{
			il.TraceWriteLine( "// Eval->: {0}", this );
			this.DoConditionalInstruction(
				il,
				() => this._thenExpression.Evaluate( il ),
				() => this._elseExpression.Evaluate( il )
				);
			il.TraceWriteLine( "// ->Eval: {0}", this );
		}
		private void Evaluate( TracingILGenerator il, bool shouldBeAddress )
		{
			if ( !this._isBound )
			{
				this._binding.Evaluate( il );
				this._isBound = true;
			}

			this._expression.LoadValue( il, shouldBeAddress );
		}
Пример #9
0
		public override void Evaluate( TracingILGenerator il )
		{
			if ( this._isLocal && this._index < 0 )
			{
				il.TraceWriteLine( "// Eval->: {0}", this );

				this._index = il.DeclareLocal( this.ContextType.ResolveRuntimeType(), this._name ).LocalIndex;

				il.TraceWriteLine( "// ->Eval: {0}", this );
			}
		}
Пример #10
0
		public override void Branch( TracingILGenerator il, Label @else )
		{
			il.TraceWriteLine( "// Brnc->: {0}", this );
			foreach ( var expression in this._expressions )
			{
				expression.LoadValue( il, false );
				il.EmitBrfalse( @else );
			}

			il.TraceWriteLine( "// ->Brnc: {0}", this );
		}
Пример #11
0
		public override void Evaluate( TracingILGenerator il )
		{
			il.TraceWriteLine( "// Eval->: {0}", this );

			foreach ( var statement in this._statements )
			{
				statement.Evaluate( il );
			}

			il.TraceWriteLine( "// ->Eval: {0}", this );
		}
Пример #12
0
		private void EvaluateCore( TracingILGenerator il )
		{
			for ( int i = 0; i < this._expressions.Count; i++ )
			{
				this._expressions[ i ].LoadValue( il, false );

				if ( i > 0 )
				{
					il.EmitAnd();
				}
			}
		}
Пример #13
0
		public sealed override void Branch( TracingILGenerator il, Label @else )
		{
			il.TraceWriteLine( "// Brnc->: {0}", this );
			if ( this.ContextType != typeof( bool ) )
			{
				throw new InvalidOperationException(
					String.Format( CultureInfo.CurrentCulture, "Cannot branch with non boolean type '{0}'.", this.ContextType )
					);
			}

			this.BranchCore( il, @else );
			il.TraceWriteLine( "// ->Brnc: {0}", this );
		}
Пример #14
0
		public override void StoreValue( TracingILGenerator il )
		{
			il.TraceWriteLine( "// Stor->: {0}", this );
			if ( this._instance != null )
			{
				this._instance.LoadValue( il, this._instance.ContextType.GetIsValueType() );
			}

			this._value.LoadValue( il, false );

			il.EmitStfld( this._field );
			il.TraceWriteLine( "// ->Stor: {0}", this );
		}
Пример #15
0
		public override void LoadValue( TracingILGenerator il, bool shouldBeAddress )
		{
			il.TraceWriteLine( "// Load->: {0}", this );
			if ( this._instance != null )
			{
				this._instance.LoadValue( il, this._instance.ContextType.GetIsValueType() );
			}

			if ( shouldBeAddress )
			{
				il.EmitLdflda( this._field );
			}
			else
			{
				il.EmitLdfld( this._field );
			}
			il.TraceWriteLine( "// ->Load: {0}", this );
		}
Пример #16
0
		public override void LoadValue( TracingILGenerator il, bool shouldBeAddress )
		{
			if ( this._statements.Length == 0 )
			{
				base.LoadValue( il, shouldBeAddress );
				return;
			}

			il.TraceWriteLine( "// Eval(Load)->: {0}", this );

			for ( var i = 0; i < this._statements.Length - 1; i++ )
			{
				this._statements[ i ].Evaluate( il );
			}

			this._statements.Last().LoadValue( il, shouldBeAddress );

			il.TraceWriteLine( "// ->Eval(Load): {0}", this );
		}
Пример #17
0
		/// <summary>
		///		Emits deserializing value instructions.
		/// </summary>
		/// <param name="emitter">The emitter.</param>
		/// <param name="il">The il generator.</param>
		/// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param>
		/// <param name="value">The value local variable which stores unpacked value.</param>
		/// <param name="targetType">The type of deserialzing type.</param>
		/// <param name="memberName">The name of the member.</param>
		/// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
		public static void EmitDeserializeValueWithoutNilImplication( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder value, Type targetType, string memberName, LocalVariableHolder localHolder )
		{
			Contract.Requires( emitter != null );
			Contract.Requires( il != null );
			Contract.Requires( unpackerArgumentIndex >= 0 );
			Contract.Requires( value != null );

			var endOfDeserialization = il.DefineLabel( "END_OF_DESERIALIZATION" );

			/*
			 * 
			 * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader )
			 * {
			 *		valueN = GET_SERIALIZER.UnpackFrom( unpacker );
			 * }
			 * else
			 * {
			 *		using( var subtreeUnpacker = unpacker.ReadSubtree )
			 *		{
			 *			valueN = GET_SERIALIZER.UnpackFrom( unpacker );
			 *		}
			 * }
			 * 
			 * isValueNUnpacked = true;
			 * END_OF_DESERIALIZATION:
			 */

			// Nil implication is not needed.
			EmitDeserializeValueCore( emitter, il, unpackerArgumentIndex, value, targetType, null, memberName, endOfDeserialization, localHolder );

			il.MarkLabel( endOfDeserialization );
		}
		public override void LoadValue( TracingILGenerator il, bool shouldBeAddress )
		{
			il.TraceWriteLine( "// Load->: {0}", this );
			this.Evaluate( il, shouldBeAddress );
			il.TraceWriteLine( "// ->Load: {0}", this );
		}
		public override void Evaluate( TracingILGenerator il )
		{
			il.TraceWriteLine( "// Eval->: {0}", this );
			this.Evaluate( il, false );
			il.TraceWriteLine( "// ->Eval: {0}", this );
		}
Пример #20
0
		public static void EmitConstruction( TracingILGenerator il, LocalBuilder target, Action<TracingILGenerator> initialCountLoadingEmitter )
		{
			Contract.Requires( il != null );
			Contract.Requires( target != null );
			Contract.Requires( initialCountLoadingEmitter != null );

			// TODO: For collection, supports .ctor(IEnumerable<> other)

			if ( target.LocalType.IsArray )
			{
				initialCountLoadingEmitter( il );
				il.EmitNewarr( target.LocalType.GetElementType() );
				il.EmitAnyStloc( target );
				return;
			}

			ConstructorInfo ctor = target.LocalType.GetConstructor( _ctor_Int32_ParameterTypes );
			if ( ctor != null && initialCountLoadingEmitter != null && typeof( IEnumerable ).IsAssignableFrom( target.LocalType ) )
			{
				if ( target.LocalType.IsValueType )
				{
					// Same as general method call
					var capacity = il.DeclareLocal( typeof( int ), "capacity" );
					initialCountLoadingEmitter( il );
					il.EmitAnyStloc( capacity );
					il.EmitAnyLdloca( target );
					il.EmitAnyLdloc( capacity );
					il.EmitCallConstructor( ctor );
				}
				else
				{
					initialCountLoadingEmitter( il );
					il.EmitNewobj( ctor );
					il.EmitAnyStloc( target );
				}
				return;
			}

			if ( target.LocalType.IsValueType )
			{
				// ValueType instance has been initialized by the runtime.
				return;
			}

			ctor = target.LocalType.GetConstructor( Type.EmptyTypes );
			if ( ctor == null )
			{
				throw SerializationExceptions.NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity( target.LocalType );
			}

			il.EmitNewobj( ctor );
			il.EmitAnyStloc( target );
		}
Пример #21
0
		public static void EmitUnpackerEndReadSubtree( TracingILGenerator il, LocalBuilder subtreeUnpacker )
		{
			Contract.Requires( il != null );
			Contract.Requires( subtreeUnpacker != null );

			/*
			 *	finally
			 *	{
			 *		if( subtreeUnpacker != null )
			 *		{
			 *			subtreeUnpacker.Dispose();
			 *		}
			 *	}
			 */

			il.BeginFinallyBlock();
			il.EmitAnyLdloc( subtreeUnpacker );
			var endIf = il.DefineLabel( "END_IF" );
			il.EmitBrfalse_S( endIf );
			il.EmitAnyLdloc( subtreeUnpacker );
			il.EmitAnyCall( Metadata._IDisposable.Dispose );
			il.MarkLabel( endIf );
			il.EndExceptionBlock();
		}
Пример #22
0
		public static void EmitUnpackerBeginReadSubtree( TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder subtreeUnpacker )
		{
			Contract.Requires( il != null );
			Contract.Requires( unpackerArgumentIndex >= 0 );
			Contract.Requires( subtreeUnpacker != null );

			/*
			 * subtreeUnpacker = unpacker.ReadSubtree()
			 */

			il.EmitAnyLdarg( unpackerArgumentIndex );
			il.EmitAnyCall( Metadata._Unpacker.ReadSubtree );
			il.EmitAnyStloc( subtreeUnpacker );
			il.BeginExceptionBlock();
		}
Пример #23
0
		/// <summary>
		/// Emits the deserialize collection value.
		/// </summary>
		/// <param name="emitter">The emitter.</param>
		/// <param name="il">The il generator.</param>
		/// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param>
		/// <param name="target">The target collection variable.</param>
		/// <param name="member">The deserializing member metadata which holds the collection.</param>
		/// <param name="memberType">Type of the deserializing member.</param>
		/// <param name="nilImplication">The nil implication.</param>
		/// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
		public static void EmitDeserializeCollectionValue( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder target, MemberInfo member, Type memberType, NilImplication nilImplication, LocalVariableHolder localHolder )
		{
			Contract.Requires( emitter != null );
			Contract.Requires( il != null );
			Contract.Requires( unpackerArgumentIndex >= 0 );
			Contract.Requires( target != null );
			Contract.Requires( member != null );
			Contract.Requires( memberType != null );
			Contract.Requires( localHolder != null );

			var endOfDeserialization = il.DefineLabel( "END_OF_DESERIALIZATION" );

			EmitGeneralRead( il, unpackerArgumentIndex );

			switch ( nilImplication )
			{
				case NilImplication.MemberDefault:
				{
					/*
					 * if( unpacker.Data.Value.IsNil )
					 * {
					 *		// Skip current.
					 *		goto END_OF_DESERIALIZATION;
					 * }
					 */
					il.EmitAnyLdarg( unpackerArgumentIndex );
					il.EmitGetProperty( Metadata._Unpacker.Data );
					var data = localHolder.UnpackedData;
					il.EmitAnyStloc( data );
					il.EmitAnyLdloca( data );
					il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value );
					var dataValue = localHolder.UnpackedDataValue;
					il.EmitAnyStloc( dataValue );
					il.EmitAnyLdloca( dataValue );
					il.EmitGetProperty( Metadata._MessagePackObject.IsNil );
					il.EmitBrtrue( endOfDeserialization );

					break;
				}
				case NilImplication.Null:
				case NilImplication.Prohibit:
				{
					/*
					 * // for Prohibit
					 * if( unpacker.Data.Value.IsNil )
					 * {
					 *		throw SerializationEceptions.NewProhibitNullException( "..." );
					 * }
					 * 
					 * // for Null, and 
					 * if( unpacker.Data.Value.IsNil )
					 * {
					 *		throw SerializationEceptions.NewReadOnlyMemberItemsMustNotBeNullMethod( "..." );
					 * }
					 */
					il.EmitAnyLdarg( unpackerArgumentIndex );
					il.EmitGetProperty( Metadata._Unpacker.Data );
					var data = localHolder.UnpackedData;
					il.EmitAnyStloc( data );
					il.EmitAnyLdloca( data );
					il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value );
					var dataValue = localHolder.UnpackedDataValue;
					il.EmitAnyStloc( dataValue );
					il.EmitAnyLdloca( dataValue );
					il.EmitGetProperty( Metadata._MessagePackObject.IsNil );
					var endIf0 = il.DefineLabel( "END_IF0" );
					il.EmitBrfalse_S( endIf0 );
					il.EmitLdstr( member.Name );
					if ( nilImplication == NilImplication.Prohibit )
					{
						il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod );
					}
					else
					{
						// Because result member is readonly collection, so the member will not be null if packed value was nil.
						il.EmitAnyCall( SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNullMethod );
					}
					il.EmitThrow();
					il.MarkLabel( endIf0 );

					break;
				}
			}

			/*
			 *	if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader )
			 *	{
			 *		throw new SerializatonException( "Cannot deserialize..." );
			 *	}
			 * 
			 *	using( var subtreeUnpacker = unpacker.ReadSubtree() )
			 *	{
			 *		GET_SERIALIZER.UnpackTo( unpacker, result ) )
			 *	}
			 *	
			 *	END_OF_DESERIALIZATION:
			 */

			var endIf = il.DefineLabel( "THEN" );
			var serializerGetter = emitter.RegisterSerializer( memberType );

			il.EmitAnyLdarg( unpackerArgumentIndex );
			il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader );
			il.EmitBrtrue_S( endIf );
			il.EmitAnyLdarg( unpackerArgumentIndex );
			il.EmitGetProperty( Metadata._Unpacker.IsMapHeader );
			il.EmitBrtrue_S( endIf );
			// else
			il.EmitLdstr( member.Name );
			il.EmitAnyCall( SerializationExceptions.NewStreamDoesNotContainCollectionForMemberMethod );
			il.EmitThrow();
			// then
			var subtreeUnpacker = localHolder.SubtreeUnpacker;
			il.MarkLabel( endIf );
			EmitUnpackerBeginReadSubtree( il, unpackerArgumentIndex, subtreeUnpacker );
			serializerGetter( il, 0 );
			il.EmitAnyLdloc( subtreeUnpacker );
			il.EmitAnyLdloc( target );
			Emittion.EmitLoadValue( il, member );
			il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( memberType ).GetMethod( "UnpackTo", new[] { typeof( Unpacker ), memberType } ) );
			EmitUnpackerEndReadSubtree( il, subtreeUnpacker );
			il.MarkLabel( endOfDeserialization );
		}
Пример #24
0
		/// <summary>
		///		Emits 'foreach' statement on the IL stream.
		/// </summary>
		/// <param name="il">IL generator to be emitted to.</param>
		/// <param name="traits"><see cref="CollectionTraits"/> which contains traits of the iterating collection.</param>
		/// <param name="collection">'collection' argument index.</param>
		/// <param name="bodyEmitter">Delegate to emit body statement.</param>
		public static void EmitForEach( TracingILGenerator il, CollectionTraits traits, LocalBuilder collection, Action<TracingILGenerator, Action> bodyEmitter )
		{
			Contract.Requires( il != null );
			Contract.Requires( collection != null );
			Contract.Requires( bodyEmitter != null );

			var enumerator = il.DeclareLocal( traits.GetEnumeratorMethod.ReturnType, "enumerator" );

			// gets enumerator
			if ( collection.LocalType.IsValueType )
			{
				il.EmitAnyLdloca( collection );
			}
			else
			{
				il.EmitAnyLdloc( collection );
			}

			il.EmitAnyCall( traits.GetEnumeratorMethod );
			il.EmitAnyStloc( enumerator );

			if ( typeof( IDisposable ).IsAssignableFrom( traits.GetEnumeratorMethod.ReturnType ) )
			{
				il.BeginExceptionBlock();
			}

			var startLoop = il.DefineLabel( "START_LOOP" );
			il.MarkLabel( startLoop );
			var endLoop = il.DefineLabel( "END_LOOP" );
			var enumeratorType = traits.GetEnumeratorMethod.ReturnType;
			MethodInfo moveNextMethod = enumeratorType.GetMethod( "MoveNext", Type.EmptyTypes );
			PropertyInfo currentProperty = traits.GetEnumeratorMethod.ReturnType.GetProperty( "Current" );

			if ( moveNextMethod == null )
			{
				moveNextMethod = Metadata._IEnumerator.MoveNext;
			}

			if ( currentProperty == null )
			{
				if ( enumeratorType == typeof( IDictionaryEnumerator ) )
				{
					currentProperty = Metadata._IDictionaryEnumerator.Current;
				}
				else if ( enumeratorType.IsInterface )
				{
					if ( enumeratorType.IsGenericType && enumeratorType.GetGenericTypeDefinition() == typeof( IEnumerator<> ) )
					{
						currentProperty = typeof( IEnumerator<> ).MakeGenericType( traits.ElementType ).GetProperty( "Current" );
					}
					else
					{
						currentProperty = Metadata._IEnumerator.Current;
					}
				}
			}

			Contract.Assert( currentProperty != null, enumeratorType.ToString() );

			// iterates
			if ( traits.GetEnumeratorMethod.ReturnType.IsValueType )
			{
				il.EmitAnyLdloca( enumerator );
			}
			else
			{
				il.EmitAnyLdloc( enumerator );
			}

			il.EmitAnyCall( moveNextMethod );
			il.EmitBrfalse( endLoop );

			bodyEmitter(
				il,
				() =>
				{
					if ( traits.GetEnumeratorMethod.ReturnType.IsValueType )
					{
						il.EmitAnyLdloca( enumerator );
					}
					else
					{
						il.EmitAnyLdloc( enumerator );
					}
					il.EmitGetProperty( currentProperty );
				}
			);

			il.EmitBr( startLoop );
			il.MarkLabel( endLoop );

			// Dispose
			if ( typeof( IDisposable ).IsAssignableFrom( traits.GetEnumeratorMethod.ReturnType ) )
			{
				il.BeginFinallyBlock();

				if ( traits.GetEnumeratorMethod.ReturnType.IsValueType )
				{
					var disposeMethod = traits.GetEnumeratorMethod.ReturnType.GetMethod( "Dispose" );
					if ( disposeMethod != null && disposeMethod.GetParameters().Length == 0 && disposeMethod.ReturnType == typeof( void ) )
					{
						il.EmitAnyLdloca( enumerator );
						il.EmitAnyCall( disposeMethod );
					}
					else
					{
						il.EmitAnyLdloc( enumerator );
						il.EmitBox( traits.GetEnumeratorMethod.ReturnType );
						il.EmitAnyCall( Metadata._IDisposable.Dispose );
					}
				}
				else
				{
					il.EmitAnyLdloc( enumerator );
					il.EmitAnyCall( Metadata._IDisposable.Dispose );
				}

				il.EndExceptionBlock();
			}
		}
Пример #25
0
		private static void EmitCompareNull( TracingILGenerator il, LocalBuilder value, Label targetIfNotNull )
		{
			if ( value.LocalType == typeof( MessagePackObject ) )
			{
				il.EmitAnyLdloca( value );
				il.EmitGetProperty( Metadata._MessagePackObject.IsNil );
				il.EmitBrfalse_S( targetIfNotNull );
			}
			else if ( value.LocalType.GetIsValueType() )
			{
				Contract.Assert( Nullable.GetUnderlyingType( value.LocalType ) != null, value.LocalType.FullName );
				il.EmitAnyLdloca( value );
				il.EmitGetProperty( value.LocalType.GetProperty( "HasValue" ) );
				il.EmitBrtrue_S( targetIfNotNull );
			}
			else // ref type
			{
				il.EmitAnyLdloc( value );
				il.EmitLdnull();
				il.EmitBne_Un_S( targetIfNotNull );
			}
		}
Пример #26
0
		private static void EmitNilImplicationForPrimitive( TracingILGenerator il, SerializingMember member, LocalBuilder value, Label endOfDeserialization )
		{
			var endIf = il.DefineLabel( "END_IF_NULL" );
			EmitCompareNull( il, value, endIf );

			switch ( member.Contract.NilImplication )
			{
				case NilImplication.MemberDefault:
				{
					/*
					 * if( value == null )
					 * {
					 *		// Skip current.
					 *		goto END_OF_DESERIALIZATION;
					 * }
					 */
					il.EmitBr( endOfDeserialization );
					break;
				}
				case NilImplication.Null:
				{
					// Throw exception for non-nullable value type.
					// Nop for nullables.
					if ( member.Member.GetMemberValueType().GetIsValueType()
						&& Nullable.GetUnderlyingType( member.Member.GetMemberValueType() ) == null )
					{
						/*
						 * if( value == null )
						 * {
						 *		throw SerializationEceptions.NewValueTypeCannotBeNull( "...", typeof( MEMBER ), typeof( TYPE ) );
						 * }
						 */
						il.EmitLdstr( member.Contract.Name );
						il.EmitLdtoken( member.Member.GetMemberValueType() );
						il.EmitAnyCall( Metadata._Type.GetTypeFromHandle );
						il.EmitLdtoken( member.Member.DeclaringType );
						il.EmitAnyCall( Metadata._Type.GetTypeFromHandle );
						il.EmitAnyCall( SerializationExceptions.NewValueTypeCannotBeNull3Method );
						il.EmitThrow();
					}

					break;
				}
				case NilImplication.Prohibit:
				{
					/*
					 * if( value == null )
					 * {
					 *		throw SerializationEceptions.NewProhibitNullException( "..." );
					 * }
					 */
					il.EmitLdstr( member.Contract.Name );
					il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod );
					il.EmitThrow();
					break;
				}
			}

			il.MarkLabel( endIf );
		}
Пример #27
0
		/// <summary>
		///		Emits the nil implication.
		/// </summary>
		/// <param name="il">The il generator.</param>
		/// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param>
		/// <param name="memberName">Name of the deserializing member.</param>
		/// <param name="nilImplication">The nil implication.</param>
		/// <param name="endOfDeserialization">The label to the end of deserialization.</param>
		/// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
		public static void EmitNilImplication(
			TracingILGenerator il,
			int unpackerArgumentIndex,
			string memberName,
			NilImplication nilImplication,
			Label endOfDeserialization,
			LocalVariableHolder localHolder
		)
		{
			switch ( nilImplication )
			{
				case NilImplication.MemberDefault:
				{
					// TODO: This should be empty for extra items.
					/*
						 * if( unpacker.Data.Value.IsNil )
						 * {
						 *		// Skip current.
						 *		goto END_OF_DESERIALIZATION;
						 * }
						 */
					il.EmitAnyLdarg( unpackerArgumentIndex );
					il.EmitGetProperty( Metadata._Unpacker.Data );
					var data = localHolder.UnpackedData;
					il.EmitAnyStloc( data );
					il.EmitAnyLdloca( data );
					il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value );
					var dataValue = localHolder.UnpackedDataValue;
					il.EmitAnyStloc( dataValue );
					il.EmitAnyLdloca( dataValue );
					il.EmitGetProperty( Metadata._MessagePackObject.IsNil );
					il.EmitBrtrue( endOfDeserialization );

					break;
				}
				case NilImplication.Prohibit:
				{
					/*
						 * if( unpacker.Data.Value.IsNil )
						 * {
						 *		throw SerializationEceptions.NewProhibitNullException( "..." );
						 * }
						 */
					il.EmitAnyLdarg( unpackerArgumentIndex );
					il.EmitGetProperty( Metadata._Unpacker.Data );
					var data = localHolder.UnpackedData;
					il.EmitAnyStloc( data );
					il.EmitAnyLdloca( data );
					il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value );
					var dataValue = localHolder.UnpackedDataValue;
					il.EmitAnyStloc( dataValue );
					il.EmitAnyLdloca( dataValue );
					il.EmitGetProperty( Metadata._MessagePackObject.IsNil );
					var endIf0 = il.DefineLabel( "END_IF0" );
					il.EmitBrfalse_S( endIf0 );
					il.EmitLdstr( memberName );
					il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod );
					il.EmitThrow();
					il.MarkLabel( endIf0 );

					break;
				}
			}
		}
Пример #28
0
		/// <summary>
		///		Emits unpacking method with flavor specific getter.
		/// </summary>
		/// <param name="emitter">SerializerEmitter which knows the emittion flavor.</param>
		/// <param name="il">The il generator.</param>
		/// <param name="result">A variable wich stores unpacked result.</param>
		/// <param name="unpacker">The local variable which stores current Unpacker.</param>
		public static void EmitUnpackFrom( SerializerEmitter emitter, TracingILGenerator il, LocalBuilder result, LocalBuilder unpacker )
		{
			var serializerGetter = emitter.RegisterSerializer( result.LocalType );
			serializerGetter( il, 0 );
			il.EmitAnyLdloc( unpacker );
			il.EmitAnyCall( Metadata._UnpackHelpers.InvokeUnpackFrom_1Method.MakeGenericMethod( result.LocalType ) );
			il.EmitAnyStloc( result );
		}
Пример #29
0
		protected virtual void BranchCore( TracingILGenerator il, Label @else )
		{
			this.LoadValue( il, false );
			il.EmitBrfalse( @else );
		}
Пример #30
0
		/// <summary>
		/// Emits the deserialize value.
		/// </summary>
		/// <param name="emitter">The emitter.</param>
		/// <param name="il">The il generator.</param>
		/// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param>
		/// <param name="value">The value local variable which stores unpacked value.</param>
		/// <param name="targetType">The type of deserialzing type.</param>
		/// <param name="member">The metadata for nil implication. Specify <c>null</c> if nil implication is not needed.</param>
		/// <param name="memberName">The name of the member.</param>
		/// <param name="endOfDeserialization">The end of deserialization label for nil implication.</param>
		/// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
		private static void EmitDeserializeValueCore( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder value, Type targetType, SerializingMember? member, string memberName, Label endOfDeserialization, LocalVariableHolder localHolder )
		{
			var directUnpacking = Metadata._Unpacker.GetDirectReadMethod( value.LocalType );
			if ( directUnpacking != null && ( member == null || !UnpackHelpers.IsReadOnlyAppendableCollectionMember( member.Value.Member ) ) )
			{
				var isSuccess = localHolder.IsDeserializationSucceeded;
				il.EmitLdc_I4_0();
				il.EmitAnyStloc( isSuccess );

				il.BeginExceptionBlock();
				il.EmitAnyLdarg( unpackerArgumentIndex );
				il.EmitAnyLdloca( value );
				il.EmitAnyCall( directUnpacking );
				il.EmitAnyStloc( isSuccess );
				il.BeginCatchBlock( typeof( MessageTypeException ) );
				var ex = localHolder.GetCatchedException( typeof( MessageTypeException ) );
				il.EmitAnyStloc( ex );
				il.EmitTypeOf( targetType );
				il.EmitLdstr( memberName );
				il.EmitAnyLdloc( ex );
				il.EmitAnyCall( SerializationExceptions.NewFailedToDeserializeMemberMethod );
				il.EmitThrow();
				il.EndExceptionBlock();
				var endIf0 = il.DefineLabel( "END_IF" );
				il.EmitAnyLdloc( isSuccess );
				il.EmitBrtrue_S( endIf0 );
				il.EmitAnyCall( SerializationExceptions.NewUnexpectedEndOfStreamMethod );
				il.EmitThrow();
				il.MarkLabel( endIf0 );
				if ( member != null )
				{
					// If null, nil implication is NOT needed.
					EmitNilImplicationForPrimitive( il, member.Value, value, endOfDeserialization );
				}
			}
			else
			{
				EmitGeneralRead( il, unpackerArgumentIndex );
				if ( member != null )
				{
					// If null, nil implication is NOT needed.
					EmitNilImplication(
						il,
						unpackerArgumentIndex,
						member.Value.Contract.Name,
						member.Value.Contract.NilImplication,
						endOfDeserialization,
						localHolder
					);
				}

				var thenIffCollection = il.DefineLabel( "THEN_IF_COLLECTION" );
				var endIfCollection = il.DefineLabel( "END_IF_COLLECTION" );

				/*
				 *	if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader )
				 *	{
				 *		value = GET_SERIALIZER().UnpackFrom( unpacker );
				 *	}
				 *	else
				 *	{
				 *		using( var subtreeUnpacker = unpacker.ReadSubtree() )
				 *		{
				 *			value = GET_SERIALIZER().UnpackFrom( subtreeUnpacker );
				 *		}
				 *	}
				 */

				il.EmitAnyLdarg( unpackerArgumentIndex );
				il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader );
				il.EmitAnyLdarg( unpackerArgumentIndex );
				il.EmitGetProperty( Metadata._Unpacker.IsMapHeader );
				il.EmitOr();
				il.EmitBrtrue_S( thenIffCollection );
				EmitUnpackFrom( emitter, il, value, unpackerArgumentIndex );
				il.EmitBr_S( endIfCollection );
				var subtreeUnpacker = localHolder.SubtreeUnpacker;
				il.MarkLabel( thenIffCollection );
				EmitUnpackerBeginReadSubtree( il, unpackerArgumentIndex, subtreeUnpacker );
				EmitUnpackFrom( emitter, il, value, subtreeUnpacker );
				EmitUnpackerEndReadSubtree( il, subtreeUnpacker );
				il.MarkLabel( endIfCollection );
			}

		}