@@ -29,6 +29,19 @@ package body Instrument.C_Utils is
2929
3030 use Cursor_Vectors;
3131
32+ function Visitor_Wrapper
33+ (Node : Cursor_T;
34+ Parent : Cursor_T;
35+ Client_Data : Client_Data_T) return Child_Visit_Result_T
36+ with Convention => C;
37+ -- Helper for Visit_Children and Visit procedures.
38+ --
39+ -- Interpret Client_Data as the Visitor anonymous function for these
40+ -- procedures and call it on Node.
41+ --
42+ -- Note: this has convention C in order to be a callback for the
43+ -- Clang.Index.Visit_Children and Clang.Index.Visit functions.
44+
3245 -- --------
3346 -- Sloc --
3447 -- --------
@@ -78,42 +91,34 @@ package body Instrument.C_Utils is
7891 return Cursor_Is_Null (N);
7992 end Is_Null ;
8093
94+ -- -------------------
95+ -- Visitor_Wrapper --
96+ -- -------------------
97+
98+ function Visitor_Wrapper
99+ (Node : Cursor_T;
100+ Parent : Cursor_T;
101+ Client_Data : Client_Data_T) return Child_Visit_Result_T
102+ is
103+ pragma Unreferenced (Parent);
104+ Callback : access function
105+ (Node : Cursor_T) return Child_Visit_Result_T
106+ with Import, Address => System.Address (Client_Data);
107+ begin
108+ return Callback.all (Node);
109+ end Visitor_Wrapper ;
110+
81111 -- ------------------
82112 -- Visit_Children --
83113 -- ------------------
84114
85115 procedure Visit_Children
86- (Parent : Cursor_T; Visitor : Cursor_Visitor_Function)
116+ (Parent : Cursor_T;
117+ Visitor : not null access function
118+ (Node : Cursor_T) return Child_Visit_Result_T)
87119 is
88- function Visitor_Wrapper
89- (Node : Cursor_T;
90- Parent : Cursor_T;
91- Client_Data : Client_Data_T) return Child_Visit_Result_T
92- with Convention => C;
93- -- Callback for Clang.Index.Visit_Children. Just a wrapper around
94- -- Visitor.
95-
96- -- -------------------
97- -- Visitor_Wrapper --
98- -- -------------------
99-
100- function Visitor_Wrapper
101- (Node : Cursor_T;
102- Parent : Cursor_T;
103- Client_Data : Client_Data_T) return Child_Visit_Result_T
104- is
105- pragma Unreferenced (Parent, Client_Data);
106- begin
107- return Visitor (Node);
108- end Visitor_Wrapper ;
109-
110120 Dummy : constant unsigned := Visit_Children
111- (Parent,
112- Visitor_Wrapper'Unrestricted_Access,
113- Client_Data_T (System.Null_Address));
114-
115- -- Start of processing for Visit_Children
116-
121+ (Parent, Visitor_Wrapper'Access , Client_Data_T (Visitor'Address));
117122 begin
118123 null ;
119124 end Visit_Children ;
@@ -122,36 +127,13 @@ package body Instrument.C_Utils is
122127 -- Visit --
123128 -- ---------
124129
125- procedure Visit (Parent : Cursor_T; Visitor : Cursor_Visitor_Function) is
126- function Visitor_Wrapper
127- (Node : Cursor_T;
128- Parent : Cursor_T;
129- Client_Data : Client_Data_T) return Child_Visit_Result_T
130- with Convention => C;
131- -- Callback for Clang.Index.Visit_Children. Just a wrapper around
132- -- Visitor.
133-
134- -- -------------------
135- -- Visitor_Wrapper --
136- -- -------------------
137-
138- function Visitor_Wrapper
139- (Node : Cursor_T;
140- Parent : Cursor_T;
141- Client_Data : Client_Data_T) return Child_Visit_Result_T
142- is
143- pragma Unreferenced (Parent, Client_Data);
144- begin
145- return Visitor (Node);
146- end Visitor_Wrapper ;
147-
130+ procedure Visit
131+ (Parent : Cursor_T;
132+ Visitor : not null access function
133+ (Node : Cursor_T) return Child_Visit_Result_T)
134+ is
148135 Dummy : constant unsigned := Visit
149- (Parent,
150- Visitor_Wrapper'Unrestricted_Access,
151- Client_Data_T (System.Null_Address));
152-
153- -- Start of processing for Visit
154-
136+ (Parent, Visitor_Wrapper'Access , Client_Data_T (Visitor'Address));
155137 begin
156138 null ;
157139 end Visit ;
@@ -163,8 +145,7 @@ package body Instrument.C_Utils is
163145 function Get_Children (N : Cursor_T) return Cursor_Vectors.Vector is
164146 Res : Vector;
165147
166- function Append_Child (Cursor : Cursor_T) return Child_Visit_Result_T
167- with Convention => C;
148+ function Append_Child (Cursor : Cursor_T) return Child_Visit_Result_T;
168149 -- Callback for Visit_Children. Append Cursor to Res and continue the
169150 -- traversal.
170151
@@ -181,7 +162,7 @@ package body Instrument.C_Utils is
181162 -- Start of processing for Get_Children
182163
183164 begin
184- Visit_Children (N, Get_Children.Append_Child'Unrestricted_Access );
165+ Visit_Children (N, Get_Children.Append_Child'Access );
185166 return Res;
186167 end Get_Children ;
187168
@@ -192,8 +173,7 @@ package body Instrument.C_Utils is
192173 function Get_Lambda_Exprs (N : Cursor_T) return Cursor_Vectors.Vector is
193174 Res : Vector;
194175
195- function Process (Cursor : Cursor_T) return Child_Visit_Result_T
196- with Convention => C;
176+ function Process (Cursor : Cursor_T) return Child_Visit_Result_T;
197177 -- Helper for Visit_Children. Add every lambda expr under Cursor to Res,
198178 -- _but_ the lambda expressions nested in other lambda expressions.
199179
@@ -213,7 +193,7 @@ package body Instrument.C_Utils is
213193 -- Start of processing for Get_Lambda_Exprs
214194
215195 begin
216- Visit_Children (N, Process'Unrestricted_Access );
196+ Visit_Children (N, Process'Access );
217197 return Res;
218198 end Get_Lambda_Exprs ;
219199
@@ -263,8 +243,7 @@ package body Instrument.C_Utils is
263243 function Get_Main (TU : Translation_Unit_T) return Cursor_T is
264244 Result : Cursor_T := Get_Null_Cursor;
265245
266- function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
267- with Convention => C;
246+ function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T;
268247 -- Callback for Visit_Children. Set Result and break the iteration if
269248 -- Cursor is the "main" function definition, continue the iteration to
270249 -- find the main otherwise.
@@ -289,7 +268,7 @@ package body Instrument.C_Utils is
289268
290269 begin
291270 Visit_Children (Parent => Get_Translation_Unit_Cursor (TU),
292- Visitor => Visit_Decl'Unrestricted_Access );
271+ Visitor => Visit_Decl'Access );
293272 return Result;
294273 end Get_Main ;
295274
@@ -304,8 +283,7 @@ package body Instrument.C_Utils is
304283 Rew : Rewriter_T;
305284 Statement : String)
306285 is
307- function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T
308- with Convention => C;
286+ function Visit_Decl (Cursor : Cursor_T) return Child_Visit_Result_T;
309287 -- Traverse the tree until the main function is found, and insert a
310288 -- statement.
311289
@@ -350,7 +328,7 @@ package body Instrument.C_Utils is
350328
351329 begin
352330 Visit_Children (Parent => Get_Translation_Unit_Cursor (TU),
353- Visitor => Visit_Decl'Unrestricted_Access );
331+ Visitor => Visit_Decl'Access );
354332 end Add_Statement_In_Main ;
355333
356334 -- ------------------------------
0 commit comments