1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with 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 Rtsfind; use Rtsfind;
42 with Sem_Case; use Sem_Case;
43 with Sem_Ch3; use Sem_Ch3;
44 with Sem_Ch8; use Sem_Ch8;
45 with Sem_Disp; use Sem_Disp;
46 with Sem_Elab; use Sem_Elab;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Res; use Sem_Res;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Sem_Warn; use Sem_Warn;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Sinfo; use Sinfo;
55 with Targparm; use Targparm;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
59 package body Sem_Ch5 is
61 Unblocked_Exit_Count : Nat := 0;
62 -- This variable is used when processing if statements, case statements,
63 -- and block statements. It counts the number of exit points that are not
64 -- blocked by unconditional transfer instructions: for IF and CASE, these
65 -- are the branches of the conditional; for a block, they are the statement
66 -- sequence of the block, and the statement sequences of any exception
67 -- handlers that are part of the block. When processing is complete, if
68 -- this count is zero, it means that control cannot fall through the IF,
69 -- CASE or block statement. This is used for the generation of warning
70 -- messages. This variable is recursively saved on entry to processing the
71 -- construct, and restored on exit.
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Analyze_Iteration_Scheme (N : Node_Id);
79 ------------------------
80 -- Analyze_Assignment --
81 ------------------------
83 procedure Analyze_Assignment (N : Node_Id) is
84 Lhs : constant Node_Id := Name (N);
85 Rhs : constant Node_Id := Expression (N);
90 procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
91 -- N is the node for the left hand side of an assignment, and it
92 -- is not a variable. This routine issues an appropriate diagnostic.
95 -- This is called to kill current value settings of a simple variable
96 -- on the left hand side. We call it if we find any error in analyzing
97 -- the assignment, and at the end of processing before setting any new
98 -- current values in place.
100 procedure Set_Assignment_Type
102 Opnd_Type : in out Entity_Id);
103 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type
104 -- is the nominal subtype. This procedure is used to deal with cases
105 -- where the nominal subtype must be replaced by the actual subtype.
107 -------------------------------
108 -- Diagnose_Non_Variable_Lhs --
109 -------------------------------
111 procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
113 -- Not worth posting another error if left hand side already
114 -- flagged as being illegal in some respect.
116 if Error_Posted (N) then
119 -- Some special bad cases of entity names
121 elsif Is_Entity_Name (N) then
122 if Ekind (Entity (N)) = E_In_Parameter then
124 ("assignment to IN mode parameter not allowed", N);
126 -- Private declarations in a protected object are turned into
127 -- constants when compiling a protected function.
129 elsif Present (Scope (Entity (N)))
130 and then Is_Protected_Type (Scope (Entity (N)))
132 (Ekind (Current_Scope) = E_Function
134 Ekind (Enclosing_Dynamic_Scope (Current_Scope)) = E_Function)
137 ("protected function cannot modify protected object", N);
139 elsif Ekind (Entity (N)) = E_Loop_Parameter then
141 ("assignment to loop parameter not allowed", N);
145 ("left hand side of assignment must be a variable", N);
148 -- For indexed components or selected components, test prefix
150 elsif Nkind (N) = N_Indexed_Component then
151 Diagnose_Non_Variable_Lhs (Prefix (N));
153 -- Another special case for assignment to discriminant
155 elsif Nkind (N) = N_Selected_Component then
156 if Present (Entity (Selector_Name (N)))
157 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
160 ("assignment to discriminant not allowed", N);
162 Diagnose_Non_Variable_Lhs (Prefix (N));
166 -- If we fall through, we have no special message to issue!
168 Error_Msg_N ("left hand side of assignment must be a variable", N);
170 end Diagnose_Non_Variable_Lhs;
176 procedure Kill_Lhs is
178 if Is_Entity_Name (Lhs) then
180 Ent : constant Entity_Id := Entity (Lhs);
182 if Present (Ent) then
183 Kill_Current_Values (Ent);
189 -------------------------
190 -- Set_Assignment_Type --
191 -------------------------
193 procedure Set_Assignment_Type
195 Opnd_Type : in out Entity_Id)
198 Require_Entity (Opnd);
200 -- If the assignment operand is an in-out or out parameter, then we
201 -- get the actual subtype (needed for the unconstrained case).
202 -- If the operand is the actual in an entry declaration, then within
203 -- the accept statement it is replaced with a local renaming, which
204 -- may also have an actual subtype.
206 if Is_Entity_Name (Opnd)
207 and then (Ekind (Entity (Opnd)) = E_Out_Parameter
208 or else Ekind (Entity (Opnd)) =
210 or else Ekind (Entity (Opnd)) =
211 E_Generic_In_Out_Parameter
213 (Ekind (Entity (Opnd)) = E_Variable
214 and then Nkind (Parent (Entity (Opnd))) =
215 N_Object_Renaming_Declaration
216 and then Nkind (Parent (Parent (Entity (Opnd)))) =
219 Opnd_Type := Get_Actual_Subtype (Opnd);
221 -- If assignment operand is a component reference, then we get the
222 -- actual subtype of the component for the unconstrained case.
225 (Nkind (Opnd) = N_Selected_Component
226 or else Nkind (Opnd) = N_Explicit_Dereference)
227 and then not Is_Unchecked_Union (Opnd_Type)
229 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
231 if Present (Decl) then
232 Insert_Action (N, Decl);
233 Mark_Rewrite_Insertion (Decl);
235 Opnd_Type := Defining_Identifier (Decl);
236 Set_Etype (Opnd, Opnd_Type);
237 Freeze_Itype (Opnd_Type, N);
239 elsif Is_Constrained (Etype (Opnd)) then
240 Opnd_Type := Etype (Opnd);
243 -- For slice, use the constrained subtype created for the slice
245 elsif Nkind (Opnd) = N_Slice then
246 Opnd_Type := Etype (Opnd);
248 end Set_Assignment_Type;
250 -- Start of processing for Analyze_Assignment
253 Mark_Coextensions (N, Rhs);
258 -- Start type analysis for assignment
262 -- In the most general case, both Lhs and Rhs can be overloaded, and we
263 -- must compute the intersection of the possible types on each side.
265 if Is_Overloaded (Lhs) then
272 Get_First_Interp (Lhs, I, It);
274 while Present (It.Typ) loop
275 if Has_Compatible_Type (Rhs, It.Typ) then
276 if T1 /= Any_Type then
278 -- An explicit dereference is overloaded if the prefix
279 -- is. Try to remove the ambiguity on the prefix, the
280 -- error will be posted there if the ambiguity is real.
282 if Nkind (Lhs) = N_Explicit_Dereference then
285 PI1 : Interp_Index := 0;
291 Get_First_Interp (Prefix (Lhs), PI, PIt);
293 while Present (PIt.Typ) loop
294 if Is_Access_Type (PIt.Typ)
295 and then Has_Compatible_Type
296 (Rhs, Designated_Type (PIt.Typ))
300 Disambiguate (Prefix (Lhs),
303 if PIt = No_Interp then
305 ("ambiguous left-hand side"
306 & " in assignment", Lhs);
309 Resolve (Prefix (Lhs), PIt.Typ);
319 Get_Next_Interp (PI, PIt);
325 ("ambiguous left-hand side in assignment", Lhs);
333 Get_Next_Interp (I, It);
337 if T1 = Any_Type then
339 ("no valid types for left-hand side for assignment", Lhs);
345 -- The resulting assignment type is T1, so now we will resolve the
346 -- left hand side of the assignment using this determined type.
350 -- Cases where Lhs is not a variable
352 if not Is_Variable (Lhs) then
354 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of
355 -- a protected object.
362 if Ada_Version >= Ada_05 then
364 -- Handle chains of renamings
367 while Nkind (Ent) in N_Has_Entity
368 and then Present (Entity (Ent))
369 and then Present (Renamed_Object (Entity (Ent)))
371 Ent := Renamed_Object (Entity (Ent));
374 if (Nkind (Ent) = N_Attribute_Reference
375 and then Attribute_Name (Ent) = Name_Priority)
377 -- Renamings of the attribute Priority applied to protected
378 -- objects have been previously expanded into calls to the
379 -- Get_Ceiling run-time subprogram.
382 (Nkind (Ent) = N_Function_Call
383 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
385 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
387 -- The enclosing subprogram cannot be a protected function
390 while not (Is_Subprogram (S)
391 and then Convention (S) = Convention_Protected)
392 and then S /= Standard_Standard
397 if Ekind (S) = E_Function
398 and then Convention (S) = Convention_Protected
401 ("protected function cannot modify protected object",
405 -- Changes of the ceiling priority of the protected object
406 -- are only effective if the Ceiling_Locking policy is in
407 -- effect (AARM D.5.2 (5/2)).
409 if Locking_Policy /= 'C' then
410 Error_Msg_N ("assignment to the attribute PRIORITY has " &
412 Error_Msg_N ("\since no Locking_Policy has been " &
421 Diagnose_Non_Variable_Lhs (Lhs);
424 -- Error of assigning to limited type. We do however allow this in
425 -- certain cases where the front end generates the assignments.
427 elsif Is_Limited_Type (T1)
428 and then not Assignment_OK (Lhs)
429 and then not Assignment_OK (Original_Node (Lhs))
430 and then not Is_Value_Type (T1)
433 ("left hand of assignment must not be limited type", Lhs);
434 Explain_Limited_Type (T1, Lhs);
438 -- Resolution may have updated the subtype, in case the left-hand
439 -- side is a private protected component. Use the correct subtype
440 -- to avoid scoping issues in the back-end.
444 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
445 -- type. For example:
449 -- type Acc is access P.T;
452 -- with Pkg; use Acc;
453 -- procedure Example is
456 -- A.all := B.all; -- ERROR
459 if Nkind (Lhs) = N_Explicit_Dereference
460 and then Ekind (T1) = E_Incomplete_Type
462 Error_Msg_N ("invalid use of incomplete type", Lhs);
467 -- Now we can complete the resolution of the right hand side
469 Set_Assignment_Type (Lhs, T1);
472 -- This is the point at which we check for an unset reference
474 Check_Unset_Reference (Rhs);
476 -- Remaining steps are skipped if Rhs was syntactically in error
485 if not Covers (T1, T2) then
486 Wrong_Type (Rhs, Etype (Lhs));
491 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
492 -- types, use the non-limited view if available
494 if Nkind (Rhs) = N_Explicit_Dereference
495 and then Ekind (T2) = E_Incomplete_Type
496 and then Is_Tagged_Type (T2)
497 and then Present (Non_Limited_View (T2))
499 T2 := Non_Limited_View (T2);
502 Set_Assignment_Type (Rhs, T2);
504 if Total_Errors_Detected /= 0 then
514 if T1 = Any_Type or else T2 = Any_Type then
519 -- If the rhs is class-wide or dynamically tagged, then require the lhs
520 -- to be class-wide. The case where the rhs is a dynamically tagged call
521 -- to a dispatching operation with a controlling access result is
522 -- excluded from this check, since the target has an access type (and
523 -- no tag propagation occurs in that case).
525 if (Is_Class_Wide_Type (T2)
526 or else (Is_Dynamically_Tagged (Rhs)
527 and then not Is_Access_Type (T1)))
528 and then not Is_Class_Wide_Type (T1)
530 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
532 elsif Is_Class_Wide_Type (T1)
533 and then not Is_Class_Wide_Type (T2)
534 and then not Is_Tag_Indeterminate (Rhs)
535 and then not Is_Dynamically_Tagged (Rhs)
537 Error_Msg_N ("dynamically tagged expression required!", Rhs);
540 -- Propagate the tag from a class-wide target to the rhs when the rhs
541 -- is a tag-indeterminate call.
543 if Is_Tag_Indeterminate (Rhs) then
544 if Is_Class_Wide_Type (T1) then
545 Propagate_Tag (Lhs, Rhs);
547 elsif Nkind (Rhs) = N_Function_Call
548 and then Is_Entity_Name (Name (Rhs))
549 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
552 ("call to abstract function must be dispatching", Name (Rhs));
554 elsif Nkind (Rhs) = N_Qualified_Expression
555 and then Nkind (Expression (Rhs)) = N_Function_Call
556 and then Is_Entity_Name (Name (Expression (Rhs)))
558 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
561 ("call to abstract function must be dispatching",
562 Name (Expression (Rhs)));
566 -- Ada 2005 (AI-230 and AI-385): When the lhs type is an anonymous
567 -- access type, apply an implicit conversion of the rhs to that type
568 -- to force appropriate static and run-time accessibility checks.
570 if Ada_Version >= Ada_05
571 and then Ekind (T1) = E_Anonymous_Access_Type
573 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
574 Analyze_And_Resolve (Rhs, T1);
577 -- Ada 2005 (AI-231): Assignment to not null variable
579 if Ada_Version >= Ada_05
580 and then Can_Never_Be_Null (T1)
581 and then not Assignment_OK (Lhs)
583 -- Case where we know the right hand side is null
585 if Known_Null (Rhs) then
586 Apply_Compile_Time_Constraint_Error
588 Msg => "(Ada 2005) null not allowed in null-excluding objects?",
589 Reason => CE_Null_Not_Allowed);
591 -- We still mark this as a possible modification, that's necessary
592 -- to reset Is_True_Constant, and desirable for xref purposes.
594 Note_Possible_Modification (Lhs);
597 -- If we know the right hand side is non-null, then we convert to the
598 -- target type, since we don't need a run time check in that case.
600 elsif not Can_Never_Be_Null (T2) then
601 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
602 Analyze_And_Resolve (Rhs, T1);
606 if Is_Scalar_Type (T1) then
607 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
609 -- For array types, verify that lengths match. If the right hand side
610 -- if a function call that has been inlined, the assignment has been
611 -- rewritten as a block, and the constraint check will be applied to the
612 -- assignment within the block.
614 elsif Is_Array_Type (T1)
616 (Nkind (Rhs) /= N_Type_Conversion
617 or else Is_Constrained (Etype (Rhs)))
619 (Nkind (Rhs) /= N_Function_Call
620 or else Nkind (N) /= N_Block_Statement)
622 -- Assignment verifies that the length of the Lsh and Rhs are equal,
623 -- but of course the indices do not have to match. If the right-hand
624 -- side is a type conversion to an unconstrained type, a length check
625 -- is performed on the expression itself during expansion. In rare
626 -- cases, the redundant length check is computed on an index type
627 -- with a different representation, triggering incorrect code in
630 Apply_Length_Check (Rhs, Etype (Lhs));
633 -- Discriminant checks are applied in the course of expansion
638 -- Note: modifications of the Lhs may only be recorded after
639 -- checks have been applied.
641 Note_Possible_Modification (Lhs);
643 -- ??? a real accessibility check is needed when ???
645 -- Post warning for redundant assignment or variable to itself
647 if Warn_On_Redundant_Constructs
649 -- We only warn for source constructs
651 and then Comes_From_Source (N)
653 -- Where the object is the same on both sides
655 and then Same_Object (Lhs, Original_Node (Rhs))
657 -- But exclude the case where the right side was an operation
658 -- that got rewritten (e.g. JUNK + K, where K was known to be
659 -- zero). We don't want to warn in such a case, since it is
660 -- reasonable to write such expressions especially when K is
661 -- defined symbolically in some other package.
663 and then Nkind (Original_Node (Rhs)) not in N_Op
665 if Nkind (Lhs) in N_Has_Entity then
667 ("?useless assignment of & to itself!", N, Entity (Lhs));
670 ("?useless assignment of object to itself!", N);
674 -- Check for non-allowed composite assignment
676 if not Support_Composite_Assign_On_Target
677 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
678 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
680 Error_Msg_CRT ("composite assignment", N);
683 -- Check elaboration warning for left side if not in elab code
685 if not In_Subprogram_Or_Concurrent_Unit then
686 Check_Elab_Assign (Lhs);
689 -- Final step. If left side is an entity, then we may be able to
690 -- reset the current tracked values to new safe values. We only have
691 -- something to do if the left side is an entity name, and expansion
692 -- has not modified the node into something other than an assignment,
693 -- and of course we only capture values if it is safe to do so.
695 if Is_Entity_Name (Lhs)
696 and then Nkind (N) = N_Assignment_Statement
699 Ent : constant Entity_Id := Entity (Lhs);
702 if Safe_To_Capture_Value (N, Ent) then
704 -- If simple variable on left side, warn if this assignment
705 -- blots out another one (rendering it useless) and note
706 -- location of assignment in case no one references value.
707 -- We only do this for source assignments, otherwise we can
708 -- generate bogus warnings when an assignment is rewritten as
709 -- another assignment, and gets tied up with itself.
711 if Warn_On_Modified_Unread
712 and then Ekind (Ent) = E_Variable
713 and then Comes_From_Source (N)
714 and then In_Extended_Main_Source_Unit (Ent)
716 Warn_On_Useless_Assignment (Ent, Sloc (N));
717 Set_Last_Assignment (Ent, Lhs);
720 -- If we are assigning an access type and the left side is an
721 -- entity, then make sure that the Is_Known_[Non_]Null flags
722 -- properly reflect the state of the entity after assignment.
724 if Is_Access_Type (T1) then
725 if Known_Non_Null (Rhs) then
726 Set_Is_Known_Non_Null (Ent, True);
728 elsif Known_Null (Rhs)
729 and then not Can_Never_Be_Null (Ent)
731 Set_Is_Known_Null (Ent, True);
734 Set_Is_Known_Null (Ent, False);
736 if not Can_Never_Be_Null (Ent) then
737 Set_Is_Known_Non_Null (Ent, False);
741 -- For discrete types, we may be able to set the current value
742 -- if the value is known at compile time.
744 elsif Is_Discrete_Type (T1)
745 and then Compile_Time_Known_Value (Rhs)
747 Set_Current_Value (Ent, Rhs);
749 Set_Current_Value (Ent, Empty);
752 -- If not safe to capture values, kill them
759 end Analyze_Assignment;
761 -----------------------------
762 -- Analyze_Block_Statement --
763 -----------------------------
765 procedure Analyze_Block_Statement (N : Node_Id) is
766 Decls : constant List_Id := Declarations (N);
767 Id : constant Node_Id := Identifier (N);
768 HSS : constant Node_Id := Handled_Statement_Sequence (N);
771 -- If no handled statement sequence is present, things are really
772 -- messed up, and we just return immediately (this is a defence
773 -- against previous errors).
779 -- Normal processing with HSS present
782 EH : constant List_Id := Exception_Handlers (HSS);
783 Ent : Entity_Id := Empty;
786 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
787 -- Recursively save value of this global, will be restored on exit
790 -- Initialize unblocked exit count for statements of begin block
791 -- plus one for each excption handler that is present.
793 Unblocked_Exit_Count := 1;
796 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
799 -- If a label is present analyze it and mark it as referenced
805 -- An error defense. If we have an identifier, but no entity,
806 -- then something is wrong. If we have previous errors, then
807 -- just remove the identifier and continue, otherwise raise
811 if Total_Errors_Detected /= 0 then
812 Set_Identifier (N, Empty);
818 Set_Ekind (Ent, E_Block);
819 Generate_Reference (Ent, N, ' ');
820 Generate_Definition (Ent);
822 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
823 Set_Label_Construct (Parent (Ent), N);
828 -- If no entity set, create a label entity
831 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
832 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
836 Set_Etype (Ent, Standard_Void_Type);
837 Set_Block_Node (Ent, Identifier (N));
840 if Present (Decls) then
841 Analyze_Declarations (Decls);
846 Process_End_Label (HSS, 'e', Ent);
848 -- If exception handlers are present, then we indicate that
849 -- enclosing scopes contain a block with handlers. We only
850 -- need to mark non-generic scopes.
855 Set_Has_Nested_Block_With_Handler (S);
856 exit when Is_Overloadable (S)
857 or else Ekind (S) = E_Package
858 or else Is_Generic_Unit (S);
863 Check_References (Ent);
864 Warn_On_Useless_Assignments (Ent);
867 if Unblocked_Exit_Count = 0 then
868 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
869 Check_Unreachable_Code (N);
871 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
874 end Analyze_Block_Statement;
876 ----------------------------
877 -- Analyze_Case_Statement --
878 ----------------------------
880 procedure Analyze_Case_Statement (N : Node_Id) is
882 Exp_Type : Entity_Id;
883 Exp_Btype : Entity_Id;
886 Others_Present : Boolean;
888 Statements_Analyzed : Boolean := False;
889 -- Set True if at least some statement sequences get analyzed.
890 -- If False on exit, means we had a serious error that prevented
891 -- full analysis of the case statement, and as a result it is not
892 -- a good idea to output warning messages about unreachable code.
894 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
895 -- Recursively save value of this global, will be restored on exit
897 procedure Non_Static_Choice_Error (Choice : Node_Id);
898 -- Error routine invoked by the generic instantiation below when
899 -- the case statment has a non static choice.
901 procedure Process_Statements (Alternative : Node_Id);
902 -- Analyzes all the statements associated to a case alternative.
903 -- Needed by the generic instantiation below.
905 package Case_Choices_Processing is new
906 Generic_Choices_Processing
907 (Get_Alternatives => Alternatives,
908 Get_Choices => Discrete_Choices,
909 Process_Empty_Choice => No_OP,
910 Process_Non_Static_Choice => Non_Static_Choice_Error,
911 Process_Associated_Node => Process_Statements);
912 use Case_Choices_Processing;
913 -- Instantiation of the generic choice processing package
915 -----------------------------
916 -- Non_Static_Choice_Error --
917 -----------------------------
919 procedure Non_Static_Choice_Error (Choice : Node_Id) is
922 ("choice given in case statement is not static!", Choice);
923 end Non_Static_Choice_Error;
925 ------------------------
926 -- Process_Statements --
927 ------------------------
929 procedure Process_Statements (Alternative : Node_Id) is
930 Choices : constant List_Id := Discrete_Choices (Alternative);
934 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
935 Statements_Analyzed := True;
937 -- An interesting optimization. If the case statement expression
938 -- is a simple entity, then we can set the current value within
939 -- an alternative if the alternative has one possible value.
943 -- when 2 | 3 => beta
944 -- when others => gamma
946 -- Here we know that N is initially 1 within alpha, but for beta
947 -- and gamma, we do not know anything more about the initial value.
949 if Is_Entity_Name (Exp) then
952 if Ekind (Ent) = E_Variable
954 Ekind (Ent) = E_In_Out_Parameter
956 Ekind (Ent) = E_Out_Parameter
958 if List_Length (Choices) = 1
959 and then Nkind (First (Choices)) in N_Subexpr
960 and then Compile_Time_Known_Value (First (Choices))
962 Set_Current_Value (Entity (Exp), First (Choices));
965 Analyze_Statements (Statements (Alternative));
967 -- After analyzing the case, set the current value to empty
968 -- since we won't know what it is for the next alternative
969 -- (unless reset by this same circuit), or after the case.
971 Set_Current_Value (Entity (Exp), Empty);
976 -- Case where expression is not an entity name of a variable
978 Analyze_Statements (Statements (Alternative));
979 end Process_Statements;
981 -- Table to record choices. Put after subprograms since we make
982 -- a call to Number_Of_Choices to get the right number of entries.
984 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
986 -- Start of processing for Analyze_Case_Statement
989 Unblocked_Exit_Count := 0;
990 Exp := Expression (N);
993 -- The expression must be of any discrete type. In rare cases, the
994 -- expander constructs a case statement whose expression has a private
995 -- type whose full view is discrete. This can happen when generating
996 -- a stream operation for a variant type after the type is frozen,
997 -- when the partial of view of the type of the discriminant is private.
998 -- In that case, use the full view to analyze case alternatives.
1000 if not Is_Overloaded (Exp)
1001 and then not Comes_From_Source (N)
1002 and then Is_Private_Type (Etype (Exp))
1003 and then Present (Full_View (Etype (Exp)))
1004 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1006 Resolve (Exp, Etype (Exp));
1007 Exp_Type := Full_View (Etype (Exp));
1010 Analyze_And_Resolve (Exp, Any_Discrete);
1011 Exp_Type := Etype (Exp);
1014 Check_Unset_Reference (Exp);
1015 Exp_Btype := Base_Type (Exp_Type);
1017 -- The expression must be of a discrete type which must be determinable
1018 -- independently of the context in which the expression occurs, but
1019 -- using the fact that the expression must be of a discrete type.
1020 -- Moreover, the type this expression must not be a character literal
1021 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1023 -- If error already reported by Resolve, nothing more to do
1025 if Exp_Btype = Any_Discrete
1026 or else Exp_Btype = Any_Type
1030 elsif Exp_Btype = Any_Character then
1032 ("character literal as case expression is ambiguous", Exp);
1035 elsif Ada_Version = Ada_83
1036 and then (Is_Generic_Type (Exp_Btype)
1037 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1040 ("(Ada 83) case expression cannot be of a generic type", Exp);
1044 -- If the case expression is a formal object of mode in out, then
1045 -- treat it as having a nonstatic subtype by forcing use of the base
1046 -- type (which has to get passed to Check_Case_Choices below). Also
1047 -- use base type when the case expression is parenthesized.
1049 if Paren_Count (Exp) > 0
1050 or else (Is_Entity_Name (Exp)
1051 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1053 Exp_Type := Exp_Btype;
1056 -- Call instantiated Analyze_Choices which does the rest of the work
1059 (N, Exp_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
1061 if Exp_Type = Universal_Integer and then not Others_Present then
1062 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1065 -- If all our exits were blocked by unconditional transfers of control,
1066 -- then the entire CASE statement acts as an unconditional transfer of
1067 -- control, so treat it like one, and check unreachable code. Skip this
1068 -- test if we had serious errors preventing any statement analysis.
1070 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1071 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1072 Check_Unreachable_Code (N);
1074 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1077 if not Expander_Active
1078 and then Compile_Time_Known_Value (Expression (N))
1079 and then Serious_Errors_Detected = 0
1082 Chosen : constant Node_Id := Find_Static_Alternative (N);
1086 Alt := First (Alternatives (N));
1087 while Present (Alt) loop
1088 if Alt /= Chosen then
1089 Remove_Warning_Messages (Statements (Alt));
1096 end Analyze_Case_Statement;
1098 ----------------------------
1099 -- Analyze_Exit_Statement --
1100 ----------------------------
1102 -- If the exit includes a name, it must be the name of a currently open
1103 -- loop. Otherwise there must be an innermost open loop on the stack,
1104 -- to which the statement implicitly refers.
1106 procedure Analyze_Exit_Statement (N : Node_Id) is
1107 Target : constant Node_Id := Name (N);
1108 Cond : constant Node_Id := Condition (N);
1109 Scope_Id : Entity_Id;
1115 Check_Unreachable_Code (N);
1118 if Present (Target) then
1120 U_Name := Entity (Target);
1122 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1123 Error_Msg_N ("invalid loop name in exit statement", N);
1126 Set_Has_Exit (U_Name);
1133 for J in reverse 0 .. Scope_Stack.Last loop
1134 Scope_Id := Scope_Stack.Table (J).Entity;
1135 Kind := Ekind (Scope_Id);
1138 and then (No (Target) or else Scope_Id = U_Name) then
1139 Set_Has_Exit (Scope_Id);
1142 elsif Kind = E_Block
1143 or else Kind = E_Loop
1144 or else Kind = E_Return_Statement
1150 ("cannot exit from program unit or accept statement", N);
1155 -- Verify that if present the condition is a Boolean expression
1157 if Present (Cond) then
1158 Analyze_And_Resolve (Cond, Any_Boolean);
1159 Check_Unset_Reference (Cond);
1161 end Analyze_Exit_Statement;
1163 ----------------------------
1164 -- Analyze_Goto_Statement --
1165 ----------------------------
1167 procedure Analyze_Goto_Statement (N : Node_Id) is
1168 Label : constant Node_Id := Name (N);
1169 Scope_Id : Entity_Id;
1170 Label_Scope : Entity_Id;
1171 Label_Ent : Entity_Id;
1174 Check_Unreachable_Code (N);
1177 Label_Ent := Entity (Label);
1179 -- Ignore previous error
1181 if Label_Ent = Any_Id then
1184 -- We just have a label as the target of a goto
1186 elsif Ekind (Label_Ent) /= E_Label then
1187 Error_Msg_N ("target of goto statement must be a label", Label);
1190 -- Check that the target of the goto is reachable according to Ada
1191 -- scoping rules. Note: the special gotos we generate for optimizing
1192 -- local handling of exceptions would violate these rules, but we mark
1193 -- such gotos as analyzed when built, so this code is never entered.
1195 elsif not Reachable (Label_Ent) then
1196 Error_Msg_N ("target of goto statement is not reachable", Label);
1200 -- Here if goto passes initial validity checks
1202 Label_Scope := Enclosing_Scope (Label_Ent);
1204 for J in reverse 0 .. Scope_Stack.Last loop
1205 Scope_Id := Scope_Stack.Table (J).Entity;
1207 if Label_Scope = Scope_Id
1208 or else (Ekind (Scope_Id) /= E_Block
1209 and then Ekind (Scope_Id) /= E_Loop
1210 and then Ekind (Scope_Id) /= E_Return_Statement)
1212 if Scope_Id /= Label_Scope then
1214 ("cannot exit from program unit or accept statement", N);
1221 raise Program_Error;
1222 end Analyze_Goto_Statement;
1224 --------------------------
1225 -- Analyze_If_Statement --
1226 --------------------------
1228 -- A special complication arises in the analysis of if statements
1230 -- The expander has circuitry to completely delete code that it
1231 -- can tell will not be executed (as a result of compile time known
1232 -- conditions). In the analyzer, we ensure that code that will be
1233 -- deleted in this manner is analyzed but not expanded. This is
1234 -- obviously more efficient, but more significantly, difficulties
1235 -- arise if code is expanded and then eliminated (e.g. exception
1236 -- table entries disappear). Similarly, itypes generated in deleted
1237 -- code must be frozen from start, because the nodes on which they
1238 -- depend will not be available at the freeze point.
1240 procedure Analyze_If_Statement (N : Node_Id) is
1243 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1244 -- Recursively save value of this global, will be restored on exit
1246 Save_In_Deleted_Code : Boolean;
1248 Del : Boolean := False;
1249 -- This flag gets set True if a True condition has been found,
1250 -- which means that remaining ELSE/ELSIF parts are deleted.
1252 procedure Analyze_Cond_Then (Cnode : Node_Id);
1253 -- This is applied to either the N_If_Statement node itself or
1254 -- to an N_Elsif_Part node. It deals with analyzing the condition
1255 -- and the THEN statements associated with it.
1257 -----------------------
1258 -- Analyze_Cond_Then --
1259 -----------------------
1261 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1262 Cond : constant Node_Id := Condition (Cnode);
1263 Tstm : constant List_Id := Then_Statements (Cnode);
1266 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1267 Analyze_And_Resolve (Cond, Any_Boolean);
1268 Check_Unset_Reference (Cond);
1269 Set_Current_Value_Condition (Cnode);
1271 -- If already deleting, then just analyze then statements
1274 Analyze_Statements (Tstm);
1276 -- Compile time known value, not deleting yet
1278 elsif Compile_Time_Known_Value (Cond) then
1279 Save_In_Deleted_Code := In_Deleted_Code;
1281 -- If condition is True, then analyze the THEN statements
1282 -- and set no expansion for ELSE and ELSIF parts.
1284 if Is_True (Expr_Value (Cond)) then
1285 Analyze_Statements (Tstm);
1287 Expander_Mode_Save_And_Set (False);
1288 In_Deleted_Code := True;
1290 -- If condition is False, analyze THEN with expansion off
1292 else -- Is_False (Expr_Value (Cond))
1293 Expander_Mode_Save_And_Set (False);
1294 In_Deleted_Code := True;
1295 Analyze_Statements (Tstm);
1296 Expander_Mode_Restore;
1297 In_Deleted_Code := Save_In_Deleted_Code;
1300 -- Not known at compile time, not deleting, normal analysis
1303 Analyze_Statements (Tstm);
1305 end Analyze_Cond_Then;
1307 -- Start of Analyze_If_Statement
1310 -- Initialize exit count for else statements. If there is no else
1311 -- part, this count will stay non-zero reflecting the fact that the
1312 -- uncovered else case is an unblocked exit.
1314 Unblocked_Exit_Count := 1;
1315 Analyze_Cond_Then (N);
1317 -- Now to analyze the elsif parts if any are present
1319 if Present (Elsif_Parts (N)) then
1320 E := First (Elsif_Parts (N));
1321 while Present (E) loop
1322 Analyze_Cond_Then (E);
1327 if Present (Else_Statements (N)) then
1328 Analyze_Statements (Else_Statements (N));
1331 -- If all our exits were blocked by unconditional transfers of control,
1332 -- then the entire IF statement acts as an unconditional transfer of
1333 -- control, so treat it like one, and check unreachable code.
1335 if Unblocked_Exit_Count = 0 then
1336 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1337 Check_Unreachable_Code (N);
1339 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1343 Expander_Mode_Restore;
1344 In_Deleted_Code := Save_In_Deleted_Code;
1347 if not Expander_Active
1348 and then Compile_Time_Known_Value (Condition (N))
1349 and then Serious_Errors_Detected = 0
1351 if Is_True (Expr_Value (Condition (N))) then
1352 Remove_Warning_Messages (Else_Statements (N));
1354 if Present (Elsif_Parts (N)) then
1355 E := First (Elsif_Parts (N));
1356 while Present (E) loop
1357 Remove_Warning_Messages (Then_Statements (E));
1363 Remove_Warning_Messages (Then_Statements (N));
1366 end Analyze_If_Statement;
1368 ----------------------------------------
1369 -- Analyze_Implicit_Label_Declaration --
1370 ----------------------------------------
1372 -- An implicit label declaration is generated in the innermost
1373 -- enclosing declarative part. This is done for labels as well as
1374 -- block and loop names.
1376 -- Note: any changes in this routine may need to be reflected in
1377 -- Analyze_Label_Entity.
1379 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1380 Id : constant Node_Id := Defining_Identifier (N);
1383 Set_Ekind (Id, E_Label);
1384 Set_Etype (Id, Standard_Void_Type);
1385 Set_Enclosing_Scope (Id, Current_Scope);
1386 end Analyze_Implicit_Label_Declaration;
1388 ------------------------------
1389 -- Analyze_Iteration_Scheme --
1390 ------------------------------
1392 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1394 procedure Process_Bounds (R : Node_Id);
1395 -- If the iteration is given by a range, create temporaries and
1396 -- assignment statements block to capture the bounds and perform
1397 -- required finalization actions in case a bound includes a function
1398 -- call that uses the temporary stack. We first pre-analyze a copy of
1399 -- the range in order to determine the expected type, and analyze and
1400 -- resolve the original bounds.
1402 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1403 -- If the bounds are given by a 'Range reference on a function call
1404 -- that returns a controlled array, introduce an explicit declaration
1405 -- to capture the bounds, so that the function result can be finalized
1406 -- in timely fashion.
1408 --------------------
1409 -- Process_Bounds --
1410 --------------------
1412 procedure Process_Bounds (R : Node_Id) is
1413 Loc : constant Source_Ptr := Sloc (N);
1414 R_Copy : constant Node_Id := New_Copy_Tree (R);
1415 Lo : constant Node_Id := Low_Bound (R);
1416 Hi : constant Node_Id := High_Bound (R);
1417 New_Lo_Bound : Node_Id := Empty;
1418 New_Hi_Bound : Node_Id := Empty;
1420 Save_Analysis : Boolean;
1423 (Original_Bound : Node_Id;
1424 Analyzed_Bound : Node_Id) return Node_Id;
1425 -- Create one declaration followed by one assignment statement
1426 -- to capture the value of bound. We create a separate assignment
1427 -- in order to force the creation of a block in case the bound
1428 -- contains a call that uses the secondary stack.
1435 (Original_Bound : Node_Id;
1436 Analyzed_Bound : Node_Id) return Node_Id
1443 -- If the bound is a constant or an object, no need for a separate
1444 -- declaration. If the bound is the result of previous expansion
1445 -- it is already analyzed and should not be modified. Note that
1446 -- the Bound will be resolved later, if needed, as part of the
1447 -- call to Make_Index (literal bounds may need to be resolved to
1450 if Analyzed (Original_Bound) then
1451 return Original_Bound;
1453 elsif Nkind (Analyzed_Bound) = N_Integer_Literal
1454 or else Nkind (Analyzed_Bound) = N_Character_Literal
1455 or else Is_Entity_Name (Analyzed_Bound)
1457 Analyze_And_Resolve (Original_Bound, Typ);
1458 return Original_Bound;
1461 Analyze_And_Resolve (Original_Bound, Typ);
1465 Make_Defining_Identifier (Loc,
1466 Chars => New_Internal_Name ('S'));
1469 Make_Object_Declaration (Loc,
1470 Defining_Identifier => Id,
1471 Object_Definition => New_Occurrence_Of (Typ, Loc));
1473 Insert_Before (Parent (N), Decl);
1477 Make_Assignment_Statement (Loc,
1478 Name => New_Occurrence_Of (Id, Loc),
1479 Expression => Relocate_Node (Original_Bound));
1481 Insert_Before (Parent (N), Assign);
1484 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
1486 if Nkind (Assign) = N_Assignment_Statement then
1487 return Expression (Assign);
1489 return Original_Bound;
1493 -- Start of processing for Process_Bounds
1496 -- Determine expected type of range by analyzing separate copy
1497 -- Do the analysis and resolution of the copy of the bounds with
1498 -- expansion disabled, to prevent the generation of finalization
1499 -- actions on each bound. This prevents memory leaks when the
1500 -- bounds contain calls to functions returning controlled arrays.
1502 Set_Parent (R_Copy, Parent (R));
1503 Save_Analysis := Full_Analysis;
1504 Full_Analysis := False;
1505 Expander_Mode_Save_And_Set (False);
1509 if Is_Overloaded (R_Copy) then
1511 -- Apply preference rules for range of predefined integer types,
1512 -- or diagnose true ambiguity.
1517 Found : Entity_Id := Empty;
1520 Get_First_Interp (R_Copy, I, It);
1521 while Present (It.Typ) loop
1522 if Is_Discrete_Type (It.Typ) then
1526 if Scope (Found) = Standard_Standard then
1529 elsif Scope (It.Typ) = Standard_Standard then
1533 -- Both of them are user-defined
1536 ("ambiguous bounds in range of iteration",
1538 Error_Msg_N ("\possible interpretations:", R_Copy);
1539 Error_Msg_NE ("\\} ", R_Copy, Found);
1540 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
1546 Get_Next_Interp (I, It);
1552 Expander_Mode_Restore;
1553 Full_Analysis := Save_Analysis;
1555 Typ := Etype (R_Copy);
1557 -- If the type of the discrete range is Universal_Integer, then
1558 -- the bound's type must be resolved to Integer, and any object
1559 -- used to hold the bound must also have type Integer, unless the
1560 -- literal bounds are constant-folded expressions that carry a user-
1563 if Typ = Universal_Integer then
1564 if Nkind (Lo) = N_Integer_Literal
1565 and then Present (Etype (Lo))
1566 and then Scope (Etype (Lo)) /= Standard_Standard
1570 elsif Nkind (Hi) = N_Integer_Literal
1571 and then Present (Etype (Hi))
1572 and then Scope (Etype (Hi)) /= Standard_Standard
1577 Typ := Standard_Integer;
1583 New_Lo_Bound := One_Bound (Lo, Low_Bound (R_Copy));
1584 New_Hi_Bound := One_Bound (Hi, High_Bound (R_Copy));
1586 -- Propagate staticness to loop range itself, in case the
1587 -- corresponding subtype is static.
1589 if New_Lo_Bound /= Lo
1590 and then Is_Static_Expression (New_Lo_Bound)
1592 Rewrite (Low_Bound (R), New_Copy (New_Lo_Bound));
1595 if New_Hi_Bound /= Hi
1596 and then Is_Static_Expression (New_Hi_Bound)
1598 Rewrite (High_Bound (R), New_Copy (New_Hi_Bound));
1602 --------------------------------------
1603 -- Check_Controlled_Array_Attribute --
1604 --------------------------------------
1606 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1608 if Nkind (DS) = N_Attribute_Reference
1609 and then Is_Entity_Name (Prefix (DS))
1610 and then Ekind (Entity (Prefix (DS))) = E_Function
1611 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1614 Component_Type (Etype (Entity (Prefix (DS)))))
1615 and then Expander_Active
1618 Loc : constant Source_Ptr := Sloc (N);
1619 Arr : constant Entity_Id :=
1620 Etype (Entity (Prefix (DS)));
1621 Indx : constant Entity_Id :=
1622 Base_Type (Etype (First_Index (Arr)));
1623 Subt : constant Entity_Id :=
1624 Make_Defining_Identifier
1625 (Loc, New_Internal_Name ('S'));
1630 Make_Subtype_Declaration (Loc,
1631 Defining_Identifier => Subt,
1632 Subtype_Indication =>
1633 Make_Subtype_Indication (Loc,
1634 Subtype_Mark => New_Reference_To (Indx, Loc),
1636 Make_Range_Constraint (Loc,
1637 Relocate_Node (DS))));
1638 Insert_Before (Parent (N), Decl);
1642 Make_Attribute_Reference (Loc,
1643 Prefix => New_Reference_To (Subt, Loc),
1644 Attribute_Name => Attribute_Name (DS)));
1648 end Check_Controlled_Array_Attribute;
1650 -- Start of processing for Analyze_Iteration_Scheme
1653 -- For an infinite loop, there is no iteration scheme
1660 Cond : constant Node_Id := Condition (N);
1663 -- For WHILE loop, verify that the condition is a Boolean
1664 -- expression and resolve and check it.
1666 if Present (Cond) then
1667 Analyze_And_Resolve (Cond, Any_Boolean);
1668 Check_Unset_Reference (Cond);
1669 Set_Current_Value_Condition (N);
1672 -- Else we have a FOR loop
1676 LP : constant Node_Id := Loop_Parameter_Specification (N);
1677 Id : constant Entity_Id := Defining_Identifier (LP);
1678 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
1683 -- We always consider the loop variable to be referenced,
1684 -- since the loop may be used just for counting purposes.
1686 Generate_Reference (Id, N, ' ');
1688 -- Check for case of loop variable hiding a local
1689 -- variable (used later on to give a nice warning
1690 -- if the hidden variable is never assigned).
1693 H : constant Entity_Id := Homonym (Id);
1696 and then Enclosing_Dynamic_Scope (H) =
1697 Enclosing_Dynamic_Scope (Id)
1698 and then Ekind (H) = E_Variable
1699 and then Is_Discrete_Type (Etype (H))
1701 Set_Hiding_Loop_Variable (H, Id);
1705 -- Now analyze the subtype definition. If it is
1706 -- a range, create temporaries for bounds.
1708 if Nkind (DS) = N_Range
1709 and then Expander_Active
1711 Process_Bounds (DS);
1720 -- The subtype indication may denote the completion
1721 -- of an incomplete type declaration.
1723 if Is_Entity_Name (DS)
1724 and then Present (Entity (DS))
1725 and then Is_Type (Entity (DS))
1726 and then Ekind (Entity (DS)) = E_Incomplete_Type
1728 Set_Entity (DS, Get_Full_View (Entity (DS)));
1729 Set_Etype (DS, Entity (DS));
1732 if not Is_Discrete_Type (Etype (DS)) then
1733 Wrong_Type (DS, Any_Discrete);
1734 Set_Etype (DS, Any_Type);
1737 Check_Controlled_Array_Attribute (DS);
1739 Make_Index (DS, LP);
1741 Set_Ekind (Id, E_Loop_Parameter);
1742 Set_Etype (Id, Etype (DS));
1743 Set_Is_Known_Valid (Id, True);
1745 -- The loop is not a declarative part, so the only entity
1746 -- declared "within" must be frozen explicitly.
1749 Flist : constant List_Id := Freeze_Entity (Id, Sloc (N));
1751 if Is_Non_Empty_List (Flist) then
1752 Insert_Actions (N, Flist);
1756 -- Check for null or possibly null range and issue warning.
1757 -- We suppress such messages in generic templates and
1758 -- instances, because in practice they tend to be dubious
1761 if Nkind (DS) = N_Range
1762 and then Comes_From_Source (N)
1765 L : constant Node_Id := Low_Bound (DS);
1766 H : constant Node_Id := High_Bound (DS);
1776 Determine_Range (L, LOK, Llo, Lhi);
1777 Determine_Range (H, HOK, Hlo, Hhi);
1779 -- If range of loop is null, issue warning
1781 if (LOK and HOK) and then Llo > Hhi then
1783 -- Suppress the warning if inside a generic
1784 -- template or instance, since in practice
1785 -- they tend to be dubious in these cases since
1786 -- they can result from intended parametrization.
1788 if not Inside_A_Generic
1789 and then not In_Instance
1792 ("?loop range is null, loop will not execute",
1796 -- Since we know the range of the loop is null,
1797 -- set the appropriate flag to suppress any
1798 -- warnings that would otherwise be issued in
1799 -- the body of the loop that will not execute.
1800 -- We do this even in the generic case, since
1801 -- if it is dubious to warn on the null loop
1802 -- itself, it is certainly dubious to warn for
1803 -- conditions that occur inside it!
1805 Set_Is_Null_Loop (Parent (N));
1807 -- The other case for a warning is a reverse loop
1808 -- where the upper bound is the integer literal
1809 -- zero or one, and the lower bound can be positive.
1811 -- For example, we have
1813 -- for J in reverse N .. 1 loop
1815 -- In practice, this is very likely to be a case
1816 -- of reversing the bounds incorrectly in the range.
1818 elsif Reverse_Present (LP)
1819 and then Nkind (Original_Node (H)) =
1821 and then (Intval (H) = Uint_0
1823 Intval (H) = Uint_1)
1826 Error_Msg_N ("?loop range may be null", DS);
1827 Error_Msg_N ("\?bounds may be wrong way round", DS);
1835 end Analyze_Iteration_Scheme;
1841 -- Note: the semantic work required for analyzing labels (setting them as
1842 -- reachable) was done in a prepass through the statements in the block,
1843 -- so that forward gotos would be properly handled. See Analyze_Statements
1844 -- for further details. The only processing required here is to deal with
1845 -- optimizations that depend on an assumption of sequential control flow,
1846 -- since of course the occurrence of a label breaks this assumption.
1848 procedure Analyze_Label (N : Node_Id) is
1849 pragma Warnings (Off, N);
1851 Kill_Current_Values;
1854 --------------------------
1855 -- Analyze_Label_Entity --
1856 --------------------------
1858 procedure Analyze_Label_Entity (E : Entity_Id) is
1860 Set_Ekind (E, E_Label);
1861 Set_Etype (E, Standard_Void_Type);
1862 Set_Enclosing_Scope (E, Current_Scope);
1863 Set_Reachable (E, True);
1864 end Analyze_Label_Entity;
1866 ----------------------------
1867 -- Analyze_Loop_Statement --
1868 ----------------------------
1870 procedure Analyze_Loop_Statement (N : Node_Id) is
1871 Loop_Statement : constant Node_Id := N;
1873 Id : constant Node_Id := Identifier (Loop_Statement);
1874 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
1878 if Present (Id) then
1880 -- Make name visible, e.g. for use in exit statements. Loop
1881 -- labels are always considered to be referenced.
1885 Generate_Reference (Ent, Loop_Statement, ' ');
1886 Generate_Definition (Ent);
1888 -- If we found a label, mark its type. If not, ignore it, since it
1889 -- means we have a conflicting declaration, which would already have
1890 -- been diagnosed at declaration time. Set Label_Construct of the
1891 -- implicit label declaration, which is not created by the parser
1892 -- for generic units.
1894 if Ekind (Ent) = E_Label then
1895 Set_Ekind (Ent, E_Loop);
1897 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1898 Set_Label_Construct (Parent (Ent), Loop_Statement);
1902 -- Case of no identifier present
1907 (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
1908 Set_Etype (Ent, Standard_Void_Type);
1909 Set_Parent (Ent, Loop_Statement);
1912 -- Kill current values on entry to loop, since statements in body
1913 -- of loop may have been executed before the loop is entered.
1914 -- Similarly we kill values after the loop, since we do not know
1915 -- that the body of the loop was executed.
1917 Kill_Current_Values;
1919 Analyze_Iteration_Scheme (Iter);
1920 Analyze_Statements (Statements (Loop_Statement));
1921 Process_End_Label (Loop_Statement, 'e', Ent);
1923 Kill_Current_Values;
1924 Check_Infinite_Loop_Warning (N);
1925 end Analyze_Loop_Statement;
1927 ----------------------------
1928 -- Analyze_Null_Statement --
1929 ----------------------------
1931 -- Note: the semantics of the null statement is implemented by a single
1932 -- null statement, too bad everything isn't as simple as this!
1934 procedure Analyze_Null_Statement (N : Node_Id) is
1935 pragma Warnings (Off, N);
1938 end Analyze_Null_Statement;
1940 ------------------------
1941 -- Analyze_Statements --
1942 ------------------------
1944 procedure Analyze_Statements (L : List_Id) is
1949 -- The labels declared in the statement list are reachable from
1950 -- statements in the list. We do this as a prepass so that any
1951 -- goto statement will be properly flagged if its target is not
1952 -- reachable. This is not required, but is nice behavior!
1955 while Present (S) loop
1956 if Nkind (S) = N_Label then
1957 Analyze (Identifier (S));
1958 Lab := Entity (Identifier (S));
1960 -- If we found a label mark it as reachable
1962 if Ekind (Lab) = E_Label then
1963 Generate_Definition (Lab);
1964 Set_Reachable (Lab);
1966 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
1967 Set_Label_Construct (Parent (Lab), S);
1970 -- If we failed to find a label, it means the implicit declaration
1971 -- of the label was hidden. A for-loop parameter can do this to
1972 -- a label with the same name inside the loop, since the implicit
1973 -- label declaration is in the innermost enclosing body or block
1977 Error_Msg_Sloc := Sloc (Lab);
1979 ("implicit label declaration for & is hidden#",
1987 -- Perform semantic analysis on all statements
1989 Conditional_Statements_Begin;
1992 while Present (S) loop
1997 Conditional_Statements_End;
1999 -- Make labels unreachable. Visibility is not sufficient, because
2000 -- labels in one if-branch for example are not reachable from the
2001 -- other branch, even though their declarations are in the enclosing
2002 -- declarative part.
2005 while Present (S) loop
2006 if Nkind (S) = N_Label then
2007 Set_Reachable (Entity (Identifier (S)), False);
2012 end Analyze_Statements;
2014 ----------------------------
2015 -- Check_Unreachable_Code --
2016 ----------------------------
2018 procedure Check_Unreachable_Code (N : Node_Id) is
2019 Error_Loc : Source_Ptr;
2023 if Is_List_Member (N)
2024 and then Comes_From_Source (N)
2030 Nxt := Original_Node (Next (N));
2032 -- If a label follows us, then we never have dead code, since
2033 -- someone could branch to the label, so we just ignore it.
2035 if Nkind (Nxt) = N_Label then
2038 -- Otherwise see if we have a real statement following us
2041 and then Comes_From_Source (Nxt)
2042 and then Is_Statement (Nxt)
2044 -- Special very annoying exception. If we have a return that
2045 -- follows a raise, then we allow it without a warning, since
2046 -- the Ada RM annoyingly requires a useless return here!
2048 if Nkind (Original_Node (N)) /= N_Raise_Statement
2049 or else Nkind (Nxt) /= N_Simple_Return_Statement
2051 -- The rather strange shenanigans with the warning message
2052 -- here reflects the fact that Kill_Dead_Code is very good
2053 -- at removing warnings in deleted code, and this is one
2054 -- warning we would prefer NOT to have removed.
2056 Error_Loc := Sloc (Nxt);
2058 -- If we have unreachable code, analyze and remove the
2059 -- unreachable code, since it is useless and we don't
2060 -- want to generate junk warnings.
2062 -- We skip this step if we are not in code generation mode.
2063 -- This is the one case where we remove dead code in the
2064 -- semantics as opposed to the expander, and we do not want
2065 -- to remove code if we are not in code generation mode,
2066 -- since this messes up the ASIS trees.
2068 -- Note that one might react by moving the whole circuit to
2069 -- exp_ch5, but then we lose the warning in -gnatc mode.
2071 if Operating_Mode = Generate_Code then
2075 -- Quit deleting when we have nothing more to delete
2076 -- or if we hit a label (since someone could transfer
2077 -- control to a label, so we should not delete it).
2079 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
2081 -- Statement/declaration is to be deleted
2085 Kill_Dead_Code (Nxt);
2089 -- Now issue the warning
2091 Error_Msg ("?unreachable code!", Error_Loc);
2094 -- If the unconditional transfer of control instruction is
2095 -- the last statement of a sequence, then see if our parent
2096 -- is one of the constructs for which we count unblocked exits,
2097 -- and if so, adjust the count.
2102 -- Statements in THEN part or ELSE part of IF statement
2104 if Nkind (P) = N_If_Statement then
2107 -- Statements in ELSIF part of an IF statement
2109 elsif Nkind (P) = N_Elsif_Part then
2111 pragma Assert (Nkind (P) = N_If_Statement);
2113 -- Statements in CASE statement alternative
2115 elsif Nkind (P) = N_Case_Statement_Alternative then
2117 pragma Assert (Nkind (P) = N_Case_Statement);
2119 -- Statements in body of block
2121 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
2122 and then Nkind (Parent (P)) = N_Block_Statement
2126 -- Statements in exception handler in a block
2128 elsif Nkind (P) = N_Exception_Handler
2129 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
2130 and then Nkind (Parent (Parent (P))) = N_Block_Statement
2134 -- None of these cases, so return
2140 -- This was one of the cases we are looking for (i.e. the
2141 -- parent construct was IF, CASE or block) so decrement count.
2143 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
2147 end Check_Unreachable_Code;