예제 #1
0
 public static ImmutableArray <ParameterSymbol> MakeParameters(
     Binder binder,
     Symbol owner,
     BaseParameterListSyntax syntax,
     out SyntaxToken arglistToken,
     BindingDiagnosticBag diagnostics,
     bool allowRefOrOut,
     bool allowThis,
     bool addRefReadOnlyModifier
     )
 {
     return(MakeParameters <ParameterSyntax, ParameterSymbol, Symbol>(
                binder,
                owner,
                syntax.Parameters,
                out arglistToken,
                diagnostics,
                allowRefOrOut,
                allowThis,
                addRefReadOnlyModifier,
                suppressUseSiteDiagnostics: false,
                lastIndex: syntax.Parameters.Count - 1,
                parameterCreationFunc: (
                    Binder context,
                    Symbol owner,
                    TypeWithAnnotations parameterType,
                    ParameterSyntax syntax,
                    RefKind refKind,
                    int ordinal,
                    SyntaxToken paramsKeyword,
                    SyntaxToken thisKeyword,
                    bool addRefReadOnlyModifier,
                    BindingDiagnosticBag declarationDiagnostics
                    ) =>
     {
         return SourceParameterSymbol.Create(
             context,
             owner,
             parameterType,
             syntax,
             refKind,
             syntax.Identifier,
             ordinal,
             isParams: paramsKeyword.Kind() != SyntaxKind.None,
             isExtensionMethodThis: ordinal == 0 &&
             thisKeyword.Kind() != SyntaxKind.None,
             addRefReadOnlyModifier,
             declarationDiagnostics
             );
     }
                ));
 }
예제 #2
0
        public static ImmutableArray <ParameterSymbol> MakeParameters(
            Binder binder,
            Symbol owner,
            BaseParameterListSyntax syntax,
            bool allowRefOrOut,
            out SyntaxToken arglistToken,
            DiagnosticBag diagnostics,
            bool beStrict)
        {
            arglistToken = default(SyntaxToken);

            int parameterIndex = 0;
            int firstDefault   = -1;

            var builder = ArrayBuilder <ParameterSymbol> .GetInstance();

            ImmutableArray <ParameterSymbol> parameters;

            foreach (var parameterSyntax in syntax.Parameters)
            {
                SyntaxToken outKeyword;
                SyntaxToken refKeyword;
                SyntaxToken paramsKeyword;
                SyntaxToken thisKeyword;
                var         refKind = GetModifiers(parameterSyntax.Modifiers, out outKeyword, out refKeyword, out paramsKeyword, out thisKeyword);

                if (parameterSyntax.IsArgList)
                {
                    arglistToken = parameterSyntax.Identifier;
                    // The native compiler produces "Expected type" here, in the parser. Roslyn produces
                    // the somewhat more informative "arglist not valid" error.
                    if (paramsKeyword.Kind() != SyntaxKind.None || outKeyword.Kind() != SyntaxKind.None ||
                        refKeyword.Kind() != SyntaxKind.None || thisKeyword.Kind() != SyntaxKind.None)
                    {
                        // CS1669: __arglist is not valid in this context
                        diagnostics.Add(ErrorCode.ERR_IllegalVarArgs, arglistToken.GetLocation());
                    }
                    continue;
                }

                if (parameterSyntax.Default != null && firstDefault == -1)
                {
                    firstDefault = parameterIndex;
                }

                Debug.Assert(parameterSyntax.Type != null);
                var parameterType = binder.BindType(parameterSyntax.Type, diagnostics);

                if (!allowRefOrOut && (refKind != RefKind.None))
                {
                    var outOrRefKeyword = (outKeyword.Kind() != SyntaxKind.None) ? outKeyword : refKeyword;
                    Debug.Assert(outOrRefKeyword.Kind() != SyntaxKind.None);

                    // error CS0631: ref and out are not valid in this context
                    diagnostics.Add(ErrorCode.ERR_IllegalRefParam, outOrRefKeyword.GetLocation());
                }

                var parameter = SourceParameterSymbol.Create(
                    binder,
                    owner,
                    parameterType,
                    parameterSyntax,
                    refKind,
                    parameterSyntax.Identifier,
                    parameterIndex,
                    (paramsKeyword.Kind() != SyntaxKind.None),
                    parameterIndex == 0 && thisKeyword.Kind() != SyntaxKind.None,
                    diagnostics,
                    beStrict);

                ReportParameterErrors(owner, parameterSyntax, parameter, firstDefault, diagnostics);

                builder.Add(parameter);
                ++parameterIndex;
            }

            parameters = builder.ToImmutableAndFree();

            var methodOwner    = owner as MethodSymbol;
            var typeParameters = (object)methodOwner != null ?
                                 methodOwner.TypeParameters :
                                 default(ImmutableArray <TypeParameterSymbol>);

            binder.ValidateParameterNameConflicts(typeParameters, parameters, diagnostics);
            return(parameters);
        }
예제 #3
0
        public static ImmutableArray <ParameterSymbol> MakeParameters(
            Binder binder,
            Symbol owner,
            BaseParameterListSyntax syntax,
            out SyntaxToken arglistToken,
            DiagnosticBag diagnostics,
            bool allowRefOrOut,
            bool allowThis,
            bool addRefReadOnlyModifier)
        {
            arglistToken = default(SyntaxToken);

            int parameterIndex = 0;
            int firstDefault   = -1;

            var builder = ArrayBuilder <ParameterSymbol> .GetInstance();

            var mustBeLastParameter = (ParameterSyntax)null;

            foreach (var parameterSyntax in syntax.Parameters)
            {
                if (mustBeLastParameter == null)
                {
                    if (parameterSyntax.Modifiers.Any(SyntaxKind.ParamsKeyword) ||
                        parameterSyntax.Identifier.Kind() == SyntaxKind.ArgListKeyword)
                    {
                        mustBeLastParameter = parameterSyntax;
                    }
                }

                CheckParameterModifiers(parameterSyntax, diagnostics);

                var refKind = GetModifiers(parameterSyntax.Modifiers, out SyntaxToken refnessKeyword, out SyntaxToken paramsKeyword, out SyntaxToken thisKeyword);
                if (thisKeyword.Kind() != SyntaxKind.None && !allowThis)
                {
                    diagnostics.Add(ErrorCode.ERR_ThisInBadContext, thisKeyword.GetLocation());
                }

                if (parameterSyntax.IsArgList)
                {
                    arglistToken = parameterSyntax.Identifier;
                    // The native compiler produces "Expected type" here, in the parser. Roslyn produces
                    // the somewhat more informative "arglist not valid" error.
                    if (paramsKeyword.Kind() != SyntaxKind.None ||
                        refnessKeyword.Kind() != SyntaxKind.None ||
                        thisKeyword.Kind() != SyntaxKind.None)
                    {
                        // CS1669: __arglist is not valid in this context
                        diagnostics.Add(ErrorCode.ERR_IllegalVarArgs, arglistToken.GetLocation());
                    }
                    continue;
                }

                if (parameterSyntax.Default != null && firstDefault == -1)
                {
                    firstDefault = parameterIndex;
                }

                Debug.Assert(parameterSyntax.Type != null);
                var parameterType = binder.BindType(parameterSyntax.Type, diagnostics);

                if (!allowRefOrOut && (refKind == RefKind.Ref || refKind == RefKind.Out))
                {
                    Debug.Assert(refnessKeyword.Kind() != SyntaxKind.None);

                    // error CS0631: ref and out are not valid in this context
                    diagnostics.Add(ErrorCode.ERR_IllegalRefParam, refnessKeyword.GetLocation());
                }

                var parameter = SourceParameterSymbol.Create(
                    binder,
                    owner,
                    parameterType,
                    parameterSyntax,
                    refKind,
                    parameterSyntax.Identifier,
                    parameterIndex,
                    (paramsKeyword.Kind() != SyntaxKind.None),
                    parameterIndex == 0 && thisKeyword.Kind() != SyntaxKind.None,
                    addRefReadOnlyModifier,
                    diagnostics);

                ReportParameterErrors(owner, parameterSyntax, parameter, thisKeyword, paramsKeyword, firstDefault, diagnostics);

                builder.Add(parameter);
                ++parameterIndex;
            }

            if (mustBeLastParameter != null && mustBeLastParameter != syntax.Parameters.Last())
            {
                diagnostics.Add(
                    mustBeLastParameter.Identifier.Kind() == SyntaxKind.ArgListKeyword
                        ? ErrorCode.ERR_VarargsLast
                        : ErrorCode.ERR_ParamsLast,
                    mustBeLastParameter.GetLocation());
            }

            ImmutableArray <ParameterSymbol> parameters = builder.ToImmutableAndFree();

            var methodOwner    = owner as MethodSymbol;
            var typeParameters = (object)methodOwner != null ?
                                 methodOwner.TypeParameters :
                                 default(ImmutableArray <TypeParameterSymbol>);

            binder.ValidateParameterNameConflicts(typeParameters, parameters, diagnostics);
            return(parameters);
        }