| File: Symbols\Synthesized\Records\SynthesizedPrimaryConstructorParameterBackingFieldSymbol.cs | Web Access |
| Project: src\roslyn\src\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj (Microsoft.CodeAnalysis.CSharp) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Immutable; using System.Linq; using Microsoft.CodeAnalysis.Collections; using Microsoft.CodeAnalysis.CSharp.Emit; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.PooledObjects; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp.Symbols { /// <summary> /// Represents a compiler generated backing field for a primary constructor parameter. /// </summary> internal sealed class SynthesizedPrimaryConstructorParameterBackingFieldSymbol : SynthesizedBackingFieldSymbolBase { public readonly ParameterSymbol ParameterSymbol; public SynthesizedPrimaryConstructorParameterBackingFieldSymbol( ParameterSymbol parameterSymbol, string name, bool isReadOnly) : base(name, isReadOnly, isStatic: false) { ParameterSymbol = parameterSymbol; } internal override bool HasInitializer => true; protected override IAttributeTargetSymbol AttributeOwner => this; internal override Location ErrorLocation => ParameterSymbol.TryGetFirstLocation() ?? NoLocation.Singleton; protected override OneOrMany<SyntaxList<AttributeListSyntax>> GetAttributeDeclarations() => OneOrMany<SyntaxList<AttributeListSyntax>>.Empty; public override Symbol? AssociatedSymbol => null; public override ImmutableArray<Location> Locations => ParameterSymbol.Locations; public override RefKind RefKind => RefKind.None; internal sealed override CallerUnsafeMode GetCallerUnsafeMode(ConsList<FieldSymbol> fieldsBeingBound) => CallerUnsafeMode.None; public override ImmutableArray<CustomModifier> RefCustomModifiers => ImmutableArray<CustomModifier>.Empty; internal override TypeWithAnnotations GetFieldType(ConsList<FieldSymbol> fieldsBeingBound) => ParameterSymbol.TypeWithAnnotations; // Some implementations (like in SourceFieldSymbolWithSyntaxReference) // try to detect this fact from syntax. It looks like the motivation // is to avoid some kind of circularity. // No tests failed when that behavior got disabled. Probably the circularity is no longer // possible. Also, while figuring out whether parameter is getting captured, we are likely // to bind arbitrary type references. It feels like the additional complexity (detecting // the fact from syntax) is not warranted. internal override bool HasPointerType => base.HasPointerType; public override Symbol ContainingSymbol => ParameterSymbol.ContainingSymbol.ContainingSymbol; public override NamedTypeSymbol ContainingType => ParameterSymbol.ContainingSymbol.ContainingType; internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes) { foreach (CSharpAttributeData attr in ParameterSymbol.GetAttributes()) { if (attr.AttributeClass is { HasCompilerLoweringPreserveAttribute: true } attributeType && (attributeType.GetAttributeUsageInfo().ValidTargets & System.AttributeTargets.Field) != 0) { AddSynthesizedAttribute(ref attributes, attr); } } base.AddSynthesizedAttributes(moduleBuilder, ref attributes); } } }