OSDN Git Service

17d544f6f3535ad244e6e111d6dd50e68f6c7ef5
[pf3gnuchains/gcc-fork.git] / gcc / ada / prj.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                  P R J                                   --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2001-2010, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Debug;
27 with Osint;    use Osint;
28 with Output;   use Output;
29 with Prj.Attr;
30 with Prj.Err;  use Prj.Err;
31 with Snames;   use Snames;
32 with Uintp;    use Uintp;
33
34 with Ada.Characters.Handling;    use Ada.Characters.Handling;
35 with Ada.Unchecked_Deallocation;
36
37 with GNAT.Case_Util;            use GNAT.Case_Util;
38 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
39 with GNAT.HTable;
40
41 package body Prj is
42
43    Object_Suffix : constant String := Get_Target_Object_Suffix.all;
44    --  File suffix for object files
45
46    Initial_Buffer_Size : constant := 100;
47    --  Initial size for extensible buffer used in Add_To_Buffer
48
49    The_Empty_String : Name_Id := No_Name;
50
51    type Cst_String_Access is access constant String;
52
53    All_Lower_Case_Image : aliased constant String := "lowercase";
54    All_Upper_Case_Image : aliased constant String := "UPPERCASE";
55    Mixed_Case_Image     : aliased constant String := "MixedCase";
56
57    The_Casing_Images : constant array (Known_Casing) of Cst_String_Access :=
58                          (All_Lower_Case => All_Lower_Case_Image'Access,
59                           All_Upper_Case => All_Upper_Case_Image'Access,
60                           Mixed_Case     => Mixed_Case_Image'Access);
61
62    Project_Empty : constant Project_Data :=
63                      (Qualifier                      => Unspecified,
64                       Externally_Built               => False,
65                       Config                         => Default_Project_Config,
66                       Name                           => No_Name,
67                       Display_Name                   => No_Name,
68                       Path                           => No_Path_Information,
69                       Virtual                        => False,
70                       Location                       => No_Location,
71                       Mains                          => Nil_String,
72                       Directory                      => No_Path_Information,
73                       Library                        => False,
74                       Library_Dir                    => No_Path_Information,
75                       Library_Src_Dir                => No_Path_Information,
76                       Library_ALI_Dir                => No_Path_Information,
77                       Library_Name                   => No_Name,
78                       Library_Kind                   => Static,
79                       Lib_Internal_Name              => No_Name,
80                       Standalone_Library             => False,
81                       Lib_Interface_ALIs             => Nil_String,
82                       Lib_Auto_Init                  => False,
83                       Libgnarl_Needed                => Unknown,
84                       Symbol_Data                    => No_Symbols,
85                       Interfaces_Defined             => False,
86                       Source_Dirs                    => Nil_String,
87                       Source_Dir_Ranks               => No_Number_List,
88                       Object_Directory               => No_Path_Information,
89                       Library_TS                     => Empty_Time_Stamp,
90                       Exec_Directory                 => No_Path_Information,
91                       Extends                        => No_Project,
92                       Extended_By                    => No_Project,
93                       Languages                      => No_Language_Index,
94                       Decl                           => No_Declarations,
95                       Imported_Projects              => null,
96                       Include_Path_File              => No_Path,
97                       All_Imported_Projects          => null,
98                       Ada_Include_Path               => null,
99                       Ada_Objects_Path               => null,
100                       Objects_Path                   => null,
101                       Objects_Path_File_With_Libs    => No_Path,
102                       Objects_Path_File_Without_Libs => No_Path,
103                       Config_File_Name               => No_Path,
104                       Config_File_Temp               => False,
105                       Config_Checked                 => False,
106                       Need_To_Build_Lib              => False,
107                       Has_Multi_Unit_Sources         => False,
108                       Depth                          => 0,
109                       Unkept_Comments                => False);
110
111    procedure Free (Project : in out Project_Id);
112    --  Free memory allocated for Project
113
114    procedure Free_List (Languages : in out Language_Ptr);
115    procedure Free_List (Source : in out Source_Id);
116    procedure Free_List (Languages : in out Language_List);
117    --  Free memory allocated for the list of languages or sources
118
119    procedure Free_Units (Table : in out Units_Htable.Instance);
120    --  Free memory allocated for unit information in the project
121
122    procedure Language_Changed (Iter : in out Source_Iterator);
123    procedure Project_Changed (Iter : in out Source_Iterator);
124    --  Called when a new project or language was selected for this iterator
125
126    function Contains_ALI_Files (Dir : Path_Name_Type) return Boolean;
127    --  Return True if there is at least one ALI file in the directory Dir
128
129    -------------------
130    -- Add_To_Buffer --
131    -------------------
132
133    procedure Add_To_Buffer
134      (S    : String;
135       To   : in out String_Access;
136       Last : in out Natural)
137    is
138    begin
139       if To = null then
140          To := new String (1 .. Initial_Buffer_Size);
141          Last := 0;
142       end if;
143
144       --  If Buffer is too small, double its size
145
146       while Last + S'Length > To'Last loop
147          declare
148             New_Buffer : constant  String_Access :=
149                            new String (1 .. 2 * Last);
150
151          begin
152             New_Buffer (1 .. Last) := To (1 .. Last);
153             Free (To);
154             To := New_Buffer;
155          end;
156       end loop;
157
158       To (Last + 1 .. Last + S'Length) := S;
159       Last := Last + S'Length;
160    end Add_To_Buffer;
161
162    ---------------------------
163    -- Delete_Temporary_File --
164    ---------------------------
165
166    procedure Delete_Temporary_File
167      (Tree : Project_Tree_Ref;
168       Path : Path_Name_Type)
169    is
170       Dont_Care : Boolean;
171       pragma Warnings (Off, Dont_Care);
172
173    begin
174       if not Debug.Debug_Flag_N then
175          if Current_Verbosity = High then
176             Write_Line ("Removing temp file: " & Get_Name_String (Path));
177          end if;
178
179          Delete_File (Get_Name_String (Path), Dont_Care);
180
181          for Index in
182            1 .. Temp_Files_Table.Last (Tree.Private_Part.Temp_Files)
183          loop
184             if Tree.Private_Part.Temp_Files.Table (Index) = Path then
185                Tree.Private_Part.Temp_Files.Table (Index) := No_Path;
186             end if;
187          end loop;
188       end if;
189    end Delete_Temporary_File;
190
191    ---------------------------
192    -- Delete_All_Temp_Files --
193    ---------------------------
194
195    procedure Delete_All_Temp_Files (Tree : Project_Tree_Ref) is
196       Dont_Care : Boolean;
197       pragma Warnings (Off, Dont_Care);
198
199       Path : Path_Name_Type;
200
201    begin
202       if not Debug.Debug_Flag_N then
203          for Index in
204            1 .. Temp_Files_Table.Last (Tree.Private_Part.Temp_Files)
205          loop
206             Path := Tree.Private_Part.Temp_Files.Table (Index);
207
208             if Path /= No_Path then
209                if Current_Verbosity = High then
210                   Write_Line ("Removing temp file: "
211                               & Get_Name_String (Path));
212                end if;
213
214                Delete_File (Get_Name_String (Path), Dont_Care);
215             end if;
216          end loop;
217
218          Temp_Files_Table.Free (Tree.Private_Part.Temp_Files);
219          Temp_Files_Table.Init (Tree.Private_Part.Temp_Files);
220       end if;
221
222       --  If any of the environment variables ADA_PRJ_INCLUDE_FILE or
223       --  ADA_PRJ_OBJECTS_FILE has been set, then reset their value to
224       --  the empty string. On VMS, this has the effect of deassigning
225       --  the logical names.
226
227       if Tree.Private_Part.Current_Source_Path_File /= No_Path then
228          Setenv (Project_Include_Path_File, "");
229       end if;
230
231       if Tree.Private_Part.Current_Object_Path_File /= No_Path then
232          Setenv (Project_Objects_Path_File, "");
233       end if;
234    end Delete_All_Temp_Files;
235
236    ---------------------
237    -- Dependency_Name --
238    ---------------------
239
240    function Dependency_Name
241      (Source_File_Name : File_Name_Type;
242       Dependency       : Dependency_File_Kind) return File_Name_Type
243    is
244    begin
245       case Dependency is
246          when None =>
247             return No_File;
248
249          when Makefile =>
250             return
251               File_Name_Type
252                 (Extend_Name
253                    (Source_File_Name, Makefile_Dependency_Suffix));
254
255          when ALI_File =>
256             return
257               File_Name_Type
258                 (Extend_Name
259                    (Source_File_Name, ALI_Dependency_Suffix));
260       end case;
261    end Dependency_Name;
262
263    ----------------
264    -- Empty_File --
265    ----------------
266
267    function Empty_File return File_Name_Type is
268    begin
269       return File_Name_Type (The_Empty_String);
270    end Empty_File;
271
272    -------------------
273    -- Empty_Project --
274    -------------------
275
276    function Empty_Project return Project_Data is
277    begin
278       Prj.Initialize (Tree => No_Project_Tree);
279       return Project_Empty;
280    end Empty_Project;
281
282    ------------------
283    -- Empty_String --
284    ------------------
285
286    function Empty_String return Name_Id is
287    begin
288       return The_Empty_String;
289    end Empty_String;
290
291    ------------
292    -- Expect --
293    ------------
294
295    procedure Expect (The_Token : Token_Type; Token_Image : String) is
296    begin
297       if Token /= The_Token then
298          --  ??? Should pass user flags here instead
299          Error_Msg (Gnatmake_Flags, Token_Image & " expected", Token_Ptr);
300       end if;
301    end Expect;
302
303    -----------------
304    -- Extend_Name --
305    -----------------
306
307    function Extend_Name
308      (File        : File_Name_Type;
309       With_Suffix : String) return File_Name_Type
310    is
311       Last : Positive;
312
313    begin
314       Get_Name_String (File);
315       Last := Name_Len + 1;
316
317       while Name_Len /= 0 and then Name_Buffer (Name_Len) /= '.' loop
318          Name_Len := Name_Len - 1;
319       end loop;
320
321       if Name_Len <= 1 then
322          Name_Len := Last;
323       end if;
324
325       for J in With_Suffix'Range loop
326          Name_Buffer (Name_Len) := With_Suffix (J);
327          Name_Len := Name_Len + 1;
328       end loop;
329
330       Name_Len := Name_Len - 1;
331       return Name_Find;
332
333    end Extend_Name;
334
335    ---------------------
336    -- Project_Changed --
337    ---------------------
338
339    procedure Project_Changed (Iter : in out Source_Iterator) is
340    begin
341       Iter.Language := Iter.Project.Project.Languages;
342       Language_Changed (Iter);
343    end Project_Changed;
344
345    ----------------------
346    -- Language_Changed --
347    ----------------------
348
349    procedure Language_Changed (Iter : in out Source_Iterator) is
350    begin
351       Iter.Current  := No_Source;
352
353       if Iter.Language_Name /= No_Name then
354          while Iter.Language /= null
355            and then Iter.Language.Name /= Iter.Language_Name
356          loop
357             Iter.Language := Iter.Language.Next;
358          end loop;
359       end if;
360
361       --  If there is no matching language in this project, move to next
362
363       if Iter.Language = No_Language_Index then
364          if Iter.All_Projects then
365             Iter.Project := Iter.Project.Next;
366
367             if Iter.Project /= null then
368                Project_Changed (Iter);
369             end if;
370
371          else
372             Iter.Project := null;
373          end if;
374
375       else
376          Iter.Current := Iter.Language.First_Source;
377
378          if Iter.Current = No_Source then
379             Iter.Language := Iter.Language.Next;
380             Language_Changed (Iter);
381          end if;
382       end if;
383    end Language_Changed;
384
385    ---------------------
386    -- For_Each_Source --
387    ---------------------
388
389    function For_Each_Source
390      (In_Tree  : Project_Tree_Ref;
391       Project  : Project_Id := No_Project;
392       Language : Name_Id := No_Name) return Source_Iterator
393    is
394       Iter : Source_Iterator;
395    begin
396       Iter := Source_Iterator'
397         (In_Tree       => In_Tree,
398          Project       => In_Tree.Projects,
399          All_Projects  => Project = No_Project,
400          Language_Name => Language,
401          Language      => No_Language_Index,
402          Current       => No_Source);
403
404       if Project /= null then
405          while Iter.Project /= null
406            and then Iter.Project.Project /= Project
407          loop
408             Iter.Project := Iter.Project.Next;
409          end loop;
410       end if;
411
412       Project_Changed (Iter);
413
414       return Iter;
415    end For_Each_Source;
416
417    -------------
418    -- Element --
419    -------------
420
421    function Element (Iter : Source_Iterator) return Source_Id is
422    begin
423       return Iter.Current;
424    end Element;
425
426    ----------
427    -- Next --
428    ----------
429
430    procedure Next (Iter : in out Source_Iterator) is
431    begin
432       Iter.Current := Iter.Current.Next_In_Lang;
433       if Iter.Current = No_Source then
434          Iter.Language := Iter.Language.Next;
435          Language_Changed (Iter);
436       end if;
437    end Next;
438
439    --------------------------------
440    -- For_Every_Project_Imported --
441    --------------------------------
442
443    procedure For_Every_Project_Imported
444      (By             : Project_Id;
445       With_State     : in out State;
446       Imported_First : Boolean := False)
447    is
448       use Project_Boolean_Htable;
449       Seen : Project_Boolean_Htable.Instance := Project_Boolean_Htable.Nil;
450
451       procedure Recursive_Check (Project : Project_Id);
452       --  Check if a project has already been seen. If not seen, mark it as
453       --  Seen, Call Action, and check all its imported projects.
454
455       ---------------------
456       -- Recursive_Check --
457       ---------------------
458
459       procedure Recursive_Check (Project : Project_Id) is
460          List : Project_List;
461
462       begin
463          if not Get (Seen, Project) then
464             Set (Seen, Project, True);
465
466             if not Imported_First then
467                Action (Project, With_State);
468             end if;
469
470             --  Visited all extended projects
471
472             if Project.Extends /= No_Project then
473                Recursive_Check (Project.Extends);
474             end if;
475
476             --  Visited all imported projects
477
478             List := Project.Imported_Projects;
479             while List /= null loop
480                Recursive_Check (List.Project);
481                List := List.Next;
482             end loop;
483
484             if Imported_First then
485                Action (Project, With_State);
486             end if;
487          end if;
488       end Recursive_Check;
489
490    --  Start of processing for For_Every_Project_Imported
491
492    begin
493       Recursive_Check (Project => By);
494       Reset (Seen);
495    end For_Every_Project_Imported;
496
497    -----------------
498    -- Find_Source --
499    -----------------
500
501    function Find_Source
502      (In_Tree          : Project_Tree_Ref;
503       Project          : Project_Id;
504       In_Imported_Only : Boolean := False;
505       In_Extended_Only : Boolean := False;
506       Base_Name        : File_Name_Type) return Source_Id
507    is
508       Result : Source_Id  := No_Source;
509
510       procedure Look_For_Sources (Proj : Project_Id; Src : in out Source_Id);
511       --  Look for Base_Name in the sources of Proj
512
513       ----------------------
514       -- Look_For_Sources --
515       ----------------------
516
517       procedure Look_For_Sources (Proj : Project_Id; Src : in out Source_Id) is
518          Iterator : Source_Iterator;
519
520       begin
521          Iterator := For_Each_Source (In_Tree => In_Tree, Project => Proj);
522          while Element (Iterator) /= No_Source loop
523             if Element (Iterator).File = Base_Name then
524                Src := Element (Iterator);
525                return;
526             end if;
527
528             Next (Iterator);
529          end loop;
530       end Look_For_Sources;
531
532       procedure For_Imported_Projects is new For_Every_Project_Imported
533         (State => Source_Id, Action => Look_For_Sources);
534
535       Proj : Project_Id;
536
537    --  Start of processing for Find_Source
538
539    begin
540       if In_Extended_Only then
541          Proj := Project;
542          while Proj /= No_Project loop
543             Look_For_Sources (Proj, Result);
544             exit when Result /= No_Source;
545
546             Proj := Proj.Extends;
547          end loop;
548
549       elsif In_Imported_Only then
550          Look_For_Sources (Project, Result);
551
552          if Result = No_Source then
553             For_Imported_Projects
554               (By         => Project,
555                With_State => Result);
556          end if;
557       else
558          Look_For_Sources (No_Project, Result);
559       end if;
560
561       return Result;
562    end Find_Source;
563
564    ----------
565    -- Hash --
566    ----------
567
568    function Hash is new GNAT.HTable.Hash (Header_Num => Header_Num);
569    --  Used in implementation of other functions Hash below
570
571    function Hash (Name : File_Name_Type) return Header_Num is
572    begin
573       return Hash (Get_Name_String (Name));
574    end Hash;
575
576    function Hash (Name : Name_Id) return Header_Num is
577    begin
578       return Hash (Get_Name_String (Name));
579    end Hash;
580
581    function Hash (Name : Path_Name_Type) return Header_Num is
582    begin
583       return Hash (Get_Name_String (Name));
584    end Hash;
585
586    function Hash (Project : Project_Id) return Header_Num is
587    begin
588       if Project = No_Project then
589          return Header_Num'First;
590       else
591          return Hash (Get_Name_String (Project.Name));
592       end if;
593    end Hash;
594
595    -----------
596    -- Image --
597    -----------
598
599    function Image (The_Casing : Casing_Type) return String is
600    begin
601       return The_Casing_Images (The_Casing).all;
602    end Image;
603
604    -----------------------------
605    -- Is_Standard_GNAT_Naming --
606    -----------------------------
607
608    function Is_Standard_GNAT_Naming
609      (Naming : Lang_Naming_Data) return Boolean
610    is
611    begin
612       return Get_Name_String (Naming.Spec_Suffix) = ".ads"
613         and then Get_Name_String (Naming.Body_Suffix) = ".adb"
614         and then Get_Name_String (Naming.Dot_Replacement) = "-";
615    end Is_Standard_GNAT_Naming;
616
617    ----------------
618    -- Initialize --
619    ----------------
620
621    procedure Initialize (Tree : Project_Tree_Ref) is
622    begin
623       if The_Empty_String = No_Name then
624          Uintp.Initialize;
625          Name_Len := 0;
626          The_Empty_String := Name_Find;
627
628          Prj.Attr.Initialize;
629          Set_Name_Table_Byte (Name_Project,  Token_Type'Pos (Tok_Project));
630          Set_Name_Table_Byte (Name_Extends,  Token_Type'Pos (Tok_Extends));
631          Set_Name_Table_Byte (Name_External, Token_Type'Pos (Tok_External));
632       end if;
633
634       if Tree /= No_Project_Tree then
635          Reset (Tree);
636       end if;
637    end Initialize;
638
639    ------------------
640    -- Is_Extending --
641    ------------------
642
643    function Is_Extending
644      (Extending : Project_Id;
645       Extended  : Project_Id) return Boolean
646    is
647       Proj : Project_Id;
648
649    begin
650       Proj := Extending;
651       while Proj /= No_Project loop
652          if Proj = Extended then
653             return True;
654          end if;
655
656          Proj := Proj.Extends;
657       end loop;
658
659       return False;
660    end Is_Extending;
661
662    -----------------
663    -- Object_Name --
664    -----------------
665
666    function Object_Name
667      (Source_File_Name   : File_Name_Type;
668       Object_File_Suffix : Name_Id := No_Name) return File_Name_Type
669    is
670    begin
671       if Object_File_Suffix = No_Name then
672          return Extend_Name
673            (Source_File_Name, Object_Suffix);
674       else
675          return Extend_Name
676            (Source_File_Name, Get_Name_String (Object_File_Suffix));
677       end if;
678    end Object_Name;
679
680    function Object_Name
681      (Source_File_Name   : File_Name_Type;
682       Source_Index       : Int;
683       Index_Separator    : Character;
684       Object_File_Suffix : Name_Id := No_Name) return File_Name_Type
685    is
686       Index_Img : constant String := Source_Index'Img;
687       Last      : Natural;
688
689    begin
690       Get_Name_String (Source_File_Name);
691
692       Last := Name_Len;
693       while Last > 1 and then Name_Buffer (Last) /= '.' loop
694          Last := Last - 1;
695       end loop;
696
697       if Last > 1 then
698          Name_Len := Last - 1;
699       end if;
700
701       Add_Char_To_Name_Buffer (Index_Separator);
702       Add_Str_To_Name_Buffer (Index_Img (2 .. Index_Img'Last));
703
704       if Object_File_Suffix = No_Name then
705          Add_Str_To_Name_Buffer (Object_Suffix);
706       else
707          Add_Str_To_Name_Buffer (Get_Name_String (Object_File_Suffix));
708       end if;
709
710       return Name_Find;
711    end Object_Name;
712
713    ----------------------
714    -- Record_Temp_File --
715    ----------------------
716
717    procedure Record_Temp_File
718      (Tree : Project_Tree_Ref;
719       Path : Path_Name_Type)
720    is
721    begin
722       Temp_Files_Table.Append (Tree.Private_Part.Temp_Files, Path);
723    end Record_Temp_File;
724
725    ----------
726    -- Free --
727    ----------
728
729    procedure Free (Project : in out Project_Id) is
730       procedure Unchecked_Free is new Ada.Unchecked_Deallocation
731         (Project_Data, Project_Id);
732
733    begin
734       if Project /= null then
735          Free (Project.Ada_Include_Path);
736          Free (Project.Objects_Path);
737          Free (Project.Ada_Objects_Path);
738          Free_List (Project.Imported_Projects, Free_Project => False);
739          Free_List (Project.All_Imported_Projects, Free_Project => False);
740          Free_List (Project.Languages);
741
742          Unchecked_Free (Project);
743       end if;
744    end Free;
745
746    ---------------
747    -- Free_List --
748    ---------------
749
750    procedure Free_List (Languages : in out Language_List) is
751       procedure Unchecked_Free is new Ada.Unchecked_Deallocation
752         (Language_List_Element, Language_List);
753       Tmp : Language_List;
754    begin
755       while Languages /= null loop
756          Tmp := Languages.Next;
757          Unchecked_Free (Languages);
758          Languages := Tmp;
759       end loop;
760    end Free_List;
761
762    ---------------
763    -- Free_List --
764    ---------------
765
766    procedure Free_List (Source : in out Source_Id) is
767       procedure Unchecked_Free is new
768         Ada.Unchecked_Deallocation (Source_Data, Source_Id);
769
770       Tmp : Source_Id;
771
772    begin
773       while Source /= No_Source loop
774          Tmp := Source.Next_In_Lang;
775          Free_List (Source.Alternate_Languages);
776
777          if Source.Unit /= null
778            and then Source.Kind in Spec_Or_Body
779          then
780             Source.Unit.File_Names (Source.Kind) := null;
781          end if;
782
783          Unchecked_Free (Source);
784          Source := Tmp;
785       end loop;
786    end Free_List;
787
788    ---------------
789    -- Free_List --
790    ---------------
791
792    procedure Free_List
793      (List         : in out Project_List;
794       Free_Project : Boolean)
795    is
796       procedure Unchecked_Free is new
797         Ada.Unchecked_Deallocation (Project_List_Element, Project_List);
798
799       Tmp : Project_List;
800
801    begin
802       while List /= null loop
803          Tmp := List.Next;
804
805          if Free_Project then
806             Free (List.Project);
807          end if;
808
809          Unchecked_Free (List);
810          List := Tmp;
811       end loop;
812    end Free_List;
813
814    ---------------
815    -- Free_List --
816    ---------------
817
818    procedure Free_List (Languages : in out Language_Ptr) is
819       procedure Unchecked_Free is new
820         Ada.Unchecked_Deallocation (Language_Data, Language_Ptr);
821
822       Tmp : Language_Ptr;
823
824    begin
825       while Languages /= null loop
826          Tmp := Languages.Next;
827          Free_List (Languages.First_Source);
828          Unchecked_Free (Languages);
829          Languages := Tmp;
830       end loop;
831    end Free_List;
832
833    ----------------
834    -- Free_Units --
835    ----------------
836
837    procedure Free_Units (Table : in out Units_Htable.Instance) is
838       procedure Unchecked_Free is new
839         Ada.Unchecked_Deallocation (Unit_Data, Unit_Index);
840
841       Unit : Unit_Index;
842
843    begin
844       Unit := Units_Htable.Get_First (Table);
845       while Unit /= No_Unit_Index loop
846          if Unit.File_Names (Spec) /= null then
847             Unit.File_Names (Spec).Unit := No_Unit_Index;
848          end if;
849
850          if Unit.File_Names (Impl) /= null then
851             Unit.File_Names (Impl).Unit := No_Unit_Index;
852          end if;
853
854          Unchecked_Free (Unit);
855          Unit := Units_Htable.Get_Next (Table);
856       end loop;
857
858       Units_Htable.Reset (Table);
859    end Free_Units;
860
861    ----------
862    -- Free --
863    ----------
864
865    procedure Free (Tree : in out Project_Tree_Ref) is
866       procedure Unchecked_Free is new
867         Ada.Unchecked_Deallocation (Project_Tree_Data, Project_Tree_Ref);
868
869    begin
870       if Tree /= null then
871          Name_List_Table.Free (Tree.Name_Lists);
872          Number_List_Table.Free (Tree.Number_Lists);
873          String_Element_Table.Free (Tree.String_Elements);
874          Variable_Element_Table.Free (Tree.Variable_Elements);
875          Array_Element_Table.Free (Tree.Array_Elements);
876          Array_Table.Free (Tree.Arrays);
877          Package_Table.Free (Tree.Packages);
878          Source_Paths_Htable.Reset (Tree.Source_Paths_HT);
879
880          Free_List (Tree.Projects, Free_Project => True);
881          Free_Units (Tree.Units_HT);
882
883          --  Private part
884
885          Temp_Files_Table.Free  (Tree.Private_Part.Temp_Files);
886
887          Unchecked_Free (Tree);
888       end if;
889    end Free;
890
891    -----------
892    -- Reset --
893    -----------
894
895    procedure Reset (Tree : Project_Tree_Ref) is
896    begin
897       --  Visible tables
898
899       Name_List_Table.Init          (Tree.Name_Lists);
900       Number_List_Table.Init        (Tree.Number_Lists);
901       String_Element_Table.Init     (Tree.String_Elements);
902       Variable_Element_Table.Init   (Tree.Variable_Elements);
903       Array_Element_Table.Init      (Tree.Array_Elements);
904       Array_Table.Init              (Tree.Arrays);
905       Package_Table.Init            (Tree.Packages);
906       Source_Paths_Htable.Reset     (Tree.Source_Paths_HT);
907
908       Free_List (Tree.Projects, Free_Project => True);
909       Free_Units (Tree.Units_HT);
910
911       --  Private part table
912
913       Temp_Files_Table.Init       (Tree.Private_Part.Temp_Files);
914
915       Tree.Private_Part.Current_Source_Path_File := No_Path;
916       Tree.Private_Part.Current_Object_Path_File := No_Path;
917    end Reset;
918
919    -------------------
920    -- Switches_Name --
921    -------------------
922
923    function Switches_Name
924      (Source_File_Name : File_Name_Type) return File_Name_Type
925    is
926    begin
927       return Extend_Name (Source_File_Name, Switches_Dependency_Suffix);
928    end Switches_Name;
929
930    -----------
931    -- Value --
932    -----------
933
934    function Value (Image : String) return Casing_Type is
935    begin
936       for Casing in The_Casing_Images'Range loop
937          if To_Lower (Image) = To_Lower (The_Casing_Images (Casing).all) then
938             return Casing;
939          end if;
940       end loop;
941
942       raise Constraint_Error;
943    end Value;
944
945    ---------------------
946    -- Has_Ada_Sources --
947    ---------------------
948
949    function Has_Ada_Sources (Data : Project_Id) return Boolean is
950       Lang : Language_Ptr;
951
952    begin
953       Lang := Data.Languages;
954       while Lang /= No_Language_Index loop
955          if Lang.Name = Name_Ada then
956             return Lang.First_Source /= No_Source;
957          end if;
958          Lang := Lang.Next;
959       end loop;
960
961       return False;
962    end Has_Ada_Sources;
963
964    ------------------------
965    -- Contains_ALI_Files --
966    ------------------------
967
968    function Contains_ALI_Files (Dir : Path_Name_Type) return Boolean is
969       Dir_Name : constant String := Get_Name_String (Dir);
970       Direct   : Dir_Type;
971       Name     : String (1 .. 1_000);
972       Last     : Natural;
973       Result   : Boolean := False;
974
975    begin
976       Open (Direct, Dir_Name);
977
978       --  For each file in the directory, check if it is an ALI file
979
980       loop
981          Read (Direct, Name, Last);
982          exit when Last = 0;
983          Canonical_Case_File_Name (Name (1 .. Last));
984          Result := Last >= 5 and then Name (Last - 3 .. Last) = ".ali";
985          exit when Result;
986       end loop;
987
988       Close (Direct);
989       return Result;
990
991    exception
992       --  If there is any problem, close the directory if open and return True.
993       --  The library directory will be added to the path.
994
995       when others =>
996          if Is_Open (Direct) then
997             Close (Direct);
998          end if;
999
1000          return True;
1001    end Contains_ALI_Files;
1002
1003    --------------------------
1004    -- Get_Object_Directory --
1005    --------------------------
1006
1007    function Get_Object_Directory
1008      (Project             : Project_Id;
1009       Including_Libraries : Boolean;
1010       Only_If_Ada         : Boolean := False) return Path_Name_Type
1011    is
1012    begin
1013       if (Project.Library and then Including_Libraries)
1014         or else
1015           (Project.Object_Directory /= No_Path_Information
1016             and then (not Including_Libraries or else not Project.Library))
1017       then
1018          --  For a library project, add the library ALI directory if there is
1019          --  no object directory or if the library ALI directory contains ALI
1020          --  files; otherwise add the object directory.
1021
1022          if Project.Library then
1023             if Project.Object_Directory = No_Path_Information
1024               or else Contains_ALI_Files (Project.Library_ALI_Dir.Name)
1025             then
1026                return Project.Library_ALI_Dir.Name;
1027             else
1028                return Project.Object_Directory.Name;
1029             end if;
1030
1031             --  For a non-library project, add object directory if it is not a
1032             --  virtual project, and if there are Ada sources in the project or
1033             --  one of the projects it extends. If there are no Ada sources,
1034             --  adding the object directory could disrupt the order of the
1035             --  object dirs in the path.
1036
1037          elsif not Project.Virtual then
1038             declare
1039                Add_Object_Dir : Boolean;
1040                Prj            : Project_Id;
1041
1042             begin
1043                Add_Object_Dir := not Only_If_Ada;
1044                Prj := Project;
1045                while not Add_Object_Dir and then Prj /= No_Project loop
1046                   if Has_Ada_Sources (Prj) then
1047                      Add_Object_Dir := True;
1048                   else
1049                      Prj := Prj.Extends;
1050                   end if;
1051                end loop;
1052
1053                if Add_Object_Dir then
1054                   return Project.Object_Directory.Name;
1055                end if;
1056             end;
1057          end if;
1058       end if;
1059
1060       return No_Path;
1061    end Get_Object_Directory;
1062
1063    -----------------------------------
1064    -- Ultimate_Extending_Project_Of --
1065    -----------------------------------
1066
1067    function Ultimate_Extending_Project_Of
1068      (Proj : Project_Id) return Project_Id
1069    is
1070       Prj : Project_Id;
1071
1072    begin
1073       Prj := Proj;
1074       while Prj /= null and then Prj.Extended_By /= No_Project loop
1075          Prj := Prj.Extended_By;
1076       end loop;
1077
1078       return Prj;
1079    end Ultimate_Extending_Project_Of;
1080
1081    -----------------------------------
1082    -- Compute_All_Imported_Projects --
1083    -----------------------------------
1084
1085    procedure Compute_All_Imported_Projects (Tree : Project_Tree_Ref) is
1086       Project : Project_Id;
1087
1088       procedure Recursive_Add (Prj : Project_Id; Dummy : in out Boolean);
1089       --  Recursively add the projects imported by project Project, but not
1090       --  those that are extended.
1091
1092       -------------------
1093       -- Recursive_Add --
1094       -------------------
1095
1096       procedure Recursive_Add (Prj : Project_Id; Dummy : in out Boolean) is
1097          pragma Unreferenced (Dummy);
1098          List    : Project_List;
1099          Prj2    : Project_Id;
1100
1101       begin
1102          --  A project is not importing itself
1103
1104          Prj2 := Ultimate_Extending_Project_Of (Prj);
1105
1106          if Project /= Prj2 then
1107
1108             --  Check that the project is not already in the list. We know the
1109             --  one passed to Recursive_Add have never been visited before, but
1110             --  the one passed it are the extended projects.
1111
1112             List := Project.All_Imported_Projects;
1113             while List /= null loop
1114                if List.Project = Prj2 then
1115                   return;
1116                end if;
1117
1118                List := List.Next;
1119             end loop;
1120
1121             --  Add it to the list
1122
1123             Project.All_Imported_Projects :=
1124               new Project_List_Element'
1125                 (Project => Prj2,
1126                  Next    => Project.All_Imported_Projects);
1127          end if;
1128       end Recursive_Add;
1129
1130       procedure For_All_Projects is
1131         new For_Every_Project_Imported (Boolean, Recursive_Add);
1132
1133       Dummy : Boolean := False;
1134       List  : Project_List;
1135
1136    begin
1137       List := Tree.Projects;
1138       while List /= null loop
1139          Project := List.Project;
1140          Free_List (Project.All_Imported_Projects, Free_Project => False);
1141          For_All_Projects (Project, Dummy);
1142          List := List.Next;
1143       end loop;
1144    end Compute_All_Imported_Projects;
1145
1146    -------------------
1147    -- Is_Compilable --
1148    -------------------
1149
1150    function Is_Compilable (Source : Source_Id) return Boolean is
1151    begin
1152       return Source.Language.Config.Compiler_Driver /= No_File
1153         and then Length_Of_Name (Source.Language.Config.Compiler_Driver) /= 0
1154         and then not Source.Locally_Removed
1155         and then (Source.Language.Config.Kind /= File_Based
1156                     or else
1157                   Source.Kind /= Spec);
1158    end Is_Compilable;
1159
1160    ------------------------------
1161    -- Object_To_Global_Archive --
1162    ------------------------------
1163
1164    function Object_To_Global_Archive (Source : Source_Id) return Boolean is
1165    begin
1166       return Source.Language.Config.Kind = File_Based
1167         and then Source.Kind = Impl
1168         and then Source.Language.Config.Objects_Linked
1169         and then Is_Compilable (Source)
1170         and then Source.Language.Config.Object_Generated;
1171    end Object_To_Global_Archive;
1172
1173    ----------------------------
1174    -- Get_Language_From_Name --
1175    ----------------------------
1176
1177    function Get_Language_From_Name
1178      (Project : Project_Id;
1179       Name    : String) return Language_Ptr
1180    is
1181       N      : Name_Id;
1182       Result : Language_Ptr;
1183
1184    begin
1185       Name_Len := Name'Length;
1186       Name_Buffer (1 .. Name_Len) := Name;
1187       To_Lower (Name_Buffer (1 .. Name_Len));
1188       N := Name_Find;
1189
1190       Result := Project.Languages;
1191       while Result /= No_Language_Index loop
1192          if Result.Name = N then
1193             return Result;
1194          end if;
1195
1196          Result := Result.Next;
1197       end loop;
1198
1199       return No_Language_Index;
1200    end Get_Language_From_Name;
1201
1202    ----------------
1203    -- Other_Part --
1204    ----------------
1205
1206    function Other_Part (Source : Source_Id) return Source_Id is
1207    begin
1208       if Source.Unit /= No_Unit_Index then
1209          case Source.Kind is
1210             when Impl =>
1211                return Source.Unit.File_Names (Spec);
1212             when Spec =>
1213                return Source.Unit.File_Names (Impl);
1214             when Sep =>
1215                return No_Source;
1216          end case;
1217       else
1218          return No_Source;
1219       end if;
1220    end Other_Part;
1221
1222    ------------------
1223    -- Create_Flags --
1224    ------------------
1225
1226    function Create_Flags
1227      (Report_Error               : Error_Handler;
1228       When_No_Sources            : Error_Warning;
1229       Require_Sources_Other_Lang : Boolean       := True;
1230       Allow_Duplicate_Basenames  : Boolean       := True;
1231       Compiler_Driver_Mandatory  : Boolean       := False;
1232       Error_On_Unknown_Language  : Boolean       := True;
1233       Require_Obj_Dirs           : Error_Warning := Error;
1234       Allow_Invalid_External     : Error_Warning := Error;
1235       Missing_Source_Files       : Error_Warning := Error)
1236       return Processing_Flags
1237    is
1238    begin
1239       return Processing_Flags'
1240         (Report_Error               => Report_Error,
1241          When_No_Sources            => When_No_Sources,
1242          Require_Sources_Other_Lang => Require_Sources_Other_Lang,
1243          Allow_Duplicate_Basenames  => Allow_Duplicate_Basenames,
1244          Error_On_Unknown_Language  => Error_On_Unknown_Language,
1245          Compiler_Driver_Mandatory  => Compiler_Driver_Mandatory,
1246          Require_Obj_Dirs           => Require_Obj_Dirs,
1247          Allow_Invalid_External     => Allow_Invalid_External,
1248          Missing_Source_Files       => Missing_Source_Files);
1249    end Create_Flags;
1250
1251    ------------
1252    -- Length --
1253    ------------
1254
1255    function Length
1256      (Table : Name_List_Table.Instance;
1257       List  : Name_List_Index) return Natural
1258    is
1259       Count : Natural := 0;
1260       Tmp   : Name_List_Index;
1261
1262    begin
1263       Tmp := List;
1264       while Tmp /= No_Name_List loop
1265          Count := Count + 1;
1266          Tmp := Table.Table (Tmp).Next;
1267       end loop;
1268
1269       return Count;
1270    end Length;
1271
1272 begin
1273    --  Make sure that the standard config and user project file extensions are
1274    --  compatible with canonical case file naming.
1275
1276    Canonical_Case_File_Name (Config_Project_File_Extension);
1277    Canonical_Case_File_Name (Project_File_Extension);
1278 end Prj;