|
| 1 | +using System.IO; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Semmle.Extraction.CSharp.Util; |
| 4 | +using Semmle.Extraction.Kinds; |
| 5 | + |
| 6 | +namespace Semmle.Extraction.CSharp.Entities |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Represents the autogenerated backing field `field` for a property. |
| 10 | + /// It is only created for properties that use the `field` keyword in their getter or setter, and |
| 11 | + /// is not created for auto-properties. |
| 12 | + /// </summary> |
| 13 | + internal class PropertyField : Field |
| 14 | + { |
| 15 | + protected PropertyField(Context cx, IFieldSymbol init) |
| 16 | + : base(cx, init) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + public static new PropertyField Create(Context cx, IFieldSymbol field) => PropertyFieldFactory.Instance.CreateEntity(cx, (field, field.AssociatedSymbol), field); |
| 21 | + |
| 22 | + public override bool NeedsPopulation => true; |
| 23 | + |
| 24 | + public override void Populate(TextWriter trapFile) |
| 25 | + { |
| 26 | + PopulateNullability(trapFile, Symbol.GetAnnotatedType()); |
| 27 | + |
| 28 | + var unboundFieldKey = PropertyField.Create(Context, Symbol.OriginalDefinition); |
| 29 | + var name = Symbol.AssociatedSymbol is not null ? $"{Symbol.AssociatedSymbol.GetName()}.field" : Symbol.Name; |
| 30 | + trapFile.fields(this, VariableKind.None, name, ContainingType!, Type.TypeRef, unboundFieldKey); |
| 31 | + trapFile.compiler_generated(this); |
| 32 | + |
| 33 | + PopulateModifiers(trapFile); |
| 34 | + |
| 35 | + if (Context.OnlyScaffold) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (Context.ExtractLocation(Symbol)) |
| 41 | + { |
| 42 | + WriteLocationsToTrap(trapFile.field_location, this, Locations); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private class PropertyFieldFactory : CachedEntityFactory<IFieldSymbol, PropertyField> |
| 47 | + { |
| 48 | + public static PropertyFieldFactory Instance { get; } = new PropertyFieldFactory(); |
| 49 | + |
| 50 | + public override PropertyField Create(Context cx, IFieldSymbol init) => new PropertyField(cx, init); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments