Skip to content

Commit

Permalink
rid of type definitions filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Unkorunk committed Oct 22, 2024
1 parent 604c11c commit d2b90c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 125 deletions.
1 change: 0 additions & 1 deletion src/Refasmer/Filters/IImportFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace JetBrains.Refasmer.Filters
{
public interface IImportFilter
{
bool AllowImport( TypeDefinition type, MetadataReader reader );
bool AllowImport( MethodDefinition method, MetadataReader reader );
bool AllowImport( FieldDefinition field, MetadataReader reader );

Expand Down
67 changes: 1 addition & 66 deletions src/Refasmer/Importer/ImportLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,11 @@
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using JetBrains.Refasmer.Filters;

namespace JetBrains.Refasmer
{
public partial class MetadataImporter
{
private bool AllowImportType( EntityHandle typeHandle )
{
if (typeHandle.IsNil)
return false;

if (Filter == null)
return true;

switch (typeHandle.Kind)
{
case HandleKind.TypeDefinition:
return Filter.AllowImport(_reader.GetTypeDefinition((TypeDefinitionHandle) typeHandle), _reader);
case HandleKind.TypeReference:
return true;
case HandleKind.TypeSpecification:
return AllowImportType(_reader.GetGenericType((TypeSpecificationHandle)typeHandle));

default:
throw new ArgumentOutOfRangeException(nameof (typeHandle));
}
}

private TypeDefinitionHandle ImportTypeDefinitionSkeleton( TypeDefinitionHandle srcHandle )
{
var src = _reader.GetTypeDefinition(srcHandle);
Expand Down Expand Up @@ -68,7 +45,6 @@ private TypeDefinitionHandle ImportTypeDefinitionSkeleton( TypeDefinitionHandle

var implementations = src.GetMethodImplementations()
.Select(_reader.GetMethodImplementation)
.Where(mi => AllowImportType(_reader.GetMethodClass(mi.MethodDeclaration)))
.Select(mi => (MethodDefinitionHandle)mi.MethodBody)
.ToImmutableHashSet();

Expand Down Expand Up @@ -413,50 +389,9 @@ public ReservedBlob<GuidHandle> Import()
var index = 1;
Debug?.Invoke("Preparing type list for import");

var checker = new CachedAttributeChecker();

foreach (var srcHandle in _reader.TypeDefinitions)
{
bool shouldImport;

var src = _reader.GetTypeDefinition(srcHandle);

// Special <Module> type
if (srcHandle.GetHashCode() == 1 && _reader.GetString(src.Name) == "<Module>")
{
shouldImport = true;
}
else if (checker.HasAttribute(_reader, src, FullNames.Embedded) &&
checker.HasAttribute(_reader, src, FullNames.CompilerGenerated))
{
Trace?.Invoke($"Embedded type found {_reader.ToString(srcHandle)}");
shouldImport = true;
}
else if (_reader.GetString(src.Namespace) == FullNames.CompilerServices &&
_reader.GetFullname(src.BaseType) == FullNames.Attribute)
{
Trace?.Invoke($"CompilerServices attribute found {_reader.ToString(srcHandle)}");
shouldImport = true;
}
else if (_reader.GetString(src.Namespace) == FullNames.CodeAnalysis &&
_reader.GetFullname(src.BaseType) == FullNames.Attribute)
{
Trace?.Invoke($"CodeAnalysis attribute found {_reader.ToString(srcHandle)}");
shouldImport = true;
}
else
{
shouldImport = Filter?.AllowImport(_reader.GetTypeDefinition(srcHandle), _reader) != false;
}

if (shouldImport)
{
_typeDefinitionCache[srcHandle] = MetadataTokens.TypeDefinitionHandle(index++);
}
else
{
Trace?.Invoke($"Type filtered and will not be imported {_reader.ToString(srcHandle)}");
}
_typeDefinitionCache[srcHandle] = MetadataTokens.TypeDefinitionHandle(index++);
}

Debug?.Invoke("Importing type definitions");
Expand Down
85 changes: 27 additions & 58 deletions src/Refasmer/Importer/SignatureImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,40 @@ public UnknownTypeInSignature( EntityHandle handle, string message )

private BlobHandle ImportTypeSignature( BlobHandle srcHandle )
{
try
{
var blobReader = _reader.GetBlobReader(srcHandle);
var blobBuilder = new BlobBuilder(blobReader.Length);

ImportTypeSignature(ref blobReader, blobBuilder);
return _builder.GetOrAddBlob(blobBuilder);
}
catch (UnknownTypeInSignature e)
{
if (e.Handle.Kind != HandleKind.TypeDefinition)
throw;

var typeDef = _reader.GetTypeDefinition((TypeDefinitionHandle) e.Handle);
var blobReader = _reader.GetBlobReader(srcHandle);
var blobBuilder = new BlobBuilder(blobReader.Length);

if (Filter?.AllowImport(typeDef, _reader) == true)
throw;

}
return default;
ImportTypeSignature(ref blobReader, blobBuilder);
return _builder.GetOrAddBlob(blobBuilder);
}

private BlobHandle ImportSignatureWithHeader( BlobHandle srcHandle )
{
try
{
var blobReader = _reader.GetBlobReader(srcHandle);
var blobBuilder = new BlobBuilder(blobReader.Length);

var header = blobReader.ReadSignatureHeader();
blobBuilder.WriteByte(header.RawValue);

switch (header.Kind)
{
case SignatureKind.Method:
case SignatureKind.Property:
ImportMethodSignature(header, ref blobReader, blobBuilder);
break;
case SignatureKind.Field:
ImportFieldSignature(ref blobReader, blobBuilder);
break;
case SignatureKind.LocalVariables:
ImportLocalSignature(ref blobReader, blobBuilder);
break;
case SignatureKind.MethodSpecification:
ImportMethodSpecSignature(ref blobReader, blobBuilder);
break;
default:
throw new BadImageFormatException();
}
return _builder.GetOrAddBlob(blobBuilder);
}
catch (UnknownTypeInSignature e)
{
if (e.Handle.Kind != HandleKind.TypeDefinition)
throw;

var typeDef = _reader.GetTypeDefinition((TypeDefinitionHandle) e.Handle);

if (Filter?.AllowImport(typeDef, _reader) == true)
throw;
var blobReader = _reader.GetBlobReader(srcHandle);
var blobBuilder = new BlobBuilder(blobReader.Length);

var header = blobReader.ReadSignatureHeader();
blobBuilder.WriteByte(header.RawValue);

switch (header.Kind)
{
case SignatureKind.Method:
case SignatureKind.Property:
ImportMethodSignature(header, ref blobReader, blobBuilder);
break;
case SignatureKind.Field:
ImportFieldSignature(ref blobReader, blobBuilder);
break;
case SignatureKind.LocalVariables:
ImportLocalSignature(ref blobReader, blobBuilder);
break;
case SignatureKind.MethodSpecification:
ImportMethodSpecSignature(ref blobReader, blobBuilder);
break;
default:
throw new BadImageFormatException();
}
return default;

return _builder.GetOrAddBlob(blobBuilder);
}

private void ImportMethodSignature( SignatureHeader header, ref BlobReader blobReader, BlobBuilder blobBuilder )
Expand Down

0 comments on commit d2b90c3

Please sign in to comment.