OSDN Git Service

PR preprocessor/20348
[pf3gnuchains/gcc-fork.git] / gcc / ada / prj-nmsc.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             P R J . N M S C                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2000-2005 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 2,  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 COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Err_Vars; use Err_Vars;
28 with Fmap;     use Fmap;
29 with Hostparm;
30 with MLib.Tgt;
31 with Namet;    use Namet;
32 with Osint;    use Osint;
33 with Output;   use Output;
34 with MLib.Tgt; use MLib.Tgt;
35 with Prj.Env;  use Prj.Env;
36 with Prj.Err;
37 with Prj.Util; use Prj.Util;
38 with Sinput.P;
39 with Snames;   use Snames;
40 with Table;    use Table;
41 with Types;    use Types;
42
43 with Ada.Characters.Handling;    use Ada.Characters.Handling;
44 with Ada.Strings;                use Ada.Strings;
45 with Ada.Strings.Fixed;          use Ada.Strings.Fixed;
46 with Ada.Strings.Maps.Constants; use Ada.Strings.Maps.Constants;
47
48 with GNAT.Case_Util;             use GNAT.Case_Util;
49 with GNAT.Directory_Operations;  use GNAT.Directory_Operations;
50 with GNAT.OS_Lib;                use GNAT.OS_Lib;
51 with GNAT.HTable;
52
53 package body Prj.Nmsc is
54
55    Error_Report : Put_Line_Access := null;
56    --  Set to point to error reporting procedure
57
58    ALI_Suffix   : constant String := ".ali";
59    --  File suffix for ali files
60
61    Object_Suffix : constant String := Get_Object_Suffix.all;
62    --  File suffix for object files
63
64    type Name_Location is record
65       Name     : Name_Id;
66       Location : Source_Ptr;
67       Found    : Boolean := False;
68    end record;
69    --  Information about file names found in string list attribute
70    --  Source_Files or in a source list file, stored in hash table
71    --  Source_Names, used by procedure Get_Path_Names_And_Record_Sources.
72
73    No_Name_Location : constant Name_Location :=
74      (Name => No_Name, Location => No_Location, Found => False);
75
76    package Source_Names is new GNAT.HTable.Simple_HTable
77      (Header_Num => Header_Num,
78       Element    => Name_Location,
79       No_Element => No_Name_Location,
80       Key        => Name_Id,
81       Hash       => Hash,
82       Equal      => "=");
83    --  Hash table to store file names found in string list attribute
84    --  Source_Files or in a source list file, stored in hash table
85    --  Source_Names, used by procedure Get_Path_Names_And_Record_Sources.
86
87    package Recursive_Dirs is new GNAT.HTable.Simple_HTable
88      (Header_Num => Header_Num,
89       Element    => Boolean,
90       No_Element => False,
91       Key        => Name_Id,
92       Hash       => Hash,
93       Equal      => "=");
94    --  Hash table to store recursive source directories, to avoid looking
95    --  several times, and to avoid cycles that may be introduced by symbolic
96    --  links.
97
98    type Ada_Naming_Exception_Id is new Nat;
99    No_Ada_Naming_Exception : constant Ada_Naming_Exception_Id := 0;
100
101    type Unit_Info is record
102       Kind : Spec_Or_Body;
103       Unit : Name_Id;
104       Next : Ada_Naming_Exception_Id := No_Ada_Naming_Exception;
105    end record;
106    --  No_Unit : constant Unit_Info :=
107    --              (Specification, No_Name, No_Ada_Naming_Exception);
108
109    package Ada_Naming_Exception_Table is new Table.Table
110      (Table_Component_Type => Unit_Info,
111       Table_Index_Type     => Ada_Naming_Exception_Id,
112       Table_Low_Bound      => 1,
113       Table_Initial        => 20,
114       Table_Increment      => 100,
115       Table_Name           => "Prj.Nmsc.Ada_Naming_Exception_Table");
116
117    package Ada_Naming_Exceptions is new GNAT.HTable.Simple_HTable
118      (Header_Num => Header_Num,
119       Element    => Ada_Naming_Exception_Id,
120       No_Element => No_Ada_Naming_Exception,
121       Key        => Name_Id,
122       Hash       => Hash,
123       Equal      => "=");
124    --  A hash table to store naming exceptions for Ada. For each file name
125    --  there is one or several unit in table Ada_Naming_Exception_Table.
126
127    function Hash (Unit : Unit_Info) return Header_Num;
128
129    type Name_And_Index is record
130       Name  : Name_Id := No_Name;
131       Index : Int     := 0;
132    end record;
133    No_Name_And_Index : constant Name_And_Index :=
134                          (Name => No_Name, Index => 0);
135
136    package Reverse_Ada_Naming_Exceptions is new GNAT.HTable.Simple_HTable
137      (Header_Num => Header_Num,
138       Element    => Name_And_Index,
139       No_Element => No_Name_And_Index,
140       Key        => Unit_Info,
141       Hash       => Hash,
142       Equal      => "=");
143    --  A table to check if a unit with an exceptional name will hide
144    --  a source with a file name following the naming convention.
145
146    function ALI_File_Name (Source : String) return String;
147    --  Return the ALI file name corresponding to a source
148
149    procedure Check_Ada_Name (Name : String; Unit : out Name_Id);
150    --  Check that a name is a valid Ada unit name
151
152    procedure Check_Naming_Scheme
153      (Data    : in out Project_Data;
154       Project : Project_Id;
155       In_Tree : Project_Tree_Ref);
156    --  Check the naming scheme part of Data
157
158    procedure Check_Ada_Naming_Scheme_Validity
159      (Project : Project_Id;
160       In_Tree : Project_Tree_Ref;
161       Naming  : Naming_Data);
162    --  Check that the package Naming is correct
163
164    procedure Check_For_Source
165      (File_Name        : Name_Id;
166       Path_Name        : Name_Id;
167       Project          : Project_Id;
168       In_Tree          : Project_Tree_Ref;
169       Data             : in out Project_Data;
170       Location         : Source_Ptr;
171       Language         : Language_Index;
172       Suffix           : String;
173       Naming_Exception : Boolean);
174    --  Check if a file, with name File_Name and path Path_Name, in a source
175    --  directory is a source for language Language in project Project of
176    --  project tree In_Tree. ???
177
178    procedure Check_If_Externally_Built
179      (Project : Project_Id;
180       In_Tree : Project_Tree_Ref;
181       Data    : in out Project_Data);
182    --  Check attribute Externally_Built of project Project in project tree
183    --  In_Tree and modify its data Data if it has the value "true".
184
185    procedure Check_Library_Attributes
186      (Project   : Project_Id;
187       In_Tree : Project_Tree_Ref;
188       Data      : in out Project_Data);
189    --  Check the library attributes of project Project in project tree In_Tree
190    --  and modify its data Data accordingly.
191
192    procedure Check_Package_Naming
193      (Project : Project_Id;
194       In_Tree : Project_Tree_Ref;
195       Data    : in out Project_Data);
196    --  Check package Naming of project Project in project tree In_Tree and
197    --  modify its data Data accordingly.
198
199    procedure Check_Programming_Languages
200      (In_Tree : Project_Tree_Ref; Data : in out Project_Data);
201    --  Check attribute Languages for the project with data Data in project
202    --  tree In_Tree and set the components of Data for all the programming
203    --  languages indicated in attribute Languages, if any.
204
205    function Check_Project
206      (P            : Project_Id;
207       Root_Project : Project_Id;
208       In_Tree      : Project_Tree_Ref;
209       Extending    : Boolean) return Boolean;
210    --  Returns True if P is Root_Project or, if Extending is True, a project
211    --  extended by Root_Project.
212
213    procedure Check_Stand_Alone_Library
214      (Project   : Project_Id;
215       In_Tree   : Project_Tree_Ref;
216       Data      : in out Project_Data;
217       Extending : Boolean);
218    --  Check if project Project in project tree In_Tree is a Stand-Alone
219    --  Library project, and modify its data Data accordingly if it is one.
220
221    function Compute_Directory_Last (Dir : String) return Natural;
222    --  Return the index of the last significant character in Dir. This is used
223    --  to avoid duplicates '/' at the end of directory names
224
225    function Body_Suffix_Of
226      (Language   : Language_Index;
227       In_Project : Project_Data;
228       In_Tree    : Project_Tree_Ref)
229       return String;
230    --  Returns the suffix of sources of language Language in project In_Project
231    --  in project tree In_Tree.
232
233    procedure Error_Msg
234      (Project       : Project_Id;
235       In_Tree       : Project_Tree_Ref;
236       Msg           : String;
237       Flag_Location : Source_Ptr);
238    --  Output an error message. If Error_Report is null, simply call
239    --  Prj.Err.Error_Msg. Otherwise, disregard Flag_Location and use
240    --  Error_Report.
241
242    procedure Find_Sources
243      (Project      : Project_Id;
244       In_Tree      : Project_Tree_Ref;
245       Data         : in out Project_Data;
246       For_Language : Language_Index;
247       Follow_Links : Boolean := False);
248    --  Find all the sources in all of the source directories of a project for
249    --  a specified language.
250
251    procedure Free_Ada_Naming_Exceptions;
252    --  Free the internal hash tables used for checking naming exceptions
253
254    procedure Get_Directories
255      (Project : Project_Id;
256       In_Tree : Project_Tree_Ref;
257       Data    : in out Project_Data);
258    --  Get the object directory, the exec directory and the source directories
259    --  of a project.
260
261    procedure Get_Mains
262      (Project : Project_Id;
263       In_Tree : Project_Tree_Ref;
264       Data    : in out Project_Data);
265    --  Get the mains of a project from attribute Main, if it exists, and put
266    --  them in the project data.
267
268    procedure Get_Sources_From_File
269      (Path     : String;
270       Location : Source_Ptr;
271       Project  : Project_Id;
272       In_Tree  : Project_Tree_Ref);
273    --  Get the list of sources from a text file and put them in hash table
274    --  Source_Names.
275
276    procedure Get_Unit
277      (Canonical_File_Name : Name_Id;
278       Naming              : Naming_Data;
279       Exception_Id        : out Ada_Naming_Exception_Id;
280       Unit_Name           : out Name_Id;
281       Unit_Kind           : out Spec_Or_Body;
282       Needs_Pragma        : out Boolean);
283    --  Find out, from a file name, the unit name, the unit kind and if a
284    --  specific SFN pragma is needed. If the file name corresponds to no
285    --  unit, then Unit_Name will be No_Name. If the file is a multi-unit source
286    --  or an exception to the naming scheme, then Exception_Id is set to
287    --  the unit or units that the source contains.
288
289    function Is_Illegal_Suffix
290      (Suffix                          : String;
291       Dot_Replacement_Is_A_Single_Dot : Boolean) return Boolean;
292    --  Returns True if the string Suffix cannot be used as
293    --  a spec suffix, a body suffix or a separate suffix.
294
295    procedure Locate_Directory
296      (Name    : Name_Id;
297       Parent  : Name_Id;
298       Dir     : out Name_Id;
299       Display : out Name_Id);
300    --  Locate a directory (returns No_Name for Dir and Display if directory
301    --  does not exist). Name is the directory name. Parent is the root
302    --  directory, if Name is a relative path name. Dir is the canonical case
303    --  path name of the directory, Display is the directory path name for
304    --  display purposes.
305
306    procedure Look_For_Sources
307      (Project      : Project_Id;
308       In_Tree      : Project_Tree_Ref;
309       Data         : in out Project_Data;
310       Follow_Links : Boolean);
311    --  Find all the sources of a project
312
313    function Path_Name_Of
314      (File_Name : Name_Id;
315       Directory : Name_Id) return String;
316    --  Returns the path name of a (non project) file.
317    --  Returns an empty string if file cannot be found.
318
319    procedure Prepare_Ada_Naming_Exceptions
320      (List    : Array_Element_Id;
321       In_Tree : Project_Tree_Ref;
322       Kind    : Spec_Or_Body);
323    --  Prepare the internal hash tables used for checking naming exceptions
324    --  for Ada. Insert all elements of List in the tables.
325
326    function Project_Extends
327      (Extending : Project_Id;
328       Extended  : Project_Id;
329       In_Tree   : Project_Tree_Ref) return Boolean;
330    --  Returns True if Extending is extending Extended either directly or
331    --  indirectly.
332
333    procedure Record_Ada_Source
334      (File_Name       : Name_Id;
335       Path_Name       : Name_Id;
336       Project         : Project_Id;
337       In_Tree         : Project_Tree_Ref;
338       Data            : in out Project_Data;
339       Location        : Source_Ptr;
340       Current_Source  : in out String_List_Id;
341       Source_Recorded : in out Boolean;
342       Follow_Links    : Boolean);
343    --  Put a unit in the list of units of a project, if the file name
344    --  corresponds to a valid unit name.
345
346    procedure Record_Other_Sources
347      (Project           : Project_Id;
348       In_Tree           : Project_Tree_Ref;
349       Data              : in out Project_Data;
350       Language          : Language_Index;
351       Naming_Exceptions : Boolean);
352    --  Record the sources of a language in a project.
353    --  When Naming_Exceptions is True, mark the found sources as such, to
354    --  later remove those that are not named in a list of sources.
355
356    procedure Show_Source_Dirs
357      (Project : Project_Id; In_Tree : Project_Tree_Ref);
358    --  List all the source directories of a project
359
360    function Suffix_For
361      (Language : Language_Index;
362       Naming   : Naming_Data;
363       In_Tree  : Project_Tree_Ref) return Name_Id;
364    --  Get the suffix for the source of a language from a package naming.
365    --  If not specified, return the default for the language.
366
367    procedure Warn_If_Not_Sources
368      (Project     : Project_Id;
369       In_Tree     : Project_Tree_Ref;
370       Conventions : Array_Element_Id;
371       Specs       : Boolean;
372       Extending   : Boolean);
373    --  Check that individual naming conventions apply to immediate
374    --  sources of the project; if not, issue a warning.
375
376    -------------------
377    -- ALI_File_Name --
378    -------------------
379
380    function ALI_File_Name (Source : String) return String is
381    begin
382       --  If the source name has an extension, then replace it with
383       --  the ALI suffix.
384
385       for Index in reverse Source'First + 1 .. Source'Last loop
386          if Source (Index) = '.' then
387             return Source (Source'First .. Index - 1) & ALI_Suffix;
388          end if;
389       end loop;
390
391       --  If there is no dot, or if it is the first character, just add the
392       --  ALI suffix.
393
394       return Source & ALI_Suffix;
395    end ALI_File_Name;
396
397    -----------
398    -- Check --
399    -----------
400
401    procedure Check
402      (Project      : Project_Id;
403       In_Tree      : Project_Tree_Ref;
404       Report_Error : Put_Line_Access;
405       Follow_Links : Boolean)
406    is
407       Data      : Project_Data := In_Tree.Projects.Table (Project);
408       Extending : Boolean := False;
409
410    begin
411       Error_Report := Report_Error;
412
413       Recursive_Dirs.Reset;
414
415       --  Object, exec and source directories
416
417       Get_Directories (Project, In_Tree, Data);
418
419       --  Get the programming languages
420
421       Check_Programming_Languages (In_Tree, Data);
422
423       --  Library attributes
424
425       Check_Library_Attributes (Project, In_Tree, Data);
426
427       Check_If_Externally_Built (Project, In_Tree, Data);
428
429       if Current_Verbosity = High then
430          Show_Source_Dirs (Project, In_Tree);
431       end if;
432
433       Check_Package_Naming (Project, In_Tree, Data);
434
435       Extending := Data.Extends /= No_Project;
436
437       Check_Naming_Scheme (Data, Project, In_Tree);
438
439       Prepare_Ada_Naming_Exceptions
440         (Data.Naming.Bodies, In_Tree, Body_Part);
441       Prepare_Ada_Naming_Exceptions
442         (Data.Naming.Specs, In_Tree, Specification);
443
444       --  Find the sources
445
446       if Data.Source_Dirs /= Nil_String then
447          Look_For_Sources (Project, In_Tree, Data, Follow_Links);
448       end if;
449
450       if Data.Ada_Sources_Present then
451
452          --  Check that all individual naming conventions apply to sources of
453          --  this project file.
454
455          Warn_If_Not_Sources
456            (Project, In_Tree, Data.Naming.Bodies,
457             Specs     => False,
458             Extending => Extending);
459          Warn_If_Not_Sources
460            (Project, In_Tree, Data.Naming.Specs,
461             Specs     => True,
462             Extending => Extending);
463       end if;
464
465       --  If it is a library project file, check if it is a standalone library
466
467       if Data.Library then
468          Check_Stand_Alone_Library (Project, In_Tree, Data, Extending);
469       end if;
470
471       --  Put the list of Mains, if any, in the project data
472
473       Get_Mains (Project, In_Tree, Data);
474
475       --  Update the project data in the Projects table
476
477       In_Tree.Projects.Table (Project) := Data;
478
479       Free_Ada_Naming_Exceptions;
480    end Check;
481
482    --------------------
483    -- Check_Ada_Name --
484    --------------------
485
486    procedure Check_Ada_Name (Name : String; Unit : out Name_Id) is
487       The_Name        : String := Name;
488       Real_Name       : Name_Id;
489       Need_Letter     : Boolean := True;
490       Last_Underscore : Boolean := False;
491       OK              : Boolean := The_Name'Length > 0;
492
493    begin
494       To_Lower (The_Name);
495
496       Name_Len := The_Name'Length;
497       Name_Buffer (1 .. Name_Len) := The_Name;
498       Real_Name := Name_Find;
499
500       --  Check first that the given name is not an Ada reserved word
501
502       if Get_Name_Table_Byte (Real_Name) /= 0
503         and then Real_Name /= Name_Project
504         and then Real_Name /= Name_Extends
505         and then Real_Name /= Name_External
506       then
507          Unit := No_Name;
508
509          if Current_Verbosity = High then
510             Write_Str (The_Name);
511             Write_Line (" is an Ada reserved word.");
512          end if;
513
514          return;
515       end if;
516
517       for Index in The_Name'Range loop
518          if Need_Letter then
519
520             --  We need a letter (at the beginning, and following a dot),
521             --  but we don't have one.
522
523             if Is_Letter (The_Name (Index)) then
524                Need_Letter := False;
525
526             else
527                OK := False;
528
529                if Current_Verbosity = High then
530                   Write_Int  (Types.Int (Index));
531                   Write_Str  (": '");
532                   Write_Char (The_Name (Index));
533                   Write_Line ("' is not a letter.");
534                end if;
535
536                exit;
537             end if;
538
539          elsif Last_Underscore
540            and then (The_Name (Index) = '_' or else The_Name (Index) = '.')
541          then
542             --  Two underscores are illegal, and a dot cannot follow
543             --  an underscore.
544
545             OK := False;
546
547             if Current_Verbosity = High then
548                Write_Int  (Types.Int (Index));
549                Write_Str  (": '");
550                Write_Char (The_Name (Index));
551                Write_Line ("' is illegal here.");
552             end if;
553
554             exit;
555
556          elsif The_Name (Index) = '.' then
557
558             --  We need a letter after a dot
559
560             Need_Letter := True;
561
562          elsif The_Name (Index) = '_' then
563             Last_Underscore := True;
564
565          else
566             --  We need an letter or a digit
567
568             Last_Underscore := False;
569
570             if not Is_Alphanumeric (The_Name (Index)) then
571                OK := False;
572
573                if Current_Verbosity = High then
574                   Write_Int  (Types.Int (Index));
575                   Write_Str  (": '");
576                   Write_Char (The_Name (Index));
577                   Write_Line ("' is not alphanumeric.");
578                end if;
579
580                exit;
581             end if;
582          end if;
583       end loop;
584
585       --  Cannot end with an underscore or a dot
586
587       OK := OK and then not Need_Letter and then not Last_Underscore;
588
589       if OK then
590          Unit := Real_Name;
591
592       else
593          --  Signal a problem with No_Name
594
595          Unit := No_Name;
596       end if;
597    end Check_Ada_Name;
598
599    --------------------------------------
600    -- Check_Ada_Naming_Scheme_Validity --
601    --------------------------------------
602
603    procedure Check_Ada_Naming_Scheme_Validity
604      (Project : Project_Id;
605       In_Tree : Project_Tree_Ref;
606       Naming  : Naming_Data)
607    is
608    begin
609       --  Only check if we are not using the Default naming scheme
610
611       if Naming /= In_Tree.Private_Part.Default_Naming then
612          declare
613             Dot_Replacement       : constant String :=
614                                      Get_Name_String
615                                        (Naming.Dot_Replacement);
616
617             Spec_Suffix : constant String :=
618                                      Get_Name_String
619                                        (Naming.Ada_Spec_Suffix);
620
621             Body_Suffix : constant String :=
622                                      Get_Name_String
623                                        (Naming.Ada_Body_Suffix);
624
625             Separate_Suffix       : constant String :=
626                                      Get_Name_String
627                                        (Naming.Separate_Suffix);
628
629          begin
630             --  Dot_Replacement cannot
631             --   - be empty
632             --   - start or end with an alphanumeric
633             --   - be a single '_'
634             --   - start with an '_' followed by an alphanumeric
635             --   - contain a '.' except if it is "."
636
637             if Dot_Replacement'Length = 0
638               or else Is_Alphanumeric
639                         (Dot_Replacement (Dot_Replacement'First))
640               or else Is_Alphanumeric
641                         (Dot_Replacement (Dot_Replacement'Last))
642               or else (Dot_Replacement (Dot_Replacement'First) = '_'
643                         and then
644                         (Dot_Replacement'Length = 1
645                           or else
646                            Is_Alphanumeric
647                              (Dot_Replacement (Dot_Replacement'First + 1))))
648               or else (Dot_Replacement'Length > 1
649                          and then
650                            Index (Source => Dot_Replacement,
651                                   Pattern => ".") /= 0)
652             then
653                Error_Msg
654                  (Project, In_Tree,
655                   '"' & Dot_Replacement &
656                   """ is illegal for Dot_Replacement.",
657                   Naming.Dot_Repl_Loc);
658             end if;
659
660             --  Suffixes cannot
661             --   - be empty
662
663             if Is_Illegal_Suffix
664                  (Spec_Suffix, Dot_Replacement = ".")
665             then
666                Err_Vars.Error_Msg_Name_1 := Naming.Ada_Spec_Suffix;
667                Error_Msg
668                  (Project, In_Tree,
669                   "{ is illegal for Spec_Suffix",
670                   Naming.Spec_Suffix_Loc);
671             end if;
672
673             if Is_Illegal_Suffix
674                  (Body_Suffix, Dot_Replacement = ".")
675             then
676                Err_Vars.Error_Msg_Name_1 := Naming.Ada_Body_Suffix;
677                Error_Msg
678                  (Project, In_Tree,
679                   "{ is illegal for Body_Suffix",
680                   Naming.Body_Suffix_Loc);
681             end if;
682
683             if Body_Suffix /= Separate_Suffix then
684                if Is_Illegal_Suffix
685                     (Separate_Suffix, Dot_Replacement = ".")
686                then
687                   Err_Vars.Error_Msg_Name_1 := Naming.Separate_Suffix;
688                   Error_Msg
689                     (Project, In_Tree,
690                      "{ is illegal for Separate_Suffix",
691                      Naming.Sep_Suffix_Loc);
692                end if;
693             end if;
694
695             --  Spec_Suffix cannot have the same termination as
696             --  Body_Suffix or Separate_Suffix
697
698             if Spec_Suffix'Length <= Body_Suffix'Length
699               and then
700                 Body_Suffix (Body_Suffix'Last -
701                              Spec_Suffix'Length + 1 ..
702                              Body_Suffix'Last) = Spec_Suffix
703             then
704                Error_Msg
705                  (Project, In_Tree,
706                   "Body_Suffix (""" &
707                   Body_Suffix &
708                   """) cannot end with" &
709                   " Spec_Suffix  (""" &
710                   Spec_Suffix & """).",
711                   Naming.Body_Suffix_Loc);
712             end if;
713
714             if Body_Suffix /= Separate_Suffix
715               and then Spec_Suffix'Length <= Separate_Suffix'Length
716               and then
717                 Separate_Suffix
718                   (Separate_Suffix'Last - Spec_Suffix'Length + 1
719                     ..
720                    Separate_Suffix'Last) = Spec_Suffix
721             then
722                Error_Msg
723                  (Project, In_Tree,
724                   "Separate_Suffix (""" &
725                   Separate_Suffix &
726                   """) cannot end with" &
727                   " Spec_Suffix (""" &
728                   Spec_Suffix & """).",
729                   Naming.Sep_Suffix_Loc);
730             end if;
731          end;
732       end if;
733    end Check_Ada_Naming_Scheme_Validity;
734
735    ----------------------
736    -- Check_For_Source --
737    ----------------------
738
739    procedure Check_For_Source
740      (File_Name        : Name_Id;
741       Path_Name        : Name_Id;
742       Project          : Project_Id;
743       In_Tree          : Project_Tree_Ref;
744       Data             : in out Project_Data;
745       Location         : Source_Ptr;
746       Language         : Language_Index;
747       Suffix           : String;
748       Naming_Exception : Boolean)
749    is
750       Name : String := Get_Name_String (File_Name);
751       Real_Location : Source_Ptr := Location;
752
753    begin
754       Canonical_Case_File_Name (Name);
755
756       --  A file is a source of a language if Naming_Exception is True (case
757       --  of naming exceptions) or if its file name ends with the suffix.
758
759       if Naming_Exception or else
760         (Name'Length > Suffix'Length and then
761          Name (Name'Last - Suffix'Length + 1 .. Name'Last) = Suffix)
762       then
763          if Real_Location = No_Location then
764             Real_Location := Data.Location;
765          end if;
766
767          declare
768             Path : String := Get_Name_String (Path_Name);
769
770             Path_Id : Name_Id;
771             --  The path name id (in canonical case)
772
773             File_Id : Name_Id;
774             --  The file name id (in canonical case)
775
776             Obj_Id : Name_Id;
777             --  The object file name
778
779             Obj_Path_Id : Name_Id;
780             --  The object path name
781
782             Dep_Id : Name_Id;
783             --  The dependency file name
784
785             Dep_Path_Id : Name_Id;
786             --  The dependency path name
787
788             Dot_Pos : Natural := 0;
789             --  Position of the last dot in Name
790
791             Source    : Other_Source;
792             Source_Id : Other_Source_Id := Data.First_Other_Source;
793
794          begin
795             Canonical_Case_File_Name (Path);
796
797             --  Get the file name id
798
799             Name_Len := Name'Length;
800             Name_Buffer (1 .. Name_Len) := Name;
801             File_Id := Name_Find;
802
803             --  Get the path name id
804
805             Name_Len := Path'Length;
806             Name_Buffer (1 .. Name_Len) := Path;
807             Path_Id := Name_Find;
808
809             --  Find the position of the last dot
810
811             for J in reverse Name'Range loop
812                if Name (J) = '.' then
813                   Dot_Pos := J;
814                   exit;
815                end if;
816             end loop;
817
818             if Dot_Pos <= Name'First then
819                Dot_Pos := Name'Last + 1;
820             end if;
821
822             --  Compute the object file name
823
824             Get_Name_String (File_Id);
825             Name_Len := Dot_Pos - Name'First;
826
827             for J in Object_Suffix'Range loop
828                Name_Len := Name_Len + 1;
829                Name_Buffer (Name_Len) := Object_Suffix (J);
830             end loop;
831
832             Obj_Id := Name_Find;
833
834             --  Compute the object path name
835
836             Get_Name_String (Data.Object_Directory);
837
838             if Name_Buffer (Name_Len) /= Directory_Separator and then
839               Name_Buffer (Name_Len) /= '/'
840             then
841                Name_Len := Name_Len + 1;
842                Name_Buffer (Name_Len) := Directory_Separator;
843             end if;
844
845             Add_Str_To_Name_Buffer (Get_Name_String (Obj_Id));
846             Obj_Path_Id := Name_Find;
847
848             --  Compute the dependency file name
849
850             Get_Name_String (File_Id);
851             Name_Len := Dot_Pos - Name'First + 1;
852             Name_Buffer (Name_Len) := '.';
853             Name_Len := Name_Len + 1;
854             Name_Buffer (Name_Len) := 'd';
855             Dep_Id := Name_Find;
856
857             --  Compute the dependency path name
858
859             Get_Name_String (Data.Object_Directory);
860
861             if Name_Buffer (Name_Len) /= Directory_Separator and then
862               Name_Buffer (Name_Len) /= '/'
863             then
864                Name_Len := Name_Len + 1;
865                Name_Buffer (Name_Len) := Directory_Separator;
866             end if;
867
868             Add_Str_To_Name_Buffer (Get_Name_String (Dep_Id));
869             Dep_Path_Id := Name_Find;
870
871             --  Check if source is already in the list of source for this
872             --  project: it may have already been specified as a naming
873             --  exception for the same language or an other language, or
874             --  they may be two identical file names in different source
875             --  directories.
876
877             while Source_Id /= No_Other_Source loop
878                Source := In_Tree.Other_Sources.Table (Source_Id);
879                Source_Id := Source.Next;
880
881                if Source.File_Name = File_Id then
882
883                   --  Two sources of different languages cannot have the same
884                   --  file name.
885
886                   if Source.Language /= Language then
887                      Error_Msg_Name_1 := File_Name;
888                      Error_Msg
889                        (Project, In_Tree,
890                         "{ cannot be a source of several languages",
891                         Real_Location);
892                      return;
893
894                   --  No problem if a file has already been specified as
895                   --  a naming exception of this language.
896
897                   elsif Source.Path_Name = Path_Id then
898
899                      --  Reset the naming exception flag, if this is not a
900                      --  naming exception.
901
902                      if not Naming_Exception then
903                         In_Tree.Other_Sources.Table
904                           (Source_Id).Naming_Exception := False;
905                      end if;
906
907                      return;
908
909                   --  There are several files with the same names, but the
910                   --  order of the source directories is known (no /**):
911                   --  only the first one encountered is kept, the other ones
912                   --  are ignored.
913
914                   elsif Data.Known_Order_Of_Source_Dirs then
915                      return;
916
917                   --  But it is an error if the order of the source directories
918                   --  is not known.
919
920                   else
921                      Error_Msg_Name_1 := File_Name;
922                      Error_Msg
923                        (Project, In_Tree,
924                         "{ is found in several source directories",
925                         Real_Location);
926                      return;
927                   end if;
928
929                --  Two sources with different file names cannot have the same
930                --  object file name.
931
932                elsif Source.Object_Name = Obj_Id then
933                   Error_Msg_Name_1 := File_Id;
934                   Error_Msg_Name_2 := Source.File_Name;
935                   Error_Msg_Name_3 := Obj_Id;
936                   Error_Msg
937                        (Project, In_Tree,
938                         "{ and { have the same object file {",
939                         Real_Location);
940                      return;
941                end if;
942             end loop;
943
944             if Current_Verbosity = High then
945                Write_Str ("      found ");
946                Display_Language_Name (Language);
947                Write_Str (" source """);
948                Write_Str (Get_Name_String (File_Name));
949                Write_Line ("""");
950                Write_Str ("      object path = ");
951                Write_Line (Get_Name_String (Obj_Path_Id));
952             end if;
953
954             --  Create the Other_Source record
955
956             Source :=
957               (Language         => Language,
958                File_Name        => File_Id,
959                Path_Name        => Path_Id,
960                Source_TS        => File_Stamp (Path_Id),
961                Object_Name      => Obj_Id,
962                Object_Path      => Obj_Path_Id,
963                Object_TS        => File_Stamp (Obj_Path_Id),
964                Dep_Name         => Dep_Id,
965                Dep_Path         => Dep_Path_Id,
966                Dep_TS           => File_Stamp (Dep_Path_Id),
967                Naming_Exception => Naming_Exception,
968                Next             => No_Other_Source);
969
970             --  And add it to the Other_Sources table
971
972             Other_Source_Table.Increment_Last
973               (In_Tree.Other_Sources);
974             In_Tree.Other_Sources.Table
975               (Other_Source_Table.Last (In_Tree.Other_Sources)) :=
976                  Source;
977
978             --  There are sources of languages other than Ada in this project
979
980             Data.Other_Sources_Present := True;
981
982             --  And there are sources of this language in this project
983
984             Set (Language, True, Data, In_Tree);
985
986             --  Add this source to the list of sources of languages other than
987             --  Ada of the project.
988
989             if Data.First_Other_Source = No_Other_Source then
990                Data.First_Other_Source :=
991                  Other_Source_Table.Last (In_Tree.Other_Sources);
992
993             else
994                In_Tree.Other_Sources.Table (Data.Last_Other_Source).Next :=
995                  Other_Source_Table.Last (In_Tree.Other_Sources);
996             end if;
997
998             Data.Last_Other_Source :=
999               Other_Source_Table.Last (In_Tree.Other_Sources);
1000          end;
1001       end if;
1002    end Check_For_Source;
1003
1004    -------------------------------
1005    -- Check_If_Externally_Built --
1006    -------------------------------
1007
1008    procedure Check_If_Externally_Built
1009      (Project : Project_Id;
1010       In_Tree : Project_Tree_Ref;
1011       Data    : in out Project_Data)
1012    is
1013       Externally_Built : constant Variable_Value :=
1014                            Util.Value_Of
1015                             (Name_Externally_Built,
1016                              Data.Decl.Attributes, In_Tree);
1017
1018    begin
1019       if not Externally_Built.Default then
1020          Get_Name_String (Externally_Built.Value);
1021          To_Lower (Name_Buffer (1 .. Name_Len));
1022
1023          if Name_Buffer (1 .. Name_Len) = "true" then
1024             Data.Externally_Built := True;
1025
1026          elsif Name_Buffer (1 .. Name_Len) /= "false" then
1027             Error_Msg (Project, In_Tree,
1028                        "Externally_Built may only be true or false",
1029                        Externally_Built.Location);
1030          end if;
1031       end if;
1032
1033       if Current_Verbosity = High then
1034          Write_Str ("Project is ");
1035
1036          if not Data.Externally_Built then
1037             Write_Str ("not ");
1038          end if;
1039
1040          Write_Line ("externally built.");
1041       end if;
1042    end Check_If_Externally_Built;
1043
1044    -----------------------------
1045    -- Check_Naming_Scheme --
1046    -----------------------------
1047
1048    procedure Check_Naming_Scheme
1049      (Data    : in out Project_Data;
1050       Project : Project_Id;
1051       In_Tree : Project_Tree_Ref)
1052    is
1053       Naming_Id : constant Package_Id :=
1054                     Util.Value_Of (Name_Naming, Data.Decl.Packages, In_Tree);
1055
1056       Naming : Package_Element;
1057
1058       procedure Check_Unit_Names (List : Array_Element_Id);
1059       --  Check that a list of unit names contains only valid names
1060
1061       ----------------------
1062       -- Check_Unit_Names --
1063       ----------------------
1064
1065       procedure Check_Unit_Names (List : Array_Element_Id) is
1066          Current   : Array_Element_Id := List;
1067          Element   : Array_Element;
1068          Unit_Name : Name_Id;
1069
1070       begin
1071          --  Loop through elements of the string list
1072
1073          while Current /= No_Array_Element loop
1074             Element := In_Tree.Array_Elements.Table (Current);
1075
1076             --  Put file name in canonical case
1077
1078             Get_Name_String (Element.Value.Value);
1079             Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1080             Element.Value.Value := Name_Find;
1081
1082             --  Check that it contains a valid unit name
1083
1084             Get_Name_String (Element.Index);
1085             Check_Ada_Name (Name_Buffer (1 .. Name_Len), Unit_Name);
1086
1087             if Unit_Name = No_Name then
1088                Err_Vars.Error_Msg_Name_1 := Element.Index;
1089                Error_Msg
1090                  (Project, In_Tree,
1091                   "{ is not a valid unit name.",
1092                   Element.Value.Location);
1093
1094             else
1095                if Current_Verbosity = High then
1096                   Write_Str ("    Unit (""");
1097                   Write_Str (Get_Name_String (Unit_Name));
1098                   Write_Line (""")");
1099                end if;
1100
1101                Element.Index := Unit_Name;
1102                In_Tree.Array_Elements.Table (Current) := Element;
1103             end if;
1104
1105             Current := Element.Next;
1106          end loop;
1107       end Check_Unit_Names;
1108
1109    --  Start of processing for Check_Naming_Scheme
1110
1111    begin
1112       --  If there is a package Naming, we will put in Data.Naming what is in
1113       --  this package Naming.
1114
1115       if Naming_Id /= No_Package then
1116          Naming := In_Tree.Packages.Table (Naming_Id);
1117
1118          if Current_Verbosity = High then
1119             Write_Line ("Checking ""Naming"" for Ada.");
1120          end if;
1121
1122          declare
1123             Bodies : constant Array_Element_Id :=
1124                        Util.Value_Of (Name_Body, Naming.Decl.Arrays, In_Tree);
1125
1126             Specs : constant Array_Element_Id :=
1127                       Util.Value_Of (Name_Spec, Naming.Decl.Arrays, In_Tree);
1128
1129          begin
1130             if Bodies /= No_Array_Element then
1131
1132                --  We have elements in the array Body_Part
1133
1134                if Current_Verbosity = High then
1135                   Write_Line ("Found Bodies.");
1136                end if;
1137
1138                Data.Naming.Bodies := Bodies;
1139                Check_Unit_Names (Bodies);
1140
1141             else
1142                if Current_Verbosity = High then
1143                   Write_Line ("No Bodies.");
1144                end if;
1145             end if;
1146
1147             if Specs /= No_Array_Element then
1148
1149                --  We have elements in the array Specs
1150
1151                if Current_Verbosity = High then
1152                   Write_Line ("Found Specs.");
1153                end if;
1154
1155                Data.Naming.Specs := Specs;
1156                Check_Unit_Names (Specs);
1157
1158             else
1159                if Current_Verbosity = High then
1160                   Write_Line ("No Specs.");
1161                end if;
1162             end if;
1163          end;
1164
1165          --  We are now checking if variables Dot_Replacement, Casing,
1166          --  Spec_Suffix, Body_Suffix and/or Separate_Suffix
1167          --  exist.
1168
1169          --  For each variable, if it does not exist, we do nothing,
1170          --  because we already have the default.
1171
1172          --  Check Dot_Replacement
1173
1174          declare
1175             Dot_Replacement : constant Variable_Value :=
1176                                 Util.Value_Of
1177                                   (Name_Dot_Replacement,
1178                                    Naming.Decl.Attributes, In_Tree);
1179
1180          begin
1181             pragma Assert (Dot_Replacement.Kind = Single,
1182                            "Dot_Replacement is not a single string");
1183
1184             if not Dot_Replacement.Default then
1185                Get_Name_String (Dot_Replacement.Value);
1186
1187                if Name_Len = 0 then
1188                   Error_Msg
1189                     (Project, In_Tree,
1190                      "Dot_Replacement cannot be empty",
1191                      Dot_Replacement.Location);
1192
1193                else
1194                   Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1195                   Data.Naming.Dot_Replacement := Name_Find;
1196                   Data.Naming.Dot_Repl_Loc := Dot_Replacement.Location;
1197                end if;
1198             end if;
1199          end;
1200
1201          if Current_Verbosity = High then
1202             Write_Str  ("  Dot_Replacement = """);
1203             Write_Str  (Get_Name_String (Data.Naming.Dot_Replacement));
1204             Write_Char ('"');
1205             Write_Eol;
1206          end if;
1207
1208          --  Check Casing
1209
1210          declare
1211             Casing_String : constant Variable_Value :=
1212                               Util.Value_Of
1213                                 (Name_Casing, Naming.Decl.Attributes, In_Tree);
1214
1215          begin
1216             pragma Assert (Casing_String.Kind = Single,
1217                            "Casing is not a single string");
1218
1219             if not Casing_String.Default then
1220                declare
1221                   Casing_Image : constant String :=
1222                                    Get_Name_String (Casing_String.Value);
1223                begin
1224                   declare
1225                      Casing_Value : constant Casing_Type :=
1226                                       Value (Casing_Image);
1227                   begin
1228                      Data.Naming.Casing := Casing_Value;
1229                   end;
1230
1231                exception
1232                   when Constraint_Error =>
1233                      if Casing_Image'Length = 0 then
1234                         Error_Msg
1235                           (Project, In_Tree,
1236                            "Casing cannot be an empty string",
1237                            Casing_String.Location);
1238
1239                      else
1240                         Name_Len := Casing_Image'Length;
1241                         Name_Buffer (1 .. Name_Len) := Casing_Image;
1242                         Err_Vars.Error_Msg_Name_1 := Name_Find;
1243                         Error_Msg
1244                           (Project, In_Tree,
1245                            "{ is not a correct Casing",
1246                            Casing_String.Location);
1247                      end if;
1248                end;
1249             end if;
1250          end;
1251
1252          if Current_Verbosity = High then
1253             Write_Str  ("  Casing = ");
1254             Write_Str  (Image (Data.Naming.Casing));
1255             Write_Char ('.');
1256             Write_Eol;
1257          end if;
1258
1259          --  Check Spec_Suffix
1260
1261          declare
1262             Ada_Spec_Suffix : constant Variable_Value :=
1263                                 Prj.Util.Value_Of
1264                                   (Index     => Name_Ada,
1265                                    Src_Index => 0,
1266                                    In_Array  => Data.Naming.Spec_Suffix,
1267                                    In_Tree   => In_Tree);
1268
1269          begin
1270             if Ada_Spec_Suffix.Kind = Single
1271               and then Get_Name_String (Ada_Spec_Suffix.Value) /= ""
1272             then
1273                Get_Name_String (Ada_Spec_Suffix.Value);
1274                Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1275                Data.Naming.Ada_Spec_Suffix := Name_Find;
1276                Data.Naming.Spec_Suffix_Loc := Ada_Spec_Suffix.Location;
1277
1278             else
1279                Data.Naming.Ada_Spec_Suffix := Default_Ada_Spec_Suffix;
1280             end if;
1281          end;
1282
1283          if Current_Verbosity = High then
1284             Write_Str  ("  Spec_Suffix = """);
1285             Write_Str  (Get_Name_String (Data.Naming.Ada_Spec_Suffix));
1286             Write_Char ('"');
1287             Write_Eol;
1288          end if;
1289
1290          --  Check Body_Suffix
1291
1292          declare
1293             Ada_Body_Suffix : constant Variable_Value :=
1294               Prj.Util.Value_Of
1295                 (Index     => Name_Ada,
1296                  Src_Index => 0,
1297                  In_Array  => Data.Naming.Body_Suffix,
1298                  In_Tree   => In_Tree);
1299
1300          begin
1301             if Ada_Body_Suffix.Kind = Single
1302               and then Get_Name_String (Ada_Body_Suffix.Value) /= ""
1303             then
1304                Get_Name_String (Ada_Body_Suffix.Value);
1305                Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1306                Data.Naming.Ada_Body_Suffix := Name_Find;
1307                Data.Naming.Body_Suffix_Loc := Ada_Body_Suffix.Location;
1308
1309             else
1310                Data.Naming.Ada_Body_Suffix := Default_Ada_Body_Suffix;
1311             end if;
1312          end;
1313
1314          if Current_Verbosity = High then
1315             Write_Str  ("  Body_Suffix = """);
1316             Write_Str  (Get_Name_String (Data.Naming.Ada_Body_Suffix));
1317             Write_Char ('"');
1318             Write_Eol;
1319          end if;
1320
1321          --  Check Separate_Suffix
1322
1323          declare
1324             Ada_Sep_Suffix : constant Variable_Value :=
1325                                Prj.Util.Value_Of
1326                                  (Variable_Name => Name_Separate_Suffix,
1327                                   In_Variables  => Naming.Decl.Attributes,
1328                                   In_Tree       => In_Tree);
1329
1330          begin
1331             if Ada_Sep_Suffix.Default then
1332                Data.Naming.Separate_Suffix :=
1333                  Data.Naming.Ada_Body_Suffix;
1334
1335             else
1336                Get_Name_String (Ada_Sep_Suffix.Value);
1337
1338                if Name_Len = 0 then
1339                   Error_Msg
1340                     (Project, In_Tree,
1341                      "Separate_Suffix cannot be empty",
1342                      Ada_Sep_Suffix.Location);
1343
1344                else
1345                   Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1346                   Data.Naming.Separate_Suffix := Name_Find;
1347                   Data.Naming.Sep_Suffix_Loc  := Ada_Sep_Suffix.Location;
1348                end if;
1349             end if;
1350          end;
1351
1352          if Current_Verbosity = High then
1353             Write_Str  ("  Separate_Suffix = """);
1354             Write_Str  (Get_Name_String (Data.Naming.Separate_Suffix));
1355             Write_Char ('"');
1356             Write_Eol;
1357          end if;
1358
1359          --  Check if Data.Naming is valid
1360
1361          Check_Ada_Naming_Scheme_Validity (Project, In_Tree, Data.Naming);
1362
1363       else
1364          Data.Naming.Ada_Spec_Suffix := Default_Ada_Spec_Suffix;
1365          Data.Naming.Ada_Body_Suffix := Default_Ada_Body_Suffix;
1366          Data.Naming.Separate_Suffix := Default_Ada_Body_Suffix;
1367       end if;
1368    end Check_Naming_Scheme;
1369
1370    ------------------------------
1371    -- Check_Library_Attributes --
1372    ------------------------------
1373
1374    procedure Check_Library_Attributes
1375      (Project : Project_Id;
1376       In_Tree : Project_Tree_Ref;
1377       Data    : in out Project_Data)
1378    is
1379       Attributes : constant Prj.Variable_Id := Data.Decl.Attributes;
1380
1381       Lib_Dir : constant Prj.Variable_Value :=
1382                   Prj.Util.Value_Of
1383                     (Snames.Name_Library_Dir, Attributes, In_Tree);
1384
1385       Lib_Name : constant Prj.Variable_Value :=
1386                    Prj.Util.Value_Of
1387                     (Snames.Name_Library_Name, Attributes, In_Tree);
1388
1389       Lib_Version : constant Prj.Variable_Value :=
1390                       Prj.Util.Value_Of
1391                         (Snames.Name_Library_Version, Attributes, In_Tree);
1392
1393       The_Lib_Kind : constant Prj.Variable_Value :=
1394                        Prj.Util.Value_Of
1395                          (Snames.Name_Library_Kind, Attributes, In_Tree);
1396
1397    begin
1398       --  Special case of extending project
1399
1400       if Data.Extends /= No_Project then
1401          declare
1402             Extended_Data : constant Project_Data :=
1403                            In_Tree.Projects.Table (Data.Extends);
1404
1405          begin
1406             --  If the project extended is a library project, we inherit
1407             --  the library name, if it is not redefined; we check that
1408             --  the library directory is specified; and we reset the
1409             --  library flag for the extended project.
1410
1411             if Extended_Data.Library then
1412                if Lib_Name.Default then
1413                   Data.Library_Name := Extended_Data.Library_Name;
1414                end if;
1415
1416                if Lib_Dir.Default then
1417                   if not Data.Virtual then
1418                      Error_Msg
1419                        (Project, In_Tree,
1420                         "a project extending a library project must " &
1421                         "specify an attribute Library_Dir",
1422                         Data.Location);
1423                   end if;
1424                end if;
1425
1426                In_Tree.Projects.Table (Data.Extends).Library :=
1427                  False;
1428             end if;
1429          end;
1430       end if;
1431
1432       pragma Assert (Lib_Dir.Kind = Single);
1433
1434       if Lib_Dir.Value = Empty_String then
1435          if Current_Verbosity = High then
1436             Write_Line ("No library directory");
1437          end if;
1438
1439       else
1440          --  Find path name, check that it is a directory
1441
1442          Locate_Directory
1443            (Lib_Dir.Value, Data.Display_Directory,
1444             Data.Library_Dir, Data.Display_Library_Dir);
1445
1446          if Data.Library_Dir = No_Name then
1447
1448             --  Get the absolute name of the library directory that
1449             --  does not exist, to report an error.
1450
1451             declare
1452                Dir_Name : constant String := Get_Name_String (Lib_Dir.Value);
1453
1454             begin
1455                if Is_Absolute_Path (Dir_Name) then
1456                   Err_Vars.Error_Msg_Name_1 := Lib_Dir.Value;
1457
1458                else
1459                   Get_Name_String (Data.Display_Directory);
1460
1461                   if Name_Buffer (Name_Len) /= Directory_Separator then
1462                      Name_Len := Name_Len + 1;
1463                      Name_Buffer (Name_Len) := Directory_Separator;
1464                   end if;
1465
1466                   Name_Buffer
1467                     (Name_Len + 1 .. Name_Len + Dir_Name'Length) :=
1468                     Dir_Name;
1469                   Name_Len := Name_Len + Dir_Name'Length;
1470                   Err_Vars.Error_Msg_Name_1 := Name_Find;
1471                end if;
1472
1473                --  Report the error
1474
1475                Error_Msg
1476                  (Project, In_Tree,
1477                   "library directory { does not exist",
1478                   Lib_Dir.Location);
1479             end;
1480
1481          --  The library directory cannot be the same as the Object directory
1482
1483          elsif Data.Library_Dir = Data.Object_Directory then
1484             Error_Msg
1485               (Project, In_Tree,
1486                "library directory cannot be the same " &
1487                "as object directory",
1488                Lib_Dir.Location);
1489             Data.Library_Dir := No_Name;
1490             Data.Display_Library_Dir := No_Name;
1491
1492          --  Display the Library directory in high verbosity
1493
1494          else
1495             if Current_Verbosity = High then
1496                Write_Str ("Library directory =""");
1497                Write_Str (Get_Name_String (Data.Display_Library_Dir));
1498                Write_Line ("""");
1499             end if;
1500          end if;
1501       end if;
1502
1503       pragma Assert (Lib_Name.Kind = Single);
1504
1505       if Lib_Name.Value = Empty_String then
1506          if Current_Verbosity = High
1507            and then Data.Library_Name = No_Name
1508          then
1509             Write_Line ("No library name");
1510          end if;
1511
1512       else
1513          --  There is no restriction on the syntax of library names
1514
1515          Data.Library_Name := Lib_Name.Value;
1516       end if;
1517
1518       if Data.Library_Name /= No_Name
1519         and then Current_Verbosity = High
1520       then
1521          Write_Str ("Library name = """);
1522          Write_Str (Get_Name_String (Data.Library_Name));
1523          Write_Line ("""");
1524       end if;
1525
1526       Data.Library :=
1527         Data.Library_Dir /= No_Name
1528         and then
1529       Data.Library_Name /= No_Name;
1530
1531       if Data.Library then
1532          if MLib.Tgt.Support_For_Libraries = MLib.Tgt.None then
1533             Error_Msg
1534               (Project, In_Tree,
1535                "?libraries are not supported on this platform",
1536                Lib_Name.Location);
1537             Data.Library := False;
1538
1539          else
1540             pragma Assert (Lib_Version.Kind = Single);
1541
1542             if Lib_Version.Value = Empty_String then
1543                if Current_Verbosity = High then
1544                   Write_Line ("No library version specified");
1545                end if;
1546
1547             else
1548                Data.Lib_Internal_Name := Lib_Version.Value;
1549             end if;
1550
1551             pragma Assert (The_Lib_Kind.Kind = Single);
1552
1553             if The_Lib_Kind.Value = Empty_String then
1554                if Current_Verbosity = High then
1555                   Write_Line ("No library kind specified");
1556                end if;
1557
1558             else
1559                Get_Name_String (The_Lib_Kind.Value);
1560
1561                declare
1562                   Kind_Name : constant String :=
1563                                 To_Lower (Name_Buffer (1 .. Name_Len));
1564
1565                   OK : Boolean := True;
1566
1567                begin
1568                   if Kind_Name = "static" then
1569                      Data.Library_Kind := Static;
1570
1571                   elsif Kind_Name = "dynamic" then
1572                      Data.Library_Kind := Dynamic;
1573
1574                   elsif Kind_Name = "relocatable" then
1575                      Data.Library_Kind := Relocatable;
1576
1577                   else
1578                      Error_Msg
1579                        (Project, In_Tree,
1580                         "illegal value for Library_Kind",
1581                         The_Lib_Kind.Location);
1582                      OK := False;
1583                   end if;
1584
1585                   if Current_Verbosity = High and then OK then
1586                      Write_Str ("Library kind = ");
1587                      Write_Line (Kind_Name);
1588                   end if;
1589
1590                   if Data.Library_Kind /= Static and then
1591                     MLib.Tgt.Support_For_Libraries = MLib.Tgt.Static_Only
1592                   then
1593                      Error_Msg
1594                        (Project, In_Tree,
1595                         "only static libraries are supported " &
1596                         "on this platform",
1597                         The_Lib_Kind.Location);
1598                      Data.Library := False;
1599                   end if;
1600                end;
1601             end if;
1602
1603             if Data.Library and then Current_Verbosity = High then
1604                Write_Line ("This is a library project file");
1605             end if;
1606
1607          end if;
1608       end if;
1609    end Check_Library_Attributes;
1610
1611    --------------------------
1612    -- Check_Package_Naming --
1613    --------------------------
1614
1615    procedure Check_Package_Naming
1616      (Project : Project_Id;
1617       In_Tree : Project_Tree_Ref;
1618       Data    : in out Project_Data)
1619    is
1620       Naming_Id : constant Package_Id :=
1621                     Util.Value_Of (Name_Naming, Data.Decl.Packages, In_Tree);
1622
1623       Naming : Package_Element;
1624
1625    begin
1626       --  If there is a package Naming, we will put in Data.Naming
1627       --  what is in this package Naming.
1628
1629       if Naming_Id /= No_Package then
1630          Naming := In_Tree.Packages.Table (Naming_Id);
1631
1632          if Current_Verbosity = High then
1633             Write_Line ("Checking ""Naming"".");
1634          end if;
1635
1636          --  Check Spec_Suffix
1637
1638          declare
1639             Spec_Suffixs : Array_Element_Id :=
1640                              Util.Value_Of
1641                                (Name_Spec_Suffix,
1642                                 Naming.Decl.Arrays,
1643                                 In_Tree);
1644
1645             Suffix  : Array_Element_Id;
1646             Element : Array_Element;
1647             Suffix2 : Array_Element_Id;
1648
1649          begin
1650             --  If some suffixs have been specified, we make sure that
1651             --  for each language for which a default suffix has been
1652             --  specified, there is a suffix specified, either the one
1653             --  in the project file or if there were none, the default.
1654
1655             if Spec_Suffixs /= No_Array_Element then
1656                Suffix := Data.Naming.Spec_Suffix;
1657
1658                while Suffix /= No_Array_Element loop
1659                   Element :=
1660                     In_Tree.Array_Elements.Table (Suffix);
1661                   Suffix2 := Spec_Suffixs;
1662
1663                   while Suffix2 /= No_Array_Element loop
1664                      exit when In_Tree.Array_Elements.Table
1665                                 (Suffix2).Index = Element.Index;
1666                      Suffix2 := In_Tree.Array_Elements.Table
1667                                  (Suffix2).Next;
1668                   end loop;
1669
1670                   --  There is a registered default suffix, but no
1671                   --  suffix specified in the project file.
1672                   --  Add the default to the array.
1673
1674                   if Suffix2 = No_Array_Element then
1675                      Array_Element_Table.Increment_Last
1676                        (In_Tree.Array_Elements);
1677                      In_Tree.Array_Elements.Table
1678                        (Array_Element_Table.Last
1679                           (In_Tree.Array_Elements)) :=
1680                        (Index                => Element.Index,
1681                         Src_Index            => Element.Src_Index,
1682                         Index_Case_Sensitive => False,
1683                         Value                => Element.Value,
1684                         Next                 => Spec_Suffixs);
1685                      Spec_Suffixs := Array_Element_Table.Last
1686                                        (In_Tree.Array_Elements);
1687                   end if;
1688
1689                   Suffix := Element.Next;
1690                end loop;
1691
1692                --  Put the resulting array as the specification suffixs
1693
1694                Data.Naming.Spec_Suffix := Spec_Suffixs;
1695             end if;
1696          end;
1697
1698          declare
1699             Current : Array_Element_Id := Data.Naming.Spec_Suffix;
1700             Element : Array_Element;
1701
1702          begin
1703             while Current /= No_Array_Element loop
1704                Element := In_Tree.Array_Elements.Table (Current);
1705                Get_Name_String (Element.Value.Value);
1706
1707                if Name_Len = 0 then
1708                   Error_Msg
1709                     (Project, In_Tree,
1710                      "Spec_Suffix cannot be empty",
1711                      Element.Value.Location);
1712                end if;
1713
1714                In_Tree.Array_Elements.Table (Current) := Element;
1715                Current := Element.Next;
1716             end loop;
1717          end;
1718
1719          --  Check Body_Suffix
1720
1721          declare
1722             Impl_Suffixs : Array_Element_Id :=
1723               Util.Value_Of
1724                 (Name_Body_Suffix,
1725                  Naming.Decl.Arrays,
1726                  In_Tree);
1727
1728             Suffix       : Array_Element_Id;
1729             Element      : Array_Element;
1730             Suffix2      : Array_Element_Id;
1731
1732          begin
1733             --  If some suffixes have been specified, we make sure that
1734             --  for each language for which a default suffix has been
1735             --  specified, there is a suffix specified, either the one
1736             --  in the project file or if there were noe, the default.
1737
1738             if Impl_Suffixs /= No_Array_Element then
1739                Suffix := Data.Naming.Body_Suffix;
1740
1741                while Suffix /= No_Array_Element loop
1742                   Element :=
1743                     In_Tree.Array_Elements.Table (Suffix);
1744                   Suffix2 := Impl_Suffixs;
1745
1746                   while Suffix2 /= No_Array_Element loop
1747                      exit when In_Tree.Array_Elements.Table
1748                                 (Suffix2).Index = Element.Index;
1749                      Suffix2 := In_Tree.Array_Elements.Table
1750                                   (Suffix2).Next;
1751                   end loop;
1752
1753                   --  There is a registered default suffix, but no suffix was
1754                   --  specified in the project file. Add the default to the
1755                   --  array.
1756
1757                   if Suffix2 = No_Array_Element then
1758                      Array_Element_Table.Increment_Last
1759                        (In_Tree.Array_Elements);
1760                      In_Tree.Array_Elements.Table
1761                        (Array_Element_Table.Last
1762                           (In_Tree.Array_Elements)) :=
1763                        (Index                => Element.Index,
1764                         Src_Index            => Element.Src_Index,
1765                         Index_Case_Sensitive => False,
1766                         Value                => Element.Value,
1767                         Next                 => Impl_Suffixs);
1768                      Impl_Suffixs := Array_Element_Table.Last
1769                                        (In_Tree.Array_Elements);
1770                   end if;
1771
1772                   Suffix := Element.Next;
1773                end loop;
1774
1775                --  Put the resulting array as the implementation suffixs
1776
1777                Data.Naming.Body_Suffix := Impl_Suffixs;
1778             end if;
1779          end;
1780
1781          declare
1782             Current : Array_Element_Id := Data.Naming.Body_Suffix;
1783             Element : Array_Element;
1784
1785          begin
1786             while Current /= No_Array_Element loop
1787                Element := In_Tree.Array_Elements.Table (Current);
1788                Get_Name_String (Element.Value.Value);
1789
1790                if Name_Len = 0 then
1791                   Error_Msg
1792                     (Project, In_Tree,
1793                      "Body_Suffix cannot be empty",
1794                      Element.Value.Location);
1795                end if;
1796
1797                In_Tree.Array_Elements.Table (Current) := Element;
1798                Current := Element.Next;
1799             end loop;
1800          end;
1801
1802          --  Get the exceptions, if any
1803
1804          Data.Naming.Specification_Exceptions :=
1805            Util.Value_Of
1806              (Name_Specification_Exceptions,
1807               In_Arrays => Naming.Decl.Arrays,
1808               In_Tree   => In_Tree);
1809
1810          Data.Naming.Implementation_Exceptions :=
1811            Util.Value_Of
1812              (Name_Implementation_Exceptions,
1813               In_Arrays => Naming.Decl.Arrays,
1814               In_Tree   => In_Tree);
1815       end if;
1816    end Check_Package_Naming;
1817
1818    ---------------------------------
1819    -- Check_Programming_Languages --
1820    ---------------------------------
1821
1822    procedure Check_Programming_Languages
1823      (In_Tree : Project_Tree_Ref;
1824       Data    : in out Project_Data)
1825    is
1826       Languages : Variable_Value := Nil_Variable_Value;
1827
1828    begin
1829       Languages :=
1830         Prj.Util.Value_Of (Name_Languages, Data.Decl.Attributes, In_Tree);
1831       Data.Ada_Sources_Present   := Data.Source_Dirs /= Nil_String;
1832       Data.Other_Sources_Present := Data.Source_Dirs /= Nil_String;
1833
1834       if Data.Source_Dirs /= Nil_String then
1835
1836          --  Check if languages are specified in this project
1837
1838          if Languages.Default then
1839
1840             --  Attribute Languages is not specified. So, it defaults to
1841             --  a project of language Ada only.
1842
1843             Data.Languages (Ada_Language_Index) := True;
1844
1845             --  No sources of languages other than Ada
1846
1847             Data.Other_Sources_Present := False;
1848
1849          else
1850             declare
1851                Current   : String_List_Id := Languages.Values;
1852                Element   : String_Element;
1853                Lang_Name : Name_Id;
1854                Index     : Language_Index;
1855
1856             begin
1857                --  Assume that there is no language specified yet
1858
1859                Data.Other_Sources_Present := False;
1860                Data.Ada_Sources_Present   := False;
1861
1862                --  Look through all the languages specified in attribute
1863                --  Languages, if any
1864
1865                while Current /= Nil_String loop
1866                   Element :=
1867                     In_Tree.String_Elements.Table (Current);
1868                   Get_Name_String (Element.Value);
1869                   To_Lower (Name_Buffer (1 .. Name_Len));
1870                   Lang_Name := Name_Find;
1871                   Index := Language_Indexes.Get (Lang_Name);
1872
1873                   if Index = No_Language_Index then
1874                      Add_Language_Name (Lang_Name);
1875                      Index := Last_Language_Index;
1876                   end if;
1877
1878                   Set (Index, True, Data, In_Tree);
1879                   Set (Language_Processing => Default_Language_Processing_Data,
1880                        For_Language        => Index,
1881                        In_Project          => Data,
1882                        In_Tree             => In_Tree);
1883
1884                   if Index = Ada_Language_Index then
1885                      Data.Ada_Sources_Present := True;
1886
1887                   else
1888                      Data.Other_Sources_Present := True;
1889                   end if;
1890
1891                   Current := Element.Next;
1892                end loop;
1893             end;
1894          end if;
1895       end if;
1896    end Check_Programming_Languages;
1897
1898    -------------------
1899    -- Check_Project --
1900    -------------------
1901
1902    function Check_Project
1903      (P            : Project_Id;
1904       Root_Project : Project_Id;
1905       In_Tree      : Project_Tree_Ref;
1906       Extending    : Boolean) return Boolean
1907    is
1908    begin
1909       if P = Root_Project then
1910          return True;
1911
1912       elsif Extending then
1913          declare
1914             Data : Project_Data := In_Tree.Projects.Table (Root_Project);
1915
1916          begin
1917             while Data.Extends /= No_Project loop
1918                if P = Data.Extends then
1919                   return True;
1920                end if;
1921
1922                Data := In_Tree.Projects.Table (Data.Extends);
1923             end loop;
1924          end;
1925       end if;
1926
1927       return False;
1928    end Check_Project;
1929
1930    -------------------------------
1931    -- Check_Stand_Alone_Library --
1932    -------------------------------
1933
1934    procedure Check_Stand_Alone_Library
1935      (Project   : Project_Id;
1936       In_Tree   : Project_Tree_Ref;
1937       Data      : in out Project_Data;
1938       Extending : Boolean)
1939    is
1940       Lib_Interfaces      : constant Prj.Variable_Value :=
1941                               Prj.Util.Value_Of
1942                                 (Snames.Name_Library_Interface,
1943                                  Data.Decl.Attributes,
1944                                  In_Tree);
1945
1946       Lib_Auto_Init       : constant Prj.Variable_Value :=
1947                               Prj.Util.Value_Of
1948                                 (Snames.Name_Library_Auto_Init,
1949                                  Data.Decl.Attributes,
1950                                  In_Tree);
1951
1952       Lib_Src_Dir         : constant Prj.Variable_Value :=
1953                               Prj.Util.Value_Of
1954                                 (Snames.Name_Library_Src_Dir,
1955                                  Data.Decl.Attributes,
1956                                  In_Tree);
1957
1958       Lib_Symbol_File     : constant Prj.Variable_Value :=
1959                               Prj.Util.Value_Of
1960                                 (Snames.Name_Library_Symbol_File,
1961                                  Data.Decl.Attributes,
1962                                  In_Tree);
1963
1964       Lib_Symbol_Policy   : constant Prj.Variable_Value :=
1965                               Prj.Util.Value_Of
1966                                 (Snames.Name_Library_Symbol_Policy,
1967                                  Data.Decl.Attributes,
1968                                  In_Tree);
1969
1970       Lib_Ref_Symbol_File : constant Prj.Variable_Value :=
1971                               Prj.Util.Value_Of
1972                                 (Snames.Name_Library_Reference_Symbol_File,
1973                                  Data.Decl.Attributes,
1974                                  In_Tree);
1975
1976       Auto_Init_Supported : constant Boolean :=
1977                               MLib.Tgt.
1978                                 Standalone_Library_Auto_Init_Is_Supported;
1979
1980       OK : Boolean := True;
1981
1982    begin
1983       pragma Assert (Lib_Interfaces.Kind = List);
1984
1985       --  It is a stand-alone library project file if attribute
1986       --  Library_Interface is defined.
1987
1988       if not Lib_Interfaces.Default then
1989          SAL_Library : declare
1990             Interfaces     : String_List_Id := Lib_Interfaces.Values;
1991             Interface_ALIs : String_List_Id := Nil_String;
1992             Unit           : Name_Id;
1993             The_Unit_Id    : Unit_Id;
1994             The_Unit_Data  : Unit_Data;
1995
1996             procedure Add_ALI_For (Source : Name_Id);
1997             --  Add an ALI file name to the list of Interface ALIs
1998
1999             -----------------
2000             -- Add_ALI_For --
2001             -----------------
2002
2003             procedure Add_ALI_For (Source : Name_Id) is
2004             begin
2005                Get_Name_String (Source);
2006
2007                declare
2008                   ALI         : constant String :=
2009                     ALI_File_Name (Name_Buffer (1 .. Name_Len));
2010                   ALI_Name_Id : Name_Id;
2011                begin
2012                   Name_Len := ALI'Length;
2013                   Name_Buffer (1 .. Name_Len) := ALI;
2014                   ALI_Name_Id := Name_Find;
2015
2016                   String_Element_Table.Increment_Last
2017                     (In_Tree.String_Elements);
2018                   In_Tree.String_Elements.Table
2019                     (String_Element_Table.Last
2020                       (In_Tree.String_Elements)) :=
2021                     (Value         => ALI_Name_Id,
2022                      Index         => 0,
2023                      Display_Value => ALI_Name_Id,
2024                      Location      =>
2025                        In_Tree.String_Elements.Table
2026                          (Interfaces).Location,
2027                      Flag          => False,
2028                      Next          => Interface_ALIs);
2029                   Interface_ALIs := String_Element_Table.Last
2030                                       (In_Tree.String_Elements);
2031                end;
2032             end Add_ALI_For;
2033
2034          --  Start of processing for SAL_Library
2035
2036          begin
2037             Data.Standalone_Library := True;
2038
2039             --  Library_Interface cannot be an empty list
2040
2041             if Interfaces = Nil_String then
2042                Error_Msg
2043                  (Project, In_Tree,
2044                   "Library_Interface cannot be an empty list",
2045                   Lib_Interfaces.Location);
2046             end if;
2047
2048             --  Process each unit name specified in the attribute
2049             --  Library_Interface.
2050
2051             while Interfaces /= Nil_String loop
2052                Get_Name_String
2053                  (In_Tree.String_Elements.Table
2054                                                      (Interfaces).Value);
2055                To_Lower (Name_Buffer (1 .. Name_Len));
2056
2057                if Name_Len = 0 then
2058                   Error_Msg
2059                     (Project, In_Tree,
2060                      "an interface cannot be an empty string",
2061                      In_Tree.String_Elements.Table
2062                                                    (Interfaces).Location);
2063
2064                else
2065                   Unit := Name_Find;
2066                   Error_Msg_Name_1 := Unit;
2067                   The_Unit_Id :=
2068                     Units_Htable.Get (In_Tree.Units_HT, Unit);
2069
2070                   if The_Unit_Id = No_Unit then
2071                      Error_Msg
2072                        (Project, In_Tree,
2073                         "unknown unit {",
2074                         In_Tree.String_Elements.Table
2075                           (Interfaces).Location);
2076
2077                   else
2078                      --  Check that the unit is part of the project
2079
2080                      The_Unit_Data :=
2081                        In_Tree.Units.Table (The_Unit_Id);
2082
2083                      if The_Unit_Data.File_Names (Body_Part).Name /= No_Name
2084                        and then The_Unit_Data.File_Names (Body_Part).Path /=
2085                                                                         Slash
2086                      then
2087                         if Check_Project
2088                           (The_Unit_Data.File_Names (Body_Part).Project,
2089                            Project, In_Tree, Extending)
2090                         then
2091                            --  There is a body for this unit.
2092                            --  If there is no spec, we need to check
2093                            --  that it is not a subunit.
2094
2095                            if The_Unit_Data.File_Names
2096                              (Specification).Name = No_Name
2097                            then
2098                               declare
2099                                  Src_Ind : Source_File_Index;
2100
2101                               begin
2102                                  Src_Ind := Sinput.P.Load_Project_File
2103                                    (Get_Name_String
2104                                       (The_Unit_Data.File_Names
2105                                          (Body_Part).Path));
2106
2107                                  if Sinput.P.Source_File_Is_Subunit
2108                                    (Src_Ind)
2109                                  then
2110                                     Error_Msg
2111                                       (Project, In_Tree,
2112                                        "{ is a subunit; " &
2113                                        "it cannot be an interface",
2114                                        In_Tree.
2115                                          String_Elements.Table
2116                                            (Interfaces).Location);
2117                                  end if;
2118                               end;
2119                            end if;
2120
2121                            --  The unit is not a subunit, so we add
2122                            --  to the Interface ALIs the ALI file
2123                            --  corresponding to the body.
2124
2125                            Add_ALI_For
2126                              (The_Unit_Data.File_Names (Body_Part).Name);
2127
2128                         else
2129                            Error_Msg
2130                              (Project, In_Tree,
2131                               "{ is not an unit of this project",
2132                               In_Tree.String_Elements.Table
2133                                 (Interfaces).Location);
2134                         end if;
2135
2136                      elsif The_Unit_Data.File_Names
2137                        (Specification).Name /= No_Name
2138                        and then The_Unit_Data.File_Names
2139                          (Specification).Path /= Slash
2140                        and then Check_Project
2141                          (The_Unit_Data.File_Names
2142                               (Specification).Project,
2143                           Project, In_Tree, Extending)
2144
2145                      then
2146                         --  The unit is part of the project, it has
2147                         --  a spec, but no body. We add to the Interface
2148                         --  ALIs the ALI file corresponding to the spec.
2149
2150                         Add_ALI_For
2151                           (The_Unit_Data.File_Names (Specification).Name);
2152
2153                      else
2154                         Error_Msg
2155                           (Project, In_Tree,
2156                            "{ is not an unit of this project",
2157                            In_Tree.String_Elements.Table
2158                                                     (Interfaces).Location);
2159                      end if;
2160                   end if;
2161
2162                end if;
2163
2164                Interfaces :=
2165                  In_Tree.String_Elements.Table (Interfaces).Next;
2166             end loop;
2167
2168             --  Put the list of Interface ALIs in the project data
2169
2170             Data.Lib_Interface_ALIs := Interface_ALIs;
2171
2172             --  Check value of attribute Library_Auto_Init and set
2173             --  Lib_Auto_Init accordingly.
2174
2175             if Lib_Auto_Init.Default then
2176
2177                --  If no attribute Library_Auto_Init is declared, then
2178                --  set auto init only if it is supported.
2179
2180                Data.Lib_Auto_Init := Auto_Init_Supported;
2181
2182             else
2183                Get_Name_String (Lib_Auto_Init.Value);
2184                To_Lower (Name_Buffer (1 .. Name_Len));
2185
2186                if Name_Buffer (1 .. Name_Len) = "false" then
2187                   Data.Lib_Auto_Init := False;
2188
2189                elsif Name_Buffer (1 .. Name_Len) = "true" then
2190                   if Auto_Init_Supported then
2191                      Data.Lib_Auto_Init := True;
2192
2193                   else
2194                      --  Library_Auto_Init cannot be "true" if auto init
2195                      --  is not supported
2196
2197                      Error_Msg
2198                        (Project, In_Tree,
2199                         "library auto init not supported " &
2200                         "on this platform",
2201                         Lib_Auto_Init.Location);
2202                   end if;
2203
2204                else
2205                   Error_Msg
2206                     (Project, In_Tree,
2207                      "invalid value for attribute Library_Auto_Init",
2208                      Lib_Auto_Init.Location);
2209                end if;
2210             end if;
2211          end SAL_Library;
2212
2213          --  If attribute Library_Src_Dir is defined and not the
2214          --  empty string, check if the directory exist and is not
2215          --  the object directory or one of the source directories.
2216          --  This is the directory where copies of the interface
2217          --  sources will be copied. Note that this directory may be
2218          --  the library directory.
2219
2220          if Lib_Src_Dir.Value /= Empty_String then
2221             declare
2222                Dir_Id : constant Name_Id := Lib_Src_Dir.Value;
2223
2224             begin
2225                Locate_Directory
2226                  (Dir_Id, Data.Display_Directory,
2227                   Data.Library_Src_Dir,
2228                   Data.Display_Library_Src_Dir);
2229
2230                --  If directory does not exist, report an error
2231
2232                if Data.Library_Src_Dir = No_Name then
2233
2234                   --  Get the absolute name of the library directory
2235                   --  that does not exist, to report an error.
2236
2237                   declare
2238                      Dir_Name : constant String :=
2239                        Get_Name_String (Dir_Id);
2240
2241                   begin
2242                      if Is_Absolute_Path (Dir_Name) then
2243                         Err_Vars.Error_Msg_Name_1 := Dir_Id;
2244
2245                      else
2246                         Get_Name_String (Data.Directory);
2247
2248                         if Name_Buffer (Name_Len) /=
2249                           Directory_Separator
2250                         then
2251                            Name_Len := Name_Len + 1;
2252                            Name_Buffer (Name_Len) :=
2253                              Directory_Separator;
2254                         end if;
2255
2256                         Name_Buffer
2257                           (Name_Len + 1 ..
2258                              Name_Len + Dir_Name'Length) :=
2259                             Dir_Name;
2260                         Name_Len := Name_Len + Dir_Name'Length;
2261                         Err_Vars.Error_Msg_Name_1 := Name_Find;
2262                      end if;
2263
2264                      --  Report the error
2265
2266                      Error_Msg
2267                        (Project, In_Tree,
2268                         "Directory { does not exist",
2269                         Lib_Src_Dir.Location);
2270                   end;
2271
2272                   --  Report an error if it is the same as the object
2273                   --  directory.
2274
2275                elsif Data.Library_Src_Dir = Data.Object_Directory then
2276                   Error_Msg
2277                     (Project, In_Tree,
2278                      "directory to copy interfaces cannot be " &
2279                      "the object directory",
2280                      Lib_Src_Dir.Location);
2281                   Data.Library_Src_Dir := No_Name;
2282
2283                   --  Check if it is same as one of the source directories
2284
2285                else
2286                   declare
2287                      Src_Dirs : String_List_Id := Data.Source_Dirs;
2288                      Src_Dir  : String_Element;
2289
2290                   begin
2291                      while Src_Dirs /= Nil_String loop
2292                         Src_Dir := In_Tree.String_Elements.Table
2293                                                           (Src_Dirs);
2294                         Src_Dirs := Src_Dir.Next;
2295
2296                         --  Report error if it is one of the source directories
2297
2298                         if Data.Library_Src_Dir = Src_Dir.Value then
2299                            Error_Msg
2300                              (Project, In_Tree,
2301                               "directory to copy interfaces cannot " &
2302                               "be one of the source directories",
2303                               Lib_Src_Dir.Location);
2304                            Data.Library_Src_Dir := No_Name;
2305                            exit;
2306                         end if;
2307                      end loop;
2308                   end;
2309
2310                   --  In high verbosity, if there is a valid Library_Src_Dir,
2311                   --  display its path name.
2312
2313                   if Data.Library_Src_Dir /= No_Name
2314                     and then Current_Verbosity = High
2315                   then
2316                      Write_Str ("Directory to copy interfaces =""");
2317                      Write_Str (Get_Name_String (Data.Library_Src_Dir));
2318                      Write_Line ("""");
2319                   end if;
2320                end if;
2321             end;
2322          end if;
2323
2324          --  Check the symbol related attributes
2325
2326          --  First, the symbol policy
2327
2328          if not Lib_Symbol_Policy.Default then
2329             declare
2330                Value : constant String :=
2331                  To_Lower
2332                    (Get_Name_String (Lib_Symbol_Policy.Value));
2333
2334             begin
2335                --  Symbol policy must hove one of a limited number of values
2336
2337                if Value = "autonomous" or else Value = "default" then
2338                   Data.Symbol_Data.Symbol_Policy := Autonomous;
2339
2340                elsif Value = "compliant" then
2341                   Data.Symbol_Data.Symbol_Policy := Compliant;
2342
2343                elsif Value = "controlled" then
2344                   Data.Symbol_Data.Symbol_Policy := Controlled;
2345
2346                elsif Value = "restricted" then
2347                   Data.Symbol_Data.Symbol_Policy := Restricted;
2348
2349                else
2350                   Error_Msg
2351                     (Project, In_Tree,
2352                      "illegal value for Library_Symbol_Policy",
2353                      Lib_Symbol_Policy.Location);
2354                end if;
2355             end;
2356          end if;
2357
2358          --  If attribute Library_Symbol_File is not specified, symbol policy
2359          --  cannot be Restricted.
2360
2361          if Lib_Symbol_File.Default then
2362             if Data.Symbol_Data.Symbol_Policy = Restricted then
2363                Error_Msg
2364                  (Project, In_Tree,
2365                   "Library_Symbol_File needs to be defined when " &
2366                   "symbol policy is Restricted",
2367                   Lib_Symbol_Policy.Location);
2368             end if;
2369
2370          else
2371             --  Library_Symbol_File is defined. Check that the file exists.
2372
2373             Data.Symbol_Data.Symbol_File := Lib_Symbol_File.Value;
2374
2375             Get_Name_String (Lib_Symbol_File.Value);
2376
2377             if Name_Len = 0 then
2378                Error_Msg
2379                  (Project, In_Tree,
2380                   "symbol file name cannot be an empty string",
2381                   Lib_Symbol_File.Location);
2382
2383             else
2384                OK := not Is_Absolute_Path (Name_Buffer (1 .. Name_Len));
2385
2386                if OK then
2387                   for J in 1 .. Name_Len loop
2388                      if Name_Buffer (J) = '/'
2389                        or else Name_Buffer (J) = Directory_Separator
2390                      then
2391                         OK := False;
2392                         exit;
2393                      end if;
2394                   end loop;
2395                end if;
2396
2397                if not OK then
2398                   Error_Msg_Name_1 := Lib_Symbol_File.Value;
2399                   Error_Msg
2400                     (Project, In_Tree,
2401                      "symbol file name { is illegal. " &
2402                      "Name canot include directory info.",
2403                      Lib_Symbol_File.Location);
2404                end if;
2405             end if;
2406          end if;
2407
2408          --  If attribute Library_Reference_Symbol_File is not defined,
2409          --  symbol policy cannot be Compilant or Controlled.
2410
2411          if Lib_Ref_Symbol_File.Default then
2412             if Data.Symbol_Data.Symbol_Policy = Compliant
2413               or else Data.Symbol_Data.Symbol_Policy = Controlled
2414             then
2415                Error_Msg
2416                  (Project, In_Tree,
2417                   "a reference symbol file need to be defined",
2418                   Lib_Symbol_Policy.Location);
2419             end if;
2420
2421          else
2422             --  Library_Reference_Symbol_File is defined, check file exists
2423
2424             Data.Symbol_Data.Reference := Lib_Ref_Symbol_File.Value;
2425
2426             Get_Name_String (Lib_Ref_Symbol_File.Value);
2427
2428             if Name_Len = 0 then
2429                Error_Msg
2430                  (Project, In_Tree,
2431                   "reference symbol file name cannot be an empty string",
2432                   Lib_Symbol_File.Location);
2433
2434             else
2435                OK := not Is_Absolute_Path (Name_Buffer (1 .. Name_Len));
2436
2437                if OK then
2438                   for J in 1 .. Name_Len loop
2439                      if Name_Buffer (J) = '/'
2440                        or else Name_Buffer (J) = Directory_Separator
2441                      then
2442                         OK := False;
2443                         exit;
2444                      end if;
2445                   end loop;
2446                end if;
2447
2448                if not OK then
2449                   Error_Msg_Name_1 := Lib_Ref_Symbol_File.Value;
2450                   Error_Msg
2451                     (Project, In_Tree,
2452                      "reference symbol file { name is illegal. " &
2453                      "Name canot include directory info.",
2454                      Lib_Ref_Symbol_File.Location);
2455                end if;
2456
2457                if not Is_Regular_File
2458                  (Get_Name_String (Data.Object_Directory) &
2459                   Directory_Separator &
2460                   Get_Name_String (Lib_Ref_Symbol_File.Value))
2461                then
2462                   Error_Msg_Name_1 := Lib_Ref_Symbol_File.Value;
2463
2464                   --  For controlled symbol policy, it is an error
2465                   --  if the reference symbol file does not exist.
2466
2467                   if Data.Symbol_Data.Symbol_Policy = Controlled then
2468                      Error_Msg
2469                        (Project, In_Tree,
2470                         "library reference symbol file { does not exist",
2471                         Lib_Ref_Symbol_File.Location);
2472
2473                   else
2474                      --  For other symbol policies, this is just a warning
2475
2476                      Error_Msg
2477                        (Project, In_Tree,
2478                         "?library reference symbol file { does not exist",
2479                         Lib_Ref_Symbol_File.Location);
2480
2481                      --  In addition, if symbol policy is Compliant, it is
2482                      --  changed to Autonomous, because there is no reference
2483                      --  to check against, and we don't want to fail in this
2484                      --  case.
2485
2486                      if Data.Symbol_Data.Symbol_Policy = Compliant then
2487                         Data.Symbol_Data.Symbol_Policy := Autonomous;
2488                      end if;
2489                   end if;
2490                end if;
2491
2492             end if;
2493          end if;
2494       end if;
2495    end Check_Stand_Alone_Library;
2496
2497    ----------------------------
2498    -- Compute_Directory_Last --
2499    ----------------------------
2500
2501    function Compute_Directory_Last (Dir : String) return Natural is
2502    begin
2503       if Dir'Length > 1
2504         and then (Dir (Dir'Last - 1) = Directory_Separator
2505                   or else Dir (Dir'Last - 1) = '/')
2506       then
2507          return Dir'Last - 1;
2508       else
2509          return Dir'Last;
2510       end if;
2511    end Compute_Directory_Last;
2512
2513    --------------------
2514    -- Body_Suffix_Of --
2515    --------------------
2516
2517    function Body_Suffix_Of
2518      (Language   : Language_Index;
2519       In_Project : Project_Data;
2520       In_Tree    : Project_Tree_Ref) return String
2521    is
2522       Suffix_Id : constant Name_Id :=
2523                     Suffix_Of (Language, In_Project, In_Tree);
2524    begin
2525       if Suffix_Id /= No_Name then
2526          return Get_Name_String (Suffix_Id);
2527       else
2528          return "." & Get_Name_String (Language_Names.Table (Language));
2529       end if;
2530    end Body_Suffix_Of;
2531
2532    ---------------
2533    -- Error_Msg --
2534    ---------------
2535
2536    procedure Error_Msg
2537      (Project       : Project_Id;
2538       In_Tree       : Project_Tree_Ref;
2539       Msg           : String;
2540       Flag_Location : Source_Ptr)
2541    is
2542       Error_Buffer : String (1 .. 5_000);
2543       Error_Last   : Natural := 0;
2544       Msg_Name     : Natural := 0;
2545       First        : Positive := Msg'First;
2546
2547       procedure Add (C : Character);
2548       --  Add a character to the buffer
2549
2550       procedure Add (S : String);
2551       --  Add a string to the buffer
2552
2553       procedure Add (Id : Name_Id);
2554       --  Add a name to the buffer
2555
2556       ---------
2557       -- Add --
2558       ---------
2559
2560       procedure Add (C : Character) is
2561       begin
2562          Error_Last := Error_Last + 1;
2563          Error_Buffer (Error_Last) := C;
2564       end Add;
2565
2566       procedure Add (S : String) is
2567       begin
2568          Error_Buffer (Error_Last + 1 .. Error_Last + S'Length) := S;
2569          Error_Last := Error_Last + S'Length;
2570       end Add;
2571
2572       procedure Add (Id : Name_Id) is
2573       begin
2574          Get_Name_String (Id);
2575          Add (Name_Buffer (1 .. Name_Len));
2576       end Add;
2577
2578    --  Start of processing for Error_Msg
2579
2580    begin
2581       if Error_Report = null then
2582          Prj.Err.Error_Msg (Msg, Flag_Location);
2583          return;
2584       end if;
2585
2586       --  Ignore continuation character
2587
2588       if Msg (First) = '\' then
2589          First := First + 1;
2590
2591       --  Warniung character is always the first one in this package
2592
2593       elsif Msg (First) = '?' then
2594          First := First + 1;
2595          Add ("Warning: ");
2596       end if;
2597
2598       for Index in First .. Msg'Last loop
2599          if Msg (Index) = '{' or else Msg (Index) = '%' then
2600
2601             --  Include a name between double quotes
2602
2603             Msg_Name := Msg_Name + 1;
2604             Add ('"');
2605
2606             case Msg_Name is
2607                when 1 => Add (Err_Vars.Error_Msg_Name_1);
2608                when 2 => Add (Err_Vars.Error_Msg_Name_2);
2609                when 3 => Add (Err_Vars.Error_Msg_Name_3);
2610
2611                when others => null;
2612             end case;
2613
2614             Add ('"');
2615
2616          else
2617             Add (Msg (Index));
2618          end if;
2619
2620       end loop;
2621
2622       Error_Report (Error_Buffer (1 .. Error_Last), Project, In_Tree);
2623    end Error_Msg;
2624
2625    ------------------
2626    -- Find_Sources --
2627    ------------------
2628
2629    procedure Find_Sources
2630      (Project      : Project_Id;
2631       In_Tree      : Project_Tree_Ref;
2632       Data         : in out Project_Data;
2633       For_Language : Language_Index;
2634       Follow_Links : Boolean := False)
2635    is
2636       Source_Dir      : String_List_Id := Data.Source_Dirs;
2637       Element         : String_Element;
2638       Dir             : Dir_Type;
2639       Current_Source  : String_List_Id := Nil_String;
2640       Source_Recorded : Boolean := False;
2641
2642    begin
2643       if Current_Verbosity = High then
2644          Write_Line ("Looking for sources:");
2645       end if;
2646
2647       --  For each subdirectory
2648
2649       while Source_Dir /= Nil_String loop
2650          begin
2651             Source_Recorded := False;
2652             Element := In_Tree.String_Elements.Table (Source_Dir);
2653             if Element.Value /= No_Name then
2654                Get_Name_String (Element.Display_Value);
2655
2656                declare
2657                   Source_Directory : constant String :=
2658                     Name_Buffer (1 .. Name_Len) & Directory_Separator;
2659                   Dir_Last  : constant Natural :=
2660                      Compute_Directory_Last (Source_Directory);
2661
2662                begin
2663                   if Current_Verbosity = High then
2664                      Write_Str ("Source_Dir = ");
2665                      Write_Line (Source_Directory);
2666                   end if;
2667
2668                   --  We look to every entry in the source directory
2669
2670                   Open (Dir, Source_Directory
2671                                (Source_Directory'First .. Dir_Last));
2672
2673                   loop
2674                      Read (Dir, Name_Buffer, Name_Len);
2675
2676                      if Current_Verbosity = High then
2677                         Write_Str  ("   Checking ");
2678                         Write_Line (Name_Buffer (1 .. Name_Len));
2679                      end if;
2680
2681                      exit when Name_Len = 0;
2682
2683                      declare
2684                         File_Name : constant Name_Id := Name_Find;
2685                         Path      : constant String :=
2686                           Normalize_Pathname
2687                             (Name      => Name_Buffer (1 .. Name_Len),
2688                              Directory => Source_Directory
2689                                (Source_Directory'First .. Dir_Last),
2690                              Resolve_Links => Follow_Links,
2691                              Case_Sensitive => True);
2692                         Path_Name : Name_Id;
2693
2694                      begin
2695                         Name_Len := Path'Length;
2696                         Name_Buffer (1 .. Name_Len) := Path;
2697                         Path_Name := Name_Find;
2698
2699                         if For_Language = Ada_Language_Index then
2700
2701                            --  We attempt to register it as a source. However,
2702                            --  there is no error if the file does not contain
2703                            --  a valid source. But there is an error if we have
2704                            --  a duplicate unit name.
2705
2706                            Record_Ada_Source
2707                              (File_Name       => File_Name,
2708                               Path_Name       => Path_Name,
2709                               Project         => Project,
2710                               In_Tree         => In_Tree,
2711                               Data            => Data,
2712                               Location        => No_Location,
2713                               Current_Source  => Current_Source,
2714                               Source_Recorded => Source_Recorded,
2715                               Follow_Links    => Follow_Links);
2716
2717                         else
2718                            Check_For_Source
2719                              (File_Name        => File_Name,
2720                               Path_Name        => Path_Name,
2721                               Project          => Project,
2722                               In_Tree          => In_Tree,
2723                               Data             => Data,
2724                               Location         => No_Location,
2725                               Language         => For_Language,
2726                               Suffix           =>
2727                                 Body_Suffix_Of (For_Language, Data, In_Tree),
2728                               Naming_Exception => False);
2729                         end if;
2730                      end;
2731                   end loop;
2732
2733                   Close (Dir);
2734                end;
2735             end if;
2736
2737          exception
2738             when Directory_Error =>
2739                null;
2740          end;
2741
2742          if Source_Recorded then
2743             In_Tree.String_Elements.Table (Source_Dir).Flag :=
2744               True;
2745          end if;
2746
2747          Source_Dir := Element.Next;
2748       end loop;
2749
2750       if Current_Verbosity = High then
2751          Write_Line ("end Looking for sources.");
2752       end if;
2753
2754       if For_Language = Ada_Language_Index then
2755
2756          --  If we have looked for sources and found none, then
2757          --  it is an error, except if it is an extending project.
2758          --  If a non extending project is not supposed to contain
2759          --  any source, then we never call Find_Sources.
2760
2761          if Current_Source /= Nil_String then
2762             Data.Ada_Sources_Present := True;
2763
2764          elsif Data.Extends = No_Project then
2765             Error_Msg
2766               (Project, In_Tree,
2767                "there are no Ada sources in this project",
2768                Data.Location);
2769          end if;
2770       end if;
2771    end Find_Sources;
2772
2773    --------------------------------
2774    -- Free_Ada_Naming_Exceptions --
2775    --------------------------------
2776
2777    procedure Free_Ada_Naming_Exceptions is
2778    begin
2779       Ada_Naming_Exception_Table.Set_Last (0);
2780       Ada_Naming_Exceptions.Reset;
2781       Reverse_Ada_Naming_Exceptions.Reset;
2782    end Free_Ada_Naming_Exceptions;
2783
2784    ---------------------
2785    -- Get_Directories --
2786    ---------------------
2787
2788    procedure Get_Directories
2789      (Project : Project_Id;
2790       In_Tree : Project_Tree_Ref;
2791       Data    : in out Project_Data)
2792    is
2793       Object_Dir : constant Variable_Value :=
2794                      Util.Value_Of
2795                        (Name_Object_Dir, Data.Decl.Attributes, In_Tree);
2796
2797       Exec_Dir   : constant Variable_Value :=
2798                      Util.Value_Of
2799                        (Name_Exec_Dir, Data.Decl.Attributes, In_Tree);
2800
2801       Source_Dirs : constant Variable_Value :=
2802                       Util.Value_Of
2803                         (Name_Source_Dirs, Data.Decl.Attributes, In_Tree);
2804
2805       Last_Source_Dir : String_List_Id  := Nil_String;
2806
2807       procedure Find_Source_Dirs (From : Name_Id; Location : Source_Ptr);
2808       --  Find one or several source directories, and add them
2809       --  to the list of source directories of the project.
2810
2811       ----------------------
2812       -- Find_Source_Dirs --
2813       ----------------------
2814
2815       procedure Find_Source_Dirs (From : Name_Id; Location : Source_Ptr) is
2816          Directory : constant String := Get_Name_String (From);
2817          Element   : String_Element;
2818
2819          procedure Recursive_Find_Dirs (Path : Name_Id);
2820          --  Find all the subdirectories (recursively) of Path and add them
2821          --  to the list of source directories of the project.
2822
2823          -------------------------
2824          -- Recursive_Find_Dirs --
2825          -------------------------
2826
2827          procedure Recursive_Find_Dirs (Path : Name_Id) is
2828             Dir      : Dir_Type;
2829             Name     : String (1 .. 250);
2830             Last     : Natural;
2831             List     : String_List_Id := Data.Source_Dirs;
2832             Element  : String_Element;
2833             Found    : Boolean := False;
2834
2835             Non_Canonical_Path : Name_Id := No_Name;
2836             Canonical_Path     : Name_Id := No_Name;
2837
2838             The_Path : constant String :=
2839                          Normalize_Pathname (Get_Name_String (Path)) &
2840                          Directory_Separator;
2841
2842             The_Path_Last : constant Natural :=
2843                               Compute_Directory_Last (The_Path);
2844
2845          begin
2846             Name_Len := The_Path_Last - The_Path'First + 1;
2847             Name_Buffer (1 .. Name_Len) :=
2848               The_Path (The_Path'First .. The_Path_Last);
2849             Non_Canonical_Path := Name_Find;
2850             Get_Name_String (Non_Canonical_Path);
2851             Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2852             Canonical_Path := Name_Find;
2853
2854             --  To avoid processing the same directory several times, check
2855             --  if the directory is already in Recursive_Dirs. If it is,
2856             --  then there is nothing to do, just return. If it is not, put
2857             --  it there and continue recursive processing.
2858
2859             if Recursive_Dirs.Get (Canonical_Path) then
2860                return;
2861
2862             else
2863                Recursive_Dirs.Set (Canonical_Path, True);
2864             end if;
2865
2866             --  Check if directory is already in list
2867
2868             while List /= Nil_String loop
2869                Element := In_Tree.String_Elements.Table (List);
2870
2871                if Element.Value /= No_Name then
2872                   Found := Element.Value = Canonical_Path;
2873                   exit when Found;
2874                end if;
2875
2876                List := Element.Next;
2877             end loop;
2878
2879             --  If directory is not already in list, put it there
2880
2881             if not Found then
2882                if Current_Verbosity = High then
2883                   Write_Str  ("   ");
2884                   Write_Line (The_Path (The_Path'First .. The_Path_Last));
2885                end if;
2886
2887                String_Element_Table.Increment_Last
2888                  (In_Tree.String_Elements);
2889                Element :=
2890                  (Value    => Canonical_Path,
2891                   Display_Value => Non_Canonical_Path,
2892                   Location => No_Location,
2893                   Flag     => False,
2894                   Next     => Nil_String,
2895                   Index    => 0);
2896
2897                --  Case of first source directory
2898
2899                if Last_Source_Dir = Nil_String then
2900                   Data.Source_Dirs := String_Element_Table.Last
2901                                         (In_Tree.String_Elements);
2902
2903                   --  Here we already have source directories
2904
2905                else
2906                   --  Link the previous last to the new one
2907
2908                   In_Tree.String_Elements.Table
2909                     (Last_Source_Dir).Next :=
2910                       String_Element_Table.Last
2911                         (In_Tree.String_Elements);
2912                end if;
2913
2914                --  And register this source directory as the new last
2915
2916                Last_Source_Dir  := String_Element_Table.Last
2917                  (In_Tree.String_Elements);
2918                In_Tree.String_Elements.Table (Last_Source_Dir) :=
2919                  Element;
2920             end if;
2921
2922             --  Now look for subdirectories. We do that even when this
2923             --  directory is already in the list, because some of its
2924             --  subdirectories may not be in the list yet.
2925
2926             Open (Dir, The_Path (The_Path'First .. The_Path_Last));
2927
2928             loop
2929                Read (Dir, Name, Last);
2930                exit when Last = 0;
2931
2932                if Name (1 .. Last) /= "."
2933                  and then Name (1 .. Last) /= ".."
2934                then
2935                   --  Avoid . and .. directories
2936
2937                   if Current_Verbosity = High then
2938                      Write_Str  ("   Checking ");
2939                      Write_Line (Name (1 .. Last));
2940                   end if;
2941
2942                   declare
2943                      Path_Name : constant String :=
2944                                    Normalize_Pathname
2945                                      (Name      => Name (1 .. Last),
2946                                       Directory =>
2947                                         The_Path
2948                                           (The_Path'First .. The_Path_Last),
2949                                       Resolve_Links  => False,
2950                                       Case_Sensitive => True);
2951
2952                   begin
2953                      if Is_Directory (Path_Name) then
2954
2955                         --  We have found a new subdirectory, call self
2956
2957                         Name_Len := Path_Name'Length;
2958                         Name_Buffer (1 .. Name_Len) := Path_Name;
2959                         Recursive_Find_Dirs (Name_Find);
2960                      end if;
2961                   end;
2962                end if;
2963             end loop;
2964
2965             Close (Dir);
2966
2967          exception
2968             when Directory_Error =>
2969                null;
2970          end Recursive_Find_Dirs;
2971
2972       --  Start of processing for Find_Source_Dirs
2973
2974       begin
2975          if Current_Verbosity = High then
2976             Write_Str ("Find_Source_Dirs (""");
2977             Write_Str (Directory);
2978             Write_Line (""")");
2979          end if;
2980
2981          --  First, check if we are looking for a directory tree,
2982          --  indicated by "/**" at the end.
2983
2984          if Directory'Length >= 3
2985            and then Directory (Directory'Last - 1 .. Directory'Last) = "**"
2986            and then (Directory (Directory'Last - 2) = '/'
2987                        or else
2988                      Directory (Directory'Last - 2) = Directory_Separator)
2989          then
2990             Data.Known_Order_Of_Source_Dirs := False;
2991
2992             Name_Len := Directory'Length - 3;
2993
2994             if Name_Len = 0 then
2995
2996                --  This is the case of "/**": all directories
2997                --  in the file system.
2998
2999                Name_Len := 1;
3000                Name_Buffer (1) := Directory (Directory'First);
3001
3002             else
3003                Name_Buffer (1 .. Name_Len) :=
3004                  Directory (Directory'First .. Directory'Last - 3);
3005             end if;
3006
3007             if Current_Verbosity = High then
3008                Write_Str ("Looking for all subdirectories of """);
3009                Write_Str (Name_Buffer (1 .. Name_Len));
3010                Write_Line ("""");
3011             end if;
3012
3013             declare
3014                Base_Dir : constant Name_Id := Name_Find;
3015                Root_Dir : constant String :=
3016                             Normalize_Pathname
3017                               (Name      => Get_Name_String (Base_Dir),
3018                                Directory =>
3019                                  Get_Name_String (Data.Display_Directory),
3020                                Resolve_Links  => False,
3021                                Case_Sensitive => True);
3022
3023             begin
3024                if Root_Dir'Length = 0 then
3025                   Err_Vars.Error_Msg_Name_1 := Base_Dir;
3026
3027                   if Location = No_Location then
3028                      Error_Msg
3029                        (Project, In_Tree,
3030                         "{ is not a valid directory.",
3031                         Data.Location);
3032                   else
3033                      Error_Msg
3034                        (Project, In_Tree,
3035                         "{ is not a valid directory.",
3036                         Location);
3037                   end if;
3038
3039                else
3040                   --  We have an existing directory, we register it and all
3041                   --  of its subdirectories.
3042
3043                   if Current_Verbosity = High then
3044                      Write_Line ("Looking for source directories:");
3045                   end if;
3046
3047                   Name_Len := Root_Dir'Length;
3048                   Name_Buffer (1 .. Name_Len) := Root_Dir;
3049                   Recursive_Find_Dirs (Name_Find);
3050
3051                   if Current_Verbosity = High then
3052                      Write_Line ("End of looking for source directories.");
3053                   end if;
3054                end if;
3055             end;
3056
3057          --  We have a single directory
3058
3059          else
3060             declare
3061                Path_Name         : Name_Id;
3062                Display_Path_Name : Name_Id;
3063
3064             begin
3065                Locate_Directory
3066                  (From, Data.Display_Directory, Path_Name, Display_Path_Name);
3067
3068                if Path_Name = No_Name then
3069                   Err_Vars.Error_Msg_Name_1 := From;
3070
3071                   if Location = No_Location then
3072                      Error_Msg
3073                        (Project, In_Tree,
3074                         "{ is not a valid directory",
3075                         Data.Location);
3076                   else
3077                      Error_Msg
3078                        (Project, In_Tree,
3079                         "{ is not a valid directory",
3080                         Location);
3081                   end if;
3082
3083                else
3084                   --  As it is an existing directory, we add it to
3085                   --  the list of directories.
3086
3087                   String_Element_Table.Increment_Last
3088                     (In_Tree.String_Elements);
3089                   Element.Value := Path_Name;
3090                   Element.Display_Value := Display_Path_Name;
3091
3092                   if Last_Source_Dir = Nil_String then
3093
3094                      --  This is the first source directory
3095
3096                      Data.Source_Dirs := String_Element_Table.Last
3097                                         (In_Tree.String_Elements);
3098
3099                   else
3100                      --  We already have source directories,
3101                      --  link the previous last to the new one.
3102
3103                      In_Tree.String_Elements.Table
3104                        (Last_Source_Dir).Next :=
3105                          String_Element_Table.Last
3106                            (In_Tree.String_Elements);
3107                   end if;
3108
3109                   --  And register this source directory as the new last
3110
3111                   Last_Source_Dir := String_Element_Table.Last
3112                     (In_Tree.String_Elements);
3113                   In_Tree.String_Elements.Table
3114                     (Last_Source_Dir) := Element;
3115                end if;
3116             end;
3117          end if;
3118       end Find_Source_Dirs;
3119
3120    --  Start of processing for Get_Directories
3121
3122    begin
3123       if Current_Verbosity = High then
3124          Write_Line ("Starting to look for directories");
3125       end if;
3126
3127       --  Check the object directory
3128
3129       pragma Assert (Object_Dir.Kind = Single,
3130                      "Object_Dir is not a single string");
3131
3132       --  We set the object directory to its default
3133
3134       Data.Object_Directory   := Data.Directory;
3135       Data.Display_Object_Dir := Data.Display_Directory;
3136
3137       if Object_Dir.Value /= Empty_String then
3138          Get_Name_String (Object_Dir.Value);
3139
3140          if Name_Len = 0 then
3141             Error_Msg
3142               (Project, In_Tree,
3143                "Object_Dir cannot be empty",
3144                Object_Dir.Location);
3145
3146          else
3147             --  We check that the specified object directory does exist
3148
3149             Locate_Directory
3150               (Object_Dir.Value, Data.Display_Directory,
3151                Data.Object_Directory, Data.Display_Object_Dir);
3152
3153             if Data.Object_Directory = No_Name then
3154
3155                --  The object directory does not exist, report an error
3156
3157                Err_Vars.Error_Msg_Name_1 := Object_Dir.Value;
3158                Error_Msg
3159                  (Project, In_Tree,
3160                   "the object directory { cannot be found",
3161                   Data.Location);
3162
3163                --  Do not keep a nil Object_Directory. Set it to the specified
3164                --  (relative or absolute) path. This is for the benefit of
3165                --  tools that recover from errors; for example, these tools
3166                --  could create the non existent directory.
3167
3168                Data.Display_Object_Dir := Object_Dir.Value;
3169                Get_Name_String (Object_Dir.Value);
3170                Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
3171                Data.Object_Directory := Name_Find;
3172             end if;
3173          end if;
3174       end if;
3175
3176       if Current_Verbosity = High then
3177          if Data.Object_Directory = No_Name then
3178             Write_Line ("No object directory");
3179          else
3180             Write_Str ("Object directory: """);
3181             Write_Str (Get_Name_String (Data.Display_Object_Dir));
3182             Write_Line ("""");
3183          end if;
3184       end if;
3185
3186       --  Check the exec directory
3187
3188       pragma Assert (Exec_Dir.Kind = Single,
3189                      "Exec_Dir is not a single string");
3190
3191       --  We set the object directory to its default
3192
3193       Data.Exec_Directory   := Data.Object_Directory;
3194       Data.Display_Exec_Dir := Data.Display_Object_Dir;
3195
3196       if Exec_Dir.Value /= Empty_String then
3197          Get_Name_String (Exec_Dir.Value);
3198
3199          if Name_Len = 0 then
3200             Error_Msg
3201               (Project, In_Tree,
3202                "Exec_Dir cannot be empty",
3203                Exec_Dir.Location);
3204
3205          else
3206             --  We check that the specified object directory
3207             --  does exist.
3208
3209             Locate_Directory
3210               (Exec_Dir.Value, Data.Directory,
3211                Data.Exec_Directory, Data.Display_Exec_Dir);
3212
3213             if Data.Exec_Directory = No_Name then
3214                Err_Vars.Error_Msg_Name_1 := Exec_Dir.Value;
3215                Error_Msg
3216                  (Project, In_Tree,
3217                   "the exec directory { cannot be found",
3218                   Data.Location);
3219             end if;
3220          end if;
3221       end if;
3222
3223       if Current_Verbosity = High then
3224          if Data.Exec_Directory = No_Name then
3225             Write_Line ("No exec directory");
3226          else
3227             Write_Str ("Exec directory: """);
3228             Write_Str (Get_Name_String (Data.Display_Exec_Dir));
3229             Write_Line ("""");
3230          end if;
3231       end if;
3232
3233       --  Look for the source directories
3234
3235       if Current_Verbosity = High then
3236          Write_Line ("Starting to look for source directories");
3237       end if;
3238
3239       pragma Assert (Source_Dirs.Kind = List, "Source_Dirs is not a list");
3240
3241       if Source_Dirs.Default then
3242
3243          --  No Source_Dirs specified: the single source directory
3244          --  is the one containing the project file
3245
3246          String_Element_Table.Increment_Last
3247            (In_Tree.String_Elements);
3248          Data.Source_Dirs := String_Element_Table.Last
3249            (In_Tree.String_Elements);
3250          In_Tree.String_Elements.Table (Data.Source_Dirs) :=
3251            (Value         => Data.Directory,
3252             Display_Value => Data.Display_Directory,
3253             Location      => No_Location,
3254             Flag          => False,
3255             Next          => Nil_String,
3256             Index         => 0);
3257
3258          if Current_Verbosity = High then
3259             Write_Line ("Single source directory:");
3260             Write_Str ("    """);
3261             Write_Str (Get_Name_String (Data.Display_Directory));
3262             Write_Line ("""");
3263          end if;
3264
3265       elsif Source_Dirs.Values = Nil_String then
3266
3267          --  If Source_Dirs is an empty string list, this means
3268          --  that this project contains no source. For projects that
3269          --  don't extend other projects, this also means that there is no
3270          --  need for an object directory, if not specified.
3271
3272          if Data.Extends = No_Project
3273            and then  Data.Object_Directory = Data.Directory
3274          then
3275             Data.Object_Directory := No_Name;
3276          end if;
3277
3278          Data.Source_Dirs           := Nil_String;
3279          Data.Ada_Sources_Present   := False;
3280          Data.Other_Sources_Present := False;
3281
3282       else
3283          declare
3284             Source_Dir : String_List_Id := Source_Dirs.Values;
3285             Element    : String_Element;
3286
3287          begin
3288             --  We will find the source directories for each
3289             --  element of the list
3290
3291             while Source_Dir /= Nil_String loop
3292                Element :=
3293                  In_Tree.String_Elements.Table (Source_Dir);
3294                Find_Source_Dirs (Element.Value, Element.Location);
3295                Source_Dir := Element.Next;
3296             end loop;
3297          end;
3298       end if;
3299
3300       if Current_Verbosity = High then
3301          Write_Line ("Putting source directories in canonical cases");
3302       end if;
3303
3304       declare
3305          Current : String_List_Id := Data.Source_Dirs;
3306          Element : String_Element;
3307
3308       begin
3309          while Current /= Nil_String loop
3310             Element := In_Tree.String_Elements.Table (Current);
3311             if Element.Value /= No_Name then
3312                Get_Name_String (Element.Value);
3313                Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
3314                Element.Value := Name_Find;
3315                In_Tree.String_Elements.Table (Current) := Element;
3316             end if;
3317
3318             Current := Element.Next;
3319          end loop;
3320       end;
3321
3322    end Get_Directories;
3323
3324    ---------------
3325    -- Get_Mains --
3326    ---------------
3327
3328    procedure Get_Mains
3329      (Project : Project_Id;
3330       In_Tree : Project_Tree_Ref;
3331       Data    : in out Project_Data) is
3332       Mains : constant Variable_Value :=
3333                 Prj.Util.Value_Of (Name_Main, Data.Decl.Attributes, In_Tree);
3334
3335    begin
3336       Data.Mains := Mains.Values;
3337
3338       --  If no Mains were specified, and if we are an extending
3339       --  project, inherit the Mains from the project we are extending.
3340
3341       if Mains.Default then
3342          if Data.Extends /= No_Project then
3343             Data.Mains :=
3344               In_Tree.Projects.Table (Data.Extends).Mains;
3345          end if;
3346
3347       --  In a library project file, Main cannot be specified
3348
3349       elsif Data.Library then
3350          Error_Msg
3351            (Project, In_Tree,
3352             "a library project file cannot have Main specified",
3353             Mains.Location);
3354       end if;
3355    end Get_Mains;
3356
3357    ---------------------------
3358    -- Get_Sources_From_File --
3359    ---------------------------
3360
3361    procedure Get_Sources_From_File
3362      (Path     : String;
3363       Location : Source_Ptr;
3364       Project  : Project_Id;
3365       In_Tree  : Project_Tree_Ref)
3366    is
3367       File        : Prj.Util.Text_File;
3368       Line        : String (1 .. 250);
3369       Last        : Natural;
3370       Source_Name : Name_Id;
3371
3372    begin
3373       Source_Names.Reset;
3374
3375       if Current_Verbosity = High then
3376          Write_Str  ("Opening """);
3377          Write_Str  (Path);
3378          Write_Line (""".");
3379       end if;
3380
3381       --  Open the file
3382
3383       Prj.Util.Open (File, Path);
3384
3385       if not Prj.Util.Is_Valid (File) then
3386          Error_Msg (Project, In_Tree, "file does not exist", Location);
3387       else
3388          --  Read the lines one by one
3389
3390          while not Prj.Util.End_Of_File (File) loop
3391             Prj.Util.Get_Line (File, Line, Last);
3392
3393             --  A non empty, non comment line should contain a file name
3394
3395             if Last /= 0
3396               and then (Last = 1 or else Line (1 .. 2) /= "--")
3397             then
3398                --  ??? we should check that there is no directory information
3399
3400                Name_Len := Last;
3401                Name_Buffer (1 .. Name_Len) := Line (1 .. Last);
3402                Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
3403                Source_Name := Name_Find;
3404                Source_Names.Set
3405                  (K => Source_Name,
3406                   E =>
3407                     (Name     => Source_Name,
3408                      Location => Location,
3409                      Found    => False));
3410             end if;
3411          end loop;
3412
3413          Prj.Util.Close (File);
3414
3415       end if;
3416    end Get_Sources_From_File;
3417
3418    --------------
3419    -- Get_Unit --
3420    --------------
3421
3422    procedure Get_Unit
3423      (Canonical_File_Name : Name_Id;
3424       Naming              : Naming_Data;
3425       Exception_Id        : out Ada_Naming_Exception_Id;
3426       Unit_Name           : out Name_Id;
3427       Unit_Kind           : out Spec_Or_Body;
3428       Needs_Pragma        : out Boolean)
3429    is
3430       Info_Id  : Ada_Naming_Exception_Id
3431         := Ada_Naming_Exceptions.Get (Canonical_File_Name);
3432       VMS_Name : Name_Id;
3433
3434    begin
3435       if Info_Id = No_Ada_Naming_Exception then
3436          if Hostparm.OpenVMS then
3437             VMS_Name := Canonical_File_Name;
3438             Get_Name_String (VMS_Name);
3439
3440             if Name_Buffer (Name_Len) = '.' then
3441                Name_Len := Name_Len - 1;
3442                VMS_Name := Name_Find;
3443             end if;
3444
3445             Info_Id := Ada_Naming_Exceptions.Get (VMS_Name);
3446          end if;
3447
3448       end if;
3449
3450       if Info_Id /= No_Ada_Naming_Exception then
3451          Exception_Id := Info_Id;
3452          Unit_Name := No_Name;
3453          Unit_Kind := Specification;
3454          Needs_Pragma := True;
3455          return;
3456       end if;
3457
3458       Needs_Pragma := False;
3459       Exception_Id := No_Ada_Naming_Exception;
3460
3461       Get_Name_String (Canonical_File_Name);
3462
3463       declare
3464          File          : String := Name_Buffer (1 .. Name_Len);
3465          First         : constant Positive := File'First;
3466          Last          : Natural           := File'Last;
3467          Standard_GNAT : Boolean;
3468
3469       begin
3470          Standard_GNAT :=
3471            Naming.Ada_Spec_Suffix = Default_Ada_Spec_Suffix
3472              and then Naming.Ada_Body_Suffix = Default_Ada_Body_Suffix;
3473
3474          --  Check if the end of the file name is Specification_Append
3475
3476          Get_Name_String (Naming.Ada_Spec_Suffix);
3477
3478          if File'Length > Name_Len
3479            and then File (Last - Name_Len + 1 .. Last) =
3480                                                 Name_Buffer (1 .. Name_Len)
3481          then
3482             --  We have a spec
3483
3484             Unit_Kind := Specification;
3485             Last := Last - Name_Len;
3486
3487             if Current_Verbosity = High then
3488                Write_Str  ("   Specification: ");
3489                Write_Line (File (First .. Last));
3490             end if;
3491
3492          else
3493             Get_Name_String (Naming.Ada_Body_Suffix);
3494
3495             --  Check if the end of the file name is Body_Append
3496
3497             if File'Length > Name_Len
3498               and then File (Last - Name_Len + 1 .. Last) =
3499                                                 Name_Buffer (1 .. Name_Len)
3500             then
3501                --  We have a body
3502
3503                Unit_Kind := Body_Part;
3504                Last := Last - Name_Len;
3505
3506                if Current_Verbosity = High then
3507                   Write_Str  ("   Body: ");
3508                   Write_Line (File (First .. Last));
3509                end if;
3510
3511             elsif Naming.Separate_Suffix /= Naming.Ada_Spec_Suffix then
3512                Get_Name_String (Naming.Separate_Suffix);
3513
3514                --  Check if the end of the file name is Separate_Append
3515
3516                if File'Length > Name_Len
3517                  and then File (Last - Name_Len + 1 .. Last) =
3518                                                 Name_Buffer (1 .. Name_Len)
3519                then
3520                   --  We have a separate (a body)
3521
3522                   Unit_Kind := Body_Part;
3523                   Last := Last - Name_Len;
3524
3525                   if Current_Verbosity = High then
3526                      Write_Str  ("   Separate: ");
3527                      Write_Line (File (First .. Last));
3528                   end if;
3529
3530                else
3531                   Last := 0;
3532                end if;
3533
3534             else
3535                Last := 0;
3536             end if;
3537          end if;
3538
3539          if Last = 0 then
3540
3541             --  This is not a source file
3542
3543             Unit_Name := No_Name;
3544             Unit_Kind := Specification;
3545
3546             if Current_Verbosity = High then
3547                Write_Line ("   Not a valid file name.");
3548             end if;
3549
3550             return;
3551          end if;
3552
3553          Get_Name_String (Naming.Dot_Replacement);
3554          Standard_GNAT :=
3555            Standard_GNAT and then Name_Buffer (1 .. Name_Len) = "-";
3556
3557          if Name_Buffer (1 .. Name_Len) /= "." then
3558
3559             --  If Dot_Replacement is not a single dot, then there should
3560             --  not be any dot in the name.
3561
3562             for Index in First .. Last loop
3563                if File (Index) = '.' then
3564                   if Current_Verbosity = High then
3565                      Write_Line
3566                        ("   Not a valid file name (some dot not replaced).");
3567                   end if;
3568
3569                   Unit_Name := No_Name;
3570                   return;
3571
3572                end if;
3573             end loop;
3574
3575             --  Replace the substring Dot_Replacement with dots
3576
3577             declare
3578                Index : Positive := First;
3579
3580             begin
3581                while Index <= Last - Name_Len + 1 loop
3582
3583                   if File (Index .. Index + Name_Len - 1) =
3584                     Name_Buffer (1 .. Name_Len)
3585                   then
3586                      File (Index) := '.';
3587
3588                      if Name_Len > 1 and then Index < Last then
3589                         File (Index + 1 .. Last - Name_Len + 1) :=
3590                           File (Index + Name_Len .. Last);
3591                      end if;
3592
3593                      Last := Last - Name_Len + 1;
3594                   end if;
3595
3596                   Index := Index + 1;
3597                end loop;
3598             end;
3599          end if;
3600
3601          --  Check if the casing is right
3602
3603          declare
3604             Src : String := File (First .. Last);
3605
3606          begin
3607             case Naming.Casing is
3608                when All_Lower_Case =>
3609                   Fixed.Translate
3610                     (Source  => Src,
3611                      Mapping => Lower_Case_Map);
3612
3613                when All_Upper_Case =>
3614                   Fixed.Translate
3615                     (Source  => Src,
3616                      Mapping => Upper_Case_Map);
3617
3618                when Mixed_Case | Unknown =>
3619                   null;
3620             end case;
3621
3622             if Src /= File (First .. Last) then
3623                if Current_Verbosity = High then
3624                   Write_Line ("   Not a valid file name (casing).");
3625                end if;
3626
3627                Unit_Name := No_Name;
3628                return;
3629             end if;
3630
3631             --  We put the name in lower case
3632
3633             Fixed.Translate
3634               (Source  => Src,
3635                Mapping => Lower_Case_Map);
3636
3637             --  In the standard GNAT naming scheme, check for special cases:
3638             --  children or separates of A, G, I or S, and run time sources.
3639
3640             if Standard_GNAT and then Src'Length >= 3 then
3641                declare
3642                   S1 : constant Character := Src (Src'First);
3643                   S2 : constant Character := Src (Src'First + 1);
3644
3645                begin
3646                   if S1 = 'a' or else S1 = 'g'
3647                     or else S1 = 'i' or else S1 = 's'
3648                   then
3649                      --  Children or separates of packages A, G, I or S
3650
3651                      if (Hostparm.OpenVMS and then S2 = '$')
3652                        or else (not Hostparm.OpenVMS and then S2 = '~')
3653                      then
3654                         Src (Src'First + 1) := '.';
3655
3656                      --  If it is potentially a run time source, disable
3657                      --  filling of the mapping file to avoid warnings.
3658
3659                      elsif S2 = '.' then
3660                         Set_Mapping_File_Initial_State_To_Empty;
3661                      end if;
3662
3663                   end if;
3664                end;
3665             end if;
3666
3667             if Current_Verbosity = High then
3668                Write_Str  ("      ");
3669                Write_Line (Src);
3670             end if;
3671
3672             --  Now, we check if this name is a valid unit name
3673
3674             Check_Ada_Name (Name => Src, Unit => Unit_Name);
3675          end;
3676
3677       end;
3678    end Get_Unit;
3679
3680    ----------
3681    -- Hash --
3682    ----------
3683
3684    function Hash (Unit : Unit_Info) return Header_Num is
3685    begin
3686       return Header_Num (Unit.Unit mod 2048);
3687    end Hash;
3688
3689    -----------------------
3690    -- Is_Illegal_Suffix --
3691    -----------------------
3692
3693    function Is_Illegal_Suffix
3694      (Suffix                          : String;
3695       Dot_Replacement_Is_A_Single_Dot : Boolean) return Boolean
3696    is
3697    begin
3698       if Suffix'Length = 0 or else Index (Suffix, ".") = 0 then
3699          return True;
3700       end if;
3701
3702       --  If dot replacement is a single dot, and first character of
3703       --  suffix is also a dot
3704
3705       if Dot_Replacement_Is_A_Single_Dot
3706         and then Suffix (Suffix'First) = '.'
3707       then
3708          for Index in Suffix'First + 1 .. Suffix'Last loop
3709
3710             --  If there is another dot
3711
3712             if Suffix (Index) = '.' then
3713
3714                --  It is illegal to have a letter following the initial dot
3715
3716                return Is_Letter (Suffix (Suffix'First + 1));
3717             end if;
3718          end loop;
3719       end if;
3720
3721       --  Everything is OK
3722
3723       return False;
3724    end Is_Illegal_Suffix;
3725
3726    ----------------------
3727    -- Locate_Directory --
3728    ----------------------
3729
3730    procedure Locate_Directory
3731      (Name    : Name_Id;
3732       Parent  : Name_Id;
3733       Dir     : out Name_Id;
3734       Display : out Name_Id)
3735    is
3736       The_Name   : constant String := Get_Name_String (Name);
3737
3738       The_Parent : constant String :=
3739                      Get_Name_String (Parent) & Directory_Separator;
3740
3741       The_Parent_Last : constant Natural :=
3742                      Compute_Directory_Last (The_Parent);
3743
3744    begin
3745       if Current_Verbosity = High then
3746          Write_Str ("Locate_Directory (""");
3747          Write_Str (The_Name);
3748          Write_Str (""", """);
3749          Write_Str (The_Parent);
3750          Write_Line (""")");
3751       end if;
3752
3753       Dir     := No_Name;
3754       Display := No_Name;
3755
3756       if Is_Absolute_Path (The_Name) then
3757          if Is_Directory (The_Name) then
3758             declare
3759                Normed : constant String :=
3760                           Normalize_Pathname
3761                             (The_Name,
3762                              Resolve_Links  => False,
3763                              Case_Sensitive => True);
3764
3765                Canonical_Path : constant String :=
3766                                   Normalize_Pathname
3767                                     (Normed,
3768                                      Resolve_Links  => True,
3769                                      Case_Sensitive => False);
3770
3771             begin
3772                Name_Len := Normed'Length;
3773                Name_Buffer (1 .. Name_Len) := Normed;
3774                Display := Name_Find;
3775
3776                Name_Len := Canonical_Path'Length;
3777                Name_Buffer (1 .. Name_Len) := Canonical_Path;
3778                Dir := Name_Find;
3779             end;
3780          end if;
3781
3782       else
3783          declare
3784             Full_Path : constant String :=
3785                           The_Parent (The_Parent'First .. The_Parent_Last) &
3786                           The_Name;
3787
3788          begin
3789             if Is_Directory (Full_Path) then
3790                declare
3791                   Normed : constant String :=
3792                              Normalize_Pathname
3793                                (Full_Path,
3794                                 Resolve_Links  => False,
3795                                 Case_Sensitive => True);
3796
3797                   Canonical_Path : constant String :=
3798                                      Normalize_Pathname
3799                                        (Normed,
3800                                         Resolve_Links  => True,
3801                                         Case_Sensitive => False);
3802
3803                begin
3804                   Name_Len := Normed'Length;
3805                   Name_Buffer (1 .. Name_Len) := Normed;
3806                   Display := Name_Find;
3807
3808                   Name_Len := Canonical_Path'Length;
3809                   Name_Buffer (1 .. Name_Len) := Canonical_Path;
3810                   Dir := Name_Find;
3811                end;
3812             end if;
3813          end;
3814       end if;
3815    end Locate_Directory;
3816
3817    ----------------------
3818    -- Look_For_Sources --
3819    ----------------------
3820
3821    procedure Look_For_Sources
3822      (Project      : Project_Id;
3823       In_Tree      : Project_Tree_Ref;
3824       Data         : in out Project_Data;
3825       Follow_Links : Boolean)
3826    is
3827       procedure Get_Path_Names_And_Record_Sources (Follow_Links : Boolean);
3828       --  Find the path names of the source files in the Source_Names table
3829       --  in the source directories and record those that are Ada sources.
3830
3831       procedure Get_Sources_From_File
3832         (Path     : String;
3833          Location : Source_Ptr);
3834       --  Get the sources of a project from a text file
3835
3836       ---------------------------------------
3837       -- Get_Path_Names_And_Record_Sources --
3838       ---------------------------------------
3839
3840       procedure Get_Path_Names_And_Record_Sources (Follow_Links : Boolean) is
3841          Source_Dir : String_List_Id := Data.Source_Dirs;
3842          Element    : String_Element;
3843          Path       : Name_Id;
3844
3845          Dir      : Dir_Type;
3846          Name     : Name_Id;
3847          Canonical_Name : Name_Id;
3848          Name_Str : String (1 .. 1_024);
3849          Last     : Natural := 0;
3850          NL       : Name_Location;
3851
3852          Current_Source : String_List_Id := Nil_String;
3853
3854          First_Error : Boolean := True;
3855
3856          Source_Recorded : Boolean := False;
3857
3858       begin
3859          --  We look in all source directories for the file names in the
3860          --  hash table Source_Names
3861
3862          while Source_Dir /= Nil_String loop
3863             Source_Recorded := False;
3864             Element := In_Tree.String_Elements.Table (Source_Dir);
3865
3866             declare
3867                Dir_Path : constant String := Get_Name_String (Element.Value);
3868             begin
3869                if Current_Verbosity = High then
3870                   Write_Str ("checking directory """);
3871                   Write_Str (Dir_Path);
3872                   Write_Line ("""");
3873                end if;
3874
3875                Open (Dir, Dir_Path);
3876
3877                loop
3878                   Read (Dir, Name_Str, Last);
3879                   exit when Last = 0;
3880                   Name_Len := Last;
3881                   Name_Buffer (1 .. Name_Len) := Name_Str (1 .. Last);
3882                   Name := Name_Find;
3883                   Canonical_Case_File_Name (Name_Str (1 .. Last));
3884                   Name_Len := Last;
3885                   Name_Buffer (1 .. Name_Len) := Name_Str (1 .. Last);
3886                   Canonical_Name := Name_Find;
3887                   NL := Source_Names.Get (Canonical_Name);
3888
3889                   if NL /= No_Name_Location and then not NL.Found then
3890                      NL.Found := True;
3891                      Source_Names.Set (Canonical_Name, NL);
3892                      Name_Len := Dir_Path'Length;
3893                      Name_Buffer (1 .. Name_Len) := Dir_Path;
3894
3895                      if Name_Buffer (Name_Len) /= Directory_Separator then
3896                         Add_Char_To_Name_Buffer (Directory_Separator);
3897                      end if;
3898
3899                      Add_Str_To_Name_Buffer (Name_Str (1 .. Last));
3900                      Path := Name_Find;
3901
3902                      if Current_Verbosity = High then
3903                         Write_Str  ("  found ");
3904                         Write_Line (Get_Name_String (Name));
3905                      end if;
3906
3907                      --  Register the source if it is an Ada compilation unit
3908
3909                      Record_Ada_Source
3910                        (File_Name       => Name,
3911                         Path_Name       => Path,
3912                         Project         => Project,
3913                         In_Tree         => In_Tree,
3914                         Data            => Data,
3915                         Location        => NL.Location,
3916                         Current_Source  => Current_Source,
3917                         Source_Recorded => Source_Recorded,
3918                         Follow_Links    => Follow_Links);
3919                   end if;
3920                end loop;
3921
3922                Close (Dir);
3923             end;
3924
3925             if Source_Recorded then
3926                In_Tree.String_Elements.Table (Source_Dir).Flag :=
3927                  True;
3928             end if;
3929
3930             Source_Dir := Element.Next;
3931          end loop;
3932
3933          --  It is an error if a source file name in a source list or
3934          --  in a source list file is not found.
3935
3936          NL := Source_Names.Get_First;
3937
3938          while NL /= No_Name_Location loop
3939             if not NL.Found then
3940                Err_Vars.Error_Msg_Name_1 := NL.Name;
3941
3942                if First_Error then
3943                   Error_Msg
3944                     (Project, In_Tree,
3945                      "source file { cannot be found",
3946                      NL.Location);
3947                   First_Error := False;
3948
3949                else
3950                   Error_Msg
3951                     (Project, In_Tree,
3952                      "\source file { cannot be found",
3953                      NL.Location);
3954                end if;
3955             end if;
3956
3957             NL := Source_Names.Get_Next;
3958          end loop;
3959       end Get_Path_Names_And_Record_Sources;
3960
3961       ---------------------------
3962       -- Get_Sources_From_File --
3963       ---------------------------
3964
3965       procedure Get_Sources_From_File
3966         (Path     : String;
3967          Location : Source_Ptr)
3968       is
3969       begin
3970          --  Get the list of sources from the file and put them in hash table
3971          --  Source_Names.
3972
3973          Get_Sources_From_File (Path, Location, Project, In_Tree);
3974
3975          --  Look in the source directories to find those sources
3976
3977          Get_Path_Names_And_Record_Sources (Follow_Links);
3978
3979          --  We should have found at least one source.
3980          --  If not, report an error.
3981
3982          if Data.Sources = Nil_String then
3983             Error_Msg (Project, In_Tree,
3984                        "there are no Ada sources in this project",
3985                        Location);
3986          end if;
3987       end Get_Sources_From_File;
3988
3989    begin
3990       if Data.Ada_Sources_Present then
3991          declare
3992             Sources          : constant Variable_Value :=
3993                                  Util.Value_Of
3994                                    (Name_Source_Files,
3995                                     Data.Decl.Attributes,
3996                                     In_Tree);
3997
3998             Source_List_File : constant Variable_Value :=
3999                                  Util.Value_Of
4000                                    (Name_Source_List_File,
4001                                     Data.Decl.Attributes,
4002                                     In_Tree);
4003
4004             Locally_Removed  : constant Variable_Value :=
4005                                  Util.Value_Of
4006                                    (Name_Locally_Removed_Files,
4007                                     Data.Decl.Attributes,
4008                                     In_Tree);
4009
4010          begin
4011             pragma Assert
4012               (Sources.Kind = List,
4013                "Source_Files is not a list");
4014
4015             pragma Assert
4016               (Source_List_File.Kind = Single,
4017                "Source_List_File is not a single string");
4018
4019             if not Sources.Default then
4020                if not Source_List_File.Default then
4021                   Error_Msg
4022                     (Project, In_Tree,
4023                      "?both variables source_files and " &
4024                      "source_list_file are present",
4025                      Source_List_File.Location);
4026                end if;
4027
4028                --  Sources is a list of file names
4029
4030                declare
4031                   Current  : String_List_Id := Sources.Values;
4032                   Element  : String_Element;
4033                   Location : Source_Ptr;
4034                   Name     : Name_Id;
4035
4036                begin
4037                   Source_Names.Reset;
4038
4039                   Data.Ada_Sources_Present := Current /= Nil_String;
4040
4041                   while Current /= Nil_String loop
4042                      Element :=
4043                        In_Tree.String_Elements.Table (Current);
4044                      Get_Name_String (Element.Value);
4045                      Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
4046                      Name := Name_Find;
4047
4048                      --  If the element has no location, then use the
4049                      --  location of Sources to report possible errors.
4050
4051                      if Element.Location = No_Location then
4052                         Location := Sources.Location;
4053                      else
4054                         Location := Element.Location;
4055                      end if;
4056
4057                      Source_Names.Set
4058                        (K => Name,
4059                         E =>
4060                           (Name     => Name,
4061                            Location => Location,
4062                            Found    => False));
4063
4064                      Current := Element.Next;
4065                   end loop;
4066
4067                   Get_Path_Names_And_Record_Sources (Follow_Links);
4068                end;
4069
4070                --  No source_files specified
4071
4072                --  We check Source_List_File has been specified
4073
4074             elsif not Source_List_File.Default then
4075
4076                --  Source_List_File is the name of the file
4077                --  that contains the source file names
4078
4079                declare
4080                   Source_File_Path_Name : constant String :=
4081                                             Path_Name_Of
4082                                               (Source_List_File.Value,
4083                                                Data.Directory);
4084
4085                begin
4086                   if Source_File_Path_Name'Length = 0 then
4087                      Err_Vars.Error_Msg_Name_1 := Source_List_File.Value;
4088                      Error_Msg
4089                        (Project, In_Tree,
4090                         "file with sources { does not exist",
4091                         Source_List_File.Location);
4092
4093                   else
4094                      Get_Sources_From_File
4095                        (Source_File_Path_Name,
4096                         Source_List_File.Location);
4097                   end if;
4098                end;
4099
4100             else
4101                --  Neither Source_Files nor Source_List_File has been
4102                --  specified. Find all the files that satisfy the naming
4103                --  scheme in all the source directories.
4104
4105                Find_Sources
4106                  (Project, In_Tree, Data, Ada_Language_Index, Follow_Links);
4107             end if;
4108
4109             --  If there are sources that are locally removed, mark them as
4110             --  such in the Units table.
4111
4112             if not Locally_Removed.Default then
4113
4114                --  Sources can be locally removed only in extending
4115                --  project files.
4116
4117                if Data.Extends = No_Project then
4118                   Error_Msg
4119                     (Project, In_Tree,
4120                      "Locally_Removed_Files can only be used " &
4121                      "in an extending project file",
4122                      Locally_Removed.Location);
4123
4124                else
4125                   declare
4126                      Current  : String_List_Id := Locally_Removed.Values;
4127                      Element  : String_Element;
4128                      Location : Source_Ptr;
4129                      OK       : Boolean;
4130                      Unit     : Unit_Data;
4131                      Name     : Name_Id;
4132                      Extended : Project_Id;
4133
4134                   begin
4135                      while Current /= Nil_String loop
4136                         Element :=
4137                           In_Tree.String_Elements.Table (Current);
4138                         Get_Name_String (Element.Value);
4139                         Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
4140                         Name := Name_Find;
4141
4142                         --  If the element has no location, then use the
4143                         --  location of Locally_Removed to report
4144                         --  possible errors.
4145
4146                         if Element.Location = No_Location then
4147                            Location := Locally_Removed.Location;
4148                         else
4149                            Location := Element.Location;
4150                         end if;
4151
4152                         OK := False;
4153
4154                         for Index in Unit_Table.First ..
4155                                   Unit_Table.Last (In_Tree.Units)
4156                         loop
4157                            Unit := In_Tree.Units.Table (Index);
4158
4159                            if Unit.File_Names (Specification).Name = Name then
4160                               OK := True;
4161
4162                               --  Check that this is from a project that
4163                               --  the current project extends, but not the
4164                               --  current project.
4165
4166                               Extended := Unit.File_Names
4167                                 (Specification).Project;
4168
4169                               if Extended = Project then
4170                                  Error_Msg
4171                                    (Project, In_Tree,
4172                                     "cannot remove a source " &
4173                                     "of the same project",
4174                                     Location);
4175
4176                               elsif
4177                                 Project_Extends (Project, Extended, In_Tree)
4178                               then
4179                                  Unit.File_Names
4180                                    (Specification).Path := Slash;
4181                                  Unit.File_Names
4182                                    (Specification).Needs_Pragma := False;
4183                                  In_Tree.Units.Table (Index) :=
4184                                    Unit;
4185                                  Add_Forbidden_File_Name
4186                                    (Unit.File_Names (Specification).Name);
4187                                  exit;
4188
4189                               else
4190                                  Error_Msg
4191                                    (Project, In_Tree,
4192                                     "cannot remove a source from " &
4193                                     "another project",
4194                                     Location);
4195                               end if;
4196
4197                            elsif
4198                              Unit.File_Names (Body_Part).Name = Name
4199                            then
4200                               OK := True;
4201
4202                               --  Check that this is from a project that
4203                               --  the current project extends, but not the
4204                               --  current project.
4205
4206                               Extended := Unit.File_Names
4207                                 (Body_Part).Project;
4208
4209                               if Extended = Project then
4210                                  Error_Msg
4211                                    (Project, In_Tree,
4212                                     "cannot remove a source " &
4213                                     "of the same project",
4214                                     Location);
4215
4216                               elsif
4217                                 Project_Extends (Project, Extended, In_Tree)
4218                               then
4219                                  Unit.File_Names (Body_Part).Path := Slash;
4220                                  Unit.File_Names (Body_Part).Needs_Pragma
4221                                    := False;
4222                                  In_Tree.Units.Table (Index) :=
4223                                    Unit;
4224                                  Add_Forbidden_File_Name
4225                                    (Unit.File_Names (Body_Part).Name);
4226                                  exit;
4227                               end if;
4228
4229                            end if;
4230                         end loop;
4231
4232                         if not OK then
4233                            Err_Vars.Error_Msg_Name_1 := Name;
4234                            Error_Msg
4235                              (Project, In_Tree, "unknown file {", Location);
4236                         end if;
4237
4238                         Current := Element.Next;
4239                      end loop;
4240                   end;
4241                end if;
4242             end if;
4243          end;
4244       end if;
4245
4246       if Data.Other_Sources_Present then
4247
4248          --  Set Source_Present to False. It will be set back to True
4249          --  whenever a source is found.
4250
4251          Data.Other_Sources_Present := False;
4252          for Lang in Ada_Language_Index + 1 .. Last_Language_Index loop
4253
4254             --  For each language (other than Ada) in the project file
4255
4256             if Is_Present (Lang, Data, In_Tree) then
4257
4258                --  Reset the indication that there are sources of this
4259                --  language. It will be set back to True whenever we find a
4260                --  source of the language.
4261
4262                Set (Lang, False, Data, In_Tree);
4263
4264                --  First, get the source suffix for the language
4265
4266                Set (Suffix       => Suffix_For (Lang, Data.Naming, In_Tree),
4267                     For_Language => Lang,
4268                     In_Project   => Data,
4269                     In_Tree      => In_Tree);
4270
4271                --  Then, deal with the naming exceptions, if any
4272
4273                Source_Names.Reset;
4274
4275                declare
4276                   Naming_Exceptions : constant Variable_Value :=
4277                     Value_Of
4278                       (Index     => Language_Names.Table (Lang),
4279                        Src_Index => 0,
4280                        In_Array  => Data.Naming.Implementation_Exceptions,
4281                        In_Tree   => In_Tree);
4282                   Element_Id        : String_List_Id;
4283                   Element           : String_Element;
4284                   File_Id           : Name_Id;
4285                   Source_Found      : Boolean := False;
4286
4287                begin
4288                   --  If there are naming exceptions, look through them one
4289                   --  by one.
4290
4291                   if Naming_Exceptions /= Nil_Variable_Value then
4292                      Element_Id := Naming_Exceptions.Values;
4293
4294                      while Element_Id /= Nil_String loop
4295                         Element := In_Tree.String_Elements.Table
4296                                                           (Element_Id);
4297                         Get_Name_String (Element.Value);
4298                         Canonical_Case_File_Name
4299                           (Name_Buffer (1 .. Name_Len));
4300                         File_Id := Name_Find;
4301
4302                         --  Put each naming exception in the Source_Names
4303                         --  hash table, but if there are repetition, don't
4304                         --  bother after the first instance.
4305
4306                         if
4307                           Source_Names.Get (File_Id) = No_Name_Location
4308                         then
4309                            Source_Found := True;
4310                            Source_Names.Set
4311                              (File_Id,
4312                               (Name     => File_Id,
4313                                Location => Element.Location,
4314                                Found    => False));
4315                         end if;
4316
4317                         Element_Id := Element.Next;
4318                      end loop;
4319
4320                      --  If there is at least one naming exception, record
4321                      --  those that are found in the source directories.
4322
4323                      if Source_Found then
4324                         Record_Other_Sources
4325                           (Project           => Project,
4326                            In_Tree           => In_Tree,
4327                            Data              => Data,
4328                            Language          => Lang,
4329                            Naming_Exceptions => True);
4330                      end if;
4331
4332                   end if;
4333                end;
4334
4335                --  Now, check if a list of sources is declared either through
4336                --  a string list (attribute Source_Files) or a text file
4337                --  (attribute Source_List_File). If a source list is declared,
4338                --  we will consider only those naming exceptions that are
4339                --  on the list.
4340
4341                declare
4342                   Sources          : constant Variable_Value :=
4343                                        Util.Value_Of
4344                                          (Name_Source_Files,
4345                                           Data.Decl.Attributes,
4346                                           In_Tree);
4347
4348                   Source_List_File : constant Variable_Value :=
4349                                        Util.Value_Of
4350                                          (Name_Source_List_File,
4351                                           Data.Decl.Attributes,
4352                                           In_Tree);
4353
4354                begin
4355                   pragma Assert
4356                     (Sources.Kind = List,
4357                      "Source_Files is not a list");
4358
4359                   pragma Assert
4360                     (Source_List_File.Kind = Single,
4361                      "Source_List_File is not a single string");
4362
4363                   if not Sources.Default then
4364                      if not Source_List_File.Default then
4365                         Error_Msg
4366                           (Project, In_Tree,
4367                            "?both variables source_files and " &
4368                            "source_list_file are present",
4369                            Source_List_File.Location);
4370                      end if;
4371
4372                      --  Sources is a list of file names
4373
4374                      declare
4375                         Current  : String_List_Id := Sources.Values;
4376                         Element  : String_Element;
4377                         Location : Source_Ptr;
4378                         Name     : Name_Id;
4379
4380                      begin
4381                         Source_Names.Reset;
4382
4383                         --  Put all the sources in the Source_Names hash table
4384
4385                         while Current /= Nil_String loop
4386                            Element :=
4387                              In_Tree.String_Elements.Table
4388                                (Current);
4389                            Get_Name_String (Element.Value);
4390                            Canonical_Case_File_Name
4391                              (Name_Buffer (1 .. Name_Len));
4392                            Name := Name_Find;
4393
4394                            --  If the element has no location, then use the
4395                            --  location of Sources to report possible errors.
4396
4397                            if Element.Location = No_Location then
4398                               Location := Sources.Location;
4399                            else
4400                               Location := Element.Location;
4401                            end if;
4402
4403                            Source_Names.Set
4404                              (K => Name,
4405                               E =>
4406                                 (Name     => Name,
4407                                  Location => Location,
4408                                  Found    => False));
4409
4410                            Current := Element.Next;
4411                         end loop;
4412
4413                         --  And look for their directories
4414
4415                         Record_Other_Sources
4416                           (Project           => Project,
4417                            In_Tree           => In_Tree,
4418                            Data              => Data,
4419                            Language          => Lang,
4420                            Naming_Exceptions => False);
4421                      end;
4422
4423                      --  No source_files specified
4424
4425                      --  We check if Source_List_File has been specified
4426
4427                   elsif not Source_List_File.Default then
4428
4429                      --  Source_List_File is the name of the file
4430                      --  that contains the source file names
4431
4432                      declare
4433                         Source_File_Path_Name : constant String :=
4434                           Path_Name_Of
4435                             (Source_List_File.Value,
4436                              Data.Directory);
4437
4438                      begin
4439                         if Source_File_Path_Name'Length = 0 then
4440                            Err_Vars.Error_Msg_Name_1 :=
4441                              Source_List_File.Value;
4442                            Error_Msg
4443                              (Project, In_Tree,
4444                               "file with sources { does not exist",
4445                               Source_List_File.Location);
4446
4447                         else
4448                            --  Read the file, putting each source in the
4449                            --  Source_Names hash table.
4450
4451                            Get_Sources_From_File
4452                              (Source_File_Path_Name,
4453                               Source_List_File.Location,
4454                               Project, In_Tree);
4455
4456                            --  And look for their directories
4457
4458                            Record_Other_Sources
4459                              (Project           => Project,
4460                               In_Tree           => In_Tree,
4461                               Data              => Data,
4462                               Language          => Lang,
4463                               Naming_Exceptions => False);
4464                         end if;
4465                      end;
4466
4467                   --  Neither Source_Files nor Source_List_File was specified
4468
4469                   else
4470                      --  Find all the files that satisfy the naming scheme in
4471                      --  all the source directories. All the naming exceptions
4472                      --  that effectively exist are also part of the source
4473                      --  of this language.
4474
4475                      Find_Sources (Project, In_Tree, Data, Lang);
4476                   end if;
4477                end;
4478             end if;
4479          end loop;
4480       end if;
4481    end Look_For_Sources;
4482
4483    ------------------
4484    -- Path_Name_Of --
4485    ------------------
4486
4487    function Path_Name_Of
4488      (File_Name : Name_Id;
4489       Directory : Name_Id) return String
4490    is
4491       Result : String_Access;
4492
4493       The_Directory : constant String := Get_Name_String (Directory);
4494
4495    begin
4496       Get_Name_String (File_Name);
4497       Result := Locate_Regular_File
4498         (File_Name => Name_Buffer (1 .. Name_Len),
4499          Path      => The_Directory);
4500
4501       if Result = null then
4502          return "";
4503       else
4504          Canonical_Case_File_Name (Result.all);
4505          return Result.all;
4506       end if;
4507    end Path_Name_Of;
4508
4509    -------------------------------
4510    -- Prepare_Ada_Naming_Exceptions --
4511    -------------------------------
4512
4513    procedure Prepare_Ada_Naming_Exceptions
4514      (List    : Array_Element_Id;
4515       In_Tree : Project_Tree_Ref;
4516       Kind    : Spec_Or_Body)
4517    is
4518       Current : Array_Element_Id := List;
4519       Element : Array_Element;
4520
4521       Unit : Unit_Info;
4522
4523    begin
4524       --  Traverse the list
4525
4526       while Current /= No_Array_Element loop
4527          Element := In_Tree.Array_Elements.Table (Current);
4528
4529          if Element.Index /= No_Name then
4530             Unit :=
4531               (Kind => Kind,
4532                Unit => Element.Index,
4533                Next => No_Ada_Naming_Exception);
4534             Reverse_Ada_Naming_Exceptions.Set
4535               (Unit, (Element.Value.Value, Element.Value.Index));
4536             Unit.Next := Ada_Naming_Exceptions.Get (Element.Value.Value);
4537             Ada_Naming_Exception_Table.Increment_Last;
4538             Ada_Naming_Exception_Table.Table
4539               (Ada_Naming_Exception_Table.Last) := Unit;
4540             Ada_Naming_Exceptions.Set
4541               (Element.Value.Value, Ada_Naming_Exception_Table.Last);
4542          end if;
4543
4544          Current := Element.Next;
4545       end loop;
4546    end Prepare_Ada_Naming_Exceptions;
4547
4548    ---------------------
4549    -- Project_Extends --
4550    ---------------------
4551
4552    function Project_Extends
4553      (Extending : Project_Id;
4554       Extended  : Project_Id;
4555       In_Tree   : Project_Tree_Ref) return Boolean
4556    is
4557       Current : Project_Id := Extending;
4558    begin
4559       loop
4560          if Current = No_Project then
4561             return False;
4562
4563          elsif Current = Extended then
4564             return True;
4565          end if;
4566
4567          Current := In_Tree.Projects.Table (Current).Extends;
4568       end loop;
4569    end Project_Extends;
4570
4571    -----------------------
4572    -- Record_Ada_Source --
4573    -----------------------
4574
4575    procedure Record_Ada_Source
4576      (File_Name       : Name_Id;
4577       Path_Name       : Name_Id;
4578       Project         : Project_Id;
4579       In_Tree         : Project_Tree_Ref;
4580       Data            : in out Project_Data;
4581       Location        : Source_Ptr;
4582       Current_Source  : in out String_List_Id;
4583       Source_Recorded : in out Boolean;
4584       Follow_Links    : Boolean)
4585    is
4586       Canonical_File_Name : Name_Id;
4587       Canonical_Path_Name : Name_Id;
4588
4589       Exception_Id : Ada_Naming_Exception_Id;
4590       Unit_Name    : Name_Id;
4591       Unit_Kind    : Spec_Or_Body;
4592       Unit_Index   : Int := 0;
4593       Info         : Unit_Info;
4594       Name_Index   : Name_And_Index;
4595       Needs_Pragma : Boolean;
4596
4597       The_Location    : Source_Ptr              := Location;
4598       Previous_Source : constant String_List_Id := Current_Source;
4599       Except_Name     : Name_And_Index          := No_Name_And_Index;
4600
4601       Unit_Prj : Unit_Project;
4602
4603       File_Name_Recorded : Boolean := False;
4604
4605    begin
4606       Get_Name_String (File_Name);
4607       Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
4608       Canonical_File_Name := Name_Find;
4609
4610       declare
4611          Canonical_Path : constant String :=
4612                             Normalize_Pathname
4613                               (Get_Name_String (Path_Name),
4614                                Resolve_Links => Follow_Links,
4615                                Case_Sensitive => False);
4616       begin
4617          Name_Len := 0;
4618          Add_Str_To_Name_Buffer (Canonical_Path);
4619          Canonical_Path_Name := Name_Find;
4620       end;
4621
4622       --  Find out the unit name, the unit kind and if it needs
4623       --  a specific SFN pragma.
4624
4625       Get_Unit
4626         (Canonical_File_Name => Canonical_File_Name,
4627          Naming              => Data.Naming,
4628          Exception_Id        => Exception_Id,
4629          Unit_Name           => Unit_Name,
4630          Unit_Kind           => Unit_Kind,
4631          Needs_Pragma        => Needs_Pragma);
4632
4633       if Exception_Id = No_Ada_Naming_Exception and then
4634         Unit_Name = No_Name
4635       then
4636          if Current_Verbosity = High then
4637             Write_Str  ("   """);
4638             Write_Str  (Get_Name_String (Canonical_File_Name));
4639             Write_Line (""" is not a valid source file name (ignored).");
4640          end if;
4641
4642       else
4643          --  Check to see if the source has been hidden by an exception,
4644          --  but only if it is not an exception.
4645
4646          if not Needs_Pragma then
4647             Except_Name :=
4648               Reverse_Ada_Naming_Exceptions.Get
4649                 ((Unit_Kind, Unit_Name, No_Ada_Naming_Exception));
4650
4651             if Except_Name /= No_Name_And_Index then
4652                if Current_Verbosity = High then
4653                   Write_Str  ("   """);
4654                   Write_Str  (Get_Name_String (Canonical_File_Name));
4655                   Write_Str  (""" contains a unit that is found in """);
4656                   Write_Str  (Get_Name_String (Except_Name.Name));
4657                   Write_Line (""" (ignored).");
4658                end if;
4659
4660                --  The file is not included in the source of the project,
4661                --  because it is hidden by the exception.
4662                --  So, there is nothing else to do.
4663
4664                return;
4665             end if;
4666          end if;
4667
4668          loop
4669             if Exception_Id /= No_Ada_Naming_Exception then
4670                Info := Ada_Naming_Exception_Table.Table (Exception_Id);
4671                Exception_Id := Info.Next;
4672                Info.Next := No_Ada_Naming_Exception;
4673                Name_Index := Reverse_Ada_Naming_Exceptions.Get (Info);
4674
4675                Unit_Name  := Info.Unit;
4676                Unit_Index := Name_Index.Index;
4677                Unit_Kind  := Info.Kind;
4678             end if;
4679
4680             --  Put the file name in the list of sources of the project
4681
4682             if not File_Name_Recorded then
4683                String_Element_Table.Increment_Last
4684                  (In_Tree.String_Elements);
4685                In_Tree.String_Elements.Table
4686                  (String_Element_Table.Last
4687                    (In_Tree.String_Elements)) :=
4688                  (Value         => Canonical_File_Name,
4689                   Display_Value => File_Name,
4690                   Location      => No_Location,
4691                   Flag          => False,
4692                   Next          => Nil_String,
4693                   Index         => Unit_Index);
4694             end if;
4695
4696             if Current_Source = Nil_String then
4697                Data.Sources := String_Element_Table.Last
4698                                  (In_Tree.String_Elements);
4699             else
4700                In_Tree.String_Elements.Table
4701                  (Current_Source).Next :=
4702                  String_Element_Table.Last
4703                    (In_Tree.String_Elements);
4704             end if;
4705
4706             Current_Source := String_Element_Table.Last
4707                                 (In_Tree.String_Elements);
4708
4709             --  Put the unit in unit list
4710
4711             declare
4712                The_Unit      : Unit_Id :=
4713                  Units_Htable.Get (In_Tree.Units_HT, Unit_Name);
4714                The_Unit_Data : Unit_Data;
4715
4716             begin
4717                if Current_Verbosity = High then
4718                   Write_Str  ("Putting ");
4719                   Write_Str  (Get_Name_String (Unit_Name));
4720                   Write_Line (" in the unit list.");
4721                end if;
4722
4723                --  The unit is already in the list, but may be it is
4724                --  only the other unit kind (spec or body), or what is
4725                --  in the unit list is a unit of a project we are extending.
4726
4727                if The_Unit /= No_Unit then
4728                   The_Unit_Data := In_Tree.Units.Table (The_Unit);
4729
4730                   if The_Unit_Data.File_Names (Unit_Kind).Name = No_Name
4731                     or else Project_Extends
4732                       (Data.Extends,
4733                        The_Unit_Data.File_Names (Unit_Kind).Project,
4734                        In_Tree)
4735                   then
4736                      if The_Unit_Data.File_Names (Unit_Kind).Path = Slash then
4737                         Remove_Forbidden_File_Name
4738                           (The_Unit_Data.File_Names (Unit_Kind).Name);
4739                      end if;
4740
4741                      --  Record the file name in the hash table Files_Htable
4742
4743                      Unit_Prj := (Unit => The_Unit, Project => Project);
4744                      Files_Htable.Set
4745                        (In_Tree.Files_HT,
4746                         Canonical_File_Name,
4747                         Unit_Prj);
4748
4749                      The_Unit_Data.File_Names (Unit_Kind) :=
4750                        (Name         => Canonical_File_Name,
4751                         Index        => Unit_Index,
4752                         Display_Name => File_Name,
4753                         Path         => Canonical_Path_Name,
4754                         Display_Path => Path_Name,
4755                         Project      => Project,
4756                         Needs_Pragma => Needs_Pragma);
4757                      In_Tree.Units.Table (The_Unit) :=
4758                        The_Unit_Data;
4759                      Source_Recorded := True;
4760
4761                   elsif The_Unit_Data.File_Names (Unit_Kind).Project = Project
4762                     and then (Data.Known_Order_Of_Source_Dirs or else
4763                               The_Unit_Data.File_Names (Unit_Kind).Path =
4764                                 Canonical_Path_Name)
4765                   then
4766                      if Previous_Source = Nil_String then
4767                         Data.Sources := Nil_String;
4768                      else
4769                         In_Tree.String_Elements.Table
4770                           (Previous_Source).Next := Nil_String;
4771                         String_Element_Table.Decrement_Last
4772                           (In_Tree.String_Elements);
4773                      end if;
4774
4775                      Current_Source := Previous_Source;
4776
4777                   else
4778                      --  It is an error to have two units with the same name
4779                      --  and the same kind (spec or body).
4780
4781                      if The_Location = No_Location then
4782                         The_Location :=
4783                           In_Tree.Projects.Table
4784                             (Project).Location;
4785                      end if;
4786
4787                      Err_Vars.Error_Msg_Name_1 := Unit_Name;
4788                      Error_Msg
4789                        (Project, In_Tree, "duplicate source {", The_Location);
4790
4791                      Err_Vars.Error_Msg_Name_1 :=
4792                        In_Tree.Projects.Table
4793                          (The_Unit_Data.File_Names (Unit_Kind).Project).Name;
4794                      Err_Vars.Error_Msg_Name_2 :=
4795                        The_Unit_Data.File_Names (Unit_Kind).Path;
4796                      Error_Msg
4797                        (Project, In_Tree,
4798                         "\   project file {, {", The_Location);
4799
4800                      Err_Vars.Error_Msg_Name_1 :=
4801                        In_Tree.Projects.Table (Project).Name;
4802                      Err_Vars.Error_Msg_Name_2 := Canonical_Path_Name;
4803                      Error_Msg
4804                        (Project, In_Tree,
4805                         "\   project file {, {", The_Location);
4806                   end if;
4807
4808                --  It is a new unit, create a new record
4809
4810                else
4811                   --  First, check if there is no other unit with this file
4812                   --  name in another project. If it is, report an error.
4813                   --  Of course, we do that only for the first unit in the
4814                   --  source file.
4815
4816                   Unit_Prj := Files_Htable.Get
4817                     (In_Tree.Files_HT, Canonical_File_Name);
4818
4819                   if not File_Name_Recorded and then
4820                     Unit_Prj /= No_Unit_Project
4821                   then
4822                      Error_Msg_Name_1 := File_Name;
4823                      Error_Msg_Name_2 :=
4824                        In_Tree.Projects.Table
4825                          (Unit_Prj.Project).Name;
4826                      Error_Msg
4827                        (Project, In_Tree,
4828                         "{ is already a source of project {",
4829                         Location);
4830
4831                   else
4832                      Unit_Table.Increment_Last (In_Tree.Units);
4833                      The_Unit := Unit_Table.Last (In_Tree.Units);
4834                      Units_Htable.Set
4835                        (In_Tree.Units_HT, Unit_Name, The_Unit);
4836                      Unit_Prj := (Unit => The_Unit, Project => Project);
4837                      Files_Htable.Set
4838                        (In_Tree.Files_HT,
4839                         Canonical_File_Name,
4840                         Unit_Prj);
4841                      The_Unit_Data.Name := Unit_Name;
4842                      The_Unit_Data.File_Names (Unit_Kind) :=
4843                        (Name         => Canonical_File_Name,
4844                         Index        => Unit_Index,
4845                         Display_Name => File_Name,
4846                         Path         => Canonical_Path_Name,
4847                         Display_Path => Path_Name,
4848                         Project      => Project,
4849                         Needs_Pragma => Needs_Pragma);
4850                      In_Tree.Units.Table (The_Unit) :=
4851                        The_Unit_Data;
4852                      Source_Recorded := True;
4853                   end if;
4854                end if;
4855             end;
4856
4857             exit when Exception_Id = No_Ada_Naming_Exception;
4858             File_Name_Recorded := True;
4859          end loop;
4860       end if;
4861    end Record_Ada_Source;
4862
4863    --------------------------
4864    -- Record_Other_Sources --
4865    --------------------------
4866
4867    procedure Record_Other_Sources
4868      (Project           : Project_Id;
4869       In_Tree           : Project_Tree_Ref;
4870       Data              : in out Project_Data;
4871       Language          : Language_Index;
4872       Naming_Exceptions : Boolean)
4873    is
4874       Source_Dir : String_List_Id := Data.Source_Dirs;
4875       Element    : String_Element;
4876       Path       : Name_Id;
4877
4878       Dir            : Dir_Type;
4879       Canonical_Name : Name_Id;
4880
4881       Name_Str : String (1 .. 1_024);
4882       Last     : Natural := 0;
4883       NL       : Name_Location;
4884
4885       First_Error : Boolean := True;
4886
4887       Suffix : constant String := Body_Suffix_Of (Language, Data, In_Tree);
4888
4889    begin
4890       while Source_Dir /= Nil_String loop
4891          Element := In_Tree.String_Elements.Table (Source_Dir);
4892
4893          declare
4894             Dir_Path : constant String := Get_Name_String (Element.Value);
4895
4896          begin
4897             if Current_Verbosity = High then
4898                Write_Str ("checking directory """);
4899                Write_Str (Dir_Path);
4900                Write_Str (""" for ");
4901
4902                if Naming_Exceptions then
4903                   Write_Str ("naming exceptions");
4904
4905                else
4906                   Write_Str ("sources");
4907                end if;
4908
4909                Write_Str (" of Language ");
4910                Display_Language_Name (Language);
4911             end if;
4912
4913             Open (Dir, Dir_Path);
4914
4915             loop
4916                Read (Dir, Name_Str, Last);
4917                exit when Last = 0;
4918
4919                if Is_Regular_File
4920                  (Dir_Path & Directory_Separator & Name_Str (1 .. Last))
4921                then
4922                   Name_Len := Last;
4923                   Name_Buffer (1 .. Name_Len) := Name_Str (1 .. Last);
4924                   Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
4925                   Canonical_Name := Name_Find;
4926                   NL := Source_Names.Get (Canonical_Name);
4927
4928                   if NL /= No_Name_Location then
4929                      if NL.Found then
4930                         if not Data.Known_Order_Of_Source_Dirs then
4931                            Error_Msg_Name_1 := Canonical_Name;
4932                            Error_Msg
4933                              (Project, In_Tree,
4934                               "{ is found in several source directories",
4935                               NL.Location);
4936                         end if;
4937
4938                      else
4939                         NL.Found := True;
4940                         Source_Names.Set (Canonical_Name, NL);
4941                         Name_Len := Dir_Path'Length;
4942                         Name_Buffer (1 .. Name_Len) := Dir_Path;
4943                         Add_Char_To_Name_Buffer (Directory_Separator);
4944                         Add_Str_To_Name_Buffer (Name_Str (1 .. Last));
4945                         Path := Name_Find;
4946
4947                         Check_For_Source
4948                           (File_Name        => Canonical_Name,
4949                            Path_Name        => Path,
4950                            Project          => Project,
4951                            In_Tree          => In_Tree,
4952                            Data             => Data,
4953                            Location         => NL.Location,
4954                            Language         => Language,
4955                            Suffix           => Suffix,
4956                            Naming_Exception => Naming_Exceptions);
4957                      end if;
4958                   end if;
4959                end if;
4960             end loop;
4961
4962             Close (Dir);
4963          end;
4964
4965          Source_Dir := Element.Next;
4966       end loop;
4967
4968       if not Naming_Exceptions then
4969          NL := Source_Names.Get_First;
4970
4971          --  It is an error if a source file name in a source list or
4972          --  in a source list file is not found.
4973
4974          while NL /= No_Name_Location loop
4975             if not NL.Found then
4976                Err_Vars.Error_Msg_Name_1 := NL.Name;
4977
4978                if First_Error then
4979                   Error_Msg
4980                     (Project, In_Tree,
4981                      "source file { cannot be found",
4982                      NL.Location);
4983                   First_Error := False;
4984
4985                else
4986                   Error_Msg
4987                     (Project, In_Tree,
4988                      "\source file { cannot be found",
4989                      NL.Location);
4990                end if;
4991             end if;
4992
4993             NL := Source_Names.Get_Next;
4994          end loop;
4995
4996          --  Any naming exception of this language that is not in a list
4997          --  of sources must be removed.
4998
4999          declare
5000             Source_Id : Other_Source_Id := Data.First_Other_Source;
5001             Prev_Id   : Other_Source_Id := No_Other_Source;
5002             Source    : Other_Source;
5003
5004          begin
5005             while Source_Id /= No_Other_Source loop
5006                Source := In_Tree.Other_Sources.Table (Source_Id);
5007
5008                if Source.Language = Language
5009                  and then Source.Naming_Exception
5010                then
5011                   if Current_Verbosity = High then
5012                      Write_Str ("Naming exception """);
5013                      Write_Str (Get_Name_String (Source.File_Name));
5014                      Write_Str (""" is not in the list of sources,");
5015                      Write_Line (" so it is removed.");
5016                   end if;
5017
5018                   if Prev_Id = No_Other_Source then
5019                      Data.First_Other_Source := Source.Next;
5020
5021                   else
5022                      In_Tree.Other_Sources.Table
5023                        (Prev_Id).Next := Source.Next;
5024                   end if;
5025
5026                   Source_Id := Source.Next;
5027
5028                   if Source_Id = No_Other_Source then
5029                      Data.Last_Other_Source := Prev_Id;
5030                   end if;
5031
5032                else
5033                   Prev_Id := Source_Id;
5034                   Source_Id := Source.Next;
5035                end if;
5036             end loop;
5037          end;
5038       end if;
5039    end Record_Other_Sources;
5040
5041    ----------------------
5042    -- Show_Source_Dirs --
5043    ----------------------
5044
5045    procedure Show_Source_Dirs
5046      (Project : Project_Id;
5047       In_Tree : Project_Tree_Ref)
5048    is
5049       Current : String_List_Id;
5050       Element : String_Element;
5051
5052    begin
5053       Write_Line ("Source_Dirs:");
5054
5055       Current := In_Tree.Projects.Table (Project).Source_Dirs;
5056       while Current /= Nil_String loop
5057          Element := In_Tree.String_Elements.Table (Current);
5058          Write_Str  ("   ");
5059          Write_Line (Get_Name_String (Element.Value));
5060          Current := Element.Next;
5061       end loop;
5062
5063       Write_Line ("end Source_Dirs.");
5064    end Show_Source_Dirs;
5065
5066    ----------------
5067    -- Suffix_For --
5068    ----------------
5069
5070    function Suffix_For
5071      (Language : Language_Index;
5072       Naming   : Naming_Data;
5073       In_Tree  : Project_Tree_Ref) return Name_Id
5074    is
5075       Suffix : constant Variable_Value :=
5076         Value_Of
5077           (Index     => Language_Names.Table (Language),
5078            Src_Index => 0,
5079            In_Array  => Naming.Body_Suffix,
5080            In_Tree   => In_Tree);
5081    begin
5082       --  If no suffix for this language in package Naming, use the default
5083
5084       if Suffix = Nil_Variable_Value then
5085          Name_Len := 0;
5086
5087          case Language is
5088             when Ada_Language_Index =>
5089                Add_Str_To_Name_Buffer (".adb");
5090
5091             when C_Language_Index =>
5092                Add_Str_To_Name_Buffer (".c");
5093
5094             when C_Plus_Plus_Language_Index =>
5095                Add_Str_To_Name_Buffer (".cpp");
5096
5097             when others =>
5098                return No_Name;
5099          end case;
5100
5101       --  Otherwise use the one specified
5102
5103       else
5104          Get_Name_String (Suffix.Value);
5105       end if;
5106
5107       Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
5108       return Name_Find;
5109    end Suffix_For;
5110
5111    -------------------------
5112    -- Warn_If_Not_Sources --
5113    -------------------------
5114
5115    --  comments needed in this body ???
5116
5117    procedure Warn_If_Not_Sources
5118      (Project     : Project_Id;
5119       In_Tree     : Project_Tree_Ref;
5120       Conventions : Array_Element_Id;
5121       Specs       : Boolean;
5122       Extending   : Boolean)
5123    is
5124       Conv          : Array_Element_Id := Conventions;
5125       Unit          : Name_Id;
5126       The_Unit_Id   : Unit_Id;
5127       The_Unit_Data : Unit_Data;
5128       Location      : Source_Ptr;
5129
5130    begin
5131       while Conv /= No_Array_Element loop
5132          Unit := In_Tree.Array_Elements.Table (Conv).Index;
5133          Error_Msg_Name_1 := Unit;
5134          Get_Name_String (Unit);
5135          To_Lower (Name_Buffer (1 .. Name_Len));
5136          Unit := Name_Find;
5137          The_Unit_Id := Units_Htable.Get
5138            (In_Tree.Units_HT, Unit);
5139          Location := In_Tree.Array_Elements.Table
5140                                             (Conv).Value.Location;
5141
5142          if The_Unit_Id = No_Unit then
5143             Error_Msg
5144               (Project, In_Tree,
5145                "?unknown unit {",
5146                Location);
5147
5148          else
5149             The_Unit_Data := In_Tree.Units.Table (The_Unit_Id);
5150
5151             if Specs then
5152                if not Check_Project
5153                  (The_Unit_Data.File_Names (Specification).Project,
5154                   Project, In_Tree, Extending)
5155                then
5156                   Error_Msg
5157                     (Project, In_Tree,
5158                      "?unit{ has no spec in this project",
5159                      Location);
5160                end if;
5161
5162             else
5163                if not Check_Project
5164                  (The_Unit_Data.File_Names (Body_Part).Project,
5165                   Project, In_Tree, Extending)
5166                then
5167                   Error_Msg
5168                     (Project, In_Tree,
5169                      "?unit{ has no body in this project",
5170                      Location);
5171                end if;
5172             end if;
5173          end if;
5174
5175          Conv := In_Tree.Array_Elements.Table (Conv).Next;
5176       end loop;
5177    end Warn_If_Not_Sources;
5178
5179 end Prj.Nmsc;