1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2011, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Handling; use Ada.Characters.Handling;
28 with Output; use Output;
31 package body Prj.PP is
35 Not_Tested : array (Project_Node_Kind) of Boolean := (others => True);
37 procedure Indicate_Tested (Kind : Project_Node_Kind);
38 -- Set the corresponding component of array Not_Tested to False.
39 -- Only called by pragmas Debug.
45 procedure Indicate_Tested (Kind : Project_Node_Kind) is
47 Not_Tested (Kind) := False;
54 procedure Pretty_Print
55 (Project : Prj.Tree.Project_Node_Id;
56 In_Tree : Prj.Tree.Project_Node_Tree_Ref;
57 Increment : Positive := 3;
58 Eliminate_Empty_Case_Constructions : Boolean := False;
59 Minimize_Empty_Lines : Boolean := False;
60 W_Char : Write_Char_Ap := null;
61 W_Eol : Write_Eol_Ap := null;
62 W_Str : Write_Str_Ap := null;
63 Backward_Compatibility : Boolean;
64 Id : Prj.Project_Id := Prj.No_Project;
65 Max_Line_Length : Max_Length_Of_Line :=
66 Max_Length_Of_Line'Last)
68 procedure Print (Node : Project_Node_Id; Indent : Natural);
69 -- A recursive procedure that traverses a project file tree and outputs
70 -- its source. Current_Prj is the project that we are printing. This
71 -- is used when printing attributes, since in nested packages they
72 -- need to use a fully qualified name.
74 procedure Output_Attribute_Name (Name : Name_Id; Indent : Natural);
75 -- Outputs an attribute name, taking into account the value of
76 -- Backward_Compatibility.
81 Capitalize : Boolean := True);
84 procedure Start_Line (Indent : Natural);
85 -- Outputs the indentation at the beginning of the line
87 procedure Output_String (S : Name_Id; Indent : Natural);
88 procedure Output_String (S : Path_Name_Type; Indent : Natural);
89 -- Outputs a string using the default output procedures
91 procedure Write_Empty_Line (Always : Boolean := False);
92 -- Outputs an empty line, only if the previous line was not empty
93 -- already and either Always is True or Minimize_Empty_Lines is
96 procedure Write_Line (S : String);
97 -- Outputs S followed by a new line
99 procedure Write_String
102 Truncated : Boolean := False);
103 -- Outputs S using Write_Str, starting a new line if line would
104 -- become too long, when Truncated = False.
105 -- When Truncated = True, only the part of the string that can fit on
106 -- the line is output.
108 procedure Write_End_Of_Line_Comment (Node : Project_Node_Id);
110 Write_Char : Write_Char_Ap := Output.Write_Char'Access;
111 Write_Eol : Write_Eol_Ap := Output.Write_Eol'Access;
112 Write_Str : Write_Str_Ap := Output.Write_Str'Access;
113 -- These three access to procedure values are used for the output
115 Last_Line_Is_Empty : Boolean := False;
116 -- Used to avoid two consecutive empty lines
118 Column : Natural := 0;
119 -- Column number of the last character in the line. Used to avoid
120 -- outputting lines longer than Max_Line_Length.
122 First_With_In_List : Boolean := True;
123 -- Indicate that the next with clause is first in a list such as
125 -- First_With_In_List will be True for "A", but not for "B".
127 ---------------------------
128 -- Output_Attribute_Name --
129 ---------------------------
131 procedure Output_Attribute_Name (Name : Name_Id; Indent : Natural) is
133 if Backward_Compatibility then
135 when Snames.Name_Spec =>
136 Output_Name (Snames.Name_Specification, Indent);
138 when Snames.Name_Spec_Suffix =>
139 Output_Name (Snames.Name_Specification_Suffix, Indent);
141 when Snames.Name_Body =>
142 Output_Name (Snames.Name_Implementation, Indent);
144 when Snames.Name_Body_Suffix =>
145 Output_Name (Snames.Name_Implementation_Suffix, Indent);
148 Output_Name (Name, Indent);
152 Output_Name (Name, Indent);
154 end Output_Attribute_Name;
160 procedure Output_Name
163 Capitalize : Boolean := True)
165 Capital : Boolean := Capitalize;
168 if Column = 0 and then Indent /= 0 then
169 Start_Line (Indent + Increment);
172 Get_Name_String (Name);
174 -- If line would become too long, create new line
176 if Column + Name_Len > Max_Line_Length then
181 Start_Line (Indent + Increment);
185 for J in 1 .. Name_Len loop
187 Write_Char (To_Upper (Name_Buffer (J)));
189 Write_Char (Name_Buffer (J));
194 Name_Buffer (J) = '_'
195 or else Is_Digit (Name_Buffer (J));
199 Column := Column + Name_Len;
206 procedure Output_String (S : Name_Id; Indent : Natural) is
208 if Column = 0 and then Indent /= 0 then
209 Start_Line (Indent + Increment);
214 -- If line could become too long, create new line. Note that the
215 -- number of characters on the line could be twice the number of
216 -- character in the string (if every character is a '"') plus two
217 -- (the initial and final '"').
219 if Column + Name_Len + Name_Len + 2 > Max_Line_Length then
224 Start_Line (Indent + Increment);
229 Column := Column + 1;
232 for J in 1 .. Name_Len loop
233 if Name_Buffer (J) = '"' then
236 Column := Column + 2;
238 Write_Char (Name_Buffer (J));
239 Column := Column + 1;
242 -- If the string does not fit on one line, cut it in parts and
245 if J < Name_Len and then Column >= Max_Line_Length then
249 Start_Line (Indent + Increment);
251 Column := Column + 1;
256 Column := Column + 1;
259 procedure Output_String (S : Path_Name_Type; Indent : Natural) is
261 Output_String (Name_Id (S), Indent);
268 procedure Start_Line (Indent : Natural) is
270 if not Minimize_Empty_Lines then
271 Write_Str ((1 .. Indent => ' '));
272 Column := Column + Indent;
276 ----------------------
277 -- Write_Empty_Line --
278 ----------------------
280 procedure Write_Empty_Line (Always : Boolean := False) is
282 if (Always or else not Minimize_Empty_Lines)
283 and then not Last_Line_Is_Empty then
286 Last_Line_Is_Empty := True;
288 end Write_Empty_Line;
290 -------------------------------
291 -- Write_End_Of_Line_Comment --
292 -------------------------------
294 procedure Write_End_Of_Line_Comment (Node : Project_Node_Id) is
295 Value : constant Name_Id := End_Of_Line_Comment (Node, In_Tree);
298 if Value /= No_Name then
299 Write_String (" --", 0);
300 Write_String (Get_Name_String (Value), 0, Truncated => True);
304 end Write_End_Of_Line_Comment;
310 procedure Write_Line (S : String) is
313 Last_Line_Is_Empty := False;
322 procedure Write_String
325 Truncated : Boolean := False) is
326 Length : Natural := S'Length;
328 if Column = 0 and then Indent /= 0 then
329 Start_Line (Indent + Increment);
332 -- If the string would not fit on the line,
335 if Column + Length > Max_Line_Length then
337 Length := Max_Line_Length - Column;
344 Start_Line (Indent + Increment);
349 Write_Str (S (S'First .. S'First + Length - 1));
350 Column := Column + Length;
357 procedure Print (Node : Project_Node_Id; Indent : Natural) is
359 if Present (Node) then
361 case Kind_Of (Node, In_Tree) is
364 pragma Debug (Indicate_Tested (N_Project));
365 if Present (First_With_Clause_Of (Node, In_Tree)) then
369 First_With_In_List := True;
370 Print (First_With_Clause_Of (Node, In_Tree), Indent);
371 Write_Empty_Line (Always => True);
374 Print (First_Comment_Before (Node, In_Tree), Indent);
377 case Project_Qualifier_Of (Node, In_Tree) is
378 when Unspecified | Standard =>
381 Write_String ("aggregate ", Indent);
382 when Aggregate_Library =>
383 Write_String ("aggregate library ", Indent);
385 Write_String ("library ", Indent);
386 when Configuration =>
387 Write_String ("configuration ", Indent);
389 Write_String ("abstract ", Indent);
392 Write_String ("project ", Indent);
394 if Id /= Prj.No_Project then
395 Output_Name (Id.Display_Name, Indent);
397 Output_Name (Name_Of (Node, In_Tree), Indent);
400 -- Check if this project extends another project
402 if Extended_Project_Path_Of (Node, In_Tree) /= No_Path then
403 Write_String (" extends ", Indent);
405 if Is_Extending_All (Node, In_Tree) then
406 Write_String ("all ", Indent);
410 (Extended_Project_Path_Of (Node, In_Tree),
414 Write_String (" is", Indent);
415 Write_End_Of_Line_Comment (Node);
417 (First_Comment_After (Node, In_Tree), Indent + Increment);
418 Write_Empty_Line (Always => True);
420 -- Output all of the declarations in the project
422 Print (Project_Declaration_Of (Node, In_Tree), Indent);
424 (First_Comment_Before_End (Node, In_Tree),
427 Write_String ("end ", Indent);
429 if Id /= Prj.No_Project then
430 Output_Name (Id.Display_Name, Indent);
432 Output_Name (Name_Of (Node, In_Tree), Indent);
436 Print (First_Comment_After_End (Node, In_Tree), Indent);
438 when N_With_Clause =>
439 pragma Debug (Indicate_Tested (N_With_Clause));
441 -- The with clause will sometimes contain an invalid name
442 -- when we are importing a virtual project from an
443 -- extending all project. Do not output anything in this
446 if Name_Of (Node, In_Tree) /= No_Name
447 and then String_Value_Of (Node, In_Tree) /= No_Name
449 if First_With_In_List then
450 Print (First_Comment_Before (Node, In_Tree), Indent);
453 if Non_Limited_Project_Node_Of (Node, In_Tree) =
456 Write_String ("limited ", Indent);
459 Write_String ("with ", Indent);
462 Output_String (String_Value_Of (Node, In_Tree), Indent);
464 if Is_Not_Last_In_List (Node, In_Tree) then
465 Write_String (", ", Indent);
466 First_With_In_List := False;
469 Write_String (";", Indent);
470 Write_End_Of_Line_Comment (Node);
471 Print (First_Comment_After (Node, In_Tree), Indent);
472 First_With_In_List := True;
476 Print (Next_With_Clause_Of (Node, In_Tree), Indent);
478 when N_Project_Declaration =>
479 pragma Debug (Indicate_Tested (N_Project_Declaration));
482 Present (First_Declarative_Item_Of (Node, In_Tree))
485 (First_Declarative_Item_Of (Node, In_Tree),
487 Write_Empty_Line (Always => True);
490 when N_Declarative_Item =>
491 pragma Debug (Indicate_Tested (N_Declarative_Item));
492 Print (Current_Item_Node (Node, In_Tree), Indent);
493 Print (Next_Declarative_Item (Node, In_Tree), Indent);
495 when N_Package_Declaration =>
496 pragma Debug (Indicate_Tested (N_Package_Declaration));
497 Write_Empty_Line (Always => True);
498 Print (First_Comment_Before (Node, In_Tree), Indent);
500 Write_String ("package ", Indent);
501 Output_Name (Name_Of (Node, In_Tree), Indent);
503 if Project_Of_Renamed_Package_Of (Node, In_Tree) /=
506 Write_String (" renames ", Indent);
509 (Project_Of_Renamed_Package_Of (Node, In_Tree),
512 Write_String (".", Indent);
513 Output_Name (Name_Of (Node, In_Tree), Indent);
514 Write_String (";", Indent);
515 Write_End_Of_Line_Comment (Node);
516 Print (First_Comment_After_End (Node, In_Tree), Indent);
519 Write_String (" is", Indent);
520 Write_End_Of_Line_Comment (Node);
521 Print (First_Comment_After (Node, In_Tree),
524 if First_Declarative_Item_Of (Node, In_Tree) /=
528 (First_Declarative_Item_Of (Node, In_Tree),
532 Print (First_Comment_Before_End (Node, In_Tree),
535 Write_String ("end ", Indent);
536 Output_Name (Name_Of (Node, In_Tree), Indent);
538 Print (First_Comment_After_End (Node, In_Tree), Indent);
542 when N_String_Type_Declaration =>
543 pragma Debug (Indicate_Tested (N_String_Type_Declaration));
544 Print (First_Comment_Before (Node, In_Tree), Indent);
546 Write_String ("type ", Indent);
547 Output_Name (Name_Of (Node, In_Tree), Indent);
549 Start_Line (Indent + Increment);
550 Write_String ("(", Indent);
553 String_Node : Project_Node_Id :=
554 First_Literal_String (Node, In_Tree);
557 while Present (String_Node) loop
559 (String_Value_Of (String_Node, In_Tree),
562 Next_Literal_String (String_Node, In_Tree);
564 if Present (String_Node) then
565 Write_String (", ", Indent);
570 Write_String (");", Indent);
571 Write_End_Of_Line_Comment (Node);
572 Print (First_Comment_After (Node, In_Tree), Indent);
574 when N_Literal_String =>
575 pragma Debug (Indicate_Tested (N_Literal_String));
576 Output_String (String_Value_Of (Node, In_Tree), Indent);
578 if Source_Index_Of (Node, In_Tree) /= 0 then
579 Write_String (" at", Indent);
581 (Source_Index_Of (Node, In_Tree)'Img,
585 when N_Attribute_Declaration =>
586 pragma Debug (Indicate_Tested (N_Attribute_Declaration));
587 Print (First_Comment_Before (Node, In_Tree), Indent);
589 Write_String ("for ", Indent);
590 Output_Attribute_Name (Name_Of (Node, In_Tree), Indent);
592 if Associative_Array_Index_Of (Node, In_Tree) /= No_Name then
593 Write_String (" (", Indent);
595 (Associative_Array_Index_Of (Node, In_Tree),
598 if Source_Index_Of (Node, In_Tree) /= 0 then
599 Write_String (" at", Indent);
601 (Source_Index_Of (Node, In_Tree)'Img,
605 Write_String (")", Indent);
608 Write_String (" use ", Indent);
610 if Present (Expression_Of (Node, In_Tree)) then
611 Print (Expression_Of (Node, In_Tree), Indent);
614 -- Full associative array declaration
617 Present (Associative_Project_Of (Node, In_Tree))
621 (Associative_Project_Of (Node, In_Tree),
626 Present (Associative_Package_Of (Node, In_Tree))
628 Write_String (".", Indent);
631 (Associative_Package_Of (Node, In_Tree),
637 Present (Associative_Package_Of (Node, In_Tree))
641 (Associative_Package_Of (Node, In_Tree),
646 Write_String ("'", Indent);
647 Output_Attribute_Name (Name_Of (Node, In_Tree), Indent);
650 Write_String (";", Indent);
651 Write_End_Of_Line_Comment (Node);
652 Print (First_Comment_After (Node, In_Tree), Indent);
654 when N_Typed_Variable_Declaration =>
656 (Indicate_Tested (N_Typed_Variable_Declaration));
657 Print (First_Comment_Before (Node, In_Tree), Indent);
659 Output_Name (Name_Of (Node, In_Tree), Indent);
660 Write_String (" : ", Indent);
662 (Name_Of (String_Type_Of (Node, In_Tree), In_Tree),
664 Write_String (" := ", Indent);
665 Print (Expression_Of (Node, In_Tree), Indent);
666 Write_String (";", Indent);
667 Write_End_Of_Line_Comment (Node);
668 Print (First_Comment_After (Node, In_Tree), Indent);
670 when N_Variable_Declaration =>
671 pragma Debug (Indicate_Tested (N_Variable_Declaration));
672 Print (First_Comment_Before (Node, In_Tree), Indent);
674 Output_Name (Name_Of (Node, In_Tree), Indent);
675 Write_String (" := ", Indent);
676 Print (Expression_Of (Node, In_Tree), Indent);
677 Write_String (";", Indent);
678 Write_End_Of_Line_Comment (Node);
679 Print (First_Comment_After (Node, In_Tree), Indent);
682 pragma Debug (Indicate_Tested (N_Expression));
684 Term : Project_Node_Id := First_Term (Node, In_Tree);
687 while Present (Term) loop
688 Print (Term, Indent);
689 Term := Next_Term (Term, In_Tree);
691 if Present (Term) then
692 Write_String (" & ", Indent);
698 pragma Debug (Indicate_Tested (N_Term));
699 Print (Current_Term (Node, In_Tree), Indent);
701 when N_Literal_String_List =>
702 pragma Debug (Indicate_Tested (N_Literal_String_List));
703 Write_String ("(", Indent);
706 Expression : Project_Node_Id :=
707 First_Expression_In_List (Node, In_Tree);
710 while Present (Expression) loop
711 Print (Expression, Indent);
713 Next_Expression_In_List (Expression, In_Tree);
715 if Present (Expression) then
716 Write_String (", ", Indent);
721 Write_String (")", Indent);
723 when N_Variable_Reference =>
724 pragma Debug (Indicate_Tested (N_Variable_Reference));
725 if Present (Project_Node_Of (Node, In_Tree)) then
727 (Name_Of (Project_Node_Of (Node, In_Tree), In_Tree),
729 Write_String (".", Indent);
732 if Present (Package_Node_Of (Node, In_Tree)) then
734 (Name_Of (Package_Node_Of (Node, In_Tree), In_Tree),
736 Write_String (".", Indent);
739 Output_Name (Name_Of (Node, In_Tree), Indent);
741 when N_External_Value =>
742 pragma Debug (Indicate_Tested (N_External_Value));
743 Write_String ("external (", Indent);
744 Print (External_Reference_Of (Node, In_Tree), Indent);
746 if Present (External_Default_Of (Node, In_Tree)) then
747 Write_String (", ", Indent);
748 Print (External_Default_Of (Node, In_Tree), Indent);
751 Write_String (")", Indent);
753 when N_Attribute_Reference =>
754 pragma Debug (Indicate_Tested (N_Attribute_Reference));
756 if Present (Project_Node_Of (Node, In_Tree))
757 and then Project_Node_Of (Node, In_Tree) /= Project
760 (Name_Of (Project_Node_Of (Node, In_Tree), In_Tree),
763 if Present (Package_Node_Of (Node, In_Tree)) then
764 Write_String (".", Indent);
766 (Name_Of (Package_Node_Of (Node, In_Tree), In_Tree),
770 elsif Present (Package_Node_Of (Node, In_Tree)) then
772 (Name_Of (Package_Node_Of (Node, In_Tree), In_Tree),
776 Write_String ("project", Indent);
779 Write_String ("'", Indent);
780 Output_Attribute_Name (Name_Of (Node, In_Tree), Indent);
783 Index : constant Name_Id :=
784 Associative_Array_Index_Of (Node, In_Tree);
787 if Index /= No_Name then
788 Write_String (" (", Indent);
789 Output_String (Index, Indent);
790 Write_String (")", Indent);
794 when N_Case_Construction =>
795 pragma Debug (Indicate_Tested (N_Case_Construction));
798 Case_Item : Project_Node_Id;
799 Is_Non_Empty : Boolean := False;
802 Case_Item := First_Case_Item_Of (Node, In_Tree);
803 while Present (Case_Item) loop
805 (First_Declarative_Item_Of (Case_Item, In_Tree))
806 or else not Eliminate_Empty_Case_Constructions
808 Is_Non_Empty := True;
812 Case_Item := Next_Case_Item (Case_Item, In_Tree);
817 Print (First_Comment_Before (Node, In_Tree), Indent);
819 Write_String ("case ", Indent);
821 (Case_Variable_Reference_Of (Node, In_Tree),
823 Write_String (" is", Indent);
824 Write_End_Of_Line_Comment (Node);
826 (First_Comment_After (Node, In_Tree),
830 Case_Item : Project_Node_Id :=
831 First_Case_Item_Of (Node, In_Tree);
833 while Present (Case_Item) loop
835 (Kind_Of (Case_Item, In_Tree) = N_Case_Item);
836 Print (Case_Item, Indent + Increment);
838 Next_Case_Item (Case_Item, In_Tree);
842 Print (First_Comment_Before_End (Node, In_Tree),
845 Write_Line ("end case;");
847 (First_Comment_After_End (Node, In_Tree), Indent);
852 pragma Debug (Indicate_Tested (N_Case_Item));
854 if Present (First_Declarative_Item_Of (Node, In_Tree))
855 or else not Eliminate_Empty_Case_Constructions
858 Print (First_Comment_Before (Node, In_Tree), Indent);
860 Write_String ("when ", Indent);
862 if No (First_Choice_Of (Node, In_Tree)) then
863 Write_String ("others", Indent);
867 Label : Project_Node_Id :=
868 First_Choice_Of (Node, In_Tree);
870 while Present (Label) loop
871 Print (Label, Indent);
872 Label := Next_Literal_String (Label, In_Tree);
874 if Present (Label) then
875 Write_String (" | ", Indent);
881 Write_String (" =>", Indent);
882 Write_End_Of_Line_Comment (Node);
884 (First_Comment_After (Node, In_Tree),
888 First : constant Project_Node_Id :=
889 First_Declarative_Item_Of (Node, In_Tree);
894 Print (First, Indent + Increment);
899 when N_Comment_Zones =>
901 -- Nothing to do, because it will not be processed directly
906 pragma Debug (Indicate_Tested (N_Comment));
908 if Follows_Empty_Line (Node, In_Tree) then
913 Write_String ("--", Indent);
915 (Get_Name_String (String_Value_Of (Node, In_Tree)),
920 if Is_Followed_By_Empty_Line (Node, In_Tree) then
924 Print (Next_Comment (Node, In_Tree), Indent);
929 -- Start of processing for Pretty_Print
932 if W_Char = null then
933 Write_Char := Output.Write_Char'Access;
935 Write_Char := W_Char;
939 Write_Eol := Output.Write_Eol'Access;
945 Write_Str := Output.Write_Str'Access;
953 -----------------------
954 -- Output_Statistics --
955 -----------------------
957 procedure Output_Statistics is
959 Output.Write_Line ("Project_Node_Kinds not tested:");
961 for Kind in Project_Node_Kind loop
962 if Kind /= N_Comment_Zones and then Not_Tested (Kind) then
963 Output.Write_Str (" ");
964 Output.Write_Line (Project_Node_Kind'Image (Kind));
969 end Output_Statistics;
976 (Project : Prj.Tree.Project_Node_Id;
977 In_Tree : Prj.Tree.Project_Node_Tree_Ref) is
979 Pretty_Print (Project, In_Tree, Backward_Compatibility => False);