1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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. --
18 -- You should have received a copy of the GNU General Public License along --
19 -- with this program; see file COPYING3. If not see --
20 -- <http://www.gnu.org/licenses/>. --
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 Atree; use Atree;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Fname; use Fname;
36 with Lib.Load; use Lib.Load;
37 with Nlists; use Nlists;
38 with Output; use Output;
39 with Sem_Attr; use Sem_Attr;
40 with Sem_Ch2; use Sem_Ch2;
41 with Sem_Ch3; use Sem_Ch3;
42 with Sem_Ch4; use Sem_Ch4;
43 with Sem_Ch5; use Sem_Ch5;
44 with Sem_Ch6; use Sem_Ch6;
45 with Sem_Ch7; use Sem_Ch7;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Ch9; use Sem_Ch9;
48 with Sem_Ch10; use Sem_Ch10;
49 with Sem_Ch11; use Sem_Ch11;
50 with Sem_Ch12; use Sem_Ch12;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Prag; use Sem_Prag;
53 with Sem_Util; use Sem_Util;
54 with Sinfo; use Sinfo;
55 with Stand; use Stand;
56 with Uintp; use Uintp;
57 with Uname; use Uname;
59 with Unchecked_Deallocation;
61 pragma Warnings (Off, Sem_Util);
62 -- Suppress warnings of unused with for Sem_Util (used only in asserts)
66 Debug_Unit_Walk : Boolean renames Debug_Flag_Dot_WW;
67 -- Controls debugging printouts for Walk_Library_Items
69 Outer_Generic_Scope : Entity_Id := Empty;
70 -- Global reference to the outer scope that is generic. In a non
71 -- generic context, it is empty. At the moment, it is only used
72 -- for avoiding freezing of external references in generics.
74 Comp_Unit_List : Elist_Id := No_Elist;
75 -- Used by Walk_Library_Items. This is a list of N_Compilation_Unit nodes
76 -- processed by Semantics, in an appropriate order. Initialized to
77 -- No_Elist, because it's too early to call New_Elmt_List; we will set it
78 -- to New_Elmt_List on first use.
80 Ignore_Comp_Units : Boolean := False;
81 -- If True, we suppress appending compilation units onto the
84 procedure Write_Unit_Info
85 (Unit_Num : Unit_Number_Type;
87 Prefix : String := "");
88 -- Print out debugging information about the unit
94 procedure Analyze (N : Node_Id) is
96 Debug_A_Entry ("analyzing ", N);
98 -- Immediate return if already analyzed
101 Debug_A_Exit ("analyzing ", N, " (done, analyzed already)");
105 -- Otherwise processing depends on the node kind
109 when N_Abort_Statement =>
110 Analyze_Abort_Statement (N);
112 when N_Abstract_Subprogram_Declaration =>
113 Analyze_Abstract_Subprogram_Declaration (N);
115 when N_Accept_Alternative =>
116 Analyze_Accept_Alternative (N);
118 when N_Accept_Statement =>
119 Analyze_Accept_Statement (N);
122 Analyze_Aggregate (N);
125 Analyze_Allocator (N);
128 Analyze_Short_Circuit (N);
130 when N_Assignment_Statement =>
131 Analyze_Assignment (N);
133 when N_Asynchronous_Select =>
134 Analyze_Asynchronous_Select (N);
137 Analyze_At_Clause (N);
139 when N_Attribute_Reference =>
140 Analyze_Attribute (N);
142 when N_Attribute_Definition_Clause =>
143 Analyze_Attribute_Definition_Clause (N);
145 when N_Block_Statement =>
146 Analyze_Block_Statement (N);
148 when N_Case_Statement =>
149 Analyze_Case_Statement (N);
151 when N_Character_Literal =>
152 Analyze_Character_Literal (N);
154 when N_Code_Statement =>
155 Analyze_Code_Statement (N);
157 when N_Compilation_Unit =>
158 Analyze_Compilation_Unit (N);
160 when N_Component_Declaration =>
161 Analyze_Component_Declaration (N);
163 when N_Conditional_Expression =>
164 Analyze_Conditional_Expression (N);
166 when N_Conditional_Entry_Call =>
167 Analyze_Conditional_Entry_Call (N);
169 when N_Delay_Alternative =>
170 Analyze_Delay_Alternative (N);
172 when N_Delay_Relative_Statement =>
173 Analyze_Delay_Relative (N);
175 when N_Delay_Until_Statement =>
176 Analyze_Delay_Until (N);
179 Analyze_Entry_Body (N);
181 when N_Entry_Body_Formal_Part =>
182 Analyze_Entry_Body_Formal_Part (N);
184 when N_Entry_Call_Alternative =>
185 Analyze_Entry_Call_Alternative (N);
187 when N_Entry_Declaration =>
188 Analyze_Entry_Declaration (N);
190 when N_Entry_Index_Specification =>
191 Analyze_Entry_Index_Specification (N);
193 when N_Enumeration_Representation_Clause =>
194 Analyze_Enumeration_Representation_Clause (N);
196 when N_Exception_Declaration =>
197 Analyze_Exception_Declaration (N);
199 when N_Exception_Renaming_Declaration =>
200 Analyze_Exception_Renaming (N);
202 when N_Exit_Statement =>
203 Analyze_Exit_Statement (N);
205 when N_Expanded_Name =>
206 Analyze_Expanded_Name (N);
208 when N_Explicit_Dereference =>
209 Analyze_Explicit_Dereference (N);
211 when N_Extended_Return_Statement =>
212 Analyze_Extended_Return_Statement (N);
214 when N_Extension_Aggregate =>
215 Analyze_Aggregate (N);
217 when N_Formal_Object_Declaration =>
218 Analyze_Formal_Object_Declaration (N);
220 when N_Formal_Package_Declaration =>
221 Analyze_Formal_Package (N);
223 when N_Formal_Subprogram_Declaration =>
224 Analyze_Formal_Subprogram (N);
226 when N_Formal_Type_Declaration =>
227 Analyze_Formal_Type_Declaration (N);
229 when N_Free_Statement =>
230 Analyze_Free_Statement (N);
232 when N_Freeze_Entity =>
233 null; -- no semantic processing required
235 when N_Full_Type_Declaration =>
236 Analyze_Type_Declaration (N);
238 when N_Function_Call =>
239 Analyze_Function_Call (N);
241 when N_Function_Instantiation =>
242 Analyze_Function_Instantiation (N);
244 when N_Generic_Function_Renaming_Declaration =>
245 Analyze_Generic_Function_Renaming (N);
247 when N_Generic_Package_Declaration =>
248 Analyze_Generic_Package_Declaration (N);
250 when N_Generic_Package_Renaming_Declaration =>
251 Analyze_Generic_Package_Renaming (N);
253 when N_Generic_Procedure_Renaming_Declaration =>
254 Analyze_Generic_Procedure_Renaming (N);
256 when N_Generic_Subprogram_Declaration =>
257 Analyze_Generic_Subprogram_Declaration (N);
259 when N_Goto_Statement =>
260 Analyze_Goto_Statement (N);
262 when N_Handled_Sequence_Of_Statements =>
263 Analyze_Handled_Statements (N);
266 Analyze_Identifier (N);
268 when N_If_Statement =>
269 Analyze_If_Statement (N);
271 when N_Implicit_Label_Declaration =>
272 Analyze_Implicit_Label_Declaration (N);
275 Analyze_Membership_Op (N);
277 when N_Incomplete_Type_Declaration =>
278 Analyze_Incomplete_Type_Decl (N);
280 when N_Indexed_Component =>
281 Analyze_Indexed_Component_Form (N);
283 when N_Integer_Literal =>
284 Analyze_Integer_Literal (N);
286 when N_Itype_Reference =>
287 Analyze_Itype_Reference (N);
292 when N_Loop_Statement =>
293 Analyze_Loop_Statement (N);
296 Analyze_Membership_Op (N);
301 when N_Null_Statement =>
302 Analyze_Null_Statement (N);
304 when N_Number_Declaration =>
305 Analyze_Number_Declaration (N);
307 when N_Object_Declaration =>
308 Analyze_Object_Declaration (N);
310 when N_Object_Renaming_Declaration =>
311 Analyze_Object_Renaming (N);
313 when N_Operator_Symbol =>
314 Analyze_Operator_Symbol (N);
317 Analyze_Unary_Op (N);
320 Analyze_Arithmetic_Op (N);
323 Analyze_Logical_Op (N);
326 Analyze_Concatenation (N);
329 Analyze_Arithmetic_Op (N);
332 Analyze_Equality_Op (N);
335 Analyze_Arithmetic_Op (N);
338 Analyze_Comparison_Op (N);
341 Analyze_Comparison_Op (N);
344 Analyze_Comparison_Op (N);
347 Analyze_Comparison_Op (N);
350 Analyze_Unary_Op (N);
353 Analyze_Arithmetic_Op (N);
355 when N_Op_Multiply =>
356 Analyze_Arithmetic_Op (N);
359 Analyze_Equality_Op (N);
362 Analyze_Negation (N);
365 Analyze_Logical_Op (N);
368 Analyze_Unary_Op (N);
371 Analyze_Arithmetic_Op (N);
373 when N_Op_Rotate_Left =>
374 Analyze_Arithmetic_Op (N);
376 when N_Op_Rotate_Right =>
377 Analyze_Arithmetic_Op (N);
379 when N_Op_Shift_Left =>
380 Analyze_Arithmetic_Op (N);
382 when N_Op_Shift_Right =>
383 Analyze_Arithmetic_Op (N);
385 when N_Op_Shift_Right_Arithmetic =>
386 Analyze_Arithmetic_Op (N);
388 when N_Op_Subtract =>
389 Analyze_Arithmetic_Op (N);
392 Analyze_Logical_Op (N);
395 Analyze_Short_Circuit (N);
397 when N_Others_Choice =>
398 Analyze_Others_Choice (N);
400 when N_Package_Body =>
401 Analyze_Package_Body (N);
403 when N_Package_Body_Stub =>
404 Analyze_Package_Body_Stub (N);
406 when N_Package_Declaration =>
407 Analyze_Package_Declaration (N);
409 when N_Package_Instantiation =>
410 Analyze_Package_Instantiation (N);
412 when N_Package_Renaming_Declaration =>
413 Analyze_Package_Renaming (N);
415 when N_Package_Specification =>
416 Analyze_Package_Specification (N);
418 when N_Parameter_Association =>
419 Analyze_Parameter_Association (N);
424 when N_Private_Extension_Declaration =>
425 Analyze_Private_Extension_Declaration (N);
427 when N_Private_Type_Declaration =>
428 Analyze_Private_Type_Declaration (N);
430 when N_Procedure_Call_Statement =>
431 Analyze_Procedure_Call (N);
433 when N_Procedure_Instantiation =>
434 Analyze_Procedure_Instantiation (N);
436 when N_Protected_Body =>
437 Analyze_Protected_Body (N);
439 when N_Protected_Body_Stub =>
440 Analyze_Protected_Body_Stub (N);
442 when N_Protected_Definition =>
443 Analyze_Protected_Definition (N);
445 when N_Protected_Type_Declaration =>
446 Analyze_Protected_Type (N);
448 when N_Qualified_Expression =>
449 Analyze_Qualified_Expression (N);
451 when N_Raise_Statement =>
452 Analyze_Raise_Statement (N);
454 when N_Raise_xxx_Error =>
455 Analyze_Raise_xxx_Error (N);
460 when N_Range_Constraint =>
461 Analyze_Range (Range_Expression (N));
463 when N_Real_Literal =>
464 Analyze_Real_Literal (N);
466 when N_Record_Representation_Clause =>
467 Analyze_Record_Representation_Clause (N);
470 Analyze_Reference (N);
472 when N_Requeue_Statement =>
475 when N_Simple_Return_Statement =>
476 Analyze_Simple_Return_Statement (N);
478 when N_Selected_Component =>
479 Find_Selected_Component (N);
480 -- ??? why not Analyze_Selected_Component, needs comments
482 when N_Selective_Accept =>
483 Analyze_Selective_Accept (N);
485 when N_Single_Protected_Declaration =>
486 Analyze_Single_Protected (N);
488 when N_Single_Task_Declaration =>
489 Analyze_Single_Task (N);
494 when N_String_Literal =>
495 Analyze_String_Literal (N);
497 when N_Subprogram_Body =>
498 Analyze_Subprogram_Body (N);
500 when N_Subprogram_Body_Stub =>
501 Analyze_Subprogram_Body_Stub (N);
503 when N_Subprogram_Declaration =>
504 Analyze_Subprogram_Declaration (N);
506 when N_Subprogram_Info =>
507 Analyze_Subprogram_Info (N);
509 when N_Subprogram_Renaming_Declaration =>
510 Analyze_Subprogram_Renaming (N);
512 when N_Subtype_Declaration =>
513 Analyze_Subtype_Declaration (N);
515 when N_Subtype_Indication =>
516 Analyze_Subtype_Indication (N);
522 Analyze_Task_Body (N);
524 when N_Task_Body_Stub =>
525 Analyze_Task_Body_Stub (N);
527 when N_Task_Definition =>
528 Analyze_Task_Definition (N);
530 when N_Task_Type_Declaration =>
531 Analyze_Task_Type (N);
533 when N_Terminate_Alternative =>
534 Analyze_Terminate_Alternative (N);
536 when N_Timed_Entry_Call =>
537 Analyze_Timed_Entry_Call (N);
539 when N_Triggering_Alternative =>
540 Analyze_Triggering_Alternative (N);
542 when N_Type_Conversion =>
543 Analyze_Type_Conversion (N);
545 when N_Unchecked_Expression =>
546 Analyze_Unchecked_Expression (N);
548 when N_Unchecked_Type_Conversion =>
549 Analyze_Unchecked_Type_Conversion (N);
551 when N_Use_Package_Clause =>
552 Analyze_Use_Package (N);
554 when N_Use_Type_Clause =>
555 Analyze_Use_Type (N);
557 when N_Validate_Unchecked_Conversion =>
560 when N_Variant_Part =>
561 Analyze_Variant_Part (N);
563 when N_With_Clause =>
564 Analyze_With_Clause (N);
566 -- A call to analyze the Empty node is an error, but most likely
567 -- it is an error caused by an attempt to analyze a malformed
568 -- piece of tree caused by some other error, so if there have
569 -- been any other errors, we just ignore it, otherwise it is
570 -- a real internal error which we complain about.
572 -- We must also consider the case of call to a runtime function
573 -- that is not available in the configurable runtime.
576 pragma Assert (Serious_Errors_Detected /= 0
577 or else Configurable_Run_Time_Violations /= 0);
580 -- A call to analyze the error node is simply ignored, to avoid
581 -- causing cascaded errors (happens of course only in error cases)
586 -- Push/Pop nodes normally don't come through an analyze call. An
587 -- exception is the dummy ones bracketing a subprogram body. In any
588 -- case there is nothing to be done to analyze such nodes.
590 when N_Push_Pop_xxx_Label =>
593 -- For the remaining node types, we generate compiler abort, because
594 -- these nodes are always analyzed within the Sem_Chn routines and
595 -- there should never be a case of making a call to the main Analyze
596 -- routine for these node kinds. For example, an N_Access_Definition
597 -- node appears only in the context of a type declaration, and is
598 -- processed by the analyze routine for type declarations.
602 N_Access_Definition |
603 N_Access_Function_Definition |
604 N_Access_Procedure_Definition |
605 N_Access_To_Object_Definition |
606 N_Case_Statement_Alternative |
607 N_Compilation_Unit_Aux |
608 N_Component_Association |
610 N_Component_Definition |
612 N_Constrained_Array_Definition |
613 N_Decimal_Fixed_Point_Definition |
614 N_Defining_Character_Literal |
615 N_Defining_Identifier |
616 N_Defining_Operator_Symbol |
617 N_Defining_Program_Unit_Name |
619 N_Derived_Type_Definition |
621 N_Digits_Constraint |
622 N_Discriminant_Association |
623 N_Discriminant_Specification |
625 N_Entry_Call_Statement |
626 N_Enumeration_Type_Definition |
627 N_Exception_Handler |
628 N_Floating_Point_Definition |
629 N_Formal_Decimal_Fixed_Point_Definition |
630 N_Formal_Derived_Type_Definition |
631 N_Formal_Discrete_Type_Definition |
632 N_Formal_Floating_Point_Definition |
633 N_Formal_Modular_Type_Definition |
634 N_Formal_Ordinary_Fixed_Point_Definition |
635 N_Formal_Private_Type_Definition |
636 N_Formal_Signed_Integer_Type_Definition |
637 N_Function_Specification |
638 N_Generic_Association |
639 N_Index_Or_Discriminant_Constraint |
641 N_Loop_Parameter_Specification |
643 N_Modular_Type_Definition |
644 N_Ordinary_Fixed_Point_Definition |
645 N_Parameter_Specification |
646 N_Pragma_Argument_Association |
647 N_Procedure_Specification |
648 N_Real_Range_Specification |
649 N_Record_Definition |
650 N_Signed_Integer_Type_Definition |
651 N_Unconstrained_Array_Definition |
659 Debug_A_Exit ("analyzing ", N, " (done)");
661 -- Now that we have analyzed the node, we call the expander to perform
662 -- possible expansion. We skip this for subexpressions, because we don't
663 -- have the type yet, and the expander will need to know the type before
664 -- it can do its job. For subexpression nodes, the call to the expander
665 -- happens in Sem_Res.Resolve. A special exception is Raise_xxx_Error,
666 -- which can appear in a statement context, and needs expanding now in
667 -- the case (distinguished by Etype, as documented in Sinfo).
669 -- The Analyzed flag is also set at this point for non-subexpression
670 -- nodes (in the case of subexpression nodes, we can't set the flag yet,
671 -- since resolution and expansion have not yet been completed). Note
672 -- that for N_Raise_xxx_Error we have to distinguish the expression
673 -- case from the statement case.
675 if Nkind (N) not in N_Subexpr
676 or else (Nkind (N) in N_Raise_xxx_Error
677 and then Etype (N) = Standard_Void_Type)
683 -- Version with check(s) suppressed
685 procedure Analyze (N : Node_Id; Suppress : Check_Id) is
687 if Suppress = All_Checks then
689 Svg : constant Suppress_Array := Scope_Suppress;
691 Scope_Suppress := (others => True);
693 Scope_Suppress := Svg;
698 Svg : constant Boolean := Scope_Suppress (Suppress);
700 Scope_Suppress (Suppress) := True;
702 Scope_Suppress (Suppress) := Svg;
711 procedure Analyze_List (L : List_Id) is
716 while Present (Node) loop
722 -- Version with check(s) suppressed
724 procedure Analyze_List (L : List_Id; Suppress : Check_Id) is
726 if Suppress = All_Checks then
728 Svg : constant Suppress_Array := Scope_Suppress;
730 Scope_Suppress := (others => True);
732 Scope_Suppress := Svg;
737 Svg : constant Boolean := Scope_Suppress (Suppress);
739 Scope_Suppress (Suppress) := True;
741 Scope_Suppress (Suppress) := Svg;
746 --------------------------
747 -- Copy_Suppress_Status --
748 --------------------------
750 procedure Copy_Suppress_Status
756 pragma Warnings (Off, Found);
758 procedure Search_Stack
759 (Top : Suppress_Stack_Entry_Ptr;
760 Found : out Boolean);
761 -- Search given suppress stack for matching entry for entity. If found
762 -- then set Checks_May_Be_Suppressed on To, and push an appropriate
763 -- entry for To onto the local suppress stack.
769 procedure Search_Stack
770 (Top : Suppress_Stack_Entry_Ptr;
773 Ptr : Suppress_Stack_Entry_Ptr;
777 while Ptr /= null loop
779 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
782 Set_Checks_May_Be_Suppressed (To, True);
783 Push_Local_Suppress_Stack_Entry
799 -- Start of processing for Copy_Suppress_Status
802 if not Checks_May_Be_Suppressed (From) then
806 -- First search the local entity suppress stack, we search this in
807 -- reverse order so that we get the innermost entry that applies to
808 -- this case if there are nested entries. Note that for the purpose
809 -- of this procedure we are ONLY looking for entries corresponding
810 -- to a two-argument Suppress, where the second argument matches From.
812 Search_Stack (Global_Suppress_Stack_Top, Found);
818 -- Now search the global entity suppress table for a matching entry
819 -- We also search this in reverse order so that if there are multiple
820 -- pragmas for the same entity, the last one applies.
822 Search_Stack (Local_Suppress_Stack_Top, Found);
823 end Copy_Suppress_Status;
825 -------------------------
826 -- Enter_Generic_Scope --
827 -------------------------
829 procedure Enter_Generic_Scope (S : Entity_Id) is
831 if No (Outer_Generic_Scope) then
832 Outer_Generic_Scope := S;
834 end Enter_Generic_Scope;
836 ------------------------
837 -- Exit_Generic_Scope --
838 ------------------------
840 procedure Exit_Generic_Scope (S : Entity_Id) is
842 if S = Outer_Generic_Scope then
843 Outer_Generic_Scope := Empty;
845 end Exit_Generic_Scope;
847 -----------------------
848 -- Explicit_Suppress --
849 -----------------------
851 function Explicit_Suppress (E : Entity_Id; C : Check_Id) return Boolean is
852 Ptr : Suppress_Stack_Entry_Ptr;
855 if not Checks_May_Be_Suppressed (E) then
859 Ptr := Global_Suppress_Stack_Top;
860 while Ptr /= null loop
862 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
872 end Explicit_Suppress;
874 -----------------------------
875 -- External_Ref_In_Generic --
876 -----------------------------
878 function External_Ref_In_Generic (E : Entity_Id) return Boolean is
882 -- Entity is global if defined outside of current outer_generic_scope:
883 -- Either the entity has a smaller depth that the outer generic, or it
884 -- is in a different compilation unit, or it is defined within a unit
885 -- in the same compilation, that is not within the outer_generic.
887 if No (Outer_Generic_Scope) then
890 elsif Scope_Depth (Scope (E)) < Scope_Depth (Outer_Generic_Scope)
891 or else not In_Same_Source_Unit (E, Outer_Generic_Scope)
898 while Present (Scop) loop
899 if Scop = Outer_Generic_Scope then
901 elsif Scope_Depth (Scop) < Scope_Depth (Outer_Generic_Scope) then
904 Scop := Scope (Scop);
910 end External_Ref_In_Generic;
916 procedure Initialize is
917 Next : Suppress_Stack_Entry_Ptr;
919 procedure Free is new Unchecked_Deallocation
920 (Suppress_Stack_Entry, Suppress_Stack_Entry_Ptr);
923 -- Free any global suppress stack entries from a previous invocation
924 -- of the compiler (in the normal case this loop does nothing).
926 while Suppress_Stack_Entries /= null loop
927 Next := Global_Suppress_Stack_Top.Next;
928 Free (Suppress_Stack_Entries);
929 Suppress_Stack_Entries := Next;
932 Local_Suppress_Stack_Top := null;
933 Global_Suppress_Stack_Top := null;
935 -- Clear scope stack, and reset global variables
938 Unloaded_Subunits := False;
941 ------------------------------
942 -- Insert_After_And_Analyze --
943 ------------------------------
945 procedure Insert_After_And_Analyze (N : Node_Id; M : Node_Id) is
951 -- If we are not at the end of the list, then the easiest
952 -- coding is simply to insert before our successor
954 if Present (Next (N)) then
955 Insert_Before_And_Analyze (Next (N), M);
957 -- Case of inserting at the end of the list
960 -- Capture the Node_Id of the node to be inserted. This Node_Id
961 -- will still be the same after the insert operation.
966 -- Now just analyze from the inserted node to the end of
967 -- the new list (note that this properly handles the case
968 -- where any of the analyze calls result in the insertion of
969 -- nodes after the analyzed node, expecting analysis).
971 while Present (Node) loop
973 Mark_Rewrite_Insertion (Node);
978 end Insert_After_And_Analyze;
980 -- Version with check(s) suppressed
982 procedure Insert_After_And_Analyze
988 if Suppress = All_Checks then
990 Svg : constant Suppress_Array := Scope_Suppress;
992 Scope_Suppress := (others => True);
993 Insert_After_And_Analyze (N, M);
994 Scope_Suppress := Svg;
999 Svg : constant Boolean := Scope_Suppress (Suppress);
1001 Scope_Suppress (Suppress) := True;
1002 Insert_After_And_Analyze (N, M);
1003 Scope_Suppress (Suppress) := Svg;
1006 end Insert_After_And_Analyze;
1008 -------------------------------
1009 -- Insert_Before_And_Analyze --
1010 -------------------------------
1012 procedure Insert_Before_And_Analyze (N : Node_Id; M : Node_Id) is
1018 -- Capture the Node_Id of the first list node to be inserted.
1019 -- This will still be the first node after the insert operation,
1020 -- since Insert_List_After does not modify the Node_Id values.
1023 Insert_Before (N, M);
1025 -- The insertion does not change the Id's of any of the nodes in
1026 -- the list, and they are still linked, so we can simply loop from
1027 -- the original first node until we meet the node before which the
1028 -- insertion is occurring. Note that this properly handles the case
1029 -- where any of the analyzed nodes insert nodes after themselves,
1030 -- expecting them to get analyzed.
1032 while Node /= N loop
1034 Mark_Rewrite_Insertion (Node);
1038 end Insert_Before_And_Analyze;
1040 -- Version with check(s) suppressed
1042 procedure Insert_Before_And_Analyze
1045 Suppress : Check_Id)
1048 if Suppress = All_Checks then
1050 Svg : constant Suppress_Array := Scope_Suppress;
1052 Scope_Suppress := (others => True);
1053 Insert_Before_And_Analyze (N, M);
1054 Scope_Suppress := Svg;
1059 Svg : constant Boolean := Scope_Suppress (Suppress);
1061 Scope_Suppress (Suppress) := True;
1062 Insert_Before_And_Analyze (N, M);
1063 Scope_Suppress (Suppress) := Svg;
1066 end Insert_Before_And_Analyze;
1068 -----------------------------------
1069 -- Insert_List_After_And_Analyze --
1070 -----------------------------------
1072 procedure Insert_List_After_And_Analyze (N : Node_Id; L : List_Id) is
1073 After : constant Node_Id := Next (N);
1077 if Is_Non_Empty_List (L) then
1079 -- Capture the Node_Id of the first list node to be inserted.
1080 -- This will still be the first node after the insert operation,
1081 -- since Insert_List_After does not modify the Node_Id values.
1084 Insert_List_After (N, L);
1086 -- Now just analyze from the original first node until we get to
1087 -- the successor of the original insertion point (which may be
1088 -- Empty if the insertion point was at the end of the list). Note
1089 -- that this properly handles the case where any of the analyze
1090 -- calls result in the insertion of nodes after the analyzed
1091 -- node (possibly calling this routine recursively).
1093 while Node /= After loop
1095 Mark_Rewrite_Insertion (Node);
1099 end Insert_List_After_And_Analyze;
1101 -- Version with check(s) suppressed
1103 procedure Insert_List_After_And_Analyze
1104 (N : Node_Id; L : List_Id; Suppress : Check_Id)
1107 if Suppress = All_Checks then
1109 Svg : constant Suppress_Array := Scope_Suppress;
1111 Scope_Suppress := (others => True);
1112 Insert_List_After_And_Analyze (N, L);
1113 Scope_Suppress := Svg;
1118 Svg : constant Boolean := Scope_Suppress (Suppress);
1120 Scope_Suppress (Suppress) := True;
1121 Insert_List_After_And_Analyze (N, L);
1122 Scope_Suppress (Suppress) := Svg;
1125 end Insert_List_After_And_Analyze;
1127 ------------------------------------
1128 -- Insert_List_Before_And_Analyze --
1129 ------------------------------------
1131 procedure Insert_List_Before_And_Analyze (N : Node_Id; L : List_Id) is
1135 if Is_Non_Empty_List (L) then
1137 -- Capture the Node_Id of the first list node to be inserted.
1138 -- This will still be the first node after the insert operation,
1139 -- since Insert_List_After does not modify the Node_Id values.
1142 Insert_List_Before (N, L);
1144 -- The insertion does not change the Id's of any of the nodes in
1145 -- the list, and they are still linked, so we can simply loop from
1146 -- the original first node until we meet the node before which the
1147 -- insertion is occurring. Note that this properly handles the case
1148 -- where any of the analyzed nodes insert nodes after themselves,
1149 -- expecting them to get analyzed.
1151 while Node /= N loop
1153 Mark_Rewrite_Insertion (Node);
1157 end Insert_List_Before_And_Analyze;
1159 -- Version with check(s) suppressed
1161 procedure Insert_List_Before_And_Analyze
1162 (N : Node_Id; L : List_Id; Suppress : Check_Id)
1165 if Suppress = All_Checks then
1167 Svg : constant Suppress_Array := Scope_Suppress;
1169 Scope_Suppress := (others => True);
1170 Insert_List_Before_And_Analyze (N, L);
1171 Scope_Suppress := Svg;
1176 Svg : constant Boolean := Scope_Suppress (Suppress);
1178 Scope_Suppress (Suppress) := True;
1179 Insert_List_Before_And_Analyze (N, L);
1180 Scope_Suppress (Suppress) := Svg;
1183 end Insert_List_Before_And_Analyze;
1185 -------------------------
1186 -- Is_Check_Suppressed --
1187 -------------------------
1189 function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is
1191 Ptr : Suppress_Stack_Entry_Ptr;
1194 -- First search the local entity suppress stack, we search this from the
1195 -- top of the stack down, so that we get the innermost entry that
1196 -- applies to this case if there are nested entries.
1198 Ptr := Local_Suppress_Stack_Top;
1199 while Ptr /= null loop
1200 if (Ptr.Entity = Empty or else Ptr.Entity = E)
1201 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
1203 return Ptr.Suppress;
1209 -- Now search the global entity suppress table for a matching entry
1210 -- We also search this from the top down so that if there are multiple
1211 -- pragmas for the same entity, the last one applies (not clear what
1212 -- or whether the RM specifies this handling, but it seems reasonable).
1214 Ptr := Global_Suppress_Stack_Top;
1215 while Ptr /= null loop
1216 if (Ptr.Entity = Empty or else Ptr.Entity = E)
1217 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
1219 return Ptr.Suppress;
1225 -- If we did not find a matching entry, then use the normal scope
1226 -- suppress value after all (actually this will be the global setting
1227 -- since it clearly was not overridden at any point). For a predefined
1228 -- check, we test the specific flag. For a user defined check, we check
1229 -- the All_Checks flag.
1231 if C in Predefined_Check_Id then
1232 return Scope_Suppress (C);
1234 return Scope_Suppress (All_Checks);
1236 end Is_Check_Suppressed;
1244 Scope_Stack.Locked := True;
1245 Scope_Stack.Release;
1248 --------------------------------------
1249 -- Push_Global_Suppress_Stack_Entry --
1250 --------------------------------------
1252 procedure Push_Global_Suppress_Stack_Entry
1253 (Entity : Entity_Id;
1258 Global_Suppress_Stack_Top :=
1259 new Suppress_Stack_Entry'
1262 Suppress => Suppress,
1263 Prev => Global_Suppress_Stack_Top,
1264 Next => Suppress_Stack_Entries);
1265 Suppress_Stack_Entries := Global_Suppress_Stack_Top;
1268 end Push_Global_Suppress_Stack_Entry;
1270 -------------------------------------
1271 -- Push_Local_Suppress_Stack_Entry --
1272 -------------------------------------
1274 procedure Push_Local_Suppress_Stack_Entry
1275 (Entity : Entity_Id;
1280 Local_Suppress_Stack_Top :=
1281 new Suppress_Stack_Entry'
1284 Suppress => Suppress,
1285 Prev => Local_Suppress_Stack_Top,
1286 Next => Suppress_Stack_Entries);
1287 Suppress_Stack_Entries := Local_Suppress_Stack_Top;
1290 end Push_Local_Suppress_Stack_Entry;
1296 procedure Semantics (Comp_Unit : Node_Id) is
1298 -- The following locations save the corresponding global flags and
1299 -- variables so that they can be restored on completion. This is
1300 -- needed so that calls to Rtsfind start with the proper default
1301 -- values for these variables, and also that such calls do not
1302 -- disturb the settings for units being analyzed at a higher level.
1304 S_Current_Sem_Unit : constant Unit_Number_Type := Current_Sem_Unit;
1305 S_Full_Analysis : constant Boolean := Full_Analysis;
1306 S_GNAT_Mode : constant Boolean := GNAT_Mode;
1307 S_Global_Dis_Names : constant Boolean := Global_Discard_Names;
1308 S_In_Spec_Expr : constant Boolean := In_Spec_Expression;
1309 S_Inside_A_Generic : constant Boolean := Inside_A_Generic;
1310 S_New_Nodes_OK : constant Int := New_Nodes_OK;
1311 S_Outer_Gen_Scope : constant Entity_Id := Outer_Generic_Scope;
1313 Generic_Main : constant Boolean :=
1314 Nkind (Unit (Cunit (Main_Unit)))
1315 in N_Generic_Declaration;
1316 -- If the main unit is generic, every compiled unit, including its
1317 -- context, is compiled with expansion disabled.
1319 Save_Config_Switches : Config_Switches_Type;
1320 -- Variable used to save values of config switches while we analyze
1321 -- the new unit, to be restored on exit for proper recursive behavior.
1323 procedure Do_Analyze;
1324 -- Procedure to analyze the compilation unit. This is called more
1325 -- than once when the high level optimizer is activated.
1331 procedure Do_Analyze is
1334 Push_Scope (Standard_Standard);
1335 Scope_Suppress := Suppress_Options;
1337 (Scope_Stack.Last).Component_Alignment_Default := Calign_Default;
1339 (Scope_Stack.Last).Is_Active_Stack_Base := True;
1340 Outer_Generic_Scope := Empty;
1342 -- Now analyze the top level compilation unit node
1344 Analyze (Comp_Unit);
1346 -- Check for scope mismatch on exit from compilation
1348 pragma Assert (Current_Scope = Standard_Standard
1349 or else Comp_Unit = Cunit (Main_Unit));
1351 -- Then pop entry for Standard, and pop implicit types
1354 Restore_Scope_Stack;
1357 Already_Analyzed : constant Boolean := Analyzed (Comp_Unit);
1359 -- Start of processing for Semantics
1362 if Debug_Unit_Walk then
1363 if Already_Analyzed then
1364 Write_Str ("(done)");
1368 (Get_Cunit_Unit_Number (Comp_Unit),
1374 Compiler_State := Analyzing;
1375 Current_Sem_Unit := Get_Cunit_Unit_Number (Comp_Unit);
1377 -- Compile predefined units with GNAT_Mode set to True, to properly
1378 -- process the categorization stuff. However, do not set GNAT_Mode
1379 -- to True for the renamings units (Text_IO, IO_Exceptions, Direct_IO,
1380 -- Sequential_IO) as this would prevent pragma Extend_System from being
1381 -- taken into account, for example when Text_IO is renaming DEC.Text_IO.
1383 -- Cleaner might be to do the kludge at the point of excluding the
1384 -- pragma (do not exclude for renamings ???)
1386 if Is_Predefined_File_Name
1387 (Unit_File_Name (Current_Sem_Unit), Renamings_Included => False)
1392 if Generic_Main then
1393 Expander_Mode_Save_And_Set (False);
1395 Expander_Mode_Save_And_Set
1396 (Operating_Mode = Generate_Code or Debug_Flag_X);
1399 Full_Analysis := True;
1400 Inside_A_Generic := False;
1401 In_Spec_Expression := False;
1403 Set_Comes_From_Source_Default (False);
1404 Save_Opt_Config_Switches (Save_Config_Switches);
1405 Set_Opt_Config_Switches
1406 (Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit)),
1407 Current_Sem_Unit = Main_Unit);
1409 -- Only do analysis of unit that has not already been analyzed
1411 if not Analyzed (Comp_Unit) then
1412 Initialize_Version (Current_Sem_Unit);
1414 Expander_Mode_Save_And_Set (False);
1417 Reset_Analyzed_Flags (Comp_Unit);
1418 Expander_Mode_Restore;
1419 High_Level_Optimize (Comp_Unit);
1423 -- Do analysis, and then append the compilation unit onto the
1424 -- Comp_Unit_List, if appropriate. This is done after analysis, so
1425 -- if this unit depends on some others, they have already been
1426 -- appended. We ignore bodies, except for the main unit itself. We
1427 -- have also to guard against ill-formed subunits that have an
1428 -- improper context.
1432 if Ignore_Comp_Units then
1435 elsif Present (Comp_Unit)
1436 and then Nkind (Unit (Comp_Unit)) in N_Proper_Body
1437 and then not In_Extended_Main_Source_Unit (Comp_Unit)
1442 pragma Assert (not Ignore_Comp_Units);
1444 -- Initialize if first time
1446 if No (Comp_Unit_List) then
1447 Comp_Unit_List := New_Elmt_List;
1450 Append_Elmt (Comp_Unit, Comp_Unit_List);
1452 if Debug_Unit_Walk then
1453 Write_Str ("Appending ");
1455 (Get_Cunit_Unit_Number (Comp_Unit), Unit (Comp_Unit));
1458 -- Ignore all units after main unit
1460 if Comp_Unit = Cunit (Main_Unit) then
1461 Ignore_Comp_Units := True;
1466 -- Save indication of dynamic elaboration checks for ALI file
1468 Set_Dynamic_Elab (Current_Sem_Unit, Dynamic_Elaboration_Checks);
1470 -- Restore settings of saved switches to entry values
1472 Current_Sem_Unit := S_Current_Sem_Unit;
1473 Full_Analysis := S_Full_Analysis;
1474 Global_Discard_Names := S_Global_Dis_Names;
1475 GNAT_Mode := S_GNAT_Mode;
1476 In_Spec_Expression := S_In_Spec_Expr;
1477 Inside_A_Generic := S_Inside_A_Generic;
1478 New_Nodes_OK := S_New_Nodes_OK;
1479 Outer_Generic_Scope := S_Outer_Gen_Scope;
1481 Restore_Opt_Config_Switches (Save_Config_Switches);
1482 Expander_Mode_Restore;
1484 if Debug_Unit_Walk then
1487 if Already_Analyzed then
1488 Write_Str ("(done)");
1492 (Get_Cunit_Unit_Number (Comp_Unit),
1498 ------------------------
1499 -- Walk_Library_Items --
1500 ------------------------
1502 procedure Walk_Library_Items is
1503 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
1504 Seen : Unit_Number_Set := (others => False);
1506 procedure Do_Action (CU : Node_Id; Item : Node_Id);
1507 -- Calls Action, with some validity checks
1513 procedure Do_Action (CU : Node_Id; Item : Node_Id) is
1515 -- This calls Action at the end. All the preceding code is just
1516 -- assertions and debugging output.
1518 pragma Assert (No (CU) or else Nkind (CU) = N_Compilation_Unit);
1520 case Nkind (Item) is
1521 when N_Generic_Subprogram_Declaration |
1522 N_Generic_Package_Declaration |
1523 N_Package_Declaration |
1524 N_Subprogram_Declaration |
1525 N_Subprogram_Renaming_Declaration |
1526 N_Package_Renaming_Declaration |
1527 N_Generic_Function_Renaming_Declaration |
1528 N_Generic_Package_Renaming_Declaration |
1529 N_Generic_Procedure_Renaming_Declaration =>
1530 null; -- Specs are OK
1532 when N_Package_Body | N_Subprogram_Body =>
1533 -- A body must be the main unit
1535 pragma Assert (CU = Cunit (Main_Unit));
1538 -- All other cases cannot happen
1540 when N_Function_Instantiation |
1541 N_Procedure_Instantiation |
1542 N_Package_Instantiation =>
1543 pragma Assert (False, "instantiation");
1547 pragma Assert (False, "subunit");
1551 pragma Assert (False);
1555 if Present (CU) then
1556 pragma Assert (Item /= Stand.Standard_Package_Node);
1557 pragma Assert (Item = Unit (CU));
1560 Unit_Num : constant Unit_Number_Type :=
1561 Get_Cunit_Unit_Number (CU);
1563 if Debug_Unit_Walk then
1564 Write_Unit_Info (Unit_Num, Item);
1567 -- This assertion is commented out because it fails in some
1568 -- circumstances related to library-level generic
1569 -- instantiations. We need to investigate why.
1570 -- ???pragma Assert (not Seen (Unit_Num));
1572 Seen (Unit_Num) := True;
1578 pragma Assert (Item = Stand.Standard_Package_Node);
1580 if Debug_Unit_Walk then
1581 Write_Line ("Standard");
1588 -- Local Declarations
1590 Cur : Elmt_Id := First_Elmt (Comp_Unit_List);
1592 -- Start of processing for Walk_Library_Items
1595 if Debug_Unit_Walk then
1596 Write_Line ("Walk_Library_Items:");
1600 -- Do Standard first, then walk the Comp_Unit_List
1602 Do_Action (Empty, Standard_Package_Node);
1604 while Present (Cur) loop
1606 CU : constant Node_Id := Node (Cur);
1607 N : constant Node_Id := Unit (CU);
1610 pragma Assert (Nkind (CU) = N_Compilation_Unit);
1614 -- If it's a body, then ignore it, unless it's an instance (in
1615 -- which case we do the spec), or it's the main unit (in which
1616 -- case we do it). Note that it could be both, in which case we
1617 -- do the spec first.
1619 when N_Package_Body | N_Subprogram_Body =>
1621 Entity : Node_Id := N;
1624 if Nkind (N) = N_Subprogram_Body then
1625 Entity := Specification (Entity);
1628 Entity := Defining_Unit_Name (Entity);
1630 if Nkind (Entity) not in N_Entity then
1632 -- Must be N_Defining_Program_Unit_Name
1634 Entity := Defining_Identifier (Entity);
1637 if Is_Generic_Instance (Entity) then
1639 Spec_Unit : constant Node_Id := Library_Unit (CU);
1641 Do_Action (Spec_Unit, Unit (Spec_Unit));
1646 if CU = Cunit (Main_Unit) then
1650 pragma Assert (No (Next_Elmt (Cur)));
1655 -- It's a spec, so just do it
1665 if Debug_Unit_Walk then
1666 if Seen /= (Seen'Range => True) then
1668 Write_Line ("Ignored units:");
1672 for Unit_Num in Seen'Range loop
1673 if not Seen (Unit_Num) then
1674 Write_Unit_Info (Unit_Num, Unit (Cunit (Unit_Num)));
1682 if Debug_Unit_Walk then
1684 Write_Line ("end Walk_Library_Items.");
1686 end Walk_Library_Items;
1688 ---------------------
1689 -- Write_Unit_Info --
1690 ---------------------
1692 procedure Write_Unit_Info
1693 (Unit_Num : Unit_Number_Type;
1695 Prefix : String := "")
1699 Write_Unit_Name (Unit_Name (Unit_Num));
1700 Write_Str (", unit ");
1701 Write_Int (Int (Unit_Num));
1703 Write_Int (Int (Item));
1705 Write_Str (Node_Kind'Image (Nkind (Item)));
1707 if Item /= Original_Node (Item) then
1708 Write_Str (", orig = ");
1709 Write_Int (Int (Original_Node (Item)));
1711 Write_Str (Node_Kind'Image (Nkind (Original_Node (Item))));
1715 end Write_Unit_Info;