sem_ch5.adb (Analyze_Loop_Parameter_Specification): If the domain of iteration is...
[official-gcc.git] / gcc / ada / sem_ch5.adb
blobead28bff0ac7313ccdadfad71b70d94fdf24076f
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-2013, 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);
138 -- Renamings of protected private components are turned into
139 -- constants when compiling a protected function. In the case
140 -- of single protected types, the private component appears
141 -- directly.
143 elsif (Is_Prival (Ent)
144 and then
145 (Ekind (Current_Scope) = E_Function
146 or else Ekind (Enclosing_Dynamic_Scope
147 (Current_Scope)) = E_Function))
148 or else
149 (Ekind (Ent) = E_Component
150 and then Is_Protected_Type (Scope (Ent)))
151 then
152 Error_Msg_N
153 ("protected function cannot modify protected object", N);
155 elsif Ekind (Ent) = E_Loop_Parameter then
156 Error_Msg_N
157 ("assignment to loop parameter not allowed", N);
159 else
160 Error_Msg_N
161 ("left hand side of assignment must be a variable", N);
162 end if;
163 end;
165 -- For indexed components or selected components, test prefix
167 elsif Nkind (N) = N_Indexed_Component then
168 Diagnose_Non_Variable_Lhs (Prefix (N));
170 -- Another special case for assignment to discriminant
172 elsif Nkind (N) = N_Selected_Component then
173 if Present (Entity (Selector_Name (N)))
174 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
175 then
176 Error_Msg_N
177 ("assignment to discriminant not allowed", N);
178 else
179 Diagnose_Non_Variable_Lhs (Prefix (N));
180 end if;
182 else
183 -- If we fall through, we have no special message to issue
185 Error_Msg_N ("left hand side of assignment must be a variable", N);
186 end if;
187 end Diagnose_Non_Variable_Lhs;
189 --------------
190 -- Kill_Lhs --
191 --------------
193 procedure Kill_Lhs is
194 begin
195 if Is_Entity_Name (Lhs) then
196 declare
197 Ent : constant Entity_Id := Entity (Lhs);
198 begin
199 if Present (Ent) then
200 Kill_Current_Values (Ent);
201 end if;
202 end;
203 end if;
204 end Kill_Lhs;
206 -------------------------
207 -- Set_Assignment_Type --
208 -------------------------
210 procedure Set_Assignment_Type
211 (Opnd : Node_Id;
212 Opnd_Type : in out Entity_Id)
214 begin
215 Require_Entity (Opnd);
217 -- If the assignment operand is an in-out or out parameter, then we
218 -- get the actual subtype (needed for the unconstrained case). If the
219 -- operand is the actual in an entry declaration, then within the
220 -- accept statement it is replaced with a local renaming, which may
221 -- also have an actual subtype.
223 if Is_Entity_Name (Opnd)
224 and then (Ekind (Entity (Opnd)) = E_Out_Parameter
225 or else Ekind_In (Entity (Opnd),
226 E_In_Out_Parameter,
227 E_Generic_In_Out_Parameter)
228 or else
229 (Ekind (Entity (Opnd)) = E_Variable
230 and then Nkind (Parent (Entity (Opnd))) =
231 N_Object_Renaming_Declaration
232 and then Nkind (Parent (Parent (Entity (Opnd)))) =
233 N_Accept_Statement))
234 then
235 Opnd_Type := Get_Actual_Subtype (Opnd);
237 -- If assignment operand is a component reference, then we get the
238 -- actual subtype of the component for the unconstrained case.
240 elsif Nkind_In (Opnd, N_Selected_Component, N_Explicit_Dereference)
241 and then not Is_Unchecked_Union (Opnd_Type)
242 then
243 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
245 if Present (Decl) then
246 Insert_Action (N, Decl);
247 Mark_Rewrite_Insertion (Decl);
248 Analyze (Decl);
249 Opnd_Type := Defining_Identifier (Decl);
250 Set_Etype (Opnd, Opnd_Type);
251 Freeze_Itype (Opnd_Type, N);
253 elsif Is_Constrained (Etype (Opnd)) then
254 Opnd_Type := Etype (Opnd);
255 end if;
257 -- For slice, use the constrained subtype created for the slice
259 elsif Nkind (Opnd) = N_Slice then
260 Opnd_Type := Etype (Opnd);
261 end if;
262 end Set_Assignment_Type;
264 -- Start of processing for Analyze_Assignment
266 begin
267 Mark_Coextensions (N, Rhs);
269 Analyze (Rhs);
270 Analyze (Lhs);
272 -- Ensure that we never do an assignment on a variable marked as
273 -- as Safe_To_Reevaluate.
275 pragma Assert (not Is_Entity_Name (Lhs)
276 or else Ekind (Entity (Lhs)) /= E_Variable
277 or else not Is_Safe_To_Reevaluate (Entity (Lhs)));
279 -- Start type analysis for assignment
281 T1 := Etype (Lhs);
283 -- In the most general case, both Lhs and Rhs can be overloaded, and we
284 -- must compute the intersection of the possible types on each side.
286 if Is_Overloaded (Lhs) then
287 declare
288 I : Interp_Index;
289 It : Interp;
291 begin
292 T1 := Any_Type;
293 Get_First_Interp (Lhs, I, It);
295 while Present (It.Typ) loop
296 if Has_Compatible_Type (Rhs, It.Typ) then
297 if T1 /= Any_Type then
299 -- An explicit dereference is overloaded if the prefix
300 -- is. Try to remove the ambiguity on the prefix, the
301 -- error will be posted there if the ambiguity is real.
303 if Nkind (Lhs) = N_Explicit_Dereference then
304 declare
305 PI : Interp_Index;
306 PI1 : Interp_Index := 0;
307 PIt : Interp;
308 Found : Boolean;
310 begin
311 Found := False;
312 Get_First_Interp (Prefix (Lhs), PI, PIt);
314 while Present (PIt.Typ) loop
315 if Is_Access_Type (PIt.Typ)
316 and then Has_Compatible_Type
317 (Rhs, Designated_Type (PIt.Typ))
318 then
319 if Found then
320 PIt :=
321 Disambiguate (Prefix (Lhs),
322 PI1, PI, Any_Type);
324 if PIt = No_Interp then
325 Error_Msg_N
326 ("ambiguous left-hand side"
327 & " in assignment", Lhs);
328 exit;
329 else
330 Resolve (Prefix (Lhs), PIt.Typ);
331 end if;
333 exit;
334 else
335 Found := True;
336 PI1 := PI;
337 end if;
338 end if;
340 Get_Next_Interp (PI, PIt);
341 end loop;
342 end;
344 else
345 Error_Msg_N
346 ("ambiguous left-hand side in assignment", Lhs);
347 exit;
348 end if;
349 else
350 T1 := It.Typ;
351 end if;
352 end if;
354 Get_Next_Interp (I, It);
355 end loop;
356 end;
358 if T1 = Any_Type then
359 Error_Msg_N
360 ("no valid types for left-hand side for assignment", Lhs);
361 Kill_Lhs;
362 return;
363 end if;
364 end if;
366 -- The resulting assignment type is T1, so now we will resolve the left
367 -- hand side of the assignment using this determined type.
369 Resolve (Lhs, T1);
371 -- Cases where Lhs is not a variable
373 if not Is_Variable (Lhs) then
375 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
376 -- protected object.
378 declare
379 Ent : Entity_Id;
380 S : Entity_Id;
382 begin
383 if Ada_Version >= Ada_2005 then
385 -- Handle chains of renamings
387 Ent := Lhs;
388 while Nkind (Ent) in N_Has_Entity
389 and then Present (Entity (Ent))
390 and then Present (Renamed_Object (Entity (Ent)))
391 loop
392 Ent := Renamed_Object (Entity (Ent));
393 end loop;
395 if (Nkind (Ent) = N_Attribute_Reference
396 and then Attribute_Name (Ent) = Name_Priority)
398 -- Renamings of the attribute Priority applied to protected
399 -- objects have been previously expanded into calls to the
400 -- Get_Ceiling run-time subprogram.
402 or else
403 (Nkind (Ent) = N_Function_Call
404 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
405 or else
406 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling)))
407 then
408 -- The enclosing subprogram cannot be a protected function
410 S := Current_Scope;
411 while not (Is_Subprogram (S)
412 and then Convention (S) = Convention_Protected)
413 and then S /= Standard_Standard
414 loop
415 S := Scope (S);
416 end loop;
418 if Ekind (S) = E_Function
419 and then Convention (S) = Convention_Protected
420 then
421 Error_Msg_N
422 ("protected function cannot modify protected object",
423 Lhs);
424 end if;
426 -- Changes of the ceiling priority of the protected object
427 -- are only effective if the Ceiling_Locking policy is in
428 -- effect (AARM D.5.2 (5/2)).
430 if Locking_Policy /= 'C' then
431 Error_Msg_N ("assignment to the attribute PRIORITY has " &
432 "no effect??", Lhs);
433 Error_Msg_N ("\since no Locking_Policy has been " &
434 "specified??", Lhs);
435 end if;
437 return;
438 end if;
439 end if;
440 end;
442 Diagnose_Non_Variable_Lhs (Lhs);
443 return;
445 -- Error of assigning to limited type. We do however allow this in
446 -- certain cases where the front end generates the assignments.
448 elsif Is_Limited_Type (T1)
449 and then not Assignment_OK (Lhs)
450 and then not Assignment_OK (Original_Node (Lhs))
451 and then not Is_Value_Type (T1)
452 then
453 -- CPP constructors can only be called in declarations
455 if Is_CPP_Constructor_Call (Rhs) then
456 Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
457 else
458 Error_Msg_N
459 ("left hand of assignment must not be limited type", Lhs);
460 Explain_Limited_Type (T1, Lhs);
461 end if;
462 return;
464 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
465 -- abstract. This is only checked when the assignment Comes_From_Source,
466 -- because in some cases the expander generates such assignments (such
467 -- in the _assign operation for an abstract type).
469 elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
470 Error_Msg_N
471 ("target of assignment operation must not be abstract", Lhs);
472 end if;
474 -- Resolution may have updated the subtype, in case the left-hand side
475 -- is a private protected component. Use the correct subtype to avoid
476 -- scoping issues in the back-end.
478 T1 := Etype (Lhs);
480 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
481 -- type. For example:
483 -- limited with P;
484 -- package Pkg is
485 -- type Acc is access P.T;
486 -- end Pkg;
488 -- with Pkg; use Acc;
489 -- procedure Example is
490 -- A, B : Acc;
491 -- begin
492 -- A.all := B.all; -- ERROR
493 -- end Example;
495 if Nkind (Lhs) = N_Explicit_Dereference
496 and then Ekind (T1) = E_Incomplete_Type
497 then
498 Error_Msg_N ("invalid use of incomplete type", Lhs);
499 Kill_Lhs;
500 return;
501 end if;
503 -- Now we can complete the resolution of the right hand side
505 Set_Assignment_Type (Lhs, T1);
506 Resolve (Rhs, T1);
508 -- This is the point at which we check for an unset reference
510 Check_Unset_Reference (Rhs);
511 Check_Unprotected_Access (Lhs, Rhs);
513 -- Remaining steps are skipped if Rhs was syntactically in error
515 if Rhs = Error then
516 Kill_Lhs;
517 return;
518 end if;
520 T2 := Etype (Rhs);
522 if not Covers (T1, T2) then
523 Wrong_Type (Rhs, Etype (Lhs));
524 Kill_Lhs;
525 return;
526 end if;
528 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
529 -- types, use the non-limited view if available
531 if Nkind (Rhs) = N_Explicit_Dereference
532 and then Ekind (T2) = E_Incomplete_Type
533 and then Is_Tagged_Type (T2)
534 and then Present (Non_Limited_View (T2))
535 then
536 T2 := Non_Limited_View (T2);
537 end if;
539 Set_Assignment_Type (Rhs, T2);
541 if Total_Errors_Detected /= 0 then
542 if No (T1) then
543 T1 := Any_Type;
544 end if;
546 if No (T2) then
547 T2 := Any_Type;
548 end if;
549 end if;
551 if T1 = Any_Type or else T2 = Any_Type then
552 Kill_Lhs;
553 return;
554 end if;
556 -- If the rhs is class-wide or dynamically tagged, then require the lhs
557 -- to be class-wide. The case where the rhs is a dynamically tagged call
558 -- to a dispatching operation with a controlling access result is
559 -- excluded from this check, since the target has an access type (and
560 -- no tag propagation occurs in that case).
562 if (Is_Class_Wide_Type (T2)
563 or else (Is_Dynamically_Tagged (Rhs)
564 and then not Is_Access_Type (T1)))
565 and then not Is_Class_Wide_Type (T1)
566 then
567 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
569 elsif Is_Class_Wide_Type (T1)
570 and then not Is_Class_Wide_Type (T2)
571 and then not Is_Tag_Indeterminate (Rhs)
572 and then not Is_Dynamically_Tagged (Rhs)
573 then
574 Error_Msg_N ("dynamically tagged expression required!", Rhs);
575 end if;
577 -- Propagate the tag from a class-wide target to the rhs when the rhs
578 -- is a tag-indeterminate call.
580 if Is_Tag_Indeterminate (Rhs) then
581 if Is_Class_Wide_Type (T1) then
582 Propagate_Tag (Lhs, Rhs);
584 elsif Nkind (Rhs) = N_Function_Call
585 and then Is_Entity_Name (Name (Rhs))
586 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
587 then
588 Error_Msg_N
589 ("call to abstract function must be dispatching", Name (Rhs));
591 elsif Nkind (Rhs) = N_Qualified_Expression
592 and then Nkind (Expression (Rhs)) = N_Function_Call
593 and then Is_Entity_Name (Name (Expression (Rhs)))
594 and then
595 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
596 then
597 Error_Msg_N
598 ("call to abstract function must be dispatching",
599 Name (Expression (Rhs)));
600 end if;
601 end if;
603 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
604 -- apply an implicit conversion of the rhs to that type to force
605 -- appropriate static and run-time accessibility checks. This applies
606 -- as well to anonymous access-to-subprogram types that are component
607 -- subtypes or formal parameters.
609 if Ada_Version >= Ada_2005 and then Is_Access_Type (T1) then
610 if Is_Local_Anonymous_Access (T1)
611 or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
613 -- Handle assignment to an Ada 2012 stand-alone object
614 -- of an anonymous access type.
616 or else (Ekind (T1) = E_Anonymous_Access_Type
617 and then Nkind (Associated_Node_For_Itype (T1)) =
618 N_Object_Declaration)
620 then
621 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
622 Analyze_And_Resolve (Rhs, T1);
623 end if;
624 end if;
626 -- Ada 2005 (AI-231): Assignment to not null variable
628 if Ada_Version >= Ada_2005
629 and then Can_Never_Be_Null (T1)
630 and then not Assignment_OK (Lhs)
631 then
632 -- Case where we know the right hand side is null
634 if Known_Null (Rhs) then
635 Apply_Compile_Time_Constraint_Error
636 (N => Rhs,
637 Msg =>
638 "(Ada 2005) null not allowed in null-excluding objects??",
639 Reason => CE_Null_Not_Allowed);
641 -- We still mark this as a possible modification, that's necessary
642 -- to reset Is_True_Constant, and desirable for xref purposes.
644 Note_Possible_Modification (Lhs, Sure => True);
645 return;
647 -- If we know the right hand side is non-null, then we convert to the
648 -- target type, since we don't need a run time check in that case.
650 elsif not Can_Never_Be_Null (T2) then
651 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
652 Analyze_And_Resolve (Rhs, T1);
653 end if;
654 end if;
656 if Is_Scalar_Type (T1) then
657 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
659 -- For array types, verify that lengths match. If the right hand side
660 -- is a function call that has been inlined, the assignment has been
661 -- rewritten as a block, and the constraint check will be applied to the
662 -- assignment within the block.
664 elsif Is_Array_Type (T1)
665 and then (Nkind (Rhs) /= N_Type_Conversion
666 or else Is_Constrained (Etype (Rhs)))
667 and then (Nkind (Rhs) /= N_Function_Call
668 or else Nkind (N) /= N_Block_Statement)
669 then
670 -- Assignment verifies that the length of the Lsh and Rhs are equal,
671 -- but of course the indexes do not have to match. If the right-hand
672 -- side is a type conversion to an unconstrained type, a length check
673 -- is performed on the expression itself during expansion. In rare
674 -- cases, the redundant length check is computed on an index type
675 -- with a different representation, triggering incorrect code in the
676 -- back end.
678 Apply_Length_Check (Rhs, Etype (Lhs));
680 else
681 -- Discriminant checks are applied in the course of expansion
683 null;
684 end if;
686 -- Note: modifications of the Lhs may only be recorded after
687 -- checks have been applied.
689 Note_Possible_Modification (Lhs, Sure => True);
691 -- ??? a real accessibility check is needed when ???
693 -- Post warning for redundant assignment or variable to itself
695 if Warn_On_Redundant_Constructs
697 -- We only warn for source constructs
699 and then Comes_From_Source (N)
701 -- Where the object is the same on both sides
703 and then Same_Object (Lhs, Original_Node (Rhs))
705 -- But exclude the case where the right side was an operation that
706 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
707 -- don't want to warn in such a case, since it is reasonable to write
708 -- such expressions especially when K is defined symbolically in some
709 -- other package.
711 and then Nkind (Original_Node (Rhs)) not in N_Op
712 then
713 if Nkind (Lhs) in N_Has_Entity then
714 Error_Msg_NE -- CODEFIX
715 ("?r?useless assignment of & to itself!", N, Entity (Lhs));
716 else
717 Error_Msg_N -- CODEFIX
718 ("?r?useless assignment of object to itself!", N);
719 end if;
720 end if;
722 -- Check for non-allowed composite assignment
724 if not Support_Composite_Assign_On_Target
725 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
726 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
727 then
728 Error_Msg_CRT ("composite assignment", N);
729 end if;
731 -- Check elaboration warning for left side if not in elab code
733 if not In_Subprogram_Or_Concurrent_Unit then
734 Check_Elab_Assign (Lhs);
735 end if;
737 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
738 -- assignment is a source assignment in the extended main source unit.
739 -- We are not interested in any reference information outside this
740 -- context, or in compiler generated assignment statements.
742 if Comes_From_Source (N)
743 and then In_Extended_Main_Source_Unit (Lhs)
744 then
745 Set_Referenced_Modified (Lhs, Out_Param => False);
746 end if;
748 -- Final step. If left side is an entity, then we may be able to reset
749 -- the current tracked values to new safe values. We only have something
750 -- to do if the left side is an entity name, and expansion has not
751 -- modified the node into something other than an assignment, and of
752 -- course we only capture values if it is safe to do so.
754 if Is_Entity_Name (Lhs)
755 and then Nkind (N) = N_Assignment_Statement
756 then
757 declare
758 Ent : constant Entity_Id := Entity (Lhs);
760 begin
761 if Safe_To_Capture_Value (N, Ent) then
763 -- If simple variable on left side, warn if this assignment
764 -- blots out another one (rendering it useless). We only do
765 -- this for source assignments, otherwise we can generate bogus
766 -- warnings when an assignment is rewritten as another
767 -- assignment, and gets tied up with itself.
769 if Warn_On_Modified_Unread
770 and then Is_Assignable (Ent)
771 and then Comes_From_Source (N)
772 and then In_Extended_Main_Source_Unit (Ent)
773 then
774 Warn_On_Useless_Assignment (Ent, N);
775 end if;
777 -- If we are assigning an access type and the left side is an
778 -- entity, then make sure that the Is_Known_[Non_]Null flags
779 -- properly reflect the state of the entity after assignment.
781 if Is_Access_Type (T1) then
782 if Known_Non_Null (Rhs) then
783 Set_Is_Known_Non_Null (Ent, True);
785 elsif Known_Null (Rhs)
786 and then not Can_Never_Be_Null (Ent)
787 then
788 Set_Is_Known_Null (Ent, True);
790 else
791 Set_Is_Known_Null (Ent, False);
793 if not Can_Never_Be_Null (Ent) then
794 Set_Is_Known_Non_Null (Ent, False);
795 end if;
796 end if;
798 -- For discrete types, we may be able to set the current value
799 -- if the value is known at compile time.
801 elsif Is_Discrete_Type (T1)
802 and then Compile_Time_Known_Value (Rhs)
803 then
804 Set_Current_Value (Ent, Rhs);
805 else
806 Set_Current_Value (Ent, Empty);
807 end if;
809 -- If not safe to capture values, kill them
811 else
812 Kill_Lhs;
813 end if;
814 end;
815 end if;
817 -- If assigning to an object in whole or in part, note location of
818 -- assignment in case no one references value. We only do this for
819 -- source assignments, otherwise we can generate bogus warnings when an
820 -- assignment is rewritten as another assignment, and gets tied up with
821 -- itself.
823 declare
824 Ent : constant Entity_Id := Get_Enclosing_Object (Lhs);
825 begin
826 if Present (Ent)
827 and then Safe_To_Capture_Value (N, Ent)
828 and then Nkind (N) = N_Assignment_Statement
829 and then Warn_On_Modified_Unread
830 and then Is_Assignable (Ent)
831 and then Comes_From_Source (N)
832 and then In_Extended_Main_Source_Unit (Ent)
833 then
834 Set_Last_Assignment (Ent, Lhs);
835 end if;
836 end;
838 Analyze_Dimension (N);
839 end Analyze_Assignment;
841 -----------------------------
842 -- Analyze_Block_Statement --
843 -----------------------------
845 procedure Analyze_Block_Statement (N : Node_Id) is
846 procedure Install_Return_Entities (Scop : Entity_Id);
847 -- Install all entities of return statement scope Scop in the visibility
848 -- chain except for the return object since its entity is reused in a
849 -- renaming.
851 -----------------------------
852 -- Install_Return_Entities --
853 -----------------------------
855 procedure Install_Return_Entities (Scop : Entity_Id) is
856 Id : Entity_Id;
858 begin
859 Id := First_Entity (Scop);
860 while Present (Id) loop
862 -- Do not install the return object
864 if not Ekind_In (Id, E_Constant, E_Variable)
865 or else not Is_Return_Object (Id)
866 then
867 Install_Entity (Id);
868 end if;
870 Next_Entity (Id);
871 end loop;
872 end Install_Return_Entities;
874 -- Local constants and variables
876 Decls : constant List_Id := Declarations (N);
877 Id : constant Node_Id := Identifier (N);
878 HSS : constant Node_Id := Handled_Statement_Sequence (N);
880 Is_BIP_Return_Statement : Boolean;
882 -- Start of processing for Analyze_Block_Statement
884 begin
885 -- In SPARK mode, we reject block statements. Note that the case of
886 -- block statements generated by the expander is fine.
888 if Nkind (Original_Node (N)) = N_Block_Statement then
889 Check_SPARK_Restriction ("block statement is not allowed", N);
890 end if;
892 -- If no handled statement sequence is present, things are really messed
893 -- up, and we just return immediately (defence against previous errors).
895 if No (HSS) then
896 Check_Error_Detected;
897 return;
898 end if;
900 -- Detect whether the block is actually a rewritten return statement of
901 -- a build-in-place function.
903 Is_BIP_Return_Statement :=
904 Present (Id)
905 and then Present (Entity (Id))
906 and then Ekind (Entity (Id)) = E_Return_Statement
907 and then Is_Build_In_Place_Function
908 (Return_Applies_To (Entity (Id)));
910 -- Normal processing with HSS present
912 declare
913 EH : constant List_Id := Exception_Handlers (HSS);
914 Ent : Entity_Id := Empty;
915 S : Entity_Id;
917 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
918 -- Recursively save value of this global, will be restored on exit
920 begin
921 -- Initialize unblocked exit count for statements of begin block
922 -- plus one for each exception handler that is present.
924 Unblocked_Exit_Count := 1;
926 if Present (EH) then
927 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
928 end if;
930 -- If a label is present analyze it and mark it as referenced
932 if Present (Id) then
933 Analyze (Id);
934 Ent := Entity (Id);
936 -- An error defense. If we have an identifier, but no entity, then
937 -- something is wrong. If previous errors, then just remove the
938 -- identifier and continue, otherwise raise an exception.
940 if No (Ent) then
941 Check_Error_Detected;
942 Set_Identifier (N, Empty);
944 else
945 Set_Ekind (Ent, E_Block);
946 Generate_Reference (Ent, N, ' ');
947 Generate_Definition (Ent);
949 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
950 Set_Label_Construct (Parent (Ent), N);
951 end if;
952 end if;
953 end if;
955 -- If no entity set, create a label entity
957 if No (Ent) then
958 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
959 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
960 Set_Parent (Ent, N);
961 end if;
963 Set_Etype (Ent, Standard_Void_Type);
964 Set_Block_Node (Ent, Identifier (N));
965 Push_Scope (Ent);
967 -- The block served as an extended return statement. Ensure that any
968 -- entities created during the analysis and expansion of the return
969 -- object declaration are once again visible.
971 if Is_BIP_Return_Statement then
972 Install_Return_Entities (Ent);
973 end if;
975 if Present (Decls) then
976 Analyze_Declarations (Decls);
977 Check_Completion;
978 Inspect_Deferred_Constant_Completion (Decls);
979 end if;
981 Analyze (HSS);
982 Process_End_Label (HSS, 'e', Ent);
984 -- If exception handlers are present, then we indicate that enclosing
985 -- scopes contain a block with handlers. We only need to mark non-
986 -- generic scopes.
988 if Present (EH) then
989 S := Scope (Ent);
990 loop
991 Set_Has_Nested_Block_With_Handler (S);
992 exit when Is_Overloadable (S)
993 or else Ekind (S) = E_Package
994 or else Is_Generic_Unit (S);
995 S := Scope (S);
996 end loop;
997 end if;
999 Check_References (Ent);
1000 Warn_On_Useless_Assignments (Ent);
1001 End_Scope;
1003 if Unblocked_Exit_Count = 0 then
1004 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1005 Check_Unreachable_Code (N);
1006 else
1007 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1008 end if;
1009 end;
1010 end Analyze_Block_Statement;
1012 ----------------------------
1013 -- Analyze_Case_Statement --
1014 ----------------------------
1016 procedure Analyze_Case_Statement (N : Node_Id) is
1017 Exp : Node_Id;
1018 Exp_Type : Entity_Id;
1019 Exp_Btype : Entity_Id;
1020 Last_Choice : Nat;
1022 Others_Present : Boolean;
1023 -- Indicates if Others was present
1025 pragma Warnings (Off, Last_Choice);
1026 -- Don't care about assigned value
1028 Statements_Analyzed : Boolean := False;
1029 -- Set True if at least some statement sequences get analyzed. If False
1030 -- on exit, means we had a serious error that prevented full analysis of
1031 -- the case statement, and as a result it is not a good idea to output
1032 -- warning messages about unreachable code.
1034 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1035 -- Recursively save value of this global, will be restored on exit
1037 procedure Non_Static_Choice_Error (Choice : Node_Id);
1038 -- Error routine invoked by the generic instantiation below when the
1039 -- case statement has a non static choice.
1041 procedure Process_Statements (Alternative : Node_Id);
1042 -- Analyzes the statements associated with a case alternative. Needed
1043 -- by instantiation below.
1045 package Analyze_Case_Choices is new
1046 Generic_Analyze_Choices
1047 (Process_Associated_Node => Process_Statements);
1048 use Analyze_Case_Choices;
1049 -- Instantiation of the generic choice analysis package
1051 package Check_Case_Choices is new
1052 Generic_Check_Choices
1053 (Process_Empty_Choice => No_OP,
1054 Process_Non_Static_Choice => Non_Static_Choice_Error,
1055 Process_Associated_Node => No_Op);
1056 use Check_Case_Choices;
1057 -- Instantiation of the generic choice processing package
1059 -----------------------------
1060 -- Non_Static_Choice_Error --
1061 -----------------------------
1063 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1064 begin
1065 Flag_Non_Static_Expr
1066 ("choice given in case statement is not static!", Choice);
1067 end Non_Static_Choice_Error;
1069 ------------------------
1070 -- Process_Statements --
1071 ------------------------
1073 procedure Process_Statements (Alternative : Node_Id) is
1074 Choices : constant List_Id := Discrete_Choices (Alternative);
1075 Ent : Entity_Id;
1077 begin
1078 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1079 Statements_Analyzed := True;
1081 -- An interesting optimization. If the case statement expression
1082 -- is a simple entity, then we can set the current value within an
1083 -- alternative if the alternative has one possible value.
1085 -- case N is
1086 -- when 1 => alpha
1087 -- when 2 | 3 => beta
1088 -- when others => gamma
1090 -- Here we know that N is initially 1 within alpha, but for beta and
1091 -- gamma, we do not know anything more about the initial value.
1093 if Is_Entity_Name (Exp) then
1094 Ent := Entity (Exp);
1096 if Ekind_In (Ent, E_Variable,
1097 E_In_Out_Parameter,
1098 E_Out_Parameter)
1099 then
1100 if List_Length (Choices) = 1
1101 and then Nkind (First (Choices)) in N_Subexpr
1102 and then Compile_Time_Known_Value (First (Choices))
1103 then
1104 Set_Current_Value (Entity (Exp), First (Choices));
1105 end if;
1107 Analyze_Statements (Statements (Alternative));
1109 -- After analyzing the case, set the current value to empty
1110 -- since we won't know what it is for the next alternative
1111 -- (unless reset by this same circuit), or after the case.
1113 Set_Current_Value (Entity (Exp), Empty);
1114 return;
1115 end if;
1116 end if;
1118 -- Case where expression is not an entity name of a variable
1120 Analyze_Statements (Statements (Alternative));
1121 end Process_Statements;
1123 -- Start of processing for Analyze_Case_Statement
1125 begin
1126 Unblocked_Exit_Count := 0;
1127 Exp := Expression (N);
1128 Analyze (Exp);
1130 -- The expression must be of any discrete type. In rare cases, the
1131 -- expander constructs a case statement whose expression has a private
1132 -- type whose full view is discrete. This can happen when generating
1133 -- a stream operation for a variant type after the type is frozen,
1134 -- when the partial of view of the type of the discriminant is private.
1135 -- In that case, use the full view to analyze case alternatives.
1137 if not Is_Overloaded (Exp)
1138 and then not Comes_From_Source (N)
1139 and then Is_Private_Type (Etype (Exp))
1140 and then Present (Full_View (Etype (Exp)))
1141 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1142 then
1143 Resolve (Exp, Etype (Exp));
1144 Exp_Type := Full_View (Etype (Exp));
1146 else
1147 Analyze_And_Resolve (Exp, Any_Discrete);
1148 Exp_Type := Etype (Exp);
1149 end if;
1151 Check_Unset_Reference (Exp);
1152 Exp_Btype := Base_Type (Exp_Type);
1154 -- The expression must be of a discrete type which must be determinable
1155 -- independently of the context in which the expression occurs, but
1156 -- using the fact that the expression must be of a discrete type.
1157 -- Moreover, the type this expression must not be a character literal
1158 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1160 -- If error already reported by Resolve, nothing more to do
1162 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1163 return;
1165 elsif Exp_Btype = Any_Character then
1166 Error_Msg_N
1167 ("character literal as case expression is ambiguous", Exp);
1168 return;
1170 elsif Ada_Version = Ada_83
1171 and then (Is_Generic_Type (Exp_Btype)
1172 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1173 then
1174 Error_Msg_N
1175 ("(Ada 83) case expression cannot be of a generic type", Exp);
1176 return;
1177 end if;
1179 -- If the case expression is a formal object of mode in out, then treat
1180 -- it as having a nonstatic subtype by forcing use of the base type
1181 -- (which has to get passed to Check_Case_Choices below). Also use base
1182 -- type when the case expression is parenthesized.
1184 if Paren_Count (Exp) > 0
1185 or else (Is_Entity_Name (Exp)
1186 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1187 then
1188 Exp_Type := Exp_Btype;
1189 end if;
1191 -- Call instantiated procedures to analyzwe and check discrete choices
1193 Analyze_Choices (Alternatives (N), Exp_Type);
1194 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1196 -- Case statement with single OTHERS alternative not allowed in SPARK
1198 if Others_Present and then List_Length (Alternatives (N)) = 1 then
1199 Check_SPARK_Restriction
1200 ("OTHERS as unique case alternative is not allowed", N);
1201 end if;
1203 if Exp_Type = Universal_Integer and then not Others_Present then
1204 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1205 end if;
1207 -- If all our exits were blocked by unconditional transfers of control,
1208 -- then the entire CASE statement acts as an unconditional transfer of
1209 -- control, so treat it like one, and check unreachable code. Skip this
1210 -- test if we had serious errors preventing any statement analysis.
1212 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1213 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1214 Check_Unreachable_Code (N);
1215 else
1216 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1217 end if;
1219 -- If the expander is active it will detect the case of a statically
1220 -- determined single alternative and remove warnings for the case, but
1221 -- if we are not doing expansion, that circuit won't be active. Here we
1222 -- duplicate the effect of removing warnings in the same way, so that
1223 -- we will get the same set of warnings in -gnatc mode.
1225 if not Expander_Active
1226 and then Compile_Time_Known_Value (Expression (N))
1227 and then Serious_Errors_Detected = 0
1228 then
1229 declare
1230 Chosen : constant Node_Id := Find_Static_Alternative (N);
1231 Alt : Node_Id;
1233 begin
1234 Alt := First (Alternatives (N));
1235 while Present (Alt) loop
1236 if Alt /= Chosen then
1237 Remove_Warning_Messages (Statements (Alt));
1238 end if;
1240 Next (Alt);
1241 end loop;
1242 end;
1243 end if;
1244 end Analyze_Case_Statement;
1246 ----------------------------
1247 -- Analyze_Exit_Statement --
1248 ----------------------------
1250 -- If the exit includes a name, it must be the name of a currently open
1251 -- loop. Otherwise there must be an innermost open loop on the stack, to
1252 -- which the statement implicitly refers.
1254 -- Additionally, in SPARK mode:
1256 -- The exit can only name the closest enclosing loop;
1258 -- An exit with a when clause must be directly contained in a loop;
1260 -- An exit without a when clause must be directly contained in an
1261 -- if-statement with no elsif or else, which is itself directly contained
1262 -- in a loop. The exit must be the last statement in the if-statement.
1264 procedure Analyze_Exit_Statement (N : Node_Id) is
1265 Target : constant Node_Id := Name (N);
1266 Cond : constant Node_Id := Condition (N);
1267 Scope_Id : Entity_Id;
1268 U_Name : Entity_Id;
1269 Kind : Entity_Kind;
1271 begin
1272 if No (Cond) then
1273 Check_Unreachable_Code (N);
1274 end if;
1276 if Present (Target) then
1277 Analyze (Target);
1278 U_Name := Entity (Target);
1280 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1281 Error_Msg_N ("invalid loop name in exit statement", N);
1282 return;
1284 else
1285 if Has_Loop_In_Inner_Open_Scopes (U_Name) then
1286 Check_SPARK_Restriction
1287 ("exit label must name the closest enclosing loop", N);
1288 end if;
1290 Set_Has_Exit (U_Name);
1291 end if;
1293 else
1294 U_Name := Empty;
1295 end if;
1297 for J in reverse 0 .. Scope_Stack.Last loop
1298 Scope_Id := Scope_Stack.Table (J).Entity;
1299 Kind := Ekind (Scope_Id);
1301 if Kind = E_Loop and then (No (Target) or else Scope_Id = U_Name) then
1302 Set_Has_Exit (Scope_Id);
1303 exit;
1305 elsif Kind = E_Block
1306 or else Kind = E_Loop
1307 or else Kind = E_Return_Statement
1308 then
1309 null;
1311 else
1312 Error_Msg_N
1313 ("cannot exit from program unit or accept statement", N);
1314 return;
1315 end if;
1316 end loop;
1318 -- Verify that if present the condition is a Boolean expression
1320 if Present (Cond) then
1321 Analyze_And_Resolve (Cond, Any_Boolean);
1322 Check_Unset_Reference (Cond);
1323 end if;
1325 -- In SPARK mode, verify that the exit statement respects the SPARK
1326 -- restrictions.
1328 if Present (Cond) then
1329 if Nkind (Parent (N)) /= N_Loop_Statement then
1330 Check_SPARK_Restriction
1331 ("exit with when clause must be directly in loop", N);
1332 end if;
1334 else
1335 if Nkind (Parent (N)) /= N_If_Statement then
1336 if Nkind (Parent (N)) = N_Elsif_Part then
1337 Check_SPARK_Restriction
1338 ("exit must be in IF without ELSIF", N);
1339 else
1340 Check_SPARK_Restriction ("exit must be directly in IF", N);
1341 end if;
1343 elsif Nkind (Parent (Parent (N))) /= N_Loop_Statement then
1344 Check_SPARK_Restriction
1345 ("exit must be in IF directly in loop", N);
1347 -- First test the presence of ELSE, so that an exit in an ELSE leads
1348 -- to an error mentioning the ELSE.
1350 elsif Present (Else_Statements (Parent (N))) then
1351 Check_SPARK_Restriction ("exit must be in IF without ELSE", N);
1353 -- An exit in an ELSIF does not reach here, as it would have been
1354 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1356 elsif Present (Elsif_Parts (Parent (N))) then
1357 Check_SPARK_Restriction ("exit must be in IF without ELSIF", N);
1358 end if;
1359 end if;
1361 -- Chain exit statement to associated loop entity
1363 Set_Next_Exit_Statement (N, First_Exit_Statement (Scope_Id));
1364 Set_First_Exit_Statement (Scope_Id, N);
1366 -- Since the exit may take us out of a loop, any previous assignment
1367 -- statement is not useless, so clear last assignment indications. It
1368 -- is OK to keep other current values, since if the exit statement
1369 -- does not exit, then the current values are still valid.
1371 Kill_Current_Values (Last_Assignment_Only => True);
1372 end Analyze_Exit_Statement;
1374 ----------------------------
1375 -- Analyze_Goto_Statement --
1376 ----------------------------
1378 procedure Analyze_Goto_Statement (N : Node_Id) is
1379 Label : constant Node_Id := Name (N);
1380 Scope_Id : Entity_Id;
1381 Label_Scope : Entity_Id;
1382 Label_Ent : Entity_Id;
1384 begin
1385 Check_SPARK_Restriction ("goto statement is not allowed", N);
1387 -- Actual semantic checks
1389 Check_Unreachable_Code (N);
1390 Kill_Current_Values (Last_Assignment_Only => True);
1392 Analyze (Label);
1393 Label_Ent := Entity (Label);
1395 -- Ignore previous error
1397 if Label_Ent = Any_Id then
1398 Check_Error_Detected;
1399 return;
1401 -- We just have a label as the target of a goto
1403 elsif Ekind (Label_Ent) /= E_Label then
1404 Error_Msg_N ("target of goto statement must be a label", Label);
1405 return;
1407 -- Check that the target of the goto is reachable according to Ada
1408 -- scoping rules. Note: the special gotos we generate for optimizing
1409 -- local handling of exceptions would violate these rules, but we mark
1410 -- such gotos as analyzed when built, so this code is never entered.
1412 elsif not Reachable (Label_Ent) then
1413 Error_Msg_N ("target of goto statement is not reachable", Label);
1414 return;
1415 end if;
1417 -- Here if goto passes initial validity checks
1419 Label_Scope := Enclosing_Scope (Label_Ent);
1421 for J in reverse 0 .. Scope_Stack.Last loop
1422 Scope_Id := Scope_Stack.Table (J).Entity;
1424 if Label_Scope = Scope_Id
1425 or else not Ekind_In (Scope_Id, E_Block, E_Loop, E_Return_Statement)
1426 then
1427 if Scope_Id /= Label_Scope then
1428 Error_Msg_N
1429 ("cannot exit from program unit or accept statement", N);
1430 end if;
1432 return;
1433 end if;
1434 end loop;
1436 raise Program_Error;
1437 end Analyze_Goto_Statement;
1439 --------------------------
1440 -- Analyze_If_Statement --
1441 --------------------------
1443 -- A special complication arises in the analysis of if statements
1445 -- The expander has circuitry to completely delete code that it can tell
1446 -- will not be executed (as a result of compile time known conditions). In
1447 -- the analyzer, we ensure that code that will be deleted in this manner
1448 -- is analyzed but not expanded. This is obviously more efficient, but
1449 -- more significantly, difficulties arise if code is expanded and then
1450 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1451 -- generated in deleted code must be frozen from start, because the nodes
1452 -- on which they depend will not be available at the freeze point.
1454 procedure Analyze_If_Statement (N : Node_Id) is
1455 E : Node_Id;
1457 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1458 -- Recursively save value of this global, will be restored on exit
1460 Save_In_Deleted_Code : Boolean;
1462 Del : Boolean := False;
1463 -- This flag gets set True if a True condition has been found, which
1464 -- means that remaining ELSE/ELSIF parts are deleted.
1466 procedure Analyze_Cond_Then (Cnode : Node_Id);
1467 -- This is applied to either the N_If_Statement node itself or to an
1468 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1469 -- statements associated with it.
1471 -----------------------
1472 -- Analyze_Cond_Then --
1473 -----------------------
1475 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1476 Cond : constant Node_Id := Condition (Cnode);
1477 Tstm : constant List_Id := Then_Statements (Cnode);
1479 begin
1480 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1481 Analyze_And_Resolve (Cond, Any_Boolean);
1482 Check_Unset_Reference (Cond);
1483 Set_Current_Value_Condition (Cnode);
1485 -- If already deleting, then just analyze then statements
1487 if Del then
1488 Analyze_Statements (Tstm);
1490 -- Compile time known value, not deleting yet
1492 elsif Compile_Time_Known_Value (Cond) then
1493 Save_In_Deleted_Code := In_Deleted_Code;
1495 -- If condition is True, then analyze the THEN statements and set
1496 -- no expansion for ELSE and ELSIF parts.
1498 if Is_True (Expr_Value (Cond)) then
1499 Analyze_Statements (Tstm);
1500 Del := True;
1501 Expander_Mode_Save_And_Set (False);
1502 In_Deleted_Code := True;
1504 -- If condition is False, analyze THEN with expansion off
1506 else -- Is_False (Expr_Value (Cond))
1507 Expander_Mode_Save_And_Set (False);
1508 In_Deleted_Code := True;
1509 Analyze_Statements (Tstm);
1510 Expander_Mode_Restore;
1511 In_Deleted_Code := Save_In_Deleted_Code;
1512 end if;
1514 -- Not known at compile time, not deleting, normal analysis
1516 else
1517 Analyze_Statements (Tstm);
1518 end if;
1519 end Analyze_Cond_Then;
1521 -- Start of Analyze_If_Statement
1523 begin
1524 -- Initialize exit count for else statements. If there is no else part,
1525 -- this count will stay non-zero reflecting the fact that the uncovered
1526 -- else case is an unblocked exit.
1528 Unblocked_Exit_Count := 1;
1529 Analyze_Cond_Then (N);
1531 -- Now to analyze the elsif parts if any are present
1533 if Present (Elsif_Parts (N)) then
1534 E := First (Elsif_Parts (N));
1535 while Present (E) loop
1536 Analyze_Cond_Then (E);
1537 Next (E);
1538 end loop;
1539 end if;
1541 if Present (Else_Statements (N)) then
1542 Analyze_Statements (Else_Statements (N));
1543 end if;
1545 -- If all our exits were blocked by unconditional transfers of control,
1546 -- then the entire IF statement acts as an unconditional transfer of
1547 -- control, so treat it like one, and check unreachable code.
1549 if Unblocked_Exit_Count = 0 then
1550 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1551 Check_Unreachable_Code (N);
1552 else
1553 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1554 end if;
1556 if Del then
1557 Expander_Mode_Restore;
1558 In_Deleted_Code := Save_In_Deleted_Code;
1559 end if;
1561 if not Expander_Active
1562 and then Compile_Time_Known_Value (Condition (N))
1563 and then Serious_Errors_Detected = 0
1564 then
1565 if Is_True (Expr_Value (Condition (N))) then
1566 Remove_Warning_Messages (Else_Statements (N));
1568 if Present (Elsif_Parts (N)) then
1569 E := First (Elsif_Parts (N));
1570 while Present (E) loop
1571 Remove_Warning_Messages (Then_Statements (E));
1572 Next (E);
1573 end loop;
1574 end if;
1576 else
1577 Remove_Warning_Messages (Then_Statements (N));
1578 end if;
1579 end if;
1581 -- Warn on redundant if statement that has no effect
1583 -- Note, we could also check empty ELSIF parts ???
1585 if Warn_On_Redundant_Constructs
1587 -- If statement must be from source
1589 and then Comes_From_Source (N)
1591 -- Condition must not have obvious side effect
1593 and then Has_No_Obvious_Side_Effects (Condition (N))
1595 -- No elsif parts of else part
1597 and then No (Elsif_Parts (N))
1598 and then No (Else_Statements (N))
1600 -- Then must be a single null statement
1602 and then List_Length (Then_Statements (N)) = 1
1603 then
1604 -- Go to original node, since we may have rewritten something as
1605 -- a null statement (e.g. a case we could figure the outcome of).
1607 declare
1608 T : constant Node_Id := First (Then_Statements (N));
1609 S : constant Node_Id := Original_Node (T);
1611 begin
1612 if Comes_From_Source (S) and then Nkind (S) = N_Null_Statement then
1613 Error_Msg_N ("if statement has no effect?r?", N);
1614 end if;
1615 end;
1616 end if;
1617 end Analyze_If_Statement;
1619 ----------------------------------------
1620 -- Analyze_Implicit_Label_Declaration --
1621 ----------------------------------------
1623 -- An implicit label declaration is generated in the innermost enclosing
1624 -- declarative part. This is done for labels, and block and loop names.
1626 -- Note: any changes in this routine may need to be reflected in
1627 -- Analyze_Label_Entity.
1629 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1630 Id : constant Node_Id := Defining_Identifier (N);
1631 begin
1632 Enter_Name (Id);
1633 Set_Ekind (Id, E_Label);
1634 Set_Etype (Id, Standard_Void_Type);
1635 Set_Enclosing_Scope (Id, Current_Scope);
1636 end Analyze_Implicit_Label_Declaration;
1638 ------------------------------
1639 -- Analyze_Iteration_Scheme --
1640 ------------------------------
1642 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1643 Cond : Node_Id;
1644 Iter_Spec : Node_Id;
1645 Loop_Spec : Node_Id;
1647 begin
1648 -- For an infinite loop, there is no iteration scheme
1650 if No (N) then
1651 return;
1652 end if;
1654 Cond := Condition (N);
1655 Iter_Spec := Iterator_Specification (N);
1656 Loop_Spec := Loop_Parameter_Specification (N);
1658 if Present (Cond) then
1659 Analyze_And_Resolve (Cond, Any_Boolean);
1660 Check_Unset_Reference (Cond);
1661 Set_Current_Value_Condition (N);
1663 elsif Present (Iter_Spec) then
1664 Analyze_Iterator_Specification (Iter_Spec);
1666 else
1667 Analyze_Loop_Parameter_Specification (Loop_Spec);
1668 end if;
1669 end Analyze_Iteration_Scheme;
1671 ------------------------------------
1672 -- Analyze_Iterator_Specification --
1673 ------------------------------------
1675 procedure Analyze_Iterator_Specification (N : Node_Id) is
1676 Loc : constant Source_Ptr := Sloc (N);
1677 Def_Id : constant Node_Id := Defining_Identifier (N);
1678 Subt : constant Node_Id := Subtype_Indication (N);
1679 Iter_Name : constant Node_Id := Name (N);
1681 Ent : Entity_Id;
1682 Typ : Entity_Id;
1683 Bas : Entity_Id;
1685 begin
1686 Enter_Name (Def_Id);
1688 if Present (Subt) then
1689 Analyze (Subt);
1691 -- Save type of subtype indication for subsequent check
1693 if Nkind (Subt) = N_Subtype_Indication then
1694 Bas := Entity (Subtype_Mark (Subt));
1695 else
1696 Bas := Entity (Subt);
1697 end if;
1698 end if;
1700 Preanalyze_Range (Iter_Name);
1702 -- Set the kind of the loop variable, which is not visible within
1703 -- the iterator name.
1705 Set_Ekind (Def_Id, E_Variable);
1707 -- Provide a link between the iterator variable and the container, for
1708 -- subsequent use in cross-reference and modification information.
1710 if Of_Present (N) then
1711 Set_Related_Expression (Def_Id, Iter_Name);
1712 end if;
1714 -- If the domain of iteration is an expression, create a declaration for
1715 -- it, so that finalization actions are introduced outside of the loop.
1716 -- The declaration must be a renaming because the body of the loop may
1717 -- assign to elements.
1719 if not Is_Entity_Name (Iter_Name)
1721 -- When the context is a quantified expression, the renaming
1722 -- declaration is delayed until the expansion phase if we are
1723 -- doing expansion.
1725 and then (Nkind (Parent (N)) /= N_Quantified_Expression
1726 or else Operating_Mode = Check_Semantics)
1728 -- Do not perform this expansion in SPARK mode, since the formal
1729 -- verification directly deals with the source form of the iterator.
1730 -- Ditto for ASIS, where the temporary may hide the transformation
1731 -- of a selected component into a prefixed function call.
1733 and then not GNATprove_Mode
1734 and then not ASIS_Mode
1735 then
1736 declare
1737 Id : constant Entity_Id := Make_Temporary (Loc, 'R', Iter_Name);
1738 Decl : Node_Id;
1740 begin
1741 Typ := Etype (Iter_Name);
1743 -- Protect against malformed iterator
1745 if Typ = Any_Type then
1746 Error_Msg_N ("invalid expression in loop iterator", Iter_Name);
1747 return;
1748 end if;
1750 -- The name in the renaming declaration may be a function call.
1751 -- Indicate that it does not come from source, to suppress
1752 -- spurious warnings on renamings of parameterless functions,
1753 -- a common enough idiom in user-defined iterators.
1755 Decl :=
1756 Make_Object_Renaming_Declaration (Loc,
1757 Defining_Identifier => Id,
1758 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
1759 Name =>
1760 New_Copy_Tree (Iter_Name, New_Sloc => Loc));
1762 Insert_Actions (Parent (Parent (N)), New_List (Decl));
1763 Rewrite (Name (N), New_Occurrence_Of (Id, Loc));
1764 Set_Etype (Id, Typ);
1765 Set_Etype (Name (N), Typ);
1766 end;
1768 -- Container is an entity or an array with uncontrolled components, or
1769 -- else it is a container iterator given by a function call, typically
1770 -- called Iterate in the case of predefined containers, even though
1771 -- Iterate is not a reserved name. What matters is that the return type
1772 -- of the function is an iterator type.
1774 elsif Is_Entity_Name (Iter_Name) then
1775 Analyze (Iter_Name);
1777 if Nkind (Iter_Name) = N_Function_Call then
1778 declare
1779 C : constant Node_Id := Name (Iter_Name);
1780 I : Interp_Index;
1781 It : Interp;
1783 begin
1784 if not Is_Overloaded (Iter_Name) then
1785 Resolve (Iter_Name, Etype (C));
1787 else
1788 Get_First_Interp (C, I, It);
1789 while It.Typ /= Empty loop
1790 if Reverse_Present (N) then
1791 if Is_Reversible_Iterator (It.Typ) then
1792 Resolve (Iter_Name, It.Typ);
1793 exit;
1794 end if;
1796 elsif Is_Iterator (It.Typ) then
1797 Resolve (Iter_Name, It.Typ);
1798 exit;
1799 end if;
1801 Get_Next_Interp (I, It);
1802 end loop;
1803 end if;
1804 end;
1806 -- Domain of iteration is not overloaded
1808 else
1809 Resolve (Iter_Name, Etype (Iter_Name));
1810 end if;
1811 end if;
1813 -- Get base type of container, for proper retrieval of Cursor type
1814 -- and primitive operations.
1816 Typ := Base_Type (Etype (Iter_Name));
1818 if Is_Array_Type (Typ) then
1819 if Of_Present (N) then
1820 Set_Etype (Def_Id, Component_Type (Typ));
1822 if Present (Subt)
1823 and then Base_Type (Bas) /= Base_Type (Component_Type (Typ))
1824 then
1825 Error_Msg_N
1826 ("subtype indication does not match component type", Subt);
1827 end if;
1829 -- Here we have a missing Range attribute
1831 else
1832 Error_Msg_N
1833 ("missing Range attribute in iteration over an array", N);
1835 -- In Ada 2012 mode, this may be an attempt at an iterator
1837 if Ada_Version >= Ada_2012 then
1838 Error_Msg_NE
1839 ("\if& is meant to designate an element of the array, use OF",
1840 N, Def_Id);
1841 end if;
1843 -- Prevent cascaded errors
1845 Set_Ekind (Def_Id, E_Loop_Parameter);
1846 Set_Etype (Def_Id, Etype (First_Index (Typ)));
1847 end if;
1849 -- Check for type error in iterator
1851 elsif Typ = Any_Type then
1852 return;
1854 -- Iteration over a container
1856 else
1857 Set_Ekind (Def_Id, E_Loop_Parameter);
1858 Error_Msg_Ada_2012_Feature ("container iterator", Sloc (N));
1860 -- OF present
1862 if Of_Present (N) then
1863 if Has_Aspect (Typ, Aspect_Iterable) then
1864 if No (Get_Iterable_Type_Primitive (Typ, Name_Element)) then
1865 Error_Msg_N ("missing Element primitive for iteration", N);
1866 end if;
1868 -- For a predefined container, The type of the loop variable is
1869 -- the Iterator_Element aspect of the container type.
1871 else
1872 declare
1873 Element : constant Entity_Id :=
1874 Find_Value_Of_Aspect (Typ, Aspect_Iterator_Element);
1876 begin
1877 if No (Element) then
1878 Error_Msg_NE ("cannot iterate over&", N, Typ);
1879 return;
1881 else
1882 Set_Etype (Def_Id, Entity (Element));
1884 -- If subtype indication was given, verify that it
1885 -- matches element type of container.
1887 if Present (Subt)
1888 and then Bas /= Base_Type (Etype (Def_Id))
1889 then
1890 Error_Msg_N
1891 ("subtype indication does not match element type",
1892 Subt);
1893 end if;
1895 -- If the container has a variable indexing aspect, the
1896 -- element is a variable and is modifiable in the loop.
1898 if Has_Aspect (Typ, Aspect_Variable_Indexing) then
1899 Set_Ekind (Def_Id, E_Variable);
1900 end if;
1901 end if;
1902 end;
1903 end if;
1905 -- OF not present
1907 else
1908 -- For an iteration of the form IN, the name must denote an
1909 -- iterator, typically the result of a call to Iterate. Give a
1910 -- useful error message when the name is a container by itself.
1912 -- The type may be a formal container type, which has to have
1913 -- an Iterable aspect detailing the required primitives.
1915 if Is_Entity_Name (Original_Node (Name (N)))
1916 and then not Is_Iterator (Typ)
1917 then
1918 if Has_Aspect (Typ, Aspect_Iterable) then
1919 null;
1921 elsif not Has_Aspect (Typ, Aspect_Iterator_Element) then
1922 Error_Msg_NE
1923 ("cannot iterate over&", Name (N), Typ);
1924 else
1925 Error_Msg_N
1926 ("name must be an iterator, not a container", Name (N));
1927 end if;
1929 if Has_Aspect (Typ, Aspect_Iterable) then
1930 null;
1931 else
1932 Error_Msg_NE
1933 ("\to iterate directly over the elements of a container, "
1934 & "write `of &`", Name (N), Original_Node (Name (N)));
1935 end if;
1936 end if;
1938 -- The result type of Iterate function is the classwide type of
1939 -- the interface parent. We need the specific Cursor type defined
1940 -- in the container package. We obtain it by name for a predefined
1941 -- container, or through the Iterable aspect for a formal one.
1943 if Has_Aspect (Typ, Aspect_Iterable) then
1944 Set_Etype (Def_Id,
1945 Get_Cursor_Type
1946 (Parent (Find_Value_Of_Aspect (Typ, Aspect_Iterable)),
1947 Typ));
1948 Ent := Etype (Def_Id);
1950 else
1951 Ent := First_Entity (Scope (Typ));
1952 while Present (Ent) loop
1953 if Chars (Ent) = Name_Cursor then
1954 Set_Etype (Def_Id, Etype (Ent));
1955 exit;
1956 end if;
1958 Next_Entity (Ent);
1959 end loop;
1960 end if;
1961 end if;
1962 end if;
1964 -- A loop parameter cannot be volatile. This check is peformed only when
1965 -- SPARK_Mode is on as it is not a standard Ada legality check.
1967 -- Not clear whether this applies to element iterators, where the
1968 -- cursor is not an explicit entity ???
1970 if SPARK_Mode = On
1971 and then not Of_Present (N)
1972 and then Is_SPARK_Volatile_Object (Ent)
1973 then
1974 Error_Msg_N
1975 ("loop parameter cannot be volatile (SPARK RM 7.1.3(6))", Ent);
1976 end if;
1977 end Analyze_Iterator_Specification;
1979 -------------------
1980 -- Analyze_Label --
1981 -------------------
1983 -- Note: the semantic work required for analyzing labels (setting them as
1984 -- reachable) was done in a prepass through the statements in the block,
1985 -- so that forward gotos would be properly handled. See Analyze_Statements
1986 -- for further details. The only processing required here is to deal with
1987 -- optimizations that depend on an assumption of sequential control flow,
1988 -- since of course the occurrence of a label breaks this assumption.
1990 procedure Analyze_Label (N : Node_Id) is
1991 pragma Warnings (Off, N);
1992 begin
1993 Kill_Current_Values;
1994 end Analyze_Label;
1996 --------------------------
1997 -- Analyze_Label_Entity --
1998 --------------------------
2000 procedure Analyze_Label_Entity (E : Entity_Id) is
2001 begin
2002 Set_Ekind (E, E_Label);
2003 Set_Etype (E, Standard_Void_Type);
2004 Set_Enclosing_Scope (E, Current_Scope);
2005 Set_Reachable (E, True);
2006 end Analyze_Label_Entity;
2008 ------------------------------------------
2009 -- Analyze_Loop_Parameter_Specification --
2010 ------------------------------------------
2012 procedure Analyze_Loop_Parameter_Specification (N : Node_Id) is
2013 Loop_Nod : constant Node_Id := Parent (Parent (N));
2015 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
2016 -- If the bounds are given by a 'Range reference on a function call
2017 -- that returns a controlled array, introduce an explicit declaration
2018 -- to capture the bounds, so that the function result can be finalized
2019 -- in timely fashion.
2021 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean;
2022 -- N is the node for an arbitrary construct. This function searches the
2023 -- construct N to see if any expressions within it contain function
2024 -- calls that use the secondary stack, returning True if any such call
2025 -- is found, and False otherwise.
2027 procedure Process_Bounds (R : Node_Id);
2028 -- If the iteration is given by a range, create temporaries and
2029 -- assignment statements block to capture the bounds and perform
2030 -- required finalization actions in case a bound includes a function
2031 -- call that uses the temporary stack. We first pre-analyze a copy of
2032 -- the range in order to determine the expected type, and analyze and
2033 -- resolve the original bounds.
2035 --------------------------------------
2036 -- Check_Controlled_Array_Attribute --
2037 --------------------------------------
2039 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
2040 begin
2041 if Nkind (DS) = N_Attribute_Reference
2042 and then Is_Entity_Name (Prefix (DS))
2043 and then Ekind (Entity (Prefix (DS))) = E_Function
2044 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
2045 and then
2046 Is_Controlled (Component_Type (Etype (Entity (Prefix (DS)))))
2047 and then Expander_Active
2048 then
2049 declare
2050 Loc : constant Source_Ptr := Sloc (N);
2051 Arr : constant Entity_Id := Etype (Entity (Prefix (DS)));
2052 Indx : constant Entity_Id :=
2053 Base_Type (Etype (First_Index (Arr)));
2054 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
2055 Decl : Node_Id;
2057 begin
2058 Decl :=
2059 Make_Subtype_Declaration (Loc,
2060 Defining_Identifier => Subt,
2061 Subtype_Indication =>
2062 Make_Subtype_Indication (Loc,
2063 Subtype_Mark => New_Occurrence_Of (Indx, Loc),
2064 Constraint =>
2065 Make_Range_Constraint (Loc, Relocate_Node (DS))));
2066 Insert_Before (Loop_Nod, Decl);
2067 Analyze (Decl);
2069 Rewrite (DS,
2070 Make_Attribute_Reference (Loc,
2071 Prefix => New_Occurrence_Of (Subt, Loc),
2072 Attribute_Name => Attribute_Name (DS)));
2074 Analyze (DS);
2075 end;
2076 end if;
2077 end Check_Controlled_Array_Attribute;
2079 ------------------------------------
2080 -- Has_Call_Using_Secondary_Stack --
2081 ------------------------------------
2083 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean is
2085 function Check_Call (N : Node_Id) return Traverse_Result;
2086 -- Check if N is a function call which uses the secondary stack
2088 ----------------
2089 -- Check_Call --
2090 ----------------
2092 function Check_Call (N : Node_Id) return Traverse_Result is
2093 Nam : Node_Id;
2094 Subp : Entity_Id;
2095 Return_Typ : Entity_Id;
2097 begin
2098 if Nkind (N) = N_Function_Call then
2099 Nam := Name (N);
2101 -- Call using access to subprogram with explicit dereference
2103 if Nkind (Nam) = N_Explicit_Dereference then
2104 Subp := Etype (Nam);
2106 -- Call using a selected component notation or Ada 2005 object
2107 -- operation notation
2109 elsif Nkind (Nam) = N_Selected_Component then
2110 Subp := Entity (Selector_Name (Nam));
2112 -- Common case
2114 else
2115 Subp := Entity (Nam);
2116 end if;
2118 Return_Typ := Etype (Subp);
2120 if Is_Composite_Type (Return_Typ)
2121 and then not Is_Constrained (Return_Typ)
2122 then
2123 return Abandon;
2125 elsif Sec_Stack_Needed_For_Return (Subp) then
2126 return Abandon;
2127 end if;
2128 end if;
2130 -- Continue traversing the tree
2132 return OK;
2133 end Check_Call;
2135 function Check_Calls is new Traverse_Func (Check_Call);
2137 -- Start of processing for Has_Call_Using_Secondary_Stack
2139 begin
2140 return Check_Calls (N) = Abandon;
2141 end Has_Call_Using_Secondary_Stack;
2143 --------------------
2144 -- Process_Bounds --
2145 --------------------
2147 procedure Process_Bounds (R : Node_Id) is
2148 Loc : constant Source_Ptr := Sloc (N);
2150 function One_Bound
2151 (Original_Bound : Node_Id;
2152 Analyzed_Bound : Node_Id;
2153 Typ : Entity_Id) return Node_Id;
2154 -- Capture value of bound and return captured value
2156 ---------------
2157 -- One_Bound --
2158 ---------------
2160 function One_Bound
2161 (Original_Bound : Node_Id;
2162 Analyzed_Bound : Node_Id;
2163 Typ : Entity_Id) return Node_Id
2165 Assign : Node_Id;
2166 Decl : Node_Id;
2167 Id : Entity_Id;
2169 begin
2170 -- If the bound is a constant or an object, no need for a separate
2171 -- declaration. If the bound is the result of previous expansion
2172 -- it is already analyzed and should not be modified. Note that
2173 -- the Bound will be resolved later, if needed, as part of the
2174 -- call to Make_Index (literal bounds may need to be resolved to
2175 -- type Integer).
2177 if Analyzed (Original_Bound) then
2178 return Original_Bound;
2180 elsif Nkind_In (Analyzed_Bound, N_Integer_Literal,
2181 N_Character_Literal)
2182 or else Is_Entity_Name (Analyzed_Bound)
2183 then
2184 Analyze_And_Resolve (Original_Bound, Typ);
2185 return Original_Bound;
2186 end if;
2188 -- Normally, the best approach is simply to generate a constant
2189 -- declaration that captures the bound. However, there is a nasty
2190 -- case where this is wrong. If the bound is complex, and has a
2191 -- possible use of the secondary stack, we need to generate a
2192 -- separate assignment statement to ensure the creation of a block
2193 -- which will release the secondary stack.
2195 -- We prefer the constant declaration, since it leaves us with a
2196 -- proper trace of the value, useful in optimizations that get rid
2197 -- of junk range checks.
2199 if not Has_Call_Using_Secondary_Stack (Analyzed_Bound) then
2200 Analyze_And_Resolve (Original_Bound, Typ);
2202 -- Ensure that the bound is valid. This check should not be
2203 -- generated when the range belongs to a quantified expression
2204 -- as the construct is still not expanded into its final form.
2206 if Nkind (Parent (R)) /= N_Loop_Parameter_Specification
2207 or else Nkind (Parent (Parent (R))) /= N_Quantified_Expression
2208 then
2209 Ensure_Valid (Original_Bound);
2210 end if;
2212 Force_Evaluation (Original_Bound);
2213 return Original_Bound;
2214 end if;
2216 Id := Make_Temporary (Loc, 'R', Original_Bound);
2218 -- Here we make a declaration with a separate assignment
2219 -- statement, and insert before loop header.
2221 Decl :=
2222 Make_Object_Declaration (Loc,
2223 Defining_Identifier => Id,
2224 Object_Definition => New_Occurrence_Of (Typ, Loc));
2226 Assign :=
2227 Make_Assignment_Statement (Loc,
2228 Name => New_Occurrence_Of (Id, Loc),
2229 Expression => Relocate_Node (Original_Bound));
2231 Insert_Actions (Loop_Nod, New_List (Decl, Assign));
2233 -- Now that this temporary variable is initialized we decorate it
2234 -- as safe-to-reevaluate to inform to the backend that no further
2235 -- asignment will be issued and hence it can be handled as side
2236 -- effect free. Note that this decoration must be done when the
2237 -- assignment has been analyzed because otherwise it will be
2238 -- rejected (see Analyze_Assignment).
2240 Set_Is_Safe_To_Reevaluate (Id);
2242 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
2244 if Nkind (Assign) = N_Assignment_Statement then
2245 return Expression (Assign);
2246 else
2247 return Original_Bound;
2248 end if;
2249 end One_Bound;
2251 Hi : constant Node_Id := High_Bound (R);
2252 Lo : constant Node_Id := Low_Bound (R);
2253 R_Copy : constant Node_Id := New_Copy_Tree (R);
2254 New_Hi : Node_Id;
2255 New_Lo : Node_Id;
2256 Typ : Entity_Id;
2258 -- Start of processing for Process_Bounds
2260 begin
2261 Set_Parent (R_Copy, Parent (R));
2262 Preanalyze_Range (R_Copy);
2263 Typ := Etype (R_Copy);
2265 -- If the type of the discrete range is Universal_Integer, then the
2266 -- bound's type must be resolved to Integer, and any object used to
2267 -- hold the bound must also have type Integer, unless the literal
2268 -- bounds are constant-folded expressions with a user-defined type.
2270 if Typ = Universal_Integer then
2271 if Nkind (Lo) = N_Integer_Literal
2272 and then Present (Etype (Lo))
2273 and then Scope (Etype (Lo)) /= Standard_Standard
2274 then
2275 Typ := Etype (Lo);
2277 elsif Nkind (Hi) = N_Integer_Literal
2278 and then Present (Etype (Hi))
2279 and then Scope (Etype (Hi)) /= Standard_Standard
2280 then
2281 Typ := Etype (Hi);
2283 else
2284 Typ := Standard_Integer;
2285 end if;
2286 end if;
2288 Set_Etype (R, Typ);
2290 New_Lo := One_Bound (Lo, Low_Bound (R_Copy), Typ);
2291 New_Hi := One_Bound (Hi, High_Bound (R_Copy), Typ);
2293 -- Propagate staticness to loop range itself, in case the
2294 -- corresponding subtype is static.
2296 if New_Lo /= Lo and then Is_Static_Expression (New_Lo) then
2297 Rewrite (Low_Bound (R), New_Copy (New_Lo));
2298 end if;
2300 if New_Hi /= Hi and then Is_Static_Expression (New_Hi) then
2301 Rewrite (High_Bound (R), New_Copy (New_Hi));
2302 end if;
2303 end Process_Bounds;
2305 -- Local variables
2307 DS : constant Node_Id := Discrete_Subtype_Definition (N);
2308 Id : constant Entity_Id := Defining_Identifier (N);
2310 DS_Copy : Node_Id;
2312 -- Start of processing for Analyze_Loop_Parameter_Specification
2314 begin
2315 Enter_Name (Id);
2317 -- We always consider the loop variable to be referenced, since the loop
2318 -- may be used just for counting purposes.
2320 Generate_Reference (Id, N, ' ');
2322 -- Check for the case of loop variable hiding a local variable (used
2323 -- later on to give a nice warning if the hidden variable is never
2324 -- assigned).
2326 declare
2327 H : constant Entity_Id := Homonym (Id);
2328 begin
2329 if Present (H)
2330 and then Ekind (H) = E_Variable
2331 and then Is_Discrete_Type (Etype (H))
2332 and then Enclosing_Dynamic_Scope (H) = Enclosing_Dynamic_Scope (Id)
2333 then
2334 Set_Hiding_Loop_Variable (H, Id);
2335 end if;
2336 end;
2338 -- Loop parameter specification must include subtype mark in SPARK
2340 if Nkind (DS) = N_Range then
2341 Check_SPARK_Restriction
2342 ("loop parameter specification must include subtype mark", N);
2343 end if;
2345 -- Analyze the subtype definition and create temporaries for the bounds.
2346 -- Do not evaluate the range when preanalyzing a quantified expression
2347 -- because bounds expressed as function calls with side effects will be
2348 -- erroneously replicated.
2350 if Nkind (DS) = N_Range
2351 and then Expander_Active
2352 and then Nkind (Parent (N)) /= N_Quantified_Expression
2353 then
2354 Process_Bounds (DS);
2356 -- Either the expander not active or the range of iteration is a subtype
2357 -- indication, an entity, or a function call that yields an aggregate or
2358 -- a container.
2360 else
2361 DS_Copy := New_Copy_Tree (DS);
2362 Set_Parent (DS_Copy, Parent (DS));
2363 Preanalyze_Range (DS_Copy);
2365 -- Ada 2012: If the domain of iteration is:
2367 -- a) a function call,
2368 -- b) an identifier that is not a type,
2369 -- c) an attribute reference 'Old (within a postcondition)
2371 -- then it is an iteration over a container. It was classified as
2372 -- a loop specification by the parser, and must be rewritten now
2373 -- to activate container iteration.
2375 if Nkind (DS_Copy) = N_Function_Call
2376 or else (Is_Entity_Name (DS_Copy)
2377 and then not Is_Type (Entity (DS_Copy)))
2378 or else (Nkind (DS_Copy) = N_Attribute_Reference
2379 and then Attribute_Name (DS_Copy) = Name_Old)
2380 then
2381 -- This is an iterator specification. Rewrite it as such and
2382 -- analyze it to capture function calls that may require
2383 -- finalization actions.
2385 declare
2386 I_Spec : constant Node_Id :=
2387 Make_Iterator_Specification (Sloc (N),
2388 Defining_Identifier => Relocate_Node (Id),
2389 Name => DS_Copy,
2390 Subtype_Indication => Empty,
2391 Reverse_Present => Reverse_Present (N));
2392 Scheme : constant Node_Id := Parent (N);
2394 begin
2395 Set_Iterator_Specification (Scheme, I_Spec);
2396 Set_Loop_Parameter_Specification (Scheme, Empty);
2397 Analyze_Iterator_Specification (I_Spec);
2399 -- In a generic context, analyze the original domain of
2400 -- iteration, for name capture.
2402 if not Expander_Active then
2403 Analyze (DS);
2404 end if;
2406 -- Set kind of loop parameter, which may be used in the
2407 -- subsequent analysis of the condition in a quantified
2408 -- expression.
2410 Set_Ekind (Id, E_Loop_Parameter);
2411 return;
2412 end;
2414 -- Domain of iteration is not a function call, and is side-effect
2415 -- free.
2417 else
2418 -- A quantified expression that appears in a pre/post condition
2419 -- is pre-analyzed several times. If the range is given by an
2420 -- attribute reference it is rewritten as a range, and this is
2421 -- done even with expansion disabled. If the type is already set
2422 -- do not reanalyze, because a range with static bounds may be
2423 -- typed Integer by default.
2425 if Nkind (Parent (N)) = N_Quantified_Expression
2426 and then Present (Etype (DS))
2427 then
2428 null;
2429 else
2430 Analyze (DS);
2431 end if;
2432 end if;
2433 end if;
2435 if DS = Error then
2436 return;
2437 end if;
2439 -- Some additional checks if we are iterating through a type
2441 if Is_Entity_Name (DS)
2442 and then Present (Entity (DS))
2443 and then Is_Type (Entity (DS))
2444 then
2445 -- The subtype indication may denote the completion of an incomplete
2446 -- type declaration.
2448 if Ekind (Entity (DS)) = E_Incomplete_Type then
2449 Set_Entity (DS, Get_Full_View (Entity (DS)));
2450 Set_Etype (DS, Entity (DS));
2451 end if;
2453 -- Attempt to iterate through non-static predicate. Note that a type
2454 -- with inherited predicates may have both static and dynamic forms.
2455 -- In this case it is not sufficent to check the static predicate
2456 -- function only, look for a dynamic predicate aspect as well.
2458 if Is_Discrete_Type (Entity (DS))
2459 and then Present (Predicate_Function (Entity (DS)))
2460 and then (No (Static_Predicate (Entity (DS)))
2461 or else Has_Dynamic_Predicate_Aspect (Entity (DS)))
2462 then
2463 Bad_Predicated_Subtype_Use
2464 ("cannot use subtype& with non-static predicate for loop " &
2465 "iteration", DS, Entity (DS), Suggest_Static => True);
2466 end if;
2467 end if;
2469 -- Error if not discrete type
2471 if not Is_Discrete_Type (Etype (DS)) then
2472 Wrong_Type (DS, Any_Discrete);
2473 Set_Etype (DS, Any_Type);
2474 end if;
2476 Check_Controlled_Array_Attribute (DS);
2478 Make_Index (DS, N, In_Iter_Schm => True);
2479 Set_Ekind (Id, E_Loop_Parameter);
2481 -- A quantified expression which appears in a pre- or post-condition may
2482 -- be analyzed multiple times. The analysis of the range creates several
2483 -- itypes which reside in different scopes depending on whether the pre-
2484 -- or post-condition has been expanded. Update the type of the loop
2485 -- variable to reflect the proper itype at each stage of analysis.
2487 if No (Etype (Id))
2488 or else Etype (Id) = Any_Type
2489 or else
2490 (Present (Etype (Id))
2491 and then Is_Itype (Etype (Id))
2492 and then Nkind (Parent (Loop_Nod)) = N_Expression_With_Actions
2493 and then Nkind (Original_Node (Parent (Loop_Nod))) =
2494 N_Quantified_Expression)
2495 then
2496 Set_Etype (Id, Etype (DS));
2497 end if;
2499 -- Treat a range as an implicit reference to the type, to inhibit
2500 -- spurious warnings.
2502 Generate_Reference (Base_Type (Etype (DS)), N, ' ');
2503 Set_Is_Known_Valid (Id, True);
2505 -- The loop is not a declarative part, so the loop variable must be
2506 -- frozen explicitly. Do not freeze while preanalyzing a quantified
2507 -- expression because the freeze node will not be inserted into the
2508 -- tree due to flag Is_Spec_Expression being set.
2510 if Nkind (Parent (N)) /= N_Quantified_Expression then
2511 declare
2512 Flist : constant List_Id := Freeze_Entity (Id, N);
2513 begin
2514 if Is_Non_Empty_List (Flist) then
2515 Insert_Actions (N, Flist);
2516 end if;
2517 end;
2518 end if;
2520 -- Check for null or possibly null range and issue warning. We suppress
2521 -- such messages in generic templates and instances, because in practice
2522 -- they tend to be dubious in these cases. The check applies as well to
2523 -- rewritten array element loops where a null range may be detected
2524 -- statically.
2526 if Nkind (DS) = N_Range then
2527 declare
2528 L : constant Node_Id := Low_Bound (DS);
2529 H : constant Node_Id := High_Bound (DS);
2531 begin
2532 -- If range of loop is null, issue warning
2534 if Compile_Time_Compare (L, H, Assume_Valid => True) = GT then
2536 -- Suppress the warning if inside a generic template or
2537 -- instance, since in practice they tend to be dubious in these
2538 -- cases since they can result from intended parameterization.
2540 if not Inside_A_Generic and then not In_Instance then
2542 -- Specialize msg if invalid values could make the loop
2543 -- non-null after all.
2545 if Compile_Time_Compare
2546 (L, H, Assume_Valid => False) = GT
2547 then
2548 -- Since we know the range of the loop is null, set the
2549 -- appropriate flag to remove the loop entirely during
2550 -- expansion.
2552 Set_Is_Null_Loop (Loop_Nod);
2554 if Comes_From_Source (N) then
2555 Error_Msg_N
2556 ("??loop range is null, loop will not execute", DS);
2557 end if;
2559 -- Here is where the loop could execute because of
2560 -- invalid values, so issue appropriate message and in
2561 -- this case we do not set the Is_Null_Loop flag since
2562 -- the loop may execute.
2564 elsif Comes_From_Source (N) then
2565 Error_Msg_N
2566 ("??loop range may be null, loop may not execute",
2567 DS);
2568 Error_Msg_N
2569 ("??can only execute if invalid values are present",
2570 DS);
2571 end if;
2572 end if;
2574 -- In either case, suppress warnings in the body of the loop,
2575 -- since it is likely that these warnings will be inappropriate
2576 -- if the loop never actually executes, which is likely.
2578 Set_Suppress_Loop_Warnings (Loop_Nod);
2580 -- The other case for a warning is a reverse loop where the
2581 -- upper bound is the integer literal zero or one, and the
2582 -- lower bound may exceed this value.
2584 -- For example, we have
2586 -- for J in reverse N .. 1 loop
2588 -- In practice, this is very likely to be a case of reversing
2589 -- the bounds incorrectly in the range.
2591 elsif Reverse_Present (N)
2592 and then Nkind (Original_Node (H)) = N_Integer_Literal
2593 and then
2594 (Intval (Original_Node (H)) = Uint_0
2595 or else
2596 Intval (Original_Node (H)) = Uint_1)
2597 then
2598 -- Lower bound may in fact be known and known not to exceed
2599 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
2601 if Compile_Time_Known_Value (L)
2602 and then Expr_Value (L) <= Expr_Value (H)
2603 then
2604 null;
2606 -- Otherwise warning is warranted
2608 else
2609 Error_Msg_N ("??loop range may be null", DS);
2610 Error_Msg_N ("\??bounds may be wrong way round", DS);
2611 end if;
2612 end if;
2613 end;
2614 end if;
2616 -- A loop parameter cannot be volatile. This check is peformed only when
2617 -- SPARK_Mode is on as it is not a standard Ada legality check.
2619 if SPARK_Mode = On and then Is_SPARK_Volatile_Object (Id) then
2620 Error_Msg_N
2621 ("loop parameter cannot be volatile (SPARK RM 7.1.3(6))", Id);
2622 end if;
2623 end Analyze_Loop_Parameter_Specification;
2625 ----------------------------
2626 -- Analyze_Loop_Statement --
2627 ----------------------------
2629 procedure Analyze_Loop_Statement (N : Node_Id) is
2631 function Is_Container_Iterator (Iter : Node_Id) return Boolean;
2632 -- Given a loop iteration scheme, determine whether it is an Ada 2012
2633 -- container iteration.
2635 function Is_Wrapped_In_Block (N : Node_Id) return Boolean;
2636 -- Determine whether node N is the sole statement of a block
2638 ---------------------------
2639 -- Is_Container_Iterator --
2640 ---------------------------
2642 function Is_Container_Iterator (Iter : Node_Id) return Boolean is
2643 begin
2644 -- Infinite loop
2646 if No (Iter) then
2647 return False;
2649 -- While loop
2651 elsif Present (Condition (Iter)) then
2652 return False;
2654 -- for Def_Id in [reverse] Name loop
2655 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
2657 elsif Present (Iterator_Specification (Iter)) then
2658 declare
2659 Nam : constant Node_Id := Name (Iterator_Specification (Iter));
2660 Nam_Copy : Node_Id;
2662 begin
2663 Nam_Copy := New_Copy_Tree (Nam);
2664 Set_Parent (Nam_Copy, Parent (Nam));
2665 Preanalyze_Range (Nam_Copy);
2667 -- The only two options here are iteration over a container or
2668 -- an array.
2670 return not Is_Array_Type (Etype (Nam_Copy));
2671 end;
2673 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
2675 else
2676 declare
2677 LP : constant Node_Id := Loop_Parameter_Specification (Iter);
2678 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
2679 DS_Copy : Node_Id;
2681 begin
2682 DS_Copy := New_Copy_Tree (DS);
2683 Set_Parent (DS_Copy, Parent (DS));
2684 Preanalyze_Range (DS_Copy);
2686 -- Check for a call to Iterate ()
2688 return
2689 Nkind (DS_Copy) = N_Function_Call
2690 and then Needs_Finalization (Etype (DS_Copy));
2691 end;
2692 end if;
2693 end Is_Container_Iterator;
2695 -------------------------
2696 -- Is_Wrapped_In_Block --
2697 -------------------------
2699 function Is_Wrapped_In_Block (N : Node_Id) return Boolean is
2700 HSS : constant Node_Id := Parent (N);
2702 begin
2703 return
2704 Nkind (HSS) = N_Handled_Sequence_Of_Statements
2705 and then Nkind (Parent (HSS)) = N_Block_Statement
2706 and then First (Statements (HSS)) = N
2707 and then No (Next (First (Statements (HSS))));
2708 end Is_Wrapped_In_Block;
2710 -- Local declarations
2712 Id : constant Node_Id := Identifier (N);
2713 Iter : constant Node_Id := Iteration_Scheme (N);
2714 Loc : constant Source_Ptr := Sloc (N);
2715 Ent : Entity_Id;
2716 Stmt : Node_Id;
2718 -- Start of processing for Analyze_Loop_Statement
2720 begin
2721 if Present (Id) then
2723 -- Make name visible, e.g. for use in exit statements. Loop labels
2724 -- are always considered to be referenced.
2726 Analyze (Id);
2727 Ent := Entity (Id);
2729 -- Guard against serious error (typically, a scope mismatch when
2730 -- semantic analysis is requested) by creating loop entity to
2731 -- continue analysis.
2733 if No (Ent) then
2734 if Total_Errors_Detected /= 0 then
2735 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
2736 else
2737 raise Program_Error;
2738 end if;
2740 else
2741 Generate_Reference (Ent, N, ' ');
2742 Generate_Definition (Ent);
2744 -- If we found a label, mark its type. If not, ignore it, since it
2745 -- means we have a conflicting declaration, which would already
2746 -- have been diagnosed at declaration time. Set Label_Construct
2747 -- of the implicit label declaration, which is not created by the
2748 -- parser for generic units.
2750 if Ekind (Ent) = E_Label then
2751 Set_Ekind (Ent, E_Loop);
2753 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
2754 Set_Label_Construct (Parent (Ent), N);
2755 end if;
2756 end if;
2757 end if;
2759 -- Case of no identifier present
2761 else
2762 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
2763 Set_Etype (Ent, Standard_Void_Type);
2764 Set_Parent (Ent, N);
2765 end if;
2767 -- Iteration over a container in Ada 2012 involves the creation of a
2768 -- controlled iterator object. Wrap the loop in a block to ensure the
2769 -- timely finalization of the iterator and release of container locks.
2771 if Ada_Version >= Ada_2012
2772 and then Is_Container_Iterator (Iter)
2773 and then not Is_Wrapped_In_Block (N)
2774 then
2775 Rewrite (N,
2776 Make_Block_Statement (Loc,
2777 Declarations => New_List,
2778 Handled_Statement_Sequence =>
2779 Make_Handled_Sequence_Of_Statements (Loc,
2780 Statements => New_List (Relocate_Node (N)))));
2782 Analyze (N);
2783 return;
2784 end if;
2786 -- Kill current values on entry to loop, since statements in the body of
2787 -- the loop may have been executed before the loop is entered. Similarly
2788 -- we kill values after the loop, since we do not know that the body of
2789 -- the loop was executed.
2791 Kill_Current_Values;
2792 Push_Scope (Ent);
2793 Analyze_Iteration_Scheme (Iter);
2795 -- Check for following case which merits a warning if the type E of is
2796 -- a multi-dimensional array (and no explicit subscript ranges present).
2798 -- for J in E'Range
2799 -- for K in E'Range
2801 if Present (Iter)
2802 and then Present (Loop_Parameter_Specification (Iter))
2803 then
2804 declare
2805 LPS : constant Node_Id := Loop_Parameter_Specification (Iter);
2806 DSD : constant Node_Id :=
2807 Original_Node (Discrete_Subtype_Definition (LPS));
2808 begin
2809 if Nkind (DSD) = N_Attribute_Reference
2810 and then Attribute_Name (DSD) = Name_Range
2811 and then No (Expressions (DSD))
2812 then
2813 declare
2814 Typ : constant Entity_Id := Etype (Prefix (DSD));
2815 begin
2816 if Is_Array_Type (Typ)
2817 and then Number_Dimensions (Typ) > 1
2818 and then Nkind (Parent (N)) = N_Loop_Statement
2819 and then Present (Iteration_Scheme (Parent (N)))
2820 then
2821 declare
2822 OIter : constant Node_Id :=
2823 Iteration_Scheme (Parent (N));
2824 OLPS : constant Node_Id :=
2825 Loop_Parameter_Specification (OIter);
2826 ODSD : constant Node_Id :=
2827 Original_Node (Discrete_Subtype_Definition (OLPS));
2828 begin
2829 if Nkind (ODSD) = N_Attribute_Reference
2830 and then Attribute_Name (ODSD) = Name_Range
2831 and then No (Expressions (ODSD))
2832 and then Etype (Prefix (ODSD)) = Typ
2833 then
2834 Error_Msg_Sloc := Sloc (ODSD);
2835 Error_Msg_N
2836 ("inner range same as outer range#??", DSD);
2837 end if;
2838 end;
2839 end if;
2840 end;
2841 end if;
2842 end;
2843 end if;
2845 -- Analyze the statements of the body except in the case of an Ada 2012
2846 -- iterator with the expander active. In this case the expander will do
2847 -- a rewrite of the loop into a while loop. We will then analyze the
2848 -- loop body when we analyze this while loop.
2850 -- We need to do this delay because if the container is for indefinite
2851 -- types the actual subtype of the components will only be determined
2852 -- when the cursor declaration is analyzed.
2854 -- If the expander is not active, or in SPARK mode, then we want to
2855 -- analyze the loop body now even in the Ada 2012 iterator case, since
2856 -- the rewriting will not be done. Insert the loop variable in the
2857 -- current scope, if not done when analysing the iteration scheme.
2859 if No (Iter)
2860 or else No (Iterator_Specification (Iter))
2861 or else not Expander_Active
2862 then
2863 if Present (Iter)
2864 and then Present (Iterator_Specification (Iter))
2865 then
2866 declare
2867 Id : constant Entity_Id :=
2868 Defining_Identifier (Iterator_Specification (Iter));
2869 begin
2870 if Scope (Id) /= Current_Scope then
2871 Enter_Name (Id);
2872 end if;
2873 end;
2874 end if;
2876 Analyze_Statements (Statements (N));
2877 end if;
2879 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
2880 -- the loop is transformed into a conditional block. Retrieve the loop.
2882 Stmt := N;
2884 if Subject_To_Loop_Entry_Attributes (Stmt) then
2885 Stmt := Find_Loop_In_Conditional_Block (Stmt);
2886 end if;
2888 -- Finish up processing for the loop. We kill all current values, since
2889 -- in general we don't know if the statements in the loop have been
2890 -- executed. We could do a bit better than this with a loop that we
2891 -- know will execute at least once, but it's not worth the trouble and
2892 -- the front end is not in the business of flow tracing.
2894 Process_End_Label (Stmt, 'e', Ent);
2895 End_Scope;
2896 Kill_Current_Values;
2898 -- Check for infinite loop. Skip check for generated code, since it
2899 -- justs waste time and makes debugging the routine called harder.
2901 -- Note that we have to wait till the body of the loop is fully analyzed
2902 -- before making this call, since Check_Infinite_Loop_Warning relies on
2903 -- being able to use semantic visibility information to find references.
2905 if Comes_From_Source (Stmt) then
2906 Check_Infinite_Loop_Warning (Stmt);
2907 end if;
2909 -- Code after loop is unreachable if the loop has no WHILE or FOR and
2910 -- contains no EXIT statements within the body of the loop.
2912 if No (Iter) and then not Has_Exit (Ent) then
2913 Check_Unreachable_Code (Stmt);
2914 end if;
2915 end Analyze_Loop_Statement;
2917 ----------------------------
2918 -- Analyze_Null_Statement --
2919 ----------------------------
2921 -- Note: the semantics of the null statement is implemented by a single
2922 -- null statement, too bad everything isn't as simple as this.
2924 procedure Analyze_Null_Statement (N : Node_Id) is
2925 pragma Warnings (Off, N);
2926 begin
2927 null;
2928 end Analyze_Null_Statement;
2930 ------------------------
2931 -- Analyze_Statements --
2932 ------------------------
2934 procedure Analyze_Statements (L : List_Id) is
2935 S : Node_Id;
2936 Lab : Entity_Id;
2938 begin
2939 -- The labels declared in the statement list are reachable from
2940 -- statements in the list. We do this as a prepass so that any goto
2941 -- statement will be properly flagged if its target is not reachable.
2942 -- This is not required, but is nice behavior.
2944 S := First (L);
2945 while Present (S) loop
2946 if Nkind (S) = N_Label then
2947 Analyze (Identifier (S));
2948 Lab := Entity (Identifier (S));
2950 -- If we found a label mark it as reachable
2952 if Ekind (Lab) = E_Label then
2953 Generate_Definition (Lab);
2954 Set_Reachable (Lab);
2956 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
2957 Set_Label_Construct (Parent (Lab), S);
2958 end if;
2960 -- If we failed to find a label, it means the implicit declaration
2961 -- of the label was hidden. A for-loop parameter can do this to
2962 -- a label with the same name inside the loop, since the implicit
2963 -- label declaration is in the innermost enclosing body or block
2964 -- statement.
2966 else
2967 Error_Msg_Sloc := Sloc (Lab);
2968 Error_Msg_N
2969 ("implicit label declaration for & is hidden#",
2970 Identifier (S));
2971 end if;
2972 end if;
2974 Next (S);
2975 end loop;
2977 -- Perform semantic analysis on all statements
2979 Conditional_Statements_Begin;
2981 S := First (L);
2982 while Present (S) loop
2983 Analyze (S);
2985 -- Remove dimension in all statements
2987 Remove_Dimension_In_Statement (S);
2988 Next (S);
2989 end loop;
2991 Conditional_Statements_End;
2993 -- Make labels unreachable. Visibility is not sufficient, because labels
2994 -- in one if-branch for example are not reachable from the other branch,
2995 -- even though their declarations are in the enclosing declarative part.
2997 S := First (L);
2998 while Present (S) loop
2999 if Nkind (S) = N_Label then
3000 Set_Reachable (Entity (Identifier (S)), False);
3001 end if;
3003 Next (S);
3004 end loop;
3005 end Analyze_Statements;
3007 ----------------------------
3008 -- Check_Unreachable_Code --
3009 ----------------------------
3011 procedure Check_Unreachable_Code (N : Node_Id) is
3012 Error_Node : Node_Id;
3013 P : Node_Id;
3015 begin
3016 if Is_List_Member (N) and then Comes_From_Source (N) then
3017 declare
3018 Nxt : Node_Id;
3020 begin
3021 Nxt := Original_Node (Next (N));
3023 -- Skip past pragmas
3025 while Nkind (Nxt) = N_Pragma loop
3026 Nxt := Original_Node (Next (Nxt));
3027 end loop;
3029 -- If a label follows us, then we never have dead code, since
3030 -- someone could branch to the label, so we just ignore it, unless
3031 -- we are in formal mode where goto statements are not allowed.
3033 if Nkind (Nxt) = N_Label
3034 and then not Restriction_Check_Required (SPARK_05)
3035 then
3036 return;
3038 -- Otherwise see if we have a real statement following us
3040 elsif Present (Nxt)
3041 and then Comes_From_Source (Nxt)
3042 and then Is_Statement (Nxt)
3043 then
3044 -- Special very annoying exception. If we have a return that
3045 -- follows a raise, then we allow it without a warning, since
3046 -- the Ada RM annoyingly requires a useless return here.
3048 if Nkind (Original_Node (N)) /= N_Raise_Statement
3049 or else Nkind (Nxt) /= N_Simple_Return_Statement
3050 then
3051 -- The rather strange shenanigans with the warning message
3052 -- here reflects the fact that Kill_Dead_Code is very good
3053 -- at removing warnings in deleted code, and this is one
3054 -- warning we would prefer NOT to have removed.
3056 Error_Node := Nxt;
3058 -- If we have unreachable code, analyze and remove the
3059 -- unreachable code, since it is useless and we don't
3060 -- want to generate junk warnings.
3062 -- We skip this step if we are not in code generation mode.
3063 -- This is the one case where we remove dead code in the
3064 -- semantics as opposed to the expander, and we do not want
3065 -- to remove code if we are not in code generation mode,
3066 -- since this messes up the ASIS trees.
3068 -- Note that one might react by moving the whole circuit to
3069 -- exp_ch5, but then we lose the warning in -gnatc mode.
3071 if Operating_Mode = Generate_Code then
3072 loop
3073 Nxt := Next (N);
3075 -- Quit deleting when we have nothing more to delete
3076 -- or if we hit a label (since someone could transfer
3077 -- control to a label, so we should not delete it).
3079 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
3081 -- Statement/declaration is to be deleted
3083 Analyze (Nxt);
3084 Remove (Nxt);
3085 Kill_Dead_Code (Nxt);
3086 end loop;
3087 end if;
3089 -- Now issue the warning (or error in formal mode)
3091 if Restriction_Check_Required (SPARK_05) then
3092 Check_SPARK_Restriction
3093 ("unreachable code is not allowed", Error_Node);
3094 else
3095 Error_Msg ("??unreachable code!", Sloc (Error_Node));
3096 end if;
3097 end if;
3099 -- If the unconditional transfer of control instruction is the
3100 -- last statement of a sequence, then see if our parent is one of
3101 -- the constructs for which we count unblocked exits, and if so,
3102 -- adjust the count.
3104 else
3105 P := Parent (N);
3107 -- Statements in THEN part or ELSE part of IF statement
3109 if Nkind (P) = N_If_Statement then
3110 null;
3112 -- Statements in ELSIF part of an IF statement
3114 elsif Nkind (P) = N_Elsif_Part then
3115 P := Parent (P);
3116 pragma Assert (Nkind (P) = N_If_Statement);
3118 -- Statements in CASE statement alternative
3120 elsif Nkind (P) = N_Case_Statement_Alternative then
3121 P := Parent (P);
3122 pragma Assert (Nkind (P) = N_Case_Statement);
3124 -- Statements in body of block
3126 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
3127 and then Nkind (Parent (P)) = N_Block_Statement
3128 then
3129 -- The original loop is now placed inside a block statement
3130 -- due to the expansion of attribute 'Loop_Entry. Return as
3131 -- this is not a "real" block for the purposes of exit
3132 -- counting.
3134 if Nkind (N) = N_Loop_Statement
3135 and then Subject_To_Loop_Entry_Attributes (N)
3136 then
3137 return;
3138 end if;
3140 -- Statements in exception handler in a block
3142 elsif Nkind (P) = N_Exception_Handler
3143 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
3144 and then Nkind (Parent (Parent (P))) = N_Block_Statement
3145 then
3146 null;
3148 -- None of these cases, so return
3150 else
3151 return;
3152 end if;
3154 -- This was one of the cases we are looking for (i.e. the
3155 -- parent construct was IF, CASE or block) so decrement count.
3157 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
3158 end if;
3159 end;
3160 end if;
3161 end Check_Unreachable_Code;
3163 ----------------------
3164 -- Preanalyze_Range --
3165 ----------------------
3167 procedure Preanalyze_Range (R_Copy : Node_Id) is
3168 Save_Analysis : constant Boolean := Full_Analysis;
3169 Typ : Entity_Id;
3171 begin
3172 Full_Analysis := False;
3173 Expander_Mode_Save_And_Set (False);
3175 Analyze (R_Copy);
3177 if Nkind (R_Copy) in N_Subexpr and then Is_Overloaded (R_Copy) then
3179 -- Apply preference rules for range of predefined integer types, or
3180 -- diagnose true ambiguity.
3182 declare
3183 I : Interp_Index;
3184 It : Interp;
3185 Found : Entity_Id := Empty;
3187 begin
3188 Get_First_Interp (R_Copy, I, It);
3189 while Present (It.Typ) loop
3190 if Is_Discrete_Type (It.Typ) then
3191 if No (Found) then
3192 Found := It.Typ;
3193 else
3194 if Scope (Found) = Standard_Standard then
3195 null;
3197 elsif Scope (It.Typ) = Standard_Standard then
3198 Found := It.Typ;
3200 else
3201 -- Both of them are user-defined
3203 Error_Msg_N
3204 ("ambiguous bounds in range of iteration", R_Copy);
3205 Error_Msg_N ("\possible interpretations:", R_Copy);
3206 Error_Msg_NE ("\\} ", R_Copy, Found);
3207 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
3208 exit;
3209 end if;
3210 end if;
3211 end if;
3213 Get_Next_Interp (I, It);
3214 end loop;
3215 end;
3216 end if;
3218 -- Subtype mark in iteration scheme
3220 if Is_Entity_Name (R_Copy) and then Is_Type (Entity (R_Copy)) then
3221 null;
3223 -- Expression in range, or Ada 2012 iterator
3225 elsif Nkind (R_Copy) in N_Subexpr then
3226 Resolve (R_Copy);
3227 Typ := Etype (R_Copy);
3229 if Is_Discrete_Type (Typ) then
3230 null;
3232 -- Check that the resulting object is an iterable container
3234 elsif Has_Aspect (Typ, Aspect_Iterator_Element)
3235 or else Has_Aspect (Typ, Aspect_Constant_Indexing)
3236 or else Has_Aspect (Typ, Aspect_Variable_Indexing)
3237 then
3238 null;
3240 -- The expression may yield an implicit reference to an iterable
3241 -- container. Insert explicit dereference so that proper type is
3242 -- visible in the loop.
3244 elsif Has_Implicit_Dereference (Etype (R_Copy)) then
3245 declare
3246 Disc : Entity_Id;
3248 begin
3249 Disc := First_Discriminant (Typ);
3250 while Present (Disc) loop
3251 if Has_Implicit_Dereference (Disc) then
3252 Build_Explicit_Dereference (R_Copy, Disc);
3253 exit;
3254 end if;
3256 Next_Discriminant (Disc);
3257 end loop;
3258 end;
3260 end if;
3261 end if;
3263 Expander_Mode_Restore;
3264 Full_Analysis := Save_Analysis;
3265 end Preanalyze_Range;
3267 end Sem_Ch5;