1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2009-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 Atree; use Atree;
27 with Debug; use Debug;
29 with Lib.Util; use Lib.Util;
30 with Namet; use Namet;
31 with Nlists; use Nlists;
33 with Output; use Output;
36 with Sinfo; use Sinfo;
37 with Sinput; use Sinput;
38 with Snames; use Snames;
41 with GNAT.HTable; use GNAT.HTable;
42 with GNAT.Heap_Sort_G;
44 package body Par_SCO is
46 -----------------------
47 -- Unit Number Table --
48 -----------------------
50 -- This table parallels the SCO_Unit_Table, keeping track of the unit
51 -- numbers corresponding to the entries made in this table, so that before
52 -- writing out the SCO information to the ALI file, we can fill in the
53 -- proper dependency numbers and file names.
55 -- Note that the zero'th entry is here for convenience in sorting the
56 -- table, the real lower bound is 1.
58 package SCO_Unit_Number_Table is new Table.Table (
59 Table_Component_Type => Unit_Number_Type,
60 Table_Index_Type => SCO_Unit_Index,
61 Table_Low_Bound => 0, -- see note above on sort
63 Table_Increment => 200,
64 Table_Name => "SCO_Unit_Number_Entry");
66 ---------------------------------
67 -- Condition/Pragma Hash Table --
68 ---------------------------------
70 -- We need to be able to get to conditions quickly for handling the calls
71 -- to Set_SCO_Condition efficiently, and similarly to get to pragmas to
72 -- handle calls to Set_SCO_Pragma_Enabled. For this purpose we identify the
73 -- conditions and pragmas in the table by their starting sloc, and use this
74 -- hash table to map from these starting sloc values to SCO_Table indexes.
76 type Header_Num is new Integer range 0 .. 996;
77 -- Type for hash table headers
79 function Hash (F : Source_Ptr) return Header_Num;
80 -- Function to Hash source pointer value
82 function Equal (F1, F2 : Source_Ptr) return Boolean;
83 -- Function to test two keys for equality
85 package Condition_Pragma_Hash_Table is new Simple_HTable
86 (Header_Num, Int, 0, Source_Ptr, Hash, Equal);
87 -- The actual hash table
89 --------------------------
90 -- Internal Subprograms --
91 --------------------------
93 function Has_Decision (N : Node_Id) return Boolean;
94 -- N is the node for a subexpression. Returns True if the subexpression
95 -- contains a nested decision (i.e. either is a logical operator, or
96 -- contains a logical operator in its subtree).
98 function Is_Logical_Operator (N : Node_Id) return Boolean;
99 -- N is the node for a subexpression. This procedure just tests N to see
100 -- if it is a logical operator (including short circuit conditions, but
101 -- excluding OR and AND) and returns True if so, False otherwise, it does
102 -- no other processing.
104 procedure Process_Decisions (N : Node_Id; T : Character);
105 -- If N is Empty, has no effect. Otherwise scans the tree for the node N,
106 -- to output any decisions it contains. T is one of IEGPWX (for context of
107 -- expression: if/exit when/entry guard/pragma/while/expression). If T is
108 -- other than X, the node N is the conditional expression involved, and a
109 -- decision is always present (at the very least a simple decision is
110 -- present at the top level).
112 procedure Process_Decisions (L : List_Id; T : Character);
113 -- Calls above procedure for each element of the list L
115 procedure Set_Table_Entry
121 Pragma_Sloc : Source_Ptr := No_Location);
122 -- Append an entry to SCO_Table with fields set as per arguments
124 procedure Traverse_Declarations_Or_Statements (L : List_Id);
125 procedure Traverse_Generic_Instantiation (N : Node_Id);
126 procedure Traverse_Generic_Package_Declaration (N : Node_Id);
127 procedure Traverse_Handled_Statement_Sequence (N : Node_Id);
128 procedure Traverse_Package_Body (N : Node_Id);
129 procedure Traverse_Package_Declaration (N : Node_Id);
130 procedure Traverse_Protected_Body (N : Node_Id);
131 procedure Traverse_Subprogram_Or_Task_Body (N : Node_Id);
132 procedure Traverse_Subprogram_Declaration (N : Node_Id);
133 -- Traverse the corresponding construct, generating SCO table entries
135 procedure Write_SCOs_To_ALI_File is new Put_SCOs;
136 -- Write SCO information to the ALI file using routines in Lib.Util
144 -- Dump SCO unit table
146 Write_Line ("SCO Unit Table");
147 Write_Line ("--------------");
149 for Index in 1 .. SCO_Unit_Table.Last loop
151 UTE : SCO_Unit_Table_Entry renames SCO_Unit_Table.Table (Index);
155 Write_Int (Int (Index));
156 Write_Str (". Dep_Num = ");
157 Write_Int (Int (UTE.Dep_Num));
158 Write_Str (" From = ");
159 Write_Int (Int (UTE.From));
160 Write_Str (" To = ");
161 Write_Int (Int (UTE.To));
163 Write_Str (" File_Name = """);
165 if UTE.File_Name /= null then
166 Write_Str (UTE.File_Name.all);
174 -- Dump SCO Unit number table if it contains any entries
176 if SCO_Unit_Number_Table.Last >= 1 then
178 Write_Line ("SCO Unit Number Table");
179 Write_Line ("---------------------");
181 for Index in 1 .. SCO_Unit_Number_Table.Last loop
183 Write_Int (Int (Index));
184 Write_Str (". Unit_Number = ");
185 Write_Int (Int (SCO_Unit_Number_Table.Table (Index)));
190 -- Dump SCO table itself
193 Write_Line ("SCO Table");
194 Write_Line ("---------");
196 for Index in 1 .. SCO_Table.Last loop
198 T : SCO_Table_Entry renames SCO_Table.Table (Index);
206 Write_Str (" C1 = '");
212 Write_Str (" C2 = '");
217 if T.From /= No_Source_Location then
218 Write_Str (" From = ");
219 Write_Int (Int (T.From.Line));
221 Write_Int (Int (T.From.Col));
224 if T.To /= No_Source_Location then
225 Write_Str (" To = ");
226 Write_Int (Int (T.To.Line));
228 Write_Int (Int (T.To.Col));
234 Write_Str (" False");
246 function Equal (F1, F2 : Source_Ptr) return Boolean is
255 function Has_Decision (N : Node_Id) return Boolean is
257 function Check_Node (N : Node_Id) return Traverse_Result;
263 function Check_Node (N : Node_Id) return Traverse_Result is
265 if Is_Logical_Operator (N) then
272 function Traverse is new Traverse_Func (Check_Node);
274 -- Start of processing for Has_Decision
277 return Traverse (N) = Abandon;
284 function Hash (F : Source_Ptr) return Header_Num is
286 return Header_Num (Nat (F) mod 997);
293 procedure Initialize is
295 SCO_Unit_Number_Table.Init;
297 -- Set dummy 0'th entry in place for sort
299 SCO_Unit_Number_Table.Increment_Last;
302 -------------------------
303 -- Is_Logical_Operator --
304 -------------------------
306 function Is_Logical_Operator (N : Node_Id) return Boolean is
308 return Nkind_In (N, N_Op_Not,
311 end Is_Logical_Operator;
313 -----------------------
314 -- Process_Decisions --
315 -----------------------
317 -- Version taking a list
319 procedure Process_Decisions (L : List_Id; T : Character) is
324 while Present (N) loop
325 Process_Decisions (N, T);
329 end Process_Decisions;
331 -- Version taking a node
333 Pragma_Sloc : Source_Ptr := No_Location;
334 -- While processing decisions within a pragma Assert/Debug/PPC, this is set
335 -- to the sloc of the pragma.
337 procedure Process_Decisions (N : Node_Id; T : Character) is
339 -- This is used to mark the location of a decision sequence in the SCO
340 -- table. We use it for backing out a simple decision in an expression
341 -- context that contains only NOT operators.
343 X_Not_Decision : Boolean;
344 -- This flag keeps track of whether a decision sequence in the SCO table
345 -- contains only NOT operators, and is for an expression context (T=X).
346 -- The flag will be set False if T is other than X, or if an operator
347 -- other than NOT is in the sequence.
349 function Process_Node (N : Node_Id) return Traverse_Result;
350 -- Processes one node in the traversal, looking for logical operators,
351 -- and if one is found, outputs the appropriate table entries.
353 procedure Output_Decision_Operand (N : Node_Id);
354 -- The node N is the top level logical operator of a decision, or it is
355 -- one of the operands of a logical operator belonging to a single
356 -- complex decision. This routine outputs the sequence of table entries
357 -- corresponding to the node. Note that we do not process the sub-
358 -- operands to look for further decisions, that processing is done in
359 -- Process_Decision_Operand, because we can't get decisions mixed up in
360 -- the global table. Call has no effect if N is Empty.
362 procedure Output_Element (N : Node_Id);
363 -- Node N is an operand of a logical operator that is not itself a
364 -- logical operator, or it is a simple decision. This routine outputs
365 -- the table entry for the element, with C1 set to ' '. Last is set
366 -- False, and an entry is made in the condition hash table.
368 procedure Output_Header (T : Character);
369 -- Outputs a decision header node. T is I/W/E/P for IF/WHILE/EXIT WHEN/
370 -- PRAGMA, and 'X' for the expression case.
372 procedure Process_Decision_Operand (N : Node_Id);
373 -- This is called on node N, the top level node of a decision, or on one
374 -- of its operands or suboperands after generating the full output for
375 -- the complex decision. It process the suboperands of the decision
376 -- looking for nested decisions.
378 -----------------------------
379 -- Output_Decision_Operand --
380 -----------------------------
382 procedure Output_Decision_Operand (N : Node_Id) is
392 elsif Is_Logical_Operator (N) then
393 if Nkind (N) = N_Op_Not then
400 if Nkind_In (N, N_Op_Or, N_Or_Else) then
414 Output_Decision_Operand (L);
415 Output_Decision_Operand (Right_Opnd (N));
417 -- Not a logical operator
422 end Output_Decision_Operand;
428 procedure Output_Element (N : Node_Id) is
432 Sloc_Range (N, FSloc, LSloc);
439 Condition_Pragma_Hash_Table.Set (FSloc, SCO_Table.Last);
446 procedure Output_Header (T : Character) is
447 Loc : Source_Ptr := No_Location;
448 -- Node whose sloc is used for the decision
452 when 'I' | 'E' | 'W' =>
454 -- For IF, EXIT, WHILE, the token SLOC can be found from
455 -- the SLOC of the parent of the expression.
457 Loc := Sloc (Parent (N));
461 -- For entry, the token sloc is from the N_Entry_Body. For
462 -- PRAGMA, we must get the location from the pragma node.
463 -- Argument N is the pragma argument, and we have to go up two
464 -- levels (through the pragma argument association) to get to
465 -- the pragma node itself.
467 Loc := Sloc (Parent (Parent (N)));
469 -- Record sloc of pragma (pragmas don't nest)
471 pragma Assert (Pragma_Sloc = No_Location);
476 -- For an expression, no Sloc
480 -- No other possibilities
492 Pragma_Sloc => Pragma_Sloc);
496 -- For pragmas we also must make an entry in the hash table for
497 -- later access by Set_SCO_Pragma_Enabled. We set the pragma as
498 -- disabled now, the call will change C2 to 'e' to enable the
499 -- pragma header entry.
501 SCO_Table.Table (SCO_Table.Last).C2 := 'd';
502 Condition_Pragma_Hash_Table.Set (Loc, SCO_Table.Last);
506 ------------------------------
507 -- Process_Decision_Operand --
508 ------------------------------
510 procedure Process_Decision_Operand (N : Node_Id) is
512 if Is_Logical_Operator (N) then
513 if Nkind (N) /= N_Op_Not then
514 Process_Decision_Operand (Left_Opnd (N));
515 X_Not_Decision := False;
518 Process_Decision_Operand (Right_Opnd (N));
521 Process_Decisions (N, 'X');
523 end Process_Decision_Operand;
529 function Process_Node (N : Node_Id) return Traverse_Result is
533 -- Logical operators, output table entries and then process
534 -- operands recursively to deal with nested conditions.
544 -- If outer level, then type comes from call, otherwise it
545 -- is more deeply nested and counts as X for expression.
547 if N = Process_Decisions.N then
548 T := Process_Decisions.T;
553 -- Output header for sequence
555 X_Not_Decision := T = 'X' and then Nkind (N) = N_Op_Not;
556 Mark := SCO_Table.Last;
559 -- Output the decision
561 Output_Decision_Operand (N);
563 -- If the decision was in an expression context (T = 'X')
564 -- and contained only NOT operators, then we don't output
567 if X_Not_Decision then
568 SCO_Table.Set_Last (Mark);
570 -- Otherwise, set Last in last table entry to mark end
573 SCO_Table.Table (SCO_Table.Last).Last := True;
576 -- Process any embedded decisions
578 Process_Decision_Operand (N);
584 when N_Case_Expression =>
587 -- Conditional expression, processed like an if statement
589 when N_Conditional_Expression =>
591 Cond : constant Node_Id := First (Expressions (N));
592 Thnx : constant Node_Id := Next (Cond);
593 Elsx : constant Node_Id := Next (Thnx);
595 Process_Decisions (Cond, 'I');
596 Process_Decisions (Thnx, 'X');
597 Process_Decisions (Elsx, 'X');
601 -- All other cases, continue scan
609 procedure Traverse is new Traverse_Proc (Process_Node);
611 -- Start of processing for Process_Decisions
618 -- See if we have simple decision at outer level and if so then
619 -- generate the decision entry for this simple decision. A simple
620 -- decision is a boolean expression (which is not a logical operator
621 -- or short circuit form) appearing as the operand of an IF, WHILE,
622 -- EXIT WHEN, or special PRAGMA construct.
624 if T /= 'X' and then not Is_Logical_Operator (N) then
628 -- Change Last in last table entry to True to mark end of
629 -- sequence, which is this case is only one element long.
631 SCO_Table.Table (SCO_Table.Last).Last := True;
636 -- Reset Pragma_Sloc after full subtree traversal
639 Pragma_Sloc := No_Location;
641 end Process_Decisions;
649 procedure Write_Info_Char (C : Character) renames Write_Char;
650 -- Write one character;
652 procedure Write_Info_Initiate (Key : Character) renames Write_Char;
653 -- Start new one and write one character;
655 procedure Write_Info_Nat (N : Nat);
658 procedure Write_Info_Terminate renames Write_Eol;
659 -- Terminate current line
665 procedure Write_Info_Nat (N : Nat) is
670 procedure Debug_Put_SCOs is new Put_SCOs;
672 -- Start of processing for pscos
682 procedure SCO_Output is
684 if Debug_Flag_Dot_OO then
688 -- Sort the unit tables based on dependency numbers
690 Unit_Table_Sort : declare
692 function Lt (Op1, Op2 : Natural) return Boolean;
693 -- Comparison routine for sort call
695 procedure Move (From : Natural; To : Natural);
696 -- Move routine for sort call
702 function Lt (Op1, Op2 : Natural) return Boolean is
706 (SCO_Unit_Number_Table.Table (SCO_Unit_Index (Op1)))
709 (SCO_Unit_Number_Table.Table (SCO_Unit_Index (Op2)));
716 procedure Move (From : Natural; To : Natural) is
718 SCO_Unit_Table.Table (SCO_Unit_Index (To)) :=
719 SCO_Unit_Table.Table (SCO_Unit_Index (From));
720 SCO_Unit_Number_Table.Table (SCO_Unit_Index (To)) :=
721 SCO_Unit_Number_Table.Table (SCO_Unit_Index (From));
724 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
726 -- Start of processing for Unit_Table_Sort
729 Sorting.Sort (Integer (SCO_Unit_Table.Last));
732 -- Loop through entries in the unit table to set file name and
733 -- dependency number entries.
735 for J in 1 .. SCO_Unit_Table.Last loop
737 U : constant Unit_Number_Type := SCO_Unit_Number_Table.Table (J);
738 UTE : SCO_Unit_Table_Entry renames SCO_Unit_Table.Table (J);
740 Get_Name_String (Reference_Name (Source_Index (U)));
741 UTE.File_Name := new String'(Name_Buffer (1 .. Name_Len));
742 UTE.Dep_Num := Dependency_Num (U);
746 -- Now the tables are all setup for output to the ALI file
748 Write_SCOs_To_ALI_File;
751 -------------------------
752 -- SCO_Pragma_Disabled --
753 -------------------------
755 function SCO_Pragma_Disabled (Loc : Source_Ptr) return Boolean is
759 if Loc = No_Location then
763 Index := Condition_Pragma_Hash_Table.Get (Loc);
765 -- The test here for zero is to deal with possible previous errors
768 pragma Assert (SCO_Table.Table (Index).C1 = 'P');
769 return SCO_Table.Table (Index).C2 = 'd';
774 end SCO_Pragma_Disabled;
780 procedure SCO_Record (U : Unit_Number_Type) is
785 -- Ignore call if not generating code and generating SCO's
787 if not (Generate_SCO and then Operating_Mode = Generate_Code) then
791 -- Ignore call if this unit already recorded
793 for J in 1 .. SCO_Unit_Number_Table.Last loop
794 if U = SCO_Unit_Number_Table.Table (J) then
799 -- Otherwise record starting entry
801 From := SCO_Table.Last + 1;
803 -- Get Unit (checking case of subunit)
805 Lu := Unit (Cunit (U));
807 if Nkind (Lu) = N_Subunit then
808 Lu := Proper_Body (Lu);
814 when N_Protected_Body =>
815 Traverse_Protected_Body (Lu);
817 when N_Subprogram_Body | N_Task_Body =>
818 Traverse_Subprogram_Or_Task_Body (Lu);
820 when N_Subprogram_Declaration =>
821 Traverse_Subprogram_Declaration (Lu);
823 when N_Package_Declaration =>
824 Traverse_Package_Declaration (Lu);
826 when N_Package_Body =>
827 Traverse_Package_Body (Lu);
829 when N_Generic_Package_Declaration =>
830 Traverse_Generic_Package_Declaration (Lu);
832 when N_Generic_Instantiation =>
833 Traverse_Generic_Instantiation (Lu);
837 -- All other cases of compilation units (e.g. renamings), generate
838 -- no SCO information.
843 -- Make entry for new unit in unit tables, we will fill in the file
844 -- name and dependency numbers later.
846 SCO_Unit_Table.Append (
850 To => SCO_Table.Last));
852 SCO_Unit_Number_Table.Append (U);
855 -----------------------
856 -- Set_SCO_Condition --
857 -----------------------
859 procedure Set_SCO_Condition (Cond : Node_Id; Val : Boolean) is
860 Orig : constant Node_Id := Original_Node (Cond);
865 Constant_Condition_Code : constant array (Boolean) of Character :=
866 (False => 'f', True => 't');
868 Sloc_Range (Orig, Start, Dummy);
869 Index := Condition_Pragma_Hash_Table.Get (Start);
871 -- The test here for zero is to deal with possible previous errors
874 pragma Assert (SCO_Table.Table (Index).C1 = ' ');
875 SCO_Table.Table (Index).C2 := Constant_Condition_Code (Val);
877 end Set_SCO_Condition;
879 ----------------------------
880 -- Set_SCO_Pragma_Enabled --
881 ----------------------------
883 procedure Set_SCO_Pragma_Enabled (Loc : Source_Ptr) is
887 -- Note: the reason we use the Sloc value as the key is that in the
888 -- generic case, the call to this procedure is made on a copy of the
889 -- original node, so we can't use the Node_Id value.
891 Index := Condition_Pragma_Hash_Table.Get (Loc);
893 -- The test here for zero is to deal with possible previous errors
896 pragma Assert (SCO_Table.Table (Index).C1 = 'P');
897 SCO_Table.Table (Index).C2 := 'e';
899 end Set_SCO_Pragma_Enabled;
901 ---------------------
902 -- Set_Table_Entry --
903 ---------------------
905 procedure Set_Table_Entry
911 Pragma_Sloc : Source_Ptr := No_Location)
913 function To_Source_Location (S : Source_Ptr) return Source_Location;
914 -- Converts Source_Ptr value to Source_Location (line/col) format
916 ------------------------
917 -- To_Source_Location --
918 ------------------------
920 function To_Source_Location (S : Source_Ptr) return Source_Location is
922 if S = No_Location then
923 return No_Source_Location;
926 (Line => Get_Logical_Line_Number (S),
927 Col => Get_Column_Number (S));
929 end To_Source_Location;
931 -- Start of processing for Set_Table_Entry
937 From => To_Source_Location (From),
938 To => To_Source_Location (To),
940 Pragma_Sloc => Pragma_Sloc);
943 -----------------------------------------
944 -- Traverse_Declarations_Or_Statements --
945 -----------------------------------------
947 -- Tables used by Traverse_Declarations_Or_Statements for temporarily
948 -- holding statement and decision entries. These are declared globally
949 -- since they are shared by recursive calls to this procedure.
951 type SC_Entry is record
956 -- Used to store a single entry in the following table, From:To represents
957 -- the range of entries in the CS line entry, and typ is the type, with
958 -- space meaning that no type letter will accompany the entry.
960 package SC is new Table.Table (
961 Table_Component_Type => SC_Entry,
962 Table_Index_Type => Nat,
963 Table_Low_Bound => 1,
964 Table_Initial => 1000,
965 Table_Increment => 200,
966 Table_Name => "SCO_SC");
967 -- Used to store statement components for a CS entry to be output
968 -- as a result of the call to this procedure. SC.Last is the last
969 -- entry stored, so the current statement sequence is represented
970 -- by SC_Array (SC_First .. SC.Last), where SC_First is saved on
971 -- entry to each recursive call to the routine.
973 -- Extend_Statement_Sequence adds an entry to this array, and then
974 -- Set_Statement_Entry clears the entries starting with SC_First,
975 -- copying these entries to the main SCO output table. The reason that
976 -- we do the temporary caching of results in this array is that we want
977 -- the SCO table entries for a given CS line to be contiguous, and the
978 -- processing may output intermediate entries such as decision entries.
980 type SD_Entry is record
985 -- Used to store a single entry in the following table. Nod is the node to
986 -- be searched for decisions for the case of Process_Decisions_Defer with a
987 -- node argument (with Lst set to No_List. Lst is the list to be searched
988 -- for decisions for the case of Process_Decisions_Defer with a List
989 -- argument (in which case Nod is set to Empty).
991 package SD is new Table.Table (
992 Table_Component_Type => SD_Entry,
993 Table_Index_Type => Nat,
994 Table_Low_Bound => 1,
995 Table_Initial => 1000,
996 Table_Increment => 200,
997 Table_Name => "SCO_SD");
998 -- Used to store possible decision information. Instead of calling the
999 -- Process_Decisions procedures directly, we call Process_Decisions_Defer,
1000 -- which simply stores the arguments in this table. Then when we clear
1001 -- out a statement sequence using Set_Statement_Entry, after generating
1002 -- the CS lines for the statements, the entries in this table result in
1003 -- calls to Process_Decision. The reason for doing things this way is to
1004 -- ensure that decisions are output after the CS line for the statements
1005 -- in which the decisions occur.
1007 procedure Traverse_Declarations_Or_Statements (L : List_Id) is
1011 SC_First : constant Nat := SC.Last + 1;
1012 SD_First : constant Nat := SD.Last + 1;
1013 -- Record first entries used in SC/SD at this recursive level
1015 procedure Extend_Statement_Sequence (N : Node_Id; Typ : Character);
1016 -- Extend the current statement sequence to encompass the node N. Typ
1017 -- is the letter that identifies the type of statement/declaration that
1018 -- is being added to the sequence.
1020 procedure Extend_Statement_Sequence
1024 -- This version extends the current statement sequence with an entry
1025 -- that starts with the first token of From, and ends with the last
1026 -- token of To. It is used for example in a CASE statement to cover
1027 -- the range from the CASE token to the last token of the expression.
1029 procedure Set_Statement_Entry;
1030 -- If Start is No_Location, does nothing, otherwise outputs a SCO_Table
1031 -- statement entry for the range Start-Stop and then sets both Start
1032 -- and Stop to No_Location.
1033 -- What are Start and Stop??? This comment seems completely unrelated
1034 -- to the implementation!???
1035 -- Unconditionally sets Term to True. What is Term???
1036 -- This is called when we find a statement or declaration that generates
1037 -- its own table entry, so that we must end the current statement
1040 procedure Process_Decisions_Defer (N : Node_Id; T : Character);
1041 pragma Inline (Process_Decisions_Defer);
1042 -- This routine is logically the same as Process_Decisions, except that
1043 -- the arguments are saved in the SD table, for later processing when
1044 -- Set_Statement_Entry is called, which goes through the saved entries
1045 -- making the corresponding calls to Process_Decision.
1047 procedure Process_Decisions_Defer (L : List_Id; T : Character);
1048 pragma Inline (Process_Decisions_Defer);
1049 -- Same case for list arguments, deferred call to Process_Decisions
1051 -------------------------
1052 -- Set_Statement_Entry --
1053 -------------------------
1055 procedure Set_Statement_Entry is
1057 SC_Last : constant Int := SC.Last;
1058 SD_Last : constant Int := SD.Last;
1061 -- Output statement entries from saved entries in SC table
1063 for J in SC_First .. SC_Last loop
1064 if J = SC_First then
1071 SCE : SC_Entry renames SC.Table (J);
1078 Last => (J = SC_Last));
1082 -- Clear out used section of SC table
1084 SC.Set_Last (SC_First - 1);
1086 -- Output any embedded decisions
1088 for J in SD_First .. SD_Last loop
1090 SDE : SD_Entry renames SD.Table (J);
1092 if Present (SDE.Nod) then
1093 Process_Decisions (SDE.Nod, SDE.Typ);
1095 Process_Decisions (SDE.Lst, SDE.Typ);
1100 -- Clear out used section of SD table
1102 SD.Set_Last (SD_First - 1);
1103 end Set_Statement_Entry;
1105 -------------------------------
1106 -- Extend_Statement_Sequence --
1107 -------------------------------
1109 procedure Extend_Statement_Sequence (N : Node_Id; Typ : Character) is
1113 Sloc_Range (N, F, T);
1114 SC.Append ((F, T, Typ));
1115 end Extend_Statement_Sequence;
1117 procedure Extend_Statement_Sequence
1125 Sloc_Range (From, F, Dummy);
1126 Sloc_Range (To, Dummy, T);
1127 SC.Append ((F, T, Typ));
1128 end Extend_Statement_Sequence;
1130 -----------------------------
1131 -- Process_Decisions_Defer --
1132 -----------------------------
1134 procedure Process_Decisions_Defer (N : Node_Id; T : Character) is
1136 SD.Append ((N, No_List, T));
1137 end Process_Decisions_Defer;
1139 procedure Process_Decisions_Defer (L : List_Id; T : Character) is
1141 SD.Append ((Empty, L, T));
1142 end Process_Decisions_Defer;
1144 -- Start of processing for Traverse_Declarations_Or_Statements
1147 if Is_Non_Empty_List (L) then
1149 -- Loop through statements or declarations
1152 while Present (N) loop
1154 -- Initialize or extend current statement sequence. Note that for
1155 -- special cases such as IF and Case statements we will modify
1156 -- the range to exclude internal statements that should not be
1157 -- counted as part of the current statement sequence.
1161 -- Package declaration
1163 when N_Package_Declaration =>
1164 Set_Statement_Entry;
1165 Traverse_Package_Declaration (N);
1167 -- Generic package declaration
1169 when N_Generic_Package_Declaration =>
1170 Set_Statement_Entry;
1171 Traverse_Generic_Package_Declaration (N);
1175 when N_Package_Body =>
1176 Set_Statement_Entry;
1177 Traverse_Package_Body (N);
1179 -- Subprogram declaration
1181 when N_Subprogram_Declaration =>
1182 Process_Decisions_Defer
1183 (Parameter_Specifications (Specification (N)), 'X');
1184 Set_Statement_Entry;
1186 -- Generic subprogram declaration
1188 when N_Generic_Subprogram_Declaration =>
1189 Process_Decisions_Defer
1190 (Generic_Formal_Declarations (N), 'X');
1191 Process_Decisions_Defer
1192 (Parameter_Specifications (Specification (N)), 'X');
1193 Set_Statement_Entry;
1195 -- Task or subprogram body
1197 when N_Task_Body | N_Subprogram_Body =>
1198 Set_Statement_Entry;
1199 Traverse_Subprogram_Or_Task_Body (N);
1203 when N_Entry_Body =>
1205 Cond : constant Node_Id :=
1206 Condition (Entry_Body_Formal_Part (N));
1209 Set_Statement_Entry;
1211 if Present (Cond) then
1212 Process_Decisions_Defer (Cond, 'G');
1215 Traverse_Subprogram_Or_Task_Body (N);
1220 when N_Protected_Body =>
1221 Set_Statement_Entry;
1222 Traverse_Protected_Body (N);
1224 -- Exit statement, which is an exit statement in the SCO sense,
1225 -- so it is included in the current statement sequence, but
1226 -- then it terminates this sequence. We also have to process
1227 -- any decisions in the exit statement expression.
1229 when N_Exit_Statement =>
1230 Extend_Statement_Sequence (N, ' ');
1231 Process_Decisions_Defer (Condition (N), 'E');
1232 Set_Statement_Entry;
1234 -- Label, which breaks the current statement sequence, but the
1235 -- label itself is not included in the next statement sequence,
1236 -- since it generates no code.
1239 Set_Statement_Entry;
1241 -- Block statement, which breaks the current statement sequence
1243 when N_Block_Statement =>
1244 Set_Statement_Entry;
1245 Traverse_Declarations_Or_Statements (Declarations (N));
1246 Traverse_Handled_Statement_Sequence
1247 (Handled_Statement_Sequence (N));
1249 -- If statement, which breaks the current statement sequence,
1250 -- but we include the condition in the current sequence.
1252 when N_If_Statement =>
1253 Extend_Statement_Sequence (N, Condition (N), 'I');
1254 Process_Decisions_Defer (Condition (N), 'I');
1255 Set_Statement_Entry;
1257 -- Now we traverse the statements in the THEN part
1259 Traverse_Declarations_Or_Statements (Then_Statements (N));
1261 -- Loop through ELSIF parts if present
1263 if Present (Elsif_Parts (N)) then
1265 Elif : Node_Id := First (Elsif_Parts (N));
1268 while Present (Elif) loop
1270 -- We generate a statement sequence for the
1271 -- construct "ELSIF condition", so that we have
1272 -- a statement for the resulting decisions.
1274 Extend_Statement_Sequence
1275 (Elif, Condition (Elif), 'I');
1276 Process_Decisions_Defer (Condition (Elif), 'I');
1277 Set_Statement_Entry;
1279 -- Traverse the statements in the ELSIF
1281 Traverse_Declarations_Or_Statements
1282 (Then_Statements (Elif));
1288 -- Finally traverse the ELSE statements if present
1290 Traverse_Declarations_Or_Statements (Else_Statements (N));
1292 -- Case statement, which breaks the current statement sequence,
1293 -- but we include the expression in the current sequence.
1295 when N_Case_Statement =>
1296 Extend_Statement_Sequence (N, Expression (N), 'C');
1297 Process_Decisions_Defer (Expression (N), 'X');
1298 Set_Statement_Entry;
1300 -- Process case branches
1305 Alt := First (Alternatives (N));
1306 while Present (Alt) loop
1307 Traverse_Declarations_Or_Statements (Statements (Alt));
1312 -- Unconditional exit points, which are included in the current
1313 -- statement sequence, but then terminate it
1315 when N_Requeue_Statement |
1317 N_Raise_Statement =>
1318 Extend_Statement_Sequence (N, ' ');
1319 Set_Statement_Entry;
1321 -- Simple return statement. which is an exit point, but we
1322 -- have to process the return expression for decisions.
1324 when N_Simple_Return_Statement =>
1325 Extend_Statement_Sequence (N, ' ');
1326 Process_Decisions_Defer (Expression (N), 'X');
1327 Set_Statement_Entry;
1329 -- Extended return statement
1331 when N_Extended_Return_Statement =>
1332 Extend_Statement_Sequence
1333 (N, Last (Return_Object_Declarations (N)), 'R');
1334 Process_Decisions_Defer
1335 (Return_Object_Declarations (N), 'X');
1336 Set_Statement_Entry;
1338 Traverse_Handled_Statement_Sequence
1339 (Handled_Statement_Sequence (N));
1341 -- Loop ends the current statement sequence, but we include
1342 -- the iteration scheme if present in the current sequence.
1343 -- But the body of the loop starts a new sequence, since it
1344 -- may not be executed as part of the current sequence.
1346 when N_Loop_Statement =>
1347 if Present (Iteration_Scheme (N)) then
1349 -- If iteration scheme present, extend the current
1350 -- statement sequence to include the iteration scheme
1351 -- and process any decisions it contains.
1354 ISC : constant Node_Id := Iteration_Scheme (N);
1359 if Present (Condition (ISC)) then
1360 Extend_Statement_Sequence (N, ISC, 'W');
1361 Process_Decisions_Defer (Condition (ISC), 'W');
1366 Extend_Statement_Sequence (N, ISC, 'F');
1367 Process_Decisions_Defer
1368 (Loop_Parameter_Specification (ISC), 'X');
1373 Set_Statement_Entry;
1374 Traverse_Declarations_Or_Statements (Statements (N));
1379 Extend_Statement_Sequence (N, 'P');
1381 -- Processing depends on the kind of pragma
1383 case Pragma_Name (N) is
1387 Name_Postcondition =>
1389 -- For Assert/Check/Precondition/Postcondition, we
1390 -- must generate a P entry for the decision. Note that
1391 -- this is done unconditionally at this stage. Output
1392 -- for disabled pragmas is suppressed later on, when
1393 -- we output the decision line in Put_SCOs.
1396 Nam : constant Name_Id :=
1397 Chars (Pragma_Identifier (N));
1399 First (Pragma_Argument_Associations (N));
1402 if Nam = Name_Check then
1406 Process_Decisions_Defer (Expression (Arg), 'P');
1409 -- For all other pragmas, we generate decision entries
1410 -- for any embedded expressions.
1413 Process_Decisions_Defer (N, 'X');
1416 -- Object declaration. Ignored if Prev_Ids is set, since the
1417 -- parser generates multiple instances of the whole declaration
1418 -- if there is more than one identifier declared, and we only
1419 -- want one entry in the SCO's, so we take the first, for which
1420 -- Prev_Ids is False.
1422 when N_Object_Declaration =>
1423 if not Prev_Ids (N) then
1424 Extend_Statement_Sequence (N, 'o');
1426 if Has_Decision (N) then
1427 Process_Decisions_Defer (N, 'X');
1431 -- All other cases, which extend the current statement sequence
1432 -- but do not terminate it, even if they have nested decisions.
1436 -- Determine required type character code
1443 when N_Full_Type_Declaration |
1444 N_Incomplete_Type_Declaration |
1445 N_Private_Type_Declaration |
1446 N_Private_Extension_Declaration =>
1449 when N_Subtype_Declaration =>
1452 when N_Renaming_Declaration =>
1455 when N_Generic_Instantiation =>
1462 Extend_Statement_Sequence (N, Typ);
1465 -- Process any embedded decisions
1467 if Has_Decision (N) then
1468 Process_Decisions_Defer (N, 'X');
1475 Set_Statement_Entry;
1477 end Traverse_Declarations_Or_Statements;
1479 ------------------------------------
1480 -- Traverse_Generic_Instantiation --
1481 ------------------------------------
1483 procedure Traverse_Generic_Instantiation (N : Node_Id) is
1488 -- First we need a statement entry to cover the instantiation
1490 Sloc_Range (N, First, Last);
1498 -- Now output any embedded decisions
1500 Process_Decisions (N, 'X');
1501 end Traverse_Generic_Instantiation;
1503 ------------------------------------------
1504 -- Traverse_Generic_Package_Declaration --
1505 ------------------------------------------
1507 procedure Traverse_Generic_Package_Declaration (N : Node_Id) is
1509 Process_Decisions (Generic_Formal_Declarations (N), 'X');
1510 Traverse_Package_Declaration (N);
1511 end Traverse_Generic_Package_Declaration;
1513 -----------------------------------------
1514 -- Traverse_Handled_Statement_Sequence --
1515 -----------------------------------------
1517 procedure Traverse_Handled_Statement_Sequence (N : Node_Id) is
1521 -- For package bodies without a statement part, the parser adds an empty
1522 -- one, to normalize the representation. The null statement therein,
1523 -- which does not come from source, does not get a SCO.
1525 if Present (N) and then Comes_From_Source (N) then
1526 Traverse_Declarations_Or_Statements (Statements (N));
1528 if Present (Exception_Handlers (N)) then
1529 Handler := First (Exception_Handlers (N));
1530 while Present (Handler) loop
1531 Traverse_Declarations_Or_Statements (Statements (Handler));
1536 end Traverse_Handled_Statement_Sequence;
1538 ---------------------------
1539 -- Traverse_Package_Body --
1540 ---------------------------
1542 procedure Traverse_Package_Body (N : Node_Id) is
1544 Traverse_Declarations_Or_Statements (Declarations (N));
1545 Traverse_Handled_Statement_Sequence (Handled_Statement_Sequence (N));
1546 end Traverse_Package_Body;
1548 ----------------------------------
1549 -- Traverse_Package_Declaration --
1550 ----------------------------------
1552 procedure Traverse_Package_Declaration (N : Node_Id) is
1553 Spec : constant Node_Id := Specification (N);
1555 Traverse_Declarations_Or_Statements (Visible_Declarations (Spec));
1556 Traverse_Declarations_Or_Statements (Private_Declarations (Spec));
1557 end Traverse_Package_Declaration;
1559 -----------------------------
1560 -- Traverse_Protected_Body --
1561 -----------------------------
1563 procedure Traverse_Protected_Body (N : Node_Id) is
1565 Traverse_Declarations_Or_Statements (Declarations (N));
1566 end Traverse_Protected_Body;
1568 --------------------------------------
1569 -- Traverse_Subprogram_Or_Task_Body --
1570 --------------------------------------
1572 procedure Traverse_Subprogram_Or_Task_Body (N : Node_Id) is
1574 Traverse_Declarations_Or_Statements (Declarations (N));
1575 Traverse_Handled_Statement_Sequence (Handled_Statement_Sequence (N));
1576 end Traverse_Subprogram_Or_Task_Body;
1578 -------------------------------------
1579 -- Traverse_Subprogram_Declaration --
1580 -------------------------------------
1582 procedure Traverse_Subprogram_Declaration (N : Node_Id) is
1583 ADN : constant Node_Id := Aux_Decls_Node (Parent (N));
1585 Traverse_Declarations_Or_Statements (Config_Pragmas (ADN));
1586 Traverse_Declarations_Or_Statements (Declarations (ADN));
1587 Traverse_Declarations_Or_Statements (Pragmas_After (ADN));
1588 end Traverse_Subprogram_Declaration;