Ejemplo n.º 1
0
 /// <summary>
 /// Check if <paramref name="type"/> is <paramref name="qualifiedType1"/> or <paramref name="qualifiedType2"/>.
 /// </summary>
 /// <param name="type">The <see cref="ITypeSymbol"/>.</param>
 /// <param name="qualifiedType1">The first <see cref="QualifiedType"/>.</param>
 /// <param name="qualifiedType2">The second <see cref="QualifiedType"/>.</param>
 /// <returns>True if <paramref name="type"/> is <paramref name="qualifiedType1"/> or <paramref name="qualifiedType2"/>.</returns>
 public static bool IsEither(this ITypeSymbol type, QualifiedType qualifiedType1, QualifiedType qualifiedType2) => type == qualifiedType1 || type == qualifiedType2;
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QualifiedEvent"/> class.
 /// </summary>
 /// <param name="containingType">The containing type.</param>
 /// <param name="name">The name.</param>
 public QualifiedEvent(QualifiedType containingType, string name)
     : base(containingType, name)
 {
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Try find an attribute of the expected type.
        /// </summary>
        /// <param name="attributeLists">The <see cref="AttributeListSyntax"/>.</param>
        /// <param name="expected">The expected type.</param>
        /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
        /// <param name="result">The match.</param>
        /// <returns>True if an attribute of the expected type was found.</returns>
        public static bool TryFind(this SyntaxList <AttributeListSyntax> attributeLists, QualifiedType expected, SemanticModel semanticModel, CancellationToken cancellationToken, out AttributeSyntax result)
        {
            result = null;
            foreach (var attributeList in attributeLists)
            {
                foreach (var attribute in attributeList.Attributes)
                {
                    if (IsType(attribute, expected, semanticModel, cancellationToken))
                    {
                        result = attribute;
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a <see cref="QualifiedParameter"/> with name only.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>A <see cref="QualifiedParameter"/>.</returns>
 public static QualifiedParameter Create(QualifiedType type) => new QualifiedParameter(null, type);
Ejemplo n.º 5
0
 public static bool IsType(AttributeSyntax attribute, QualifiedType expected, SemanticModel semanticModel, CancellationToken cancellationToken)
 {
     return(semanticModel.TryGetNamedType(attribute, expected, cancellationToken, out _));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Find the matching parameter for the argument.
 /// </summary>
 /// <param name="method">The <see cref="IMethodSymbol"/>.</param>
 /// <param name="type">The type of the parameter.</param>
 /// <returns><see cref="IParameterSymbol"/> if a matching parameter was found.</returns>
 public static IParameterSymbol?FindParameter(this IMethodSymbol method, QualifiedType type)
 {
     return(TryFindParameter(method, type, out var match) ? match : null);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Try getting the <see cref="IMethodSymbol"/> for the node.
        /// Gets the semantic model for the tree if the node is not in the tree corresponding to <paramref name="semanticModel"/>.
        /// </summary>
        /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
        /// <param name="node">The <see cref="ObjectCreationExpressionSyntax"/>.</param>
        /// <param name="expected">The expected method.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
        /// <param name="symbol">The symbol if found.</param>
        /// <returns>True if a symbol was found.</returns>
        public static bool TryGetSymbol(this SemanticModel semanticModel, ObjectCreationExpressionSyntax node, QualifiedType expected, CancellationToken cancellationToken, out IMethodSymbol symbol)
        {
            if (node.Type is SimpleNameSyntax typeName &&
                (typeName.Identifier.ValueText == expected.Type ||
                 AliasWalker.TryGet(node.SyntaxTree, typeName.Identifier.ValueText, out _)))
            {
                symbol = semanticModel.GetSymbolSafe(node, cancellationToken);
                return(symbol?.ContainingType == expected);
            }

            if (node.Type is QualifiedNameSyntax qualifiedName &&
                qualifiedName.Right.Identifier.ValueText == expected.Type)
            {
                symbol = semanticModel.GetSymbolSafe(node, cancellationToken);
                return(symbol?.ContainingType == expected);
            }

            symbol = null;
            return(false);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QualifiedArrayType"/> class.
 /// </summary>
 /// <param name="elementType">The element type.</param>
 public QualifiedArrayType(QualifiedType elementType)
     : base(elementType?.FullName + "[]", NamespaceParts, elementType?.Type + "[]")
 {
     this.ElementType = elementType ?? throw new ArgumentNullException(nameof(elementType));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QualifiedProperty"/> class.
 /// </summary>
 /// <param name="containingType">The containing type.</param>
 /// <param name="name">The name.</param>
 public QualifiedProperty(QualifiedType containingType, string name)
     : base(containingType, name)
 {
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Check if <paramref name="type"/> is assignable to <paramref name="qualifiedType1"/> or <paramref name="qualifiedType2"/>.
        /// </summary>
        /// <param name="type">The <see cref="ITypeSymbol"/>.</param>
        /// <param name="qualifiedType1">The first <see cref="QualifiedType"/>.</param>
        /// <param name="qualifiedType2">The second <see cref="QualifiedType"/>.</param>
        /// <param name="compilation">The <see cref="Compilation"/>.</param>
        /// <returns>True if <paramref name="type"/> is assignable to <paramref name="qualifiedType1"/> or <paramref name="qualifiedType2"/>.</returns>
        public static bool IsAssignableToEither(this ITypeSymbol type, QualifiedType qualifiedType1, QualifiedType qualifiedType2, Compilation compilation)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (qualifiedType1 is null)
            {
                throw new ArgumentNullException(nameof(qualifiedType1));
            }

            return(type.IsAssignableTo(qualifiedType1, compilation) ||
                   type.IsAssignableTo(qualifiedType2, compilation));
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QualifiedMethod"/> class.
 /// </summary>
 /// <param name="containingType">The containing type.</param>
 /// <param name="name">The name.</param>
 public QualifiedMethod(QualifiedType containingType, string name)
     : base(containingType, name)
 {
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Check if <paramref name="node"/> is <paramref name="type"/>.
 /// Optimized so that the stuff that can be checked in syntax mode is done before calling get symbol.
 /// </summary>
 /// <param name="node">The <see cref="ParameterSyntax"/>.</param>
 /// <param name="type">The <see cref="QualifiedType"/>.</param>
 /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
 /// <returns>True  if <paramref name="node"/> is <paramref name="type"/>.</returns>
 public static bool IsType(this ParameterSyntax node, QualifiedType type, SemanticModel semanticModel, CancellationToken cancellationToken)
 {
     return(node switch
     {
         { Type : IdentifierNameSyntax identifierName } => identifierName == type &&