|
| 1 | +using Microsoft.CodeAnalysis; |
1 | 2 | using System.IO;
|
2 | 3 |
|
3 | 4 | namespace Semmle.Extraction.CSharp.Entities
|
4 | 5 | {
|
5 |
| - internal class TypeParameterConstraints : FreshEntity |
| 6 | + internal class TypeParameterConstraints : CachedEntity<ITypeParameterSymbol> |
6 | 7 | {
|
7 |
| - public TypeParameterConstraints(Context cx) |
8 |
| - : base(cx) { } |
| 8 | + private readonly TypeParameter parent; |
9 | 9 |
|
10 |
| - protected override void Populate(TextWriter trapFile) |
| 10 | + public TypeParameterConstraints(Context cx, TypeParameter parent) |
| 11 | + : base(cx, parent.Symbol) |
11 | 12 | {
|
| 13 | + this.parent = parent; |
| 14 | + } |
| 15 | + |
| 16 | + public override void WriteId(EscapingTextWriter trapFile) |
| 17 | + { |
| 18 | + trapFile.WriteSubId(parent); |
| 19 | + trapFile.Write(";typeparameterconstraints"); |
| 20 | + } |
| 21 | + |
| 22 | + public override bool NeedsPopulation => true; |
| 23 | + |
| 24 | + public override void Populate(TextWriter trapFile) |
| 25 | + { |
| 26 | + trapFile.type_parameter_constraints(this, parent); |
| 27 | + |
| 28 | + if (Symbol.HasReferenceTypeConstraint) |
| 29 | + trapFile.general_type_parameter_constraints(this, 1); |
| 30 | + |
| 31 | + if (Symbol.HasValueTypeConstraint) |
| 32 | + trapFile.general_type_parameter_constraints(this, 2); |
| 33 | + |
| 34 | + if (Symbol.HasConstructorConstraint) |
| 35 | + trapFile.general_type_parameter_constraints(this, 3); |
| 36 | + |
| 37 | + if (Symbol.HasUnmanagedTypeConstraint) |
| 38 | + trapFile.general_type_parameter_constraints(this, 4); |
| 39 | + |
| 40 | + if (Symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated) |
| 41 | + trapFile.general_type_parameter_constraints(this, 5); |
| 42 | + |
| 43 | + foreach (var abase in Symbol.GetAnnotatedTypeConstraints()) |
| 44 | + { |
| 45 | + var t = Type.Create(Context, abase.Symbol); |
| 46 | + trapFile.specific_type_parameter_constraints(this, t.TypeRef); |
| 47 | + if (!abase.HasObliviousNullability()) |
| 48 | + trapFile.specific_type_parameter_nullability(this, t.TypeRef, NullabilityEntity.Create(Context, Nullability.Create(abase))); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public override Location? ReportingLocation => null; |
| 53 | + |
| 54 | + public static TypeParameterConstraints Create(Context cx, TypeParameter p) => |
| 55 | + TypeParameterConstraintsFactory.Instance.CreateEntity(cx, (typeof(TypeParameterConstraints), p), p); |
| 56 | + |
| 57 | + private class TypeParameterConstraintsFactory : CachedEntityFactory<TypeParameter, TypeParameterConstraints> |
| 58 | + { |
| 59 | + public static TypeParameterConstraintsFactory Instance { get; } = new TypeParameterConstraintsFactory(); |
| 60 | + |
| 61 | + public override TypeParameterConstraints Create(Context cx, TypeParameter init) => new(cx, init); |
12 | 62 | }
|
13 | 63 | }
|
14 | 64 | }
|
| 65 | + |
0 commit comments