コード例 #1
0
        public static string SwitchSection(SwitchSectionSyntax section)
        {
            var output = section.Labels.Aggregate("", (current, label) => current + SyntaxNode(label));

            return section.Statements.TakeWhile(statement => !(statement is BreakStatementSyntax)) // Swift doesn't use break; statements.
                .Aggregate(output, (current, statement) => current + ("    " + SyntaxNode(statement))); //TODO: Handle case/switch indenting in Indenter.cs
        }
コード例 #2
0
 public override void VisitSwitchSection(SwitchSectionSyntax node)
 {
     foreach (var label in node.Labels)
     {
         var match = label as CasePatternSwitchLabelSyntax;
         if (match != null)
         {
             Visit(match.Pattern);
             if (match.WhenClause != null)
             {
                 Visit(match.WhenClause.Condition);
             }
         }
     }
 }
コード例 #3
0
 internal static bool HasBraces(SwitchSectionSyntax section)
 {
     switch (section.Statements.Count)
     {
         case 1:
             if (section.Statements.First() is BlockSyntax)
                 return true;
             break;
         case 2:
             if (section.Statements.First() is BlockSyntax && section.Statements.Last() is BreakStatementSyntax)
                 return true;
             break;
     }
     return false;
 }
コード例 #4
0
            static SwitchSectionSyntax CreateNewSection(SwitchSectionSyntax section)
            {
                SyntaxList <StatementSyntax> statements = section.GetStatements();

                if (statements.Last().IsKind(SyntaxKind.ThrowStatement))
                {
                    return(section);
                }

                var expressionStatement = (ExpressionStatementSyntax)statements.LastButOne();

                var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression;

                section = section.ReplaceNode(expressionStatement, ReturnStatement(assignment.Right).WithTriviaFrom(expressionStatement));

                return(section.RemoveStatement(section.GetStatements().Last()));
            }
コード例 #5
0
        public static bool CanAddBraces(SwitchSectionSyntax section)
        {
            SyntaxList <StatementSyntax> statements = section.Statements;

            if (statements.Count > 1)
            {
                return(true);
            }
            else if (statements.Count == 1 && statements[0].Kind() != SyntaxKind.Block)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #6
0
        public static async Task <Document> RefactorAsync(
            Document document,
            SwitchSectionSyntax switchSection,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            SwitchSectionSyntax newNode = switchSection
                                          .WithStatements(
                List <StatementSyntax>(
                    SingletonList(
                        Block(switchSection.Statements))))
                                          .WithFormatterAnnotation();

            SyntaxNode newRoot = root.ReplaceNode(switchSection, newNode);

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #7
0
        private static bool IsEmptyOrContainsOnlyDefaultSection(SwitchStatementSyntax switchStatement)
        {
            if (switchStatement.Sections.Count == 0)
            {
                return(true);
            }
            else if (switchStatement.Sections.Count == 1)
            {
                SwitchSectionSyntax section = switchStatement.Sections[0];

                return(section.Labels.Count == 1 &&
                       section.Labels[0].IsKind(SyntaxKind.DefaultSwitchLabel));
            }
            else
            {
                return(false);
            }
        }
コード例 #8
0
        public override SyntaxNode VisitSwitchSection(SwitchSectionSyntax node)
        {
            node = (SwitchSectionSyntax)base.VisitSwitchSection(node);

            var oldStatements = node.Statements;

            if (oldStatements.Count == 0) //empty case - used for fall-through
            {
                return(node);
            }

            var caseTrailing = node.GetTrailingTrivia();
            var caseLeading  = node.GetLeadingTrivia();
            var trueBlock    = ToBlockSyntax(oldStatements, caseLeading, caseTrailing);

            node = node.WithStatements(trueBlock);

            return(node);
        }
コード例 #9
0
        public override void VisitSwitchSection(SwitchSectionSyntax node)
        {
            foreach (var label in node.Labels)
            {
                var match = label as CasePatternSwitchLabelSyntax;
                if (match != null)
                {
                    var previousNodeToBind = _nodeToBind;
                    _nodeToBind = match;
                    Visit(match.Pattern);
                    if (match.WhenClause != null)
                    {
                        VisitNodeToBind(match.WhenClause.Condition);
                    }

                    _nodeToBind = previousNodeToBind;
                }
            }
        }
コード例 #10
0
        private static Task <Document> RefactorAsync(
            Document document,
            SyntaxListSelection <SwitchLabelSyntax> selectedLabels,
            IComparer <SwitchLabelSyntax> comparer,
            CancellationToken cancellationToken = default)
        {
            SyntaxList <SwitchLabelSyntax> labels = selectedLabels.UnderlyingList;

            SyntaxList <SwitchLabelSyntax> newLabels = labels.ReplaceRange(
                selectedLabels.FirstIndex,
                selectedLabels.Count,
                selectedLabels.OrderBy(f => f, comparer));

            var section = (SwitchSectionSyntax)labels[0].Parent;

            SwitchSectionSyntax newSection = section.WithLabels(newLabels);

            return(document.ReplaceNodeAsync(section, newSection, cancellationToken));
        }
コード例 #11
0
        public static string SwitchSection(SwitchSectionSyntax node)
        {
            var output = "";

            foreach (var label in node.Labels)
            {
                output += SyntaxNode(label);
            }

            foreach (var statement in node.Statements)
            {
                if (statement is BreakStatementSyntax)
                {
                    break;
                }
                output += "    " + SyntaxNode(statement); //TODO: fix the tabbing here with Indenter.cs
            }
            return(output);
        }
コード例 #12
0
        private static SwitchSectionSyntax CreateSectionWithoutStatements(SwitchSectionSyntax section)
        {
            SwitchSectionSyntax newSection = section.WithStatements(List <StatementSyntax>());

            if (newSection
                .GetTrailingTrivia()
                .All(f => f.IsWhitespaceTrivia()))
            {
                newSection = newSection.WithoutTrailingTrivia();
            }

            if (section
                .SyntaxTree
                .IsSingleLineSpan(TextSpan.FromBounds(section.Labels.Last().SpanStart, section.Span.End)))
            {
                newSection = newSection.AppendToTrailingTrivia(section.GetTrailingTrivia());
            }

            return(newSection);
        }
コード例 #13
0
        internal static bool HasBraces(SwitchSectionSyntax section)
        {
            switch (section.Statements.Count)
            {
            case 1:
                if (section.Statements.First() is BlockSyntax)
                {
                    return(true);
                }
                break;

            case 2:
                if (section.Statements.First() is BlockSyntax && section.Statements.Last() is BreakStatementSyntax)
                {
                    return(true);
                }
                break;
            }
            return(false);
        }
コード例 #14
0
        internal static SyntaxList <StatementSyntax> GetStatementsOrDefault(SwitchSectionSyntax section)
        {
            foreach (SwitchLabelSyntax label in section.Labels)
            {
                if (!label.Kind().Is(SyntaxKind.CaseSwitchLabel, SyntaxKind.DefaultSwitchLabel))
                {
                    return(default(SyntaxList <StatementSyntax>));
                }
            }

            SyntaxList <StatementSyntax> statements = section.Statements;

            if (statements.Count == 1 &&
                (statements[0] is BlockSyntax block))
            {
                return(block.Statements);
            }

            return(statements);
        }
コード例 #15
0
ファイル: BlockStatements.cs プロジェクト: kobi2187/CsDisplay
        public override void VisitSwitchSection(SwitchSectionSyntax node)
        {
            if (debug)
            {
                Console.WriteLine(node.ToFullString());
            }
            var nl = OurLine.NewLine(LineKind.Decl, "SwitchSection");

            OurLine.AddEssentialInfo(ref nl, "labels:" + node.Labels.ToString());
            // OurLine.AddEssentialInfo(ref nl, node.Labels.Select((l) => l.Value.ToString()));
            OurLine.AddEssentialInfo(ref nl, "statements:" + node.Statements.ToString());

            nl.Source     = node.ToFullString();
            nl.ParentKind = node.Parent.RawKind;
            nl.RawKind    = node.RawKind;
            LogCommand(nl);
            StartBlock("SwitchSection");
            base.VisitSwitchSection(node);
            EndBlock("SwitchSection");
        }
コード例 #16
0
        public static bool CanAddBracesToSection(SwitchSectionSyntax section)
        {
            if (section == null)
            {
                throw new ArgumentNullException(nameof(section));
            }

            if (section.Statements.Count > 1)
            {
                return(true);
            }
            else if (section.Statements.Count == 1 && !section.Statements[0].IsKind(SyntaxKind.Block))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #17
0
        private static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
        {
            var switchStatement = (SwitchStatementSyntax)context.Node;

            SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

            SyntaxList <SwitchSectionSyntax> .Enumerator en = sections.GetEnumerator();

            if (!en.MoveNext())
            {
                return;
            }

            SwitchSectionSyntax previousSection = en.Current;

            var previousBlock = previousSection.Statements.SingleOrDefault(shouldThrow: false) as BlockSyntax;

            while (en.MoveNext())
            {
                SyntaxTriviaList leadingTrivia = en.Current.Labels[0].GetLeadingTrivia();

                if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(leadingTrivia))
                {
                    SyntaxTriviaList trailingTrivia = previousSection.GetTrailingTrivia();

                    if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenOptionalSingleLineCommentThenEndOfLineTrivia(trailingTrivia) &&
                        (context.GetBlankLineBetweenClosingBraceAndSwitchSection() != false ||
                         previousBlock == null))
                    {
                        DiagnosticHelpers.ReportDiagnostic(
                            context,
                            DiagnosticRules.AddBlankLineBetweenSwitchSections,
                            Location.Create(switchStatement.SyntaxTree, trailingTrivia.Last().Span.WithLength(0)));
                    }
                }

                previousSection = en.Current;

                previousBlock = en.Current.Statements.SingleOrDefault(shouldThrow: false) as BlockSyntax;
            }
        }
コード例 #18
0
        public override void VisitSwitchSection(SwitchSectionSyntax node)
        {
            if (!PreVisit(node))
            {
                return;
            }

            foreach (SwitchLabelSyntax labelSyntax in node.Labels)
            {
                labelSyntax.Accept(this);
            }

            foreach (StatementSyntax statement in node.Statements)
            {
                statement.Accept(this);
            }

            base.VisitSwitchSection(node);

            PostVisit(node);
        }
コード例 #19
0
        private static void AddUsingToEndOfBlock(DocumentEditor editor, SwitchSectionSyntax switchSection, LocalDeclarationStatementSyntax statement)
        {
            var statements = switchSection.Statements
                             .Where(s => s.SpanStart > statement.SpanStart)
                             .Where(s => !(s == switchSection.Statements.Last() &&
                                           s is BreakStatementSyntax))
                             .ToArray();

            foreach (var statementSyntax in statements)
            {
                editor.RemoveNode(statementSyntax);
            }

            editor.ReplaceNode(
                statement,
                SyntaxFactory.UsingStatement(
                    declaration: statement.Declaration,
                    expression: null,
                    statement: SyntaxFactory.Block(SyntaxFactory.List(statements))
                    .WithAdditionalAnnotations(Formatter.Annotation)));
        }
コード例 #20
0
        private static bool ContainsOnlyBreakStatement(SwitchSectionSyntax switchSection)
        {
            StatementSyntax statement = switchSection.SingleStatementOrDefault();

            switch (statement?.Kind())
            {
            case SyntaxKind.Block:
            {
                return(((BlockSyntax)statement)
                       .SingleStatementOrDefault()?
                       .IsKind(SyntaxKind.BreakStatement) == true);
            }

            case SyntaxKind.BreakStatement:
            {
                return(true);
            }
            }

            return(false);
        }
コード例 #21
0
ファイル: LLOC.cs プロジェクト: smartshark/OpenStaticAnalyzer
 public override void VisitSwitchSection(SwitchSectionSyntax node)
 {
     if (entryNode is AnonymousFunctionExpressionSyntax && embeddedNode is AnonymousFunctionExpressionSyntax)
     {
         return;
     }
     if (_weComeFromMethod && _weInAnonymousMethod)
     {
         return;
     }
     InsertLLOCMap(node.GetLocation());
     foreach (var statement in node.Statements)
     {
         InsertLLOCMap(statement.GetLocation());
     }
     foreach (var label in node.Labels)
     {
         InsertLLOCMap(label.GetLocation());
     }
     base.VisitSwitchSection(node);
 }
コード例 #22
0
        private static void AnalyzeFirstStatement(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection)
        {
            SyntaxList <StatementSyntax> statements = switchSection.Statements;

            if (statements.Count == 0)
            {
                return;
            }

            if (switchSection.Labels.Count == 0)
            {
                return;
            }

            if (switchSection.Labels.Last().GetSpanEndLine() == statements[0].GetSpanStartLine())
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.FormatSwitchSectionStatementOnSeparateLine,
                    statements[0].GetLocation());
            }
        }
コード例 #23
0
        private static bool IsEmptyOrContainsOnlyDefaultSection(SwitchStatementSyntax switchStatement)
        {
            SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

            if (!sections.Any())
            {
                return(true);
            }
            else if (sections.Count == 1)
            {
                SwitchSectionSyntax            section = sections[0];
                SyntaxList <SwitchLabelSyntax> labels  = section.Labels;

                return(labels.Count == 1 &&
                       labels[0].IsKind(SyntaxKind.DefaultSwitchLabel));
            }
            else
            {
                return(false);
            }
        }
コード例 #24
0
        internal static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
        {
            var switchStatement = (SwitchStatementSyntax)context.Node;

            if (!switchStatement.ContainsDiagnostics)
            {
                SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

                SwitchSectionSyntax defaultSection = FindDefaultSection(sections);

                if (defaultSection != null &&
                    ContainsOnlyBreakStatement(defaultSection) &&
                    !switchStatement.DescendantNodes(sections.Span).Any(f => f.IsKind(SyntaxKind.GotoDefaultStatement)) &&
                    defaultSection
                    .DescendantTrivia(defaultSection.Span)
                    .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantDefaultSwitchSection, defaultSection);
                }
            }
        }
コード例 #25
0
        public static Doc Print(SwitchSectionSyntax node)
        {
            var docs = new List <Doc> {
                Doc.Join(Doc.HardLine, node.Labels.Select(Node.Print))
            };

            if (node.Statements.Count == 1 && node.Statements[0] is BlockSyntax blockSyntax)
            {
                docs.Add(Block.Print(blockSyntax));
            }
            else
            {
                docs.Add(
                    Doc.Indent(
                        Doc.HardLine,
                        Doc.Join(Doc.HardLine, node.Statements.Select(Node.Print).ToArray())
                        )
                    );
            }
            return(Doc.Concat(docs));
        }
        public static void Analyze(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection)
        {
            SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels;

            for (int i = 0; i < labels.Count - 1; i++)
            {
                SwitchLabelSyntax label = labels[i];

                if (label.IsKind(SyntaxKind.DefaultSwitchLabel))
                {
                    TextSpan span = TextSpan.FromBounds(label.Span.End, labels.Last().Span.Start);

                    if (!switchSection.ContainsDirectives(span))
                    {
                        context.ReportDiagnostic(
                            DiagnosticDescriptors.DefaultLabelShouldBeLastLabelInSwitchSection,
                            label);
                    }
                }
            }
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            SwitchSectionSyntax switchSection,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels;

            SwitchLabelSyntax defaultLabel = labels.First(f => f.IsKind(SyntaxKind.DefaultSwitchLabel));

            int index = labels.IndexOf(defaultLabel);

            SwitchLabelSyntax lastLabel = labels.Last();

            labels = labels.Replace(lastLabel, defaultLabel.WithTriviaFrom(lastLabel));

            labels = labels.Replace(labels[index], lastLabel.WithTriviaFrom(defaultLabel));

            SwitchSectionSyntax newSwitchSection = switchSection.WithLabels(labels);

            return(await document.ReplaceNodeAsync(switchSection, newSwitchSection, cancellationToken).ConfigureAwait(false));
        }
コード例 #28
0
        public static bool CanRefactor(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection)
        {
            SwitchLabelSyntax label = switchSection.Labels.LastOrDefault();

            if (label != null)
            {
                ImmutableArray <Diagnostic> diagnostics = context.SemanticModel.GetDiagnostics(label.Span, context.CancellationToken);

                for (int i = 0; i < diagnostics.Length; i++)
                {
                    switch (diagnostics[i].Id)
                    {
                    case "CS0163":
                    case "CS8070":
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #29
0
        private static IEnumerable <SwitchSectionSyntax> CreateSwitchSections(
            SwitchSectionSyntax section,
            SyntaxListSelection <SwitchLabelSyntax> selectedLabels,
            int lastIndex)
        {
            int firstIndex = selectedLabels.FirstIndex;

            if (firstIndex > 0)
            {
                yield return(SwitchSection(section.Labels.Take(firstIndex + 1).ToSyntaxList(), BreakStatement())
                             .WithFormatterAnnotation());

                firstIndex++;
            }

            for (int i = firstIndex; i <= lastIndex; i++)
            {
                yield return(SwitchSection(selectedLabels.UnderlyingList[i], BreakStatement())
                             .WithFormatterAnnotation());
            }
        }
コード例 #30
0
        public static void Analyze(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection)
        {
            SyntaxList <StatementSyntax> statements = switchSection.Statements;

            if (statements.Any())
            {
                SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels;

                if (labels.Any())
                {
                    StatementSyntax statement = statements.First();

                    if (switchSection.SyntaxTree.IsSingleLineSpan(TextSpan.FromBounds(labels.Last().Span.End, statement.SpanStart)))
                    {
                        context.ReportDiagnostic(
                            DiagnosticDescriptors.FormatSwitchSectionStatementOnSeparateLine,
                            statement);
                    }
                }
            }
        }
コード例 #31
0
 internal static bool HasBraces(SwitchSectionSyntax section)
 {
     if (section.Statements.Count == 1)
     {
         var firstStatement = section.Statements.First();
         if (firstStatement is BlockSyntax)
         {
             return(true);
         }
     }
     else if (section.Statements.Count == 2)
     {
         var firstStatement = section.Statements.First();
         var lastStatement  = section.Statements.Last();
         if (firstStatement is BlockSyntax && lastStatement is BreakStatementSyntax)
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #32
0
        public override void VisitSwitchSection(SwitchSectionSyntax node)
        {
            // switch label should initialize the LastWhen clause
            LastWhen = null;
            foreach (var label in node.Labels.EmptyIfNull())
            {
                label.Accept(this);
            }

            // no labels per swith section? looks like the source file is broken
            if (LastWhen == null)
            {
                throw new InvalidOperationException("Unsupported switch section format. " +
                                                    "Case expression, pattern or default label is expected.");
            }

            var lastWhen = LastWhen;

            lastWhen.LeadingComments = GetLeadingAndNoApexComments(node);
            lastWhen.Block           = new ApexBlockSyntax();

            foreach (var stmt in node.Statements.EmptyIfNull())
            {
                stmt.Accept(this);
                if (LastStatement != null)
                {
                    lastWhen.Block.Statements.Add(LastStatement);
                    LastStatement = null;
                }
            }

            // remove C#'s break statement from the block keeping its trailing comments
            if (lastWhen.Block.Statements.LastOrDefault() is ApexBreakStatementSyntax @break)
            {
                lastWhen.Block.Statements.Remove(@break);
                lastWhen.Block.TrailingComments = @break.TrailingComments;
            }

            LastWhen = lastWhen;
        }
コード例 #33
0
        private static BoundPatternSwitchSection BindPatternSwitchSection(BoundExpression boundSwitchExpression, SwitchSectionSyntax node, Binder originalBinder, ref DefaultSwitchLabelSyntax defaultLabel, DiagnosticBag diagnostics)
        {
            // Bind match section labels
            var boundLabelsBuilder = ArrayBuilder<BoundPatternSwitchLabel>.GetInstance();
            var sectionBinder = originalBinder.GetBinder(node); // this binder can bind pattern variables from the section.
            Debug.Assert(sectionBinder != null);

            foreach (var labelSyntax in node.Labels)
            {
                BoundPatternSwitchLabel boundLabel = BindPatternSwitchSectionLabel(sectionBinder, boundSwitchExpression, labelSyntax, ref defaultLabel, diagnostics);
                boundLabelsBuilder.Add(boundLabel);
            }

            // Bind switch section statements
            var boundStatementsBuilder = ArrayBuilder<BoundStatement>.GetInstance();
            foreach (var statement in node.Statements)
            {
                boundStatementsBuilder.Add(sectionBinder.BindStatement(statement, diagnostics));
            }

            return new BoundPatternSwitchSection(node, sectionBinder.GetDeclaredLocalsForScope(node), boundLabelsBuilder.ToImmutableAndFree(), boundStatementsBuilder.ToImmutableAndFree());
        }
コード例 #34
0
        private static SyntaxList <StatementSyntax> GetSectionStatements(SwitchSectionSyntax switchSection)
        {
            SyntaxList <StatementSyntax> statements = switchSection.Statements;

            if (statements.Count == 1 &&
                statements[0].IsKind(SyntaxKind.Block))
            {
                statements = ((BlockSyntax)statements[0]).Statements;
            }

            if (statements.Any())
            {
                StatementSyntax last = statements.Last();

                if (last.IsKind(SyntaxKind.BreakStatement))
                {
                    return(statements.Remove(last));
                }
            }

            return(statements);
        }
コード例 #35
0
 public virtual void VisitSwitchSection(SwitchSectionSyntax node)
 {
     DefaultVisit(node);
 }
コード例 #36
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitSwitchSection(SwitchSectionSyntax node)
 {
     this.OnNodeVisited(node);
     if (!this.traverseRootOnly) base.VisitSwitchSection(node);
 }
コード例 #37
0
ファイル: SwitchBinder.cs プロジェクト: vslsnap/roslyn
        private BoundSwitchSection BindSwitchSection(SwitchSectionSyntax node, Binder originalBinder, DiagnosticBag diagnostics)
        {
            var sectionBinder = originalBinder.GetBinder(node);

            // Bind switch section labels
            var boundLabelsBuilder = ArrayBuilder<BoundSwitchLabel>.GetInstance();
            foreach (var labelSyntax in node.Labels)
            {
                LabelSymbol label = LabelsByNode[labelSyntax];
                BoundSwitchLabel boundLabel = BindSwitchSectionLabel(labelSyntax, sectionBinder, label, diagnostics);
                boundLabelsBuilder.Add(boundLabel);
            }

            // Bind switch section statements
            var boundStatementsBuilder = ArrayBuilder<BoundStatement>.GetInstance();
            foreach (var statement in node.Statements)
            {
                boundStatementsBuilder.Add(sectionBinder.BindStatement(statement, diagnostics));
            }

            return new BoundSwitchSection(node, boundLabelsBuilder.ToImmutableAndFree(), boundStatementsBuilder.ToImmutableAndFree());
        }
コード例 #38
0
        /// <summary>
        /// Bind the pattern switch section, producing subsumption diagnostics.
        /// </summary>
        /// <param name="boundSwitchExpression"/>
        /// <param name="node"/>
        /// <param name="originalBinder"/>
        /// <param name="defaultLabel">If a default label is found in this section, assigned that label</param>
        /// <param name="someValueMatched">If a constant label is found that matches the constant input, assigned that label</param>
        /// <param name="subsumption">A helper class that uses a decision tree to produce subsumption diagnostics.</param>
        /// <param name="diagnostics"></param>
        /// <returns></returns>
        private BoundPatternSwitchSection BindPatternSwitchSection(
            BoundExpression boundSwitchExpression,
            SwitchSectionSyntax node,
            Binder originalBinder,
            ref BoundPatternSwitchLabel defaultLabel,
            ref bool someValueMatched,
            SubsumptionDiagnosticBuilder subsumption,
            DiagnosticBag diagnostics)
        {
            // Bind match section labels
            var boundLabelsBuilder = ArrayBuilder<BoundPatternSwitchLabel>.GetInstance();
            var sectionBinder = originalBinder.GetBinder(node); // this binder can bind pattern variables from the section.
            Debug.Assert(sectionBinder != null);
            var labelsByNode = LabelsByNode;

            foreach (var labelSyntax in node.Labels)
            {
                LabelSymbol label = labelsByNode[labelSyntax];
                BoundPatternSwitchLabel boundLabel = BindPatternSwitchSectionLabel(sectionBinder, boundSwitchExpression, labelSyntax, label, ref defaultLabel, diagnostics);
                bool valueMatched; // true if we find an unconditional constant label that matches the input constant's value
                bool isReachable = subsumption.AddLabel(boundLabel, diagnostics, out valueMatched);
                boundLabel = boundLabel.Update(boundLabel.Label, boundLabel.Pattern, boundLabel.Guard, isReachable && !someValueMatched);
                someValueMatched |= valueMatched;
                boundLabelsBuilder.Add(boundLabel);
            }

            // Bind switch section statements
            var boundStatementsBuilder = ArrayBuilder<BoundStatement>.GetInstance();
            foreach (var statement in node.Statements)
            {
                boundStatementsBuilder.Add(sectionBinder.BindStatement(statement, diagnostics));
            }

            return new BoundPatternSwitchSection(node, sectionBinder.GetDeclaredLocalsForScope(node), boundLabelsBuilder.ToImmutableAndFree(), boundStatementsBuilder.ToImmutableAndFree());
        }
コード例 #39
0
 public SwitchSectionTranslation(SwitchSectionSyntax syntax, SyntaxTranslation parent) : base(syntax, parent)
 {
     Labels = syntax.Labels.Get<SwitchLabelSyntax, SwitchLabelTranslation>(this);
     Statements = syntax.Statements.Get<StatementSyntax, StatementTranslation>(this);
 }
コード例 #40
0
ファイル: SwitchBinder.cs プロジェクト: riversky/roslyn
        private BoundSwitchSection BindSwitchSection(SwitchSectionSyntax node, DiagnosticBag diagnostics)
        {
            // Bind switch section labels
            var boundLabelsBuilder = ArrayBuilder<BoundSwitchLabel>.GetInstance();
            foreach (var labelSyntax in node.Labels)
            {
                BoundSwitchLabel boundLabel = BindSwitchSectionLabel(labelSyntax, diagnostics);
                boundLabelsBuilder.Add(boundLabel);
            }

            // Bind switch section statements
            var boundStatementsBuilder = ArrayBuilder<BoundStatement>.GetInstance();
            foreach (var statement in node.Statements)
            {
                boundStatementsBuilder.Add(BindStatement(statement, diagnostics));
            }

            return new BoundSwitchSection(node, boundLabelsBuilder.ToImmutableAndFree(), boundStatementsBuilder.ToImmutableAndFree());
        }
 private static void ReportSection(SyntaxNodeAnalysisContext c, SwitchSectionSyntax switchSection, SwitchSectionSyntax precedingSection)
 {
     ReportSyntaxNode(c, switchSection, precedingSection, "case");
 }
コード例 #42
0
        public override void VisitSwitchSection(SwitchSectionSyntax node)
        {
            var patternBinder = new ExpressionVariableBinder(node, _enclosing);
            AddToMap(node, patternBinder);

            foreach (var label in node.Labels)
            {
                switch (label.Kind())
                {
                    case SyntaxKind.CasePatternSwitchLabel:
                        {
                            var switchLabel = (CasePatternSwitchLabelSyntax)label;
                            Visit(switchLabel.Pattern, patternBinder);
                            if (switchLabel.WhenClause != null)
                            {
                                Visit(switchLabel.WhenClause.Condition, patternBinder);
                            }
                            break;
                        }
                    case SyntaxKind.CaseSwitchLabel:
                        {
                            var switchLabel = (CaseSwitchLabelSyntax)label;
                            Visit(switchLabel.Value, patternBinder);
                            break;
                        }
                }
            }

            foreach (StatementSyntax statement in node.Statements)
            {
                Visit(statement, patternBinder);
            }
        }
コード例 #43
0
ファイル: Binder.Statements.cs プロジェクト: Samana/HlslTools
 private BoundSwitchSection BindSwitchSection(SwitchSectionSyntax syntax, Symbol parent)
 {
     return new BoundSwitchSection(
         syntax.Labels.Select(x => Bind(x, BindSwitchLabel)).ToImmutableArray(),
         syntax.Statements.Select(x => Bind(x, y => BindStatement(y, parent))).ToImmutableArray());
 }
コード例 #44
0
 private bool Equal(SwitchSectionSyntax left, SwitchSectionSyntax right)
 {
     return SyntaxFactory.AreEquivalent(left.Labels, right.Labels, null)
         && SyntaxFactory.AreEquivalent(left.Statements, right.Statements, ignoreChildNode: IgnoreLabeledChild);
 }
コード例 #45
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitSwitchSection(SwitchSectionSyntax node)
 {
     this.OnNodeVisited(node, this.type.IsInstanceOfType(node));
     base.VisitSwitchSection(node);
 }
コード例 #46
0
ファイル: LookupPosition.cs プロジェクト: tvsonar/roslyn
 /// <remarks>
 /// Used to determine whether it would be appropriate to use the binder for the switch section (if any).
 /// Not used to determine whether the position is syntactically within the statement.
 /// </remarks>
 internal static bool IsInSwitchSectionScope(int position, SwitchSectionSyntax section)
 {
     Debug.Assert(section != null);
     return section.Span.Contains(position);
 }
コード例 #47
0
ファイル: LocalBinderFactory.cs プロジェクト: CAPCHIK/roslyn
        public override void VisitSwitchSection(SwitchSectionSyntax node)
        {
            var patternBinder = new PatternVariableBinder(node, _enclosing);
            AddToMap(node, patternBinder);

            foreach (var label in node.Labels)
            {
                var match = label as CasePatternSwitchLabelSyntax;
                if (match != null)
                {
                    Visit(match.Pattern, patternBinder);
                    if (match.WhenClause != null)
                    {
                        Visit(match.WhenClause.Condition, patternBinder);
                    }
                }
            }

            foreach (StatementSyntax statement in node.Statements)
            {
                Visit(statement, patternBinder);
            }
        }
コード例 #48
0
ファイル: CppImplWalker.cs プロジェクト: ActiveMesa/Blackmire
 public override void VisitSwitchSection(SwitchSectionSyntax node)
 {
 }
コード例 #49
0
 public override void VisitSwitchSection(SwitchSectionSyntax node)
 {
     foreach (StatementSyntax statement in node.Statements)
     {
         Visit(statement, _enclosing);
     }
 }
コード例 #50
0
 public override SyntaxNode VisitSwitchSection(SwitchSectionSyntax node) //TODO: Should fix string switch here
 {
     return base.VisitSwitchSection(node);
 }
 private static SwitchSectionSyntax AddBraces(SwitchSectionSyntax section)
 {
     StatementSyntax blockStatement = SyntaxFactory.Block(section.Statements).WithoutTrailingTrivia();
     return section.Update(section.Labels, SyntaxFactory.SingletonList(blockStatement));
 }
コード例 #52
0
 public BoundSwitchSection(SwitchSectionSyntax syntax, ImmutableArray<BoundSwitchLabel> labels, ImmutableArray<BoundStatement> statements)
     : base(BoundNodeKind.SwitchSection, syntax)
 {
     Labels = labels;
     Statements = statements;
 }
コード例 #53
0
        public void VisitSwitchSection(SwitchSectionSyntax node)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            node.Validate();

            WriteLeadingTrivia(node);

            bool hadOne = false;

            foreach (var label in node.Labels)
            {
                if (hadOne)
                    _writer.WriteLine();
                else
                    hadOne = true;

                label.Accept(this);
            }

            _writer.PushBraceFormatting(_writer.Configuration.BracesLayout.BlockUnderCaseLabel, false);

            foreach (var statement in node.Statements)
            {
                VisitBlockStatement(statement);
            }

            _writer.PopBraceFormatting();

            WriteTrailingTrivia(node);
        }