OSDN Git Service

ada:
[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-2011, 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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Aspects;  use Aspects;
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_Ch6;  use Exp_Ch6;
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 Namet;    use Namet;
38 with Nlists;   use Nlists;
39 with Nmake;    use Nmake;
40 with Opt;      use Opt;
41 with Restrict; use Restrict;
42 with Rident;   use Rident;
43 with Rtsfind;  use Rtsfind;
44 with Sem;      use Sem;
45 with Sem_Aux;  use Sem_Aux;
46 with Sem_Case; use Sem_Case;
47 with Sem_Ch3;  use Sem_Ch3;
48 with Sem_Ch6;  use Sem_Ch6;
49 with Sem_Ch8;  use Sem_Ch8;
50 with Sem_Disp; use Sem_Disp;
51 with Sem_Elab; use Sem_Elab;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res;  use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Snames;   use Snames;
58 with Stand;    use Stand;
59 with Sinfo;    use Sinfo;
60 with Targparm; use Targparm;
61 with Tbuild;   use Tbuild;
62 with Uintp;    use Uintp;
63
64 package body Sem_Ch5 is
65
66    Unblocked_Exit_Count : Nat := 0;
67    --  This variable is used when processing if statements, case statements,
68    --  and block statements. It counts the number of exit points that are not
69    --  blocked by unconditional transfer instructions: for IF and CASE, these
70    --  are the branches of the conditional; for a block, they are the statement
71    --  sequence of the block, and the statement sequences of any exception
72    --  handlers that are part of the block. When processing is complete, if
73    --  this count is zero, it means that control cannot fall through the IF,
74    --  CASE or block statement. This is used for the generation of warning
75    --  messages. This variable is recursively saved on entry to processing the
76    --  construct, and restored on exit.
77
78    ------------------------
79    -- Analyze_Assignment --
80    ------------------------
81
82    procedure Analyze_Assignment (N : Node_Id) is
83       Lhs  : constant Node_Id := Name (N);
84       Rhs  : constant Node_Id := Expression (N);
85       T1   : Entity_Id;
86       T2   : Entity_Id;
87       Decl : Node_Id;
88
89       procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
90       --  N is the node for the left hand side of an assignment, and it is not
91       --  a variable. This routine issues an appropriate diagnostic.
92
93       procedure Kill_Lhs;
94       --  This is called to kill current value settings of a simple variable
95       --  on the left hand side. We call it if we find any error in analyzing
96       --  the assignment, and at the end of processing before setting any new
97       --  current values in place.
98
99       procedure Set_Assignment_Type
100         (Opnd      : Node_Id;
101          Opnd_Type : in out Entity_Id);
102       --  Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
103       --  nominal subtype. This procedure is used to deal with cases where the
104       --  nominal subtype must be replaced by the actual subtype.
105
106       -------------------------------
107       -- Diagnose_Non_Variable_Lhs --
108       -------------------------------
109
110       procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
111       begin
112          --  Not worth posting another error if left hand side already flagged
113          --  as being illegal in some respect.
114
115          if Error_Posted (N) then
116             return;
117
118          --  Some special bad cases of entity names
119
120          elsif Is_Entity_Name (N) then
121             declare
122                Ent : constant Entity_Id := Entity (N);
123
124             begin
125                if Ekind (Ent) = E_In_Parameter then
126                   Error_Msg_N
127                     ("assignment to IN mode parameter not allowed", N);
128
129                --  Renamings of protected private components are turned into
130                --  constants when compiling a protected function. In the case
131                --  of single protected types, the private component appears
132                --  directly.
133
134                elsif (Is_Prival (Ent)
135                         and then
136                           (Ekind (Current_Scope) = E_Function
137                              or else Ekind (Enclosing_Dynamic_Scope
138                                              (Current_Scope)) = E_Function))
139                    or else
140                      (Ekind (Ent) = E_Component
141                         and then Is_Protected_Type (Scope (Ent)))
142                then
143                   Error_Msg_N
144                     ("protected function cannot modify protected object", N);
145
146                elsif Ekind (Ent) = E_Loop_Parameter then
147                   Error_Msg_N
148                     ("assignment to loop parameter not allowed", N);
149
150                else
151                   Error_Msg_N
152                     ("left hand side of assignment must be a variable", N);
153                end if;
154             end;
155
156          --  For indexed components or selected components, test prefix
157
158          elsif Nkind (N) = N_Indexed_Component then
159             Diagnose_Non_Variable_Lhs (Prefix (N));
160
161          --  Another special case for assignment to discriminant
162
163          elsif Nkind (N) = N_Selected_Component then
164             if Present (Entity (Selector_Name (N)))
165               and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
166             then
167                Error_Msg_N
168                  ("assignment to discriminant not allowed", N);
169             else
170                Diagnose_Non_Variable_Lhs (Prefix (N));
171             end if;
172
173          else
174             --  If we fall through, we have no special message to issue!
175
176             Error_Msg_N ("left hand side of assignment must be a variable", N);
177          end if;
178       end Diagnose_Non_Variable_Lhs;
179
180       --------------
181       -- Kill_LHS --
182       --------------
183
184       procedure Kill_Lhs is
185       begin
186          if Is_Entity_Name (Lhs) then
187             declare
188                Ent : constant Entity_Id := Entity (Lhs);
189             begin
190                if Present (Ent) then
191                   Kill_Current_Values (Ent);
192                end if;
193             end;
194          end if;
195       end Kill_Lhs;
196
197       -------------------------
198       -- Set_Assignment_Type --
199       -------------------------
200
201       procedure Set_Assignment_Type
202         (Opnd      : Node_Id;
203          Opnd_Type : in out Entity_Id)
204       is
205       begin
206          Require_Entity (Opnd);
207
208          --  If the assignment operand is an in-out or out parameter, then we
209          --  get the actual subtype (needed for the unconstrained case). If the
210          --  operand is the actual in an entry declaration, then within the
211          --  accept statement it is replaced with a local renaming, which may
212          --  also have an actual subtype.
213
214          if Is_Entity_Name (Opnd)
215            and then (Ekind (Entity (Opnd)) = E_Out_Parameter
216                       or else Ekind (Entity (Opnd)) =
217                            E_In_Out_Parameter
218                       or else Ekind (Entity (Opnd)) =
219                            E_Generic_In_Out_Parameter
220                       or else
221                         (Ekind (Entity (Opnd)) = E_Variable
222                           and then Nkind (Parent (Entity (Opnd))) =
223                              N_Object_Renaming_Declaration
224                           and then Nkind (Parent (Parent (Entity (Opnd)))) =
225                              N_Accept_Statement))
226          then
227             Opnd_Type := Get_Actual_Subtype (Opnd);
228
229          --  If assignment operand is a component reference, then we get the
230          --  actual subtype of the component for the unconstrained case.
231
232          elsif Nkind_In (Opnd, N_Selected_Component, N_Explicit_Dereference)
233            and then not Is_Unchecked_Union (Opnd_Type)
234          then
235             Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
236
237             if Present (Decl) then
238                Insert_Action (N, Decl);
239                Mark_Rewrite_Insertion (Decl);
240                Analyze (Decl);
241                Opnd_Type := Defining_Identifier (Decl);
242                Set_Etype (Opnd, Opnd_Type);
243                Freeze_Itype (Opnd_Type, N);
244
245             elsif Is_Constrained (Etype (Opnd)) then
246                Opnd_Type := Etype (Opnd);
247             end if;
248
249          --  For slice, use the constrained subtype created for the slice
250
251          elsif Nkind (Opnd) = N_Slice then
252             Opnd_Type := Etype (Opnd);
253          end if;
254       end Set_Assignment_Type;
255
256    --  Start of processing for Analyze_Assignment
257
258    begin
259       Mark_Coextensions (N, Rhs);
260
261       Analyze (Rhs);
262       Analyze (Lhs);
263
264       --  Ensure that we never do an assignment on a variable marked as
265       --  as Safe_To_Reevaluate.
266
267       pragma Assert (not Is_Entity_Name (Lhs)
268         or else Ekind (Entity (Lhs)) /= E_Variable
269         or else not Is_Safe_To_Reevaluate (Entity (Lhs)));
270
271       --  Start type analysis for assignment
272
273       T1 := Etype (Lhs);
274
275       --  In the most general case, both Lhs and Rhs can be overloaded, and we
276       --  must compute the intersection of the possible types on each side.
277
278       if Is_Overloaded (Lhs) then
279          declare
280             I  : Interp_Index;
281             It : Interp;
282
283          begin
284             T1 := Any_Type;
285             Get_First_Interp (Lhs, I, It);
286
287             while Present (It.Typ) loop
288                if Has_Compatible_Type (Rhs, It.Typ) then
289                   if T1 /= Any_Type then
290
291                      --  An explicit dereference is overloaded if the prefix
292                      --  is. Try to remove the ambiguity on the prefix, the
293                      --  error will be posted there if the ambiguity is real.
294
295                      if Nkind (Lhs) = N_Explicit_Dereference then
296                         declare
297                            PI    : Interp_Index;
298                            PI1   : Interp_Index := 0;
299                            PIt   : Interp;
300                            Found : Boolean;
301
302                         begin
303                            Found := False;
304                            Get_First_Interp (Prefix (Lhs), PI, PIt);
305
306                            while Present (PIt.Typ) loop
307                               if Is_Access_Type (PIt.Typ)
308                                 and then Has_Compatible_Type
309                                            (Rhs, Designated_Type (PIt.Typ))
310                               then
311                                  if Found then
312                                     PIt :=
313                                       Disambiguate (Prefix (Lhs),
314                                         PI1, PI, Any_Type);
315
316                                     if PIt = No_Interp then
317                                        Error_Msg_N
318                                          ("ambiguous left-hand side"
319                                             & " in assignment", Lhs);
320                                        exit;
321                                     else
322                                        Resolve (Prefix (Lhs), PIt.Typ);
323                                     end if;
324
325                                     exit;
326                                  else
327                                     Found := True;
328                                     PI1 := PI;
329                                  end if;
330                               end if;
331
332                               Get_Next_Interp (PI, PIt);
333                            end loop;
334                         end;
335
336                      else
337                         Error_Msg_N
338                           ("ambiguous left-hand side in assignment", Lhs);
339                         exit;
340                      end if;
341                   else
342                      T1 := It.Typ;
343                   end if;
344                end if;
345
346                Get_Next_Interp (I, It);
347             end loop;
348          end;
349
350          if T1 = Any_Type then
351             Error_Msg_N
352               ("no valid types for left-hand side for assignment", Lhs);
353             Kill_Lhs;
354             return;
355          end if;
356       end if;
357
358       --  The resulting assignment type is T1, so now we will resolve the left
359       --  hand side of the assignment using this determined type.
360
361       Resolve (Lhs, T1);
362
363       --  Cases where Lhs is not a variable
364
365       if not Is_Variable (Lhs) then
366
367          --  Ada 2005 (AI-327): Check assignment to the attribute Priority of a
368          --  protected object.
369
370          declare
371             Ent : Entity_Id;
372             S   : Entity_Id;
373
374          begin
375             if Ada_Version >= Ada_2005 then
376
377                --  Handle chains of renamings
378
379                Ent := Lhs;
380                while Nkind (Ent) in N_Has_Entity
381                  and then Present (Entity (Ent))
382                  and then Present (Renamed_Object (Entity (Ent)))
383                loop
384                   Ent := Renamed_Object (Entity (Ent));
385                end loop;
386
387                if (Nkind (Ent) = N_Attribute_Reference
388                      and then Attribute_Name (Ent) = Name_Priority)
389
390                   --  Renamings of the attribute Priority applied to protected
391                   --  objects have been previously expanded into calls to the
392                   --  Get_Ceiling run-time subprogram.
393
394                  or else
395                   (Nkind (Ent) = N_Function_Call
396                      and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
397                                 or else
398                                Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
399                then
400                   --  The enclosing subprogram cannot be a protected function
401
402                   S := Current_Scope;
403                   while not (Is_Subprogram (S)
404                                and then Convention (S) = Convention_Protected)
405                      and then S /= Standard_Standard
406                   loop
407                      S := Scope (S);
408                   end loop;
409
410                   if Ekind (S) = E_Function
411                     and then Convention (S) = Convention_Protected
412                   then
413                      Error_Msg_N
414                        ("protected function cannot modify protected object",
415                         Lhs);
416                   end if;
417
418                   --  Changes of the ceiling priority of the protected object
419                   --  are only effective if the Ceiling_Locking policy is in
420                   --  effect (AARM D.5.2 (5/2)).
421
422                   if Locking_Policy /= 'C' then
423                      Error_Msg_N ("assignment to the attribute PRIORITY has " &
424                                   "no effect?", Lhs);
425                      Error_Msg_N ("\since no Locking_Policy has been " &
426                                   "specified", Lhs);
427                   end if;
428
429                   return;
430                end if;
431             end if;
432          end;
433
434          Diagnose_Non_Variable_Lhs (Lhs);
435          return;
436
437       --  Error of assigning to limited type. We do however allow this in
438       --  certain cases where the front end generates the assignments.
439
440       elsif Is_Limited_Type (T1)
441         and then not Assignment_OK (Lhs)
442         and then not Assignment_OK (Original_Node (Lhs))
443         and then not Is_Value_Type (T1)
444       then
445          --  CPP constructors can only be called in declarations
446
447          if Is_CPP_Constructor_Call (Rhs) then
448             Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
449          else
450             Error_Msg_N
451               ("left hand of assignment must not be limited type", Lhs);
452             Explain_Limited_Type (T1, Lhs);
453          end if;
454          return;
455
456       --  Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
457       --  abstract. This is only checked when the assignment Comes_From_Source,
458       --  because in some cases the expander generates such assignments (such
459       --  in the _assign operation for an abstract type).
460
461       elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
462          Error_Msg_N
463            ("target of assignment operation must not be abstract", Lhs);
464       end if;
465
466       --  Resolution may have updated the subtype, in case the left-hand side
467       --  is a private protected component. Use the correct subtype to avoid
468       --  scoping issues in the back-end.
469
470       T1 := Etype (Lhs);
471
472       --  Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
473       --  type. For example:
474
475       --    limited with P;
476       --    package Pkg is
477       --      type Acc is access P.T;
478       --    end Pkg;
479
480       --    with Pkg; use Acc;
481       --    procedure Example is
482       --       A, B : Acc;
483       --    begin
484       --       A.all := B.all;  -- ERROR
485       --    end Example;
486
487       if Nkind (Lhs) = N_Explicit_Dereference
488         and then Ekind (T1) = E_Incomplete_Type
489       then
490          Error_Msg_N ("invalid use of incomplete type", Lhs);
491          Kill_Lhs;
492          return;
493       end if;
494
495       --  Now we can complete the resolution of the right hand side
496
497       Set_Assignment_Type (Lhs, T1);
498       Resolve (Rhs, T1);
499
500       --  This is the point at which we check for an unset reference
501
502       Check_Unset_Reference (Rhs);
503       Check_Unprotected_Access (Lhs, Rhs);
504
505       --  Remaining steps are skipped if Rhs was syntactically in error
506
507       if Rhs = Error then
508          Kill_Lhs;
509          return;
510       end if;
511
512       T2 := Etype (Rhs);
513
514       if not Covers (T1, T2) then
515          Wrong_Type (Rhs, Etype (Lhs));
516          Kill_Lhs;
517          return;
518       end if;
519
520       --  Ada 2005 (AI-326): In case of explicit dereference of incomplete
521       --  types, use the non-limited view if available
522
523       if Nkind (Rhs) = N_Explicit_Dereference
524         and then Ekind (T2) = E_Incomplete_Type
525         and then Is_Tagged_Type (T2)
526         and then Present (Non_Limited_View (T2))
527       then
528          T2 := Non_Limited_View (T2);
529       end if;
530
531       Set_Assignment_Type (Rhs, T2);
532
533       if Total_Errors_Detected /= 0 then
534          if No (T1) then
535             T1 := Any_Type;
536          end if;
537
538          if No (T2) then
539             T2 := Any_Type;
540          end if;
541       end if;
542
543       if T1 = Any_Type or else T2 = Any_Type then
544          Kill_Lhs;
545          return;
546       end if;
547
548       --  If the rhs is class-wide or dynamically tagged, then require the lhs
549       --  to be class-wide. The case where the rhs is a dynamically tagged call
550       --  to a dispatching operation with a controlling access result is
551       --  excluded from this check, since the target has an access type (and
552       --  no tag propagation occurs in that case).
553
554       if (Is_Class_Wide_Type (T2)
555            or else (Is_Dynamically_Tagged (Rhs)
556                      and then not Is_Access_Type (T1)))
557         and then not Is_Class_Wide_Type (T1)
558       then
559          Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
560
561       elsif Is_Class_Wide_Type (T1)
562         and then not Is_Class_Wide_Type (T2)
563         and then not Is_Tag_Indeterminate (Rhs)
564         and then not Is_Dynamically_Tagged (Rhs)
565       then
566          Error_Msg_N ("dynamically tagged expression required!", Rhs);
567       end if;
568
569       --  Propagate the tag from a class-wide target to the rhs when the rhs
570       --  is a tag-indeterminate call.
571
572       if Is_Tag_Indeterminate (Rhs) then
573          if Is_Class_Wide_Type (T1) then
574             Propagate_Tag (Lhs, Rhs);
575
576          elsif Nkind (Rhs) = N_Function_Call
577               and then Is_Entity_Name (Name (Rhs))
578               and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
579          then
580             Error_Msg_N
581               ("call to abstract function must be dispatching", Name (Rhs));
582
583          elsif Nkind (Rhs) = N_Qualified_Expression
584            and then Nkind (Expression (Rhs)) = N_Function_Call
585               and then Is_Entity_Name (Name (Expression (Rhs)))
586               and then
587                 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
588          then
589             Error_Msg_N
590               ("call to abstract function must be dispatching",
591                 Name (Expression (Rhs)));
592          end if;
593       end if;
594
595       --  Ada 2005 (AI-385): When the lhs type is an anonymous access type,
596       --  apply an implicit conversion of the rhs to that type to force
597       --  appropriate static and run-time accessibility checks. This applies
598       --  as well to anonymous access-to-subprogram types that are component
599       --  subtypes or formal parameters.
600
601       if Ada_Version >= Ada_2005
602         and then Is_Access_Type (T1)
603       then
604          if Is_Local_Anonymous_Access (T1)
605            or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
606
607            --  Handle assignment to an Ada 2012 stand-alone object
608            --  of an anonymous access type.
609
610            or else (Ekind (T1) = E_Anonymous_Access_Type
611                      and then Nkind (Associated_Node_For_Itype (T1)) =
612                                                        N_Object_Declaration)
613
614          then
615             Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
616             Analyze_And_Resolve (Rhs, T1);
617          end if;
618       end if;
619
620       --  Ada 2005 (AI-231): Assignment to not null variable
621
622       if Ada_Version >= Ada_2005
623         and then Can_Never_Be_Null (T1)
624         and then not Assignment_OK (Lhs)
625       then
626          --  Case where we know the right hand side is null
627
628          if Known_Null (Rhs) then
629             Apply_Compile_Time_Constraint_Error
630               (N   => Rhs,
631                Msg => "(Ada 2005) null not allowed in null-excluding objects?",
632                Reason => CE_Null_Not_Allowed);
633
634             --  We still mark this as a possible modification, that's necessary
635             --  to reset Is_True_Constant, and desirable for xref purposes.
636
637             Note_Possible_Modification (Lhs, Sure => True);
638             return;
639
640          --  If we know the right hand side is non-null, then we convert to the
641          --  target type, since we don't need a run time check in that case.
642
643          elsif not Can_Never_Be_Null (T2) then
644             Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
645             Analyze_And_Resolve (Rhs, T1);
646          end if;
647       end if;
648
649       if Is_Scalar_Type (T1) then
650          Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
651
652       --  For array types, verify that lengths match. If the right hand side
653       --  is a function call that has been inlined, the assignment has been
654       --  rewritten as a block, and the constraint check will be applied to the
655       --  assignment within the block.
656
657       elsif Is_Array_Type (T1)
658         and then
659           (Nkind (Rhs) /= N_Type_Conversion
660             or else Is_Constrained (Etype (Rhs)))
661         and then
662           (Nkind (Rhs) /= N_Function_Call
663             or else Nkind (N) /= N_Block_Statement)
664       then
665          --  Assignment verifies that the length of the Lsh and Rhs are equal,
666          --  but of course the indexes do not have to match. If the right-hand
667          --  side is a type conversion to an unconstrained type, a length check
668          --  is performed on the expression itself during expansion. In rare
669          --  cases, the redundant length check is computed on an index type
670          --  with a different representation, triggering incorrect code in the
671          --  back end.
672
673          Apply_Length_Check (Rhs, Etype (Lhs));
674
675       else
676          --  Discriminant checks are applied in the course of expansion
677
678          null;
679       end if;
680
681       --  Note: modifications of the Lhs may only be recorded after
682       --  checks have been applied.
683
684       Note_Possible_Modification (Lhs, Sure => True);
685       Check_Order_Dependence;
686
687       --  ??? a real accessibility check is needed when ???
688
689       --  Post warning for redundant assignment or variable to itself
690
691       if Warn_On_Redundant_Constructs
692
693          --  We only warn for source constructs
694
695          and then Comes_From_Source (N)
696
697          --  Where the object is the same on both sides
698
699          and then Same_Object (Lhs, Original_Node (Rhs))
700
701          --  But exclude the case where the right side was an operation that
702          --  got rewritten (e.g. JUNK + K, where K was known to be zero). We
703          --  don't want to warn in such a case, since it is reasonable to write
704          --  such expressions especially when K is defined symbolically in some
705          --  other package.
706
707         and then Nkind (Original_Node (Rhs)) not in N_Op
708       then
709          if Nkind (Lhs) in N_Has_Entity then
710             Error_Msg_NE -- CODEFIX
711               ("?useless assignment of & to itself!", N, Entity (Lhs));
712          else
713             Error_Msg_N -- CODEFIX
714               ("?useless assignment of object to itself!", N);
715          end if;
716       end if;
717
718       --  Check for non-allowed composite assignment
719
720       if not Support_Composite_Assign_On_Target
721         and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
722         and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
723       then
724          Error_Msg_CRT ("composite assignment", N);
725       end if;
726
727       --  Check elaboration warning for left side if not in elab code
728
729       if not In_Subprogram_Or_Concurrent_Unit then
730          Check_Elab_Assign (Lhs);
731       end if;
732
733       --  Set Referenced_As_LHS if appropriate. We only set this flag if the
734       --  assignment is a source assignment in the extended main source unit.
735       --  We are not interested in any reference information outside this
736       --  context, or in compiler generated assignment statements.
737
738       if Comes_From_Source (N)
739         and then In_Extended_Main_Source_Unit (Lhs)
740       then
741          Set_Referenced_Modified (Lhs, Out_Param => False);
742       end if;
743
744       --  Final step. If left side is an entity, then we may be able to reset
745       --  the current tracked values to new safe values. We only have something
746       --  to do if the left side is an entity name, and expansion has not
747       --  modified the node into something other than an assignment, and of
748       --  course we only capture values if it is safe to do so.
749
750       if Is_Entity_Name (Lhs)
751         and then Nkind (N) = N_Assignment_Statement
752       then
753          declare
754             Ent : constant Entity_Id := Entity (Lhs);
755
756          begin
757             if Safe_To_Capture_Value (N, Ent) then
758
759                --  If simple variable on left side, warn if this assignment
760                --  blots out another one (rendering it useless). We only do
761                --  this for source assignments, otherwise we can generate bogus
762                --  warnings when an assignment is rewritten as another
763                --  assignment, and gets tied up with itself.
764
765                if Warn_On_Modified_Unread
766                  and then Is_Assignable (Ent)
767                  and then Comes_From_Source (N)
768                  and then In_Extended_Main_Source_Unit (Ent)
769                then
770                   Warn_On_Useless_Assignment (Ent, N);
771                end if;
772
773                --  If we are assigning an access type and the left side is an
774                --  entity, then make sure that the Is_Known_[Non_]Null flags
775                --  properly reflect the state of the entity after assignment.
776
777                if Is_Access_Type (T1) then
778                   if Known_Non_Null (Rhs) then
779                      Set_Is_Known_Non_Null (Ent, True);
780
781                   elsif Known_Null (Rhs)
782                     and then not Can_Never_Be_Null (Ent)
783                   then
784                      Set_Is_Known_Null (Ent, True);
785
786                   else
787                      Set_Is_Known_Null (Ent, False);
788
789                      if not Can_Never_Be_Null (Ent) then
790                         Set_Is_Known_Non_Null (Ent, False);
791                      end if;
792                   end if;
793
794                --  For discrete types, we may be able to set the current value
795                --  if the value is known at compile time.
796
797                elsif Is_Discrete_Type (T1)
798                  and then Compile_Time_Known_Value (Rhs)
799                then
800                   Set_Current_Value (Ent, Rhs);
801                else
802                   Set_Current_Value (Ent, Empty);
803                end if;
804
805             --  If not safe to capture values, kill them
806
807             else
808                Kill_Lhs;
809             end if;
810          end;
811       end if;
812
813       --  If assigning to an object in whole or in part, note location of
814       --  assignment in case no one references value. We only do this for
815       --  source assignments, otherwise we can generate bogus warnings when an
816       --  assignment is rewritten as another assignment, and gets tied up with
817       --  itself.
818
819       declare
820          Ent : constant Entity_Id := Get_Enclosing_Object (Lhs);
821
822       begin
823          if Present (Ent)
824            and then Safe_To_Capture_Value (N, Ent)
825            and then Nkind (N) = N_Assignment_Statement
826            and then Warn_On_Modified_Unread
827            and then Is_Assignable (Ent)
828            and then Comes_From_Source (N)
829            and then In_Extended_Main_Source_Unit (Ent)
830          then
831             Set_Last_Assignment (Ent, Lhs);
832          end if;
833       end;
834    end Analyze_Assignment;
835
836    -----------------------------
837    -- Analyze_Block_Statement --
838    -----------------------------
839
840    procedure Analyze_Block_Statement (N : Node_Id) is
841       procedure Install_Return_Entities (Scop : Entity_Id);
842       --  Install all entities of return statement scope Scop in the visibility
843       --  chain except for the return object since its entity is reused in a
844       --  renaming.
845
846       -----------------------------
847       -- Install_Return_Entities --
848       -----------------------------
849
850       procedure Install_Return_Entities (Scop : Entity_Id) is
851          Id : Entity_Id;
852
853       begin
854          Id := First_Entity (Scop);
855          while Present (Id) loop
856
857             --  Do not install the return object
858
859             if not Ekind_In (Id, E_Constant, E_Variable)
860               or else not Is_Return_Object (Id)
861             then
862                Install_Entity (Id);
863             end if;
864
865             Next_Entity (Id);
866          end loop;
867       end Install_Return_Entities;
868
869       --  Local constants and variables
870
871       Decls : constant List_Id := Declarations (N);
872       Id    : constant Node_Id := Identifier (N);
873       HSS   : constant Node_Id := Handled_Statement_Sequence (N);
874
875       Is_BIP_Return_Statement : Boolean;
876
877    --  Start of processing for Analyze_Block_Statement
878
879    begin
880       --  In SPARK mode, we reject block statements. Note that the case of
881       --  block statements generated by the expander is fine.
882
883       if Nkind (Original_Node (N)) = N_Block_Statement then
884          Check_SPARK_Restriction ("block statement is not allowed", N);
885       end if;
886
887       --  If no handled statement sequence is present, things are really messed
888       --  up, and we just return immediately (defence against previous errors).
889
890       if No (HSS) then
891          return;
892       end if;
893
894       --  Detect whether the block is actually a rewritten return statement of
895       --  a build-in-place function.
896
897       Is_BIP_Return_Statement :=
898         Present (Id)
899           and then Present (Entity (Id))
900           and then Ekind (Entity (Id)) = E_Return_Statement
901           and then Is_Build_In_Place_Function
902                      (Return_Applies_To (Entity (Id)));
903
904       --  Normal processing with HSS present
905
906       declare
907          EH  : constant List_Id := Exception_Handlers (HSS);
908          Ent : Entity_Id        := Empty;
909          S   : Entity_Id;
910
911          Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
912          --  Recursively save value of this global, will be restored on exit
913
914       begin
915          --  Initialize unblocked exit count for statements of begin block
916          --  plus one for each exception handler that is present.
917
918          Unblocked_Exit_Count := 1;
919
920          if Present (EH) then
921             Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
922          end if;
923
924          --  If a label is present analyze it and mark it as referenced
925
926          if Present (Id) then
927             Analyze (Id);
928             Ent := Entity (Id);
929
930             --  An error defense. If we have an identifier, but no entity, then
931             --  something is wrong. If previous errors, then just remove the
932             --  identifier and continue, otherwise raise an exception.
933
934             if No (Ent) then
935                if Total_Errors_Detected /= 0 then
936                   Set_Identifier (N, Empty);
937                else
938                   raise Program_Error;
939                end if;
940
941             else
942                Set_Ekind (Ent, E_Block);
943                Generate_Reference (Ent, N, ' ');
944                Generate_Definition (Ent);
945
946                if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
947                   Set_Label_Construct (Parent (Ent), N);
948                end if;
949             end if;
950          end if;
951
952          --  If no entity set, create a label entity
953
954          if No (Ent) then
955             Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
956             Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
957             Set_Parent (Ent, N);
958          end if;
959
960          Set_Etype (Ent, Standard_Void_Type);
961          Set_Block_Node (Ent, Identifier (N));
962          Push_Scope (Ent);
963
964          --  The block served as an extended return statement. Ensure that any
965          --  entities created during the analysis and expansion of the return
966          --  object declaration are once again visible.
967
968          if Is_BIP_Return_Statement then
969             Install_Return_Entities (Ent);
970          end if;
971
972          if Present (Decls) then
973             Analyze_Declarations (Decls);
974             Check_Completion;
975             Inspect_Deferred_Constant_Completion (Decls);
976          end if;
977
978          Analyze (HSS);
979          Process_End_Label (HSS, 'e', Ent);
980
981          --  If exception handlers are present, then we indicate that enclosing
982          --  scopes contain a block with handlers. We only need to mark non-
983          --  generic scopes.
984
985          if Present (EH) then
986             S := Scope (Ent);
987             loop
988                Set_Has_Nested_Block_With_Handler (S);
989                exit when Is_Overloadable (S)
990                  or else Ekind (S) = E_Package
991                  or else Is_Generic_Unit (S);
992                S := Scope (S);
993             end loop;
994          end if;
995
996          Check_References (Ent);
997          Warn_On_Useless_Assignments (Ent);
998          End_Scope;
999
1000          if Unblocked_Exit_Count = 0 then
1001             Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1002             Check_Unreachable_Code (N);
1003          else
1004             Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1005          end if;
1006       end;
1007    end Analyze_Block_Statement;
1008
1009    ----------------------------
1010    -- Analyze_Case_Statement --
1011    ----------------------------
1012
1013    procedure Analyze_Case_Statement (N : Node_Id) is
1014       Exp            : Node_Id;
1015       Exp_Type       : Entity_Id;
1016       Exp_Btype      : Entity_Id;
1017       Last_Choice    : Nat;
1018       Dont_Care      : Boolean;
1019       Others_Present : Boolean;
1020
1021       pragma Warnings (Off, Last_Choice);
1022       pragma Warnings (Off, Dont_Care);
1023       --  Don't care about assigned values
1024
1025       Statements_Analyzed : Boolean := False;
1026       --  Set True if at least some statement sequences get analyzed. If False
1027       --  on exit, means we had a serious error that prevented full analysis of
1028       --  the case statement, and as a result it is not a good idea to output
1029       --  warning messages about unreachable code.
1030
1031       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1032       --  Recursively save value of this global, will be restored on exit
1033
1034       procedure Non_Static_Choice_Error (Choice : Node_Id);
1035       --  Error routine invoked by the generic instantiation below when the
1036       --  case statement has a non static choice.
1037
1038       procedure Process_Statements (Alternative : Node_Id);
1039       --  Analyzes all the statements associated with a case alternative.
1040       --  Needed by the generic instantiation below.
1041
1042       package Case_Choices_Processing is new
1043         Generic_Choices_Processing
1044           (Get_Alternatives          => Alternatives,
1045            Get_Choices               => Discrete_Choices,
1046            Process_Empty_Choice      => No_OP,
1047            Process_Non_Static_Choice => Non_Static_Choice_Error,
1048            Process_Associated_Node   => Process_Statements);
1049       use Case_Choices_Processing;
1050       --  Instantiation of the generic choice processing package
1051
1052       -----------------------------
1053       -- Non_Static_Choice_Error --
1054       -----------------------------
1055
1056       procedure Non_Static_Choice_Error (Choice : Node_Id) is
1057       begin
1058          Flag_Non_Static_Expr
1059            ("choice given in case statement is not static!", Choice);
1060       end Non_Static_Choice_Error;
1061
1062       ------------------------
1063       -- Process_Statements --
1064       ------------------------
1065
1066       procedure Process_Statements (Alternative : Node_Id) is
1067          Choices : constant List_Id := Discrete_Choices (Alternative);
1068          Ent     : Entity_Id;
1069
1070       begin
1071          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1072          Statements_Analyzed := True;
1073
1074          --  An interesting optimization. If the case statement expression
1075          --  is a simple entity, then we can set the current value within an
1076          --  alternative if the alternative has one possible value.
1077
1078          --    case N is
1079          --      when 1      => alpha
1080          --      when 2 | 3  => beta
1081          --      when others => gamma
1082
1083          --  Here we know that N is initially 1 within alpha, but for beta and
1084          --  gamma, we do not know anything more about the initial value.
1085
1086          if Is_Entity_Name (Exp) then
1087             Ent := Entity (Exp);
1088
1089             if Ekind_In (Ent, E_Variable,
1090                               E_In_Out_Parameter,
1091                               E_Out_Parameter)
1092             then
1093                if List_Length (Choices) = 1
1094                  and then Nkind (First (Choices)) in N_Subexpr
1095                  and then Compile_Time_Known_Value (First (Choices))
1096                then
1097                   Set_Current_Value (Entity (Exp), First (Choices));
1098                end if;
1099
1100                Analyze_Statements (Statements (Alternative));
1101
1102                --  After analyzing the case, set the current value to empty
1103                --  since we won't know what it is for the next alternative
1104                --  (unless reset by this same circuit), or after the case.
1105
1106                Set_Current_Value (Entity (Exp), Empty);
1107                return;
1108             end if;
1109          end if;
1110
1111          --  Case where expression is not an entity name of a variable
1112
1113          Analyze_Statements (Statements (Alternative));
1114       end Process_Statements;
1115
1116    --  Start of processing for Analyze_Case_Statement
1117
1118    begin
1119       Unblocked_Exit_Count := 0;
1120       Exp := Expression (N);
1121       Analyze (Exp);
1122
1123       --  The expression must be of any discrete type. In rare cases, the
1124       --  expander constructs a case statement whose expression has a private
1125       --  type whose full view is discrete. This can happen when generating
1126       --  a stream operation for a variant type after the type is frozen,
1127       --  when the partial of view of the type of the discriminant is private.
1128       --  In that case, use the full view to analyze case alternatives.
1129
1130       if not Is_Overloaded (Exp)
1131         and then not Comes_From_Source (N)
1132         and then Is_Private_Type (Etype (Exp))
1133         and then Present (Full_View (Etype (Exp)))
1134         and then Is_Discrete_Type (Full_View (Etype (Exp)))
1135       then
1136          Resolve (Exp, Etype (Exp));
1137          Exp_Type := Full_View (Etype (Exp));
1138
1139       else
1140          Analyze_And_Resolve (Exp, Any_Discrete);
1141          Exp_Type := Etype (Exp);
1142       end if;
1143
1144       Check_Unset_Reference (Exp);
1145       Exp_Btype := Base_Type (Exp_Type);
1146
1147       --  The expression must be of a discrete type which must be determinable
1148       --  independently of the context in which the expression occurs, but
1149       --  using the fact that the expression must be of a discrete type.
1150       --  Moreover, the type this expression must not be a character literal
1151       --  (which is always ambiguous) or, for Ada-83, a generic formal type.
1152
1153       --  If error already reported by Resolve, nothing more to do
1154
1155       if Exp_Btype = Any_Discrete
1156         or else Exp_Btype = Any_Type
1157       then
1158          return;
1159
1160       elsif Exp_Btype = Any_Character then
1161          Error_Msg_N
1162            ("character literal as case expression is ambiguous", Exp);
1163          return;
1164
1165       elsif Ada_Version = Ada_83
1166         and then (Is_Generic_Type (Exp_Btype)
1167                     or else Is_Generic_Type (Root_Type (Exp_Btype)))
1168       then
1169          Error_Msg_N
1170            ("(Ada 83) case expression cannot be of a generic type", Exp);
1171          return;
1172       end if;
1173
1174       --  If the case expression is a formal object of mode in out, then treat
1175       --  it as having a nonstatic subtype by forcing use of the base type
1176       --  (which has to get passed to Check_Case_Choices below). Also use base
1177       --  type when the case expression is parenthesized.
1178
1179       if Paren_Count (Exp) > 0
1180         or else (Is_Entity_Name (Exp)
1181                   and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1182       then
1183          Exp_Type := Exp_Btype;
1184       end if;
1185
1186       --  Call instantiated Analyze_Choices which does the rest of the work
1187
1188       Analyze_Choices (N, Exp_Type, Dont_Care, Others_Present);
1189
1190       --  A case statement with a single OTHERS alternative is not allowed
1191       --  in SPARK.
1192
1193       if Others_Present
1194         and then List_Length (Alternatives (N)) = 1
1195       then
1196          Check_SPARK_Restriction
1197            ("OTHERS as unique case alternative is not allowed", N);
1198       end if;
1199
1200       if Exp_Type = Universal_Integer and then not Others_Present then
1201          Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1202       end if;
1203
1204       --  If all our exits were blocked by unconditional transfers of control,
1205       --  then the entire CASE statement acts as an unconditional transfer of
1206       --  control, so treat it like one, and check unreachable code. Skip this
1207       --  test if we had serious errors preventing any statement analysis.
1208
1209       if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1210          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1211          Check_Unreachable_Code (N);
1212       else
1213          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1214       end if;
1215
1216       if not Expander_Active
1217         and then Compile_Time_Known_Value (Expression (N))
1218         and then Serious_Errors_Detected = 0
1219       then
1220          declare
1221             Chosen : constant Node_Id := Find_Static_Alternative (N);
1222             Alt    : Node_Id;
1223
1224          begin
1225             Alt := First (Alternatives (N));
1226             while Present (Alt) loop
1227                if Alt /= Chosen then
1228                   Remove_Warning_Messages (Statements (Alt));
1229                end if;
1230
1231                Next (Alt);
1232             end loop;
1233          end;
1234       end if;
1235    end Analyze_Case_Statement;
1236
1237    ----------------------------
1238    -- Analyze_Exit_Statement --
1239    ----------------------------
1240
1241    --  If the exit includes a name, it must be the name of a currently open
1242    --  loop. Otherwise there must be an innermost open loop on the stack, to
1243    --  which the statement implicitly refers.
1244
1245    --  Additionally, in SPARK mode:
1246
1247    --    The exit can only name the closest enclosing loop;
1248
1249    --    An exit with a when clause must be directly contained in a loop;
1250
1251    --    An exit without a when clause must be directly contained in an
1252    --    if-statement with no elsif or else, which is itself directly contained
1253    --    in a loop. The exit must be the last statement in the if-statement.
1254
1255    procedure Analyze_Exit_Statement (N : Node_Id) is
1256       Target   : constant Node_Id := Name (N);
1257       Cond     : constant Node_Id := Condition (N);
1258       Scope_Id : Entity_Id;
1259       U_Name   : Entity_Id;
1260       Kind     : Entity_Kind;
1261
1262    begin
1263       if No (Cond) then
1264          Check_Unreachable_Code (N);
1265       end if;
1266
1267       if Present (Target) then
1268          Analyze (Target);
1269          U_Name := Entity (Target);
1270
1271          if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1272             Error_Msg_N ("invalid loop name in exit statement", N);
1273             return;
1274
1275          else
1276             if Has_Loop_In_Inner_Open_Scopes (U_Name) then
1277                Check_SPARK_Restriction
1278                  ("exit label must name the closest enclosing loop", N);
1279             end if;
1280
1281             Set_Has_Exit (U_Name);
1282          end if;
1283
1284       else
1285          U_Name := Empty;
1286       end if;
1287
1288       for J in reverse 0 .. Scope_Stack.Last loop
1289          Scope_Id := Scope_Stack.Table (J).Entity;
1290          Kind := Ekind (Scope_Id);
1291
1292          if Kind = E_Loop
1293            and then (No (Target) or else Scope_Id = U_Name)
1294          then
1295             Set_Has_Exit (Scope_Id);
1296             exit;
1297
1298          elsif Kind = E_Block
1299            or else Kind = E_Loop
1300            or else Kind = E_Return_Statement
1301          then
1302             null;
1303
1304          else
1305             Error_Msg_N
1306               ("cannot exit from program unit or accept statement", N);
1307             return;
1308          end if;
1309       end loop;
1310
1311       --  Verify that if present the condition is a Boolean expression
1312
1313       if Present (Cond) then
1314          Analyze_And_Resolve (Cond, Any_Boolean);
1315          Check_Unset_Reference (Cond);
1316       end if;
1317
1318       --  In SPARK mode, verify that the exit statement respects the SPARK
1319       --  restrictions.
1320
1321       if Present (Cond) then
1322          if Nkind (Parent (N)) /= N_Loop_Statement then
1323             Check_SPARK_Restriction
1324               ("exit with when clause must be directly in loop", N);
1325          end if;
1326
1327       else
1328          if Nkind (Parent (N)) /= N_If_Statement then
1329             if Nkind (Parent (N)) = N_Elsif_Part then
1330                Check_SPARK_Restriction
1331                  ("exit must be in IF without ELSIF", N);
1332             else
1333                Check_SPARK_Restriction ("exit must be directly in IF", N);
1334             end if;
1335
1336          elsif Nkind (Parent (Parent (N))) /= N_Loop_Statement then
1337             Check_SPARK_Restriction
1338               ("exit must be in IF directly in loop", N);
1339
1340          --  First test the presence of ELSE, so that an exit in an ELSE leads
1341          --  to an error mentioning the ELSE.
1342
1343          elsif Present (Else_Statements (Parent (N))) then
1344             Check_SPARK_Restriction ("exit must be in IF without ELSE", N);
1345
1346          --  An exit in an ELSIF does not reach here, as it would have been
1347          --  detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1348
1349          elsif Present (Elsif_Parts (Parent (N))) then
1350             Check_SPARK_Restriction ("exit must be in IF without ELSIF", N);
1351          end if;
1352       end if;
1353
1354       --  Chain exit statement to associated loop entity
1355
1356       Set_Next_Exit_Statement  (N, First_Exit_Statement (Scope_Id));
1357       Set_First_Exit_Statement (Scope_Id, N);
1358
1359       --  Since the exit may take us out of a loop, any previous assignment
1360       --  statement is not useless, so clear last assignment indications. It
1361       --  is OK to keep other current values, since if the exit statement
1362       --  does not exit, then the current values are still valid.
1363
1364       Kill_Current_Values (Last_Assignment_Only => True);
1365    end Analyze_Exit_Statement;
1366
1367    ----------------------------
1368    -- Analyze_Goto_Statement --
1369    ----------------------------
1370
1371    procedure Analyze_Goto_Statement (N : Node_Id) is
1372       Label       : constant Node_Id := Name (N);
1373       Scope_Id    : Entity_Id;
1374       Label_Scope : Entity_Id;
1375       Label_Ent   : Entity_Id;
1376
1377    begin
1378       Check_SPARK_Restriction ("goto statement is not allowed", N);
1379
1380       --  Actual semantic checks
1381
1382       Check_Unreachable_Code (N);
1383       Kill_Current_Values (Last_Assignment_Only => True);
1384
1385       Analyze (Label);
1386       Label_Ent := Entity (Label);
1387
1388       --  Ignore previous error
1389
1390       if Label_Ent = Any_Id then
1391          return;
1392
1393       --  We just have a label as the target of a goto
1394
1395       elsif Ekind (Label_Ent) /= E_Label then
1396          Error_Msg_N ("target of goto statement must be a label", Label);
1397          return;
1398
1399       --  Check that the target of the goto is reachable according to Ada
1400       --  scoping rules. Note: the special gotos we generate for optimizing
1401       --  local handling of exceptions would violate these rules, but we mark
1402       --  such gotos as analyzed when built, so this code is never entered.
1403
1404       elsif not Reachable (Label_Ent) then
1405          Error_Msg_N ("target of goto statement is not reachable", Label);
1406          return;
1407       end if;
1408
1409       --  Here if goto passes initial validity checks
1410
1411       Label_Scope := Enclosing_Scope (Label_Ent);
1412
1413       for J in reverse 0 .. Scope_Stack.Last loop
1414          Scope_Id := Scope_Stack.Table (J).Entity;
1415
1416          if Label_Scope = Scope_Id
1417            or else (Ekind (Scope_Id) /= E_Block
1418                      and then Ekind (Scope_Id) /= E_Loop
1419                      and then Ekind (Scope_Id) /= E_Return_Statement)
1420          then
1421             if Scope_Id /= Label_Scope then
1422                Error_Msg_N
1423                  ("cannot exit from program unit or accept statement", N);
1424             end if;
1425
1426             return;
1427          end if;
1428       end loop;
1429
1430       raise Program_Error;
1431    end Analyze_Goto_Statement;
1432
1433    --------------------------
1434    -- Analyze_If_Statement --
1435    --------------------------
1436
1437    --  A special complication arises in the analysis of if statements
1438
1439    --  The expander has circuitry to completely delete code that it can tell
1440    --  will not be executed (as a result of compile time known conditions). In
1441    --  the analyzer, we ensure that code that will be deleted in this manner is
1442    --  analyzed but not expanded. This is obviously more efficient, but more
1443    --  significantly, difficulties arise if code is expanded and then
1444    --  eliminated (e.g. exception table entries disappear). Similarly, itypes
1445    --  generated in deleted code must be frozen from start, because the nodes
1446    --  on which they depend will not be available at the freeze point.
1447
1448    procedure Analyze_If_Statement (N : Node_Id) is
1449       E : Node_Id;
1450
1451       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1452       --  Recursively save value of this global, will be restored on exit
1453
1454       Save_In_Deleted_Code : Boolean;
1455
1456       Del : Boolean := False;
1457       --  This flag gets set True if a True condition has been found, which
1458       --  means that remaining ELSE/ELSIF parts are deleted.
1459
1460       procedure Analyze_Cond_Then (Cnode : Node_Id);
1461       --  This is applied to either the N_If_Statement node itself or to an
1462       --  N_Elsif_Part node. It deals with analyzing the condition and the THEN
1463       --  statements associated with it.
1464
1465       -----------------------
1466       -- Analyze_Cond_Then --
1467       -----------------------
1468
1469       procedure Analyze_Cond_Then (Cnode : Node_Id) is
1470          Cond : constant Node_Id := Condition (Cnode);
1471          Tstm : constant List_Id := Then_Statements (Cnode);
1472
1473       begin
1474          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1475          Analyze_And_Resolve (Cond, Any_Boolean);
1476          Check_Unset_Reference (Cond);
1477          Set_Current_Value_Condition (Cnode);
1478
1479          --  If already deleting, then just analyze then statements
1480
1481          if Del then
1482             Analyze_Statements (Tstm);
1483
1484          --  Compile time known value, not deleting yet
1485
1486          elsif Compile_Time_Known_Value (Cond) then
1487             Save_In_Deleted_Code := In_Deleted_Code;
1488
1489             --  If condition is True, then analyze the THEN statements and set
1490             --  no expansion for ELSE and ELSIF parts.
1491
1492             if Is_True (Expr_Value (Cond)) then
1493                Analyze_Statements (Tstm);
1494                Del := True;
1495                Expander_Mode_Save_And_Set (False);
1496                In_Deleted_Code := True;
1497
1498             --  If condition is False, analyze THEN with expansion off
1499
1500             else -- Is_False (Expr_Value (Cond))
1501                Expander_Mode_Save_And_Set (False);
1502                In_Deleted_Code := True;
1503                Analyze_Statements (Tstm);
1504                Expander_Mode_Restore;
1505                In_Deleted_Code := Save_In_Deleted_Code;
1506             end if;
1507
1508          --  Not known at compile time, not deleting, normal analysis
1509
1510          else
1511             Analyze_Statements (Tstm);
1512          end if;
1513       end Analyze_Cond_Then;
1514
1515    --  Start of Analyze_If_Statement
1516
1517    begin
1518       --  Initialize exit count for else statements. If there is no else part,
1519       --  this count will stay non-zero reflecting the fact that the uncovered
1520       --  else case is an unblocked exit.
1521
1522       Unblocked_Exit_Count := 1;
1523       Analyze_Cond_Then (N);
1524
1525       --  Now to analyze the elsif parts if any are present
1526
1527       if Present (Elsif_Parts (N)) then
1528          E := First (Elsif_Parts (N));
1529          while Present (E) loop
1530             Analyze_Cond_Then (E);
1531             Next (E);
1532          end loop;
1533       end if;
1534
1535       if Present (Else_Statements (N)) then
1536          Analyze_Statements (Else_Statements (N));
1537       end if;
1538
1539       --  If all our exits were blocked by unconditional transfers of control,
1540       --  then the entire IF statement acts as an unconditional transfer of
1541       --  control, so treat it like one, and check unreachable code.
1542
1543       if Unblocked_Exit_Count = 0 then
1544          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1545          Check_Unreachable_Code (N);
1546       else
1547          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1548       end if;
1549
1550       if Del then
1551          Expander_Mode_Restore;
1552          In_Deleted_Code := Save_In_Deleted_Code;
1553       end if;
1554
1555       if not Expander_Active
1556         and then Compile_Time_Known_Value (Condition (N))
1557         and then Serious_Errors_Detected = 0
1558       then
1559          if Is_True (Expr_Value (Condition (N))) then
1560             Remove_Warning_Messages (Else_Statements (N));
1561
1562             if Present (Elsif_Parts (N)) then
1563                E := First (Elsif_Parts (N));
1564                while Present (E) loop
1565                   Remove_Warning_Messages (Then_Statements (E));
1566                   Next (E);
1567                end loop;
1568             end if;
1569
1570          else
1571             Remove_Warning_Messages (Then_Statements (N));
1572          end if;
1573       end if;
1574    end Analyze_If_Statement;
1575
1576    ----------------------------------------
1577    -- Analyze_Implicit_Label_Declaration --
1578    ----------------------------------------
1579
1580    --  An implicit label declaration is generated in the innermost enclosing
1581    --  declarative part. This is done for labels, and block and loop names.
1582
1583    --  Note: any changes in this routine may need to be reflected in
1584    --  Analyze_Label_Entity.
1585
1586    procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1587       Id : constant Node_Id := Defining_Identifier (N);
1588    begin
1589       Enter_Name          (Id);
1590       Set_Ekind           (Id, E_Label);
1591       Set_Etype           (Id, Standard_Void_Type);
1592       Set_Enclosing_Scope (Id, Current_Scope);
1593    end Analyze_Implicit_Label_Declaration;
1594
1595    ------------------------------
1596    -- Analyze_Iteration_Scheme --
1597    ------------------------------
1598
1599    procedure Analyze_Iteration_Scheme (N : Node_Id) is
1600
1601       procedure Process_Bounds (R : Node_Id);
1602       --  If the iteration is given by a range, create temporaries and
1603       --  assignment statements block to capture the bounds and perform
1604       --  required finalization actions in case a bound includes a function
1605       --  call that uses the temporary stack. We first pre-analyze a copy of
1606       --  the range in order to determine the expected type, and analyze and
1607       --  resolve the original bounds.
1608
1609       procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1610       --  If the bounds are given by a 'Range reference on a function call
1611       --  that returns a controlled array, introduce an explicit declaration
1612       --  to capture the bounds, so that the function result can be finalized
1613       --  in timely fashion.
1614
1615       function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean;
1616       --  N is the node for an arbitrary construct. This function searches the
1617       --  construct N to see if any expressions within it contain function
1618       --  calls that use the secondary stack, returning True if any such call
1619       --  is found, and False otherwise.
1620
1621       procedure Pre_Analyze_Range (R_Copy : Node_Id);
1622       --  Determine expected type of range or domain of iteration of Ada 2012
1623       --  loop by analyzing separate copy. Do the analysis and resolution of
1624       --  the copy of the bound(s) with expansion disabled, to prevent the
1625       --  generation of finalization actions. This prevents memory leaks when
1626       --  the bounds contain calls to functions returning controlled arrays or
1627       --  when the domain of iteration is a container.
1628
1629       -----------------------
1630       -- Pre_Analyze_Range --
1631       -----------------------
1632
1633       procedure Pre_Analyze_Range (R_Copy : Node_Id) is
1634          Save_Analysis : Boolean;
1635       begin
1636          Save_Analysis := Full_Analysis;
1637          Full_Analysis := False;
1638          Expander_Mode_Save_And_Set (False);
1639
1640          Analyze (R_Copy);
1641
1642          if Nkind (R_Copy) in N_Subexpr
1643            and then Is_Overloaded (R_Copy)
1644          then
1645
1646             --  Apply preference rules for range of predefined integer types,
1647             --  or diagnose true ambiguity.
1648
1649             declare
1650                I     : Interp_Index;
1651                It    : Interp;
1652                Found : Entity_Id := Empty;
1653
1654             begin
1655                Get_First_Interp (R_Copy, I, It);
1656                while Present (It.Typ) loop
1657                   if Is_Discrete_Type (It.Typ) then
1658                      if No (Found) then
1659                         Found := It.Typ;
1660                      else
1661                         if Scope (Found) = Standard_Standard then
1662                            null;
1663
1664                         elsif Scope (It.Typ) = Standard_Standard then
1665                            Found := It.Typ;
1666
1667                         else
1668                            --  Both of them are user-defined
1669
1670                            Error_Msg_N
1671                              ("ambiguous bounds in range of iteration",
1672                                R_Copy);
1673                            Error_Msg_N ("\possible interpretations:", R_Copy);
1674                            Error_Msg_NE ("\\} ", R_Copy, Found);
1675                            Error_Msg_NE ("\\} ", R_Copy, It.Typ);
1676                            exit;
1677                         end if;
1678                      end if;
1679                   end if;
1680
1681                   Get_Next_Interp (I, It);
1682                end loop;
1683             end;
1684          end if;
1685
1686          if  Is_Entity_Name (R_Copy)
1687            and then Is_Type (Entity (R_Copy))
1688          then
1689
1690             --  Subtype mark in iteration scheme
1691
1692             null;
1693
1694          elsif Nkind (R_Copy) in N_Subexpr then
1695
1696             --  Expression in range, or Ada 2012 iterator
1697
1698             Resolve (R_Copy);
1699          end if;
1700
1701          Expander_Mode_Restore;
1702          Full_Analysis := Save_Analysis;
1703       end Pre_Analyze_Range;
1704
1705       --------------------
1706       -- Process_Bounds --
1707       --------------------
1708
1709       procedure Process_Bounds (R : Node_Id) is
1710          Loc          : constant Source_Ptr := Sloc (N);
1711          R_Copy       : constant Node_Id := New_Copy_Tree (R);
1712          Lo           : constant Node_Id := Low_Bound  (R);
1713          Hi           : constant Node_Id := High_Bound (R);
1714          New_Lo_Bound : Node_Id;
1715          New_Hi_Bound : Node_Id;
1716          Typ          : Entity_Id;
1717
1718          function One_Bound
1719            (Original_Bound : Node_Id;
1720             Analyzed_Bound : Node_Id) return Node_Id;
1721          --  Capture value of bound and return captured value
1722
1723          ---------------
1724          -- One_Bound --
1725          ---------------
1726
1727          function One_Bound
1728            (Original_Bound : Node_Id;
1729             Analyzed_Bound : Node_Id) return Node_Id
1730          is
1731             Assign : Node_Id;
1732             Id     : Entity_Id;
1733             Decl   : Node_Id;
1734
1735          begin
1736             --  If the bound is a constant or an object, no need for a separate
1737             --  declaration. If the bound is the result of previous expansion
1738             --  it is already analyzed and should not be modified. Note that
1739             --  the Bound will be resolved later, if needed, as part of the
1740             --  call to Make_Index (literal bounds may need to be resolved to
1741             --  type Integer).
1742
1743             if Analyzed (Original_Bound) then
1744                return Original_Bound;
1745
1746             elsif Nkind_In (Analyzed_Bound, N_Integer_Literal,
1747                                             N_Character_Literal)
1748               or else Is_Entity_Name (Analyzed_Bound)
1749             then
1750                Analyze_And_Resolve (Original_Bound, Typ);
1751                return Original_Bound;
1752             end if;
1753
1754             --  Here we need to capture the value
1755
1756             Analyze_And_Resolve (Original_Bound, Typ);
1757
1758             --  Normally, the best approach is simply to generate a constant
1759             --  declaration that captures the bound. However, there is a nasty
1760             --  case where this is wrong. If the bound is complex, and has a
1761             --  possible use of the secondary stack, we need to generate a
1762             --  separate assignment statement to ensure the creation of a block
1763             --  which will release the secondary stack.
1764
1765             --  We prefer the constant declaration, since it leaves us with a
1766             --  proper trace of the value, useful in optimizations that get rid
1767             --  of junk range checks.
1768
1769             if not Has_Call_Using_Secondary_Stack (Original_Bound) then
1770                Force_Evaluation (Original_Bound);
1771                return Original_Bound;
1772             end if;
1773
1774             Id := Make_Temporary (Loc, 'R', Original_Bound);
1775
1776             --  Here we make a declaration with a separate assignment
1777             --  statement, and insert before loop header.
1778
1779             Decl :=
1780               Make_Object_Declaration (Loc,
1781                 Defining_Identifier => Id,
1782                 Object_Definition   => New_Occurrence_Of (Typ, Loc));
1783
1784             Assign :=
1785               Make_Assignment_Statement (Loc,
1786                 Name        => New_Occurrence_Of (Id, Loc),
1787                 Expression  => Relocate_Node (Original_Bound));
1788
1789             --  We must recursively clean in the relocated expression the flag
1790             --  analyzed to ensure that the expression is reanalyzed. Required
1791             --  to ensure that the transient scope is established now (because
1792             --  Establish_Transient_Scope discarded generating transient scopes
1793             --  in the analysis of the iteration scheme).
1794
1795             Reset_Analyzed_Flags (Expression (Assign));
1796
1797             Insert_Actions (Parent (N), New_List (Decl, Assign));
1798
1799             --  Now that this temporary variable is initialized we decorate it
1800             --  as safe-to-reevaluate to inform to the backend that no further
1801             --  asignment will be issued and hence it can be handled as side
1802             --  effect free. Note that this decoration must be done when the
1803             --  assignment has been analyzed because otherwise it will be
1804             --  rejected (see Analyze_Assignment).
1805
1806             Set_Is_Safe_To_Reevaluate (Id);
1807
1808             Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
1809
1810             if Nkind (Assign) = N_Assignment_Statement then
1811                return Expression (Assign);
1812             else
1813                return Original_Bound;
1814             end if;
1815          end One_Bound;
1816
1817       --  Start of processing for Process_Bounds
1818
1819       begin
1820          Set_Parent (R_Copy, Parent (R));
1821          Pre_Analyze_Range (R_Copy);
1822          Typ := Etype (R_Copy);
1823
1824          --  If the type of the discrete range is Universal_Integer, then the
1825          --  bound's type must be resolved to Integer, and any object used to
1826          --  hold the bound must also have type Integer, unless the literal
1827          --  bounds are constant-folded expressions with a user-defined type.
1828
1829          if Typ = Universal_Integer then
1830             if Nkind (Lo) = N_Integer_Literal
1831               and then Present (Etype (Lo))
1832               and then Scope (Etype (Lo)) /= Standard_Standard
1833             then
1834                Typ := Etype (Lo);
1835
1836             elsif Nkind (Hi) = N_Integer_Literal
1837               and then Present (Etype (Hi))
1838               and then Scope (Etype (Hi)) /= Standard_Standard
1839             then
1840                Typ := Etype (Hi);
1841
1842             else
1843                Typ := Standard_Integer;
1844             end if;
1845          end if;
1846
1847          Set_Etype (R, Typ);
1848
1849          New_Lo_Bound := One_Bound (Lo, Low_Bound  (R_Copy));
1850          New_Hi_Bound := One_Bound (Hi, High_Bound (R_Copy));
1851
1852          --  Propagate staticness to loop range itself, in case the
1853          --  corresponding subtype is static.
1854
1855          if New_Lo_Bound /= Lo
1856            and then Is_Static_Expression (New_Lo_Bound)
1857          then
1858             Rewrite  (Low_Bound (R), New_Copy (New_Lo_Bound));
1859          end if;
1860
1861          if New_Hi_Bound /= Hi
1862            and then Is_Static_Expression (New_Hi_Bound)
1863          then
1864             Rewrite (High_Bound (R), New_Copy (New_Hi_Bound));
1865          end if;
1866       end Process_Bounds;
1867
1868       --------------------------------------
1869       -- Check_Controlled_Array_Attribute --
1870       --------------------------------------
1871
1872       procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1873       begin
1874          if Nkind (DS) = N_Attribute_Reference
1875             and then Is_Entity_Name (Prefix (DS))
1876             and then Ekind (Entity (Prefix (DS))) = E_Function
1877             and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1878             and then
1879               Is_Controlled (
1880                 Component_Type (Etype (Entity (Prefix (DS)))))
1881             and then Expander_Active
1882          then
1883             declare
1884                Loc  : constant Source_Ptr := Sloc (N);
1885                Arr  : constant Entity_Id := Etype (Entity (Prefix (DS)));
1886                Indx : constant Entity_Id :=
1887                         Base_Type (Etype (First_Index (Arr)));
1888                Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
1889                Decl : Node_Id;
1890
1891             begin
1892                Decl :=
1893                  Make_Subtype_Declaration (Loc,
1894                    Defining_Identifier => Subt,
1895                    Subtype_Indication  =>
1896                       Make_Subtype_Indication (Loc,
1897                         Subtype_Mark  => New_Reference_To (Indx, Loc),
1898                         Constraint =>
1899                           Make_Range_Constraint (Loc,
1900                             Relocate_Node (DS))));
1901                Insert_Before (Parent (N), Decl);
1902                Analyze (Decl);
1903
1904                Rewrite (DS,
1905                   Make_Attribute_Reference (Loc,
1906                     Prefix => New_Reference_To (Subt, Loc),
1907                     Attribute_Name => Attribute_Name (DS)));
1908                Analyze (DS);
1909             end;
1910          end if;
1911       end Check_Controlled_Array_Attribute;
1912
1913       ------------------------------------
1914       -- Has_Call_Using_Secondary_Stack --
1915       ------------------------------------
1916
1917       function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean is
1918
1919          function Check_Call (N : Node_Id) return Traverse_Result;
1920          --  Check if N is a function call which uses the secondary stack
1921
1922          ----------------
1923          -- Check_Call --
1924          ----------------
1925
1926          function Check_Call (N : Node_Id) return Traverse_Result is
1927             Nam        : Node_Id;
1928             Subp       : Entity_Id;
1929             Return_Typ : Entity_Id;
1930
1931          begin
1932             if Nkind (N) = N_Function_Call then
1933                Nam := Name (N);
1934
1935                --  Call using access to subprogram with explicit dereference
1936
1937                if Nkind (Nam) = N_Explicit_Dereference then
1938                   Subp := Etype (Nam);
1939
1940                --  Normal case
1941
1942                else
1943                   Subp := Entity (Nam);
1944                end if;
1945
1946                Return_Typ := Etype (Subp);
1947
1948                if Is_Composite_Type (Return_Typ)
1949                  and then not Is_Constrained (Return_Typ)
1950                then
1951                   return Abandon;
1952
1953                elsif Sec_Stack_Needed_For_Return (Subp) then
1954                   return Abandon;
1955                end if;
1956             end if;
1957
1958             --  Continue traversing the tree
1959
1960             return OK;
1961          end Check_Call;
1962
1963          function Check_Calls is new Traverse_Func (Check_Call);
1964
1965       --  Start of processing for Has_Call_Using_Secondary_Stack
1966
1967       begin
1968          return Check_Calls (N) = Abandon;
1969       end Has_Call_Using_Secondary_Stack;
1970
1971    --  Start of processing for Analyze_Iteration_Scheme
1972
1973    begin
1974       --  If this is a rewritten quantified expression, the iteration scheme
1975       --  has been analyzed already. Do no repeat analysis because the loop
1976       --  variable is already declared.
1977
1978       if Analyzed (N) then
1979          return;
1980       end if;
1981
1982       --  For an infinite loop, there is no iteration scheme
1983
1984       if No (N) then
1985          return;
1986       end if;
1987
1988       --  Iteration scheme is present
1989
1990       declare
1991          Cond : constant Node_Id := Condition (N);
1992
1993       begin
1994          --  For WHILE loop, verify that the condition is a Boolean expression
1995          --  and resolve and check it.
1996
1997          if Present (Cond) then
1998             Analyze_And_Resolve (Cond, Any_Boolean);
1999             Check_Unset_Reference (Cond);
2000             Set_Current_Value_Condition (N);
2001             return;
2002
2003          --  For an iterator specification with "of", pre-analyze range to
2004          --  capture function calls that may require finalization actions.
2005
2006          elsif Present (Iterator_Specification (N)) then
2007             Pre_Analyze_Range (Name (Iterator_Specification (N)));
2008             Analyze_Iterator_Specification (Iterator_Specification (N));
2009
2010          --  Else we have a FOR loop
2011
2012          else
2013             declare
2014                LP : constant Node_Id   := Loop_Parameter_Specification (N);
2015                Id : constant Entity_Id := Defining_Identifier (LP);
2016                DS : constant Node_Id   := Discrete_Subtype_Definition (LP);
2017
2018                D_Copy : Node_Id;
2019
2020             begin
2021                Enter_Name (Id);
2022
2023                --  We always consider the loop variable to be referenced, since
2024                --  the loop may be used just for counting purposes.
2025
2026                Generate_Reference (Id, N, ' ');
2027
2028                --  Check for the case of loop variable hiding a local variable
2029                --  (used later on to give a nice warning if the hidden variable
2030                --  is never assigned).
2031
2032                declare
2033                   H : constant Entity_Id := Homonym (Id);
2034                begin
2035                   if Present (H)
2036                     and then Enclosing_Dynamic_Scope (H) =
2037                     Enclosing_Dynamic_Scope (Id)
2038                     and then Ekind (H) = E_Variable
2039                     and then Is_Discrete_Type (Etype (H))
2040                   then
2041                      Set_Hiding_Loop_Variable (H, Id);
2042                   end if;
2043                end;
2044
2045                --  Loop parameter specification must include subtype mark in
2046                --  SPARK.
2047
2048                if Nkind (DS) = N_Range then
2049                   Check_SPARK_Restriction
2050                     ("loop parameter specification must include subtype mark",
2051                      N);
2052                end if;
2053
2054                --  Now analyze the subtype definition. If it is a range, create
2055                --  temporaries for bounds.
2056
2057                if Nkind (DS) = N_Range
2058                  and then Expander_Active
2059                then
2060                   Process_Bounds (DS);
2061
2062                --  expander not active or else range of iteration is a subtype
2063                --  indication, an entity, or a function call that yields an
2064                --  aggregate or a container.
2065
2066                else
2067                   D_Copy := New_Copy_Tree (DS);
2068                   Set_Parent (D_Copy, Parent (DS));
2069                   Pre_Analyze_Range (D_Copy);
2070
2071                   --  Ada2012: If the domain of iteration is a function call,
2072                   --  it is the new iterator form.
2073
2074                   --  We have also implemented the shorter form : for X in S
2075                   --  for Alfa use. In this case, 'Old and 'Result must be
2076                   --  treated as entity names over which iterators are legal.
2077
2078                   if Nkind (D_Copy) = N_Function_Call
2079                     or else
2080                       (Alfa_Mode
2081                         and then (Nkind (D_Copy) = N_Attribute_Reference
2082                         and then
2083                           (Attribute_Name (D_Copy) = Name_Result
2084                             or else Attribute_Name (D_Copy) = Name_Old)))
2085                     or else
2086                       (Is_Entity_Name (D_Copy)
2087                         and then not Is_Type (Entity (D_Copy)))
2088                   then
2089                      --  This is an iterator specification. Rewrite as such
2090                      --  and analyze, to capture function calls that may
2091                      --  require finalization actions.
2092
2093                      declare
2094                         I_Spec : constant Node_Id :=
2095                                    Make_Iterator_Specification (Sloc (LP),
2096                                      Defining_Identifier =>
2097                                        Relocate_Node (Id),
2098                                      Name                => D_Copy,
2099                                      Subtype_Indication  => Empty,
2100                                      Reverse_Present     =>
2101                                        Reverse_Present (LP));
2102                      begin
2103                         Set_Iterator_Specification (N, I_Spec);
2104                         Set_Loop_Parameter_Specification (N, Empty);
2105                         Analyze_Iterator_Specification (I_Spec);
2106
2107                         --  In a generic context, analyze the original domain
2108                         --  of iteration, for name capture.
2109
2110                         if not Expander_Active then
2111                            Analyze (DS);
2112                         end if;
2113
2114                         --  Set kind of loop parameter, which may be used in
2115                         --  the subsequent analysis of the condition in a
2116                         --  quantified expression.
2117
2118                         Set_Ekind (Id, E_Loop_Parameter);
2119                         return;
2120                      end;
2121
2122                   --  Domain of iteration is not a function call, and is
2123                   --  side-effect free.
2124
2125                   else
2126                      Analyze (DS);
2127                   end if;
2128                end if;
2129
2130                if DS = Error then
2131                   return;
2132                end if;
2133
2134                --  Some additional checks if we are iterating through a type
2135
2136                if Is_Entity_Name (DS)
2137                  and then Present (Entity (DS))
2138                  and then Is_Type (Entity (DS))
2139                then
2140                   --  The subtype indication may denote the completion of an
2141                   --  incomplete type declaration.
2142
2143                   if Ekind (Entity (DS)) = E_Incomplete_Type then
2144                      Set_Entity (DS, Get_Full_View (Entity (DS)));
2145                      Set_Etype  (DS, Entity (DS));
2146                   end if;
2147
2148                   --  Attempt to iterate through non-static predicate
2149
2150                   if Is_Discrete_Type (Entity (DS))
2151                     and then Present (Predicate_Function (Entity (DS)))
2152                     and then No (Static_Predicate (Entity (DS)))
2153                   then
2154                      Bad_Predicated_Subtype_Use
2155                        ("cannot use subtype& with non-static "
2156                         & "predicate for loop iteration", DS, Entity (DS));
2157                   end if;
2158                end if;
2159
2160                --  Error if not discrete type
2161
2162                if not Is_Discrete_Type (Etype (DS)) then
2163                   Wrong_Type (DS, Any_Discrete);
2164                   Set_Etype (DS, Any_Type);
2165                end if;
2166
2167                Check_Controlled_Array_Attribute (DS);
2168
2169                Make_Index (DS, LP, In_Iter_Schm => True);
2170
2171                Set_Ekind (Id, E_Loop_Parameter);
2172
2173                --  If the loop is part of a predicate or precondition, it may
2174                --  be analyzed twice, once in the source and once on the copy
2175                --  used to check conformance. Preserve the original itype
2176                --  because the second one may be created in a different scope,
2177                --  e.g. a precondition procedure, leading to a crash in GIGI.
2178
2179                if No (Etype (Id)) or else Etype (Id) = Any_Type then
2180                   Set_Etype (Id, Etype (DS));
2181                end if;
2182
2183                --  Treat a range as an implicit reference to the type, to
2184                --  inhibit spurious warnings.
2185
2186                Generate_Reference (Base_Type (Etype (DS)), N, ' ');
2187                Set_Is_Known_Valid (Id, True);
2188
2189                --  The loop is not a declarative part, so the only entity
2190                --  declared "within" must be frozen explicitly.
2191
2192                declare
2193                   Flist : constant List_Id := Freeze_Entity (Id, N);
2194                begin
2195                   if Is_Non_Empty_List (Flist) then
2196                      Insert_Actions (N, Flist);
2197                   end if;
2198                end;
2199
2200                --  Check for null or possibly null range and issue warning. We
2201                --  suppress such messages in generic templates and instances,
2202                --  because in practice they tend to be dubious in these cases.
2203
2204                if Nkind (DS) = N_Range and then Comes_From_Source (N) then
2205                   declare
2206                      L : constant Node_Id := Low_Bound  (DS);
2207                      H : constant Node_Id := High_Bound (DS);
2208
2209                   begin
2210                      --  If range of loop is null, issue warning
2211
2212                      if Compile_Time_Compare
2213                           (L, H, Assume_Valid => True) = GT
2214                      then
2215                         --  Suppress the warning if inside a generic template
2216                         --  or instance, since in practice they tend to be
2217                         --  dubious in these cases since they can result from
2218                         --  intended parametrization.
2219
2220                         if not Inside_A_Generic
2221                           and then not In_Instance
2222                         then
2223                            --  Specialize msg if invalid values could make the
2224                            --  loop non-null after all.
2225
2226                            if Compile_Time_Compare
2227                                 (L, H, Assume_Valid => False) = GT
2228                            then
2229                               Error_Msg_N
2230                                 ("?loop range is null, loop will not execute",
2231                                  DS);
2232
2233                               --  Since we know the range of the loop is null,
2234                               --  set the appropriate flag to remove the loop
2235                               --  entirely during expansion.
2236
2237                               Set_Is_Null_Loop (Parent (N));
2238
2239                               --  Here is where the loop could execute because
2240                               --  of invalid values, so issue appropriate
2241                               --  message and in this case we do not set the
2242                               --  Is_Null_Loop flag since the loop may execute.
2243
2244                            else
2245                               Error_Msg_N
2246                                 ("?loop range may be null, "
2247                                  & "loop may not execute",
2248                                  DS);
2249                               Error_Msg_N
2250                                 ("?can only execute if invalid values "
2251                                  & "are present",
2252                                  DS);
2253                            end if;
2254                         end if;
2255
2256                         --  In either case, suppress warnings in the body of
2257                         --  the loop, since it is likely that these warnings
2258                         --  will be inappropriate if the loop never actually
2259                         --  executes, which is likely.
2260
2261                         Set_Suppress_Loop_Warnings (Parent (N));
2262
2263                         --  The other case for a warning is a reverse loop
2264                         --  where the upper bound is the integer literal zero
2265                         --  or one, and the lower bound can be positive.
2266
2267                         --  For example, we have
2268
2269                         --     for J in reverse N .. 1 loop
2270
2271                         --  In practice, this is very likely to be a case of
2272                         --  reversing the bounds incorrectly in the range.
2273
2274                      elsif Reverse_Present (LP)
2275                        and then Nkind (Original_Node (H)) =
2276                                                       N_Integer_Literal
2277                        and then (Intval (Original_Node (H)) = Uint_0
2278                                   or else
2279                                     Intval (Original_Node (H)) = Uint_1)
2280                      then
2281                         Error_Msg_N ("?loop range may be null", DS);
2282                         Error_Msg_N ("\?bounds may be wrong way round", DS);
2283                      end if;
2284                   end;
2285                end if;
2286             end;
2287          end if;
2288       end;
2289    end Analyze_Iteration_Scheme;
2290
2291    -------------------------------------
2292    --  Analyze_Iterator_Specification --
2293    -------------------------------------
2294
2295    procedure Analyze_Iterator_Specification (N : Node_Id) is
2296       Loc       : constant Source_Ptr := Sloc (N);
2297       Def_Id    : constant Node_Id    := Defining_Identifier (N);
2298       Subt      : constant Node_Id    := Subtype_Indication (N);
2299       Iter_Name : constant Node_Id    := Name (N);
2300
2301       Ent : Entity_Id;
2302       Typ : Entity_Id;
2303
2304    begin
2305       --  In semantics/Alfa modes, we won't be further expanding the loop, so
2306       --  introduce loop variable so that loop body can be properly analyzed.
2307       --  Otherwise this happens after expansion.
2308
2309       if Operating_Mode = Check_Semantics
2310         or else Alfa_Mode
2311       then
2312          Enter_Name (Def_Id);
2313       end if;
2314
2315       Set_Ekind (Def_Id, E_Variable);
2316
2317       if Present (Subt) then
2318          Analyze (Subt);
2319       end if;
2320
2321       --  If domain of iteration is an expression, create a declaration for
2322       --  it, so that finalization actions are introduced outside of the loop.
2323       --  The declaration must be a renaming because the body of the loop may
2324       --  assign to elements.
2325
2326       if not Is_Entity_Name (Iter_Name) then
2327          declare
2328             Id   : constant Entity_Id := Make_Temporary (Loc, 'R', Iter_Name);
2329             Decl : Node_Id;
2330
2331          begin
2332             Typ := Etype (Iter_Name);
2333
2334             Decl :=
2335               Make_Object_Renaming_Declaration (Loc,
2336                 Defining_Identifier => Id,
2337                 Subtype_Mark        => New_Occurrence_Of (Typ, Loc),
2338                 Name                => Relocate_Node (Iter_Name));
2339
2340             Insert_Actions (Parent (Parent (N)), New_List (Decl));
2341             Rewrite (Name (N), New_Occurrence_Of (Id, Loc));
2342             Set_Etype (Id, Typ);
2343             Set_Etype (Name (N), Typ);
2344          end;
2345
2346       --  Container is an entity or an array with uncontrolled components, or
2347       --  else it is a container iterator given by a function call, typically
2348       --  called Iterate in the case of predefined containers, even though
2349       --  Iterate is not a reserved name. What matter is that the return type
2350       --  of the function is an iterator type.
2351
2352       else
2353          Analyze (Iter_Name);
2354
2355          if Nkind (Iter_Name) = N_Function_Call then
2356             declare
2357                C  : constant Node_Id := Name (Iter_Name);
2358                I  : Interp_Index;
2359                It : Interp;
2360
2361             begin
2362                if not Is_Overloaded (Iter_Name) then
2363                   Resolve (Iter_Name, Etype (C));
2364
2365                else
2366                   Get_First_Interp (C, I, It);
2367                   while It.Typ /= Empty loop
2368                      if Reverse_Present (N) then
2369                         if Is_Reversible_Iterator (It.Typ) then
2370                            Resolve (Iter_Name, It.Typ);
2371                            exit;
2372                         end if;
2373
2374                      elsif Is_Iterator (It.Typ) then
2375                         Resolve (Iter_Name, It.Typ);
2376                         exit;
2377                      end if;
2378
2379                      Get_Next_Interp (I, It);
2380                   end loop;
2381                end if;
2382             end;
2383
2384          --  Domain of iteration is not overloaded
2385
2386          else
2387             Resolve (Iter_Name, Etype (Iter_Name));
2388          end if;
2389       end if;
2390
2391       Typ := Etype (Iter_Name);
2392
2393       if Is_Array_Type (Typ) then
2394          if Of_Present (N) then
2395             Set_Etype (Def_Id, Component_Type (Typ));
2396
2397          --  Here we have a missing Range attribute
2398
2399          else
2400             Error_Msg_N
2401               ("missing Range attribute in iteration over an array", N);
2402
2403             --  In Ada 2012 mode, this may be an attempt at an iterator
2404
2405             if Ada_Version >= Ada_2012 then
2406                Error_Msg_NE
2407                  ("\if& is meant to designate an element of the array, use OF",
2408                     N, Def_Id);
2409             end if;
2410
2411             --  Prevent cascaded errors
2412
2413             Set_Ekind (Def_Id, E_Loop_Parameter);
2414             Set_Etype (Def_Id, Etype (First_Index (Typ)));
2415          end if;
2416
2417          --  Check for type error in iterator
2418
2419       elsif Typ = Any_Type then
2420          return;
2421
2422       --  Iteration over a container
2423
2424       else
2425          Set_Ekind (Def_Id, E_Loop_Parameter);
2426
2427          if Of_Present (N) then
2428
2429             --  The type of the loop variable is the Iterator_Element aspect of
2430             --  the container type.
2431
2432             Set_Etype (Def_Id,
2433               Entity (Find_Aspect (Typ, Aspect_Iterator_Element)));
2434
2435          else
2436             --  For an iteration of the form IN, the name must denote an
2437             --  iterator, typically the result of a call to Iterate. Give a
2438             --  useful error message when the name is a container by itself.
2439
2440             if Is_Entity_Name (Original_Node (Name (N)))
2441               and then not Is_Iterator (Typ)
2442             then
2443                Error_Msg_N
2444                  ("name must be an iterator, not a container", Name (N));
2445
2446                Error_Msg_NE
2447                  ("\to iterate directly over a container, write `of &`",
2448                     Name (N), Original_Node (Name (N)));
2449             end if;
2450
2451             --  The result type of Iterate function is the classwide type of
2452             --  the interface parent. We need the specific Cursor type defined
2453             --  in the container package.
2454
2455             Ent := First_Entity (Scope (Typ));
2456             while Present (Ent) loop
2457                if Chars (Ent) = Name_Cursor then
2458                   Set_Etype (Def_Id, Etype (Ent));
2459                   exit;
2460                end if;
2461
2462                Next_Entity (Ent);
2463             end loop;
2464          end if;
2465       end if;
2466    end Analyze_Iterator_Specification;
2467
2468    -------------------
2469    -- Analyze_Label --
2470    -------------------
2471
2472    --  Note: the semantic work required for analyzing labels (setting them as
2473    --  reachable) was done in a prepass through the statements in the block,
2474    --  so that forward gotos would be properly handled. See Analyze_Statements
2475    --  for further details. The only processing required here is to deal with
2476    --  optimizations that depend on an assumption of sequential control flow,
2477    --  since of course the occurrence of a label breaks this assumption.
2478
2479    procedure Analyze_Label (N : Node_Id) is
2480       pragma Warnings (Off, N);
2481    begin
2482       Kill_Current_Values;
2483    end Analyze_Label;
2484
2485    --------------------------
2486    -- Analyze_Label_Entity --
2487    --------------------------
2488
2489    procedure Analyze_Label_Entity (E : Entity_Id) is
2490    begin
2491       Set_Ekind           (E, E_Label);
2492       Set_Etype           (E, Standard_Void_Type);
2493       Set_Enclosing_Scope (E, Current_Scope);
2494       Set_Reachable       (E, True);
2495    end Analyze_Label_Entity;
2496
2497    ----------------------------
2498    -- Analyze_Loop_Statement --
2499    ----------------------------
2500
2501    procedure Analyze_Loop_Statement (N : Node_Id) is
2502       Loop_Statement : constant Node_Id := N;
2503
2504       Id   : constant Node_Id := Identifier (Loop_Statement);
2505       Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
2506       Ent  : Entity_Id;
2507
2508    begin
2509       if Present (Id) then
2510
2511          --  Make name visible, e.g. for use in exit statements. Loop labels
2512          --  are always considered to be referenced.
2513
2514          Analyze (Id);
2515          Ent := Entity (Id);
2516
2517          --  Guard against serious error (typically, a scope mismatch when
2518          --  semantic analysis is requested) by creating loop entity to
2519          --  continue analysis.
2520
2521          if No (Ent) then
2522             if Total_Errors_Detected /= 0 then
2523                Ent :=
2524                  New_Internal_Entity
2525                    (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
2526             else
2527                raise Program_Error;
2528             end if;
2529
2530          else
2531             Generate_Reference  (Ent, Loop_Statement, ' ');
2532             Generate_Definition (Ent);
2533
2534             --  If we found a label, mark its type. If not, ignore it, since it
2535             --  means we have a conflicting declaration, which would already
2536             --  have been diagnosed at declaration time. Set Label_Construct
2537             --  of the implicit label declaration, which is not created by the
2538             --  parser for generic units.
2539
2540             if Ekind (Ent) = E_Label then
2541                Set_Ekind (Ent, E_Loop);
2542
2543                if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
2544                   Set_Label_Construct (Parent (Ent), Loop_Statement);
2545                end if;
2546             end if;
2547          end if;
2548
2549       --  Case of no identifier present
2550
2551       else
2552          Ent :=
2553            New_Internal_Entity
2554              (E_Loop, Current_Scope, Sloc (Loop_Statement), 'L');
2555          Set_Etype (Ent,  Standard_Void_Type);
2556          Set_Parent (Ent, Loop_Statement);
2557       end if;
2558
2559       --  Kill current values on entry to loop, since statements in the body of
2560       --  the loop may have been executed before the loop is entered. Similarly
2561       --  we kill values after the loop, since we do not know that the body of
2562       --  the loop was executed.
2563
2564       Kill_Current_Values;
2565       Push_Scope (Ent);
2566       Analyze_Iteration_Scheme (Iter);
2567
2568       --  Analyze the statements of the body except in the case of an Ada 2012
2569       --  iterator with the expander active. In this case the expander will do
2570       --  a rewrite of the loop into a while loop. We will then analyze the
2571       --  loop body when we analyze this while loop.
2572
2573       --  We need to do this delay because if the container is for indefinite
2574       --  types the actual subtype of the components will only be determined
2575       --  when the cursor declaration is analyzed.
2576
2577       --  If the expander is not active, then we want to analyze the loop body
2578       --  now even in the Ada 2012 iterator case, since the rewriting will not
2579       --  be done. Insert the loop variable in the current scope, if not done
2580       --  when analysing the iteration scheme.
2581
2582       if No (Iter)
2583         or else No (Iterator_Specification (Iter))
2584         or else not Expander_Active
2585       then
2586          if Present (Iter)
2587            and then Present (Iterator_Specification (Iter))
2588          then
2589             declare
2590                Id : constant Entity_Id :=
2591                       Defining_Identifier (Iterator_Specification (Iter));
2592             begin
2593                if Scope (Id) /= Current_Scope then
2594                   Enter_Name (Id);
2595                end if;
2596             end;
2597          end if;
2598
2599          Analyze_Statements (Statements (Loop_Statement));
2600       end if;
2601
2602       --  Finish up processing for the loop. We kill all current values, since
2603       --  in general we don't know if the statements in the loop have been
2604       --  executed. We could do a bit better than this with a loop that we
2605       --  know will execute at least once, but it's not worth the trouble and
2606       --  the front end is not in the business of flow tracing.
2607
2608       Process_End_Label (Loop_Statement, 'e', Ent);
2609       End_Scope;
2610       Kill_Current_Values;
2611
2612       --  Check for infinite loop. Skip check for generated code, since it
2613       --  justs waste time and makes debugging the routine called harder.
2614
2615       --  Note that we have to wait till the body of the loop is fully analyzed
2616       --  before making this call, since Check_Infinite_Loop_Warning relies on
2617       --  being able to use semantic visibility information to find references.
2618
2619       if Comes_From_Source (N) then
2620          Check_Infinite_Loop_Warning (N);
2621       end if;
2622
2623       --  Code after loop is unreachable if the loop has no WHILE or FOR and
2624       --  contains no EXIT statements within the body of the loop.
2625
2626       if No (Iter) and then not Has_Exit (Ent) then
2627          Check_Unreachable_Code (N);
2628       end if;
2629    end Analyze_Loop_Statement;
2630
2631    ----------------------------
2632    -- Analyze_Null_Statement --
2633    ----------------------------
2634
2635    --  Note: the semantics of the null statement is implemented by a single
2636    --  null statement, too bad everything isn't as simple as this!
2637
2638    procedure Analyze_Null_Statement (N : Node_Id) is
2639       pragma Warnings (Off, N);
2640    begin
2641       null;
2642    end Analyze_Null_Statement;
2643
2644    ------------------------
2645    -- Analyze_Statements --
2646    ------------------------
2647
2648    procedure Analyze_Statements (L : List_Id) is
2649       S   : Node_Id;
2650       Lab : Entity_Id;
2651
2652    begin
2653       --  The labels declared in the statement list are reachable from
2654       --  statements in the list. We do this as a prepass so that any goto
2655       --  statement will be properly flagged if its target is not reachable.
2656       --  This is not required, but is nice behavior!
2657
2658       S := First (L);
2659       while Present (S) loop
2660          if Nkind (S) = N_Label then
2661             Analyze (Identifier (S));
2662             Lab := Entity (Identifier (S));
2663
2664             --  If we found a label mark it as reachable
2665
2666             if Ekind (Lab) = E_Label then
2667                Generate_Definition (Lab);
2668                Set_Reachable (Lab);
2669
2670                if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
2671                   Set_Label_Construct (Parent (Lab), S);
2672                end if;
2673
2674             --  If we failed to find a label, it means the implicit declaration
2675             --  of the label was hidden.  A for-loop parameter can do this to
2676             --  a label with the same name inside the loop, since the implicit
2677             --  label declaration is in the innermost enclosing body or block
2678             --  statement.
2679
2680             else
2681                Error_Msg_Sloc := Sloc (Lab);
2682                Error_Msg_N
2683                  ("implicit label declaration for & is hidden#",
2684                   Identifier (S));
2685             end if;
2686          end if;
2687
2688          Next (S);
2689       end loop;
2690
2691       --  Perform semantic analysis on all statements
2692
2693       Conditional_Statements_Begin;
2694
2695       S := First (L);
2696       while Present (S) loop
2697          Analyze (S);
2698          Next (S);
2699       end loop;
2700
2701       Conditional_Statements_End;
2702
2703       --  Make labels unreachable. Visibility is not sufficient, because labels
2704       --  in one if-branch for example are not reachable from the other branch,
2705       --  even though their declarations are in the enclosing declarative part.
2706
2707       S := First (L);
2708       while Present (S) loop
2709          if Nkind (S) = N_Label then
2710             Set_Reachable (Entity (Identifier (S)), False);
2711          end if;
2712
2713          Next (S);
2714       end loop;
2715    end Analyze_Statements;
2716
2717    ----------------------------
2718    -- Check_Unreachable_Code --
2719    ----------------------------
2720
2721    procedure Check_Unreachable_Code (N : Node_Id) is
2722       Error_Node : Node_Id;
2723       P          : Node_Id;
2724
2725    begin
2726       if Is_List_Member (N)
2727         and then Comes_From_Source (N)
2728       then
2729          declare
2730             Nxt : Node_Id;
2731
2732          begin
2733             Nxt := Original_Node (Next (N));
2734
2735             --  If a label follows us, then we never have dead code, since
2736             --  someone could branch to the label, so we just ignore it, unless
2737             --  we are in formal mode where goto statements are not allowed.
2738
2739             if Nkind (Nxt) = N_Label
2740               and then not Restriction_Check_Required (SPARK)
2741             then
2742                return;
2743
2744             --  Otherwise see if we have a real statement following us
2745
2746             elsif Present (Nxt)
2747               and then Comes_From_Source (Nxt)
2748               and then Is_Statement (Nxt)
2749             then
2750                --  Special very annoying exception. If we have a return that
2751                --  follows a raise, then we allow it without a warning, since
2752                --  the Ada RM annoyingly requires a useless return here!
2753
2754                if Nkind (Original_Node (N)) /= N_Raise_Statement
2755                  or else Nkind (Nxt) /= N_Simple_Return_Statement
2756                then
2757                   --  The rather strange shenanigans with the warning message
2758                   --  here reflects the fact that Kill_Dead_Code is very good
2759                   --  at removing warnings in deleted code, and this is one
2760                   --  warning we would prefer NOT to have removed.
2761
2762                   Error_Node := Nxt;
2763
2764                   --  If we have unreachable code, analyze and remove the
2765                   --  unreachable code, since it is useless and we don't
2766                   --  want to generate junk warnings.
2767
2768                   --  We skip this step if we are not in code generation mode.
2769                   --  This is the one case where we remove dead code in the
2770                   --  semantics as opposed to the expander, and we do not want
2771                   --  to remove code if we are not in code generation mode,
2772                   --  since this messes up the ASIS trees.
2773
2774                   --  Note that one might react by moving the whole circuit to
2775                   --  exp_ch5, but then we lose the warning in -gnatc mode.
2776
2777                   if Operating_Mode = Generate_Code then
2778                      loop
2779                         Nxt := Next (N);
2780
2781                         --  Quit deleting when we have nothing more to delete
2782                         --  or if we hit a label (since someone could transfer
2783                         --  control to a label, so we should not delete it).
2784
2785                         exit when No (Nxt) or else Nkind (Nxt) = N_Label;
2786
2787                         --  Statement/declaration is to be deleted
2788
2789                         Analyze (Nxt);
2790                         Remove (Nxt);
2791                         Kill_Dead_Code (Nxt);
2792                      end loop;
2793                   end if;
2794
2795                   --  Now issue the warning (or error in formal mode)
2796
2797                   if Restriction_Check_Required (SPARK) then
2798                      Check_SPARK_Restriction
2799                        ("unreachable code is not allowed", Error_Node);
2800                   else
2801                      Error_Msg ("?unreachable code!", Sloc (Error_Node));
2802                   end if;
2803                end if;
2804
2805             --  If the unconditional transfer of control instruction is the
2806             --  last statement of a sequence, then see if our parent is one of
2807             --  the constructs for which we count unblocked exits, and if so,
2808             --  adjust the count.
2809
2810             else
2811                P := Parent (N);
2812
2813                --  Statements in THEN part or ELSE part of IF statement
2814
2815                if Nkind (P) = N_If_Statement then
2816                   null;
2817
2818                --  Statements in ELSIF part of an IF statement
2819
2820                elsif Nkind (P) = N_Elsif_Part then
2821                   P := Parent (P);
2822                   pragma Assert (Nkind (P) = N_If_Statement);
2823
2824                --  Statements in CASE statement alternative
2825
2826                elsif Nkind (P) = N_Case_Statement_Alternative then
2827                   P := Parent (P);
2828                   pragma Assert (Nkind (P) = N_Case_Statement);
2829
2830                --  Statements in body of block
2831
2832                elsif Nkind (P) = N_Handled_Sequence_Of_Statements
2833                  and then Nkind (Parent (P)) = N_Block_Statement
2834                then
2835                   null;
2836
2837                --  Statements in exception handler in a block
2838
2839                elsif Nkind (P) = N_Exception_Handler
2840                  and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
2841                  and then Nkind (Parent (Parent (P))) = N_Block_Statement
2842                then
2843                   null;
2844
2845                --  None of these cases, so return
2846
2847                else
2848                   return;
2849                end if;
2850
2851                --  This was one of the cases we are looking for (i.e. the
2852                --  parent construct was IF, CASE or block) so decrement count.
2853
2854                Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
2855             end if;
2856          end;
2857       end if;
2858    end Check_Unreachable_Code;
2859
2860 end Sem_Ch5;