Skip to content

Commit 6e6cd05

Browse files
authored
Merge pull request #4758 from tamasvajk/feature/cil-structure-change
C#: Cleanup CIL extraction structure
2 parents 0cc324b + 9ab930f commit 6e6cd05

Some content is hidden

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

55 files changed

+2385
-2342
lines changed

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

File renamed without changes.

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

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;
54
using System.Reflection.Metadata;
@@ -111,74 +110,9 @@ public Entities.Type ErrorType
111110
/// </summary>
112111
/// <param name="handle">The handle of the method.</param>
113112
/// <returns>The debugging information, or null if the information could not be located.</returns>
114-
public PDB.IMethod? GetMethodDebugInformation(MethodDefinitionHandle handle)
113+
public PDB.Method? GetMethodDebugInformation(MethodDefinitionHandle handle)
115114
{
116115
return Pdb?.GetMethod(handle.ToDebugInformationHandle());
117116
}
118117
}
119-
120-
/// <summary>
121-
/// When we decode a type/method signature, we need access to
122-
/// generic parameters.
123-
/// </summary>
124-
public abstract class GenericContext
125-
{
126-
public Context Cx { get; }
127-
128-
protected GenericContext(Context cx)
129-
{
130-
this.Cx = cx;
131-
}
132-
133-
/// <summary>
134-
/// The list of generic type parameters, including type parameters of
135-
/// containing types.
136-
/// </summary>
137-
public abstract IEnumerable<Entities.Type> TypeParameters { get; }
138-
139-
/// <summary>
140-
/// The list of generic method parameters.
141-
/// </summary>
142-
public abstract IEnumerable<Entities.Type> MethodParameters { get; }
143-
144-
/// <summary>
145-
/// Gets the `p`th type parameter.
146-
/// </summary>
147-
/// <param name="p">The index of the parameter.</param>
148-
/// <returns>
149-
/// For constructed types, the supplied type.
150-
/// For unbound types, the type parameter.
151-
/// </returns>
152-
public Entities.Type GetGenericTypeParameter(int p)
153-
{
154-
return TypeParameters.ElementAt(p);
155-
}
156-
157-
/// <summary>
158-
/// Gets the `p`th method type parameter.
159-
/// </summary>
160-
/// <param name="p">The index of the parameter.</param>
161-
/// <returns>
162-
/// For constructed types, the supplied type.
163-
/// For unbound types, the type parameter.
164-
/// </returns>
165-
public Entities.Type GetGenericMethodParameter(int p)
166-
{
167-
return MethodParameters.ElementAt(p);
168-
}
169-
}
170-
171-
/// <summary>
172-
/// A generic context which does not contain any type parameters.
173-
/// </summary>
174-
public class EmptyContext : GenericContext
175-
{
176-
public EmptyContext(Context cx) : base(cx)
177-
{
178-
}
179-
180-
public override IEnumerable<Entities.Type> TypeParameters { get { yield break; } }
181-
182-
public override IEnumerable<Entities.Type> MethodParameters { get { yield break; } }
183-
}
184118
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
3+
namespace Semmle.Extraction.CIL
4+
{
5+
/// <summary>
6+
/// A generic context which does not contain any type parameters.
7+
/// </summary>
8+
public class EmptyContext : GenericContext
9+
{
10+
public EmptyContext(Context cx) : base(cx)
11+
{
12+
}
13+
14+
public override IEnumerable<Entities.Type> TypeParameters { get { yield break; } }
15+
16+
public override IEnumerable<Entities.Type> MethodParameters { get { yield break; } }
17+
}
18+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace Semmle.Extraction.CIL.Entities
6+
{
7+
/// <summary>
8+
/// An array type.
9+
/// </summary>
10+
internal sealed class ArrayType : Type
11+
{
12+
private readonly Type elementType;
13+
private readonly int rank;
14+
15+
public ArrayType(Context cx, Type elementType, int rank) : base(cx)
16+
{
17+
this.rank = rank;
18+
this.elementType = elementType;
19+
}
20+
21+
public ArrayType(Context cx, Type elementType) : this(cx, elementType, 1)
22+
{
23+
}
24+
25+
public override bool Equals(object? obj)
26+
{
27+
return obj is ArrayType array && elementType.Equals(array.elementType) && rank == array.rank;
28+
}
29+
30+
public override int GetHashCode()
31+
{
32+
return elementType.GetHashCode() * 5 + rank;
33+
}
34+
35+
public override void WriteId(TextWriter trapFile, bool inContext)
36+
{
37+
elementType.GetId(trapFile, inContext);
38+
trapFile.Write('[');
39+
for (var i = 1; i < rank; ++i)
40+
trapFile.Write(',');
41+
trapFile.Write(']');
42+
}
43+
44+
public override string Name => elementType.Name + "[]";
45+
46+
public override Namespace Namespace => Cx.SystemNamespace;
47+
48+
public override Type? ContainingType => null;
49+
50+
public override int ThisTypeParameters => elementType.ThisTypeParameters;
51+
52+
public override CilTypeKind Kind => CilTypeKind.Array;
53+
54+
public override Type Construct(IEnumerable<Type> typeArguments) => Cx.Populate(new ArrayType(Cx, elementType.Construct(typeArguments)));
55+
56+
public override Type SourceDeclaration => Cx.Populate(new ArrayType(Cx, elementType.SourceDeclaration));
57+
58+
public override IEnumerable<IExtractionProduct> Contents
59+
{
60+
get
61+
{
62+
foreach (var c in base.Contents)
63+
yield return c;
64+
65+
yield return Tuples.cil_array_type(this, elementType, rank);
66+
}
67+
}
68+
69+
public override void WriteAssemblyPrefix(TextWriter trapFile) => elementType.WriteAssemblyPrefix(trapFile);
70+
71+
public override IEnumerable<Type> GenericArguments => elementType.GenericArguments;
72+
73+
public override IEnumerable<Type> TypeParameters => elementType.TypeParameters;
74+
75+
public override IEnumerable<Type> MethodParameters => throw new NotImplementedException();
76+
}
77+
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@
99

1010
namespace Semmle.Extraction.CIL.Entities
1111
{
12-
public interface ILocation : IEntity
13-
{
14-
}
15-
16-
internal interface IAssembly : ILocation
17-
{
18-
}
19-
2012
/// <summary>
2113
/// An assembly to extract.
2214
/// </summary>
23-
public class Assembly : LabelledEntity, IAssembly
15+
public class Assembly : LabelledEntity, ILocation
2416
{
2517
private readonly File file;
2618
private readonly AssemblyName assemblyName;

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44

55
namespace Semmle.Extraction.CIL.Entities
66
{
7-
/// <summary>
8-
/// A CIL attribute.
9-
/// </summary>
10-
internal interface IAttribute : IExtractedEntity
11-
{
12-
}
13-
147
/// <summary>
158
/// Entity representing a CIL attribute.
169
/// </summary>
17-
internal sealed class Attribute : UnlabelledEntity, IAttribute
10+
internal sealed class Attribute : UnlabelledEntity
1811
{
1912
private readonly CustomAttributeHandle handle;
2013
private readonly CustomAttribute attrib;
@@ -79,33 +72,4 @@ public static IEnumerable<IExtractionProduct> Populate(Context cx, IEntity @obje
7972
}
8073
}
8174
}
82-
83-
/// <summary>
84-
/// Helper class to decode the attribute structure.
85-
/// Note that there are some unhandled cases that should be fixed in due course.
86-
/// </summary>
87-
internal class CustomAttributeDecoder : ICustomAttributeTypeProvider<Type>
88-
{
89-
private readonly Context cx;
90-
public CustomAttributeDecoder(Context cx) { this.cx = cx; }
91-
92-
public Type GetPrimitiveType(PrimitiveTypeCode typeCode) => cx.Create(typeCode);
93-
94-
public Type GetSystemType() => throw new NotImplementedException();
95-
96-
public Type GetSZArrayType(Type elementType) =>
97-
cx.Populate(new ArrayType(cx, elementType));
98-
99-
public Type GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) =>
100-
(Type)cx.Create(handle);
101-
102-
public Type GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) =>
103-
(Type)cx.Create(handle);
104-
105-
public Type GetTypeFromSerializedName(string name) => throw new NotImplementedException();
106-
107-
public PrimitiveTypeCode GetUnderlyingEnumType(Type type) => throw new NotImplementedException();
108-
109-
public bool IsSystemType(Type type) => type is PrimitiveType; // ??
110-
}
11175
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Semmle.Extraction.CIL.Entities
2+
{
3+
/// <summary>
4+
/// The CIL database type-kind.
5+
/// </summary>
6+
public enum CilTypeKind
7+
{
8+
ValueOrRefType,
9+
TypeParameter,
10+
Array,
11+
Pointer
12+
}
13+
}

0 commit comments

Comments
 (0)