1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
30 with Atree; use Atree;
31 with Casing; use Casing;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Lib.Util; use Lib.Util;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Gnatvsn; use Gnatvsn;
42 with Osint; use Osint;
44 with Restrict; use Restrict;
46 with Sinfo; use Sinfo;
47 with Sinput; use Sinput;
48 with Stringt; use Stringt;
49 with Targparm; use Targparm;
50 with Uname; use Uname;
52 with System.WCh_Con; use System.WCh_Con;
54 package body Lib.Writ is
56 ------------------------------
57 -- Ensure_System_Dependency --
58 ------------------------------
60 procedure Ensure_System_Dependency is
63 System_Uname : Unit_Name_Type;
64 -- Unit name for system spec if needed for dummy entry
66 System_Fname : File_Name_Type;
67 -- File name for system spec if needed for dummy entry
70 -- Nothing to do if we already compiled System
72 for Unum in Units.First .. Last_Unit loop
73 if Units.Table (Unum).Source_Index = System_Source_File_Index then
78 -- If no entry for system.ads in the units table, then add a entry
79 -- to the units table for system.ads, which will be referenced when
80 -- the ali file is generated. We need this because every unit depends
81 -- on system as a result of Targparm scanning the system.ads file to
82 -- determine the target dependent parameters for the compilation.
85 Name_Buffer (1 .. 6) := "system";
86 System_Uname := Name_To_Unit_Name (Name_Enter);
87 System_Fname := File_Name (System_Source_File_Index);
90 Units.Table (Units.Last) := (
91 Unit_File_Name => System_Fname,
92 Unit_Name => System_Uname,
93 Expected_Unit => System_Uname,
94 Source_Index => System_Source_File_Index,
96 Cunit_Entity => Empty,
98 Dependent_Unit => True,
99 Dynamic_Elab => False,
100 Fatal_Error => False,
101 Generate_Code => False,
103 Ident_String => Empty,
108 Error_Location => No_Location);
110 -- Parse system.ads so that the checksum is set right
112 Initialize_Scanner (Units.Last, System_Source_File_Index);
113 Discard := Par (Configuration_Pragmas => False);
114 end Ensure_System_Dependency;
120 procedure Write_ALI (Object : Boolean) is
126 Last_Unit : constant Unit_Number_Type := Units.Last;
127 -- Record unit number of last unit. We capture this in case we
128 -- have to add a dummy entry to the unit table for package System.
130 With_Flags : array (Units.First .. Last_Unit) of Boolean;
131 -- Array of flags to show which units are with'ed
133 Elab_Flags : array (Units.First .. Last_Unit) of Boolean;
134 -- Array of flags to show which units have pragma Elaborate set
136 Elab_All_Flags : array (Units.First .. Last_Unit) of Boolean;
137 -- Array of flags to show which units have pragma Elaborate All set
139 Elab_Des_Flags : array (Units.First .. Last_Unit) of Boolean;
140 -- Array of flags to show which units have Elaborate_All_Desirable set
142 Sdep_Table : Unit_Ref_Table (1 .. Pos (Last_Unit - Units.First + 2));
143 -- Sorted table of source dependencies. One extra entry in case we
144 -- have to add a dummy entry for System.
147 -- Number of active entries in Sdep_Table
149 -----------------------
150 -- Local Subprograms --
151 -----------------------
153 procedure Collect_Withs (Cunit : Node_Id);
154 -- Collect with lines for entries in the context clause of the
155 -- given compilation unit, Cunit.
157 procedure Update_Tables_From_ALI_File;
158 -- Given an up to date ALI file (see Up_To_Date_ALI_file_Exists
159 -- function), update tables from the ALI information, including
160 -- specifically the Compilation_Switches table.
162 function Up_To_Date_ALI_File_Exists return Boolean;
163 -- If there exists an ALI file that is up to date, then this function
164 -- initializes the tables in the ALI spec to contain information on
165 -- this file (using Scan_ALI) and returns True. If no file exists,
166 -- or the file is not up to date, then False is returned.
168 procedure Write_Unit_Information (Unit_Num : Unit_Number_Type);
169 -- Write out the library information for one unit for which code is
170 -- generated (includes unit line and with lines).
172 procedure Write_With_Lines;
173 -- Write out with lines collected by calls to Collect_Withs
179 procedure Collect_Withs (Cunit : Node_Id) is
181 Unum : Unit_Number_Type;
184 Item := First (Context_Items (Cunit));
185 while Present (Item) loop
187 if Nkind (Item) = N_With_Clause then
188 Unum := Get_Cunit_Unit_Number (Library_Unit (Item));
189 With_Flags (Unum) := True;
191 if Elaborate_Present (Item) then
192 Elab_Flags (Unum) := True;
195 if Elaborate_All_Present (Item) then
196 Elab_All_Flags (Unum) := True;
199 if Elaborate_All_Desirable (Cunit_Entity (Unum)) then
200 Elab_Des_Flags (Unum) := True;
208 --------------------------------
209 -- Up_To_Date_ALI_File_Exists --
210 --------------------------------
212 function Up_To_Date_ALI_File_Exists return Boolean is
213 Name : File_Name_Type;
214 Text : Text_Buffer_Ptr;
216 Sind : Source_File_Index;
219 Opt.Check_Object_Consistency := True;
220 Read_Library_Info (Name, Text);
222 -- Return if we could not find an ALI file
228 -- Return if ALI file has bad format
232 if Scan_ALI (Name, Text, False, Err => True) = No_ALI_Id then
236 -- If we have an OK ALI file, check if it is up to date
237 -- Note that we assume that the ALI read has all the entries
238 -- we have in our table, plus some additional ones (that can
239 -- come from expansion).
241 Id := First_Sdep_Entry;
242 for J in 1 .. Num_Sdep loop
243 Sind := Units.Table (Sdep_Table (J)).Source_Index;
245 while Sdep.Table (Id).Sfile /= File_Name (Sind) loop
246 if Id = Sdep.Last then
253 if Sdep.Table (Id).Stamp /= Time_Stamp (Sind) then
259 end Up_To_Date_ALI_File_Exists;
261 ---------------------------------
262 -- Update_Tables_From_ALI_File --
263 ---------------------------------
265 procedure Update_Tables_From_ALI_File is
267 -- Build Compilation_Switches table
269 Compilation_Switches.Init;
271 for J in First_Arg_Entry .. Args.Last loop
272 Compilation_Switches.Increment_Last;
273 Compilation_Switches.Table (Compilation_Switches.Last) :=
276 end Update_Tables_From_ALI_File;
278 ----------------------------
279 -- Write_Unit_Information --
280 ----------------------------
282 procedure Write_Unit_Information (Unit_Num : Unit_Number_Type) is
283 Unode : constant Node_Id := Cunit (Unit_Num);
284 Ukind : constant Node_Kind := Nkind (Unit (Unode));
285 Uent : constant Entity_Id := Cunit_Entity (Unit_Num);
289 Write_Info_Initiate ('U');
290 Write_Info_Char (' ');
291 Write_Info_Name (Unit_Name (Unit_Num));
293 Write_Info_Name (Unit_File_Name (Unit_Num));
296 Write_Info_Str (Version_Get (Unit_Num));
298 if Dynamic_Elab (Unit_Num) then
299 Write_Info_Str (" DE");
302 -- We set the Elaborate_Body indication if either an explicit pragma
303 -- was present, or if this is an instantiation. RM 12.3(20) requires
304 -- that the body be immediately elaborated after the spec. We would
305 -- normally do that anyway, but the EB we generate here ensures that
306 -- this gets done even when we use the -p gnatbind switch.
308 if Has_Pragma_Elaborate_Body (Uent)
309 or else (Ukind = N_Package_Declaration
310 and then Is_Generic_Instance (Uent)
311 and then Present (Corresponding_Body (Unit (Unode))))
313 Write_Info_Str (" EB");
316 -- Now see if we should tell the binder that an elaboration entity
317 -- is present, which must be reset to true during elaboration. We
318 -- generate the indication if the following condition is met:
320 -- If this is a spec ...
322 if (Is_Subprogram (Uent)
324 Ekind (Uent) = E_Package
326 Is_Generic_Unit (Uent))
328 -- and an elaboration entity was declared ...
330 and then Present (Elaboration_Entity (Uent))
332 -- and either the elaboration flag is required ...
335 (Elaboration_Entity_Required (Uent)
337 -- or this unit has elaboration code ...
339 or else not Has_No_Elaboration_Code (Unode)
341 -- or this unit has a separate body and this
342 -- body has elaboration code.
345 (Ekind (Uent) = E_Package
346 and then Present (Body_Entity (Uent))
348 not Has_No_Elaboration_Code
351 (Body_Entity (Uent))))))
353 Write_Info_Str (" EE");
356 if Has_No_Elaboration_Code (Unode) then
357 Write_Info_Str (" NE");
360 if Is_Preelaborated (Uent) then
361 Write_Info_Str (" PR");
364 if Is_Pure (Uent) then
365 Write_Info_Str (" PU");
368 if Has_RACW (Unit_Num) then
369 Write_Info_Str (" RA");
372 if Is_Remote_Call_Interface (Uent) then
373 Write_Info_Str (" RC");
376 if Is_Remote_Types (Uent) then
377 Write_Info_Str (" RT");
380 if Is_Shared_Passive (Uent) then
381 Write_Info_Str (" SP");
384 if Ukind = N_Subprogram_Declaration
385 or else Ukind = N_Subprogram_Body
387 Write_Info_Str (" SU");
389 elsif Ukind = N_Package_Declaration
391 Ukind = N_Package_Body
393 -- If this is a wrapper package for a subprogram instantiation,
394 -- the user view is the subprogram. Note that in this case the
395 -- ali file contains both the spec and body of the instance.
397 if Is_Wrapper_Package (Uent) then
398 Write_Info_Str (" SU");
400 Write_Info_Str (" PK");
403 elsif Ukind = N_Generic_Package_Declaration then
404 Write_Info_Str (" PK");
408 if Ukind in N_Generic_Declaration
410 (Present (Library_Unit (Unode))
412 Nkind (Unit (Library_Unit (Unode))) in N_Generic_Declaration)
414 Write_Info_Str (" GE");
417 if not Is_Internal_File_Name (Unit_File_Name (Unit_Num), True) then
418 case Identifier_Casing (Source_Index (Unit_Num)) is
419 when All_Lower_Case => Write_Info_Str (" IL");
420 when All_Upper_Case => Write_Info_Str (" IU");
424 case Keyword_Casing (Source_Index (Unit_Num)) is
425 when Mixed_Case => Write_Info_Str (" KM");
426 when All_Upper_Case => Write_Info_Str (" KU");
431 if Initialize_Scalars then
432 Write_Info_Str (" IS");
437 -- Generate with lines, first those that are directly with'ed
439 for J in With_Flags'Range loop
440 With_Flags (J) := False;
441 Elab_Flags (J) := False;
442 Elab_All_Flags (J) := False;
443 Elab_Des_Flags (J) := False;
446 Collect_Withs (Unode);
448 -- For a body, we must also check for any subunits which belong to
449 -- it and which have context clauses of their own, since these
450 -- with'ed units are part of its own elaboration dependencies.
452 if Nkind (Unit (Unode)) in N_Unit_Body then
453 for S in Units.First .. Last_Unit loop
455 -- We are only interested in subunits
457 if Nkind (Unit (Cunit (S))) = N_Subunit then
458 Pnode := Library_Unit (Cunit (S));
460 -- In gnatc mode, the errors in the subunits will not
461 -- have been recorded, but the analysis of the subunit
462 -- may have failed. There is no information to add to
463 -- ALI file in this case.
469 -- Find ultimate parent of the subunit
471 while Nkind (Unit (Pnode)) = N_Subunit loop
472 Pnode := Library_Unit (Pnode);
475 -- See if it belongs to current unit, and if so, include
478 if Pnode = Unode then
479 Collect_Withs (Cunit (S));
486 end Write_Unit_Information;
488 ----------------------
489 -- Write_With_Lines --
490 ----------------------
492 procedure Write_With_Lines is
493 With_Table : Unit_Ref_Table (1 .. Pos (Last_Unit - Units.First + 1));
494 Num_Withs : Int := 0;
495 Unum : Unit_Number_Type;
498 Uname : Unit_Name_Type;
499 Fname : File_Name_Type;
500 Pname : constant Unit_Name_Type :=
501 Get_Parent_Spec_Name (Unit_Name (Main_Unit));
502 Body_Fname : File_Name_Type;
505 -- Loop to build the with table. A with on the main unit itself
506 -- is ignored (AARM 10.2(14a)). Such a with-clause can occur if
507 -- the main unit is a subprogram with no spec, and a subunit of
508 -- it unecessarily withs the parent.
510 for J in Units.First + 1 .. Last_Unit loop
512 -- Add element to with table if it is with'ed or if it is the
513 -- parent spec of the main unit (case of main unit is a child
514 -- unit). The latter with is not needed for semantic purposes,
515 -- but is required by the binder for elaboration purposes.
517 if (With_Flags (J) or else Unit_Name (J) = Pname)
518 and then Units.Table (J).Dependent_Unit
520 Num_Withs := Num_Withs + 1;
521 With_Table (Num_Withs) := J;
525 -- Sort and output the table
527 Sort (With_Table (1 .. Num_Withs));
529 for J in 1 .. Num_Withs loop
530 Unum := With_Table (J);
531 Cunit := Units.Table (Unum).Cunit;
532 Cunite := Units.Table (Unum).Cunit_Entity;
533 Uname := Units.Table (Unum).Unit_Name;
534 Fname := Units.Table (Unum).Unit_File_Name;
536 Write_Info_Initiate ('W');
537 Write_Info_Char (' ');
538 Write_Info_Name (Uname);
540 -- Now we need to figure out the names of the files that contain
541 -- the with'ed unit. These will usually be the files for the body,
542 -- except in the case of a package that has no body.
544 if (Nkind (Unit (Cunit)) not in N_Generic_Declaration
546 Nkind (Unit (Cunit)) not in N_Generic_Renaming_Declaration)
547 or else Generic_Separately_Compiled (Cunite)
551 if Is_Spec_Name (Uname) then
553 Get_File_Name (Get_Body_Name (Uname), Subunit => False);
555 Body_Fname := Get_File_Name (Uname, Subunit => False);
558 -- A package is considered to have a body if it requires
559 -- a body or if a body is present in Ada 83 mode.
561 if Body_Required (Cunit)
563 and then Full_Source_Name (Body_Fname) /= No_File)
565 Write_Info_Name (Body_Fname);
567 Write_Info_Name (Lib_File_Name (Body_Fname));
569 Write_Info_Name (Fname);
571 Write_Info_Name (Lib_File_Name (Fname));
574 if Elab_Flags (Unum) then
575 Write_Info_Str (" E");
578 if Elab_All_Flags (Unum) then
579 Write_Info_Str (" EA");
582 if Elab_Des_Flags (Unum) then
583 Write_Info_Str (" ED");
589 end Write_With_Lines;
591 -- Start of processing for Writ_ALI
594 -- Build sorted source dependency table. We do this right away,
595 -- because it is referenced by Up_To_Date_ALI_File_Exists.
597 for Unum in Units.First .. Last_Unit loop
598 Num_Sdep := Num_Sdep + 1;
599 Sdep_Table (Num_Sdep) := Unum;
602 -- Sort the table so that the D lines are in order
604 Lib.Sort (Sdep_Table (1 .. Num_Sdep));
606 -- If we are not generating code, and there is an up to date
607 -- ali file accessible, read it, and acquire the compilation
608 -- arguments from this file.
610 if Operating_Mode /= Generate_Code then
611 if Up_To_Date_ALI_File_Exists then
612 Update_Tables_From_ALI_File;
617 -- Otherwise acquire compilation arguments and prepare to write
618 -- out a new ali file.
620 Create_Output_Library_Info;
622 -- Output version line
624 Write_Info_Initiate ('V');
625 Write_Info_Str (" """);
626 Write_Info_Str (Library_Version);
627 Write_Info_Char ('"');
631 -- Output main program line if this is acceptable main program
634 U : Node_Id := Unit (Units.Table (Main_Unit).Cunit);
637 procedure M_Parameters;
638 -- Output parameters for main program line
640 procedure M_Parameters is
642 if Main_Priority (Main_Unit) /= Default_Main_Priority then
643 Write_Info_Char (' ');
644 Write_Info_Nat (Main_Priority (Main_Unit));
647 if Opt.Time_Slice_Set then
648 Write_Info_Str (" T=");
649 Write_Info_Nat (Opt.Time_Slice_Value);
652 Write_Info_Str (" W=");
654 (WC_Encoding_Letters (Wide_Character_Encoding_Method));
660 if Nkind (U) = N_Subprogram_Body
661 or else (Nkind (U) = N_Package_Body
663 (Nkind (Original_Node (U)) = N_Function_Instantiation
665 Nkind (Original_Node (U)) =
666 N_Procedure_Instantiation))
668 -- If the unit is a subprogram instance, the entity for the
669 -- subprogram is the alias of the visible entity, which is the
670 -- related instance of the wrapper package. We retrieve the
671 -- subprogram declaration of the desired entity.
673 if Nkind (U) = N_Package_Body then
674 U := Parent (Parent (
675 Alias (Related_Instance (Defining_Unit_Name
676 (Specification (Unit (Library_Unit (Parent (U)))))))));
679 S := Specification (U);
681 if not Present (Parameter_Specifications (S)) then
682 if Nkind (S) = N_Procedure_Specification then
683 Write_Info_Initiate ('M');
684 Write_Info_Str (" P");
689 Nam : Node_Id := Defining_Unit_Name (S);
692 -- If it is a child unit, get its simple name.
694 if Nkind (Nam) = N_Defining_Program_Unit_Name then
695 Nam := Defining_Identifier (Nam);
698 if Is_Integer_Type (Etype (Nam)) then
699 Write_Info_Initiate ('M');
700 Write_Info_Str (" F");
709 -- Write command argmument ('A') lines
711 for A in 1 .. Compilation_Switches.Last loop
712 Write_Info_Initiate ('A');
713 Write_Info_Char (' ');
714 Write_Info_Str (Compilation_Switches.Table (A).all);
715 Write_Info_Terminate;
718 -- Output parameters ('P') line
720 Write_Info_Initiate ('P');
722 if Compilation_Errors then
723 Write_Info_Str (" CE");
726 if Opt.Float_Format /= ' ' then
727 Write_Info_Str (" F");
729 if Opt.Float_Format = 'I' then
730 Write_Info_Char ('I');
732 elsif Opt.Float_Format_Long = 'D' then
733 Write_Info_Char ('D');
736 Write_Info_Char ('G');
741 and then not Is_Predefined_File_Name (Unit_File_Name (Main_Unit))
743 if Locking_Policy /= ' ' then
744 Write_Info_Str (" L");
745 Write_Info_Char (Locking_Policy);
748 if Queuing_Policy /= ' ' then
749 Write_Info_Str (" Q");
750 Write_Info_Char (Queuing_Policy);
753 if Task_Dispatching_Policy /= ' ' then
754 Write_Info_Str (" T");
755 Write_Info_Char (Task_Dispatching_Policy);
756 Write_Info_Char (' ');
761 Write_Info_Str (" NO");
765 Write_Info_Str (" NR");
768 if Normalize_Scalars then
769 Write_Info_Str (" NS");
772 if Unreserve_All_Interrupts then
773 Write_Info_Str (" UA");
776 if ZCX_By_Default_On_Target then
777 if Unit_Exception_Table_Present then
778 Write_Info_Str (" UX");
781 Write_Info_Str (" ZX");
786 -- Output restrictions line
788 Write_Info_Initiate ('R');
789 Write_Info_Char (' ');
791 for J in Partition_Restrictions loop
792 if Main_Restrictions (J) then
793 Write_Info_Char ('r');
794 elsif Violations (J) then
795 Write_Info_Char ('v');
797 Write_Info_Char ('n');
803 -- Loop through file table to output information for all units for which
804 -- we have generated code, as marked by the Generate_Code flag.
806 for Unit in Units.First .. Last_Unit loop
807 if Units.Table (Unit).Generate_Code
808 or else Unit = Main_Unit
810 Write_Info_EOL; -- blank line
811 Write_Unit_Information (Unit);
815 Write_Info_EOL; -- blank line
817 -- Output linker option lines
819 for J in 1 .. Linker_Option_Lines.Last loop
821 S : constant String_Id := Linker_Option_Lines.Table (J);
825 Write_Info_Initiate ('L');
826 Write_Info_Str (" """);
828 for J in 1 .. String_Length (S) loop
829 C := Get_Character (Get_String_Char (S, J));
831 if C in Character'Val (16#20#) .. Character'Val (16#7E#)
842 Hex : array (0 .. 15) of Character := "0123456789ABCDEF";
845 Write_Info_Char ('{');
846 Write_Info_Char (Hex (Character'Pos (C) / 16));
847 Write_Info_Char (Hex (Character'Pos (C) mod 16));
848 Write_Info_Char ('}');
853 Write_Info_Char ('"');
858 -- Output external version reference lines
860 for J in 1 .. Version_Ref.Last loop
861 Write_Info_Initiate ('E');
862 Write_Info_Char (' ');
864 for K in 1 .. String_Length (Version_Ref.Table (J)) loop
865 Write_Info_Char_Code (Get_String_Char (Version_Ref.Table (J), K));
871 -- Prepare to output the source dependency lines
874 Unum : Unit_Number_Type;
875 -- Number of unit being output
877 Sind : Source_File_Index;
878 -- Index of corresponding source file
881 for J in 1 .. Num_Sdep loop
882 Unum := Sdep_Table (J);
883 Units.Table (Unum).Dependency_Num := J;
884 Sind := Units.Table (Unum).Source_Index;
886 Write_Info_Initiate ('D');
887 Write_Info_Char (' ');
889 -- Normal case of a dependent unit entry with a source index
891 if Sind /= No_Source_File
892 and then Units.Table (Unum).Dependent_Unit
894 Write_Info_Name (File_Name (Sind));
896 Write_Info_Str (String (Time_Stamp (Sind)));
897 Write_Info_Char (' ');
898 Write_Info_Str (Get_Hex_String (Source_Checksum (Sind)));
900 -- If subunit, add unit name, omitting the %b at the end
902 if Present (Cunit (Unum))
903 and then Nkind (Unit (Cunit (Unum))) = N_Subunit
905 Get_Decoded_Name_String (Unit_Name (Unum));
906 Write_Info_Char (' ');
907 Write_Info_Str (Name_Buffer (1 .. Name_Len - 2));
910 -- If Source_Reference pragma used output information
912 if Num_SRef_Pragmas (Sind) > 0 then
913 Write_Info_Char (' ');
915 if Num_SRef_Pragmas (Sind) = 1 then
916 Write_Info_Nat (Int (First_Mapped_Line (Sind)));
921 Write_Info_Char (':');
922 Write_Info_Name (Reference_Name (Sind));
925 -- Case where there is no source index (happens for missing files)
926 -- Also come here for non-dependent units.
929 Write_Info_Name (Unit_File_Name (Unum));
931 Write_Info_Str (String (Dummy_Time_Stamp));
932 Write_Info_Char (' ');
933 Write_Info_Str (Get_Hex_String (0));
941 Write_Info_Terminate;
942 Close_Output_Library_Info;