OSDN Git Service

* builtins.c (std_expand_builtin_va_arg): Remove.
[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-2004 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Einfo;    use Einfo;
30 with Errout;   use Errout;
31 with Expander; use Expander;
32 with Exp_Util; use Exp_Util;
33 with Freeze;   use Freeze;
34 with Lib.Xref; use Lib.Xref;
35 with Nlists;   use Nlists;
36 with Nmake;    use Nmake;
37 with Opt;      use Opt;
38 with Sem;      use Sem;
39 with Sem_Case; use Sem_Case;
40 with Sem_Ch3;  use Sem_Ch3;
41 with Sem_Ch8;  use Sem_Ch8;
42 with Sem_Disp; use Sem_Disp;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res;  use Sem_Res;
45 with Sem_Type; use Sem_Type;
46 with Sem_Util; use Sem_Util;
47 with Sem_Warn; use Sem_Warn;
48 with Stand;    use Stand;
49 with Sinfo;    use Sinfo;
50 with Targparm; use Targparm;
51 with Tbuild;   use Tbuild;
52 with Uintp;    use Uintp;
53
54 package body Sem_Ch5 is
55
56    Unblocked_Exit_Count : Nat := 0;
57    --  This variable is used when processing if statements or case
58    --  statements, it counts the number of branches of the conditional
59    --  that are not blocked by unconditional transfer instructions. At
60    --  the end of processing, if the count is zero, it means that control
61    --  cannot fall through the conditional statement. This is used for
62    --  the generation of warning messages. This variable is recursively
63    --  saved on entry to processing an if or case, and restored on exit.
64
65    -----------------------
66    -- Local Subprograms --
67    -----------------------
68
69    procedure Analyze_Iteration_Scheme (N : Node_Id);
70
71    procedure Check_Possible_Current_Value_Condition (Cnode : Node_Id);
72    --  Cnode is N_If_Statement, N_Elsif_Part, or N_Iteration_Scheme
73    --  (the latter when a WHILE condition is present). This call checks
74    --  if Condition (Cnode) is of the form ([NOT] var op val), where var
75    --  is a simple object, val is known at compile time, and op is one
76    --  of the six relational operators. If this is the case, and the
77    --  Current_Value field of "var" is not set, then it is set to Cnode.
78    --  See Exp_Util.Set_Current_Value_Condition for further details.
79
80    ------------------------
81    -- Analyze_Assignment --
82    ------------------------
83
84    procedure Analyze_Assignment (N : Node_Id) is
85       Lhs  : constant Node_Id := Name (N);
86       Rhs  : constant Node_Id := Expression (N);
87       T1   : Entity_Id;
88       T2   : Entity_Id;
89       Decl : Node_Id;
90       Ent  : Entity_Id;
91
92       procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
93       --  N is the node for the left hand side of an assignment, and it
94       --  is not a variable. This routine issues an appropriate diagnostic.
95
96       procedure Set_Assignment_Type
97         (Opnd      : Node_Id;
98          Opnd_Type : in out Entity_Id);
99       --  Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type
100       --  is the nominal subtype. This procedure is used to deal with cases
101       --  where the nominal subtype must be replaced by the actual subtype.
102
103       -------------------------------
104       -- Diagnose_Non_Variable_Lhs --
105       -------------------------------
106
107       procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
108       begin
109          --  Not worth posting another error if left hand side already
110          --  flagged as being illegal in some respect
111
112          if Error_Posted (N) then
113             return;
114
115          --  Some special bad cases of entity names
116
117          elsif Is_Entity_Name (N) then
118             if Ekind (Entity (N)) = E_In_Parameter then
119                Error_Msg_N
120                  ("assignment to IN mode parameter not allowed", N);
121
122             --  Private declarations in a protected object are turned into
123             --  constants when compiling a protected function.
124
125             elsif Present (Scope (Entity (N)))
126               and then Is_Protected_Type (Scope (Entity (N)))
127               and then
128                 (Ekind (Current_Scope) = E_Function
129                   or else
130                  Ekind (Enclosing_Dynamic_Scope (Current_Scope)) = E_Function)
131             then
132                Error_Msg_N
133                  ("protected function cannot modify protected object", N);
134
135             elsif Ekind (Entity (N)) = E_Loop_Parameter then
136                Error_Msg_N
137                  ("assignment to loop parameter not allowed", N);
138
139             else
140                Error_Msg_N
141                  ("left hand side of assignment must be a variable", N);
142             end if;
143
144          --  For indexed components or selected components, test prefix
145
146          elsif Nkind (N) = N_Indexed_Component then
147             Diagnose_Non_Variable_Lhs (Prefix (N));
148
149          --  Another special case for assignment to discriminant.
150
151          elsif Nkind (N) = N_Selected_Component then
152             if Present (Entity (Selector_Name (N)))
153               and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
154             then
155                Error_Msg_N
156                  ("assignment to discriminant not allowed", N);
157             else
158                Diagnose_Non_Variable_Lhs (Prefix (N));
159             end if;
160
161          else
162             --  If we fall through, we have no special message to issue!
163
164             Error_Msg_N ("left hand side of assignment must be a variable", N);
165          end if;
166       end Diagnose_Non_Variable_Lhs;
167
168       -------------------------
169       -- Set_Assignment_Type --
170       -------------------------
171
172       procedure Set_Assignment_Type
173         (Opnd      : Node_Id;
174          Opnd_Type : in out Entity_Id)
175       is
176       begin
177          Require_Entity (Opnd);
178
179          --  If the assignment operand is an in-out or out parameter, then we
180          --  get the actual subtype (needed for the unconstrained case).
181          --  If the operand is the actual in an entry declaration, then within
182          --  the accept statement it is replaced with a local renaming, which
183          --  may also have an actual subtype.
184
185          if Is_Entity_Name (Opnd)
186            and then (Ekind (Entity (Opnd)) = E_Out_Parameter
187                       or else Ekind (Entity (Opnd)) =
188                            E_In_Out_Parameter
189                       or else Ekind (Entity (Opnd)) =
190                            E_Generic_In_Out_Parameter
191                       or else
192                         (Ekind (Entity (Opnd)) = E_Variable
193                           and then Nkind (Parent (Entity (Opnd))) =
194                              N_Object_Renaming_Declaration
195                           and then Nkind (Parent (Parent (Entity (Opnd)))) =
196                              N_Accept_Statement))
197          then
198             Opnd_Type := Get_Actual_Subtype (Opnd);
199
200          --  If assignment operand is a component reference, then we get the
201          --  actual subtype of the component for the unconstrained case.
202
203          elsif
204            (Nkind (Opnd) = N_Selected_Component
205              or else Nkind (Opnd) = N_Explicit_Dereference)
206            and then not Is_Unchecked_Union (Opnd_Type)
207          then
208             Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
209
210             if Present (Decl) then
211                Insert_Action (N, Decl);
212                Mark_Rewrite_Insertion (Decl);
213                Analyze (Decl);
214                Opnd_Type := Defining_Identifier (Decl);
215                Set_Etype (Opnd, Opnd_Type);
216                Freeze_Itype (Opnd_Type, N);
217
218             elsif Is_Constrained (Etype (Opnd)) then
219                Opnd_Type := Etype (Opnd);
220             end if;
221
222          --  For slice, use the constrained subtype created for the slice
223
224          elsif Nkind (Opnd) = N_Slice then
225             Opnd_Type := Etype (Opnd);
226          end if;
227       end Set_Assignment_Type;
228
229    --  Start of processing for Analyze_Assignment
230
231    begin
232       Analyze (Rhs);
233       Analyze (Lhs);
234       T1 := Etype (Lhs);
235
236       --  In the most general case, both Lhs and Rhs can be overloaded, and we
237       --  must compute the intersection of the possible types on each side.
238
239       if Is_Overloaded (Lhs) then
240          declare
241             I  : Interp_Index;
242             It : Interp;
243
244          begin
245             T1 := Any_Type;
246             Get_First_Interp (Lhs, I, It);
247
248             while Present (It.Typ) loop
249                if Has_Compatible_Type (Rhs, It.Typ) then
250                   if T1 /= Any_Type then
251
252                      --  An explicit dereference is overloaded if the prefix
253                      --  is. Try to remove the ambiguity on the prefix, the
254                      --  error will be posted there if the ambiguity is real.
255
256                      if Nkind (Lhs) = N_Explicit_Dereference then
257                         declare
258                            PI    : Interp_Index;
259                            PI1   : Interp_Index := 0;
260                            PIt   : Interp;
261                            Found : Boolean;
262
263                         begin
264                            Found := False;
265                            Get_First_Interp (Prefix (Lhs), PI, PIt);
266
267                            while Present (PIt.Typ) loop
268                               if Is_Access_Type (PIt.Typ)
269                                 and then Has_Compatible_Type
270                                            (Rhs, Designated_Type (PIt.Typ))
271                               then
272                                  if Found then
273                                     PIt :=
274                                       Disambiguate (Prefix (Lhs),
275                                         PI1, PI, Any_Type);
276
277                                     if PIt = No_Interp then
278                                        Error_Msg_N
279                                          ("ambiguous left-hand side"
280                                             & " in assignment", Lhs);
281                                        exit;
282                                     else
283                                        Resolve (Prefix (Lhs), PIt.Typ);
284                                     end if;
285
286                                     exit;
287                                  else
288                                     Found := True;
289                                     PI1 := PI;
290                                  end if;
291                               end if;
292
293                               Get_Next_Interp (PI, PIt);
294                            end loop;
295                         end;
296
297                      else
298                         Error_Msg_N
299                           ("ambiguous left-hand side in assignment", Lhs);
300                         exit;
301                      end if;
302                   else
303                      T1 := It.Typ;
304                   end if;
305                end if;
306
307                Get_Next_Interp (I, It);
308             end loop;
309          end;
310
311          if T1 = Any_Type then
312             Error_Msg_N
313               ("no valid types for left-hand side for assignment", Lhs);
314             return;
315          end if;
316       end if;
317
318       Resolve (Lhs, T1);
319
320       if not Is_Variable (Lhs) then
321          Diagnose_Non_Variable_Lhs (Lhs);
322          return;
323
324       elsif Is_Limited_Type (T1)
325         and then not Assignment_OK (Lhs)
326         and then not Assignment_OK (Original_Node (Lhs))
327       then
328          Error_Msg_N
329            ("left hand of assignment must not be limited type", Lhs);
330          Explain_Limited_Type (T1, Lhs);
331          return;
332       end if;
333
334       --  Resolution may have updated the subtype, in case the left-hand
335       --  side is a private protected component. Use the correct subtype
336       --  to avoid scoping issues in the back-end.
337
338       T1 := Etype (Lhs);
339       Set_Assignment_Type (Lhs, T1);
340
341       Resolve (Rhs, T1);
342       Check_Unset_Reference (Rhs);
343
344       --  Remaining steps are skipped if Rhs was syntactically in error
345
346       if Rhs = Error then
347          return;
348       end if;
349
350       T2 := Etype (Rhs);
351
352       if Covers (T1, T2) then
353          null;
354       else
355          Wrong_Type (Rhs, Etype (Lhs));
356          return;
357       end if;
358
359       Set_Assignment_Type (Rhs, T2);
360
361       if Total_Errors_Detected /= 0 then
362          if No (T1) then
363             T1 := Any_Type;
364          end if;
365
366          if No (T2) then
367             T2 := Any_Type;
368          end if;
369       end if;
370
371       if T1 = Any_Type or else T2 = Any_Type then
372          return;
373       end if;
374
375       if (Is_Class_Wide_Type (T2) or else Is_Dynamically_Tagged (Rhs))
376         and then not Is_Class_Wide_Type (T1)
377       then
378          Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
379
380       elsif Is_Class_Wide_Type (T1)
381         and then not Is_Class_Wide_Type (T2)
382         and then not Is_Tag_Indeterminate (Rhs)
383         and then not Is_Dynamically_Tagged (Rhs)
384       then
385          Error_Msg_N ("dynamically tagged expression required!", Rhs);
386       end if;
387
388       --  Tag propagation is done only in semantics mode only. If expansion
389       --  is on, the rhs tag indeterminate function call has been expanded
390       --  and tag propagation would have happened too late, so the
391       --  propagation take place in expand_call instead.
392
393       if not Expander_Active
394         and then Is_Class_Wide_Type (T1)
395         and then Is_Tag_Indeterminate (Rhs)
396       then
397          Propagate_Tag (Lhs, Rhs);
398       end if;
399
400       --  Ada 2005 (AI-231)
401
402       if Ada_Version >= Ada_05
403         and then Nkind (Rhs) = N_Null
404         and then Is_Access_Type (T1)
405         and then not Assignment_OK (Lhs)
406         and then ((Is_Entity_Name (Lhs)
407                      and then Can_Never_Be_Null (Entity (Lhs)))
408                    or else Can_Never_Be_Null (Etype (Lhs)))
409       then
410          Error_Msg_N
411            ("(Ada 2005) NULL not allowed in null-excluding objects", Lhs);
412       end if;
413
414       if Is_Scalar_Type (T1) then
415          Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
416
417       elsif Is_Array_Type (T1)
418         and then
419           (Nkind (Rhs) /= N_Type_Conversion
420              or else Is_Constrained (Etype (Rhs)))
421       then
422          --  Assignment verifies that the length of the Lsh and Rhs are equal,
423          --  but of course the indices do not have to match. If the right-hand
424          --  side is a type conversion to an unconstrained type, a length check
425          --  is performed on the expression itself during expansion. In rare
426          --  cases, the redundant length check is computed on an index type
427          --  with a different representation, triggering incorrect code in
428          --  the back end.
429
430          Apply_Length_Check (Rhs, Etype (Lhs));
431
432       else
433          --  Discriminant checks are applied in the course of expansion
434
435          null;
436       end if;
437
438       --  Note: modifications of the Lhs may only be recorded after
439       --  checks have been applied.
440
441       Note_Possible_Modification (Lhs);
442
443       --  ??? a real accessibility check is needed when ???
444
445       --  Post warning for useless assignment
446
447       if Warn_On_Redundant_Constructs
448
449          --  We only warn for source constructs
450
451          and then Comes_From_Source (N)
452
453          --  Where the entity is the same on both sides
454
455          and then Is_Entity_Name (Lhs)
456          and then Is_Entity_Name (Original_Node (Rhs))
457          and then Entity (Lhs) = Entity (Original_Node (Rhs))
458
459          --  But exclude the case where the right side was an operation
460          --  that got rewritten (e.g. JUNK + K, where K was known to be
461          --  zero). We don't want to warn in such a case, since it is
462          --  reasonable to write such expressions especially when K is
463          --  defined symbolically in some other package.
464
465         and then Nkind (Original_Node (Rhs)) not in N_Op
466       then
467          Error_Msg_NE
468            ("?useless assignment of & to itself", N, Entity (Lhs));
469       end if;
470
471       --  Check for non-allowed composite assignment
472
473       if not Support_Composite_Assign_On_Target
474         and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
475         and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
476       then
477          Error_Msg_CRT ("composite assignment", N);
478       end if;
479
480       --  One more step. Let's see if we have a simple assignment of a
481       --  known at compile time value to a simple variable. If so, we
482       --  can record the value as the current value providing that:
483
484       --    We still have a simple assignment statement (no expansion
485       --    activity has modified it in some peculiar manner)
486
487       --    The type is a discrete type
488
489       --    The assignment is to a named entity
490
491       --    The value is known at compile time
492
493       if Nkind (N) /= N_Assignment_Statement
494         or else not Is_Discrete_Type (T1)
495         or else not Is_Entity_Name (Lhs)
496         or else not Compile_Time_Known_Value (Rhs)
497       then
498          return;
499       end if;
500
501       Ent := Entity (Lhs);
502
503       --  Capture value if save to do so
504
505       if Safe_To_Capture_Value (N, Ent) then
506          Set_Current_Value (Ent, Rhs);
507       end if;
508    end Analyze_Assignment;
509
510    -----------------------------
511    -- Analyze_Block_Statement --
512    -----------------------------
513
514    procedure Analyze_Block_Statement (N : Node_Id) is
515       Decls : constant List_Id := Declarations (N);
516       Id    : constant Node_Id := Identifier (N);
517       Ent   : Entity_Id        := Empty;
518
519    begin
520       --  If a label is present analyze it and mark it as referenced
521
522       if Present (Id) then
523          Analyze (Id);
524          Ent := Entity (Id);
525
526          --  An error defense. If we have an identifier, but no entity, then
527          --  something is wrong. If we have previous errors, then just remove
528          --  the identifier and continue, otherwise raise an exception.
529
530          if No (Ent) then
531             if Total_Errors_Detected /= 0 then
532                Set_Identifier (N, Empty);
533             else
534                raise Program_Error;
535             end if;
536
537          else
538             Set_Ekind (Ent, E_Block);
539             Generate_Reference (Ent, N, ' ');
540             Generate_Definition (Ent);
541
542             if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
543                Set_Label_Construct (Parent (Ent), N);
544             end if;
545          end if;
546       end if;
547
548       --  If no entity set, create a label entity
549
550       if No (Ent) then
551          Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
552          Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
553          Set_Parent (Ent, N);
554       end if;
555
556       Set_Etype (Ent, Standard_Void_Type);
557       Set_Block_Node (Ent, Identifier (N));
558       New_Scope (Ent);
559
560       if Present (Decls) then
561          Analyze_Declarations (Decls);
562          Check_Completion;
563       end if;
564
565       Analyze (Handled_Statement_Sequence (N));
566       Process_End_Label (Handled_Statement_Sequence (N), 'e', Ent);
567
568       --  Analyze exception handlers if present. Note that the test for
569       --  HSS being present is an error defence against previous errors.
570
571       if Present (Handled_Statement_Sequence (N))
572         and then Present (Exception_Handlers (Handled_Statement_Sequence (N)))
573       then
574          declare
575             S : Entity_Id := Scope (Ent);
576
577          begin
578             --  Indicate that enclosing scopes contain a block with handlers.
579             --  Only non-generic scopes need to be marked.
580
581             loop
582                Set_Has_Nested_Block_With_Handler (S);
583                exit when Is_Overloadable (S)
584                  or else Ekind (S) = E_Package
585                  or else Is_Generic_Unit (S);
586                S := Scope (S);
587             end loop;
588          end;
589       end if;
590
591       Check_References (Ent);
592       End_Scope;
593    end Analyze_Block_Statement;
594
595    ----------------------------
596    -- Analyze_Case_Statement --
597    ----------------------------
598
599    procedure Analyze_Case_Statement (N : Node_Id) is
600
601       Statements_Analyzed : Boolean := False;
602       --  Set True if at least some statement sequences get analyzed.
603       --  If False on exit, means we had a serious error that prevented
604       --  full analysis of the case statement, and as a result it is not
605       --  a good idea to output warning messages about unreachable code.
606
607       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
608       --  Recursively save value of this global, will be restored on exit
609
610       procedure Non_Static_Choice_Error (Choice : Node_Id);
611       --  Error routine invoked by the generic instantiation below when
612       --  the case statment has a non static choice.
613
614       procedure Process_Statements (Alternative : Node_Id);
615       --  Analyzes all the statements associated to a case alternative.
616       --  Needed by the generic instantiation below.
617
618       package Case_Choices_Processing is new
619         Generic_Choices_Processing
620           (Get_Alternatives          => Alternatives,
621            Get_Choices               => Discrete_Choices,
622            Process_Empty_Choice      => No_OP,
623            Process_Non_Static_Choice => Non_Static_Choice_Error,
624            Process_Associated_Node   => Process_Statements);
625       use Case_Choices_Processing;
626       --  Instantiation of the generic choice processing package
627
628       -----------------------------
629       -- Non_Static_Choice_Error --
630       -----------------------------
631
632       procedure Non_Static_Choice_Error (Choice : Node_Id) is
633       begin
634          Flag_Non_Static_Expr
635            ("choice given in case statement is not static!", Choice);
636       end Non_Static_Choice_Error;
637
638       ------------------------
639       -- Process_Statements --
640       ------------------------
641
642       procedure Process_Statements (Alternative : Node_Id) is
643       begin
644          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
645          Statements_Analyzed := True;
646          Analyze_Statements (Statements (Alternative));
647       end Process_Statements;
648
649       --  Variables local to Analyze_Case_Statement.
650
651       Exp       : Node_Id;
652       Exp_Type  : Entity_Id;
653       Exp_Btype : Entity_Id;
654
655       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
656       Last_Choice    : Nat;
657       Dont_Care      : Boolean;
658       Others_Present : Boolean;
659
660    --  Start of processing for Analyze_Case_Statement
661
662    begin
663       Unblocked_Exit_Count := 0;
664       Exp := Expression (N);
665       Analyze_And_Resolve (Exp, Any_Discrete);
666       Check_Unset_Reference (Exp);
667       Exp_Type  := Etype (Exp);
668       Exp_Btype := Base_Type (Exp_Type);
669
670       --  The expression must be of a discrete type which must be determinable
671       --  independently of the context in which the expression occurs, but
672       --  using the fact that the expression must be of a discrete type.
673       --  Moreover, the type this expression must not be a character literal
674       --  (which is always ambiguous) or, for Ada-83, a generic formal type.
675
676       --  If error already reported by Resolve, nothing more to do
677
678       if Exp_Btype = Any_Discrete
679         or else Exp_Btype = Any_Type
680       then
681          return;
682
683       elsif Exp_Btype = Any_Character then
684          Error_Msg_N
685            ("character literal as case expression is ambiguous", Exp);
686          return;
687
688       elsif Ada_Version = Ada_83
689         and then (Is_Generic_Type (Exp_Btype)
690                     or else Is_Generic_Type (Root_Type (Exp_Btype)))
691       then
692          Error_Msg_N
693            ("(Ada 83) case expression cannot be of a generic type", Exp);
694          return;
695       end if;
696
697       --  If the case expression is a formal object of mode in out, then
698       --  treat it as having a nonstatic subtype by forcing use of the base
699       --  type (which has to get passed to Check_Case_Choices below).  Also
700       --  use base type when the case expression is parenthesized.
701
702       if Paren_Count (Exp) > 0
703         or else (Is_Entity_Name (Exp)
704                   and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
705       then
706          Exp_Type := Exp_Btype;
707       end if;
708
709       --  Call instantiated Analyze_Choices which does the rest of the work
710
711       Analyze_Choices
712         (N, Exp_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
713
714       if Exp_Type = Universal_Integer and then not Others_Present then
715          Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
716       end if;
717
718       --  If all our exits were blocked by unconditional transfers of control,
719       --  then the entire CASE statement acts as an unconditional transfer of
720       --  control, so treat it like one, and check unreachable code. Skip this
721       --  test if we had serious errors preventing any statement analysis.
722
723       if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
724          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
725          Check_Unreachable_Code (N);
726       else
727          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
728       end if;
729
730       if not Expander_Active
731         and then Compile_Time_Known_Value (Expression (N))
732         and then Serious_Errors_Detected = 0
733       then
734          declare
735             Chosen : constant Node_Id := Find_Static_Alternative (N);
736             Alt    : Node_Id;
737
738          begin
739             Alt := First (Alternatives (N));
740
741             while Present (Alt) loop
742                if Alt /= Chosen then
743                   Remove_Warning_Messages (Statements (Alt));
744                end if;
745
746                Next (Alt);
747             end loop;
748          end;
749       end if;
750    end Analyze_Case_Statement;
751
752    ----------------------------
753    -- Analyze_Exit_Statement --
754    ----------------------------
755
756    --  If the exit includes a name, it must be the name of a currently open
757    --  loop. Otherwise there must be an innermost open loop on the stack,
758    --  to which the statement implicitly refers.
759
760    procedure Analyze_Exit_Statement (N : Node_Id) is
761       Target   : constant Node_Id := Name (N);
762       Cond     : constant Node_Id := Condition (N);
763       Scope_Id : Entity_Id;
764       U_Name   : Entity_Id;
765       Kind     : Entity_Kind;
766
767    begin
768       if No (Cond) then
769          Check_Unreachable_Code (N);
770       end if;
771
772       if Present (Target) then
773          Analyze (Target);
774          U_Name := Entity (Target);
775
776          if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
777             Error_Msg_N ("invalid loop name in exit statement", N);
778             return;
779          else
780             Set_Has_Exit (U_Name);
781          end if;
782
783       else
784          U_Name := Empty;
785       end if;
786
787       for J in reverse 0 .. Scope_Stack.Last loop
788          Scope_Id := Scope_Stack.Table (J).Entity;
789          Kind := Ekind (Scope_Id);
790
791          if Kind = E_Loop
792            and then (No (Target) or else Scope_Id = U_Name) then
793             Set_Has_Exit (Scope_Id);
794             exit;
795
796          elsif Kind = E_Block or else Kind = E_Loop then
797             null;
798
799          else
800             Error_Msg_N
801               ("cannot exit from program unit or accept statement", N);
802             exit;
803          end if;
804       end loop;
805
806       --  Verify that if present the condition is a Boolean expression
807
808       if Present (Cond) then
809          Analyze_And_Resolve (Cond, Any_Boolean);
810          Check_Unset_Reference (Cond);
811       end if;
812    end Analyze_Exit_Statement;
813
814    ----------------------------
815    -- Analyze_Goto_Statement --
816    ----------------------------
817
818    procedure Analyze_Goto_Statement (N : Node_Id) is
819       Label       : constant Node_Id := Name (N);
820       Scope_Id    : Entity_Id;
821       Label_Scope : Entity_Id;
822
823    begin
824       Check_Unreachable_Code (N);
825
826       Analyze (Label);
827
828       if Entity (Label) = Any_Id then
829          return;
830
831       elsif Ekind (Entity (Label)) /= E_Label then
832          Error_Msg_N ("target of goto statement must be a label", Label);
833          return;
834
835       elsif not Reachable (Entity (Label)) then
836          Error_Msg_N ("target of goto statement is not reachable", Label);
837          return;
838       end if;
839
840       Label_Scope := Enclosing_Scope (Entity (Label));
841
842       for J in reverse 0 .. Scope_Stack.Last loop
843          Scope_Id := Scope_Stack.Table (J).Entity;
844
845          if Label_Scope = Scope_Id
846            or else (Ekind (Scope_Id) /= E_Block
847                      and then Ekind (Scope_Id) /= E_Loop)
848          then
849             if Scope_Id /= Label_Scope then
850                Error_Msg_N
851                  ("cannot exit from program unit or accept statement", N);
852             end if;
853
854             return;
855          end if;
856       end loop;
857
858       raise Program_Error;
859    end Analyze_Goto_Statement;
860
861    --------------------------
862    -- Analyze_If_Statement --
863    --------------------------
864
865    --  A special complication arises in the analysis of if statements.
866
867    --  The expander has circuitry to completely delete code that it
868    --  can tell will not be executed (as a result of compile time known
869    --  conditions). In the analyzer, we ensure that code that will be
870    --  deleted in this manner is analyzed but not expanded. This is
871    --  obviously more efficient, but more significantly, difficulties
872    --  arise if code is expanded and then eliminated (e.g. exception
873    --  table entries disappear). Similarly, itypes generated in deleted
874    --  code must be frozen from start, because the nodes on which they
875    --  depend will not be available at the freeze point.
876
877    procedure Analyze_If_Statement (N : Node_Id) is
878       E : Node_Id;
879
880       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
881       --  Recursively save value of this global, will be restored on exit
882
883       Save_In_Deleted_Code : Boolean;
884
885       Del : Boolean := False;
886       --  This flag gets set True if a True condition has been found,
887       --  which means that remaining ELSE/ELSIF parts are deleted.
888
889       procedure Analyze_Cond_Then (Cnode : Node_Id);
890       --  This is applied to either the N_If_Statement node itself or
891       --  to an N_Elsif_Part node. It deals with analyzing the condition
892       --  and the THEN statements associated with it.
893
894       -----------------------
895       -- Analyze_Cond_Then --
896       -----------------------
897
898       procedure Analyze_Cond_Then (Cnode : Node_Id) is
899          Cond : constant Node_Id := Condition (Cnode);
900          Tstm : constant List_Id := Then_Statements (Cnode);
901
902       begin
903          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
904          Analyze_And_Resolve (Cond, Any_Boolean);
905          Check_Unset_Reference (Cond);
906          Check_Possible_Current_Value_Condition (Cnode);
907
908          --  If already deleting, then just analyze then statements
909
910          if Del then
911             Analyze_Statements (Tstm);
912
913          --  Compile time known value, not deleting yet
914
915          elsif Compile_Time_Known_Value (Cond) then
916             Save_In_Deleted_Code := In_Deleted_Code;
917
918             --  If condition is True, then analyze the THEN statements
919             --  and set no expansion for ELSE and ELSIF parts.
920
921             if Is_True (Expr_Value (Cond)) then
922                Analyze_Statements (Tstm);
923                Del := True;
924                Expander_Mode_Save_And_Set (False);
925                In_Deleted_Code := True;
926
927             --  If condition is False, analyze THEN with expansion off
928
929             else -- Is_False (Expr_Value (Cond))
930                Expander_Mode_Save_And_Set (False);
931                In_Deleted_Code := True;
932                Analyze_Statements (Tstm);
933                Expander_Mode_Restore;
934                In_Deleted_Code := Save_In_Deleted_Code;
935             end if;
936
937          --  Not known at compile time, not deleting, normal analysis
938
939          else
940             Analyze_Statements (Tstm);
941          end if;
942       end Analyze_Cond_Then;
943
944    --  Start of Analyze_If_Statement
945
946    begin
947       --  Initialize exit count for else statements. If there is no else
948       --  part, this count will stay non-zero reflecting the fact that the
949       --  uncovered else case is an unblocked exit.
950
951       Unblocked_Exit_Count := 1;
952       Analyze_Cond_Then (N);
953
954       --  Now to analyze the elsif parts if any are present
955
956       if Present (Elsif_Parts (N)) then
957          E := First (Elsif_Parts (N));
958          while Present (E) loop
959             Analyze_Cond_Then (E);
960             Next (E);
961          end loop;
962       end if;
963
964       if Present (Else_Statements (N)) then
965          Analyze_Statements (Else_Statements (N));
966       end if;
967
968       --  If all our exits were blocked by unconditional transfers of control,
969       --  then the entire IF statement acts as an unconditional transfer of
970       --  control, so treat it like one, and check unreachable code.
971
972       if Unblocked_Exit_Count = 0 then
973          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
974          Check_Unreachable_Code (N);
975       else
976          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
977       end if;
978
979       if Del then
980          Expander_Mode_Restore;
981          In_Deleted_Code := Save_In_Deleted_Code;
982       end if;
983
984       if not Expander_Active
985         and then Compile_Time_Known_Value (Condition (N))
986         and then Serious_Errors_Detected = 0
987       then
988          if Is_True (Expr_Value (Condition (N))) then
989             Remove_Warning_Messages (Else_Statements (N));
990
991             if Present (Elsif_Parts (N)) then
992                E := First (Elsif_Parts (N));
993
994                while Present (E) loop
995                   Remove_Warning_Messages (Then_Statements (E));
996                   Next (E);
997                end loop;
998             end if;
999
1000          else
1001             Remove_Warning_Messages (Then_Statements (N));
1002          end if;
1003       end if;
1004    end Analyze_If_Statement;
1005
1006    ----------------------------------------
1007    -- Analyze_Implicit_Label_Declaration --
1008    ----------------------------------------
1009
1010    --  An implicit label declaration is generated in the innermost
1011    --  enclosing declarative part. This is done for labels as well as
1012    --  block and loop names.
1013
1014    --  Note: any changes in this routine may need to be reflected in
1015    --  Analyze_Label_Entity.
1016
1017    procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1018       Id : constant Node_Id := Defining_Identifier (N);
1019    begin
1020       Enter_Name          (Id);
1021       Set_Ekind           (Id, E_Label);
1022       Set_Etype           (Id, Standard_Void_Type);
1023       Set_Enclosing_Scope (Id, Current_Scope);
1024    end Analyze_Implicit_Label_Declaration;
1025
1026    ------------------------------
1027    -- Analyze_Iteration_Scheme --
1028    ------------------------------
1029
1030    procedure Analyze_Iteration_Scheme (N : Node_Id) is
1031       procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1032       --  If the bounds are given by a 'Range reference on a function call
1033       --  that returns a controlled array, introduce an explicit declaration
1034       --  to capture the bounds, so that the function result can be finalized
1035       --  in timely fashion.
1036
1037       --------------------------------------
1038       -- Check_Controlled_Array_Attribute --
1039       --------------------------------------
1040
1041       procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1042       begin
1043          if Nkind (DS) = N_Attribute_Reference
1044             and then Is_Entity_Name (Prefix (DS))
1045             and then Ekind (Entity (Prefix (DS))) = E_Function
1046             and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1047             and then
1048               Is_Controlled (
1049                 Component_Type (Etype (Entity (Prefix (DS)))))
1050             and then Expander_Active
1051          then
1052             declare
1053                Loc  : constant Source_Ptr := Sloc (N);
1054                Arr  : constant Entity_Id :=
1055                         Etype (Entity (Prefix (DS)));
1056                Indx : constant Entity_Id :=
1057                         Base_Type (Etype (First_Index (Arr)));
1058                Subt : constant Entity_Id :=
1059                         Make_Defining_Identifier
1060                           (Loc, New_Internal_Name ('S'));
1061                Decl : Node_Id;
1062
1063             begin
1064                Decl :=
1065                  Make_Subtype_Declaration (Loc,
1066                    Defining_Identifier => Subt,
1067                    Subtype_Indication  =>
1068                       Make_Subtype_Indication (Loc,
1069                         Subtype_Mark  => New_Reference_To (Indx, Loc),
1070                         Constraint =>
1071                           Make_Range_Constraint (Loc,
1072                             Relocate_Node (DS))));
1073                Insert_Before (Parent (N), Decl);
1074                Analyze (Decl);
1075
1076                Rewrite (DS,
1077                   Make_Attribute_Reference (Loc,
1078                     Prefix => New_Reference_To (Subt, Loc),
1079                     Attribute_Name => Attribute_Name (DS)));
1080                Analyze (DS);
1081             end;
1082          end if;
1083       end Check_Controlled_Array_Attribute;
1084
1085    --  Start of processing for Analyze_Iteration_Scheme
1086
1087    begin
1088       --  For an infinite loop, there is no iteration scheme
1089
1090       if No (N) then
1091          return;
1092
1093       else
1094          declare
1095             Cond : constant Node_Id := Condition (N);
1096
1097          begin
1098             --  For WHILE loop, verify that the condition is a Boolean
1099             --  expression and resolve and check it.
1100
1101             if Present (Cond) then
1102                Analyze_And_Resolve (Cond, Any_Boolean);
1103                Check_Unset_Reference (Cond);
1104
1105             --  Else we have a FOR loop
1106
1107             else
1108                declare
1109                   LP : constant Node_Id   := Loop_Parameter_Specification (N);
1110                   Id : constant Entity_Id := Defining_Identifier (LP);
1111                   DS : constant Node_Id   := Discrete_Subtype_Definition (LP);
1112
1113                begin
1114                   Enter_Name (Id);
1115
1116                   --  We always consider the loop variable to be referenced,
1117                   --  since the loop may be used just for counting purposes.
1118
1119                   Generate_Reference (Id, N, ' ');
1120
1121                   --  Check for case of loop variable hiding a local
1122                   --  variable (used later on to give a nice warning
1123                   --  if the hidden variable is never assigned).
1124
1125                   declare
1126                      H : constant Entity_Id := Homonym (Id);
1127                   begin
1128                      if Present (H)
1129                        and then Enclosing_Dynamic_Scope (H) =
1130                                 Enclosing_Dynamic_Scope (Id)
1131                        and then Ekind (H) = E_Variable
1132                        and then Is_Discrete_Type (Etype (H))
1133                      then
1134                         Set_Hiding_Loop_Variable (H, Id);
1135                      end if;
1136                   end;
1137
1138                   --  Now analyze the subtype definition
1139
1140                   Analyze (DS);
1141
1142                   if DS = Error then
1143                      return;
1144                   end if;
1145
1146                   --  The subtype indication may denote the completion
1147                   --  of an incomplete type declaration.
1148
1149                   if Is_Entity_Name (DS)
1150                     and then Present (Entity (DS))
1151                     and then Is_Type (Entity (DS))
1152                     and then Ekind (Entity (DS)) = E_Incomplete_Type
1153                   then
1154                      Set_Entity (DS, Get_Full_View (Entity (DS)));
1155                      Set_Etype  (DS, Entity (DS));
1156                   end if;
1157
1158                   if not Is_Discrete_Type (Etype (DS)) then
1159                      Wrong_Type (DS, Any_Discrete);
1160                      Set_Etype (DS, Any_Type);
1161                   end if;
1162
1163                   Check_Controlled_Array_Attribute (DS);
1164                   Make_Index (DS, LP);
1165
1166                   Set_Ekind          (Id, E_Loop_Parameter);
1167                   Set_Etype          (Id, Etype (DS));
1168                   Set_Is_Known_Valid (Id, True);
1169
1170                   --  The loop is not a declarative part, so the only entity
1171                   --  declared "within" must be frozen explicitly.
1172
1173                   declare
1174                      Flist : constant List_Id := Freeze_Entity (Id, Sloc (N));
1175                   begin
1176                      if Is_Non_Empty_List (Flist) then
1177                         Insert_Actions (N, Flist);
1178                      end if;
1179                   end;
1180
1181                   --  Check for null or possibly null range and issue warning.
1182                   --  We suppress such messages in generic templates and
1183                   --  instances, because in practice they tend to be dubious
1184                   --  in these cases.
1185
1186                   if Nkind (DS) = N_Range
1187                     and then Comes_From_Source (N)
1188                   then
1189                      declare
1190                         L : constant Node_Id := Low_Bound  (DS);
1191                         H : constant Node_Id := High_Bound (DS);
1192
1193                         Llo : Uint;
1194                         Lhi : Uint;
1195                         LOK : Boolean;
1196                         Hlo : Uint;
1197                         Hhi : Uint;
1198                         HOK : Boolean;
1199
1200                      begin
1201                         Determine_Range (L, LOK, Llo, Lhi);
1202                         Determine_Range (H, HOK, Hlo, Hhi);
1203
1204                         --  If range of loop is null, issue warning
1205
1206                         if (LOK and HOK) and then Llo > Hhi then
1207
1208                            --  Suppress the warning if inside a generic
1209                            --  template or instance, since in practice
1210                            --  they tend to be dubious in these cases since
1211                            --  they can result from intended parametrization.
1212
1213                            if not Inside_A_Generic
1214                               and then not In_Instance
1215                            then
1216                               Error_Msg_N
1217                                 ("?loop range is null, loop will not execute",
1218                                  DS);
1219                            end if;
1220
1221                            --  Since we know the range of the loop is null,
1222                            --  set the appropriate flag to suppress any
1223                            --  warnings that would otherwise be issued in
1224                            --  the body of the loop that will not execute.
1225                            --  We do this even in the generic case, since
1226                            --  if it is dubious to warn on the null loop
1227                            --  itself, it is certainly dubious to warn for
1228                            --  conditions that occur inside it!
1229
1230                            Set_Is_Null_Loop (Parent (N));
1231
1232                         --  The other case for a warning is a reverse loop
1233                         --  where the upper bound is the integer literal
1234                         --  zero or one, and the lower bound can be positive.
1235
1236                         --  For example, we have
1237
1238                         --     for J in reverse N .. 1 loop
1239
1240                         --  In practice, this is very likely to be a case
1241                         --  of reversing the bounds incorrectly in the range.
1242
1243                         elsif Reverse_Present (LP)
1244                           and then Nkind (H) = N_Integer_Literal
1245                           and then (Intval (H) = Uint_0
1246                                       or else
1247                                     Intval (H) = Uint_1)
1248                           and then Lhi > Hhi
1249                         then
1250                            Error_Msg_N ("?loop range may be null", DS);
1251                         end if;
1252                      end;
1253                   end if;
1254                end;
1255             end if;
1256          end;
1257       end if;
1258    end Analyze_Iteration_Scheme;
1259
1260    -------------------
1261    -- Analyze_Label --
1262    -------------------
1263
1264    --  Note: the semantic work required for analyzing labels (setting them as
1265    --  reachable) was done in a prepass through the statements in the block,
1266    --  so that forward gotos would be properly handled. See Analyze_Statements
1267    --  for further details. The only processing required here is to deal with
1268    --  optimizations that depend on an assumption of sequential control flow,
1269    --  since of course the occurrence of a label breaks this assumption.
1270
1271    procedure Analyze_Label (N : Node_Id) is
1272       pragma Warnings (Off, N);
1273    begin
1274       Kill_Current_Values;
1275    end Analyze_Label;
1276
1277    --------------------------
1278    -- Analyze_Label_Entity --
1279    --------------------------
1280
1281    procedure Analyze_Label_Entity (E : Entity_Id) is
1282    begin
1283       Set_Ekind           (E, E_Label);
1284       Set_Etype           (E, Standard_Void_Type);
1285       Set_Enclosing_Scope (E, Current_Scope);
1286       Set_Reachable       (E, True);
1287    end Analyze_Label_Entity;
1288
1289    ----------------------------
1290    -- Analyze_Loop_Statement --
1291    ----------------------------
1292
1293    procedure Analyze_Loop_Statement (N : Node_Id) is
1294       Id  : constant Node_Id := Identifier (N);
1295       Ent : Entity_Id;
1296
1297    begin
1298       if Present (Id) then
1299
1300          --  Make name visible, e.g. for use in exit statements. Loop
1301          --  labels are always considered to be referenced.
1302
1303          Analyze (Id);
1304          Ent := Entity (Id);
1305          Generate_Reference  (Ent, N, ' ');
1306          Generate_Definition (Ent);
1307
1308          --  If we found a label, mark its type. If not, ignore it, since it
1309          --  means we have a conflicting declaration, which would already have
1310          --  been diagnosed at declaration time. Set Label_Construct of the
1311          --  implicit label declaration, which is not created by the parser
1312          --  for generic units.
1313
1314          if Ekind (Ent) = E_Label then
1315             Set_Ekind (Ent, E_Loop);
1316
1317             if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1318                Set_Label_Construct (Parent (Ent), N);
1319             end if;
1320          end if;
1321
1322       --  Case of no identifier present
1323
1324       else
1325          Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
1326          Set_Etype (Ent,  Standard_Void_Type);
1327          Set_Parent (Ent, N);
1328       end if;
1329
1330       --  Kill current values on entry to loop, since statements in body
1331       --  of loop may have been executed before the loop is entered.
1332       --  Similarly we kill values after the loop, since we do not know
1333       --  that the body of the loop was executed.
1334
1335       Kill_Current_Values;
1336       New_Scope (Ent);
1337       Analyze_Iteration_Scheme (Iteration_Scheme (N));
1338       Analyze_Statements (Statements (N));
1339       Process_End_Label (N, 'e', Ent);
1340       End_Scope;
1341       Kill_Current_Values;
1342    end Analyze_Loop_Statement;
1343
1344    ----------------------------
1345    -- Analyze_Null_Statement --
1346    ----------------------------
1347
1348    --  Note: the semantics of the null statement is implemented by a single
1349    --  null statement, too bad everything isn't as simple as this!
1350
1351    procedure Analyze_Null_Statement (N : Node_Id) is
1352       pragma Warnings (Off, N);
1353    begin
1354       null;
1355    end Analyze_Null_Statement;
1356
1357    ------------------------
1358    -- Analyze_Statements --
1359    ------------------------
1360
1361    procedure Analyze_Statements (L : List_Id) is
1362       S   : Node_Id;
1363       Lab : Entity_Id;
1364
1365    begin
1366       --  The labels declared in the statement list are reachable from
1367       --  statements in the list. We do this as a prepass so that any
1368       --  goto statement will be properly flagged if its target is not
1369       --  reachable. This is not required, but is nice behavior!
1370
1371       S := First (L);
1372       while Present (S) loop
1373          if Nkind (S) = N_Label then
1374             Analyze (Identifier (S));
1375             Lab := Entity (Identifier (S));
1376
1377             --  If we found a label mark it as reachable.
1378
1379             if Ekind (Lab) = E_Label then
1380                Generate_Definition (Lab);
1381                Set_Reachable (Lab);
1382
1383                if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
1384                   Set_Label_Construct (Parent (Lab), S);
1385                end if;
1386
1387             --  If we failed to find a label, it means the implicit declaration
1388             --  of the label was hidden.  A for-loop parameter can do this to
1389             --  a label with the same name inside the loop, since the implicit
1390             --  label declaration is in the innermost enclosing body or block
1391             --  statement.
1392
1393             else
1394                Error_Msg_Sloc := Sloc (Lab);
1395                Error_Msg_N
1396                  ("implicit label declaration for & is hidden#",
1397                   Identifier (S));
1398             end if;
1399          end if;
1400
1401          Next (S);
1402       end loop;
1403
1404       --  Perform semantic analysis on all statements
1405
1406       Conditional_Statements_Begin;
1407
1408       S := First (L);
1409       while Present (S) loop
1410          Analyze (S);
1411          Next (S);
1412       end loop;
1413
1414       Conditional_Statements_End;
1415
1416       --  Make labels unreachable. Visibility is not sufficient, because
1417       --  labels in one if-branch for example are not reachable from the
1418       --  other branch, even though their declarations are in the enclosing
1419       --  declarative part.
1420
1421       S := First (L);
1422       while Present (S) loop
1423          if Nkind (S) = N_Label then
1424             Set_Reachable (Entity (Identifier (S)), False);
1425          end if;
1426
1427          Next (S);
1428       end loop;
1429    end Analyze_Statements;
1430
1431    --------------------------------------------
1432    -- Check_Possible_Current_Value_Condition --
1433    --------------------------------------------
1434
1435    procedure Check_Possible_Current_Value_Condition (Cnode : Node_Id) is
1436       Cond : Node_Id;
1437
1438    begin
1439       --  Loop to deal with (ignore for now) any NOT operators present
1440
1441       Cond := Condition (Cnode);
1442       while Nkind (Cond) = N_Op_Not loop
1443          Cond := Right_Opnd (Cond);
1444       end loop;
1445
1446       --  Check possible relational operator
1447
1448       if Nkind (Cond) = N_Op_Eq
1449            or else
1450          Nkind (Cond) = N_Op_Ne
1451            or else
1452          Nkind (Cond) = N_Op_Ge
1453            or else
1454          Nkind (Cond) = N_Op_Le
1455            or else
1456          Nkind (Cond) = N_Op_Gt
1457            or else
1458          Nkind (Cond) = N_Op_Lt
1459       then
1460          if Compile_Time_Known_Value (Right_Opnd (Cond))
1461            and then Nkind (Left_Opnd (Cond)) = N_Identifier
1462          then
1463             declare
1464                Ent : constant Entity_Id := Entity (Left_Opnd (Cond));
1465
1466             begin
1467                if Ekind (Ent) = E_Variable
1468                     or else
1469                   Ekind (Ent) = E_Constant
1470                     or else
1471                   Is_Formal (Ent)
1472                     or else
1473                   Ekind (Ent) = E_Loop_Parameter
1474                then
1475                   --  Here we have a case where the Current_Value field
1476                   --  may need to be set. We set it if it is not already
1477                   --  set to a compile time expression value.
1478
1479                   --  Note that this represents a decision that one
1480                   --  condition blots out another previous one. That's
1481                   --  certainly right if they occur at the same level.
1482                   --  If the second one is nested, then the decision is
1483                   --  neither right nor wrong (it would be equally OK
1484                   --  to leave the outer one in place, or take the new
1485                   --  inner one. Really we should record both, but our
1486                   --  data structures are not that elaborate.
1487
1488                   if Nkind (Current_Value (Ent)) not in N_Subexpr then
1489                      Set_Current_Value (Ent, Cnode);
1490                   end if;
1491                end if;
1492             end;
1493          end if;
1494       end if;
1495    end Check_Possible_Current_Value_Condition;
1496
1497    ----------------------------
1498    -- Check_Unreachable_Code --
1499    ----------------------------
1500
1501    procedure Check_Unreachable_Code (N : Node_Id) is
1502       Error_Loc : Source_Ptr;
1503       P         : Node_Id;
1504
1505    begin
1506       if Is_List_Member (N)
1507         and then Comes_From_Source (N)
1508       then
1509          declare
1510             Nxt : Node_Id;
1511
1512          begin
1513             Nxt := Original_Node (Next (N));
1514
1515             if Present (Nxt)
1516               and then Comes_From_Source (Nxt)
1517               and then Is_Statement (Nxt)
1518             then
1519                --  Special very annoying exception. If we have a return that
1520                --  follows a raise, then we allow it without a warning, since
1521                --  the Ada RM annoyingly requires a useless return here!
1522
1523                if Nkind (Original_Node (N)) /= N_Raise_Statement
1524                  or else Nkind (Nxt) /= N_Return_Statement
1525                then
1526                   --  The rather strange shenanigans with the warning message
1527                   --  here reflects the fact that Kill_Dead_Code is very good
1528                   --  at removing warnings in deleted code, and this is one
1529                   --  warning we would prefer NOT to have removed :-)
1530
1531                   Error_Loc := Sloc (Nxt);
1532
1533                   --  If we have unreachable code, analyze and remove the
1534                   --  unreachable code, since it is useless and we don't
1535                   --  want to generate junk warnings.
1536
1537                   --  We skip this step if we are not in code generation mode.
1538                   --  This is the one case where we remove dead code in the
1539                   --  semantics as opposed to the expander, and we do not want
1540                   --  to remove code if we are not in code generation mode,
1541                   --  since this messes up the ASIS trees.
1542
1543                   --  Note that one might react by moving the whole circuit to
1544                   --  exp_ch5, but then we lose the warning in -gnatc mode.
1545
1546                   if Operating_Mode = Generate_Code then
1547                      loop
1548                         Nxt := Next (N);
1549
1550                         --  Quit deleting when we have nothing more to delete
1551                         --  or if we hit a label (since someone could transfer
1552                         --  control to a label, so we should not delete it).
1553
1554                         exit when No (Nxt) or else Nkind (Nxt) = N_Label;
1555
1556                         --  Statement/declaration is to be deleted
1557
1558                         Analyze (Nxt);
1559                         Remove (Nxt);
1560                         Kill_Dead_Code (Nxt);
1561                      end loop;
1562                   end if;
1563
1564                   --  Now issue the warning
1565
1566                   Error_Msg ("?unreachable code", Error_Loc);
1567                end if;
1568
1569             --  If the unconditional transfer of control instruction is
1570             --  the last statement of a sequence, then see if our parent
1571             --  is an IF statement, and if so adjust the unblocked exit
1572             --  count of the if statement to reflect the fact that this
1573             --  branch of the if is indeed blocked by a transfer of control.
1574
1575             else
1576                P := Parent (N);
1577
1578                if Nkind (P) = N_If_Statement then
1579                   null;
1580
1581                elsif Nkind (P) = N_Elsif_Part then
1582                   P := Parent (P);
1583                   pragma Assert (Nkind (P) = N_If_Statement);
1584
1585                elsif Nkind (P) = N_Case_Statement_Alternative then
1586                   P := Parent (P);
1587                   pragma Assert (Nkind (P) = N_Case_Statement);
1588
1589                else
1590                   return;
1591                end if;
1592
1593                Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
1594             end if;
1595          end;
1596       end if;
1597    end Check_Unreachable_Code;
1598
1599 end Sem_Ch5;