Add Python callback provider for custom unit resolution #976
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces a callback-based unit provider that lets Python applications implement custom unit resolution logic without requiring GPR project files or predefined file lists.
The existing providers (
for_projectandauto) work well for standard Ada projects, but many applications need dynamic resolution based on runtime configuration, database lookups, virtual file systems, or non-standard naming conventions.API
Implementation
The implementation spans three layers:
libadalang-callback_provider.adb/ads) — implementsUnit_Provider_Interface, manages callback invocation and memory ownership for returned filenameslibadalang-implementation-c-extensions.adb/ads) — provides C-compatible callback types, handles charset conversion, documents memory contractsextensions/python_api/unit_providers/methods) — wraps C callbacks, handlesmalloc/freefor string returns, retains callback references to prevent GCMemory ownership: Python allocates filename strings with
malloc(), Ada callsfree()after copying.NULLreturns indicate "unit not found".Exceptions in callbacks are logged via
_log_uncaught_errorand the callback returnsNULL, allowing analysis to continue.Limitations
PLE_Root_Index := 1)Testing
Tests cover basic resolution,
Nonereturns, exception handling, charset support (UTF-8 and default), multiple invocations, memory stress (110+ invocations), and Unicode unit names. All pass.Documentation
The user manual now documents
UnitProvider.from_callback()with a practical example, memory ownership contracts for C API users, and the limitations above.