Skip to content

Commit bd2b3e7

Browse files
committed
C#: Use covariant return for context overrides
1 parent cfd8a87 commit bd2b3e7

38 files changed

+168
-176
lines changed

csharp/extractor/Semmle.Extraction.CIL/Context.Factories.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private IExtractedEntity CreateGenericHandle(IGenericContext gc, Handle handle)
114114
entity = new MethodSpecificationMethod(gc, (MethodSpecificationHandle)handle);
115115
break;
116116
case HandleKind.FieldDefinition:
117-
entity = new DefinitionField(gc.Cx, (FieldDefinitionHandle)handle);
117+
entity = new DefinitionField(gc.Context, (FieldDefinitionHandle)handle);
118118
break;
119119
case HandleKind.TypeReference:
120120
var tr = new TypeReferenceType(this, (TypeReferenceHandle)handle);
@@ -130,7 +130,7 @@ private IExtractedEntity CreateGenericHandle(IGenericContext gc, Handle handle)
130130
break;
131131
case HandleKind.StandaloneSignature:
132132
var signature = MdReader.GetStandaloneSignature((StandaloneSignatureHandle)handle);
133-
var method = signature.DecodeMethodSignature(gc.Cx.TypeSignatureDecoder, gc);
133+
var method = signature.DecodeMethodSignature(gc.Context.TypeSignatureDecoder, gc);
134134
entity = new FunctionPointerType(this, method);
135135
break;
136136
default:

csharp/extractor/Semmle.Extraction.CIL/EmptyContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ internal class EmptyContext : IGenericContext
99
{
1010
public EmptyContext(Context cx)
1111
{
12-
Cx = cx;
12+
Context = cx;
1313
}
1414

15-
public Context Cx { get; }
15+
public Context Context { get; }
1616

1717
public IEnumerable<Entities.Type> TypeParameters { get { yield break; } }
1818

csharp/extractor/Semmle.Extraction.CIL/Entities/ArrayType.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public override void WriteId(TextWriter trapFile, bool inContext)
4242

4343
public override string Name => elementType.Name + "[]";
4444

45-
public override Namespace ContainingNamespace => Cx.SystemNamespace;
45+
public override Namespace ContainingNamespace => Context.SystemNamespace;
4646

4747
public override Type? ContainingType => null;
4848

4949
public override int ThisTypeParameterCount => elementType.ThisTypeParameterCount;
5050

5151
public override CilTypeKind Kind => CilTypeKind.Array;
5252

53-
public override Type Construct(IEnumerable<Type> typeArguments) => Cx.Populate(new ArrayType(Cx, elementType.Construct(typeArguments)));
53+
public override Type Construct(IEnumerable<Type> typeArguments) => Context.Populate(new ArrayType(Context, elementType.Construct(typeArguments)));
5454

55-
public override Type SourceDeclaration => Cx.Populate(new ArrayType(Cx, elementType.SourceDeclaration));
55+
public override Type SourceDeclaration => Context.Populate(new ArrayType(Context, elementType.SourceDeclaration));
5656

5757
public override IEnumerable<IExtractionProduct> Contents
5858
{

csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override void WriteId(TextWriter trapFile)
3636
{
3737
trapFile.Write(FullName);
3838
trapFile.Write("#file:///");
39-
trapFile.Write(Cx.AssemblyPath.Replace("\\", "/"));
39+
trapFile.Write(Context.AssemblyPath.Replace("\\", "/"));
4040
trapFile.Write(";assembly");
4141
}
4242

@@ -56,41 +56,41 @@ public override IEnumerable<IExtractionProduct> Contents
5656
yield return file;
5757
yield return Tuples.assemblies(this, file, FullName, assemblyName.Name ?? string.Empty, assemblyName.Version?.ToString() ?? string.Empty);
5858

59-
if (Cx.Pdb != null)
59+
if (Context.Pdb != null)
6060
{
61-
foreach (var f in Cx.Pdb.SourceFiles)
61+
foreach (var f in Context.Pdb.SourceFiles)
6262
{
63-
yield return Cx.CreateSourceFile(f);
63+
yield return Context.CreateSourceFile(f);
6464
}
6565
}
6666

67-
foreach (var handle in Cx.MdReader.TypeDefinitions)
67+
foreach (var handle in Context.MdReader.TypeDefinitions)
6868
{
6969
IExtractionProduct? product = null;
7070
try
7171
{
72-
product = Cx.Create(handle);
72+
product = Context.Create(handle);
7373
}
7474
catch (InternalError e)
7575
{
76-
Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
76+
Context.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Context), e.StackTrace);
7777
}
7878

7979
// Limitation of C#: Cannot yield return inside a try-catch.
8080
if (product != null)
8181
yield return product;
8282
}
8383

84-
foreach (var handle in Cx.MdReader.MethodDefinitions)
84+
foreach (var handle in Context.MdReader.MethodDefinitions)
8585
{
8686
IExtractionProduct? product = null;
8787
try
8888
{
89-
product = Cx.Create(handle);
89+
product = Context.Create(handle);
9090
}
9191
catch (InternalError e)
9292
{
93-
Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
93+
Context.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Context), e.StackTrace);
9494
}
9595

9696
if (product != null)

csharp/extractor/Semmle.Extraction.CIL/Entities/Attribute.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override IEnumerable<IExtractionProduct> Contents
3232
{
3333
get
3434
{
35-
var constructor = (Method)Cx.Create(attrib.Constructor);
35+
var constructor = (Method)Context.Create(attrib.Constructor);
3636
yield return constructor;
3737

3838
yield return Tuples.cil_attribute(this, @object, constructor);
@@ -41,11 +41,11 @@ public override IEnumerable<IExtractionProduct> Contents
4141

4242
try
4343
{
44-
decoded = attrib.DecodeValue(new CustomAttributeDecoder(Cx));
44+
decoded = attrib.DecodeValue(new CustomAttributeDecoder(Context));
4545
}
4646
catch
4747
{
48-
Cx.Extractor.Logger.Log(Util.Logging.Severity.Info,
48+
Context.Extractor.Logger.Log(Util.Logging.Severity.Info,
4949
$"Attribute decoding is partial. Decoding attribute {constructor.DeclaringType.GetQualifiedName()} failed on {@object}.");
5050
yield break;
5151
}

csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IGenericContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Semmle.Extraction.CIL
88
/// </summary>
99
internal interface IGenericContext
1010
{
11-
Context Cx { get; }
11+
Context Context { get; }
1212

1313
/// <summary>
1414
/// The list of generic type parameters/arguments, including type parameters/arguments of

csharp/extractor/Semmle.Extraction.CIL/Entities/Base/LabelledEntity.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ namespace Semmle.Extraction.CIL
1010
/// </summary>
1111
internal abstract class LabelledEntity : Extraction.LabelledEntity, IExtractedEntity
1212
{
13-
// todo: with .NET 5 this can override the base context, and change the return type.
14-
public Context Cx => (Context)base.Context;
13+
public override Context Context => (Context)base.Context;
1514

1615
protected LabelledEntity(Context cx) : base(cx)
1716
{

csharp/extractor/Semmle.Extraction.CIL/Entities/Base/UnlabelledEntity.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ namespace Semmle.Extraction.CIL
99
/// </summary>
1010
internal abstract class UnlabelledEntity : Extraction.UnlabelledEntity, IExtractedEntity
1111
{
12-
// todo: with .NET 5 this can override the base context, and change the return type.
13-
public Context Cx => (Context)base.Context;
12+
public override Context Context => (Context)base.Context;
1413

1514
protected UnlabelledEntity(Context cx) : base(cx)
1615
{

csharp/extractor/Semmle.Extraction.CIL/Entities/DefinitionField.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal sealed class DefinitionField : Field
1414
public DefinitionField(Context cx, FieldDefinitionHandle handle) : base(cx)
1515
{
1616
this.handle = handle;
17-
fd = Cx.MdReader.GetFieldDefinition(handle);
17+
fd = Context.MdReader.GetFieldDefinition(handle);
1818
}
1919

2020
public override bool Equals(object? obj)
@@ -28,7 +28,7 @@ public override IEnumerable<IExtractionProduct> Contents
2828
{
2929
get
3030
{
31-
yield return Tuples.metadata_handle(this, Cx.Assembly, MetadataTokens.GetToken(handle));
31+
yield return Tuples.metadata_handle(this, Context.Assembly, MetadataTokens.GetToken(handle));
3232

3333
foreach (var c in base.Contents)
3434
yield return c;
@@ -48,16 +48,16 @@ public override IEnumerable<IExtractionProduct> Contents
4848
if (fd.Attributes.HasFlag(FieldAttributes.Assembly))
4949
yield return Tuples.cil_internal(this);
5050

51-
foreach (var c in Attribute.Populate(Cx, this, fd.GetCustomAttributes()))
51+
foreach (var c in Attribute.Populate(Context, this, fd.GetCustomAttributes()))
5252
yield return c;
5353
}
5454
}
5555

56-
public override string Name => Cx.GetString(fd.Name);
56+
public override string Name => Context.GetString(fd.Name);
5757

58-
public override Type DeclaringType => (Type)Cx.Create(fd.GetDeclaringType());
58+
public override Type DeclaringType => (Type)Context.Create(fd.GetDeclaringType());
5959

60-
public override Type Type => fd.DecodeSignature(Cx.TypeSignatureDecoder, DeclaringType);
60+
public override Type Type => fd.DecodeSignature(Context.TypeSignatureDecoder, DeclaringType);
6161

6262
public override IEnumerable<Type> TypeParameters => throw new NotImplementedException();
6363

csharp/extractor/Semmle.Extraction.CIL/Entities/DefinitionMethod.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ internal sealed class DefinitionMethod : Method
2525

2626
public DefinitionMethod(IGenericContext gc, MethodDefinitionHandle handle) : base(gc)
2727
{
28-
md = Cx.MdReader.GetMethodDefinition(handle);
28+
md = Context.MdReader.GetMethodDefinition(handle);
2929
this.gc = gc;
3030
this.handle = handle;
31-
name = Cx.GetString(md.Name);
31+
name = Context.GetString(md.Name);
3232

33-
declaringType = (Type)Cx.CreateGeneric(this, md.GetDeclaringType());
33+
declaringType = (Type)Context.CreateGeneric(this, md.GetDeclaringType());
3434

3535
signature = md.DecodeSignature(new SignatureDecoder(), this);
3636

37-
methodDebugInformation = Cx.GetMethodDebugInformation(handle);
37+
methodDebugInformation = Context.GetMethodDebugInformation(handle);
3838
}
3939

4040
public override bool Equals(object? obj)
@@ -48,7 +48,7 @@ public override bool Equals(object? obj)
4848

4949
public override Type DeclaringType => declaringType;
5050

51-
public override string Name => Cx.ShortName(md.Name);
51+
public override string Name => Context.ShortName(md.Name);
5252

5353
public override string NameLabel => name;
5454

@@ -67,14 +67,14 @@ public override IEnumerable<IExtractionProduct> Contents
6767
// depend on other type parameters (as a constraint).
6868
genericParams = new MethodTypeParameter[md.GetGenericParameters().Count];
6969
for (var i = 0; i < genericParams.Length; ++i)
70-
genericParams[i] = Cx.Populate(new MethodTypeParameter(this, this, i));
70+
genericParams[i] = Context.Populate(new MethodTypeParameter(this, this, i));
7171
for (var i = 0; i < genericParams.Length; ++i)
7272
genericParams[i].PopulateHandle(md.GetGenericParameters()[i]);
7373
foreach (var p in genericParams)
7474
yield return p;
7575
}
7676

77-
var typeSignature = md.DecodeSignature(Cx.TypeSignatureDecoder, this);
77+
var typeSignature = md.DecodeSignature(Context.TypeSignatureDecoder, this);
7878

7979
var parameters = GetParameterExtractionProducts(typeSignature.ParameterTypes).ToArray();
8080
Parameters = parameters.OfType<Parameter>().ToArray();
@@ -85,37 +85,37 @@ public override IEnumerable<IExtractionProduct> Contents
8585
foreach (var c in PopulateFlags)
8686
yield return c;
8787

88-
foreach (var p in md.GetParameters().Select(h => Cx.MdReader.GetParameter(h)).Where(p => p.SequenceNumber > 0))
88+
foreach (var p in md.GetParameters().Select(h => Context.MdReader.GetParameter(h)).Where(p => p.SequenceNumber > 0))
8989
{
9090
var pe = Parameters[IsStatic ? p.SequenceNumber - 1 : p.SequenceNumber];
9191
if (p.Attributes.HasFlag(ParameterAttributes.Out))
9292
yield return Tuples.cil_parameter_out(pe);
9393
if (p.Attributes.HasFlag(ParameterAttributes.In))
9494
yield return Tuples.cil_parameter_in(pe);
95-
Attribute.Populate(Cx, pe, p.GetCustomAttributes());
95+
Attribute.Populate(Context, pe, p.GetCustomAttributes());
9696
}
9797

98-
yield return Tuples.metadata_handle(this, Cx.Assembly, MetadataTokens.GetToken(handle));
98+
yield return Tuples.metadata_handle(this, Context.Assembly, MetadataTokens.GetToken(handle));
9999

100100
foreach (var m in GetMethodExtractionProducts(Name, declaringType, typeSignature.ReturnType))
101101
{
102102
yield return m;
103103
}
104104

105105
yield return Tuples.cil_method_source_declaration(this, this);
106-
yield return Tuples.cil_method_location(this, Cx.Assembly);
106+
yield return Tuples.cil_method_location(this, Context.Assembly);
107107

108108
if (HasBytecode)
109109
{
110110
Implementation = new MethodImplementation(this);
111111
yield return Implementation;
112112

113-
var body = Cx.PeReader.GetMethodBody(md.RelativeVirtualAddress);
113+
var body = Context.PeReader.GetMethodBody(md.RelativeVirtualAddress);
114114

115115
if (!body.LocalSignature.IsNil)
116116
{
117-
var locals = Cx.MdReader.GetStandaloneSignature(body.LocalSignature);
118-
var localVariableTypes = locals.DecodeLocalSignature(Cx.TypeSignatureDecoder, this);
117+
var locals = Context.MdReader.GetStandaloneSignature(body.LocalSignature);
118+
var localVariableTypes = locals.DecodeLocalSignature(Context.TypeSignatureDecoder, this);
119119

120120
this.locals = new LocalVariable[localVariableTypes.Length];
121121

@@ -125,13 +125,13 @@ public override IEnumerable<IExtractionProduct> Contents
125125
if (t is ByRefType brt)
126126
{
127127
t = brt.ElementType;
128-
this.locals[l] = Cx.Populate(new LocalVariable(Cx, Implementation, l, t));
128+
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
129129
yield return this.locals[l];
130130
yield return Tuples.cil_type_annotation(this.locals[l], TypeAnnotation.Ref);
131131
}
132132
else
133133
{
134-
this.locals[l] = Cx.Populate(new LocalVariable(Cx, Implementation, l, t));
134+
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
135135
yield return this.locals[l];
136136
}
137137
}
@@ -152,7 +152,7 @@ public override IEnumerable<IExtractionProduct> Contents
152152

153153
if (methodDebugInformation != null)
154154
{
155-
var sourceLocation = Cx.CreateSourceLocation(methodDebugInformation.Location);
155+
var sourceLocation = Context.CreateSourceLocation(methodDebugInformation.Location);
156156
yield return sourceLocation;
157157
yield return Tuples.cil_method_location(this, sourceLocation);
158158
}
@@ -191,7 +191,7 @@ public override IEnumerable<IExtractionProduct> Contents
191191
yield return Tuples.cil_newslot(this);
192192

193193
// Populate attributes
194-
Attribute.Populate(Cx, this, md.GetCustomAttributes());
194+
Attribute.Populate(Context, this, md.GetCustomAttributes());
195195
}
196196
}
197197

@@ -210,7 +210,7 @@ private IEnumerable<IExtractionProduct> Decode(byte[]? ilbytes, Dictionary<int,
210210
nextSequencePoint = methodDebugInformation.SequencePoints.GetEnumerator();
211211
if (nextSequencePoint.MoveNext())
212212
{
213-
instructionLocation = Cx.CreateSourceLocation(nextSequencePoint.Current.Location);
213+
instructionLocation = Context.CreateSourceLocation(nextSequencePoint.Current.Location);
214214
yield return instructionLocation;
215215
}
216216
else
@@ -222,12 +222,12 @@ private IEnumerable<IExtractionProduct> Decode(byte[]? ilbytes, Dictionary<int,
222222
var child = 0;
223223
for (var offset = 0; offset < (ilbytes?.Length ?? 0);)
224224
{
225-
var instruction = new Instruction(Cx, this, ilbytes!, offset, child++);
225+
var instruction = new Instruction(Context, this, ilbytes!, offset, child++);
226226
yield return instruction;
227227

228228
if (nextSequencePoint != null && offset >= nextSequencePoint.Current.Offset)
229229
{
230-
instructionLocation = Cx.CreateSourceLocation(nextSequencePoint.Current.Location);
230+
instructionLocation = Context.CreateSourceLocation(nextSequencePoint.Current.Location);
231231
yield return instructionLocation;
232232
if (!nextSequencePoint.MoveNext())
233233
nextSequencePoint = null;
@@ -257,7 +257,7 @@ public IEnumerable<Instruction> DebugInstructions
257257
{
258258
if (md.ImplAttributes == MethodImplAttributes.IL && md.RelativeVirtualAddress != 0)
259259
{
260-
var body = Cx.PeReader.GetMethodBody(md.RelativeVirtualAddress);
260+
var body = Context.PeReader.GetMethodBody(md.RelativeVirtualAddress);
261261

262262
var ilbytes = body.GetILBytes();
263263

@@ -267,7 +267,7 @@ public IEnumerable<Instruction> DebugInstructions
267267
Instruction decoded;
268268
try
269269
{
270-
decoded = new Instruction(Cx, this, ilbytes!, offset, child++);
270+
decoded = new Instruction(Context, this, ilbytes!, offset, child++);
271271
offset += decoded.Width;
272272
}
273273
catch // lgtm[cs/catch-of-all-exceptions]

0 commit comments

Comments
 (0)