public static bool DoesMethodUseTAsObject(IMethodSymbol method, SemanticModel semanticModel,
                                                  ITypeParameterSymbol typeParameter, IMethodSymbol[] relevantObjectMethods, KnownSymbols knownSymbols,
                                                  RecursiveStateForNotUsedAsObject recursiveStateForNotUsedAsObject)
        {
            if (recursiveStateForNotUsedAsObject.ItemsInStack.Contains((method, typeParameter)))
            {
                return(false);
            }

            recursiveStateForNotUsedAsObject = recursiveStateForNotUsedAsObject.Add(method, typeParameter);

            if (typeParameter.GetAttributes().Any(x => Utils.IsNotUsedAsObjectAttribute(x.AttributeClass.Name)))
            {
                return(false);
            }

            if (method.GetAttributes().Any(x =>
                                           Utils.IsDoesNotUseClassTypeParameterAsObjectAttributeForTypeParameter(x, typeParameter.Name)))
            {
                return(false);
            }

            if (method.IsInCode())
            {
                var location = method.Locations.First();

                var locationSourceTree = location.SourceTree;

                var methodSyntax = locationSourceTree.GetRoot().FindNode(location.SourceSpan);

                return(GetNodesWhereTIsUsedAsObject(methodSyntax, semanticModel, relevantObjectMethods, typeParameter,
                                                    knownSymbols, recursiveStateForNotUsedAsObject).Any());
            }

            if (typeParameter.DeclaringMethod != null)
            {
                if (knownSymbols.KnownNotUsedAsObjectMethodTypeParameters.TryGetValue(
                        Utils.GetFullMetaDataName(typeParameter.DeclaringMethod.ContainingType),
                        out var methods) &&
                    methods.Keys.FirstOrNoValue(x => x.Matches(typeParameter.DeclaringMethod))
                    .HasValueAnd(x => methods[x].Contains(typeParameter.Name)))
                {
                    return(false);
                }
            }
            else
            {
                if (knownSymbols.KnownNotUsedAsObjectClassTypeParameters.TryGetValue(
                        Utils.GetFullMetaDataName(typeParameter.DeclaringType),
                        out var methods) &&
                    methods.Contains(typeParameter.Name))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        private static TypeParameterSyntax GenerateTypeParameter(ITypeParameterSymbol symbol, CodeGenerationOptions options)
        {
            var varianceKeyword =
                symbol.Variance == VarianceKind.In ? SyntaxFactory.Token(SyntaxKind.InKeyword) :
                symbol.Variance == VarianceKind.Out ? SyntaxFactory.Token(SyntaxKind.OutKeyword) : default(SyntaxToken);

            return SyntaxFactory.TypeParameter(
                AttributeGenerator.GenerateAttributeLists(symbol.GetAttributes(), options),
                varianceKeyword,
                symbol.Name.ToIdentifierToken());
        }
Ejemplo n.º 3
0
        private static TypeParameterSyntax GenerateTypeParameter(ITypeParameterSymbol symbol, CodeGenerationOptions options)
        {
            var varianceKeyword =
                symbol.Variance == VarianceKind.In ? SyntaxFactory.Token(SyntaxKind.InKeyword) :
                symbol.Variance == VarianceKind.Out ? SyntaxFactory.Token(SyntaxKind.OutKeyword) : default;

            return(SyntaxFactory.TypeParameter(
                       AttributeGenerator.GenerateAttributeLists(symbol.GetAttributes(), options),
                       varianceKeyword,
                       symbol.Name.ToIdentifierToken()));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Construct a new TypeParameterApiView instance, represented by the provided symbol.
        /// </summary>
        /// <param name="symbol">The symbol representing the type parameter.</param>
        public TypeParameterApiView(ITypeParameterSymbol symbol)
        {
            this.Name = symbol.ToString();

            List <string> attributes = new List <string>();

            foreach (AttributeData attribute in symbol.GetAttributes())
            {
                attributes.Add(attribute.ToString());
            }
            this.Attributes = attributes.ToArray();
        }