Exemplo n.º 1
0
        public static CecilHelpers.DelegateProducingSequence AnalyzeForEachInvocationInstruction(MethodDefinition methodToAnalyze, Instruction withCodeInvocationInstruction)
        {
            var delegatePushingInstruction = CecilHelpers.FindInstructionThatPushedArg(methodToAnalyze, 1, withCodeInvocationInstruction);

            var result = CecilHelpers.MatchesDelegateProducingPattern(methodToAnalyze, delegatePushingInstruction, CecilHelpers.DelegateProducingPattern.MatchSide.Start);

            if (result == null)
            {
                // Make sure we aren't generating this lambdajob from a stored delegate
                bool LivesInUniversalDelegatesNamespace(TypeReference type) => type.Namespace == typeof(UniversalDelegates.R <>).Namespace;

                if ((delegatePushingInstruction.OpCode == OpCodes.Ldfld && LivesInUniversalDelegatesNamespace(((FieldReference)delegatePushingInstruction.Operand).FieldType) ||
                     (delegatePushingInstruction.IsLoadLocal(out var localIndex) && LivesInUniversalDelegatesNamespace(methodToAnalyze.Body.Variables[localIndex].VariableType)) ||
                     (delegatePushingInstruction.IsLoadArg(out var argIndex) &&
                      LivesInUniversalDelegatesNamespace(methodToAnalyze.Parameters[methodToAnalyze.HasThis ? (argIndex - 1) : argIndex].ParameterType))))
                {
                    UserError.DC0044(methodToAnalyze, delegatePushingInstruction).Throw();
                }
                else if (((delegatePushingInstruction.OpCode == OpCodes.Call || delegatePushingInstruction.OpCode == OpCodes.Callvirt) &&
                          delegatePushingInstruction.Operand is MethodReference callMethod))
                {
                    if (LivesInUniversalDelegatesNamespace(callMethod.ReturnType))
                    {
                        UserError.DC0044(methodToAnalyze, delegatePushingInstruction).Throw();
                    }
                }

                InternalCompilerError.DCICE002(methodToAnalyze, delegatePushingInstruction).Throw();
            }
        public static CecilHelpers.DelegateProducingSequence AnalyzeForEachInvocationInstruction(MethodDefinition methodToAnalyze, Instruction withCodeInvocationInstruction)
        {
            var delegatePushingInstruction = CecilHelpers.FindInstructionThatPushedArg(methodToAnalyze, 1, withCodeInvocationInstruction);

            var result = CecilHelpers.MatchesDelegateProducingPattern(methodToAnalyze, delegatePushingInstruction, CecilHelpers.DelegateProducingPattern.MatchSide.Start);

            if (result == null)
            {
                InternalCompilerError.DCICE002(methodToAnalyze, delegatePushingInstruction).Throw();
            }

            return(result);
        }
        static (Instruction, Instruction) FindClosureCreatingInstructions(MethodBody body, Instruction callInstruction)
        {
            body.EnsurePreviousAndNextAreSet();
            var cursor = callInstruction;

            while (cursor != null)
            {
                if ((cursor.OpCode == OpCodes.Ldftn) && cursor.Next?.OpCode == OpCodes.Newobj)
                {
                    return(cursor, cursor.Next);
                }

                cursor = cursor.Previous;
            }

            InternalCompilerError.DCICE002(body.Method, callInstruction).Throw();
            return(null, null);
        }