Skip to content

Commit

Permalink
(#38) ImportLogic: process generic parameters in type signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Dec 29, 2024
1 parent 7da8424 commit c91d13d
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup Label="Packaging">
<Version>2.0.1-pre03</Version>
<Version>2.0.1-pre04</Version>
<Copyright>Copyright © JetBrains 2024</Copyright>

<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
Expand Down
16 changes: 11 additions & 5 deletions src/Refasmer/Importer/ImportLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,17 +619,23 @@ private IEnumerable<TypeDefinitionHandle> CalculateInternalTypesToPreserve(

var parentTypes = new List<EntityHandle>();
if (!type.BaseType.IsNil) parentTypes.Add(type.BaseType);
foreach (var interfaceImpl in type.GetInterfaceImplementations())
foreach (var interfaceImplHandle in type.GetInterfaceImplementations())
{
var @interface = _reader.GetInterfaceImplementation(interfaceImpl);
parentTypes.Add(@interface.Interface);
var interfaceImpl = _reader.GetInterfaceImplementation(interfaceImplHandle);
parentTypes.Add(interfaceImpl.Interface);
}

foreach (var parentTypeHandle in parentTypes)
{
if (parentTypeHandle.Kind == HandleKind.TypeDefinition)
switch (parentTypeHandle.Kind)
{
candidateTypes.Add((TypeDefinitionHandle)parentTypeHandle);
case HandleKind.TypeDefinition:
candidateTypes.Add((TypeDefinitionHandle)parentTypeHandle);
break;
case HandleKind.TypeSpecification:
var specification = _reader.GetTypeSpecification((TypeSpecificationHandle)parentTypeHandle);
AcceptTypeSignature(specification.Signature, collector);
break;
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/Refasmer.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ await VerifyTypeContents(

[TestCase("PublicClassWithInternalTypeInApi", "Class1ToBeMarkedInternal")]
[TestCase("PublicClassDerivingFromInternal", "Class2ToBeMarkedInternal")]
[TestCase("PublicClassImplementingInternal", "InterfaceToBeMarkedInternal")]
public async Task InternalTypeInPublicApi(string mainClassName, string auxiliaryClassName)
[TestCase("PublicClassImplementingInternal", "IInterface1ToBeMarkedInternal")]
[TestCase("PublicClassWithInternalInterfaceImpl", "Class3ToBeMarkedInternal,IInterface2ToBeMarkedInternal`1")]
public async Task InternalTypeInPublicApi(string mainClassName, string auxiliaryClassNames)
{
var assemblyPath = await BuildTestAssemblyWithInternalTypeInPublicApi();
var resultAssembly = RefasmTestAssembly(assemblyPath, omitNonApiMembers: true);

var fullMainClassName = $"RefasmerTestAssembly.{mainClassName}";
var fullAuxiliaryClassName = $"RefasmerTestAssembly.{auxiliaryClassName}";
var fullAuxiliaryClassNames = auxiliaryClassNames.Split(',').Select(x => $"RefasmerTestAssembly.{x}");

await VerifyTypeContents(
resultAssembly,
[fullMainClassName, fullAuxiliaryClassName],
[fullMainClassName, ..fullAuxiliaryClassNames],
parameters: [mainClassName]);
}
}
24 changes: 20 additions & 4 deletions tests/Refasmer.Tests/Printer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public static class Printer
public static void PrintType(TypeDefinition type, StringBuilder printout, string indent = "")
{
var access = GetAccessString(type);
printout.AppendLine($"{indent}{access} type: {type.FullName}");
var typeKind = GetTypeKindString(type);
printout.AppendLine($"{indent}{access} {typeKind}: {type.FullName}");
if (type.HasFields)
{
printout.AppendLine($"{indent}fields:");
Expand All @@ -34,10 +35,15 @@ public static void PrintType(TypeDefinition type, StringBuilder printout, string
}
}

printout.AppendLine($"{indent}): {method.ReturnType}:");
foreach (var instruction in method.Body.Instructions)
printout.AppendLine($"): {method.ReturnType}:");
if (method.IsAbstract)
printout.AppendLine($"{indent} - <abstract>");
else
{
printout.AppendLine($"{indent} - {instruction}");
foreach (var instruction in method.Body.Instructions)
{
printout.AppendLine($"{indent} - {instruction}");
}
}
}
}
Expand All @@ -62,4 +68,14 @@ private static string GetAccessString(TypeDefinition type)
if (type.IsNestedFamilyAndAssembly) return "private protected";
return "internal";
}

private static string GetTypeKindString(TypeDefinition typeDefinition)
{
var result = new StringBuilder();
if (typeDefinition.IsInterface) result.Append("interface");
else if (typeDefinition.IsAbstract) result.Append("abstract ");
if (typeDefinition.IsValueType) result.Append("struct");
else if (typeDefinition.IsClass) result.Append("class");
return result.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public type: RefasmerTestAssembly.BlittableGraph
public struct: RefasmerTestAssembly.BlittableGraph
fields:
- <SyntheticNonEmptyStructMarker>: System.Int32
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public type: RefasmerTestAssembly.BlittableStructWithPrivateFields
public struct: RefasmerTestAssembly.BlittableStructWithPrivateFields
fields:
- <SyntheticNonEmptyStructMarker>: System.Int32
Original file line number Diff line number Diff line change
@@ -1 +1 @@
public type: RefasmerTestAssembly.EmptyStructWithStaticMember
public struct: RefasmerTestAssembly.EmptyStructWithStaticMember
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public type: RefasmerTestAssembly.NonBlittableGraph
public struct: RefasmerTestAssembly.NonBlittableGraph
fields:
- <SyntheticNonEmptyStructMarker>: System.Int32
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public type: RefasmerTestAssembly.NonBlittableStructWithPrivateFields
public struct: RefasmerTestAssembly.NonBlittableStructWithPrivateFields
fields:
- <SyntheticNonEmptyStructMarker>: System.Int32
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public type: RefasmerTestAssembly.NonEmptyStructWithStaticMember
public struct: RefasmerTestAssembly.NonEmptyStructWithStaticMember
fields:
- <SyntheticNonEmptyStructMarker>: System.Int32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public type: RefasmerTestAssembly.PublicClassWithPrivateFields
public class: RefasmerTestAssembly.PublicClassWithPrivateFields
fields:
- PublicInt: System.Int32
methods:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public type: RefasmerTestAssembly.PublicStructWithPrivateFields
public struct: RefasmerTestAssembly.PublicStructWithPrivateFields
fields:
- PublicInt: System.Int32
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public type: RefasmerTestAssembly.StructWithNestedPrivateTypes
public struct: RefasmerTestAssembly.StructWithNestedPrivateTypes
fields:
- <SyntheticNonEmptyStructMarker>: System.Int32
types:
internal type: RefasmerTestAssembly.StructWithNestedPrivateTypes/UnusedPublicStruct
internal struct: RefasmerTestAssembly.StructWithNestedPrivateTypes/UnusedPublicStruct
fields:
- <SyntheticNonEmptyStructMarker>: System.Int32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public type: RefasmerTestAssembly.UnsafeClassWithFunctionPointer
public class: RefasmerTestAssembly.UnsafeClassWithFunctionPointer
methods:
- MethodWithFunctionPointer(method System.Void *() functionPointer): System.Void:
- .ctor(): System.Void:
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
public type: RefasmerTestAssembly.NonBlittableGraph
public struct: RefasmerTestAssembly.NonBlittableGraph
fields:
- x: RefasmerTestAssembly.NonBlittableGraph/DubiouslyBlittableType
types:
private type: RefasmerTestAssembly.NonBlittableGraph/DubiouslyBlittableType
private struct: RefasmerTestAssembly.NonBlittableGraph/DubiouslyBlittableType
fields:
- x: RefasmerTestAssembly.NonBlittableGraph/DubiouslyBlittableType/NonBlittableType
types:
private type: RefasmerTestAssembly.NonBlittableGraph/DubiouslyBlittableType/NonBlittableType
private struct: RefasmerTestAssembly.NonBlittableGraph/DubiouslyBlittableType/NonBlittableType
fields:
- x: System.String
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public type: RefasmerTestAssembly.PublicClassImplementingInternal
public class: RefasmerTestAssembly.PublicClassImplementingInternal
methods:
- .ctor(): System.Void:
internal type: RefasmerTestAssembly.InterfaceToBeMarkedInternal
internal interface: RefasmerTestAssembly.IInterface1ToBeMarkedInternal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class: RefasmerTestAssembly.PublicClassWithInternalInterfaceImpl
methods:
- RefasmerTestAssembly.IInterface2ToBeMarkedInternal<RefasmerTestAssembly.Class3ToBeMarkedInternal>.Foo(): RefasmerTestAssembly.Class3ToBeMarkedInternal:
- .ctor(): System.Void:
internal class: RefasmerTestAssembly.Class3ToBeMarkedInternal
public interface: RefasmerTestAssembly.IInterface2ToBeMarkedInternal`1
methods:
- Foo(): T:
- <abstract>
16 changes: 14 additions & 2 deletions tests/RefasmerTestAssembly/InternalClassInApi.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
using System;

namespace RefasmerTestAssembly;

// Post-processed and converted to internal by tests:
public class Class1ToBeMarkedInternal;
public class Class2ToBeMarkedInternal;
public class InterfaceToBeMarkedInternal;
public class Class3ToBeMarkedInternal;
public interface IInterface1ToBeMarkedInternal;
public interface IInterface2ToBeMarkedInternal<T>
{
T Foo();
}

public class PublicClassWithInternalTypeInApi
{
public void Accept(Class1ToBeMarkedInternal argument) {}
}

public class PublicClassDerivingFromInternal : Class2ToBeMarkedInternal;
public class PublicClassImplementingInternal : InterfaceToBeMarkedInternal;
public class PublicClassImplementingInternal : IInterface1ToBeMarkedInternal;

public class PublicClassWithInternalInterfaceImpl : IInterface2ToBeMarkedInternal<Class3ToBeMarkedInternal>
{
Class3ToBeMarkedInternal IInterface2ToBeMarkedInternal<Class3ToBeMarkedInternal>.Foo() => throw new Exception("123");
}

0 comments on commit c91d13d

Please sign in to comment.