Debian Codegraph @

gcc/ada/sinfo-utils.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S I N F O . U T I L S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2020-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with GNAT.Lists;
29 with Output; use Output;
30 with Seinfo;
31 with Sinput; use Sinput;
32
33 package body Sinfo.Utils is
34
35 function Spec_Lib_Unit
36 (N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id
37 is
38 pragma Assert (Unit (N) in N_Lib_Unit_Body_Id);
39 begin
40 return Val : constant Opt_N_Compilation_Unit_Id :=
41 Spec_Or_Body_Lib_Unit (N)
42 do
43 pragma Assert
44 (if Present (Val) then
45 Unit (Val) in N_Lib_Unit_Declaration_Id
46 | N_Lib_Unit_Renaming_Declaration_Id -- only in case of error
47 or else (N = Val
48 and then Unit (N) in N_Subprogram_Body_Id
49 and then Acts_As_Spec (N)));
50 end return;
51 end Spec_Lib_Unit;
52
53 procedure Set_Spec_Lib_Unit (N, Val : N_Compilation_Unit_Id) is
54 pragma Assert (Unit (N) in N_Lib_Unit_Body_Id);
55 pragma Assert
56 (Unit (Val) in N_Lib_Unit_Declaration_Id
57 | N_Lib_Unit_Renaming_Declaration_Id -- only in case of error
58 or else (N = Val
59 and then Unit (N) in N_Subprogram_Body_Id
60 and then Acts_As_Spec (N)));
61 begin
62 Set_Library_Unit (N, Val);
63 end Set_Spec_Lib_Unit;
64
65 function Body_Lib_Unit
66 (N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id
67 is
68 pragma Assert
69 (Unit (N) in N_Lib_Unit_Declaration_Id
70 | N_Lib_Unit_Renaming_Declaration_Id); -- only in case of error
71 begin
72 return Val : constant Opt_N_Compilation_Unit_Id :=
73 Spec_Or_Body_Lib_Unit (N)
74 do
75 pragma Assert
76 (if Present (Val) then Unit (Val) in N_Lib_Unit_Body_Id);
77 end return;
78 end Body_Lib_Unit;
79
80 procedure Set_Body_Lib_Unit (N, Val : N_Compilation_Unit_Id) is
81 pragma Assert
82 (Unit (N) in N_Lib_Unit_Declaration_Id
83 | N_Lib_Unit_Renaming_Declaration_Id); -- only in case of error
84 pragma Assert (Unit (Val) in N_Lib_Unit_Body_Id);
85 begin
86 Set_Library_Unit (N, Val);
87 end Set_Body_Lib_Unit;
88
89 function Spec_Or_Body_Lib_Unit
90 (N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id
91 is
92 pragma Assert
93 (Unit (N) in
94 N_Lib_Unit_Declaration_Id | N_Lib_Unit_Body_Id
95 | N_Lib_Unit_Renaming_Declaration_Id);
96 begin
97 return Other_Comp_Unit (N);
98 end Spec_Or_Body_Lib_Unit;
99
100 function Subunit_Parent
101 (N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id
102 is
103 pragma Assert (Unit (N) in N_Subunit_Id);
104 begin
105 return Val : constant Opt_N_Compilation_Unit_Id := Other_Comp_Unit (N) do
106 pragma Assert
107 (if Present (Val) then
108 Unit (Val) in N_Lib_Unit_Body_Id | N_Subunit_Id);
109 end return;
110 end Subunit_Parent;
111
112 procedure Set_Subunit_Parent (N, Val : N_Compilation_Unit_Id) is
113 pragma Assert (Unit (N) in N_Subunit_Id);
114 pragma Assert (Unit (Val) in N_Lib_Unit_Body_Id | N_Subunit_Id);
115 begin
116 Set_Library_Unit (N, Val);
117 end Set_Subunit_Parent;
118
119 function Other_Comp_Unit
120 (N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id
121 is
122 pragma Assert (N in N_Compilation_Unit_Id);
123 Val : constant Opt_N_Compilation_Unit_Id := Library_Unit (N);
124 begin
125 if Unit (N) in N_Subunit_Id then
126 pragma Assert
127 (if Present (Val) then
128 Unit (Val) in N_Lib_Unit_Body_Id | N_Subunit_Id);
129 end if;
130
131 return Library_Unit (N);
132 end Other_Comp_Unit;
133
134 function Stub_Subunit
135 (N : N_Body_Stub_Id) return Opt_N_Compilation_Unit_Id is
136 begin
137 return Val : constant Opt_N_Compilation_Unit_Id := Library_Unit (N) do
138 pragma Assert (if Present (Val) then Unit (Val) in N_Subunit_Id);
139 end return;
140 end Stub_Subunit;
141
142 procedure Set_Stub_Subunit
143 (N : N_Body_Stub_Id; Val : N_Compilation_Unit_Id)
144 is
145 pragma Assert (Unit (Val) in N_Subunit_Id);
146 begin
147 Set_Library_Unit (N, Val);
148 end Set_Stub_Subunit;
149
150 function Withed_Lib_Unit
151 (N : N_With_Clause_Id) return Opt_N_Compilation_Unit_Id is
152 begin
153 return Val : constant Opt_N_Compilation_Unit_Id := Library_Unit (N) do
154 pragma Assert
155 (if Present (Val) then
156 Unit (Val) in N_Lib_Unit_Declaration_Id
157 | N_Lib_Unit_Renaming_Declaration_Id
158 | N_Package_Body_Id | N_Subprogram_Body_Id
159 | N_Null_Statement_Id); -- for ignored ghost code
160 end return;
161 end Withed_Lib_Unit;
162
163 procedure Set_Withed_Lib_Unit
164 (N : N_With_Clause_Id; Val : N_Compilation_Unit_Id)
165 is
166 pragma Assert
167 (Unit (Val) in N_Lib_Unit_Declaration_Id
168 | N_Lib_Unit_Renaming_Declaration_Id
169 | N_Package_Body_Id | N_Subprogram_Body_Id);
170 begin
171 Set_Library_Unit (N, Val);
172 end Set_Withed_Lib_Unit;
173
174 ---------------
175 -- Debugging --
176 ---------------
177
178 -- Suppose you find that node 12345 is messed up. You might want to find
179 -- the code that created that node. There are two ways to do this:
180
181 -- One way is to set a conditional breakpoint on New_Node_Debugging_Output
182 -- (nickname "nnd"):
183 -- break nnd if n = 12345
184 -- and run gnat1 again from the beginning.
185
186 -- The other way is to set a breakpoint near the beginning (e.g. on
187 -- gnat1drv), and run. Then set Watch_Node (nickname "ww") to 12345 in gdb:
188 -- ww := 12345
189 -- and set a breakpoint on New_Node_Breakpoint (nickname "nn"). Continue.
190
191 -- Either way, gnat1 will stop when node 12345 is created, or certain other
192 -- interesting operations are performed, such as Rewrite. To see exactly
193 -- which operations, search for "New_Node_Debugging_Output" in Atree.
194
195 -- The second method is much faster if the amount of Ada code being
196 -- compiled is large.
197
198 ww : Node_Id'Base := Node_Low_Bound - 1;
199 pragma Export (Ada, ww);
200 Watch_Node : Node_Id'Base renames ww;
201 -- Node to "watch"; that is, whenever a node is created, we check if it
202 -- is equal to Watch_Node, and if so, call New_Node_Breakpoint. You have
203 -- presumably set a breakpoint on New_Node_Breakpoint. Note that the
204 -- initial value of Node_Id'First - 1 ensures that by default, no node
205 -- will be equal to Watch_Node.
206
207 procedure nn;
208 pragma Export (Ada, nn);
209 procedure New_Node_Breakpoint renames nn;
210 -- This doesn't do anything interesting; it's just for setting breakpoint
211 -- on as explained above.
212
213 procedure nnd (N : Node_Id);
214 pragma Export (Ada, nnd);
215 -- For debugging. If debugging is turned on, New_Node and New_Entity (etc.)
216 -- call this. If debug flag N is turned on, this prints out the new node.
217 --
218 -- If Node = Watch_Node, this prints out the new node and calls
219 -- New_Node_Breakpoint. Otherwise, does nothing.
220
221 procedure Node_Debug_Output (Op : String; N : Node_Id);
222 -- Called by nnd; writes Op followed by information about N
223
224 -------------------------
225 -- New_Node_Breakpoint --
226 -------------------------
227
228 procedure nn is
229 begin
230 Write_Str ("Watched node ");
231 Write_Int (Int (Watch_Node));
232 Write_Eol;
233 end nn;
234
235 -------------------------------
236 -- New_Node_Debugging_Output --
237 -------------------------------
238
239 procedure nnd (N : Node_Id) is
240 Node_Is_Watched : constant Boolean := N = Watch_Node;
241
242 begin
243 if Debug_Flag_N or else Node_Is_Watched then
244 Node_Debug_Output ("Node", N);
245
246 if Node_Is_Watched then
247 New_Node_Breakpoint;
248 end if;
249 end if;
250 end nnd;
251
252 procedure New_Node_Debugging_Output (N : Node_Id) is
253 begin
254 pragma Debug (nnd (N));
255 end New_Node_Debugging_Output;
256
257 -----------------------
258 -- Node_Debug_Output --
259 -----------------------
260
261 procedure Node_Debug_Output (Op : String; N : Node_Id) is
262 begin
263 Write_Str (Op);
264
265 if Nkind (N) in N_Entity then
266 Write_Str (" entity");
267 else
268 Write_Str (" node");
269 end if;
270
271 Write_Str (" Id = ");
272 Write_Int (Int (N));
273 Write_Str (" ");
274 Write_Location (Sloc (N));
275 Write_Str (" ");
276 Write_Str (Node_Kind'Image (Nkind (N)));
277 Write_Eol;
278 end Node_Debug_Output;
279
280 -------------------------------
281 -- Parent-related operations --
282 -------------------------------
283
284 procedure Copy_Parent (To, From : Node_Or_Entity_Id) is
285 begin
286 if Atree.Present (To) and Atree.Present (From) then
287 Atree.Set_Parent (To, Atree.Parent (From));
288 else
289 pragma Assert
290 (if Atree.Present (To) then Atree.No (Atree.Parent (To)));
291 end if;
292 end Copy_Parent;
293
294 function Parent_Kind (N : Node_Id) return Node_Kind is
295 begin
296 if Atree.No (N) then
297 return N_Empty;
298 else
299 return Nkind (Atree.Parent (N));
300 end if;
301 end Parent_Kind;
302
303 -------------------------
304 -- Iterator Procedures --
305 -------------------------
306
307 procedure Next_Entity (N : in out Node_Id) is
308 begin
309 N := Next_Entity (N);
310 end Next_Entity;
311
312 procedure Next_Named_Actual (N : in out Node_Id) is
313 begin
314 N := Next_Named_Actual (N);
315 end Next_Named_Actual;
316
317 procedure Next_Rep_Item (N : in out Node_Id) is
318 begin
319 N := Next_Rep_Item (N);
320 end Next_Rep_Item;
321
322 procedure Next_Use_Clause (N : in out Node_Id) is
323 begin
324 N := Next_Use_Clause (N);
325 end Next_Use_Clause;
326
327 ------------------
328 -- End_Location --
329 ------------------
330
331 function End_Location (N : Node_Id) return Source_Ptr is
332 L : constant Valid_Uint := End_Span (N);
333 begin
334 return Sloc (N) + Source_Ptr (UI_To_Int (L));
335 end End_Location;
336
337 --------------------
338 -- Get_Pragma_Arg --
339 --------------------
340
341 function Get_Pragma_Arg (Arg : Node_Id) return Node_Id is
342 begin
343 if Nkind (Arg) = N_Pragma_Argument_Association then
344 return Expression (Arg);
345 else
346 return Arg;
347 end if;
348 end Get_Pragma_Arg;
349
350 procedure Destroy_Element (Elem : in out Union_Id);
351 -- Does not do anything but is used to instantiate
352 -- GNAT.Lists.Doubly_Linked_Lists.
353
354 ---------------------
355 -- Destroy_Element --
356 ---------------------
357
358 procedure Destroy_Element (Elem : in out Union_Id) is
359 begin
360 null;
361 end Destroy_Element;
362
363 package Lists is
364 new GNAT.Lists.Doubly_Linked_Lists
365 (Element_Type => Union_Id, "=" => "=",
366 Destroy_Element => Destroy_Element, Check_Tampering => False);
367
368 ----------------------------
369 -- Lowest_Common_Ancestor --
370 ----------------------------
371
372 function Lowest_Common_Ancestor (N1, N2 : Node_Id) return Union_Id is
373 function Path_From_Root (N : Node_Id) return Lists.Doubly_Linked_List;
374
375 --------------------
376 -- Path_From_Root --
377 --------------------
378
379 function Path_From_Root (N : Node_Id) return Lists.Doubly_Linked_List is
380 L : constant Lists.Doubly_Linked_List := Lists.Create;
381
382 X : Union_Id := Union_Id (N);
383 begin
384 while X /= Union_Id (Empty) loop
385 Lists.Prepend (L, X);
386 X := Parent_Or_List_Containing (X);
387 end loop;
388
389 return L;
390 end Path_From_Root;
391
392 L1 : Lists.Doubly_Linked_List := Path_From_Root (N1);
393 L2 : Lists.Doubly_Linked_List := Path_From_Root (N2);
394
395 X1, X2 : Union_Id;
396
397 Common_Ancestor : Union_Id := Union_Id (Empty);
398 begin
399 while not Lists.Is_Empty (L1) and then not Lists.Is_Empty (L2) loop
400 X1 := Lists.First (L1);
401 Lists.Delete_First (L1);
402
403 X2 := Lists.First (L2);
404 Lists.Delete_First (L2);
405
406 exit when X1 /= X2;
407
408 Common_Ancestor := X1;
409 end loop;
410
411 Lists.Destroy (L1);
412 Lists.Destroy (L2);
413
414 return Common_Ancestor;
415 end Lowest_Common_Ancestor;
416
417 ----------------------
418 -- Set_End_Location --
419 ----------------------
420
421 procedure Set_End_Location (N : Node_Id; S : Source_Ptr) is
422 begin
423 Set_End_Span (N,
424 UI_From_Int (Int (S - Sloc (N))));
425 end Set_End_Location;
426
427 --------------------------
428 -- Pragma_Name_Unmapped --
429 --------------------------
430
431 function Pragma_Name_Unmapped (N : Node_Id) return Name_Id is
432 begin
433 return Chars (Pragma_Identifier (N));
434 end Pragma_Name_Unmapped;
435
436 ------------------------------------
437 -- Helpers for Walk_Sinfo_Fields* --
438 ------------------------------------
439
440 function Get_Node_Field_Union is new
441 Atree.Atree_Private_Part.Get_32_Bit_Field (Union_Id) with Inline;
442 procedure Set_Node_Field_Union is new
443 Atree.Atree_Private_Part.Set_32_Bit_Field (Union_Id) with Inline;
444
445 use Seinfo;
446
447 function Is_In_Union_Id (F_Kind : Field_Kind) return Boolean is
448 -- True if the field type is one that can be converted to Types.Union_Id
449 (case F_Kind is
450 when Node_Id_Field
451 | List_Id_Field
452 | Elist_Id_Field
453 | Name_Id_Field
454 | String_Id_Field
455 | Valid_Uint_Field
456 | Unat_Field
457 | Upos_Field
458 | Nonzero_Uint_Field
459 | Uint_Field
460 | Ureal_Field
461 | Union_Id_Field => True,
462 when Flag_Field
463 | Node_Kind_Type_Field
464 | Entity_Kind_Type_Field
465 | Source_File_Index_Field
466 | Source_Ptr_Field
467 | Small_Paren_Count_Type_Field
468 | Convention_Id_Field
469 | Component_Alignment_Kind_Field
470 | Mechanism_Type_Field => False);
471
472 -----------------------
473 -- Walk_Sinfo_Fields --
474 -----------------------
475
476 procedure Walk_Sinfo_Fields (N : Node_Id) is
477 Fields : Node_Field_Array renames
478 Node_Field_Table (Nkind (N)).all;
479
480 begin
481 for J in Fields'Range loop
482 if Fields (J) /= F_Link then -- Don't walk Parent!
483 declare
484 Desc : Field_Descriptor renames
485 Field_Descriptors (Fields (J));
486 pragma Assert (Desc.Type_Only = No_Type_Only);
487 -- Type_Only is for entities
488 begin
489 if Is_In_Union_Id (Desc.Kind) then
490 Action (Get_Node_Field_Union (N, Desc.Offset));
491 end if;
492 end;
493 end if;
494 end loop;
495 end Walk_Sinfo_Fields;
496
497 --------------------------------
498 -- Walk_Sinfo_Fields_Pairwise --
499 --------------------------------
500
501 procedure Walk_Sinfo_Fields_Pairwise (N1, N2 : Node_Id) is
502 pragma Assert (Nkind (N1) = Nkind (N2));
503
504 Fields : Node_Field_Array renames
505 Node_Field_Table (Nkind (N1)).all;
506
507 begin
508 for J in Fields'Range loop
509 if Fields (J) /= F_Link then -- Don't walk Parent!
510 declare
511 Desc : Field_Descriptor renames
512 Field_Descriptors (Fields (J));
513 pragma Assert (Desc.Type_Only = No_Type_Only);
514 -- Type_Only is for entities
515 begin
516 if Is_In_Union_Id (Desc.Kind) then
517 Set_Node_Field_Union
518 (N1, Desc.Offset,
519 Transform (Get_Node_Field_Union (N2, Desc.Offset)));
520 end if;
521 end;
522 end if;
523 end loop;
524 end Walk_Sinfo_Fields_Pairwise;
525
526 ---------------------
527 -- Map_Pragma_Name --
528 ---------------------
529
530 -- We don't want to introduce a dependence on some hash table package or
531 -- similar, so we use a simple array of Key => Value pairs, and do a linear
532 -- search. Linear search is plenty efficient, given that we don't expect
533 -- more than a couple of entries in the mapping.
534
535 type Name_Pair is record
536 Key : Name_Id;
537 Value : Name_Id;
538 end record;
539
540 type Pragma_Map_Index is range 1 .. 100;
541 Pragma_Map : array (Pragma_Map_Index) of Name_Pair;
542 Last_Pair : Pragma_Map_Index'Base range 0 .. Pragma_Map_Index'Last := 0;
543
544 procedure Map_Pragma_Name (From, To : Name_Id) is
545 begin
546 if Last_Pair = Pragma_Map'Last then
547 raise Too_Many_Pragma_Mappings;
548 end if;
549
550 Last_Pair := Last_Pair + 1;
551 Pragma_Map (Last_Pair) := (Key => From, Value => To);
552 end Map_Pragma_Name;
553
554 -----------------
555 -- Pragma_Name --
556 -----------------
557
558 function Pragma_Name (N : Node_Id) return Name_Id is
559 Result : constant Name_Id := Pragma_Name_Unmapped (N);
560 begin
561 for J in Pragma_Map'First .. Last_Pair loop
562 if Result = Pragma_Map (J).Key then
563 return Pragma_Map (J).Value;
564 end if;
565 end loop;
566
567 return Result;
568 end Pragma_Name;
569
570 end Sinfo.Utils;