Merged with mainline at revision 128810.
[official-gcc.git] / gcc / ada / sem.adb
blob7dab13496c110c33fd1e4edc3329fb7cb6b08dac
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Fname; use Fname;
33 with HLO; use HLO;
34 with Lib; use Lib;
35 with Lib.Load; use Lib.Load;
36 with Nlists; use Nlists;
37 with Sem_Attr; use Sem_Attr;
38 with Sem_Ch2; use Sem_Ch2;
39 with Sem_Ch3; use Sem_Ch3;
40 with Sem_Ch4; use Sem_Ch4;
41 with Sem_Ch5; use Sem_Ch5;
42 with Sem_Ch6; use Sem_Ch6;
43 with Sem_Ch7; use Sem_Ch7;
44 with Sem_Ch8; use Sem_Ch8;
45 with Sem_Ch9; use Sem_Ch9;
46 with Sem_Ch10; use Sem_Ch10;
47 with Sem_Ch11; use Sem_Ch11;
48 with Sem_Ch12; use Sem_Ch12;
49 with Sem_Ch13; use Sem_Ch13;
50 with Sem_Prag; use Sem_Prag;
51 with Sem_Util; use Sem_Util;
52 with Sinfo; use Sinfo;
53 with Stand; use Stand;
54 with Uintp; use Uintp;
56 with Unchecked_Deallocation;
58 pragma Warnings (Off, Sem_Util);
59 -- Suppress warnings of unused with for Sem_Util (used only in asserts)
61 package body Sem is
63 Outer_Generic_Scope : Entity_Id := Empty;
64 -- Global reference to the outer scope that is generic. In a non
65 -- generic context, it is empty. At the moment, it is only used
66 -- for avoiding freezing of external references in generics.
68 -------------
69 -- Analyze --
70 -------------
72 procedure Analyze (N : Node_Id) is
73 begin
74 Debug_A_Entry ("analyzing ", N);
76 -- Immediate return if already analyzed
78 if Analyzed (N) then
79 Debug_A_Exit ("analyzing ", N, " (done, analyzed already)");
80 return;
81 end if;
83 -- Otherwise processing depends on the node kind
85 case Nkind (N) is
87 when N_Abort_Statement =>
88 Analyze_Abort_Statement (N);
90 when N_Abstract_Subprogram_Declaration =>
91 Analyze_Abstract_Subprogram_Declaration (N);
93 when N_Accept_Alternative =>
94 Analyze_Accept_Alternative (N);
96 when N_Accept_Statement =>
97 Analyze_Accept_Statement (N);
99 when N_Aggregate =>
100 Analyze_Aggregate (N);
102 when N_Allocator =>
103 Analyze_Allocator (N);
105 when N_And_Then =>
106 Analyze_Short_Circuit (N);
108 when N_Assignment_Statement =>
109 Analyze_Assignment (N);
111 when N_Asynchronous_Select =>
112 Analyze_Asynchronous_Select (N);
114 when N_At_Clause =>
115 Analyze_At_Clause (N);
117 when N_Attribute_Reference =>
118 Analyze_Attribute (N);
120 when N_Attribute_Definition_Clause =>
121 Analyze_Attribute_Definition_Clause (N);
123 when N_Block_Statement =>
124 Analyze_Block_Statement (N);
126 when N_Case_Statement =>
127 Analyze_Case_Statement (N);
129 when N_Character_Literal =>
130 Analyze_Character_Literal (N);
132 when N_Code_Statement =>
133 Analyze_Code_Statement (N);
135 when N_Compilation_Unit =>
136 Analyze_Compilation_Unit (N);
138 when N_Component_Declaration =>
139 Analyze_Component_Declaration (N);
141 when N_Conditional_Expression =>
142 Analyze_Conditional_Expression (N);
144 when N_Conditional_Entry_Call =>
145 Analyze_Conditional_Entry_Call (N);
147 when N_Delay_Alternative =>
148 Analyze_Delay_Alternative (N);
150 when N_Delay_Relative_Statement =>
151 Analyze_Delay_Relative (N);
153 when N_Delay_Until_Statement =>
154 Analyze_Delay_Until (N);
156 when N_Entry_Body =>
157 Analyze_Entry_Body (N);
159 when N_Entry_Body_Formal_Part =>
160 Analyze_Entry_Body_Formal_Part (N);
162 when N_Entry_Call_Alternative =>
163 Analyze_Entry_Call_Alternative (N);
165 when N_Entry_Declaration =>
166 Analyze_Entry_Declaration (N);
168 when N_Entry_Index_Specification =>
169 Analyze_Entry_Index_Specification (N);
171 when N_Enumeration_Representation_Clause =>
172 Analyze_Enumeration_Representation_Clause (N);
174 when N_Exception_Declaration =>
175 Analyze_Exception_Declaration (N);
177 when N_Exception_Renaming_Declaration =>
178 Analyze_Exception_Renaming (N);
180 when N_Exit_Statement =>
181 Analyze_Exit_Statement (N);
183 when N_Expanded_Name =>
184 Analyze_Expanded_Name (N);
186 when N_Explicit_Dereference =>
187 Analyze_Explicit_Dereference (N);
189 when N_Extended_Return_Statement =>
190 Analyze_Extended_Return_Statement (N);
192 when N_Extension_Aggregate =>
193 Analyze_Aggregate (N);
195 when N_Formal_Object_Declaration =>
196 Analyze_Formal_Object_Declaration (N);
198 when N_Formal_Package_Declaration =>
199 Analyze_Formal_Package (N);
201 when N_Formal_Subprogram_Declaration =>
202 Analyze_Formal_Subprogram (N);
204 when N_Formal_Type_Declaration =>
205 Analyze_Formal_Type_Declaration (N);
207 when N_Free_Statement =>
208 Analyze_Free_Statement (N);
210 when N_Freeze_Entity =>
211 null; -- no semantic processing required
213 when N_Full_Type_Declaration =>
214 Analyze_Type_Declaration (N);
216 when N_Function_Call =>
217 Analyze_Function_Call (N);
219 when N_Function_Instantiation =>
220 Analyze_Function_Instantiation (N);
222 when N_Generic_Function_Renaming_Declaration =>
223 Analyze_Generic_Function_Renaming (N);
225 when N_Generic_Package_Declaration =>
226 Analyze_Generic_Package_Declaration (N);
228 when N_Generic_Package_Renaming_Declaration =>
229 Analyze_Generic_Package_Renaming (N);
231 when N_Generic_Procedure_Renaming_Declaration =>
232 Analyze_Generic_Procedure_Renaming (N);
234 when N_Generic_Subprogram_Declaration =>
235 Analyze_Generic_Subprogram_Declaration (N);
237 when N_Goto_Statement =>
238 Analyze_Goto_Statement (N);
240 when N_Handled_Sequence_Of_Statements =>
241 Analyze_Handled_Statements (N);
243 when N_Identifier =>
244 Analyze_Identifier (N);
246 when N_If_Statement =>
247 Analyze_If_Statement (N);
249 when N_Implicit_Label_Declaration =>
250 Analyze_Implicit_Label_Declaration (N);
252 when N_In =>
253 Analyze_Membership_Op (N);
255 when N_Incomplete_Type_Declaration =>
256 Analyze_Incomplete_Type_Decl (N);
258 when N_Indexed_Component =>
259 Analyze_Indexed_Component_Form (N);
261 when N_Integer_Literal =>
262 Analyze_Integer_Literal (N);
264 when N_Itype_Reference =>
265 Analyze_Itype_Reference (N);
267 when N_Label =>
268 Analyze_Label (N);
270 when N_Loop_Statement =>
271 Analyze_Loop_Statement (N);
273 when N_Not_In =>
274 Analyze_Membership_Op (N);
276 when N_Null =>
277 Analyze_Null (N);
279 when N_Null_Statement =>
280 Analyze_Null_Statement (N);
282 when N_Number_Declaration =>
283 Analyze_Number_Declaration (N);
285 when N_Object_Declaration =>
286 Analyze_Object_Declaration (N);
288 when N_Object_Renaming_Declaration =>
289 Analyze_Object_Renaming (N);
291 when N_Operator_Symbol =>
292 Analyze_Operator_Symbol (N);
294 when N_Op_Abs =>
295 Analyze_Unary_Op (N);
297 when N_Op_Add =>
298 Analyze_Arithmetic_Op (N);
300 when N_Op_And =>
301 Analyze_Logical_Op (N);
303 when N_Op_Concat =>
304 Analyze_Concatenation (N);
306 when N_Op_Divide =>
307 Analyze_Arithmetic_Op (N);
309 when N_Op_Eq =>
310 Analyze_Equality_Op (N);
312 when N_Op_Expon =>
313 Analyze_Arithmetic_Op (N);
315 when N_Op_Ge =>
316 Analyze_Comparison_Op (N);
318 when N_Op_Gt =>
319 Analyze_Comparison_Op (N);
321 when N_Op_Le =>
322 Analyze_Comparison_Op (N);
324 when N_Op_Lt =>
325 Analyze_Comparison_Op (N);
327 when N_Op_Minus =>
328 Analyze_Unary_Op (N);
330 when N_Op_Mod =>
331 Analyze_Arithmetic_Op (N);
333 when N_Op_Multiply =>
334 Analyze_Arithmetic_Op (N);
336 when N_Op_Ne =>
337 Analyze_Equality_Op (N);
339 when N_Op_Not =>
340 Analyze_Negation (N);
342 when N_Op_Or =>
343 Analyze_Logical_Op (N);
345 when N_Op_Plus =>
346 Analyze_Unary_Op (N);
348 when N_Op_Rem =>
349 Analyze_Arithmetic_Op (N);
351 when N_Op_Rotate_Left =>
352 Analyze_Arithmetic_Op (N);
354 when N_Op_Rotate_Right =>
355 Analyze_Arithmetic_Op (N);
357 when N_Op_Shift_Left =>
358 Analyze_Arithmetic_Op (N);
360 when N_Op_Shift_Right =>
361 Analyze_Arithmetic_Op (N);
363 when N_Op_Shift_Right_Arithmetic =>
364 Analyze_Arithmetic_Op (N);
366 when N_Op_Subtract =>
367 Analyze_Arithmetic_Op (N);
369 when N_Op_Xor =>
370 Analyze_Logical_Op (N);
372 when N_Or_Else =>
373 Analyze_Short_Circuit (N);
375 when N_Others_Choice =>
376 Analyze_Others_Choice (N);
378 when N_Package_Body =>
379 Analyze_Package_Body (N);
381 when N_Package_Body_Stub =>
382 Analyze_Package_Body_Stub (N);
384 when N_Package_Declaration =>
385 Analyze_Package_Declaration (N);
387 when N_Package_Instantiation =>
388 Analyze_Package_Instantiation (N);
390 when N_Package_Renaming_Declaration =>
391 Analyze_Package_Renaming (N);
393 when N_Package_Specification =>
394 Analyze_Package_Specification (N);
396 when N_Parameter_Association =>
397 Analyze_Parameter_Association (N);
399 when N_Pragma =>
400 Analyze_Pragma (N);
402 when N_Private_Extension_Declaration =>
403 Analyze_Private_Extension_Declaration (N);
405 when N_Private_Type_Declaration =>
406 Analyze_Private_Type_Declaration (N);
408 when N_Procedure_Call_Statement =>
409 Analyze_Procedure_Call (N);
411 when N_Procedure_Instantiation =>
412 Analyze_Procedure_Instantiation (N);
414 when N_Protected_Body =>
415 Analyze_Protected_Body (N);
417 when N_Protected_Body_Stub =>
418 Analyze_Protected_Body_Stub (N);
420 when N_Protected_Definition =>
421 Analyze_Protected_Definition (N);
423 when N_Protected_Type_Declaration =>
424 Analyze_Protected_Type (N);
426 when N_Qualified_Expression =>
427 Analyze_Qualified_Expression (N);
429 when N_Raise_Statement =>
430 Analyze_Raise_Statement (N);
432 when N_Raise_xxx_Error =>
433 Analyze_Raise_xxx_Error (N);
435 when N_Range =>
436 Analyze_Range (N);
438 when N_Range_Constraint =>
439 Analyze_Range (Range_Expression (N));
441 when N_Real_Literal =>
442 Analyze_Real_Literal (N);
444 when N_Record_Representation_Clause =>
445 Analyze_Record_Representation_Clause (N);
447 when N_Reference =>
448 Analyze_Reference (N);
450 when N_Requeue_Statement =>
451 Analyze_Requeue (N);
453 when N_Simple_Return_Statement =>
454 Analyze_Simple_Return_Statement (N);
456 when N_Selected_Component =>
457 Find_Selected_Component (N);
458 -- ??? why not Analyze_Selected_Component, needs comments
460 when N_Selective_Accept =>
461 Analyze_Selective_Accept (N);
463 when N_Single_Protected_Declaration =>
464 Analyze_Single_Protected (N);
466 when N_Single_Task_Declaration =>
467 Analyze_Single_Task (N);
469 when N_Slice =>
470 Analyze_Slice (N);
472 when N_String_Literal =>
473 Analyze_String_Literal (N);
475 when N_Subprogram_Body =>
476 Analyze_Subprogram_Body (N);
478 when N_Subprogram_Body_Stub =>
479 Analyze_Subprogram_Body_Stub (N);
481 when N_Subprogram_Declaration =>
482 Analyze_Subprogram_Declaration (N);
484 when N_Subprogram_Info =>
485 Analyze_Subprogram_Info (N);
487 when N_Subprogram_Renaming_Declaration =>
488 Analyze_Subprogram_Renaming (N);
490 when N_Subtype_Declaration =>
491 Analyze_Subtype_Declaration (N);
493 when N_Subtype_Indication =>
494 Analyze_Subtype_Indication (N);
496 when N_Subunit =>
497 Analyze_Subunit (N);
499 when N_Task_Body =>
500 Analyze_Task_Body (N);
502 when N_Task_Body_Stub =>
503 Analyze_Task_Body_Stub (N);
505 when N_Task_Definition =>
506 Analyze_Task_Definition (N);
508 when N_Task_Type_Declaration =>
509 Analyze_Task_Type (N);
511 when N_Terminate_Alternative =>
512 Analyze_Terminate_Alternative (N);
514 when N_Timed_Entry_Call =>
515 Analyze_Timed_Entry_Call (N);
517 when N_Triggering_Alternative =>
518 Analyze_Triggering_Alternative (N);
520 when N_Type_Conversion =>
521 Analyze_Type_Conversion (N);
523 when N_Unchecked_Expression =>
524 Analyze_Unchecked_Expression (N);
526 when N_Unchecked_Type_Conversion =>
527 Analyze_Unchecked_Type_Conversion (N);
529 when N_Use_Package_Clause =>
530 Analyze_Use_Package (N);
532 when N_Use_Type_Clause =>
533 Analyze_Use_Type (N);
535 when N_Validate_Unchecked_Conversion =>
536 null;
538 when N_Variant_Part =>
539 Analyze_Variant_Part (N);
541 when N_With_Clause =>
542 Analyze_With_Clause (N);
544 -- A call to analyze the Empty node is an error, but most likely
545 -- it is an error caused by an attempt to analyze a malformed
546 -- piece of tree caused by some other error, so if there have
547 -- been any other errors, we just ignore it, otherwise it is
548 -- a real internal error which we complain about.
550 when N_Empty =>
551 pragma Assert (Serious_Errors_Detected /= 0);
552 null;
554 -- A call to analyze the error node is simply ignored, to avoid
555 -- causing cascaded errors (happens of course only in error cases)
557 when N_Error =>
558 null;
560 -- Push/Pop nodes normally don't come through an analyze call. An
561 -- exception is the dummy ones bracketing a subprogram body. In any
562 -- case there is nothing to be done to analyze such nodes.
564 when N_Push_Pop_xxx_Label =>
565 null;
567 -- For the remaining node types, we generate compiler abort, because
568 -- these nodes are always analyzed within the Sem_Chn routines and
569 -- there should never be a case of making a call to the main Analyze
570 -- routine for these node kinds. For example, an N_Access_Definition
571 -- node appears only in the context of a type declaration, and is
572 -- processed by the analyze routine for type declarations.
574 when
575 N_Abortable_Part |
576 N_Access_Definition |
577 N_Access_Function_Definition |
578 N_Access_Procedure_Definition |
579 N_Access_To_Object_Definition |
580 N_Case_Statement_Alternative |
581 N_Compilation_Unit_Aux |
582 N_Component_Association |
583 N_Component_Clause |
584 N_Component_Definition |
585 N_Component_List |
586 N_Constrained_Array_Definition |
587 N_Decimal_Fixed_Point_Definition |
588 N_Defining_Character_Literal |
589 N_Defining_Identifier |
590 N_Defining_Operator_Symbol |
591 N_Defining_Program_Unit_Name |
592 N_Delta_Constraint |
593 N_Derived_Type_Definition |
594 N_Designator |
595 N_Digits_Constraint |
596 N_Discriminant_Association |
597 N_Discriminant_Specification |
598 N_Elsif_Part |
599 N_Entry_Call_Statement |
600 N_Enumeration_Type_Definition |
601 N_Exception_Handler |
602 N_Floating_Point_Definition |
603 N_Formal_Decimal_Fixed_Point_Definition |
604 N_Formal_Derived_Type_Definition |
605 N_Formal_Discrete_Type_Definition |
606 N_Formal_Floating_Point_Definition |
607 N_Formal_Modular_Type_Definition |
608 N_Formal_Ordinary_Fixed_Point_Definition |
609 N_Formal_Private_Type_Definition |
610 N_Formal_Signed_Integer_Type_Definition |
611 N_Function_Specification |
612 N_Generic_Association |
613 N_Index_Or_Discriminant_Constraint |
614 N_Iteration_Scheme |
615 N_Loop_Parameter_Specification |
616 N_Mod_Clause |
617 N_Modular_Type_Definition |
618 N_Ordinary_Fixed_Point_Definition |
619 N_Parameter_Specification |
620 N_Pragma_Argument_Association |
621 N_Procedure_Specification |
622 N_Real_Range_Specification |
623 N_Record_Definition |
624 N_Signed_Integer_Type_Definition |
625 N_Unconstrained_Array_Definition |
626 N_Unused_At_Start |
627 N_Unused_At_End |
628 N_Variant =>
630 raise Program_Error;
631 end case;
633 Debug_A_Exit ("analyzing ", N, " (done)");
635 -- Now that we have analyzed the node, we call the expander to perform
636 -- possible expansion. We skip this for subexpressions, because we don't
637 -- have the type yet, and the expander will need to know the type before
638 -- it can do its job. For subexpression nodes, the call to the expander
639 -- happens in Sem_Res.Resolve. A special exception is Raise_xxx_Error,
640 -- which can appear in a statement context, and needs expanding now in
641 -- the case (distinguished by Etype, as documented in Sinfo).
643 -- The Analyzed flag is also set at this point for non-subexpression
644 -- nodes (in the case of subexpression nodes, we can't set the flag yet,
645 -- since resolution and expansion have not yet been completed). Note
646 -- that for N_Raise_xxx_Error we have to distinguish the expression
647 -- case from the statement case.
649 if Nkind (N) not in N_Subexpr
650 or else (Nkind (N) in N_Raise_xxx_Error
651 and then Etype (N) = Standard_Void_Type)
652 then
653 Expand (N);
654 end if;
655 end Analyze;
657 -- Version with check(s) suppressed
659 procedure Analyze (N : Node_Id; Suppress : Check_Id) is
660 begin
661 if Suppress = All_Checks then
662 declare
663 Svg : constant Suppress_Array := Scope_Suppress;
664 begin
665 Scope_Suppress := (others => True);
666 Analyze (N);
667 Scope_Suppress := Svg;
668 end;
670 else
671 declare
672 Svg : constant Boolean := Scope_Suppress (Suppress);
673 begin
674 Scope_Suppress (Suppress) := True;
675 Analyze (N);
676 Scope_Suppress (Suppress) := Svg;
677 end;
678 end if;
679 end Analyze;
681 ------------------
682 -- Analyze_List --
683 ------------------
685 procedure Analyze_List (L : List_Id) is
686 Node : Node_Id;
688 begin
689 Node := First (L);
690 while Present (Node) loop
691 Analyze (Node);
692 Next (Node);
693 end loop;
694 end Analyze_List;
696 -- Version with check(s) suppressed
698 procedure Analyze_List (L : List_Id; Suppress : Check_Id) is
699 begin
700 if Suppress = All_Checks then
701 declare
702 Svg : constant Suppress_Array := Scope_Suppress;
703 begin
704 Scope_Suppress := (others => True);
705 Analyze_List (L);
706 Scope_Suppress := Svg;
707 end;
709 else
710 declare
711 Svg : constant Boolean := Scope_Suppress (Suppress);
712 begin
713 Scope_Suppress (Suppress) := True;
714 Analyze_List (L);
715 Scope_Suppress (Suppress) := Svg;
716 end;
717 end if;
718 end Analyze_List;
720 --------------------------
721 -- Copy_Suppress_Status --
722 --------------------------
724 procedure Copy_Suppress_Status
725 (C : Check_Id;
726 From : Entity_Id;
727 To : Entity_Id)
729 Found : Boolean;
731 procedure Search_Stack
732 (Top : Suppress_Stack_Entry_Ptr;
733 Found : out Boolean);
734 -- Search given suppress stack for matching entry for entity. If found
735 -- then set Checks_May_Be_Suppressed on To, and push an appropriate
736 -- entry for To onto the local suppress stack.
738 ------------------
739 -- Search_Stack --
740 ------------------
742 procedure Search_Stack
743 (Top : Suppress_Stack_Entry_Ptr;
744 Found : out Boolean)
746 Ptr : Suppress_Stack_Entry_Ptr;
748 begin
749 Ptr := Top;
750 while Ptr /= null loop
751 if Ptr.Entity = From
752 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
753 then
754 if Ptr.Suppress then
755 Set_Checks_May_Be_Suppressed (To, True);
756 Push_Local_Suppress_Stack_Entry
757 (Entity => To,
758 Check => C,
759 Suppress => True);
760 Found := True;
761 return;
762 end if;
763 end if;
765 Ptr := Ptr.Prev;
766 end loop;
768 Found := False;
769 return;
770 end Search_Stack;
772 -- Start of processing for Copy_Suppress_Status
774 begin
775 if not Checks_May_Be_Suppressed (From) then
776 return;
777 end if;
779 -- First search the local entity suppress stack, we search this in
780 -- reverse order so that we get the innermost entry that applies to
781 -- this case if there are nested entries. Note that for the purpose
782 -- of this procedure we are ONLY looking for entries corresponding
783 -- to a two-argument Suppress, where the second argument matches From.
785 Search_Stack (Global_Suppress_Stack_Top, Found);
787 if Found then
788 return;
789 end if;
791 -- Now search the global entity suppress table for a matching entry
792 -- We also search this in reverse order so that if there are multiple
793 -- pragmas for the same entity, the last one applies.
795 Search_Stack (Local_Suppress_Stack_Top, Found);
796 end Copy_Suppress_Status;
798 -------------------------
799 -- Enter_Generic_Scope --
800 -------------------------
802 procedure Enter_Generic_Scope (S : Entity_Id) is
803 begin
804 if No (Outer_Generic_Scope) then
805 Outer_Generic_Scope := S;
806 end if;
807 end Enter_Generic_Scope;
809 ------------------------
810 -- Exit_Generic_Scope --
811 ------------------------
813 procedure Exit_Generic_Scope (S : Entity_Id) is
814 begin
815 if S = Outer_Generic_Scope then
816 Outer_Generic_Scope := Empty;
817 end if;
818 end Exit_Generic_Scope;
820 -----------------------
821 -- Explicit_Suppress --
822 -----------------------
824 function Explicit_Suppress (E : Entity_Id; C : Check_Id) return Boolean is
825 Ptr : Suppress_Stack_Entry_Ptr;
827 begin
828 if not Checks_May_Be_Suppressed (E) then
829 return False;
831 else
832 Ptr := Global_Suppress_Stack_Top;
833 while Ptr /= null loop
834 if Ptr.Entity = E
835 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
836 then
837 return Ptr.Suppress;
838 end if;
840 Ptr := Ptr.Prev;
841 end loop;
842 end if;
844 return False;
845 end Explicit_Suppress;
847 -----------------------------
848 -- External_Ref_In_Generic --
849 -----------------------------
851 function External_Ref_In_Generic (E : Entity_Id) return Boolean is
852 Scop : Entity_Id;
854 begin
855 -- Entity is global if defined outside of current outer_generic_scope:
856 -- Either the entity has a smaller depth that the outer generic, or it
857 -- is in a different compilation unit, or it is defined within a unit
858 -- in the same compilation, that is not within the outer_generic.
860 if No (Outer_Generic_Scope) then
861 return False;
863 elsif Scope_Depth (Scope (E)) < Scope_Depth (Outer_Generic_Scope)
864 or else not In_Same_Source_Unit (E, Outer_Generic_Scope)
865 then
866 return True;
868 else
869 Scop := Scope (E);
871 while Present (Scop) loop
872 if Scop = Outer_Generic_Scope then
873 return False;
874 elsif Scope_Depth (Scop) < Scope_Depth (Outer_Generic_Scope) then
875 return True;
876 else
877 Scop := Scope (Scop);
878 end if;
879 end loop;
881 return True;
882 end if;
883 end External_Ref_In_Generic;
885 ----------------
886 -- Initialize --
887 ----------------
889 procedure Initialize is
890 Next : Suppress_Stack_Entry_Ptr;
892 procedure Free is new Unchecked_Deallocation
893 (Suppress_Stack_Entry, Suppress_Stack_Entry_Ptr);
895 begin
896 -- Free any global suppress stack entries from a previous invocation
897 -- of the compiler (in the normal case this loop does nothing).
899 while Suppress_Stack_Entries /= null loop
900 Next := Global_Suppress_Stack_Top.Next;
901 Free (Suppress_Stack_Entries);
902 Suppress_Stack_Entries := Next;
903 end loop;
905 Local_Suppress_Stack_Top := null;
906 Global_Suppress_Stack_Top := null;
908 -- Clear scope stack, and reset global variables
910 Scope_Stack.Init;
911 Unloaded_Subunits := False;
912 end Initialize;
914 ------------------------------
915 -- Insert_After_And_Analyze --
916 ------------------------------
918 procedure Insert_After_And_Analyze (N : Node_Id; M : Node_Id) is
919 Node : Node_Id;
921 begin
922 if Present (M) then
924 -- If we are not at the end of the list, then the easiest
925 -- coding is simply to insert before our successor
927 if Present (Next (N)) then
928 Insert_Before_And_Analyze (Next (N), M);
930 -- Case of inserting at the end of the list
932 else
933 -- Capture the Node_Id of the node to be inserted. This Node_Id
934 -- will still be the same after the insert operation.
936 Node := M;
937 Insert_After (N, M);
939 -- Now just analyze from the inserted node to the end of
940 -- the new list (note that this properly handles the case
941 -- where any of the analyze calls result in the insertion of
942 -- nodes after the analyzed node, expecting analysis).
944 while Present (Node) loop
945 Analyze (Node);
946 Mark_Rewrite_Insertion (Node);
947 Next (Node);
948 end loop;
949 end if;
950 end if;
951 end Insert_After_And_Analyze;
953 -- Version with check(s) suppressed
955 procedure Insert_After_And_Analyze
956 (N : Node_Id;
957 M : Node_Id;
958 Suppress : Check_Id)
960 begin
961 if Suppress = All_Checks then
962 declare
963 Svg : constant Suppress_Array := Scope_Suppress;
964 begin
965 Scope_Suppress := (others => True);
966 Insert_After_And_Analyze (N, M);
967 Scope_Suppress := Svg;
968 end;
970 else
971 declare
972 Svg : constant Boolean := Scope_Suppress (Suppress);
973 begin
974 Scope_Suppress (Suppress) := True;
975 Insert_After_And_Analyze (N, M);
976 Scope_Suppress (Suppress) := Svg;
977 end;
978 end if;
979 end Insert_After_And_Analyze;
981 -------------------------------
982 -- Insert_Before_And_Analyze --
983 -------------------------------
985 procedure Insert_Before_And_Analyze (N : Node_Id; M : Node_Id) is
986 Node : Node_Id;
988 begin
989 if Present (M) then
991 -- Capture the Node_Id of the first list node to be inserted.
992 -- This will still be the first node after the insert operation,
993 -- since Insert_List_After does not modify the Node_Id values.
995 Node := M;
996 Insert_Before (N, M);
998 -- The insertion does not change the Id's of any of the nodes in
999 -- the list, and they are still linked, so we can simply loop from
1000 -- the original first node until we meet the node before which the
1001 -- insertion is occurring. Note that this properly handles the case
1002 -- where any of the analyzed nodes insert nodes after themselves,
1003 -- expecting them to get analyzed.
1005 while Node /= N loop
1006 Analyze (Node);
1007 Mark_Rewrite_Insertion (Node);
1008 Next (Node);
1009 end loop;
1010 end if;
1011 end Insert_Before_And_Analyze;
1013 -- Version with check(s) suppressed
1015 procedure Insert_Before_And_Analyze
1016 (N : Node_Id;
1017 M : Node_Id;
1018 Suppress : Check_Id)
1020 begin
1021 if Suppress = All_Checks then
1022 declare
1023 Svg : constant Suppress_Array := Scope_Suppress;
1024 begin
1025 Scope_Suppress := (others => True);
1026 Insert_Before_And_Analyze (N, M);
1027 Scope_Suppress := Svg;
1028 end;
1030 else
1031 declare
1032 Svg : constant Boolean := Scope_Suppress (Suppress);
1033 begin
1034 Scope_Suppress (Suppress) := True;
1035 Insert_Before_And_Analyze (N, M);
1036 Scope_Suppress (Suppress) := Svg;
1037 end;
1038 end if;
1039 end Insert_Before_And_Analyze;
1041 -----------------------------------
1042 -- Insert_List_After_And_Analyze --
1043 -----------------------------------
1045 procedure Insert_List_After_And_Analyze (N : Node_Id; L : List_Id) is
1046 After : constant Node_Id := Next (N);
1047 Node : Node_Id;
1049 begin
1050 if Is_Non_Empty_List (L) then
1052 -- Capture the Node_Id of the first list node to be inserted.
1053 -- This will still be the first node after the insert operation,
1054 -- since Insert_List_After does not modify the Node_Id values.
1056 Node := First (L);
1057 Insert_List_After (N, L);
1059 -- Now just analyze from the original first node until we get to
1060 -- the successor of the original insertion point (which may be
1061 -- Empty if the insertion point was at the end of the list). Note
1062 -- that this properly handles the case where any of the analyze
1063 -- calls result in the insertion of nodes after the analyzed
1064 -- node (possibly calling this routine recursively).
1066 while Node /= After loop
1067 Analyze (Node);
1068 Mark_Rewrite_Insertion (Node);
1069 Next (Node);
1070 end loop;
1071 end if;
1072 end Insert_List_After_And_Analyze;
1074 -- Version with check(s) suppressed
1076 procedure Insert_List_After_And_Analyze
1077 (N : Node_Id; L : List_Id; Suppress : Check_Id)
1079 begin
1080 if Suppress = All_Checks then
1081 declare
1082 Svg : constant Suppress_Array := Scope_Suppress;
1083 begin
1084 Scope_Suppress := (others => True);
1085 Insert_List_After_And_Analyze (N, L);
1086 Scope_Suppress := Svg;
1087 end;
1089 else
1090 declare
1091 Svg : constant Boolean := Scope_Suppress (Suppress);
1092 begin
1093 Scope_Suppress (Suppress) := True;
1094 Insert_List_After_And_Analyze (N, L);
1095 Scope_Suppress (Suppress) := Svg;
1096 end;
1097 end if;
1098 end Insert_List_After_And_Analyze;
1100 ------------------------------------
1101 -- Insert_List_Before_And_Analyze --
1102 ------------------------------------
1104 procedure Insert_List_Before_And_Analyze (N : Node_Id; L : List_Id) is
1105 Node : Node_Id;
1107 begin
1108 if Is_Non_Empty_List (L) then
1110 -- Capture the Node_Id of the first list node to be inserted.
1111 -- This will still be the first node after the insert operation,
1112 -- since Insert_List_After does not modify the Node_Id values.
1114 Node := First (L);
1115 Insert_List_Before (N, L);
1117 -- The insertion does not change the Id's of any of the nodes in
1118 -- the list, and they are still linked, so we can simply loop from
1119 -- the original first node until we meet the node before which the
1120 -- insertion is occurring. Note that this properly handles the case
1121 -- where any of the analyzed nodes insert nodes after themselves,
1122 -- expecting them to get analyzed.
1124 while Node /= N loop
1125 Analyze (Node);
1126 Mark_Rewrite_Insertion (Node);
1127 Next (Node);
1128 end loop;
1129 end if;
1130 end Insert_List_Before_And_Analyze;
1132 -- Version with check(s) suppressed
1134 procedure Insert_List_Before_And_Analyze
1135 (N : Node_Id; L : List_Id; Suppress : Check_Id)
1137 begin
1138 if Suppress = All_Checks then
1139 declare
1140 Svg : constant Suppress_Array := Scope_Suppress;
1141 begin
1142 Scope_Suppress := (others => True);
1143 Insert_List_Before_And_Analyze (N, L);
1144 Scope_Suppress := Svg;
1145 end;
1147 else
1148 declare
1149 Svg : constant Boolean := Scope_Suppress (Suppress);
1150 begin
1151 Scope_Suppress (Suppress) := True;
1152 Insert_List_Before_And_Analyze (N, L);
1153 Scope_Suppress (Suppress) := Svg;
1154 end;
1155 end if;
1156 end Insert_List_Before_And_Analyze;
1158 -------------------------
1159 -- Is_Check_Suppressed --
1160 -------------------------
1162 function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is
1164 Ptr : Suppress_Stack_Entry_Ptr;
1166 begin
1167 -- First search the local entity suppress stack, we search this from the
1168 -- top of the stack down, so that we get the innermost entry that
1169 -- applies to this case if there are nested entries.
1171 Ptr := Local_Suppress_Stack_Top;
1172 while Ptr /= null loop
1173 if (Ptr.Entity = Empty or else Ptr.Entity = E)
1174 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
1175 then
1176 return Ptr.Suppress;
1177 end if;
1179 Ptr := Ptr.Prev;
1180 end loop;
1182 -- Now search the global entity suppress table for a matching entry
1183 -- We also search this from the top down so that if there are multiple
1184 -- pragmas for the same entity, the last one applies (not clear what
1185 -- or whether the RM specifies this handling, but it seems reasonable).
1187 Ptr := Global_Suppress_Stack_Top;
1188 while Ptr /= null loop
1189 if (Ptr.Entity = Empty or else Ptr.Entity = E)
1190 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
1191 then
1192 return Ptr.Suppress;
1193 end if;
1195 Ptr := Ptr.Prev;
1196 end loop;
1198 -- If we did not find a matching entry, then use the normal scope
1199 -- suppress value after all (actually this will be the global setting
1200 -- since it clearly was not overridden at any point). For a predefined
1201 -- check, we test the specific flag. For a user defined check, we check
1202 -- the All_Checks flag.
1204 if C in Predefined_Check_Id then
1205 return Scope_Suppress (C);
1206 else
1207 return Scope_Suppress (All_Checks);
1208 end if;
1209 end Is_Check_Suppressed;
1211 ----------
1212 -- Lock --
1213 ----------
1215 procedure Lock is
1216 begin
1217 Scope_Stack.Locked := True;
1218 Scope_Stack.Release;
1219 end Lock;
1221 --------------------------------------
1222 -- Push_Global_Suppress_Stack_Entry --
1223 --------------------------------------
1225 procedure Push_Global_Suppress_Stack_Entry
1226 (Entity : Entity_Id;
1227 Check : Check_Id;
1228 Suppress : Boolean)
1230 begin
1231 Global_Suppress_Stack_Top :=
1232 new Suppress_Stack_Entry'
1233 (Entity => Entity,
1234 Check => Check,
1235 Suppress => Suppress,
1236 Prev => Global_Suppress_Stack_Top,
1237 Next => Suppress_Stack_Entries);
1238 Suppress_Stack_Entries := Global_Suppress_Stack_Top;
1239 return;
1241 end Push_Global_Suppress_Stack_Entry;
1243 -------------------------------------
1244 -- Push_Local_Suppress_Stack_Entry --
1245 -------------------------------------
1247 procedure Push_Local_Suppress_Stack_Entry
1248 (Entity : Entity_Id;
1249 Check : Check_Id;
1250 Suppress : Boolean)
1252 begin
1253 Local_Suppress_Stack_Top :=
1254 new Suppress_Stack_Entry'
1255 (Entity => Entity,
1256 Check => Check,
1257 Suppress => Suppress,
1258 Prev => Local_Suppress_Stack_Top,
1259 Next => Suppress_Stack_Entries);
1260 Suppress_Stack_Entries := Local_Suppress_Stack_Top;
1262 return;
1263 end Push_Local_Suppress_Stack_Entry;
1265 ---------------
1266 -- Semantics --
1267 ---------------
1269 procedure Semantics (Comp_Unit : Node_Id) is
1271 -- The following locations save the corresponding global flags and
1272 -- variables so that they can be restored on completion. This is
1273 -- needed so that calls to Rtsfind start with the proper default
1274 -- values for these variables, and also that such calls do not
1275 -- disturb the settings for units being analyzed at a higher level.
1277 S_Full_Analysis : constant Boolean := Full_Analysis;
1278 S_In_Default_Expr : constant Boolean := In_Default_Expression;
1279 S_Inside_A_Generic : constant Boolean := Inside_A_Generic;
1280 S_New_Nodes_OK : constant Int := New_Nodes_OK;
1281 S_Outer_Gen_Scope : constant Entity_Id := Outer_Generic_Scope;
1282 S_Sem_Unit : constant Unit_Number_Type := Current_Sem_Unit;
1283 S_GNAT_Mode : constant Boolean := GNAT_Mode;
1284 S_Discard_Names : constant Boolean := Global_Discard_Names;
1285 Generic_Main : constant Boolean :=
1286 Nkind (Unit (Cunit (Main_Unit)))
1287 in N_Generic_Declaration;
1289 -- If the main unit is generic, every compiled unit, including its
1290 -- context, is compiled with expansion disabled.
1292 Save_Config_Switches : Config_Switches_Type;
1293 -- Variable used to save values of config switches while we analyze
1294 -- the new unit, to be restored on exit for proper recursive behavior.
1296 procedure Do_Analyze;
1297 -- Procedure to analyze the compilation unit. This is called more
1298 -- than once when the high level optimizer is activated.
1300 ----------------
1301 -- Do_Analyze --
1302 ----------------
1304 procedure Do_Analyze is
1305 begin
1306 Save_Scope_Stack;
1307 Push_Scope (Standard_Standard);
1308 Scope_Suppress := Suppress_Options;
1309 Scope_Stack.Table
1310 (Scope_Stack.Last).Component_Alignment_Default := Calign_Default;
1311 Scope_Stack.Table
1312 (Scope_Stack.Last).Is_Active_Stack_Base := True;
1313 Outer_Generic_Scope := Empty;
1315 -- Now analyze the top level compilation unit node
1317 Analyze (Comp_Unit);
1319 -- Check for scope mismatch on exit from compilation
1321 pragma Assert (Current_Scope = Standard_Standard
1322 or else Comp_Unit = Cunit (Main_Unit));
1324 -- Then pop entry for Standard, and pop implicit types
1326 Pop_Scope;
1327 Restore_Scope_Stack;
1328 end Do_Analyze;
1330 -- Start of processing for Semantics
1332 begin
1333 Compiler_State := Analyzing;
1334 Current_Sem_Unit := Get_Cunit_Unit_Number (Comp_Unit);
1336 -- Compile predefined units with GNAT_Mode set to True, to properly
1337 -- process the categorization stuff. However, do not set set GNAT_Mode
1338 -- to True for the renamings units (Text_IO, IO_Exceptions, Direct_IO,
1339 -- Sequential_IO) as this would prevent pragma System_Extend to be
1340 -- taken into account, for example when Text_IO is renaming DEC.Text_IO.
1342 -- Cleaner might be to do the kludge at the point of excluding the
1343 -- pragma (do not exclude for renamings ???)
1345 GNAT_Mode :=
1346 GNAT_Mode
1347 or else Is_Predefined_File_Name
1348 (Unit_File_Name (Current_Sem_Unit),
1349 Renamings_Included => False);
1351 if Generic_Main then
1352 Expander_Mode_Save_And_Set (False);
1353 else
1354 Expander_Mode_Save_And_Set
1355 (Operating_Mode = Generate_Code or Debug_Flag_X);
1356 end if;
1358 Full_Analysis := True;
1359 Inside_A_Generic := False;
1360 In_Default_Expression := False;
1362 Set_Comes_From_Source_Default (False);
1363 Save_Opt_Config_Switches (Save_Config_Switches);
1364 Set_Opt_Config_Switches
1365 (Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit)),
1366 Current_Sem_Unit = Main_Unit);
1368 -- Only do analysis of unit that has not already been analyzed
1370 if not Analyzed (Comp_Unit) then
1371 Initialize_Version (Current_Sem_Unit);
1372 if HLO_Active then
1373 Expander_Mode_Save_And_Set (False);
1374 New_Nodes_OK := 1;
1375 Do_Analyze;
1376 Reset_Analyzed_Flags (Comp_Unit);
1377 Expander_Mode_Restore;
1378 High_Level_Optimize (Comp_Unit);
1379 New_Nodes_OK := 0;
1380 end if;
1382 Do_Analyze;
1383 end if;
1385 -- Save indication of dynamic elaboration checks for ALI file
1387 Set_Dynamic_Elab (Current_Sem_Unit, Dynamic_Elaboration_Checks);
1389 -- Restore settings of saved switches to entry values
1391 Current_Sem_Unit := S_Sem_Unit;
1392 Full_Analysis := S_Full_Analysis;
1393 In_Default_Expression := S_In_Default_Expr;
1394 Inside_A_Generic := S_Inside_A_Generic;
1395 New_Nodes_OK := S_New_Nodes_OK;
1396 Outer_Generic_Scope := S_Outer_Gen_Scope;
1397 GNAT_Mode := S_GNAT_Mode;
1398 Global_Discard_Names := S_Discard_Names;
1400 Restore_Opt_Config_Switches (Save_Config_Switches);
1401 Expander_Mode_Restore;
1403 end Semantics;
1404 end Sem;