1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Util; use Exp_Util;
35 with Freeze; use Freeze;
36 with Lib.Xref; use Lib.Xref;
37 with Nlists; use Nlists;
40 with Sem_Case; use Sem_Case;
41 with Sem_Ch3; use Sem_Ch3;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Disp; use Sem_Disp;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Res; use Sem_Res;
46 with Sem_Type; use Sem_Type;
47 with Sem_Util; use Sem_Util;
48 with Sem_Warn; use Sem_Warn;
49 with Stand; use Stand;
50 with Sinfo; use Sinfo;
51 with Tbuild; use Tbuild;
52 with Uintp; use Uintp;
54 package body Sem_Ch5 is
56 Unblocked_Exit_Count : Nat := 0;
57 -- This variable is used when processing if statements or case
58 -- statements, it counts the number of branches of the conditional
59 -- that are not blocked by unconditional transfer instructions. At
60 -- the end of processing, if the count is zero, it means that control
61 -- cannot fall through the conditional statement. This is used for
62 -- the generation of warning messages. This variable is recursively
63 -- saved on entry to processing an if or case, and restored on exit.
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Analyze_Iteration_Scheme (N : Node_Id);
71 ------------------------
72 -- Analyze_Assignment --
73 ------------------------
75 procedure Analyze_Assignment (N : Node_Id) is
76 Lhs : constant Node_Id := Name (N);
77 Rhs : constant Node_Id := Expression (N);
81 procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
82 -- N is the node for the left hand side of an assignment, and it
83 -- is not a variable. This routine issues an appropriate diagnostic.
85 procedure Set_Assignment_Type
87 Opnd_Type : in out Entity_Id);
88 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type
89 -- is the nominal subtype. This procedure is used to deal with cases
90 -- where the nominal subtype must be replaced by the actual subtype.
92 -------------------------------
93 -- Diagnose_Non_Variable_Lhs --
94 -------------------------------
96 procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
98 -- Not worth posting another error if left hand side already
99 -- flagged as being illegal in some respect
101 if Error_Posted (N) then
104 -- Some special bad cases of entity names
106 elsif Is_Entity_Name (N) then
108 if Ekind (Entity (N)) = E_In_Parameter then
110 ("assignment to IN mode parameter not allowed", N);
113 -- Private declarations in a protected object are turned into
114 -- constants when compiling a protected function.
116 elsif Present (Scope (Entity (N)))
117 and then Is_Protected_Type (Scope (Entity (N)))
119 (Ekind (Current_Scope) = E_Function
121 Ekind (Enclosing_Dynamic_Scope (Current_Scope)) = E_Function)
124 ("protected function cannot modify protected object", N);
127 elsif Ekind (Entity (N)) = E_Loop_Parameter then
129 ("assignment to loop parameter not allowed", N);
134 -- For indexed components, or selected components, test prefix
136 elsif Nkind (N) = N_Indexed_Component
137 or else Nkind (N) = N_Selected_Component
139 Diagnose_Non_Variable_Lhs (Prefix (N));
143 -- If we fall through, we have no special message to issue!
145 Error_Msg_N ("left hand side of assignment must be a variable", N);
147 end Diagnose_Non_Variable_Lhs;
149 -------------------------
150 -- Set_Assignment_Type --
151 -------------------------
153 procedure Set_Assignment_Type
155 Opnd_Type : in out Entity_Id)
158 -- If the assignment operand is an in-out or out parameter, then we
159 -- get the actual subtype (needed for the unconstrained case).
161 if Is_Entity_Name (Opnd)
162 and then (Ekind (Entity (Opnd)) = E_Out_Parameter
163 or else Ekind (Entity (Opnd)) =
165 or else Ekind (Entity (Opnd)) =
166 E_Generic_In_Out_Parameter)
168 Opnd_Type := Get_Actual_Subtype (Opnd);
170 -- If assignment operand is a component reference, then we get the
171 -- actual subtype of the component for the unconstrained case.
173 elsif Nkind (Opnd) = N_Selected_Component
174 or else Nkind (Opnd) = N_Explicit_Dereference
176 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
178 if Present (Decl) then
179 Insert_Action (N, Decl);
180 Mark_Rewrite_Insertion (Decl);
182 Opnd_Type := Defining_Identifier (Decl);
183 Set_Etype (Opnd, Opnd_Type);
184 Freeze_Itype (Opnd_Type, N);
186 elsif Is_Constrained (Etype (Opnd)) then
187 Opnd_Type := Etype (Opnd);
190 -- For slice, use the constrained subtype created for the slice
192 elsif Nkind (Opnd) = N_Slice then
193 Opnd_Type := Etype (Opnd);
195 end Set_Assignment_Type;
197 -- Start of processing for Analyze_Assignment
204 -- In the most general case, both Lhs and Rhs can be overloaded, and we
205 -- must compute the intersection of the possible types on each side.
207 if Is_Overloaded (Lhs) then
214 Get_First_Interp (Lhs, I, It);
216 while Present (It.Typ) loop
217 if Has_Compatible_Type (Rhs, It.Typ) then
219 if T1 /= Any_Type then
221 -- An explicit dereference is overloaded if the prefix
222 -- is. Try to remove the ambiguity on the prefix, the
223 -- error will be posted there if the ambiguity is real.
225 if Nkind (Lhs) = N_Explicit_Dereference then
228 PI1 : Interp_Index := 0;
234 Get_First_Interp (Prefix (Lhs), PI, PIt);
236 while Present (PIt.Typ) loop
237 if Has_Compatible_Type (Rhs,
238 Designated_Type (PIt.Typ))
242 Disambiguate (Prefix (Lhs),
245 if PIt = No_Interp then
248 Resolve (Prefix (Lhs), PIt.Typ);
258 Get_Next_Interp (PI, PIt);
264 ("ambiguous left-hand side in assignment", Lhs);
272 Get_Next_Interp (I, It);
276 if T1 = Any_Type then
278 ("no valid types for left-hand side for assignment", Lhs);
285 if not Is_Variable (Lhs) then
286 Diagnose_Non_Variable_Lhs (Lhs);
289 elsif Is_Limited_Type (T1)
290 and then not Assignment_OK (Lhs)
291 and then not Assignment_OK (Original_Node (Lhs))
294 ("left hand of assignment must not be limited type", Lhs);
298 -- Resolution may have updated the subtype, in case the left-hand
299 -- side is a private protected component. Use the correct subtype
300 -- to avoid scoping issues in the back-end.
303 Set_Assignment_Type (Lhs, T1);
307 -- Remaining steps are skipped if Rhs was synatactically in error
314 Check_Unset_Reference (Rhs);
315 Note_Possible_Modification (Lhs);
317 if Covers (T1, T2) then
320 Wrong_Type (Rhs, Etype (Lhs));
324 Set_Assignment_Type (Rhs, T2);
326 if T1 = Any_Type or else T2 = Any_Type then
330 if (Is_Class_Wide_Type (T2) or else Is_Dynamically_Tagged (Rhs))
331 and then not Is_Class_Wide_Type (T1)
333 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
335 elsif Is_Class_Wide_Type (T1)
336 and then not Is_Class_Wide_Type (T2)
337 and then not Is_Tag_Indeterminate (Rhs)
338 and then not Is_Dynamically_Tagged (Rhs)
340 Error_Msg_N ("dynamically tagged expression required!", Rhs);
343 -- Tag propagation is done only in semantics mode only. If expansion
344 -- is on, the rhs tag indeterminate function call has been expanded
345 -- and tag propagation would have happened too late, so the
346 -- propagation take place in expand_call instead.
348 if not Expander_Active
349 and then Is_Class_Wide_Type (T1)
350 and then Is_Tag_Indeterminate (Rhs)
352 Propagate_Tag (Lhs, Rhs);
355 if Is_Scalar_Type (T1) then
356 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
358 elsif Is_Array_Type (T1) then
360 -- Assignment verifies that the length of the Lsh and Rhs are equal,
361 -- but of course the indices do not have to match.
363 Apply_Length_Check (Rhs, Etype (Lhs));
366 -- Discriminant checks are applied in the course of expansion.
370 -- ??? a real accessibility check is needed when ???
372 -- Post warning for useless assignment
374 if Warn_On_Redundant_Constructs
376 -- We only warn for source constructs
378 and then Comes_From_Source (N)
380 -- Where the entity is the same on both sides
382 and then Is_Entity_Name (Lhs)
383 and then Is_Entity_Name (Rhs)
384 and then Entity (Lhs) = Entity (Rhs)
386 -- But exclude the case where the right side was an operation
387 -- that got rewritten (e.g. JUNK + K, where K was known to be
388 -- zero). We don't want to warn in such a case, since it is
389 -- reasonable to write such expressions especially when K is
390 -- defined symbolically in some other package.
392 and then Nkind (Original_Node (Rhs)) not in N_Op
395 ("?useless assignment of & to itself", N, Entity (Lhs));
397 end Analyze_Assignment;
399 -----------------------------
400 -- Analyze_Block_Statement --
401 -----------------------------
403 procedure Analyze_Block_Statement (N : Node_Id) is
404 Decls : constant List_Id := Declarations (N);
405 Id : constant Node_Id := Identifier (N);
409 -- If a label is present analyze it and mark it as referenced
414 Set_Ekind (Ent, E_Block);
415 Generate_Reference (Ent, N, ' ');
416 Generate_Definition (Ent);
418 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
419 Set_Label_Construct (Parent (Ent), N);
422 -- Otherwise create a label entity
425 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
426 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
429 Set_Etype (Ent, Standard_Void_Type);
430 Set_Block_Node (Ent, Identifier (N));
433 if Present (Decls) then
434 Analyze_Declarations (Decls);
438 Analyze (Handled_Statement_Sequence (N));
439 Process_End_Label (Handled_Statement_Sequence (N), 'e');
441 -- Analyze exception handlers if present. Note that the test for
442 -- HSS being present is an error defence against previous errors.
444 if Present (Handled_Statement_Sequence (N))
445 and then Present (Exception_Handlers (Handled_Statement_Sequence (N)))
448 S : Entity_Id := Scope (Ent);
451 -- Indicate that enclosing scopes contain a block with handlers.
452 -- Only non-generic scopes need to be marked.
455 Set_Has_Nested_Block_With_Handler (S);
456 exit when Is_Overloadable (S)
457 or else Ekind (S) = E_Package
458 or else Ekind (S) = E_Generic_Function
459 or else Ekind (S) = E_Generic_Package
460 or else Ekind (S) = E_Generic_Procedure;
466 Check_References (Ent);
468 end Analyze_Block_Statement;
470 ----------------------------
471 -- Analyze_Case_Statement --
472 ----------------------------
474 procedure Analyze_Case_Statement (N : Node_Id) is
476 Statements_Analyzed : Boolean := False;
477 -- Set True if at least some statement sequences get analyzed.
478 -- If False on exit, means we had a serious error that prevented
479 -- full analysis of the case statement, and as a result it is not
480 -- a good idea to output warning messages about unreachable code.
482 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
483 -- Recursively save value of this global, will be restored on exit
485 procedure Non_Static_Choice_Error (Choice : Node_Id);
486 -- Error routine invoked by the generic instantiation below when
487 -- the case statement has a non static choice.
489 procedure Process_Statements (Alternative : Node_Id);
490 -- Analyzes all the statements associated to a case alternative.
491 -- Needed by the generic instantiation below.
493 package Case_Choices_Processing is new
494 Generic_Choices_Processing
495 (Get_Alternatives => Alternatives,
496 Get_Choices => Discrete_Choices,
497 Process_Empty_Choice => No_OP,
498 Process_Non_Static_Choice => Non_Static_Choice_Error,
499 Process_Associated_Node => Process_Statements);
500 use Case_Choices_Processing;
501 -- Instantiation of the generic choice processing package.
503 -----------------------------
504 -- Non_Static_Choice_Error --
505 -----------------------------
507 procedure Non_Static_Choice_Error (Choice : Node_Id) is
509 Error_Msg_N ("choice given in case statement is not static", Choice);
510 end Non_Static_Choice_Error;
512 ------------------------
513 -- Process_Statements --
514 ------------------------
516 procedure Process_Statements (Alternative : Node_Id) is
518 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
519 Statements_Analyzed := True;
520 Analyze_Statements (Statements (Alternative));
521 end Process_Statements;
523 -- Variables local to Analyze_Case_Statement.
526 Exp_Type : Entity_Id;
527 Exp_Btype : Entity_Id;
529 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
532 Others_Present : Boolean;
534 -- Start of processing for Analyze_Case_Statement
537 Unblocked_Exit_Count := 0;
538 Exp := Expression (N);
539 Analyze_And_Resolve (Exp, Any_Discrete);
540 Check_Unset_Reference (Exp);
541 Exp_Type := Etype (Exp);
542 Exp_Btype := Base_Type (Exp_Type);
544 -- The expression must be of a discrete type which must be determinable
545 -- independently of the context in which the expression occurs, but
546 -- using the fact that the expression must be of a discrete type.
547 -- Moreover, the type this expression must not be a character literal
548 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
550 -- If error already reported by Resolve, nothing more to do
552 if Exp_Btype = Any_Discrete
553 or else Exp_Btype = Any_Type
557 elsif Exp_Btype = Any_Character then
559 ("character literal as case expression is ambiguous", Exp);
563 and then (Is_Generic_Type (Exp_Btype)
564 or else Is_Generic_Type (Root_Type (Exp_Btype)))
567 ("(Ada 83) case expression cannot be of a generic type", Exp);
571 -- If the case expression is a formal object of mode in out,
572 -- then treat it as having a nonstatic subtype by forcing
573 -- use of the base type (which has to get passed to
574 -- Check_Case_Choices below). Also use base type when
575 -- the case expression is parenthesized.
577 if Paren_Count (Exp) > 0
578 or else (Is_Entity_Name (Exp)
579 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
581 Exp_Type := Exp_Btype;
584 -- Call the instantiated Analyze_Choices which does the rest of the work
587 (N, Exp_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
589 if Exp_Type = Universal_Integer and then not Others_Present then
590 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
593 -- If all our exits were blocked by unconditional transfers of control,
594 -- then the entire CASE statement acts as an unconditional transfer of
595 -- control, so treat it like one, and check unreachable code. Skip this
596 -- test if we had serious errors preventing any statement analysis.
598 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
599 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
600 Check_Unreachable_Code (N);
602 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
604 end Analyze_Case_Statement;
606 ----------------------------
607 -- Analyze_Exit_Statement --
608 ----------------------------
610 -- If the exit includes a name, it must be the name of a currently open
611 -- loop. Otherwise there must be an innermost open loop on the stack,
612 -- to which the statement implicitly refers.
614 procedure Analyze_Exit_Statement (N : Node_Id) is
615 Target : constant Node_Id := Name (N);
616 Cond : constant Node_Id := Condition (N);
617 Scope_Id : Entity_Id;
623 Check_Unreachable_Code (N);
626 if Present (Target) then
628 U_Name := Entity (Target);
630 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
631 Error_Msg_N ("invalid loop name in exit statement", N);
634 Set_Has_Exit (U_Name);
641 for J in reverse 0 .. Scope_Stack.Last loop
642 Scope_Id := Scope_Stack.Table (J).Entity;
643 Kind := Ekind (Scope_Id);
646 and then (No (Target) or else Scope_Id = U_Name) then
647 Set_Has_Exit (Scope_Id);
650 elsif Kind = E_Block or else Kind = E_Loop then
655 ("cannot exit from program unit or accept statement", N);
660 -- Verify that if present the condition is a Boolean expression.
662 if Present (Cond) then
663 Analyze_And_Resolve (Cond, Any_Boolean);
664 Check_Unset_Reference (Cond);
666 end Analyze_Exit_Statement;
668 ----------------------------
669 -- Analyze_Goto_Statement --
670 ----------------------------
672 procedure Analyze_Goto_Statement (N : Node_Id) is
673 Label : constant Node_Id := Name (N);
674 Scope_Id : Entity_Id;
675 Label_Scope : Entity_Id;
678 Check_Unreachable_Code (N);
682 if Entity (Label) = Any_Id then
685 elsif Ekind (Entity (Label)) /= E_Label then
686 Error_Msg_N ("target of goto statement must be a label", Label);
689 elsif not Reachable (Entity (Label)) then
690 Error_Msg_N ("target of goto statement is not reachable", Label);
694 Label_Scope := Enclosing_Scope (Entity (Label));
696 for J in reverse 0 .. Scope_Stack.Last loop
697 Scope_Id := Scope_Stack.Table (J).Entity;
699 if Label_Scope = Scope_Id
700 or else (Ekind (Scope_Id) /= E_Block
701 and then Ekind (Scope_Id) /= E_Loop)
703 if Scope_Id /= Label_Scope then
705 ("cannot exit from program unit or accept statement", N);
714 end Analyze_Goto_Statement;
716 --------------------------
717 -- Analyze_If_Statement --
718 --------------------------
720 -- A special complication arises in the analysis of if statements.
721 -- The expander has circuitry to completely deleted code that it
722 -- can tell will not be executed (as a result of compile time known
723 -- conditions). In the analyzer, we ensure that code that will be
724 -- deleted in this manner is analyzed but not expanded. This is
725 -- obviously more efficient, but more significantly, difficulties
726 -- arise if code is expanded and then eliminated (e.g. exception
727 -- table entries disappear).
729 procedure Analyze_If_Statement (N : Node_Id) is
732 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
733 -- Recursively save value of this global, will be restored on exit
735 Del : Boolean := False;
736 -- This flag gets set True if a True condition has been found,
737 -- which means that remaining ELSE/ELSIF parts are deleted.
739 procedure Analyze_Cond_Then (Cnode : Node_Id);
740 -- This is applied to either the N_If_Statement node itself or
741 -- to an N_Elsif_Part node. It deals with analyzing the condition
742 -- and the THEN statements associated with it.
744 procedure Analyze_Cond_Then (Cnode : Node_Id) is
745 Cond : constant Node_Id := Condition (Cnode);
746 Tstm : constant List_Id := Then_Statements (Cnode);
749 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
750 Analyze_And_Resolve (Cond, Any_Boolean);
751 Check_Unset_Reference (Cond);
753 -- If already deleting, then just analyze then statements
756 Analyze_Statements (Tstm);
758 -- Compile time known value, not deleting yet
760 elsif Compile_Time_Known_Value (Cond) then
762 -- If condition is True, then analyze the THEN statements
763 -- and set no expansion for ELSE and ELSIF parts.
765 if Is_True (Expr_Value (Cond)) then
766 Analyze_Statements (Tstm);
768 Expander_Mode_Save_And_Set (False);
770 -- If condition is False, analyze THEN with expansion off
772 else -- Is_False (Expr_Value (Cond))
773 Expander_Mode_Save_And_Set (False);
774 Analyze_Statements (Tstm);
775 Expander_Mode_Restore;
778 -- Not known at compile time, not deleting, normal analysis
781 Analyze_Statements (Tstm);
783 end Analyze_Cond_Then;
785 -- Start of Analyze_If_Statement
788 -- Initialize exit count for else statements. If there is no else
789 -- part, this count will stay non-zero reflecting the fact that the
790 -- uncovered else case is an unblocked exit.
792 Unblocked_Exit_Count := 1;
793 Analyze_Cond_Then (N);
795 -- Now to analyze the elsif parts if any are present
797 if Present (Elsif_Parts (N)) then
798 E := First (Elsif_Parts (N));
799 while Present (E) loop
800 Analyze_Cond_Then (E);
805 if Present (Else_Statements (N)) then
806 Analyze_Statements (Else_Statements (N));
809 -- If all our exits were blocked by unconditional transfers of control,
810 -- then the entire IF statement acts as an unconditional transfer of
811 -- control, so treat it like one, and check unreachable code.
813 if Unblocked_Exit_Count = 0 then
814 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
815 Check_Unreachable_Code (N);
817 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
821 Expander_Mode_Restore;
824 end Analyze_If_Statement;
826 ----------------------------------------
827 -- Analyze_Implicit_Label_Declaration --
828 ----------------------------------------
830 -- An implicit label declaration is generated in the innermost
831 -- enclosing declarative part. This is done for labels as well as
832 -- block and loop names.
834 -- Note: any changes in this routine may need to be reflected in
835 -- Analyze_Label_Entity.
837 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
838 Id : Node_Id := Defining_Identifier (N);
842 Set_Ekind (Id, E_Label);
843 Set_Etype (Id, Standard_Void_Type);
844 Set_Enclosing_Scope (Id, Current_Scope);
845 end Analyze_Implicit_Label_Declaration;
847 ------------------------------
848 -- Analyze_Iteration_Scheme --
849 ------------------------------
851 procedure Analyze_Iteration_Scheme (N : Node_Id) is
853 -- For an infinite loop, there is no iteration scheme
860 Cond : constant Node_Id := Condition (N);
863 -- For WHILE loop, verify that the condition is a Boolean
864 -- expression and resolve and check it.
866 if Present (Cond) then
867 Analyze_And_Resolve (Cond, Any_Boolean);
868 Check_Unset_Reference (Cond);
870 -- Else we have a FOR loop
874 LP : constant Node_Id := Loop_Parameter_Specification (N);
875 Id : constant Entity_Id := Defining_Identifier (LP);
876 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
882 -- We always consider the loop variable to be referenced,
883 -- since the loop may be used just for counting purposes.
885 Generate_Reference (Id, N, ' ');
887 -- Check for case of loop variable hiding a local
888 -- variable (used later on to give a nice warning
889 -- if the hidden variable is never assigned).
892 H : constant Entity_Id := Homonym (Id);
896 and then Enclosing_Dynamic_Scope (H) =
897 Enclosing_Dynamic_Scope (Id)
898 and then Ekind (H) = E_Variable
899 and then Is_Discrete_Type (Etype (H))
901 Set_Hiding_Loop_Variable (H, Id);
905 -- Now analyze the subtype definition
913 -- The subtype indication may denote the completion
914 -- of an incomplete type declaration.
916 if Is_Entity_Name (DS)
917 and then Present (Entity (DS))
918 and then Is_Type (Entity (DS))
919 and then Ekind (Entity (DS)) = E_Incomplete_Type
921 Set_Entity (DS, Get_Full_View (Entity (DS)));
922 Set_Etype (DS, Entity (DS));
925 if not Is_Discrete_Type (Etype (DS)) then
926 Wrong_Type (DS, Any_Discrete);
927 Set_Etype (DS, Any_Type);
932 Set_Ekind (Id, E_Loop_Parameter);
933 Set_Etype (Id, Etype (DS));
934 Set_Is_Known_Valid (Id, True);
936 -- The loop is not a declarative part, so the only entity
937 -- declared "within" must be frozen explicitly. Since the
938 -- type of this entity has already been frozen, this cannot
939 -- generate any freezing actions.
941 F := Freeze_Entity (Id, Sloc (LP));
942 pragma Assert (F = No_List);
944 -- Check for null or possibly null range and issue warning.
945 -- We suppress such messages in generic templates and
946 -- instances, because in practice they tend to be dubious
949 if Nkind (DS) = N_Range
950 and then Comes_From_Source (N)
951 and then not Inside_A_Generic
952 and then not In_Instance
955 L : constant Node_Id := Low_Bound (DS);
956 H : constant Node_Id := High_Bound (DS);
966 Determine_Range (L, LOK, Llo, Lhi);
967 Determine_Range (H, HOK, Hlo, Hhi);
969 -- If range of loop is null, issue warning
971 if (LOK and HOK) and then Llo > Hhi then
973 ("?loop range is null, loop will not execute",
976 -- The other case for a warning is a reverse loop
977 -- where the upper bound is the integer literal
978 -- zero or one, and the lower bound can be positive.
980 elsif Reverse_Present (LP)
981 and then Nkind (H) = N_Integer_Literal
982 and then (Intval (H) = Uint_0
987 Warn_On_Instance := True;
988 Error_Msg_N ("?loop range may be null", DS);
989 Warn_On_Instance := False;
997 end Analyze_Iteration_Scheme;
1003 -- Important note: normally this routine is called from Analyze_Statements
1004 -- which does a prescan, to make sure that the Reachable flags are set on
1005 -- all labels before encountering a possible goto to one of these labels.
1006 -- If expanded code analyzes labels via the normal Sem path, then it must
1007 -- ensure that Reachable is set early enough to avoid problems in the case
1008 -- of a forward goto.
1010 procedure Analyze_Label (N : Node_Id) is
1014 Analyze (Identifier (N));
1015 Lab := Entity (Identifier (N));
1017 -- If we found a label mark it as reachable.
1019 if Ekind (Lab) = E_Label then
1020 Generate_Definition (Lab);
1021 Set_Reachable (Lab);
1023 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
1024 Set_Label_Construct (Parent (Lab), N);
1027 -- If we failed to find a label, it means the implicit declaration
1028 -- of the label was hidden. A for-loop parameter can do this to a
1029 -- label with the same name inside the loop, since the implicit label
1030 -- declaration is in the innermost enclosing body or block statement.
1033 Error_Msg_Sloc := Sloc (Lab);
1035 ("implicit label declaration for & is hidden#",
1040 --------------------------
1041 -- Analyze_Label_Entity --
1042 --------------------------
1044 procedure Analyze_Label_Entity (E : Entity_Id) is
1046 Set_Ekind (E, E_Label);
1047 Set_Etype (E, Standard_Void_Type);
1048 Set_Enclosing_Scope (E, Current_Scope);
1049 Set_Reachable (E, True);
1050 end Analyze_Label_Entity;
1052 ----------------------------
1053 -- Analyze_Loop_Statement --
1054 ----------------------------
1056 procedure Analyze_Loop_Statement (N : Node_Id) is
1057 Id : constant Node_Id := Identifier (N);
1061 if Present (Id) then
1063 -- Make name visible, e.g. for use in exit statements. Loop
1064 -- labels are always considered to be referenced.
1068 Generate_Reference (Ent, N, ' ');
1069 Generate_Definition (Ent);
1071 -- If we found a label, mark its type. If not, ignore it, since it
1072 -- means we have a conflicting declaration, which would already have
1073 -- been diagnosed at declaration time. Set Label_Construct of the
1074 -- implicit label declaration, which is not created by the parser
1075 -- for generic units.
1077 if Ekind (Ent) = E_Label then
1078 Set_Ekind (Ent, E_Loop);
1080 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1081 Set_Label_Construct (Parent (Ent), N);
1085 -- Case of no identifier present
1088 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
1089 Set_Etype (Ent, Standard_Void_Type);
1090 Set_Parent (Ent, N);
1094 Analyze_Iteration_Scheme (Iteration_Scheme (N));
1095 Analyze_Statements (Statements (N));
1096 Process_End_Label (N, 'e');
1098 end Analyze_Loop_Statement;
1100 ----------------------------
1101 -- Analyze_Null_Statement --
1102 ----------------------------
1104 -- Note: the semantics of the null statement is implemented by a single
1105 -- null statement, too bad everything isn't as simple as this!
1107 procedure Analyze_Null_Statement (N : Node_Id) is
1110 end Analyze_Null_Statement;
1112 ------------------------
1113 -- Analyze_Statements --
1114 ------------------------
1116 procedure Analyze_Statements (L : List_Id) is
1120 -- The labels declared in the statement list are reachable from
1121 -- statements in the list. We do this as a prepass so that any
1122 -- goto statement will be properly flagged if its target is not
1123 -- reachable. This is not required, but is nice behavior!
1127 while Present (S) loop
1128 if Nkind (S) = N_Label then
1135 -- Perform semantic analysis on all statements
1139 while Present (S) loop
1141 if Nkind (S) /= N_Label then
1148 -- Make labels unreachable. Visibility is not sufficient, because
1149 -- labels in one if-branch for example are not reachable from the
1150 -- other branch, even though their declarations are in the enclosing
1151 -- declarative part.
1155 while Present (S) loop
1156 if Nkind (S) = N_Label then
1157 Set_Reachable (Entity (Identifier (S)), False);
1162 end Analyze_Statements;
1164 ----------------------------
1165 -- Check_Unreachable_Code --
1166 ----------------------------
1168 procedure Check_Unreachable_Code (N : Node_Id) is
1169 Error_Loc : Source_Ptr;
1173 if Is_List_Member (N)
1174 and then Comes_From_Source (N)
1180 Nxt := Original_Node (Next (N));
1183 and then Comes_From_Source (Nxt)
1184 and then Is_Statement (Nxt)
1186 -- Special very annoying exception. If we have a return that
1187 -- follows a raise, then we allow it without a warning, since
1188 -- the Ada RM annoyingly requires a useless return here!
1190 if Nkind (Original_Node (N)) /= N_Raise_Statement
1191 or else Nkind (Nxt) /= N_Return_Statement
1193 -- The rather strange shenanigans with the warning message
1194 -- here reflects the fact that Kill_Dead_Code is very good
1195 -- at removing warnings in deleted code, and this is one
1196 -- warning we would prefer NOT to have removed :-)
1198 Error_Loc := Sloc (Nxt);
1200 -- If we have unreachable code, analyze and remove the
1201 -- unreachable code, since it is useless and we don't
1202 -- want to generate junk warnings.
1204 -- We skip this step if we are not in code generation mode.
1205 -- This is the one case where we remove dead code in the
1206 -- semantics as opposed to the expander, and we do not want
1207 -- to remove code if we are not in code generation mode,
1208 -- since this messes up the ASIS trees.
1210 -- Note that one might react by moving the whole circuit to
1211 -- exp_ch5, but then we lose the warning in -gnatc mode.
1213 if Operating_Mode = Generate_Code then
1216 exit when No (Nxt) or else not Is_Statement (Nxt);
1219 Kill_Dead_Code (Nxt);
1223 -- Now issue the warning
1225 Error_Msg ("?unreachable code", Error_Loc);
1228 -- If the unconditional transfer of control instruction is
1229 -- the last statement of a sequence, then see if our parent
1230 -- is an IF statement, and if so adjust the unblocked exit
1231 -- count of the if statement to reflect the fact that this
1232 -- branch of the if is indeed blocked by a transfer of control.
1237 if Nkind (P) = N_If_Statement then
1240 elsif Nkind (P) = N_Elsif_Part then
1242 pragma Assert (Nkind (P) = N_If_Statement);
1244 elsif Nkind (P) = N_Case_Statement_Alternative then
1246 pragma Assert (Nkind (P) = N_Case_Statement);
1252 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
1256 end Check_Unreachable_Code;