📄 core/src/CalleBuilder.cs
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Cfa;

internal sealed class CalleeBuilder
{
    public static IImmutableDictionary<ISymbol, IImmutableList<Call>> FindCallees(
        IReadOnlyCollection<Compilation> compilations,
        ISymbol symbol,
        CancellationToken cancellationToken
    )
    {
        var builder = new CalleeBuilder(compilations);
        builder.FindCallees(symbol, cancellationToken);
        return builder.reachableFromSymbols.ToImmutable();
    }

    private readonly IReadOnlyCollection<Compilation> compilations;
    private readonly HashSet<ISymbol> seenMember = [with(SymbolEqualityComparer.Default)];
    private readonly ImmutableDictionary<ISymbol, IImmutableList<Call>>.Builder reachableFromSymbols =
        ImmutableDictionary.CreateBuilder<ISymbol, IImmutableList<Call>>(SymbolEqualityComparer.Default);

    private CalleeBuilder(IReadOnlyCollection<Compilation> compilations)
    {
        this.compilations = compilations;
    }

    private void FindCallees(ISymbol symbol, CancellationToken cancellationToken)
    {
        if (!seenMember.Add(symbol))
        {
            return;
        }

        var seenCall = new HashSet<ISymbol>(SymbolEqualityComparer.Default);
        var callees = ImmutableList.CreateBuilder<Call>();
        foreach (var reference in symbol.DeclaringSyntaxReferences)
        {
            var syntax = reference.GetSyntax(cancellationToken);
            var semanticModelForSyntax = compilations.GetSemanticModel(syntax.SyntaxTree);
            var accesses = syntax
                .DescendantNodesAndSelf()
                .Where(node =>
                    node is SimpleNameSyntax or ElementAccessExpressionSyntax or ElementBindingExpressionSyntax
                );
            foreach (var access in accesses)
            {
                var accessedSymbol = semanticModelForSyntax.GetSymbolInfo(access, cancellationToken).Symbol;
                if (
                    accessedSymbol is not (IMethodSymbol or IPropertySymbol)
                    || !seenCall.Add(accessedSymbol)
                    || !IsDeclaredInSource(accessedSymbol)
                )
                {
                    continue;
                }

                if (accessedSymbol is IPropertySymbol property && !HasBody(property, cancellationToken))
                {
                    continue;
                }

                callees.Add(new Call(accessedSymbol, LinkKind.Direct));
                FindCallees(accessedSymbol, cancellationToken);
            }
        }
        reachableFromSymbols.Add(symbol, callees.ToImmutable());
    }

    private static bool IsDeclaredInSource(ISymbol symbol) =>
        symbol.DeclaringSyntaxReferences.Length > 0 && !symbol.IsImplicitlyDeclared;

    private static bool HasBody(IPropertySymbol property, CancellationToken cancellationToken) =>
        property.DeclaringSyntaxReferences.Any(reference =>
            reference.GetSyntax(cancellationToken) switch
            {
                PropertyDeclarationSyntax { ExpressionBody: not null } => true,
                IndexerDeclarationSyntax { ExpressionBody: not null } => true,
                BasePropertyDeclarationSyntax declaration => declaration.AccessorList?.Accessors.Any(accessor =>
                    accessor.Body is not null || accessor.ExpressionBody is not null
                )
                    is true,
                _ => false,
            }
        );
}