Exemplo 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);
        }
Exemplo n.º 2
0
        public override void VisitConstructorInitializer(ConstructorInitializerSyntax node)
        {
            if (this.method.MethodKind == MethodKind.Constructor)
            {
                var calledCtor = this.semanticModel.GetSymbolSafe(node, this.cancellationToken);
                if (this.IsValidCtor(calledCtor))
                {
                    var callingCtor = this.semanticModel.GetDeclaredSymbolSafe(node.Parent, this.cancellationToken) as IMethodSymbol;
                    if (TypeSymbolComparer.Equals(calledCtor.ContainingType, callingCtor?.ContainingType))
                    {
                        this.initializers.Add(node);
                    }
                    else if (this.ctors.Contains(callingCtor))
                    {
                        this.initializers.Add(node);
                    }
                }
            }
            else
            {
                this.initializers.Add(node);
            }

            base.VisitConstructorInitializer(node);
        }
Exemplo n.º 3
0
        internal static bool Is(this ITypeSymbol type, ITypeSymbol other)
        {
            if (other.IsInterface())
            {
                foreach (var @interface in type.AllInterfaces)
                {
                    if (TypeSymbolComparer.Equals(@interface, other))
                    {
                        return(true);
                    }
                }

                return(false);
            }

            while (type?.BaseType != null)
            {
                if (TypeSymbolComparer.Equals(type, other))
                {
                    return(true);
                }

                type = type.BaseType;
            }

            return(false);
        }
Exemplo n.º 4
0
        public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            var ctor = this.semanticModel.GetSymbolSafe(node, this.cancellationToken) as IMethodSymbol;

            if (TypeSymbolComparer.Equals(this.type, ctor?.ContainingType))
            {
                this.objectCreations.Add(node);
            }

            base.VisitObjectCreationExpression(node);
        }
Exemplo n.º 5
0
        internal static bool IsSameType(this ExpressionSyntax expression, ITypeSymbol type, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (expression == null || type == null)
            {
                return(false);
            }

            var symbol = semanticModel.GetTypeInfoSafe(expression, cancellationToken)
                         .Type;

            return(TypeSymbolComparer.Equals(symbol, type));
        }
Exemplo n.º 6
0
        private static bool IsRunBefore(IMethodSymbol first, IMethodSymbol other, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (first == null ||
                other == null)
            {
                return(false);
            }

            if (TryGetInitializer(other, cancellationToken, out ConstructorInitializerSyntax initializer))
            {
                if (TypeSymbolComparer.Equals(first.ContainingType, other.ContainingType) &&
                    !initializer.ThisOrBaseKeyword.IsKind(SyntaxKind.ThisKeyword))
                {
                    return(false);
                }

                if (!other.ContainingType.Is(first.ContainingType) &&
                    initializer.ThisOrBaseKeyword.IsKind(SyntaxKind.BaseKeyword))
                {
                    return(false);
                }
            }
            else
            {
                if (TypeSymbolComparer.Equals(first.ContainingType, other.ContainingType) ||
                    !other.ContainingType.Is(first.ContainingType))
                {
                    return(false);
                }
            }

            var next = semanticModel.GetSymbolSafe(initializer, cancellationToken);

            if (MethodSymbolComparer.Equals(first, next))
            {
                return(true);
            }

            if (next == null)
            {
                if (TryGetDefault(other.ContainingType?.BaseType, out next))
                {
                    return(MethodSymbolComparer.Equals(first, next));
                }

                return(false);
            }

            return(IsRunBefore(first, next, semanticModel, cancellationToken));
        }
Exemplo n.º 7
0
        private bool IsValidCtor(IMethodSymbol ctor)
        {
            if (!MethodSymbolComparer.Equals(this.method, ctor))
            {
                return(false);
            }

            if (TypeSymbolComparer.Equals(this.contextType, ctor?.ContainingType))
            {
                return(true);
            }

            return(this.ctors.Contains(ctor));
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        internal static bool IsNullable(this ITypeSymbol nullableType, ExpressionSyntax value, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            var namedTypeSymbol = nullableType as INamedTypeSymbol;

            if (namedTypeSymbol == null || !namedTypeSymbol.IsGenericType || namedTypeSymbol.Name != "Nullable" || namedTypeSymbol.TypeParameters.Length != 1)
            {
                return(false);
            }

            if (value.IsKind(SyntaxKind.NullLiteralExpression))
            {
                return(true);
            }

            var typeInfo = semanticModel.GetTypeInfoSafe(value, cancellationToken);

            return(TypeSymbolComparer.Equals(namedTypeSymbol.TypeArguments[0], typeInfo.Type));
        }
Exemplo n.º 10
0
        public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            var ctor = this.semanticModel.GetDeclaredSymbolSafe(node, this.cancellationToken);

            if (TypeSymbolComparer.Equals(this.type, ctor.ContainingType))
            {
                if (ctor.DeclaredAccessibility != Accessibility.Private)
                {
                    this.nonPrivateCtors.Add(node);
                }

                if (ctor.Parameters.Length == 0)
                {
                    this.Default = node;
                }
            }

            base.VisitConstructorDeclaration(node);
        }
Exemplo n.º 11
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));
        }