Skip to content

Commit

Permalink
Support implicit conversions of lambda expressions to LambdaExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
cston committed Sep 4, 2021
1 parent c8deeaa commit 9c5f712
Show file tree
Hide file tree
Showing 2 changed files with 300 additions and 19 deletions.
57 changes: 43 additions & 14 deletions src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,34 +383,58 @@ public static bool IsPointerOrFunctionPointer(this TypeSymbol type)
}

/// <summary>
/// return true if the type is constructed from System.Linq.Expressions.Expression`1
/// Returns true if the type is constructed from a generic type named "System.Linq.Expressions.Expression"
/// with one type parameter.
/// </summary>
public static bool IsExpressionTree(this TypeSymbol type)
{
return type.IsGenericOrNonGenericExpressionType(out bool isGenericType) && isGenericType;
}

/// <summary>
/// Returns true if the type is a non-generic type named "System.Linq.Expressions.Expression"
/// or "System.Linq.Expressions.LambdaExpression".
/// </summary>
public static bool IsNonGenericExpressionType(this TypeSymbol type)
{
return type.IsGenericOrNonGenericExpressionType(out bool isGenericType) && !isGenericType;
}

/// <summary>
/// Returns true if the type is constructed from a generic type named "System.Linq.Expressions.Expression"
/// with one type parameter, or if the type is a non-generic type named "System.Linq.Expressions.Expression"
/// or "System.Linq.Expressions.LambdaExpression".
/// </summary>
public static bool IsGenericOrNonGenericExpressionType(this TypeSymbol _type, out bool isGenericType)
{
if (_type.OriginalDefinition is NamedTypeSymbol type &&
type.Name == "Expression" &&
CheckFullName(type.ContainingSymbol, s_expressionsNamespaceName))
if (_type.OriginalDefinition is NamedTypeSymbol type)
{
if (type.Arity == 0)
{
isGenericType = false;
return true;
}
if (type.Arity == 1 &&
type.MangleName)
switch (type.Name)
{
isGenericType = true;
return true;
case "Expression":
if (IsNamespaceName(type.ContainingSymbol, s_expressionsNamespaceName))
{
if (type.Arity == 0)
{
isGenericType = false;
return true;
}
if (type.Arity == 1 &&
type.MangleName)
{
isGenericType = true;
return true;
}
}
break;
case "LambdaExpression":
if (IsNamespaceName(type.ContainingSymbol, s_expressionsNamespaceName) &&
type.Arity == 0)
{
isGenericType = false;
return true;
}
break;
}
}
isGenericType = false;
Expand Down Expand Up @@ -446,8 +470,13 @@ public static bool IsPossibleArrayGenericInterface(this TypeSymbol type)

private static readonly string[] s_expressionsNamespaceName = { "Expressions", "Linq", MetadataHelpers.SystemString, "" };

private static bool CheckFullName(Symbol symbol, string[] names)
private static bool IsNamespaceName(Symbol symbol, string[] names)
{
if (symbol.Kind != SymbolKind.Namespace)
{
return false;
}

for (int i = 0; i < names.Length; i++)
{
if ((object)symbol == null || symbol.Name != names[i]) return false;
Expand Down
Loading

0 comments on commit 9c5f712

Please sign in to comment.