#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Reflection.Emit
{
    internal abstract class InvocationEmitter_ : BaseEmitter_
    {
        protected InvocationEmitter_(CallInfo_ callInfo, DelegateCache cache)
            : base(callInfo, cache)
        {
        }

        protected void LoadLocalsFromArguments(ILGenerator generator, int paramArrayIndex)
        {
            for (int i = 0; i < CallInfo.ParamTypes.Length; i++)
            {
                var paramType = CallInfo.ParamTypes[i];
                generator.DeclareLocal(paramType);
                generator.Emit(OpCodes.Ldarg, paramArrayIndex);
                generator.Emit(OpCodes.Ldc_I4, i);
                generator.Emit(OpCodes.Ldelem_Ref);
                UnboxOrCast(generator, paramType);
                generator.Emit(OpCodes.Stloc, i);
            }
        }

        protected void PushLocalsToStack(ILGenerator generator)
        {
            for (int i = 0; i < CallInfo.ParamTypes.Length; i++)
            {
                generator.Emit(OpCodes.Ldloc, i);
            }
        }

        protected void LoadTarget(ILGenerator generator)
        {
            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Castclass, CallInfo.TargetType);
        }

        protected void ReturnValue(ILGenerator generator, Type returnType)
        {
            if (returnType == Constants.VoidType)
            {
                generator.Emit(OpCodes.Ldnull);
            }
            else
            {
                BoxIfValueType(generator, returnType);
            }
            generator.Emit(OpCodes.Ret);
        }
    }
}
