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); }
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); }
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); }
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); }
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)); }
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)); }
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)); }
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); }
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)); }
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); }
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)); }