IEnumerable <Cci.TypeReferenceWithAttributes> Cci.IGenericParameter.GetConstraints(EmitContext context)
        {
            var moduleBeingBuilt = (PEModuleBuilder)context.Module;

            var seenValueType = false;

            if (this.HasUnmanagedTypeConstraint)
            {
                var typeRef = moduleBeingBuilt.GetSpecialType(
                    SpecialType.System_ValueType,
                    syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt,
                    diagnostics: context.Diagnostics);

                var modifier = CSharpCustomModifier.CreateRequired(
                    moduleBeingBuilt.Compilation.GetWellKnownType(WellKnownType.core_runtime_UnmanagedType));

                // emit "(class [mscorlib]System.ValueType modreq([mscorlib]System.Runtime.InteropServices.UnmanagedType" pattern as "unmanaged"
                yield return(new Cci.TypeReferenceWithAttributes(new Cci.ModifiedTypeReference(typeRef, ImmutableArray.Create <Cci.ICustomModifier>(modifier))));

                // do not emit another one for Dev11 similarities
                seenValueType = true;
            }

            foreach (var type in this.ConstraintTypesNoUseSiteDiagnostics)
            {
                switch (type.SpecialType)
                {
                case SpecialType.System_Object:
                    Debug.Assert(!type.NullableAnnotation.IsAnyNullable());
                    break;

                case SpecialType.System_ValueType:
                    seenValueType = true;
                    break;
                }
                var typeRef = moduleBeingBuilt.Translate(type.TypeSymbol,
                                                         syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt,
                                                         diagnostics: context.Diagnostics);

                yield return(type.GetTypeRefWithAttributes(
                                 moduleBeingBuilt,
                                 declaringSymbol: this,
                                 typeRef));
            }

            if (this.HasValueTypeConstraint && !seenValueType)
            {
                // Add System.ValueType constraint to comply with Dev11 output
                var typeRef = moduleBeingBuilt.GetSpecialType(SpecialType.System_ValueType,
                                                              syntaxNodeOpt: (CSharpSyntaxNode)context.SyntaxNodeOpt,
                                                              diagnostics: context.Diagnostics);

                yield return(new Cci.TypeReferenceWithAttributes(typeRef));
            }
        }
Exemple #2
0
            internal InvokeMethod(
                SourceMemberContainerTypeSymbol delegateType,
                RefKind refKind,
                TypeSymbolWithAnnotations returnType,
                DelegateDeclarationSyntax syntax,
                Binder binder,
                DiagnosticBag diagnostics)
                : base(delegateType, returnType, syntax, MethodKind.DelegateInvoke, DeclarationModifiers.Virtual | DeclarationModifiers.Public)
            {
                this._refKind = refKind;

                SyntaxToken arglistToken;
                var         parameters = ParameterHelpers.MakeParameters(
                    binder, this, syntax.ParameterList, out arglistToken,
                    allowRefOrOut: true,
                    allowThis: false,
                    addRefReadOnlyModifier: true,
                    diagnostics: diagnostics);

                if (arglistToken.Kind() == SyntaxKind.ArgListKeyword)
                {
                    // This is a parse-time error in the native compiler; it is a semantic analysis error in Roslyn.

                    // error CS1669: __arglist is not valid in this context
                    diagnostics.Add(ErrorCode.ERR_IllegalVarArgs, new SourceLocation(arglistToken));
                }

                if (_refKind == RefKind.RefReadOnly)
                {
                    var modifierType = binder.GetWellKnownType(WellKnownType.core_runtime_InAttribute, diagnostics, syntax.ReturnType);
                    _refCustomModifiers = ImmutableArray.Create(CSharpCustomModifier.CreateRequired(modifierType));
                }
                else
                {
                    _refCustomModifiers = ImmutableArray <CustomModifier> .Empty;
                }

                InitializeParameters(parameters);
            }
        internal ImmutableArray <CustomModifier> SubstituteCustomModifiers(ImmutableArray <CustomModifier> customModifiers)
        {
            if (customModifiers.IsDefaultOrEmpty)
            {
                return(customModifiers);
            }

            for (int i = 0; i < customModifiers.Length; i++)
            {
                var modifier    = (NamedTypeSymbol)customModifiers[i].Modifier;
                var substituted = SubstituteNamedType(modifier);

                if (!TypeSymbol.Equals(modifier, substituted, TypeCompareKind.ConsiderEverything2))
                {
                    var builder = ArrayBuilder <CustomModifier> .GetInstance(customModifiers.Length);

                    builder.AddRange(customModifiers, i);

                    builder.Add(customModifiers[i].IsOptional ? CSharpCustomModifier.CreateOptional(substituted) : CSharpCustomModifier.CreateRequired(substituted));
                    for (i++; i < customModifiers.Length; i++)
                    {
                        modifier    = (NamedTypeSymbol)customModifiers[i].Modifier;
                        substituted = SubstituteNamedType(modifier);

                        if (!TypeSymbol.Equals(modifier, substituted, TypeCompareKind.ConsiderEverything2))
                        {
                            builder.Add(customModifiers[i].IsOptional ? CSharpCustomModifier.CreateOptional(substituted) : CSharpCustomModifier.CreateRequired(substituted));
                        }
                        else
                        {
                            builder.Add(customModifiers[i]);
                        }
                    }

                    Debug.Assert(builder.Count == customModifiers.Length);
                    return(builder.ToImmutableAndFree());
                }
            }

            return(customModifiers);
        }
Exemple #4
0
        public static SourceParameterSymbol Create(
            Binder context,
            Symbol owner,
            TypeSymbolWithAnnotations parameterType,
            ParameterSyntax syntax,
            RefKind refKind,
            SyntaxToken identifier,
            int ordinal,
            bool isParams,
            bool isExtensionMethodThis,
            bool addRefReadOnlyModifier,
            DiagnosticBag declarationDiagnostics)
        {
            var name      = identifier.ValueText;
            var locations = ImmutableArray.Create <Location>(new SourceLocation(identifier));

            if (isParams)
            {
                // touch the constructor in order to generate proper use-site diagnostics
                Binder.ReportUseSiteDiagnosticForSynthesizedAttribute(context.Compilation,
                                                                      WellKnownMember.System_ParamArrayAttribute__ctor,
                                                                      declarationDiagnostics,
                                                                      identifier.Parent.GetLocation());
            }

            if (addRefReadOnlyModifier && refKind == RefKind.In)
            {
                var modifierType = context.GetWellKnownType(WellKnownType.core_runtime_InAttribute, declarationDiagnostics, syntax);

                return(new SourceComplexParameterSymbolWithCustomModifiersPrecedingByRef(
                           owner,
                           ordinal,
                           parameterType,
                           refKind,
                           ImmutableArray.Create(CSharpCustomModifier.CreateRequired(modifierType)),
                           name,
                           locations,
                           syntax.GetReference(),
                           ConstantValue.Unset,
                           isParams,
                           isExtensionMethodThis));
            }

            if (!isParams &&
                !isExtensionMethodThis &&
                (syntax.Default == null) &&
                (syntax.AttributeLists.Count == 0) &&
                !owner.IsPartialMethod())
            {
                return(new SourceSimpleParameterSymbol(owner, parameterType, ordinal, refKind, name, locations));
            }

            return(new SourceComplexParameterSymbol(
                       owner,
                       ordinal,
                       parameterType,
                       refKind,
                       name,
                       locations,
                       syntax.GetReference(),
                       ConstantValue.Unset,
                       isParams,
                       isExtensionMethodThis));
        }