-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from Washi1337/feature/is-assignable-to
TypeSignature Compatibility and Assignability Rules
- Loading branch information
Showing
19 changed files
with
1,124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/AsmResolver.DotNet/Signatures/Types/ArrayBaseTypeSignature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace AsmResolver.DotNet.Signatures.Types | ||
{ | ||
/// <summary> | ||
/// Represents a type signature representing an array. | ||
/// </summary> | ||
public abstract class ArrayBaseTypeSignature : TypeSpecificationSignature | ||
{ | ||
/// <summary> | ||
/// Initializes an array type signature. | ||
/// </summary> | ||
/// <param name="baseType">The element type of the array.</param> | ||
protected ArrayBaseTypeSignature(TypeSignature baseType) | ||
: base(baseType) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool IsValueType => false; | ||
|
||
/// <summary> | ||
/// Gets the number of dimensions this array defines. | ||
/// </summary> | ||
public abstract int Rank | ||
{ | ||
get; | ||
} | ||
|
||
/// <summary> | ||
/// Obtains the dimensions this array defines. | ||
/// </summary> | ||
/// <returns>The dimensions.</returns> | ||
public abstract IEnumerable<ArrayDimension> GetDimensions(); | ||
|
||
/// <inheritdoc /> | ||
protected override bool IsDirectlyCompatibleWith(TypeSignature other, SignatureComparer comparer) | ||
{ | ||
if (base.IsDirectlyCompatibleWith(other, comparer)) | ||
return true; | ||
|
||
TypeSignature? elementType = null; | ||
if (other is ArrayBaseTypeSignature otherArrayType && Rank == otherArrayType.Rank) | ||
{ | ||
// Arrays are only compatible if they have the same rank. | ||
elementType = otherArrayType.BaseType; | ||
} | ||
else if (Rank == 1 | ||
&& other is GenericInstanceTypeSignature genericInstanceType | ||
&& genericInstanceType.GenericType.IsTypeOf("System.Collections.Generic", "IList`1")) | ||
{ | ||
// Arrays are also compatible with IList<T> if they've only one dimension. | ||
elementType = genericInstanceType.TypeArguments[0]; | ||
} | ||
|
||
if (elementType is null) | ||
return false; | ||
|
||
var v = BaseType.GetUnderlyingType(); | ||
var w = elementType.GetUnderlyingType(); | ||
|
||
return comparer.Equals(v.GetReducedType(), w.GetReducedType()) | ||
|| v.IsCompatibleWith(w, comparer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.