diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs
index c52abfae..92d122e3 100644
--- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs
+++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs
@@ -1,3 +1,4 @@
+using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis;
@@ -155,4 +156,24 @@ internal static class ISymbolExtensions
return candidateNode is not null;
}
+
+ ///
+ /// Gets the first symbol of a specific type among the ancestors of a given symbol.
+ ///
+ /// The input symbol to start the search from.
+ /// An optional predicate to filter symbols.
+ /// The resulting symbol, if a match was found.
+ public static TSymbol? FirstAncestorOrSelf(this ISymbol symbol, Func? predicate = null)
+ where TSymbol : class, ISymbol
+ {
+ for (ISymbol? parentSymbol = symbol; parentSymbol is not null; parentSymbol = parentSymbol.ContainingSymbol)
+ {
+ if (parentSymbol is TSymbol targetSymbol && (predicate?.Invoke(targetSymbol) is not false))
+ {
+ return targetSymbol;
+ }
+ }
+
+ return null;
+ }
}
\ No newline at end of file