[multiple changes]
[gcc.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-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Ghost; use Ghost;
36 with Lib; use Lib;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Elab; use Sem_Elab;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Type; use Sem_Type;
57 with Sem_Util; use Sem_Util;
58 with Sem_Warn; use Sem_Warn;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Sinfo; use Sinfo;
62 with Targparm; use Targparm;
63 with Tbuild; use Tbuild;
64 with Uintp; use Uintp;
65
66 package body Sem_Ch5 is
67
68 Unblocked_Exit_Count : Nat := 0;
69 -- This variable is used when processing if statements, case statements,
70 -- and block statements. It counts the number of exit points that are not
71 -- blocked by unconditional transfer instructions: for IF and CASE, these
72 -- are the branches of the conditional; for a block, they are the statement
73 -- sequence of the block, and the statement sequences of any exception
74 -- handlers that are part of the block. When processing is complete, if
75 -- this count is zero, it means that control cannot fall through the IF,
76 -- CASE or block statement. This is used for the generation of warning
77 -- messages. This variable is recursively saved on entry to processing the
78 -- construct, and restored on exit.
79
80 procedure Preanalyze_Range (R_Copy : Node_Id);
81 -- Determine expected type of range or domain of iteration of Ada 2012
82 -- loop by analyzing separate copy. Do the analysis and resolution of the
83 -- copy of the bound(s) with expansion disabled, to prevent the generation
84 -- of finalization actions. This prevents memory leaks when the bounds
85 -- contain calls to functions returning controlled arrays or when the
86 -- domain of iteration is a container.
87
88 ------------------------
89 -- Analyze_Assignment --
90 ------------------------
91
92 procedure Analyze_Assignment (N : Node_Id) is
93 Lhs : constant Node_Id := Name (N);
94 Rhs : constant Node_Id := Expression (N);
95 T1 : Entity_Id;
96 T2 : Entity_Id;
97 Decl : Node_Id;
98
99 procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
100 -- N is the node for the left hand side of an assignment, and it is not
101 -- a variable. This routine issues an appropriate diagnostic.
102
103 procedure Kill_Lhs;
104 -- This is called to kill current value settings of a simple variable
105 -- on the left hand side. We call it if we find any error in analyzing
106 -- the assignment, and at the end of processing before setting any new
107 -- current values in place.
108
109 procedure Set_Assignment_Type
110 (Opnd : Node_Id;
111 Opnd_Type : in out Entity_Id);
112 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
113 -- nominal subtype. This procedure is used to deal with cases where the
114 -- nominal subtype must be replaced by the actual subtype.
115
116 -------------------------------
117 -- Diagnose_Non_Variable_Lhs --
118 -------------------------------
119
120 procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
121 begin
122 -- Not worth posting another error if left hand side already flagged
123 -- as being illegal in some respect.
124
125 if Error_Posted (N) then
126 return;
127
128 -- Some special bad cases of entity names
129
130 elsif Is_Entity_Name (N) then
131 declare
132 Ent : constant Entity_Id := Entity (N);
133
134 begin
135 if Ekind (Ent) = E_In_Parameter then
136 Error_Msg_N
137 ("assignment to IN mode parameter not allowed", N);
138 return;
139
140 -- Renamings of protected private components are turned into
141 -- constants when compiling a protected function. In the case
142 -- of single protected types, the private component appears
143 -- directly.
144
145 elsif (Is_Prival (Ent)
146 and then
147 (Ekind (Current_Scope) = E_Function
148 or else Ekind (Enclosing_Dynamic_Scope
149 (Current_Scope)) = E_Function))
150 or else
151 (Ekind (Ent) = E_Component
152 and then Is_Protected_Type (Scope (Ent)))
153 then
154 Error_Msg_N
155 ("protected function cannot modify protected object", N);
156 return;
157
158 elsif Ekind (Ent) = E_Loop_Parameter then
159 Error_Msg_N ("assignment to loop parameter not allowed", N);
160 return;
161 end if;
162 end;
163
164 -- For indexed components, test prefix if it is in array. We do not
165 -- want to recurse for cases where the prefix is a pointer, since we
166 -- may get a message confusing the pointer and what it references.
167
168 elsif Nkind (N) = N_Indexed_Component
169 and then Is_Array_Type (Etype (Prefix (N)))
170 then
171 Diagnose_Non_Variable_Lhs (Prefix (N));
172 return;
173
174 -- Another special case for assignment to discriminant
175
176 elsif Nkind (N) = N_Selected_Component then
177 if Present (Entity (Selector_Name (N)))
178 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
179 then
180 Error_Msg_N ("assignment to discriminant not allowed", N);
181 return;
182
183 -- For selection from record, diagnose prefix, but note that again
184 -- we only do this for a record, not e.g. for a pointer.
185
186 elsif Is_Record_Type (Etype (Prefix (N))) then
187 Diagnose_Non_Variable_Lhs (Prefix (N));
188 return;
189 end if;
190 end if;
191
192 -- If we fall through, we have no special message to issue
193
194 Error_Msg_N ("left hand side of assignment must be a variable", N);
195 end Diagnose_Non_Variable_Lhs;
196
197 --------------
198 -- Kill_Lhs --
199 --------------
200
201 procedure Kill_Lhs is
202 begin
203 if Is_Entity_Name (Lhs) then
204 declare
205 Ent : constant Entity_Id := Entity (Lhs);
206 begin
207 if Present (Ent) then
208 Kill_Current_Values (Ent);
209 end if;
210 end;
211 end if;
212 end Kill_Lhs;
213
214 -------------------------
215 -- Set_Assignment_Type --
216 -------------------------
217
218 procedure Set_Assignment_Type
219 (Opnd : Node_Id;
220 Opnd_Type : in out Entity_Id)
221 is
222 begin
223 Require_Entity (Opnd);
224
225 -- If the assignment operand is an in-out or out parameter, then we
226 -- get the actual subtype (needed for the unconstrained case). If the
227 -- operand is the actual in an entry declaration, then within the
228 -- accept statement it is replaced with a local renaming, which may
229 -- also have an actual subtype.
230
231 if Is_Entity_Name (Opnd)
232 and then (Ekind (Entity (Opnd)) = E_Out_Parameter
233 or else Ekind_In (Entity (Opnd),
234 E_In_Out_Parameter,
235 E_Generic_In_Out_Parameter)
236 or else
237 (Ekind (Entity (Opnd)) = E_Variable
238 and then Nkind (Parent (Entity (Opnd))) =
239 N_Object_Renaming_Declaration
240 and then Nkind (Parent (Parent (Entity (Opnd)))) =
241 N_Accept_Statement))
242 then
243 Opnd_Type := Get_Actual_Subtype (Opnd);
244
245 -- If assignment operand is a component reference, then we get the
246 -- actual subtype of the component for the unconstrained case.
247
248 elsif Nkind_In (Opnd, N_Selected_Component, N_Explicit_Dereference)
249 and then not Is_Unchecked_Union (Opnd_Type)
250 then
251 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
252
253 if Present (Decl) then
254 Insert_Action (N, Decl);
255 Mark_Rewrite_Insertion (Decl);
256 Analyze (Decl);
257 Opnd_Type := Defining_Identifier (Decl);
258 Set_Etype (Opnd, Opnd_Type);
259 Freeze_Itype (Opnd_Type, N);
260
261 elsif Is_Constrained (Etype (Opnd)) then
262 Opnd_Type := Etype (Opnd);
263 end if;
264
265 -- For slice, use the constrained subtype created for the slice
266
267 elsif Nkind (Opnd) = N_Slice then
268 Opnd_Type := Etype (Opnd);
269 end if;
270 end Set_Assignment_Type;
271
272 -- Start of processing for Analyze_Assignment
273
274 begin
275 Mark_Coextensions (N, Rhs);
276
277 -- Analyze the target of the assignment first in case the expression
278 -- contains references to Ghost entities. The checks that verify the
279 -- proper use of a Ghost entity need to know the enclosing context.
280
281 Analyze (Lhs);
282
283 -- The left hand side of an assignment may reference an entity subject
284 -- to pragma Ghost with policy Ignore. Set the mode now to ensure that
285 -- any nodes generated during analysis and expansion are properly
286 -- flagged as ignored Ghost.
287
288 Set_Ghost_Mode (N);
289 Analyze (Rhs);
290
291 -- Ensure that we never do an assignment on a variable marked as
292 -- as Safe_To_Reevaluate.
293
294 pragma Assert (not Is_Entity_Name (Lhs)
295 or else Ekind (Entity (Lhs)) /= E_Variable
296 or else not Is_Safe_To_Reevaluate (Entity (Lhs)));
297
298 -- Start type analysis for assignment
299
300 T1 := Etype (Lhs);
301
302 -- In the most general case, both Lhs and Rhs can be overloaded, and we
303 -- must compute the intersection of the possible types on each side.
304
305 if Is_Overloaded (Lhs) then
306 declare
307 I : Interp_Index;
308 It : Interp;
309
310 begin
311 T1 := Any_Type;
312 Get_First_Interp (Lhs, I, It);
313
314 while Present (It.Typ) loop
315 if Has_Compatible_Type (Rhs, It.Typ) then
316 if T1 /= Any_Type then
317
318 -- An explicit dereference is overloaded if the prefix
319 -- is. Try to remove the ambiguity on the prefix, the
320 -- error will be posted there if the ambiguity is real.
321
322 if Nkind (Lhs) = N_Explicit_Dereference then
323 declare
324 PI : Interp_Index;
325 PI1 : Interp_Index := 0;
326 PIt : Interp;
327 Found : Boolean;
328
329 begin
330 Found := False;
331 Get_First_Interp (Prefix (Lhs), PI, PIt);
332
333 while Present (PIt.Typ) loop
334 if Is_Access_Type (PIt.Typ)
335 and then Has_Compatible_Type
336 (Rhs, Designated_Type (PIt.Typ))
337 then
338 if Found then
339 PIt :=
340 Disambiguate (Prefix (Lhs),
341 PI1, PI, Any_Type);
342
343 if PIt = No_Interp then
344 Error_Msg_N
345 ("ambiguous left-hand side"
346 & " in assignment", Lhs);
347 exit;
348 else
349 Resolve (Prefix (Lhs), PIt.Typ);
350 end if;
351
352 exit;
353 else
354 Found := True;
355 PI1 := PI;
356 end if;
357 end if;
358
359 Get_Next_Interp (PI, PIt);
360 end loop;
361 end;
362
363 else
364 Error_Msg_N
365 ("ambiguous left-hand side in assignment", Lhs);
366 exit;
367 end if;
368 else
369 T1 := It.Typ;
370 end if;
371 end if;
372
373 Get_Next_Interp (I, It);
374 end loop;
375 end;
376
377 if T1 = Any_Type then
378 Error_Msg_N
379 ("no valid types for left-hand side for assignment", Lhs);
380 Kill_Lhs;
381 return;
382 end if;
383 end if;
384
385 -- The resulting assignment type is T1, so now we will resolve the left
386 -- hand side of the assignment using this determined type.
387
388 Resolve (Lhs, T1);
389
390 -- Cases where Lhs is not a variable
391
392 if not Is_Variable (Lhs) then
393
394 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
395 -- protected object.
396
397 declare
398 Ent : Entity_Id;
399 S : Entity_Id;
400
401 begin
402 if Ada_Version >= Ada_2005 then
403
404 -- Handle chains of renamings
405
406 Ent := Lhs;
407 while Nkind (Ent) in N_Has_Entity
408 and then Present (Entity (Ent))
409 and then Present (Renamed_Object (Entity (Ent)))
410 loop
411 Ent := Renamed_Object (Entity (Ent));
412 end loop;
413
414 if (Nkind (Ent) = N_Attribute_Reference
415 and then Attribute_Name (Ent) = Name_Priority)
416
417 -- Renamings of the attribute Priority applied to protected
418 -- objects have been previously expanded into calls to the
419 -- Get_Ceiling run-time subprogram.
420
421 or else
422 (Nkind (Ent) = N_Function_Call
423 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
424 or else
425 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
426 then
427 -- The enclosing subprogram cannot be a protected function
428
429 S := Current_Scope;
430 while not (Is_Subprogram (S)
431 and then Convention (S) = Convention_Protected)
432 and then S /= Standard_Standard
433 loop
434 S := Scope (S);
435 end loop;
436
437 if Ekind (S) = E_Function
438 and then Convention (S) = Convention_Protected
439 then
440 Error_Msg_N
441 ("protected function cannot modify protected object",
442 Lhs);
443 end if;
444
445 -- Changes of the ceiling priority of the protected object
446 -- are only effective if the Ceiling_Locking policy is in
447 -- effect (AARM D.5.2 (5/2)).
448
449 if Locking_Policy /= 'C' then
450 Error_Msg_N ("assignment to the attribute PRIORITY has " &
451 "no effect??", Lhs);
452 Error_Msg_N ("\since no Locking_Policy has been " &
453 "specified??", Lhs);
454 end if;
455
456 return;
457 end if;
458 end if;
459 end;
460
461 Diagnose_Non_Variable_Lhs (Lhs);
462 return;
463
464 -- Error of assigning to limited type. We do however allow this in
465 -- certain cases where the front end generates the assignments.
466
467 elsif Is_Limited_Type (T1)
468 and then not Assignment_OK (Lhs)
469 and then not Assignment_OK (Original_Node (Lhs))
470 and then not Is_Value_Type (T1)
471 then
472 -- CPP constructors can only be called in declarations
473
474 if Is_CPP_Constructor_Call (Rhs) then
475 Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
476 else
477 Error_Msg_N
478 ("left hand of assignment must not be limited type", Lhs);
479 Explain_Limited_Type (T1, Lhs);
480 end if;
481 return;
482
483 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
484 -- abstract. This is only checked when the assignment Comes_From_Source,
485 -- because in some cases the expander generates such assignments (such
486 -- in the _assign operation for an abstract type).
487
488 elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
489 Error_Msg_N
490 ("target of assignment operation must not be abstract", Lhs);
491 end if;
492
493 -- Resolution may have updated the subtype, in case the left-hand side
494 -- is a private protected component. Use the correct subtype to avoid
495 -- scoping issues in the back-end.
496
497 T1 := Etype (Lhs);
498
499 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
500 -- type. For example:
501
502 -- limited with P;
503 -- package Pkg is
504 -- type Acc is access P.T;
505 -- end Pkg;
506
507 -- with Pkg; use Acc;
508 -- procedure Example is
509 -- A, B : Acc;
510 -- begin
511 -- A.all := B.all; -- ERROR
512 -- end Example;
513
514 if Nkind (Lhs) = N_Explicit_Dereference
515 and then Ekind (T1) = E_Incomplete_Type
516 then
517 Error_Msg_N ("invalid use of incomplete type", Lhs);
518 Kill_Lhs;
519 return;
520 end if;
521
522 -- Now we can complete the resolution of the right hand side
523
524 Set_Assignment_Type (Lhs, T1);
525 Resolve (Rhs, T1);
526
527 -- This is the point at which we check for an unset reference
528
529 Check_Unset_Reference (Rhs);
530 Check_Unprotected_Access (Lhs, Rhs);
531
532 -- Remaining steps are skipped if Rhs was syntactically in error
533
534 if Rhs = Error then
535 Kill_Lhs;
536 return;
537 end if;
538
539 T2 := Etype (Rhs);
540
541 if not Covers (T1, T2) then
542 Wrong_Type (Rhs, Etype (Lhs));
543 Kill_Lhs;
544 return;
545 end if;
546
547 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
548 -- types, use the non-limited view if available
549
550 if Nkind (Rhs) = N_Explicit_Dereference
551 and then Is_Tagged_Type (T2)
552 and then Has_Non_Limited_View (T2)
553 then
554 T2 := Non_Limited_View (T2);
555 end if;
556
557 Set_Assignment_Type (Rhs, T2);
558
559 if Total_Errors_Detected /= 0 then
560 if No (T1) then
561 T1 := Any_Type;
562 end if;
563
564 if No (T2) then
565 T2 := Any_Type;
566 end if;
567 end if;
568
569 if T1 = Any_Type or else T2 = Any_Type then
570 Kill_Lhs;
571 return;
572 end if;
573
574 -- If the rhs is class-wide or dynamically tagged, then require the lhs
575 -- to be class-wide. The case where the rhs is a dynamically tagged call
576 -- to a dispatching operation with a controlling access result is
577 -- excluded from this check, since the target has an access type (and
578 -- no tag propagation occurs in that case).
579
580 if (Is_Class_Wide_Type (T2)
581 or else (Is_Dynamically_Tagged (Rhs)
582 and then not Is_Access_Type (T1)))
583 and then not Is_Class_Wide_Type (T1)
584 then
585 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
586
587 elsif Is_Class_Wide_Type (T1)
588 and then not Is_Class_Wide_Type (T2)
589 and then not Is_Tag_Indeterminate (Rhs)
590 and then not Is_Dynamically_Tagged (Rhs)
591 then
592 Error_Msg_N ("dynamically tagged expression required!", Rhs);
593 end if;
594
595 -- Propagate the tag from a class-wide target to the rhs when the rhs
596 -- is a tag-indeterminate call.
597
598 if Is_Tag_Indeterminate (Rhs) then
599 if Is_Class_Wide_Type (T1) then
600 Propagate_Tag (Lhs, Rhs);
601
602 elsif Nkind (Rhs) = N_Function_Call
603 and then Is_Entity_Name (Name (Rhs))
604 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
605 then
606 Error_Msg_N
607 ("call to abstract function must be dispatching", Name (Rhs));
608
609 elsif Nkind (Rhs) = N_Qualified_Expression
610 and then Nkind (Expression (Rhs)) = N_Function_Call
611 and then Is_Entity_Name (Name (Expression (Rhs)))
612 and then
613 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
614 then
615 Error_Msg_N
616 ("call to abstract function must be dispatching",
617 Name (Expression (Rhs)));
618 end if;
619 end if;
620
621 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
622 -- apply an implicit conversion of the rhs to that type to force
623 -- appropriate static and run-time accessibility checks. This applies
624 -- as well to anonymous access-to-subprogram types that are component
625 -- subtypes or formal parameters.
626
627 if Ada_Version >= Ada_2005 and then Is_Access_Type (T1) then
628 if Is_Local_Anonymous_Access (T1)
629 or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
630
631 -- Handle assignment to an Ada 2012 stand-alone object
632 -- of an anonymous access type.
633
634 or else (Ekind (T1) = E_Anonymous_Access_Type
635 and then Nkind (Associated_Node_For_Itype (T1)) =
636 N_Object_Declaration)
637
638 then
639 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
640 Analyze_And_Resolve (Rhs, T1);
641 end if;
642 end if;
643
644 -- Ada 2005 (AI-231): Assignment to not null variable
645
646 if Ada_Version >= Ada_2005
647 and then Can_Never_Be_Null (T1)
648 and then not Assignment_OK (Lhs)
649 then
650 -- Case where we know the right hand side is null
651
652 if Known_Null (Rhs) then
653 Apply_Compile_Time_Constraint_Error
654 (N => Rhs,
655 Msg =>
656 "(Ada 2005) null not allowed in null-excluding objects??",
657 Reason => CE_Null_Not_Allowed);
658
659 -- We still mark this as a possible modification, that's necessary
660 -- to reset Is_True_Constant, and desirable for xref purposes.
661
662 Note_Possible_Modification (Lhs, Sure => True);
663 return;
664
665 -- If we know the right hand side is non-null, then we convert to the
666 -- target type, since we don't need a run time check in that case.
667
668 elsif not Can_Never_Be_Null (T2) then
669 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
670 Analyze_And_Resolve (Rhs, T1);
671 end if;
672 end if;
673
674 if Is_Scalar_Type (T1) then
675 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
676
677 -- For array types, verify that lengths match. If the right hand side
678 -- is a function call that has been inlined, the assignment has been
679 -- rewritten as a block, and the constraint check will be applied to the
680 -- assignment within the block.
681
682 elsif Is_Array_Type (T1)
683 and then (Nkind (Rhs) /= N_Type_Conversion
684 or else Is_Constrained (Etype (Rhs)))
685 and then (Nkind (Rhs) /= N_Function_Call
686 or else Nkind (N) /= N_Block_Statement)
687 then
688 -- Assignment verifies that the length of the Lsh and Rhs are equal,
689 -- but of course the indexes do not have to match. If the right-hand
690 -- side is a type conversion to an unconstrained type, a length check
691 -- is performed on the expression itself during expansion. In rare
692 -- cases, the redundant length check is computed on an index type
693 -- with a different representation, triggering incorrect code in the
694 -- back end.
695
696 Apply_Length_Check (Rhs, Etype (Lhs));
697
698 else
699 -- Discriminant checks are applied in the course of expansion
700
701 null;
702 end if;
703
704 -- Note: modifications of the Lhs may only be recorded after
705 -- checks have been applied.
706
707 Note_Possible_Modification (Lhs, Sure => True);
708
709 -- ??? a real accessibility check is needed when ???
710
711 -- Post warning for redundant assignment or variable to itself
712
713 if Warn_On_Redundant_Constructs
714
715 -- We only warn for source constructs
716
717 and then Comes_From_Source (N)
718
719 -- Where the object is the same on both sides
720
721 and then Same_Object (Lhs, Original_Node (Rhs))
722
723 -- But exclude the case where the right side was an operation that
724 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
725 -- don't want to warn in such a case, since it is reasonable to write
726 -- such expressions especially when K is defined symbolically in some
727 -- other package.
728
729 and then Nkind (Original_Node (Rhs)) not in N_Op
730 then
731 if Nkind (Lhs) in N_Has_Entity then
732 Error_Msg_NE -- CODEFIX
733 ("?r?useless assignment of & to itself!", N, Entity (Lhs));
734 else
735 Error_Msg_N -- CODEFIX
736 ("?r?useless assignment of object to itself!", N);
737 end if;
738 end if;
739
740 -- Check for non-allowed composite assignment
741
742 if not Support_Composite_Assign_On_Target
743 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
744 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
745 then
746 Error_Msg_CRT ("composite assignment", N);
747 end if;
748
749 -- Check elaboration warning for left side if not in elab code
750
751 if not In_Subprogram_Or_Concurrent_Unit then
752 Check_Elab_Assign (Lhs);
753 end if;
754
755 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
756 -- assignment is a source assignment in the extended main source unit.
757 -- We are not interested in any reference information outside this
758 -- context, or in compiler generated assignment statements.
759
760 if Comes_From_Source (N)
761 and then In_Extended_Main_Source_Unit (Lhs)
762 then
763 Set_Referenced_Modified (Lhs, Out_Param => False);
764 end if;
765
766 -- RM 7.3.2 (12/3) An assignment to a view conversion (from a type
767 -- to one of its ancestors) requires an invariant check. Apply check
768 -- only if expression comes from source, otherwise it will be applied
769 -- when value is assigned to source entity.
770
771 if Nkind (Lhs) = N_Type_Conversion
772 and then Has_Invariants (Etype (Expression (Lhs)))
773 and then Comes_From_Source (Expression (Lhs))
774 then
775 Insert_After (N, Make_Invariant_Call (Expression (Lhs)));
776 end if;
777
778 -- Final step. If left side is an entity, then we may be able to reset
779 -- the current tracked values to new safe values. We only have something
780 -- to do if the left side is an entity name, and expansion has not
781 -- modified the node into something other than an assignment, and of
782 -- course we only capture values if it is safe to do so.
783
784 if Is_Entity_Name (Lhs)
785 and then Nkind (N) = N_Assignment_Statement
786 then
787 declare
788 Ent : constant Entity_Id := Entity (Lhs);
789
790 begin
791 if Safe_To_Capture_Value (N, Ent) then
792
793 -- If simple variable on left side, warn if this assignment
794 -- blots out another one (rendering it useless). We only do
795 -- this for source assignments, otherwise we can generate bogus
796 -- warnings when an assignment is rewritten as another
797 -- assignment, and gets tied up with itself.
798
799 if Warn_On_Modified_Unread
800 and then Is_Assignable (Ent)
801 and then Comes_From_Source (N)
802 and then In_Extended_Main_Source_Unit (Ent)
803 then
804 Warn_On_Useless_Assignment (Ent, N);
805 end if;
806
807 -- If we are assigning an access type and the left side is an
808 -- entity, then make sure that the Is_Known_[Non_]Null flags
809 -- properly reflect the state of the entity after assignment.
810
811 if Is_Access_Type (T1) then
812 if Known_Non_Null (Rhs) then
813 Set_Is_Known_Non_Null (Ent, True);
814
815 elsif Known_Null (Rhs)
816 and then not Can_Never_Be_Null (Ent)
817 then
818 Set_Is_Known_Null (Ent, True);
819
820 else
821 Set_Is_Known_Null (Ent, False);
822
823 if not Can_Never_Be_Null (Ent) then
824 Set_Is_Known_Non_Null (Ent, False);
825 end if;
826 end if;
827
828 -- For discrete types, we may be able to set the current value
829 -- if the value is known at compile time.
830
831 elsif Is_Discrete_Type (T1)
832 and then Compile_Time_Known_Value (Rhs)
833 then
834 Set_Current_Value (Ent, Rhs);
835 else
836 Set_Current_Value (Ent, Empty);
837 end if;
838
839 -- If not safe to capture values, kill them
840
841 else
842 Kill_Lhs;
843 end if;
844 end;
845 end if;
846
847 -- If assigning to an object in whole or in part, note location of
848 -- assignment in case no one references value. We only do this for
849 -- source assignments, otherwise we can generate bogus warnings when an
850 -- assignment is rewritten as another assignment, and gets tied up with
851 -- itself.
852
853 declare
854 Ent : constant Entity_Id := Get_Enclosing_Object (Lhs);
855 begin
856 if Present (Ent)
857 and then Safe_To_Capture_Value (N, Ent)
858 and then Nkind (N) = N_Assignment_Statement
859 and then Warn_On_Modified_Unread
860 and then Is_Assignable (Ent)
861 and then Comes_From_Source (N)
862 and then In_Extended_Main_Source_Unit (Ent)
863 then
864 Set_Last_Assignment (Ent, Lhs);
865 end if;
866 end;
867
868 Analyze_Dimension (N);
869 end Analyze_Assignment;
870
871 -----------------------------
872 -- Analyze_Block_Statement --
873 -----------------------------
874
875 procedure Analyze_Block_Statement (N : Node_Id) is
876 procedure Install_Return_Entities (Scop : Entity_Id);
877 -- Install all entities of return statement scope Scop in the visibility
878 -- chain except for the return object since its entity is reused in a
879 -- renaming.
880
881 -----------------------------
882 -- Install_Return_Entities --
883 -----------------------------
884
885 procedure Install_Return_Entities (Scop : Entity_Id) is
886 Id : Entity_Id;
887
888 begin
889 Id := First_Entity (Scop);
890 while Present (Id) loop
891
892 -- Do not install the return object
893
894 if not Ekind_In (Id, E_Constant, E_Variable)
895 or else not Is_Return_Object (Id)
896 then
897 Install_Entity (Id);
898 end if;
899
900 Next_Entity (Id);
901 end loop;
902 end Install_Return_Entities;
903
904 -- Local constants and variables
905
906 Decls : constant List_Id := Declarations (N);
907 Id : constant Node_Id := Identifier (N);
908 HSS : constant Node_Id := Handled_Statement_Sequence (N);
909
910 Is_BIP_Return_Statement : Boolean;
911
912 -- Start of processing for Analyze_Block_Statement
913
914 begin
915 -- In SPARK mode, we reject block statements. Note that the case of
916 -- block statements generated by the expander is fine.
917
918 if Nkind (Original_Node (N)) = N_Block_Statement then
919 Check_SPARK_05_Restriction ("block statement is not allowed", N);
920 end if;
921
922 -- If no handled statement sequence is present, things are really messed
923 -- up, and we just return immediately (defence against previous errors).
924
925 if No (HSS) then
926 Check_Error_Detected;
927 return;
928 end if;
929
930 -- Detect whether the block is actually a rewritten return statement of
931 -- a build-in-place function.
932
933 Is_BIP_Return_Statement :=
934 Present (Id)
935 and then Present (Entity (Id))
936 and then Ekind (Entity (Id)) = E_Return_Statement
937 and then Is_Build_In_Place_Function
938 (Return_Applies_To (Entity (Id)));
939
940 -- Normal processing with HSS present
941
942 declare
943 EH : constant List_Id := Exception_Handlers (HSS);
944 Ent : Entity_Id := Empty;
945 S : Entity_Id;
946
947 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
948 -- Recursively save value of this global, will be restored on exit
949
950 begin
951 -- Initialize unblocked exit count for statements of begin block
952 -- plus one for each exception handler that is present.
953
954 Unblocked_Exit_Count := 1;
955
956 if Present (EH) then
957 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
958 end if;
959
960 -- If a label is present analyze it and mark it as referenced
961
962 if Present (Id) then
963 Analyze (Id);
964 Ent := Entity (Id);
965
966 -- An error defense. If we have an identifier, but no entity, then
967 -- something is wrong. If previous errors, then just remove the
968 -- identifier and continue, otherwise raise an exception.
969
970 if No (Ent) then
971 Check_Error_Detected;
972 Set_Identifier (N, Empty);
973
974 else
975 Set_Ekind (Ent, E_Block);
976 Generate_Reference (Ent, N, ' ');
977 Generate_Definition (Ent);
978
979 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
980 Set_Label_Construct (Parent (Ent), N);
981 end if;
982 end if;
983 end if;
984
985 -- If no entity set, create a label entity
986
987 if No (Ent) then
988 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
989 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
990 Set_Parent (Ent, N);
991 end if;
992
993 Set_Etype (Ent, Standard_Void_Type);
994 Set_Block_Node (Ent, Identifier (N));
995 Push_Scope (Ent);
996
997 -- The block served as an extended return statement. Ensure that any
998 -- entities created during the analysis and expansion of the return
999 -- object declaration are once again visible.
1000
1001 if Is_BIP_Return_Statement then
1002 Install_Return_Entities (Ent);
1003 end if;
1004
1005 if Present (Decls) then
1006 Analyze_Declarations (Decls);
1007 Check_Completion;
1008 Inspect_Deferred_Constant_Completion (Decls);
1009 end if;
1010
1011 Analyze (HSS);
1012 Process_End_Label (HSS, 'e', Ent);
1013
1014 -- If exception handlers are present, then we indicate that enclosing
1015 -- scopes contain a block with handlers. We only need to mark non-
1016 -- generic scopes.
1017
1018 if Present (EH) then
1019 S := Scope (Ent);
1020 loop
1021 Set_Has_Nested_Block_With_Handler (S);
1022 exit when Is_Overloadable (S)
1023 or else Ekind (S) = E_Package
1024 or else Is_Generic_Unit (S);
1025 S := Scope (S);
1026 end loop;
1027 end if;
1028
1029 Check_References (Ent);
1030 Warn_On_Useless_Assignments (Ent);
1031 End_Scope;
1032
1033 if Unblocked_Exit_Count = 0 then
1034 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1035 Check_Unreachable_Code (N);
1036 else
1037 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1038 end if;
1039 end;
1040 end Analyze_Block_Statement;
1041
1042 --------------------------------
1043 -- Analyze_Compound_Statement --
1044 --------------------------------
1045
1046 procedure Analyze_Compound_Statement (N : Node_Id) is
1047 begin
1048 Analyze_List (Actions (N));
1049 end Analyze_Compound_Statement;
1050
1051 ----------------------------
1052 -- Analyze_Case_Statement --
1053 ----------------------------
1054
1055 procedure Analyze_Case_Statement (N : Node_Id) is
1056 Exp : Node_Id;
1057 Exp_Type : Entity_Id;
1058 Exp_Btype : Entity_Id;
1059 Last_Choice : Nat;
1060
1061 Others_Present : Boolean;
1062 -- Indicates if Others was present
1063
1064 pragma Warnings (Off, Last_Choice);
1065 -- Don't care about assigned value
1066
1067 Statements_Analyzed : Boolean := False;
1068 -- Set True if at least some statement sequences get analyzed. If False
1069 -- on exit, means we had a serious error that prevented full analysis of
1070 -- the case statement, and as a result it is not a good idea to output
1071 -- warning messages about unreachable code.
1072
1073 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1074 -- Recursively save value of this global, will be restored on exit
1075
1076 procedure Non_Static_Choice_Error (Choice : Node_Id);
1077 -- Error routine invoked by the generic instantiation below when the
1078 -- case statement has a non static choice.
1079
1080 procedure Process_Statements (Alternative : Node_Id);
1081 -- Analyzes the statements associated with a case alternative. Needed
1082 -- by instantiation below.
1083
1084 package Analyze_Case_Choices is new
1085 Generic_Analyze_Choices
1086 (Process_Associated_Node => Process_Statements);
1087 use Analyze_Case_Choices;
1088 -- Instantiation of the generic choice analysis package
1089
1090 package Check_Case_Choices is new
1091 Generic_Check_Choices
1092 (Process_Empty_Choice => No_OP,
1093 Process_Non_Static_Choice => Non_Static_Choice_Error,
1094 Process_Associated_Node => No_OP);
1095 use Check_Case_Choices;
1096 -- Instantiation of the generic choice processing package
1097
1098 -----------------------------
1099 -- Non_Static_Choice_Error --
1100 -----------------------------
1101
1102 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1103 begin
1104 Flag_Non_Static_Expr
1105 ("choice given in case statement is not static!", Choice);
1106 end Non_Static_Choice_Error;
1107
1108 ------------------------
1109 -- Process_Statements --
1110 ------------------------
1111
1112 procedure Process_Statements (Alternative : Node_Id) is
1113 Choices : constant List_Id := Discrete_Choices (Alternative);
1114 Ent : Entity_Id;
1115
1116 begin
1117 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1118 Statements_Analyzed := True;
1119
1120 -- An interesting optimization. If the case statement expression
1121 -- is a simple entity, then we can set the current value within an
1122 -- alternative if the alternative has one possible value.
1123
1124 -- case N is
1125 -- when 1 => alpha
1126 -- when 2 | 3 => beta
1127 -- when others => gamma
1128
1129 -- Here we know that N is initially 1 within alpha, but for beta and
1130 -- gamma, we do not know anything more about the initial value.
1131
1132 if Is_Entity_Name (Exp) then
1133 Ent := Entity (Exp);
1134
1135 if Ekind_In (Ent, E_Variable,
1136 E_In_Out_Parameter,
1137 E_Out_Parameter)
1138 then
1139 if List_Length (Choices) = 1
1140 and then Nkind (First (Choices)) in N_Subexpr
1141 and then Compile_Time_Known_Value (First (Choices))
1142 then
1143 Set_Current_Value (Entity (Exp), First (Choices));
1144 end if;
1145
1146 Analyze_Statements (Statements (Alternative));
1147
1148 -- After analyzing the case, set the current value to empty
1149 -- since we won't know what it is for the next alternative
1150 -- (unless reset by this same circuit), or after the case.
1151
1152 Set_Current_Value (Entity (Exp), Empty);
1153 return;
1154 end if;
1155 end if;
1156
1157 -- Case where expression is not an entity name of a variable
1158
1159 Analyze_Statements (Statements (Alternative));
1160 end Process_Statements;
1161
1162 -- Start of processing for Analyze_Case_Statement
1163
1164 begin
1165 Unblocked_Exit_Count := 0;
1166 Exp := Expression (N);
1167 Analyze (Exp);
1168
1169 -- The expression must be of any discrete type. In rare cases, the
1170 -- expander constructs a case statement whose expression has a private
1171 -- type whose full view is discrete. This can happen when generating
1172 -- a stream operation for a variant type after the type is frozen,
1173 -- when the partial of view of the type of the discriminant is private.
1174 -- In that case, use the full view to analyze case alternatives.
1175
1176 if not Is_Overloaded (Exp)
1177 and then not Comes_From_Source (N)
1178 and then Is_Private_Type (Etype (Exp))
1179 and then Present (Full_View (Etype (Exp)))
1180 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1181 then
1182 Resolve (Exp, Etype (Exp));
1183 Exp_Type := Full_View (Etype (Exp));
1184
1185 else
1186 Analyze_And_Resolve (Exp, Any_Discrete);
1187 Exp_Type := Etype (Exp);
1188 end if;
1189
1190 Check_Unset_Reference (Exp);
1191 Exp_Btype := Base_Type (Exp_Type);
1192
1193 -- The expression must be of a discrete type which must be determinable
1194 -- independently of the context in which the expression occurs, but
1195 -- using the fact that the expression must be of a discrete type.
1196 -- Moreover, the type this expression must not be a character literal
1197 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1198
1199 -- If error already reported by Resolve, nothing more to do
1200
1201 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1202 return;
1203
1204 elsif Exp_Btype = Any_Character then
1205 Error_Msg_N
1206 ("character literal as case expression is ambiguous", Exp);
1207 return;
1208
1209 elsif Ada_Version = Ada_83
1210 and then (Is_Generic_Type (Exp_Btype)
1211 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1212 then
1213 Error_Msg_N
1214 ("(Ada 83) case expression cannot be of a generic type", Exp);
1215 return;
1216 end if;
1217
1218 -- If the case expression is a formal object of mode in out, then treat
1219 -- it as having a nonstatic subtype by forcing use of the base type
1220 -- (which has to get passed to Check_Case_Choices below). Also use base
1221 -- type when the case expression is parenthesized.
1222
1223 if Paren_Count (Exp) > 0
1224 or else (Is_Entity_Name (Exp)
1225 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1226 then
1227 Exp_Type := Exp_Btype;
1228 end if;
1229
1230 -- Call instantiated procedures to analyzwe and check discrete choices
1231
1232 Analyze_Choices (Alternatives (N), Exp_Type);
1233 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1234
1235 -- Case statement with single OTHERS alternative not allowed in SPARK
1236
1237 if Others_Present and then List_Length (Alternatives (N)) = 1 then
1238 Check_SPARK_05_Restriction
1239 ("OTHERS as unique case alternative is not allowed", N);
1240 end if;
1241
1242 if Exp_Type = Universal_Integer and then not Others_Present then
1243 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1244 end if;
1245
1246 -- If all our exits were blocked by unconditional transfers of control,
1247 -- then the entire CASE statement acts as an unconditional transfer of
1248 -- control, so treat it like one, and check unreachable code. Skip this
1249 -- test if we had serious errors preventing any statement analysis.
1250
1251 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1252 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1253 Check_Unreachable_Code (N);
1254 else
1255 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1256 end if;
1257
1258 -- If the expander is active it will detect the case of a statically
1259 -- determined single alternative and remove warnings for the case, but
1260 -- if we are not doing expansion, that circuit won't be active. Here we
1261 -- duplicate the effect of removing warnings in the same way, so that
1262 -- we will get the same set of warnings in -gnatc mode.
1263
1264 if not Expander_Active
1265 and then Compile_Time_Known_Value (Expression (N))
1266 and then Serious_Errors_Detected = 0
1267 then
1268 declare
1269 Chosen : constant Node_Id := Find_Static_Alternative (N);
1270 Alt : Node_Id;
1271
1272 begin
1273 Alt := First (Alternatives (N));
1274 while Present (Alt) loop
1275 if Alt /= Chosen then
1276 Remove_Warning_Messages (Statements (Alt));
1277 end if;
1278
1279 Next (Alt);
1280 end loop;
1281 end;
1282 end if;
1283 end Analyze_Case_Statement;
1284
1285 ----------------------------
1286 -- Analyze_Exit_Statement --
1287 ----------------------------
1288
1289 -- If the exit includes a name, it must be the name of a currently open
1290 -- loop. Otherwise there must be an innermost open loop on the stack, to
1291 -- which the statement implicitly refers.
1292
1293 -- Additionally, in SPARK mode:
1294
1295 -- The exit can only name the closest enclosing loop;
1296
1297 -- An exit with a when clause must be directly contained in a loop;
1298
1299 -- An exit without a when clause must be directly contained in an
1300 -- if-statement with no elsif or else, which is itself directly contained
1301 -- in a loop. The exit must be the last statement in the if-statement.
1302
1303 procedure Analyze_Exit_Statement (N : Node_Id) is
1304 Target : constant Node_Id := Name (N);
1305 Cond : constant Node_Id := Condition (N);
1306 Scope_Id : Entity_Id;
1307 U_Name : Entity_Id;
1308 Kind : Entity_Kind;
1309
1310 begin
1311 if No (Cond) then
1312 Check_Unreachable_Code (N);
1313 end if;
1314
1315 if Present (Target) then
1316 Analyze (Target);
1317 U_Name := Entity (Target);
1318
1319 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1320 Error_Msg_N ("invalid loop name in exit statement", N);
1321 return;
1322
1323 else
1324 if Has_Loop_In_Inner_Open_Scopes (U_Name) then
1325 Check_SPARK_05_Restriction
1326 ("exit label must name the closest enclosing loop", N);
1327 end if;
1328
1329 Set_Has_Exit (U_Name);
1330 end if;
1331
1332 else
1333 U_Name := Empty;
1334 end if;
1335
1336 for J in reverse 0 .. Scope_Stack.Last loop
1337 Scope_Id := Scope_Stack.Table (J).Entity;
1338 Kind := Ekind (Scope_Id);
1339
1340 if Kind = E_Loop and then (No (Target) or else Scope_Id = U_Name) then
1341 Set_Has_Exit (Scope_Id);
1342 exit;
1343
1344 elsif Kind = E_Block
1345 or else Kind = E_Loop
1346 or else Kind = E_Return_Statement
1347 then
1348 null;
1349
1350 else
1351 Error_Msg_N
1352 ("cannot exit from program unit or accept statement", N);
1353 return;
1354 end if;
1355 end loop;
1356
1357 -- Verify that if present the condition is a Boolean expression
1358
1359 if Present (Cond) then
1360 Analyze_And_Resolve (Cond, Any_Boolean);
1361 Check_Unset_Reference (Cond);
1362 end if;
1363
1364 -- In SPARK mode, verify that the exit statement respects the SPARK
1365 -- restrictions.
1366
1367 if Present (Cond) then
1368 if Nkind (Parent (N)) /= N_Loop_Statement then
1369 Check_SPARK_05_Restriction
1370 ("exit with when clause must be directly in loop", N);
1371 end if;
1372
1373 else
1374 if Nkind (Parent (N)) /= N_If_Statement then
1375 if Nkind (Parent (N)) = N_Elsif_Part then
1376 Check_SPARK_05_Restriction
1377 ("exit must be in IF without ELSIF", N);
1378 else
1379 Check_SPARK_05_Restriction ("exit must be directly in IF", N);
1380 end if;
1381
1382 elsif Nkind (Parent (Parent (N))) /= N_Loop_Statement then
1383 Check_SPARK_05_Restriction
1384 ("exit must be in IF directly in loop", N);
1385
1386 -- First test the presence of ELSE, so that an exit in an ELSE leads
1387 -- to an error mentioning the ELSE.
1388
1389 elsif Present (Else_Statements (Parent (N))) then
1390 Check_SPARK_05_Restriction ("exit must be in IF without ELSE", N);
1391
1392 -- An exit in an ELSIF does not reach here, as it would have been
1393 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1394
1395 elsif Present (Elsif_Parts (Parent (N))) then
1396 Check_SPARK_05_Restriction ("exit must be in IF without ELSIF", N);
1397 end if;
1398 end if;
1399
1400 -- Chain exit statement to associated loop entity
1401
1402 Set_Next_Exit_Statement (N, First_Exit_Statement (Scope_Id));
1403 Set_First_Exit_Statement (Scope_Id, N);
1404
1405 -- Since the exit may take us out of a loop, any previous assignment
1406 -- statement is not useless, so clear last assignment indications. It
1407 -- is OK to keep other current values, since if the exit statement
1408 -- does not exit, then the current values are still valid.
1409
1410 Kill_Current_Values (Last_Assignment_Only => True);
1411 end Analyze_Exit_Statement;
1412
1413 ----------------------------
1414 -- Analyze_Goto_Statement --
1415 ----------------------------
1416
1417 procedure Analyze_Goto_Statement (N : Node_Id) is
1418 Label : constant Node_Id := Name (N);
1419 Scope_Id : Entity_Id;
1420 Label_Scope : Entity_Id;
1421 Label_Ent : Entity_Id;
1422
1423 begin
1424 Check_SPARK_05_Restriction ("goto statement is not allowed", N);
1425
1426 -- Actual semantic checks
1427
1428 Check_Unreachable_Code (N);
1429 Kill_Current_Values (Last_Assignment_Only => True);
1430
1431 Analyze (Label);
1432 Label_Ent := Entity (Label);
1433
1434 -- Ignore previous error
1435
1436 if Label_Ent = Any_Id then
1437 Check_Error_Detected;
1438 return;
1439
1440 -- We just have a label as the target of a goto
1441
1442 elsif Ekind (Label_Ent) /= E_Label then
1443 Error_Msg_N ("target of goto statement must be a label", Label);
1444 return;
1445
1446 -- Check that the target of the goto is reachable according to Ada
1447 -- scoping rules. Note: the special gotos we generate for optimizing
1448 -- local handling of exceptions would violate these rules, but we mark
1449 -- such gotos as analyzed when built, so this code is never entered.
1450
1451 elsif not Reachable (Label_Ent) then
1452 Error_Msg_N ("target of goto statement is not reachable", Label);
1453 return;
1454 end if;
1455
1456 -- Here if goto passes initial validity checks
1457
1458 Label_Scope := Enclosing_Scope (Label_Ent);
1459
1460 for J in reverse 0 .. Scope_Stack.Last loop
1461 Scope_Id := Scope_Stack.Table (J).Entity;
1462
1463 if Label_Scope = Scope_Id
1464 or else not Ekind_In (Scope_Id, E_Block, E_Loop, E_Return_Statement)
1465 then
1466 if Scope_Id /= Label_Scope then
1467 Error_Msg_N
1468 ("cannot exit from program unit or accept statement", N);
1469 end if;
1470
1471 return;
1472 end if;
1473 end loop;
1474
1475 raise Program_Error;
1476 end Analyze_Goto_Statement;
1477
1478 --------------------------
1479 -- Analyze_If_Statement --
1480 --------------------------
1481
1482 -- A special complication arises in the analysis of if statements
1483
1484 -- The expander has circuitry to completely delete code that it can tell
1485 -- will not be executed (as a result of compile time known conditions). In
1486 -- the analyzer, we ensure that code that will be deleted in this manner
1487 -- is analyzed but not expanded. This is obviously more efficient, but
1488 -- more significantly, difficulties arise if code is expanded and then
1489 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1490 -- generated in deleted code must be frozen from start, because the nodes
1491 -- on which they depend will not be available at the freeze point.
1492
1493 procedure Analyze_If_Statement (N : Node_Id) is
1494 E : Node_Id;
1495
1496 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1497 -- Recursively save value of this global, will be restored on exit
1498
1499 Save_In_Deleted_Code : Boolean;
1500
1501 Del : Boolean := False;
1502 -- This flag gets set True if a True condition has been found, which
1503 -- means that remaining ELSE/ELSIF parts are deleted.
1504
1505 procedure Analyze_Cond_Then (Cnode : Node_Id);
1506 -- This is applied to either the N_If_Statement node itself or to an
1507 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1508 -- statements associated with it.
1509
1510 -----------------------
1511 -- Analyze_Cond_Then --
1512 -----------------------
1513
1514 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1515 Cond : constant Node_Id := Condition (Cnode);
1516 Tstm : constant List_Id := Then_Statements (Cnode);
1517
1518 begin
1519 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1520 Analyze_And_Resolve (Cond, Any_Boolean);
1521 Check_Unset_Reference (Cond);
1522 Set_Current_Value_Condition (Cnode);
1523
1524 -- If already deleting, then just analyze then statements
1525
1526 if Del then
1527 Analyze_Statements (Tstm);
1528
1529 -- Compile time known value, not deleting yet
1530
1531 elsif Compile_Time_Known_Value (Cond) then
1532 Save_In_Deleted_Code := In_Deleted_Code;
1533
1534 -- If condition is True, then analyze the THEN statements and set
1535 -- no expansion for ELSE and ELSIF parts.
1536
1537 if Is_True (Expr_Value (Cond)) then
1538 Analyze_Statements (Tstm);
1539 Del := True;
1540 Expander_Mode_Save_And_Set (False);
1541 In_Deleted_Code := True;
1542
1543 -- If condition is False, analyze THEN with expansion off
1544
1545 else -- Is_False (Expr_Value (Cond))
1546 Expander_Mode_Save_And_Set (False);
1547 In_Deleted_Code := True;
1548 Analyze_Statements (Tstm);
1549 Expander_Mode_Restore;
1550 In_Deleted_Code := Save_In_Deleted_Code;
1551 end if;
1552
1553 -- Not known at compile time, not deleting, normal analysis
1554
1555 else
1556 Analyze_Statements (Tstm);
1557 end if;
1558 end Analyze_Cond_Then;
1559
1560 -- Start of Analyze_If_Statement
1561
1562 begin
1563 -- Initialize exit count for else statements. If there is no else part,
1564 -- this count will stay non-zero reflecting the fact that the uncovered
1565 -- else case is an unblocked exit.
1566
1567 Unblocked_Exit_Count := 1;
1568 Analyze_Cond_Then (N);
1569
1570 -- Now to analyze the elsif parts if any are present
1571
1572 if Present (Elsif_Parts (N)) then
1573 E := First (Elsif_Parts (N));
1574 while Present (E) loop
1575 Analyze_Cond_Then (E);
1576 Next (E);
1577 end loop;
1578 end if;
1579
1580 if Present (Else_Statements (N)) then
1581 Analyze_Statements (Else_Statements (N));
1582 end if;
1583
1584 -- If all our exits were blocked by unconditional transfers of control,
1585 -- then the entire IF statement acts as an unconditional transfer of
1586 -- control, so treat it like one, and check unreachable code.
1587
1588 if Unblocked_Exit_Count = 0 then
1589 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1590 Check_Unreachable_Code (N);
1591 else
1592 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1593 end if;
1594
1595 if Del then
1596 Expander_Mode_Restore;
1597 In_Deleted_Code := Save_In_Deleted_Code;
1598 end if;
1599
1600 if not Expander_Active
1601 and then Compile_Time_Known_Value (Condition (N))
1602 and then Serious_Errors_Detected = 0
1603 then
1604 if Is_True (Expr_Value (Condition (N))) then
1605 Remove_Warning_Messages (Else_Statements (N));
1606
1607 if Present (Elsif_Parts (N)) then
1608 E := First (Elsif_Parts (N));
1609 while Present (E) loop
1610 Remove_Warning_Messages (Then_Statements (E));
1611 Next (E);
1612 end loop;
1613 end if;
1614
1615 else
1616 Remove_Warning_Messages (Then_Statements (N));
1617 end if;
1618 end if;
1619
1620 -- Warn on redundant if statement that has no effect
1621
1622 -- Note, we could also check empty ELSIF parts ???
1623
1624 if Warn_On_Redundant_Constructs
1625
1626 -- If statement must be from source
1627
1628 and then Comes_From_Source (N)
1629
1630 -- Condition must not have obvious side effect
1631
1632 and then Has_No_Obvious_Side_Effects (Condition (N))
1633
1634 -- No elsif parts of else part
1635
1636 and then No (Elsif_Parts (N))
1637 and then No (Else_Statements (N))
1638
1639 -- Then must be a single null statement
1640
1641 and then List_Length (Then_Statements (N)) = 1
1642 then
1643 -- Go to original node, since we may have rewritten something as
1644 -- a null statement (e.g. a case we could figure the outcome of).
1645
1646 declare
1647 T : constant Node_Id := First (Then_Statements (N));
1648 S : constant Node_Id := Original_Node (T);
1649
1650 begin
1651 if Comes_From_Source (S) and then Nkind (S) = N_Null_Statement then
1652 Error_Msg_N ("if statement has no effect?r?", N);
1653 end if;
1654 end;
1655 end if;
1656 end Analyze_If_Statement;
1657
1658 ----------------------------------------
1659 -- Analyze_Implicit_Label_Declaration --
1660 ----------------------------------------
1661
1662 -- An implicit label declaration is generated in the innermost enclosing
1663 -- declarative part. This is done for labels, and block and loop names.
1664
1665 -- Note: any changes in this routine may need to be reflected in
1666 -- Analyze_Label_Entity.
1667
1668 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1669 Id : constant Node_Id := Defining_Identifier (N);
1670 begin
1671 Enter_Name (Id);
1672 Set_Ekind (Id, E_Label);
1673 Set_Etype (Id, Standard_Void_Type);
1674 Set_Enclosing_Scope (Id, Current_Scope);
1675 end Analyze_Implicit_Label_Declaration;
1676
1677 ------------------------------
1678 -- Analyze_Iteration_Scheme --
1679 ------------------------------
1680
1681 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1682 Cond : Node_Id;
1683 Iter_Spec : Node_Id;
1684 Loop_Spec : Node_Id;
1685
1686 begin
1687 -- For an infinite loop, there is no iteration scheme
1688
1689 if No (N) then
1690 return;
1691 end if;
1692
1693 Cond := Condition (N);
1694 Iter_Spec := Iterator_Specification (N);
1695 Loop_Spec := Loop_Parameter_Specification (N);
1696
1697 if Present (Cond) then
1698 Analyze_And_Resolve (Cond, Any_Boolean);
1699 Check_Unset_Reference (Cond);
1700 Set_Current_Value_Condition (N);
1701
1702 elsif Present (Iter_Spec) then
1703 Analyze_Iterator_Specification (Iter_Spec);
1704
1705 else
1706 Analyze_Loop_Parameter_Specification (Loop_Spec);
1707 end if;
1708 end Analyze_Iteration_Scheme;
1709
1710 ------------------------------------
1711 -- Analyze_Iterator_Specification --
1712 ------------------------------------
1713
1714 procedure Analyze_Iterator_Specification (N : Node_Id) is
1715 Loc : constant Source_Ptr := Sloc (N);
1716 Def_Id : constant Node_Id := Defining_Identifier (N);
1717 Subt : constant Node_Id := Subtype_Indication (N);
1718 Iter_Name : constant Node_Id := Name (N);
1719
1720 Ent : Entity_Id;
1721 Typ : Entity_Id;
1722 Bas : Entity_Id;
1723
1724 procedure Check_Reverse_Iteration (Typ : Entity_Id);
1725 -- For an iteration over a container, if the loop carries the Reverse
1726 -- indicator, verify that the container type has an Iterate aspect that
1727 -- implements the reversible iterator interface.
1728
1729 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id;
1730 -- For containers with Iterator and related aspects, the cursor is
1731 -- obtained by locating an entity with the proper name in the scope
1732 -- of the type.
1733
1734 -----------------------------
1735 -- Check_Reverse_Iteration --
1736 -----------------------------
1737
1738 procedure Check_Reverse_Iteration (Typ : Entity_Id) is
1739 begin
1740 if Reverse_Present (N)
1741 and then not Is_Array_Type (Typ)
1742 and then not Is_Reversible_Iterator (Typ)
1743 then
1744 Error_Msg_NE
1745 ("container type does not support reverse iteration", N, Typ);
1746 end if;
1747 end Check_Reverse_Iteration;
1748
1749 ---------------------
1750 -- Get_Cursor_Type --
1751 ---------------------
1752
1753 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
1754 Ent : Entity_Id;
1755
1756 begin
1757 Ent := First_Entity (Scope (Typ));
1758 while Present (Ent) loop
1759 exit when Chars (Ent) = Name_Cursor;
1760 Next_Entity (Ent);
1761 end loop;
1762
1763 if No (Ent) then
1764 return Any_Type;
1765 end if;
1766
1767 -- The cursor is the target of generated assignments in the
1768 -- loop, and cannot have a limited type.
1769
1770 if Is_Limited_Type (Etype (Ent)) then
1771 Error_Msg_N ("cursor type cannot be limited", N);
1772 end if;
1773
1774 return Etype (Ent);
1775 end Get_Cursor_Type;
1776
1777 -- Start of processing for Analyze_iterator_Specification
1778
1779 begin
1780 Enter_Name (Def_Id);
1781
1782 -- AI12-0151 specifies that when the subtype indication is present, it
1783 -- must statically match the type of the array or container element.
1784 -- To simplify this check, we introduce a subtype declaration with the
1785 -- given subtype indication when it carries a constraint, and rewrite
1786 -- the original as a reference to the created subtype entity.
1787
1788 if Present (Subt) then
1789 if Nkind (Subt) = N_Subtype_Indication then
1790 declare
1791 S : constant Entity_Id := Make_Temporary (Sloc (Subt), 'S');
1792 Decl : constant Node_Id :=
1793 Make_Subtype_Declaration (Loc,
1794 Defining_Identifier => S,
1795 Subtype_Indication => New_Copy_Tree (Subt));
1796 begin
1797 Insert_Before (Parent (Parent (N)), Decl);
1798 Analyze (Decl);
1799 Rewrite (Subt, New_Occurrence_Of (S, Sloc (Subt)));
1800 end;
1801 else
1802 Analyze (Subt);
1803 end if;
1804
1805 -- Save entity of subtype indication for subsequent check
1806
1807 Bas := Entity (Subt);
1808 end if;
1809
1810 Preanalyze_Range (Iter_Name);
1811
1812 -- Set the kind of the loop variable, which is not visible within
1813 -- the iterator name.
1814
1815 Set_Ekind (Def_Id, E_Variable);
1816
1817 -- Provide a link between the iterator variable and the container, for
1818 -- subsequent use in cross-reference and modification information.
1819
1820 if Of_Present (N) then
1821 Set_Related_Expression (Def_Id, Iter_Name);
1822
1823 -- For a container, the iterator is specified through the aspect
1824
1825 if not Is_Array_Type (Etype (Iter_Name)) then
1826 declare
1827 Iterator : constant Entity_Id :=
1828 Find_Value_Of_Aspect
1829 (Etype (Iter_Name), Aspect_Default_Iterator);
1830
1831 I : Interp_Index;
1832 It : Interp;
1833
1834 begin
1835 if No (Iterator) then
1836 null; -- error reported below.
1837
1838 elsif not Is_Overloaded (Iterator) then
1839 Check_Reverse_Iteration (Etype (Iterator));
1840
1841 -- If Iterator is overloaded, use reversible iterator if
1842 -- one is available.
1843
1844 elsif Is_Overloaded (Iterator) then
1845 Get_First_Interp (Iterator, I, It);
1846 while Present (It.Nam) loop
1847 if Ekind (It.Nam) = E_Function
1848 and then Is_Reversible_Iterator (Etype (It.Nam))
1849 then
1850 Set_Etype (Iterator, It.Typ);
1851 Set_Entity (Iterator, It.Nam);
1852 exit;
1853 end if;
1854
1855 Get_Next_Interp (I, It);
1856 end loop;
1857
1858 Check_Reverse_Iteration (Etype (Iterator));
1859 end if;
1860 end;
1861 end if;
1862 end if;
1863
1864 -- If the domain of iteration is an expression, create a declaration for
1865 -- it, so that finalization actions are introduced outside of the loop.
1866 -- The declaration must be a renaming because the body of the loop may
1867 -- assign to elements.
1868
1869 if not Is_Entity_Name (Iter_Name)
1870
1871 -- When the context is a quantified expression, the renaming
1872 -- declaration is delayed until the expansion phase if we are
1873 -- doing expansion.
1874
1875 and then (Nkind (Parent (N)) /= N_Quantified_Expression
1876 or else Operating_Mode = Check_Semantics)
1877
1878 -- Do not perform this expansion in SPARK mode, since the formal
1879 -- verification directly deals with the source form of the iterator.
1880 -- Ditto for ASIS, where the temporary may hide the transformation
1881 -- of a selected component into a prefixed function call.
1882
1883 and then not GNATprove_Mode
1884 and then not ASIS_Mode
1885 then
1886 declare
1887 Id : constant Entity_Id := Make_Temporary (Loc, 'R', Iter_Name);
1888 Decl : Node_Id;
1889 Act_S : Node_Id;
1890
1891 begin
1892
1893 -- If the domain of iteration is an array component that depends
1894 -- on a discriminant, create actual subtype for it. Pre-analysis
1895 -- does not generate the actual subtype of a selected component.
1896
1897 if Nkind (Iter_Name) = N_Selected_Component
1898 and then Is_Array_Type (Etype (Iter_Name))
1899 then
1900 Act_S :=
1901 Build_Actual_Subtype_Of_Component
1902 (Etype (Selector_Name (Iter_Name)), Iter_Name);
1903 Insert_Action (N, Act_S);
1904
1905 if Present (Act_S) then
1906 Typ := Defining_Identifier (Act_S);
1907 else
1908 Typ := Etype (Iter_Name);
1909 end if;
1910
1911 else
1912 Typ := Etype (Iter_Name);
1913
1914 -- Verify that the expression produces an iterator
1915
1916 if not Of_Present (N) and then not Is_Iterator (Typ)
1917 and then not Is_Array_Type (Typ)
1918 and then No (Find_Aspect (Typ, Aspect_Iterable))
1919 then
1920 Error_Msg_N
1921 ("expect object that implements iterator interface",
1922 Iter_Name);
1923 end if;
1924 end if;
1925
1926 -- Protect against malformed iterator
1927
1928 if Typ = Any_Type then
1929 Error_Msg_N ("invalid expression in loop iterator", Iter_Name);
1930 return;
1931 end if;
1932
1933 if not Of_Present (N) then
1934 Check_Reverse_Iteration (Typ);
1935 end if;
1936
1937 -- The name in the renaming declaration may be a function call.
1938 -- Indicate that it does not come from source, to suppress
1939 -- spurious warnings on renamings of parameterless functions,
1940 -- a common enough idiom in user-defined iterators.
1941
1942 Decl :=
1943 Make_Object_Renaming_Declaration (Loc,
1944 Defining_Identifier => Id,
1945 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
1946 Name =>
1947 New_Copy_Tree (Iter_Name, New_Sloc => Loc));
1948
1949 Insert_Actions (Parent (Parent (N)), New_List (Decl));
1950 Rewrite (Name (N), New_Occurrence_Of (Id, Loc));
1951 Set_Etype (Id, Typ);
1952 Set_Etype (Name (N), Typ);
1953 end;
1954
1955 -- Container is an entity or an array with uncontrolled components, or
1956 -- else it is a container iterator given by a function call, typically
1957 -- called Iterate in the case of predefined containers, even though
1958 -- Iterate is not a reserved name. What matters is that the return type
1959 -- of the function is an iterator type.
1960
1961 elsif Is_Entity_Name (Iter_Name) then
1962 Analyze (Iter_Name);
1963
1964 if Nkind (Iter_Name) = N_Function_Call then
1965 declare
1966 C : constant Node_Id := Name (Iter_Name);
1967 I : Interp_Index;
1968 It : Interp;
1969
1970 begin
1971 if not Is_Overloaded (Iter_Name) then
1972 Resolve (Iter_Name, Etype (C));
1973
1974 else
1975 Get_First_Interp (C, I, It);
1976 while It.Typ /= Empty loop
1977 if Reverse_Present (N) then
1978 if Is_Reversible_Iterator (It.Typ) then
1979 Resolve (Iter_Name, It.Typ);
1980 exit;
1981 end if;
1982
1983 elsif Is_Iterator (It.Typ) then
1984 Resolve (Iter_Name, It.Typ);
1985 exit;
1986 end if;
1987
1988 Get_Next_Interp (I, It);
1989 end loop;
1990 end if;
1991 end;
1992
1993 -- Domain of iteration is not overloaded
1994
1995 else
1996 Resolve (Iter_Name, Etype (Iter_Name));
1997 end if;
1998
1999 if not Of_Present (N) then
2000 Check_Reverse_Iteration (Etype (Iter_Name));
2001 end if;
2002 end if;
2003
2004 -- Get base type of container, for proper retrieval of Cursor type
2005 -- and primitive operations.
2006
2007 Typ := Base_Type (Etype (Iter_Name));
2008
2009 if Is_Array_Type (Typ) then
2010 if Of_Present (N) then
2011 Set_Etype (Def_Id, Component_Type (Typ));
2012
2013 -- AI12-0151 stipulates that the container cannot be a component
2014 -- that depends on a discriminant if the enclosing object is
2015 -- mutable, to prevent a modification of the container in the
2016 -- course of an iteration.
2017
2018 -- Should comment on need to go to Original_Node ???
2019
2020 if Nkind (Original_Node (Iter_Name)) = N_Selected_Component
2021 and then Is_Dependent_Component_Of_Mutable_Object
2022 (Original_Node (Iter_Name))
2023 then
2024 Error_Msg_N
2025 ("container cannot be a discriminant-dependent "
2026 & "component of a mutable object", N);
2027 end if;
2028
2029 if Present (Subt)
2030 and then
2031 (Base_Type (Bas) /= Base_Type (Component_Type (Typ))
2032 or else
2033 not Subtypes_Statically_Match (Bas, Component_Type (Typ)))
2034 then
2035 Error_Msg_N
2036 ("subtype indication does not match component type", Subt);
2037 end if;
2038
2039 -- Here we have a missing Range attribute
2040
2041 else
2042 Error_Msg_N
2043 ("missing Range attribute in iteration over an array", N);
2044
2045 -- In Ada 2012 mode, this may be an attempt at an iterator
2046
2047 if Ada_Version >= Ada_2012 then
2048 Error_Msg_NE
2049 ("\if& is meant to designate an element of the array, use OF",
2050 N, Def_Id);
2051 end if;
2052
2053 -- Prevent cascaded errors
2054
2055 Set_Ekind (Def_Id, E_Loop_Parameter);
2056 Set_Etype (Def_Id, Etype (First_Index (Typ)));
2057 end if;
2058
2059 -- Check for type error in iterator
2060
2061 elsif Typ = Any_Type then
2062 return;
2063
2064 -- Iteration over a container
2065
2066 else
2067 Set_Ekind (Def_Id, E_Loop_Parameter);
2068 Error_Msg_Ada_2012_Feature ("container iterator", Sloc (N));
2069
2070 -- OF present
2071
2072 if Of_Present (N) then
2073 if Has_Aspect (Typ, Aspect_Iterable) then
2074 declare
2075 Elt : constant Entity_Id :=
2076 Get_Iterable_Type_Primitive (Typ, Name_Element);
2077 begin
2078 if No (Elt) then
2079 Error_Msg_N
2080 ("missing Element primitive for iteration", N);
2081 else
2082 Set_Etype (Def_Id, Etype (Elt));
2083 end if;
2084 end;
2085
2086 -- For a predefined container, The type of the loop variable is
2087 -- the Iterator_Element aspect of the container type.
2088
2089 else
2090 declare
2091 Element : constant Entity_Id :=
2092 Find_Value_Of_Aspect (Typ, Aspect_Iterator_Element);
2093 Iterator : constant Entity_Id :=
2094 Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
2095 Cursor_Type : Entity_Id;
2096
2097 begin
2098 if No (Element) then
2099 Error_Msg_NE ("cannot iterate over&", N, Typ);
2100 return;
2101
2102 else
2103 Set_Etype (Def_Id, Entity (Element));
2104 Cursor_Type := Get_Cursor_Type (Typ);
2105 pragma Assert (Present (Cursor_Type));
2106
2107 -- If subtype indication was given, verify that it covers
2108 -- the element type of the container.
2109
2110 if Present (Subt)
2111 and then (not Covers (Bas, Etype (Def_Id))
2112 or else not Subtypes_Statically_Match
2113 (Bas, Etype (Def_Id)))
2114 then
2115 Error_Msg_N
2116 ("subtype indication does not match element type",
2117 Subt);
2118 end if;
2119
2120 -- If the container has a variable indexing aspect, the
2121 -- element is a variable and is modifiable in the loop.
2122
2123 if Has_Aspect (Typ, Aspect_Variable_Indexing) then
2124 Set_Ekind (Def_Id, E_Variable);
2125 end if;
2126
2127 -- If the container is a constant, iterating over it
2128 -- requires a Constant_Indexing operation.
2129
2130 if not Is_Variable (Iter_Name)
2131 and then not Has_Aspect (Typ, Aspect_Constant_Indexing)
2132 then
2133 Error_Msg_N ("iteration over constant container "
2134 & "require constant_indexing aspect", N);
2135
2136 -- The Iterate function may have an in_out parameter,
2137 -- and a constant container is thus illegal.
2138
2139 elsif Present (Iterator)
2140 and then Ekind (Entity (Iterator)) = E_Function
2141 and then Ekind (First_Formal (Entity (Iterator))) /=
2142 E_In_Parameter
2143 and then not Is_Variable (Iter_Name)
2144 then
2145 Error_Msg_N
2146 ("variable container expected", N);
2147 end if;
2148
2149 if Nkind (Original_Node (Iter_Name))
2150 = N_Selected_Component
2151 and then
2152 Is_Dependent_Component_Of_Mutable_Object
2153 (Original_Node (Iter_Name))
2154 then
2155 Error_Msg_N
2156 ("container cannot be a discriminant-dependent "
2157 & "component of a mutable object", N);
2158 end if;
2159 end if;
2160 end;
2161 end if;
2162
2163 -- IN iterator, domain is a range, or a call to Iterate function
2164
2165 else
2166 -- For an iteration of the form IN, the name must denote an
2167 -- iterator, typically the result of a call to Iterate. Give a
2168 -- useful error message when the name is a container by itself.
2169
2170 -- The type may be a formal container type, which has to have
2171 -- an Iterable aspect detailing the required primitives.
2172
2173 if Is_Entity_Name (Original_Node (Name (N)))
2174 and then not Is_Iterator (Typ)
2175 then
2176 if Has_Aspect (Typ, Aspect_Iterable) then
2177 null;
2178
2179 elsif not Has_Aspect (Typ, Aspect_Iterator_Element) then
2180 Error_Msg_NE
2181 ("cannot iterate over&", Name (N), Typ);
2182 else
2183 Error_Msg_N
2184 ("name must be an iterator, not a container", Name (N));
2185 end if;
2186
2187 if Has_Aspect (Typ, Aspect_Iterable) then
2188 null;
2189 else
2190 Error_Msg_NE
2191 ("\to iterate directly over the elements of a container, "
2192 & "write `of &`", Name (N), Original_Node (Name (N)));
2193
2194 -- No point in continuing analysis of iterator spec
2195
2196 return;
2197 end if;
2198 end if;
2199
2200 -- If the name is a call (typically prefixed) to some Iterate
2201 -- function, it has been rewritten as an object declaration.
2202 -- If that object is a selected component, verify that it is not
2203 -- a component of an unconstrained mutable object.
2204
2205 if Nkind (Iter_Name) = N_Identifier then
2206 declare
2207 Orig_Node : constant Node_Id := Original_Node (Iter_Name);
2208 Iter_Kind : constant Node_Kind := Nkind (Orig_Node);
2209 Obj : Node_Id;
2210
2211 begin
2212 if Iter_Kind = N_Selected_Component then
2213 Obj := Prefix (Orig_Node);
2214
2215 elsif Iter_Kind = N_Function_Call then
2216 Obj := First_Actual (Orig_Node);
2217
2218 -- If neither, the name comes from source
2219
2220 else
2221 Obj := Iter_Name;
2222 end if;
2223
2224 if Nkind (Obj) = N_Selected_Component
2225 and then Is_Dependent_Component_Of_Mutable_Object (Obj)
2226 then
2227 Error_Msg_N
2228 ("container cannot be a discriminant-dependent "
2229 & "component of a mutable object", N);
2230 end if;
2231 end;
2232 end if;
2233
2234 -- The result type of Iterate function is the classwide type of
2235 -- the interface parent. We need the specific Cursor type defined
2236 -- in the container package. We obtain it by name for a predefined
2237 -- container, or through the Iterable aspect for a formal one.
2238
2239 if Has_Aspect (Typ, Aspect_Iterable) then
2240 Set_Etype (Def_Id,
2241 Get_Cursor_Type
2242 (Parent (Find_Value_Of_Aspect (Typ, Aspect_Iterable)),
2243 Typ));
2244 Ent := Etype (Def_Id);
2245
2246 else
2247 Set_Etype (Def_Id, Get_Cursor_Type (Typ));
2248 end if;
2249
2250 end if;
2251 end if;
2252
2253 -- A loop parameter cannot be effectively volatile. This check is
2254 -- peformed only when SPARK_Mode is on as it is not a standard Ada
2255 -- legality check (SPARK RM 7.1.3(6)).
2256
2257 -- Not clear whether this applies to element iterators, where the
2258 -- cursor is not an explicit entity ???
2259
2260 if SPARK_Mode = On
2261 and then not Of_Present (N)
2262 and then Is_Effectively_Volatile (Ent)
2263 then
2264 Error_Msg_N ("loop parameter cannot be volatile", Ent);
2265 end if;
2266 end Analyze_Iterator_Specification;
2267
2268 -------------------
2269 -- Analyze_Label --
2270 -------------------
2271
2272 -- Note: the semantic work required for analyzing labels (setting them as
2273 -- reachable) was done in a prepass through the statements in the block,
2274 -- so that forward gotos would be properly handled. See Analyze_Statements
2275 -- for further details. The only processing required here is to deal with
2276 -- optimizations that depend on an assumption of sequential control flow,
2277 -- since of course the occurrence of a label breaks this assumption.
2278
2279 procedure Analyze_Label (N : Node_Id) is
2280 pragma Warnings (Off, N);
2281 begin
2282 Kill_Current_Values;
2283 end Analyze_Label;
2284
2285 --------------------------
2286 -- Analyze_Label_Entity --
2287 --------------------------
2288
2289 procedure Analyze_Label_Entity (E : Entity_Id) is
2290 begin
2291 Set_Ekind (E, E_Label);
2292 Set_Etype (E, Standard_Void_Type);
2293 Set_Enclosing_Scope (E, Current_Scope);
2294 Set_Reachable (E, True);
2295 end Analyze_Label_Entity;
2296
2297 ------------------------------------------
2298 -- Analyze_Loop_Parameter_Specification --
2299 ------------------------------------------
2300
2301 procedure Analyze_Loop_Parameter_Specification (N : Node_Id) is
2302 Loop_Nod : constant Node_Id := Parent (Parent (N));
2303
2304 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
2305 -- If the bounds are given by a 'Range reference on a function call
2306 -- that returns a controlled array, introduce an explicit declaration
2307 -- to capture the bounds, so that the function result can be finalized
2308 -- in timely fashion.
2309
2310 procedure Check_Predicate_Use (T : Entity_Id);
2311 -- Diagnose Attempt to iterate through non-static predicate. Note that
2312 -- a type with inherited predicates may have both static and dynamic
2313 -- forms. In this case it is not sufficent to check the static predicate
2314 -- function only, look for a dynamic predicate aspect as well.
2315
2316 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean;
2317 -- N is the node for an arbitrary construct. This function searches the
2318 -- construct N to see if any expressions within it contain function
2319 -- calls that use the secondary stack, returning True if any such call
2320 -- is found, and False otherwise.
2321
2322 procedure Process_Bounds (R : Node_Id);
2323 -- If the iteration is given by a range, create temporaries and
2324 -- assignment statements block to capture the bounds and perform
2325 -- required finalization actions in case a bound includes a function
2326 -- call that uses the temporary stack. We first pre-analyze a copy of
2327 -- the range in order to determine the expected type, and analyze and
2328 -- resolve the original bounds.
2329
2330 --------------------------------------
2331 -- Check_Controlled_Array_Attribute --
2332 --------------------------------------
2333
2334 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
2335 begin
2336 if Nkind (DS) = N_Attribute_Reference
2337 and then Is_Entity_Name (Prefix (DS))
2338 and then Ekind (Entity (Prefix (DS))) = E_Function
2339 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
2340 and then
2341 Is_Controlled (Component_Type (Etype (Entity (Prefix (DS)))))
2342 and then Expander_Active
2343 then
2344 declare
2345 Loc : constant Source_Ptr := Sloc (N);
2346 Arr : constant Entity_Id := Etype (Entity (Prefix (DS)));
2347 Indx : constant Entity_Id :=
2348 Base_Type (Etype (First_Index (Arr)));
2349 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
2350 Decl : Node_Id;
2351
2352 begin
2353 Decl :=
2354 Make_Subtype_Declaration (Loc,
2355 Defining_Identifier => Subt,
2356 Subtype_Indication =>
2357 Make_Subtype_Indication (Loc,
2358 Subtype_Mark => New_Occurrence_Of (Indx, Loc),
2359 Constraint =>
2360 Make_Range_Constraint (Loc, Relocate_Node (DS))));
2361 Insert_Before (Loop_Nod, Decl);
2362 Analyze (Decl);
2363
2364 Rewrite (DS,
2365 Make_Attribute_Reference (Loc,
2366 Prefix => New_Occurrence_Of (Subt, Loc),
2367 Attribute_Name => Attribute_Name (DS)));
2368
2369 Analyze (DS);
2370 end;
2371 end if;
2372 end Check_Controlled_Array_Attribute;
2373
2374 -------------------------
2375 -- Check_Predicate_Use --
2376 -------------------------
2377
2378 procedure Check_Predicate_Use (T : Entity_Id) is
2379 begin
2380 -- A predicated subtype is illegal in loops and related constructs
2381 -- if the predicate is not static, or if it is a non-static subtype
2382 -- of a statically predicated subtype.
2383
2384 if Is_Discrete_Type (T)
2385 and then Has_Predicates (T)
2386 and then (not Has_Static_Predicate (T)
2387 or else not Is_Static_Subtype (T)
2388 or else Has_Dynamic_Predicate_Aspect (T))
2389 then
2390 -- Seems a confusing message for the case of a static predicate
2391 -- with a non-static subtype???
2392
2393 Bad_Predicated_Subtype_Use
2394 ("cannot use subtype& with non-static predicate for loop "
2395 & "iteration", Discrete_Subtype_Definition (N),
2396 T, Suggest_Static => True);
2397
2398 elsif Inside_A_Generic and then Is_Generic_Formal (T) then
2399 Set_No_Dynamic_Predicate_On_Actual (T);
2400 end if;
2401 end Check_Predicate_Use;
2402
2403 ------------------------------------
2404 -- Has_Call_Using_Secondary_Stack --
2405 ------------------------------------
2406
2407 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean is
2408
2409 function Check_Call (N : Node_Id) return Traverse_Result;
2410 -- Check if N is a function call which uses the secondary stack
2411
2412 ----------------
2413 -- Check_Call --
2414 ----------------
2415
2416 function Check_Call (N : Node_Id) return Traverse_Result is
2417 Nam : Node_Id;
2418 Subp : Entity_Id;
2419 Return_Typ : Entity_Id;
2420
2421 begin
2422 if Nkind (N) = N_Function_Call then
2423 Nam := Name (N);
2424
2425 -- Call using access to subprogram with explicit dereference
2426
2427 if Nkind (Nam) = N_Explicit_Dereference then
2428 Subp := Etype (Nam);
2429
2430 -- Call using a selected component notation or Ada 2005 object
2431 -- operation notation
2432
2433 elsif Nkind (Nam) = N_Selected_Component then
2434 Subp := Entity (Selector_Name (Nam));
2435
2436 -- Common case
2437
2438 else
2439 Subp := Entity (Nam);
2440 end if;
2441
2442 Return_Typ := Etype (Subp);
2443
2444 if Is_Composite_Type (Return_Typ)
2445 and then not Is_Constrained (Return_Typ)
2446 then
2447 return Abandon;
2448
2449 elsif Sec_Stack_Needed_For_Return (Subp) then
2450 return Abandon;
2451 end if;
2452 end if;
2453
2454 -- Continue traversing the tree
2455
2456 return OK;
2457 end Check_Call;
2458
2459 function Check_Calls is new Traverse_Func (Check_Call);
2460
2461 -- Start of processing for Has_Call_Using_Secondary_Stack
2462
2463 begin
2464 return Check_Calls (N) = Abandon;
2465 end Has_Call_Using_Secondary_Stack;
2466
2467 --------------------
2468 -- Process_Bounds --
2469 --------------------
2470
2471 procedure Process_Bounds (R : Node_Id) is
2472 Loc : constant Source_Ptr := Sloc (N);
2473
2474 function One_Bound
2475 (Original_Bound : Node_Id;
2476 Analyzed_Bound : Node_Id;
2477 Typ : Entity_Id) return Node_Id;
2478 -- Capture value of bound and return captured value
2479
2480 ---------------
2481 -- One_Bound --
2482 ---------------
2483
2484 function One_Bound
2485 (Original_Bound : Node_Id;
2486 Analyzed_Bound : Node_Id;
2487 Typ : Entity_Id) return Node_Id
2488 is
2489 Assign : Node_Id;
2490 Decl : Node_Id;
2491 Id : Entity_Id;
2492
2493 begin
2494 -- If the bound is a constant or an object, no need for a separate
2495 -- declaration. If the bound is the result of previous expansion
2496 -- it is already analyzed and should not be modified. Note that
2497 -- the Bound will be resolved later, if needed, as part of the
2498 -- call to Make_Index (literal bounds may need to be resolved to
2499 -- type Integer).
2500
2501 if Analyzed (Original_Bound) then
2502 return Original_Bound;
2503
2504 elsif Nkind_In (Analyzed_Bound, N_Integer_Literal,
2505 N_Character_Literal)
2506 or else Is_Entity_Name (Analyzed_Bound)
2507 then
2508 Analyze_And_Resolve (Original_Bound, Typ);
2509 return Original_Bound;
2510 end if;
2511
2512 -- Normally, the best approach is simply to generate a constant
2513 -- declaration that captures the bound. However, there is a nasty
2514 -- case where this is wrong. If the bound is complex, and has a
2515 -- possible use of the secondary stack, we need to generate a
2516 -- separate assignment statement to ensure the creation of a block
2517 -- which will release the secondary stack.
2518
2519 -- We prefer the constant declaration, since it leaves us with a
2520 -- proper trace of the value, useful in optimizations that get rid
2521 -- of junk range checks.
2522
2523 if not Has_Call_Using_Secondary_Stack (Analyzed_Bound) then
2524 Analyze_And_Resolve (Original_Bound, Typ);
2525
2526 -- Ensure that the bound is valid. This check should not be
2527 -- generated when the range belongs to a quantified expression
2528 -- as the construct is still not expanded into its final form.
2529
2530 if Nkind (Parent (R)) /= N_Loop_Parameter_Specification
2531 or else Nkind (Parent (Parent (R))) /= N_Quantified_Expression
2532 then
2533 Ensure_Valid (Original_Bound);
2534 end if;
2535
2536 Force_Evaluation (Original_Bound);
2537 return Original_Bound;
2538 end if;
2539
2540 Id := Make_Temporary (Loc, 'R', Original_Bound);
2541
2542 -- Here we make a declaration with a separate assignment
2543 -- statement, and insert before loop header.
2544
2545 Decl :=
2546 Make_Object_Declaration (Loc,
2547 Defining_Identifier => Id,
2548 Object_Definition => New_Occurrence_Of (Typ, Loc));
2549
2550 Assign :=
2551 Make_Assignment_Statement (Loc,
2552 Name => New_Occurrence_Of (Id, Loc),
2553 Expression => Relocate_Node (Original_Bound));
2554
2555 Insert_Actions (Loop_Nod, New_List (Decl, Assign));
2556
2557 -- Now that this temporary variable is initialized we decorate it
2558 -- as safe-to-reevaluate to inform to the backend that no further
2559 -- asignment will be issued and hence it can be handled as side
2560 -- effect free. Note that this decoration must be done when the
2561 -- assignment has been analyzed because otherwise it will be
2562 -- rejected (see Analyze_Assignment).
2563
2564 Set_Is_Safe_To_Reevaluate (Id);
2565
2566 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
2567
2568 if Nkind (Assign) = N_Assignment_Statement then
2569 return Expression (Assign);
2570 else
2571 return Original_Bound;
2572 end if;
2573 end One_Bound;
2574
2575 Hi : constant Node_Id := High_Bound (R);
2576 Lo : constant Node_Id := Low_Bound (R);
2577 R_Copy : constant Node_Id := New_Copy_Tree (R);
2578 New_Hi : Node_Id;
2579 New_Lo : Node_Id;
2580 Typ : Entity_Id;
2581
2582 -- Start of processing for Process_Bounds
2583
2584 begin
2585 Set_Parent (R_Copy, Parent (R));
2586 Preanalyze_Range (R_Copy);
2587 Typ := Etype (R_Copy);
2588
2589 -- If the type of the discrete range is Universal_Integer, then the
2590 -- bound's type must be resolved to Integer, and any object used to
2591 -- hold the bound must also have type Integer, unless the literal
2592 -- bounds are constant-folded expressions with a user-defined type.
2593
2594 if Typ = Universal_Integer then
2595 if Nkind (Lo) = N_Integer_Literal
2596 and then Present (Etype (Lo))
2597 and then Scope (Etype (Lo)) /= Standard_Standard
2598 then
2599 Typ := Etype (Lo);
2600
2601 elsif Nkind (Hi) = N_Integer_Literal
2602 and then Present (Etype (Hi))
2603 and then Scope (Etype (Hi)) /= Standard_Standard
2604 then
2605 Typ := Etype (Hi);
2606
2607 else
2608 Typ := Standard_Integer;
2609 end if;
2610 end if;
2611
2612 Set_Etype (R, Typ);
2613
2614 New_Lo := One_Bound (Lo, Low_Bound (R_Copy), Typ);
2615 New_Hi := One_Bound (Hi, High_Bound (R_Copy), Typ);
2616
2617 -- Propagate staticness to loop range itself, in case the
2618 -- corresponding subtype is static.
2619
2620 if New_Lo /= Lo and then Is_OK_Static_Expression (New_Lo) then
2621 Rewrite (Low_Bound (R), New_Copy (New_Lo));
2622 end if;
2623
2624 if New_Hi /= Hi and then Is_OK_Static_Expression (New_Hi) then
2625 Rewrite (High_Bound (R), New_Copy (New_Hi));
2626 end if;
2627 end Process_Bounds;
2628
2629 -- Local variables
2630
2631 DS : constant Node_Id := Discrete_Subtype_Definition (N);
2632 Id : constant Entity_Id := Defining_Identifier (N);
2633
2634 DS_Copy : Node_Id;
2635
2636 -- Start of processing for Analyze_Loop_Parameter_Specification
2637
2638 begin
2639 Enter_Name (Id);
2640
2641 -- We always consider the loop variable to be referenced, since the loop
2642 -- may be used just for counting purposes.
2643
2644 Generate_Reference (Id, N, ' ');
2645
2646 -- Check for the case of loop variable hiding a local variable (used
2647 -- later on to give a nice warning if the hidden variable is never
2648 -- assigned).
2649
2650 declare
2651 H : constant Entity_Id := Homonym (Id);
2652 begin
2653 if Present (H)
2654 and then Ekind (H) = E_Variable
2655 and then Is_Discrete_Type (Etype (H))
2656 and then Enclosing_Dynamic_Scope (H) = Enclosing_Dynamic_Scope (Id)
2657 then
2658 Set_Hiding_Loop_Variable (H, Id);
2659 end if;
2660 end;
2661
2662 -- Loop parameter specification must include subtype mark in SPARK
2663
2664 if Nkind (DS) = N_Range then
2665 Check_SPARK_05_Restriction
2666 ("loop parameter specification must include subtype mark", N);
2667 end if;
2668
2669 -- Analyze the subtype definition and create temporaries for the bounds.
2670 -- Do not evaluate the range when preanalyzing a quantified expression
2671 -- because bounds expressed as function calls with side effects will be
2672 -- incorrectly replicated.
2673
2674 if Nkind (DS) = N_Range
2675 and then Expander_Active
2676 and then Nkind (Parent (N)) /= N_Quantified_Expression
2677 then
2678 Process_Bounds (DS);
2679
2680 -- Either the expander not active or the range of iteration is a subtype
2681 -- indication, an entity, or a function call that yields an aggregate or
2682 -- a container.
2683
2684 else
2685 DS_Copy := New_Copy_Tree (DS);
2686 Set_Parent (DS_Copy, Parent (DS));
2687 Preanalyze_Range (DS_Copy);
2688
2689 -- Ada 2012: If the domain of iteration is:
2690
2691 -- a) a function call,
2692 -- b) an identifier that is not a type,
2693 -- c) an attribute reference 'Old (within a postcondition)
2694 -- d) an unchecked conversion
2695
2696 -- then it is an iteration over a container. It was classified as
2697 -- a loop specification by the parser, and must be rewritten now
2698 -- to activate container iteration. The last case will occur within
2699 -- an expanded inlined call, where the expansion wraps an actual in
2700 -- an unchecked conversion when needed. The expression of the
2701 -- conversion is always an object.
2702
2703 if Nkind (DS_Copy) = N_Function_Call
2704 or else (Is_Entity_Name (DS_Copy)
2705 and then not Is_Type (Entity (DS_Copy)))
2706 or else (Nkind (DS_Copy) = N_Attribute_Reference
2707 and then Nam_In (Attribute_Name (DS_Copy),
2708 Name_Old, Name_Loop_Entry))
2709 or else Nkind (DS_Copy) = N_Unchecked_Type_Conversion
2710 or else Has_Aspect (Etype (DS_Copy), Aspect_Iterable)
2711 then
2712 -- This is an iterator specification. Rewrite it as such and
2713 -- analyze it to capture function calls that may require
2714 -- finalization actions.
2715
2716 declare
2717 I_Spec : constant Node_Id :=
2718 Make_Iterator_Specification (Sloc (N),
2719 Defining_Identifier => Relocate_Node (Id),
2720 Name => DS_Copy,
2721 Subtype_Indication => Empty,
2722 Reverse_Present => Reverse_Present (N));
2723 Scheme : constant Node_Id := Parent (N);
2724
2725 begin
2726 Set_Iterator_Specification (Scheme, I_Spec);
2727 Set_Loop_Parameter_Specification (Scheme, Empty);
2728 Analyze_Iterator_Specification (I_Spec);
2729
2730 -- In a generic context, analyze the original domain of
2731 -- iteration, for name capture.
2732
2733 if not Expander_Active then
2734 Analyze (DS);
2735 end if;
2736
2737 -- Set kind of loop parameter, which may be used in the
2738 -- subsequent analysis of the condition in a quantified
2739 -- expression.
2740
2741 Set_Ekind (Id, E_Loop_Parameter);
2742 return;
2743 end;
2744
2745 -- Domain of iteration is not a function call, and is side-effect
2746 -- free.
2747
2748 else
2749 -- A quantified expression that appears in a pre/post condition
2750 -- is pre-analyzed several times. If the range is given by an
2751 -- attribute reference it is rewritten as a range, and this is
2752 -- done even with expansion disabled. If the type is already set
2753 -- do not reanalyze, because a range with static bounds may be
2754 -- typed Integer by default.
2755
2756 if Nkind (Parent (N)) = N_Quantified_Expression
2757 and then Present (Etype (DS))
2758 then
2759 null;
2760 else
2761 Analyze (DS);
2762 end if;
2763 end if;
2764 end if;
2765
2766 if DS = Error then
2767 return;
2768 end if;
2769
2770 -- Some additional checks if we are iterating through a type
2771
2772 if Is_Entity_Name (DS)
2773 and then Present (Entity (DS))
2774 and then Is_Type (Entity (DS))
2775 then
2776 -- The subtype indication may denote the completion of an incomplete
2777 -- type declaration.
2778
2779 if Ekind (Entity (DS)) = E_Incomplete_Type then
2780 Set_Entity (DS, Get_Full_View (Entity (DS)));
2781 Set_Etype (DS, Entity (DS));
2782 end if;
2783
2784 Check_Predicate_Use (Entity (DS));
2785 end if;
2786
2787 -- Error if not discrete type
2788
2789 if not Is_Discrete_Type (Etype (DS)) then
2790 Wrong_Type (DS, Any_Discrete);
2791 Set_Etype (DS, Any_Type);
2792 end if;
2793
2794 Check_Controlled_Array_Attribute (DS);
2795
2796 if Nkind (DS) = N_Subtype_Indication then
2797 Check_Predicate_Use (Entity (Subtype_Mark (DS)));
2798 end if;
2799
2800 Make_Index (DS, N, In_Iter_Schm => True);
2801 Set_Ekind (Id, E_Loop_Parameter);
2802
2803 -- A quantified expression which appears in a pre- or post-condition may
2804 -- be analyzed multiple times. The analysis of the range creates several
2805 -- itypes which reside in different scopes depending on whether the pre-
2806 -- or post-condition has been expanded. Update the type of the loop
2807 -- variable to reflect the proper itype at each stage of analysis.
2808
2809 if No (Etype (Id))
2810 or else Etype (Id) = Any_Type
2811 or else
2812 (Present (Etype (Id))
2813 and then Is_Itype (Etype (Id))
2814 and then Nkind (Parent (Loop_Nod)) = N_Expression_With_Actions
2815 and then Nkind (Original_Node (Parent (Loop_Nod))) =
2816 N_Quantified_Expression)
2817 then
2818 Set_Etype (Id, Etype (DS));
2819 end if;
2820
2821 -- Treat a range as an implicit reference to the type, to inhibit
2822 -- spurious warnings.
2823
2824 Generate_Reference (Base_Type (Etype (DS)), N, ' ');
2825 Set_Is_Known_Valid (Id, True);
2826
2827 -- The loop is not a declarative part, so the loop variable must be
2828 -- frozen explicitly. Do not freeze while preanalyzing a quantified
2829 -- expression because the freeze node will not be inserted into the
2830 -- tree due to flag Is_Spec_Expression being set.
2831
2832 if Nkind (Parent (N)) /= N_Quantified_Expression then
2833 declare
2834 Flist : constant List_Id := Freeze_Entity (Id, N);
2835 begin
2836 if Is_Non_Empty_List (Flist) then
2837 Insert_Actions (N, Flist);
2838 end if;
2839 end;
2840 end if;
2841
2842 -- Case where we have a range or a subtype, get type bounds
2843
2844 if Nkind_In (DS, N_Range, N_Subtype_Indication)
2845 and then not Error_Posted (DS)
2846 and then Etype (DS) /= Any_Type
2847 and then Is_Discrete_Type (Etype (DS))
2848 then
2849 declare
2850 L : Node_Id;
2851 H : Node_Id;
2852
2853 begin
2854 if Nkind (DS) = N_Range then
2855 L := Low_Bound (DS);
2856 H := High_Bound (DS);
2857 else
2858 L :=
2859 Type_Low_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
2860 H :=
2861 Type_High_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
2862 end if;
2863
2864 -- Check for null or possibly null range and issue warning. We
2865 -- suppress such messages in generic templates and instances,
2866 -- because in practice they tend to be dubious in these cases. The
2867 -- check applies as well to rewritten array element loops where a
2868 -- null range may be detected statically.
2869
2870 if Compile_Time_Compare (L, H, Assume_Valid => True) = GT then
2871
2872 -- Suppress the warning if inside a generic template or
2873 -- instance, since in practice they tend to be dubious in these
2874 -- cases since they can result from intended parameterization.
2875
2876 if not Inside_A_Generic and then not In_Instance then
2877
2878 -- Specialize msg if invalid values could make the loop
2879 -- non-null after all.
2880
2881 if Compile_Time_Compare
2882 (L, H, Assume_Valid => False) = GT
2883 then
2884 -- Since we know the range of the loop is null, set the
2885 -- appropriate flag to remove the loop entirely during
2886 -- expansion.
2887
2888 Set_Is_Null_Loop (Loop_Nod);
2889
2890 if Comes_From_Source (N) then
2891 Error_Msg_N
2892 ("??loop range is null, loop will not execute", DS);
2893 end if;
2894
2895 -- Here is where the loop could execute because of
2896 -- invalid values, so issue appropriate message and in
2897 -- this case we do not set the Is_Null_Loop flag since
2898 -- the loop may execute.
2899
2900 elsif Comes_From_Source (N) then
2901 Error_Msg_N
2902 ("??loop range may be null, loop may not execute",
2903 DS);
2904 Error_Msg_N
2905 ("??can only execute if invalid values are present",
2906 DS);
2907 end if;
2908 end if;
2909
2910 -- In either case, suppress warnings in the body of the loop,
2911 -- since it is likely that these warnings will be inappropriate
2912 -- if the loop never actually executes, which is likely.
2913
2914 Set_Suppress_Loop_Warnings (Loop_Nod);
2915
2916 -- The other case for a warning is a reverse loop where the
2917 -- upper bound is the integer literal zero or one, and the
2918 -- lower bound may exceed this value.
2919
2920 -- For example, we have
2921
2922 -- for J in reverse N .. 1 loop
2923
2924 -- In practice, this is very likely to be a case of reversing
2925 -- the bounds incorrectly in the range.
2926
2927 elsif Reverse_Present (N)
2928 and then Nkind (Original_Node (H)) = N_Integer_Literal
2929 and then
2930 (Intval (Original_Node (H)) = Uint_0
2931 or else
2932 Intval (Original_Node (H)) = Uint_1)
2933 then
2934 -- Lower bound may in fact be known and known not to exceed
2935 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
2936
2937 if Compile_Time_Known_Value (L)
2938 and then Expr_Value (L) <= Expr_Value (H)
2939 then
2940 null;
2941
2942 -- Otherwise warning is warranted
2943
2944 else
2945 Error_Msg_N ("??loop range may be null", DS);
2946 Error_Msg_N ("\??bounds may be wrong way round", DS);
2947 end if;
2948 end if;
2949
2950 -- Check if either bound is known to be outside the range of the
2951 -- loop parameter type, this is e.g. the case of a loop from
2952 -- 20..X where the type is 1..19.
2953
2954 -- Such a loop is dubious since either it raises CE or it executes
2955 -- zero times, and that cannot be useful!
2956
2957 if Etype (DS) /= Any_Type
2958 and then not Error_Posted (DS)
2959 and then Nkind (DS) = N_Subtype_Indication
2960 and then Nkind (Constraint (DS)) = N_Range_Constraint
2961 then
2962 declare
2963 LLo : constant Node_Id :=
2964 Low_Bound (Range_Expression (Constraint (DS)));
2965 LHi : constant Node_Id :=
2966 High_Bound (Range_Expression (Constraint (DS)));
2967
2968 Bad_Bound : Node_Id := Empty;
2969 -- Suspicious loop bound
2970
2971 begin
2972 -- At this stage L, H are the bounds of the type, and LLo
2973 -- Lhi are the low bound and high bound of the loop.
2974
2975 if Compile_Time_Compare (LLo, L, Assume_Valid => True) = LT
2976 or else
2977 Compile_Time_Compare (LLo, H, Assume_Valid => True) = GT
2978 then
2979 Bad_Bound := LLo;
2980 end if;
2981
2982 if Compile_Time_Compare (LHi, L, Assume_Valid => True) = LT
2983 or else
2984 Compile_Time_Compare (LHi, H, Assume_Valid => True) = GT
2985 then
2986 Bad_Bound := LHi;
2987 end if;
2988
2989 if Present (Bad_Bound) then
2990 Error_Msg_N
2991 ("suspicious loop bound out of range of "
2992 & "loop subtype??", Bad_Bound);
2993 Error_Msg_N
2994 ("\loop executes zero times or raises "
2995 & "Constraint_Error??", Bad_Bound);
2996 end if;
2997 end;
2998 end if;
2999
3000 -- This declare block is about warnings, if we get an exception while
3001 -- testing for warnings, we simply abandon the attempt silently. This
3002 -- most likely occurs as the result of a previous error, but might
3003 -- just be an obscure case we have missed. In either case, not giving
3004 -- the warning is perfectly acceptable.
3005
3006 exception
3007 when others => null;
3008 end;
3009 end if;
3010
3011 -- A loop parameter cannot be effectively volatile. This check is
3012 -- peformed only when SPARK_Mode is on as it is not a standard Ada
3013 -- legality check (SPARK RM 7.1.3(6)).
3014
3015 if SPARK_Mode = On and then Is_Effectively_Volatile (Id) then
3016 Error_Msg_N ("loop parameter cannot be volatile", Id);
3017 end if;
3018 end Analyze_Loop_Parameter_Specification;
3019
3020 ----------------------------
3021 -- Analyze_Loop_Statement --
3022 ----------------------------
3023
3024 procedure Analyze_Loop_Statement (N : Node_Id) is
3025
3026 function Is_Container_Iterator (Iter : Node_Id) return Boolean;
3027 -- Given a loop iteration scheme, determine whether it is an Ada 2012
3028 -- container iteration.
3029
3030 function Is_Wrapped_In_Block (N : Node_Id) return Boolean;
3031 -- Determine whether loop statement N has been wrapped in a block to
3032 -- capture finalization actions that may be generated for container
3033 -- iterators. Prevents infinite recursion when block is analyzed.
3034 -- Routine is a noop if loop is single statement within source block.
3035
3036 ---------------------------
3037 -- Is_Container_Iterator --
3038 ---------------------------
3039
3040 function Is_Container_Iterator (Iter : Node_Id) return Boolean is
3041 begin
3042 -- Infinite loop
3043
3044 if No (Iter) then
3045 return False;
3046
3047 -- While loop
3048
3049 elsif Present (Condition (Iter)) then
3050 return False;
3051
3052 -- for Def_Id in [reverse] Name loop
3053 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
3054
3055 elsif Present (Iterator_Specification (Iter)) then
3056 declare
3057 Nam : constant Node_Id := Name (Iterator_Specification (Iter));
3058 Nam_Copy : Node_Id;
3059
3060 begin
3061 Nam_Copy := New_Copy_Tree (Nam);
3062 Set_Parent (Nam_Copy, Parent (Nam));
3063 Preanalyze_Range (Nam_Copy);
3064
3065 -- The only two options here are iteration over a container or
3066 -- an array.
3067
3068 return not Is_Array_Type (Etype (Nam_Copy));
3069 end;
3070
3071 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
3072
3073 else
3074 declare
3075 LP : constant Node_Id := Loop_Parameter_Specification (Iter);
3076 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
3077 DS_Copy : Node_Id;
3078
3079 begin
3080 DS_Copy := New_Copy_Tree (DS);
3081 Set_Parent (DS_Copy, Parent (DS));
3082 Preanalyze_Range (DS_Copy);
3083
3084 -- Check for a call to Iterate ()
3085
3086 return
3087 Nkind (DS_Copy) = N_Function_Call
3088 and then Needs_Finalization (Etype (DS_Copy));
3089 end;
3090 end if;
3091 end Is_Container_Iterator;
3092
3093 -------------------------
3094 -- Is_Wrapped_In_Block --
3095 -------------------------
3096
3097 function Is_Wrapped_In_Block (N : Node_Id) return Boolean is
3098 HSS : Node_Id;
3099 Stat : Node_Id;
3100
3101 begin
3102
3103 -- Check if current scope is a block that is not a transient block.
3104
3105 if Ekind (Current_Scope) /= E_Block
3106 or else No (Block_Node (Current_Scope))
3107 then
3108 return False;
3109
3110 else
3111 HSS :=
3112 Handled_Statement_Sequence (Parent (Block_Node (Current_Scope)));
3113
3114 -- Skip leading pragmas that may be introduced for invariant and
3115 -- predicate checks.
3116
3117 Stat := First (Statements (HSS));
3118 while Present (Stat) and then Nkind (Stat) = N_Pragma loop
3119 Stat := Next (Stat);
3120 end loop;
3121
3122 return Stat = N and then No (Next (Stat));
3123 end if;
3124 end Is_Wrapped_In_Block;
3125
3126 -- Local declarations
3127
3128 Id : constant Node_Id := Identifier (N);
3129 Iter : constant Node_Id := Iteration_Scheme (N);
3130 Loc : constant Source_Ptr := Sloc (N);
3131 Ent : Entity_Id;
3132 Stmt : Node_Id;
3133
3134 -- Start of processing for Analyze_Loop_Statement
3135
3136 begin
3137 if Present (Id) then
3138
3139 -- Make name visible, e.g. for use in exit statements. Loop labels
3140 -- are always considered to be referenced.
3141
3142 Analyze (Id);
3143 Ent := Entity (Id);
3144
3145 -- Guard against serious error (typically, a scope mismatch when
3146 -- semantic analysis is requested) by creating loop entity to
3147 -- continue analysis.
3148
3149 if No (Ent) then
3150 if Total_Errors_Detected /= 0 then
3151 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3152 else
3153 raise Program_Error;
3154 end if;
3155
3156 -- Verify that the loop name is hot hidden by an unrelated
3157 -- declaration in an inner scope.
3158
3159 elsif Ekind (Ent) /= E_Label and then Ekind (Ent) /= E_Loop then
3160 Error_Msg_Sloc := Sloc (Ent);
3161 Error_Msg_N ("implicit label declaration for & is hidden#", Id);
3162
3163 if Present (Homonym (Ent))
3164 and then Ekind (Homonym (Ent)) = E_Label
3165 then
3166 Set_Entity (Id, Ent);
3167 Set_Ekind (Ent, E_Loop);
3168 end if;
3169
3170 else
3171 Generate_Reference (Ent, N, ' ');
3172 Generate_Definition (Ent);
3173
3174 -- If we found a label, mark its type. If not, ignore it, since it
3175 -- means we have a conflicting declaration, which would already
3176 -- have been diagnosed at declaration time. Set Label_Construct
3177 -- of the implicit label declaration, which is not created by the
3178 -- parser for generic units.
3179
3180 if Ekind (Ent) = E_Label then
3181 Set_Ekind (Ent, E_Loop);
3182
3183 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
3184 Set_Label_Construct (Parent (Ent), N);
3185 end if;
3186 end if;
3187 end if;
3188
3189 -- Case of no identifier present
3190
3191 else
3192 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3193 Set_Etype (Ent, Standard_Void_Type);
3194 Set_Parent (Ent, N);
3195 end if;
3196
3197 -- Iteration over a container in Ada 2012 involves the creation of a
3198 -- controlled iterator object. Wrap the loop in a block to ensure the
3199 -- timely finalization of the iterator and release of container locks.
3200 -- The same applies to the use of secondary stack when obtaining an
3201 -- iterator.
3202
3203 if Ada_Version >= Ada_2012
3204 and then Is_Container_Iterator (Iter)
3205 and then not Is_Wrapped_In_Block (N)
3206 then
3207 declare
3208 Block_Nod : Node_Id;
3209 Block_Id : Entity_Id;
3210
3211 begin
3212 Block_Nod :=
3213 Make_Block_Statement (Loc,
3214 Declarations => New_List,
3215 Handled_Statement_Sequence =>
3216 Make_Handled_Sequence_Of_Statements (Loc,
3217 Statements => New_List (Relocate_Node (N))));
3218
3219 Add_Block_Identifier (Block_Nod, Block_Id);
3220
3221 -- The expansion of iterator loops generates an iterator in order
3222 -- to traverse the elements of a container:
3223
3224 -- Iter : <iterator type> := Iterate (Container)'reference;
3225
3226 -- The iterator is controlled and returned on the secondary stack.
3227 -- The analysis of the call to Iterate establishes a transient
3228 -- scope to deal with the secondary stack management, but never
3229 -- really creates a physical block as this would kill the iterator
3230 -- too early (see Wrap_Transient_Declaration). To address this
3231 -- case, mark the generated block as needing secondary stack
3232 -- management.
3233
3234 Set_Uses_Sec_Stack (Block_Id);
3235
3236 Rewrite (N, Block_Nod);
3237 Analyze (N);
3238 return;
3239 end;
3240 end if;
3241
3242 -- Kill current values on entry to loop, since statements in the body of
3243 -- the loop may have been executed before the loop is entered. Similarly
3244 -- we kill values after the loop, since we do not know that the body of
3245 -- the loop was executed.
3246
3247 Kill_Current_Values;
3248 Push_Scope (Ent);
3249 Analyze_Iteration_Scheme (Iter);
3250
3251 -- Check for following case which merits a warning if the type E of is
3252 -- a multi-dimensional array (and no explicit subscript ranges present).
3253
3254 -- for J in E'Range
3255 -- for K in E'Range
3256
3257 if Present (Iter)
3258 and then Present (Loop_Parameter_Specification (Iter))
3259 then
3260 declare
3261 LPS : constant Node_Id := Loop_Parameter_Specification (Iter);
3262 DSD : constant Node_Id :=
3263 Original_Node (Discrete_Subtype_Definition (LPS));
3264 begin
3265 if Nkind (DSD) = N_Attribute_Reference
3266 and then Attribute_Name (DSD) = Name_Range
3267 and then No (Expressions (DSD))
3268 then
3269 declare
3270 Typ : constant Entity_Id := Etype (Prefix (DSD));
3271 begin
3272 if Is_Array_Type (Typ)
3273 and then Number_Dimensions (Typ) > 1
3274 and then Nkind (Parent (N)) = N_Loop_Statement
3275 and then Present (Iteration_Scheme (Parent (N)))
3276 then
3277 declare
3278 OIter : constant Node_Id :=
3279 Iteration_Scheme (Parent (N));
3280 OLPS : constant Node_Id :=
3281 Loop_Parameter_Specification (OIter);
3282 ODSD : constant Node_Id :=
3283 Original_Node (Discrete_Subtype_Definition (OLPS));
3284 begin
3285 if Nkind (ODSD) = N_Attribute_Reference
3286 and then Attribute_Name (ODSD) = Name_Range
3287 and then No (Expressions (ODSD))
3288 and then Etype (Prefix (ODSD)) = Typ
3289 then
3290 Error_Msg_Sloc := Sloc (ODSD);
3291 Error_Msg_N
3292 ("inner range same as outer range#??", DSD);
3293 end if;
3294 end;
3295 end if;
3296 end;
3297 end if;
3298 end;
3299 end if;
3300
3301 -- Analyze the statements of the body except in the case of an Ada 2012
3302 -- iterator with the expander active. In this case the expander will do
3303 -- a rewrite of the loop into a while loop. We will then analyze the
3304 -- loop body when we analyze this while loop.
3305
3306 -- We need to do this delay because if the container is for indefinite
3307 -- types the actual subtype of the components will only be determined
3308 -- when the cursor declaration is analyzed.
3309
3310 -- If the expander is not active, or in SPARK mode, then we want to
3311 -- analyze the loop body now even in the Ada 2012 iterator case, since
3312 -- the rewriting will not be done. Insert the loop variable in the
3313 -- current scope, if not done when analysing the iteration scheme.
3314 -- Set its kind properly to detect improper uses in the loop body.
3315
3316 if Present (Iter)
3317 and then Present (Iterator_Specification (Iter))
3318 then
3319 if not Expander_Active then
3320 declare
3321 I_Spec : constant Node_Id := Iterator_Specification (Iter);
3322 Id : constant Entity_Id := Defining_Identifier (I_Spec);
3323
3324 begin
3325 if Scope (Id) /= Current_Scope then
3326 Enter_Name (Id);
3327 end if;
3328
3329 -- In an element iterator, The loop parameter is a variable if
3330 -- the domain of iteration (container or array) is a variable.
3331
3332 if not Of_Present (I_Spec)
3333 or else not Is_Variable (Name (I_Spec))
3334 then
3335 Set_Ekind (Id, E_Loop_Parameter);
3336 end if;
3337 end;
3338
3339 Analyze_Statements (Statements (N));
3340 end if;
3341
3342 else
3343
3344 -- Pre-Ada2012 for-loops and while loops.
3345
3346 Analyze_Statements (Statements (N));
3347 end if;
3348
3349 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
3350 -- the loop is transformed into a conditional block. Retrieve the loop.
3351
3352 Stmt := N;
3353
3354 if Subject_To_Loop_Entry_Attributes (Stmt) then
3355 Stmt := Find_Loop_In_Conditional_Block (Stmt);
3356 end if;
3357
3358 -- Finish up processing for the loop. We kill all current values, since
3359 -- in general we don't know if the statements in the loop have been
3360 -- executed. We could do a bit better than this with a loop that we
3361 -- know will execute at least once, but it's not worth the trouble and
3362 -- the front end is not in the business of flow tracing.
3363
3364 Process_End_Label (Stmt, 'e', Ent);
3365 End_Scope;
3366 Kill_Current_Values;
3367
3368 -- Check for infinite loop. Skip check for generated code, since it
3369 -- justs waste time and makes debugging the routine called harder.
3370
3371 -- Note that we have to wait till the body of the loop is fully analyzed
3372 -- before making this call, since Check_Infinite_Loop_Warning relies on
3373 -- being able to use semantic visibility information to find references.
3374
3375 if Comes_From_Source (Stmt) then
3376 Check_Infinite_Loop_Warning (Stmt);
3377 end if;
3378
3379 -- Code after loop is unreachable if the loop has no WHILE or FOR and
3380 -- contains no EXIT statements within the body of the loop.
3381
3382 if No (Iter) and then not Has_Exit (Ent) then
3383 Check_Unreachable_Code (Stmt);
3384 end if;
3385 end Analyze_Loop_Statement;
3386
3387 ----------------------------
3388 -- Analyze_Null_Statement --
3389 ----------------------------
3390
3391 -- Note: the semantics of the null statement is implemented by a single
3392 -- null statement, too bad everything isn't as simple as this.
3393
3394 procedure Analyze_Null_Statement (N : Node_Id) is
3395 pragma Warnings (Off, N);
3396 begin
3397 null;
3398 end Analyze_Null_Statement;
3399
3400 ------------------------
3401 -- Analyze_Statements --
3402 ------------------------
3403
3404 procedure Analyze_Statements (L : List_Id) is
3405 S : Node_Id;
3406 Lab : Entity_Id;
3407
3408 begin
3409 -- The labels declared in the statement list are reachable from
3410 -- statements in the list. We do this as a prepass so that any goto
3411 -- statement will be properly flagged if its target is not reachable.
3412 -- This is not required, but is nice behavior.
3413
3414 S := First (L);
3415 while Present (S) loop
3416 if Nkind (S) = N_Label then
3417 Analyze (Identifier (S));
3418 Lab := Entity (Identifier (S));
3419
3420 -- If we found a label mark it as reachable
3421
3422 if Ekind (Lab) = E_Label then
3423 Generate_Definition (Lab);
3424 Set_Reachable (Lab);
3425
3426 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
3427 Set_Label_Construct (Parent (Lab), S);
3428 end if;
3429
3430 -- If we failed to find a label, it means the implicit declaration
3431 -- of the label was hidden. A for-loop parameter can do this to
3432 -- a label with the same name inside the loop, since the implicit
3433 -- label declaration is in the innermost enclosing body or block
3434 -- statement.
3435
3436 else
3437 Error_Msg_Sloc := Sloc (Lab);
3438 Error_Msg_N
3439 ("implicit label declaration for & is hidden#",
3440 Identifier (S));
3441 end if;
3442 end if;
3443
3444 Next (S);
3445 end loop;
3446
3447 -- Perform semantic analysis on all statements
3448
3449 Conditional_Statements_Begin;
3450
3451 S := First (L);
3452 while Present (S) loop
3453 Analyze (S);
3454
3455 -- Remove dimension in all statements
3456
3457 Remove_Dimension_In_Statement (S);
3458 Next (S);
3459 end loop;
3460
3461 Conditional_Statements_End;
3462
3463 -- Make labels unreachable. Visibility is not sufficient, because labels
3464 -- in one if-branch for example are not reachable from the other branch,
3465 -- even though their declarations are in the enclosing declarative part.
3466
3467 S := First (L);
3468 while Present (S) loop
3469 if Nkind (S) = N_Label then
3470 Set_Reachable (Entity (Identifier (S)), False);
3471 end if;
3472
3473 Next (S);
3474 end loop;
3475 end Analyze_Statements;
3476
3477 ----------------------------
3478 -- Check_Unreachable_Code --
3479 ----------------------------
3480
3481 procedure Check_Unreachable_Code (N : Node_Id) is
3482 Error_Node : Node_Id;
3483 P : Node_Id;
3484
3485 begin
3486 if Is_List_Member (N) and then Comes_From_Source (N) then
3487 declare
3488 Nxt : Node_Id;
3489
3490 begin
3491 Nxt := Original_Node (Next (N));
3492
3493 -- Skip past pragmas
3494
3495 while Nkind (Nxt) = N_Pragma loop
3496 Nxt := Original_Node (Next (Nxt));
3497 end loop;
3498
3499 -- If a label follows us, then we never have dead code, since
3500 -- someone could branch to the label, so we just ignore it, unless
3501 -- we are in formal mode where goto statements are not allowed.
3502
3503 if Nkind (Nxt) = N_Label
3504 and then not Restriction_Check_Required (SPARK_05)
3505 then
3506 return;
3507
3508 -- Otherwise see if we have a real statement following us
3509
3510 elsif Present (Nxt)
3511 and then Comes_From_Source (Nxt)
3512 and then Is_Statement (Nxt)
3513 then
3514 -- Special very annoying exception. If we have a return that
3515 -- follows a raise, then we allow it without a warning, since
3516 -- the Ada RM annoyingly requires a useless return here.
3517
3518 if Nkind (Original_Node (N)) /= N_Raise_Statement
3519 or else Nkind (Nxt) /= N_Simple_Return_Statement
3520 then
3521 -- The rather strange shenanigans with the warning message
3522 -- here reflects the fact that Kill_Dead_Code is very good
3523 -- at removing warnings in deleted code, and this is one
3524 -- warning we would prefer NOT to have removed.
3525
3526 Error_Node := Nxt;
3527
3528 -- If we have unreachable code, analyze and remove the
3529 -- unreachable code, since it is useless and we don't
3530 -- want to generate junk warnings.
3531
3532 -- We skip this step if we are not in code generation mode
3533 -- or CodePeer mode.
3534
3535 -- This is the one case where we remove dead code in the
3536 -- semantics as opposed to the expander, and we do not want
3537 -- to remove code if we are not in code generation mode,
3538 -- since this messes up the ASIS trees or loses useful
3539 -- information in the CodePeer tree.
3540
3541 -- Note that one might react by moving the whole circuit to
3542 -- exp_ch5, but then we lose the warning in -gnatc mode.
3543
3544 if Operating_Mode = Generate_Code
3545 and then not CodePeer_Mode
3546 then
3547 loop
3548 Nxt := Next (N);
3549
3550 -- Quit deleting when we have nothing more to delete
3551 -- or if we hit a label (since someone could transfer
3552 -- control to a label, so we should not delete it).
3553
3554 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
3555
3556 -- Statement/declaration is to be deleted
3557
3558 Analyze (Nxt);
3559 Remove (Nxt);
3560 Kill_Dead_Code (Nxt);
3561 end loop;
3562 end if;
3563
3564 -- Now issue the warning (or error in formal mode)
3565
3566 if Restriction_Check_Required (SPARK_05) then
3567 Check_SPARK_05_Restriction
3568 ("unreachable code is not allowed", Error_Node);
3569 else
3570 Error_Msg ("??unreachable code!", Sloc (Error_Node));
3571 end if;
3572 end if;
3573
3574 -- If the unconditional transfer of control instruction is the
3575 -- last statement of a sequence, then see if our parent is one of
3576 -- the constructs for which we count unblocked exits, and if so,
3577 -- adjust the count.
3578
3579 else
3580 P := Parent (N);
3581
3582 -- Statements in THEN part or ELSE part of IF statement
3583
3584 if Nkind (P) = N_If_Statement then
3585 null;
3586
3587 -- Statements in ELSIF part of an IF statement
3588
3589 elsif Nkind (P) = N_Elsif_Part then
3590 P := Parent (P);
3591 pragma Assert (Nkind (P) = N_If_Statement);
3592
3593 -- Statements in CASE statement alternative
3594
3595 elsif Nkind (P) = N_Case_Statement_Alternative then
3596 P := Parent (P);
3597 pragma Assert (Nkind (P) = N_Case_Statement);
3598
3599 -- Statements in body of block
3600
3601 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
3602 and then Nkind (Parent (P)) = N_Block_Statement
3603 then
3604 -- The original loop is now placed inside a block statement
3605 -- due to the expansion of attribute 'Loop_Entry. Return as
3606 -- this is not a "real" block for the purposes of exit
3607 -- counting.
3608
3609 if Nkind (N) = N_Loop_Statement
3610 and then Subject_To_Loop_Entry_Attributes (N)
3611 then
3612 return;
3613 end if;
3614
3615 -- Statements in exception handler in a block
3616
3617 elsif Nkind (P) = N_Exception_Handler
3618 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
3619 and then Nkind (Parent (Parent (P))) = N_Block_Statement
3620 then
3621 null;
3622
3623 -- None of these cases, so return
3624
3625 else
3626 return;
3627 end if;
3628
3629 -- This was one of the cases we are looking for (i.e. the
3630 -- parent construct was IF, CASE or block) so decrement count.
3631
3632 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
3633 end if;
3634 end;
3635 end if;
3636 end Check_Unreachable_Code;
3637
3638 ----------------------
3639 -- Preanalyze_Range --
3640 ----------------------
3641
3642 procedure Preanalyze_Range (R_Copy : Node_Id) is
3643 Save_Analysis : constant Boolean := Full_Analysis;
3644 Typ : Entity_Id;
3645
3646 begin
3647 Full_Analysis := False;
3648 Expander_Mode_Save_And_Set (False);
3649
3650 Analyze (R_Copy);
3651
3652 if Nkind (R_Copy) in N_Subexpr and then Is_Overloaded (R_Copy) then
3653
3654 -- Apply preference rules for range of predefined integer types, or
3655 -- diagnose true ambiguity.
3656
3657 declare
3658 I : Interp_Index;
3659 It : Interp;
3660 Found : Entity_Id := Empty;
3661
3662 begin
3663 Get_First_Interp (R_Copy, I, It);
3664 while Present (It.Typ) loop
3665 if Is_Discrete_Type (It.Typ) then
3666 if No (Found) then
3667 Found := It.Typ;
3668 else
3669 if Scope (Found) = Standard_Standard then
3670 null;
3671
3672 elsif Scope (It.Typ) = Standard_Standard then
3673 Found := It.Typ;
3674
3675 else
3676 -- Both of them are user-defined
3677
3678 Error_Msg_N
3679 ("ambiguous bounds in range of iteration", R_Copy);
3680 Error_Msg_N ("\possible interpretations:", R_Copy);
3681 Error_Msg_NE ("\\} ", R_Copy, Found);
3682 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
3683 exit;
3684 end if;
3685 end if;
3686 end if;
3687
3688 Get_Next_Interp (I, It);
3689 end loop;
3690 end;
3691 end if;
3692
3693 -- Subtype mark in iteration scheme
3694
3695 if Is_Entity_Name (R_Copy) and then Is_Type (Entity (R_Copy)) then
3696 null;
3697
3698 -- Expression in range, or Ada 2012 iterator
3699
3700 elsif Nkind (R_Copy) in N_Subexpr then
3701 Resolve (R_Copy);
3702 Typ := Etype (R_Copy);
3703
3704 if Is_Discrete_Type (Typ) then
3705 null;
3706
3707 -- Check that the resulting object is an iterable container
3708
3709 elsif Has_Aspect (Typ, Aspect_Iterator_Element)
3710 or else Has_Aspect (Typ, Aspect_Constant_Indexing)
3711 or else Has_Aspect (Typ, Aspect_Variable_Indexing)
3712 then
3713 null;
3714
3715 -- The expression may yield an implicit reference to an iterable
3716 -- container. Insert explicit dereference so that proper type is
3717 -- visible in the loop.
3718
3719 elsif Has_Implicit_Dereference (Etype (R_Copy)) then
3720 declare
3721 Disc : Entity_Id;
3722
3723 begin
3724 Disc := First_Discriminant (Typ);
3725 while Present (Disc) loop
3726 if Has_Implicit_Dereference (Disc) then
3727 Build_Explicit_Dereference (R_Copy, Disc);
3728 exit;
3729 end if;
3730
3731 Next_Discriminant (Disc);
3732 end loop;
3733 end;
3734
3735 end if;
3736 end if;
3737
3738 Expander_Mode_Restore;
3739 Full_Analysis := Save_Analysis;
3740 end Preanalyze_Range;
3741
3742 end Sem_Ch5;