OSDN Git Service

Fix aliasing bug that also caused memory usage problems.
[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       Exp            : Node_Id;
601       Exp_Type       : Entity_Id;
602       Exp_Btype      : Entity_Id;
603       Last_Choice    : Nat;
604       Dont_Care      : Boolean;
605       Others_Present : Boolean;
606
607       Statements_Analyzed : Boolean := False;
608       --  Set True if at least some statement sequences get analyzed.
609       --  If False on exit, means we had a serious error that prevented
610       --  full analysis of the case statement, and as a result it is not
611       --  a good idea to output warning messages about unreachable code.
612
613       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
614       --  Recursively save value of this global, will be restored on exit
615
616       procedure Non_Static_Choice_Error (Choice : Node_Id);
617       --  Error routine invoked by the generic instantiation below when
618       --  the case statment has a non static choice.
619
620       procedure Process_Statements (Alternative : Node_Id);
621       --  Analyzes all the statements associated to a case alternative.
622       --  Needed by the generic instantiation below.
623
624       package Case_Choices_Processing is new
625         Generic_Choices_Processing
626           (Get_Alternatives          => Alternatives,
627            Get_Choices               => Discrete_Choices,
628            Process_Empty_Choice      => No_OP,
629            Process_Non_Static_Choice => Non_Static_Choice_Error,
630            Process_Associated_Node   => Process_Statements);
631       use Case_Choices_Processing;
632       --  Instantiation of the generic choice processing package
633
634       -----------------------------
635       -- Non_Static_Choice_Error --
636       -----------------------------
637
638       procedure Non_Static_Choice_Error (Choice : Node_Id) is
639       begin
640          Flag_Non_Static_Expr
641            ("choice given in case statement is not static!", Choice);
642       end Non_Static_Choice_Error;
643
644       ------------------------
645       -- Process_Statements --
646       ------------------------
647
648       procedure Process_Statements (Alternative : Node_Id) is
649          Choices : constant List_Id := Discrete_Choices (Alternative);
650          Ent     : Entity_Id;
651
652       begin
653          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
654          Statements_Analyzed := True;
655
656          --  An interesting optimization. If the case statement expression
657          --  is a simple entity, then we can set the current value within
658          --  an alternative if the alternative has one possible value.
659
660          --    case N is
661          --      when 1      => alpha
662          --      when 2 | 3  => beta
663          --      when others => gamma
664
665          --  Here we know that N is initially 1 within alpha, but for beta
666          --  and gamma, we do not know anything more about the initial value.
667
668          if Is_Entity_Name (Exp) then
669             Ent := Entity (Exp);
670
671             if Ekind (Ent) = E_Variable
672                  or else
673                Ekind (Ent) = E_In_Out_Parameter
674                  or else
675                Ekind (Ent) = E_Out_Parameter
676             then
677                if List_Length (Choices) = 1
678                  and then Nkind (First (Choices)) in N_Subexpr
679                  and then Compile_Time_Known_Value (First (Choices))
680                then
681                   Set_Current_Value (Entity (Exp), First (Choices));
682                end if;
683
684                Analyze_Statements (Statements (Alternative));
685
686                --  After analyzing the case, set the current value to empty
687                --  since we won't know what it is for the next alternative
688                --  (unless reset by this same circuit), or after the case.
689
690                Set_Current_Value (Entity (Exp), Empty);
691                return;
692             end if;
693          end if;
694
695          --  Case where expression is not an entity name of a variable
696
697          Analyze_Statements (Statements (Alternative));
698       end Process_Statements;
699
700       --  Table to record choices. Put after subprograms since we make
701       --  a call to Number_Of_Choices to get the right number of entries.
702
703       Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
704
705    --  Start of processing for Analyze_Case_Statement
706
707    begin
708       Unblocked_Exit_Count := 0;
709       Exp := Expression (N);
710       Analyze_And_Resolve (Exp, Any_Discrete);
711       Check_Unset_Reference (Exp);
712       Exp_Type  := Etype (Exp);
713       Exp_Btype := Base_Type (Exp_Type);
714
715       --  The expression must be of a discrete type which must be determinable
716       --  independently of the context in which the expression occurs, but
717       --  using the fact that the expression must be of a discrete type.
718       --  Moreover, the type this expression must not be a character literal
719       --  (which is always ambiguous) or, for Ada-83, a generic formal type.
720
721       --  If error already reported by Resolve, nothing more to do
722
723       if Exp_Btype = Any_Discrete
724         or else Exp_Btype = Any_Type
725       then
726          return;
727
728       elsif Exp_Btype = Any_Character then
729          Error_Msg_N
730            ("character literal as case expression is ambiguous", Exp);
731          return;
732
733       elsif Ada_Version = Ada_83
734         and then (Is_Generic_Type (Exp_Btype)
735                     or else Is_Generic_Type (Root_Type (Exp_Btype)))
736       then
737          Error_Msg_N
738            ("(Ada 83) case expression cannot be of a generic type", Exp);
739          return;
740       end if;
741
742       --  If the case expression is a formal object of mode in out, then
743       --  treat it as having a nonstatic subtype by forcing use of the base
744       --  type (which has to get passed to Check_Case_Choices below).  Also
745       --  use base type when the case expression is parenthesized.
746
747       if Paren_Count (Exp) > 0
748         or else (Is_Entity_Name (Exp)
749                   and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
750       then
751          Exp_Type := Exp_Btype;
752       end if;
753
754       --  Call instantiated Analyze_Choices which does the rest of the work
755
756       Analyze_Choices
757         (N, Exp_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
758
759       if Exp_Type = Universal_Integer and then not Others_Present then
760          Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
761       end if;
762
763       --  If all our exits were blocked by unconditional transfers of control,
764       --  then the entire CASE statement acts as an unconditional transfer of
765       --  control, so treat it like one, and check unreachable code. Skip this
766       --  test if we had serious errors preventing any statement analysis.
767
768       if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
769          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
770          Check_Unreachable_Code (N);
771       else
772          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
773       end if;
774
775       if not Expander_Active
776         and then Compile_Time_Known_Value (Expression (N))
777         and then Serious_Errors_Detected = 0
778       then
779          declare
780             Chosen : constant Node_Id := Find_Static_Alternative (N);
781             Alt    : Node_Id;
782
783          begin
784             Alt := First (Alternatives (N));
785
786             while Present (Alt) loop
787                if Alt /= Chosen then
788                   Remove_Warning_Messages (Statements (Alt));
789                end if;
790
791                Next (Alt);
792             end loop;
793          end;
794       end if;
795    end Analyze_Case_Statement;
796
797    ----------------------------
798    -- Analyze_Exit_Statement --
799    ----------------------------
800
801    --  If the exit includes a name, it must be the name of a currently open
802    --  loop. Otherwise there must be an innermost open loop on the stack,
803    --  to which the statement implicitly refers.
804
805    procedure Analyze_Exit_Statement (N : Node_Id) is
806       Target   : constant Node_Id := Name (N);
807       Cond     : constant Node_Id := Condition (N);
808       Scope_Id : Entity_Id;
809       U_Name   : Entity_Id;
810       Kind     : Entity_Kind;
811
812    begin
813       if No (Cond) then
814          Check_Unreachable_Code (N);
815       end if;
816
817       if Present (Target) then
818          Analyze (Target);
819          U_Name := Entity (Target);
820
821          if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
822             Error_Msg_N ("invalid loop name in exit statement", N);
823             return;
824          else
825             Set_Has_Exit (U_Name);
826          end if;
827
828       else
829          U_Name := Empty;
830       end if;
831
832       for J in reverse 0 .. Scope_Stack.Last loop
833          Scope_Id := Scope_Stack.Table (J).Entity;
834          Kind := Ekind (Scope_Id);
835
836          if Kind = E_Loop
837            and then (No (Target) or else Scope_Id = U_Name) then
838             Set_Has_Exit (Scope_Id);
839             exit;
840
841          elsif Kind = E_Block or else Kind = E_Loop then
842             null;
843
844          else
845             Error_Msg_N
846               ("cannot exit from program unit or accept statement", N);
847             exit;
848          end if;
849       end loop;
850
851       --  Verify that if present the condition is a Boolean expression
852
853       if Present (Cond) then
854          Analyze_And_Resolve (Cond, Any_Boolean);
855          Check_Unset_Reference (Cond);
856       end if;
857    end Analyze_Exit_Statement;
858
859    ----------------------------
860    -- Analyze_Goto_Statement --
861    ----------------------------
862
863    procedure Analyze_Goto_Statement (N : Node_Id) is
864       Label       : constant Node_Id := Name (N);
865       Scope_Id    : Entity_Id;
866       Label_Scope : Entity_Id;
867
868    begin
869       Check_Unreachable_Code (N);
870
871       Analyze (Label);
872
873       if Entity (Label) = Any_Id then
874          return;
875
876       elsif Ekind (Entity (Label)) /= E_Label then
877          Error_Msg_N ("target of goto statement must be a label", Label);
878          return;
879
880       elsif not Reachable (Entity (Label)) then
881          Error_Msg_N ("target of goto statement is not reachable", Label);
882          return;
883       end if;
884
885       Label_Scope := Enclosing_Scope (Entity (Label));
886
887       for J in reverse 0 .. Scope_Stack.Last loop
888          Scope_Id := Scope_Stack.Table (J).Entity;
889
890          if Label_Scope = Scope_Id
891            or else (Ekind (Scope_Id) /= E_Block
892                      and then Ekind (Scope_Id) /= E_Loop)
893          then
894             if Scope_Id /= Label_Scope then
895                Error_Msg_N
896                  ("cannot exit from program unit or accept statement", N);
897             end if;
898
899             return;
900          end if;
901       end loop;
902
903       raise Program_Error;
904    end Analyze_Goto_Statement;
905
906    --------------------------
907    -- Analyze_If_Statement --
908    --------------------------
909
910    --  A special complication arises in the analysis of if statements.
911
912    --  The expander has circuitry to completely delete code that it
913    --  can tell will not be executed (as a result of compile time known
914    --  conditions). In the analyzer, we ensure that code that will be
915    --  deleted in this manner is analyzed but not expanded. This is
916    --  obviously more efficient, but more significantly, difficulties
917    --  arise if code is expanded and then eliminated (e.g. exception
918    --  table entries disappear). Similarly, itypes generated in deleted
919    --  code must be frozen from start, because the nodes on which they
920    --  depend will not be available at the freeze point.
921
922    procedure Analyze_If_Statement (N : Node_Id) is
923       E : Node_Id;
924
925       Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
926       --  Recursively save value of this global, will be restored on exit
927
928       Save_In_Deleted_Code : Boolean;
929
930       Del : Boolean := False;
931       --  This flag gets set True if a True condition has been found,
932       --  which means that remaining ELSE/ELSIF parts are deleted.
933
934       procedure Analyze_Cond_Then (Cnode : Node_Id);
935       --  This is applied to either the N_If_Statement node itself or
936       --  to an N_Elsif_Part node. It deals with analyzing the condition
937       --  and the THEN statements associated with it.
938
939       -----------------------
940       -- Analyze_Cond_Then --
941       -----------------------
942
943       procedure Analyze_Cond_Then (Cnode : Node_Id) is
944          Cond : constant Node_Id := Condition (Cnode);
945          Tstm : constant List_Id := Then_Statements (Cnode);
946
947       begin
948          Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
949          Analyze_And_Resolve (Cond, Any_Boolean);
950          Check_Unset_Reference (Cond);
951          Check_Possible_Current_Value_Condition (Cnode);
952
953          --  If already deleting, then just analyze then statements
954
955          if Del then
956             Analyze_Statements (Tstm);
957
958          --  Compile time known value, not deleting yet
959
960          elsif Compile_Time_Known_Value (Cond) then
961             Save_In_Deleted_Code := In_Deleted_Code;
962
963             --  If condition is True, then analyze the THEN statements
964             --  and set no expansion for ELSE and ELSIF parts.
965
966             if Is_True (Expr_Value (Cond)) then
967                Analyze_Statements (Tstm);
968                Del := True;
969                Expander_Mode_Save_And_Set (False);
970                In_Deleted_Code := True;
971
972             --  If condition is False, analyze THEN with expansion off
973
974             else -- Is_False (Expr_Value (Cond))
975                Expander_Mode_Save_And_Set (False);
976                In_Deleted_Code := True;
977                Analyze_Statements (Tstm);
978                Expander_Mode_Restore;
979                In_Deleted_Code := Save_In_Deleted_Code;
980             end if;
981
982          --  Not known at compile time, not deleting, normal analysis
983
984          else
985             Analyze_Statements (Tstm);
986          end if;
987       end Analyze_Cond_Then;
988
989    --  Start of Analyze_If_Statement
990
991    begin
992       --  Initialize exit count for else statements. If there is no else
993       --  part, this count will stay non-zero reflecting the fact that the
994       --  uncovered else case is an unblocked exit.
995
996       Unblocked_Exit_Count := 1;
997       Analyze_Cond_Then (N);
998
999       --  Now to analyze the elsif parts if any are present
1000
1001       if Present (Elsif_Parts (N)) then
1002          E := First (Elsif_Parts (N));
1003          while Present (E) loop
1004             Analyze_Cond_Then (E);
1005             Next (E);
1006          end loop;
1007       end if;
1008
1009       if Present (Else_Statements (N)) then
1010          Analyze_Statements (Else_Statements (N));
1011       end if;
1012
1013       --  If all our exits were blocked by unconditional transfers of control,
1014       --  then the entire IF statement acts as an unconditional transfer of
1015       --  control, so treat it like one, and check unreachable code.
1016
1017       if Unblocked_Exit_Count = 0 then
1018          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1019          Check_Unreachable_Code (N);
1020       else
1021          Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1022       end if;
1023
1024       if Del then
1025          Expander_Mode_Restore;
1026          In_Deleted_Code := Save_In_Deleted_Code;
1027       end if;
1028
1029       if not Expander_Active
1030         and then Compile_Time_Known_Value (Condition (N))
1031         and then Serious_Errors_Detected = 0
1032       then
1033          if Is_True (Expr_Value (Condition (N))) then
1034             Remove_Warning_Messages (Else_Statements (N));
1035
1036             if Present (Elsif_Parts (N)) then
1037                E := First (Elsif_Parts (N));
1038
1039                while Present (E) loop
1040                   Remove_Warning_Messages (Then_Statements (E));
1041                   Next (E);
1042                end loop;
1043             end if;
1044
1045          else
1046             Remove_Warning_Messages (Then_Statements (N));
1047          end if;
1048       end if;
1049    end Analyze_If_Statement;
1050
1051    ----------------------------------------
1052    -- Analyze_Implicit_Label_Declaration --
1053    ----------------------------------------
1054
1055    --  An implicit label declaration is generated in the innermost
1056    --  enclosing declarative part. This is done for labels as well as
1057    --  block and loop names.
1058
1059    --  Note: any changes in this routine may need to be reflected in
1060    --  Analyze_Label_Entity.
1061
1062    procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1063       Id : constant Node_Id := Defining_Identifier (N);
1064    begin
1065       Enter_Name          (Id);
1066       Set_Ekind           (Id, E_Label);
1067       Set_Etype           (Id, Standard_Void_Type);
1068       Set_Enclosing_Scope (Id, Current_Scope);
1069    end Analyze_Implicit_Label_Declaration;
1070
1071    ------------------------------
1072    -- Analyze_Iteration_Scheme --
1073    ------------------------------
1074
1075    procedure Analyze_Iteration_Scheme (N : Node_Id) is
1076       procedure Check_Controlled_Array_Attribute (DS : Node_Id);
1077       --  If the bounds are given by a 'Range reference on a function call
1078       --  that returns a controlled array, introduce an explicit declaration
1079       --  to capture the bounds, so that the function result can be finalized
1080       --  in timely fashion.
1081
1082       --------------------------------------
1083       -- Check_Controlled_Array_Attribute --
1084       --------------------------------------
1085
1086       procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
1087       begin
1088          if Nkind (DS) = N_Attribute_Reference
1089             and then Is_Entity_Name (Prefix (DS))
1090             and then Ekind (Entity (Prefix (DS))) = E_Function
1091             and then Is_Array_Type (Etype (Entity (Prefix (DS))))
1092             and then
1093               Is_Controlled (
1094                 Component_Type (Etype (Entity (Prefix (DS)))))
1095             and then Expander_Active
1096          then
1097             declare
1098                Loc  : constant Source_Ptr := Sloc (N);
1099                Arr  : constant Entity_Id :=
1100                         Etype (Entity (Prefix (DS)));
1101                Indx : constant Entity_Id :=
1102                         Base_Type (Etype (First_Index (Arr)));
1103                Subt : constant Entity_Id :=
1104                         Make_Defining_Identifier
1105                           (Loc, New_Internal_Name ('S'));
1106                Decl : Node_Id;
1107
1108             begin
1109                Decl :=
1110                  Make_Subtype_Declaration (Loc,
1111                    Defining_Identifier => Subt,
1112                    Subtype_Indication  =>
1113                       Make_Subtype_Indication (Loc,
1114                         Subtype_Mark  => New_Reference_To (Indx, Loc),
1115                         Constraint =>
1116                           Make_Range_Constraint (Loc,
1117                             Relocate_Node (DS))));
1118                Insert_Before (Parent (N), Decl);
1119                Analyze (Decl);
1120
1121                Rewrite (DS,
1122                   Make_Attribute_Reference (Loc,
1123                     Prefix => New_Reference_To (Subt, Loc),
1124                     Attribute_Name => Attribute_Name (DS)));
1125                Analyze (DS);
1126             end;
1127          end if;
1128       end Check_Controlled_Array_Attribute;
1129
1130    --  Start of processing for Analyze_Iteration_Scheme
1131
1132    begin
1133       --  For an infinite loop, there is no iteration scheme
1134
1135       if No (N) then
1136          return;
1137
1138       else
1139          declare
1140             Cond : constant Node_Id := Condition (N);
1141
1142          begin
1143             --  For WHILE loop, verify that the condition is a Boolean
1144             --  expression and resolve and check it.
1145
1146             if Present (Cond) then
1147                Analyze_And_Resolve (Cond, Any_Boolean);
1148                Check_Unset_Reference (Cond);
1149
1150             --  Else we have a FOR loop
1151
1152             else
1153                declare
1154                   LP : constant Node_Id   := Loop_Parameter_Specification (N);
1155                   Id : constant Entity_Id := Defining_Identifier (LP);
1156                   DS : constant Node_Id   := Discrete_Subtype_Definition (LP);
1157
1158                begin
1159                   Enter_Name (Id);
1160
1161                   --  We always consider the loop variable to be referenced,
1162                   --  since the loop may be used just for counting purposes.
1163
1164                   Generate_Reference (Id, N, ' ');
1165
1166                   --  Check for case of loop variable hiding a local
1167                   --  variable (used later on to give a nice warning
1168                   --  if the hidden variable is never assigned).
1169
1170                   declare
1171                      H : constant Entity_Id := Homonym (Id);
1172                   begin
1173                      if Present (H)
1174                        and then Enclosing_Dynamic_Scope (H) =
1175                                 Enclosing_Dynamic_Scope (Id)
1176                        and then Ekind (H) = E_Variable
1177                        and then Is_Discrete_Type (Etype (H))
1178                      then
1179                         Set_Hiding_Loop_Variable (H, Id);
1180                      end if;
1181                   end;
1182
1183                   --  Now analyze the subtype definition
1184
1185                   Analyze (DS);
1186
1187                   if DS = Error then
1188                      return;
1189                   end if;
1190
1191                   --  The subtype indication may denote the completion
1192                   --  of an incomplete type declaration.
1193
1194                   if Is_Entity_Name (DS)
1195                     and then Present (Entity (DS))
1196                     and then Is_Type (Entity (DS))
1197                     and then Ekind (Entity (DS)) = E_Incomplete_Type
1198                   then
1199                      Set_Entity (DS, Get_Full_View (Entity (DS)));
1200                      Set_Etype  (DS, Entity (DS));
1201                   end if;
1202
1203                   if not Is_Discrete_Type (Etype (DS)) then
1204                      Wrong_Type (DS, Any_Discrete);
1205                      Set_Etype (DS, Any_Type);
1206                   end if;
1207
1208                   Check_Controlled_Array_Attribute (DS);
1209                   Make_Index (DS, LP);
1210
1211                   Set_Ekind          (Id, E_Loop_Parameter);
1212                   Set_Etype          (Id, Etype (DS));
1213                   Set_Is_Known_Valid (Id, True);
1214
1215                   --  The loop is not a declarative part, so the only entity
1216                   --  declared "within" must be frozen explicitly.
1217
1218                   declare
1219                      Flist : constant List_Id := Freeze_Entity (Id, Sloc (N));
1220                   begin
1221                      if Is_Non_Empty_List (Flist) then
1222                         Insert_Actions (N, Flist);
1223                      end if;
1224                   end;
1225
1226                   --  Check for null or possibly null range and issue warning.
1227                   --  We suppress such messages in generic templates and
1228                   --  instances, because in practice they tend to be dubious
1229                   --  in these cases.
1230
1231                   if Nkind (DS) = N_Range
1232                     and then Comes_From_Source (N)
1233                   then
1234                      declare
1235                         L : constant Node_Id := Low_Bound  (DS);
1236                         H : constant Node_Id := High_Bound (DS);
1237
1238                         Llo : Uint;
1239                         Lhi : Uint;
1240                         LOK : Boolean;
1241                         Hlo : Uint;
1242                         Hhi : Uint;
1243                         HOK : Boolean;
1244
1245                      begin
1246                         Determine_Range (L, LOK, Llo, Lhi);
1247                         Determine_Range (H, HOK, Hlo, Hhi);
1248
1249                         --  If range of loop is null, issue warning
1250
1251                         if (LOK and HOK) and then Llo > Hhi then
1252
1253                            --  Suppress the warning if inside a generic
1254                            --  template or instance, since in practice
1255                            --  they tend to be dubious in these cases since
1256                            --  they can result from intended parametrization.
1257
1258                            if not Inside_A_Generic
1259                               and then not In_Instance
1260                            then
1261                               Error_Msg_N
1262                                 ("?loop range is null, loop will not execute",
1263                                  DS);
1264                            end if;
1265
1266                            --  Since we know the range of the loop is null,
1267                            --  set the appropriate flag to suppress any
1268                            --  warnings that would otherwise be issued in
1269                            --  the body of the loop that will not execute.
1270                            --  We do this even in the generic case, since
1271                            --  if it is dubious to warn on the null loop
1272                            --  itself, it is certainly dubious to warn for
1273                            --  conditions that occur inside it!
1274
1275                            Set_Is_Null_Loop (Parent (N));
1276
1277                         --  The other case for a warning is a reverse loop
1278                         --  where the upper bound is the integer literal
1279                         --  zero or one, and the lower bound can be positive.
1280
1281                         --  For example, we have
1282
1283                         --     for J in reverse N .. 1 loop
1284
1285                         --  In practice, this is very likely to be a case
1286                         --  of reversing the bounds incorrectly in the range.
1287
1288                         elsif Reverse_Present (LP)
1289                           and then Nkind (H) = N_Integer_Literal
1290                           and then (Intval (H) = Uint_0
1291                                       or else
1292                                     Intval (H) = Uint_1)
1293                           and then Lhi > Hhi
1294                         then
1295                            Error_Msg_N ("?loop range may be null", DS);
1296                         end if;
1297                      end;
1298                   end if;
1299                end;
1300             end if;
1301          end;
1302       end if;
1303    end Analyze_Iteration_Scheme;
1304
1305    -------------------
1306    -- Analyze_Label --
1307    -------------------
1308
1309    --  Note: the semantic work required for analyzing labels (setting them as
1310    --  reachable) was done in a prepass through the statements in the block,
1311    --  so that forward gotos would be properly handled. See Analyze_Statements
1312    --  for further details. The only processing required here is to deal with
1313    --  optimizations that depend on an assumption of sequential control flow,
1314    --  since of course the occurrence of a label breaks this assumption.
1315
1316    procedure Analyze_Label (N : Node_Id) is
1317       pragma Warnings (Off, N);
1318    begin
1319       Kill_Current_Values;
1320    end Analyze_Label;
1321
1322    --------------------------
1323    -- Analyze_Label_Entity --
1324    --------------------------
1325
1326    procedure Analyze_Label_Entity (E : Entity_Id) is
1327    begin
1328       Set_Ekind           (E, E_Label);
1329       Set_Etype           (E, Standard_Void_Type);
1330       Set_Enclosing_Scope (E, Current_Scope);
1331       Set_Reachable       (E, True);
1332    end Analyze_Label_Entity;
1333
1334    ----------------------------
1335    -- Analyze_Loop_Statement --
1336    ----------------------------
1337
1338    procedure Analyze_Loop_Statement (N : Node_Id) is
1339       Id  : constant Node_Id := Identifier (N);
1340       Ent : Entity_Id;
1341
1342    begin
1343       if Present (Id) then
1344
1345          --  Make name visible, e.g. for use in exit statements. Loop
1346          --  labels are always considered to be referenced.
1347
1348          Analyze (Id);
1349          Ent := Entity (Id);
1350          Generate_Reference  (Ent, N, ' ');
1351          Generate_Definition (Ent);
1352
1353          --  If we found a label, mark its type. If not, ignore it, since it
1354          --  means we have a conflicting declaration, which would already have
1355          --  been diagnosed at declaration time. Set Label_Construct of the
1356          --  implicit label declaration, which is not created by the parser
1357          --  for generic units.
1358
1359          if Ekind (Ent) = E_Label then
1360             Set_Ekind (Ent, E_Loop);
1361
1362             if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1363                Set_Label_Construct (Parent (Ent), N);
1364             end if;
1365          end if;
1366
1367       --  Case of no identifier present
1368
1369       else
1370          Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
1371          Set_Etype (Ent,  Standard_Void_Type);
1372          Set_Parent (Ent, N);
1373       end if;
1374
1375       --  Kill current values on entry to loop, since statements in body
1376       --  of loop may have been executed before the loop is entered.
1377       --  Similarly we kill values after the loop, since we do not know
1378       --  that the body of the loop was executed.
1379
1380       Kill_Current_Values;
1381       New_Scope (Ent);
1382       Analyze_Iteration_Scheme (Iteration_Scheme (N));
1383       Analyze_Statements (Statements (N));
1384       Process_End_Label (N, 'e', Ent);
1385       End_Scope;
1386       Kill_Current_Values;
1387    end Analyze_Loop_Statement;
1388
1389    ----------------------------
1390    -- Analyze_Null_Statement --
1391    ----------------------------
1392
1393    --  Note: the semantics of the null statement is implemented by a single
1394    --  null statement, too bad everything isn't as simple as this!
1395
1396    procedure Analyze_Null_Statement (N : Node_Id) is
1397       pragma Warnings (Off, N);
1398    begin
1399       null;
1400    end Analyze_Null_Statement;
1401
1402    ------------------------
1403    -- Analyze_Statements --
1404    ------------------------
1405
1406    procedure Analyze_Statements (L : List_Id) is
1407       S   : Node_Id;
1408       Lab : Entity_Id;
1409
1410    begin
1411       --  The labels declared in the statement list are reachable from
1412       --  statements in the list. We do this as a prepass so that any
1413       --  goto statement will be properly flagged if its target is not
1414       --  reachable. This is not required, but is nice behavior!
1415
1416       S := First (L);
1417       while Present (S) loop
1418          if Nkind (S) = N_Label then
1419             Analyze (Identifier (S));
1420             Lab := Entity (Identifier (S));
1421
1422             --  If we found a label mark it as reachable.
1423
1424             if Ekind (Lab) = E_Label then
1425                Generate_Definition (Lab);
1426                Set_Reachable (Lab);
1427
1428                if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
1429                   Set_Label_Construct (Parent (Lab), S);
1430                end if;
1431
1432             --  If we failed to find a label, it means the implicit declaration
1433             --  of the label was hidden.  A for-loop parameter can do this to
1434             --  a label with the same name inside the loop, since the implicit
1435             --  label declaration is in the innermost enclosing body or block
1436             --  statement.
1437
1438             else
1439                Error_Msg_Sloc := Sloc (Lab);
1440                Error_Msg_N
1441                  ("implicit label declaration for & is hidden#",
1442                   Identifier (S));
1443             end if;
1444          end if;
1445
1446          Next (S);
1447       end loop;
1448
1449       --  Perform semantic analysis on all statements
1450
1451       Conditional_Statements_Begin;
1452
1453       S := First (L);
1454       while Present (S) loop
1455          Analyze (S);
1456          Next (S);
1457       end loop;
1458
1459       Conditional_Statements_End;
1460
1461       --  Make labels unreachable. Visibility is not sufficient, because
1462       --  labels in one if-branch for example are not reachable from the
1463       --  other branch, even though their declarations are in the enclosing
1464       --  declarative part.
1465
1466       S := First (L);
1467       while Present (S) loop
1468          if Nkind (S) = N_Label then
1469             Set_Reachable (Entity (Identifier (S)), False);
1470          end if;
1471
1472          Next (S);
1473       end loop;
1474    end Analyze_Statements;
1475
1476    --------------------------------------------
1477    -- Check_Possible_Current_Value_Condition --
1478    --------------------------------------------
1479
1480    procedure Check_Possible_Current_Value_Condition (Cnode : Node_Id) is
1481       Cond : Node_Id;
1482
1483    begin
1484       --  Loop to deal with (ignore for now) any NOT operators present
1485
1486       Cond := Condition (Cnode);
1487       while Nkind (Cond) = N_Op_Not loop
1488          Cond := Right_Opnd (Cond);
1489       end loop;
1490
1491       --  Check possible relational operator
1492
1493       if Nkind (Cond) = N_Op_Eq
1494            or else
1495          Nkind (Cond) = N_Op_Ne
1496            or else
1497          Nkind (Cond) = N_Op_Ge
1498            or else
1499          Nkind (Cond) = N_Op_Le
1500            or else
1501          Nkind (Cond) = N_Op_Gt
1502            or else
1503          Nkind (Cond) = N_Op_Lt
1504       then
1505          if Compile_Time_Known_Value (Right_Opnd (Cond))
1506            and then Nkind (Left_Opnd (Cond)) = N_Identifier
1507          then
1508             declare
1509                Ent : constant Entity_Id := Entity (Left_Opnd (Cond));
1510
1511             begin
1512                if Ekind (Ent) = E_Variable
1513                     or else
1514                   Ekind (Ent) = E_Constant
1515                     or else
1516                   Is_Formal (Ent)
1517                     or else
1518                   Ekind (Ent) = E_Loop_Parameter
1519                then
1520                   --  Here we have a case where the Current_Value field
1521                   --  may need to be set. We set it if it is not already
1522                   --  set to a compile time expression value.
1523
1524                   --  Note that this represents a decision that one
1525                   --  condition blots out another previous one. That's
1526                   --  certainly right if they occur at the same level.
1527                   --  If the second one is nested, then the decision is
1528                   --  neither right nor wrong (it would be equally OK
1529                   --  to leave the outer one in place, or take the new
1530                   --  inner one. Really we should record both, but our
1531                   --  data structures are not that elaborate.
1532
1533                   if Nkind (Current_Value (Ent)) not in N_Subexpr then
1534                      Set_Current_Value (Ent, Cnode);
1535                   end if;
1536                end if;
1537             end;
1538          end if;
1539       end if;
1540    end Check_Possible_Current_Value_Condition;
1541
1542    ----------------------------
1543    -- Check_Unreachable_Code --
1544    ----------------------------
1545
1546    procedure Check_Unreachable_Code (N : Node_Id) is
1547       Error_Loc : Source_Ptr;
1548       P         : Node_Id;
1549
1550    begin
1551       if Is_List_Member (N)
1552         and then Comes_From_Source (N)
1553       then
1554          declare
1555             Nxt : Node_Id;
1556
1557          begin
1558             Nxt := Original_Node (Next (N));
1559
1560             if Present (Nxt)
1561               and then Comes_From_Source (Nxt)
1562               and then Is_Statement (Nxt)
1563             then
1564                --  Special very annoying exception. If we have a return that
1565                --  follows a raise, then we allow it without a warning, since
1566                --  the Ada RM annoyingly requires a useless return here!
1567
1568                if Nkind (Original_Node (N)) /= N_Raise_Statement
1569                  or else Nkind (Nxt) /= N_Return_Statement
1570                then
1571                   --  The rather strange shenanigans with the warning message
1572                   --  here reflects the fact that Kill_Dead_Code is very good
1573                   --  at removing warnings in deleted code, and this is one
1574                   --  warning we would prefer NOT to have removed :-)
1575
1576                   Error_Loc := Sloc (Nxt);
1577
1578                   --  If we have unreachable code, analyze and remove the
1579                   --  unreachable code, since it is useless and we don't
1580                   --  want to generate junk warnings.
1581
1582                   --  We skip this step if we are not in code generation mode.
1583                   --  This is the one case where we remove dead code in the
1584                   --  semantics as opposed to the expander, and we do not want
1585                   --  to remove code if we are not in code generation mode,
1586                   --  since this messes up the ASIS trees.
1587
1588                   --  Note that one might react by moving the whole circuit to
1589                   --  exp_ch5, but then we lose the warning in -gnatc mode.
1590
1591                   if Operating_Mode = Generate_Code then
1592                      loop
1593                         Nxt := Next (N);
1594
1595                         --  Quit deleting when we have nothing more to delete
1596                         --  or if we hit a label (since someone could transfer
1597                         --  control to a label, so we should not delete it).
1598
1599                         exit when No (Nxt) or else Nkind (Nxt) = N_Label;
1600
1601                         --  Statement/declaration is to be deleted
1602
1603                         Analyze (Nxt);
1604                         Remove (Nxt);
1605                         Kill_Dead_Code (Nxt);
1606                      end loop;
1607                   end if;
1608
1609                   --  Now issue the warning
1610
1611                   Error_Msg ("?unreachable code", Error_Loc);
1612                end if;
1613
1614             --  If the unconditional transfer of control instruction is
1615             --  the last statement of a sequence, then see if our parent
1616             --  is an IF statement, and if so adjust the unblocked exit
1617             --  count of the if statement to reflect the fact that this
1618             --  branch of the if is indeed blocked by a transfer of control.
1619
1620             else
1621                P := Parent (N);
1622
1623                if Nkind (P) = N_If_Statement then
1624                   null;
1625
1626                elsif Nkind (P) = N_Elsif_Part then
1627                   P := Parent (P);
1628                   pragma Assert (Nkind (P) = N_If_Statement);
1629
1630                elsif Nkind (P) = N_Case_Statement_Alternative then
1631                   P := Parent (P);
1632                   pragma Assert (Nkind (P) = N_Case_Statement);
1633
1634                else
1635                   return;
1636                end if;
1637
1638                Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
1639             end if;
1640          end;
1641       end if;
1642    end Check_Unreachable_Code;
1643
1644 end Sem_Ch5;