Ejemplo n.º 1
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(IParameterSymbol x, IParameterSymbol y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

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

            return(x.MetadataName == y.MetadataName &&
                   SymbolComparer.Equals(x.ContainingSymbol, y.ContainingSymbol));
        }
Ejemplo n.º 2
0
        public static bool IsSameType(this ITypeSymbol x, ITypeSymbol y)
        {
            if (Equals(x, y))
            {
                return(true);
            }

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

            if (x is ITypeParameterSymbol xtp)
            {
                if (y is ITypeParameterSymbol ytp)
                {
                    return(xtp.MetadataName == ytp.MetadataName &&
                           SymbolComparer.Equals(xtp.ContainingSymbol, ytp.ContainingSymbol));
                }

                foreach (var constraintType in xtp.ConstraintTypes)
                {
                    if (Is(constraintType, y))
                    {
                        return(true);
                    }
                }

                return(false);
            }
            else if (y is ITypeParameterSymbol ytp)
            {
                foreach (var constraintType in ytp.ConstraintTypes)
                {
                    if (Is(x, constraintType))
                    {
                        return(true);
                    }
                }

                return(false);
            }

            return(x is INamedTypeSymbol firstNamed &&
                   y is INamedTypeSymbol otherNamed &&
                   IsSameType(firstNamed, otherNamed));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get a walker with all mutations for <paramref name="fieldOrProperty"/>.
        /// </summary>
        /// <param name="fieldOrProperty">The <see cref="FieldOrProperty"/>.</param>
        /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
        /// <returns>A walker with all mutations for <paramref name="fieldOrProperty"/>.</returns>
        public static MutationWalker For(FieldOrProperty fieldOrProperty, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (fieldOrProperty.Symbol.ContainingType.TrySingleDeclaration(cancellationToken, out TypeDeclarationSyntax? typeDeclaration))
            {
                var walker = Borrow(typeDeclaration, SearchScope.Instance, semanticModel, cancellationToken);
                walker.assignments.RemoveAll(x => !IsFieldOrProperty(x.Left));
                walker.prefixUnaries.RemoveAll(x => !IsFieldOrProperty(x.Operand));
                walker.postfixUnaries.RemoveAll(x => !IsFieldOrProperty(x.Operand));
                walker.refOrOutArguments.RemoveAll(x => !IsFieldOrProperty(x.Expression));
                return(walker);
            }

            return(Borrow(() => new MutationWalker()));

            bool IsFieldOrProperty(ExpressionSyntax expression)
            {
                return(semanticModel.TryGetSymbol(expression, cancellationToken, out var symbol) &&
                       SymbolComparer.Equal(symbol, fieldOrProperty.Symbol));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get a walker with all mutations for <paramref name="localOrParameter"/>.
        /// </summary>
        /// <param name="localOrParameter">The <see cref="LocalOrParameter"/>.</param>
        /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
        /// <returns>A walker with all mutations for <paramref name="localOrParameter"/>.</returns>
        public static MutationWalker For(LocalOrParameter localOrParameter, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (localOrParameter.TryGetScope(cancellationToken, out var node))
            {
                var walker = Borrow(node, SearchScope.Member, semanticModel, cancellationToken);
                walker.assignments.RemoveAll(x => !IsMatch(x.Left));
                walker.prefixUnaries.RemoveAll(x => !IsMatch(x.Operand));
                walker.postfixUnaries.RemoveAll(x => !IsMatch(x.Operand));
                walker.refOrOutArguments.RemoveAll(x => !IsMatch(x.Expression));
                return(walker);
            }

            return(Borrow(() => new MutationWalker()));

            bool IsMatch(ExpressionSyntax expression)
            {
                return(semanticModel.TryGetSymbol(expression, cancellationToken, out var symbol) &&
                       SymbolComparer.Equal(symbol, localOrParameter.Symbol));
            }
        }
 static bool Definition(IParameterSymbol x, IParameterSymbol y)
 {
     return(x.Name == y.Name &&
            SymbolComparer.Equivalent(x.ContainingSymbol, y.ContainingSymbol));
 }