예제 #1
0
        /// <summary> Determines equality by name, containing type, arity and namespace. </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(ITypeSymbol?x, ITypeSymbol?y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

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

            return(x.MetadataName == y.MetadataName &&
                   NamespaceSymbolComparer.Equal(x.ContainingNamespace, y.ContainingNamespace) &&
                   NullableAnnotation() &&
                   TypeArguments());

            bool NullableAnnotation()
            {
                if (!x.IsReferenceType)
                {
                    return(true);
                }

                return((x.NullableAnnotation, y.NullableAnnotation) switch
                {
                    (Microsoft.CodeAnalysis.NullableAnnotation.None, _) => true,
                    (_, Microsoft.CodeAnalysis.NullableAnnotation.None) => true,
                    var(xa, ya) => xa == ya,
                });
            }
예제 #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));
        }
예제 #3
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 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),
            });
예제 #4
0
        /// <summary> Determines equality by name, containing type, arity and namespace. </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(INamedTypeSymbol x, INamedTypeSymbol y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

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