Debian Codegraph @

gcc/ada/exp_cg.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2010-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 Einfo; use Einfo;
28 with Einfo.Entities; use Einfo.Entities;
29 with Einfo.Utils; use Einfo.Utils;
30 with Elists; use Elists;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Tss; use Exp_Tss;
33 with Lib; use Lib;
34 with Namet; use Namet;
35 with Opt; use Opt;
36 with Output; use Output;
37 with Sem_Aux; use Sem_Aux;
38 with Sem_Disp; use Sem_Disp;
39 with Sem_Type; use Sem_Type;
40 with Sem_Util; use Sem_Util;
41 with Sinfo; use Sinfo;
42 with Sinfo.Nodes; use Sinfo.Nodes;
43 with Sinfo.Utils; use Sinfo.Utils;
44 with Sinput; use Sinput;
45 with Snames; use Snames;
46 with System; use System;
47 with Table;
48 with Uintp; use Uintp;
49
50 package body Exp_CG is
51
52 -- We duplicate here some declarations from packages Interfaces.C and
53 -- Interfaces.C_Streams because adding their dependence to the frontend
54 -- causes bootstrapping problems with old versions of the compiler.
55
56 subtype FILEs is System.Address;
57 -- Corresponds to the C type FILE*
58
59 subtype C_chars is System.Address;
60 -- Pointer to null-terminated array of characters
61
62 function fputs (Strng : C_chars; Stream : FILEs) return Integer;
63 pragma Import (C, fputs, "fputs");
64
65 -- Import the file stream associated with the "ci" output file. Done to
66 -- generate the output in the file created and left opened by routine
67 -- toplev.c before calling gnat1drv.
68
69 Callgraph_Info_File : FILEs;
70 pragma Import (C, Callgraph_Info_File);
71
72 package Call_Graph_Nodes is new Table.Table (
73 Table_Component_Type => Node_Id,
74 Table_Index_Type => Natural,
75 Table_Low_Bound => 1,
76 Table_Initial => 50,
77 Table_Increment => 100,
78 Table_Name => "Call_Graph_Nodes");
79 -- This table records nodes associated with dispatching calls and tagged
80 -- type declarations found in the main compilation unit. Used as an
81 -- auxiliary storage because the call-graph output requires fully qualified
82 -- names and they are not available until the backend is called.
83
84 function Is_Predefined_Dispatching_Operation (E : Entity_Id) return Boolean;
85 -- Determines if E is a predefined primitive operation.
86 -- Note: This routine should replace the routine with the same name that is
87 -- currently available in exp_disp because it extends its functionality to
88 -- handle fully qualified names. It's actually in Sem_Util. ???
89
90 function Slot_Number (Prim : Entity_Id) return Uint;
91 -- Returns the slot number associated with Prim. For predefined primitives
92 -- the slot is returned as a negative number.
93
94 procedure Write_Output (Str : String);
95 -- Used to print a line in the output file (this is used as the
96 -- argument for a call to Set_Special_Output in package Output).
97
98 procedure Write_Call_Info (Call : Node_Id);
99 -- Subsidiary of Generate_CG_Output that generates the output associated
100 -- with a dispatching call.
101
102 procedure Write_Type_Info (Typ : Entity_Id);
103 -- Subsidiary of Generate_CG_Output that generates the output associated
104 -- with a tagged type declaration.
105
106 ------------------------
107 -- Generate_CG_Output --
108 ------------------------
109
110 procedure Generate_CG_Output is
111 N : Node_Id;
112
113 begin
114 -- No output if the "ci" output file has not been previously opened
115 -- by toplev.c
116
117 if Callgraph_Info_File = Null_Address then
118 return;
119 end if;
120
121 -- Setup write routine, create the output file and generate the output
122
123 Set_Special_Output (Write_Output'Access);
124
125 for J in Call_Graph_Nodes.First .. Call_Graph_Nodes.Last loop
126 N := Call_Graph_Nodes.Table (J);
127
128 if Nkind (N) in N_Subprogram_Call then
129 Write_Call_Info (N);
130
131 else pragma Assert (Nkind (N) = N_Defining_Identifier);
132
133 -- The type may be a private untagged type whose completion is
134 -- tagged, in which case we must use the full tagged view.
135
136 if not Is_Tagged_Type (N) and then Is_Private_Type (N) then
137 N := Full_View (N);
138 end if;
139
140 pragma Assert (Is_Tagged_Type (N));
141
142 Write_Type_Info (N);
143 end if;
144 end loop;
145
146 Cancel_Special_Output;
147 end Generate_CG_Output;
148
149 ----------------
150 -- Initialize --
151 ----------------
152
153 procedure Initialize is
154 begin
155 Call_Graph_Nodes.Init;
156 end Initialize;
157
158 -----------------------------------------
159 -- Is_Predefined_Dispatching_Operation --
160 -----------------------------------------
161
162 function Is_Predefined_Dispatching_Operation
163 (E : Entity_Id) return Boolean
164 is
165 function Homonym_Suffix_Length (E : Entity_Id) return Natural;
166 -- Returns the length of the homonym suffix corresponding to E.
167 -- Note: This routine relies on the functionality provided by routines
168 -- of Exp_Dbug. Further work needed here to decide if it should be
169 -- located in that package???
170
171 ---------------------------
172 -- Homonym_Suffix_Length --
173 ---------------------------
174
175 function Homonym_Suffix_Length (E : Entity_Id) return Natural is
176 Prefix_Length : constant := 2;
177 -- Length of prefix "__"
178
179 H : Entity_Id;
180 Nr : Nat := 1;
181
182 begin
183 if not Has_Homonym (E) then
184 return 0;
185
186 else
187 H := Homonym (E);
188 while Present (H) loop
189 if Scope (H) = Scope (E) then
190 Nr := Nr + 1;
191 end if;
192
193 H := Homonym (H);
194 end loop;
195
196 if Nr = 1 then
197 return 0;
198
199 -- Prefix "__" followed by number
200
201 else
202 declare
203 Result : Natural := Prefix_Length + 1;
204
205 begin
206 while Nr >= 10 loop
207 Result := Result + 1;
208 Nr := Nr / 10;
209 end loop;
210
211 return Result;
212 end;
213 end if;
214 end if;
215 end Homonym_Suffix_Length;
216
217 -- Local variables
218
219 Full_Name : constant String := Get_Name_String (Chars (E));
220 Suffix_Length : Natural;
221 TSS_Name : TSS_Name_Type;
222
223 -- Start of processing for Is_Predefined_Dispatching_Operation
224
225 begin
226 if not Is_Dispatching_Operation (E) then
227 return False;
228 end if;
229
230 -- Search for and strip suffix for body-nested package entities
231
232 Suffix_Length := Homonym_Suffix_Length (E);
233 for J in reverse Full_Name'First + 2 .. Full_Name'Last loop
234 if Full_Name (J) = 'X' then
235
236 -- Include the "X", "Xb", "Xn", ... in the part of the
237 -- suffix to be removed.
238
239 Suffix_Length := Suffix_Length + Full_Name'Last - J + 1;
240 exit;
241 end if;
242
243 exit when Full_Name (J) /= 'b' and then Full_Name (J) /= 'n';
244 end loop;
245
246 -- Most predefined primitives have internally generated names. Equality
247 -- must be treated differently; the predefined operation is recognized
248 -- as a homogeneous binary operator that returns Boolean.
249
250 if Full_Name'Length > TSS_Name_Type'Length then
251 TSS_Name :=
252 TSS_Name_Type
253 (Full_Name
254 (Full_Name'Last - TSS_Name'Length - Suffix_Length + 1
255 .. Full_Name'Last - Suffix_Length));
256
257 if TSS_Name = TSS_Stream_Read
258 or else TSS_Name = TSS_Stream_Write
259 or else TSS_Name = TSS_Stream_Input
260 or else TSS_Name = TSS_Stream_Output
261 or else TSS_Name = TSS_Put_Image
262 or else TSS_Name = TSS_Deep_Adjust
263 or else TSS_Name = TSS_Deep_Finalize
264 then
265 return True;
266
267 elsif not Has_Fully_Qualified_Name (E) then
268 if Chars (E) in Name_uSize | Name_uAlignment | Name_uAssign
269 or else
270 (Chars (E) = Name_Op_Eq
271 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
272 or else Is_Predefined_Interface_Primitive (E)
273 then
274 return True;
275 end if;
276
277 -- Handle fully qualified names
278
279 else
280 declare
281 type Names_Table is array (Positive range <>) of Name_Id;
282
283 Predef_Names_95 : constant Names_Table :=
284 (Name_uSize,
285 Name_uAlignment,
286 Name_Op_Eq,
287 Name_uAssign);
288
289 Predef_Names_05 : constant Names_Table :=
290 (Name_uDisp_Asynchronous_Select,
291 Name_uDisp_Conditional_Select,
292 Name_uDisp_Get_Prim_Op_Kind,
293 Name_uDisp_Get_Task_Id,
294 Name_uDisp_Requeue,
295 Name_uDisp_Timed_Select);
296
297 begin
298 for J in Predef_Names_95'Range loop
299 Get_Name_String (Predef_Names_95 (J));
300
301 -- The predefined primitive operations are identified by the
302 -- names "_size", "_alignment", etc. If we try a pattern
303 -- matching against this string, we can wrongly match other
304 -- primitive operations like "get_size". To avoid this, we
305 -- add the "__" scope separator, which can only prepend
306 -- predefined primitive operations because other primitive
307 -- operations can neither start with an underline nor
308 -- contain two consecutive underlines in its name.
309
310 if Full_Name'Last - Suffix_Length > Name_Len + 2
311 and then
312 Full_Name
313 (Full_Name'Last - Name_Len - 2 - Suffix_Length + 1
314 .. Full_Name'Last - Suffix_Length) =
315 "__" & Name_Buffer (1 .. Name_Len)
316 then
317 -- For the equality operator the type of the two operands
318 -- must also match.
319
320 return Predef_Names_95 (J) /= Name_Op_Eq
321 or else
322 Etype (First_Formal (E)) = Etype (Last_Formal (E));
323 end if;
324 end loop;
325
326 if Ada_Version >= Ada_2005 then
327 for J in Predef_Names_05'Range loop
328 Get_Name_String (Predef_Names_05 (J));
329
330 if Full_Name'Last - Suffix_Length > Name_Len + 2
331 and then
332 Full_Name
333 (Full_Name'Last - Name_Len - 2 - Suffix_Length + 1
334 .. Full_Name'Last - Suffix_Length) =
335 "__" & Name_Buffer (1 .. Name_Len)
336 then
337 return True;
338 end if;
339 end loop;
340 end if;
341 end;
342 end if;
343 end if;
344
345 return False;
346 end Is_Predefined_Dispatching_Operation;
347
348 ----------------------
349 -- Register_CG_Node --
350 ----------------------
351
352 procedure Register_CG_Node (N : Node_Id) is
353 begin
354 -- Skip ignored ghost calls that will be removed by the expander
355
356 if Is_Ignored_Ghost_Node (N) then
357 null;
358
359 elsif Nkind (N) in N_Subprogram_Call then
360
361 if Current_Scope = Main_Unit_Entity
362 or else Entity_Is_In_Main_Unit (Current_Scope)
363 then
364 -- Register a copy of the dispatching call node. Needed since the
365 -- node containing a dispatching call is rewritten by the
366 -- expander.
367
368 declare
369 Copy : constant Node_Id := New_Copy (N);
370 Par : Node_Id;
371
372 begin
373 -- Determine the enclosing scope to use when generating the
374 -- call graph. This must be done now to avoid problems with
375 -- control structures that may be rewritten during expansion.
376
377 Par := Parent (N);
378 while Nkind (Par) /= N_Subprogram_Body
379 and then Nkind (Parent (Par)) /= N_Compilation_Unit
380 loop
381 Par := Parent (Par);
382
383 -- Par can legitimately be empty inside a class-wide
384 -- precondition; the "real" call will be found inside the
385 -- generated pragma.
386
387 if No (Par) then
388 return;
389 end if;
390 end loop;
391
392 Set_Parent (Copy, Par);
393 Call_Graph_Nodes.Append (Copy);
394 end;
395 end if;
396
397 else pragma Assert (Nkind (N) = N_Defining_Identifier);
398 if Entity_Is_In_Main_Unit (N) then
399 Call_Graph_Nodes.Append (N);
400 end if;
401 end if;
402 end Register_CG_Node;
403
404 -----------------
405 -- Slot_Number --
406 -----------------
407
408 function Slot_Number (Prim : Entity_Id) return Uint is
409 E : constant Entity_Id := Ultimate_Alias (Prim);
410 begin
411 if Is_Predefined_Dispatching_Operation (E) then
412 return -DT_Position (E);
413 else
414 return DT_Position (E);
415 end if;
416 end Slot_Number;
417
418 ------------------
419 -- Write_Output --
420 ------------------
421
422 procedure Write_Output (Str : String) is
423 Nul : constant Character := Character'First;
424 Line : String (Str'First .. Str'Last + 1);
425 Errno : Integer;
426
427 begin
428 -- Add the null character to the string as required by fputs
429
430 Line := Str & Nul;
431 Errno := fputs (Line'Address, Callgraph_Info_File);
432 pragma Assert (Errno >= 0);
433 end Write_Output;
434
435 ---------------------
436 -- Write_Call_Info --
437 ---------------------
438
439 procedure Write_Call_Info (Call : Node_Id) is
440 Ctrl_Arg : constant Node_Id := Controlling_Argument (Call);
441 Ctrl_Typ : constant Entity_Id := Base_Type (Etype (Ctrl_Arg));
442 Prim : constant Entity_Id := Entity (Sinfo.Nodes.Name (Call));
443 P : constant Node_Id := Parent (Call);
444
445 begin
446 Write_Str ("edge: { sourcename: ");
447 Write_Char ('"');
448
449 -- The parent node is the construct that contains the call: subprogram
450 -- body or library-level package. Display the qualified name of the
451 -- entity of the construct. For a subprogram, it is the entity of the
452 -- spec, which carries a homonym counter when it is overloaded.
453
454 if Nkind (P) = N_Subprogram_Body
455 and then not Acts_As_Spec (P)
456 then
457 Get_External_Name (Corresponding_Spec (P));
458
459 else
460 Get_External_Name (Defining_Entity (P));
461 end if;
462
463 Write_Str (Name_Buffer (1 .. Name_Len));
464
465 if Nkind (P) = N_Package_Declaration then
466 Write_Str ("___elabs");
467
468 elsif Nkind (P) = N_Package_Body then
469 Write_Str ("___elabb");
470 end if;
471
472 Write_Char ('"');
473 Write_Eol;
474
475 -- The targetname is a triple:
476 -- N: the index in a vtable used for dispatch
477 -- V: the type who's vtable is used
478 -- S: the static type of the expression
479
480 Write_Str (" targetname: ");
481 Write_Char ('"');
482
483 pragma Assert (No (Interface_Alias (Prim)));
484
485 -- The check on Is_Ancestor is done here to avoid problems with
486 -- renamings of primitives. For example:
487
488 -- type Root is tagged ...
489 -- procedure Base (Obj : Root);
490 -- procedure Base2 (Obj : Root) renames Base;
491
492 if Present (Alias (Prim))
493 and then
494 Is_Ancestor
495 (Find_Dispatching_Type (Ultimate_Alias (Prim)),
496 Root_Type (Ctrl_Typ),
497 Use_Full_View => True)
498 then
499 -- This is a special case in which we generate in the ci file the
500 -- slot number of the renaming primitive (i.e. Base2) but instead of
501 -- generating the name of this renaming entity we reference directly
502 -- the renamed entity (i.e. Base).
503
504 Write_Int (UI_To_Int (Slot_Number (Prim)));
505 Write_Char (':');
506 Write_Name
507 (Chars (Find_Dispatching_Type (Ultimate_Alias (Prim))));
508 else
509 Write_Int (UI_To_Int (Slot_Number (Prim)));
510 Write_Char (':');
511 Write_Name (Chars (Root_Type (Ctrl_Typ)));
512 end if;
513
514 Write_Char (',');
515 Write_Name (Chars (Root_Type (Ctrl_Typ)));
516
517 Write_Char ('"');
518 Write_Eol;
519
520 Write_Str (" label: ");
521 Write_Char ('"');
522 Write_Location (Sloc (Call));
523 Write_Char ('"');
524 Write_Eol;
525
526 Write_Char ('}');
527 Write_Eol;
528 end Write_Call_Info;
529
530 ---------------------
531 -- Write_Type_Info --
532 ---------------------
533
534 procedure Write_Type_Info (Typ : Entity_Id) is
535 Elmt : Elmt_Id;
536 Prim : Node_Id;
537
538 Parent_Typ : Entity_Id;
539 Separator_Needed : Boolean := False;
540
541 begin
542 -- Initialize Parent_Typ handling private types
543
544 Parent_Typ := Etype (Typ);
545
546 if Present (Full_View (Parent_Typ)) then
547 Parent_Typ := Full_View (Parent_Typ);
548 end if;
549
550 Write_Str ("class {");
551 Write_Eol;
552
553 Write_Str (" classname: ");
554 Write_Char ('"');
555 Write_Name (Chars (Typ));
556 Write_Char ('"');
557 Write_Eol;
558
559 Write_Str (" label: ");
560 Write_Char ('"');
561 Write_Name (Chars (Typ));
562 Write_Char ('\');
563 Write_Location (Sloc (Typ));
564 Write_Char ('"');
565 Write_Eol;
566
567 if Parent_Typ /= Typ then
568 Write_Str (" parent: ");
569 Write_Char ('"');
570 Write_Name (Chars (Parent_Typ));
571
572 -- Note: Einfo.Entities prefix not needed if this routine is moved to
573 -- exp_disp???
574
575 if Present (Einfo.Entities.Interfaces (Typ))
576 and then not Is_Empty_Elmt_List (Einfo.Entities.Interfaces (Typ))
577 then
578 Elmt := First_Elmt (Einfo.Entities.Interfaces (Typ));
579 while Present (Elmt) loop
580 Write_Str (", ");
581 Write_Name (Chars (Node (Elmt)));
582 Next_Elmt (Elmt);
583 end loop;
584 end if;
585
586 Write_Char ('"');
587 Write_Eol;
588 end if;
589
590 Write_Str (" virtuals: ");
591 Write_Char ('"');
592
593 Elmt := First_Elmt (Primitive_Operations (Typ));
594 while Present (Elmt) loop
595 Prim := Node (Elmt);
596
597 -- Skip internal entities associated with overridden interface
598 -- primitives, and also inherited primitives.
599
600 if Present (Interface_Alias (Prim))
601 or else
602 (Present (Alias (Prim))
603 and then Find_Dispatching_Type (Prim) /=
604 Find_Dispatching_Type (Alias (Prim)))
605 then
606 goto Continue;
607 end if;
608
609 -- Do not generate separator for output of first primitive
610
611 if Separator_Needed then
612 Write_Str ("\n");
613 Write_Eol;
614 Write_Str (" ");
615 else
616 Separator_Needed := True;
617 end if;
618
619 Write_Int (UI_To_Int (Slot_Number (Prim)));
620 Write_Char (':');
621
622 -- Handle renamed primitives
623
624 if Present (Alias (Prim)) then
625 Write_Name (Chars (Ultimate_Alias (Prim)));
626 else
627 Write_Name (Chars (Prim));
628 end if;
629
630 -- Display overriding of parent primitives
631
632 if Present (Overridden_Operation (Prim))
633 and then
634 Is_Ancestor
635 (Find_Dispatching_Type (Overridden_Operation (Prim)), Typ,
636 Use_Full_View => True)
637 then
638 Write_Char (',');
639 Write_Int
640 (UI_To_Int (Slot_Number (Overridden_Operation (Prim))));
641 Write_Char (':');
642 Write_Name
643 (Chars (Find_Dispatching_Type (Overridden_Operation (Prim))));
644 end if;
645
646 -- Display overriding of interface primitives
647
648 if Has_Interfaces (Typ) then
649 declare
650 Prim_Elmt : Elmt_Id;
651 Prim_Op : Node_Id;
652 Int_Alias : Entity_Id;
653
654 begin
655 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
656 while Present (Prim_Elmt) loop
657 Prim_Op := Node (Prim_Elmt);
658 Int_Alias := Interface_Alias (Prim_Op);
659
660 if Present (Int_Alias)
661 and then
662 not Is_Ancestor (Find_Dispatching_Type (Int_Alias), Typ,
663 Use_Full_View => True)
664 and then (Alias (Prim_Op)) = Prim
665 then
666 Write_Char (',');
667 Write_Int (UI_To_Int (Slot_Number (Int_Alias)));
668 Write_Char (':');
669 Write_Name (Chars (Find_Dispatching_Type (Int_Alias)));
670 end if;
671
672 Next_Elmt (Prim_Elmt);
673 end loop;
674 end;
675 end if;
676
677 <<Continue>>
678 Next_Elmt (Elmt);
679 end loop;
680
681 Write_Char ('"');
682 Write_Eol;
683
684 Write_Char ('}');
685 Write_Eol;
686 end Write_Type_Info;
687
688 end Exp_CG;