コード例 #1
0
        internal static ConstructorsWalker Borrow(TypeDeclarationSyntax context, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            var walker = Borrow(() => new ConstructorsWalker());

            walker.semanticModel     = semanticModel;
            walker.cancellationToken = cancellationToken;
            walker.Visit(context);
            walker.type = semanticModel.GetDeclaredSymbolSafe(context, cancellationToken) as INamedTypeSymbol;
            if (walker.type == null)
            {
                return(walker);
            }

            foreach (var reference in walker.type.DeclaringSyntaxReferences)
            {
                walker.Visit(reference.GetSyntax(cancellationToken));
            }

            if (walker.nonPrivateCtors.Count == 0 &&
                walker.Default == null)
            {
                if (Constructor.TryGetDefault(walker.type, out var @default))
                {
                    foreach (var reference in @default.DeclaringSyntaxReferences)
                    {
                        walker.Default = (ConstructorDeclarationSyntax)reference.GetSyntax(cancellationToken);
                        walker.Visit(walker.Default);
                    }
                }
            }

            return(walker);
        }
コード例 #2
0
        public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            if (node.Initializer != null)
            {
                if (this.visitedLocations.Add(node.Initializer))
                {
                    var ctor = this.semanticModel.GetSymbolSafe(node.Initializer, this.cancellationToken);
                    this.HandleInvoke(ctor, node.Initializer.ArgumentList);
                }
            }
            else
            {
                var ctor = this.semanticModel.GetDeclaredSymbolSafe(node, this.cancellationToken);
                if (Constructor.TryGetDefault(ctor?.ContainingType?.BaseType, out var baseCtor))
                {
                    this.HandleInvoke(baseCtor, null);
                }
            }

            var contextCtor = this.context?.FirstAncestorOrSelf <ConstructorDeclarationSyntax>();

            if (contextCtor != null)
            {
                if (contextCtor == node ||
                    node.IsRunBefore(contextCtor, this.semanticModel, this.cancellationToken))
                {
                    base.VisitConstructorDeclaration(node);
                }
            }
            else
            {
                base.VisitConstructorDeclaration(node);
            }
        }
コード例 #3
0
        internal static Pool <ConstructorsWalker> .Pooled Create(TypeDeclarationSyntax context, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            var pooled = Pool.GetOrCreate();

            pooled.Item.semanticModel     = semanticModel;
            pooled.Item.cancellationToken = cancellationToken;
            pooled.Item.Visit(context);
            pooled.Item.type = semanticModel.GetDeclaredSymbolSafe(context, cancellationToken) as INamedTypeSymbol;
            if (pooled.Item.type == null)
            {
                return(pooled);
            }

            foreach (var reference in pooled.Item.type.DeclaringSyntaxReferences)
            {
                pooled.Item.Visit(reference.GetSyntax(cancellationToken));
            }

            if (pooled.Item.nonPrivateCtors.Count == 0 &&
                pooled.Item.Default == null)
            {
                if (Constructor.TryGetDefault(pooled.Item.type, out IMethodSymbol @default))
                {
                    foreach (var reference in @default.DeclaringSyntaxReferences)
                    {
                        pooled.Item.Default = (ConstructorDeclarationSyntax)reference.GetSyntax(cancellationToken);
                        pooled.Item.Visit(pooled.Item.Default);
                    }
                }
            }

            return(pooled);
        }