OSDN Git Service

2006-10-31 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch5.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 5                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2006, Free Software Foundation, Inc.         --
10 --                                                                          --
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.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
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;
35 with Lib;      use Lib;
36 with Lib.Xref; use Lib.Xref;
37 with Nlists;   use Nlists;
38 with Nmake;    use Nmake;
39 with Opt;      use Opt;
40 with Rtsfind;  use Rtsfind;
41 with Sem;      use Sem;
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;
58
59 package body Sem_Ch5 is
60
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.
72
73    -----------------------
74    -- Local Subprograms --
75    -----------------------
76
77    procedure Analyze_Iteration_Scheme (N : Node_Id);
78
79    ------------------------
80    -- Analyze_Assignment --
81    ------------------------
82
83    procedure Analyze_Assignment (N : Node_Id) is
84       Lhs  : constant Node_Id := Name (N);
85       Rhs  : constant Node_Id := Expression (N);
86       T1   : Entity_Id;
87       T2   : Entity_Id;
88       Decl : Node_Id;
89
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.
93
94       procedure Kill_Lhs;
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.
99
100       procedure Set_Assignment_Type
101         (Opnd      : Node_Id;
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.
106
107       -------------------------------
108       -- Diagnose_Non_Variable_Lhs --
109       -------------------------------
110
111       procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
112       begin
113          --  Not worth posting another error if left hand side already
114          --  flagged as being illegal in some respect
115
116          if Error_Posted (N) then
117             return;
118
119          --  Some special bad cases of entity names
120
121          elsif Is_Entity_Name (N) then
122             if Ekind (Entity (N)) = E_In_Parameter then
123                Error_Msg_N
124                  ("assignment to IN mode parameter not allowed", N);
125
126             --  Private declarations in a protected object are turned into
127             --  constants when compiling a protected function.
128
129             elsif Present (Scope (Entity (N)))
130               and then Is_Protected_Type (Scope (Entity (N)))
131               and then
132                 (Ekind (Current_Scope) = E_Function
133                   or else
134                  Ekind (Enclosing_Dynamic_Scope (Current_Scope)) = E_Function)
135             then
136                Error_Msg_N
137                  ("protected function cannot modify protected object", N);
138
139             elsif Ekind (Entity (N)) = E_Loop_Parameter then
140                Error_Msg_N
141                  ("assignment to loop parameter not allowed", N);
142
143             else
144                Error_Msg_N
145                  ("left hand side of assignment must be a variable", N);
146             end if;
147
148          --  For indexed components or selected components, test prefix
149
150          elsif Nkind (N) = N_Indexed_Component then
151             Diagnose_Non_Variable_Lhs (Prefix (N));
152
153          --  Another special case for assignment to discriminant
154
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
158             then
159                Error_Msg_N
160                  ("assignment to discriminant not allowed", N);
161             else
162                Diagnose_Non_Variable_Lhs (Prefix (N));
163             end if;
164
165          else
166             --  If we fall through, we have no special message to issue!
167
168             Error_Msg_N ("left hand side of assignment must be a variable", N);
169          end if;
170       end Diagnose_Non_Variable_Lhs;
171
172       --------------
173       -- Kill_LHS --
174       --------------
175
176       procedure Kill_Lhs is
177       begin
178          if Is_Entity_Name (Lhs) then
179             declare
180                Ent : constant Entity_Id := Entity (Lhs);
181             begin
182                if Present (Ent) then
183                   Kill_Current_Values (Ent);
184                end if;
185             end;
186          end if;
187       end Kill_Lhs;
188
189       -------------------------
190       -- Set_Assignment_Type --
191       -------------------------
192
193       procedure Set_Assignment_Type
194         (Opnd      : Node_Id;
195          Opnd_Type : in out Entity_Id)
196       is
197       begin
198          Require_Entity (Opnd);
199
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.
205
206          if Is_Entity_Name (Opnd)
207            and then (Ekind (Entity (Opnd)) = E_Out_Parameter
208                       or else Ekind (Entity (Opnd)) =
209                            E_In_Out_Parameter
210                       or else Ekind (Entity (Opnd)) =
211                            E_Generic_In_Out_Parameter
212                       or else
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)))) =
217                              N_Accept_Statement))
218          then
219             Opnd_Type := Get_Actual_Subtype (Opnd);
220
221          --  If assignment operand is a component reference, then we get the
222          --  actual subtype of the component for the unconstrained case.
223
224          elsif
225            (Nkind (Opnd) = N_Selected_Component
226              or else Nkind (Opnd) = N_Explicit_Dereference)
227            and then not Is_Unchecked_Union (Opnd_Type)
228          then
229             Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
230
231             if Present (Decl) then
232                Insert_Action (N, Decl);
233                Mark_Rewrite_Insertion (Decl);
234                Analyze (Decl);
235                Opnd_Type := Defining_Identifier (Decl);
236                Set_Etype (Opnd, Opnd_Type);
237                Freeze_Itype (Opnd_Type, N);
238
239             elsif Is_Constrained (Etype (Opnd)) then
240                Opnd_Type := Etype (Opnd);
241             end if;
242
243          --  For slice, use the constrained subtype created for the slice
244
245          elsif Nkind (Opnd) = N_Slice then
246             Opnd_Type := Etype (Opnd);
247          end if;
248       end Set_Assignment_Type;
249
250    --  Start of processing for Analyze_Assignment
251
252    begin
253       Analyze (Rhs);
254       Analyze (Lhs);
255
256       --  Start type analysis for assignment
257
258       T1 := Etype (Lhs);
259
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.
262
263       if Is_Overloaded (Lhs) then
264          declare
265             I  : Interp_Index;
266             It : Interp;
267
268          begin
269             T1 := Any_Type;
270             Get_First_Interp (Lhs, I, It);
271
272             while Present (It.Typ) loop
273                if Has_Compatible_Type (Rhs, It.Typ) then
274                   if T1 /= Any_Type then
275
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.
279
280                      if Nkind (Lhs) = N_Explicit_Dereference then
281                         declare
282                            PI    : Interp_Index;
283                            PI1   : Interp_Index := 0;
284                            PIt   : Interp;
285                            Found : Boolean;
286
287                         begin
288                            Found := False;
289                            Get_First_Interp (Prefix (Lhs), PI, PIt);
290
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))
295                               then
296                                  if Found then
297                                     PIt :=
298                                       Disambiguate (Prefix (Lhs),
299                                         PI1, PI, Any_Type);
300
301                                     if PIt = No_Interp then
302                                        Error_Msg_N
303                                          ("ambiguous left-hand side"
304                                             & " in assignment", Lhs);
305                                        exit;
306                                     else
307                                        Resolve (Prefix (Lhs), PIt.Typ);
308                                     end if;
309
310                                     exit;
311                                  else
312                                     Found := True;
313                                     PI1 := PI;
314                                  end if;
315                               end if;
316
317                               Get_Next_Interp (PI, PIt);
318                            end loop;
319                         end;
320
321                      else
322                         Error_Msg_N
323                           ("ambiguous left-hand side in assignment", Lhs);
324                         exit;
325                      end if;
326                   else
327                      T1 := It.Typ;
328                   end if;
329                end if;
330
331                Get_Next_Interp (I, It);
332             end loop;
333          end;
334
335          if T1 = Any_Type then
336             Error_Msg_N
337               ("no valid types for left-hand side for assignment", Lhs);
338             Kill_Lhs;
339             return;
340          end if;
341       end if;
342
343       Resolve (Lhs, T1);
344
345       if not Is_Variable (Lhs) then
346
347          --  Ada 2005 (AI-327): Check assignment to the attribute Priority of
348          --  a protected object.
349
350          declare
351             Ent : Entity_Id;
352             S   : Entity_Id;
353
354          begin
355             if Ada_Version >= Ada_05 then
356
357                --  Handle chains of renamings
358
359                Ent := Lhs;
360                while Nkind (Ent) in N_Has_Entity
361                  and then Present (Entity (Ent))
362                  and then Present (Renamed_Object (Entity (Ent)))
363                loop
364                   Ent := Renamed_Object (Entity (Ent));
365                end loop;
366
367                if (Nkind (Ent) = N_Attribute_Reference
368                      and then Attribute_Name (Ent) = Name_Priority)
369
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.
373
374                  or else
375                   (Nkind (Ent) = N_Function_Call
376                      and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
377                                 or else
378                                Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
379                then
380                   --  The enclosing subprogram cannot be a protected function
381
382                   S := Current_Scope;
383                   while not (Is_Subprogram (S)
384                                and then Convention (S) = Convention_Protected)
385                      and then S /= Standard_Standard
386                   loop
387                      S := Scope (S);
388                   end loop;
389
390                   if Ekind (S) = E_Function
391                     and then Convention (S) = Convention_Protected
392                   then
393                      Error_Msg_N
394                        ("protected function cannot modify protected object",
395                         Lhs);
396                   end if;
397
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)).
401
402                   if Locking_Policy /= 'C' then
403                      Error_Msg_N ("assignment to the attribute PRIORITY has " &
404                                   "no effect?", Lhs);
405                      Error_Msg_N ("\since no Locking_Policy has been " &
406                                   "specified", Lhs);
407                   end if;
408
409                   return;
410                end if;
411             end if;
412          end;
413
414          Diagnose_Non_Variable_Lhs (Lhs);
415          return;
416
417       elsif Is_Limited_Type (T1)
418         and then not Assignment_OK (Lhs)
419         and then not Assignment_OK (Original_Node (Lhs))
420       then
421          Error_Msg_N
422            ("left hand of assignment must not be limited type", Lhs);
423          Explain_Limited_Type (T1, Lhs);
424          return;
425       end if;
426
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.
430
431       T1 := Etype (Lhs);
432
433       --  Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
434       --  type. For example:
435
436       --    limited with P;
437       --    package Pkg is
438       --      type Acc is access P.T;
439       --    end Pkg;
440
441       --    with Pkg; use Acc;
442       --    procedure Example is
443       --       A, B : Acc;
444       --    begin
445       --       A.all := B.all;  -- ERROR
446       --    end Example;
447
448       if Nkind (Lhs) = N_Explicit_Dereference
449         and then Ekind (T1) = E_Incomplete_Type
450       then
451          Error_Msg_N ("invalid use of incomplete type", Lhs);
452          Kill_Lhs;
453          return;
454       end if;
455
456       Set_Assignment_Type (Lhs, T1);
457
458       Resolve (Rhs, T1);
459       Check_Unset_Reference (Rhs);
460
461       --  Remaining steps are skipped if Rhs was syntactically in error
462
463       if Rhs = Error then
464          Kill_Lhs;
465          return;
466       end if;
467
468       T2 := Etype (Rhs);
469
470       if not Covers (T1, T2) then
471          Wrong_Type (Rhs, Etype (Lhs));
472          Kill_Lhs;
473          return;
474       end if;
475
476       --  Ada 2005 (AI-326): In case of explicit dereference of incomplete
477       --  types, use the non-limited view if available
478
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))
483       then
484          T2 := Non_Limited_View (T2);
485       end if;
486
487       Set_Assignment_Type (Rhs, T2);
488
489       if Total_Errors_Detected /= 0 then
490          if No (T1) then
491             T1 := Any_Type;
492          end if;
493
494          if No (T2) then
495             T2 := Any_Type;
496          end if;
497       end if;
498
499       if T1 = Any_Type or else T2 = Any_Type then
500          Kill_Lhs;
501          return;
502       end if;
503
504       if (Is_Class_Wide_Type (T2) or else Is_Dynamically_Tagged (Rhs))
505         and then not Is_Class_Wide_Type (T1)
506       then
507          Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
508
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)
513       then
514          Error_Msg_N ("dynamically tagged expression required!", Rhs);
515       end if;
516
517       --  Propagate the tag from a class-wide target to the rhs when the rhs
518       --  is a tag-indeterminate call.
519
520       if Is_Class_Wide_Type (T1)
521         and then Is_Tag_Indeterminate (Rhs)
522       then
523          Propagate_Tag (Lhs, Rhs);
524       end if;
525
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.
529
530       if Ada_Version >= Ada_05
531         and then Ekind (T1) = E_Anonymous_Access_Type
532       then
533          Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
534          Analyze_And_Resolve (Rhs, T1);
535       end if;
536
537       --  Ada 2005 (AI-231)
538
539       if Ada_Version >= Ada_05
540         and then Can_Never_Be_Null (T1)
541         and then not Assignment_OK (Lhs)
542       then
543          if Nkind (Rhs) = N_Null then
544             Apply_Compile_Time_Constraint_Error
545               (N   => Rhs,
546                Msg => "(Ada 2005) NULL not allowed in null-excluding objects?",
547                Reason => CE_Null_Not_Allowed);
548             return;
549
550          elsif not Can_Never_Be_Null (T2) then
551             Rewrite (Rhs,
552               Convert_To (T1, Relocate_Node (Rhs)));
553             Analyze_And_Resolve (Rhs, T1);
554          end if;
555       end if;
556
557       if Is_Scalar_Type (T1) then
558          Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
559
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.
564
565       elsif Is_Array_Type (T1)
566         and then
567           (Nkind (Rhs) /= N_Type_Conversion
568             or else Is_Constrained (Etype (Rhs)))
569         and then
570           (Nkind (Rhs) /= N_Function_Call
571             or else Nkind (N) /= N_Block_Statement)
572       then
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
579          --  the back end.
580
581          Apply_Length_Check (Rhs, Etype (Lhs));
582
583       else
584          --  Discriminant checks are applied in the course of expansion
585
586          null;
587       end if;
588
589       --  Note: modifications of the Lhs may only be recorded after
590       --  checks have been applied.
591
592       Note_Possible_Modification (Lhs);
593
594       --  ??? a real accessibility check is needed when ???
595
596       --  Post warning for redundant assignment or variable to itself
597
598       if Warn_On_Redundant_Constructs
599
600          --  We only warn for source constructs
601
602          and then Comes_From_Source (N)
603
604          --  Where the entity is the same on both sides
605
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))
609
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.
615
616         and then Nkind (Original_Node (Rhs)) not in N_Op
617       then
618          Error_Msg_NE
619            ("?useless assignment of & to itself", N, Entity (Lhs));
620       end if;
621
622       --  Check for non-allowed composite assignment
623
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)
627       then
628          Error_Msg_CRT ("composite assignment", N);
629       end if;
630
631       --  Check elaboration warning for left side if not in elab code
632
633       if not In_Subprogram_Or_Concurrent_Unit then
634          Check_Elab_Assign (Lhs);
635       end if;
636
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.
642
643       if Is_Entity_Name (Lhs)
644         and then Nkind (N) = N_Assignment_Statement
645       then
646          declare
647             Ent : constant Entity_Id := Entity (Lhs);
648
649          begin
650             if Safe_To_Capture_Value (N, Ent) then
651
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.
658
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)
663                then
664                   Warn_On_Useless_Assignment (Ent, Sloc (N));
665                   Set_Last_Assignment (Ent, Lhs);
666                end if;
667
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.
671
672                if Is_Access_Type (T1) then
673                   if Known_Non_Null (Rhs) then
674                      Set_Is_Known_Non_Null (Ent, True);
675
676                   elsif Known_Null (Rhs)
677                     and then not Can_Never_Be_Null (Ent)
678                   then
679                      Set_Is_Known_Null (Ent, True);
680
681                   else
682                      Set_Is_Known_Null (Ent, False);
683
684                      if not Can_Never_Be_Null (Ent) then
685                         Set_Is_Known_Non_Null (Ent, False);
686                      end if;
687                   end if;
688
689                --  For discrete types, we may be able to set the current value
690                --  if the value is known at compile time.
691
692                elsif Is_Discrete_Type (T1)
693                  and then Compile_Time_Known_Value (Rhs)
694                then
695                   Set_Current_Value (Ent, Rhs);
696                else
697                   Set_Current_Value (Ent, Empty);
698                end if;
699
700             --  If not safe to capture values, kill them
701
702             else
703                Kill_Lhs;
704             end if;
705          end;
706       end if;
707    end Analyze_Assignment;
708
709    -----------------------------
710    -- Analyze_Block_Statement --
711    -----------------------------
712
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);
717
718    begin
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).
722
723       if No (HSS) then
724          return;
725       end if;
726
727       --  Normal processing with HSS present
728
729       declare
730          EH  : constant List_Id := Exception_Handlers (HSS);
731          Ent : Entity_Id        := Empty;
732          S   : Entity_Id;
733
734          Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
735          --  Recursively save value of this global, will be restored on exit
736
737       begin
738          --  Initialize unblocked exit count for statements of begin block
739          --  plus one for each excption handler that is present.
740
741          Unblocked_Exit_Count := 1;
742
743          if Present (EH) then
744             Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
745          end if;
746
747          --  If a label is present analyze it and mark it as referenced
748
749          if Present (Id) then
750             Analyze (Id);
751             Ent := Entity (Id);
752
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
756             --  an exception.
757
758             if No (Ent) then
759                if Total_Errors_Detected /= 0 then
760                   Set_Identifier (N, Empty);
761                else
762                   raise Program_Error;
763                end if;
764
765             else
766                Set_Ekind (Ent, E_Block);
767                Generate_Reference (Ent, N, ' ');
768                Generate_Definition (Ent);
769
770                if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
771                   Set_Label_Construct (Parent (Ent), N);
772                end if;
773             end if;
774          end if;
775
776          --  If no entity set, create a label entity
777
778          if No (Ent) then
779             Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
780             Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
781             Set_Parent (Ent, N);
782          end if;
783
784          Set_Etype (Ent, Standard_Void_Type);
785          Set_Block_Node (Ent, Identifier (N));
786          New_Scope (Ent);
787
788          if Present (Decls) then
789             Analyze_Declarations (Decls);
790             Check_Completion;
791          end if;
792
793          Analyze (HSS);
794          Process_End_Label (HSS, 'e', Ent);
795
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.
799
800          if Present (EH) then
801             S := Scope (Ent);
802             loop
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);
807                S := Scope (S);
808             end loop;
809          end if;
810
811          Check_References (Ent);
812          Warn_On_Useless_Assignments (Ent);
813          End_Scope;
814
815          if Unblocked_Exit_Count = 0 then
816             Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
817             Check_Unreachable_Code (N);
818          else
819             Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
820          end if;
821       end;
822    end Analyze_Block_Statement;
823
824    ----------------------------
825    -- Analyze_Case_Statement --
826    ----------------------------
827
828    procedure Analyze_Case_Statement (N : Node_Id) is
829       Exp            : Node_Id;
830       Exp_Type       : Entity_Id;
831       Exp_Btype      : Entity_Id;
832       Last_Choice    : Nat;
833       Dont_Care      : Boolean;
834       Others_Present : Boolean;
835
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.
841
842       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
843       --  Recursively save value of this global, will be restored on exit
844
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.
848
849       procedure Process_Statements (Alternative : Node_Id);
850       --  Analyzes all the statements associated to a case alternative.
851       --  Needed by the generic instantiation below.
852
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
862
863       -----------------------------
864       -- Non_Static_Choice_Error --
865       -----------------------------
866
867       procedure Non_Static_Choice_Error (Choice : Node_Id) is
868       begin
869          Flag_Non_Static_Expr
870            ("choice given in case statement is not static!", Choice);
871       end Non_Static_Choice_Error;
872
873       ------------------------
874       -- Process_Statements --
875       ------------------------
876
877       procedure Process_Statements (Alternative : Node_Id) is
878          Choices : constant List_Id := Discrete_Choices (Alternative);
879          Ent     : Entity_Id;
880
881       begin
882          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
883          Statements_Analyzed := True;
884
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.
888
889          --    case N is
890          --      when 1      => alpha
891          --      when 2 | 3  => beta
892          --      when others => gamma
893
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.
896
897          if Is_Entity_Name (Exp) then
898             Ent := Entity (Exp);
899
900             if Ekind (Ent) = E_Variable
901                  or else
902                Ekind (Ent) = E_In_Out_Parameter
903                  or else
904                Ekind (Ent) = E_Out_Parameter
905             then
906                if List_Length (Choices) = 1
907                  and then Nkind (First (Choices)) in N_Subexpr
908                  and then Compile_Time_Known_Value (First (Choices))
909                then
910                   Set_Current_Value (Entity (Exp), First (Choices));
911                end if;
912
913                Analyze_Statements (Statements (Alternative));
914
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.
918
919                Set_Current_Value (Entity (Exp), Empty);
920                return;
921             end if;
922          end if;
923
924          --  Case where expression is not an entity name of a variable
925
926          Analyze_Statements (Statements (Alternative));
927       end Process_Statements;
928
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.
931
932       Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
933
934    --  Start of processing for Analyze_Case_Statement
935
936    begin
937       Unblocked_Exit_Count := 0;
938       Exp := Expression (N);
939       Analyze (Exp);
940
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.
947
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)))
953       then
954          Resolve (Exp, Etype (Exp));
955          Exp_Type := Full_View (Etype (Exp));
956
957       else
958          Analyze_And_Resolve (Exp, Any_Discrete);
959          Exp_Type := Etype (Exp);
960       end if;
961
962       Check_Unset_Reference (Exp);
963       Exp_Btype := Base_Type (Exp_Type);
964
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.
970
971       --  If error already reported by Resolve, nothing more to do
972
973       if Exp_Btype = Any_Discrete
974         or else Exp_Btype = Any_Type
975       then
976          return;
977
978       elsif Exp_Btype = Any_Character then
979          Error_Msg_N
980            ("character literal as case expression is ambiguous", Exp);
981          return;
982
983       elsif Ada_Version = Ada_83
984         and then (Is_Generic_Type (Exp_Btype)
985                     or else Is_Generic_Type (Root_Type (Exp_Btype)))
986       then
987          Error_Msg_N
988            ("(Ada 83) case expression cannot be of a generic type", Exp);
989          return;
990       end if;
991
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.
996
997       if Paren_Count (Exp) > 0
998         or else (Is_Entity_Name (Exp)
999                   and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1000       then
1001          Exp_Type := Exp_Btype;
1002       end if;
1003
1004       --  Call instantiated Analyze_Choices which does the rest of the work
1005
1006       Analyze_Choices
1007         (N, Exp_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
1008
1009       if Exp_Type = Universal_Integer and then not Others_Present then
1010          Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1011       end if;
1012
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.
1017
1018       if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1019          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1020          Check_Unreachable_Code (N);
1021       else
1022          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1023       end if;
1024
1025       if not Expander_Active
1026         and then Compile_Time_Known_Value (Expression (N))
1027         and then Serious_Errors_Detected = 0
1028       then
1029          declare
1030             Chosen : constant Node_Id := Find_Static_Alternative (N);
1031             Alt    : Node_Id;
1032
1033          begin
1034             Alt := First (Alternatives (N));
1035
1036             while Present (Alt) loop
1037                if Alt /= Chosen then
1038                   Remove_Warning_Messages (Statements (Alt));
1039                end if;
1040
1041                Next (Alt);
1042             end loop;
1043          end;
1044       end if;
1045    end Analyze_Case_Statement;
1046
1047    ----------------------------
1048    -- Analyze_Exit_Statement --
1049    ----------------------------
1050
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.
1054
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;
1059       U_Name   : Entity_Id;
1060       Kind     : Entity_Kind;
1061
1062    begin
1063       if No (Cond) then
1064          Check_Unreachable_Code (N);
1065       end if;
1066
1067       if Present (Target) then
1068          Analyze (Target);
1069          U_Name := Entity (Target);
1070
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);
1073             return;
1074          else
1075             Set_Has_Exit (U_Name);
1076          end if;
1077
1078       else
1079          U_Name := Empty;
1080       end if;
1081
1082       for J in reverse 0 .. Scope_Stack.Last loop
1083          Scope_Id := Scope_Stack.Table (J).Entity;
1084          Kind := Ekind (Scope_Id);
1085
1086          if Kind = E_Loop
1087            and then (No (Target) or else Scope_Id = U_Name) then
1088             Set_Has_Exit (Scope_Id);
1089             exit;
1090
1091          elsif Kind = E_Block
1092            or else Kind = E_Loop
1093            or else Kind = E_Return_Statement
1094          then
1095             null;
1096
1097          else
1098             Error_Msg_N
1099               ("cannot exit from program unit or accept statement", N);
1100             exit;
1101          end if;
1102       end loop;
1103
1104       --  Verify that if present the condition is a Boolean expression
1105
1106       if Present (Cond) then
1107          Analyze_And_Resolve (Cond, Any_Boolean);
1108          Check_Unset_Reference (Cond);
1109       end if;
1110    end Analyze_Exit_Statement;
1111
1112    ----------------------------
1113    -- Analyze_Goto_Statement --
1114    ----------------------------
1115
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;
1120
1121    begin
1122       Check_Unreachable_Code (N);
1123
1124       Analyze (Label);
1125
1126       if Entity (Label) = Any_Id then
1127          return;
1128
1129       elsif Ekind (Entity (Label)) /= E_Label then
1130          Error_Msg_N ("target of goto statement must be a label", Label);
1131          return;
1132
1133       elsif not Reachable (Entity (Label)) then
1134          Error_Msg_N ("target of goto statement is not reachable", Label);
1135          return;
1136       end if;
1137
1138       Label_Scope := Enclosing_Scope (Entity (Label));
1139
1140       for J in reverse 0 .. Scope_Stack.Last loop
1141          Scope_Id := Scope_Stack.Table (J).Entity;
1142
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)
1147          then
1148             if Scope_Id /= Label_Scope then
1149                Error_Msg_N
1150                  ("cannot exit from program unit or accept statement", N);
1151             end if;
1152
1153             return;
1154          end if;
1155       end loop;
1156
1157       raise Program_Error;
1158    end Analyze_Goto_Statement;
1159
1160    --------------------------
1161    -- Analyze_If_Statement --
1162    --------------------------
1163
1164    --  A special complication arises in the analysis of if statements
1165
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.
1175
1176    procedure Analyze_If_Statement (N : Node_Id) is
1177       E : Node_Id;
1178
1179       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1180       --  Recursively save value of this global, will be restored on exit
1181
1182       Save_In_Deleted_Code : Boolean;
1183
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.
1187
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.
1192
1193       -----------------------
1194       -- Analyze_Cond_Then --
1195       -----------------------
1196
1197       procedure Analyze_Cond_Then (Cnode : Node_Id) is
1198          Cond : constant Node_Id := Condition (Cnode);
1199          Tstm : constant List_Id := Then_Statements (Cnode);
1200
1201       begin
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);
1206
1207          --  If already deleting, then just analyze then statements
1208
1209          if Del then
1210             Analyze_Statements (Tstm);
1211
1212          --  Compile time known value, not deleting yet
1213
1214          elsif Compile_Time_Known_Value (Cond) then
1215             Save_In_Deleted_Code := In_Deleted_Code;
1216
1217             --  If condition is True, then analyze the THEN statements
1218             --  and set no expansion for ELSE and ELSIF parts.
1219
1220             if Is_True (Expr_Value (Cond)) then
1221                Analyze_Statements (Tstm);
1222                Del := True;
1223                Expander_Mode_Save_And_Set (False);
1224                In_Deleted_Code := True;
1225
1226             --  If condition is False, analyze THEN with expansion off
1227
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;
1234             end if;
1235
1236          --  Not known at compile time, not deleting, normal analysis
1237
1238          else
1239             Analyze_Statements (Tstm);
1240          end if;
1241       end Analyze_Cond_Then;
1242
1243    --  Start of Analyze_If_Statement
1244
1245    begin
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.
1249
1250       Unblocked_Exit_Count := 1;
1251       Analyze_Cond_Then (N);
1252
1253       --  Now to analyze the elsif parts if any are present
1254
1255       if Present (Elsif_Parts (N)) then
1256          E := First (Elsif_Parts (N));
1257          while Present (E) loop
1258             Analyze_Cond_Then (E);
1259             Next (E);
1260          end loop;
1261       end if;
1262
1263       if Present (Else_Statements (N)) then
1264          Analyze_Statements (Else_Statements (N));
1265       end if;
1266
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.
1270
1271       if Unblocked_Exit_Count = 0 then
1272          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1273          Check_Unreachable_Code (N);
1274       else
1275          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1276       end if;
1277
1278       if Del then
1279          Expander_Mode_Restore;
1280          In_Deleted_Code := Save_In_Deleted_Code;
1281       end if;
1282
1283       if not Expander_Active
1284         and then Compile_Time_Known_Value (Condition (N))
1285         and then Serious_Errors_Detected = 0
1286       then
1287          if Is_True (Expr_Value (Condition (N))) then
1288             Remove_Warning_Messages (Else_Statements (N));
1289
1290             if Present (Elsif_Parts (N)) then
1291                E := First (Elsif_Parts (N));
1292
1293                while Present (E) loop
1294                   Remove_Warning_Messages (Then_Statements (E));
1295                   Next (E);
1296                end loop;
1297             end if;
1298
1299          else
1300             Remove_Warning_Messages (Then_Statements (N));
1301          end if;
1302       end if;
1303    end Analyze_If_Statement;
1304
1305    ----------------------------------------
1306    -- Analyze_Implicit_Label_Declaration --
1307    ----------------------------------------
1308
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.
1312
1313    --  Note: any changes in this routine may need to be reflected in
1314    --  Analyze_Label_Entity.
1315
1316    procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1317       Id : constant Node_Id := Defining_Identifier (N);
1318    begin
1319       Enter_Name          (Id);
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;
1324
1325    ------------------------------
1326    -- Analyze_Iteration_Scheme --
1327    ------------------------------
1328
1329    procedure Analyze_Iteration_Scheme (N : Node_Id) is
1330
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.
1338
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.
1344
1345       --------------------
1346       -- Process_Bounds --
1347       --------------------
1348
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;
1356          Typ          : Entity_Id;
1357          Save_Analysis : Boolean;
1358
1359          function One_Bound
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.
1366
1367          ---------------
1368          -- One_Bound --
1369          ---------------
1370
1371          function One_Bound
1372            (Original_Bound : Node_Id;
1373             Analyzed_Bound : Node_Id) return Node_Id
1374          is
1375             Assign : Node_Id;
1376             Id     : Entity_Id;
1377             Decl   : Node_Id;
1378
1379          begin
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
1385             --  type Integer).
1386
1387             if Analyzed (Original_Bound) then
1388                return Original_Bound;
1389
1390             elsif Nkind (Analyzed_Bound) = N_Integer_Literal
1391               or else Is_Entity_Name (Analyzed_Bound)
1392             then
1393                Analyze_And_Resolve (Original_Bound, Typ);
1394                return Original_Bound;
1395
1396             else
1397                Analyze_And_Resolve (Original_Bound, Typ);
1398             end if;
1399
1400             Id :=
1401               Make_Defining_Identifier (Loc,
1402                 Chars => New_Internal_Name ('S'));
1403
1404             Decl :=
1405               Make_Object_Declaration (Loc,
1406                 Defining_Identifier => Id,
1407                 Object_Definition   => New_Occurrence_Of (Typ, Loc));
1408
1409             Insert_Before (Parent (N), Decl);
1410             Analyze (Decl);
1411
1412             Assign :=
1413               Make_Assignment_Statement (Loc,
1414                 Name        => New_Occurrence_Of (Id, Loc),
1415                 Expression  => Relocate_Node (Original_Bound));
1416
1417             Insert_Before (Parent (N), Assign);
1418             Analyze (Assign);
1419
1420             Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
1421
1422             if Nkind (Assign) = N_Assignment_Statement then
1423                return Expression (Assign);
1424             else
1425                return Original_Bound;
1426             end if;
1427          end One_Bound;
1428
1429       --  Start of processing for Process_Bounds
1430
1431       begin
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.
1437
1438          Set_Parent (R_Copy, Parent (R));
1439          Save_Analysis := Full_Analysis;
1440          Full_Analysis := False;
1441          Expander_Mode_Save_And_Set (False);
1442
1443          Analyze (R_Copy);
1444
1445          if Is_Overloaded (R_Copy) then
1446
1447             --  Apply preference rules for range of predefined integer types,
1448             --  or diagnose true ambiguity.
1449
1450             declare
1451                I     : Interp_Index;
1452                It    : Interp;
1453                Found : Entity_Id := Empty;
1454
1455             begin
1456                Get_First_Interp (R_Copy, I, It);
1457                while Present (It.Typ) loop
1458                   if Is_Discrete_Type (It.Typ) then
1459                      if No (Found) then
1460                         Found := It.Typ;
1461                      else
1462                         if Scope (Found) = Standard_Standard then
1463                            null;
1464
1465                         elsif Scope (It.Typ) = Standard_Standard then
1466                            Found := It.Typ;
1467
1468                         else
1469                            --  Both of them are user-defined
1470
1471                            Error_Msg_N
1472                              ("ambiguous bounds in range of iteration",
1473                                R_Copy);
1474                            Error_Msg_N ("\possible interpretations:", R_Copy);
1475                            Error_Msg_NE ("\\} ", R_Copy, Found);
1476                            Error_Msg_NE ("\\} ", R_Copy, It.Typ);
1477                            exit;
1478                         end if;
1479                      end if;
1480                   end if;
1481
1482                   Get_Next_Interp (I, It);
1483                end loop;
1484             end;
1485          end if;
1486
1487          Resolve (R_Copy);
1488          Expander_Mode_Restore;
1489          Full_Analysis := Save_Analysis;
1490
1491          Typ := Etype (R_Copy);
1492
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-
1497          --  defined type.
1498
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
1503             then
1504                Typ := Etype (Lo);
1505
1506             elsif Nkind (Hi) = N_Integer_Literal
1507               and then Present (Etype (Hi))
1508               and then Scope (Etype (Hi)) /= Standard_Standard
1509             then
1510                Typ := Etype (Hi);
1511
1512             else
1513                Typ := Standard_Integer;
1514             end if;
1515          end if;
1516
1517          Set_Etype (R, Typ);
1518
1519          New_Lo_Bound := One_Bound (Lo, Low_Bound  (R_Copy));
1520          New_Hi_Bound := One_Bound (Hi, High_Bound (R_Copy));
1521
1522          --  Propagate staticness to loop range itself, in case the
1523          --  corresponding subtype is static.
1524
1525          if New_Lo_Bound /= Lo
1526            and then Is_Static_Expression (New_Lo_Bound)
1527          then
1528             Rewrite  (Low_Bound (R), New_Copy (New_Lo_Bound));
1529          end if;
1530
1531          if New_Hi_Bound /= Hi
1532            and then Is_Static_Expression (New_Hi_Bound)
1533          then
1534             Rewrite (High_Bound (R), New_Copy (New_Hi_Bound));
1535          end if;
1536       end Process_Bounds;
1537
1538       --------------------------------------
1539       -- Check_Controlled_Array_Attribute --
1540       --------------------------------------
1541
1542       procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1543       begin
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))))
1548             and then
1549               Is_Controlled (
1550                 Component_Type (Etype (Entity (Prefix (DS)))))
1551             and then Expander_Active
1552          then
1553             declare
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'));
1562                Decl : Node_Id;
1563
1564             begin
1565                Decl :=
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),
1571                         Constraint =>
1572                           Make_Range_Constraint (Loc,
1573                             Relocate_Node (DS))));
1574                Insert_Before (Parent (N), Decl);
1575                Analyze (Decl);
1576
1577                Rewrite (DS,
1578                   Make_Attribute_Reference (Loc,
1579                     Prefix => New_Reference_To (Subt, Loc),
1580                     Attribute_Name => Attribute_Name (DS)));
1581                Analyze (DS);
1582             end;
1583          end if;
1584       end Check_Controlled_Array_Attribute;
1585
1586    --  Start of processing for Analyze_Iteration_Scheme
1587
1588    begin
1589       --  For an infinite loop, there is no iteration scheme
1590
1591       if No (N) then
1592          return;
1593
1594       else
1595          declare
1596             Cond : constant Node_Id := Condition (N);
1597
1598          begin
1599             --  For WHILE loop, verify that the condition is a Boolean
1600             --  expression and resolve and check it.
1601
1602             if Present (Cond) then
1603                Analyze_And_Resolve (Cond, Any_Boolean);
1604                Check_Unset_Reference (Cond);
1605                Set_Current_Value_Condition (N);
1606                return;
1607
1608             --  Else we have a FOR loop
1609
1610             else
1611                declare
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);
1615
1616                begin
1617                   Enter_Name (Id);
1618
1619                   --  We always consider the loop variable to be referenced,
1620                   --  since the loop may be used just for counting purposes.
1621
1622                   Generate_Reference (Id, N, ' ');
1623
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).
1627
1628                   declare
1629                      H : constant Entity_Id := Homonym (Id);
1630                   begin
1631                      if Present (H)
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))
1636                      then
1637                         Set_Hiding_Loop_Variable (H, Id);
1638                      end if;
1639                   end;
1640
1641                   --  Now analyze the subtype definition. If it is
1642                   --  a range, create temporaries for bounds.
1643
1644                   if Nkind (DS) = N_Range
1645                     and then Expander_Active
1646                   then
1647                      Process_Bounds (DS);
1648                   else
1649                      Analyze (DS);
1650                   end if;
1651
1652                   if DS = Error then
1653                      return;
1654                   end if;
1655
1656                   --  The subtype indication may denote the completion
1657                   --  of an incomplete type declaration.
1658
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
1663                   then
1664                      Set_Entity (DS, Get_Full_View (Entity (DS)));
1665                      Set_Etype  (DS, Entity (DS));
1666                   end if;
1667
1668                   if not Is_Discrete_Type (Etype (DS)) then
1669                      Wrong_Type (DS, Any_Discrete);
1670                      Set_Etype (DS, Any_Type);
1671                   end if;
1672
1673                   Check_Controlled_Array_Attribute (DS);
1674
1675                   Make_Index (DS, LP);
1676
1677                   Set_Ekind          (Id, E_Loop_Parameter);
1678                   Set_Etype          (Id, Etype (DS));
1679                   Set_Is_Known_Valid (Id, True);
1680
1681                   --  The loop is not a declarative part, so the only entity
1682                   --  declared "within" must be frozen explicitly.
1683
1684                   declare
1685                      Flist : constant List_Id := Freeze_Entity (Id, Sloc (N));
1686                   begin
1687                      if Is_Non_Empty_List (Flist) then
1688                         Insert_Actions (N, Flist);
1689                      end if;
1690                   end;
1691
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
1695                   --  in these cases.
1696
1697                   if Nkind (DS) = N_Range
1698                     and then Comes_From_Source (N)
1699                   then
1700                      declare
1701                         L : constant Node_Id := Low_Bound  (DS);
1702                         H : constant Node_Id := High_Bound (DS);
1703
1704                         Llo : Uint;
1705                         Lhi : Uint;
1706                         LOK : Boolean;
1707                         Hlo : Uint;
1708                         Hhi : Uint;
1709                         HOK : Boolean;
1710
1711                      begin
1712                         Determine_Range (L, LOK, Llo, Lhi);
1713                         Determine_Range (H, HOK, Hlo, Hhi);
1714
1715                         --  If range of loop is null, issue warning
1716
1717                         if (LOK and HOK) and then Llo > Hhi then
1718
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.
1723
1724                            if not Inside_A_Generic
1725                               and then not In_Instance
1726                            then
1727                               Error_Msg_N
1728                                 ("?loop range is null, loop will not execute",
1729                                  DS);
1730                            end if;
1731
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!
1740
1741                            Set_Is_Null_Loop (Parent (N));
1742
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.
1746
1747                         --  For example, we have
1748
1749                         --     for J in reverse N .. 1 loop
1750
1751                         --  In practice, this is very likely to be a case
1752                         --  of reversing the bounds incorrectly in the range.
1753
1754                         elsif Reverse_Present (LP)
1755                           and then Nkind (Original_Node (H)) =
1756                                                           N_Integer_Literal
1757                           and then (Intval (H) = Uint_0
1758                                       or else
1759                                     Intval (H) = Uint_1)
1760                           and then Lhi > Hhi
1761                         then
1762                            Error_Msg_N ("?loop range may be null", DS);
1763                            Error_Msg_N ("\?bounds may be wrong way round", DS);
1764                         end if;
1765                      end;
1766                   end if;
1767                end;
1768             end if;
1769          end;
1770       end if;
1771    end Analyze_Iteration_Scheme;
1772
1773    -------------------
1774    -- Analyze_Label --
1775    -------------------
1776
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.
1783
1784    procedure Analyze_Label (N : Node_Id) is
1785       pragma Warnings (Off, N);
1786    begin
1787       Kill_Current_Values;
1788    end Analyze_Label;
1789
1790    --------------------------
1791    -- Analyze_Label_Entity --
1792    --------------------------
1793
1794    procedure Analyze_Label_Entity (E : Entity_Id) is
1795    begin
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;
1801
1802    ----------------------------
1803    -- Analyze_Loop_Statement --
1804    ----------------------------
1805
1806    procedure Analyze_Loop_Statement (N : Node_Id) is
1807       Id   : constant Node_Id := Identifier (N);
1808       Iter : constant Node_Id := Iteration_Scheme (N);
1809       Ent  : Entity_Id;
1810
1811    begin
1812       if Present (Id) then
1813
1814          --  Make name visible, e.g. for use in exit statements. Loop
1815          --  labels are always considered to be referenced.
1816
1817          Analyze (Id);
1818          Ent := Entity (Id);
1819          Generate_Reference  (Ent, N, ' ');
1820          Generate_Definition (Ent);
1821
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.
1827
1828          if Ekind (Ent) = E_Label then
1829             Set_Ekind (Ent, E_Loop);
1830
1831             if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1832                Set_Label_Construct (Parent (Ent), N);
1833             end if;
1834          end if;
1835
1836       --  Case of no identifier present
1837
1838       else
1839          Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
1840          Set_Etype (Ent,  Standard_Void_Type);
1841          Set_Parent (Ent, N);
1842       end if;
1843
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.
1848
1849       Kill_Current_Values;
1850       New_Scope (Ent);
1851       Analyze_Iteration_Scheme (Iter);
1852       Analyze_Statements (Statements (N));
1853       Process_End_Label (N, 'e', Ent);
1854       End_Scope;
1855       Kill_Current_Values;
1856
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.
1861
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.
1864
1865       if No (Iter)
1866         or else No (Condition (Iter))
1867         or else Present (Condition_Actions (Iter))
1868         or else Debug_Flag_Dot_W
1869       then
1870          return;
1871       end if;
1872
1873       --  Initial conditions met, see if condition is of right form
1874
1875       declare
1876          Cond : constant Node_Id := Condition (Iter);
1877          Var  : Entity_Id;
1878          Loc  : Node_Id;
1879
1880       begin
1881          --  Condition is a direct variable reference
1882
1883          if Is_Entity_Name (Cond)
1884            and then not Is_Library_Level_Entity (Entity (Cond))
1885          then
1886             Loc := Cond;
1887
1888          --  Case of condition is a comparison with compile time known value
1889
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))
1893             then
1894                Loc := Left_Opnd (Cond);
1895
1896             elsif Is_Entity_Name (Right_Opnd (Cond))
1897               and then Compile_Time_Known_Value (Left_Opnd (Cond))
1898             then
1899                Loc := Right_Opnd (Cond);
1900
1901             else
1902                return;
1903             end if;
1904
1905          --  Case of condition is function call with one parameter
1906
1907          elsif Nkind (Cond) = N_Function_Call then
1908             declare
1909                PA : constant List_Id := Parameter_Associations (Cond);
1910             begin
1911                if Present (PA)
1912                  and then List_Length (PA) = 1
1913                  and then Is_Entity_Name (First (PA))
1914                then
1915                   Loc := First (PA);
1916                else
1917                   return;
1918                end if;
1919             end;
1920
1921          else
1922             return;
1923          end if;
1924
1925          --  If we fall through Loc is set to the node that is an entity ref
1926
1927          Var := Entity (Loc);
1928
1929          if Present (Var)
1930            and then Ekind (Var) = E_Variable
1931            and then not Is_Library_Level_Entity (Var)
1932            and then Comes_From_Source (Var)
1933          then
1934             null;
1935          else
1936             return;
1937          end if;
1938
1939          --  Search for reference to variable in loop
1940
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.
1945
1946             function Find_Ref is new Traverse_Func (Test_Ref);
1947             --  Function to traverse body of procedure. Returns Abandon if
1948             --  matching reference found.
1949
1950             --------------
1951             -- Test_Ref --
1952             --------------
1953
1954             function Test_Ref (N : Node_Id) return Traverse_Result is
1955             begin
1956                --  Waste of time to look at iteration scheme
1957
1958                if N = Iter then
1959                   return Skip;
1960
1961                --  Direct reference to variable in question
1962
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)
1967                then
1968                   return Abandon;
1969
1970                   --  Reference to variable renaming variable in question
1971
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)
1979                then
1980                   return Abandon;
1981
1982                --  Check for call to other than library level subprogram
1983
1984                elsif Nkind (N) = N_Procedure_Call_Statement
1985                  or else Nkind (N) = N_Function_Call
1986                then
1987                   if not Is_Entity_Name (Name (N))
1988                     or else not Is_Library_Level_Entity (Entity (Name (N)))
1989                   then
1990                      return Abandon;
1991                   end if;
1992                end if;
1993
1994                --  All OK, continue scan
1995
1996                return OK;
1997             end Test_Ref;
1998
1999          --  Start of processing for Ref_Search
2000
2001          begin
2002             if Find_Ref (N) = OK then
2003                Error_Msg_NE
2004                  ("variable& is not modified in loop body?", Loc, Var);
2005                Error_Msg_N
2006                  ("\possible infinite loop", Loc);
2007             end if;
2008          end Ref_Search;
2009       end;
2010    end Analyze_Loop_Statement;
2011
2012    ----------------------------
2013    -- Analyze_Null_Statement --
2014    ----------------------------
2015
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!
2018
2019    procedure Analyze_Null_Statement (N : Node_Id) is
2020       pragma Warnings (Off, N);
2021    begin
2022       null;
2023    end Analyze_Null_Statement;
2024
2025    ------------------------
2026    -- Analyze_Statements --
2027    ------------------------
2028
2029    procedure Analyze_Statements (L : List_Id) is
2030       S   : Node_Id;
2031       Lab : Entity_Id;
2032
2033    begin
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!
2038
2039       S := First (L);
2040       while Present (S) loop
2041          if Nkind (S) = N_Label then
2042             Analyze (Identifier (S));
2043             Lab := Entity (Identifier (S));
2044
2045             --  If we found a label mark it as reachable
2046
2047             if Ekind (Lab) = E_Label then
2048                Generate_Definition (Lab);
2049                Set_Reachable (Lab);
2050
2051                if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
2052                   Set_Label_Construct (Parent (Lab), S);
2053                end if;
2054
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
2059             --  statement.
2060
2061             else
2062                Error_Msg_Sloc := Sloc (Lab);
2063                Error_Msg_N
2064                  ("implicit label declaration for & is hidden#",
2065                   Identifier (S));
2066             end if;
2067          end if;
2068
2069          Next (S);
2070       end loop;
2071
2072       --  Perform semantic analysis on all statements
2073
2074       Conditional_Statements_Begin;
2075
2076       S := First (L);
2077       while Present (S) loop
2078          Analyze (S);
2079          Next (S);
2080       end loop;
2081
2082       Conditional_Statements_End;
2083
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.
2088
2089       S := First (L);
2090       while Present (S) loop
2091          if Nkind (S) = N_Label then
2092             Set_Reachable (Entity (Identifier (S)), False);
2093          end if;
2094
2095          Next (S);
2096       end loop;
2097    end Analyze_Statements;
2098
2099    ----------------------------
2100    -- Check_Unreachable_Code --
2101    ----------------------------
2102
2103    procedure Check_Unreachable_Code (N : Node_Id) is
2104       Error_Loc : Source_Ptr;
2105       P         : Node_Id;
2106
2107    begin
2108       if Is_List_Member (N)
2109         and then Comes_From_Source (N)
2110       then
2111          declare
2112             Nxt : Node_Id;
2113
2114          begin
2115             Nxt := Original_Node (Next (N));
2116
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.
2119
2120             if Nkind (Nxt) = N_Label then
2121                return;
2122
2123             --  Otherwise see if we have a real statement following us
2124
2125             elsif Present (Nxt)
2126               and then Comes_From_Source (Nxt)
2127               and then Is_Statement (Nxt)
2128             then
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!
2132
2133                if Nkind (Original_Node (N)) /= N_Raise_Statement
2134                  or else Nkind (Nxt) /= N_Return_Statement
2135                then
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 :-)
2140
2141                   Error_Loc := Sloc (Nxt);
2142
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.
2146
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.
2152
2153                   --  Note that one might react by moving the whole circuit to
2154                   --  exp_ch5, but then we lose the warning in -gnatc mode.
2155
2156                   if Operating_Mode = Generate_Code then
2157                      loop
2158                         Nxt := Next (N);
2159
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).
2163
2164                         exit when No (Nxt) or else Nkind (Nxt) = N_Label;
2165
2166                         --  Statement/declaration is to be deleted
2167
2168                         Analyze (Nxt);
2169                         Remove (Nxt);
2170                         Kill_Dead_Code (Nxt);
2171                      end loop;
2172                   end if;
2173
2174                   --  Now issue the warning
2175
2176                   Error_Msg ("?unreachable code", Error_Loc);
2177                end if;
2178
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.
2183
2184             else
2185                P := Parent (N);
2186
2187                --  Statements in THEN part or ELSE part of IF statement
2188
2189                if Nkind (P) = N_If_Statement then
2190                   null;
2191
2192                --  Statements in ELSIF part of an IF statement
2193
2194                elsif Nkind (P) = N_Elsif_Part then
2195                   P := Parent (P);
2196                   pragma Assert (Nkind (P) = N_If_Statement);
2197
2198                --  Statements in CASE statement alternative
2199
2200                elsif Nkind (P) = N_Case_Statement_Alternative then
2201                   P := Parent (P);
2202                   pragma Assert (Nkind (P) = N_Case_Statement);
2203
2204                --  Statements in body of block
2205
2206                elsif Nkind (P) = N_Handled_Sequence_Of_Statements
2207                  and then Nkind (Parent (P)) = N_Block_Statement
2208                then
2209                   null;
2210
2211                --  Statements in exception handler in a block
2212
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
2216                then
2217                   null;
2218
2219                --  None of these cases, so return
2220
2221                else
2222                   return;
2223                end if;
2224
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.
2227
2228                Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
2229             end if;
2230          end;
2231       end if;
2232    end Check_Unreachable_Code;
2233
2234 end Sem_Ch5;