Beispiel #1
0
        private void BuildSwitchLabels(SyntaxList <SwitchLabelSyntax> labelsSyntax, Binder sectionBinder, ArrayBuilder <LabelSymbol> labels, DiagnosticBag tempDiagnosticBag)
        {
            // add switch case/default labels
            foreach (var labelSyntax in labelsSyntax)
            {
                ConstantValue boundLabelConstantOpt = null;
                switch (labelSyntax.Kind())
                {
                case SyntaxKind.CaseSwitchLabel:
                    // compute the constant value to place in the label symbol
                    var caseLabel = (CaseSwitchLabelSyntax)labelSyntax;
                    Debug.Assert(caseLabel.Value != null);
                    var boundLabelExpression = sectionBinder.BindValue(caseLabel.Value, tempDiagnosticBag, BindValueKind.RValue);
                    boundLabelExpression = ConvertCaseExpression(labelSyntax, boundLabelExpression, sectionBinder, out boundLabelConstantOpt, tempDiagnosticBag);
                    break;

                case SyntaxKind.CasePatternSwitchLabel:
                    // bind the pattern, to cause its pattern variables to be inferred if necessary
                    var matchLabel = (CasePatternSwitchLabelSyntax)labelSyntax;
                    var pattern    = sectionBinder.BindPattern(
                        matchLabel.Pattern, SwitchGoverningType, SwitchGoverningValEscape, labelSyntax.HasErrors, tempDiagnosticBag);
                    break;

                default:
                    // No constant value
                    break;
                }

                // Create the label symbol
                labels.Add(new SourceLabelSymbol((MethodSymbol)this.ContainingMemberOrLambda, labelSyntax, boundLabelConstantOpt));
            }
        }
        internal override BoundSwitchExpressionArm BindSwitchExpressionArm(SwitchExpressionArmSyntax node, DiagnosticBag diagnostics)
        {
            Debug.Assert(node == _arm);
            Binder armBinder = this.GetBinder(node);
            bool   hasErrors = _switchExpressionBinder.SwitchGoverningType.IsErrorType();
            ImmutableArray <LocalSymbol> locals = _armScopeBinder.Locals;
            BoundPattern    pattern             = armBinder.BindPattern(node.Pattern, _switchExpressionBinder.SwitchGoverningType, _switchExpressionBinder.SwitchGoverningValEscape, hasErrors, diagnostics);
            BoundExpression whenClause          = node.WhenClause != null
                ? armBinder.BindBooleanExpression(node.WhenClause.Condition, diagnostics)
                : null;

            BoundExpression armResult = armBinder.BindValue(node.Expression, diagnostics, BindValueKind.RValue);
            var             label     = new GeneratedLabelSymbol("arm");

            return(new BoundSwitchExpressionArm(node, locals, pattern, whenClause, armResult, label, hasErrors | pattern.HasErrors));
        }
Beispiel #3
0
        protected BoundExpression BindTargetExpression(DiagnosticBag diagnostics, Binder originalBinder)
        {
            if (_lazyExpressionAndDiagnostics == null)
            {
                // Filter out method group in conversion.
                DiagnosticBag   expressionDiagnostics = DiagnosticBag.GetInstance();
                BoundExpression boundExpression       = originalBinder.BindValue(TargetExpressionSyntax, expressionDiagnostics, Binder.BindValueKind.RValueOrMethodGroup);
                Interlocked.CompareExchange(ref _lazyExpressionAndDiagnostics, new ExpressionAndDiagnostics(boundExpression, expressionDiagnostics.ToReadOnlyAndFree()), null);
            }
            Debug.Assert(_lazyExpressionAndDiagnostics != null);

            if (diagnostics != null)
            {
                diagnostics.AddRange(_lazyExpressionAndDiagnostics.Diagnostics);
            }

            return(_lazyExpressionAndDiagnostics.Expression);
        }