1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Util; use Exp_Util;
33 with Freeze; use Freeze;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
40 with Restrict; use Restrict;
41 with Rident; use Rident;
42 with Rtsfind; use Rtsfind;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Case; use Sem_Case;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Disp; use Sem_Disp;
49 with Sem_Elab; use Sem_Elab;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Res; use Sem_Res;
52 with Sem_Type; use Sem_Type;
53 with Sem_Util; use Sem_Util;
54 with Sem_Warn; use Sem_Warn;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Uintp; use Uintp;
62 package body Sem_Ch5 is
64 Unblocked_Exit_Count : Nat := 0;
65 -- This variable is used when processing if statements, case statements,
66 -- and block statements. It counts the number of exit points that are not
67 -- blocked by unconditional transfer instructions: for IF and CASE, these
68 -- are the branches of the conditional; for a block, they are the statement
69 -- sequence of the block, and the statement sequences of any exception
70 -- handlers that are part of the block. When processing is complete, if
71 -- this count is zero, it means that control cannot fall through the IF,
72 -- CASE or block statement. This is used for the generation of warning
73 -- messages. This variable is recursively saved on entry to processing the
74 -- construct, and restored on exit.
76 ------------------------
77 -- Analyze_Assignment --
78 ------------------------
80 procedure Analyze_Assignment (N : Node_Id) is
81 Lhs : constant Node_Id := Name (N);
82 Rhs : constant Node_Id := Expression (N);
87 procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
88 -- N is the node for the left hand side of an assignment, and it is not
89 -- a variable. This routine issues an appropriate diagnostic.
92 -- This is called to kill current value settings of a simple variable
93 -- on the left hand side. We call it if we find any error in analyzing
94 -- the assignment, and at the end of processing before setting any new
95 -- current values in place.
97 procedure Set_Assignment_Type
99 Opnd_Type : in out Entity_Id);
100 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
101 -- nominal subtype. This procedure is used to deal with cases where the
102 -- nominal subtype must be replaced by the actual subtype.
104 -------------------------------
105 -- Diagnose_Non_Variable_Lhs --
106 -------------------------------
108 procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
110 -- Not worth posting another error if left hand side already flagged
111 -- as being illegal in some respect.
113 if Error_Posted (N) then
116 -- Some special bad cases of entity names
118 elsif Is_Entity_Name (N) then
120 Ent : constant Entity_Id := Entity (N);
123 if Ekind (Ent) = E_In_Parameter then
125 ("assignment to IN mode parameter not allowed", N);
127 -- Renamings of protected private components are turned into
128 -- constants when compiling a protected function. In the case
129 -- of single protected types, the private component appears
132 elsif (Is_Prival (Ent)
134 (Ekind (Current_Scope) = E_Function
135 or else Ekind (Enclosing_Dynamic_Scope
136 (Current_Scope)) = E_Function))
138 (Ekind (Ent) = E_Component
139 and then Is_Protected_Type (Scope (Ent)))
142 ("protected function cannot modify protected object", N);
144 elsif Ekind (Ent) = E_Loop_Parameter then
146 ("assignment to loop parameter not allowed", N);
150 ("left hand side of assignment must be a variable", N);
154 -- For indexed components or selected components, test prefix
156 elsif Nkind (N) = N_Indexed_Component then
157 Diagnose_Non_Variable_Lhs (Prefix (N));
159 -- Another special case for assignment to discriminant
161 elsif Nkind (N) = N_Selected_Component then
162 if Present (Entity (Selector_Name (N)))
163 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
166 ("assignment to discriminant not allowed", N);
168 Diagnose_Non_Variable_Lhs (Prefix (N));
172 -- If we fall through, we have no special message to issue!
174 Error_Msg_N ("left hand side of assignment must be a variable", N);
176 end Diagnose_Non_Variable_Lhs;
182 procedure Kill_Lhs is
184 if Is_Entity_Name (Lhs) then
186 Ent : constant Entity_Id := Entity (Lhs);
188 if Present (Ent) then
189 Kill_Current_Values (Ent);
195 -------------------------
196 -- Set_Assignment_Type --
197 -------------------------
199 procedure Set_Assignment_Type
201 Opnd_Type : in out Entity_Id)
204 Require_Entity (Opnd);
206 -- If the assignment operand is an in-out or out parameter, then we
207 -- get the actual subtype (needed for the unconstrained case). If the
208 -- operand is the actual in an entry declaration, then within the
209 -- accept statement it is replaced with a local renaming, which may
210 -- also have an actual subtype.
212 if Is_Entity_Name (Opnd)
213 and then (Ekind (Entity (Opnd)) = E_Out_Parameter
214 or else Ekind (Entity (Opnd)) =
216 or else Ekind (Entity (Opnd)) =
217 E_Generic_In_Out_Parameter
219 (Ekind (Entity (Opnd)) = E_Variable
220 and then Nkind (Parent (Entity (Opnd))) =
221 N_Object_Renaming_Declaration
222 and then Nkind (Parent (Parent (Entity (Opnd)))) =
225 Opnd_Type := Get_Actual_Subtype (Opnd);
227 -- If assignment operand is a component reference, then we get the
228 -- actual subtype of the component for the unconstrained case.
230 elsif Nkind_In (Opnd, N_Selected_Component, N_Explicit_Dereference)
231 and then not Is_Unchecked_Union (Opnd_Type)
233 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
235 if Present (Decl) then
236 Insert_Action (N, Decl);
237 Mark_Rewrite_Insertion (Decl);
239 Opnd_Type := Defining_Identifier (Decl);
240 Set_Etype (Opnd, Opnd_Type);
241 Freeze_Itype (Opnd_Type, N);
243 elsif Is_Constrained (Etype (Opnd)) then
244 Opnd_Type := Etype (Opnd);
247 -- For slice, use the constrained subtype created for the slice
249 elsif Nkind (Opnd) = N_Slice then
250 Opnd_Type := Etype (Opnd);
252 end Set_Assignment_Type;
254 -- Start of processing for Analyze_Assignment
257 Mark_Coextensions (N, Rhs);
262 -- Ensure that we never do an assignment on a variable marked as
263 -- as Safe_To_Reevaluate.
265 pragma Assert (not Is_Entity_Name (Lhs)
266 or else Ekind (Entity (Lhs)) /= E_Variable
267 or else not Is_Safe_To_Reevaluate (Entity (Lhs)));
269 -- Start type analysis for assignment
273 -- In the most general case, both Lhs and Rhs can be overloaded, and we
274 -- must compute the intersection of the possible types on each side.
276 if Is_Overloaded (Lhs) then
283 Get_First_Interp (Lhs, I, It);
285 while Present (It.Typ) loop
286 if Has_Compatible_Type (Rhs, It.Typ) then
287 if T1 /= Any_Type then
289 -- An explicit dereference is overloaded if the prefix
290 -- is. Try to remove the ambiguity on the prefix, the
291 -- error will be posted there if the ambiguity is real.
293 if Nkind (Lhs) = N_Explicit_Dereference then
296 PI1 : Interp_Index := 0;
302 Get_First_Interp (Prefix (Lhs), PI, PIt);
304 while Present (PIt.Typ) loop
305 if Is_Access_Type (PIt.Typ)
306 and then Has_Compatible_Type
307 (Rhs, Designated_Type (PIt.Typ))
311 Disambiguate (Prefix (Lhs),
314 if PIt = No_Interp then
316 ("ambiguous left-hand side"
317 & " in assignment", Lhs);
320 Resolve (Prefix (Lhs), PIt.Typ);
330 Get_Next_Interp (PI, PIt);
336 ("ambiguous left-hand side in assignment", Lhs);
344 Get_Next_Interp (I, It);
348 if T1 = Any_Type then
350 ("no valid types for left-hand side for assignment", Lhs);
356 -- The resulting assignment type is T1, so now we will resolve the left
357 -- hand side of the assignment using this determined type.
361 -- Cases where Lhs is not a variable
363 if not Is_Variable (Lhs) then
365 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
373 if Ada_Version >= Ada_2005 then
375 -- Handle chains of renamings
378 while Nkind (Ent) in N_Has_Entity
379 and then Present (Entity (Ent))
380 and then Present (Renamed_Object (Entity (Ent)))
382 Ent := Renamed_Object (Entity (Ent));
385 if (Nkind (Ent) = N_Attribute_Reference
386 and then Attribute_Name (Ent) = Name_Priority)
388 -- Renamings of the attribute Priority applied to protected
389 -- objects have been previously expanded into calls to the
390 -- Get_Ceiling run-time subprogram.
393 (Nkind (Ent) = N_Function_Call
394 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
396 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
398 -- The enclosing subprogram cannot be a protected function
401 while not (Is_Subprogram (S)
402 and then Convention (S) = Convention_Protected)
403 and then S /= Standard_Standard
408 if Ekind (S) = E_Function
409 and then Convention (S) = Convention_Protected
412 ("protected function cannot modify protected object",
416 -- Changes of the ceiling priority of the protected object
417 -- are only effective if the Ceiling_Locking policy is in
418 -- effect (AARM D.5.2 (5/2)).
420 if Locking_Policy /= 'C' then
421 Error_Msg_N ("assignment to the attribute PRIORITY has " &
423 Error_Msg_N ("\since no Locking_Policy has been " &
432 Diagnose_Non_Variable_Lhs (Lhs);
435 -- Error of assigning to limited type. We do however allow this in
436 -- certain cases where the front end generates the assignments.
438 elsif Is_Limited_Type (T1)
439 and then not Assignment_OK (Lhs)
440 and then not Assignment_OK (Original_Node (Lhs))
441 and then not Is_Value_Type (T1)
443 -- CPP constructors can only be called in declarations
445 if Is_CPP_Constructor_Call (Rhs) then
446 Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
449 ("left hand of assignment must not be limited type", Lhs);
450 Explain_Limited_Type (T1, Lhs);
454 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
455 -- abstract. This is only checked when the assignment Comes_From_Source,
456 -- because in some cases the expander generates such assignments (such
457 -- in the _assign operation for an abstract type).
459 elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
461 ("target of assignment operation must not be abstract", Lhs);
464 -- Resolution may have updated the subtype, in case the left-hand side
465 -- is a private protected component. Use the correct subtype to avoid
466 -- scoping issues in the back-end.
470 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
471 -- type. For example:
475 -- type Acc is access P.T;
478 -- with Pkg; use Acc;
479 -- procedure Example is
482 -- A.all := B.all; -- ERROR
485 if Nkind (Lhs) = N_Explicit_Dereference
486 and then Ekind (T1) = E_Incomplete_Type
488 Error_Msg_N ("invalid use of incomplete type", Lhs);
493 -- Now we can complete the resolution of the right hand side
495 Set_Assignment_Type (Lhs, T1);
498 -- This is the point at which we check for an unset reference
500 Check_Unset_Reference (Rhs);
501 Check_Unprotected_Access (Lhs, Rhs);
503 -- Remaining steps are skipped if Rhs was syntactically in error
512 if not Covers (T1, T2) then
513 Wrong_Type (Rhs, Etype (Lhs));
518 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
519 -- types, use the non-limited view if available
521 if Nkind (Rhs) = N_Explicit_Dereference
522 and then Ekind (T2) = E_Incomplete_Type
523 and then Is_Tagged_Type (T2)
524 and then Present (Non_Limited_View (T2))
526 T2 := Non_Limited_View (T2);
529 Set_Assignment_Type (Rhs, T2);
531 if Total_Errors_Detected /= 0 then
541 if T1 = Any_Type or else T2 = Any_Type then
546 -- If the rhs is class-wide or dynamically tagged, then require the lhs
547 -- to be class-wide. The case where the rhs is a dynamically tagged call
548 -- to a dispatching operation with a controlling access result is
549 -- excluded from this check, since the target has an access type (and
550 -- no tag propagation occurs in that case).
552 if (Is_Class_Wide_Type (T2)
553 or else (Is_Dynamically_Tagged (Rhs)
554 and then not Is_Access_Type (T1)))
555 and then not Is_Class_Wide_Type (T1)
557 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
559 elsif Is_Class_Wide_Type (T1)
560 and then not Is_Class_Wide_Type (T2)
561 and then not Is_Tag_Indeterminate (Rhs)
562 and then not Is_Dynamically_Tagged (Rhs)
564 Error_Msg_N ("dynamically tagged expression required!", Rhs);
567 -- Propagate the tag from a class-wide target to the rhs when the rhs
568 -- is a tag-indeterminate call.
570 if Is_Tag_Indeterminate (Rhs) then
571 if Is_Class_Wide_Type (T1) then
572 Propagate_Tag (Lhs, Rhs);
574 elsif Nkind (Rhs) = N_Function_Call
575 and then Is_Entity_Name (Name (Rhs))
576 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
579 ("call to abstract function must be dispatching", Name (Rhs));
581 elsif Nkind (Rhs) = N_Qualified_Expression
582 and then Nkind (Expression (Rhs)) = N_Function_Call
583 and then Is_Entity_Name (Name (Expression (Rhs)))
585 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
588 ("call to abstract function must be dispatching",
589 Name (Expression (Rhs)));
593 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
594 -- apply an implicit conversion of the rhs to that type to force
595 -- appropriate static and run-time accessibility checks. This applies
596 -- as well to anonymous access-to-subprogram types that are component
597 -- subtypes or formal parameters.
599 if Ada_Version >= Ada_2005
600 and then Is_Access_Type (T1)
602 if Is_Local_Anonymous_Access (T1)
603 or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
605 -- Handle assignment to an Ada 2012 stand-alone object
606 -- of an anonymous access type.
608 or else (Ekind (T1) = E_Anonymous_Access_Type
609 and then Nkind (Associated_Node_For_Itype (T1)) =
610 N_Object_Declaration)
613 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
614 Analyze_And_Resolve (Rhs, T1);
618 -- Ada 2005 (AI-231): Assignment to not null variable
620 if Ada_Version >= Ada_2005
621 and then Can_Never_Be_Null (T1)
622 and then not Assignment_OK (Lhs)
624 -- Case where we know the right hand side is null
626 if Known_Null (Rhs) then
627 Apply_Compile_Time_Constraint_Error
629 Msg => "(Ada 2005) null not allowed in null-excluding objects?",
630 Reason => CE_Null_Not_Allowed);
632 -- We still mark this as a possible modification, that's necessary
633 -- to reset Is_True_Constant, and desirable for xref purposes.
635 Note_Possible_Modification (Lhs, Sure => True);
638 -- If we know the right hand side is non-null, then we convert to the
639 -- target type, since we don't need a run time check in that case.
641 elsif not Can_Never_Be_Null (T2) then
642 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
643 Analyze_And_Resolve (Rhs, T1);
647 if Is_Scalar_Type (T1) then
648 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
650 -- For array types, verify that lengths match. If the right hand side
651 -- is a function call that has been inlined, the assignment has been
652 -- rewritten as a block, and the constraint check will be applied to the
653 -- assignment within the block.
655 elsif Is_Array_Type (T1)
657 (Nkind (Rhs) /= N_Type_Conversion
658 or else Is_Constrained (Etype (Rhs)))
660 (Nkind (Rhs) /= N_Function_Call
661 or else Nkind (N) /= N_Block_Statement)
663 -- Assignment verifies that the length of the Lsh and Rhs are equal,
664 -- but of course the indexes do not have to match. If the right-hand
665 -- side is a type conversion to an unconstrained type, a length check
666 -- is performed on the expression itself during expansion. In rare
667 -- cases, the redundant length check is computed on an index type
668 -- with a different representation, triggering incorrect code in the
671 Apply_Length_Check (Rhs, Etype (Lhs));
674 -- Discriminant checks are applied in the course of expansion
679 -- Note: modifications of the Lhs may only be recorded after
680 -- checks have been applied.
682 Note_Possible_Modification (Lhs, Sure => True);
683 Check_Order_Dependence;
685 -- ??? a real accessibility check is needed when ???
687 -- Post warning for redundant assignment or variable to itself
689 if Warn_On_Redundant_Constructs
691 -- We only warn for source constructs
693 and then Comes_From_Source (N)
695 -- Where the object is the same on both sides
697 and then Same_Object (Lhs, Original_Node (Rhs))
699 -- But exclude the case where the right side was an operation that
700 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
701 -- don't want to warn in such a case, since it is reasonable to write
702 -- such expressions especially when K is defined symbolically in some
705 and then Nkind (Original_Node (Rhs)) not in N_Op
707 if Nkind (Lhs) in N_Has_Entity then
708 Error_Msg_NE -- CODEFIX
709 ("?useless assignment of & to itself!", N, Entity (Lhs));
711 Error_Msg_N -- CODEFIX
712 ("?useless assignment of object to itself!", N);
716 -- Check for non-allowed composite assignment
718 if not Support_Composite_Assign_On_Target
719 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
720 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
722 Error_Msg_CRT ("composite assignment", N);
725 -- Check elaboration warning for left side if not in elab code
727 if not In_Subprogram_Or_Concurrent_Unit then
728 Check_Elab_Assign (Lhs);
731 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
732 -- assignment is a source assignment in the extended main source unit.
733 -- We are not interested in any reference information outside this
734 -- context, or in compiler generated assignment statements.
736 if Comes_From_Source (N)
737 and then In_Extended_Main_Source_Unit (Lhs)
739 Set_Referenced_Modified (Lhs, Out_Param => False);
742 -- Final step. If left side is an entity, then we may be able to reset
743 -- the current tracked values to new safe values. We only have something
744 -- to do if the left side is an entity name, and expansion has not
745 -- modified the node into something other than an assignment, and of
746 -- course we only capture values if it is safe to do so.
748 if Is_Entity_Name (Lhs)
749 and then Nkind (N) = N_Assignment_Statement
752 Ent : constant Entity_Id := Entity (Lhs);
755 if Safe_To_Capture_Value (N, Ent) then
757 -- If simple variable on left side, warn if this assignment
758 -- blots out another one (rendering it useless). We only do
759 -- this for source assignments, otherwise we can generate bogus
760 -- warnings when an assignment is rewritten as another
761 -- assignment, and gets tied up with itself.
763 if Warn_On_Modified_Unread
764 and then Is_Assignable (Ent)
765 and then Comes_From_Source (N)
766 and then In_Extended_Main_Source_Unit (Ent)
768 Warn_On_Useless_Assignment (Ent, N);
771 -- If we are assigning an access type and the left side is an
772 -- entity, then make sure that the Is_Known_[Non_]Null flags
773 -- properly reflect the state of the entity after assignment.
775 if Is_Access_Type (T1) then
776 if Known_Non_Null (Rhs) then
777 Set_Is_Known_Non_Null (Ent, True);
779 elsif Known_Null (Rhs)
780 and then not Can_Never_Be_Null (Ent)
782 Set_Is_Known_Null (Ent, True);
785 Set_Is_Known_Null (Ent, False);
787 if not Can_Never_Be_Null (Ent) then
788 Set_Is_Known_Non_Null (Ent, False);
792 -- For discrete types, we may be able to set the current value
793 -- if the value is known at compile time.
795 elsif Is_Discrete_Type (T1)
796 and then Compile_Time_Known_Value (Rhs)
798 Set_Current_Value (Ent, Rhs);
800 Set_Current_Value (Ent, Empty);
803 -- If not safe to capture values, kill them
811 -- If assigning to an object in whole or in part, note location of
812 -- assignment in case no one references value. We only do this for
813 -- source assignments, otherwise we can generate bogus warnings when an
814 -- assignment is rewritten as another assignment, and gets tied up with
818 Ent : constant Entity_Id := Get_Enclosing_Object (Lhs);
822 and then Safe_To_Capture_Value (N, Ent)
823 and then Nkind (N) = N_Assignment_Statement
824 and then Warn_On_Modified_Unread
825 and then Is_Assignable (Ent)
826 and then Comes_From_Source (N)
827 and then In_Extended_Main_Source_Unit (Ent)
829 Set_Last_Assignment (Ent, Lhs);
832 end Analyze_Assignment;
834 -----------------------------
835 -- Analyze_Block_Statement --
836 -----------------------------
838 procedure Analyze_Block_Statement (N : Node_Id) is
839 Decls : constant List_Id := Declarations (N);
840 Id : constant Node_Id := Identifier (N);
841 HSS : constant Node_Id := Handled_Statement_Sequence (N);
844 -- In SPARK mode, we reject block statements. Note that the case of
845 -- block statements generated by the expander is fine.
847 if Nkind (Original_Node (N)) = N_Block_Statement then
848 Check_SPARK_Restriction ("block statement is not allowed", N);
851 -- If no handled statement sequence is present, things are really messed
852 -- up, and we just return immediately (defence against previous errors).
858 -- Normal processing with HSS present
861 EH : constant List_Id := Exception_Handlers (HSS);
862 Ent : Entity_Id := Empty;
865 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
866 -- Recursively save value of this global, will be restored on exit
869 -- Initialize unblocked exit count for statements of begin block
870 -- plus one for each exception handler that is present.
872 Unblocked_Exit_Count := 1;
875 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
878 -- If a label is present analyze it and mark it as referenced
884 -- An error defense. If we have an identifier, but no entity, then
885 -- something is wrong. If previous errors, then just remove the
886 -- identifier and continue, otherwise raise an exception.
889 if Total_Errors_Detected /= 0 then
890 Set_Identifier (N, Empty);
896 Set_Ekind (Ent, E_Block);
897 Generate_Reference (Ent, N, ' ');
898 Generate_Definition (Ent);
900 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
901 Set_Label_Construct (Parent (Ent), N);
906 -- If no entity set, create a label entity
909 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
910 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
914 Set_Etype (Ent, Standard_Void_Type);
915 Set_Block_Node (Ent, Identifier (N));
918 if Present (Decls) then
919 Analyze_Declarations (Decls);
921 Inspect_Deferred_Constant_Completion (Decls);
925 Process_End_Label (HSS, 'e', Ent);
927 -- If exception handlers are present, then we indicate that enclosing
928 -- scopes contain a block with handlers. We only need to mark non-
934 Set_Has_Nested_Block_With_Handler (S);
935 exit when Is_Overloadable (S)
936 or else Ekind (S) = E_Package
937 or else Is_Generic_Unit (S);
942 Check_References (Ent);
943 Warn_On_Useless_Assignments (Ent);
946 if Unblocked_Exit_Count = 0 then
947 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
948 Check_Unreachable_Code (N);
950 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
953 end Analyze_Block_Statement;
955 ----------------------------
956 -- Analyze_Case_Statement --
957 ----------------------------
959 procedure Analyze_Case_Statement (N : Node_Id) is
961 Exp_Type : Entity_Id;
962 Exp_Btype : Entity_Id;
965 Others_Present : Boolean;
967 pragma Warnings (Off, Last_Choice);
968 pragma Warnings (Off, Dont_Care);
969 -- Don't care about assigned values
971 Statements_Analyzed : Boolean := False;
972 -- Set True if at least some statement sequences get analyzed. If False
973 -- on exit, means we had a serious error that prevented full analysis of
974 -- the case statement, and as a result it is not a good idea to output
975 -- warning messages about unreachable code.
977 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
978 -- Recursively save value of this global, will be restored on exit
980 procedure Non_Static_Choice_Error (Choice : Node_Id);
981 -- Error routine invoked by the generic instantiation below when the
982 -- case statement has a non static choice.
984 procedure Process_Statements (Alternative : Node_Id);
985 -- Analyzes all the statements associated with a case alternative.
986 -- Needed by the generic instantiation below.
988 package Case_Choices_Processing is new
989 Generic_Choices_Processing
990 (Get_Alternatives => Alternatives,
991 Get_Choices => Discrete_Choices,
992 Process_Empty_Choice => No_OP,
993 Process_Non_Static_Choice => Non_Static_Choice_Error,
994 Process_Associated_Node => Process_Statements);
995 use Case_Choices_Processing;
996 -- Instantiation of the generic choice processing package
998 -----------------------------
999 -- Non_Static_Choice_Error --
1000 -----------------------------
1002 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1004 Flag_Non_Static_Expr
1005 ("choice given in case statement is not static!", Choice);
1006 end Non_Static_Choice_Error;
1008 ------------------------
1009 -- Process_Statements --
1010 ------------------------
1012 procedure Process_Statements (Alternative : Node_Id) is
1013 Choices : constant List_Id := Discrete_Choices (Alternative);
1017 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1018 Statements_Analyzed := True;
1020 -- An interesting optimization. If the case statement expression
1021 -- is a simple entity, then we can set the current value within an
1022 -- alternative if the alternative has one possible value.
1026 -- when 2 | 3 => beta
1027 -- when others => gamma
1029 -- Here we know that N is initially 1 within alpha, but for beta and
1030 -- gamma, we do not know anything more about the initial value.
1032 if Is_Entity_Name (Exp) then
1033 Ent := Entity (Exp);
1035 if Ekind_In (Ent, E_Variable,
1039 if List_Length (Choices) = 1
1040 and then Nkind (First (Choices)) in N_Subexpr
1041 and then Compile_Time_Known_Value (First (Choices))
1043 Set_Current_Value (Entity (Exp), First (Choices));
1046 Analyze_Statements (Statements (Alternative));
1048 -- After analyzing the case, set the current value to empty
1049 -- since we won't know what it is for the next alternative
1050 -- (unless reset by this same circuit), or after the case.
1052 Set_Current_Value (Entity (Exp), Empty);
1057 -- Case where expression is not an entity name of a variable
1059 Analyze_Statements (Statements (Alternative));
1060 end Process_Statements;
1062 -- Start of processing for Analyze_Case_Statement
1065 Unblocked_Exit_Count := 0;
1066 Exp := Expression (N);
1069 -- The expression must be of any discrete type. In rare cases, the
1070 -- expander constructs a case statement whose expression has a private
1071 -- type whose full view is discrete. This can happen when generating
1072 -- a stream operation for a variant type after the type is frozen,
1073 -- when the partial of view of the type of the discriminant is private.
1074 -- In that case, use the full view to analyze case alternatives.
1076 if not Is_Overloaded (Exp)
1077 and then not Comes_From_Source (N)
1078 and then Is_Private_Type (Etype (Exp))
1079 and then Present (Full_View (Etype (Exp)))
1080 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1082 Resolve (Exp, Etype (Exp));
1083 Exp_Type := Full_View (Etype (Exp));
1086 Analyze_And_Resolve (Exp, Any_Discrete);
1087 Exp_Type := Etype (Exp);
1090 Check_Unset_Reference (Exp);
1091 Exp_Btype := Base_Type (Exp_Type);
1093 -- The expression must be of a discrete type which must be determinable
1094 -- independently of the context in which the expression occurs, but
1095 -- using the fact that the expression must be of a discrete type.
1096 -- Moreover, the type this expression must not be a character literal
1097 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1099 -- If error already reported by Resolve, nothing more to do
1101 if Exp_Btype = Any_Discrete
1102 or else Exp_Btype = Any_Type
1106 elsif Exp_Btype = Any_Character then
1108 ("character literal as case expression is ambiguous", Exp);
1111 elsif Ada_Version = Ada_83
1112 and then (Is_Generic_Type (Exp_Btype)
1113 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1116 ("(Ada 83) case expression cannot be of a generic type", Exp);
1120 -- If the case expression is a formal object of mode in out, then treat
1121 -- it as having a nonstatic subtype by forcing use of the base type
1122 -- (which has to get passed to Check_Case_Choices below). Also use base
1123 -- type when the case expression is parenthesized.
1125 if Paren_Count (Exp) > 0
1126 or else (Is_Entity_Name (Exp)
1127 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1129 Exp_Type := Exp_Btype;
1132 -- Call instantiated Analyze_Choices which does the rest of the work
1134 Analyze_Choices (N, Exp_Type, Dont_Care, Others_Present);
1136 -- A case statement with a single OTHERS alternative is not allowed
1140 and then List_Length (Alternatives (N)) = 1
1142 Check_SPARK_Restriction
1143 ("OTHERS as unique case alternative is not allowed", N);
1146 if Exp_Type = Universal_Integer and then not Others_Present then
1147 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1150 -- If all our exits were blocked by unconditional transfers of control,
1151 -- then the entire CASE statement acts as an unconditional transfer of
1152 -- control, so treat it like one, and check unreachable code. Skip this
1153 -- test if we had serious errors preventing any statement analysis.
1155 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1156 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1157 Check_Unreachable_Code (N);
1159 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1162 if not Expander_Active
1163 and then Compile_Time_Known_Value (Expression (N))
1164 and then Serious_Errors_Detected = 0
1167 Chosen : constant Node_Id := Find_Static_Alternative (N);
1171 Alt := First (Alternatives (N));
1172 while Present (Alt) loop
1173 if Alt /= Chosen then
1174 Remove_Warning_Messages (Statements (Alt));
1181 end Analyze_Case_Statement;
1183 ----------------------------
1184 -- Analyze_Exit_Statement --
1185 ----------------------------
1187 -- If the exit includes a name, it must be the name of a currently open
1188 -- loop. Otherwise there must be an innermost open loop on the stack, to
1189 -- which the statement implicitly refers.
1191 -- Additionally, in SPARK mode:
1193 -- The exit can only name the closest enclosing loop;
1195 -- An exit with a when clause must be directly contained in a loop;
1197 -- An exit without a when clause must be directly contained in an
1198 -- if-statement with no elsif or else, which is itself directly contained
1199 -- in a loop. The exit must be the last statement in the if-statement.
1201 procedure Analyze_Exit_Statement (N : Node_Id) is
1202 Target : constant Node_Id := Name (N);
1203 Cond : constant Node_Id := Condition (N);
1204 Scope_Id : Entity_Id;
1210 Check_Unreachable_Code (N);
1213 if Present (Target) then
1215 U_Name := Entity (Target);
1217 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1218 Error_Msg_N ("invalid loop name in exit statement", N);
1222 if Has_Loop_In_Inner_Open_Scopes (U_Name) then
1223 Check_SPARK_Restriction
1224 ("exit label must name the closest enclosing loop", N);
1227 Set_Has_Exit (U_Name);
1234 for J in reverse 0 .. Scope_Stack.Last loop
1235 Scope_Id := Scope_Stack.Table (J).Entity;
1236 Kind := Ekind (Scope_Id);
1239 and then (No (Target) or else Scope_Id = U_Name)
1241 Set_Has_Exit (Scope_Id);
1244 elsif Kind = E_Block
1245 or else Kind = E_Loop
1246 or else Kind = E_Return_Statement
1252 ("cannot exit from program unit or accept statement", N);
1257 -- Verify that if present the condition is a Boolean expression
1259 if Present (Cond) then
1260 Analyze_And_Resolve (Cond, Any_Boolean);
1261 Check_Unset_Reference (Cond);
1264 -- In SPARK mode, verify that the exit statement respects the SPARK
1267 if Present (Cond) then
1268 if Nkind (Parent (N)) /= N_Loop_Statement then
1269 Check_SPARK_Restriction
1270 ("exit with when clause must be directly in loop", N);
1274 if Nkind (Parent (N)) /= N_If_Statement then
1275 if Nkind (Parent (N)) = N_Elsif_Part then
1276 Check_SPARK_Restriction
1277 ("exit must be in IF without ELSIF", N);
1279 Check_SPARK_Restriction ("exit must be directly in IF", N);
1282 elsif Nkind (Parent (Parent (N))) /= N_Loop_Statement then
1283 Check_SPARK_Restriction
1284 ("exit must be in IF directly in loop", N);
1286 -- First test the presence of ELSE, so that an exit in an ELSE leads
1287 -- to an error mentioning the ELSE.
1289 elsif Present (Else_Statements (Parent (N))) then
1290 Check_SPARK_Restriction ("exit must be in IF without ELSE", N);
1292 -- An exit in an ELSIF does not reach here, as it would have been
1293 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1295 elsif Present (Elsif_Parts (Parent (N))) then
1296 Check_SPARK_Restriction ("exit must be in IF without ELSIF", N);
1300 -- Chain exit statement to associated loop entity
1302 Set_Next_Exit_Statement (N, First_Exit_Statement (Scope_Id));
1303 Set_First_Exit_Statement (Scope_Id, N);
1305 -- Since the exit may take us out of a loop, any previous assignment
1306 -- statement is not useless, so clear last assignment indications. It
1307 -- is OK to keep other current values, since if the exit statement
1308 -- does not exit, then the current values are still valid.
1310 Kill_Current_Values (Last_Assignment_Only => True);
1311 end Analyze_Exit_Statement;
1313 ----------------------------
1314 -- Analyze_Goto_Statement --
1315 ----------------------------
1317 procedure Analyze_Goto_Statement (N : Node_Id) is
1318 Label : constant Node_Id := Name (N);
1319 Scope_Id : Entity_Id;
1320 Label_Scope : Entity_Id;
1321 Label_Ent : Entity_Id;
1324 Check_SPARK_Restriction ("goto statement is not allowed", N);
1326 -- Actual semantic checks
1328 Check_Unreachable_Code (N);
1329 Kill_Current_Values (Last_Assignment_Only => True);
1332 Label_Ent := Entity (Label);
1334 -- Ignore previous error
1336 if Label_Ent = Any_Id then
1339 -- We just have a label as the target of a goto
1341 elsif Ekind (Label_Ent) /= E_Label then
1342 Error_Msg_N ("target of goto statement must be a label", Label);
1345 -- Check that the target of the goto is reachable according to Ada
1346 -- scoping rules. Note: the special gotos we generate for optimizing
1347 -- local handling of exceptions would violate these rules, but we mark
1348 -- such gotos as analyzed when built, so this code is never entered.
1350 elsif not Reachable (Label_Ent) then
1351 Error_Msg_N ("target of goto statement is not reachable", Label);
1355 -- Here if goto passes initial validity checks
1357 Label_Scope := Enclosing_Scope (Label_Ent);
1359 for J in reverse 0 .. Scope_Stack.Last loop
1360 Scope_Id := Scope_Stack.Table (J).Entity;
1362 if Label_Scope = Scope_Id
1363 or else (Ekind (Scope_Id) /= E_Block
1364 and then Ekind (Scope_Id) /= E_Loop
1365 and then Ekind (Scope_Id) /= E_Return_Statement)
1367 if Scope_Id /= Label_Scope then
1369 ("cannot exit from program unit or accept statement", N);
1376 raise Program_Error;
1377 end Analyze_Goto_Statement;
1379 --------------------------
1380 -- Analyze_If_Statement --
1381 --------------------------
1383 -- A special complication arises in the analysis of if statements
1385 -- The expander has circuitry to completely delete code that it can tell
1386 -- will not be executed (as a result of compile time known conditions). In
1387 -- the analyzer, we ensure that code that will be deleted in this manner is
1388 -- analyzed but not expanded. This is obviously more efficient, but more
1389 -- significantly, difficulties arise if code is expanded and then
1390 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1391 -- generated in deleted code must be frozen from start, because the nodes
1392 -- on which they depend will not be available at the freeze point.
1394 procedure Analyze_If_Statement (N : Node_Id) is
1397 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1398 -- Recursively save value of this global, will be restored on exit
1400 Save_In_Deleted_Code : Boolean;
1402 Del : Boolean := False;
1403 -- This flag gets set True if a True condition has been found, which
1404 -- means that remaining ELSE/ELSIF parts are deleted.
1406 procedure Analyze_Cond_Then (Cnode : Node_Id);
1407 -- This is applied to either the N_If_Statement node itself or to an
1408 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1409 -- statements associated with it.
1411 -----------------------
1412 -- Analyze_Cond_Then --
1413 -----------------------
1415 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1416 Cond : constant Node_Id := Condition (Cnode);
1417 Tstm : constant List_Id := Then_Statements (Cnode);
1420 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1421 Analyze_And_Resolve (Cond, Any_Boolean);
1422 Check_Unset_Reference (Cond);
1423 Set_Current_Value_Condition (Cnode);
1425 -- If already deleting, then just analyze then statements
1428 Analyze_Statements (Tstm);
1430 -- Compile time known value, not deleting yet
1432 elsif Compile_Time_Known_Value (Cond) then
1433 Save_In_Deleted_Code := In_Deleted_Code;
1435 -- If condition is True, then analyze the THEN statements and set
1436 -- no expansion for ELSE and ELSIF parts.
1438 if Is_True (Expr_Value (Cond)) then
1439 Analyze_Statements (Tstm);
1441 Expander_Mode_Save_And_Set (False);
1442 In_Deleted_Code := True;
1444 -- If condition is False, analyze THEN with expansion off
1446 else -- Is_False (Expr_Value (Cond))
1447 Expander_Mode_Save_And_Set (False);
1448 In_Deleted_Code := True;
1449 Analyze_Statements (Tstm);
1450 Expander_Mode_Restore;
1451 In_Deleted_Code := Save_In_Deleted_Code;
1454 -- Not known at compile time, not deleting, normal analysis
1457 Analyze_Statements (Tstm);
1459 end Analyze_Cond_Then;
1461 -- Start of Analyze_If_Statement
1464 -- Initialize exit count for else statements. If there is no else part,
1465 -- this count will stay non-zero reflecting the fact that the uncovered
1466 -- else case is an unblocked exit.
1468 Unblocked_Exit_Count := 1;
1469 Analyze_Cond_Then (N);
1471 -- Now to analyze the elsif parts if any are present
1473 if Present (Elsif_Parts (N)) then
1474 E := First (Elsif_Parts (N));
1475 while Present (E) loop
1476 Analyze_Cond_Then (E);
1481 if Present (Else_Statements (N)) then
1482 Analyze_Statements (Else_Statements (N));
1485 -- If all our exits were blocked by unconditional transfers of control,
1486 -- then the entire IF statement acts as an unconditional transfer of
1487 -- control, so treat it like one, and check unreachable code.
1489 if Unblocked_Exit_Count = 0 then
1490 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1491 Check_Unreachable_Code (N);
1493 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1497 Expander_Mode_Restore;
1498 In_Deleted_Code := Save_In_Deleted_Code;
1501 if not Expander_Active
1502 and then Compile_Time_Known_Value (Condition (N))
1503 and then Serious_Errors_Detected = 0
1505 if Is_True (Expr_Value (Condition (N))) then
1506 Remove_Warning_Messages (Else_Statements (N));
1508 if Present (Elsif_Parts (N)) then
1509 E := First (Elsif_Parts (N));
1510 while Present (E) loop
1511 Remove_Warning_Messages (Then_Statements (E));
1517 Remove_Warning_Messages (Then_Statements (N));
1520 end Analyze_If_Statement;
1522 ----------------------------------------
1523 -- Analyze_Implicit_Label_Declaration --
1524 ----------------------------------------
1526 -- An implicit label declaration is generated in the innermost enclosing
1527 -- declarative part. This is done for labels, and block and loop names.
1529 -- Note: any changes in this routine may need to be reflected in
1530 -- Analyze_Label_Entity.
1532 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1533 Id : constant Node_Id := Defining_Identifier (N);
1536 Set_Ekind (Id, E_Label);
1537 Set_Etype (Id, Standard_Void_Type);
1538 Set_Enclosing_Scope (Id, Current_Scope);
1539 end Analyze_Implicit_Label_Declaration;
1541 ------------------------------
1542 -- Analyze_Iteration_Scheme --
1543 ------------------------------
1545 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1547 procedure Process_Bounds (R : Node_Id);
1548 -- If the iteration is given by a range, create temporaries and
1549 -- assignment statements block to capture the bounds and perform
1550 -- required finalization actions in case a bound includes a function
1551 -- call that uses the temporary stack. We first pre-analyze a copy of
1552 -- the range in order to determine the expected type, and analyze and
1553 -- resolve the original bounds.
1555 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1556 -- If the bounds are given by a 'Range reference on a function call
1557 -- that returns a controlled array, introduce an explicit declaration
1558 -- to capture the bounds, so that the function result can be finalized
1559 -- in timely fashion.
1561 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean;
1562 -- N is the node for an arbitrary construct. This function searches the
1563 -- construct N to see if any expressions within it contain function
1564 -- calls that use the secondary stack, returning True if any such call
1565 -- is found, and False otherwise.
1567 procedure Pre_Analyze_Range (R_Copy : Node_Id);
1568 -- Determine expected type of range or domain of iteration of Ada 2012
1569 -- loop by analyzing separate copy. Do the analysis and resolution of
1570 -- the copy of the bound(s) with expansion disabled, to prevent the
1571 -- generation of finalization actions. This prevents memory leaks when
1572 -- the bounds contain calls to functions returning controlled arrays or
1573 -- when the domain of iteration is a container.
1575 -----------------------
1576 -- Pre_Analyze_Range --
1577 -----------------------
1579 procedure Pre_Analyze_Range (R_Copy : Node_Id) is
1580 Save_Analysis : Boolean;
1582 Save_Analysis := Full_Analysis;
1583 Full_Analysis := False;
1584 Expander_Mode_Save_And_Set (False);
1588 if Nkind (R_Copy) in N_Subexpr
1589 and then Is_Overloaded (R_Copy)
1592 -- Apply preference rules for range of predefined integer types,
1593 -- or diagnose true ambiguity.
1598 Found : Entity_Id := Empty;
1601 Get_First_Interp (R_Copy, I, It);
1602 while Present (It.Typ) loop
1603 if Is_Discrete_Type (It.Typ) then
1607 if Scope (Found) = Standard_Standard then
1610 elsif Scope (It.Typ) = Standard_Standard then
1614 -- Both of them are user-defined
1617 ("ambiguous bounds in range of iteration",
1619 Error_Msg_N ("\possible interpretations:", R_Copy);
1620 Error_Msg_NE ("\\} ", R_Copy, Found);
1621 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
1627 Get_Next_Interp (I, It);
1632 if Is_Entity_Name (R_Copy)
1633 and then Is_Type (Entity (R_Copy))
1636 -- Subtype mark in iteration scheme
1640 elsif Nkind (R_Copy) in N_Subexpr then
1642 -- Expression in range, or Ada 2012 iterator
1647 Expander_Mode_Restore;
1648 Full_Analysis := Save_Analysis;
1649 end Pre_Analyze_Range;
1651 --------------------
1652 -- Process_Bounds --
1653 --------------------
1655 procedure Process_Bounds (R : Node_Id) is
1656 Loc : constant Source_Ptr := Sloc (N);
1657 R_Copy : constant Node_Id := New_Copy_Tree (R);
1658 Lo : constant Node_Id := Low_Bound (R);
1659 Hi : constant Node_Id := High_Bound (R);
1660 New_Lo_Bound : Node_Id;
1661 New_Hi_Bound : Node_Id;
1665 (Original_Bound : Node_Id;
1666 Analyzed_Bound : Node_Id) return Node_Id;
1667 -- Capture value of bound and return captured value
1674 (Original_Bound : Node_Id;
1675 Analyzed_Bound : Node_Id) return Node_Id
1682 -- If the bound is a constant or an object, no need for a separate
1683 -- declaration. If the bound is the result of previous expansion
1684 -- it is already analyzed and should not be modified. Note that
1685 -- the Bound will be resolved later, if needed, as part of the
1686 -- call to Make_Index (literal bounds may need to be resolved to
1689 if Analyzed (Original_Bound) then
1690 return Original_Bound;
1692 elsif Nkind_In (Analyzed_Bound, N_Integer_Literal,
1693 N_Character_Literal)
1694 or else Is_Entity_Name (Analyzed_Bound)
1696 Analyze_And_Resolve (Original_Bound, Typ);
1697 return Original_Bound;
1700 -- Here we need to capture the value
1702 Analyze_And_Resolve (Original_Bound, Typ);
1704 -- Normally, the best approach is simply to generate a constant
1705 -- declaration that captures the bound. However, there is a nasty
1706 -- case where this is wrong. If the bound is complex, and has a
1707 -- possible use of the secondary stack, we need to generate a
1708 -- separate assignment statement to ensure the creation of a block
1709 -- which will release the secondary stack.
1711 -- We prefer the constant declaration, since it leaves us with a
1712 -- proper trace of the value, useful in optimizations that get rid
1713 -- of junk range checks.
1715 if not Has_Call_Using_Secondary_Stack (Original_Bound) then
1716 Force_Evaluation (Original_Bound);
1717 return Original_Bound;
1720 Id := Make_Temporary (Loc, 'R', Original_Bound);
1722 -- Here we make a declaration with a separate assignment
1723 -- statement, and insert before loop header.
1726 Make_Object_Declaration (Loc,
1727 Defining_Identifier => Id,
1728 Object_Definition => New_Occurrence_Of (Typ, Loc));
1731 Make_Assignment_Statement (Loc,
1732 Name => New_Occurrence_Of (Id, Loc),
1733 Expression => Relocate_Node (Original_Bound));
1735 -- We must recursively clean in the relocated expression the flag
1736 -- analyzed to ensure that the expression is reanalyzed. Required
1737 -- to ensure that the transient scope is established now (because
1738 -- Establish_Transient_Scope discarded generating transient scopes
1739 -- in the analysis of the iteration scheme).
1741 Reset_Analyzed_Flags (Expression (Assign));
1743 Insert_Actions (Parent (N), New_List (Decl, Assign));
1745 -- Now that this temporary variable is initialized we decorate it
1746 -- as safe-to-reevaluate to inform to the backend that no further
1747 -- asignment will be issued and hence it can be handled as side
1748 -- effect free. Note that this decoration must be done when the
1749 -- assignment has been analyzed because otherwise it will be
1750 -- rejected (see Analyze_Assignment).
1752 Set_Is_Safe_To_Reevaluate (Id);
1754 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
1756 if Nkind (Assign) = N_Assignment_Statement then
1757 return Expression (Assign);
1759 return Original_Bound;
1763 -- Start of processing for Process_Bounds
1766 Set_Parent (R_Copy, Parent (R));
1767 Pre_Analyze_Range (R_Copy);
1768 Typ := Etype (R_Copy);
1770 -- If the type of the discrete range is Universal_Integer, then the
1771 -- bound's type must be resolved to Integer, and any object used to
1772 -- hold the bound must also have type Integer, unless the literal
1773 -- bounds are constant-folded expressions with a user-defined type.
1775 if Typ = Universal_Integer then
1776 if Nkind (Lo) = N_Integer_Literal
1777 and then Present (Etype (Lo))
1778 and then Scope (Etype (Lo)) /= Standard_Standard
1782 elsif Nkind (Hi) = N_Integer_Literal
1783 and then Present (Etype (Hi))
1784 and then Scope (Etype (Hi)) /= Standard_Standard
1789 Typ := Standard_Integer;
1795 New_Lo_Bound := One_Bound (Lo, Low_Bound (R_Copy));
1796 New_Hi_Bound := One_Bound (Hi, High_Bound (R_Copy));
1798 -- Propagate staticness to loop range itself, in case the
1799 -- corresponding subtype is static.
1801 if New_Lo_Bound /= Lo
1802 and then Is_Static_Expression (New_Lo_Bound)
1804 Rewrite (Low_Bound (R), New_Copy (New_Lo_Bound));
1807 if New_Hi_Bound /= Hi
1808 and then Is_Static_Expression (New_Hi_Bound)
1810 Rewrite (High_Bound (R), New_Copy (New_Hi_Bound));
1814 --------------------------------------
1815 -- Check_Controlled_Array_Attribute --
1816 --------------------------------------
1818 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1820 if Nkind (DS) = N_Attribute_Reference
1821 and then Is_Entity_Name (Prefix (DS))
1822 and then Ekind (Entity (Prefix (DS))) = E_Function
1823 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1826 Component_Type (Etype (Entity (Prefix (DS)))))
1827 and then Expander_Active
1830 Loc : constant Source_Ptr := Sloc (N);
1831 Arr : constant Entity_Id := Etype (Entity (Prefix (DS)));
1832 Indx : constant Entity_Id :=
1833 Base_Type (Etype (First_Index (Arr)));
1834 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
1839 Make_Subtype_Declaration (Loc,
1840 Defining_Identifier => Subt,
1841 Subtype_Indication =>
1842 Make_Subtype_Indication (Loc,
1843 Subtype_Mark => New_Reference_To (Indx, Loc),
1845 Make_Range_Constraint (Loc,
1846 Relocate_Node (DS))));
1847 Insert_Before (Parent (N), Decl);
1851 Make_Attribute_Reference (Loc,
1852 Prefix => New_Reference_To (Subt, Loc),
1853 Attribute_Name => Attribute_Name (DS)));
1857 end Check_Controlled_Array_Attribute;
1859 ------------------------------------
1860 -- Has_Call_Using_Secondary_Stack --
1861 ------------------------------------
1863 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean is
1865 function Check_Call (N : Node_Id) return Traverse_Result;
1866 -- Check if N is a function call which uses the secondary stack
1872 function Check_Call (N : Node_Id) return Traverse_Result is
1875 Return_Typ : Entity_Id;
1878 if Nkind (N) = N_Function_Call then
1881 -- Call using access to subprogram with explicit dereference
1883 if Nkind (Nam) = N_Explicit_Dereference then
1884 Subp := Etype (Nam);
1889 Subp := Entity (Nam);
1892 Return_Typ := Etype (Subp);
1894 if Is_Composite_Type (Return_Typ)
1895 and then not Is_Constrained (Return_Typ)
1899 elsif Sec_Stack_Needed_For_Return (Subp) then
1904 -- Continue traversing the tree
1909 function Check_Calls is new Traverse_Func (Check_Call);
1911 -- Start of processing for Has_Call_Using_Secondary_Stack
1914 return Check_Calls (N) = Abandon;
1915 end Has_Call_Using_Secondary_Stack;
1917 -- Start of processing for Analyze_Iteration_Scheme
1920 -- If this is a rewritten quantified expression, the iteration scheme
1921 -- has been analyzed already. Do no repeat analysis because the loop
1922 -- variable is already declared.
1924 if Analyzed (N) then
1928 -- For an infinite loop, there is no iteration scheme
1934 -- Iteration scheme is present
1937 Cond : constant Node_Id := Condition (N);
1940 -- For WHILE loop, verify that the condition is a Boolean expression
1941 -- and resolve and check it.
1943 if Present (Cond) then
1944 Analyze_And_Resolve (Cond, Any_Boolean);
1945 Check_Unset_Reference (Cond);
1946 Set_Current_Value_Condition (N);
1949 -- For an iterator specification with "of", pre-analyze range to
1950 -- capture function calls that may require finalization actions.
1952 elsif Present (Iterator_Specification (N)) then
1953 Pre_Analyze_Range (Name (Iterator_Specification (N)));
1954 Analyze_Iterator_Specification (Iterator_Specification (N));
1956 -- Else we have a FOR loop
1960 LP : constant Node_Id := Loop_Parameter_Specification (N);
1961 Id : constant Entity_Id := Defining_Identifier (LP);
1962 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
1969 -- We always consider the loop variable to be referenced, since
1970 -- the loop may be used just for counting purposes.
1972 Generate_Reference (Id, N, ' ');
1974 -- Check for the case of loop variable hiding a local variable
1975 -- (used later on to give a nice warning if the hidden variable
1976 -- is never assigned).
1979 H : constant Entity_Id := Homonym (Id);
1982 and then Enclosing_Dynamic_Scope (H) =
1983 Enclosing_Dynamic_Scope (Id)
1984 and then Ekind (H) = E_Variable
1985 and then Is_Discrete_Type (Etype (H))
1987 Set_Hiding_Loop_Variable (H, Id);
1991 -- Loop parameter specification must include subtype mark in
1994 if Nkind (DS) = N_Range then
1995 Check_SPARK_Restriction
1996 ("loop parameter specification must include subtype mark",
2000 -- Now analyze the subtype definition. If it is a range, create
2001 -- temporaries for bounds.
2003 if Nkind (DS) = N_Range
2004 and then Expander_Active
2006 Process_Bounds (DS);
2008 -- expander not active or else range of iteration is a subtype
2009 -- indication, an entity, or a function call that yields an
2010 -- aggregate or a container.
2013 D_Copy := New_Copy_Tree (DS);
2014 Set_Parent (D_Copy, Parent (DS));
2015 Pre_Analyze_Range (D_Copy);
2017 -- Ada2012: If the domain of iteration is a function call,
2018 -- it is the new iterator form.
2020 -- We have also implemented the shorter form : for X in S
2021 -- for Alfa use. In this case, 'Old and 'Result must be
2022 -- treated as entity names over which iterators are legal.
2024 if Nkind (D_Copy) = N_Function_Call
2027 and then (Nkind (D_Copy) = N_Attribute_Reference
2029 (Attribute_Name (D_Copy) = Name_Result
2030 or else Attribute_Name (D_Copy) = Name_Old)))
2032 (Is_Entity_Name (D_Copy)
2033 and then not Is_Type (Entity (D_Copy)))
2035 -- This is an iterator specification. Rewrite as such
2036 -- and analyze, to capture function calls that may
2037 -- require finalization actions.
2040 I_Spec : constant Node_Id :=
2041 Make_Iterator_Specification (Sloc (LP),
2042 Defining_Identifier =>
2045 Subtype_Indication => Empty,
2047 Reverse_Present (LP));
2049 Set_Iterator_Specification (N, I_Spec);
2050 Set_Loop_Parameter_Specification (N, Empty);
2051 Analyze_Iterator_Specification (I_Spec);
2053 -- In a generic context, analyze the original domain
2054 -- of iteration, for name capture.
2056 if not Expander_Active then
2060 -- Set kind of loop parameter, which may be used in
2061 -- the subsequent analysis of the condition in a
2062 -- quantified expression.
2064 Set_Ekind (Id, E_Loop_Parameter);
2068 -- Domain of iteration is not a function call, and is
2069 -- side-effect free.
2080 -- Some additional checks if we are iterating through a type
2082 if Is_Entity_Name (DS)
2083 and then Present (Entity (DS))
2084 and then Is_Type (Entity (DS))
2086 -- The subtype indication may denote the completion of an
2087 -- incomplete type declaration.
2089 if Ekind (Entity (DS)) = E_Incomplete_Type then
2090 Set_Entity (DS, Get_Full_View (Entity (DS)));
2091 Set_Etype (DS, Entity (DS));
2094 -- Attempt to iterate through non-static predicate
2096 if Is_Discrete_Type (Entity (DS))
2097 and then Present (Predicate_Function (Entity (DS)))
2098 and then No (Static_Predicate (Entity (DS)))
2100 Bad_Predicated_Subtype_Use
2101 ("cannot use subtype& with non-static "
2102 & "predicate for loop iteration", DS, Entity (DS));
2106 -- Error if not discrete type
2108 if not Is_Discrete_Type (Etype (DS)) then
2109 Wrong_Type (DS, Any_Discrete);
2110 Set_Etype (DS, Any_Type);
2113 Check_Controlled_Array_Attribute (DS);
2115 Make_Index (DS, LP, In_Iter_Schm => True);
2117 Set_Ekind (Id, E_Loop_Parameter);
2119 -- If the loop is part of a predicate or precondition, it may
2120 -- be analyzed twice, once in the source and once on the copy
2121 -- used to check conformance. Preserve the original itype
2122 -- because the second one may be created in a different scope,
2123 -- e.g. a precondition procedure, leading to a crash in GIGI.
2125 if No (Etype (Id)) or else Etype (Id) = Any_Type then
2126 Set_Etype (Id, Etype (DS));
2129 -- Treat a range as an implicit reference to the type, to
2130 -- inhibit spurious warnings.
2132 Generate_Reference (Base_Type (Etype (DS)), N, ' ');
2133 Set_Is_Known_Valid (Id, True);
2135 -- The loop is not a declarative part, so the only entity
2136 -- declared "within" must be frozen explicitly.
2139 Flist : constant List_Id := Freeze_Entity (Id, N);
2141 if Is_Non_Empty_List (Flist) then
2142 Insert_Actions (N, Flist);
2146 -- Check for null or possibly null range and issue warning. We
2147 -- suppress such messages in generic templates and instances,
2148 -- because in practice they tend to be dubious in these cases.
2150 if Nkind (DS) = N_Range and then Comes_From_Source (N) then
2152 L : constant Node_Id := Low_Bound (DS);
2153 H : constant Node_Id := High_Bound (DS);
2156 -- If range of loop is null, issue warning
2158 if Compile_Time_Compare
2159 (L, H, Assume_Valid => True) = GT
2161 -- Suppress the warning if inside a generic template
2162 -- or instance, since in practice they tend to be
2163 -- dubious in these cases since they can result from
2164 -- intended parametrization.
2166 if not Inside_A_Generic
2167 and then not In_Instance
2169 -- Specialize msg if invalid values could make the
2170 -- loop non-null after all.
2172 if Compile_Time_Compare
2173 (L, H, Assume_Valid => False) = GT
2176 ("?loop range is null, loop will not execute",
2179 -- Since we know the range of the loop is null,
2180 -- set the appropriate flag to remove the loop
2181 -- entirely during expansion.
2183 Set_Is_Null_Loop (Parent (N));
2185 -- Here is where the loop could execute because
2186 -- of invalid values, so issue appropriate
2187 -- message and in this case we do not set the
2188 -- Is_Null_Loop flag since the loop may execute.
2192 ("?loop range may be null, "
2193 & "loop may not execute",
2196 ("?can only execute if invalid values "
2202 -- In either case, suppress warnings in the body of
2203 -- the loop, since it is likely that these warnings
2204 -- will be inappropriate if the loop never actually
2205 -- executes, which is likely.
2207 Set_Suppress_Loop_Warnings (Parent (N));
2209 -- The other case for a warning is a reverse loop
2210 -- where the upper bound is the integer literal zero
2211 -- or one, and the lower bound can be positive.
2213 -- For example, we have
2215 -- for J in reverse N .. 1 loop
2217 -- In practice, this is very likely to be a case of
2218 -- reversing the bounds incorrectly in the range.
2220 elsif Reverse_Present (LP)
2221 and then Nkind (Original_Node (H)) =
2223 and then (Intval (Original_Node (H)) = Uint_0
2225 Intval (Original_Node (H)) = Uint_1)
2227 Error_Msg_N ("?loop range may be null", DS);
2228 Error_Msg_N ("\?bounds may be wrong way round", DS);
2235 end Analyze_Iteration_Scheme;
2237 -------------------------------------
2238 -- Analyze_Iterator_Specification --
2239 -------------------------------------
2241 procedure Analyze_Iterator_Specification (N : Node_Id) is
2242 Loc : constant Source_Ptr := Sloc (N);
2243 Def_Id : constant Node_Id := Defining_Identifier (N);
2244 Subt : constant Node_Id := Subtype_Indication (N);
2245 Iter_Name : constant Node_Id := Name (N);
2251 -- In semantics mode, introduce loop variable so that loop body can be
2252 -- properly analyzed. Otherwise this is one after expansion.
2254 if Operating_Mode = Check_Semantics then
2255 Enter_Name (Def_Id);
2258 Set_Ekind (Def_Id, E_Variable);
2260 if Present (Subt) then
2264 -- If domain of iteration is an expression, create a declaration for it,
2265 -- so that finalization actions are introduced outside of the loop.
2266 -- The declaration must be a renaming because the body of the loop may
2267 -- assign to elements.
2269 if not Is_Entity_Name (Iter_Name) then
2271 Id : constant Entity_Id := Make_Temporary (Loc, 'R', Iter_Name);
2275 Typ := Etype (Iter_Name);
2278 Make_Object_Renaming_Declaration (Loc,
2279 Defining_Identifier => Id,
2280 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
2281 Name => Relocate_Node (Iter_Name));
2283 Insert_Actions (Parent (Parent (N)), New_List (Decl));
2284 Rewrite (Name (N), New_Occurrence_Of (Id, Loc));
2285 Set_Etype (Id, Typ);
2286 Set_Etype (Name (N), Typ);
2289 -- Container is an entity or an array with uncontrolled components, or
2290 -- else it is a container iterator given by a function call, typically
2291 -- called Iterate in the case of predefined containers, even though
2292 -- Iterate is not a reserved name. What matter is that the return type
2293 -- of the function is an iterator type.
2296 Analyze (Iter_Name);
2298 if Nkind (Iter_Name) = N_Function_Call then
2300 C : constant Node_Id := Name (Iter_Name);
2305 if not Is_Overloaded (Iter_Name) then
2306 Resolve (Iter_Name, Etype (C));
2309 Get_First_Interp (C, I, It);
2310 while It.Typ /= Empty loop
2311 if Reverse_Present (N) then
2312 if Is_Reversible_Iterator (It.Typ) then
2313 Resolve (Iter_Name, It.Typ);
2317 elsif Is_Iterator (It.Typ) then
2318 Resolve (Iter_Name, It.Typ);
2322 Get_Next_Interp (I, It);
2327 -- Domain of iteration is not overloaded
2330 Resolve (Iter_Name, Etype (Iter_Name));
2334 Typ := Etype (Iter_Name);
2336 if Is_Array_Type (Typ) then
2337 if Of_Present (N) then
2338 Set_Etype (Def_Id, Component_Type (Typ));
2341 ("to iterate over the elements of an array, use OF", N);
2343 -- Prevent cascaded errors
2345 Set_Ekind (Def_Id, E_Constant);
2346 Set_Etype (Def_Id, Etype (First_Index (Typ)));
2349 -- Check for type error in iterator
2351 elsif Typ = Any_Type then
2354 -- Iteration over a container
2357 Set_Ekind (Def_Id, E_Loop_Parameter);
2359 if Of_Present (N) then
2361 -- The type of the loop variable is the Iterator_Element aspect of
2362 -- the container type.
2365 Entity (Find_Aspect (Typ, Aspect_Iterator_Element)));
2368 -- The result type of Iterate function is the classwide type of
2369 -- the interface parent. We need the specific Cursor type defined
2370 -- in the container package.
2372 Ent := First_Entity (Scope (Typ));
2373 while Present (Ent) loop
2374 if Chars (Ent) = Name_Cursor then
2375 Set_Etype (Def_Id, Etype (Ent));
2383 end Analyze_Iterator_Specification;
2389 -- Note: the semantic work required for analyzing labels (setting them as
2390 -- reachable) was done in a prepass through the statements in the block,
2391 -- so that forward gotos would be properly handled. See Analyze_Statements
2392 -- for further details. The only processing required here is to deal with
2393 -- optimizations that depend on an assumption of sequential control flow,
2394 -- since of course the occurrence of a label breaks this assumption.
2396 procedure Analyze_Label (N : Node_Id) is
2397 pragma Warnings (Off, N);
2399 Kill_Current_Values;
2402 --------------------------
2403 -- Analyze_Label_Entity --
2404 --------------------------
2406 procedure Analyze_Label_Entity (E : Entity_Id) is
2408 Set_Ekind (E, E_Label);
2409 Set_Etype (E, Standard_Void_Type);
2410 Set_Enclosing_Scope (E, Current_Scope);
2411 Set_Reachable (E, True);
2412 end Analyze_Label_Entity;
2414 ----------------------------
2415 -- Analyze_Loop_Statement --
2416 ----------------------------
2418 procedure Analyze_Loop_Statement (N : Node_Id) is
2419 Loop_Statement : constant Node_Id := N;
2421 Id : constant Node_Id := Identifier (Loop_Statement);
2422 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
2426 if Present (Id) then
2428 -- Make name visible, e.g. for use in exit statements. Loop labels
2429 -- are always considered to be referenced.
2434 -- Guard against serious error (typically, a scope mismatch when
2435 -- semantic analysis is requested) by creating loop entity to
2436 -- continue analysis.
2439 if Total_Errors_Detected /= 0 then
2442 (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
2444 raise Program_Error;
2448 Generate_Reference (Ent, Loop_Statement, ' ');
2449 Generate_Definition (Ent);
2451 -- If we found a label, mark its type. If not, ignore it, since it
2452 -- means we have a conflicting declaration, which would already
2453 -- have been diagnosed at declaration time. Set Label_Construct
2454 -- of the implicit label declaration, which is not created by the
2455 -- parser for generic units.
2457 if Ekind (Ent) = E_Label then
2458 Set_Ekind (Ent, E_Loop);
2460 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
2461 Set_Label_Construct (Parent (Ent), Loop_Statement);
2466 -- Case of no identifier present
2471 (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
2472 Set_Etype (Ent, Standard_Void_Type);
2473 Set_Parent (Ent, Loop_Statement);
2476 -- Kill current values on entry to loop, since statements in the body of
2477 -- the loop may have been executed before the loop is entered. Similarly
2478 -- we kill values after the loop, since we do not know that the body of
2479 -- the loop was executed.
2481 Kill_Current_Values;
2483 Analyze_Iteration_Scheme (Iter);
2485 -- Analyze the statements of the body except in the case of an Ada 2012
2486 -- iterator with the expander active. In this case the expander will do
2487 -- a rewrite of the loop into a while loop. We will then analyze the
2488 -- loop body when we analyze this while loop.
2490 -- We need to do this delay because if the container is for indefinite
2491 -- types the actual subtype of the components will only be determined
2492 -- when the cursor declaration is analyzed.
2494 -- If the expander is not active, then we want to analyze the loop body
2495 -- now even in the Ada 2012 iterator case, since the rewriting will not
2496 -- be done. Insert the loop variable in the current scope, if not done
2497 -- when analysing the iteration scheme.
2500 or else No (Iterator_Specification (Iter))
2501 or else not Expander_Active
2504 and then Present (Iterator_Specification (Iter))
2507 Id : constant Entity_Id :=
2508 Defining_Identifier (Iterator_Specification (Iter));
2510 if Scope (Id) /= Current_Scope then
2516 Analyze_Statements (Statements (Loop_Statement));
2519 -- Finish up processing for the loop. We kill all current values, since
2520 -- in general we don't know if the statements in the loop have been
2521 -- executed. We could do a bit better than this with a loop that we
2522 -- know will execute at least once, but it's not worth the trouble and
2523 -- the front end is not in the business of flow tracing.
2525 Process_End_Label (Loop_Statement, 'e', Ent);
2527 Kill_Current_Values;
2529 -- Check for infinite loop. Skip check for generated code, since it
2530 -- justs waste time and makes debugging the routine called harder.
2532 -- Note that we have to wait till the body of the loop is fully analyzed
2533 -- before making this call, since Check_Infinite_Loop_Warning relies on
2534 -- being able to use semantic visibility information to find references.
2536 if Comes_From_Source (N) then
2537 Check_Infinite_Loop_Warning (N);
2540 -- Code after loop is unreachable if the loop has no WHILE or FOR and
2541 -- contains no EXIT statements within the body of the loop.
2543 if No (Iter) and then not Has_Exit (Ent) then
2544 Check_Unreachable_Code (N);
2546 end Analyze_Loop_Statement;
2548 ----------------------------
2549 -- Analyze_Null_Statement --
2550 ----------------------------
2552 -- Note: the semantics of the null statement is implemented by a single
2553 -- null statement, too bad everything isn't as simple as this!
2555 procedure Analyze_Null_Statement (N : Node_Id) is
2556 pragma Warnings (Off, N);
2559 end Analyze_Null_Statement;
2561 ------------------------
2562 -- Analyze_Statements --
2563 ------------------------
2565 procedure Analyze_Statements (L : List_Id) is
2570 -- The labels declared in the statement list are reachable from
2571 -- statements in the list. We do this as a prepass so that any goto
2572 -- statement will be properly flagged if its target is not reachable.
2573 -- This is not required, but is nice behavior!
2576 while Present (S) loop
2577 if Nkind (S) = N_Label then
2578 Analyze (Identifier (S));
2579 Lab := Entity (Identifier (S));
2581 -- If we found a label mark it as reachable
2583 if Ekind (Lab) = E_Label then
2584 Generate_Definition (Lab);
2585 Set_Reachable (Lab);
2587 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
2588 Set_Label_Construct (Parent (Lab), S);
2591 -- If we failed to find a label, it means the implicit declaration
2592 -- of the label was hidden. A for-loop parameter can do this to
2593 -- a label with the same name inside the loop, since the implicit
2594 -- label declaration is in the innermost enclosing body or block
2598 Error_Msg_Sloc := Sloc (Lab);
2600 ("implicit label declaration for & is hidden#",
2608 -- Perform semantic analysis on all statements
2610 Conditional_Statements_Begin;
2613 while Present (S) loop
2618 Conditional_Statements_End;
2620 -- Make labels unreachable. Visibility is not sufficient, because labels
2621 -- in one if-branch for example are not reachable from the other branch,
2622 -- even though their declarations are in the enclosing declarative part.
2625 while Present (S) loop
2626 if Nkind (S) = N_Label then
2627 Set_Reachable (Entity (Identifier (S)), False);
2632 end Analyze_Statements;
2634 ----------------------------
2635 -- Check_Unreachable_Code --
2636 ----------------------------
2638 procedure Check_Unreachable_Code (N : Node_Id) is
2639 Error_Node : Node_Id;
2643 if Is_List_Member (N)
2644 and then Comes_From_Source (N)
2650 Nxt := Original_Node (Next (N));
2652 -- If a label follows us, then we never have dead code, since
2653 -- someone could branch to the label, so we just ignore it, unless
2654 -- we are in formal mode where goto statements are not allowed.
2656 if Nkind (Nxt) = N_Label
2657 and then not Restriction_Check_Required (SPARK)
2661 -- Otherwise see if we have a real statement following us
2664 and then Comes_From_Source (Nxt)
2665 and then Is_Statement (Nxt)
2667 -- Special very annoying exception. If we have a return that
2668 -- follows a raise, then we allow it without a warning, since
2669 -- the Ada RM annoyingly requires a useless return here!
2671 if Nkind (Original_Node (N)) /= N_Raise_Statement
2672 or else Nkind (Nxt) /= N_Simple_Return_Statement
2674 -- The rather strange shenanigans with the warning message
2675 -- here reflects the fact that Kill_Dead_Code is very good
2676 -- at removing warnings in deleted code, and this is one
2677 -- warning we would prefer NOT to have removed.
2681 -- If we have unreachable code, analyze and remove the
2682 -- unreachable code, since it is useless and we don't
2683 -- want to generate junk warnings.
2685 -- We skip this step if we are not in code generation mode.
2686 -- This is the one case where we remove dead code in the
2687 -- semantics as opposed to the expander, and we do not want
2688 -- to remove code if we are not in code generation mode,
2689 -- since this messes up the ASIS trees.
2691 -- Note that one might react by moving the whole circuit to
2692 -- exp_ch5, but then we lose the warning in -gnatc mode.
2694 if Operating_Mode = Generate_Code then
2698 -- Quit deleting when we have nothing more to delete
2699 -- or if we hit a label (since someone could transfer
2700 -- control to a label, so we should not delete it).
2702 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
2704 -- Statement/declaration is to be deleted
2708 Kill_Dead_Code (Nxt);
2712 -- Now issue the warning (or error in formal mode)
2714 if Restriction_Check_Required (SPARK) then
2715 Check_SPARK_Restriction
2716 ("unreachable code is not allowed", Error_Node);
2718 Error_Msg ("?unreachable code!", Sloc (Error_Node));
2722 -- If the unconditional transfer of control instruction is the
2723 -- last statement of a sequence, then see if our parent is one of
2724 -- the constructs for which we count unblocked exits, and if so,
2725 -- adjust the count.
2730 -- Statements in THEN part or ELSE part of IF statement
2732 if Nkind (P) = N_If_Statement then
2735 -- Statements in ELSIF part of an IF statement
2737 elsif Nkind (P) = N_Elsif_Part then
2739 pragma Assert (Nkind (P) = N_If_Statement);
2741 -- Statements in CASE statement alternative
2743 elsif Nkind (P) = N_Case_Statement_Alternative then
2745 pragma Assert (Nkind (P) = N_Case_Statement);
2747 -- Statements in body of block
2749 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
2750 and then Nkind (Parent (P)) = N_Block_Statement
2754 -- Statements in exception handler in a block
2756 elsif Nkind (P) = N_Exception_Handler
2757 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
2758 and then Nkind (Parent (Parent (P))) = N_Block_Statement
2762 -- None of these cases, so return
2768 -- This was one of the cases we are looking for (i.e. the
2769 -- parent construct was IF, CASE or block) so decrement count.
2771 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
2775 end Check_Unreachable_Code;