Skip to content

Commit 151379e

Browse files
committed
C#: Cleanup CIL extraction 'Type' classes
1 parent b649ccd commit 151379e

File tree

13 files changed

+101
-165
lines changed

13 files changed

+101
-165
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,26 @@ public override bool Equals(object? obj)
2727
return obj is ArrayType array && elementType.Equals(array.elementType) && rank == array.rank;
2828
}
2929

30-
public override int GetHashCode()
31-
{
32-
return elementType.GetHashCode() * 5 + rank;
33-
}
30+
public override int GetHashCode() => HashCode.Combine(elementType, rank);
3431

3532
public override void WriteId(TextWriter trapFile, bool inContext)
3633
{
3734
elementType.GetId(trapFile, inContext);
3835
trapFile.Write('[');
39-
for (var i = 1; i < rank; ++i)
40-
trapFile.Write(',');
36+
if (rank > 1)
37+
{
38+
trapFile.Write(new string(',', rank - 1));
39+
}
4140
trapFile.Write(']');
4241
}
4342

4443
public override string Name => elementType.Name + "[]";
4544

46-
public override Namespace Namespace => Cx.SystemNamespace;
45+
public override Namespace ContainingNamespace => Cx.SystemNamespace;
4746

4847
public override Type? ContainingType => null;
4948

50-
public override int ThisTypeParameters => elementType.ThisTypeParameters;
49+
public override int ThisTypeParameterCount => elementType.ThisTypeParameterCount;
5150

5251
public override CilTypeKind Kind => CilTypeKind.Array;
5352

@@ -71,7 +70,5 @@ public override IEnumerable<IExtractionProduct> Contents
7170
public override IEnumerable<Type> GenericArguments => elementType.GenericArguments;
7271

7372
public override IEnumerable<Type> TypeParameters => elementType.TypeParameters;
74-
75-
public override IEnumerable<Type> MethodParameters => throw new NotImplementedException();
7673
}
7774
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override IEnumerable<IExtractionProduct> Contents
4343
{
4444
decoded = attrib.DecodeValue(new CustomAttributeDecoder(Cx));
4545
}
46-
catch (NotImplementedException)
46+
catch (Exception exc)
4747
{
4848
// Attribute decoding is only partial at this stage.
4949
yield break;

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public sealed class ConstructedType : Type
1717
// Either null or notEmpty
1818
private readonly Type[]? thisTypeArguments;
1919

20+
private readonly Type? containingType;
21+
2022
public override IEnumerable<Type> ThisTypeArguments => thisTypeArguments.EnumerateNull();
2123

2224
public override IEnumerable<Type> ThisGenericArguments => thisTypeArguments.EnumerateNull();
@@ -29,7 +31,7 @@ public override IEnumerable<IExtractionProduct> Contents
2931
yield return c;
3032

3133
var i = 0;
32-
foreach (var type in ThisGenericArguments)
34+
foreach (var type in ThisTypeArguments)
3335
{
3436
yield return type;
3537
yield return Tuples.cil_type_argument(this, i++, type);
@@ -42,11 +44,11 @@ public override IEnumerable<IExtractionProduct> Contents
4244
public ConstructedType(Context cx, Type unboundType, IEnumerable<Type> typeArguments) : base(cx)
4345
{
4446
var suppliedArgs = typeArguments.Count();
45-
if (suppliedArgs != unboundType.TotalTypeParametersCheck)
47+
if (suppliedArgs != unboundType.TotalTypeParametersCount)
4648
throw new InternalError("Unexpected number of type arguments in ConstructedType");
4749

4850
unboundGenericType = unboundType;
49-
var thisParams = unboundType.ThisTypeParameters;
51+
var thisParams = unboundType.ThisTypeParameterCount;
5052

5153
if (typeArguments.Count() == thisParams)
5254
{
@@ -88,14 +90,13 @@ public override int GetHashCode()
8890
return h;
8991
}
9092

91-
private readonly Type? containingType;
9293
public override Type? ContainingType => containingType;
9394

9495
public override string Name => unboundGenericType.Name;
9596

96-
public override Namespace Namespace => unboundGenericType.Namespace!;
97+
public override Namespace ContainingNamespace => unboundGenericType.ContainingNamespace!;
9798

98-
public override int ThisTypeParameters => thisTypeArguments == null ? 0 : thisTypeArguments.Length;
99+
public override int ThisTypeParameterCount => thisTypeArguments == null ? 0 : thisTypeArguments.Length;
99100

100101
public override CilTypeKind Kind => unboundGenericType.Kind;
101102

@@ -115,9 +116,9 @@ public override void WriteId(TextWriter trapFile, bool inContext)
115116
{
116117
WriteAssemblyPrefix(trapFile);
117118

118-
if (!Namespace.IsGlobalNamespace)
119+
if (!ContainingNamespace.IsGlobalNamespace)
119120
{
120-
Namespace.WriteId(trapFile);
121+
ContainingNamespace.WriteId(trapFile);
121122
trapFile.Write('.');
122123
}
123124
}
@@ -139,7 +140,5 @@ public override void WriteId(TextWriter trapFile, bool inContext)
139140
public override void WriteAssemblyPrefix(TextWriter trapFile) => unboundGenericType.WriteAssemblyPrefix(trapFile);
140141

141142
public override IEnumerable<Type> TypeParameters => GenericArguments;
142-
143-
public override IEnumerable<Type> MethodParameters => throw new NotImplementedException();
144143
}
145144
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ public ErrorType(Context cx) : base(cx)
1616

1717
public override string Name => "!error";
1818

19-
public override Namespace Namespace => Cx.GlobalNamespace;
19+
public override Namespace ContainingNamespace => Cx.GlobalNamespace;
2020

2121
public override Type? ContainingType => null;
2222

23-
public override int ThisTypeParameters => 0;
23+
public override int ThisTypeParameterCount => 0;
2424

2525
public override void WriteAssemblyPrefix(TextWriter trapFile) => throw new NotImplementedException();
2626

2727
public override IEnumerable<Type> TypeParameters => throw new NotImplementedException();
2828

29-
public override IEnumerable<Type> MethodParameters => throw new NotImplementedException();
30-
3129
public override Type Construct(IEnumerable<Type> typeArguments) => throw new NotImplementedException();
3230
}
3331
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public override int GetHashCode()
4141

4242
public override IEnumerable<Type> TypeParameters => throw new NotImplementedException();
4343

44-
public override IEnumerable<Type> MethodParameters => throw new NotImplementedException();
45-
4644
public override IEnumerable<IExtractionProduct> Contents
4745
{
4846
get

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ public override bool Equals(object? obj)
1818
return obj is PointerType pt && pointee.Equals(pt.pointee);
1919
}
2020

21-
22-
public override int GetHashCode()
23-
{
24-
return pointee.GetHashCode() * 29;
25-
}
21+
public override int GetHashCode() => HashCode.Combine(pointee, nameof(PointerType));
2622

2723
public override void WriteId(TextWriter trapFile, bool inContext)
2824
{
@@ -32,22 +28,20 @@ public override void WriteId(TextWriter trapFile, bool inContext)
3228

3329
public override string Name => pointee.Name + "*";
3430

35-
public override Namespace? Namespace => pointee.Namespace;
31+
public override Namespace? ContainingNamespace => pointee.ContainingNamespace;
3632

3733
public override Type? ContainingType => pointee.ContainingType;
3834

3935
public override TypeContainer Parent => pointee.Parent;
4036

41-
public override int ThisTypeParameters => 0;
37+
public override int ThisTypeParameterCount => 0;
4238

4339
public override CilTypeKind Kind => CilTypeKind.Pointer;
4440

4541
public override void WriteAssemblyPrefix(TextWriter trapFile) => pointee.WriteAssemblyPrefix(trapFile);
4642

4743
public override IEnumerable<Type> TypeParameters => throw new NotImplementedException();
4844

49-
public override IEnumerable<Type> MethodParameters => throw new NotImplementedException();
50-
5145
public override Type Construct(IEnumerable<Type> typeArguments) => throw new NotImplementedException();
5246

5347
public override IEnumerable<IExtractionProduct> Contents

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,27 @@ public override bool Equals(object? obj)
1818
return obj is PrimitiveType pt && typeCode == pt.typeCode;
1919
}
2020

21-
public override int GetHashCode()
22-
{
23-
return 1337 * (int)typeCode;
24-
}
21+
public override int GetHashCode() => typeCode.GetHashCode();
2522

2623
public override void WriteId(TextWriter trapFile, bool inContext)
2724
{
28-
trapFile.Write("builtin:");
29-
trapFile.Write(Name);
25+
Type.WritePrimitiveTypeId(trapFile, Name);
3026
}
3127

3228
public override string Name => typeCode.Id();
3329

34-
public override Namespace Namespace => Cx.SystemNamespace;
30+
public override Namespace ContainingNamespace => Cx.SystemNamespace;
3531

3632
public override Type? ContainingType => null;
3733

38-
public override int ThisTypeParameters => 0;
34+
public override int ThisTypeParameterCount => 0;
3935

4036
public override CilTypeKind Kind => CilTypeKind.ValueOrRefType;
4137

4238
public override void WriteAssemblyPrefix(TextWriter trapFile) { }
4339

4440
public override IEnumerable<Type> TypeParameters => throw new NotImplementedException();
4541

46-
public override IEnumerable<Type> MethodParameters => throw new NotImplementedException();
47-
4842
public override Type Construct(IEnumerable<Type> typeArguments) => throw new NotImplementedException();
4943
}
5044
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public void WriteId(TextWriter trapFile, GenericContext gc)
2121
{
2222
elementType.WriteId(trapFile, gc);
2323
trapFile.Write('[');
24-
for (var i = 1; i < shape.Rank; ++i)
25-
trapFile.Write(',');
24+
if (shape.Rank > 1)
25+
{
26+
trapFile.Write(string.Join(",", shape.Rank - 1));
27+
}
2628
trapFile.Write(']');
2729
}
2830
}

0 commit comments

Comments
 (0)