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