Skip to content

Commit f3814c6

Browse files
authored
Merge pull request github#5144 from tamasvajk/feature/refactor-2
C# Share entity base classes between CIL and source extraction
2 parents 8e7a823 + a75b952 commit f3814c6

File tree

155 files changed

+1107
-1163
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+1107
-1163
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ public PrimitiveType Create(PrimitiveTypeCode code)
9595
/// <param name="h">The handle of the entity.</param>
9696
/// <param name="genericContext">The generic context.</param>
9797
/// <returns></returns>
98-
public IExtractedEntity CreateGeneric(GenericContext genericContext, Handle h) => genericHandleFactory[genericContext, h];
98+
public IExtractedEntity CreateGeneric(IGenericContext genericContext, Handle h) => genericHandleFactory[genericContext, h];
9999

100-
private readonly GenericContext defaultGenericContext;
100+
private readonly IGenericContext defaultGenericContext;
101101

102-
private IExtractedEntity CreateGenericHandle(GenericContext gc, Handle handle)
102+
private IExtractedEntity CreateGenericHandle(IGenericContext gc, Handle handle)
103103
{
104104
IExtractedEntity entity;
105105
switch (handle.Kind)
@@ -136,7 +136,7 @@ private IExtractedEntity CreateGenericHandle(GenericContext gc, Handle handle)
136136
return entity;
137137
}
138138

139-
private IExtractedEntity Create(GenericContext gc, MemberReferenceHandle handle)
139+
private IExtractedEntity Create(IGenericContext gc, MemberReferenceHandle handle)
140140
{
141141
var mr = MdReader.GetMemberReference(handle);
142142
switch (mr.GetKind())
@@ -228,7 +228,7 @@ private Namespace CreateNamespace(NamespaceDefinitionHandle handle)
228228

229229
#endregion
230230

231-
private readonly CachedFunction<GenericContext, Handle, IExtractedEntity> genericHandleFactory;
231+
private readonly CachedFunction<IGenericContext, Handle, IExtractedEntity> genericHandleFactory;
232232

233233
/// <summary>
234234
/// Gets the short name of a member, without the preceding interface qualifier.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Context(Extraction.Context cx, string assemblyPath, bool extractPdbs)
3737

3838
globalNamespace = new Lazy<Entities.Namespace>(() => Populate(new Entities.Namespace(this, "", null)));
3939
systemNamespace = new Lazy<Entities.Namespace>(() => Populate(new Entities.Namespace(this, "System")));
40-
genericHandleFactory = new CachedFunction<GenericContext, Handle, IExtractedEntity>(CreateGenericHandle);
40+
genericHandleFactory = new CachedFunction<IGenericContext, Handle, IExtractedEntity>(CreateGenericHandle);
4141
namespaceFactory = new CachedFunction<StringHandle, Entities.Namespace>(n => CreateNamespace(MdReader.GetString(n)));
4242
namespaceDefinitionFactory = new CachedFunction<NamespaceDefinitionHandle, Entities.Namespace>(CreateNamespace);
4343
sourceFiles = new CachedFunction<PDB.ISourceFile, Entities.PdbSourceFile>(path => new Entities.PdbSourceFile(this, path));

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ namespace Semmle.Extraction.CIL
55
/// <summary>
66
/// A generic context which does not contain any type parameters.
77
/// </summary>
8-
public class EmptyContext : GenericContext
8+
public class EmptyContext : IGenericContext
99
{
10-
public EmptyContext(Context cx) : base(cx)
10+
public EmptyContext(Context cx)
1111
{
12+
Cx = cx;
1213
}
1314

14-
public override IEnumerable<Entities.Type> TypeParameters { get { yield break; } }
15+
public Context Cx { get; }
16+
17+
public IEnumerable<Entities.Type> TypeParameters { get { yield break; } }
18+
19+
public IEnumerable<Entities.Type> MethodParameters { get { yield break; } }
1520

16-
public override IEnumerable<Entities.Type> MethodParameters { get { yield break; } }
1721
}
1822
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public override void WriteId(TextWriter trapFile)
4040
trapFile.Write(FullName);
4141
trapFile.Write("#file:///");
4242
trapFile.Write(Cx.AssemblyPath.Replace("\\", "/"));
43+
trapFile.Write(";assembly");
4344
}
4445

4546
public override bool Equals(object? obj)
@@ -49,8 +50,6 @@ public override bool Equals(object? obj)
4950

5051
public override int GetHashCode() => 7 * file.GetHashCode();
5152

52-
public override string IdSuffix => ";assembly";
53-
5453
private string FullName => assemblyName.GetPublicKey() is null ? assemblyName.FullName + ", PublicKeyToken=null" : assemblyName.FullName;
5554

5655
public override IEnumerable<IExtractionProduct> Contents

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ internal sealed class Attribute : UnlabelledEntity
1212
{
1313
private readonly CustomAttributeHandle handle;
1414
private readonly CustomAttribute attrib;
15-
private readonly IEntity @object;
15+
private readonly IExtractedEntity @object;
1616

17-
public Attribute(Context cx, IEntity @object, CustomAttributeHandle handle) : base(cx)
17+
public Attribute(Context cx, IExtractedEntity @object, CustomAttributeHandle handle) : base(cx)
1818
{
1919
attrib = cx.MdReader.GetCustomAttribute(handle);
2020
this.handle = handle;
@@ -80,7 +80,7 @@ private static string GetStringValue(Type type, object? value)
8080
return value?.ToString() ?? "null";
8181
}
8282

83-
public static IEnumerable<IExtractionProduct> Populate(Context cx, IEntity @object, CustomAttributeHandleCollection attributes)
83+
public static IEnumerable<IExtractionProduct> Populate(Context cx, IExtractedEntity @object, CustomAttributeHandleCollection attributes)
8484
{
8585
foreach (var attrib in attributes)
8686
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
3+
namespace Semmle.Extraction.CIL
4+
{
5+
/// <summary>
6+
/// A CIL entity which has been extracted.
7+
/// </summary>
8+
public interface IExtractedEntity : IExtractionProduct, IEntity
9+
{
10+
/// <summary>
11+
/// The contents of the entity.
12+
/// </summary>
13+
14+
IEnumerable<IExtractionProduct> Contents { get; }
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Semmle.Extraction.CIL
2+
{
3+
/// <summary>
4+
/// Something that is extracted from an entity.
5+
/// </summary>
6+
///
7+
/// <remarks>
8+
/// The extraction algorithm proceeds as follows:
9+
/// - Construct entity
10+
/// - Call Extract()
11+
/// - IExtractedEntity check if already extracted
12+
/// - Enumerate Contents to produce more extraction products
13+
/// - Extract these until there is nothing left to extract
14+
/// </remarks>
15+
public interface IExtractionProduct
16+
{
17+
/// <summary>
18+
/// Perform further extraction/population of this item as necessary.
19+
/// </summary>
20+
///
21+
/// <param name="cx">The extraction context.</param>
22+
void Extract(Context cx);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
3+
namespace Semmle.Extraction.CIL
4+
{
5+
/// <summary>
6+
/// When we decode a type/method signature, we need access to
7+
/// generic parameters.
8+
/// </summary>
9+
public interface IGenericContext
10+
{
11+
Context Cx { get; }
12+
13+
/// <summary>
14+
/// The list of generic type parameters/arguments, including type parameters/arguments of
15+
/// containing types.
16+
/// </summary>
17+
IEnumerable<Entities.Type> TypeParameters { get; }
18+
19+
/// <summary>
20+
/// The list of generic method parameters/arguments.
21+
/// </summary>
22+
IEnumerable<Entities.Type> MethodParameters { get; }
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace Semmle.Extraction.CIL
6+
{
7+
/// <summary>
8+
/// An entity that needs to be populated during extraction.
9+
/// This assigns a key and optionally extracts its contents.
10+
/// </summary>
11+
public abstract class LabelledEntity : Extraction.LabelledEntity, IExtractedEntity
12+
{
13+
// todo: with .NET 5 this can override the base context, and change the return type.
14+
public Context Cx { get; }
15+
16+
protected LabelledEntity(Context cx) : base(cx.Cx)
17+
{
18+
this.Cx = cx;
19+
}
20+
21+
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();
22+
23+
public void Extract(Context cx2)
24+
{
25+
cx2.Populate(this);
26+
}
27+
28+
public override string ToString()
29+
{
30+
using var writer = new StringWriter();
31+
WriteQuotedId(writer);
32+
return writer.ToString();
33+
}
34+
35+
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
36+
37+
public abstract IEnumerable<IExtractionProduct> Contents { get; }
38+
}
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Semmle.Extraction.CIL
2+
{
3+
/// <summary>
4+
/// A tuple that is an extraction product.
5+
/// </summary>
6+
internal class Tuple : IExtractionProduct
7+
{
8+
private readonly Extraction.Tuple tuple;
9+
10+
public Tuple(string name, params object[] args)
11+
{
12+
tuple = new Extraction.Tuple(name, args);
13+
}
14+
15+
public void Extract(Context cx)
16+
{
17+
cx.Cx.Emit(tuple);
18+
}
19+
20+
public override string ToString() => tuple.ToString();
21+
}
22+
}

0 commit comments

Comments
 (0)