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