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