1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2004 Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Err_Vars
; use Err_Vars
;
28 with Namet
; use Namet
;
30 with Osint
; use Osint
;
31 with Output
; use Output
;
32 with Prj
.Com
; use Prj
.Com
;
34 with Prj
.Err
; use Prj
.Err
;
35 with Scans
; use Scans
;
37 with Sinput
; use Sinput
;
38 with Sinput
.P
; use Sinput
.P
;
41 with Types
; use Types
;
43 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
44 with Ada
.Exceptions
; use Ada
.Exceptions
;
46 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
47 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
49 with System
.HTable
; use System
.HTable
;
51 pragma Elaborate_All
(GNAT
.OS_Lib
);
53 package body Prj
.Part
is
55 Dir_Sep
: Character renames GNAT
.OS_Lib
.Directory_Separator
;
57 Project_Path
: String_Access
;
58 -- The project path; initialized during package elaboration.
59 -- Contains at least the current working directory.
61 Ada_Project_Path
: constant String := "ADA_PROJECT_PATH";
62 -- Name of the env. variable that contains path name(s) of directories
63 -- where project files may reside.
65 Prj_Path
: constant String_Access
:= Getenv
(Ada_Project_Path
);
66 -- The path name(s) of directories where project files may reside.
69 type Extension_Origin
is (None
, Extending_Simple
, Extending_All
);
70 -- Type of parameter From_Extended for procedures Parse_Single_Project and
71 -- Post_Parse_Context_Clause. Extending_All means that we are parsing the
72 -- tree rooted at an extending all project.
74 ------------------------------------
75 -- Local Packages and Subprograms --
76 ------------------------------------
78 type With_Id
is new Nat
;
79 No_With
: constant With_Id
:= 0;
81 type With_Record
is record
83 Location
: Source_Ptr
;
84 Limited_With
: Boolean;
85 Node
: Project_Node_Id
;
88 -- Information about an imported project, to be put in table Withs below
90 package Withs
is new Table
.Table
91 (Table_Component_Type
=> With_Record
,
92 Table_Index_Type
=> With_Id
,
95 Table_Increment
=> 50,
96 Table_Name
=> "Prj.Part.Withs");
97 -- Table used to store temporarily paths and locations of imported
98 -- projects. These imported projects will be effectively parsed after the
99 -- name of the current project has been extablished.
101 type Names_And_Id
is record
103 Canonical_Path_Name
: Name_Id
;
104 Id
: Project_Node_Id
;
107 package Project_Stack
is new Table
.Table
108 (Table_Component_Type
=> Names_And_Id
,
109 Table_Index_Type
=> Nat
,
110 Table_Low_Bound
=> 1,
112 Table_Increment
=> 50,
113 Table_Name
=> "Prj.Part.Project_Stack");
114 -- This table is used to detect circular dependencies
115 -- for imported and extended projects and to get the project ids of
116 -- limited imported projects when there is a circularity with at least
117 -- one limited imported project file.
119 package Virtual_Hash
is new Simple_HTable
120 (Header_Num
=> Header_Num
,
121 Element
=> Project_Node_Id
,
122 No_Element
=> Empty_Node
,
123 Key
=> Project_Node_Id
,
124 Hash
=> Prj
.Tree
.Hash
,
126 -- Hash table to store the node id of the project for which a virtual
127 -- extending project need to be created.
129 package Processed_Hash
is new Simple_HTable
130 (Header_Num
=> Header_Num
,
133 Key
=> Project_Node_Id
,
134 Hash
=> Prj
.Tree
.Hash
,
136 -- Hash table to store the project process when looking for project that
137 -- need to have a virtual extending project, to avoid processing the same
140 procedure Create_Virtual_Extending_Project
141 (For_Project
: Project_Node_Id
;
142 Main_Project
: Project_Node_Id
);
143 -- Create a virtual extending project of For_Project. Main_Project is
144 -- the extending all project.
146 procedure Look_For_Virtual_Projects_For
147 (Proj
: Project_Node_Id
;
148 Potentially_Virtual
: Boolean);
149 -- Look for projects that need to have a virtual extending project.
150 -- This procedure is recursive. If called with Potentially_Virtual set to
151 -- True, then Proj may need an virtual extending project; otherwise it
152 -- does not (because it is already extended), but other projects that it
153 -- imports may need to be virtually extended.
155 procedure Pre_Parse_Context_Clause
(Context_Clause
: out With_Id
);
156 -- Parse the context clause of a project.
157 -- Store the paths and locations of the imported projects in table Withs.
158 -- Does nothing if there is no context clause (if the current
159 -- token is not "with" or "limited" followed by "with").
161 procedure Post_Parse_Context_Clause
162 (Context_Clause
: With_Id
;
163 Imported_Projects
: out Project_Node_Id
;
164 Project_Directory
: Name_Id
;
165 From_Extended
: Extension_Origin
;
166 In_Limited
: Boolean);
167 -- Parse the imported projects that have been stored in table Withs,
168 -- if any. From_Extended is used for the call to Parse_Single_Project
169 -- below. When In_Limited is True, the importing path includes at least
170 -- one "limited with".
172 procedure Parse_Single_Project
173 (Project
: out Project_Node_Id
;
174 Extends_All
: out Boolean;
177 From_Extended
: Extension_Origin
;
178 In_Limited
: Boolean);
179 -- Parse a project file.
180 -- Recursive procedure: it calls itself for imported and extended
181 -- projects. When From_Extended is not None, if the project has already
182 -- been parsed and is an extended project A, return the ultimate
183 -- (not extended) project that extends A. When In_Limited is True,
184 -- the importing path includes at least one "limited with".
186 function Project_Path_Name_Of
187 (Project_File_Name
: String;
188 Directory
: String) return String;
189 -- Returns the path name of a project file. Returns an empty string
190 -- if project file cannot be found.
192 function Immediate_Directory_Of
(Path_Name
: Name_Id
) return Name_Id
;
193 -- Get the directory of the file with the specified path name.
194 -- This includes the directory separator as the last character.
195 -- Returns "./" if Path_Name contains no directory separator.
197 function Project_Name_From
(Path_Name
: String) return Name_Id
;
198 -- Returns the name of the project that corresponds to its path name.
199 -- Returns No_Name if the path name is invalid, because the corresponding
200 -- project name does not have the syntax of an ada identifier.
202 --------------------------------------
203 -- Create_Virtual_Extending_Project --
204 --------------------------------------
206 procedure Create_Virtual_Extending_Project
207 (For_Project
: Project_Node_Id
;
208 Main_Project
: Project_Node_Id
)
211 Virtual_Name
: constant String :=
213 Get_Name_String
(Name_Of
(For_Project
));
214 -- The name of the virtual extending project
216 Virtual_Name_Id
: Name_Id
;
217 -- Virtual extending project name id
219 Virtual_Path_Id
: Name_Id
;
220 -- Fake path name of the virtual extending project. The directory is
221 -- the same directory as the extending all project.
223 Virtual_Dir_Id
: constant Name_Id
:=
224 Immediate_Directory_Of
(Path_Name_Of
(Main_Project
));
225 -- The directory of the extending all project
227 -- The source of the virtual extending project is something like:
229 -- project V$<project name> extends <project path> is
231 -- for Source_Dirs use ();
233 -- end V$<project name>;
235 -- The project directory cannot be specified during parsing; it will be
236 -- put directly in the virtual extending project data during processing.
238 -- Nodes that made up the virtual extending project
240 Virtual_Project
: constant Project_Node_Id
:=
241 Default_Project_Node
(N_Project
);
242 With_Clause
: constant Project_Node_Id
:=
243 Default_Project_Node
(N_With_Clause
);
244 Project_Declaration
: constant Project_Node_Id
:=
245 Default_Project_Node
(N_Project_Declaration
);
246 Source_Dirs_Declaration
: constant Project_Node_Id
:=
247 Default_Project_Node
(N_Declarative_Item
);
248 Source_Dirs_Attribute
: constant Project_Node_Id
:=
250 (N_Attribute_Declaration
, List
);
251 Source_Dirs_Expression
: constant Project_Node_Id
:=
252 Default_Project_Node
(N_Expression
, List
);
253 Source_Dirs_Term
: constant Project_Node_Id
:=
254 Default_Project_Node
(N_Term
, List
);
255 Source_Dirs_List
: constant Project_Node_Id
:=
257 (N_Literal_String_List
, List
);
260 -- Get the virtual name id
262 Name_Len
:= Virtual_Name
'Length;
263 Name_Buffer
(1 .. Name_Len
) := Virtual_Name
;
264 Virtual_Name_Id
:= Name_Find
;
266 -- Get the virtual path name
268 Get_Name_String
(Path_Name_Of
(Main_Project
));
271 and then Name_Buffer
(Name_Len
) /= Directory_Separator
272 and then Name_Buffer
(Name_Len
) /= '/'
274 Name_Len
:= Name_Len
- 1;
277 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ Virtual_Name
'Length) :=
279 Name_Len
:= Name_Len
+ Virtual_Name
'Length;
280 Virtual_Path_Id
:= Name_Find
;
284 Set_Name_Of
(With_Clause
, Virtual_Name_Id
);
285 Set_Path_Name_Of
(With_Clause
, Virtual_Path_Id
);
286 Set_Project_Node_Of
(With_Clause
, Virtual_Project
);
287 Set_Next_With_Clause_Of
288 (With_Clause
, First_With_Clause_Of
(Main_Project
));
289 Set_First_With_Clause_Of
(Main_Project
, With_Clause
);
291 -- Virtual project node
293 Set_Name_Of
(Virtual_Project
, Virtual_Name_Id
);
294 Set_Path_Name_Of
(Virtual_Project
, Virtual_Path_Id
);
295 Set_Location_Of
(Virtual_Project
, Location_Of
(Main_Project
));
296 Set_Directory_Of
(Virtual_Project
, Virtual_Dir_Id
);
297 Set_Project_Declaration_Of
(Virtual_Project
, Project_Declaration
);
298 Set_Extended_Project_Path_Of
299 (Virtual_Project
, Path_Name_Of
(For_Project
));
301 -- Project declaration
303 Set_First_Declarative_Item_Of
304 (Project_Declaration
, Source_Dirs_Declaration
);
305 Set_Extended_Project_Of
(Project_Declaration
, For_Project
);
307 -- Source_Dirs declaration
309 Set_Current_Item_Node
(Source_Dirs_Declaration
, Source_Dirs_Attribute
);
311 -- Source_Dirs attribute
313 Set_Name_Of
(Source_Dirs_Attribute
, Snames
.Name_Source_Dirs
);
314 Set_Expression_Of
(Source_Dirs_Attribute
, Source_Dirs_Expression
);
316 -- Source_Dirs expression
318 Set_First_Term
(Source_Dirs_Expression
, Source_Dirs_Term
);
322 Set_Current_Term
(Source_Dirs_Term
, Source_Dirs_List
);
324 -- Source_Dirs empty list: nothing to do
326 end Create_Virtual_Extending_Project
;
328 ----------------------------
329 -- Immediate_Directory_Of --
330 ----------------------------
332 function Immediate_Directory_Of
(Path_Name
: Name_Id
) return Name_Id
is
334 Get_Name_String
(Path_Name
);
336 for Index
in reverse 1 .. Name_Len
loop
337 if Name_Buffer
(Index
) = '/'
338 or else Name_Buffer
(Index
) = Dir_Sep
340 -- Remove all chars after last directory separator from name
343 Name_Len
:= Index
- 1;
353 -- There is no directory separator in name. Return "./" or ".\"
356 Name_Buffer
(1) := '.';
357 Name_Buffer
(2) := Dir_Sep
;
359 end Immediate_Directory_Of
;
361 -----------------------------------
362 -- Look_For_Virtual_Projects_For --
363 -----------------------------------
365 procedure Look_For_Virtual_Projects_For
366 (Proj
: Project_Node_Id
;
367 Potentially_Virtual
: Boolean)
370 Declaration
: Project_Node_Id
:= Empty_Node
;
371 -- Node for the project declaration of Proj
373 With_Clause
: Project_Node_Id
:= Empty_Node
;
374 -- Node for a with clause of Proj
376 Imported
: Project_Node_Id
:= Empty_Node
;
377 -- Node for a project imported by Proj
379 Extended
: Project_Node_Id
:= Empty_Node
;
380 -- Node for the eventual project extended by Proj
383 -- Nothing to do if Proj is not defined or if it has already been
386 if Proj
/= Empty_Node
and then not Processed_Hash
.Get
(Proj
) then
387 -- Make sure the project will not be processed again
389 Processed_Hash
.Set
(Proj
, True);
391 Declaration
:= Project_Declaration_Of
(Proj
);
393 if Declaration
/= Empty_Node
then
394 Extended
:= Extended_Project_Of
(Declaration
);
397 -- If this is a project that may need a virtual extending project
398 -- and it is not itself an extending project, put it in the list.
400 if Potentially_Virtual
and then Extended
= Empty_Node
then
401 Virtual_Hash
.Set
(Proj
, Proj
);
404 -- Now check the projects it imports
406 With_Clause
:= First_With_Clause_Of
(Proj
);
408 while With_Clause
/= Empty_Node
loop
409 Imported
:= Project_Node_Of
(With_Clause
);
411 if Imported
/= Empty_Node
then
412 Look_For_Virtual_Projects_For
413 (Imported
, Potentially_Virtual
=> True);
416 With_Clause
:= Next_With_Clause_Of
(With_Clause
);
419 -- Check also the eventual project extended by Proj. As this project
420 -- is already extended, call recursively with Potentially_Virtual
423 Look_For_Virtual_Projects_For
424 (Extended
, Potentially_Virtual
=> False);
426 end Look_For_Virtual_Projects_For
;
433 (Project
: out Project_Node_Id
;
434 Project_File_Name
: String;
435 Always_Errout_Finalize
: Boolean;
436 Packages_To_Check
: String_List_Access
:= All_Packages
;
437 Store_Comments
: Boolean := False)
439 Current_Directory
: constant String := Get_Current_Dir
;
443 -- Save the Packages_To_Check in Prj, so that it is visible from
446 Current_Packages_To_Check
:= Packages_To_Check
;
448 Project
:= Empty_Node
;
450 if Current_Verbosity
>= Medium
then
451 Write_Str
("ADA_PROJECT_PATH=""");
452 Write_Str
(Project_Path
.all);
457 Path_Name
: constant String :=
458 Project_Path_Name_Of
(Project_File_Name
,
459 Directory
=> Current_Directory
);
463 Prj
.Err
.Scanner
.Set_Comment_As_Token
(Store_Comments
);
464 Prj
.Err
.Scanner
.Set_End_Of_Line_As_Token
(Store_Comments
);
466 -- Parse the main project file
468 if Path_Name
= "" then
470 ("project file """, Project_File_Name
, """ not found");
471 Project
:= Empty_Node
;
477 Extends_All
=> Dummy
,
478 Path_Name
=> Path_Name
,
480 From_Extended
=> None
,
481 In_Limited
=> False);
483 -- If Project is an extending-all project, create the eventual
484 -- virtual extending projects and check that there are no illegally
485 -- imported projects.
487 if Project
/= Empty_Node
and then Is_Extending_All
(Project
) then
488 -- First look for projects that potentially need a virtual
489 -- extending project.
492 Processed_Hash
.Reset
;
494 -- Mark the extending all project as processed, to avoid checking
495 -- the imported projects in case of a "limited with" on this
496 -- extending all project.
498 Processed_Hash
.Set
(Project
, True);
501 Declaration
: constant Project_Node_Id
:=
502 Project_Declaration_Of
(Project
);
504 Look_For_Virtual_Projects_For
505 (Extended_Project_Of
(Declaration
),
506 Potentially_Virtual
=> False);
509 -- Now, check the projects directly imported by the main project.
510 -- Remove from the potentially virtual any project extended by one
511 -- of these imported projects. For non extending imported
512 -- projects, check that they do not belong to the project tree of
513 -- the project being "extended-all" by the main project.
516 With_Clause
: Project_Node_Id
:=
517 First_With_Clause_Of
(Project
);
518 Imported
: Project_Node_Id
:= Empty_Node
;
519 Declaration
: Project_Node_Id
:= Empty_Node
;
522 while With_Clause
/= Empty_Node
loop
523 Imported
:= Project_Node_Of
(With_Clause
);
525 if Imported
/= Empty_Node
then
526 Declaration
:= Project_Declaration_Of
(Imported
);
528 if Extended_Project_Of
(Declaration
) /= Empty_Node
then
530 Imported
:= Extended_Project_Of
(Declaration
);
531 exit when Imported
= Empty_Node
;
532 Virtual_Hash
.Remove
(Imported
);
533 Declaration
:= Project_Declaration_Of
(Imported
);
539 With_Clause
:= Next_With_Clause_Of
(With_Clause
);
543 -- Now create all the virtual extending projects
546 Proj
: Project_Node_Id
:= Virtual_Hash
.Get_First
;
548 while Proj
/= Empty_Node
loop
549 Create_Virtual_Extending_Project
(Proj
, Project
);
550 Proj
:= Virtual_Hash
.Get_Next
;
555 -- If there were any kind of error during the parsing, serious
556 -- or not, then the parsing fails.
558 if Err_Vars
.Total_Errors_Detected
> 0 then
559 Project
:= Empty_Node
;
562 if Project
= Empty_Node
or else Always_Errout_Finalize
then
572 Write_Line
(Exception_Information
(X
));
573 Write_Str
("Exception ");
574 Write_Str
(Exception_Name
(X
));
575 Write_Line
(" raised, while processing project file");
576 Project
:= Empty_Node
;
579 ------------------------------
580 -- Pre_Parse_Context_Clause --
581 ------------------------------
583 procedure Pre_Parse_Context_Clause
(Context_Clause
: out With_Id
) is
584 Current_With_Clause
: With_Id
:= No_With
;
585 Limited_With
: Boolean := False;
587 Current_With
: With_Record
;
589 Current_With_Node
: Project_Node_Id
:= Empty_Node
;
592 -- Assume no context clause
594 Context_Clause
:= No_With
;
597 -- If Token is not WITH or LIMITED, there is no context clause,
598 -- or we have exhausted the with clauses.
600 while Token
= Tok_With
or else Token
= Tok_Limited
loop
601 Current_With_Node
:= Default_Project_Node
(Of_Kind
=> N_With_Clause
);
602 Limited_With
:= Token
= Tok_Limited
;
605 Scan
; -- scan past LIMITED
606 Expect
(Tok_With
, "WITH");
607 exit With_Loop
when Token
/= Tok_With
;
612 Scan
; -- scan past WITH or ","
614 Expect
(Tok_String_Literal
, "literal string");
616 if Token
/= Tok_String_Literal
then
620 -- Store path and location in table Withs
624 Location
=> Token_Ptr
,
625 Limited_With
=> Limited_With
,
626 Node
=> Current_With_Node
,
629 Withs
.Increment_Last
;
630 Withs
.Table
(Withs
.Last
) := Current_With
;
632 if Current_With_Clause
= No_With
then
633 Context_Clause
:= Withs
.Last
;
636 Withs
.Table
(Current_With_Clause
).Next
:= Withs
.Last
;
639 Current_With_Clause
:= Withs
.Last
;
643 if Token
= Tok_Semicolon
then
644 Set_End_Of_Line
(Current_With_Node
);
645 Set_Previous_Line_Node
(Current_With_Node
);
647 -- End of (possibly multiple) with clause;
649 Scan
; -- scan past the semicolon.
652 elsif Token
/= Tok_Comma
then
653 Error_Msg
("expected comma or semi colon", Token_Ptr
);
658 Default_Project_Node
(Of_Kind
=> N_With_Clause
);
661 end Pre_Parse_Context_Clause
;
664 -------------------------------
665 -- Post_Parse_Context_Clause --
666 -------------------------------
668 procedure Post_Parse_Context_Clause
669 (Context_Clause
: With_Id
;
670 Imported_Projects
: out Project_Node_Id
;
671 Project_Directory
: Name_Id
;
672 From_Extended
: Extension_Origin
;
673 In_Limited
: Boolean)
675 Current_With_Clause
: With_Id
:= Context_Clause
;
677 Current_Project
: Project_Node_Id
:= Empty_Node
;
678 Previous_Project
: Project_Node_Id
:= Empty_Node
;
679 Next_Project
: Project_Node_Id
:= Empty_Node
;
681 Project_Directory_Path
: constant String :=
682 Get_Name_String
(Project_Directory
);
684 Current_With
: With_Record
;
685 Limited_With
: Boolean := False;
686 Extends_All
: Boolean := False;
689 Imported_Projects
:= Empty_Node
;
691 while Current_With_Clause
/= No_With
loop
692 Current_With
:= Withs
.Table
(Current_With_Clause
);
693 Current_With_Clause
:= Current_With
.Next
;
695 Limited_With
:= In_Limited
or Current_With
.Limited_With
;
698 Original_Path
: constant String :=
699 Get_Name_String
(Current_With
.Path
);
701 Imported_Path_Name
: constant String :=
704 Project_Directory_Path
);
706 Resolved_Path
: constant String :=
709 Resolve_Links
=> True,
710 Case_Sensitive
=> False);
712 Withed_Project
: Project_Node_Id
:= Empty_Node
;
715 if Imported_Path_Name
= "" then
717 -- The project file cannot be found
719 Error_Msg_Name_1
:= Current_With
.Path
;
721 Error_Msg
("unknown project file: {", Current_With
.Location
);
723 -- If this is not imported by the main project file,
724 -- display the import path.
726 if Project_Stack
.Last
> 1 then
727 for Index
in reverse 1 .. Project_Stack
.Last
loop
728 Error_Msg_Name_1
:= Project_Stack
.Table
(Index
).Path_Name
;
729 Error_Msg
("\imported by {", Current_With
.Location
);
736 Previous_Project
:= Current_Project
;
738 if Current_Project
= Empty_Node
then
740 -- First with clause of the context clause
742 Current_Project
:= Current_With
.Node
;
743 Imported_Projects
:= Current_Project
;
746 Next_Project
:= Current_With
.Node
;
747 Set_Next_With_Clause_Of
(Current_Project
, Next_Project
);
748 Current_Project
:= Next_Project
;
752 (Current_Project
, Current_With
.Path
);
753 Set_Location_Of
(Current_Project
, Current_With
.Location
);
755 -- If this is a "limited with", check if we have a circularity.
756 -- If we have one, get the project id of the limited imported
757 -- project file, and do not parse it.
759 if Limited_With
and then Project_Stack
.Last
> 1 then
761 Canonical_Path_Name
: Name_Id
;
764 Name_Len
:= Resolved_Path
'Length;
765 Name_Buffer
(1 .. Name_Len
) := Resolved_Path
;
766 Canonical_Path_Name
:= Name_Find
;
768 for Index
in 1 .. Project_Stack
.Last
loop
769 if Project_Stack
.Table
(Index
).Canonical_Path_Name
=
772 -- We have found the limited imported project,
773 -- get its project id, and do not parse it.
775 Withed_Project
:= Project_Stack
.Table
(Index
).Id
;
782 -- Parse the imported project, if its project id is unknown
784 if Withed_Project
= Empty_Node
then
786 (Project
=> Withed_Project
,
787 Extends_All
=> Extends_All
,
788 Path_Name
=> Imported_Path_Name
,
790 From_Extended
=> From_Extended
,
791 In_Limited
=> Limited_With
);
794 Extends_All
:= Is_Extending_All
(Withed_Project
);
797 if Withed_Project
= Empty_Node
then
798 -- If parsing was not successful, remove the
801 Current_Project
:= Previous_Project
;
803 if Current_Project
= Empty_Node
then
804 Imported_Projects
:= Empty_Node
;
807 Set_Next_With_Clause_Of
808 (Current_Project
, Empty_Node
);
811 -- If parsing was successful, record project name
812 -- and path name in with clause
815 (Node
=> Current_Project
,
816 To
=> Withed_Project
,
817 Limited_With
=> Limited_With
);
818 Set_Name_Of
(Current_Project
, Name_Of
(Withed_Project
));
820 Name_Len
:= Resolved_Path
'Length;
821 Name_Buffer
(1 .. Name_Len
) := Resolved_Path
;
822 Set_Path_Name_Of
(Current_Project
, Name_Find
);
825 Set_Is_Extending_All
(Current_Project
);
831 end Post_Parse_Context_Clause
;
833 --------------------------
834 -- Parse_Single_Project --
835 --------------------------
837 procedure Parse_Single_Project
838 (Project
: out Project_Node_Id
;
839 Extends_All
: out Boolean;
842 From_Extended
: Extension_Origin
;
843 In_Limited
: Boolean)
845 Normed_Path_Name
: Name_Id
;
846 Canonical_Path_Name
: Name_Id
;
847 Project_Directory
: Name_Id
;
848 Project_Scan_State
: Saved_Project_Scan_State
;
849 Source_Index
: Source_File_Index
;
851 Extending
: Boolean := False;
853 Extended_Project
: Project_Node_Id
:= Empty_Node
;
855 A_Project_Name_And_Node
: Tree_Private_Part
.Project_Name_And_Node
:=
856 Tree_Private_Part
.Projects_Htable
.Get_First
;
858 Name_From_Path
: constant Name_Id
:= Project_Name_From
(Path_Name
);
860 Name_Of_Project
: Name_Id
:= No_Name
;
862 First_With
: With_Id
;
864 use Tree_Private_Part
;
866 Project_Comment_State
: Tree
.Comment_State
;
869 Extends_All
:= False;
872 Normed_Path
: constant String := Normalize_Pathname
873 (Path_Name
, Resolve_Links
=> False,
874 Case_Sensitive
=> True);
875 Canonical_Path
: constant String := Normalize_Pathname
876 (Normed_Path
, Resolve_Links
=> True,
877 Case_Sensitive
=> False);
880 Name_Len
:= Normed_Path
'Length;
881 Name_Buffer
(1 .. Name_Len
) := Normed_Path
;
882 Normed_Path_Name
:= Name_Find
;
883 Name_Len
:= Canonical_Path
'Length;
884 Name_Buffer
(1 .. Name_Len
) := Canonical_Path
;
885 Canonical_Path_Name
:= Name_Find
;
888 -- Check for a circular dependency
890 for Index
in 1 .. Project_Stack
.Last
loop
891 if Canonical_Path_Name
=
892 Project_Stack
.Table
(Index
).Canonical_Path_Name
894 Error_Msg
("circular dependency detected", Token_Ptr
);
895 Error_Msg_Name_1
:= Normed_Path_Name
;
896 Error_Msg
("\ { is imported by", Token_Ptr
);
898 for Current
in reverse 1 .. Project_Stack
.Last
loop
899 Error_Msg_Name_1
:= Project_Stack
.Table
(Current
).Path_Name
;
901 if Project_Stack
.Table
(Current
).Canonical_Path_Name
/=
905 ("\ { which itself is imported by", Token_Ptr
);
908 Error_Msg
("\ {", Token_Ptr
);
913 Project
:= Empty_Node
;
918 -- Put the new path name on the stack
920 Project_Stack
.Increment_Last
;
921 Project_Stack
.Table
(Project_Stack
.Last
).Path_Name
:= Normed_Path_Name
;
922 Project_Stack
.Table
(Project_Stack
.Last
).Canonical_Path_Name
:=
925 -- Check if the project file has already been parsed.
928 A_Project_Name_And_Node
/= Tree_Private_Part
.No_Project_Name_And_Node
931 Path_Id
: Name_Id
:= Path_Name_Of
(A_Project_Name_And_Node
.Node
);
934 if Path_Id
/= No_Name
then
935 Get_Name_String
(Path_Id
);
936 Canonical_Case_File_Name
(Name_Buffer
(1 .. Name_Len
));
937 Path_Id
:= Name_Find
;
940 if Path_Id
= Canonical_Path_Name
then
943 if A_Project_Name_And_Node
.Extended
then
945 ("cannot extend the same project file several times",
950 ("cannot extend an already imported project file",
954 elsif A_Project_Name_And_Node
.Extended
then
956 Is_Extending_All
(A_Project_Name_And_Node
.Node
);
958 -- If the imported project is an extended project A,
959 -- and we are in an extended project, replace A with the
960 -- ultimate project extending A.
962 if From_Extended
/= None
then
964 Decl
: Project_Node_Id
:=
965 Project_Declaration_Of
966 (A_Project_Name_And_Node
.Node
);
968 Prj
: Project_Node_Id
:=
969 Extending_Project_Of
(Decl
);
973 Decl
:= Project_Declaration_Of
(Prj
);
974 exit when Extending_Project_Of
(Decl
) = Empty_Node
;
975 Prj
:= Extending_Project_Of
(Decl
);
978 A_Project_Name_And_Node
.Node
:= Prj
;
982 ("cannot import an already extended project file",
987 Project
:= A_Project_Name_And_Node
.Node
;
988 Project_Stack
.Decrement_Last
;
993 A_Project_Name_And_Node
:= Tree_Private_Part
.Projects_Htable
.Get_Next
;
996 -- We never encountered this project file
997 -- Save the scan state, load the project file and start to scan it.
999 Save_Project_Scan_State
(Project_Scan_State
);
1000 Source_Index
:= Load_Project_File
(Path_Name
);
1001 Tree
.Save
(Project_Comment_State
);
1003 -- If we cannot find it, we stop
1005 if Source_Index
= No_Source_File
then
1006 Project
:= Empty_Node
;
1007 Project_Stack
.Decrement_Last
;
1011 Prj
.Err
.Scanner
.Initialize_Scanner
(Types
.No_Unit
, Source_Index
);
1015 if Name_From_Path
= No_Name
then
1017 -- The project file name is not correct (no or bad extension,
1018 -- or not following Ada identifier's syntax).
1020 Error_Msg_Name_1
:= Canonical_Path_Name
;
1021 Error_Msg
("?{ is not a valid path name for a project file",
1025 if Current_Verbosity
>= Medium
then
1026 Write_Str
("Parsing """);
1027 Write_Str
(Path_Name
);
1032 -- Is there any imported project?
1034 Pre_Parse_Context_Clause
(First_With
);
1036 Project_Directory
:= Immediate_Directory_Of
(Normed_Path_Name
);
1037 Project
:= Default_Project_Node
(Of_Kind
=> N_Project
);
1038 Project_Stack
.Table
(Project_Stack
.Last
).Id
:= Project
;
1039 Set_Directory_Of
(Project
, Project_Directory
);
1040 Set_Path_Name_Of
(Project
, Canonical_Path_Name
);
1041 Set_Location_Of
(Project
, Token_Ptr
);
1043 Expect
(Tok_Project
, "PROJECT");
1045 -- Mark location of PROJECT token if present
1047 if Token
= Tok_Project
then
1048 Set_Location_Of
(Project
, Token_Ptr
);
1049 Scan
; -- scan past project
1057 Expect
(Tok_Identifier
, "identifier");
1059 -- If the token is not an identifier, clear the buffer before
1060 -- exiting to indicate that the name of the project is ill-formed.
1062 if Token
/= Tok_Identifier
then
1067 -- Add the identifier name to the buffer
1069 Get_Name_String
(Token_Name
);
1070 Add_To_Buffer
(Name_Buffer
(1 .. Name_Len
));
1072 -- Scan past the identifier
1076 -- If we have a dot, add a dot the the Buffer and look for the next
1079 exit when Token
/= Tok_Dot
;
1080 Add_To_Buffer
(".");
1082 -- Scan past the dot
1087 -- See if this is an extending project
1089 if Token
= Tok_Extends
then
1091 -- Make sure that gnatmake will use mapping files
1093 Create_Mapping_File
:= True;
1095 -- We are extending another project
1099 Scan
; -- scan past EXTENDS
1101 if Token
= Tok_All
then
1102 Extends_All
:= True;
1103 Set_Is_Extending_All
(Project
);
1104 Scan
; -- scan past ALL
1108 -- If the name is well formed, Buffer_Last is > 0
1110 if Buffer_Last
> 0 then
1112 -- The Buffer contains the name of the project
1114 Name_Len
:= Buffer_Last
;
1115 Name_Buffer
(1 .. Name_Len
) := Buffer
(1 .. Buffer_Last
);
1116 Name_Of_Project
:= Name_Find
;
1117 Set_Name_Of
(Project
, Name_Of_Project
);
1119 -- To get expected name of the project file, replace dots by dashes
1121 Name_Len
:= Buffer_Last
;
1122 Name_Buffer
(1 .. Name_Len
) := Buffer
(1 .. Buffer_Last
);
1124 for Index
in 1 .. Name_Len
loop
1125 if Name_Buffer
(Index
) = '.' then
1126 Name_Buffer
(Index
) := '-';
1130 Canonical_Case_File_Name
(Name_Buffer
(1 .. Name_Len
));
1133 Expected_Name
: constant Name_Id
:= Name_Find
;
1136 -- Output a warning if the actual name is not the expected name
1138 if Name_From_Path
/= No_Name
1139 and then Expected_Name
/= Name_From_Path
1141 Error_Msg_Name_1
:= Expected_Name
;
1142 Error_Msg
("?file name does not match unit name, " &
1143 "should be `{" & Project_File_Extension
& "`",
1149 Imported_Projects
: Project_Node_Id
:= Empty_Node
;
1150 From_Ext
: Extension_Origin
:= None
;
1153 -- Extending_All is always propagated
1155 if From_Extended
= Extending_All
or else Extends_All
then
1156 From_Ext
:= Extending_All
;
1158 -- Otherwise, From_Extended is set to Extending_Single if the
1159 -- current project is an extending project.
1162 From_Ext
:= Extending_Simple
;
1165 Post_Parse_Context_Clause
1166 (Context_Clause
=> First_With
,
1167 Imported_Projects
=> Imported_Projects
,
1168 Project_Directory
=> Project_Directory
,
1169 From_Extended
=> From_Ext
,
1170 In_Limited
=> In_Limited
);
1171 Set_First_With_Clause_Of
(Project
, Imported_Projects
);
1175 Name_And_Node
: Tree_Private_Part
.Project_Name_And_Node
:=
1176 Tree_Private_Part
.Projects_Htable
.Get_First
;
1177 Project_Name
: Name_Id
:= Name_And_Node
.Name
;
1180 -- Check if we already have a project with this name
1182 while Project_Name
/= No_Name
1183 and then Project_Name
/= Name_Of_Project
1185 Name_And_Node
:= Tree_Private_Part
.Projects_Htable
.Get_Next
;
1186 Project_Name
:= Name_And_Node
.Name
;
1189 -- Report an error if we already have a project with this name
1191 if Project_Name
/= No_Name
then
1192 Error_Msg_Name_1
:= Project_Name
;
1193 Error_Msg
("duplicate project name {", Location_Of
(Project
));
1194 Error_Msg_Name_1
:= Path_Name_Of
(Name_And_Node
.Node
);
1195 Error_Msg
("\already in {", Location_Of
(Project
));
1198 -- Otherwise, add the name of the project to the hash table, so
1199 -- that we can check that no other subsequent project will have
1202 Tree_Private_Part
.Projects_Htable
.Set
1203 (K
=> Name_Of_Project
,
1204 E
=> (Name
=> Name_Of_Project
,
1206 Extended
=> Extended
));
1213 Expect
(Tok_String_Literal
, "literal string");
1215 if Token
= Tok_String_Literal
then
1216 Set_Extended_Project_Path_Of
(Project
, Token_Name
);
1219 Original_Path_Name
: constant String :=
1220 Get_Name_String
(Token_Name
);
1222 Extended_Project_Path_Name
: constant String :=
1223 Project_Path_Name_Of
1224 (Original_Path_Name
,
1226 (Project_Directory
));
1229 if Extended_Project_Path_Name
= "" then
1231 -- We could not find the project file to extend
1233 Error_Msg_Name_1
:= Token_Name
;
1235 Error_Msg
("unknown project file: {", Token_Ptr
);
1237 -- If we are not in the main project file, display the
1240 if Project_Stack
.Last
> 1 then
1242 Project_Stack
.Table
(Project_Stack
.Last
).Path_Name
;
1243 Error_Msg
("\extended by {", Token_Ptr
);
1245 for Index
in reverse 1 .. Project_Stack
.Last
- 1 loop
1247 Project_Stack
.Table
(Index
).Path_Name
;
1248 Error_Msg
("\imported by {", Token_Ptr
);
1254 From_Ext
: Extension_Origin
:= None
;
1257 if From_Extended
= Extending_All
or else Extends_All
then
1258 From_Ext
:= Extending_All
;
1261 Parse_Single_Project
1262 (Project
=> Extended_Project
,
1263 Extends_All
=> Extends_All
,
1264 Path_Name
=> Extended_Project_Path_Name
,
1266 From_Extended
=> From_Ext
,
1267 In_Limited
=> In_Limited
);
1270 -- A project that extends an extending-all project is also
1271 -- an extending-all project.
1273 if Extended_Project
/= Empty_Node
1274 and then Is_Extending_All
(Extended_Project
)
1276 Set_Is_Extending_All
(Project
);
1281 Scan
; -- scan past the extended project path
1285 -- Check that a non extending-all project does not import an
1286 -- extending-all project.
1288 if not Is_Extending_All
(Project
) then
1290 With_Clause
: Project_Node_Id
:= First_With_Clause_Of
(Project
);
1291 Imported
: Project_Node_Id
:= Empty_Node
;
1295 while With_Clause
/= Empty_Node
loop
1296 Imported
:= Project_Node_Of
(With_Clause
);
1298 if Is_Extending_All
(With_Clause
) then
1299 Error_Msg_Name_1
:= Name_Of
(Imported
);
1300 Error_Msg
("cannot import extending-all project {",
1302 exit With_Clause_Loop
;
1305 With_Clause
:= Next_With_Clause_Of
(With_Clause
);
1306 end loop With_Clause_Loop
;
1310 -- Check that a project with a name including a dot either imports
1311 -- or extends the project whose name precedes the last dot.
1313 if Name_Of_Project
/= No_Name
then
1314 Get_Name_String
(Name_Of_Project
);
1320 -- Look for the last dot
1322 while Name_Len
> 0 and then Name_Buffer
(Name_Len
) /= '.' loop
1323 Name_Len
:= Name_Len
- 1;
1326 -- If a dot was find, check if the parent project is imported
1329 if Name_Len
> 0 then
1330 Name_Len
:= Name_Len
- 1;
1333 Parent_Name
: constant Name_Id
:= Name_Find
;
1334 Parent_Found
: Boolean := False;
1335 With_Clause
: Project_Node_Id
:= First_With_Clause_Of
(Project
);
1338 -- If there is an extended project, check its name
1340 if Extended_Project
/= Empty_Node
then
1341 Parent_Found
:= Name_Of
(Extended_Project
) = Parent_Name
;
1344 -- If the parent project is not the extended project,
1345 -- check each imported project until we find the parent project.
1347 while not Parent_Found
and then With_Clause
/= Empty_Node
loop
1348 Parent_Found
:= Name_Of
(Project_Node_Of
(With_Clause
))
1350 With_Clause
:= Next_With_Clause_Of
(With_Clause
);
1353 -- If the parent project was not found, report an error
1355 if not Parent_Found
then
1356 Error_Msg_Name_1
:= Name_Of_Project
;
1357 Error_Msg_Name_2
:= Parent_Name
;
1358 Error_Msg
("project { does not import or extend project {",
1359 Location_Of
(Project
));
1364 Expect
(Tok_Is
, "IS");
1365 Set_End_Of_Line
(Project
);
1366 Set_Previous_Line_Node
(Project
);
1367 Set_Next_End_Node
(Project
);
1370 Project_Declaration
: Project_Node_Id
:= Empty_Node
;
1373 -- No need to Scan past "is", Prj.Dect.Parse will do it.
1376 (Declarations
=> Project_Declaration
,
1377 Current_Project
=> Project
,
1378 Extends
=> Extended_Project
);
1379 Set_Project_Declaration_Of
(Project
, Project_Declaration
);
1381 if Extended_Project
/= Empty_Node
then
1382 Set_Extending_Project_Of
1383 (Project_Declaration_Of
(Extended_Project
), To
=> Project
);
1387 Expect
(Tok_End
, "END");
1388 Remove_Next_End_Node
;
1390 -- Skip "end" if present
1392 if Token
= Tok_End
then
1400 -- Store the name following "end" in the Buffer. The name may be made of
1401 -- several simple names.
1404 Expect
(Tok_Identifier
, "identifier");
1406 -- If we don't have an identifier, clear the buffer before exiting to
1407 -- avoid checking the name.
1409 if Token
/= Tok_Identifier
then
1414 -- Add the identifier to the Buffer
1415 Get_Name_String
(Token_Name
);
1416 Add_To_Buffer
(Name_Buffer
(1 .. Name_Len
));
1418 -- Scan past the identifier
1421 exit when Token
/= Tok_Dot
;
1422 Add_To_Buffer
(".");
1426 -- If we have a valid name, check if it is the name of the project
1428 if Name_Of_Project
/= No_Name
and then Buffer_Last
> 0 then
1429 if To_Lower
(Buffer
(1 .. Buffer_Last
)) /=
1430 Get_Name_String
(Name_Of
(Project
))
1432 -- Invalid name: report an error
1434 Error_Msg
("Expected """ &
1435 Get_Name_String
(Name_Of
(Project
)) & """",
1440 Expect
(Tok_Semicolon
, "`;`");
1442 -- Check that there is no more text following the end of the project
1445 if Token
= Tok_Semicolon
then
1446 Set_Previous_End_Node
(Project
);
1449 if Token
/= Tok_EOF
then
1451 ("Unexpected text following end of project", Token_Ptr
);
1455 -- Restore the scan state, in case we are not the main project
1457 Restore_Project_Scan_State
(Project_Scan_State
);
1459 -- And remove the project from the project stack
1461 Project_Stack
.Decrement_Last
;
1463 -- Indicate if there are unkept comments
1465 Tree
.Set_Project_File_Includes_Unkept_Comments
1466 (Node
=> Project
, To
=> Tree
.There_Are_Unkept_Comments
);
1468 -- And restore the comment state that was saved
1470 Tree
.Restore
(Project_Comment_State
);
1471 end Parse_Single_Project
;
1473 -----------------------
1474 -- Project_Name_From --
1475 -----------------------
1477 function Project_Name_From
(Path_Name
: String) return Name_Id
is
1478 Canonical
: String (1 .. Path_Name
'Length) := Path_Name
;
1479 First
: Natural := Canonical
'Last;
1480 Last
: Natural := First
;
1484 if Current_Verbosity
= High
then
1485 Write_Str
("Project_Name_From (""");
1486 Write_Str
(Canonical
);
1490 -- If the path name is empty, return No_Name to indicate failure
1496 Canonical_Case_File_Name
(Canonical
);
1498 -- Look for the last dot in the path name
1502 Canonical
(First
) /= '.'
1507 -- If we have a dot, check that it is followed by the correct extension
1509 if First
> 0 and then Canonical
(First
) = '.' then
1510 if Canonical
(First
.. Last
) = Project_File_Extension
1513 -- Look for the last directory separator, if any
1519 and then Canonical
(First
) /= '/'
1520 and then Canonical
(First
) /= Dir_Sep
1526 -- Not the correct extension, return No_Name to indicate failure
1531 -- If no dot in the path name, return No_Name to indicate failure
1539 -- If the extension is the file name, return No_Name to indicate failure
1541 if First
> Last
then
1545 -- Put the name in lower case into Name_Buffer
1547 Name_Len
:= Last
- First
+ 1;
1548 Name_Buffer
(1 .. Name_Len
) := To_Lower
(Canonical
(First
.. Last
));
1552 -- Check if it is a well formed project name. Return No_Name if it is
1556 if not Is_Letter
(Name_Buffer
(Index
)) then
1563 exit when Index
>= Name_Len
;
1565 if Name_Buffer
(Index
) = '_' then
1566 if Name_Buffer
(Index
+ 1) = '_' then
1571 exit when Name_Buffer
(Index
) = '-';
1573 if Name_Buffer
(Index
) /= '_'
1574 and then not Is_Alphanumeric
(Name_Buffer
(Index
))
1582 if Index
>= Name_Len
then
1583 if Is_Alphanumeric
(Name_Buffer
(Name_Len
)) then
1585 -- All checks have succeeded. Return name in Name_Buffer
1593 elsif Name_Buffer
(Index
) = '-' then
1597 end Project_Name_From
;
1599 --------------------------
1600 -- Project_Path_Name_Of --
1601 --------------------------
1603 function Project_Path_Name_Of
1604 (Project_File_Name
: String;
1605 Directory
: String) return String
1607 Result
: String_Access
;
1610 if Current_Verbosity
= High
then
1611 Write_Str
("Project_Path_Name_Of (""");
1612 Write_Str
(Project_File_Name
);
1613 Write_Str
(""", """);
1614 Write_Str
(Directory
);
1615 Write_Line
(""");");
1618 if not Is_Absolute_Path
(Project_File_Name
) then
1619 -- First we try <directory>/<file_name>.<extension>
1621 if Current_Verbosity
= High
then
1622 Write_Str
(" Trying ");
1623 Write_Str
(Directory
);
1624 Write_Char
(Directory_Separator
);
1625 Write_Str
(Project_File_Name
);
1626 Write_Line
(Project_File_Extension
);
1631 (File_Name
=> Directory
& Directory_Separator
&
1632 Project_File_Name
& Project_File_Extension
,
1633 Path
=> Project_Path
.all);
1635 -- Then we try <directory>/<file_name>
1637 if Result
= null then
1638 if Current_Verbosity
= High
then
1639 Write_Str
(" Trying ");
1640 Write_Str
(Directory
);
1641 Write_Char
(Directory_Separator
);
1642 Write_Line
(Project_File_Name
);
1647 (File_Name
=> Directory
& Directory_Separator
&
1649 Path
=> Project_Path
.all);
1653 if Result
= null then
1655 -- Then we try <file_name>.<extension>
1657 if Current_Verbosity
= High
then
1658 Write_Str
(" Trying ");
1659 Write_Str
(Project_File_Name
);
1660 Write_Line
(Project_File_Extension
);
1665 (File_Name
=> Project_File_Name
& Project_File_Extension
,
1666 Path
=> Project_Path
.all);
1669 if Result
= null then
1671 -- Then we try <file_name>
1673 if Current_Verbosity
= High
then
1674 Write_Str
(" Trying ");
1675 Write_Line
(Project_File_Name
);
1680 (File_Name
=> Project_File_Name
,
1681 Path
=> Project_Path
.all);
1684 -- If we cannot find the project file, we return an empty string
1686 if Result
= null then
1691 Final_Result
: constant String :=
1692 GNAT
.OS_Lib
.Normalize_Pathname
1694 Resolve_Links
=> False,
1695 Case_Sensitive
=> True);
1698 return Final_Result
;
1701 end Project_Path_Name_Of
;
1704 -- Initialize Project_Path during package elaboration
1706 if Prj_Path
.all = "" then
1708 new String'("." & Path_Separator & Sdefault.Search_Dir_Prefix.all &
1709 ".." & Directory_Separator & ".." & Directory_Separator &
1710 ".." & Directory_Separator & "gnat");
1712 Project_Path := new String'("." & Path_Separator
& Prj_Path
.all);