2013-11-13 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / ada / lib.adb
blobe220b20e08e87d28128433310cd1dba4dd57bd11
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- L I B --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2013, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 pragma Style_Checks (All_Checks);
33 -- Subprogram ordering not enforced in this unit
34 -- (because of some logical groupings).
36 with Atree; use Atree;
37 with Csets; use Csets;
38 with Einfo; use Einfo;
39 with Fname; use Fname;
40 with Nlists; use Nlists;
41 with Output; use Output;
42 with Sinfo; use Sinfo;
43 with Sinput; use Sinput;
44 with Stand; use Stand;
45 with Stringt; use Stringt;
46 with Tree_IO; use Tree_IO;
47 with Uname; use Uname;
48 with Widechar; use Widechar;
50 package body Lib is
52 Switch_Storing_Enabled : Boolean := True;
53 -- Controlled by Enable_Switch_Storing/Disable_Switch_Storing
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 type SEU_Result is (
60 Yes_Before, -- S1 is in same extended unit as S2 and appears before it
61 Yes_Same, -- S1 is in same extended unit as S2, Slocs are the same
62 Yes_After, -- S1 is in same extended unit as S2, and appears after it
63 No); -- S2 is not in same extended unit as S2
65 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result;
66 -- Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
67 -- value as described above.
69 function Get_Code_Or_Source_Unit
70 (S : Source_Ptr;
71 Unwind_Instances : Boolean) return Unit_Number_Type;
72 -- Common code for Get_Code_Unit (get unit of instantiation for location)
73 -- and Get_Source_Unit (get unit of template for location).
75 --------------------------------------------
76 -- Access Functions for Unit Table Fields --
77 --------------------------------------------
79 function Cunit (U : Unit_Number_Type) return Node_Id is
80 begin
81 return Units.Table (U).Cunit;
82 end Cunit;
84 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
85 begin
86 return Units.Table (U).Cunit_Entity;
87 end Cunit_Entity;
89 function Dependency_Num (U : Unit_Number_Type) return Nat is
90 begin
91 return Units.Table (U).Dependency_Num;
92 end Dependency_Num;
94 function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
95 begin
96 return Units.Table (U).Dynamic_Elab;
97 end Dynamic_Elab;
99 function Error_Location (U : Unit_Number_Type) return Source_Ptr is
100 begin
101 return Units.Table (U).Error_Location;
102 end Error_Location;
104 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
105 begin
106 return Units.Table (U).Expected_Unit;
107 end Expected_Unit;
109 function Fatal_Error (U : Unit_Number_Type) return Boolean is
110 begin
111 return Units.Table (U).Fatal_Error;
112 end Fatal_Error;
114 function Generate_Code (U : Unit_Number_Type) return Boolean is
115 begin
116 return Units.Table (U).Generate_Code;
117 end Generate_Code;
119 function Has_Allocator (U : Unit_Number_Type) return Boolean is
120 begin
121 return Units.Table (U).Has_Allocator;
122 end Has_Allocator;
124 function Has_RACW (U : Unit_Number_Type) return Boolean is
125 begin
126 return Units.Table (U).Has_RACW;
127 end Has_RACW;
129 function Is_Compiler_Unit (U : Unit_Number_Type) return Boolean is
130 begin
131 return Units.Table (U).Is_Compiler_Unit;
132 end Is_Compiler_Unit;
134 function Ident_String (U : Unit_Number_Type) return Node_Id is
135 begin
136 return Units.Table (U).Ident_String;
137 end Ident_String;
139 function Loading (U : Unit_Number_Type) return Boolean is
140 begin
141 return Units.Table (U).Loading;
142 end Loading;
144 function Main_CPU (U : Unit_Number_Type) return Int is
145 begin
146 return Units.Table (U).Main_CPU;
147 end Main_CPU;
149 function Main_Priority (U : Unit_Number_Type) return Int is
150 begin
151 return Units.Table (U).Main_Priority;
152 end Main_Priority;
154 function Munit_Index (U : Unit_Number_Type) return Nat is
155 begin
156 return Units.Table (U).Munit_Index;
157 end Munit_Index;
159 function OA_Setting (U : Unit_Number_Type) return Character is
160 begin
161 return Units.Table (U).OA_Setting;
162 end OA_Setting;
164 function Source_Index (U : Unit_Number_Type) return Source_File_Index is
165 begin
166 return Units.Table (U).Source_Index;
167 end Source_Index;
169 function SPARK_Mode_Pragma (U : Unit_Number_Type) return Node_Id is
170 begin
171 return Units.Table (U).SPARK_Mode_Pragma;
172 end SPARK_Mode_Pragma;
174 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
175 begin
176 return Units.Table (U).Unit_File_Name;
177 end Unit_File_Name;
179 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
180 begin
181 return Units.Table (U).Unit_Name;
182 end Unit_Name;
184 ------------------------------------------
185 -- Subprograms to Set Unit Table Fields --
186 ------------------------------------------
188 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
189 begin
190 Units.Table (U).Cunit := N;
191 end Set_Cunit;
193 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
194 begin
195 Units.Table (U).Cunit_Entity := E;
196 Set_Is_Compilation_Unit (E);
197 end Set_Cunit_Entity;
199 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
200 begin
201 Units.Table (U).Dynamic_Elab := B;
202 end Set_Dynamic_Elab;
204 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
205 begin
206 Units.Table (U).Error_Location := W;
207 end Set_Error_Location;
209 procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
210 begin
211 Units.Table (U).Fatal_Error := B;
212 end Set_Fatal_Error;
214 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
215 begin
216 Units.Table (U).Generate_Code := B;
217 end Set_Generate_Code;
219 procedure Set_Has_Allocator (U : Unit_Number_Type; B : Boolean := True) is
220 begin
221 Units.Table (U).Has_Allocator := B;
222 end Set_Has_Allocator;
224 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
225 begin
226 Units.Table (U).Has_RACW := B;
227 end Set_Has_RACW;
229 procedure Set_Is_Compiler_Unit
230 (U : Unit_Number_Type;
231 B : Boolean := True)
233 begin
234 Units.Table (U).Is_Compiler_Unit := B;
235 end Set_Is_Compiler_Unit;
237 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
238 begin
239 Units.Table (U).Ident_String := N;
240 end Set_Ident_String;
242 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
243 begin
244 Units.Table (U).Loading := B;
245 end Set_Loading;
247 procedure Set_Main_CPU (U : Unit_Number_Type; P : Int) is
248 begin
249 Units.Table (U).Main_CPU := P;
250 end Set_Main_CPU;
252 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
253 begin
254 Units.Table (U).Main_Priority := P;
255 end Set_Main_Priority;
257 procedure Set_OA_Setting (U : Unit_Number_Type; C : Character) is
258 begin
259 Units.Table (U).OA_Setting := C;
260 end Set_OA_Setting;
262 procedure Set_SPARK_Mode_Pragma (U : Unit_Number_Type; N : Node_Id) is
263 begin
264 Units.Table (U).SPARK_Mode_Pragma := N;
265 end Set_SPARK_Mode_Pragma;
267 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
268 begin
269 Units.Table (U).Unit_Name := N;
270 end Set_Unit_Name;
272 ------------------------------
273 -- Check_Same_Extended_Unit --
274 ------------------------------
276 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
277 Sloc1 : Source_Ptr;
278 Sloc2 : Source_Ptr;
279 Sind1 : Source_File_Index;
280 Sind2 : Source_File_Index;
281 Inst1 : Source_Ptr;
282 Inst2 : Source_Ptr;
283 Unum1 : Unit_Number_Type;
284 Unum2 : Unit_Number_Type;
285 Unit1 : Node_Id;
286 Unit2 : Node_Id;
287 Depth1 : Nat;
288 Depth2 : Nat;
290 begin
291 if S1 = No_Location or else S2 = No_Location then
292 return No;
294 elsif S1 = Standard_Location then
295 if S2 = Standard_Location then
296 return Yes_Same;
297 else
298 return No;
299 end if;
301 elsif S2 = Standard_Location then
302 return No;
303 end if;
305 Sloc1 := S1;
306 Sloc2 := S2;
308 Unum1 := Get_Source_Unit (Sloc1);
309 Unum2 := Get_Source_Unit (Sloc2);
311 loop
312 -- Step 1: Check whether the two locations are in the same source
313 -- file.
315 Sind1 := Get_Source_File_Index (Sloc1);
316 Sind2 := Get_Source_File_Index (Sloc2);
318 if Sind1 = Sind2 then
319 if Sloc1 < Sloc2 then
320 return Yes_Before;
321 elsif Sloc1 > Sloc2 then
322 return Yes_After;
323 else
324 return Yes_Same;
325 end if;
326 end if;
328 -- Step 2: Check subunits. If a subunit is instantiated, follow the
329 -- instantiation chain rather than the stub chain.
331 Unit1 := Unit (Cunit (Unum1));
332 Unit2 := Unit (Cunit (Unum2));
333 Inst1 := Instantiation (Sind1);
334 Inst2 := Instantiation (Sind2);
336 if Nkind (Unit1) = N_Subunit
337 and then Present (Corresponding_Stub (Unit1))
338 and then Inst1 = No_Location
339 then
340 if Nkind (Unit2) = N_Subunit
341 and then Present (Corresponding_Stub (Unit2))
342 and then Inst2 = No_Location
343 then
344 -- Both locations refer to subunits which may have a common
345 -- ancestor. If they do, the deeper subunit must have a longer
346 -- unit name. Replace the deeper one with its corresponding
347 -- stub in order to find the nearest ancestor.
349 if Length_Of_Name (Unit_Name (Unum1)) <
350 Length_Of_Name (Unit_Name (Unum2))
351 then
352 Sloc2 := Sloc (Corresponding_Stub (Unit2));
353 Unum2 := Get_Source_Unit (Sloc2);
354 goto Continue;
356 else
357 Sloc1 := Sloc (Corresponding_Stub (Unit1));
358 Unum1 := Get_Source_Unit (Sloc1);
359 goto Continue;
360 end if;
362 -- Sloc1 in subunit, Sloc2 not
364 else
365 Sloc1 := Sloc (Corresponding_Stub (Unit1));
366 Unum1 := Get_Source_Unit (Sloc1);
367 goto Continue;
368 end if;
370 -- Sloc2 in subunit, Sloc1 not
372 elsif Nkind (Unit2) = N_Subunit
373 and then Present (Corresponding_Stub (Unit2))
374 and then Inst2 = No_Location
375 then
376 Sloc2 := Sloc (Corresponding_Stub (Unit2));
377 Unum2 := Get_Source_Unit (Sloc2);
378 goto Continue;
379 end if;
381 -- Step 3: Check instances. The two locations may yield a common
382 -- ancestor.
384 if Inst1 /= No_Location then
385 if Inst2 /= No_Location then
387 -- Both locations denote instantiations
389 Depth1 := Instantiation_Depth (Sloc1);
390 Depth2 := Instantiation_Depth (Sloc2);
392 if Depth1 < Depth2 then
393 Sloc2 := Inst2;
394 Unum2 := Get_Source_Unit (Sloc2);
395 goto Continue;
397 elsif Depth1 > Depth2 then
398 Sloc1 := Inst1;
399 Unum1 := Get_Source_Unit (Sloc1);
400 goto Continue;
402 else
403 Sloc1 := Inst1;
404 Sloc2 := Inst2;
405 Unum1 := Get_Source_Unit (Sloc1);
406 Unum2 := Get_Source_Unit (Sloc2);
407 goto Continue;
408 end if;
410 -- Sloc1 is an instantiation
412 else
413 Sloc1 := Inst1;
414 Unum1 := Get_Source_Unit (Sloc1);
415 goto Continue;
416 end if;
418 -- Sloc2 is an instantiation
420 elsif Inst2 /= No_Location then
421 Sloc2 := Inst2;
422 Unum2 := Get_Source_Unit (Sloc2);
423 goto Continue;
424 end if;
426 -- Step 4: One location in the spec, the other in the corresponding
427 -- body of the same unit. The location in the spec is considered
428 -- earlier.
430 if Nkind (Unit1) = N_Subprogram_Body
431 or else
432 Nkind (Unit1) = N_Package_Body
433 then
434 if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
435 return Yes_After;
436 end if;
438 elsif Nkind (Unit2) = N_Subprogram_Body
439 or else
440 Nkind (Unit2) = N_Package_Body
441 then
442 if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
443 return Yes_Before;
444 end if;
445 end if;
447 -- At this point it is certain that the two locations denote two
448 -- entirely separate units.
450 return No;
452 <<Continue>>
453 null;
454 end loop;
455 end Check_Same_Extended_Unit;
457 -------------------------------
458 -- Compilation_Switches_Last --
459 -------------------------------
461 function Compilation_Switches_Last return Nat is
462 begin
463 return Compilation_Switches.Last;
464 end Compilation_Switches_Last;
466 ---------------------------
467 -- Enable_Switch_Storing --
468 ---------------------------
470 procedure Enable_Switch_Storing is
471 begin
472 Switch_Storing_Enabled := True;
473 end Enable_Switch_Storing;
475 ----------------------------
476 -- Disable_Switch_Storing --
477 ----------------------------
479 procedure Disable_Switch_Storing is
480 begin
481 Switch_Storing_Enabled := False;
482 end Disable_Switch_Storing;
484 ------------------------------
485 -- Earlier_In_Extended_Unit --
486 ------------------------------
488 function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
489 begin
490 return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
491 end Earlier_In_Extended_Unit;
493 -----------------------
494 -- Exact_Source_Name --
495 -----------------------
497 function Exact_Source_Name (Loc : Source_Ptr) return String is
498 U : constant Unit_Number_Type := Get_Source_Unit (Loc);
499 Buf : constant Source_Buffer_Ptr := Source_Text (Source_Index (U));
500 Orig : constant Source_Ptr := Original_Location (Loc);
501 P : Source_Ptr;
503 WC : Char_Code;
504 Err : Boolean;
505 pragma Warnings (Off, WC);
506 pragma Warnings (Off, Err);
508 begin
509 -- Entity is character literal
511 if Buf (Orig) = ''' then
512 return String (Buf (Orig .. Orig + 2));
514 -- Entity is operator symbol
516 elsif Buf (Orig) = '"' or else Buf (Orig) = '%' then
517 P := Orig;
519 loop
520 P := P + 1;
521 exit when Buf (P) = Buf (Orig);
522 end loop;
524 return String (Buf (Orig .. P));
526 -- Entity is identifier
528 else
529 P := Orig;
531 loop
532 if Is_Start_Of_Wide_Char (Buf, P) then
533 Scan_Wide (Buf, P, WC, Err);
534 elsif not Identifier_Char (Buf (P)) then
535 exit;
536 else
537 P := P + 1;
538 end if;
539 end loop;
541 -- Write out the identifier by copying the exact source characters
542 -- used in its declaration. Note that this means wide characters will
543 -- be in their original encoded form.
545 return String (Buf (Orig .. P - 1));
546 end if;
547 end Exact_Source_Name;
549 ----------------------------
550 -- Entity_Is_In_Main_Unit --
551 ----------------------------
553 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
554 S : Entity_Id;
556 begin
557 S := Scope (E);
559 while S /= Standard_Standard loop
560 if S = Main_Unit_Entity then
561 return True;
562 elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
563 return False;
564 else
565 S := Scope (S);
566 end if;
567 end loop;
569 return False;
570 end Entity_Is_In_Main_Unit;
572 --------------------------
573 -- Generic_May_Lack_ALI --
574 --------------------------
576 function Generic_May_Lack_ALI (Sfile : File_Name_Type) return Boolean is
577 begin
578 -- We allow internal generic units to be used without having a
579 -- corresponding ALI files to help bootstrapping with older compilers
580 -- that did not support generating ALIs for such generics. It is safe
581 -- to do so because the only thing the generated code would contain
582 -- is the elaboration boolean, and we are careful to elaborate all
583 -- predefined units first anyway.
585 return Is_Internal_File_Name
586 (Fname => Sfile,
587 Renamings_Included => True);
588 end Generic_May_Lack_ALI;
590 -----------------------------
591 -- Get_Code_Or_Source_Unit --
592 -----------------------------
594 function Get_Code_Or_Source_Unit
595 (S : Source_Ptr;
596 Unwind_Instances : Boolean) return Unit_Number_Type
598 begin
599 -- Search table unless we have No_Location, which can happen if the
600 -- relevant location has not been set yet. Happens for example when
601 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
603 if S /= No_Location then
604 declare
605 Source_File : Source_File_Index;
606 Source_Unit : Unit_Number_Type;
608 begin
609 Source_File := Get_Source_File_Index (S);
611 if Unwind_Instances then
612 while Template (Source_File) /= No_Source_File loop
613 Source_File := Template (Source_File);
614 end loop;
615 end if;
617 Source_Unit := Unit (Source_File);
619 if Source_Unit /= No_Unit then
620 return Source_Unit;
621 end if;
622 end;
623 end if;
625 -- If S was No_Location, or was not in the table, we must be in the main
626 -- source unit (and the value has not been placed in the table yet),
627 -- or in one of the configuration pragma files.
629 return Main_Unit;
630 end Get_Code_Or_Source_Unit;
632 -------------------
633 -- Get_Code_Unit --
634 -------------------
636 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
637 begin
638 return Get_Code_Or_Source_Unit (Top_Level_Location (S),
639 Unwind_Instances => False);
640 end Get_Code_Unit;
642 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
643 begin
644 return Get_Code_Unit (Sloc (N));
645 end Get_Code_Unit;
647 ----------------------------
648 -- Get_Compilation_Switch --
649 ----------------------------
651 function Get_Compilation_Switch (N : Pos) return String_Ptr is
652 begin
653 if N <= Compilation_Switches.Last then
654 return Compilation_Switches.Table (N);
656 else
657 return null;
658 end if;
659 end Get_Compilation_Switch;
661 ----------------------------------
662 -- Get_Cunit_Entity_Unit_Number --
663 ----------------------------------
665 function Get_Cunit_Entity_Unit_Number
666 (E : Entity_Id) return Unit_Number_Type
668 begin
669 for U in Units.First .. Units.Last loop
670 if Cunit_Entity (U) = E then
671 return U;
672 end if;
673 end loop;
675 -- If not in the table, must be the main source unit, and we just
676 -- have not got it put into the table yet.
678 return Main_Unit;
679 end Get_Cunit_Entity_Unit_Number;
681 ---------------------------
682 -- Get_Cunit_Unit_Number --
683 ---------------------------
685 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
686 begin
687 for U in Units.First .. Units.Last loop
688 if Cunit (U) = N then
689 return U;
690 end if;
691 end loop;
693 -- If not in the table, must be a spec created for a main unit that is a
694 -- child subprogram body which we have not inserted into the table yet.
696 if N = Library_Unit (Cunit (Main_Unit)) then
697 return Main_Unit;
699 -- If it is anything else, something is seriously wrong, and we really
700 -- don't want to proceed, even if assertions are off, so we explicitly
701 -- raise an exception in this case to terminate compilation.
703 else
704 raise Program_Error;
705 end if;
706 end Get_Cunit_Unit_Number;
708 ---------------------
709 -- Get_Source_Unit --
710 ---------------------
712 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
713 begin
714 return Get_Code_Or_Source_Unit (S, Unwind_Instances => True);
715 end Get_Source_Unit;
717 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
718 begin
719 return Get_Source_Unit (Sloc (N));
720 end Get_Source_Unit;
722 --------------------------------
723 -- In_Extended_Main_Code_Unit --
724 --------------------------------
726 function In_Extended_Main_Code_Unit
727 (N : Node_Or_Entity_Id) return Boolean
729 begin
730 if Sloc (N) = Standard_Location then
731 return True;
733 elsif Sloc (N) = No_Location then
734 return False;
736 -- Special case Itypes to test the Sloc of the associated node. The
737 -- reason we do this is for possible calls from gigi after -gnatD
738 -- processing is complete in sprint. This processing updates the
739 -- sloc fields of all nodes in the tree, but itypes are not in the
740 -- tree so their slocs do not get updated.
742 elsif Nkind (N) = N_Defining_Identifier
743 and then Is_Itype (N)
744 then
745 return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
747 -- Otherwise see if we are in the main unit
749 elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
750 return True;
752 -- Node may be in spec (or subunit etc) of main unit
754 else
755 return
756 In_Same_Extended_Unit (N, Cunit (Main_Unit));
757 end if;
758 end In_Extended_Main_Code_Unit;
760 function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
761 begin
762 if Loc = Standard_Location then
763 return True;
765 elsif Loc = No_Location then
766 return False;
768 -- Otherwise see if we are in the main unit
770 elsif Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
771 return True;
773 -- Location may be in spec (or subunit etc) of main unit
775 else
776 return
777 In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
778 end if;
779 end In_Extended_Main_Code_Unit;
781 ----------------------------------
782 -- In_Extended_Main_Source_Unit --
783 ----------------------------------
785 function In_Extended_Main_Source_Unit
786 (N : Node_Or_Entity_Id) return Boolean
788 Nloc : constant Source_Ptr := Sloc (N);
789 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
791 begin
792 -- If parsing, then use the global flag to indicate result
794 if Compiler_State = Parsing then
795 return Parsing_Main_Extended_Source;
797 -- Special value cases
799 elsif Nloc = Standard_Location then
800 return True;
802 elsif Nloc = No_Location then
803 return False;
805 -- Special case Itypes to test the Sloc of the associated node. The
806 -- reason we do this is for possible calls from gigi after -gnatD
807 -- processing is complete in sprint. This processing updates the
808 -- sloc fields of all nodes in the tree, but itypes are not in the
809 -- tree so their slocs do not get updated.
811 elsif Nkind (N) = N_Defining_Identifier
812 and then Is_Itype (N)
813 then
814 return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
816 -- Otherwise compare original locations to see if in same unit
818 else
819 return
820 In_Same_Extended_Unit
821 (Original_Location (Nloc), Original_Location (Mloc));
822 end if;
823 end In_Extended_Main_Source_Unit;
825 function In_Extended_Main_Source_Unit
826 (Loc : Source_Ptr) return Boolean
828 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
830 begin
831 -- If parsing, then use the global flag to indicate result
833 if Compiler_State = Parsing then
834 return Parsing_Main_Extended_Source;
836 -- Special value cases
838 elsif Loc = Standard_Location then
839 return True;
841 elsif Loc = No_Location then
842 return False;
844 -- Otherwise compare original locations to see if in same unit
846 else
847 return
848 In_Same_Extended_Unit
849 (Original_Location (Loc), Original_Location (Mloc));
850 end if;
851 end In_Extended_Main_Source_Unit;
853 ------------------------
854 -- In_Predefined_Unit --
855 ------------------------
857 function In_Predefined_Unit (N : Node_Or_Entity_Id) return Boolean is
858 begin
859 return In_Predefined_Unit (Sloc (N));
860 end In_Predefined_Unit;
862 function In_Predefined_Unit (S : Source_Ptr) return Boolean is
863 Unit : constant Unit_Number_Type := Get_Source_Unit (S);
864 File : constant File_Name_Type := Unit_File_Name (Unit);
865 begin
866 return Is_Predefined_File_Name (File);
867 end In_Predefined_Unit;
869 -----------------------
870 -- In_Same_Code_Unit --
871 -----------------------
873 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
874 S1 : constant Source_Ptr := Sloc (N1);
875 S2 : constant Source_Ptr := Sloc (N2);
877 begin
878 if S1 = No_Location or else S2 = No_Location then
879 return False;
881 elsif S1 = Standard_Location then
882 return S2 = Standard_Location;
884 elsif S2 = Standard_Location then
885 return False;
886 end if;
888 return Get_Code_Unit (N1) = Get_Code_Unit (N2);
889 end In_Same_Code_Unit;
891 ---------------------------
892 -- In_Same_Extended_Unit --
893 ---------------------------
895 function In_Same_Extended_Unit
896 (N1, N2 : Node_Or_Entity_Id) return Boolean
898 begin
899 return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
900 end In_Same_Extended_Unit;
902 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
903 begin
904 return Check_Same_Extended_Unit (S1, S2) /= No;
905 end In_Same_Extended_Unit;
907 -------------------------
908 -- In_Same_Source_Unit --
909 -------------------------
911 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
912 S1 : constant Source_Ptr := Sloc (N1);
913 S2 : constant Source_Ptr := Sloc (N2);
915 begin
916 if S1 = No_Location or else S2 = No_Location then
917 return False;
919 elsif S1 = Standard_Location then
920 return S2 = Standard_Location;
922 elsif S2 = Standard_Location then
923 return False;
924 end if;
926 return Get_Source_Unit (N1) = Get_Source_Unit (N2);
927 end In_Same_Source_Unit;
929 -----------------------------
930 -- Increment_Serial_Number --
931 -----------------------------
933 function Increment_Serial_Number return Nat is
934 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
935 begin
936 TSN := TSN + 1;
937 return TSN;
938 end Increment_Serial_Number;
940 ----------------
941 -- Initialize --
942 ----------------
944 procedure Initialize is
945 begin
946 Linker_Option_Lines.Init;
947 Notes.Init;
948 Load_Stack.Init;
949 Units.Init;
950 Compilation_Switches.Init;
951 end Initialize;
953 ---------------
954 -- Is_Loaded --
955 ---------------
957 function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
958 begin
959 for Unum in Units.First .. Units.Last loop
960 if Uname = Unit_Name (Unum) then
961 return True;
962 end if;
963 end loop;
965 return False;
966 end Is_Loaded;
968 ---------------
969 -- Last_Unit --
970 ---------------
972 function Last_Unit return Unit_Number_Type is
973 begin
974 return Units.Last;
975 end Last_Unit;
977 ----------
978 -- List --
979 ----------
981 procedure List (File_Names_Only : Boolean := False) is separate;
983 ----------
984 -- Lock --
985 ----------
987 procedure Lock is
988 begin
989 Linker_Option_Lines.Locked := True;
990 Load_Stack.Locked := True;
991 Units.Locked := True;
992 Linker_Option_Lines.Release;
993 Load_Stack.Release;
994 Units.Release;
995 end Lock;
997 ---------------
998 -- Num_Units --
999 ---------------
1001 function Num_Units return Nat is
1002 begin
1003 return Int (Units.Last) - Int (Main_Unit) + 1;
1004 end Num_Units;
1006 -----------------
1007 -- Remove_Unit --
1008 -----------------
1010 procedure Remove_Unit (U : Unit_Number_Type) is
1011 begin
1012 if U = Units.Last then
1013 Units.Decrement_Last;
1014 end if;
1015 end Remove_Unit;
1017 ----------------------------------
1018 -- Replace_Linker_Option_String --
1019 ----------------------------------
1021 procedure Replace_Linker_Option_String
1022 (S : String_Id; Match_String : String)
1024 begin
1025 if Match_String'Length > 0 then
1026 for J in 1 .. Linker_Option_Lines.Last loop
1027 String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
1029 if Match_String = Name_Buffer (1 .. Match_String'Length) then
1030 Linker_Option_Lines.Table (J).Option := S;
1031 return;
1032 end if;
1033 end loop;
1034 end if;
1036 Store_Linker_Option_String (S);
1037 end Replace_Linker_Option_String;
1039 ----------
1040 -- Sort --
1041 ----------
1043 procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
1045 ------------------------------
1046 -- Store_Compilation_Switch --
1047 ------------------------------
1049 procedure Store_Compilation_Switch (Switch : String) is
1050 begin
1051 if Switch_Storing_Enabled then
1052 Compilation_Switches.Increment_Last;
1053 Compilation_Switches.Table (Compilation_Switches.Last) :=
1054 new String'(Switch);
1056 -- Fix up --RTS flag which has been transformed by the gcc driver
1057 -- into -fRTS
1059 if Switch'Last >= Switch'First + 4
1060 and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
1061 then
1062 Compilation_Switches.Table
1063 (Compilation_Switches.Last) (Switch'First + 1) := '-';
1064 end if;
1065 end if;
1066 end Store_Compilation_Switch;
1068 --------------------------------
1069 -- Store_Linker_Option_String --
1070 --------------------------------
1072 procedure Store_Linker_Option_String (S : String_Id) is
1073 begin
1074 Linker_Option_Lines.Append ((Option => S, Unit => Current_Sem_Unit));
1075 end Store_Linker_Option_String;
1077 ----------------
1078 -- Store_Note --
1079 ----------------
1081 procedure Store_Note (N : Node_Id) is
1082 begin
1083 Notes.Append ((Pragma_Node => N, Unit => Current_Sem_Unit));
1084 end Store_Note;
1086 -------------------------------
1087 -- Synchronize_Serial_Number --
1088 -------------------------------
1090 procedure Synchronize_Serial_Number is
1091 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
1092 begin
1093 TSN := TSN + 1;
1094 end Synchronize_Serial_Number;
1096 ---------------
1097 -- Tree_Read --
1098 ---------------
1100 procedure Tree_Read is
1101 N : Nat;
1102 S : String_Ptr;
1104 begin
1105 Units.Tree_Read;
1107 -- Read Compilation_Switches table. First release the memory occupied
1108 -- by the previously loaded switches.
1110 for J in Compilation_Switches.First .. Compilation_Switches.Last loop
1111 Free (Compilation_Switches.Table (J));
1112 end loop;
1114 Tree_Read_Int (N);
1115 Compilation_Switches.Set_Last (N);
1117 for J in 1 .. N loop
1118 Tree_Read_Str (S);
1119 Compilation_Switches.Table (J) := S;
1120 end loop;
1121 end Tree_Read;
1123 ----------------
1124 -- Tree_Write --
1125 ----------------
1127 procedure Tree_Write is
1128 begin
1129 Units.Tree_Write;
1131 -- Write Compilation_Switches table
1133 Tree_Write_Int (Compilation_Switches.Last);
1135 for J in 1 .. Compilation_Switches.Last loop
1136 Tree_Write_Str (Compilation_Switches.Table (J));
1137 end loop;
1138 end Tree_Write;
1140 ------------
1141 -- Unlock --
1142 ------------
1144 procedure Unlock is
1145 begin
1146 Linker_Option_Lines.Locked := False;
1147 Load_Stack.Locked := False;
1148 Units.Locked := False;
1149 end Unlock;
1151 -----------------
1152 -- Version_Get --
1153 -----------------
1155 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1156 begin
1157 return Get_Hex_String (Units.Table (U).Version);
1158 end Version_Get;
1160 ------------------------
1161 -- Version_Referenced --
1162 ------------------------
1164 procedure Version_Referenced (S : String_Id) is
1165 begin
1166 Version_Ref.Append (S);
1167 end Version_Referenced;
1169 ---------------------
1170 -- Write_Unit_Info --
1171 ---------------------
1173 procedure Write_Unit_Info
1174 (Unit_Num : Unit_Number_Type;
1175 Item : Node_Id;
1176 Prefix : String := "";
1177 Withs : Boolean := False)
1179 begin
1180 Write_Str (Prefix);
1181 Write_Unit_Name (Unit_Name (Unit_Num));
1182 Write_Str (", unit ");
1183 Write_Int (Int (Unit_Num));
1184 Write_Str (", ");
1185 Write_Int (Int (Item));
1186 Write_Str ("=");
1187 Write_Str (Node_Kind'Image (Nkind (Item)));
1189 if Item /= Original_Node (Item) then
1190 Write_Str (", orig = ");
1191 Write_Int (Int (Original_Node (Item)));
1192 Write_Str ("=");
1193 Write_Str (Node_Kind'Image (Nkind (Original_Node (Item))));
1194 end if;
1196 Write_Eol;
1198 -- Skip the rest if we're not supposed to print the withs
1200 if not Withs then
1201 return;
1202 end if;
1204 declare
1205 Context_Item : Node_Id;
1207 begin
1208 Context_Item := First (Context_Items (Cunit (Unit_Num)));
1209 while Present (Context_Item)
1210 and then (Nkind (Context_Item) /= N_With_Clause
1211 or else Limited_Present (Context_Item))
1212 loop
1213 Context_Item := Next (Context_Item);
1214 end loop;
1216 if Present (Context_Item) then
1217 Indent;
1218 Write_Line ("withs:");
1219 Indent;
1221 while Present (Context_Item) loop
1222 if Nkind (Context_Item) = N_With_Clause
1223 and then not Limited_Present (Context_Item)
1224 then
1225 pragma Assert (Present (Library_Unit (Context_Item)));
1226 Write_Unit_Name
1227 (Unit_Name
1228 (Get_Cunit_Unit_Number (Library_Unit (Context_Item))));
1230 if Implicit_With (Context_Item) then
1231 Write_Str (" -- implicit");
1232 end if;
1234 Write_Eol;
1235 end if;
1237 Context_Item := Next (Context_Item);
1238 end loop;
1240 Outdent;
1241 Write_Line ("end withs");
1242 Outdent;
1243 end if;
1244 end;
1245 end Write_Unit_Info;
1247 end Lib;