1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
36 with Lib.Xref; use Lib.Xref;
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
64 -- not blocked by unconditional transfer instructions: for IF and CASE,
65 -- these are the branches of the conditional; for a block, they are the
66 -- statement sequence of the block, and the statement sequences of any
67 -- exception handlers that are part of the block. When processing is
68 -- complete, if this count is zero, it means that control cannot fall
69 -- through the IF, CASE or block statement. This is used for the
70 -- generation of warning messages. This variable is recursively saved
71 -- on entry to processing the 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
256 -- Start type analysis for assignment
260 -- In the most general case, both Lhs and Rhs can be overloaded, and we
261 -- must compute the intersection of the possible types on each side.
263 if Is_Overloaded (Lhs) then
270 Get_First_Interp (Lhs, I, It);
272 while Present (It.Typ) loop
273 if Has_Compatible_Type (Rhs, It.Typ) then
274 if T1 /= Any_Type then
276 -- An explicit dereference is overloaded if the prefix
277 -- is. Try to remove the ambiguity on the prefix, the
278 -- error will be posted there if the ambiguity is real.
280 if Nkind (Lhs) = N_Explicit_Dereference then
283 PI1 : Interp_Index := 0;
289 Get_First_Interp (Prefix (Lhs), PI, PIt);
291 while Present (PIt.Typ) loop
292 if Is_Access_Type (PIt.Typ)
293 and then Has_Compatible_Type
294 (Rhs, Designated_Type (PIt.Typ))
298 Disambiguate (Prefix (Lhs),
301 if PIt = No_Interp then
303 ("ambiguous left-hand side"
304 & " in assignment", Lhs);
307 Resolve (Prefix (Lhs), PIt.Typ);
317 Get_Next_Interp (PI, PIt);
323 ("ambiguous left-hand side in assignment", Lhs);
331 Get_Next_Interp (I, It);
335 if T1 = Any_Type then
337 ("no valid types for left-hand side for assignment", Lhs);
345 if not Is_Variable (Lhs) then
347 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of
348 -- a protected object.
355 if Ada_Version >= Ada_05 then
357 -- Handle chains of renamings
360 while Nkind (Ent) in N_Has_Entity
361 and then Present (Entity (Ent))
362 and then Present (Renamed_Object (Entity (Ent)))
364 Ent := Renamed_Object (Entity (Ent));
367 if (Nkind (Ent) = N_Attribute_Reference
368 and then Attribute_Name (Ent) = Name_Priority)
370 -- Renamings of the attribute Priority applied to protected
371 -- objects have been previously expanded into calls to the
372 -- Get_Ceiling run-time subprogram.
375 (Nkind (Ent) = N_Function_Call
376 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
378 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
380 -- The enclosing subprogram cannot be a protected function
383 while not (Is_Subprogram (S)
384 and then Convention (S) = Convention_Protected)
385 and then S /= Standard_Standard
390 if Ekind (S) = E_Function
391 and then Convention (S) = Convention_Protected
394 ("protected function cannot modify protected object",
398 -- Changes of the ceiling priority of the protected object
399 -- are only effective if the Ceiling_Locking policy is in
400 -- effect (AARM D.5.2 (5/2)).
402 if Locking_Policy /= 'C' then
403 Error_Msg_N ("assignment to the attribute PRIORITY has " &
405 Error_Msg_N ("\since no Locking_Policy has been " &
414 Diagnose_Non_Variable_Lhs (Lhs);
417 elsif Is_Limited_Type (T1)
418 and then not Assignment_OK (Lhs)
419 and then not Assignment_OK (Original_Node (Lhs))
422 ("left hand of assignment must not be limited type", Lhs);
423 Explain_Limited_Type (T1, Lhs);
427 -- Resolution may have updated the subtype, in case the left-hand
428 -- side is a private protected component. Use the correct subtype
429 -- to avoid scoping issues in the back-end.
433 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
434 -- type. For example:
438 -- type Acc is access P.T;
441 -- with Pkg; use Acc;
442 -- procedure Example is
445 -- A.all := B.all; -- ERROR
448 if Nkind (Lhs) = N_Explicit_Dereference
449 and then Ekind (T1) = E_Incomplete_Type
451 Error_Msg_N ("invalid use of incomplete type", Lhs);
456 Set_Assignment_Type (Lhs, T1);
459 Check_Unset_Reference (Rhs);
461 -- Remaining steps are skipped if Rhs was syntactically in error
470 if not Covers (T1, T2) then
471 Wrong_Type (Rhs, Etype (Lhs));
476 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
477 -- types, use the non-limited view if available
479 if Nkind (Rhs) = N_Explicit_Dereference
480 and then Ekind (T2) = E_Incomplete_Type
481 and then Is_Tagged_Type (T2)
482 and then Present (Non_Limited_View (T2))
484 T2 := Non_Limited_View (T2);
487 Set_Assignment_Type (Rhs, T2);
489 if Total_Errors_Detected /= 0 then
499 if T1 = Any_Type or else T2 = Any_Type then
504 if (Is_Class_Wide_Type (T2) or else Is_Dynamically_Tagged (Rhs))
505 and then not Is_Class_Wide_Type (T1)
507 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
509 elsif Is_Class_Wide_Type (T1)
510 and then not Is_Class_Wide_Type (T2)
511 and then not Is_Tag_Indeterminate (Rhs)
512 and then not Is_Dynamically_Tagged (Rhs)
514 Error_Msg_N ("dynamically tagged expression required!", Rhs);
517 -- Propagate the tag from a class-wide target to the rhs when the rhs
518 -- is a tag-indeterminate call.
520 if Is_Class_Wide_Type (T1)
521 and then Is_Tag_Indeterminate (Rhs)
523 Propagate_Tag (Lhs, Rhs);
526 -- Ada 2005 (AI-230 and AI-385): When the lhs type is an anonymous
527 -- access type, apply an implicit conversion of the rhs to that type
528 -- to force appropriate static and run-time accessibility checks.
530 if Ada_Version >= Ada_05
531 and then Ekind (T1) = E_Anonymous_Access_Type
533 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
534 Analyze_And_Resolve (Rhs, T1);
539 if Ada_Version >= Ada_05
540 and then Can_Never_Be_Null (T1)
541 and then not Assignment_OK (Lhs)
543 if Nkind (Rhs) = N_Null then
544 Apply_Compile_Time_Constraint_Error
546 Msg => "(Ada 2005) NULL not allowed in null-excluding objects?",
547 Reason => CE_Null_Not_Allowed);
550 elsif not Can_Never_Be_Null (T2) then
552 Convert_To (T1, Relocate_Node (Rhs)));
553 Analyze_And_Resolve (Rhs, T1);
557 if Is_Scalar_Type (T1) then
558 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
560 -- For array types, verify that lengths match. If the right hand side
561 -- if a function call that has been inlined, the assignment has been
562 -- rewritten as a block, and the constraint check will be applied to the
563 -- assignment within the block.
565 elsif Is_Array_Type (T1)
567 (Nkind (Rhs) /= N_Type_Conversion
568 or else Is_Constrained (Etype (Rhs)))
570 (Nkind (Rhs) /= N_Function_Call
571 or else Nkind (N) /= N_Block_Statement)
573 -- Assignment verifies that the length of the Lsh and Rhs are equal,
574 -- but of course the indices do not have to match. If the right-hand
575 -- side is a type conversion to an unconstrained type, a length check
576 -- is performed on the expression itself during expansion. In rare
577 -- cases, the redundant length check is computed on an index type
578 -- with a different representation, triggering incorrect code in
581 Apply_Length_Check (Rhs, Etype (Lhs));
584 -- Discriminant checks are applied in the course of expansion
589 -- Note: modifications of the Lhs may only be recorded after
590 -- checks have been applied.
592 Note_Possible_Modification (Lhs);
594 -- ??? a real accessibility check is needed when ???
596 -- Post warning for redundant assignment or variable to itself
598 if Warn_On_Redundant_Constructs
600 -- We only warn for source constructs
602 and then Comes_From_Source (N)
604 -- Where the entity is the same on both sides
606 and then Is_Entity_Name (Lhs)
607 and then Is_Entity_Name (Original_Node (Rhs))
608 and then Entity (Lhs) = Entity (Original_Node (Rhs))
610 -- But exclude the case where the right side was an operation
611 -- that got rewritten (e.g. JUNK + K, where K was known to be
612 -- zero). We don't want to warn in such a case, since it is
613 -- reasonable to write such expressions especially when K is
614 -- defined symbolically in some other package.
616 and then Nkind (Original_Node (Rhs)) not in N_Op
619 ("?useless assignment of & to itself", N, Entity (Lhs));
622 -- Check for non-allowed composite assignment
624 if not Support_Composite_Assign_On_Target
625 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
626 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
628 Error_Msg_CRT ("composite assignment", N);
631 -- Check elaboration warning for left side if not in elab code
633 if not In_Subprogram_Or_Concurrent_Unit then
634 Check_Elab_Assign (Lhs);
637 -- Final step. If left side is an entity, then we may be able to
638 -- reset the current tracked values to new safe values. We only have
639 -- something to do if the left side is an entity name, and expansion
640 -- has not modified the node into something other than an assignment,
641 -- and of course we only capture values if it is safe to do so.
643 if Is_Entity_Name (Lhs)
644 and then Nkind (N) = N_Assignment_Statement
647 Ent : constant Entity_Id := Entity (Lhs);
650 if Safe_To_Capture_Value (N, Ent) then
652 -- If simple variable on left side, warn if this assignment
653 -- blots out another one (rendering it useless) and note
654 -- location of assignment in case no one references value.
655 -- We only do this for source assignments, otherwise we can
656 -- generate bogus warnings when an assignment is rewritten as
657 -- another assignment, and gets tied up with itself.
659 if Warn_On_Modified_Unread
660 and then Ekind (Ent) = E_Variable
661 and then Comes_From_Source (N)
662 and then In_Extended_Main_Source_Unit (Ent)
664 Warn_On_Useless_Assignment (Ent, Sloc (N));
665 Set_Last_Assignment (Ent, Lhs);
668 -- If we are assigning an access type and the left side is an
669 -- entity, then make sure that the Is_Known_[Non_]Null flags
670 -- properly reflect the state of the entity after assignment.
672 if Is_Access_Type (T1) then
673 if Known_Non_Null (Rhs) then
674 Set_Is_Known_Non_Null (Ent, True);
676 elsif Known_Null (Rhs)
677 and then not Can_Never_Be_Null (Ent)
679 Set_Is_Known_Null (Ent, True);
682 Set_Is_Known_Null (Ent, False);
684 if not Can_Never_Be_Null (Ent) then
685 Set_Is_Known_Non_Null (Ent, False);
689 -- For discrete types, we may be able to set the current value
690 -- if the value is known at compile time.
692 elsif Is_Discrete_Type (T1)
693 and then Compile_Time_Known_Value (Rhs)
695 Set_Current_Value (Ent, Rhs);
697 Set_Current_Value (Ent, Empty);
700 -- If not safe to capture values, kill them
707 end Analyze_Assignment;
709 -----------------------------
710 -- Analyze_Block_Statement --
711 -----------------------------
713 procedure Analyze_Block_Statement (N : Node_Id) is
714 Decls : constant List_Id := Declarations (N);
715 Id : constant Node_Id := Identifier (N);
716 HSS : constant Node_Id := Handled_Statement_Sequence (N);
719 -- If no handled statement sequence is present, things are really
720 -- messed up, and we just return immediately (this is a defence
721 -- against previous errors).
727 -- Normal processing with HSS present
730 EH : constant List_Id := Exception_Handlers (HSS);
731 Ent : Entity_Id := Empty;
734 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
735 -- Recursively save value of this global, will be restored on exit
738 -- Initialize unblocked exit count for statements of begin block
739 -- plus one for each excption handler that is present.
741 Unblocked_Exit_Count := 1;
744 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
747 -- If a label is present analyze it and mark it as referenced
753 -- An error defense. If we have an identifier, but no entity,
754 -- then something is wrong. If we have previous errors, then
755 -- just remove the identifier and continue, otherwise raise
759 if Total_Errors_Detected /= 0 then
760 Set_Identifier (N, Empty);
766 Set_Ekind (Ent, E_Block);
767 Generate_Reference (Ent, N, ' ');
768 Generate_Definition (Ent);
770 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
771 Set_Label_Construct (Parent (Ent), N);
776 -- If no entity set, create a label entity
779 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
780 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
784 Set_Etype (Ent, Standard_Void_Type);
785 Set_Block_Node (Ent, Identifier (N));
788 if Present (Decls) then
789 Analyze_Declarations (Decls);
794 Process_End_Label (HSS, 'e', Ent);
796 -- If exception handlers are present, then we indicate that
797 -- enclosing scopes contain a block with handlers. We only
798 -- need to mark non-generic scopes.
803 Set_Has_Nested_Block_With_Handler (S);
804 exit when Is_Overloadable (S)
805 or else Ekind (S) = E_Package
806 or else Is_Generic_Unit (S);
811 Check_References (Ent);
812 Warn_On_Useless_Assignments (Ent);
815 if Unblocked_Exit_Count = 0 then
816 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
817 Check_Unreachable_Code (N);
819 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
822 end Analyze_Block_Statement;
824 ----------------------------
825 -- Analyze_Case_Statement --
826 ----------------------------
828 procedure Analyze_Case_Statement (N : Node_Id) is
830 Exp_Type : Entity_Id;
831 Exp_Btype : Entity_Id;
834 Others_Present : Boolean;
836 Statements_Analyzed : Boolean := False;
837 -- Set True if at least some statement sequences get analyzed.
838 -- If False on exit, means we had a serious error that prevented
839 -- full analysis of the case statement, and as a result it is not
840 -- a good idea to output warning messages about unreachable code.
842 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
843 -- Recursively save value of this global, will be restored on exit
845 procedure Non_Static_Choice_Error (Choice : Node_Id);
846 -- Error routine invoked by the generic instantiation below when
847 -- the case statment has a non static choice.
849 procedure Process_Statements (Alternative : Node_Id);
850 -- Analyzes all the statements associated to a case alternative.
851 -- Needed by the generic instantiation below.
853 package Case_Choices_Processing is new
854 Generic_Choices_Processing
855 (Get_Alternatives => Alternatives,
856 Get_Choices => Discrete_Choices,
857 Process_Empty_Choice => No_OP,
858 Process_Non_Static_Choice => Non_Static_Choice_Error,
859 Process_Associated_Node => Process_Statements);
860 use Case_Choices_Processing;
861 -- Instantiation of the generic choice processing package
863 -----------------------------
864 -- Non_Static_Choice_Error --
865 -----------------------------
867 procedure Non_Static_Choice_Error (Choice : Node_Id) is
870 ("choice given in case statement is not static!", Choice);
871 end Non_Static_Choice_Error;
873 ------------------------
874 -- Process_Statements --
875 ------------------------
877 procedure Process_Statements (Alternative : Node_Id) is
878 Choices : constant List_Id := Discrete_Choices (Alternative);
882 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
883 Statements_Analyzed := True;
885 -- An interesting optimization. If the case statement expression
886 -- is a simple entity, then we can set the current value within
887 -- an alternative if the alternative has one possible value.
891 -- when 2 | 3 => beta
892 -- when others => gamma
894 -- Here we know that N is initially 1 within alpha, but for beta
895 -- and gamma, we do not know anything more about the initial value.
897 if Is_Entity_Name (Exp) then
900 if Ekind (Ent) = E_Variable
902 Ekind (Ent) = E_In_Out_Parameter
904 Ekind (Ent) = E_Out_Parameter
906 if List_Length (Choices) = 1
907 and then Nkind (First (Choices)) in N_Subexpr
908 and then Compile_Time_Known_Value (First (Choices))
910 Set_Current_Value (Entity (Exp), First (Choices));
913 Analyze_Statements (Statements (Alternative));
915 -- After analyzing the case, set the current value to empty
916 -- since we won't know what it is for the next alternative
917 -- (unless reset by this same circuit), or after the case.
919 Set_Current_Value (Entity (Exp), Empty);
924 -- Case where expression is not an entity name of a variable
926 Analyze_Statements (Statements (Alternative));
927 end Process_Statements;
929 -- Table to record choices. Put after subprograms since we make
930 -- a call to Number_Of_Choices to get the right number of entries.
932 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
934 -- Start of processing for Analyze_Case_Statement
937 Unblocked_Exit_Count := 0;
938 Exp := Expression (N);
941 -- The expression must be of any discrete type. In rare cases, the
942 -- expander constructs a case statement whose expression has a private
943 -- type whose full view is discrete. This can happen when generating
944 -- a stream operation for a variant type after the type is frozen,
945 -- when the partial of view of the type of the discriminant is private.
946 -- In that case, use the full view to analyze case alternatives.
948 if not Is_Overloaded (Exp)
949 and then not Comes_From_Source (N)
950 and then Is_Private_Type (Etype (Exp))
951 and then Present (Full_View (Etype (Exp)))
952 and then Is_Discrete_Type (Full_View (Etype (Exp)))
954 Resolve (Exp, Etype (Exp));
955 Exp_Type := Full_View (Etype (Exp));
958 Analyze_And_Resolve (Exp, Any_Discrete);
959 Exp_Type := Etype (Exp);
962 Check_Unset_Reference (Exp);
963 Exp_Btype := Base_Type (Exp_Type);
965 -- The expression must be of a discrete type which must be determinable
966 -- independently of the context in which the expression occurs, but
967 -- using the fact that the expression must be of a discrete type.
968 -- Moreover, the type this expression must not be a character literal
969 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
971 -- If error already reported by Resolve, nothing more to do
973 if Exp_Btype = Any_Discrete
974 or else Exp_Btype = Any_Type
978 elsif Exp_Btype = Any_Character then
980 ("character literal as case expression is ambiguous", Exp);
983 elsif Ada_Version = Ada_83
984 and then (Is_Generic_Type (Exp_Btype)
985 or else Is_Generic_Type (Root_Type (Exp_Btype)))
988 ("(Ada 83) case expression cannot be of a generic type", Exp);
992 -- If the case expression is a formal object of mode in out, then
993 -- treat it as having a nonstatic subtype by forcing use of the base
994 -- type (which has to get passed to Check_Case_Choices below). Also
995 -- use base type when the case expression is parenthesized.
997 if Paren_Count (Exp) > 0
998 or else (Is_Entity_Name (Exp)
999 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1001 Exp_Type := Exp_Btype;
1004 -- Call instantiated Analyze_Choices which does the rest of the work
1007 (N, Exp_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
1009 if Exp_Type = Universal_Integer and then not Others_Present then
1010 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1013 -- If all our exits were blocked by unconditional transfers of control,
1014 -- then the entire CASE statement acts as an unconditional transfer of
1015 -- control, so treat it like one, and check unreachable code. Skip this
1016 -- test if we had serious errors preventing any statement analysis.
1018 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1019 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1020 Check_Unreachable_Code (N);
1022 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1025 if not Expander_Active
1026 and then Compile_Time_Known_Value (Expression (N))
1027 and then Serious_Errors_Detected = 0
1030 Chosen : constant Node_Id := Find_Static_Alternative (N);
1034 Alt := First (Alternatives (N));
1036 while Present (Alt) loop
1037 if Alt /= Chosen then
1038 Remove_Warning_Messages (Statements (Alt));
1045 end Analyze_Case_Statement;
1047 ----------------------------
1048 -- Analyze_Exit_Statement --
1049 ----------------------------
1051 -- If the exit includes a name, it must be the name of a currently open
1052 -- loop. Otherwise there must be an innermost open loop on the stack,
1053 -- to which the statement implicitly refers.
1055 procedure Analyze_Exit_Statement (N : Node_Id) is
1056 Target : constant Node_Id := Name (N);
1057 Cond : constant Node_Id := Condition (N);
1058 Scope_Id : Entity_Id;
1064 Check_Unreachable_Code (N);
1067 if Present (Target) then
1069 U_Name := Entity (Target);
1071 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1072 Error_Msg_N ("invalid loop name in exit statement", N);
1075 Set_Has_Exit (U_Name);
1082 for J in reverse 0 .. Scope_Stack.Last loop
1083 Scope_Id := Scope_Stack.Table (J).Entity;
1084 Kind := Ekind (Scope_Id);
1087 and then (No (Target) or else Scope_Id = U_Name) then
1088 Set_Has_Exit (Scope_Id);
1091 elsif Kind = E_Block
1092 or else Kind = E_Loop
1093 or else Kind = E_Return_Statement
1099 ("cannot exit from program unit or accept statement", N);
1104 -- Verify that if present the condition is a Boolean expression
1106 if Present (Cond) then
1107 Analyze_And_Resolve (Cond, Any_Boolean);
1108 Check_Unset_Reference (Cond);
1110 end Analyze_Exit_Statement;
1112 ----------------------------
1113 -- Analyze_Goto_Statement --
1114 ----------------------------
1116 procedure Analyze_Goto_Statement (N : Node_Id) is
1117 Label : constant Node_Id := Name (N);
1118 Scope_Id : Entity_Id;
1119 Label_Scope : Entity_Id;
1122 Check_Unreachable_Code (N);
1126 if Entity (Label) = Any_Id then
1129 elsif Ekind (Entity (Label)) /= E_Label then
1130 Error_Msg_N ("target of goto statement must be a label", Label);
1133 elsif not Reachable (Entity (Label)) then
1134 Error_Msg_N ("target of goto statement is not reachable", Label);
1138 Label_Scope := Enclosing_Scope (Entity (Label));
1140 for J in reverse 0 .. Scope_Stack.Last loop
1141 Scope_Id := Scope_Stack.Table (J).Entity;
1143 if Label_Scope = Scope_Id
1144 or else (Ekind (Scope_Id) /= E_Block
1145 and then Ekind (Scope_Id) /= E_Loop
1146 and then Ekind (Scope_Id) /= E_Return_Statement)
1148 if Scope_Id /= Label_Scope then
1150 ("cannot exit from program unit or accept statement", N);
1157 raise Program_Error;
1158 end Analyze_Goto_Statement;
1160 --------------------------
1161 -- Analyze_If_Statement --
1162 --------------------------
1164 -- A special complication arises in the analysis of if statements
1166 -- The expander has circuitry to completely delete code that it
1167 -- can tell will not be executed (as a result of compile time known
1168 -- conditions). In the analyzer, we ensure that code that will be
1169 -- deleted in this manner is analyzed but not expanded. This is
1170 -- obviously more efficient, but more significantly, difficulties
1171 -- arise if code is expanded and then eliminated (e.g. exception
1172 -- table entries disappear). Similarly, itypes generated in deleted
1173 -- code must be frozen from start, because the nodes on which they
1174 -- depend will not be available at the freeze point.
1176 procedure Analyze_If_Statement (N : Node_Id) is
1179 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1180 -- Recursively save value of this global, will be restored on exit
1182 Save_In_Deleted_Code : Boolean;
1184 Del : Boolean := False;
1185 -- This flag gets set True if a True condition has been found,
1186 -- which means that remaining ELSE/ELSIF parts are deleted.
1188 procedure Analyze_Cond_Then (Cnode : Node_Id);
1189 -- This is applied to either the N_If_Statement node itself or
1190 -- to an N_Elsif_Part node. It deals with analyzing the condition
1191 -- and the THEN statements associated with it.
1193 -----------------------
1194 -- Analyze_Cond_Then --
1195 -----------------------
1197 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1198 Cond : constant Node_Id := Condition (Cnode);
1199 Tstm : constant List_Id := Then_Statements (Cnode);
1202 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1203 Analyze_And_Resolve (Cond, Any_Boolean);
1204 Check_Unset_Reference (Cond);
1205 Set_Current_Value_Condition (Cnode);
1207 -- If already deleting, then just analyze then statements
1210 Analyze_Statements (Tstm);
1212 -- Compile time known value, not deleting yet
1214 elsif Compile_Time_Known_Value (Cond) then
1215 Save_In_Deleted_Code := In_Deleted_Code;
1217 -- If condition is True, then analyze the THEN statements
1218 -- and set no expansion for ELSE and ELSIF parts.
1220 if Is_True (Expr_Value (Cond)) then
1221 Analyze_Statements (Tstm);
1223 Expander_Mode_Save_And_Set (False);
1224 In_Deleted_Code := True;
1226 -- If condition is False, analyze THEN with expansion off
1228 else -- Is_False (Expr_Value (Cond))
1229 Expander_Mode_Save_And_Set (False);
1230 In_Deleted_Code := True;
1231 Analyze_Statements (Tstm);
1232 Expander_Mode_Restore;
1233 In_Deleted_Code := Save_In_Deleted_Code;
1236 -- Not known at compile time, not deleting, normal analysis
1239 Analyze_Statements (Tstm);
1241 end Analyze_Cond_Then;
1243 -- Start of Analyze_If_Statement
1246 -- Initialize exit count for else statements. If there is no else
1247 -- part, this count will stay non-zero reflecting the fact that the
1248 -- uncovered else case is an unblocked exit.
1250 Unblocked_Exit_Count := 1;
1251 Analyze_Cond_Then (N);
1253 -- Now to analyze the elsif parts if any are present
1255 if Present (Elsif_Parts (N)) then
1256 E := First (Elsif_Parts (N));
1257 while Present (E) loop
1258 Analyze_Cond_Then (E);
1263 if Present (Else_Statements (N)) then
1264 Analyze_Statements (Else_Statements (N));
1267 -- If all our exits were blocked by unconditional transfers of control,
1268 -- then the entire IF statement acts as an unconditional transfer of
1269 -- control, so treat it like one, and check unreachable code.
1271 if Unblocked_Exit_Count = 0 then
1272 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1273 Check_Unreachable_Code (N);
1275 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1279 Expander_Mode_Restore;
1280 In_Deleted_Code := Save_In_Deleted_Code;
1283 if not Expander_Active
1284 and then Compile_Time_Known_Value (Condition (N))
1285 and then Serious_Errors_Detected = 0
1287 if Is_True (Expr_Value (Condition (N))) then
1288 Remove_Warning_Messages (Else_Statements (N));
1290 if Present (Elsif_Parts (N)) then
1291 E := First (Elsif_Parts (N));
1293 while Present (E) loop
1294 Remove_Warning_Messages (Then_Statements (E));
1300 Remove_Warning_Messages (Then_Statements (N));
1303 end Analyze_If_Statement;
1305 ----------------------------------------
1306 -- Analyze_Implicit_Label_Declaration --
1307 ----------------------------------------
1309 -- An implicit label declaration is generated in the innermost
1310 -- enclosing declarative part. This is done for labels as well as
1311 -- block and loop names.
1313 -- Note: any changes in this routine may need to be reflected in
1314 -- Analyze_Label_Entity.
1316 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1317 Id : constant Node_Id := Defining_Identifier (N);
1320 Set_Ekind (Id, E_Label);
1321 Set_Etype (Id, Standard_Void_Type);
1322 Set_Enclosing_Scope (Id, Current_Scope);
1323 end Analyze_Implicit_Label_Declaration;
1325 ------------------------------
1326 -- Analyze_Iteration_Scheme --
1327 ------------------------------
1329 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1331 procedure Process_Bounds (R : Node_Id);
1332 -- If the iteration is given by a range, create temporaries and
1333 -- assignment statements block to capture the bounds and perform
1334 -- required finalization actions in case a bound includes a function
1335 -- call that uses the temporary stack. We first pre-analyze a copy of
1336 -- the range in order to determine the expected type, and analyze and
1337 -- resolve the original bounds.
1339 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1340 -- If the bounds are given by a 'Range reference on a function call
1341 -- that returns a controlled array, introduce an explicit declaration
1342 -- to capture the bounds, so that the function result can be finalized
1343 -- in timely fashion.
1345 --------------------
1346 -- Process_Bounds --
1347 --------------------
1349 procedure Process_Bounds (R : Node_Id) is
1350 Loc : constant Source_Ptr := Sloc (N);
1351 R_Copy : constant Node_Id := New_Copy_Tree (R);
1352 Lo : constant Node_Id := Low_Bound (R);
1353 Hi : constant Node_Id := High_Bound (R);
1354 New_Lo_Bound : Node_Id := Empty;
1355 New_Hi_Bound : Node_Id := Empty;
1357 Save_Analysis : Boolean;
1360 (Original_Bound : Node_Id;
1361 Analyzed_Bound : Node_Id) return Node_Id;
1362 -- Create one declaration followed by one assignment statement
1363 -- to capture the value of bound. We create a separate assignment
1364 -- in order to force the creation of a block in case the bound
1365 -- contains a call that uses the secondary stack.
1372 (Original_Bound : Node_Id;
1373 Analyzed_Bound : Node_Id) return Node_Id
1380 -- If the bound is a constant or an object, no need for a separate
1381 -- declaration. If the bound is the result of previous expansion
1382 -- it is already analyzed and should not be modified. Note that
1383 -- the Bound will be resolved later, if needed, as part of the
1384 -- call to Make_Index (literal bounds may need to be resolved to
1387 if Analyzed (Original_Bound) then
1388 return Original_Bound;
1390 elsif Nkind (Analyzed_Bound) = N_Integer_Literal
1391 or else Is_Entity_Name (Analyzed_Bound)
1393 Analyze_And_Resolve (Original_Bound, Typ);
1394 return Original_Bound;
1397 Analyze_And_Resolve (Original_Bound, Typ);
1401 Make_Defining_Identifier (Loc,
1402 Chars => New_Internal_Name ('S'));
1405 Make_Object_Declaration (Loc,
1406 Defining_Identifier => Id,
1407 Object_Definition => New_Occurrence_Of (Typ, Loc));
1409 Insert_Before (Parent (N), Decl);
1413 Make_Assignment_Statement (Loc,
1414 Name => New_Occurrence_Of (Id, Loc),
1415 Expression => Relocate_Node (Original_Bound));
1417 Insert_Before (Parent (N), Assign);
1420 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
1422 if Nkind (Assign) = N_Assignment_Statement then
1423 return Expression (Assign);
1425 return Original_Bound;
1429 -- Start of processing for Process_Bounds
1432 -- Determine expected type of range by analyzing separate copy
1433 -- Do the analysis and resolution of the copy of the bounds with
1434 -- expansion disabled, to prevent the generation of finalization
1435 -- actions on each bound. This prevents memory leaks when the
1436 -- bounds contain calls to functions returning controlled arrays.
1438 Set_Parent (R_Copy, Parent (R));
1439 Save_Analysis := Full_Analysis;
1440 Full_Analysis := False;
1441 Expander_Mode_Save_And_Set (False);
1445 if Is_Overloaded (R_Copy) then
1447 -- Apply preference rules for range of predefined integer types,
1448 -- or diagnose true ambiguity.
1453 Found : Entity_Id := Empty;
1456 Get_First_Interp (R_Copy, I, It);
1457 while Present (It.Typ) loop
1458 if Is_Discrete_Type (It.Typ) then
1462 if Scope (Found) = Standard_Standard then
1465 elsif Scope (It.Typ) = Standard_Standard then
1469 -- Both of them are user-defined
1472 ("ambiguous bounds in range of iteration",
1474 Error_Msg_N ("\possible interpretations:", R_Copy);
1475 Error_Msg_NE ("\\} ", R_Copy, Found);
1476 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
1482 Get_Next_Interp (I, It);
1488 Expander_Mode_Restore;
1489 Full_Analysis := Save_Analysis;
1491 Typ := Etype (R_Copy);
1493 -- If the type of the discrete range is Universal_Integer, then
1494 -- the bound's type must be resolved to Integer, and any object
1495 -- used to hold the bound must also have type Integer, unless the
1496 -- literal bounds are constant-folded expressions that carry a user-
1499 if Typ = Universal_Integer then
1500 if Nkind (Lo) = N_Integer_Literal
1501 and then Present (Etype (Lo))
1502 and then Scope (Etype (Lo)) /= Standard_Standard
1506 elsif Nkind (Hi) = N_Integer_Literal
1507 and then Present (Etype (Hi))
1508 and then Scope (Etype (Hi)) /= Standard_Standard
1513 Typ := Standard_Integer;
1519 New_Lo_Bound := One_Bound (Lo, Low_Bound (R_Copy));
1520 New_Hi_Bound := One_Bound (Hi, High_Bound (R_Copy));
1522 -- Propagate staticness to loop range itself, in case the
1523 -- corresponding subtype is static.
1525 if New_Lo_Bound /= Lo
1526 and then Is_Static_Expression (New_Lo_Bound)
1528 Rewrite (Low_Bound (R), New_Copy (New_Lo_Bound));
1531 if New_Hi_Bound /= Hi
1532 and then Is_Static_Expression (New_Hi_Bound)
1534 Rewrite (High_Bound (R), New_Copy (New_Hi_Bound));
1538 --------------------------------------
1539 -- Check_Controlled_Array_Attribute --
1540 --------------------------------------
1542 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1544 if Nkind (DS) = N_Attribute_Reference
1545 and then Is_Entity_Name (Prefix (DS))
1546 and then Ekind (Entity (Prefix (DS))) = E_Function
1547 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1550 Component_Type (Etype (Entity (Prefix (DS)))))
1551 and then Expander_Active
1554 Loc : constant Source_Ptr := Sloc (N);
1555 Arr : constant Entity_Id :=
1556 Etype (Entity (Prefix (DS)));
1557 Indx : constant Entity_Id :=
1558 Base_Type (Etype (First_Index (Arr)));
1559 Subt : constant Entity_Id :=
1560 Make_Defining_Identifier
1561 (Loc, New_Internal_Name ('S'));
1566 Make_Subtype_Declaration (Loc,
1567 Defining_Identifier => Subt,
1568 Subtype_Indication =>
1569 Make_Subtype_Indication (Loc,
1570 Subtype_Mark => New_Reference_To (Indx, Loc),
1572 Make_Range_Constraint (Loc,
1573 Relocate_Node (DS))));
1574 Insert_Before (Parent (N), Decl);
1578 Make_Attribute_Reference (Loc,
1579 Prefix => New_Reference_To (Subt, Loc),
1580 Attribute_Name => Attribute_Name (DS)));
1584 end Check_Controlled_Array_Attribute;
1586 -- Start of processing for Analyze_Iteration_Scheme
1589 -- For an infinite loop, there is no iteration scheme
1596 Cond : constant Node_Id := Condition (N);
1599 -- For WHILE loop, verify that the condition is a Boolean
1600 -- expression and resolve and check it.
1602 if Present (Cond) then
1603 Analyze_And_Resolve (Cond, Any_Boolean);
1604 Check_Unset_Reference (Cond);
1605 Set_Current_Value_Condition (N);
1608 -- Else we have a FOR loop
1612 LP : constant Node_Id := Loop_Parameter_Specification (N);
1613 Id : constant Entity_Id := Defining_Identifier (LP);
1614 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
1619 -- We always consider the loop variable to be referenced,
1620 -- since the loop may be used just for counting purposes.
1622 Generate_Reference (Id, N, ' ');
1624 -- Check for case of loop variable hiding a local
1625 -- variable (used later on to give a nice warning
1626 -- if the hidden variable is never assigned).
1629 H : constant Entity_Id := Homonym (Id);
1632 and then Enclosing_Dynamic_Scope (H) =
1633 Enclosing_Dynamic_Scope (Id)
1634 and then Ekind (H) = E_Variable
1635 and then Is_Discrete_Type (Etype (H))
1637 Set_Hiding_Loop_Variable (H, Id);
1641 -- Now analyze the subtype definition. If it is
1642 -- a range, create temporaries for bounds.
1644 if Nkind (DS) = N_Range
1645 and then Expander_Active
1647 Process_Bounds (DS);
1656 -- The subtype indication may denote the completion
1657 -- of an incomplete type declaration.
1659 if Is_Entity_Name (DS)
1660 and then Present (Entity (DS))
1661 and then Is_Type (Entity (DS))
1662 and then Ekind (Entity (DS)) = E_Incomplete_Type
1664 Set_Entity (DS, Get_Full_View (Entity (DS)));
1665 Set_Etype (DS, Entity (DS));
1668 if not Is_Discrete_Type (Etype (DS)) then
1669 Wrong_Type (DS, Any_Discrete);
1670 Set_Etype (DS, Any_Type);
1673 Check_Controlled_Array_Attribute (DS);
1675 Make_Index (DS, LP);
1677 Set_Ekind (Id, E_Loop_Parameter);
1678 Set_Etype (Id, Etype (DS));
1679 Set_Is_Known_Valid (Id, True);
1681 -- The loop is not a declarative part, so the only entity
1682 -- declared "within" must be frozen explicitly.
1685 Flist : constant List_Id := Freeze_Entity (Id, Sloc (N));
1687 if Is_Non_Empty_List (Flist) then
1688 Insert_Actions (N, Flist);
1692 -- Check for null or possibly null range and issue warning.
1693 -- We suppress such messages in generic templates and
1694 -- instances, because in practice they tend to be dubious
1697 if Nkind (DS) = N_Range
1698 and then Comes_From_Source (N)
1701 L : constant Node_Id := Low_Bound (DS);
1702 H : constant Node_Id := High_Bound (DS);
1712 Determine_Range (L, LOK, Llo, Lhi);
1713 Determine_Range (H, HOK, Hlo, Hhi);
1715 -- If range of loop is null, issue warning
1717 if (LOK and HOK) and then Llo > Hhi then
1719 -- Suppress the warning if inside a generic
1720 -- template or instance, since in practice
1721 -- they tend to be dubious in these cases since
1722 -- they can result from intended parametrization.
1724 if not Inside_A_Generic
1725 and then not In_Instance
1728 ("?loop range is null, loop will not execute",
1732 -- Since we know the range of the loop is null,
1733 -- set the appropriate flag to suppress any
1734 -- warnings that would otherwise be issued in
1735 -- the body of the loop that will not execute.
1736 -- We do this even in the generic case, since
1737 -- if it is dubious to warn on the null loop
1738 -- itself, it is certainly dubious to warn for
1739 -- conditions that occur inside it!
1741 Set_Is_Null_Loop (Parent (N));
1743 -- The other case for a warning is a reverse loop
1744 -- where the upper bound is the integer literal
1745 -- zero or one, and the lower bound can be positive.
1747 -- For example, we have
1749 -- for J in reverse N .. 1 loop
1751 -- In practice, this is very likely to be a case
1752 -- of reversing the bounds incorrectly in the range.
1754 elsif Reverse_Present (LP)
1755 and then Nkind (Original_Node (H)) =
1757 and then (Intval (H) = Uint_0
1759 Intval (H) = Uint_1)
1762 Error_Msg_N ("?loop range may be null", DS);
1763 Error_Msg_N ("\?bounds may be wrong way round", DS);
1771 end Analyze_Iteration_Scheme;
1777 -- Note: the semantic work required for analyzing labels (setting them as
1778 -- reachable) was done in a prepass through the statements in the block,
1779 -- so that forward gotos would be properly handled. See Analyze_Statements
1780 -- for further details. The only processing required here is to deal with
1781 -- optimizations that depend on an assumption of sequential control flow,
1782 -- since of course the occurrence of a label breaks this assumption.
1784 procedure Analyze_Label (N : Node_Id) is
1785 pragma Warnings (Off, N);
1787 Kill_Current_Values;
1790 --------------------------
1791 -- Analyze_Label_Entity --
1792 --------------------------
1794 procedure Analyze_Label_Entity (E : Entity_Id) is
1796 Set_Ekind (E, E_Label);
1797 Set_Etype (E, Standard_Void_Type);
1798 Set_Enclosing_Scope (E, Current_Scope);
1799 Set_Reachable (E, True);
1800 end Analyze_Label_Entity;
1802 ----------------------------
1803 -- Analyze_Loop_Statement --
1804 ----------------------------
1806 procedure Analyze_Loop_Statement (N : Node_Id) is
1807 Id : constant Node_Id := Identifier (N);
1808 Iter : constant Node_Id := Iteration_Scheme (N);
1812 if Present (Id) then
1814 -- Make name visible, e.g. for use in exit statements. Loop
1815 -- labels are always considered to be referenced.
1819 Generate_Reference (Ent, N, ' ');
1820 Generate_Definition (Ent);
1822 -- If we found a label, mark its type. If not, ignore it, since it
1823 -- means we have a conflicting declaration, which would already have
1824 -- been diagnosed at declaration time. Set Label_Construct of the
1825 -- implicit label declaration, which is not created by the parser
1826 -- for generic units.
1828 if Ekind (Ent) = E_Label then
1829 Set_Ekind (Ent, E_Loop);
1831 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1832 Set_Label_Construct (Parent (Ent), N);
1836 -- Case of no identifier present
1839 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
1840 Set_Etype (Ent, Standard_Void_Type);
1841 Set_Parent (Ent, N);
1844 -- Kill current values on entry to loop, since statements in body
1845 -- of loop may have been executed before the loop is entered.
1846 -- Similarly we kill values after the loop, since we do not know
1847 -- that the body of the loop was executed.
1849 Kill_Current_Values;
1851 Analyze_Iteration_Scheme (Iter);
1852 Analyze_Statements (Statements (N));
1853 Process_End_Label (N, 'e', Ent);
1855 Kill_Current_Values;
1857 -- Check for possible infinite loop which we can diagnose successfully.
1858 -- The case we look for is a while loop which tests a local variable,
1859 -- where there is no obvious direct or indirect update of the variable
1860 -- within the body of the loop.
1862 -- Note: we don't try to give a warning if condition actions are
1863 -- present, since the loop structure can be very complex in this case.
1866 or else No (Condition (Iter))
1867 or else Present (Condition_Actions (Iter))
1868 or else Debug_Flag_Dot_W
1873 -- Initial conditions met, see if condition is of right form
1876 Cond : constant Node_Id := Condition (Iter);
1881 -- Condition is a direct variable reference
1883 if Is_Entity_Name (Cond)
1884 and then not Is_Library_Level_Entity (Entity (Cond))
1888 -- Case of condition is a comparison with compile time known value
1890 elsif Nkind (Cond) in N_Op_Compare then
1891 if Is_Entity_Name (Left_Opnd (Cond))
1892 and then Compile_Time_Known_Value (Right_Opnd (Cond))
1894 Loc := Left_Opnd (Cond);
1896 elsif Is_Entity_Name (Right_Opnd (Cond))
1897 and then Compile_Time_Known_Value (Left_Opnd (Cond))
1899 Loc := Right_Opnd (Cond);
1905 -- Case of condition is function call with one parameter
1907 elsif Nkind (Cond) = N_Function_Call then
1909 PA : constant List_Id := Parameter_Associations (Cond);
1912 and then List_Length (PA) = 1
1913 and then Is_Entity_Name (First (PA))
1925 -- If we fall through Loc is set to the node that is an entity ref
1927 Var := Entity (Loc);
1930 and then Ekind (Var) = E_Variable
1931 and then not Is_Library_Level_Entity (Var)
1932 and then Comes_From_Source (Var)
1939 -- Search for reference to variable in loop
1941 Ref_Search : declare
1942 function Test_Ref (N : Node_Id) return Traverse_Result;
1943 -- Test for reference to variable in question. Returns Abandon
1944 -- if matching reference found.
1946 function Find_Ref is new Traverse_Func (Test_Ref);
1947 -- Function to traverse body of procedure. Returns Abandon if
1948 -- matching reference found.
1954 function Test_Ref (N : Node_Id) return Traverse_Result is
1956 -- Waste of time to look at iteration scheme
1961 -- Direct reference to variable in question
1963 elsif Is_Entity_Name (N)
1964 and then Present (Entity (N))
1965 and then Entity (N) = Var
1966 and then May_Be_Lvalue (N)
1970 -- Reference to variable renaming variable in question
1972 elsif Is_Entity_Name (N)
1973 and then Present (Entity (N))
1974 and then Ekind (Entity (N)) = E_Variable
1975 and then Present (Renamed_Object (Entity (N)))
1976 and then Is_Entity_Name (Renamed_Object (Entity (N)))
1977 and then Entity (Renamed_Object (Entity (N))) = Var
1978 and then May_Be_Lvalue (N)
1982 -- Check for call to other than library level subprogram
1984 elsif Nkind (N) = N_Procedure_Call_Statement
1985 or else Nkind (N) = N_Function_Call
1987 if not Is_Entity_Name (Name (N))
1988 or else not Is_Library_Level_Entity (Entity (Name (N)))
1994 -- All OK, continue scan
1999 -- Start of processing for Ref_Search
2002 if Find_Ref (N) = OK then
2004 ("variable& is not modified in loop body?", Loc, Var);
2006 ("\possible infinite loop", Loc);
2010 end Analyze_Loop_Statement;
2012 ----------------------------
2013 -- Analyze_Null_Statement --
2014 ----------------------------
2016 -- Note: the semantics of the null statement is implemented by a single
2017 -- null statement, too bad everything isn't as simple as this!
2019 procedure Analyze_Null_Statement (N : Node_Id) is
2020 pragma Warnings (Off, N);
2023 end Analyze_Null_Statement;
2025 ------------------------
2026 -- Analyze_Statements --
2027 ------------------------
2029 procedure Analyze_Statements (L : List_Id) is
2034 -- The labels declared in the statement list are reachable from
2035 -- statements in the list. We do this as a prepass so that any
2036 -- goto statement will be properly flagged if its target is not
2037 -- reachable. This is not required, but is nice behavior!
2040 while Present (S) loop
2041 if Nkind (S) = N_Label then
2042 Analyze (Identifier (S));
2043 Lab := Entity (Identifier (S));
2045 -- If we found a label mark it as reachable
2047 if Ekind (Lab) = E_Label then
2048 Generate_Definition (Lab);
2049 Set_Reachable (Lab);
2051 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
2052 Set_Label_Construct (Parent (Lab), S);
2055 -- If we failed to find a label, it means the implicit declaration
2056 -- of the label was hidden. A for-loop parameter can do this to
2057 -- a label with the same name inside the loop, since the implicit
2058 -- label declaration is in the innermost enclosing body or block
2062 Error_Msg_Sloc := Sloc (Lab);
2064 ("implicit label declaration for & is hidden#",
2072 -- Perform semantic analysis on all statements
2074 Conditional_Statements_Begin;
2077 while Present (S) loop
2082 Conditional_Statements_End;
2084 -- Make labels unreachable. Visibility is not sufficient, because
2085 -- labels in one if-branch for example are not reachable from the
2086 -- other branch, even though their declarations are in the enclosing
2087 -- declarative part.
2090 while Present (S) loop
2091 if Nkind (S) = N_Label then
2092 Set_Reachable (Entity (Identifier (S)), False);
2097 end Analyze_Statements;
2099 ----------------------------
2100 -- Check_Unreachable_Code --
2101 ----------------------------
2103 procedure Check_Unreachable_Code (N : Node_Id) is
2104 Error_Loc : Source_Ptr;
2108 if Is_List_Member (N)
2109 and then Comes_From_Source (N)
2115 Nxt := Original_Node (Next (N));
2117 -- If a label follows us, then we never have dead code, since
2118 -- someone could branch to the label, so we just ignore it.
2120 if Nkind (Nxt) = N_Label then
2123 -- Otherwise see if we have a real statement following us
2126 and then Comes_From_Source (Nxt)
2127 and then Is_Statement (Nxt)
2129 -- Special very annoying exception. If we have a return that
2130 -- follows a raise, then we allow it without a warning, since
2131 -- the Ada RM annoyingly requires a useless return here!
2133 if Nkind (Original_Node (N)) /= N_Raise_Statement
2134 or else Nkind (Nxt) /= N_Return_Statement
2136 -- The rather strange shenanigans with the warning message
2137 -- here reflects the fact that Kill_Dead_Code is very good
2138 -- at removing warnings in deleted code, and this is one
2139 -- warning we would prefer NOT to have removed :-)
2141 Error_Loc := Sloc (Nxt);
2143 -- If we have unreachable code, analyze and remove the
2144 -- unreachable code, since it is useless and we don't
2145 -- want to generate junk warnings.
2147 -- We skip this step if we are not in code generation mode.
2148 -- This is the one case where we remove dead code in the
2149 -- semantics as opposed to the expander, and we do not want
2150 -- to remove code if we are not in code generation mode,
2151 -- since this messes up the ASIS trees.
2153 -- Note that one might react by moving the whole circuit to
2154 -- exp_ch5, but then we lose the warning in -gnatc mode.
2156 if Operating_Mode = Generate_Code then
2160 -- Quit deleting when we have nothing more to delete
2161 -- or if we hit a label (since someone could transfer
2162 -- control to a label, so we should not delete it).
2164 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
2166 -- Statement/declaration is to be deleted
2170 Kill_Dead_Code (Nxt);
2174 -- Now issue the warning
2176 Error_Msg ("?unreachable code", Error_Loc);
2179 -- If the unconditional transfer of control instruction is
2180 -- the last statement of a sequence, then see if our parent
2181 -- is one of the constructs for which we count unblocked exits,
2182 -- and if so, adjust the count.
2187 -- Statements in THEN part or ELSE part of IF statement
2189 if Nkind (P) = N_If_Statement then
2192 -- Statements in ELSIF part of an IF statement
2194 elsif Nkind (P) = N_Elsif_Part then
2196 pragma Assert (Nkind (P) = N_If_Statement);
2198 -- Statements in CASE statement alternative
2200 elsif Nkind (P) = N_Case_Statement_Alternative then
2202 pragma Assert (Nkind (P) = N_Case_Statement);
2204 -- Statements in body of block
2206 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
2207 and then Nkind (Parent (P)) = N_Block_Statement
2211 -- Statements in exception handler in a block
2213 elsif Nkind (P) = N_Exception_Handler
2214 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
2215 and then Nkind (Parent (Parent (P))) = N_Block_Statement
2219 -- None of these cases, so return
2225 -- This was one of the cases we are looking for (i.e. the
2226 -- parent construct was IF, CASE or block) so decrement count.
2228 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
2232 end Check_Unreachable_Code;