Ejemplo n.º 1
0
        public static void AppendCast(this TranslationContext context, IType source, IType target)
        {
            if (ReferenceEquals(source, target))
            {
                return;
            }

            Checks.CheckValidCast(source, target);

            var cast = (context.Provider.Cast(source, target, false) ?? Enumerable.Empty <IInstruction>()).ToArray();

            if (cast.Length > 0)
            {
                var block = context.Block;
                var stack = block.Stack;
                var item  = stack.Pop();
                stack.PushResult(item.Instruction, target);

                var code = block.TranslatedCode;
                int n    = code.Count;
                if (n == 0)
                {
                    throw new ILTranslatorException("Translated code is empty");
                }

                if (code[n - 1].IsBranchOrSwitch())
                {
                    code.InsertRange(n - 1, cast);
                }
                else
                {
                    code.AddRange(cast);
                }
            }
        }
Ejemplo n.º 2
0
        private static bool CastToParamType(TranslationContext context, Instruction instruction, IParameter p, bool force)
        {
            if (p == null)
            {
                return(false);
            }

            var block = context.Block;

            if (block.Stack.Count == 0)
            {
                return(false);
            }

            var v = block.Stack.Peek();

            if (instruction == null)
            {
                instruction = v.Instruction;
            }

            if (instruction != null)
            {
                switch (instruction.Code)
                {
                case InstructionCode.Box:
                case InstructionCode.Ldtoken:
                    return(false);
                }
            }

            if (v.Value.Kind == ValueKind.Token)
            {
                return(false);
            }

            var vtype = v.Type;
            var ptype = p.GetUnwrappedType();

            if (!ReferenceEquals(vtype, ptype))
            {
                Checks.CheckValidCast(vtype, ptype);

                if (!force)
                {
                    if (!NeedCast(vtype, ptype))
                    {
                        return(true);
                    }
                }

                context.EmitCast(vtype, ptype);
            }

            return(true);
        }