/// <summary> Determines equality by delegating to other compare. </summary>
        /// <param name="x">The first instance.</param>
        /// <param name="y">The other instance.</param>
        /// <returns>True if the instances are found equal.</returns>
        public static bool Equal(ISymbol?x, ISymbol?y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

            if (x is null ||
                y is null ||
                x.Kind != y.Kind ||
                x.MetadataName != y.MetadataName)
            {
                return(false);
            }

            return((x.Kind, y.Kind) switch
            {
                (SymbolKind.Field, SymbolKind.Field) => FieldSymbolComparer.Equal((IFieldSymbol)x, (IFieldSymbol)y),
                (SymbolKind.Event, SymbolKind.Event) => EventSymbolComparer.Equal((IEventSymbol)x, (IEventSymbol)y),
                (SymbolKind.Property, SymbolKind.Property) => PropertySymbolComparer.Equal((IPropertySymbol)x, (IPropertySymbol)y),
                (SymbolKind.Method, SymbolKind.Method) => MethodSymbolComparer.Equal((IMethodSymbol)x, (IMethodSymbol)y),
                (SymbolKind.Parameter, SymbolKind.Parameter) => ParameterSymbolComparer.Equal((IParameterSymbol)x, (IParameterSymbol)y),
                (SymbolKind.Local, SymbolKind.Local) => LocalSymbolComparer.Equal((ILocalSymbol)x, (ILocalSymbol)y),
                (SymbolKind.NamedType, SymbolKind.NamedType) => NamedTypeSymbolComparer.Equal((INamedTypeSymbol)x, (INamedTypeSymbol)y),
                (SymbolKind.ArrayType, SymbolKind.ArrayType) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y),
                (SymbolKind.DynamicType, SymbolKind.DynamicType) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y),
                (SymbolKind.ErrorType, SymbolKind.ErrorType) => false,
                (SymbolKind.PointerType, SymbolKind.PointerType) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y),
                (SymbolKind.TypeParameter, SymbolKind.TypeParameter) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y),
                (SymbolKind.Namespace, SymbolKind.Namespace) => NamespaceSymbolComparer.Equal((INamespaceSymbol)x, (INamespaceSymbol)y),
                (_, _) => SymbolEqualityComparer.Default.Equals(x, y),
            });
Esempio n. 2
0
        /// <summary> Determines equality by name and containing symbol. </summary>
        /// <param name="x">The first instance.</param>
        /// <param name="y">The other instance.</param>
        /// <returns>True if the instances are found equal.</returns>
        public static bool Equals(ITypeSymbol x, ITypeSymbol y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

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

            if (x.TypeKind != y.TypeKind)
            {
                return(false);
            }

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

            if (x.TypeKind == TypeKind.TypeParameter)
            {
                return(x.MetadataName == y.MetadataName &&
                       SymbolComparer.Equals(x.ContainingSymbol, y.ContainingSymbol));
            }

            return(x.MetadataName == y.MetadataName &&
                   Equals(x.ContainingType, y.ContainingType) &&
                   NamespaceSymbolComparer.Equals(x.ContainingNamespace, y.ContainingNamespace));
        }
Esempio n. 3
0
        public static bool IsSameType(this INamedTypeSymbol x, INamedTypeSymbol y)
        {
            if (x is null ||
                y is null)
            {
                return(false);
            }

            if (x.IsDefinition ^ y.IsDefinition)
            {
                return(IsSameType(x.OriginalDefinition, y.OriginalDefinition));
            }

            return(NamedTypeSymbolComparer.Equals(x, y));
        }
Esempio n. 4
0
        /// <summary> Compares equality by name and containing type. </summary>
        /// <param name="x">The first instance.</param>
        /// <param name="y">The other instance.</param>
        /// <returns>True if the instances are found equal.</returns>
        public static bool Equals(IFieldSymbol x, IFieldSymbol y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

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

            return(x.MetadataName == y.MetadataName &&
                   NamedTypeSymbolComparer.Equals(x.ContainingType, y.ContainingType));
        }
        /// <summary> Determines equality by name, containing type, return type and parameters. </summary>
        /// <param name="x">The first instance.</param>
        /// <param name="y">The other instance.</param>
        /// <returns>True if the instances are found equal.</returns>
        public static bool Equal(IMethodSymbol?x, IMethodSymbol?y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

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

            return(x.MetadataName == y.MetadataName &&
                   NamedTypeSymbolComparer.Equal(x.ContainingType, y.ContainingType) &&
                   ParametersMatches(x.Parameters, y.Parameters));
        /// <summary> Determines equality by name, containing type and type. </summary>
        /// <param name="x">The first instance.</param>
        /// <param name="y">The other instance.</param>
        /// <returns>True if the instances are found equal.</returns>
        public static bool Equal(IPropertySymbol?x, IPropertySymbol?y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

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

            return(x.MetadataName == y.MetadataName &&
                   NamedTypeSymbolComparer.Equal(x.ContainingType, y.ContainingType) &&
                   TypeSymbolComparer.Equal(x.Type, y.Type));
        }
Esempio n. 7
0
        /// <summary> Determines equality by delegating to other compare. </summary>
        /// <param name="x">The first instance.</param>
        /// <param name="y">The other instance.</param>
        /// <returns>True if the instances are found equal.</returns>
        public static bool Equals(ISymbol x, ISymbol y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

            if (x is null ||
                y is 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));
        }