Example #1
0
        /// <summary>
        /// Takes the outputs from the previous deconstructionStep and depending on the structure of variables, will:
        /// - generate further deconstructions,
        /// - or simply conversions and assignments.
        ///
        /// Returns true for success, but false if has errors.
        /// </summary>
        private bool DeconstructOrAssignOutputs(
                        BoundDeconstructionDeconstructStep deconstructionStep,
                        ArrayBuilder<DeconstructionVariable> variables,
                        CSharpSyntaxNode syntax,
                        DiagnosticBag diagnostics,
                        ArrayBuilder<BoundDeconstructionDeconstructStep> deconstructionSteps,
                        ArrayBuilder<BoundDeconstructionAssignmentStep> conversionSteps,
                        ArrayBuilder<BoundDeconstructionAssignmentStep> assignmentSteps,
                        ArrayBuilder<BoundDeconstructionConstructionStep> constructionStepsOpt)
        {
            bool hasErrors = false;
            var constructionInputs = constructionStepsOpt == null ? null : ArrayBuilder<BoundDeconstructValuePlaceholder>.GetInstance();

            int count = variables.Count;
            for (int i = 0; i < count; i++)
            {
                var variable = variables[i];
                var valuePlaceholder = deconstructionStep.OutputPlaceholders[i];

                if (variable.HasNestedVariables)
                {
                    var nested = variable.NestedVariables;
                    if (!DeconstructIntoSteps(valuePlaceholder, syntax, diagnostics, nested, deconstructionSteps, conversionSteps, assignmentSteps, constructionStepsOpt))
                    {
                        hasErrors = true;
                    }
                    else if (constructionInputs != null)
                    {
                        constructionInputs.Add(constructionStepsOpt.Last().OutputPlaceholder);
                    }
                }
                else
                {
                    var conversion = MakeDeconstructionAssignmentStep(variable.Single, valuePlaceholder, syntax, diagnostics);
                    conversionSteps.Add(conversion);

                    var assignment = MakeDeconstructionAssignmentStep(variable.Single, conversion.OutputPlaceholder, syntax, diagnostics);
                    assignmentSteps.Add(assignment);

                    if (constructionInputs != null)
                    {
                        constructionInputs.Add(conversion.OutputPlaceholder);
                    }
                }
            }

            if (constructionStepsOpt != null)
            {
                if (hasErrors)
                {
                    constructionInputs.Free();
                }
                else
                {
                    var construct = MakeDeconstructionConstructionStep(syntax, diagnostics, constructionInputs.ToImmutableAndFree());
                    constructionStepsOpt.Add(construct);
                }
            }

            return !hasErrors;
        }