Ejemplo n.º 1
0
        public static bool Equals(INamedTypeSymbol x, INamedTypeSymbol y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

            if (x == null ||
                y == null)
            {
                return(false);
            }

            if (x.MetadataName != y.MetadataName ||
                !TypeSymbolComparer.Equals(x.ContainingType, y.ContainingType) ||
                !NamespaceSymbolComparer.Equals(x.ContainingNamespace, y.ContainingNamespace) ||
                x.Arity != y.Arity)
            {
                return(false);
            }

            for (var i = 0; i < x.Arity; i++)
            {
                if (!TypeSymbolComparer.Equals(x.TypeArguments[i], y.TypeArguments[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public int GetHashCode(ISymbol obj)
        {
            if (obj is ITypeSymbol typeSymbol)
            {
                return(TypeSymbolComparer.GetHashCode(typeSymbol));
            }

            return(obj?.MetadataName.GetHashCode() ?? 0);
        }
Ejemplo n.º 3
0
        private static bool ParametersMatches(IMethodSymbol x, IMethodSymbol y)
        {
            if (x.Parameters.Length != y.Parameters.Length)
            {
                return(false);
            }

            for (var i = 0; i < x.Parameters.Length; i++)
            {
                if (!TypeSymbolComparer.Equals(x.Parameters[i].Type, y.Parameters[i].Type))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        internal static bool IsNullableEquals(ExpressionSyntax condition, SemanticModel semanticModel, CancellationToken cancellationToken, ISymbol first, ISymbol other)
        {
            return(condition is InvocationExpressionSyntax invocation &&
                   invocation.TryGetInvokedMethodName(out var methodName) &&
                   methodName == "Equals" &&
                   invocation.Expression is MemberAccessExpressionSyntax memberAccess &&
                   TryGetName(memberAccess.Expression, out var className) &&
                   className == "Nullable" &&
                   invocation.ArgumentList?.Arguments.Count == 2 &&
                   IsMatchingNullable(GetSymbolType(first) as INamedTypeSymbol, GetSymbolType(other) as INamedTypeSymbol) &&
                   IsArguments(invocation, semanticModel, cancellationToken, first, other) &&
                   semanticModel.GetSymbolSafe(invocation, cancellationToken) as IMethodSymbol == KnownSymbol.Nullable.Equals);

            bool IsMatchingNullable(INamedTypeSymbol type1, INamedTypeSymbol type2)
            {
                if (type1 == null ||
                    type2 == null)
                {
                    return(false);
                }

                if (type1 == KnownSymbol.NullableOfT &&
                    type2 == KnownSymbol.NullableOfT)
                {
                    return(TypeSymbolComparer.Equals(type1.TypeArguments[0], type2.TypeArguments[0]));
                }

                if (type1 == KnownSymbol.NullableOfT)
                {
                    return(TypeSymbolComparer.Equals(type1.TypeArguments[0], type2));
                }

                if (type2 == KnownSymbol.NullableOfT)
                {
                    return(TypeSymbolComparer.Equals(type2.TypeArguments[0], type1));
                }

                return(false);
            }
        }
Ejemplo n.º 5
0
        public static bool Equals(ISymbol x, ISymbol y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

            if (x == null ||
                y == null)
            {
                return(false);
            }

            if (x is IEventSymbol xEvent &&
                y is IEventSymbol yEvent)
            {
                return(EventSymbolComparer.Equals(xEvent, yEvent));
            }

            if (x is IFieldSymbol xField &&
                y is IFieldSymbol yField)
            {
                return(FieldSymbolComparer.Equals(xField, yField));
            }

            if (x is ILocalSymbol xLocal &&
                y is ILocalSymbol yLocal)
            {
                return(LocalSymbolComparer.Equals(xLocal, yLocal));
            }

            if (x is IMethodSymbol xMethod &&
                y is IMethodSymbol yMethod)
            {
                return(MethodSymbolComparer.Equals(xMethod, yMethod));
            }

            if (x is INamedTypeSymbol xNamedType &&
                y is INamedTypeSymbol yNamedType)
            {
                return(NamedTypeSymbolComparer.Equals(xNamedType, yNamedType));
            }

            if (x is INamespaceSymbol xNamespace &&
                y is INamespaceSymbol yNamespace)
            {
                return(NamespaceSymbolComparer.Equals(xNamespace, yNamespace));
            }

            if (x is IParameterSymbol xParameter &&
                y is IParameterSymbol yParameter)
            {
                return(ParameterSymbolComparer.Equals(xParameter, yParameter));
            }

            if (x is IPropertySymbol xProperty &&
                y is IPropertySymbol yProperty)
            {
                return(PropertySymbolComparer.Equals(xProperty, yProperty));
            }

            if (x is ITypeSymbol xType &&
                y is ITypeSymbol yType)
            {
                return(TypeSymbolComparer.Equals(xType, yType));
            }

            return(x.Equals(y));
        }
Ejemplo n.º 6
0
 /// <inheritdoc/>
 public int GetHashCode(INamedTypeSymbol obj) => TypeSymbolComparer.GetHashCode(obj);