From daf1aa4937c4eec4bfda82829e22bb07a261eb70 Mon Sep 17 00:00:00 2001 From: Pierre-Marie de Rodat Date: Thu, 20 Jun 2024 13:40:14 +0000 Subject: [PATCH] Generic API: bind the Lookup function for nodes --- .../langkit_support-generic_api-analysis.adb | 16 ++++++++++++++++ .../langkit_support-generic_api-analysis.ads | 6 ++++++ .../langkit_support-internal-descriptor.ads | 4 ++++ langkit/templates/pkg_generic_impl_body_ada.mako | 11 +++++++++++ langkit/templates/pkg_generic_impl_spec_ada.mako | 4 ++++ testsuite/tests/ada_api/generic_api/analysis.adb | 5 +++++ testsuite/tests/ada_api/generic_api/test.out | 3 +++ 7 files changed, 49 insertions(+) diff --git a/langkit/support/langkit_support-generic_api-analysis.adb b/langkit/support/langkit_support-generic_api-analysis.adb index 8614dd778..782b6df2f 100644 --- a/langkit/support/langkit_support-generic_api-analysis.adb +++ b/langkit/support/langkit_support-generic_api-analysis.adb @@ -1149,6 +1149,22 @@ package body Langkit_Support.Generic_API.Analysis is return Self.Desc.Node_Sloc_Range (Self.Internal.Node); end Sloc_Range; + ------------ + -- Lookup -- + ------------ + + function Lookup + (Self : Lk_Node'Class; Sloc : Source_Location) return Lk_Node is + begin + Check_Safety_Net (Self); + if Self.Is_Null then + return No_Lk_Node; + end if; + + return Wrap_Node + (Self.Desc.Node_Lookup (Self.Internal.Node, Sloc), Self); + end Lookup; + ------------------- -- Is_Incomplete -- ------------------- diff --git a/langkit/support/langkit_support-generic_api-analysis.ads b/langkit/support/langkit_support-generic_api-analysis.ads index 15f42f81b..0d2925295 100644 --- a/langkit/support/langkit_support-generic_api-analysis.ads +++ b/langkit/support/langkit_support-generic_api-analysis.ads @@ -333,6 +333,12 @@ package Langkit_Support.Generic_API.Analysis is -- -- Note that this returns the sloc of the parent for synthetic nodes. + function Lookup + (Self : Lk_Node'Class; Sloc : Source_Location) return Lk_Node; + -- Look for the bottom-most node in the ``Self`` subtree whose sloc range + -- contains ``Sloc``. Return it, or ``No_Lk_Node`` if no such node was + -- found. + -- TODO??? Bind all other node primitives function Is_Incomplete (Self : Lk_Node) return Boolean; diff --git a/langkit/support/langkit_support-internal-descriptor.ads b/langkit/support/langkit_support-internal-descriptor.ads index 4e45e0af0..b5473eb39 100644 --- a/langkit/support/langkit_support-internal-descriptor.ads +++ b/langkit/support/langkit_support-internal-descriptor.ads @@ -109,6 +109,9 @@ package Langkit_Support.Internal.Descriptor is (Node : Analysis.Internal_Node) return Text_Type; type Node_Sloc_Range_Type is access function (Node : Analysis.Internal_Node) return Source_Location_Range; + type Node_Lookup_Type is access function + (Node : Analysis.Internal_Node; + Sloc : Source_Location) return Analysis.Internal_Node; type Node_Last_Attempted_Child_Type is access function (Node : Analysis.Internal_Node) return Integer; @@ -217,6 +220,7 @@ package Langkit_Support.Internal.Descriptor is Node_Token_End : Node_Token_Getter_Type; Node_Text : Node_Text_Type; Node_Sloc_Range : Node_Sloc_Range_Type; + Node_Lookup : Node_Lookup_Type; Node_Last_Attempted_Child : Node_Last_Attempted_Child_Type; Entity_Image : Entity_Image_Type; diff --git a/langkit/templates/pkg_generic_impl_body_ada.mako b/langkit/templates/pkg_generic_impl_body_ada.mako index 06b60abf6..2e4081fd0 100644 --- a/langkit/templates/pkg_generic_impl_body_ada.mako +++ b/langkit/templates/pkg_generic_impl_body_ada.mako @@ -465,6 +465,17 @@ package body ${ada_lib_name}.Generic_Impl is return Implementation.Sloc_Range (+Node); end Node_Sloc_Range; + ----------------- + -- Node_Lookup -- + ----------------- + + function Node_Lookup + (Node : Analysis.Internal_Node; + Sloc : Source_Location) return Analysis.Internal_Node is + begin + return +Implementation.Lookup (+Node, Sloc); + end Node_Lookup; + ------------------------------- -- Node_Last_Attempted_Child -- ------------------------------- diff --git a/langkit/templates/pkg_generic_impl_spec_ada.mako b/langkit/templates/pkg_generic_impl_spec_ada.mako index 4af55a200..717797134 100644 --- a/langkit/templates/pkg_generic_impl_spec_ada.mako +++ b/langkit/templates/pkg_generic_impl_spec_ada.mako @@ -180,6 +180,9 @@ private package ${ada_lib_name}.Generic_Impl is function Node_Text (Node : Internal_Node) return Text_Type; function Node_Sloc_Range (Node : Internal_Node) return Source_Location_Range; + function Node_Lookup + (Node : Analysis.Internal_Node; + Sloc : Source_Location) return Analysis.Internal_Node; function Node_Last_Attempted_Child (Node : Internal_Node) return Integer; function Entity_Image (Entity : Internal_Entity) return String; @@ -267,6 +270,7 @@ private package ${ada_lib_name}.Generic_Impl is Node_Token_End => Node_Token_End'Access, Node_Text => Node_Text'Access, Node_Sloc_Range => Node_Sloc_Range'Access, + Node_Lookup => Node_Lookup'Access, Node_Last_Attempted_Child => Node_Last_Attempted_Child'Access, Entity_Image => Entity_Image'Access, diff --git a/testsuite/tests/ada_api/generic_api/analysis.adb b/testsuite/tests/ada_api/generic_api/analysis.adb index be496fd99..31d637dbf 100644 --- a/testsuite/tests/ada_api/generic_api/analysis.adb +++ b/testsuite/tests/ada_api/generic_api/analysis.adb @@ -330,6 +330,11 @@ begin end; New_Line; + Put_Line + ("No_Lk_Node.Lookup (1, 1) -> " & Image (No_Lk_Node.Lookup ((1, 1)))); + Put_Line ("Root.Lookup (2, 7) -> " & Image (U.Root.Lookup ((2, 7)))); + New_Line; + Put_Line ("Root.Is_Incomplete -> " & U.Root.Is_Incomplete'Image); Put ("No_Lk_Node.Is_Incomplete -> "); declare diff --git a/testsuite/tests/ada_api/generic_api/test.out b/testsuite/tests/ada_api/generic_api/test.out index c57359b13..992ffd6e6 100644 --- a/testsuite/tests/ada_api/generic_api/test.out +++ b/testsuite/tests/ada_api/generic_api/test.out @@ -151,6 +151,9 @@ No_Lk_Node.Text -> Got a Precondition_Failure exception: null node Root.Sloc_Range -> 1:1-3:15 No_Lk_Node.Sloc_Range -> Got a Precondition_Failure exception: null node +No_Lk_Node.Lookup (1, 1) -> None +Root.Lookup (2, 7) -> + Root.Is_Incomplete -> FALSE No_Lk_Node.Is_Incomplete -> Got a Precondition_Failure exception: null node