OSDN Git Service

2007-08-31 Hristian Kirtchev <kirtchev@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-2007, 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 Einfo;    use Einfo;
30 with Errout;   use Errout;
31 with Expander; use Expander;
32 with Exp_Util; use Exp_Util;
33 with Freeze;   use Freeze;
34 with Lib;      use Lib;
35 with Lib.Xref; use Lib.Xref;
36 with Namet;    use Namet;
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 not
64    --  blocked by unconditional transfer instructions: for IF and CASE, these
65    --  are the branches of the conditional; for a block, they are the statement
66    --  sequence of the block, and the statement sequences of any exception
67    --  handlers that are part of the block. When processing is complete, if
68    --  this count is zero, it means that control cannot fall through the IF,
69    --  CASE or block statement. This is used for the generation of warning
70    --  messages. This variable is recursively saved on entry to processing the
71    --  construct, and restored on exit.
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       Mark_Coextensions (N, Rhs);
254
255       Analyze (Rhs);
256       Analyze (Lhs);
257
258       --  Start type analysis for assignment
259
260       T1 := Etype (Lhs);
261
262       --  In the most general case, both Lhs and Rhs can be overloaded, and we
263       --  must compute the intersection of the possible types on each side.
264
265       if Is_Overloaded (Lhs) then
266          declare
267             I  : Interp_Index;
268             It : Interp;
269
270          begin
271             T1 := Any_Type;
272             Get_First_Interp (Lhs, I, It);
273
274             while Present (It.Typ) loop
275                if Has_Compatible_Type (Rhs, It.Typ) then
276                   if T1 /= Any_Type then
277
278                      --  An explicit dereference is overloaded if the prefix
279                      --  is. Try to remove the ambiguity on the prefix, the
280                      --  error will be posted there if the ambiguity is real.
281
282                      if Nkind (Lhs) = N_Explicit_Dereference then
283                         declare
284                            PI    : Interp_Index;
285                            PI1   : Interp_Index := 0;
286                            PIt   : Interp;
287                            Found : Boolean;
288
289                         begin
290                            Found := False;
291                            Get_First_Interp (Prefix (Lhs), PI, PIt);
292
293                            while Present (PIt.Typ) loop
294                               if Is_Access_Type (PIt.Typ)
295                                 and then Has_Compatible_Type
296                                            (Rhs, Designated_Type (PIt.Typ))
297                               then
298                                  if Found then
299                                     PIt :=
300                                       Disambiguate (Prefix (Lhs),
301                                         PI1, PI, Any_Type);
302
303                                     if PIt = No_Interp then
304                                        Error_Msg_N
305                                          ("ambiguous left-hand side"
306                                             & " in assignment", Lhs);
307                                        exit;
308                                     else
309                                        Resolve (Prefix (Lhs), PIt.Typ);
310                                     end if;
311
312                                     exit;
313                                  else
314                                     Found := True;
315                                     PI1 := PI;
316                                  end if;
317                               end if;
318
319                               Get_Next_Interp (PI, PIt);
320                            end loop;
321                         end;
322
323                      else
324                         Error_Msg_N
325                           ("ambiguous left-hand side in assignment", Lhs);
326                         exit;
327                      end if;
328                   else
329                      T1 := It.Typ;
330                   end if;
331                end if;
332
333                Get_Next_Interp (I, It);
334             end loop;
335          end;
336
337          if T1 = Any_Type then
338             Error_Msg_N
339               ("no valid types for left-hand side for assignment", Lhs);
340             Kill_Lhs;
341             return;
342          end if;
343       end if;
344
345       --  The resulting assignment type is T1, so now we will resolve the
346       --  left hand side of the assignment using this determined type.
347
348       Resolve (Lhs, T1);
349
350       --  Cases where Lhs is not a variable
351
352       if not Is_Variable (Lhs) then
353
354          --  Ada 2005 (AI-327): Check assignment to the attribute Priority of
355          --  a protected object.
356
357          declare
358             Ent : Entity_Id;
359             S   : Entity_Id;
360
361          begin
362             if Ada_Version >= Ada_05 then
363
364                --  Handle chains of renamings
365
366                Ent := Lhs;
367                while Nkind (Ent) in N_Has_Entity
368                  and then Present (Entity (Ent))
369                  and then Present (Renamed_Object (Entity (Ent)))
370                loop
371                   Ent := Renamed_Object (Entity (Ent));
372                end loop;
373
374                if (Nkind (Ent) = N_Attribute_Reference
375                      and then Attribute_Name (Ent) = Name_Priority)
376
377                   --  Renamings of the attribute Priority applied to protected
378                   --  objects have been previously expanded into calls to the
379                   --  Get_Ceiling run-time subprogram.
380
381                  or else
382                   (Nkind (Ent) = N_Function_Call
383                      and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
384                                 or else
385                                Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
386                then
387                   --  The enclosing subprogram cannot be a protected function
388
389                   S := Current_Scope;
390                   while not (Is_Subprogram (S)
391                                and then Convention (S) = Convention_Protected)
392                      and then S /= Standard_Standard
393                   loop
394                      S := Scope (S);
395                   end loop;
396
397                   if Ekind (S) = E_Function
398                     and then Convention (S) = Convention_Protected
399                   then
400                      Error_Msg_N
401                        ("protected function cannot modify protected object",
402                         Lhs);
403                   end if;
404
405                   --  Changes of the ceiling priority of the protected object
406                   --  are only effective if the Ceiling_Locking policy is in
407                   --  effect (AARM D.5.2 (5/2)).
408
409                   if Locking_Policy /= 'C' then
410                      Error_Msg_N ("assignment to the attribute PRIORITY has " &
411                                   "no effect?", Lhs);
412                      Error_Msg_N ("\since no Locking_Policy has been " &
413                                   "specified", Lhs);
414                   end if;
415
416                   return;
417                end if;
418             end if;
419          end;
420
421          Diagnose_Non_Variable_Lhs (Lhs);
422          return;
423
424       --  Error of assigning to limited type. We do however allow this in
425       --  certain cases where the front end generates the assignments.
426
427       elsif Is_Limited_Type (T1)
428         and then not Assignment_OK (Lhs)
429         and then not Assignment_OK (Original_Node (Lhs))
430         and then not Is_Value_Type (T1)
431       then
432          Error_Msg_N
433            ("left hand of assignment must not be limited type", Lhs);
434          Explain_Limited_Type (T1, Lhs);
435          return;
436       end if;
437
438       --  Resolution may have updated the subtype, in case the left-hand
439       --  side is a private protected component. Use the correct subtype
440       --  to avoid scoping issues in the back-end.
441
442       T1 := Etype (Lhs);
443
444       --  Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
445       --  type. For example:
446
447       --    limited with P;
448       --    package Pkg is
449       --      type Acc is access P.T;
450       --    end Pkg;
451
452       --    with Pkg; use Acc;
453       --    procedure Example is
454       --       A, B : Acc;
455       --    begin
456       --       A.all := B.all;  -- ERROR
457       --    end Example;
458
459       if Nkind (Lhs) = N_Explicit_Dereference
460         and then Ekind (T1) = E_Incomplete_Type
461       then
462          Error_Msg_N ("invalid use of incomplete type", Lhs);
463          Kill_Lhs;
464          return;
465       end if;
466
467       --  Now we can complete the resolution of the right hand side
468
469       Set_Assignment_Type (Lhs, T1);
470       Resolve (Rhs, T1);
471
472       --  This is the point at which we check for an unset reference
473
474       Check_Unset_Reference (Rhs);
475
476       --  Remaining steps are skipped if Rhs was syntactically in error
477
478       if Rhs = Error then
479          Kill_Lhs;
480          return;
481       end if;
482
483       T2 := Etype (Rhs);
484
485       if not Covers (T1, T2) then
486          Wrong_Type (Rhs, Etype (Lhs));
487          Kill_Lhs;
488          return;
489       end if;
490
491       --  Ada 2005 (AI-326): In case of explicit dereference of incomplete
492       --  types, use the non-limited view if available
493
494       if Nkind (Rhs) = N_Explicit_Dereference
495         and then Ekind (T2) = E_Incomplete_Type
496         and then Is_Tagged_Type (T2)
497         and then Present (Non_Limited_View (T2))
498       then
499          T2 := Non_Limited_View (T2);
500       end if;
501
502       Set_Assignment_Type (Rhs, T2);
503
504       if Total_Errors_Detected /= 0 then
505          if No (T1) then
506             T1 := Any_Type;
507          end if;
508
509          if No (T2) then
510             T2 := Any_Type;
511          end if;
512       end if;
513
514       if T1 = Any_Type or else T2 = Any_Type then
515          Kill_Lhs;
516          return;
517       end if;
518
519       --  If the rhs is class-wide or dynamically tagged, then require the lhs
520       --  to be class-wide. The case where the rhs is a dynamically tagged call
521       --  to a dispatching operation with a controlling access result is
522       --  excluded from this check, since the target has an access type (and
523       --  no tag propagation occurs in that case).
524
525       if (Is_Class_Wide_Type (T2)
526            or else (Is_Dynamically_Tagged (Rhs)
527                      and then not Is_Access_Type (T1)))
528         and then not Is_Class_Wide_Type (T1)
529       then
530          Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
531
532       elsif Is_Class_Wide_Type (T1)
533         and then not Is_Class_Wide_Type (T2)
534         and then not Is_Tag_Indeterminate (Rhs)
535         and then not Is_Dynamically_Tagged (Rhs)
536       then
537          Error_Msg_N ("dynamically tagged expression required!", Rhs);
538       end if;
539
540       --  Propagate the tag from a class-wide target to the rhs when the rhs
541       --  is a tag-indeterminate call.
542
543       if Is_Tag_Indeterminate (Rhs) then
544          if Is_Class_Wide_Type (T1) then
545             Propagate_Tag (Lhs, Rhs);
546
547          elsif Nkind (Rhs) = N_Function_Call
548               and then Is_Entity_Name (Name (Rhs))
549               and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
550          then
551             Error_Msg_N
552               ("call to abstract function must be dispatching", Name (Rhs));
553
554          elsif Nkind (Rhs) = N_Qualified_Expression
555            and then Nkind (Expression (Rhs)) = N_Function_Call
556               and then Is_Entity_Name (Name (Expression (Rhs)))
557               and then
558                 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
559          then
560             Error_Msg_N
561               ("call to abstract function must be dispatching",
562                 Name (Expression (Rhs)));
563          end if;
564       end if;
565
566       --  Ada 2005 (AI-230 and AI-385): When the lhs type is an anonymous
567       --  access type, apply an implicit conversion of the rhs to that type
568       --  to force appropriate static and run-time accessibility checks.
569
570       if Ada_Version >= Ada_05
571         and then Ekind (T1) = E_Anonymous_Access_Type
572       then
573          Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
574          Analyze_And_Resolve (Rhs, T1);
575       end if;
576
577       --  Ada 2005 (AI-231): Assignment to not null variable
578
579       if Ada_Version >= Ada_05
580         and then Can_Never_Be_Null (T1)
581         and then not Assignment_OK (Lhs)
582       then
583          --  Case where we know the right hand side is null
584
585          if Known_Null (Rhs) then
586             Apply_Compile_Time_Constraint_Error
587               (N   => Rhs,
588                Msg => "(Ada 2005) null not allowed in null-excluding objects?",
589                Reason => CE_Null_Not_Allowed);
590
591             --  We still mark this as a possible modification, that's necessary
592             --  to reset Is_True_Constant, and desirable for xref purposes.
593
594             Note_Possible_Modification (Lhs);
595             return;
596
597          --  If we know the right hand side is non-null, then we convert to the
598          --  target type, since we don't need a run time check in that case.
599
600          elsif not Can_Never_Be_Null (T2) then
601             Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
602             Analyze_And_Resolve (Rhs, T1);
603          end if;
604       end if;
605
606       if Is_Scalar_Type (T1) then
607          Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
608
609       --  For array types, verify that lengths match. If the right hand side
610       --  if a function call that has been inlined, the assignment has been
611       --  rewritten as a block, and the constraint check will be applied to the
612       --  assignment within the block.
613
614       elsif Is_Array_Type (T1)
615         and then
616           (Nkind (Rhs) /= N_Type_Conversion
617             or else Is_Constrained (Etype (Rhs)))
618         and then
619           (Nkind (Rhs) /= N_Function_Call
620             or else Nkind (N) /= N_Block_Statement)
621       then
622          --  Assignment verifies that the length of the Lsh and Rhs are equal,
623          --  but of course the indices do not have to match. If the right-hand
624          --  side is a type conversion to an unconstrained type, a length check
625          --  is performed on the expression itself during expansion. In rare
626          --  cases, the redundant length check is computed on an index type
627          --  with a different representation, triggering incorrect code in
628          --  the back end.
629
630          Apply_Length_Check (Rhs, Etype (Lhs));
631
632       else
633          --  Discriminant checks are applied in the course of expansion
634
635          null;
636       end if;
637
638       --  Note: modifications of the Lhs may only be recorded after
639       --  checks have been applied.
640
641       Note_Possible_Modification (Lhs);
642
643       --  ??? a real accessibility check is needed when ???
644
645       --  Post warning for redundant assignment or variable to itself
646
647       if Warn_On_Redundant_Constructs
648
649          --  We only warn for source constructs
650
651          and then Comes_From_Source (N)
652
653          --  Where the object is the same on both sides
654
655          and then Same_Object (Lhs, Original_Node (Rhs))
656
657          --  But exclude the case where the right side was an operation
658          --  that got rewritten (e.g. JUNK + K, where K was known to be
659          --  zero). We don't want to warn in such a case, since it is
660          --  reasonable to write such expressions especially when K is
661          --  defined symbolically in some other package.
662
663         and then Nkind (Original_Node (Rhs)) not in N_Op
664       then
665          if Nkind (Lhs) in N_Has_Entity then
666             Error_Msg_NE
667               ("?useless assignment of & to itself!", N, Entity (Lhs));
668          else
669             Error_Msg_N
670               ("?useless assignment of object to itself!", N);
671          end if;
672       end if;
673
674       --  Check for non-allowed composite assignment
675
676       if not Support_Composite_Assign_On_Target
677         and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
678         and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
679       then
680          Error_Msg_CRT ("composite assignment", N);
681       end if;
682
683       --  Check elaboration warning for left side if not in elab code
684
685       if not In_Subprogram_Or_Concurrent_Unit then
686          Check_Elab_Assign (Lhs);
687       end if;
688
689       --  Final step. If left side is an entity, then we may be able to
690       --  reset the current tracked values to new safe values. We only have
691       --  something to do if the left side is an entity name, and expansion
692       --  has not modified the node into something other than an assignment,
693       --  and of course we only capture values if it is safe to do so.
694
695       if Is_Entity_Name (Lhs)
696         and then Nkind (N) = N_Assignment_Statement
697       then
698          declare
699             Ent : constant Entity_Id := Entity (Lhs);
700
701          begin
702             if Safe_To_Capture_Value (N, Ent) then
703
704                --  If simple variable on left side, warn if this assignment
705                --  blots out another one (rendering it useless) and note
706                --  location of assignment in case no one references value.
707                --  We only do this for source assignments, otherwise we can
708                --  generate bogus warnings when an assignment is rewritten as
709                --  another assignment, and gets tied up with itself.
710
711                if Warn_On_Modified_Unread
712                  and then Ekind (Ent) = E_Variable
713                  and then Comes_From_Source (N)
714                  and then In_Extended_Main_Source_Unit (Ent)
715                then
716                   Warn_On_Useless_Assignment (Ent, Sloc (N));
717                   Set_Last_Assignment (Ent, Lhs);
718                end if;
719
720                --  If we are assigning an access type and the left side is an
721                --  entity, then make sure that the Is_Known_[Non_]Null flags
722                --  properly reflect the state of the entity after assignment.
723
724                if Is_Access_Type (T1) then
725                   if Known_Non_Null (Rhs) then
726                      Set_Is_Known_Non_Null (Ent, True);
727
728                   elsif Known_Null (Rhs)
729                     and then not Can_Never_Be_Null (Ent)
730                   then
731                      Set_Is_Known_Null (Ent, True);
732
733                   else
734                      Set_Is_Known_Null (Ent, False);
735
736                      if not Can_Never_Be_Null (Ent) then
737                         Set_Is_Known_Non_Null (Ent, False);
738                      end if;
739                   end if;
740
741                --  For discrete types, we may be able to set the current value
742                --  if the value is known at compile time.
743
744                elsif Is_Discrete_Type (T1)
745                  and then Compile_Time_Known_Value (Rhs)
746                then
747                   Set_Current_Value (Ent, Rhs);
748                else
749                   Set_Current_Value (Ent, Empty);
750                end if;
751
752             --  If not safe to capture values, kill them
753
754             else
755                Kill_Lhs;
756             end if;
757          end;
758       end if;
759    end Analyze_Assignment;
760
761    -----------------------------
762    -- Analyze_Block_Statement --
763    -----------------------------
764
765    procedure Analyze_Block_Statement (N : Node_Id) is
766       Decls : constant List_Id := Declarations (N);
767       Id    : constant Node_Id := Identifier (N);
768       HSS   : constant Node_Id := Handled_Statement_Sequence (N);
769
770    begin
771       --  If no handled statement sequence is present, things are really
772       --  messed up, and we just return immediately (this is a defence
773       --  against previous errors).
774
775       if No (HSS) then
776          return;
777       end if;
778
779       --  Normal processing with HSS present
780
781       declare
782          EH  : constant List_Id := Exception_Handlers (HSS);
783          Ent : Entity_Id        := Empty;
784          S   : Entity_Id;
785
786          Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
787          --  Recursively save value of this global, will be restored on exit
788
789       begin
790          --  Initialize unblocked exit count for statements of begin block
791          --  plus one for each excption handler that is present.
792
793          Unblocked_Exit_Count := 1;
794
795          if Present (EH) then
796             Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
797          end if;
798
799          --  If a label is present analyze it and mark it as referenced
800
801          if Present (Id) then
802             Analyze (Id);
803             Ent := Entity (Id);
804
805             --  An error defense. If we have an identifier, but no entity,
806             --  then something is wrong. If we have previous errors, then
807             --  just remove the identifier and continue, otherwise raise
808             --  an exception.
809
810             if No (Ent) then
811                if Total_Errors_Detected /= 0 then
812                   Set_Identifier (N, Empty);
813                else
814                   raise Program_Error;
815                end if;
816
817             else
818                Set_Ekind (Ent, E_Block);
819                Generate_Reference (Ent, N, ' ');
820                Generate_Definition (Ent);
821
822                if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
823                   Set_Label_Construct (Parent (Ent), N);
824                end if;
825             end if;
826          end if;
827
828          --  If no entity set, create a label entity
829
830          if No (Ent) then
831             Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
832             Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
833             Set_Parent (Ent, N);
834          end if;
835
836          Set_Etype (Ent, Standard_Void_Type);
837          Set_Block_Node (Ent, Identifier (N));
838          Push_Scope (Ent);
839
840          if Present (Decls) then
841             Analyze_Declarations (Decls);
842             Check_Completion;
843          end if;
844
845          Analyze (HSS);
846          Process_End_Label (HSS, 'e', Ent);
847
848          --  If exception handlers are present, then we indicate that
849          --  enclosing scopes contain a block with handlers. We only
850          --  need to mark non-generic scopes.
851
852          if Present (EH) then
853             S := Scope (Ent);
854             loop
855                Set_Has_Nested_Block_With_Handler (S);
856                exit when Is_Overloadable (S)
857                  or else Ekind (S) = E_Package
858                  or else Is_Generic_Unit (S);
859                S := Scope (S);
860             end loop;
861          end if;
862
863          Check_References (Ent);
864          Warn_On_Useless_Assignments (Ent);
865          End_Scope;
866
867          if Unblocked_Exit_Count = 0 then
868             Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
869             Check_Unreachable_Code (N);
870          else
871             Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
872          end if;
873       end;
874    end Analyze_Block_Statement;
875
876    ----------------------------
877    -- Analyze_Case_Statement --
878    ----------------------------
879
880    procedure Analyze_Case_Statement (N : Node_Id) is
881       Exp            : Node_Id;
882       Exp_Type       : Entity_Id;
883       Exp_Btype      : Entity_Id;
884       Last_Choice    : Nat;
885       Dont_Care      : Boolean;
886       Others_Present : Boolean;
887
888       Statements_Analyzed : Boolean := False;
889       --  Set True if at least some statement sequences get analyzed.
890       --  If False on exit, means we had a serious error that prevented
891       --  full analysis of the case statement, and as a result it is not
892       --  a good idea to output warning messages about unreachable code.
893
894       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
895       --  Recursively save value of this global, will be restored on exit
896
897       procedure Non_Static_Choice_Error (Choice : Node_Id);
898       --  Error routine invoked by the generic instantiation below when
899       --  the case statment has a non static choice.
900
901       procedure Process_Statements (Alternative : Node_Id);
902       --  Analyzes all the statements associated to a case alternative.
903       --  Needed by the generic instantiation below.
904
905       package Case_Choices_Processing is new
906         Generic_Choices_Processing
907           (Get_Alternatives          => Alternatives,
908            Get_Choices               => Discrete_Choices,
909            Process_Empty_Choice      => No_OP,
910            Process_Non_Static_Choice => Non_Static_Choice_Error,
911            Process_Associated_Node   => Process_Statements);
912       use Case_Choices_Processing;
913       --  Instantiation of the generic choice processing package
914
915       -----------------------------
916       -- Non_Static_Choice_Error --
917       -----------------------------
918
919       procedure Non_Static_Choice_Error (Choice : Node_Id) is
920       begin
921          Flag_Non_Static_Expr
922            ("choice given in case statement is not static!", Choice);
923       end Non_Static_Choice_Error;
924
925       ------------------------
926       -- Process_Statements --
927       ------------------------
928
929       procedure Process_Statements (Alternative : Node_Id) is
930          Choices : constant List_Id := Discrete_Choices (Alternative);
931          Ent     : Entity_Id;
932
933       begin
934          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
935          Statements_Analyzed := True;
936
937          --  An interesting optimization. If the case statement expression
938          --  is a simple entity, then we can set the current value within
939          --  an alternative if the alternative has one possible value.
940
941          --    case N is
942          --      when 1      => alpha
943          --      when 2 | 3  => beta
944          --      when others => gamma
945
946          --  Here we know that N is initially 1 within alpha, but for beta
947          --  and gamma, we do not know anything more about the initial value.
948
949          if Is_Entity_Name (Exp) then
950             Ent := Entity (Exp);
951
952             if Ekind (Ent) = E_Variable
953                  or else
954                Ekind (Ent) = E_In_Out_Parameter
955                  or else
956                Ekind (Ent) = E_Out_Parameter
957             then
958                if List_Length (Choices) = 1
959                  and then Nkind (First (Choices)) in N_Subexpr
960                  and then Compile_Time_Known_Value (First (Choices))
961                then
962                   Set_Current_Value (Entity (Exp), First (Choices));
963                end if;
964
965                Analyze_Statements (Statements (Alternative));
966
967                --  After analyzing the case, set the current value to empty
968                --  since we won't know what it is for the next alternative
969                --  (unless reset by this same circuit), or after the case.
970
971                Set_Current_Value (Entity (Exp), Empty);
972                return;
973             end if;
974          end if;
975
976          --  Case where expression is not an entity name of a variable
977
978          Analyze_Statements (Statements (Alternative));
979       end Process_Statements;
980
981       --  Table to record choices. Put after subprograms since we make
982       --  a call to Number_Of_Choices to get the right number of entries.
983
984       Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
985
986    --  Start of processing for Analyze_Case_Statement
987
988    begin
989       Unblocked_Exit_Count := 0;
990       Exp := Expression (N);
991       Analyze (Exp);
992
993       --  The expression must be of any discrete type. In rare cases, the
994       --  expander constructs a case statement whose expression has a private
995       --  type whose full view is discrete. This can happen when generating
996       --  a stream operation for a variant type after the type is frozen,
997       --  when the partial of view of the type of the discriminant is private.
998       --  In that case, use the full view to analyze case alternatives.
999
1000       if not Is_Overloaded (Exp)
1001         and then not Comes_From_Source (N)
1002         and then Is_Private_Type (Etype (Exp))
1003         and then Present (Full_View (Etype (Exp)))
1004         and then Is_Discrete_Type (Full_View (Etype (Exp)))
1005       then
1006          Resolve (Exp, Etype (Exp));
1007          Exp_Type := Full_View (Etype (Exp));
1008
1009       else
1010          Analyze_And_Resolve (Exp, Any_Discrete);
1011          Exp_Type := Etype (Exp);
1012       end if;
1013
1014       Check_Unset_Reference (Exp);
1015       Exp_Btype := Base_Type (Exp_Type);
1016
1017       --  The expression must be of a discrete type which must be determinable
1018       --  independently of the context in which the expression occurs, but
1019       --  using the fact that the expression must be of a discrete type.
1020       --  Moreover, the type this expression must not be a character literal
1021       --  (which is always ambiguous) or, for Ada-83, a generic formal type.
1022
1023       --  If error already reported by Resolve, nothing more to do
1024
1025       if Exp_Btype = Any_Discrete
1026         or else Exp_Btype = Any_Type
1027       then
1028          return;
1029
1030       elsif Exp_Btype = Any_Character then
1031          Error_Msg_N
1032            ("character literal as case expression is ambiguous", Exp);
1033          return;
1034
1035       elsif Ada_Version = Ada_83
1036         and then (Is_Generic_Type (Exp_Btype)
1037                     or else Is_Generic_Type (Root_Type (Exp_Btype)))
1038       then
1039          Error_Msg_N
1040            ("(Ada 83) case expression cannot be of a generic type", Exp);
1041          return;
1042       end if;
1043
1044       --  If the case expression is a formal object of mode in out, then
1045       --  treat it as having a nonstatic subtype by forcing use of the base
1046       --  type (which has to get passed to Check_Case_Choices below).  Also
1047       --  use base type when the case expression is parenthesized.
1048
1049       if Paren_Count (Exp) > 0
1050         or else (Is_Entity_Name (Exp)
1051                   and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1052       then
1053          Exp_Type := Exp_Btype;
1054       end if;
1055
1056       --  Call instantiated Analyze_Choices which does the rest of the work
1057
1058       Analyze_Choices
1059         (N, Exp_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
1060
1061       if Exp_Type = Universal_Integer and then not Others_Present then
1062          Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1063       end if;
1064
1065       --  If all our exits were blocked by unconditional transfers of control,
1066       --  then the entire CASE statement acts as an unconditional transfer of
1067       --  control, so treat it like one, and check unreachable code. Skip this
1068       --  test if we had serious errors preventing any statement analysis.
1069
1070       if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1071          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1072          Check_Unreachable_Code (N);
1073       else
1074          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1075       end if;
1076
1077       if not Expander_Active
1078         and then Compile_Time_Known_Value (Expression (N))
1079         and then Serious_Errors_Detected = 0
1080       then
1081          declare
1082             Chosen : constant Node_Id := Find_Static_Alternative (N);
1083             Alt    : Node_Id;
1084
1085          begin
1086             Alt := First (Alternatives (N));
1087             while Present (Alt) loop
1088                if Alt /= Chosen then
1089                   Remove_Warning_Messages (Statements (Alt));
1090                end if;
1091
1092                Next (Alt);
1093             end loop;
1094          end;
1095       end if;
1096    end Analyze_Case_Statement;
1097
1098    ----------------------------
1099    -- Analyze_Exit_Statement --
1100    ----------------------------
1101
1102    --  If the exit includes a name, it must be the name of a currently open
1103    --  loop. Otherwise there must be an innermost open loop on the stack,
1104    --  to which the statement implicitly refers.
1105
1106    procedure Analyze_Exit_Statement (N : Node_Id) is
1107       Target   : constant Node_Id := Name (N);
1108       Cond     : constant Node_Id := Condition (N);
1109       Scope_Id : Entity_Id;
1110       U_Name   : Entity_Id;
1111       Kind     : Entity_Kind;
1112
1113    begin
1114       if No (Cond) then
1115          Check_Unreachable_Code (N);
1116       end if;
1117
1118       if Present (Target) then
1119          Analyze (Target);
1120          U_Name := Entity (Target);
1121
1122          if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1123             Error_Msg_N ("invalid loop name in exit statement", N);
1124             return;
1125          else
1126             Set_Has_Exit (U_Name);
1127          end if;
1128
1129       else
1130          U_Name := Empty;
1131       end if;
1132
1133       for J in reverse 0 .. Scope_Stack.Last loop
1134          Scope_Id := Scope_Stack.Table (J).Entity;
1135          Kind := Ekind (Scope_Id);
1136
1137          if Kind = E_Loop
1138            and then (No (Target) or else Scope_Id = U_Name) then
1139             Set_Has_Exit (Scope_Id);
1140             exit;
1141
1142          elsif Kind = E_Block
1143            or else Kind = E_Loop
1144            or else Kind = E_Return_Statement
1145          then
1146             null;
1147
1148          else
1149             Error_Msg_N
1150               ("cannot exit from program unit or accept statement", N);
1151             exit;
1152          end if;
1153       end loop;
1154
1155       --  Verify that if present the condition is a Boolean expression
1156
1157       if Present (Cond) then
1158          Analyze_And_Resolve (Cond, Any_Boolean);
1159          Check_Unset_Reference (Cond);
1160       end if;
1161    end Analyze_Exit_Statement;
1162
1163    ----------------------------
1164    -- Analyze_Goto_Statement --
1165    ----------------------------
1166
1167    procedure Analyze_Goto_Statement (N : Node_Id) is
1168       Label       : constant Node_Id := Name (N);
1169       Scope_Id    : Entity_Id;
1170       Label_Scope : Entity_Id;
1171       Label_Ent   : Entity_Id;
1172
1173    begin
1174       Check_Unreachable_Code (N);
1175
1176       Analyze (Label);
1177       Label_Ent := Entity (Label);
1178
1179       --  Ignore previous error
1180
1181       if Label_Ent = Any_Id then
1182          return;
1183
1184       --  We just have a label as the target of a goto
1185
1186       elsif Ekind (Label_Ent) /= E_Label then
1187          Error_Msg_N ("target of goto statement must be a label", Label);
1188          return;
1189
1190       --  Check that the target of the goto is reachable according to Ada
1191       --  scoping rules. Note: the special gotos we generate for optimizing
1192       --  local handling of exceptions would violate these rules, but we mark
1193       --  such gotos as analyzed when built, so this code is never entered.
1194
1195       elsif not Reachable (Label_Ent) then
1196          Error_Msg_N ("target of goto statement is not reachable", Label);
1197          return;
1198       end if;
1199
1200       --  Here if goto passes initial validity checks
1201
1202       Label_Scope := Enclosing_Scope (Label_Ent);
1203
1204       for J in reverse 0 .. Scope_Stack.Last loop
1205          Scope_Id := Scope_Stack.Table (J).Entity;
1206
1207          if Label_Scope = Scope_Id
1208            or else (Ekind (Scope_Id) /= E_Block
1209                      and then Ekind (Scope_Id) /= E_Loop
1210                      and then Ekind (Scope_Id) /= E_Return_Statement)
1211          then
1212             if Scope_Id /= Label_Scope then
1213                Error_Msg_N
1214                  ("cannot exit from program unit or accept statement", N);
1215             end if;
1216
1217             return;
1218          end if;
1219       end loop;
1220
1221       raise Program_Error;
1222    end Analyze_Goto_Statement;
1223
1224    --------------------------
1225    -- Analyze_If_Statement --
1226    --------------------------
1227
1228    --  A special complication arises in the analysis of if statements
1229
1230    --  The expander has circuitry to completely delete code that it
1231    --  can tell will not be executed (as a result of compile time known
1232    --  conditions). In the analyzer, we ensure that code that will be
1233    --  deleted in this manner is analyzed but not expanded. This is
1234    --  obviously more efficient, but more significantly, difficulties
1235    --  arise if code is expanded and then eliminated (e.g. exception
1236    --  table entries disappear). Similarly, itypes generated in deleted
1237    --  code must be frozen from start, because the nodes on which they
1238    --  depend will not be available at the freeze point.
1239
1240    procedure Analyze_If_Statement (N : Node_Id) is
1241       E : Node_Id;
1242
1243       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1244       --  Recursively save value of this global, will be restored on exit
1245
1246       Save_In_Deleted_Code : Boolean;
1247
1248       Del : Boolean := False;
1249       --  This flag gets set True if a True condition has been found,
1250       --  which means that remaining ELSE/ELSIF parts are deleted.
1251
1252       procedure Analyze_Cond_Then (Cnode : Node_Id);
1253       --  This is applied to either the N_If_Statement node itself or
1254       --  to an N_Elsif_Part node. It deals with analyzing the condition
1255       --  and the THEN statements associated with it.
1256
1257       -----------------------
1258       -- Analyze_Cond_Then --
1259       -----------------------
1260
1261       procedure Analyze_Cond_Then (Cnode : Node_Id) is
1262          Cond : constant Node_Id := Condition (Cnode);
1263          Tstm : constant List_Id := Then_Statements (Cnode);
1264
1265       begin
1266          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1267          Analyze_And_Resolve (Cond, Any_Boolean);
1268          Check_Unset_Reference (Cond);
1269          Set_Current_Value_Condition (Cnode);
1270
1271          --  If already deleting, then just analyze then statements
1272
1273          if Del then
1274             Analyze_Statements (Tstm);
1275
1276          --  Compile time known value, not deleting yet
1277
1278          elsif Compile_Time_Known_Value (Cond) then
1279             Save_In_Deleted_Code := In_Deleted_Code;
1280
1281             --  If condition is True, then analyze the THEN statements
1282             --  and set no expansion for ELSE and ELSIF parts.
1283
1284             if Is_True (Expr_Value (Cond)) then
1285                Analyze_Statements (Tstm);
1286                Del := True;
1287                Expander_Mode_Save_And_Set (False);
1288                In_Deleted_Code := True;
1289
1290             --  If condition is False, analyze THEN with expansion off
1291
1292             else -- Is_False (Expr_Value (Cond))
1293                Expander_Mode_Save_And_Set (False);
1294                In_Deleted_Code := True;
1295                Analyze_Statements (Tstm);
1296                Expander_Mode_Restore;
1297                In_Deleted_Code := Save_In_Deleted_Code;
1298             end if;
1299
1300          --  Not known at compile time, not deleting, normal analysis
1301
1302          else
1303             Analyze_Statements (Tstm);
1304          end if;
1305       end Analyze_Cond_Then;
1306
1307    --  Start of Analyze_If_Statement
1308
1309    begin
1310       --  Initialize exit count for else statements. If there is no else
1311       --  part, this count will stay non-zero reflecting the fact that the
1312       --  uncovered else case is an unblocked exit.
1313
1314       Unblocked_Exit_Count := 1;
1315       Analyze_Cond_Then (N);
1316
1317       --  Now to analyze the elsif parts if any are present
1318
1319       if Present (Elsif_Parts (N)) then
1320          E := First (Elsif_Parts (N));
1321          while Present (E) loop
1322             Analyze_Cond_Then (E);
1323             Next (E);
1324          end loop;
1325       end if;
1326
1327       if Present (Else_Statements (N)) then
1328          Analyze_Statements (Else_Statements (N));
1329       end if;
1330
1331       --  If all our exits were blocked by unconditional transfers of control,
1332       --  then the entire IF statement acts as an unconditional transfer of
1333       --  control, so treat it like one, and check unreachable code.
1334
1335       if Unblocked_Exit_Count = 0 then
1336          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1337          Check_Unreachable_Code (N);
1338       else
1339          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1340       end if;
1341
1342       if Del then
1343          Expander_Mode_Restore;
1344          In_Deleted_Code := Save_In_Deleted_Code;
1345       end if;
1346
1347       if not Expander_Active
1348         and then Compile_Time_Known_Value (Condition (N))
1349         and then Serious_Errors_Detected = 0
1350       then
1351          if Is_True (Expr_Value (Condition (N))) then
1352             Remove_Warning_Messages (Else_Statements (N));
1353
1354             if Present (Elsif_Parts (N)) then
1355                E := First (Elsif_Parts (N));
1356                while Present (E) loop
1357                   Remove_Warning_Messages (Then_Statements (E));
1358                   Next (E);
1359                end loop;
1360             end if;
1361
1362          else
1363             Remove_Warning_Messages (Then_Statements (N));
1364          end if;
1365       end if;
1366    end Analyze_If_Statement;
1367
1368    ----------------------------------------
1369    -- Analyze_Implicit_Label_Declaration --
1370    ----------------------------------------
1371
1372    --  An implicit label declaration is generated in the innermost
1373    --  enclosing declarative part. This is done for labels as well as
1374    --  block and loop names.
1375
1376    --  Note: any changes in this routine may need to be reflected in
1377    --  Analyze_Label_Entity.
1378
1379    procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1380       Id : constant Node_Id := Defining_Identifier (N);
1381    begin
1382       Enter_Name          (Id);
1383       Set_Ekind           (Id, E_Label);
1384       Set_Etype           (Id, Standard_Void_Type);
1385       Set_Enclosing_Scope (Id, Current_Scope);
1386    end Analyze_Implicit_Label_Declaration;
1387
1388    ------------------------------
1389    -- Analyze_Iteration_Scheme --
1390    ------------------------------
1391
1392    procedure Analyze_Iteration_Scheme (N : Node_Id) is
1393
1394       procedure Process_Bounds (R : Node_Id);
1395       --  If the iteration is given by a range, create temporaries and
1396       --  assignment statements block to capture the bounds and perform
1397       --  required finalization actions in case a bound includes a function
1398       --  call that uses the temporary stack. We first pre-analyze a copy of
1399       --  the range in order to determine the expected type, and analyze and
1400       --  resolve the original bounds.
1401
1402       procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1403       --  If the bounds are given by a 'Range reference on a function call
1404       --  that returns a controlled array, introduce an explicit declaration
1405       --  to capture the bounds, so that the function result can be finalized
1406       --  in timely fashion.
1407
1408       --------------------
1409       -- Process_Bounds --
1410       --------------------
1411
1412       procedure Process_Bounds (R : Node_Id) is
1413          Loc          : constant Source_Ptr := Sloc (N);
1414          R_Copy       : constant Node_Id := New_Copy_Tree (R);
1415          Lo           : constant Node_Id := Low_Bound  (R);
1416          Hi           : constant Node_Id := High_Bound (R);
1417          New_Lo_Bound : Node_Id := Empty;
1418          New_Hi_Bound : Node_Id := Empty;
1419          Typ          : Entity_Id;
1420          Save_Analysis : Boolean;
1421
1422          function One_Bound
1423            (Original_Bound : Node_Id;
1424             Analyzed_Bound : Node_Id) return Node_Id;
1425          --  Create one declaration followed by one assignment statement
1426          --  to capture the value of bound. We create a separate assignment
1427          --  in order to force the creation of a block in case the bound
1428          --  contains a call that uses the secondary stack.
1429
1430          ---------------
1431          -- One_Bound --
1432          ---------------
1433
1434          function One_Bound
1435            (Original_Bound : Node_Id;
1436             Analyzed_Bound : Node_Id) return Node_Id
1437          is
1438             Assign : Node_Id;
1439             Id     : Entity_Id;
1440             Decl   : Node_Id;
1441
1442          begin
1443             --  If the bound is a constant or an object, no need for a separate
1444             --  declaration. If the bound is the result of previous expansion
1445             --  it is already analyzed and should not be modified. Note that
1446             --  the Bound will be resolved later, if needed, as part of the
1447             --  call to Make_Index (literal bounds may need to be resolved to
1448             --  type Integer).
1449
1450             if Analyzed (Original_Bound) then
1451                return Original_Bound;
1452
1453             elsif Nkind (Analyzed_Bound) = N_Integer_Literal
1454               or else Nkind (Analyzed_Bound) = N_Character_Literal
1455               or else Is_Entity_Name (Analyzed_Bound)
1456             then
1457                Analyze_And_Resolve (Original_Bound, Typ);
1458                return Original_Bound;
1459
1460             else
1461                Analyze_And_Resolve (Original_Bound, Typ);
1462             end if;
1463
1464             Id :=
1465               Make_Defining_Identifier (Loc,
1466                 Chars => New_Internal_Name ('S'));
1467
1468             Decl :=
1469               Make_Object_Declaration (Loc,
1470                 Defining_Identifier => Id,
1471                 Object_Definition   => New_Occurrence_Of (Typ, Loc));
1472
1473             Insert_Before (Parent (N), Decl);
1474             Analyze (Decl);
1475
1476             Assign :=
1477               Make_Assignment_Statement (Loc,
1478                 Name        => New_Occurrence_Of (Id, Loc),
1479                 Expression  => Relocate_Node (Original_Bound));
1480
1481             Insert_Before (Parent (N), Assign);
1482             Analyze (Assign);
1483
1484             Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
1485
1486             if Nkind (Assign) = N_Assignment_Statement then
1487                return Expression (Assign);
1488             else
1489                return Original_Bound;
1490             end if;
1491          end One_Bound;
1492
1493       --  Start of processing for Process_Bounds
1494
1495       begin
1496          --  Determine expected type of range by analyzing separate copy
1497          --  Do the analysis and resolution of the copy of the bounds with
1498          --  expansion disabled, to prevent the generation of finalization
1499          --  actions on each bound. This prevents memory leaks when the
1500          --  bounds contain calls to functions returning controlled arrays.
1501
1502          Set_Parent (R_Copy, Parent (R));
1503          Save_Analysis := Full_Analysis;
1504          Full_Analysis := False;
1505          Expander_Mode_Save_And_Set (False);
1506
1507          Analyze (R_Copy);
1508
1509          if Is_Overloaded (R_Copy) then
1510
1511             --  Apply preference rules for range of predefined integer types,
1512             --  or diagnose true ambiguity.
1513
1514             declare
1515                I     : Interp_Index;
1516                It    : Interp;
1517                Found : Entity_Id := Empty;
1518
1519             begin
1520                Get_First_Interp (R_Copy, I, It);
1521                while Present (It.Typ) loop
1522                   if Is_Discrete_Type (It.Typ) then
1523                      if No (Found) then
1524                         Found := It.Typ;
1525                      else
1526                         if Scope (Found) = Standard_Standard then
1527                            null;
1528
1529                         elsif Scope (It.Typ) = Standard_Standard then
1530                            Found := It.Typ;
1531
1532                         else
1533                            --  Both of them are user-defined
1534
1535                            Error_Msg_N
1536                              ("ambiguous bounds in range of iteration",
1537                                R_Copy);
1538                            Error_Msg_N ("\possible interpretations:", R_Copy);
1539                            Error_Msg_NE ("\\} ", R_Copy, Found);
1540                            Error_Msg_NE ("\\} ", R_Copy, It.Typ);
1541                            exit;
1542                         end if;
1543                      end if;
1544                   end if;
1545
1546                   Get_Next_Interp (I, It);
1547                end loop;
1548             end;
1549          end if;
1550
1551          Resolve (R_Copy);
1552          Expander_Mode_Restore;
1553          Full_Analysis := Save_Analysis;
1554
1555          Typ := Etype (R_Copy);
1556
1557          --  If the type of the discrete range is Universal_Integer, then
1558          --  the bound's type must be resolved to Integer, and any object
1559          --  used to hold the bound must also have type Integer, unless the
1560          --  literal bounds are constant-folded expressions that carry a user-
1561          --  defined type.
1562
1563          if Typ = Universal_Integer then
1564             if Nkind (Lo) = N_Integer_Literal
1565               and then Present (Etype (Lo))
1566               and then Scope (Etype (Lo)) /= Standard_Standard
1567             then
1568                Typ := Etype (Lo);
1569
1570             elsif Nkind (Hi) = N_Integer_Literal
1571               and then Present (Etype (Hi))
1572               and then Scope (Etype (Hi)) /= Standard_Standard
1573             then
1574                Typ := Etype (Hi);
1575
1576             else
1577                Typ := Standard_Integer;
1578             end if;
1579          end if;
1580
1581          Set_Etype (R, Typ);
1582
1583          New_Lo_Bound := One_Bound (Lo, Low_Bound  (R_Copy));
1584          New_Hi_Bound := One_Bound (Hi, High_Bound (R_Copy));
1585
1586          --  Propagate staticness to loop range itself, in case the
1587          --  corresponding subtype is static.
1588
1589          if New_Lo_Bound /= Lo
1590            and then Is_Static_Expression (New_Lo_Bound)
1591          then
1592             Rewrite  (Low_Bound (R), New_Copy (New_Lo_Bound));
1593          end if;
1594
1595          if New_Hi_Bound /= Hi
1596            and then Is_Static_Expression (New_Hi_Bound)
1597          then
1598             Rewrite (High_Bound (R), New_Copy (New_Hi_Bound));
1599          end if;
1600       end Process_Bounds;
1601
1602       --------------------------------------
1603       -- Check_Controlled_Array_Attribute --
1604       --------------------------------------
1605
1606       procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1607       begin
1608          if Nkind (DS) = N_Attribute_Reference
1609             and then Is_Entity_Name (Prefix (DS))
1610             and then Ekind (Entity (Prefix (DS))) = E_Function
1611             and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1612             and then
1613               Is_Controlled (
1614                 Component_Type (Etype (Entity (Prefix (DS)))))
1615             and then Expander_Active
1616          then
1617             declare
1618                Loc  : constant Source_Ptr := Sloc (N);
1619                Arr  : constant Entity_Id :=
1620                         Etype (Entity (Prefix (DS)));
1621                Indx : constant Entity_Id :=
1622                         Base_Type (Etype (First_Index (Arr)));
1623                Subt : constant Entity_Id :=
1624                         Make_Defining_Identifier
1625                           (Loc, New_Internal_Name ('S'));
1626                Decl : Node_Id;
1627
1628             begin
1629                Decl :=
1630                  Make_Subtype_Declaration (Loc,
1631                    Defining_Identifier => Subt,
1632                    Subtype_Indication  =>
1633                       Make_Subtype_Indication (Loc,
1634                         Subtype_Mark  => New_Reference_To (Indx, Loc),
1635                         Constraint =>
1636                           Make_Range_Constraint (Loc,
1637                             Relocate_Node (DS))));
1638                Insert_Before (Parent (N), Decl);
1639                Analyze (Decl);
1640
1641                Rewrite (DS,
1642                   Make_Attribute_Reference (Loc,
1643                     Prefix => New_Reference_To (Subt, Loc),
1644                     Attribute_Name => Attribute_Name (DS)));
1645                Analyze (DS);
1646             end;
1647          end if;
1648       end Check_Controlled_Array_Attribute;
1649
1650    --  Start of processing for Analyze_Iteration_Scheme
1651
1652    begin
1653       --  For an infinite loop, there is no iteration scheme
1654
1655       if No (N) then
1656          return;
1657
1658       else
1659          declare
1660             Cond : constant Node_Id := Condition (N);
1661
1662          begin
1663             --  For WHILE loop, verify that the condition is a Boolean
1664             --  expression and resolve and check it.
1665
1666             if Present (Cond) then
1667                Analyze_And_Resolve (Cond, Any_Boolean);
1668                Check_Unset_Reference (Cond);
1669                Set_Current_Value_Condition (N);
1670                return;
1671
1672             --  Else we have a FOR loop
1673
1674             else
1675                declare
1676                   LP : constant Node_Id   := Loop_Parameter_Specification (N);
1677                   Id : constant Entity_Id := Defining_Identifier (LP);
1678                   DS : constant Node_Id   := Discrete_Subtype_Definition (LP);
1679
1680                begin
1681                   Enter_Name (Id);
1682
1683                   --  We always consider the loop variable to be referenced,
1684                   --  since the loop may be used just for counting purposes.
1685
1686                   Generate_Reference (Id, N, ' ');
1687
1688                   --  Check for case of loop variable hiding a local
1689                   --  variable (used later on to give a nice warning
1690                   --  if the hidden variable is never assigned).
1691
1692                   declare
1693                      H : constant Entity_Id := Homonym (Id);
1694                   begin
1695                      if Present (H)
1696                        and then Enclosing_Dynamic_Scope (H) =
1697                                 Enclosing_Dynamic_Scope (Id)
1698                        and then Ekind (H) = E_Variable
1699                        and then Is_Discrete_Type (Etype (H))
1700                      then
1701                         Set_Hiding_Loop_Variable (H, Id);
1702                      end if;
1703                   end;
1704
1705                   --  Now analyze the subtype definition. If it is
1706                   --  a range, create temporaries for bounds.
1707
1708                   if Nkind (DS) = N_Range
1709                     and then Expander_Active
1710                   then
1711                      Process_Bounds (DS);
1712                   else
1713                      Analyze (DS);
1714                   end if;
1715
1716                   if DS = Error then
1717                      return;
1718                   end if;
1719
1720                   --  The subtype indication may denote the completion
1721                   --  of an incomplete type declaration.
1722
1723                   if Is_Entity_Name (DS)
1724                     and then Present (Entity (DS))
1725                     and then Is_Type (Entity (DS))
1726                     and then Ekind (Entity (DS)) = E_Incomplete_Type
1727                   then
1728                      Set_Entity (DS, Get_Full_View (Entity (DS)));
1729                      Set_Etype  (DS, Entity (DS));
1730                   end if;
1731
1732                   if not Is_Discrete_Type (Etype (DS)) then
1733                      Wrong_Type (DS, Any_Discrete);
1734                      Set_Etype (DS, Any_Type);
1735                   end if;
1736
1737                   Check_Controlled_Array_Attribute (DS);
1738
1739                   Make_Index (DS, LP);
1740
1741                   Set_Ekind          (Id, E_Loop_Parameter);
1742                   Set_Etype          (Id, Etype (DS));
1743                   Set_Is_Known_Valid (Id, True);
1744
1745                   --  The loop is not a declarative part, so the only entity
1746                   --  declared "within" must be frozen explicitly.
1747
1748                   declare
1749                      Flist : constant List_Id := Freeze_Entity (Id, Sloc (N));
1750                   begin
1751                      if Is_Non_Empty_List (Flist) then
1752                         Insert_Actions (N, Flist);
1753                      end if;
1754                   end;
1755
1756                   --  Check for null or possibly null range and issue warning.
1757                   --  We suppress such messages in generic templates and
1758                   --  instances, because in practice they tend to be dubious
1759                   --  in these cases.
1760
1761                   if Nkind (DS) = N_Range
1762                     and then Comes_From_Source (N)
1763                   then
1764                      declare
1765                         L : constant Node_Id := Low_Bound  (DS);
1766                         H : constant Node_Id := High_Bound (DS);
1767
1768                         Llo : Uint;
1769                         Lhi : Uint;
1770                         LOK : Boolean;
1771                         Hlo : Uint;
1772                         Hhi : Uint;
1773                         HOK : Boolean;
1774
1775                      begin
1776                         Determine_Range (L, LOK, Llo, Lhi);
1777                         Determine_Range (H, HOK, Hlo, Hhi);
1778
1779                         --  If range of loop is null, issue warning
1780
1781                         if (LOK and HOK) and then Llo > Hhi then
1782
1783                            --  Suppress the warning if inside a generic
1784                            --  template or instance, since in practice
1785                            --  they tend to be dubious in these cases since
1786                            --  they can result from intended parametrization.
1787
1788                            if not Inside_A_Generic
1789                               and then not In_Instance
1790                            then
1791                               Error_Msg_N
1792                                 ("?loop range is null, loop will not execute",
1793                                  DS);
1794                            end if;
1795
1796                            --  Since we know the range of the loop is null,
1797                            --  set the appropriate flag to suppress any
1798                            --  warnings that would otherwise be issued in
1799                            --  the body of the loop that will not execute.
1800                            --  We do this even in the generic case, since
1801                            --  if it is dubious to warn on the null loop
1802                            --  itself, it is certainly dubious to warn for
1803                            --  conditions that occur inside it!
1804
1805                            Set_Is_Null_Loop (Parent (N));
1806
1807                         --  The other case for a warning is a reverse loop
1808                         --  where the upper bound is the integer literal
1809                         --  zero or one, and the lower bound can be positive.
1810
1811                         --  For example, we have
1812
1813                         --     for J in reverse N .. 1 loop
1814
1815                         --  In practice, this is very likely to be a case
1816                         --  of reversing the bounds incorrectly in the range.
1817
1818                         elsif Reverse_Present (LP)
1819                           and then Nkind (Original_Node (H)) =
1820                                                           N_Integer_Literal
1821                           and then (Intval (H) = Uint_0
1822                                       or else
1823                                     Intval (H) = Uint_1)
1824                           and then Lhi > Hhi
1825                         then
1826                            Error_Msg_N ("?loop range may be null", DS);
1827                            Error_Msg_N ("\?bounds may be wrong way round", DS);
1828                         end if;
1829                      end;
1830                   end if;
1831                end;
1832             end if;
1833          end;
1834       end if;
1835    end Analyze_Iteration_Scheme;
1836
1837    -------------------
1838    -- Analyze_Label --
1839    -------------------
1840
1841    --  Note: the semantic work required for analyzing labels (setting them as
1842    --  reachable) was done in a prepass through the statements in the block,
1843    --  so that forward gotos would be properly handled. See Analyze_Statements
1844    --  for further details. The only processing required here is to deal with
1845    --  optimizations that depend on an assumption of sequential control flow,
1846    --  since of course the occurrence of a label breaks this assumption.
1847
1848    procedure Analyze_Label (N : Node_Id) is
1849       pragma Warnings (Off, N);
1850    begin
1851       Kill_Current_Values;
1852    end Analyze_Label;
1853
1854    --------------------------
1855    -- Analyze_Label_Entity --
1856    --------------------------
1857
1858    procedure Analyze_Label_Entity (E : Entity_Id) is
1859    begin
1860       Set_Ekind           (E, E_Label);
1861       Set_Etype           (E, Standard_Void_Type);
1862       Set_Enclosing_Scope (E, Current_Scope);
1863       Set_Reachable       (E, True);
1864    end Analyze_Label_Entity;
1865
1866    ----------------------------
1867    -- Analyze_Loop_Statement --
1868    ----------------------------
1869
1870    procedure Analyze_Loop_Statement (N : Node_Id) is
1871       Loop_Statement : constant Node_Id := N;
1872
1873       Id   : constant Node_Id := Identifier (Loop_Statement);
1874       Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
1875       Ent  : Entity_Id;
1876
1877    begin
1878       if Present (Id) then
1879
1880          --  Make name visible, e.g. for use in exit statements. Loop
1881          --  labels are always considered to be referenced.
1882
1883          Analyze (Id);
1884          Ent := Entity (Id);
1885          Generate_Reference  (Ent, Loop_Statement, ' ');
1886          Generate_Definition (Ent);
1887
1888          --  If we found a label, mark its type. If not, ignore it, since it
1889          --  means we have a conflicting declaration, which would already have
1890          --  been diagnosed at declaration time. Set Label_Construct of the
1891          --  implicit label declaration, which is not created by the parser
1892          --  for generic units.
1893
1894          if Ekind (Ent) = E_Label then
1895             Set_Ekind (Ent, E_Loop);
1896
1897             if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1898                Set_Label_Construct (Parent (Ent), Loop_Statement);
1899             end if;
1900          end if;
1901
1902       --  Case of no identifier present
1903
1904       else
1905          Ent :=
1906            New_Internal_Entity
1907              (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
1908          Set_Etype (Ent,  Standard_Void_Type);
1909          Set_Parent (Ent, Loop_Statement);
1910       end if;
1911
1912       --  Kill current values on entry to loop, since statements in body
1913       --  of loop may have been executed before the loop is entered.
1914       --  Similarly we kill values after the loop, since we do not know
1915       --  that the body of the loop was executed.
1916
1917       Kill_Current_Values;
1918       Push_Scope (Ent);
1919       Analyze_Iteration_Scheme (Iter);
1920       Analyze_Statements (Statements (Loop_Statement));
1921       Process_End_Label (Loop_Statement, 'e', Ent);
1922       End_Scope;
1923       Kill_Current_Values;
1924       Check_Infinite_Loop_Warning (N);
1925    end Analyze_Loop_Statement;
1926
1927    ----------------------------
1928    -- Analyze_Null_Statement --
1929    ----------------------------
1930
1931    --  Note: the semantics of the null statement is implemented by a single
1932    --  null statement, too bad everything isn't as simple as this!
1933
1934    procedure Analyze_Null_Statement (N : Node_Id) is
1935       pragma Warnings (Off, N);
1936    begin
1937       null;
1938    end Analyze_Null_Statement;
1939
1940    ------------------------
1941    -- Analyze_Statements --
1942    ------------------------
1943
1944    procedure Analyze_Statements (L : List_Id) is
1945       S   : Node_Id;
1946       Lab : Entity_Id;
1947
1948    begin
1949       --  The labels declared in the statement list are reachable from
1950       --  statements in the list. We do this as a prepass so that any
1951       --  goto statement will be properly flagged if its target is not
1952       --  reachable. This is not required, but is nice behavior!
1953
1954       S := First (L);
1955       while Present (S) loop
1956          if Nkind (S) = N_Label then
1957             Analyze (Identifier (S));
1958             Lab := Entity (Identifier (S));
1959
1960             --  If we found a label mark it as reachable
1961
1962             if Ekind (Lab) = E_Label then
1963                Generate_Definition (Lab);
1964                Set_Reachable (Lab);
1965
1966                if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
1967                   Set_Label_Construct (Parent (Lab), S);
1968                end if;
1969
1970             --  If we failed to find a label, it means the implicit declaration
1971             --  of the label was hidden.  A for-loop parameter can do this to
1972             --  a label with the same name inside the loop, since the implicit
1973             --  label declaration is in the innermost enclosing body or block
1974             --  statement.
1975
1976             else
1977                Error_Msg_Sloc := Sloc (Lab);
1978                Error_Msg_N
1979                  ("implicit label declaration for & is hidden#",
1980                   Identifier (S));
1981             end if;
1982          end if;
1983
1984          Next (S);
1985       end loop;
1986
1987       --  Perform semantic analysis on all statements
1988
1989       Conditional_Statements_Begin;
1990
1991       S := First (L);
1992       while Present (S) loop
1993          Analyze (S);
1994          Next (S);
1995       end loop;
1996
1997       Conditional_Statements_End;
1998
1999       --  Make labels unreachable. Visibility is not sufficient, because
2000       --  labels in one if-branch for example are not reachable from the
2001       --  other branch, even though their declarations are in the enclosing
2002       --  declarative part.
2003
2004       S := First (L);
2005       while Present (S) loop
2006          if Nkind (S) = N_Label then
2007             Set_Reachable (Entity (Identifier (S)), False);
2008          end if;
2009
2010          Next (S);
2011       end loop;
2012    end Analyze_Statements;
2013
2014    ----------------------------
2015    -- Check_Unreachable_Code --
2016    ----------------------------
2017
2018    procedure Check_Unreachable_Code (N : Node_Id) is
2019       Error_Loc : Source_Ptr;
2020       P         : Node_Id;
2021
2022    begin
2023       if Is_List_Member (N)
2024         and then Comes_From_Source (N)
2025       then
2026          declare
2027             Nxt : Node_Id;
2028
2029          begin
2030             Nxt := Original_Node (Next (N));
2031
2032             --  If a label follows us, then we never have dead code, since
2033             --  someone could branch to the label, so we just ignore it.
2034
2035             if Nkind (Nxt) = N_Label then
2036                return;
2037
2038             --  Otherwise see if we have a real statement following us
2039
2040             elsif Present (Nxt)
2041               and then Comes_From_Source (Nxt)
2042               and then Is_Statement (Nxt)
2043             then
2044                --  Special very annoying exception. If we have a return that
2045                --  follows a raise, then we allow it without a warning, since
2046                --  the Ada RM annoyingly requires a useless return here!
2047
2048                if Nkind (Original_Node (N)) /= N_Raise_Statement
2049                  or else Nkind (Nxt) /= N_Simple_Return_Statement
2050                then
2051                   --  The rather strange shenanigans with the warning message
2052                   --  here reflects the fact that Kill_Dead_Code is very good
2053                   --  at removing warnings in deleted code, and this is one
2054                   --  warning we would prefer NOT to have removed.
2055
2056                   Error_Loc := Sloc (Nxt);
2057
2058                   --  If we have unreachable code, analyze and remove the
2059                   --  unreachable code, since it is useless and we don't
2060                   --  want to generate junk warnings.
2061
2062                   --  We skip this step if we are not in code generation mode.
2063                   --  This is the one case where we remove dead code in the
2064                   --  semantics as opposed to the expander, and we do not want
2065                   --  to remove code if we are not in code generation mode,
2066                   --  since this messes up the ASIS trees.
2067
2068                   --  Note that one might react by moving the whole circuit to
2069                   --  exp_ch5, but then we lose the warning in -gnatc mode.
2070
2071                   if Operating_Mode = Generate_Code then
2072                      loop
2073                         Nxt := Next (N);
2074
2075                         --  Quit deleting when we have nothing more to delete
2076                         --  or if we hit a label (since someone could transfer
2077                         --  control to a label, so we should not delete it).
2078
2079                         exit when No (Nxt) or else Nkind (Nxt) = N_Label;
2080
2081                         --  Statement/declaration is to be deleted
2082
2083                         Analyze (Nxt);
2084                         Remove (Nxt);
2085                         Kill_Dead_Code (Nxt);
2086                      end loop;
2087                   end if;
2088
2089                   --  Now issue the warning
2090
2091                   Error_Msg ("?unreachable code!", Error_Loc);
2092                end if;
2093
2094             --  If the unconditional transfer of control instruction is
2095             --  the last statement of a sequence, then see if our parent
2096             --  is one of the constructs for which we count unblocked exits,
2097             --  and if so, adjust the count.
2098
2099             else
2100                P := Parent (N);
2101
2102                --  Statements in THEN part or ELSE part of IF statement
2103
2104                if Nkind (P) = N_If_Statement then
2105                   null;
2106
2107                --  Statements in ELSIF part of an IF statement
2108
2109                elsif Nkind (P) = N_Elsif_Part then
2110                   P := Parent (P);
2111                   pragma Assert (Nkind (P) = N_If_Statement);
2112
2113                --  Statements in CASE statement alternative
2114
2115                elsif Nkind (P) = N_Case_Statement_Alternative then
2116                   P := Parent (P);
2117                   pragma Assert (Nkind (P) = N_Case_Statement);
2118
2119                --  Statements in body of block
2120
2121                elsif Nkind (P) = N_Handled_Sequence_Of_Statements
2122                  and then Nkind (Parent (P)) = N_Block_Statement
2123                then
2124                   null;
2125
2126                --  Statements in exception handler in a block
2127
2128                elsif Nkind (P) = N_Exception_Handler
2129                  and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
2130                  and then Nkind (Parent (Parent (P))) = N_Block_Statement
2131                then
2132                   null;
2133
2134                --  None of these cases, so return
2135
2136                else
2137                   return;
2138                end if;
2139
2140                --  This was one of the cases we are looking for (i.e. the
2141                --  parent construct was IF, CASE or block) so decrement count.
2142
2143                Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
2144             end if;
2145          end;
2146       end if;
2147    end Check_Unreachable_Code;
2148
2149 end Sem_Ch5;