protected override ILConstruct EmitNotExpression(TContext context, ILConstruct booleanExpression)
        {
            if (booleanExpression.ContextType != typeof(bool))
            {
                throw new ArgumentException(
                          String.Format(CultureInfo.CurrentCulture, "Not expression must be Boolean elementType, but actual is '{0}'.", booleanExpression.ContextType),
                          "booleanExpression"
                          );
            }

            return
                (ILConstruct.UnaryOperator(
                     "!",
                     booleanExpression,
                     (il, val) =>
            {
                val.LoadValue(il, false);
                il.EmitLdc_I4_0();
                il.EmitCeq();
            },
                     (il, val, @else) =>
            {
                val.LoadValue(il, false);
                il.EmitBrtrue(@else);
            }
                     ));
        }
        protected override ILConstruct EmitGetSerializerExpression(AssemblyBuilderEmittingContext context, Type targetType, SerializingMember?memberInfo, PolymorphismSchema itemsSchema)
        {
            var realSchema   = itemsSchema ?? PolymorphismSchema.Create(targetType, memberInfo);
            var instructions =
                context.Emitter.RegisterSerializer(
                    targetType,
                    memberInfo == null
                                                ? EnumMemberSerializationMethod.Default
                                                : memberInfo.Value.GetEnumMemberSerializationMethod(),
                    memberInfo == null
                                                ? DateTimeMemberConversionMethod.Default
                                                : memberInfo.Value.GetDateTimeMemberConversionMethod(),
                    realSchema,
                    () => this.EmitConstructPolymorphismSchema(context, realSchema)
                    );

            return
                (ILConstruct.Instruction(
                     "getserializer",
                     typeof(MessagePackSerializer <>).MakeGenericType(targetType),
                     false,
                     // Both of this pointer for FieldBasedSerializerEmitter and context argument of methods for ContextBasedSerializerEmitter are 0.
                     il => instructions(il, 0)
                     ));
        }
 /// <summary>
 ///		Initializes a new instance of the <see cref="DynamicMethodEmittingContext"/> class.
 /// </summary>
 /// <param name="context">The serialization context.</param>
 /// <param name="targetType">Type of the serialization target.</param>
 /// <param name="emitterFactory">
 ///		The factory for <see cref="SerializerEmitter"/> to be used.
 /// </param>
 /// <param name="enumEmitterFactory">
 ///		The factory for <see cref="EnumSerializerEmitter"/> to be used.
 /// </param>
 public DynamicMethodEmittingContext(SerializationContext context, Type targetType,
                                     Func <SerializerEmitter> emitterFactory, Func <EnumSerializerEmitter> enumEmitterFactory)
     : base(context, emitterFactory, enumEmitterFactory)
 {
     this._context = ILConstruct.Argument(0, typeof(SerializationContext), "context");
     this.Reset(targetType, null);
 }
 public StoreFieldILConstruct(ILConstruct instance, FieldInfo field, ILConstruct value)
     : base(TypeDefinition.VoidType)
 {
     this._instance = instance;
     this._field    = field;
     this._value    = value;
 }
Exemple #5
0
 public StoreFieldILConstruct(ILConstruct instance, FieldInfo field, ILConstruct value)
     : base(typeof(void))
 {
     this._instance = instance;
     this._field    = field;
     this._value    = value;
 }
Exemple #6
0
        public InvocationILConsruct(MethodInfo method, Type @interface, ILConstruct target, IEnumerable <ILConstruct> arguments)
            : base(method.ReturnType)
        {
            if (method.IsStatic)
            {
                if (target != null)
                {
                    throw new ArgumentException(
                              String.Format(CultureInfo.CurrentCulture, "target must be null for static method '{0}'", method)
                              );
                }
            }
            else
            {
                if (target == null)
                {
                    throw new ArgumentException(
                              String.Format(CultureInfo.CurrentCulture, "target must not be null for instance method '{0}'", method)
                              );
                }
            }

            this._method    = method;
            this._target    = target;
            this._arguments = arguments;
            this._interface = @interface;
        }
Exemple #7
0
 protected override ILConstruct EmitSetArrayElementStatement(TContext context, ILConstruct array, ILConstruct index, ILConstruct value)
 {
     return
         (ILConstruct.Instruction(
              "SetArrayElement",
              array.ContextType,
              false,
              il =>
     {
         il.EmitAnyStelem(
             value.ContextType,
             il0 =>
         {
             array.LoadValue(il0, false);
         },
             il0 =>
         {
             index.LoadValue(il0, false);
         },
             il0 =>
         {
             value.LoadValue(il0, true);
         }
             );
     }
              ));
 }
Exemple #8
0
        protected override ILConstruct EmitCreateNewArrayExpression(TContext context, Type elementType, int length)
        {
            var array =
                ILConstruct.Variable(
                    elementType.MakeArrayType(),
                    "array"
                    );

            return
                (ILConstruct.Composite(
                     ILConstruct.Sequence(
                         array.ContextType,
                         new[]
            {
                array,
                ILConstruct.Instruction(
                    "NewArray",
                    array.ContextType,
                    false,
                    il =>
                {
                    il.EmitNewarr(elementType, length);
                    array.StoreValue(il);
                }
                    )
            }
                         ),
                     array
                     ));
        }
 protected override ILConstruct DeclareLocal(TContext context, Type type, string name)
 {
     return
         (ILConstruct.Variable(
              type,
              name
              ));
 }
 public UnaryOperatorILConstruct(string @operator, ILConstruct input, Action <TracingILGenerator, ILConstruct> operation, Action <TracingILGenerator, ILConstruct, Label> branchOperation)
     : base(input.ContextType)
 {
     this._operator        = @operator;
     this._input           = input;
     this._operation       = operation;
     this._branchOperation = branchOperation;
 }
 protected override ILConstruct EmitConditionalExpression(TContext context, ILConstruct conditionExpression, ILConstruct thenExpression, ILConstruct elseExpression)
 {
     return
         (ILConstruct.IfThenElse(
              conditionExpression,
              thenExpression,
              elseExpression
              ));
 }
 protected override ILConstruct EmitTypeOfExpression(TContext context, Type type)
 {
     return
         (ILConstruct.Literal(
              typeof(Type),
              type,
              il => il.EmitTypeOf(type)
              ));
 }
 protected override ILConstruct EmitAndConditionalExpression(TContext context, IList <ILConstruct> conditionExpressions, ILConstruct thenExpression, ILConstruct elseExpression)
 {
     return
         (ILConstruct.IfThenElse(
              ILConstruct.AndCondition(conditionExpressions),
              thenExpression,
              elseExpression
              ));
 }
Exemple #14
0
 public BinaryOperatorILConstruct(string @operator, TypeDefinition resultType, ILConstruct left, ILConstruct right, Action <TracingILGenerator, ILConstruct, ILConstruct> operation, Action <TracingILGenerator, ILConstruct, ILConstruct, Label> branchOperation)
     : base(resultType)
 {
     ValidateContextTypeMatch(left, right);
     this._operator        = @operator;
     this._left            = left;
     this._right           = right;
     this._operation       = operation;
     this._branchOperation = branchOperation;
 }
        protected override ILConstruct EmitSetProprety(TContext context, ILConstruct instance, PropertyInfo property, ILConstruct value)
        {
#if DEBUG
            // ReSharper disable PossibleNullReferenceException
            Contract.Assert(
                property.GetSetMethod(true) != null,
                property.DeclaringType.FullName + "::" + property.Name + ".set != null"
                );
            // ReSharper restore PossibleNullReferenceException
#endif
            return(ILConstruct.Invoke(instance, property.GetSetMethod(true), new[] { value }));
        }
 protected override ILConstruct EmitBoxExpression(TContext context, Type valueType, ILConstruct value)
 {
     return
         (ILConstruct.UnaryOperator(
              "box",
              value,
              (il, val) =>
     {
         val.LoadValue(il, false);
         il.EmitBox(valueType);
     }
              ));
 }
 protected override ILConstruct EmitUnboxAnyExpression(TContext context, Type targetType, ILConstruct value)
 {
     return
         (ILConstruct.UnaryOperator(
              "unbox.any",
              value,
              (il, val) =>
     {
         val.LoadValue(il, false);
         il.EmitUnbox_Any(targetType);
     }
              ));
 }
 protected override ILConstruct EmitMethodOfExpression(TContext context, MethodBase method)
 {
     return
         (ILConstruct.Literal(
              typeof(MethodInfo),
              method,
              il =>
     {
         il.EmitLdtoken(method);
         il.EmitCall(Metadata._MethodBase.GetMethodFromHandle);
     }
              ));
 }
 protected override ILConstruct EmitFieldOfExpression(TContext context, FieldInfo field)
 {
     return
         (ILConstruct.Literal(
              typeof(MethodInfo),
              field,
              il =>
     {
         il.EmitLdtoken(field);
         il.EmitCall(Metadata._FieldInfo.GetFieldFromHandle);
     }
              ));
 }
 protected override ILConstruct EmitInvokeVoidMethod(TContext context, ILConstruct instance, MethodInfo method, params ILConstruct[] arguments)
 {
     return
         (method.ReturnType == typeof(void)
                                 ? ILConstruct.Invoke(instance, method, arguments)
                                 : ILConstruct.Sequence(
              typeof(void),
              new[]
     {
         ILConstruct.Invoke(instance, method, arguments),
         ILConstruct.Instruction("pop", typeof(void), false, il => il.EmitPop())
     }
              ));
 }
 protected override ILConstruct EmitThrowExpression(TContext context, Type expressionType, ILConstruct exceptionExpression)
 {
     return
         (ILConstruct.Instruction(
              "throw",
              expressionType,
              true,
              il =>
     {
         exceptionExpression.LoadValue(il, false);
         il.EmitThrow();
     }
              ));
 }
 protected static void ValidateContextTypeMatch(ILConstruct left, ILConstruct right)
 {
     if (GetNormalizedType(left.ContextType.ResolveRuntimeType()) != GetNormalizedType(right.ContextType.ResolveRuntimeType()))
     {
         throw new ArgumentException(
                   String.Format(
                       CultureInfo.CurrentCulture,
                       "Right type '{1}' does not equal to left type '{0}'.",
                       left.ContextType,
                       right.ContextType
                       ),
                   "right"
                   );
     }
 }
 protected override ILConstruct EmitIncrement(TContext context, ILConstruct int32Value)
 {
     return
         (ILConstruct.UnaryOperator(
              "++",
              int32Value,
              (il, variable) =>
     {
         variable.LoadValue(il, false);
         il.EmitLdc_I4_1();
         il.EmitAdd();
         variable.StoreValue(il);
     }
              ));
 }
Exemple #24
0
 protected override ILConstruct MakeDefaultLiteral(TContext context, Type type)
 {
     return
         (ILConstruct.Literal(
              type,
              "default(" + type + ")",
              il =>
     {
         var temp = il.DeclareLocal(type);
         il.EmitAnyLdloca(temp);
         il.EmitInitobj(type);
         il.EmitAnyLdloc(temp);
     }
              ));
 }
Exemple #25
0
        protected override ILConstruct EmitMethodOfExpression(AssemblyBuilderEmittingContext context, MethodBase method)
        {
            var instructions =
                context.Emitter.RegisterMethod(
                    method
                    );

            return
                (ILConstruct.Instruction(
                     "getsetter",
                     typeof(MethodBase),
                     false,
                     // Both of this pointer for FieldBasedSerializerEmitter and context argument of methods for ContextBasedSerializerEmitter are 0.
                     il => instructions(il, 0)
                     ));
        }
        private static void EmitMethodEpilogue(TContext context, ILConstruct construct)
        {
            try
            {
                if (construct != null)
                {
                    construct.Evaluate(context.IL);
                }

                context.IL.EmitRet();
            }
            finally
            {
                context.IL.FlushTrace();
            }
        }
Exemple #27
0
        public InvocationILConsruct(ConstructorInfo ctor, ILConstruct target, IEnumerable <ILConstruct> arguments)
            : base(ctor.DeclaringType)
        {
            if (ctor.DeclaringType.GetIsValueType())
            {
                if (target == null)
                {
                    throw new ArgumentException(
                              String.Format(CultureInfo.CurrentCulture, "target must not be null for expression type constructor '{0}'", ctor)
                              );
                }
            }

            this._method    = ctor;
            this._target    = target;
            this._arguments = arguments;
        }
        public ConditionalILConstruct(ILConstruct condition, ILConstruct thenExpression, ILConstruct elseExpression)
            : base(thenExpression.ContextType)
        {
            if (condition.ContextType.ResolveRuntimeType() != typeof(bool))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "condition must be boolean: {0}", condition), "condition");
            }

            if (elseExpression != null && elseExpression.ContextType.ResolveRuntimeType() != thenExpression.ContextType.ResolveRuntimeType())
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "elseExpression type must be '{0}' but '{1}':{2}", thenExpression.ContextType, elseExpression.ContextType, elseExpression), "elseExpression");
            }

            this._condition      = condition;
            this._thenExpression = thenExpression;
            this._elseExpression = elseExpression;
        }
 protected override ILConstruct EmitTryFinally(TContext context, ILConstruct tryStatement, ILConstruct finallyStatement)
 {
     return
         (ILConstruct.Instruction(
              "try-finally",
              tryStatement.ContextType,
              false,
              il =>
     {
         il.BeginExceptionBlock();
         tryStatement.Evaluate(il);
         il.BeginFinallyBlock();
         finallyStatement.Evaluate(il);
         il.EndExceptionBlock();
     }
              ));
 }
Exemple #30
0
        protected override ILConstruct EmitGetSerializerExpression(AssemblyBuilderEmittingContext context, Type targetType, SerializingMember?memberInfo)
        {
            var instructions =
                context.Emitter.RegisterSerializer(
                    targetType,
                    memberInfo == null
                                        ? EnumMemberSerializationMethod.Default
                                        : memberInfo.Value.GetEnumMemberSerializationMethod()
                    );

            return
                (ILConstruct.Instruction(
                     "getserializer",
                     typeof(MessagePackSerializer <>).MakeGenericType(targetType),
                     false,
                     // Both of this pointer for FieldBasedSerializerEmitter and context argument of methods for ContextBasedSerializerEmitter are 0.
                     il => instructions(il, 0)
                     ));
        }