Beispiel #1
0
        private void CreateEmbeddedAttributesIfNeeded(DiagnosticBag diagnostics)
        {
            EmbeddableAttributes needsAttributes = GetNeedsGeneratedAttributes();

            if (needsAttributes == 0)
            {
                return;
            }

            CreateEmbeddedAttributeIfNeeded(
                ref _lazyEmbeddedAttribute,
                diagnostics,
                AttributeDescription.CodeAnalysisEmbeddedAttribute);

            if ((needsAttributes & EmbeddableAttributes.IsReadOnlyAttribute) != 0)
            {
                CreateEmbeddedAttributeIfNeeded(
                    ref _lazyIsReadOnlyAttribute,
                    diagnostics,
                    AttributeDescription.IsReadOnlyAttribute);
            }

            if ((needsAttributes & EmbeddableAttributes.IsByRefLikeAttribute) != 0)
            {
                CreateEmbeddedAttributeIfNeeded(
                    ref _lazyIsByRefLikeAttribute,
                    diagnostics,
                    AttributeDescription.IsByRefLikeAttribute);
            }

            if ((needsAttributes & EmbeddableAttributes.IsUnmanagedAttribute) != 0)
            {
                CreateEmbeddedAttributeIfNeeded(
                    ref _lazyIsUnmanagedAttribute,
                    diagnostics,
                    AttributeDescription.IsUnmanagedAttribute);
            }

            if ((needsAttributes & EmbeddableAttributes.NullableAttribute) != 0)
            {
                CreateEmbeddedNullableAttributeIfNeeded(
                    ref _lazyNullableAttribute,
                    diagnostics);
            }

            if ((needsAttributes & EmbeddableAttributes.NullableContextAttribute) != 0)
            {
                CreateEmbeddedNullableContextAttributeIfNeeded(
                    ref _lazyNullableContextAttribute,
                    diagnostics);
            }

            if ((needsAttributes & EmbeddableAttributes.NullablePublicOnlyAttribute) != 0)
            {
                CreateEmbeddedNullablePublicOnlyAttributeIfNeeded(
                    ref _lazyNullablePublicOnlyAttribute,
                    diagnostics);
            }
        }
        private void EnsureEmbeddableAttributeExists(EmbeddableAttributes attribute, DiagnosticBag diagnostics, Location location, bool modifyCompilation)
        {
            Debug.Assert(!modifyCompilation || !_needsGeneratedAttributes_IsFrozen);

            if (CheckIfAttributeShouldBeEmbedded(attribute, diagnostics, location) && modifyCompilation)
            {
                SetNeedsGeneratedAttributes(attribute);
            }
        }
        internal bool CheckIfAttributeShouldBeEmbedded(EmbeddableAttributes attribute, DiagnosticBag diagnosticsOpt, Location locationOpt)
        {
            switch (attribute)
            {
            case EmbeddableAttributes.IsReadOnlyAttribute:
                return(CheckIfAttributeShouldBeEmbedded(
                           diagnosticsOpt,
                           locationOpt,
                           WellKnownType.System_Runtime_CompilerServices_IsReadOnlyAttribute,
                           WellKnownMember.System_Runtime_CompilerServices_IsReadOnlyAttribute__ctor));

            case EmbeddableAttributes.IsByRefLikeAttribute:
                return(CheckIfAttributeShouldBeEmbedded(
                           diagnosticsOpt,
                           locationOpt,
                           WellKnownType.System_Runtime_CompilerServices_IsByRefLikeAttribute,
                           WellKnownMember.System_Runtime_CompilerServices_IsByRefLikeAttribute__ctor));

            case EmbeddableAttributes.IsUnmanagedAttribute:
                return(CheckIfAttributeShouldBeEmbedded(
                           diagnosticsOpt,
                           locationOpt,
                           WellKnownType.System_Runtime_CompilerServices_IsUnmanagedAttribute,
                           WellKnownMember.System_Runtime_CompilerServices_IsUnmanagedAttribute__ctor));

            case EmbeddableAttributes.NullableAttribute:
                // Note: if the type exists, we'll check both constructors, regardless of which one(s) we'll eventually need
                return(CheckIfAttributeShouldBeEmbedded(
                           diagnosticsOpt,
                           locationOpt,
                           WellKnownType.System_Runtime_CompilerServices_NullableAttribute,
                           WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorByte,
                           WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorTransformFlags));

            case EmbeddableAttributes.NullableContextAttribute:
                return(CheckIfAttributeShouldBeEmbedded(
                           diagnosticsOpt,
                           locationOpt,
                           WellKnownType.System_Runtime_CompilerServices_NullableContextAttribute,
                           WellKnownMember.System_Runtime_CompilerServices_NullableContextAttribute__ctor));

            case EmbeddableAttributes.NullablePublicOnlyAttribute:
                return(CheckIfAttributeShouldBeEmbedded(
                           diagnosticsOpt,
                           locationOpt,
                           WellKnownType.System_Runtime_CompilerServices_NullablePublicOnlyAttribute,
                           WellKnownMember.System_Runtime_CompilerServices_NullablePublicOnlyAttribute__ctor));

            default:
                throw ExceptionUtilities.UnexpectedValue(attribute);
            }
        }
        private void CreateEmbeddedAttributesIfNeeded(DiagnosticBag diagnostics)
        {
            EmbeddableAttributes needsAttributes = GetNeedsGeneratedAttributes();

            if (ShouldEmitNullablePublicOnlyAttribute() &&
                Compilation.CheckIfAttributeShouldBeEmbedded(EmbeddableAttributes.NullablePublicOnlyAttribute, diagnostics, Location.None))
            {
                needsAttributes |= EmbeddableAttributes.NullablePublicOnlyAttribute;
            }
            else if (needsAttributes == 0)
            {
                return;
            }

            var createParameterlessEmbeddedAttributeSymbol = new Func <string, NamespaceSymbol, DiagnosticBag, SynthesizedEmbeddedAttributeSymbol>(CreateParameterlessEmbeddedAttributeSymbol);

            CreateAttributeIfNeeded(
                ref _lazyEmbeddedAttribute,
                diagnostics,
                AttributeDescription.CodeAnalysisEmbeddedAttribute,
                createParameterlessEmbeddedAttributeSymbol);

            if ((needsAttributes & EmbeddableAttributes.IsReadOnlyAttribute) != 0)
            {
                CreateAttributeIfNeeded(
                    ref _lazyIsReadOnlyAttribute,
                    diagnostics,
                    AttributeDescription.IsReadOnlyAttribute,
                    createParameterlessEmbeddedAttributeSymbol);
            }

            if ((needsAttributes & EmbeddableAttributes.IsByRefLikeAttribute) != 0)
            {
                CreateAttributeIfNeeded(
                    ref _lazyIsByRefLikeAttribute,
                    diagnostics,
                    AttributeDescription.IsByRefLikeAttribute,
                    createParameterlessEmbeddedAttributeSymbol);
            }

            if ((needsAttributes & EmbeddableAttributes.IsUnmanagedAttribute) != 0)
            {
                CreateAttributeIfNeeded(
                    ref _lazyIsUnmanagedAttribute,
                    diagnostics,
                    AttributeDescription.IsUnmanagedAttribute,
                    createParameterlessEmbeddedAttributeSymbol);
            }

            if ((needsAttributes & EmbeddableAttributes.NullableAttribute) != 0)
            {
                CreateAttributeIfNeeded(
                    ref _lazyNullableAttribute,
                    diagnostics,
                    AttributeDescription.NullableAttribute,
                    CreateNullableAttributeSymbol);
            }

            if ((needsAttributes & EmbeddableAttributes.NullableContextAttribute) != 0)
            {
                CreateAttributeIfNeeded(
                    ref _lazyNullableContextAttribute,
                    diagnostics,
                    AttributeDescription.NullableContextAttribute,
                    CreateNullableContextAttributeSymbol);
            }

            if ((needsAttributes & EmbeddableAttributes.NullablePublicOnlyAttribute) != 0)
            {
                CreateAttributeIfNeeded(
                    ref _lazyNullablePublicOnlyAttribute,
                    diagnostics,
                    AttributeDescription.NullablePublicOnlyAttribute,
                    CreateNullablePublicOnlyAttributeSymbol);
            }

            if ((needsAttributes & EmbeddableAttributes.NativeIntegerAttribute) != 0)
            {
                CreateAttributeIfNeeded(
                    ref _lazyNativeIntegerAttribute,
                    diagnostics,
                    AttributeDescription.NativeIntegerAttribute,
                    CreateNativeIntegerAttributeSymbol);
            }
        }
Beispiel #5
0
 private void SetNeedsGeneratedAttributes(EmbeddableAttributes attributes)
 {
     Debug.Assert(!_needsGeneratedAttributes_IsFrozen);
     ThreadSafeFlagOperations.Set(ref _needsGeneratedAttributes, (int)attributes);
 }
Beispiel #6
0
        private void CreateEmbeddedAttributesIfNeeded(DiagnosticBag diagnostics)
        {
            EmbeddableAttributes needsAttributes = GetNeedsGeneratedAttributes();

            if (ShouldEmitNullablePublicOnlyAttribute() &&
                Compilation.CheckIfAttributeShouldBeEmbedded(EmbeddableAttributes.NullablePublicOnlyAttribute, diagnostics, Location.None))
            {
                needsAttributes |= EmbeddableAttributes.NullablePublicOnlyAttribute;
            }
            else if (needsAttributes == 0)
            {
                return;
            }

            CreateEmbeddedAttributeIfNeeded(
                ref _lazyEmbeddedAttribute,
                diagnostics,
                AttributeDescription.CodeAnalysisEmbeddedAttribute);

            if ((needsAttributes & EmbeddableAttributes.IsReadOnlyAttribute) != 0)
            {
                CreateEmbeddedAttributeIfNeeded(
                    ref _lazyIsReadOnlyAttribute,
                    diagnostics,
                    AttributeDescription.IsReadOnlyAttribute);
            }

            if ((needsAttributes & EmbeddableAttributes.IsByRefLikeAttribute) != 0)
            {
                CreateEmbeddedAttributeIfNeeded(
                    ref _lazyIsByRefLikeAttribute,
                    diagnostics,
                    AttributeDescription.IsByRefLikeAttribute);
            }

            if ((needsAttributes & EmbeddableAttributes.IsUnmanagedAttribute) != 0)
            {
                CreateEmbeddedAttributeIfNeeded(
                    ref _lazyIsUnmanagedAttribute,
                    diagnostics,
                    AttributeDescription.IsUnmanagedAttribute);
            }

            if ((needsAttributes & EmbeddableAttributes.NullableAttribute) != 0)
            {
                CreateEmbeddedNullableAttributeIfNeeded(
                    ref _lazyNullableAttribute,
                    diagnostics);
            }

            if ((needsAttributes & EmbeddableAttributes.NullableContextAttribute) != 0)
            {
                CreateEmbeddedNullableContextAttributeIfNeeded(
                    ref _lazyNullableContextAttribute,
                    diagnostics);
            }

            if ((needsAttributes & EmbeddableAttributes.NullablePublicOnlyAttribute) != 0)
            {
                CreateEmbeddedNullablePublicOnlyAttributeIfNeeded(
                    ref _lazyNullablePublicOnlyAttribute,
                    diagnostics);
            }
        }