Debian Codegraph @

gcc/ada/lib.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- L I B --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 pragma Style_Checks (All_Checks);
27 -- Subprogram ordering not enforced in this unit
28 -- (because of some logical groupings).
29
30 with Atree; use Atree;
31 with Csets; use Csets;
32 with Einfo; use Einfo;
33 with Einfo.Entities; use Einfo.Entities;
34 with Nlists; use Nlists;
35 with Opt; use Opt;
36 with Output; use Output;
37 with Sinfo; use Sinfo;
38 with Sinfo.Nodes; use Sinfo.Nodes;
39 with Sinfo.Utils; use Sinfo.Utils;
40 with Sinput; use Sinput;
41 with Stand; use Stand;
42 with Stringt; use Stringt;
43 with Uname; use Uname;
44 with Widechar; use Widechar;
45
46 package body Lib is
47
48 Switch_Storing_Enabled : Boolean := True;
49 -- Controlled by Enable_Switch_Storing/Disable_Switch_Storing
50
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
54
55 type SEU_Result is (
56 Yes_Before, -- S1 is in same extended unit as S2 and appears before it
57 Yes_Same, -- S1 is in same extended unit as S2, Slocs are the same
58 Yes_After, -- S1 is in same extended unit as S2, and appears after it
59 No); -- S2 is not in same extended unit as S2
60
61 function Check_Same_Extended_Unit
62 (S1 : Source_Ptr;
63 S2 : Source_Ptr) return SEU_Result;
64 -- Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
65 -- value as described above.
66
67 function Get_Code_Or_Source_Unit
68 (S : Source_Ptr;
69 Unwind_Instances : Boolean;
70 Unwind_Subunits : Boolean) return Unit_Number_Type;
71 -- Common processing for routines Get_Code_Unit, Get_Source_Unit, and
72 -- Get_Top_Level_Code_Unit. Unwind_Instances is True when the unit for the
73 -- top-level instantiation should be returned instead of the unit for the
74 -- template, in the case of an instantiation. Unwind_Subunits is True when
75 -- the corresponding top-level unit should be returned instead of a
76 -- subunit, in the case of a subunit.
77
78 --------------------------------------------
79 -- Access Functions for Unit Table Fields --
80 --------------------------------------------
81
82 function Cunit (U : Unit_Number_Type) return Node_Id is
83 begin
84 return Units.Table (U).Cunit;
85 end Cunit;
86
87 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
88 begin
89 return Units.Table (U).Cunit_Entity;
90 end Cunit_Entity;
91
92 function Dependency_Num (U : Unit_Number_Type) return Nat is
93 begin
94 return Units.Table (U).Dependency_Num;
95 end Dependency_Num;
96
97 function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
98 begin
99 return Units.Table (U).Dynamic_Elab;
100 end Dynamic_Elab;
101
102 function Error_Location (U : Unit_Number_Type) return Source_Ptr is
103 begin
104 return Units.Table (U).Error_Location;
105 end Error_Location;
106
107 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
108 begin
109 return Units.Table (U).Expected_Unit;
110 end Expected_Unit;
111
112 function Fatal_Error (U : Unit_Number_Type) return Fatal_Type is
113 begin
114 return Units.Table (U).Fatal_Error;
115 end Fatal_Error;
116
117 function Generate_Code (U : Unit_Number_Type) return Boolean is
118 begin
119 return Units.Table (U).Generate_Code;
120 end Generate_Code;
121
122 function Has_RACW (U : Unit_Number_Type) return Boolean is
123 begin
124 return Units.Table (U).Has_RACW;
125 end Has_RACW;
126
127 function Is_Predefined_Renaming (U : Unit_Number_Type) return Boolean is
128 begin
129 return Units.Table (U).Is_Predefined_Renaming;
130 end Is_Predefined_Renaming;
131
132 function Is_Internal_Unit (U : Unit_Number_Type) return Boolean is
133 begin
134 return Units.Table (U).Is_Internal_Unit;
135 end Is_Internal_Unit;
136
137 function Is_Predefined_Unit (U : Unit_Number_Type) return Boolean is
138 begin
139 return Units.Table (U).Is_Predefined_Unit;
140 end Is_Predefined_Unit;
141
142 function Ident_String (U : Unit_Number_Type) return Node_Id is
143 begin
144 return Units.Table (U).Ident_String;
145 end Ident_String;
146
147 function Loading (U : Unit_Number_Type) return Boolean is
148 begin
149 return Units.Table (U).Loading;
150 end Loading;
151
152 function Main_CPU (U : Unit_Number_Type) return Int is
153 begin
154 return Units.Table (U).Main_CPU;
155 end Main_CPU;
156
157 function Main_Priority (U : Unit_Number_Type) return Int is
158 begin
159 return Units.Table (U).Main_Priority;
160 end Main_Priority;
161
162 function Munit_Index (U : Unit_Number_Type) return Nat is
163 begin
164 return Units.Table (U).Munit_Index;
165 end Munit_Index;
166
167 function No_Elab_Code_All (U : Unit_Number_Type) return Boolean is
168 begin
169 return Units.Table (U).No_Elab_Code_All;
170 end No_Elab_Code_All;
171
172 function OA_Setting (U : Unit_Number_Type) return Character is
173 begin
174 return Units.Table (U).OA_Setting;
175 end OA_Setting;
176
177 function Primary_Stack_Count (U : Unit_Number_Type) return Nat is
178 begin
179 return Units.Table (U).Primary_Stack_Count;
180 end Primary_Stack_Count;
181
182 function Sec_Stack_Count (U : Unit_Number_Type) return Nat is
183 begin
184 return Units.Table (U).Sec_Stack_Count;
185 end Sec_Stack_Count;
186
187 function Source_Index (U : Unit_Number_Type) return Source_File_Index is
188 begin
189 return Units.Table (U).Source_Index;
190 end Source_Index;
191
192 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
193 begin
194 return Units.Table (U).Unit_File_Name;
195 end Unit_File_Name;
196
197 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
198 begin
199 return Units.Table (U).Unit_Name;
200 end Unit_Name;
201
202 ------------------------------------------
203 -- Subprograms to Set Unit Table Fields --
204 ------------------------------------------
205
206 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
207 begin
208 Units.Table (U).Cunit := N;
209 end Set_Cunit;
210
211 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
212 begin
213 Units.Table (U).Cunit_Entity := E;
214 Set_Is_Compilation_Unit (E);
215 end Set_Cunit_Entity;
216
217 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
218 begin
219 Units.Table (U).Dynamic_Elab := B;
220 end Set_Dynamic_Elab;
221
222 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
223 begin
224 Units.Table (U).Error_Location := W;
225 end Set_Error_Location;
226
227 procedure Set_Fatal_Error (U : Unit_Number_Type; V : Fatal_Type) is
228 begin
229 Units.Table (U).Fatal_Error := V;
230 end Set_Fatal_Error;
231
232 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
233 begin
234 Units.Table (U).Generate_Code := B;
235 end Set_Generate_Code;
236
237 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
238 begin
239 Units.Table (U).Has_RACW := B;
240 end Set_Has_RACW;
241
242 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
243 begin
244 Units.Table (U).Ident_String := N;
245 end Set_Ident_String;
246
247 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
248 begin
249 Units.Table (U).Loading := B;
250 end Set_Loading;
251
252 procedure Set_Main_CPU (U : Unit_Number_Type; P : Int) is
253 begin
254 Units.Table (U).Main_CPU := P;
255 end Set_Main_CPU;
256
257 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
258 begin
259 Units.Table (U).Main_Priority := P;
260 end Set_Main_Priority;
261
262 procedure Set_No_Elab_Code_All
263 (U : Unit_Number_Type;
264 B : Boolean := True)
265 is
266 begin
267 Units.Table (U).No_Elab_Code_All := B;
268 end Set_No_Elab_Code_All;
269
270 procedure Set_OA_Setting (U : Unit_Number_Type; C : Character) is
271 begin
272 Units.Table (U).OA_Setting := C;
273 end Set_OA_Setting;
274
275 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
276 Old_N : constant Unit_Name_Type := Units.Table (U).Unit_Name;
277
278 begin
279 -- First unregister the old name, if any
280
281 if Present (Old_N) and then Unit_Names.Get (Old_N) = U then
282 Unit_Names.Set (Old_N, No_Unit);
283 end if;
284
285 -- Then set the new name
286
287 Units.Table (U).Unit_Name := N;
288
289 -- Finally register the new name
290
291 if Unit_Names.Get (N) = No_Unit then
292 Unit_Names.Set (N, U);
293 end if;
294 end Set_Unit_Name;
295
296 ------------------------------
297 -- Check_Same_Extended_Unit --
298 ------------------------------
299
300 function Check_Same_Extended_Unit
301 (S1 : Source_Ptr;
302 S2 : Source_Ptr) return SEU_Result
303 is
304 Max_Iterations : constant Nat := Maximum_Instantiations * 2;
305 -- Limit to prevent a potential infinite loop
306
307 Counter : Nat := 0;
308 Depth1 : Nat;
309 Depth2 : Nat;
310 Inst1 : Source_Ptr;
311 Inst2 : Source_Ptr;
312 Sind1 : Source_File_Index;
313 Sind2 : Source_File_Index;
314 Sloc1 : Source_Ptr;
315 Sloc2 : Source_Ptr;
316 Unit1 : Node_Id;
317 Unit2 : Node_Id;
318 Unum1 : Unit_Number_Type;
319 Unum2 : Unit_Number_Type;
320
321 begin
322 if S1 = No_Location or else S2 = No_Location then
323 return No;
324 end if;
325
326 if S1 = S2 then
327 return Yes_Same;
328 end if;
329
330 if S1 = Standard_Location or else S2 = Standard_Location then
331 return No;
332 end if;
333
334 Sloc1 := S1;
335 Sloc2 := S2;
336
337 Unum1 := Get_Source_Unit (Sloc1);
338 Unum2 := Get_Source_Unit (Sloc2);
339
340 loop
341 -- Step 1: Check whether the two locations are in the same source
342 -- file.
343
344 Sind1 := Get_Source_File_Index (Sloc1);
345 Sind2 := Get_Source_File_Index (Sloc2);
346
347 if Sind1 = Sind2 then
348 if Sloc1 < Sloc2 then
349 return Yes_Before;
350 elsif Sloc1 > Sloc2 then
351 return Yes_After;
352 else
353 return Yes_Same;
354 end if;
355 end if;
356
357 -- Step 2: Check subunits. If a subunit is instantiated, follow the
358 -- instantiation chain rather than the stub chain.
359
360 -- Note that we must handle the case where the subunit exists in the
361 -- same body as the main unit (which may happen when Naming gets
362 -- manually specified within a project file or through tools like
363 -- gprname). Otherwise, we will have an infinite loop jumping around
364 -- the same file.
365
366 Unit1 := Unit (Cunit (Unum1));
367 Unit2 := Unit (Cunit (Unum2));
368 Inst1 := Instantiation (Sind1);
369 Inst2 := Instantiation (Sind2);
370
371 if Nkind (Unit1) = N_Subunit
372 and then Present (Corresponding_Stub (Unit1))
373 and then Inst1 = No_Location
374 then
375 if Nkind (Unit2) = N_Subunit
376 and then Present (Corresponding_Stub (Unit2))
377 and then Inst2 = No_Location
378 then
379 -- Both locations refer to subunits which may have a common
380 -- ancestor. If they do, the deeper subunit must have a longer
381 -- unit name. Replace the deeper one with its corresponding
382 -- stub in order to find the nearest ancestor.
383
384 if Length_Of_Name (Unit_Name (Unum1)) <
385 Length_Of_Name (Unit_Name (Unum2))
386 then
387 Sloc2 := Sloc (Corresponding_Stub (Unit2));
388
389 if Unum2 /= Get_Source_Unit (Sloc2) then
390 Unum2 := Get_Source_Unit (Sloc2);
391 goto Continue;
392 else
393 null; -- Unum2 already designates the correct unit
394 end if;
395 else
396 Sloc1 := Sloc (Corresponding_Stub (Unit1));
397
398 if Unum1 /= Get_Source_Unit (Sloc1) then
399 Unum1 := Get_Source_Unit (Sloc1);
400 goto Continue;
401 else
402 null; -- Unum1 already designates the correct unit
403 end if;
404 end if;
405
406 -- Sloc1 in subunit, Sloc2 not
407
408 else
409 Sloc1 := Sloc (Corresponding_Stub (Unit1));
410
411 if Unum1 /= Get_Source_Unit (Sloc1) then
412 Unum1 := Get_Source_Unit (Sloc1);
413 goto Continue;
414 else
415 null; -- Unum1 already designates the correct unit
416 end if;
417 end if;
418
419 -- Sloc2 in subunit, Sloc1 not
420
421 elsif Nkind (Unit2) = N_Subunit
422 and then Present (Corresponding_Stub (Unit2))
423 and then Inst2 = No_Location
424 then
425 Sloc2 := Sloc (Corresponding_Stub (Unit2));
426
427 if Unum2 /= Get_Source_Unit (Sloc2) then
428 Unum2 := Get_Source_Unit (Sloc2);
429 goto Continue;
430 else
431 null; -- Unum2 already designates the correct unit
432 end if;
433 end if;
434
435 -- Step 3: Check instances. The two locations may yield a common
436 -- ancestor.
437
438 if Inst1 /= No_Location then
439 if Inst2 /= No_Location then
440
441 -- Both locations denote instantiations
442
443 Depth1 := Instantiation_Depth (Sloc1);
444 Depth2 := Instantiation_Depth (Sloc2);
445
446 if Depth1 < Depth2 then
447 Sloc2 := Inst2;
448 Unum2 := Get_Source_Unit (Sloc2);
449 goto Continue;
450
451 elsif Depth1 > Depth2 then
452 Sloc1 := Inst1;
453 Unum1 := Get_Source_Unit (Sloc1);
454 goto Continue;
455
456 else
457 Sloc1 := Inst1;
458 Sloc2 := Inst2;
459 Unum1 := Get_Source_Unit (Sloc1);
460 Unum2 := Get_Source_Unit (Sloc2);
461 goto Continue;
462 end if;
463
464 -- Sloc1 is an instantiation
465
466 else
467 Sloc1 := Inst1;
468 Unum1 := Get_Source_Unit (Sloc1);
469 goto Continue;
470 end if;
471
472 -- Sloc2 is an instantiation
473
474 elsif Inst2 /= No_Location then
475 Sloc2 := Inst2;
476 Unum2 := Get_Source_Unit (Sloc2);
477 goto Continue;
478 end if;
479
480 -- Step 4: One location in the spec, the other in the corresponding
481 -- body of the same unit. The location in the spec is considered
482 -- earlier.
483
484 if Nkind (Unit1) in N_Subprogram_Body | N_Package_Body then
485 if Spec_Lib_Unit (Cunit (Unum1)) = Cunit (Unum2) then
486 return Yes_After;
487 end if;
488
489 elsif Nkind (Unit2) in N_Subprogram_Body | N_Package_Body then
490 if Spec_Lib_Unit (Cunit (Unum2)) = Cunit (Unum1) then
491 return Yes_Before;
492 end if;
493 end if;
494
495 -- At this point it is certain that the two locations denote two
496 -- entirely separate units.
497
498 return No;
499
500 <<Continue>>
501 Counter := Counter + 1;
502
503 -- Prevent looping forever
504
505 if Counter > Max_Iterations then
506
507 -- In CodePeer_Mode, return a value to be able to generate SCIL
508 -- files and hope for the best.
509
510 if CodePeer_Mode then
511 return No;
512 else
513 raise Program_Error;
514 end if;
515 end if;
516 end loop;
517 end Check_Same_Extended_Unit;
518
519 -------------------------------
520 -- Compilation_Switches_Last --
521 -------------------------------
522
523 function Compilation_Switches_Last return Nat is
524 begin
525 return Compilation_Switches.Last;
526 end Compilation_Switches_Last;
527
528 ---------------------------
529 -- Enable_Switch_Storing --
530 ---------------------------
531
532 procedure Enable_Switch_Storing is
533 begin
534 Switch_Storing_Enabled := True;
535 end Enable_Switch_Storing;
536
537 ----------------------------
538 -- Disable_Switch_Storing --
539 ----------------------------
540
541 procedure Disable_Switch_Storing is
542 begin
543 Switch_Storing_Enabled := False;
544 end Disable_Switch_Storing;
545
546 ------------------------------
547 -- Earlier_In_Extended_Unit --
548 ------------------------------
549
550 function Earlier_In_Extended_Unit
551 (S1 : Source_Ptr;
552 S2 : Source_Ptr) return Boolean
553 is
554 begin
555 return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
556 end Earlier_In_Extended_Unit;
557
558 function Earlier_In_Extended_Unit
559 (N1 : Node_Or_Entity_Id;
560 N2 : Node_Or_Entity_Id) return Boolean
561 is
562 begin
563 return Earlier_In_Extended_Unit (Sloc (N1), Sloc (N2));
564 end Earlier_In_Extended_Unit;
565
566 -----------------------
567 -- Exact_Source_Name --
568 -----------------------
569
570 function Exact_Source_Name (Loc : Source_Ptr) return String is
571 U : constant Unit_Number_Type := Get_Source_Unit (Loc);
572 Buf : constant Source_Buffer_Ptr := Source_Text (Source_Index (U));
573 Orig : constant Source_Ptr := Original_Location (Loc);
574 P : Source_Ptr;
575
576 WC : Char_Code;
577 Err : Boolean;
578 pragma Warnings (Off, WC);
579 pragma Warnings (Off, Err);
580
581 begin
582 -- Entity is character literal
583
584 if Buf (Orig) = ''' then
585 return String (Buf (Orig .. Orig + 2));
586
587 -- Entity is operator symbol
588
589 elsif Buf (Orig) = '"' or else Buf (Orig) = '%' then
590 P := Orig;
591
592 loop
593 P := P + 1;
594 exit when Buf (P) = Buf (Orig);
595 end loop;
596
597 return String (Buf (Orig .. P));
598
599 -- Entity is identifier
600
601 else
602 P := Orig;
603
604 loop
605 if Is_Start_Of_Wide_Char (Buf, P) then
606 Scan_Wide (Buf, P, WC, Err);
607 elsif not Identifier_Char (Buf (P)) then
608 exit;
609 else
610 P := P + 1;
611 end if;
612 end loop;
613
614 -- Write out the identifier by copying the exact source characters
615 -- used in its declaration. Note that this means wide characters will
616 -- be in their original encoded form.
617
618 return String (Buf (Orig .. P - 1));
619 end if;
620 end Exact_Source_Name;
621
622 ----------------------------
623 -- Entity_Is_In_Main_Unit --
624 ----------------------------
625
626 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
627 S : Entity_Id;
628
629 begin
630 S := Scope (E);
631
632 while S /= Standard_Standard loop
633 if S = Main_Unit_Entity then
634 return True;
635 elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
636 return False;
637 else
638 S := Scope (S);
639 end if;
640 end loop;
641
642 return False;
643 end Entity_Is_In_Main_Unit;
644
645 --------------------------
646 -- Generic_May_Lack_ALI --
647 --------------------------
648
649 function Generic_May_Lack_ALI (Unum : Unit_Number_Type) return Boolean is
650 begin
651 -- We allow internal generic units to be used without having a
652 -- corresponding ALI files to help bootstrapping with older compilers
653 -- that did not support generating ALIs for such generics. It is safe
654 -- to do so because the only thing the generated code would contain
655 -- is the elaboration boolean, and we are careful to elaborate all
656 -- predefined units first anyway.
657
658 return Is_Internal_Unit (Unum);
659 end Generic_May_Lack_ALI;
660
661 -----------------------------
662 -- Get_Code_Or_Source_Unit --
663 -----------------------------
664
665 function Get_Code_Or_Source_Unit
666 (S : Source_Ptr;
667 Unwind_Instances : Boolean;
668 Unwind_Subunits : Boolean) return Unit_Number_Type
669 is
670 begin
671 -- Search table unless we have No_Location, which can happen if the
672 -- relevant location has not been set yet. Happens for example when
673 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
674
675 if S /= No_Location then
676 declare
677 Source_File : Source_File_Index;
678 Source_Unit : Unit_Number_Type;
679 Unit_Node : Node_Id;
680
681 begin
682 Source_File := Get_Source_File_Index (S);
683
684 if Unwind_Instances then
685 while Template (Source_File) > No_Source_File loop
686 Source_File := Template (Source_File);
687 end loop;
688 end if;
689
690 Source_Unit := Unit (Source_File);
691
692 if Unwind_Subunits then
693 Unit_Node := Unit (Cunit (Source_Unit));
694
695 while Nkind (Unit_Node) = N_Subunit
696 and then Present (Corresponding_Stub (Unit_Node))
697 loop
698 Source_Unit :=
699 Get_Code_Or_Source_Unit
700 (Sloc (Corresponding_Stub (Unit_Node)),
701 Unwind_Instances => Unwind_Instances,
702 Unwind_Subunits => Unwind_Subunits);
703 Unit_Node := Unit (Cunit (Source_Unit));
704 end loop;
705 end if;
706
707 if Source_Unit /= No_Unit then
708 return Source_Unit;
709 end if;
710 end;
711 end if;
712
713 -- If S was No_Location, or was not in the table, we must be in the main
714 -- source unit (and the value has not been placed in the table yet),
715 -- or in one of the configuration pragma files.
716
717 return Main_Unit;
718 end Get_Code_Or_Source_Unit;
719
720 -------------------
721 -- Get_Code_Unit --
722 -------------------
723
724 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
725 begin
726 return
727 Get_Code_Or_Source_Unit
728 (Top_Level_Location (S),
729 Unwind_Instances => False,
730 Unwind_Subunits => False);
731 end Get_Code_Unit;
732
733 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
734 begin
735 return Get_Code_Unit (Sloc (N));
736 end Get_Code_Unit;
737
738 ----------------------------
739 -- Get_Compilation_Switch --
740 ----------------------------
741
742 function Get_Compilation_Switch (N : Pos) return String_Ptr is
743 begin
744 if N <= Compilation_Switches.Last then
745 return Compilation_Switches.Table (N);
746 else
747 return null;
748 end if;
749 end Get_Compilation_Switch;
750
751 ----------------------------------
752 -- Get_Cunit_Entity_Unit_Number --
753 ----------------------------------
754
755 function Get_Cunit_Entity_Unit_Number
756 (E : Entity_Id) return Unit_Number_Type
757 is
758 begin
759 for U in Units.First .. Units.Last loop
760 if Cunit_Entity (U) = E then
761 return U;
762 end if;
763 end loop;
764
765 -- If not in the table, must be the main source unit, and we just
766 -- have not got it put into the table yet.
767
768 return Main_Unit;
769 end Get_Cunit_Entity_Unit_Number;
770
771 ---------------------------
772 -- Get_Cunit_Unit_Number --
773 ---------------------------
774
775 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
776 begin
777 for U in Units.First .. Units.Last loop
778 if Cunit (U) = N then
779 return U;
780 end if;
781 end loop;
782
783 -- Not in the table. Empty N is some already-detected error; otherwise,
784 -- it must be a spec created for a main unit that is a child subprogram
785 -- body which we have not inserted into the table yet.
786
787 if No (N) then
788 pragma Assert (Serious_Errors_Detected > 0);
789 return Main_Unit;
790 end if;
791
792 if N = Spec_Lib_Unit (Cunit (Main_Unit)) then
793 return Main_Unit;
794
795 -- If it is anything else, something is seriously wrong, and we really
796 -- don't want to proceed, even if assertions are off, so we explicitly
797 -- raise an exception in this case to terminate compilation.
798
799 else
800 raise Program_Error;
801 end if;
802 end Get_Cunit_Unit_Number;
803
804 ---------------------
805 -- Get_Source_Unit --
806 ---------------------
807
808 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
809 begin
810 return
811 Get_Code_Or_Source_Unit
812 (S => S,
813 Unwind_Instances => True,
814 Unwind_Subunits => False);
815 end Get_Source_Unit;
816
817 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
818 begin
819 return Get_Source_Unit (Sloc (N));
820 end Get_Source_Unit;
821
822 -----------------------------
823 -- Get_Top_Level_Code_Unit --
824 -----------------------------
825
826 function Get_Top_Level_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
827 begin
828 return
829 Get_Code_Or_Source_Unit
830 (Top_Level_Location (S),
831 Unwind_Instances => False,
832 Unwind_Subunits => True);
833 end Get_Top_Level_Code_Unit;
834
835 function Get_Top_Level_Code_Unit
836 (N : Node_Or_Entity_Id) return Unit_Number_Type is
837 begin
838 return Get_Top_Level_Code_Unit (Sloc (N));
839 end Get_Top_Level_Code_Unit;
840
841 --------------------------------
842 -- In_Extended_Main_Code_Unit --
843 --------------------------------
844
845 function In_Extended_Main_Code_Unit
846 (N : Node_Or_Entity_Id) return Boolean
847 is
848 begin
849 -- Special case Itypes to test the Sloc of the associated node. The
850 -- reason we do this is for possible calls from gigi after -gnatD
851 -- processing is complete in sprint. This processing updates the
852 -- sloc fields of all nodes in the tree, but itypes are not in the
853 -- tree so their slocs do not get updated.
854
855 if Nkind (N) = N_Defining_Identifier and then Is_Itype (N) then
856 return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
857 end if;
858
859 return In_Extended_Main_Code_Unit (Sloc (N));
860 end In_Extended_Main_Code_Unit;
861
862 function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
863 begin
864 -- Special value cases
865
866 if Loc in No_Location | Standard_Location then
867 return False;
868 end if;
869
870 -- Otherwise see if we are in the main unit
871
872 if Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
873 return True;
874 end if;
875
876 -- Location may be in spec (or subunit etc) of main unit
877
878 return In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
879 end In_Extended_Main_Code_Unit;
880
881 ----------------------------------
882 -- In_Extended_Main_Source_Unit --
883 ----------------------------------
884
885 function In_Extended_Main_Source_Unit
886 (N : Node_Or_Entity_Id) return Boolean
887 is
888 begin
889 -- Special case Itypes to test the Sloc of the associated node. The
890 -- reason we do this is for possible calls from gigi after -gnatD
891 -- processing is complete in sprint. This processing updates the
892 -- sloc fields of all nodes in the tree, but itypes are not in the
893 -- tree so their slocs do not get updated.
894
895 if Nkind (N) = N_Defining_Identifier and then Is_Itype (N) then
896 pragma Assert (Compiler_State /= Parsing);
897 return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
898 end if;
899
900 return In_Extended_Main_Source_Unit (Sloc (N));
901 end In_Extended_Main_Source_Unit;
902
903 function In_Extended_Main_Source_Unit
904 (Loc : Source_Ptr) return Boolean
905 is
906 begin
907 -- If parsing, then use the global flag to indicate result
908
909 if Compiler_State = Parsing then
910 return Parsing_Main_Extended_Source;
911 end if;
912
913 -- Special value cases
914
915 if Loc in No_Location | Standard_Location then
916 return False;
917 end if;
918
919 -- Otherwise compare original locations
920
921 return In_Same_Extended_Unit
922 (Original_Location (Loc),
923 Original_Location (Sloc (Cunit (Main_Unit))));
924 end In_Extended_Main_Source_Unit;
925
926 ----------------------
927 -- In_Internal_Unit --
928 ----------------------
929
930 function In_Internal_Unit (N : Node_Or_Entity_Id) return Boolean is
931 begin
932 return In_Internal_Unit (Sloc (N));
933 end In_Internal_Unit;
934
935 function In_Internal_Unit (S : Source_Ptr) return Boolean is
936 Unit : constant Unit_Number_Type := Get_Source_Unit (S);
937 begin
938 return Is_Internal_Unit (Unit);
939 end In_Internal_Unit;
940
941 ----------------------------
942 -- In_Predefined_Renaming --
943 ----------------------------
944
945 function In_Predefined_Renaming (N : Node_Or_Entity_Id) return Boolean is
946 begin
947 return In_Predefined_Renaming (Sloc (N));
948 end In_Predefined_Renaming;
949
950 function In_Predefined_Renaming (S : Source_Ptr) return Boolean is
951 Unit : constant Unit_Number_Type := Get_Source_Unit (S);
952 begin
953 return Is_Predefined_Renaming (Unit);
954 end In_Predefined_Renaming;
955
956 ---------
957 -- ipu --
958 ---------
959
960 function ipu (N : Node_Or_Entity_Id) return Boolean is
961 begin
962 return In_Predefined_Unit (N);
963 end ipu;
964
965 ------------------------
966 -- In_Predefined_Unit --
967 ------------------------
968
969 function In_Predefined_Unit (N : Node_Or_Entity_Id) return Boolean is
970 begin
971 return In_Predefined_Unit (Sloc (N));
972 end In_Predefined_Unit;
973
974 function In_Predefined_Unit (S : Source_Ptr) return Boolean is
975 Unit : constant Unit_Number_Type := Get_Source_Unit (S);
976 begin
977 return Is_Predefined_Unit (Unit);
978 end In_Predefined_Unit;
979
980 -----------------------
981 -- In_Same_Code_Unit --
982 -----------------------
983
984 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
985 S1 : constant Source_Ptr := Sloc (N1);
986 S2 : constant Source_Ptr := Sloc (N2);
987
988 begin
989 if S1 = No_Location or else S2 = No_Location then
990 return False;
991
992 elsif S1 = Standard_Location then
993 return S2 = Standard_Location;
994
995 elsif S2 = Standard_Location then
996 return False;
997 end if;
998
999 return Get_Code_Unit (N1) = Get_Code_Unit (N2);
1000 end In_Same_Code_Unit;
1001
1002 ---------------------------
1003 -- In_Same_Extended_Unit --
1004 ---------------------------
1005
1006 function In_Same_Extended_Unit
1007 (N1, N2 : Node_Or_Entity_Id) return Boolean
1008 is
1009 begin
1010 return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
1011 end In_Same_Extended_Unit;
1012
1013 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
1014 begin
1015 return Check_Same_Extended_Unit (S1, S2) /= No;
1016 end In_Same_Extended_Unit;
1017
1018 -------------------------
1019 -- In_Same_Source_Unit --
1020 -------------------------
1021
1022 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
1023 S1 : constant Source_Ptr := Sloc (N1);
1024 S2 : constant Source_Ptr := Sloc (N2);
1025
1026 begin
1027 if S1 = No_Location or else S2 = No_Location then
1028 return False;
1029
1030 elsif S1 = Standard_Location then
1031 return S2 = Standard_Location;
1032
1033 elsif S2 = Standard_Location then
1034 return False;
1035 end if;
1036
1037 return Get_Source_Unit (N1) = Get_Source_Unit (N2);
1038 end In_Same_Source_Unit;
1039
1040 -----------------------------------
1041 -- Increment_Primary_Stack_Count --
1042 -----------------------------------
1043
1044 procedure Increment_Primary_Stack_Count (Increment : Nat) is
1045 PSC : Nat renames Units.Table (Current_Sem_Unit).Primary_Stack_Count;
1046 begin
1047 PSC := PSC + Increment;
1048 end Increment_Primary_Stack_Count;
1049
1050 -------------------------------
1051 -- Increment_Sec_Stack_Count --
1052 -------------------------------
1053
1054 procedure Increment_Sec_Stack_Count (Increment : Nat) is
1055 SSC : Nat renames Units.Table (Current_Sem_Unit).Sec_Stack_Count;
1056 begin
1057 SSC := SSC + Increment;
1058 end Increment_Sec_Stack_Count;
1059
1060 -----------------------------
1061 -- Increment_Serial_Number --
1062 -----------------------------
1063
1064 function Increment_Serial_Number return Nat is
1065 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
1066 begin
1067 TSN := TSN + 1;
1068 return TSN;
1069 end Increment_Serial_Number;
1070
1071 ----------------------
1072 -- Init_Unit_Name --
1073 ----------------------
1074
1075 procedure Init_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
1076 begin
1077 Units.Table (U).Unit_Name := N;
1078 Unit_Names.Set (N, U);
1079 end Init_Unit_Name;
1080
1081 ----------------
1082 -- Initialize --
1083 ----------------
1084
1085 procedure Initialize is
1086 begin
1087 Linker_Option_Lines.Init;
1088 Notes.Init;
1089 Load_Stack.Init;
1090 Units.Init;
1091 Compilation_Switches.Init;
1092 end Initialize;
1093
1094 ---------------
1095 -- Is_Loaded --
1096 ---------------
1097
1098 function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
1099 begin
1100 return Unit_Names.Get (Uname) /= No_Unit;
1101 end Is_Loaded;
1102
1103 ---------------
1104 -- Last_Unit --
1105 ---------------
1106
1107 function Last_Unit return Unit_Number_Type is
1108 begin
1109 return Units.Last;
1110 end Last_Unit;
1111
1112 ----------
1113 -- List --
1114 ----------
1115
1116 procedure List (File_Names_Only : Boolean := False) is separate;
1117
1118 ----------
1119 -- Lock --
1120 ----------
1121
1122 procedure Lock is
1123 begin
1124 Linker_Option_Lines.Release;
1125 Linker_Option_Lines.Locked := True;
1126 Load_Stack.Release;
1127 Load_Stack.Locked := True;
1128 Units.Release;
1129 Units.Locked := True;
1130 end Lock;
1131
1132 ---------------
1133 -- Num_Units --
1134 ---------------
1135
1136 function Num_Units return Nat is
1137 begin
1138 return Int (Units.Last) - Int (Main_Unit) + 1;
1139 end Num_Units;
1140
1141 -----------------
1142 -- Remove_Unit --
1143 -----------------
1144
1145 procedure Remove_Unit (U : Unit_Number_Type) is
1146 begin
1147 pragma Assert (U = Units.Last);
1148 Unit_Names.Set (Unit_Name (U), No_Unit);
1149 Units.Decrement_Last;
1150 end Remove_Unit;
1151
1152 ----------------------------------
1153 -- Replace_Linker_Option_String --
1154 ----------------------------------
1155
1156 procedure Replace_Linker_Option_String
1157 (S : String_Id; Match_String : String)
1158 is
1159 begin
1160 if Match_String'Length > 0 then
1161 for J in 1 .. Linker_Option_Lines.Last loop
1162 String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
1163
1164 if Match_String = Name_Buffer (1 .. Match_String'Length) then
1165 Linker_Option_Lines.Table (J).Option := S;
1166 return;
1167 end if;
1168 end loop;
1169 end if;
1170
1171 Store_Linker_Option_String (S);
1172 end Replace_Linker_Option_String;
1173
1174 ----------
1175 -- Sort --
1176 ----------
1177
1178 procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
1179
1180 ------------------------------
1181 -- Store_Compilation_Switch --
1182 ------------------------------
1183
1184 procedure Store_Compilation_Switch (Switch : String) is
1185 begin
1186 if Switch_Storing_Enabled then
1187 Compilation_Switches.Increment_Last;
1188 Compilation_Switches.Table (Compilation_Switches.Last) :=
1189 new String'(Switch);
1190
1191 -- Fix up --RTS flag which has been transformed by the gcc driver
1192 -- into -fRTS
1193
1194 if Switch'Last >= Switch'First + 4
1195 and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
1196 then
1197 Compilation_Switches.Table
1198 (Compilation_Switches.Last) (Switch'First + 1) := '-';
1199 end if;
1200 end if;
1201 end Store_Compilation_Switch;
1202
1203 --------------------------------
1204 -- Store_Linker_Option_String --
1205 --------------------------------
1206
1207 procedure Store_Linker_Option_String (S : String_Id) is
1208 begin
1209 Linker_Option_Lines.Append ((Option => S, Unit => Current_Sem_Unit));
1210 end Store_Linker_Option_String;
1211
1212 ----------------
1213 -- Store_Note --
1214 ----------------
1215
1216 procedure Store_Note (N : Node_Id) is
1217 Sfile : constant Source_File_Index := Get_Source_File_Index (Sloc (N));
1218
1219 begin
1220 -- Notes for a generic are emitted when processing the template, never
1221 -- in instances.
1222
1223 if In_Extended_Main_Code_Unit (N)
1224 and then Instance (Sfile) = No_Instance_Id
1225 then
1226 Notes.Append (N);
1227 end if;
1228 end Store_Note;
1229
1230 -------------------------------
1231 -- Synchronize_Serial_Number --
1232 -------------------------------
1233
1234 procedure Synchronize_Serial_Number (SN : Nat) is
1235 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
1236 begin
1237 -- We should not be trying to synchronize downward
1238
1239 pragma Assert (TSN <= SN);
1240
1241 if TSN < SN then
1242 TSN := SN;
1243 end if;
1244 end Synchronize_Serial_Number;
1245
1246 --------------------
1247 -- Unit_Name_Hash --
1248 --------------------
1249
1250 function Unit_Name_Hash (Id : Unit_Name_Type) return Unit_Name_Header_Num is
1251 begin
1252 return Unit_Name_Header_Num (Id mod Unit_Name_Table_Size);
1253 end Unit_Name_Hash;
1254
1255 ------------
1256 -- Unlock --
1257 ------------
1258
1259 procedure Unlock is
1260 begin
1261 Linker_Option_Lines.Locked := False;
1262 Load_Stack.Locked := False;
1263 Units.Locked := False;
1264 end Unlock;
1265
1266 -----------------
1267 -- Version_Get --
1268 -----------------
1269
1270 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1271 begin
1272 return Get_Hex_String (Units.Table (U).Version);
1273 end Version_Get;
1274
1275 ------------------------
1276 -- Version_Referenced --
1277 ------------------------
1278
1279 procedure Version_Referenced (S : String_Id) is
1280 begin
1281 Version_Ref.Append (S);
1282 end Version_Referenced;
1283
1284 ---------------------
1285 -- Write_Unit_Info --
1286 ---------------------
1287
1288 procedure Write_Unit_Info
1289 (Unit_Num : Unit_Number_Type;
1290 Item : Node_Id;
1291 Prefix : String := "";
1292 Withs : Boolean := False)
1293 is
1294 begin
1295 Write_Str (Prefix);
1296 Write_Unit_Name (Unit_Name (Unit_Num));
1297 Write_Str (", unit ");
1298 Write_Int (Int (Unit_Num));
1299 Write_Str (", ");
1300 Write_Int (Int (Item));
1301 Write_Str ("=");
1302 Write_Str (Node_Kind'Image (Nkind (Item)));
1303
1304 if Is_Rewrite_Substitution (Item) then
1305 Write_Str (", orig = ");
1306 Write_Int (Int (Original_Node (Item)));
1307 Write_Str ("=");
1308 Write_Str (Node_Kind'Image (Nkind (Original_Node (Item))));
1309 end if;
1310
1311 Write_Eol;
1312
1313 -- Skip the rest if we're not supposed to print the withs
1314
1315 if not Withs then
1316 return;
1317 end if;
1318
1319 declare
1320 Context_Item : Node_Id;
1321
1322 begin
1323 Context_Item := First (Context_Items (Cunit (Unit_Num)));
1324 while Present (Context_Item)
1325 and then (Nkind (Context_Item) /= N_With_Clause
1326 or else Limited_Present (Context_Item))
1327 loop
1328 Next (Context_Item);
1329 end loop;
1330
1331 if Present (Context_Item) then
1332 Indent;
1333 Write_Line ("withs:");
1334 Indent;
1335
1336 while Present (Context_Item) loop
1337 if Nkind (Context_Item) = N_With_Clause
1338 and then not Limited_Present (Context_Item)
1339 then
1340 pragma Assert (Present (Withed_Lib_Unit (Context_Item)));
1341 Write_Unit_Name
1342 (Unit_Name
1343 (Get_Cunit_Unit_Number
1344 (Withed_Lib_Unit (Context_Item))));
1345
1346 if Is_Implicit_With (Context_Item) then
1347 Write_Str (" -- implicit");
1348 end if;
1349
1350 Write_Eol;
1351 end if;
1352
1353 Next (Context_Item);
1354 end loop;
1355
1356 Outdent;
1357 Write_Line ("end withs");
1358 Outdent;
1359 end if;
1360 end;
1361 end Write_Unit_Info;
1362
1363 end Lib;