) As BoundExpression

in src/Compilers/VisualBasic/Portable/Binding/Binder_Invocation.vb [1351:1649]


        ) As BoundExpression

            Debug.Assert(commonReturnType IsNot Nothing AndAlso bestSymbols.Length > 0 AndAlso bestCandidates.Count >= bestSymbols.Length)
            Debug.Assert(groupOpt Is Nothing OrElse lookupResult = groupOpt.ResultKind)

            Dim state = OverloadResolution.CandidateAnalysisResultState.Count

            If bestCandidates.Count > 0 Then
                state = bestCandidates(0).State
            End If

            If boundArguments.IsDefault Then
                boundArguments = ImmutableArray(Of BoundExpression).Empty
            End If

            Dim singleCandidateAnalysisResult As OverloadResolution.CandidateAnalysisResult = Nothing
            Dim singleCandidate As OverloadResolution.Candidate = Nothing
            Dim allowUnexpandedParamArrayForm As Boolean = False
            Dim allowExpandedParamArrayForm As Boolean = False

            
            If bestSymbols.Length = 1 AndAlso bestCandidates.Count < 3 Then
                singleCandidateAnalysisResult = bestCandidates(0)
                singleCandidate = singleCandidateAnalysisResult.Candidate
                allowExpandedParamArrayForm = singleCandidateAnalysisResult.IsExpandedParamArrayForm
                allowUnexpandedParamArrayForm = Not allowExpandedParamArrayForm

                If bestCandidates.Count > 1 Then
                    If bestCandidates(1).IsExpandedParamArrayForm Then
                        allowExpandedParamArrayForm = True
                    Else
                        allowUnexpandedParamArrayForm = True
                    End If
                End If
            End If

            If lookupResult = LookupResultKind.Inaccessible Then
                If singleCandidate IsNot Nothing Then
                    ReportDiagnostic(diagnostics, If(groupOpt IsNot Nothing, groupOpt.Syntax, node), GetInaccessibleErrorInfo(singleCandidate.UnderlyingSymbol, useSiteDiagnostics:=Nothing))
                Else
                    If Not queryMode Then
                        ReportDiagnostic(diagnostics, If(groupOpt IsNot Nothing, groupOpt.Syntax, node), ERRID.ERR_NoViableOverloadCandidates1, bestSymbols(0).Name)
                    End If

                    
                    GoTo ProduceBoundNode
                End If
            Else
                Debug.Assert(lookupResult = LookupResultKind.Good)
            End If

            If diagnosticLocationOpt Is Nothing Then
                diagnosticLocationOpt = GetLocationForOverloadResolutionDiagnostic(node, groupOpt)
            End If

            
            Select Case state

                Case VisualBasic.OverloadResolution.CandidateAnalysisResultState.HasUseSiteError, OverloadResolution.CandidateAnalysisResultState.HasUnsupportedMetadata

                    If singleCandidate IsNot Nothing Then
                        ReportOverloadResolutionFailureForASingleCandidate(node, diagnosticLocationOpt, lookupResult, singleCandidateAnalysisResult,
                                                                 boundArguments, argumentNames,
                                                                 allowUnexpandedParamArrayForm,
                                                                 allowExpandedParamArrayForm,
                                                                 True,
                                                                 False,
                                                                 diagnostics,
                                                                 delegateSymbol:=delegateSymbol,
                                                                 queryMode:=queryMode,
                                                                 callerInfoOpt:=callerInfoOpt,
                                                                 representCandidateInDiagnosticsOpt:=representCandidateInDiagnosticsOpt)

                    Else
                        ReportOverloadResolutionFailureForASetOfCandidates(node, diagnosticLocationOpt, lookupResult,
                                                        ERRID.ERR_BadOverloadCandidates2,
                                                        bestCandidates,
                                                        boundArguments,
                                                        argumentNames,
                                                        diagnostics,
                                                        delegateSymbol:=delegateSymbol,
                                                        queryMode:=queryMode,
                                                        callerInfoOpt:=callerInfoOpt)
                    End If

                Case VisualBasic.OverloadResolution.CandidateAnalysisResultState.Ambiguous
                    Dim candidate As Symbol = bestSymbols(0).OriginalDefinition
                    Dim container As Symbol = candidate.ContainingSymbol
                    ReportDiagnostic(diagnostics, diagnosticLocationOpt, ERRID.ERR_MetadataMembersAmbiguous3, candidate.Name, container.GetKindText(), container)

                Case OverloadResolution.CandidateAnalysisResultState.BadGenericArity
                    Debug.Assert(groupOpt IsNot Nothing AndAlso groupOpt.Kind = BoundKind.MethodGroup)
                    Dim mg = DirectCast(groupOpt, BoundMethodGroup)

                    If singleCandidate IsNot Nothing Then
                        Dim typeArguments = If(mg.TypeArgumentsOpt IsNot Nothing, mg.TypeArgumentsOpt.Arguments, ImmutableArray(Of TypeSymbol).Empty)

                        If typeArguments.IsDefault Then
                            typeArguments = ImmutableArray(Of TypeSymbol).Empty
                        End If

                        Dim singleSymbol As Symbol = singleCandidate.UnderlyingSymbol
                        Dim isExtension As Boolean = singleCandidate.IsExtensionMethod

                        If singleCandidate.Arity < typeArguments.Length Then
                            If isExtension Then
                                ReportDiagnostic(diagnostics, mg.TypeArgumentsOpt.Syntax,
                                                 If(singleCandidate.Arity = 0, ERRID.ERR_TypeOrMemberNotGeneric2, ERRID.ERR_TooManyGenericArguments2),
                                                 singleSymbol, singleSymbol.ContainingType)
                            Else
                                ReportDiagnostic(diagnostics, mg.TypeArgumentsOpt.Syntax,
                                                 If(singleCandidate.Arity = 0, ERRID.ERR_TypeOrMemberNotGeneric1, ERRID.ERR_TooManyGenericArguments1),
                                                 singleSymbol)
                            End If
                        Else
                            Debug.Assert(singleCandidate.Arity > typeArguments.Length)

                            If isExtension Then
                                ReportDiagnostic(diagnostics, mg.TypeArgumentsOpt.Syntax,
                                                 ERRID.ERR_TooFewGenericArguments2,
                                                 singleSymbol, singleSymbol.ContainingType)
                            Else
                                ReportDiagnostic(diagnostics, mg.TypeArgumentsOpt.Syntax,
                                                 ERRID.ERR_TooFewGenericArguments1, singleSymbol)
                            End If
                        End If
                    Else
                        ReportDiagnostic(diagnostics, diagnosticLocationOpt, ERRID.ERR_NoTypeArgumentCountOverloadCand1, CustomSymbolDisplayFormatter.ShortErrorName(bestSymbols(0)))
                    End If

                Case OverloadResolution.CandidateAnalysisResultState.ArgumentCountMismatch

                    If node.Kind = SyntaxKind.IdentifierName AndAlso
                        node.Parent IsNot Nothing AndAlso
                        node.Parent.Kind = SyntaxKind.NamedFieldInitializer AndAlso
                        groupOpt IsNot Nothing AndAlso
                        groupOpt.Kind = BoundKind.PropertyGroup Then

                        
                        
                        ReportDiagnostic(diagnostics,
                                         diagnosticLocationOpt,
                                         If(singleCandidate IsNot Nothing,
                                            ERRID.ERR_ParameterizedPropertyInAggrInit1,
                                            ERRID.ERR_NoZeroCountArgumentInitCandidates1),
                                         CustomSymbolDisplayFormatter.ShortErrorName(bestSymbols(0)))
                    Else
                        If singleCandidate IsNot Nothing AndAlso
                           (Not queryMode OrElse singleCandidate.ParameterCount <= boundArguments.Length) Then
                            ReportOverloadResolutionFailureForASingleCandidate(node, diagnosticLocationOpt, lookupResult, singleCandidateAnalysisResult,
                                                                         boundArguments, argumentNames,
                                                                         allowUnexpandedParamArrayForm,
                                                                         allowExpandedParamArrayForm,
                                                                         True,
                                                                         False,
                                                                         diagnostics,
                                                                         delegateSymbol:=delegateSymbol,
                                                                         queryMode:=queryMode,
                                                                         callerInfoOpt:=callerInfoOpt,
                                                                         representCandidateInDiagnosticsOpt:=representCandidateInDiagnosticsOpt)

                        Else
                            ReportDiagnostic(diagnostics, diagnosticLocationOpt, ERRID.ERR_NoArgumentCountOverloadCandidates1, CustomSymbolDisplayFormatter.ShortErrorName(bestSymbols(0)))
                        End If
                    End If

                Case OverloadResolution.CandidateAnalysisResultState.ArgumentMismatch,
                     OverloadResolution.CandidateAnalysisResultState.GenericConstraintsViolated

                    Dim haveBadArgument As Boolean = False

                    For i As Integer = 0 To boundArguments.Length - 1 Step 1
                        Dim type = boundArguments(i).Type

                        If boundArguments(i).HasErrors OrElse (type IsNot Nothing AndAlso type.IsErrorType()) Then
                            haveBadArgument = True
                            Exit For
                        End If
                    Next

                    If Not haveBadArgument Then
                        If singleCandidate IsNot Nothing Then
                            ReportOverloadResolutionFailureForASingleCandidate(node, diagnosticLocationOpt, lookupResult, singleCandidateAnalysisResult,
                                                                     boundArguments, argumentNames,
                                                                     allowUnexpandedParamArrayForm,
                                                                     allowExpandedParamArrayForm,
                                                                     True,
                                                                     False,
                                                                     diagnostics,
                                                                     delegateSymbol:=delegateSymbol,
                                                                     queryMode:=queryMode,
                                                                     callerInfoOpt:=callerInfoOpt,
                                                                     representCandidateInDiagnosticsOpt:=representCandidateInDiagnosticsOpt)

                        Else
                            ReportOverloadResolutionFailureForASetOfCandidates(node, diagnosticLocationOpt, lookupResult,
                                                            If(delegateSymbol Is Nothing,
                                                               ERRID.ERR_NoCallableOverloadCandidates2,
                                                               ERRID.ERR_DelegateBindingFailure3),
                                                            bestCandidates,
                                                            boundArguments,
                                                            argumentNames,
                                                            diagnostics,
                                                            delegateSymbol:=delegateSymbol,
                                                            queryMode:=queryMode,
                                                            callerInfoOpt:=callerInfoOpt)
                        End If
                    End If

                Case OverloadResolution.CandidateAnalysisResultState.TypeInferenceFailed

                    If singleCandidate IsNot Nothing Then
                        ReportOverloadResolutionFailureForASingleCandidate(node, diagnosticLocationOpt, lookupResult, singleCandidateAnalysisResult,
                                                                 boundArguments, argumentNames,
                                                                 allowUnexpandedParamArrayForm,
                                                                 allowExpandedParamArrayForm,
                                                                 True,
                                                                 False,
                                                                 diagnostics,
                                                                 delegateSymbol:=delegateSymbol,
                                                                 queryMode:=queryMode,
                                                                 callerInfoOpt:=callerInfoOpt,
                                                                 representCandidateInDiagnosticsOpt:=representCandidateInDiagnosticsOpt)

                    Else
                        ReportOverloadResolutionFailureForASetOfCandidates(node, diagnosticLocationOpt, lookupResult,
                                                        ERRID.ERR_NoCallableOverloadCandidates2,
                                                        bestCandidates,
                                                        boundArguments,
                                                        argumentNames,
                                                        diagnostics,
                                                        delegateSymbol:=delegateSymbol,
                                                        queryMode:=queryMode,
                                                        callerInfoOpt:=callerInfoOpt)
                    End If

                Case OverloadResolution.CandidateAnalysisResultState.Applicable

                    
                    
                    Debug.Assert(singleCandidate Is Nothing OrElse
                                 singleCandidate.ParameterCount <> 0 AndAlso
                                 singleCandidate.Parameters(singleCandidate.ParameterCount - 1).IsParamArray)

                    If bestCandidates(0).RequiresNarrowingConversion Then
                        ReportOverloadResolutionFailureForASetOfCandidates(node, diagnosticLocationOpt, lookupResult,
                                                        If(delegateSymbol Is Nothing,
                                                            ERRID.ERR_NoNonNarrowingOverloadCandidates2,
                                                            ERRID.ERR_DelegateBindingFailure3),
                                                        bestCandidates,
                                                        boundArguments,
                                                        argumentNames,
                                                        diagnostics,
                                                        delegateSymbol:=delegateSymbol,
                                                        queryMode:=queryMode,
                                                        callerInfoOpt:=callerInfoOpt)
                    Else
                        ReportUnspecificProcedures(diagnosticLocationOpt, bestSymbols, diagnostics, (delegateSymbol IsNot Nothing))
                    End If

                Case Else
                    
                    Throw ExceptionUtilities.UnexpectedValue(state)
            End Select

ProduceBoundNode:
            Dim childBoundNodes As ImmutableArray(Of BoundExpression)

            If boundArguments.IsEmpty AndAlso boundTypeExpression Is Nothing Then
                If groupOpt Is Nothing Then
                    childBoundNodes = ImmutableArray(Of BoundExpression).Empty
                Else
                    childBoundNodes = ImmutableArray.Create(Of BoundExpression)(groupOpt)
                End If
            Else
                Dim builder = ArrayBuilder(Of BoundExpression).GetInstance()

                If groupOpt IsNot Nothing Then
                    builder.Add(groupOpt)
                End If

                If Not boundArguments.IsEmpty Then
                    builder.AddRange(boundArguments)
                End If

                If boundTypeExpression IsNot Nothing Then
                    builder.Add(boundTypeExpression)
                End If

                childBoundNodes = builder.ToImmutableAndFree()
            End If

            Dim resultKind = LookupResultKind.OverloadResolutionFailure
            If lookupResult < resultKind Then
                resultKind = lookupResult
            End If

            Return New BoundBadExpression(node, resultKind, bestSymbols, childBoundNodes, commonReturnType, hasErrors:=True)
        End Function