OSDN Git Service

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