示例#1
0
        /// <summary>
        /// Creates a <see cref="SwitchSection"/> that represents a switch statement section.
        /// </summary>
        /// <param name="locals">The locals that are in scope of the section.</param>
        /// <param name="labels">The labels handled by the section.</param>
        /// <param name="statements">The statements in the body of the section.</param>
        /// <returns>The created <see cref="SwitchLabel"/>.</returns>
        public static SwitchSection SwitchSection(IEnumerable <ParameterExpression> locals, IEnumerable <SwitchLabel> labels, IEnumerable <Expression> statements)
        {
            var localsList = CheckUniqueVariables(locals, nameof(locals));

            var labelsList = labels.ToReadOnly();

            RequiresNotEmpty(labelsList, nameof(labels));
            RequiresNotNullItems(labelsList, nameof(labels));

            var allLabels        = new HashSet <LabelTarget>();
            var patternInputType = default(Type);

            foreach (var label in labelsList)
            {
                if (label.Label != null && !allLabels.Add(label.Label))
                {
                    throw Error.DuplicateLabelInSwitchSection(label.Label);
                }

                if (patternInputType == null)
                {
                    patternInputType = label.Pattern.InputType;
                }
                else if (patternInputType != label.Pattern.InputType)
                {
                    throw Error.InconsistentPatternInputType(label.Pattern.InputType, patternInputType);
                }
            }

            var statementsList = GetStatements(statements);

            return(new SwitchSection(localsList, labelsList, statementsList));
        }