1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                                  P R J                                   --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2001-2003 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--  The following package declares the data types for GNAT project.
28--  These data types may be used by GNAT Project-aware tools.
29
30--  Children of these package implements various services on these data types.
31--  See in particular Prj.Pars and Prj.Env.
32
33with Casing; use Casing;
34with Scans;  use Scans;
35with Table;
36with Types;  use Types;
37
38with GNAT.OS_Lib; use GNAT.OS_Lib;
39
40package Prj is
41
42   Empty_Name : Name_Id;
43   --  Name_Id for an empty name (no characters).
44   --  Initialized by procedure Initialize.
45
46   All_Packages : constant String_List_Access := null;
47   --  Default value of parameter Packages of procedures Parse, in Prj.Pars and
48   --  Prj.Part, indicating that all packages should be checked.
49
50   Virtual_Prefix : constant String := "v$";
51   --  The prefix for virtual extending projects. Because of the '$', which is
52   --  normally forbidden for project names, there cannot be any name clash.
53
54   Project_File_Extension : String := ".gpr";
55   --  The standard project file name extension.
56   --  It is not a constant, because Canonical_Case_File_Name is called
57   --  on this variable in the body of Prj.
58
59   Default_Ada_Spec_Suffix : Name_Id;
60   --  The Name_Id for the standard GNAT suffix for Ada spec source file
61   --  name ".ads". Initialized by Prj.Initialize.
62
63   Default_Ada_Body_Suffix : Name_Id;
64   --  The Name_Id for the standard GNAT suffix for Ada body source file
65   --  name ".adb". Initialized by Prj.Initialize.
66
67   Slash : Name_Id;
68   --  "/", used as the path of locally removed files
69
70   type Verbosity is (Default, Medium, High);
71   --  Verbosity when parsing GNAT Project Files
72   --    Default is default (very quiet, if no errors).
73   --    Medium is more verbose.
74   --    High is extremely verbose.
75
76   type Lib_Kind is (Static, Dynamic, Relocatable);
77
78   type Policy is (Autonomous, Compliant, Controlled);
79   --  See explaination about this type in package Symbol
80
81   type Symbol_Record is record
82      Symbol_File   : Name_Id := No_Name;
83      Reference     : Name_Id := No_Name;
84      Symbol_Policy : Policy  := Autonomous;
85   end record;
86   --  Type to keep the symbol data to be used when building a shared library
87
88   No_Symbols : Symbol_Record :=
89     (Symbol_File   => No_Name,
90      Reference     => No_Name,
91      Symbol_Policy => Autonomous);
92
93   function Empty_String return Name_Id;
94
95   type Project_Id is new Nat;
96   No_Project : constant Project_Id := 0;
97   --  Id of a Project File
98
99   type String_List_Id is new Nat;
100   Nil_String : constant String_List_Id := 0;
101   type String_Element is record
102      Value    : Name_Id        := No_Name;
103      Display_Value : Name_Id   := No_Name;
104      Location : Source_Ptr     := No_Location;
105      Flag     : Boolean        := False;
106      Next     : String_List_Id := Nil_String;
107   end record;
108   --  To hold values for string list variables and array elements.
109   --  Component Flag may be used for various purposes. For source
110   --  directories, it indicates if the directory contains Ada source(s).
111
112   package String_Elements is new Table.Table
113     (Table_Component_Type => String_Element,
114      Table_Index_Type     => String_List_Id,
115      Table_Low_Bound      => 1,
116      Table_Initial        => 200,
117      Table_Increment      => 100,
118      Table_Name           => "Prj.String_Elements");
119   --  The table for string elements in string lists
120
121   type Variable_Kind is (Undefined, List, Single);
122   --  Different kinds of variables
123
124   Ignored : constant Variable_Kind := Single;
125   --  Used to indicate that a package declaration must be ignored
126   --  while processing the project tree (unknown package name).
127
128   type Variable_Value (Kind : Variable_Kind := Undefined) is record
129      Project  : Project_Id := No_Project;
130      Location : Source_Ptr := No_Location;
131      Default  : Boolean    := False;
132      case Kind is
133         when Undefined =>
134            null;
135         when List =>
136            Values : String_List_Id := Nil_String;
137         when Single =>
138            Value : Name_Id := No_Name;
139      end case;
140   end record;
141   --  Values for variables and array elements.
142   --  Default is True if the current value is the default one for the variable
143
144   Nil_Variable_Value : constant Variable_Value :=
145     (Project  => No_Project,
146      Kind     => Undefined,
147      Location => No_Location,
148      Default  => False);
149   --  Value of a non existing variable or array element
150
151   type Variable_Id is new Nat;
152   No_Variable : constant Variable_Id := 0;
153   type Variable is record
154      Next     : Variable_Id := No_Variable;
155      Name     : Name_Id;
156      Value    : Variable_Value;
157   end record;
158   --  To hold the list of variables in a project file and in packages
159
160   package Variable_Elements is new Table.Table
161     (Table_Component_Type => Variable,
162      Table_Index_Type     => Variable_Id,
163      Table_Low_Bound      => 1,
164      Table_Initial        => 200,
165      Table_Increment      => 100,
166      Table_Name           => "Prj.Variable_Elements");
167   --  The table of variable in list of variables
168
169   type Array_Element_Id is new Nat;
170   No_Array_Element : constant Array_Element_Id := 0;
171   type Array_Element is record
172      Index                : Name_Id;
173      Index_Case_Sensitive : Boolean := True;
174      Value                : Variable_Value;
175      Next                 : Array_Element_Id := No_Array_Element;
176   end record;
177   --  Each Array_Element represents an array element and is linked (Next)
178   --  to the next array element, if any, in the array.
179
180   package Array_Elements is new Table.Table
181     (Table_Component_Type => Array_Element,
182      Table_Index_Type     => Array_Element_Id,
183      Table_Low_Bound      => 1,
184      Table_Initial        => 200,
185      Table_Increment      => 100,
186      Table_Name           => "Prj.Array_Elements");
187   --  The table that contains all array elements
188
189   type Array_Id is new Nat;
190   No_Array : constant Array_Id := 0;
191   type Array_Data is record
192      Name  : Name_Id          := No_Name;
193      Value : Array_Element_Id := No_Array_Element;
194      Next  : Array_Id         := No_Array;
195   end record;
196   --  Each Array_Data value represents an array.
197   --  Value is the id of the first element.
198   --  Next is the id of the next array in the project file or package.
199
200   package Arrays is new Table.Table
201     (Table_Component_Type => Array_Data,
202      Table_Index_Type     => Array_Id,
203      Table_Low_Bound      => 1,
204      Table_Initial        => 200,
205      Table_Increment      => 100,
206      Table_Name           => "Prj.Arrays");
207   --  The table that contains all arrays
208
209   type Package_Id is new Nat;
210   No_Package : constant Package_Id := 0;
211   type Declarations is record
212      Variables  : Variable_Id := No_Variable;
213      Attributes : Variable_Id := No_Variable;
214      Arrays     : Array_Id    := No_Array;
215      Packages   : Package_Id  := No_Package;
216   end record;
217
218   No_Declarations : constant Declarations :=
219     (Variables  => No_Variable,
220      Attributes => No_Variable,
221      Arrays     => No_Array,
222      Packages   => No_Package);
223   --  Declarations. Used in project structures and packages (what for???)
224
225   type Package_Element is record
226      Name   : Name_Id      := No_Name;
227      Decl   : Declarations := No_Declarations;
228      Parent : Package_Id   := No_Package;
229      Next   : Package_Id   := No_Package;
230   end record;
231   --  A package. Includes declarations that may include other packages.
232
233   package Packages is new Table.Table
234     (Table_Component_Type => Package_Element,
235      Table_Index_Type     => Package_Id,
236      Table_Low_Bound      => 1,
237      Table_Initial        => 100,
238      Table_Increment      => 100,
239      Table_Name           => "Prj.Packages");
240   --  The table that contains all packages.
241
242   function Image (Casing : Casing_Type) return String;
243   --  Similar to 'Image (but avoid use of this attribute in compiler)
244
245   function Value (Image : String) return Casing_Type;
246   --  Similar to 'Value (but avoid use of this attribute in compiler)
247   --  Raises Constraint_Error if not a Casing_Type image.
248
249   --  The following record contains data for a naming scheme
250
251   type Naming_Data is record
252      Current_Language : Name_Id := No_Name;
253      --  The programming language being currently considered
254
255      Dot_Replacement : Name_Id := No_Name;
256      --  The string to replace '.' in the source file name (for Ada).
257
258      Dot_Repl_Loc : Source_Ptr := No_Location;
259      --  The position in the project file source where
260      --  Dot_Replacement is defined.
261
262      Casing : Casing_Type := All_Lower_Case;
263      --  The casing of the source file name (for Ada).
264
265      Spec_Suffix : Array_Element_Id := No_Array_Element;
266      --  The string to append to the unit name for the
267      --  source file name of a spec.
268      --  Indexed by the programming language.
269
270      Current_Spec_Suffix : Name_Id := No_Name;
271      --  The "spec" suffix of the current programming language
272
273      Spec_Suffix_Loc : Source_Ptr := No_Location;
274      --  The position in the project file source where
275      --  Current_Spec_Suffix is defined.
276
277      Body_Suffix : Array_Element_Id := No_Array_Element;
278      --  The string to append to the unit name for the
279      --  source file name of a body.
280      --  Indexed by the programming language.
281
282      Current_Body_Suffix : Name_Id := No_Name;
283      --  The "body" suffix of the current programming language
284
285      Body_Suffix_Loc : Source_Ptr := No_Location;
286      --  The position in the project file source where
287      --  Current_Body_Suffix is defined.
288
289      Separate_Suffix : Name_Id := No_Name;
290      --  The string to append to the unit name for the
291      --  source file name of an Ada subunit.
292
293      Sep_Suffix_Loc : Source_Ptr := No_Location;
294      --  The position in the project file source where
295      --  Separate_Suffix is defined.
296
297      Specs : Array_Element_Id := No_Array_Element;
298      --  An associative array mapping individual specs
299      --  to source file names. Specific to Ada.
300
301      Bodies : Array_Element_Id := No_Array_Element;
302      --  An associative array mapping individual bodies
303      --  to source file names. Specific to Ada.
304
305      Specification_Exceptions : Array_Element_Id := No_Array_Element;
306      --  An associative array listing spec file names that don't have the
307      --  spec suffix. Not used by Ada. Indexed by the programming language
308      --  name.
309
310      Implementation_Exceptions : Array_Element_Id := No_Array_Element;
311      --  An associative array listing body file names that don't have the
312      --  body suffix. Not used by Ada. Indexed by the programming language
313      --  name.
314
315   end record;
316
317   function Standard_Naming_Data return Naming_Data;
318   pragma Inline (Standard_Naming_Data);
319   --  The standard GNAT naming scheme.
320
321   function Same_Naming_Scheme
322     (Left, Right : Naming_Data)
323      return        Boolean;
324   --  Returns True if Left and Right are the same naming scheme
325   --  not considering Specs and Bodies.
326
327   type Project_List is new Nat;
328   Empty_Project_List : constant Project_List := 0;
329   --  A list of project files.
330
331   type Project_Element is record
332      Project : Project_Id   := No_Project;
333      Next    : Project_List := Empty_Project_List;
334   end record;
335   --  Element in a list of project file.
336   --  Next is the id of the next project file in the list.
337
338   package Project_Lists is new Table.Table
339     (Table_Component_Type => Project_Element,
340      Table_Index_Type     => Project_List,
341      Table_Low_Bound      => 1,
342      Table_Initial        => 100,
343      Table_Increment      => 100,
344      Table_Name           => "Prj.Project_Lists");
345   --  The table that contains the lists of project files.
346
347   --  The following record describes a project file representation
348
349   type Project_Data is record
350      First_Referred_By  : Project_Id := No_Project;
351      --  The project, if any, that was the first to be known
352      --  as importing or extending this project.
353      --  Set by Prj.Proc.Process.
354
355      Name : Name_Id := No_Name;
356      --  The name of the project.
357      --  Set by Prj.Proc.Process.
358
359      Path_Name : Name_Id := No_Name;
360      --  The path name of the project file.
361      --  Set by Prj.Proc.Process.
362
363      Virtual : Boolean := False;
364      --  True for virtual extending projects
365
366      Display_Path_Name : Name_Id := No_Name;
367
368      Location : Source_Ptr := No_Location;
369      --  The location in the project file source of the
370      --  reserved word project.
371      --  Set by Prj.Proc.Process.
372
373      Mains : String_List_Id := Nil_String;
374      --  The list of mains as specified by attribute Main.
375      --  Set by Prj.Nmsc.Ada_Check.
376
377      Directory : Name_Id := No_Name;
378      --  The directory where the project file resides.
379      --  Set by Prj.Proc.Process.
380
381      Display_Directory : Name_Id := No_Name;
382
383      Dir_Path : String_Access;
384      --  Same as Directory, but as an access to String.
385      --  Set by Make.Compile_Sources.Collect_Arguments_And_Compile.
386
387      Library : Boolean := False;
388      --  True if this is a library project.
389      --  Set by Prj.Nmsc.Language_Independent_Check.
390
391      Library_Dir : Name_Id := No_Name;
392      --  If a library project, directory where resides the library
393      --  Set by Prj.Nmsc.Language_Independent_Check.
394
395      Display_Library_Dir : Name_Id := No_Name;
396
397      Library_Src_Dir : Name_Id := No_Name;
398      --  If a library project, directory where the sources and the ALI files
399      --  of the library are copied. By default, if attribute Library_Src_Dir
400      --  is not specified, sources are not copied anywhere and ALI files are
401      --  copied in the Library Directory.
402      --  Set by Prj.Nmsc.Language_Independent_Check.
403
404      Display_Library_Src_Dir : Name_Id := No_Name;
405
406      Library_Name : Name_Id := No_Name;
407      --  If a library project, name of the library
408      --  Set by Prj.Nmsc.Language_Independent_Check.
409
410      Library_Kind : Lib_Kind := Static;
411      --  If a library project, kind of library
412      --  Set by Prj.Nmsc.Language_Independent_Check.
413
414      Lib_Internal_Name : Name_Id := No_Name;
415      --  If a library project, internal name store inside the library
416      --  Set by Prj.Nmsc.Language_Independent_Check.
417
418      Lib_Elaboration : Boolean := False;
419      --  If a library project, indicate if <lib>init and <lib>final
420      --  procedures need to be defined.
421      --  Set by Prj.Nmsc.Language_Independent_Check.
422
423      Standalone_Library : Boolean := False;
424      --  Indicate that this is a Standalone Library Project File.
425      --  Set by Prj.Nmsc.Ada_Check.
426
427      Lib_Interface_ALIs : String_List_Id := Nil_String;
428      --  For Standalone Library Project Files, indicate the list
429      --  of Interface ALI files.
430      --  Set by Prj.Nmsc.Ada_Check.
431
432      Lib_Auto_Init  : Boolean := False;
433      --  For non static Standalone Library Project Files, indicate if
434      --  the library initialisation should be automatic.
435
436      Symbol_Data : Symbol_Record := No_Symbols;
437      --  Symbol file name, reference symbol file name, symbol policy
438
439      Sources_Present : Boolean := True;
440      --  A flag that indicates if there are sources in this project file.
441      --  There are no sources if 1) Source_Dirs is specified as an
442      --  empty list, 2) Source_Files is specified as an empty list, or
443      --  3) the current language is not in the list of the specified
444      --  Languages.
445
446      Sources : String_List_Id := Nil_String;
447      --  The list of all the source file names.
448      --  Set by Prj.Nmsc.Check_Naming_Scheme.
449
450      Source_Dirs : String_List_Id := Nil_String;
451      --  The list of all the source directories.
452      --  Set by Prj.Nmsc.Check_Naming_Scheme.
453
454      Known_Order_Of_Source_Dirs : Boolean := True;
455      --  False, if there is any /** in the Source_Dirs, because in this case
456      --  the ordering of the source subdirs depend on the OS. If True,
457      --  duplicate file names in the same project file are allowed.
458
459      Object_Directory : Name_Id := No_Name;
460      --  The object directory of this project file.
461      --  Set by Prj.Nmsc.Check_Naming_Scheme.
462
463      Display_Object_Dir : Name_Id := No_Name;
464
465      Exec_Directory   : Name_Id := No_Name;
466      --  The exec directory of this project file.
467      --  Default is equal to Object_Directory.
468      --  Set by Prj.Nmsc.Check_Naming_Scheme.
469
470      Display_Exec_Dir : Name_Id := No_Name;
471
472      Extends : Project_Id := No_Project;
473      --  The reference of the project file, if any, that this
474      --  project file extends.
475      --  Set by Prj.Proc.Process.
476
477      Extended_By : Project_Id := No_Project;
478      --  The reference of the project file, if any, that
479      --  extends this project file.
480      --  Set by Prj.Proc.Process.
481
482      Naming : Naming_Data := Standard_Naming_Data;
483      --  The naming scheme of this project file.
484      --  Set by Prj.Nmsc.Check_Naming_Scheme.
485
486      Decl : Declarations := No_Declarations;
487      --  The declarations (variables, attributes and packages)
488      --  of this project file.
489      --  Set by Prj.Proc.Process.
490
491      Imported_Projects : Project_List := Empty_Project_List;
492      --  The list of all directly imported projects, if any.
493      --  Set by Prj.Proc.Process.
494
495      Ada_Include_Path  : String_Access := null;
496      --  The cached value of ADA_INCLUDE_PATH for this project file.
497      --  Do not use this field directly outside of the compiler, use
498      --  Prj.Env.Ada_Include_Path instead.
499      --  Set by Prj.Env.Ada_Include_Path.
500
501      Ada_Objects_Path  : String_Access := null;
502      --  The cached value of ADA_OBJECTS_PATH for this project file.
503      --  Do not use this field directly outside of the compiler, use
504      --  Prj.Env.Ada_Objects_Path instead.
505      --  Set by Prj.Env.Ada_Objects_Path
506
507      Include_Path_File : Name_Id := No_Name;
508      --  The cached value of the source path temp file for this project file.
509      --  Set by gnatmake (Prj.Env.Set_Ada_Paths).
510
511      Objects_Path_File_With_Libs : Name_Id := No_Name;
512      --  The cached value of the object path temp file (including library
513      --  dirs) for this project file.
514      --  Set by gnatmake (Prj.Env.Set_Ada_Paths).
515
516      Objects_Path_File_Without_Libs : Name_Id := No_Name;
517      --  The cached value of the object path temp file (excluding library
518      --  dirs) for this project file.
519      --  Set by gnatmake (Prj.Env.Set_Ada_Paths).
520
521      Config_File_Name : Name_Id := No_Name;
522      --  The name of the configuration pragmas file, if any.
523      --  Set by gnatmake (Prj.Env.Create_Config_Pragmas_File).
524
525      Config_File_Temp : Boolean := False;
526      --  An indication that the configuration pragmas file is
527      --  a temporary file that must be deleted at the end.
528      --  Set by gnatmake (Prj.Env.Create_Config_Pragmas_File).
529
530      Config_Checked : Boolean := False;
531      --  A flag to avoid checking repetitively the configuration pragmas file.
532      --  Set by gnatmake (Prj.Env.Create_Config_Pragmas_File).
533
534      Language_Independent_Checked : Boolean := False;
535      --  A flag that indicates that the project file has been checked
536      --  for language independent features: Object_Directory,
537      --  Source_Directories, Library, non empty Naming Suffixs.
538
539      Checked : Boolean := False;
540      --  A flag to avoid checking repetitively the naming scheme of
541      --  this project file.
542      --  Set by Prj.Nmsc.Check_Naming_Scheme.
543
544      Seen  : Boolean := False;
545      Flag1 : Boolean := False;
546      Flag2 : Boolean := False;
547      --  Various flags that are used in an ad hoc manner
548      --  That's really not a good enough comment ??? we need to know what
549      --  these flags are used for, and give them proper names. If Flag1
550      --  and Flag2 have multiple uses, then either we use multiple fields
551      --  or a renaming scheme.
552
553      Depth : Natural := 0;
554      --  The maximum depth of a project in the project graph.
555      --  Depth of main project is 0.
556
557      Unkept_Comments : Boolean := False;
558      --  True if there are comments in the project sources that cannot
559      --  be kept in the project tree.
560
561   end record;
562
563   function Empty_Project return Project_Data;
564   --  Return the representation of an empty project.
565
566   package Projects is new Table.Table (
567     Table_Component_Type => Project_Data,
568     Table_Index_Type     => Project_Id,
569     Table_Low_Bound      => 1,
570     Table_Initial        => 100,
571     Table_Increment      => 100,
572     Table_Name           => "Prj.Projects");
573   --  The set of all project files.
574
575   type Put_Line_Access is access procedure
576     (Line    : String;
577      Project : Project_Id);
578   --  Use to customize error reporting in Prj.Proc and Prj.Nmsc.
579
580   procedure Expect (The_Token : Token_Type; Token_Image : String);
581   --  Check that the current token is The_Token. If it is not, then
582   --  output an error message.
583
584   procedure Initialize;
585   --  This procedure must be called before using any services from the Prj
586   --  hierarchy. Namet.Initialize must be called before Prj.Initialize.
587
588   procedure Reset;
589   --  This procedure resets all the tables that are used when processing a
590   --  project file tree. Initialize must be called before the call to Reset.
591
592   procedure Register_Default_Naming_Scheme
593     (Language : Name_Id;
594      Default_Spec_Suffix : Name_Id;
595      Default_Body_Suffix : Name_Id);
596   --  Register the default suffixs for a given language. These extensions
597   --  will be ignored if the user has specified a new naming scheme in a
598   --  project file.
599   --
600   --  Otherwise, this information will be automatically added to Naming_Data
601   --  when a project is processed, in the lists Spec_Suffix and Body_Suffix.
602
603   generic
604      type State is limited private;
605      with procedure Action
606        (Project    : Project_Id;
607         With_State : in out State);
608   procedure For_Every_Project_Imported
609     (By         : Project_Id;
610      With_State : in out State);
611   --  Call Action for each project imported directly or indirectly by project
612   --  By. Action is called according to the order of importation: if A
613   --  imports B, directly or indirectly, Action will be called for A before
614   --  it is called for B. With_State may be used by Action to choose a
615   --  behavior or to report some global result.
616
617private
618
619   Initial_Buffer_Size : constant := 100;
620
621   Buffer : String_Access := new String (1 .. Initial_Buffer_Size);
622   --  An extensible character buffer to store names. Used in Prj.Part and
623   --  Prj.Strt.
624
625   Buffer_Last : Natural := 0;
626   --  The index of the last character in the Buffer
627
628   Current_Packages_To_Check : String_List_Access := All_Packages;
629   --  Global variable, set by Prj.Part.Parse, used by Prj.Dect.
630
631   procedure Add_To_Buffer (S : String);
632   --  Append a String to the Buffer
633
634end Prj;
635