Skip to content

Commit 864b61a

Browse files
authored
Merge pull request github#7766 from hvitved/csharp/extractor/type-param-constraints
C#: Make `TypeParameterConstraints` a `CachedEntity`
2 parents 28702df + f419521 commit 864b61a

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameter.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp.Syntax;
3-
using Semmle.Extraction.Entities;
4-
using System.Collections.Generic;
53
using System.IO;
64
using System.Linq;
75

@@ -21,34 +19,10 @@ private TypeParameter(Context cx, ITypeParameterSymbol init)
2119

2220
public override void Populate(TextWriter trapFile)
2321
{
24-
var constraints = new TypeParameterConstraints(Context);
25-
trapFile.type_parameter_constraints(constraints, this);
26-
27-
if (Symbol.HasReferenceTypeConstraint)
28-
trapFile.general_type_parameter_constraints(constraints, 1);
29-
30-
if (Symbol.HasValueTypeConstraint)
31-
trapFile.general_type_parameter_constraints(constraints, 2);
32-
33-
if (Symbol.HasConstructorConstraint)
34-
trapFile.general_type_parameter_constraints(constraints, 3);
35-
36-
if (Symbol.HasUnmanagedTypeConstraint)
37-
trapFile.general_type_parameter_constraints(constraints, 4);
38-
39-
if (Symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated)
40-
trapFile.general_type_parameter_constraints(constraints, 5);
41-
42-
foreach (var abase in Symbol.GetAnnotatedTypeConstraints())
43-
{
44-
var t = Create(Context, abase.Symbol);
45-
trapFile.specific_type_parameter_constraints(constraints, t.TypeRef);
46-
if (!abase.HasObliviousNullability())
47-
trapFile.specific_type_parameter_nullability(constraints, t.TypeRef, NullabilityEntity.Create(Context, Nullability.Create(abase)));
48-
}
49-
5022
trapFile.types(this, Kinds.TypeKind.TYPE_PARAMETER, Symbol.Name);
5123

24+
TypeParameterConstraints.Create(Context, this);
25+
5226
var parentNs = Namespace.Create(Context, Symbol.TypeParameterKind == TypeParameterKind.Method ? Context.Compilation.GlobalNamespace : Symbol.ContainingNamespace);
5327
trapFile.parent_namespace(this, parentNs);
5428

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
1+
using Microsoft.CodeAnalysis;
12
using System.IO;
23

34
namespace Semmle.Extraction.CSharp.Entities
45
{
5-
internal class TypeParameterConstraints : FreshEntity
6+
internal class TypeParameterConstraints : CachedEntity<ITypeParameterSymbol>
67
{
7-
public TypeParameterConstraints(Context cx)
8-
: base(cx) { }
8+
private readonly TypeParameter parent;
99

10-
protected override void Populate(TextWriter trapFile)
10+
public TypeParameterConstraints(Context cx, TypeParameter parent)
11+
: base(cx, parent.Symbol)
1112
{
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);
1262
}
1363
}
1464
}
65+

0 commit comments

Comments
 (0)