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 Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Expander; use Expander;
31 with Exp_Util; use Exp_Util;
32 with Freeze; use Freeze;
34 with Lib.Xref; use Lib.Xref;
35 with Namet; use Namet;
36 with Nlists; use Nlists;
37 with Nmake; use Nmake;
39 with Restrict; use Restrict;
40 with Rident; use Rident;
41 with Rtsfind; use Rtsfind;
43 with Sem_Aux; use Sem_Aux;
44 with Sem_Case; use Sem_Case;
45 with Sem_Ch3; use Sem_Ch3;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Disp; use Sem_Disp;
48 with Sem_Elab; use Sem_Elab;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Sem_Warn; use Sem_Warn;
54 with Snames; use Snames;
55 with Stand; use Stand;
56 with Sinfo; use Sinfo;
57 with Targparm; use Targparm;
58 with Tbuild; use Tbuild;
59 with Uintp; use Uintp;
61 package body Sem_Ch5 is
63 Unblocked_Exit_Count : Nat := 0;
64 -- This variable is used when processing if statements, case statements,
65 -- and block statements. It counts the number of exit points that are not
66 -- blocked by unconditional transfer instructions: for IF and CASE, these
67 -- are the branches of the conditional; for a block, they are the statement
68 -- sequence of the block, and the statement sequences of any exception
69 -- handlers that are part of the block. When processing is complete, if
70 -- this count is zero, it means that control cannot fall through the IF,
71 -- CASE or block statement. This is used for the generation of warning
72 -- messages. This variable is recursively saved on entry to processing the
73 -- construct, and restored on exit.
75 ------------------------
76 -- Analyze_Assignment --
77 ------------------------
79 procedure Analyze_Assignment (N : Node_Id) is
80 Lhs : constant Node_Id := Name (N);
81 Rhs : constant Node_Id := Expression (N);
86 procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
87 -- N is the node for the left hand side of an assignment, and it is not
88 -- a variable. This routine issues an appropriate diagnostic.
91 -- This is called to kill current value settings of a simple variable
92 -- on the left hand side. We call it if we find any error in analyzing
93 -- the assignment, and at the end of processing before setting any new
94 -- current values in place.
96 procedure Set_Assignment_Type
98 Opnd_Type : in out Entity_Id);
99 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
100 -- nominal subtype. This procedure is used to deal with cases where the
101 -- nominal subtype must be replaced by the actual subtype.
103 -------------------------------
104 -- Diagnose_Non_Variable_Lhs --
105 -------------------------------
107 procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
109 -- Not worth posting another error if left hand side already flagged
110 -- as being illegal in some respect.
112 if Error_Posted (N) then
115 -- Some special bad cases of entity names
117 elsif Is_Entity_Name (N) then
119 Ent : constant Entity_Id := Entity (N);
122 if Ekind (Ent) = E_In_Parameter then
124 ("assignment to IN mode parameter not allowed", N);
126 -- Renamings of protected private components are turned into
127 -- constants when compiling a protected function. In the case
128 -- of single protected types, the private component appears
131 elsif (Is_Prival (Ent)
133 (Ekind (Current_Scope) = E_Function
134 or else Ekind (Enclosing_Dynamic_Scope
135 (Current_Scope)) = E_Function))
137 (Ekind (Ent) = E_Component
138 and then Is_Protected_Type (Scope (Ent)))
141 ("protected function cannot modify protected object", N);
143 elsif Ekind (Ent) = E_Loop_Parameter then
145 ("assignment to loop parameter not allowed", N);
149 ("left hand side of assignment must be a variable", N);
153 -- For indexed components or selected components, test prefix
155 elsif Nkind (N) = N_Indexed_Component then
156 Diagnose_Non_Variable_Lhs (Prefix (N));
158 -- Another special case for assignment to discriminant
160 elsif Nkind (N) = N_Selected_Component then
161 if Present (Entity (Selector_Name (N)))
162 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
165 ("assignment to discriminant not allowed", N);
167 Diagnose_Non_Variable_Lhs (Prefix (N));
171 -- If we fall through, we have no special message to issue!
173 Error_Msg_N ("left hand side of assignment must be a variable", N);
175 end Diagnose_Non_Variable_Lhs;
181 procedure Kill_Lhs is
183 if Is_Entity_Name (Lhs) then
185 Ent : constant Entity_Id := Entity (Lhs);
187 if Present (Ent) then
188 Kill_Current_Values (Ent);
194 -------------------------
195 -- Set_Assignment_Type --
196 -------------------------
198 procedure Set_Assignment_Type
200 Opnd_Type : in out Entity_Id)
203 Require_Entity (Opnd);
205 -- If the assignment operand is an in-out or out parameter, then we
206 -- get the actual subtype (needed for the unconstrained case). If the
207 -- operand is the actual in an entry declaration, then within the
208 -- accept statement it is replaced with a local renaming, which may
209 -- also have an actual subtype.
211 if Is_Entity_Name (Opnd)
212 and then (Ekind (Entity (Opnd)) = E_Out_Parameter
213 or else Ekind (Entity (Opnd)) =
215 or else Ekind (Entity (Opnd)) =
216 E_Generic_In_Out_Parameter
218 (Ekind (Entity (Opnd)) = E_Variable
219 and then Nkind (Parent (Entity (Opnd))) =
220 N_Object_Renaming_Declaration
221 and then Nkind (Parent (Parent (Entity (Opnd)))) =
224 Opnd_Type := Get_Actual_Subtype (Opnd);
226 -- If assignment operand is a component reference, then we get the
227 -- actual subtype of the component for the unconstrained case.
229 elsif Nkind_In (Opnd, N_Selected_Component, N_Explicit_Dereference)
230 and then not Is_Unchecked_Union (Opnd_Type)
232 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
234 if Present (Decl) then
235 Insert_Action (N, Decl);
236 Mark_Rewrite_Insertion (Decl);
238 Opnd_Type := Defining_Identifier (Decl);
239 Set_Etype (Opnd, Opnd_Type);
240 Freeze_Itype (Opnd_Type, N);
242 elsif Is_Constrained (Etype (Opnd)) then
243 Opnd_Type := Etype (Opnd);
246 -- For slice, use the constrained subtype created for the slice
248 elsif Nkind (Opnd) = N_Slice then
249 Opnd_Type := Etype (Opnd);
251 end Set_Assignment_Type;
253 -- Start of processing for Analyze_Assignment
256 Mark_Coextensions (N, Rhs);
261 -- Ensure that we never do an assignment on a variable marked as
262 -- as Safe_To_Reevaluate.
264 pragma Assert (not Is_Entity_Name (Lhs)
265 or else Ekind (Entity (Lhs)) /= E_Variable
266 or else not Is_Safe_To_Reevaluate (Entity (Lhs)));
268 -- Start type analysis for assignment
272 -- In the most general case, both Lhs and Rhs can be overloaded, and we
273 -- must compute the intersection of the possible types on each side.
275 if Is_Overloaded (Lhs) then
282 Get_First_Interp (Lhs, I, It);
284 while Present (It.Typ) loop
285 if Has_Compatible_Type (Rhs, It.Typ) then
286 if T1 /= Any_Type then
288 -- An explicit dereference is overloaded if the prefix
289 -- is. Try to remove the ambiguity on the prefix, the
290 -- error will be posted there if the ambiguity is real.
292 if Nkind (Lhs) = N_Explicit_Dereference then
295 PI1 : Interp_Index := 0;
301 Get_First_Interp (Prefix (Lhs), PI, PIt);
303 while Present (PIt.Typ) loop
304 if Is_Access_Type (PIt.Typ)
305 and then Has_Compatible_Type
306 (Rhs, Designated_Type (PIt.Typ))
310 Disambiguate (Prefix (Lhs),
313 if PIt = No_Interp then
315 ("ambiguous left-hand side"
316 & " in assignment", Lhs);
319 Resolve (Prefix (Lhs), PIt.Typ);
329 Get_Next_Interp (PI, PIt);
335 ("ambiguous left-hand side in assignment", Lhs);
343 Get_Next_Interp (I, It);
347 if T1 = Any_Type then
349 ("no valid types for left-hand side for assignment", Lhs);
355 -- The resulting assignment type is T1, so now we will resolve the left
356 -- hand side of the assignment using this determined type.
360 -- Cases where Lhs is not a variable
362 if not Is_Variable (Lhs) then
364 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
372 if Ada_Version >= Ada_2005 then
374 -- Handle chains of renamings
377 while Nkind (Ent) in N_Has_Entity
378 and then Present (Entity (Ent))
379 and then Present (Renamed_Object (Entity (Ent)))
381 Ent := Renamed_Object (Entity (Ent));
384 if (Nkind (Ent) = N_Attribute_Reference
385 and then Attribute_Name (Ent) = Name_Priority)
387 -- Renamings of the attribute Priority applied to protected
388 -- objects have been previously expanded into calls to the
389 -- Get_Ceiling run-time subprogram.
392 (Nkind (Ent) = N_Function_Call
393 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
395 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
397 -- The enclosing subprogram cannot be a protected function
400 while not (Is_Subprogram (S)
401 and then Convention (S) = Convention_Protected)
402 and then S /= Standard_Standard
407 if Ekind (S) = E_Function
408 and then Convention (S) = Convention_Protected
411 ("protected function cannot modify protected object",
415 -- Changes of the ceiling priority of the protected object
416 -- are only effective if the Ceiling_Locking policy is in
417 -- effect (AARM D.5.2 (5/2)).
419 if Locking_Policy /= 'C' then
420 Error_Msg_N ("assignment to the attribute PRIORITY has " &
422 Error_Msg_N ("\since no Locking_Policy has been " &
431 Diagnose_Non_Variable_Lhs (Lhs);
434 -- Error of assigning to limited type. We do however allow this in
435 -- certain cases where the front end generates the assignments.
437 elsif Is_Limited_Type (T1)
438 and then not Assignment_OK (Lhs)
439 and then not Assignment_OK (Original_Node (Lhs))
440 and then not Is_Value_Type (T1)
442 -- CPP constructors can only be called in declarations
444 if Is_CPP_Constructor_Call (Rhs) then
445 Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
448 ("left hand of assignment must not be limited type", Lhs);
449 Explain_Limited_Type (T1, Lhs);
453 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
454 -- abstract. This is only checked when the assignment Comes_From_Source,
455 -- because in some cases the expander generates such assignments (such
456 -- in the _assign operation for an abstract type).
458 elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
460 ("target of assignment operation must not be abstract", Lhs);
463 -- Resolution may have updated the subtype, in case the left-hand side
464 -- is a private protected component. Use the correct subtype to avoid
465 -- scoping issues in the back-end.
469 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
470 -- type. For example:
474 -- type Acc is access P.T;
477 -- with Pkg; use Acc;
478 -- procedure Example is
481 -- A.all := B.all; -- ERROR
484 if Nkind (Lhs) = N_Explicit_Dereference
485 and then Ekind (T1) = E_Incomplete_Type
487 Error_Msg_N ("invalid use of incomplete type", Lhs);
492 -- Now we can complete the resolution of the right hand side
494 Set_Assignment_Type (Lhs, T1);
497 -- This is the point at which we check for an unset reference
499 Check_Unset_Reference (Rhs);
500 Check_Unprotected_Access (Lhs, Rhs);
502 -- Remaining steps are skipped if Rhs was syntactically in error
511 if not Covers (T1, T2) then
512 Wrong_Type (Rhs, Etype (Lhs));
517 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
518 -- types, use the non-limited view if available
520 if Nkind (Rhs) = N_Explicit_Dereference
521 and then Ekind (T2) = E_Incomplete_Type
522 and then Is_Tagged_Type (T2)
523 and then Present (Non_Limited_View (T2))
525 T2 := Non_Limited_View (T2);
528 Set_Assignment_Type (Rhs, T2);
530 if Total_Errors_Detected /= 0 then
540 if T1 = Any_Type or else T2 = Any_Type then
545 -- If the rhs is class-wide or dynamically tagged, then require the lhs
546 -- to be class-wide. The case where the rhs is a dynamically tagged call
547 -- to a dispatching operation with a controlling access result is
548 -- excluded from this check, since the target has an access type (and
549 -- no tag propagation occurs in that case).
551 if (Is_Class_Wide_Type (T2)
552 or else (Is_Dynamically_Tagged (Rhs)
553 and then not Is_Access_Type (T1)))
554 and then not Is_Class_Wide_Type (T1)
556 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
558 elsif Is_Class_Wide_Type (T1)
559 and then not Is_Class_Wide_Type (T2)
560 and then not Is_Tag_Indeterminate (Rhs)
561 and then not Is_Dynamically_Tagged (Rhs)
563 Error_Msg_N ("dynamically tagged expression required!", Rhs);
566 -- Propagate the tag from a class-wide target to the rhs when the rhs
567 -- is a tag-indeterminate call.
569 if Is_Tag_Indeterminate (Rhs) then
570 if Is_Class_Wide_Type (T1) then
571 Propagate_Tag (Lhs, Rhs);
573 elsif Nkind (Rhs) = N_Function_Call
574 and then Is_Entity_Name (Name (Rhs))
575 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
578 ("call to abstract function must be dispatching", Name (Rhs));
580 elsif Nkind (Rhs) = N_Qualified_Expression
581 and then Nkind (Expression (Rhs)) = N_Function_Call
582 and then Is_Entity_Name (Name (Expression (Rhs)))
584 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
587 ("call to abstract function must be dispatching",
588 Name (Expression (Rhs)));
592 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
593 -- apply an implicit conversion of the rhs to that type to force
594 -- appropriate static and run-time accessibility checks. This applies
595 -- as well to anonymous access-to-subprogram types that are component
596 -- subtypes or formal parameters.
598 if Ada_Version >= Ada_2005
599 and then Is_Access_Type (T1)
601 if Is_Local_Anonymous_Access (T1)
602 or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
604 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
605 Analyze_And_Resolve (Rhs, T1);
609 -- Ada 2005 (AI-231): Assignment to not null variable
611 if Ada_Version >= Ada_2005
612 and then Can_Never_Be_Null (T1)
613 and then not Assignment_OK (Lhs)
615 -- Case where we know the right hand side is null
617 if Known_Null (Rhs) then
618 Apply_Compile_Time_Constraint_Error
620 Msg => "(Ada 2005) null not allowed in null-excluding objects?",
621 Reason => CE_Null_Not_Allowed);
623 -- We still mark this as a possible modification, that's necessary
624 -- to reset Is_True_Constant, and desirable for xref purposes.
626 Note_Possible_Modification (Lhs, Sure => True);
629 -- If we know the right hand side is non-null, then we convert to the
630 -- target type, since we don't need a run time check in that case.
632 elsif not Can_Never_Be_Null (T2) then
633 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
634 Analyze_And_Resolve (Rhs, T1);
638 if Is_Scalar_Type (T1) then
639 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
641 -- For array types, verify that lengths match. If the right hand side
642 -- is a function call that has been inlined, the assignment has been
643 -- rewritten as a block, and the constraint check will be applied to the
644 -- assignment within the block.
646 elsif Is_Array_Type (T1)
648 (Nkind (Rhs) /= N_Type_Conversion
649 or else Is_Constrained (Etype (Rhs)))
651 (Nkind (Rhs) /= N_Function_Call
652 or else Nkind (N) /= N_Block_Statement)
654 -- Assignment verifies that the length of the Lsh and Rhs are equal,
655 -- but of course the indexes do not have to match. If the right-hand
656 -- side is a type conversion to an unconstrained type, a length check
657 -- is performed on the expression itself during expansion. In rare
658 -- cases, the redundant length check is computed on an index type
659 -- with a different representation, triggering incorrect code in the
662 Apply_Length_Check (Rhs, Etype (Lhs));
665 -- Discriminant checks are applied in the course of expansion
670 -- Note: modifications of the Lhs may only be recorded after
671 -- checks have been applied.
673 Note_Possible_Modification (Lhs, Sure => True);
674 Check_Order_Dependence;
676 -- ??? a real accessibility check is needed when ???
678 -- Post warning for redundant assignment or variable to itself
680 if Warn_On_Redundant_Constructs
682 -- We only warn for source constructs
684 and then Comes_From_Source (N)
686 -- Where the object is the same on both sides
688 and then Same_Object (Lhs, Original_Node (Rhs))
690 -- But exclude the case where the right side was an operation that
691 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
692 -- don't want to warn in such a case, since it is reasonable to write
693 -- such expressions especially when K is defined symbolically in some
696 and then Nkind (Original_Node (Rhs)) not in N_Op
698 if Nkind (Lhs) in N_Has_Entity then
699 Error_Msg_NE -- CODEFIX
700 ("?useless assignment of & to itself!", N, Entity (Lhs));
702 Error_Msg_N -- CODEFIX
703 ("?useless assignment of object to itself!", N);
707 -- Check for non-allowed composite assignment
709 if not Support_Composite_Assign_On_Target
710 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
711 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
713 Error_Msg_CRT ("composite assignment", N);
716 -- Check elaboration warning for left side if not in elab code
718 if not In_Subprogram_Or_Concurrent_Unit then
719 Check_Elab_Assign (Lhs);
722 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
723 -- assignment is a source assignment in the extended main source unit.
724 -- We are not interested in any reference information outside this
725 -- context, or in compiler generated assignment statements.
727 if Comes_From_Source (N)
728 and then In_Extended_Main_Source_Unit (Lhs)
730 Set_Referenced_Modified (Lhs, Out_Param => False);
733 -- Final step. If left side is an entity, then we may be able to reset
734 -- the current tracked values to new safe values. We only have something
735 -- to do if the left side is an entity name, and expansion has not
736 -- modified the node into something other than an assignment, and of
737 -- course we only capture values if it is safe to do so.
739 if Is_Entity_Name (Lhs)
740 and then Nkind (N) = N_Assignment_Statement
743 Ent : constant Entity_Id := Entity (Lhs);
746 if Safe_To_Capture_Value (N, Ent) then
748 -- If simple variable on left side, warn if this assignment
749 -- blots out another one (rendering it useless) and note
750 -- location of assignment in case no one references value. We
751 -- only do this for source assignments, otherwise we can
752 -- generate bogus warnings when an assignment is rewritten as
753 -- another assignment, and gets tied up with itself.
755 -- Note: we don't use Record_Last_Assignment here, because we
756 -- have lots of other stuff to do under control of this test.
758 if Warn_On_Modified_Unread
759 and then Is_Assignable (Ent)
760 and then Comes_From_Source (N)
761 and then In_Extended_Main_Source_Unit (Ent)
763 Warn_On_Useless_Assignment (Ent, N);
764 Set_Last_Assignment (Ent, Lhs);
767 -- If we are assigning an access type and the left side is an
768 -- entity, then make sure that the Is_Known_[Non_]Null flags
769 -- properly reflect the state of the entity after assignment.
771 if Is_Access_Type (T1) then
772 if Known_Non_Null (Rhs) then
773 Set_Is_Known_Non_Null (Ent, True);
775 elsif Known_Null (Rhs)
776 and then not Can_Never_Be_Null (Ent)
778 Set_Is_Known_Null (Ent, True);
781 Set_Is_Known_Null (Ent, False);
783 if not Can_Never_Be_Null (Ent) then
784 Set_Is_Known_Non_Null (Ent, False);
788 -- For discrete types, we may be able to set the current value
789 -- if the value is known at compile time.
791 elsif Is_Discrete_Type (T1)
792 and then Compile_Time_Known_Value (Rhs)
794 Set_Current_Value (Ent, Rhs);
796 Set_Current_Value (Ent, Empty);
799 -- If not safe to capture values, kill them
806 end Analyze_Assignment;
808 -----------------------------
809 -- Analyze_Block_Statement --
810 -----------------------------
812 procedure Analyze_Block_Statement (N : Node_Id) is
813 Decls : constant List_Id := Declarations (N);
814 Id : constant Node_Id := Identifier (N);
815 HSS : constant Node_Id := Handled_Statement_Sequence (N);
818 -- In SPARK mode, we reject block statements. Note that the case of
819 -- block statements generated by the expander is fine.
821 if Nkind (Original_Node (N)) = N_Block_Statement then
822 Check_SPARK_Restriction ("block statement is not allowed", N);
825 -- If no handled statement sequence is present, things are really messed
826 -- up, and we just return immediately (defence against previous errors).
832 -- Normal processing with HSS present
835 EH : constant List_Id := Exception_Handlers (HSS);
836 Ent : Entity_Id := Empty;
839 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
840 -- Recursively save value of this global, will be restored on exit
843 -- Initialize unblocked exit count for statements of begin block
844 -- plus one for each exception handler that is present.
846 Unblocked_Exit_Count := 1;
849 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
852 -- If a label is present analyze it and mark it as referenced
858 -- An error defense. If we have an identifier, but no entity, then
859 -- something is wrong. If previous errors, then just remove the
860 -- identifier and continue, otherwise raise an exception.
863 if Total_Errors_Detected /= 0 then
864 Set_Identifier (N, Empty);
870 Set_Ekind (Ent, E_Block);
871 Generate_Reference (Ent, N, ' ');
872 Generate_Definition (Ent);
874 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
875 Set_Label_Construct (Parent (Ent), N);
880 -- If no entity set, create a label entity
883 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
884 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
888 Set_Etype (Ent, Standard_Void_Type);
889 Set_Block_Node (Ent, Identifier (N));
892 if Present (Decls) then
893 Analyze_Declarations (Decls);
895 Inspect_Deferred_Constant_Completion (Decls);
899 Process_End_Label (HSS, 'e', Ent);
901 -- If exception handlers are present, then we indicate that enclosing
902 -- scopes contain a block with handlers. We only need to mark non-
908 Set_Has_Nested_Block_With_Handler (S);
909 exit when Is_Overloadable (S)
910 or else Ekind (S) = E_Package
911 or else Is_Generic_Unit (S);
916 Check_References (Ent);
917 Warn_On_Useless_Assignments (Ent);
920 if Unblocked_Exit_Count = 0 then
921 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
922 Check_Unreachable_Code (N);
924 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
927 end Analyze_Block_Statement;
929 ----------------------------
930 -- Analyze_Case_Statement --
931 ----------------------------
933 procedure Analyze_Case_Statement (N : Node_Id) is
935 Exp_Type : Entity_Id;
936 Exp_Btype : Entity_Id;
939 Others_Present : Boolean;
941 pragma Warnings (Off, Last_Choice);
942 pragma Warnings (Off, Dont_Care);
943 -- Don't care about assigned values
945 Statements_Analyzed : Boolean := False;
946 -- Set True if at least some statement sequences get analyzed. If False
947 -- on exit, means we had a serious error that prevented full analysis of
948 -- the case statement, and as a result it is not a good idea to output
949 -- warning messages about unreachable code.
951 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
952 -- Recursively save value of this global, will be restored on exit
954 procedure Non_Static_Choice_Error (Choice : Node_Id);
955 -- Error routine invoked by the generic instantiation below when the
956 -- case statement has a non static choice.
958 procedure Process_Statements (Alternative : Node_Id);
959 -- Analyzes all the statements associated with a case alternative.
960 -- Needed by the generic instantiation below.
962 package Case_Choices_Processing is new
963 Generic_Choices_Processing
964 (Get_Alternatives => Alternatives,
965 Get_Choices => Discrete_Choices,
966 Process_Empty_Choice => No_OP,
967 Process_Non_Static_Choice => Non_Static_Choice_Error,
968 Process_Associated_Node => Process_Statements);
969 use Case_Choices_Processing;
970 -- Instantiation of the generic choice processing package
972 -----------------------------
973 -- Non_Static_Choice_Error --
974 -----------------------------
976 procedure Non_Static_Choice_Error (Choice : Node_Id) is
979 ("choice given in case statement is not static!", Choice);
980 end Non_Static_Choice_Error;
982 ------------------------
983 -- Process_Statements --
984 ------------------------
986 procedure Process_Statements (Alternative : Node_Id) is
987 Choices : constant List_Id := Discrete_Choices (Alternative);
991 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
992 Statements_Analyzed := True;
994 -- An interesting optimization. If the case statement expression
995 -- is a simple entity, then we can set the current value within an
996 -- alternative if the alternative has one possible value.
1000 -- when 2 | 3 => beta
1001 -- when others => gamma
1003 -- Here we know that N is initially 1 within alpha, but for beta and
1004 -- gamma, we do not know anything more about the initial value.
1006 if Is_Entity_Name (Exp) then
1007 Ent := Entity (Exp);
1009 if Ekind_In (Ent, E_Variable,
1013 if List_Length (Choices) = 1
1014 and then Nkind (First (Choices)) in N_Subexpr
1015 and then Compile_Time_Known_Value (First (Choices))
1017 Set_Current_Value (Entity (Exp), First (Choices));
1020 Analyze_Statements (Statements (Alternative));
1022 -- After analyzing the case, set the current value to empty
1023 -- since we won't know what it is for the next alternative
1024 -- (unless reset by this same circuit), or after the case.
1026 Set_Current_Value (Entity (Exp), Empty);
1031 -- Case where expression is not an entity name of a variable
1033 Analyze_Statements (Statements (Alternative));
1034 end Process_Statements;
1036 -- Start of processing for Analyze_Case_Statement
1039 Unblocked_Exit_Count := 0;
1040 Exp := Expression (N);
1043 -- The expression must be of any discrete type. In rare cases, the
1044 -- expander constructs a case statement whose expression has a private
1045 -- type whose full view is discrete. This can happen when generating
1046 -- a stream operation for a variant type after the type is frozen,
1047 -- when the partial of view of the type of the discriminant is private.
1048 -- In that case, use the full view to analyze case alternatives.
1050 if not Is_Overloaded (Exp)
1051 and then not Comes_From_Source (N)
1052 and then Is_Private_Type (Etype (Exp))
1053 and then Present (Full_View (Etype (Exp)))
1054 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1056 Resolve (Exp, Etype (Exp));
1057 Exp_Type := Full_View (Etype (Exp));
1060 Analyze_And_Resolve (Exp, Any_Discrete);
1061 Exp_Type := Etype (Exp);
1064 Check_Unset_Reference (Exp);
1065 Exp_Btype := Base_Type (Exp_Type);
1067 -- The expression must be of a discrete type which must be determinable
1068 -- independently of the context in which the expression occurs, but
1069 -- using the fact that the expression must be of a discrete type.
1070 -- Moreover, the type this expression must not be a character literal
1071 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1073 -- If error already reported by Resolve, nothing more to do
1075 if Exp_Btype = Any_Discrete
1076 or else Exp_Btype = Any_Type
1080 elsif Exp_Btype = Any_Character then
1082 ("character literal as case expression is ambiguous", Exp);
1085 elsif Ada_Version = Ada_83
1086 and then (Is_Generic_Type (Exp_Btype)
1087 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1090 ("(Ada 83) case expression cannot be of a generic type", Exp);
1094 -- If the case expression is a formal object of mode in out, then treat
1095 -- it as having a nonstatic subtype by forcing use of the base type
1096 -- (which has to get passed to Check_Case_Choices below). Also use base
1097 -- type when the case expression is parenthesized.
1099 if Paren_Count (Exp) > 0
1100 or else (Is_Entity_Name (Exp)
1101 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1103 Exp_Type := Exp_Btype;
1106 -- Call instantiated Analyze_Choices which does the rest of the work
1108 Analyze_Choices (N, Exp_Type, Dont_Care, Others_Present);
1110 -- A case statement with a single OTHERS alternative is not allowed
1114 and then List_Length (Alternatives (N)) = 1
1116 Mark_Non_ALFA_Subprogram
1117 ("OTHERS as unique case alternative is not in ALFA", N);
1118 Check_SPARK_Restriction
1119 ("OTHERS as unique case alternative is not allowed", N);
1122 if Exp_Type = Universal_Integer and then not Others_Present then
1123 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1126 -- If all our exits were blocked by unconditional transfers of control,
1127 -- then the entire CASE statement acts as an unconditional transfer of
1128 -- control, so treat it like one, and check unreachable code. Skip this
1129 -- test if we had serious errors preventing any statement analysis.
1131 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1132 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1133 Check_Unreachable_Code (N);
1135 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1138 if not Expander_Active
1139 and then Compile_Time_Known_Value (Expression (N))
1140 and then Serious_Errors_Detected = 0
1143 Chosen : constant Node_Id := Find_Static_Alternative (N);
1147 Alt := First (Alternatives (N));
1148 while Present (Alt) loop
1149 if Alt /= Chosen then
1150 Remove_Warning_Messages (Statements (Alt));
1157 end Analyze_Case_Statement;
1159 ----------------------------
1160 -- Analyze_Exit_Statement --
1161 ----------------------------
1163 -- If the exit includes a name, it must be the name of a currently open
1164 -- loop. Otherwise there must be an innermost open loop on the stack, to
1165 -- which the statement implicitly refers.
1167 -- Additionally, in formal mode:
1169 -- The exit can only name the closest enclosing loop;
1171 -- An exit with a when clause must be directly contained in a loop;
1173 -- An exit without a when clause must be directly contained in an
1174 -- if-statement with no elsif or else, which is itself directly contained
1175 -- in a loop. The exit must be the last statement in the if-statement.
1177 procedure Analyze_Exit_Statement (N : Node_Id) is
1178 Target : constant Node_Id := Name (N);
1179 Cond : constant Node_Id := Condition (N);
1180 Scope_Id : Entity_Id;
1186 Check_Unreachable_Code (N);
1189 if Present (Target) then
1191 U_Name := Entity (Target);
1193 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1194 Error_Msg_N ("invalid loop name in exit statement", N);
1198 if Has_Loop_In_Inner_Open_Scopes (U_Name) then
1199 Mark_Non_ALFA_Subprogram
1200 ("exit label must name the closest enclosing loop"
1202 Check_SPARK_Restriction
1203 ("exit label must name the closest enclosing loop", N);
1206 Set_Has_Exit (U_Name);
1213 for J in reverse 0 .. Scope_Stack.Last loop
1214 Scope_Id := Scope_Stack.Table (J).Entity;
1215 Kind := Ekind (Scope_Id);
1218 and then (No (Target) or else Scope_Id = U_Name)
1220 Set_Has_Exit (Scope_Id);
1223 elsif Kind = E_Block
1224 or else Kind = E_Loop
1225 or else Kind = E_Return_Statement
1231 ("cannot exit from program unit or accept statement", N);
1236 -- Verify that if present the condition is a Boolean expression
1238 if Present (Cond) then
1239 Analyze_And_Resolve (Cond, Any_Boolean);
1240 Check_Unset_Reference (Cond);
1243 -- In SPARK mode, verify that the exit statement respects the SPARK
1246 if Present (Cond) then
1247 if Nkind (Parent (N)) /= N_Loop_Statement then
1248 Mark_Non_ALFA_Subprogram
1249 ("exit with when clause must be directly in loop"
1251 Check_SPARK_Restriction
1252 ("exit with when clause must be directly in loop", N);
1256 if Nkind (Parent (N)) /= N_If_Statement then
1257 if Nkind (Parent (N)) = N_Elsif_Part then
1258 Mark_Non_ALFA_Subprogram
1259 ("exit must be in IF without ELSIF in ALFA", N);
1260 Check_SPARK_Restriction
1261 ("exit must be in IF without ELSIF", N);
1263 Mark_Non_ALFA_Subprogram
1264 ("exit must be directly in IF in ALFA", N);
1265 Check_SPARK_Restriction ("exit must be directly in IF", N);
1268 elsif Nkind (Parent (Parent (N))) /= N_Loop_Statement then
1269 Mark_Non_ALFA_Subprogram
1270 ("exit must be in IF directly in loop in ALFA", N);
1271 Check_SPARK_Restriction
1272 ("exit must be in IF directly in loop", N);
1274 -- First test the presence of ELSE, so that an exit in an ELSE
1275 -- leads to an error mentioning the ELSE.
1277 elsif Present (Else_Statements (Parent (N))) then
1278 Mark_Non_ALFA_Subprogram
1279 ("exit must be in IF without ELSE in ALFA", N);
1280 Check_SPARK_Restriction ("exit must be in IF without ELSE", N);
1282 -- An exit in an ELSIF does not reach here, as it would have been
1283 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1285 elsif Present (Elsif_Parts (Parent (N))) then
1286 Mark_Non_ALFA_Subprogram
1287 ("exit must be in IF without ELSIF in ALFA", N);
1288 Check_SPARK_Restriction ("exit must be in IF without ELSIF", N);
1292 -- Chain exit statement to associated loop entity
1294 Set_Next_Exit_Statement (N, First_Exit_Statement (Scope_Id));
1295 Set_First_Exit_Statement (Scope_Id, N);
1297 -- Since the exit may take us out of a loop, any previous assignment
1298 -- statement is not useless, so clear last assignment indications. It
1299 -- is OK to keep other current values, since if the exit statement
1300 -- does not exit, then the current values are still valid.
1302 Kill_Current_Values (Last_Assignment_Only => True);
1303 end Analyze_Exit_Statement;
1305 ----------------------------
1306 -- Analyze_Goto_Statement --
1307 ----------------------------
1309 procedure Analyze_Goto_Statement (N : Node_Id) is
1310 Label : constant Node_Id := Name (N);
1311 Scope_Id : Entity_Id;
1312 Label_Scope : Entity_Id;
1313 Label_Ent : Entity_Id;
1316 Mark_Non_ALFA_Subprogram ("goto statement is not in ALFA", N);
1317 Check_SPARK_Restriction ("goto statement is not allowed", N);
1319 -- Actual semantic checks
1321 Check_Unreachable_Code (N);
1322 Kill_Current_Values (Last_Assignment_Only => True);
1325 Label_Ent := Entity (Label);
1327 -- Ignore previous error
1329 if Label_Ent = Any_Id then
1332 -- We just have a label as the target of a goto
1334 elsif Ekind (Label_Ent) /= E_Label then
1335 Error_Msg_N ("target of goto statement must be a label", Label);
1338 -- Check that the target of the goto is reachable according to Ada
1339 -- scoping rules. Note: the special gotos we generate for optimizing
1340 -- local handling of exceptions would violate these rules, but we mark
1341 -- such gotos as analyzed when built, so this code is never entered.
1343 elsif not Reachable (Label_Ent) then
1344 Error_Msg_N ("target of goto statement is not reachable", Label);
1348 -- Here if goto passes initial validity checks
1350 Label_Scope := Enclosing_Scope (Label_Ent);
1352 for J in reverse 0 .. Scope_Stack.Last loop
1353 Scope_Id := Scope_Stack.Table (J).Entity;
1355 if Label_Scope = Scope_Id
1356 or else (Ekind (Scope_Id) /= E_Block
1357 and then Ekind (Scope_Id) /= E_Loop
1358 and then Ekind (Scope_Id) /= E_Return_Statement)
1360 if Scope_Id /= Label_Scope then
1362 ("cannot exit from program unit or accept statement", N);
1369 raise Program_Error;
1370 end Analyze_Goto_Statement;
1372 --------------------------
1373 -- Analyze_If_Statement --
1374 --------------------------
1376 -- A special complication arises in the analysis of if statements
1378 -- The expander has circuitry to completely delete code that it can tell
1379 -- will not be executed (as a result of compile time known conditions). In
1380 -- the analyzer, we ensure that code that will be deleted in this manner is
1381 -- analyzed but not expanded. This is obviously more efficient, but more
1382 -- significantly, difficulties arise if code is expanded and then
1383 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1384 -- generated in deleted code must be frozen from start, because the nodes
1385 -- on which they depend will not be available at the freeze point.
1387 procedure Analyze_If_Statement (N : Node_Id) is
1390 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1391 -- Recursively save value of this global, will be restored on exit
1393 Save_In_Deleted_Code : Boolean;
1395 Del : Boolean := False;
1396 -- This flag gets set True if a True condition has been found, which
1397 -- means that remaining ELSE/ELSIF parts are deleted.
1399 procedure Analyze_Cond_Then (Cnode : Node_Id);
1400 -- This is applied to either the N_If_Statement node itself or to an
1401 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1402 -- statements associated with it.
1404 -----------------------
1405 -- Analyze_Cond_Then --
1406 -----------------------
1408 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1409 Cond : constant Node_Id := Condition (Cnode);
1410 Tstm : constant List_Id := Then_Statements (Cnode);
1413 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1414 Analyze_And_Resolve (Cond, Any_Boolean);
1415 Check_Unset_Reference (Cond);
1416 Set_Current_Value_Condition (Cnode);
1418 -- If already deleting, then just analyze then statements
1421 Analyze_Statements (Tstm);
1423 -- Compile time known value, not deleting yet
1425 elsif Compile_Time_Known_Value (Cond) then
1426 Save_In_Deleted_Code := In_Deleted_Code;
1428 -- If condition is True, then analyze the THEN statements and set
1429 -- no expansion for ELSE and ELSIF parts.
1431 if Is_True (Expr_Value (Cond)) then
1432 Analyze_Statements (Tstm);
1434 Expander_Mode_Save_And_Set (False);
1435 In_Deleted_Code := True;
1437 -- If condition is False, analyze THEN with expansion off
1439 else -- Is_False (Expr_Value (Cond))
1440 Expander_Mode_Save_And_Set (False);
1441 In_Deleted_Code := True;
1442 Analyze_Statements (Tstm);
1443 Expander_Mode_Restore;
1444 In_Deleted_Code := Save_In_Deleted_Code;
1447 -- Not known at compile time, not deleting, normal analysis
1450 Analyze_Statements (Tstm);
1452 end Analyze_Cond_Then;
1454 -- Start of Analyze_If_Statement
1457 -- Initialize exit count for else statements. If there is no else part,
1458 -- this count will stay non-zero reflecting the fact that the uncovered
1459 -- else case is an unblocked exit.
1461 Unblocked_Exit_Count := 1;
1462 Analyze_Cond_Then (N);
1464 -- Now to analyze the elsif parts if any are present
1466 if Present (Elsif_Parts (N)) then
1467 E := First (Elsif_Parts (N));
1468 while Present (E) loop
1469 Analyze_Cond_Then (E);
1474 if Present (Else_Statements (N)) then
1475 Analyze_Statements (Else_Statements (N));
1478 -- If all our exits were blocked by unconditional transfers of control,
1479 -- then the entire IF statement acts as an unconditional transfer of
1480 -- control, so treat it like one, and check unreachable code.
1482 if Unblocked_Exit_Count = 0 then
1483 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1484 Check_Unreachable_Code (N);
1486 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1490 Expander_Mode_Restore;
1491 In_Deleted_Code := Save_In_Deleted_Code;
1494 if not Expander_Active
1495 and then Compile_Time_Known_Value (Condition (N))
1496 and then Serious_Errors_Detected = 0
1498 if Is_True (Expr_Value (Condition (N))) then
1499 Remove_Warning_Messages (Else_Statements (N));
1501 if Present (Elsif_Parts (N)) then
1502 E := First (Elsif_Parts (N));
1503 while Present (E) loop
1504 Remove_Warning_Messages (Then_Statements (E));
1510 Remove_Warning_Messages (Then_Statements (N));
1513 end Analyze_If_Statement;
1515 ----------------------------------------
1516 -- Analyze_Implicit_Label_Declaration --
1517 ----------------------------------------
1519 -- An implicit label declaration is generated in the innermost enclosing
1520 -- declarative part. This is done for labels, and block and loop names.
1522 -- Note: any changes in this routine may need to be reflected in
1523 -- Analyze_Label_Entity.
1525 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1526 Id : constant Node_Id := Defining_Identifier (N);
1529 Set_Ekind (Id, E_Label);
1530 Set_Etype (Id, Standard_Void_Type);
1531 Set_Enclosing_Scope (Id, Current_Scope);
1532 end Analyze_Implicit_Label_Declaration;
1534 ------------------------------
1535 -- Analyze_Iteration_Scheme --
1536 ------------------------------
1538 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1540 procedure Process_Bounds (R : Node_Id);
1541 -- If the iteration is given by a range, create temporaries and
1542 -- assignment statements block to capture the bounds and perform
1543 -- required finalization actions in case a bound includes a function
1544 -- call that uses the temporary stack. We first pre-analyze a copy of
1545 -- the range in order to determine the expected type, and analyze and
1546 -- resolve the original bounds.
1548 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1549 -- If the bounds are given by a 'Range reference on a function call
1550 -- that returns a controlled array, introduce an explicit declaration
1551 -- to capture the bounds, so that the function result can be finalized
1552 -- in timely fashion.
1554 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean;
1555 -- N is the node for an arbitrary construct. This function searches the
1556 -- construct N to see if any expressions within it contain function
1557 -- calls that use the secondary stack, returning True if any such call
1558 -- is found, and False otherwise.
1560 procedure Pre_Analyze_Range (R_Copy : Node_Id);
1561 -- Determine expected type of range or domain of iteration of Ada 2012
1562 -- loop by analyzing separate copy. Do the analysis and resolution of
1563 -- the copy of the bound(s) with expansion disabled, to prevent the
1564 -- generation of finalization actions. This prevents memory leaks when
1565 -- the bounds contain calls to functions returning controlled arrays or
1566 -- when the domain of iteration is a container.
1568 -----------------------
1569 -- Pre_Analyze_Range --
1570 -----------------------
1572 procedure Pre_Analyze_Range (R_Copy : Node_Id) is
1573 Save_Analysis : Boolean;
1575 Save_Analysis := Full_Analysis;
1576 Full_Analysis := False;
1577 Expander_Mode_Save_And_Set (False);
1581 if Nkind (R_Copy) in N_Subexpr
1582 and then Is_Overloaded (R_Copy)
1585 -- Apply preference rules for range of predefined integer types,
1586 -- or diagnose true ambiguity.
1591 Found : Entity_Id := Empty;
1594 Get_First_Interp (R_Copy, I, It);
1595 while Present (It.Typ) loop
1596 if Is_Discrete_Type (It.Typ) then
1600 if Scope (Found) = Standard_Standard then
1603 elsif Scope (It.Typ) = Standard_Standard then
1607 -- Both of them are user-defined
1610 ("ambiguous bounds in range of iteration",
1612 Error_Msg_N ("\possible interpretations:", R_Copy);
1613 Error_Msg_NE ("\\} ", R_Copy, Found);
1614 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
1620 Get_Next_Interp (I, It);
1625 if Is_Entity_Name (R_Copy)
1626 and then Is_Type (Entity (R_Copy))
1629 -- Subtype mark in iteration scheme
1633 elsif Nkind (R_Copy) in N_Subexpr then
1635 -- Expression in range, or Ada 2012 iterator
1640 Expander_Mode_Restore;
1641 Full_Analysis := Save_Analysis;
1642 end Pre_Analyze_Range;
1644 --------------------
1645 -- Process_Bounds --
1646 --------------------
1648 procedure Process_Bounds (R : Node_Id) is
1649 Loc : constant Source_Ptr := Sloc (N);
1650 R_Copy : constant Node_Id := New_Copy_Tree (R);
1651 Lo : constant Node_Id := Low_Bound (R);
1652 Hi : constant Node_Id := High_Bound (R);
1653 New_Lo_Bound : Node_Id;
1654 New_Hi_Bound : Node_Id;
1658 (Original_Bound : Node_Id;
1659 Analyzed_Bound : Node_Id) return Node_Id;
1660 -- Capture value of bound and return captured value
1667 (Original_Bound : Node_Id;
1668 Analyzed_Bound : Node_Id) return Node_Id
1675 -- If the bound is a constant or an object, no need for a separate
1676 -- declaration. If the bound is the result of previous expansion
1677 -- it is already analyzed and should not be modified. Note that
1678 -- the Bound will be resolved later, if needed, as part of the
1679 -- call to Make_Index (literal bounds may need to be resolved to
1682 if Analyzed (Original_Bound) then
1683 return Original_Bound;
1685 elsif Nkind_In (Analyzed_Bound, N_Integer_Literal,
1686 N_Character_Literal)
1687 or else Is_Entity_Name (Analyzed_Bound)
1689 Analyze_And_Resolve (Original_Bound, Typ);
1690 return Original_Bound;
1693 -- Here we need to capture the value
1695 Analyze_And_Resolve (Original_Bound, Typ);
1697 -- Normally, the best approach is simply to generate a constant
1698 -- declaration that captures the bound. However, there is a nasty
1699 -- case where this is wrong. If the bound is complex, and has a
1700 -- possible use of the secondary stack, we need to generate a
1701 -- separate assignment statement to ensure the creation of a block
1702 -- which will release the secondary stack.
1704 -- We prefer the constant declaration, since it leaves us with a
1705 -- proper trace of the value, useful in optimizations that get rid
1706 -- of junk range checks.
1708 if not Has_Call_Using_Secondary_Stack (Original_Bound) then
1709 Force_Evaluation (Original_Bound);
1710 return Original_Bound;
1713 Id := Make_Temporary (Loc, 'R', Original_Bound);
1715 -- Here we make a declaration with a separate assignment
1716 -- statement, and insert before loop header.
1719 Make_Object_Declaration (Loc,
1720 Defining_Identifier => Id,
1721 Object_Definition => New_Occurrence_Of (Typ, Loc));
1724 Make_Assignment_Statement (Loc,
1725 Name => New_Occurrence_Of (Id, Loc),
1726 Expression => Relocate_Node (Original_Bound));
1728 -- We must recursively clean in the relocated expression the flag
1729 -- analyzed to ensure that the expression is reanalyzed. Required
1730 -- to ensure that the transient scope is established now (because
1731 -- Establish_Transient_Scope discarded generating transient scopes
1732 -- in the analysis of the iteration scheme).
1734 Reset_Analyzed_Flags (Expression (Assign));
1736 Insert_Actions (Parent (N), New_List (Decl, Assign));
1738 -- Now that this temporary variable is initialized we decorate it
1739 -- as safe-to-reevaluate to inform to the backend that no further
1740 -- asignment will be issued and hence it can be handled as side
1741 -- effect free. Note that this decoration must be done when the
1742 -- assignment has been analyzed because otherwise it will be
1743 -- rejected (see Analyze_Assignment).
1745 Set_Is_Safe_To_Reevaluate (Id);
1747 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
1749 if Nkind (Assign) = N_Assignment_Statement then
1750 return Expression (Assign);
1752 return Original_Bound;
1756 -- Start of processing for Process_Bounds
1759 Set_Parent (R_Copy, Parent (R));
1760 Pre_Analyze_Range (R_Copy);
1761 Typ := Etype (R_Copy);
1763 -- If the type of the discrete range is Universal_Integer, then the
1764 -- bound's type must be resolved to Integer, and any object used to
1765 -- hold the bound must also have type Integer, unless the literal
1766 -- bounds are constant-folded expressions with a user-defined type.
1768 if Typ = Universal_Integer then
1769 if Nkind (Lo) = N_Integer_Literal
1770 and then Present (Etype (Lo))
1771 and then Scope (Etype (Lo)) /= Standard_Standard
1775 elsif Nkind (Hi) = N_Integer_Literal
1776 and then Present (Etype (Hi))
1777 and then Scope (Etype (Hi)) /= Standard_Standard
1782 Typ := Standard_Integer;
1788 New_Lo_Bound := One_Bound (Lo, Low_Bound (R_Copy));
1789 New_Hi_Bound := One_Bound (Hi, High_Bound (R_Copy));
1791 -- Propagate staticness to loop range itself, in case the
1792 -- corresponding subtype is static.
1794 if New_Lo_Bound /= Lo
1795 and then Is_Static_Expression (New_Lo_Bound)
1797 Rewrite (Low_Bound (R), New_Copy (New_Lo_Bound));
1800 if New_Hi_Bound /= Hi
1801 and then Is_Static_Expression (New_Hi_Bound)
1803 Rewrite (High_Bound (R), New_Copy (New_Hi_Bound));
1807 --------------------------------------
1808 -- Check_Controlled_Array_Attribute --
1809 --------------------------------------
1811 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1813 if Nkind (DS) = N_Attribute_Reference
1814 and then Is_Entity_Name (Prefix (DS))
1815 and then Ekind (Entity (Prefix (DS))) = E_Function
1816 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1819 Component_Type (Etype (Entity (Prefix (DS)))))
1820 and then Expander_Active
1823 Loc : constant Source_Ptr := Sloc (N);
1824 Arr : constant Entity_Id := Etype (Entity (Prefix (DS)));
1825 Indx : constant Entity_Id :=
1826 Base_Type (Etype (First_Index (Arr)));
1827 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
1832 Make_Subtype_Declaration (Loc,
1833 Defining_Identifier => Subt,
1834 Subtype_Indication =>
1835 Make_Subtype_Indication (Loc,
1836 Subtype_Mark => New_Reference_To (Indx, Loc),
1838 Make_Range_Constraint (Loc,
1839 Relocate_Node (DS))));
1840 Insert_Before (Parent (N), Decl);
1844 Make_Attribute_Reference (Loc,
1845 Prefix => New_Reference_To (Subt, Loc),
1846 Attribute_Name => Attribute_Name (DS)));
1850 end Check_Controlled_Array_Attribute;
1852 ------------------------------------
1853 -- Has_Call_Using_Secondary_Stack --
1854 ------------------------------------
1856 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean is
1858 function Check_Call (N : Node_Id) return Traverse_Result;
1859 -- Check if N is a function call which uses the secondary stack
1865 function Check_Call (N : Node_Id) return Traverse_Result is
1868 Return_Typ : Entity_Id;
1871 if Nkind (N) = N_Function_Call then
1874 -- Call using access to subprogram with explicit dereference
1876 if Nkind (Nam) = N_Explicit_Dereference then
1877 Subp := Etype (Nam);
1882 Subp := Entity (Nam);
1885 Return_Typ := Etype (Subp);
1887 if Is_Composite_Type (Return_Typ)
1888 and then not Is_Constrained (Return_Typ)
1892 elsif Sec_Stack_Needed_For_Return (Subp) then
1897 -- Continue traversing the tree
1902 function Check_Calls is new Traverse_Func (Check_Call);
1904 -- Start of processing for Has_Call_Using_Secondary_Stack
1907 return Check_Calls (N) = Abandon;
1908 end Has_Call_Using_Secondary_Stack;
1910 -- Start of processing for Analyze_Iteration_Scheme
1913 -- If this is a rewritten quantified expression, the iteration scheme
1914 -- has been analyzed already. Do no repeat analysis because the loop
1915 -- variable is already declared.
1917 if Analyzed (N) then
1921 -- For an infinite loop, there is no iteration scheme
1927 -- Iteration scheme is present
1930 Cond : constant Node_Id := Condition (N);
1933 -- For WHILE loop, verify that the condition is a Boolean expression
1934 -- and resolve and check it.
1936 if Present (Cond) then
1937 Analyze_And_Resolve (Cond, Any_Boolean);
1938 Check_Unset_Reference (Cond);
1939 Set_Current_Value_Condition (N);
1942 -- For an iterator specification with "of", pre-analyze range to
1943 -- capture function calls that may require finalization actions.
1945 elsif Present (Iterator_Specification (N)) then
1946 Pre_Analyze_Range (Name (Iterator_Specification (N)));
1947 Analyze_Iterator_Specification (Iterator_Specification (N));
1949 -- Else we have a FOR loop
1953 LP : constant Node_Id := Loop_Parameter_Specification (N);
1954 Id : constant Entity_Id := Defining_Identifier (LP);
1955 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
1962 -- We always consider the loop variable to be referenced, since
1963 -- the loop may be used just for counting purposes.
1965 Generate_Reference (Id, N, ' ');
1967 -- Check for the case of loop variable hiding a local variable
1968 -- (used later on to give a nice warning if the hidden variable
1969 -- is never assigned).
1972 H : constant Entity_Id := Homonym (Id);
1975 and then Enclosing_Dynamic_Scope (H) =
1976 Enclosing_Dynamic_Scope (Id)
1977 and then Ekind (H) = E_Variable
1978 and then Is_Discrete_Type (Etype (H))
1980 Set_Hiding_Loop_Variable (H, Id);
1984 -- Loop parameter specification must include subtype mark in
1987 if Nkind (DS) = N_Range then
1988 Check_SPARK_Restriction
1989 ("loop parameter specification must include subtype mark",
1993 -- Now analyze the subtype definition. If it is a range, create
1994 -- temporaries for bounds.
1996 if Nkind (DS) = N_Range
1997 and then Expander_Active
1999 Process_Bounds (DS);
2001 -- expander not active or else range of iteration is a subtype
2002 -- indication, an entity, or a function call that yields an
2003 -- aggregate or a container.
2006 D_Copy := New_Copy_Tree (DS);
2007 Set_Parent (D_Copy, Parent (DS));
2008 Pre_Analyze_Range (D_Copy);
2010 if Nkind (D_Copy) = N_Function_Call
2012 (Is_Entity_Name (D_Copy)
2013 and then not Is_Type (Entity (D_Copy)))
2015 -- This is an iterator specification. Rewrite as such
2016 -- and analyze, to capture function calls that may
2017 -- require finalization actions.
2020 I_Spec : constant Node_Id :=
2021 Make_Iterator_Specification (Sloc (LP),
2022 Defining_Identifier =>
2025 Subtype_Indication => Empty,
2027 Reverse_Present (LP));
2029 Set_Iterator_Specification (N, I_Spec);
2030 Set_Loop_Parameter_Specification (N, Empty);
2031 Analyze_Iterator_Specification (I_Spec);
2035 -- Domain of iteration is not a function call, and is
2036 -- side-effect free.
2047 -- Some additional checks if we are iterating through a type
2049 if Is_Entity_Name (DS)
2050 and then Present (Entity (DS))
2051 and then Is_Type (Entity (DS))
2053 -- The subtype indication may denote the completion of an
2054 -- incomplete type declaration.
2056 if Ekind (Entity (DS)) = E_Incomplete_Type then
2057 Set_Entity (DS, Get_Full_View (Entity (DS)));
2058 Set_Etype (DS, Entity (DS));
2061 -- Attempt to iterate through non-static predicate
2063 if Is_Discrete_Type (Entity (DS))
2064 and then Present (Predicate_Function (Entity (DS)))
2065 and then No (Static_Predicate (Entity (DS)))
2067 Bad_Predicated_Subtype_Use
2068 ("cannot use subtype& with non-static "
2069 & "predicate for loop iteration", DS, Entity (DS));
2073 -- Error if not discrete type
2075 if not Is_Discrete_Type (Etype (DS)) then
2076 Wrong_Type (DS, Any_Discrete);
2077 Set_Etype (DS, Any_Type);
2080 Check_Controlled_Array_Attribute (DS);
2082 Make_Index (DS, LP, In_Iter_Schm => True);
2084 Set_Ekind (Id, E_Loop_Parameter);
2086 -- If the loop is part of a predicate or precondition, it may
2087 -- be analyzed twice, once in the source and once on the copy
2088 -- used to check conformance. Preserve the original itype
2089 -- because the second one may be created in a different scope,
2090 -- e.g. a precondition procedure, leading to a crash in GIGI.
2092 if No (Etype (Id)) or else Etype (Id) = Any_Type then
2093 Set_Etype (Id, Etype (DS));
2096 -- The entity for iterating over a loop is always in ALFA if
2097 -- its type is in ALFA, and it is not an iteration over
2098 -- elements of a container using the OF syntax.
2100 if Is_In_ALFA (Etype (Id))
2102 (No (Iterator_Specification (N))
2103 or else not Of_Present (Iterator_Specification (N)))
2105 Set_Is_In_ALFA (Id);
2108 -- Treat a range as an implicit reference to the type, to
2109 -- inhibit spurious warnings.
2111 Generate_Reference (Base_Type (Etype (DS)), N, ' ');
2112 Set_Is_Known_Valid (Id, True);
2114 -- The loop is not a declarative part, so the only entity
2115 -- declared "within" must be frozen explicitly.
2118 Flist : constant List_Id := Freeze_Entity (Id, N);
2120 if Is_Non_Empty_List (Flist) then
2121 Insert_Actions (N, Flist);
2125 -- Check for null or possibly null range and issue warning. We
2126 -- suppress such messages in generic templates and instances,
2127 -- because in practice they tend to be dubious in these cases.
2129 if Nkind (DS) = N_Range and then Comes_From_Source (N) then
2131 L : constant Node_Id := Low_Bound (DS);
2132 H : constant Node_Id := High_Bound (DS);
2135 -- If range of loop is null, issue warning
2137 if Compile_Time_Compare
2138 (L, H, Assume_Valid => True) = GT
2140 -- Suppress the warning if inside a generic template
2141 -- or instance, since in practice they tend to be
2142 -- dubious in these cases since they can result from
2143 -- intended parametrization.
2145 if not Inside_A_Generic
2146 and then not In_Instance
2148 -- Specialize msg if invalid values could make the
2149 -- loop non-null after all.
2151 if Compile_Time_Compare
2152 (L, H, Assume_Valid => False) = GT
2155 ("?loop range is null, loop will not execute",
2158 -- Since we know the range of the loop is null,
2159 -- set the appropriate flag to remove the loop
2160 -- entirely during expansion.
2162 Set_Is_Null_Loop (Parent (N));
2164 -- Here is where the loop could execute because
2165 -- of invalid values, so issue appropriate
2166 -- message and in this case we do not set the
2167 -- Is_Null_Loop flag since the loop may execute.
2171 ("?loop range may be null, "
2172 & "loop may not execute",
2175 ("?can only execute if invalid values "
2181 -- In either case, suppress warnings in the body of
2182 -- the loop, since it is likely that these warnings
2183 -- will be inappropriate if the loop never actually
2184 -- executes, which is likely.
2186 Set_Suppress_Loop_Warnings (Parent (N));
2188 -- The other case for a warning is a reverse loop
2189 -- where the upper bound is the integer literal zero
2190 -- or one, and the lower bound can be positive.
2192 -- For example, we have
2194 -- for J in reverse N .. 1 loop
2196 -- In practice, this is very likely to be a case of
2197 -- reversing the bounds incorrectly in the range.
2199 elsif Reverse_Present (LP)
2200 and then Nkind (Original_Node (H)) =
2202 and then (Intval (Original_Node (H)) = Uint_0
2204 Intval (Original_Node (H)) = Uint_1)
2206 Error_Msg_N ("?loop range may be null", DS);
2207 Error_Msg_N ("\?bounds may be wrong way round", DS);
2214 end Analyze_Iteration_Scheme;
2216 -------------------------------------
2217 -- Analyze_Iterator_Specification --
2218 -------------------------------------
2220 procedure Analyze_Iterator_Specification (N : Node_Id) is
2221 Loc : constant Source_Ptr := Sloc (N);
2222 Def_Id : constant Node_Id := Defining_Identifier (N);
2223 Subt : constant Node_Id := Subtype_Indication (N);
2224 Container : constant Node_Id := Name (N);
2230 Enter_Name (Def_Id);
2231 Set_Ekind (Def_Id, E_Variable);
2233 if Present (Subt) then
2237 -- If it is an expression, the container is pre-analyzed in the caller.
2238 -- If it it of a controlled type we need a block for the finalization
2239 -- actions. As for loop bounds that need finalization, we create a
2240 -- declaration and an assignment to trigger these actions.
2242 if Present (Etype (Container))
2243 and then Is_Controlled (Etype (Container))
2244 and then not Is_Entity_Name (Container)
2247 Id : constant Entity_Id := Make_Temporary (Loc, 'R', Container);
2253 Typ := Etype (Container);
2256 Make_Object_Declaration (Loc,
2257 Defining_Identifier => Id,
2258 Object_Definition => New_Occurrence_Of (Typ, Loc));
2261 Make_Assignment_Statement (Loc,
2262 Name => New_Occurrence_Of (Id, Loc),
2263 Expression => Relocate_Node (Container));
2265 Insert_Actions (Parent (N), New_List (Decl, Assign));
2270 -- Container is an entity or an array with uncontrolled components
2272 Analyze_And_Resolve (Container);
2275 Typ := Etype (Container);
2277 if Is_Array_Type (Typ) then
2278 if Of_Present (N) then
2279 Set_Etype (Def_Id, Component_Type (Typ));
2282 ("to iterate over the elements of an array, use OF", N);
2283 Set_Etype (Def_Id, Etype (First_Index (Typ)));
2286 -- Iteration over a container
2289 Set_Ekind (Def_Id, E_Loop_Parameter);
2291 if Of_Present (N) then
2293 -- Find the Element_Type in the package instance that defines the
2296 Ent := First_Entity (Scope (Base_Type (Typ)));
2297 while Present (Ent) loop
2298 if Chars (Ent) = Name_Element_Type then
2299 Set_Etype (Def_Id, Ent);
2307 -- Find the Cursor type in similar fashion
2309 Ent := First_Entity (Scope (Base_Type (Typ)));
2310 while Present (Ent) loop
2311 if Chars (Ent) = Name_Cursor then
2312 Set_Etype (Def_Id, Ent);
2320 end Analyze_Iterator_Specification;
2326 -- Note: the semantic work required for analyzing labels (setting them as
2327 -- reachable) was done in a prepass through the statements in the block,
2328 -- so that forward gotos would be properly handled. See Analyze_Statements
2329 -- for further details. The only processing required here is to deal with
2330 -- optimizations that depend on an assumption of sequential control flow,
2331 -- since of course the occurrence of a label breaks this assumption.
2333 procedure Analyze_Label (N : Node_Id) is
2334 pragma Warnings (Off, N);
2336 Kill_Current_Values;
2339 --------------------------
2340 -- Analyze_Label_Entity --
2341 --------------------------
2343 procedure Analyze_Label_Entity (E : Entity_Id) is
2345 Set_Ekind (E, E_Label);
2346 Set_Etype (E, Standard_Void_Type);
2347 Set_Enclosing_Scope (E, Current_Scope);
2348 Set_Reachable (E, True);
2349 end Analyze_Label_Entity;
2351 ----------------------------
2352 -- Analyze_Loop_Statement --
2353 ----------------------------
2355 procedure Analyze_Loop_Statement (N : Node_Id) is
2356 Loop_Statement : constant Node_Id := N;
2358 Id : constant Node_Id := Identifier (Loop_Statement);
2359 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
2363 if Present (Id) then
2365 -- Make name visible, e.g. for use in exit statements. Loop labels
2366 -- are always considered to be referenced.
2371 -- Guard against serious error (typically, a scope mismatch when
2372 -- semantic analysis is requested) by creating loop entity to
2373 -- continue analysis.
2376 if Total_Errors_Detected /= 0 then
2379 (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
2381 raise Program_Error;
2385 Generate_Reference (Ent, Loop_Statement, ' ');
2386 Generate_Definition (Ent);
2388 -- If we found a label, mark its type. If not, ignore it, since it
2389 -- means we have a conflicting declaration, which would already
2390 -- have been diagnosed at declaration time. Set Label_Construct
2391 -- of the implicit label declaration, which is not created by the
2392 -- parser for generic units.
2394 if Ekind (Ent) = E_Label then
2395 Set_Ekind (Ent, E_Loop);
2397 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
2398 Set_Label_Construct (Parent (Ent), Loop_Statement);
2403 -- Case of no identifier present
2408 (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
2409 Set_Etype (Ent, Standard_Void_Type);
2410 Set_Parent (Ent, Loop_Statement);
2413 -- Kill current values on entry to loop, since statements in the body of
2414 -- the loop may have been executed before the loop is entered. Similarly
2415 -- we kill values after the loop, since we do not know that the body of
2416 -- the loop was executed.
2418 Kill_Current_Values;
2420 Analyze_Iteration_Scheme (Iter);
2422 -- Analyze the statements of the body except in the case of an Ada 2012
2423 -- iterator with the expander active. In this case the expander will do
2424 -- a rewrite of the loop into a while loop. We will then analyze the
2425 -- loop body when we analyze this while loop.
2427 -- We need to do this delay because if the container is for indefinite
2428 -- types the actual subtype of the components will only be determined
2429 -- when the cursor declaration is analyzed.
2431 -- If the expander is not active, then we want to analyze the loop body
2432 -- now even in the Ada 2012 iterator case, since the rewriting will not
2436 or else No (Iterator_Specification (Iter))
2437 or else not Expander_Active
2439 Analyze_Statements (Statements (Loop_Statement));
2442 -- Finish up processing for the loop. We kill all current values, since
2443 -- in general we don't know if the statements in the loop have been
2444 -- executed. We could do a bit better than this with a loop that we
2445 -- know will execute at least once, but it's not worth the trouble and
2446 -- the front end is not in the business of flow tracing.
2448 Process_End_Label (Loop_Statement, 'e', Ent);
2450 Kill_Current_Values;
2452 -- Check for infinite loop. Skip check for generated code, since it
2453 -- justs waste time and makes debugging the routine called harder.
2455 -- Note that we have to wait till the body of the loop is fully analyzed
2456 -- before making this call, since Check_Infinite_Loop_Warning relies on
2457 -- being able to use semantic visibility information to find references.
2459 if Comes_From_Source (N) then
2460 Check_Infinite_Loop_Warning (N);
2463 -- Code after loop is unreachable if the loop has no WHILE or FOR and
2464 -- contains no EXIT statements within the body of the loop.
2466 if No (Iter) and then not Has_Exit (Ent) then
2467 Check_Unreachable_Code (N);
2469 end Analyze_Loop_Statement;
2471 ----------------------------
2472 -- Analyze_Null_Statement --
2473 ----------------------------
2475 -- Note: the semantics of the null statement is implemented by a single
2476 -- null statement, too bad everything isn't as simple as this!
2478 procedure Analyze_Null_Statement (N : Node_Id) is
2479 pragma Warnings (Off, N);
2482 end Analyze_Null_Statement;
2484 ------------------------
2485 -- Analyze_Statements --
2486 ------------------------
2488 procedure Analyze_Statements (L : List_Id) is
2493 -- The labels declared in the statement list are reachable from
2494 -- statements in the list. We do this as a prepass so that any goto
2495 -- statement will be properly flagged if its target is not reachable.
2496 -- This is not required, but is nice behavior!
2499 while Present (S) loop
2500 if Nkind (S) = N_Label then
2501 Analyze (Identifier (S));
2502 Lab := Entity (Identifier (S));
2504 -- If we found a label mark it as reachable
2506 if Ekind (Lab) = E_Label then
2507 Generate_Definition (Lab);
2508 Set_Reachable (Lab);
2510 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
2511 Set_Label_Construct (Parent (Lab), S);
2514 -- If we failed to find a label, it means the implicit declaration
2515 -- of the label was hidden. A for-loop parameter can do this to
2516 -- a label with the same name inside the loop, since the implicit
2517 -- label declaration is in the innermost enclosing body or block
2521 Error_Msg_Sloc := Sloc (Lab);
2523 ("implicit label declaration for & is hidden#",
2531 -- Perform semantic analysis on all statements
2533 Conditional_Statements_Begin;
2536 while Present (S) loop
2541 Conditional_Statements_End;
2543 -- Make labels unreachable. Visibility is not sufficient, because labels
2544 -- in one if-branch for example are not reachable from the other branch,
2545 -- even though their declarations are in the enclosing declarative part.
2548 while Present (S) loop
2549 if Nkind (S) = N_Label then
2550 Set_Reachable (Entity (Identifier (S)), False);
2555 end Analyze_Statements;
2557 ----------------------------
2558 -- Check_Unreachable_Code --
2559 ----------------------------
2561 procedure Check_Unreachable_Code (N : Node_Id) is
2562 Error_Node : Node_Id;
2566 if Is_List_Member (N)
2567 and then Comes_From_Source (N)
2573 Nxt := Original_Node (Next (N));
2575 -- If a label follows us, then we never have dead code, since
2576 -- someone could branch to the label, so we just ignore it, unless
2577 -- we are in formal mode where goto statements are not allowed.
2579 if Nkind (Nxt) = N_Label
2580 and then not Restriction_Check_Required (SPARK)
2584 -- Otherwise see if we have a real statement following us
2587 and then Comes_From_Source (Nxt)
2588 and then Is_Statement (Nxt)
2590 -- Special very annoying exception. If we have a return that
2591 -- follows a raise, then we allow it without a warning, since
2592 -- the Ada RM annoyingly requires a useless return here!
2594 if Nkind (Original_Node (N)) /= N_Raise_Statement
2595 or else Nkind (Nxt) /= N_Simple_Return_Statement
2597 -- The rather strange shenanigans with the warning message
2598 -- here reflects the fact that Kill_Dead_Code is very good
2599 -- at removing warnings in deleted code, and this is one
2600 -- warning we would prefer NOT to have removed.
2604 -- If we have unreachable code, analyze and remove the
2605 -- unreachable code, since it is useless and we don't
2606 -- want to generate junk warnings.
2608 -- We skip this step if we are not in code generation mode.
2609 -- This is the one case where we remove dead code in the
2610 -- semantics as opposed to the expander, and we do not want
2611 -- to remove code if we are not in code generation mode,
2612 -- since this messes up the ASIS trees.
2614 -- Note that one might react by moving the whole circuit to
2615 -- exp_ch5, but then we lose the warning in -gnatc mode.
2617 if Operating_Mode = Generate_Code then
2621 -- Quit deleting when we have nothing more to delete
2622 -- or if we hit a label (since someone could transfer
2623 -- control to a label, so we should not delete it).
2625 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
2627 -- Statement/declaration is to be deleted
2631 Kill_Dead_Code (Nxt);
2635 -- Now issue the warning (or error in formal mode)
2637 if SPARK_Mode or else Restriction_Check_Required (SPARK) then
2638 Check_SPARK_Restriction
2639 ("unreachable code is not allowed", Error_Node);
2641 Error_Msg ("?unreachable code!", Sloc (Error_Node));
2645 -- If the unconditional transfer of control instruction is the
2646 -- last statement of a sequence, then see if our parent is one of
2647 -- the constructs for which we count unblocked exits, and if so,
2648 -- adjust the count.
2653 -- Statements in THEN part or ELSE part of IF statement
2655 if Nkind (P) = N_If_Statement then
2658 -- Statements in ELSIF part of an IF statement
2660 elsif Nkind (P) = N_Elsif_Part then
2662 pragma Assert (Nkind (P) = N_If_Statement);
2664 -- Statements in CASE statement alternative
2666 elsif Nkind (P) = N_Case_Statement_Alternative then
2668 pragma Assert (Nkind (P) = N_Case_Statement);
2670 -- Statements in body of block
2672 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
2673 and then Nkind (Parent (P)) = N_Block_Statement
2677 -- Statements in exception handler in a block
2679 elsif Nkind (P) = N_Exception_Handler
2680 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
2681 and then Nkind (Parent (Parent (P))) = N_Block_Statement
2685 -- None of these cases, so return
2691 -- This was one of the cases we are looking for (i.e. the
2692 -- parent construct was IF, CASE or block) so decrement count.
2694 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
2698 end Check_Unreachable_Code;