Skip several gcc.dg/builtin-dynamic-object-size tests on hppa*-*-hpux*
[official-gcc.git] / gcc / ada / sem_ch5.adb
blob43dee2b2b6febbe50b7d3d8d0e391d89c9e1eff4
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-2023, 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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Ghost; use Ghost;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Case; use Sem_Case;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Elab; use Sem_Elab;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.Nodes; use Sinfo.Nodes;
64 with Sinfo.Utils; use Sinfo.Utils;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
69 with Warnsw; use Warnsw;
71 package body Sem_Ch5 is
73 Current_Assignment : Node_Id := Empty;
74 -- This variable holds the node for an assignment that contains target
75 -- names. The corresponding flag has been set by the parser, and when
76 -- set the analysis of the RHS must be done with all expansion disabled,
77 -- because the assignment is reanalyzed after expansion has replaced all
78 -- occurrences of the target name appropriately.
80 Unblocked_Exit_Count : Nat := 0;
81 -- This variable is used when processing if statements, case statements,
82 -- and block statements. It counts the number of exit points that are not
83 -- blocked by unconditional transfer instructions: for IF and CASE, these
84 -- are the branches of the conditional; for a block, they are the statement
85 -- sequence of the block, and the statement sequences of any exception
86 -- handlers that are part of the block. When processing is complete, if
87 -- this count is zero, it means that control cannot fall through the IF,
88 -- CASE or block statement. This is used for the generation of warning
89 -- messages. This variable is recursively saved on entry to processing the
90 -- construct, and restored on exit.
92 function Has_Sec_Stack_Call (N : Node_Id) return Boolean;
93 -- N is the node for an arbitrary construct. This function searches the
94 -- construct N to see if it contains a function call that returns on the
95 -- secondary stack, returning True if any such call is found, and False
96 -- otherwise.
98 -- ??? The implementation invokes Sem_Util.Requires_Transient_Scope so it
99 -- will return True if N contains a function call that needs finalization,
100 -- in addition to the above specification. See Analyze_Loop_Statement for
101 -- a similar comment about this entanglement.
103 procedure Preanalyze_Range (R_Copy : Node_Id);
104 -- Determine expected type of range or domain of iteration of Ada 2012
105 -- loop by analyzing separate copy. Do the analysis and resolution of the
106 -- copy of the bound(s) with expansion disabled, to prevent the generation
107 -- of finalization actions. This prevents memory leaks when the bounds
108 -- contain calls to functions returning controlled arrays or when the
109 -- domain of iteration is a container.
111 ------------------------
112 -- Analyze_Assignment --
113 ------------------------
115 -- WARNING: This routine manages Ghost regions. Return statements must be
116 -- replaced by gotos which jump to the end of the routine and restore the
117 -- Ghost mode.
119 procedure Analyze_Assignment (N : Node_Id) is
120 Lhs : constant Node_Id := Name (N);
121 Rhs : constant Node_Id := Expression (N);
123 procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
124 -- N is the node for the left hand side of an assignment, and it is not
125 -- a variable. This routine issues an appropriate diagnostic.
127 function Is_Protected_Part_Of_Constituent
128 (Nod : Node_Id) return Boolean;
129 -- Determine whether arbitrary node Nod denotes a Part_Of constituent of
130 -- a single protected type.
132 procedure Kill_Lhs;
133 -- This is called to kill current value settings of a simple variable
134 -- on the left hand side. We call it if we find any error in analyzing
135 -- the assignment, and at the end of processing before setting any new
136 -- current values in place.
138 procedure Set_Assignment_Type
139 (Opnd : Node_Id;
140 Opnd_Type : in out Entity_Id);
141 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
142 -- nominal subtype. This procedure is used to deal with cases where the
143 -- nominal subtype must be replaced by the actual subtype.
145 function Within_Function return Boolean;
146 -- Determine whether the current scope is a function or appears within
147 -- one.
149 -------------------------------
150 -- Diagnose_Non_Variable_Lhs --
151 -------------------------------
153 procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
154 begin
155 -- Not worth posting another error if left hand side already flagged
156 -- as being illegal in some respect.
158 if Error_Posted (N) then
159 return;
161 -- Some special bad cases of entity names
163 elsif Is_Entity_Name (N) then
164 declare
165 Ent : constant Entity_Id := Entity (N);
167 begin
168 if Ekind (Ent) = E_Loop_Parameter
169 or else Is_Loop_Parameter (Ent)
170 then
171 Error_Msg_N ("assignment to loop parameter not allowed", N);
172 return;
174 elsif Ekind (Ent) = E_In_Parameter then
175 Error_Msg_N
176 ("assignment to IN mode parameter not allowed", N);
177 return;
179 -- Renamings of protected private components are turned into
180 -- constants when compiling a protected function. In the case
181 -- of single protected types, the private component appears
182 -- directly.
184 elsif (Is_Prival (Ent) and then Within_Function)
185 or else Is_Protected_Component (Ent)
186 then
187 Error_Msg_N
188 ("protected function cannot modify its protected object",
190 return;
191 end if;
192 end;
194 -- For indexed components, test prefix if it is in array. We do not
195 -- want to recurse for cases where the prefix is a pointer, since we
196 -- may get a message confusing the pointer and what it references.
198 elsif Nkind (N) = N_Indexed_Component
199 and then Is_Array_Type (Etype (Prefix (N)))
200 then
201 Diagnose_Non_Variable_Lhs (Prefix (N));
202 return;
204 -- Another special case for assignment to discriminant
206 elsif Nkind (N) = N_Selected_Component then
207 if Present (Entity (Selector_Name (N)))
208 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
209 then
210 Error_Msg_N ("assignment to discriminant not allowed", N);
211 return;
213 -- For selection from record, diagnose prefix, but note that again
214 -- we only do this for a record, not e.g. for a pointer.
216 elsif Is_Record_Type (Etype (Prefix (N))) then
217 Diagnose_Non_Variable_Lhs (Prefix (N));
218 return;
219 end if;
220 end if;
222 -- If we fall through, we have no special message to issue
224 Error_Msg_N ("left hand side of assignment must be a variable", N);
225 end Diagnose_Non_Variable_Lhs;
227 --------------------------------------
228 -- Is_Protected_Part_Of_Constituent --
229 --------------------------------------
231 function Is_Protected_Part_Of_Constituent
232 (Nod : Node_Id) return Boolean
234 Encap_Id : Entity_Id;
235 Var_Id : Entity_Id;
237 begin
238 -- Abstract states and variables may act as Part_Of constituents of
239 -- single protected types, however only variables can be modified by
240 -- an assignment.
242 if Is_Entity_Name (Nod) then
243 Var_Id := Entity (Nod);
245 if Present (Var_Id) and then Ekind (Var_Id) = E_Variable then
246 Encap_Id := Encapsulating_State (Var_Id);
248 -- To qualify, the node must denote a reference to a variable
249 -- whose encapsulating state is a single protected object.
251 return
252 Present (Encap_Id)
253 and then Is_Single_Protected_Object (Encap_Id);
254 end if;
255 end if;
257 return False;
258 end Is_Protected_Part_Of_Constituent;
260 --------------
261 -- Kill_Lhs --
262 --------------
264 procedure Kill_Lhs is
265 begin
266 if Is_Entity_Name (Lhs) then
267 declare
268 Ent : constant Entity_Id := Entity (Lhs);
269 begin
270 if Present (Ent) then
271 Kill_Current_Values (Ent);
272 end if;
273 end;
274 end if;
275 end Kill_Lhs;
277 -------------------------
278 -- Set_Assignment_Type --
279 -------------------------
281 procedure Set_Assignment_Type
282 (Opnd : Node_Id;
283 Opnd_Type : in out Entity_Id)
285 Decl : Node_Id;
287 begin
288 Require_Entity (Opnd);
290 -- If the assignment operand is an in-out or out parameter, then we
291 -- get the actual subtype (needed for the unconstrained case). If the
292 -- operand is the actual in an entry declaration, then within the
293 -- accept statement it is replaced with a local renaming, which may
294 -- also have an actual subtype. Likewise for a return object that
295 -- lives on the secondary stack.
297 if Is_Entity_Name (Opnd)
298 and then (Ekind (Entity (Opnd)) in E_Out_Parameter
299 | E_In_Out_Parameter
300 | E_Generic_In_Out_Parameter
301 or else
302 (Ekind (Entity (Opnd)) = E_Variable
303 and then Nkind (Parent (Entity (Opnd))) =
304 N_Object_Renaming_Declaration
305 and then Nkind (Parent (Parent (Entity (Opnd)))) =
306 N_Accept_Statement)
307 or else Is_Secondary_Stack_Return_Object (Entity (Opnd)))
308 then
309 Opnd_Type := Get_Actual_Subtype (Opnd);
311 -- If the assignment operand is a component reference, then we build
312 -- the actual subtype of the component for the unconstrained case,
313 -- unless there is already one or the type is an unchecked union.
315 elsif (Nkind (Opnd) = N_Selected_Component
316 or else (Nkind (Opnd) = N_Explicit_Dereference
317 and then No (Actual_Designated_Subtype (Opnd))))
318 and then not Is_Unchecked_Union (Opnd_Type)
319 then
320 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
322 if Present (Decl) then
323 Insert_Action (N, Decl);
324 Mark_Rewrite_Insertion (Decl);
325 Analyze (Decl);
326 Opnd_Type := Defining_Identifier (Decl);
327 Set_Etype (Opnd, Opnd_Type);
328 Freeze_Itype (Opnd_Type, N);
330 elsif Is_Constrained (Etype (Opnd)) then
331 Opnd_Type := Etype (Opnd);
332 end if;
334 -- For slice, use the constrained subtype created for the slice
336 elsif Nkind (Opnd) = N_Slice then
337 Opnd_Type := Etype (Opnd);
338 end if;
339 end Set_Assignment_Type;
341 ---------------------
342 -- Within_Function --
343 ---------------------
345 function Within_Function return Boolean is
346 Scop_Id : constant Entity_Id := Current_Scope;
348 begin
349 if Ekind (Scop_Id) = E_Function then
350 return True;
352 elsif Ekind (Enclosing_Dynamic_Scope (Scop_Id)) = E_Function then
353 return True;
354 end if;
356 return False;
357 end Within_Function;
359 -- Local variables
361 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
362 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
363 -- Save the Ghost-related attributes to restore on exit
365 T1 : Entity_Id;
366 T2 : Entity_Id;
368 Save_Full_Analysis : Boolean := False;
369 -- Force initialization to facilitate static analysis
371 -- Start of processing for Analyze_Assignment
373 begin
374 Mark_Coextensions (N, Rhs);
376 -- Preserve relevant elaboration-related attributes of the context which
377 -- are no longer available or very expensive to recompute once analysis,
378 -- resolution, and expansion are over.
380 Mark_Elaboration_Attributes
381 (N_Id => N,
382 Checks => True,
383 Modes => True);
385 -- An assignment statement is Ghost when the left hand side denotes a
386 -- Ghost entity. Set the mode now to ensure that any nodes generated
387 -- during analysis and expansion are properly marked as Ghost.
389 Mark_And_Set_Ghost_Assignment (N);
391 if Has_Target_Names (N) then
392 pragma Assert (No (Current_Assignment));
393 Current_Assignment := N;
394 Expander_Mode_Save_And_Set (False);
395 Save_Full_Analysis := Full_Analysis;
396 Full_Analysis := False;
397 end if;
399 Analyze (Lhs);
400 Analyze (Rhs);
402 -- Ensure that we never do an assignment on a variable marked as
403 -- Is_Safe_To_Reevaluate.
405 pragma Assert
406 (not Is_Entity_Name (Lhs)
407 or else Ekind (Entity (Lhs)) /= E_Variable
408 or else not Is_Safe_To_Reevaluate (Entity (Lhs)));
410 -- Start type analysis for assignment
412 T1 := Etype (Lhs);
414 -- In the most general case, both Lhs and Rhs can be overloaded, and we
415 -- must compute the intersection of the possible types on each side.
417 if Is_Overloaded (Lhs) then
418 declare
419 I : Interp_Index;
420 It : Interp;
422 begin
423 T1 := Any_Type;
424 Get_First_Interp (Lhs, I, It);
426 while Present (It.Typ) loop
428 -- An indexed component with generalized indexing is always
429 -- overloaded with the corresponding dereference. Discard the
430 -- interpretation that yields a reference type, which is not
431 -- assignable.
433 if Nkind (Lhs) = N_Indexed_Component
434 and then Present (Generalized_Indexing (Lhs))
435 and then Has_Implicit_Dereference (It.Typ)
436 then
437 null;
439 -- This may be a call to a parameterless function through an
440 -- implicit dereference, so discard interpretation as well.
442 elsif Is_Entity_Name (Lhs)
443 and then Has_Implicit_Dereference (It.Typ)
444 then
445 null;
447 elsif Has_Compatible_Type (Rhs, It.Typ) then
448 if T1 = Any_Type then
449 T1 := It.Typ;
450 else
451 -- An explicit dereference is overloaded if the prefix
452 -- is. Try to remove the ambiguity on the prefix, the
453 -- error will be posted there if the ambiguity is real.
455 if Nkind (Lhs) = N_Explicit_Dereference then
456 declare
457 PI : Interp_Index;
458 PI1 : Interp_Index := 0;
459 PIt : Interp;
460 Found : Boolean;
462 begin
463 Found := False;
464 Get_First_Interp (Prefix (Lhs), PI, PIt);
466 while Present (PIt.Typ) loop
467 if Is_Access_Type (PIt.Typ)
468 and then Has_Compatible_Type
469 (Rhs, Designated_Type (PIt.Typ))
470 then
471 if Found then
472 PIt :=
473 Disambiguate (Prefix (Lhs),
474 PI1, PI, Any_Type);
476 if PIt = No_Interp then
477 Error_Msg_N
478 ("ambiguous left-hand side in "
479 & "assignment", Lhs);
480 exit;
481 else
482 Resolve (Prefix (Lhs), PIt.Typ);
483 end if;
485 exit;
486 else
487 Found := True;
488 PI1 := PI;
489 end if;
490 end if;
492 Get_Next_Interp (PI, PIt);
493 end loop;
494 end;
496 else
497 Error_Msg_N
498 ("ambiguous left-hand side in assignment", Lhs);
499 exit;
500 end if;
501 end if;
502 end if;
504 Get_Next_Interp (I, It);
505 end loop;
506 end;
508 if T1 = Any_Type then
509 Error_Msg_N
510 ("no valid types for left-hand side for assignment", Lhs);
511 Kill_Lhs;
512 goto Leave;
513 end if;
514 end if;
516 -- The resulting assignment type is T1, so now we will resolve the left
517 -- hand side of the assignment using this determined type.
519 Resolve (Lhs, T1);
521 -- Cases where Lhs is not a variable. In an instance or an inlined body
522 -- no need for further check because assignment was legal in template.
524 if In_Inlined_Body then
525 null;
527 elsif not Is_Variable (Lhs) then
529 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
530 -- protected object.
532 declare
533 Ent : Entity_Id;
534 S : Entity_Id;
536 begin
537 if Ada_Version >= Ada_2005 then
539 -- Handle chains of renamings
541 Ent := Lhs;
542 while Nkind (Ent) in N_Has_Entity
543 and then Present (Entity (Ent))
544 and then Is_Object (Entity (Ent))
545 and then Present (Renamed_Object (Entity (Ent)))
546 loop
547 Ent := Renamed_Object (Entity (Ent));
548 end loop;
550 if (Nkind (Ent) = N_Attribute_Reference
551 and then Attribute_Name (Ent) = Name_Priority)
553 -- Renamings of the attribute Priority applied to protected
554 -- objects have been previously expanded into calls to the
555 -- Get_Ceiling run-time subprogram.
557 or else Is_Expanded_Priority_Attribute (Ent)
558 then
559 -- The enclosing subprogram cannot be a protected function
561 S := Current_Scope;
562 while not (Is_Subprogram (S)
563 and then Convention (S) = Convention_Protected)
564 and then S /= Standard_Standard
565 loop
566 S := Scope (S);
567 end loop;
569 if Ekind (S) = E_Function
570 and then Convention (S) = Convention_Protected
571 then
572 Error_Msg_N
573 ("protected function cannot modify its protected " &
574 "object",
575 Lhs);
576 end if;
578 -- Changes of the ceiling priority of the protected object
579 -- are only effective if the Ceiling_Locking policy is in
580 -- effect (AARM D.5.2 (5/2)).
582 if Locking_Policy /= 'C' then
583 Error_Msg_N
584 ("assignment to the attribute PRIORITY has no effect??",
585 Lhs);
586 Error_Msg_N
587 ("\since no Locking_Policy has been specified??", Lhs);
588 end if;
590 goto Leave;
591 end if;
592 end if;
593 end;
595 Diagnose_Non_Variable_Lhs (Lhs);
596 goto Leave;
598 -- Error of assigning to limited type. We do however allow this in
599 -- certain cases where the front end generates the assignments.
601 elsif Is_Limited_Type (T1)
602 and then not Assignment_OK (Lhs)
603 and then not Assignment_OK (Original_Node (Lhs))
604 then
605 -- CPP constructors can only be called in declarations
607 if Is_CPP_Constructor_Call (Rhs) then
608 Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
609 else
610 Error_Msg_N
611 ("left hand of assignment must not be limited type", Lhs);
612 Explain_Limited_Type (T1, Lhs);
613 end if;
615 goto Leave;
617 -- A class-wide type may be a limited view. This illegal case is not
618 -- caught by previous checks.
620 elsif Ekind (T1) = E_Class_Wide_Type and then From_Limited_With (T1) then
621 Error_Msg_NE ("invalid use of limited view of&", Lhs, T1);
622 goto Leave;
624 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
625 -- abstract. This is only checked when the assignment Comes_From_Source,
626 -- because in some cases the expander generates such assignments (such
627 -- in the _assign operation for an abstract type).
629 elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
630 Error_Msg_N
631 ("target of assignment operation must not be abstract", Lhs);
632 end if;
634 -- Variables which are Part_Of constituents of single protected types
635 -- behave in similar fashion to protected components. Such variables
636 -- cannot be modified by protected functions.
638 if Is_Protected_Part_Of_Constituent (Lhs) and then Within_Function then
639 Error_Msg_N
640 ("protected function cannot modify its protected object", Lhs);
641 end if;
643 -- Resolution may have updated the subtype, in case the left-hand side
644 -- is a private protected component. Use the correct subtype to avoid
645 -- scoping issues in the back-end.
647 T1 := Etype (Lhs);
649 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
650 -- type. For example:
652 -- limited with P;
653 -- package Pkg is
654 -- type Acc is access P.T;
655 -- end Pkg;
657 -- with Pkg; use Acc;
658 -- procedure Example is
659 -- A, B : Acc;
660 -- begin
661 -- A.all := B.all; -- ERROR
662 -- end Example;
664 if Nkind (Lhs) = N_Explicit_Dereference
665 and then Ekind (T1) = E_Incomplete_Type
666 then
667 Error_Msg_N ("invalid use of incomplete type", Lhs);
668 Kill_Lhs;
669 goto Leave;
670 end if;
672 -- Now we can complete the resolution of the right hand side
674 Set_Assignment_Type (Lhs, T1);
676 -- If the target of the assignment is an entity of a mutable type and
677 -- the expression is a conditional expression, its alternatives can be
678 -- of different subtypes of the nominal type of the LHS, so they must be
679 -- resolved with the base type, given that their subtype may differ from
680 -- that of the target mutable object.
682 if Is_Entity_Name (Lhs)
683 and then Is_Assignable (Entity (Lhs))
684 and then Is_Composite_Type (T1)
685 and then not Is_Constrained (Etype (Entity (Lhs)))
686 and then Nkind (Rhs) in N_If_Expression | N_Case_Expression
687 then
688 Resolve (Rhs, Base_Type (T1));
690 else
691 Resolve (Rhs, T1);
692 end if;
694 -- This is the point at which we check for an unset reference
696 Check_Unset_Reference (Rhs);
697 Check_Unprotected_Access (Lhs, Rhs);
699 -- Remaining steps are skipped if Rhs was syntactically in error
701 if Rhs = Error then
702 Kill_Lhs;
703 goto Leave;
704 end if;
706 T2 := Etype (Rhs);
708 if not Covers (T1, T2) then
709 Wrong_Type (Rhs, Etype (Lhs));
710 Kill_Lhs;
711 goto Leave;
712 end if;
714 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
715 -- types, use the non-limited view if available
717 if Nkind (Rhs) = N_Explicit_Dereference
718 and then Is_Tagged_Type (T2)
719 and then Has_Non_Limited_View (T2)
720 then
721 T2 := Non_Limited_View (T2);
722 end if;
724 Set_Assignment_Type (Rhs, T2);
726 if Total_Errors_Detected /= 0 then
727 if No (T1) then
728 T1 := Any_Type;
729 end if;
731 if No (T2) then
732 T2 := Any_Type;
733 end if;
734 end if;
736 if T1 = Any_Type or else T2 = Any_Type then
737 Kill_Lhs;
738 goto Leave;
739 end if;
741 -- If the rhs is class-wide or dynamically tagged, then require the lhs
742 -- to be class-wide. The case where the rhs is a dynamically tagged call
743 -- to a dispatching operation with a controlling access result is
744 -- excluded from this check, since the target has an access type (and
745 -- no tag propagation occurs in that case).
747 if (Is_Class_Wide_Type (T2)
748 or else (Is_Dynamically_Tagged (Rhs)
749 and then not Is_Access_Type (T1)))
750 and then not Is_Class_Wide_Type (T1)
751 then
752 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
754 elsif Is_Class_Wide_Type (T1)
755 and then not Is_Class_Wide_Type (T2)
756 and then not Is_Tag_Indeterminate (Rhs)
757 and then not Is_Dynamically_Tagged (Rhs)
758 then
759 Error_Msg_N ("dynamically tagged expression required!", Rhs);
760 end if;
762 -- Propagate the tag from a class-wide target to the rhs when the rhs
763 -- is a tag-indeterminate call.
765 if Is_Tag_Indeterminate (Rhs) then
766 if Is_Class_Wide_Type (T1) then
767 Propagate_Tag (Lhs, Rhs);
769 elsif Nkind (Rhs) = N_Function_Call
770 and then Is_Entity_Name (Name (Rhs))
771 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
772 then
773 Error_Msg_N
774 ("call to abstract function must be dispatching", Name (Rhs));
776 elsif Nkind (Rhs) = N_Qualified_Expression
777 and then Nkind (Expression (Rhs)) = N_Function_Call
778 and then Is_Entity_Name (Name (Expression (Rhs)))
779 and then
780 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
781 then
782 Error_Msg_N
783 ("call to abstract function must be dispatching",
784 Name (Expression (Rhs)));
785 end if;
786 end if;
788 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
789 -- apply an implicit conversion of the rhs to that type to force
790 -- appropriate static and run-time accessibility checks. This applies
791 -- as well to anonymous access-to-subprogram types that are component
792 -- subtypes or formal parameters.
794 if Ada_Version >= Ada_2005 and then Is_Access_Type (T1) then
795 if Is_Local_Anonymous_Access (T1)
796 or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
798 -- Handle assignment to an Ada 2012 stand-alone object
799 -- of an anonymous access type.
801 or else (Ekind (T1) = E_Anonymous_Access_Type
802 and then Nkind (Associated_Node_For_Itype (T1)) =
803 N_Object_Declaration)
805 then
806 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
807 Analyze_And_Resolve (Rhs, T1);
808 end if;
809 end if;
811 -- Ada 2005 (AI-231): Assignment to not null variable
813 if Ada_Version >= Ada_2005
814 and then Can_Never_Be_Null (T1)
815 and then not Assignment_OK (Lhs)
816 then
817 -- Case where we know the right hand side is null
819 if Known_Null (Rhs) then
820 Apply_Compile_Time_Constraint_Error
821 (N => Rhs,
822 Msg =>
823 "(Ada 2005) NULL not allowed in null-excluding objects??",
824 Reason => CE_Null_Not_Allowed);
826 -- We still mark this as a possible modification, that's necessary
827 -- to reset Is_True_Constant, and desirable for xref purposes.
829 Note_Possible_Modification (Lhs, Sure => True);
830 goto Leave;
832 -- If we know the right hand side is non-null, then we convert to the
833 -- target type, since we don't need a run time check in that case.
835 elsif not Can_Never_Be_Null (T2) then
836 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
837 Analyze_And_Resolve (Rhs, T1);
838 end if;
839 end if;
841 if Is_Scalar_Type (T1) then
842 declare
844 function Omit_Range_Check_For_Streaming return Boolean;
845 -- Return True if this assignment statement is the expansion of
846 -- a Some_Scalar_Type'Read procedure call such that all conditions
847 -- of 13.3.2(35)'s "no check is made" rule are met.
849 ------------------------------------
850 -- Omit_Range_Check_For_Streaming --
851 ------------------------------------
853 function Omit_Range_Check_For_Streaming return Boolean is
854 begin
855 -- Have we got an implicitly generated assignment to a
856 -- component of a composite object? If not, return False.
858 if Comes_From_Source (N)
859 or else Serious_Errors_Detected > 0
860 or else Nkind (Lhs)
861 not in N_Selected_Component | N_Indexed_Component
862 then
863 return False;
864 end if;
866 declare
867 Pref : constant Node_Id := Prefix (Lhs);
868 begin
869 -- Are we in the implicitly-defined Read subprogram
870 -- for a composite type, reading the value of a scalar
871 -- component from the stream? If not, return False.
873 if Nkind (Pref) /= N_Identifier
874 or else not Is_TSS (Scope (Entity (Pref)), TSS_Stream_Read)
875 then
876 return False;
877 end if;
879 -- Return False if Default_Value or Default_Component_Value
880 -- aspect applies.
882 if Has_Default_Aspect (Etype (Lhs))
883 or else Has_Default_Aspect (Etype (Pref))
884 then
885 return False;
887 -- Are we assigning to a record component (as opposed to
888 -- an array component)?
890 elsif Nkind (Lhs) = N_Selected_Component then
892 -- Are we assigning to a nondiscriminant component
893 -- that lacks a default initial value expression?
894 -- If so, return True.
896 declare
897 Comp_Id : constant Entity_Id :=
898 Original_Record_Component
899 (Entity (Selector_Name (Lhs)));
900 begin
901 if Ekind (Comp_Id) = E_Component
902 and then Nkind (Parent (Comp_Id))
903 = N_Component_Declaration
904 and then No (Expression (Parent (Comp_Id)))
905 then
906 return True;
907 end if;
908 return False;
909 end;
911 -- We are assigning to a component of an array
912 -- (and we tested for both Default_Value and
913 -- Default_Component_Value above), so return True.
915 else
916 pragma Assert (Nkind (Lhs) = N_Indexed_Component);
917 return True;
918 end if;
919 end;
920 end Omit_Range_Check_For_Streaming;
922 begin
923 if not Omit_Range_Check_For_Streaming then
924 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
925 end if;
926 end;
928 -- For array types, verify that lengths match. If the right hand side
929 -- is a function call that has been inlined, the assignment has been
930 -- rewritten as a block, and the constraint check will be applied to the
931 -- assignment within the block.
933 elsif Is_Array_Type (T1)
934 and then (Nkind (Rhs) /= N_Type_Conversion
935 or else Is_Constrained (Etype (Rhs)))
936 and then (Nkind (Rhs) /= N_Function_Call
937 or else Nkind (N) /= N_Block_Statement)
938 then
939 -- Assignment verifies that the length of the Lhs and Rhs are equal,
940 -- but of course the indexes do not have to match. If the right-hand
941 -- side is a type conversion to an unconstrained type, a length check
942 -- is performed on the expression itself during expansion. In rare
943 -- cases, the redundant length check is computed on an index type
944 -- with a different representation, triggering incorrect code in the
945 -- back end.
947 Apply_Length_Check_On_Assignment (Rhs, Etype (Lhs), Lhs);
949 else
950 -- Discriminant checks are applied in the course of expansion
952 null;
953 end if;
955 -- Note: modifications of the Lhs may only be recorded after
956 -- checks have been applied.
958 Note_Possible_Modification (Lhs, Sure => True);
960 -- ??? a real accessibility check is needed when ???
962 -- Post warning for redundant assignment or variable to itself
964 if Warn_On_Redundant_Constructs
966 -- We only warn for source constructs
968 and then Comes_From_Source (N)
970 -- Where the object is the same on both sides
972 and then Same_Object (Lhs, Rhs)
974 -- But exclude the case where the right side was an operation that
975 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
976 -- don't want to warn in such a case, since it is reasonable to write
977 -- such expressions especially when K is defined symbolically in some
978 -- other package.
980 and then Nkind (Original_Node (Rhs)) not in N_Op
981 then
982 if Nkind (Lhs) in N_Has_Entity then
983 Error_Msg_NE -- CODEFIX
984 ("?r?useless assignment of & to itself!", N, Entity (Lhs));
985 else
986 Error_Msg_N -- CODEFIX
987 ("?r?useless assignment of object to itself!", N);
988 end if;
989 end if;
991 -- Check for non-allowed composite assignment
993 if not Support_Composite_Assign_On_Target
994 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
995 and then (not Has_Size_Clause (T1)
996 or else Esize (T1) > Ttypes.System_Max_Integer_Size)
997 then
998 Error_Msg_CRT ("composite assignment", N);
999 end if;
1001 -- Check elaboration warning for left side if not in elab code
1003 if Legacy_Elaboration_Checks
1004 and not In_Subprogram_Or_Concurrent_Unit
1005 then
1006 Check_Elab_Assign (Lhs);
1007 end if;
1009 -- Save the scenario for later examination by the ABE Processing phase
1011 Record_Elaboration_Scenario (N);
1013 -- Set Referenced_As_LHS if appropriate. We are not interested in
1014 -- compiler-generated assignment statements, nor in references outside
1015 -- the extended main source unit. We check whether the Original_Node is
1016 -- in the extended main source unit because in the case of a renaming of
1017 -- a component of a packed array, the Lhs itself has a Sloc from the
1018 -- place of the renaming.
1020 if Comes_From_Source (N)
1021 and then (In_Extended_Main_Source_Unit (Lhs)
1022 or else In_Extended_Main_Source_Unit (Original_Node (Lhs)))
1023 then
1024 Set_Referenced_Modified (Lhs, Out_Param => False);
1025 end if;
1027 -- RM 7.3.2 (12/3): An assignment to a view conversion (from a type to
1028 -- one of its ancestors) requires an invariant check. Apply check only
1029 -- if expression comes from source, otherwise it will be applied when
1030 -- value is assigned to source entity. This is not done in GNATprove
1031 -- mode, as GNATprove handles invariant checks itself.
1033 if Nkind (Lhs) = N_Type_Conversion
1034 and then Has_Invariants (Etype (Expression (Lhs)))
1035 and then Comes_From_Source (Expression (Lhs))
1036 and then not GNATprove_Mode
1037 then
1038 Insert_After (N, Make_Invariant_Call (Expression (Lhs)));
1039 end if;
1041 -- Final step. If left side is an entity, then we may be able to reset
1042 -- the current tracked values to new safe values. We only have something
1043 -- to do if the left side is an entity name, and expansion has not
1044 -- modified the node into something other than an assignment, and of
1045 -- course we only capture values if it is safe to do so.
1047 if Is_Entity_Name (Lhs)
1048 and then Nkind (N) = N_Assignment_Statement
1049 then
1050 declare
1051 Ent : constant Entity_Id := Entity (Lhs);
1053 begin
1054 if Safe_To_Capture_Value (N, Ent) then
1056 -- If simple variable on left side, warn if this assignment
1057 -- blots out another one (rendering it useless). We only do
1058 -- this for source assignments, otherwise we can generate bogus
1059 -- warnings when an assignment is rewritten as another
1060 -- assignment, and gets tied up with itself.
1062 -- We also omit the warning if the RHS includes target names,
1063 -- that is to say the Ada 2022 "@" that denotes an instance of
1064 -- the LHS, which indicates that the current value is being
1065 -- used. Note that this implicit reference to the entity on
1066 -- the RHS is not treated as a source reference.
1068 -- There may have been a previous reference to a component of
1069 -- the variable, which in general removes the Last_Assignment
1070 -- field of the variable to indicate a relevant use of the
1071 -- previous assignment.
1073 if Warn_On_Modified_Unread
1074 and then Is_Assignable (Ent)
1075 and then Comes_From_Source (N)
1076 and then In_Extended_Main_Source_Unit (Ent)
1077 and then not Has_Target_Names (N)
1078 then
1079 Warn_On_Useless_Assignment (Ent, N);
1080 end if;
1082 -- If we are assigning an access type and the left side is an
1083 -- entity, then make sure that the Is_Known_[Non_]Null flags
1084 -- properly reflect the state of the entity after assignment.
1086 if Is_Access_Type (T1) then
1087 if Known_Non_Null (Rhs) then
1088 Set_Is_Known_Non_Null (Ent, True);
1090 elsif Known_Null (Rhs)
1091 and then not Can_Never_Be_Null (Ent)
1092 then
1093 Set_Is_Known_Null (Ent, True);
1095 else
1096 Set_Is_Known_Null (Ent, False);
1098 if not Can_Never_Be_Null (Ent) then
1099 Set_Is_Known_Non_Null (Ent, False);
1100 end if;
1101 end if;
1103 -- For discrete types, we may be able to set the current value
1104 -- if the value is known at compile time.
1106 elsif Is_Discrete_Type (T1)
1107 and then Compile_Time_Known_Value (Rhs)
1108 then
1109 Set_Current_Value (Ent, Rhs);
1110 else
1111 Set_Current_Value (Ent, Empty);
1112 end if;
1114 -- If not safe to capture values, kill them
1116 else
1117 Kill_Lhs;
1118 end if;
1119 end;
1120 end if;
1122 -- If assigning to an object in whole or in part, note location of
1123 -- assignment in case no one references value. We only do this for
1124 -- source assignments, otherwise we can generate bogus warnings when an
1125 -- assignment is rewritten as another assignment, and gets tied up with
1126 -- itself.
1128 declare
1129 Ent : constant Entity_Id := Get_Enclosing_Object (Lhs);
1130 begin
1131 if Present (Ent)
1132 and then Safe_To_Capture_Value (N, Ent)
1133 and then Nkind (N) = N_Assignment_Statement
1134 and then Warn_On_Modified_Unread
1135 and then Is_Assignable (Ent)
1136 and then Comes_From_Source (N)
1137 and then In_Extended_Main_Source_Unit (Ent)
1138 then
1139 Set_Last_Assignment (Ent, Lhs);
1140 end if;
1141 end;
1143 Analyze_Dimension (N);
1145 <<Leave>>
1146 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1148 -- If the right-hand side contains target names, expansion has been
1149 -- disabled to prevent expansion that might move target names out of
1150 -- the context of the assignment statement. Restore the expander mode
1151 -- now so that assignment statement can be properly expanded.
1153 if Nkind (N) = N_Assignment_Statement then
1154 if Has_Target_Names (N) then
1155 Expander_Mode_Restore;
1156 Full_Analysis := Save_Full_Analysis;
1157 Current_Assignment := Empty;
1158 end if;
1159 end if;
1160 end Analyze_Assignment;
1162 -----------------------------
1163 -- Analyze_Block_Statement --
1164 -----------------------------
1166 procedure Analyze_Block_Statement (N : Node_Id) is
1167 procedure Install_Return_Entities (Scop : Entity_Id);
1168 -- Install all entities of return statement scope Scop in the visibility
1169 -- chain except for the return object since its entity is reused in a
1170 -- renaming.
1172 -----------------------------
1173 -- Install_Return_Entities --
1174 -----------------------------
1176 procedure Install_Return_Entities (Scop : Entity_Id) is
1177 Id : Entity_Id;
1179 begin
1180 Id := First_Entity (Scop);
1181 while Present (Id) loop
1183 -- Do not install the return object
1185 if Ekind (Id) not in E_Constant | E_Variable
1186 or else not Is_Return_Object (Id)
1187 then
1188 Install_Entity (Id);
1189 end if;
1191 Next_Entity (Id);
1192 end loop;
1193 end Install_Return_Entities;
1195 -- Local constants and variables
1197 Decls : constant List_Id := Declarations (N);
1198 Id : constant Node_Id := Identifier (N);
1199 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1201 Is_BIP_Return_Statement : Boolean;
1203 -- Start of processing for Analyze_Block_Statement
1205 begin
1206 -- If no handled statement sequence is present, things are really messed
1207 -- up, and we just return immediately (defence against previous errors).
1209 if No (HSS) then
1210 Check_Error_Detected;
1211 return;
1212 end if;
1214 -- Detect whether the block is actually a rewritten return statement of
1215 -- a build-in-place function.
1217 Is_BIP_Return_Statement :=
1218 Present (Id)
1219 and then Present (Entity (Id))
1220 and then Ekind (Entity (Id)) = E_Return_Statement
1221 and then Is_Build_In_Place_Function
1222 (Return_Applies_To (Entity (Id)));
1224 -- Normal processing with HSS present
1226 declare
1227 EH : constant List_Id := Exception_Handlers (HSS);
1228 Ent : Entity_Id := Empty;
1229 S : Entity_Id;
1231 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1232 -- Recursively save value of this global, will be restored on exit
1234 begin
1235 -- Initialize unblocked exit count for statements of begin block
1236 -- plus one for each exception handler that is present.
1238 Unblocked_Exit_Count := 1 + List_Length (EH);
1240 -- If a label is present analyze it and mark it as referenced
1242 if Present (Id) then
1243 Analyze (Id);
1244 Ent := Entity (Id);
1246 -- An error defense. If we have an identifier, but no entity, then
1247 -- something is wrong. If previous errors, then just remove the
1248 -- identifier and continue, otherwise raise an exception.
1250 if No (Ent) then
1251 Check_Error_Detected;
1252 Set_Identifier (N, Empty);
1254 else
1255 if Ekind (Ent) = E_Label then
1256 Reinit_Field_To_Zero (Ent, F_Enclosing_Scope);
1257 end if;
1259 Mutate_Ekind (Ent, E_Block);
1260 Generate_Reference (Ent, N, ' ');
1261 Generate_Definition (Ent);
1263 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1264 Set_Label_Construct (Parent (Ent), N);
1265 end if;
1266 end if;
1267 end if;
1269 -- If no entity set, create a label entity
1271 if No (Ent) then
1272 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
1273 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
1274 Set_Parent (Ent, N);
1275 end if;
1277 Set_Etype (Ent, Standard_Void_Type);
1278 Set_Block_Node (Ent, Identifier (N));
1279 Push_Scope (Ent);
1281 -- The block served as an extended return statement. Ensure that any
1282 -- entities created during the analysis and expansion of the return
1283 -- object declaration are once again visible.
1285 if Is_BIP_Return_Statement then
1286 Install_Return_Entities (Ent);
1287 end if;
1289 if Present (Decls) then
1290 Analyze_Declarations (Decls);
1291 Check_Completion;
1292 Inspect_Deferred_Constant_Completion (Decls);
1293 end if;
1295 Analyze (HSS);
1296 Process_End_Label (HSS, 'e', Ent);
1298 -- If exception handlers are present, then we indicate that enclosing
1299 -- scopes contain a block with handlers. We only need to mark non-
1300 -- generic scopes.
1302 if Present (EH) then
1303 S := Scope (Ent);
1304 loop
1305 Set_Has_Nested_Block_With_Handler (S);
1306 exit when Is_Overloadable (S)
1307 or else Ekind (S) = E_Package
1308 or else Is_Generic_Unit (S);
1309 S := Scope (S);
1310 end loop;
1311 end if;
1313 Check_References (Ent);
1314 Update_Use_Clause_Chain;
1315 End_Scope;
1317 if Unblocked_Exit_Count = 0 then
1318 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1319 Check_Unreachable_Code (N);
1320 else
1321 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1322 end if;
1323 end;
1324 end Analyze_Block_Statement;
1326 --------------------------------
1327 -- Analyze_Compound_Statement --
1328 --------------------------------
1330 procedure Analyze_Compound_Statement (N : Node_Id) is
1331 begin
1332 Analyze_List (Actions (N));
1333 end Analyze_Compound_Statement;
1335 ----------------------------
1336 -- Analyze_Case_Statement --
1337 ----------------------------
1339 procedure Analyze_Case_Statement (N : Node_Id) is
1340 Exp : constant Node_Id := Expression (N);
1342 Statements_Analyzed : Boolean := False;
1343 -- Set True if at least some statement sequences get analyzed. If False
1344 -- on exit, means we had a serious error that prevented full analysis of
1345 -- the case statement, and as a result it is not a good idea to output
1346 -- warning messages about unreachable code.
1348 Is_General_Case_Statement : Boolean := False;
1349 -- Set True (later) if type of case expression is not discrete
1351 procedure Non_Static_Choice_Error (Choice : Node_Id);
1352 -- Error routine invoked by the generic instantiation below when the
1353 -- case statement has a non static choice.
1355 procedure Process_Statements (Alternative : Node_Id);
1356 -- Analyzes the statements associated with a case alternative. Needed
1357 -- by instantiation below.
1359 package Analyze_Case_Choices is new
1360 Generic_Analyze_Choices
1361 (Process_Associated_Node => Process_Statements);
1362 use Analyze_Case_Choices;
1363 -- Instantiation of the generic choice analysis package
1365 package Check_Case_Choices is new
1366 Generic_Check_Choices
1367 (Process_Empty_Choice => No_OP,
1368 Process_Non_Static_Choice => Non_Static_Choice_Error,
1369 Process_Associated_Node => No_OP);
1370 use Check_Case_Choices;
1371 -- Instantiation of the generic choice processing package
1373 -----------------------------
1374 -- Non_Static_Choice_Error --
1375 -----------------------------
1377 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1378 begin
1379 Flag_Non_Static_Expr
1380 ("choice given in case statement is not static!", Choice);
1381 end Non_Static_Choice_Error;
1383 ------------------------
1384 -- Process_Statements --
1385 ------------------------
1387 procedure Process_Statements (Alternative : Node_Id) is
1388 Choices : constant List_Id := Discrete_Choices (Alternative);
1389 Ent : Entity_Id;
1391 begin
1392 if Is_General_Case_Statement then
1393 return;
1394 -- Processing deferred in this case; decls associated with
1395 -- pattern match bindings don't exist yet.
1396 end if;
1398 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1399 Statements_Analyzed := True;
1401 -- An interesting optimization. If the case statement expression
1402 -- is a simple entity, then we can set the current value within an
1403 -- alternative if the alternative has one possible value.
1405 -- case N is
1406 -- when 1 => alpha
1407 -- when 2 | 3 => beta
1408 -- when others => gamma
1410 -- Here we know that N is initially 1 within alpha, but for beta and
1411 -- gamma, we do not know anything more about the initial value.
1413 if Is_Entity_Name (Exp) then
1414 Ent := Entity (Exp);
1416 if Is_Object (Ent) then
1417 if List_Length (Choices) = 1
1418 and then Nkind (First (Choices)) in N_Subexpr
1419 and then Compile_Time_Known_Value (First (Choices))
1420 then
1421 Set_Current_Value (Entity (Exp), First (Choices));
1422 end if;
1424 Analyze_Statements (Statements (Alternative));
1426 -- After analyzing the case, set the current value to empty
1427 -- since we won't know what it is for the next alternative
1428 -- (unless reset by this same circuit), or after the case.
1430 Set_Current_Value (Entity (Exp), Empty);
1431 return;
1432 end if;
1433 end if;
1435 -- Case where expression is not an entity name of an object
1437 Analyze_Statements (Statements (Alternative));
1438 end Process_Statements;
1440 -- Local variables
1442 Exp_Type : Entity_Id;
1443 Exp_Btype : Entity_Id;
1445 Others_Present : Boolean;
1446 -- Indicates if Others was present
1448 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1449 -- Recursively save value of this global, will be restored on exit
1451 -- Start of processing for Analyze_Case_Statement
1453 begin
1454 Analyze (Exp);
1456 -- The expression must be of any discrete type. In rare cases, the
1457 -- expander constructs a case statement whose expression has a private
1458 -- type whose full view is discrete. This can happen when generating
1459 -- a stream operation for a variant type after the type is frozen,
1460 -- when the partial of view of the type of the discriminant is private.
1461 -- In that case, use the full view to analyze case alternatives.
1463 if not Is_Overloaded (Exp)
1464 and then not Comes_From_Source (N)
1465 and then Is_Private_Type (Etype (Exp))
1466 and then Present (Full_View (Etype (Exp)))
1467 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1468 then
1469 Resolve (Exp);
1470 Exp_Type := Full_View (Etype (Exp));
1472 -- For Ada, overloading might be ok because subsequently filtering
1473 -- out non-discretes may resolve the ambiguity.
1474 -- But GNAT extensions allow casing on non-discretes.
1476 elsif Core_Extensions_Allowed and then Is_Overloaded (Exp) then
1478 -- It would be nice if we could generate all the right error
1479 -- messages by calling "Resolve (Exp, Any_Type);" in the
1480 -- same way that they are generated a few lines below by the
1481 -- call "Analyze_And_Resolve (Exp, Any_Discrete);".
1482 -- Unfortunately, Any_Type and Any_Discrete are not treated
1483 -- consistently (specifically, by Sem_Type.Covers), so that
1484 -- doesn't work.
1486 Error_Msg_N
1487 ("selecting expression of general case statement is ambiguous",
1488 Exp);
1489 return;
1491 -- Check for a GNAT-extension "general" case statement (i.e., one where
1492 -- the type of the selecting expression is not discrete).
1494 elsif Core_Extensions_Allowed
1495 and then not Is_Discrete_Type (Etype (Exp))
1496 then
1497 Resolve (Exp, Etype (Exp));
1498 Exp_Type := Etype (Exp);
1499 Is_General_Case_Statement := True;
1500 else
1501 Analyze_And_Resolve (Exp, Any_Discrete);
1502 Exp_Type := Etype (Exp);
1503 end if;
1505 Check_Unset_Reference (Exp);
1506 Exp_Btype := Base_Type (Exp_Type);
1508 -- The expression must be of a discrete type which must be determinable
1509 -- independently of the context in which the expression occurs, but
1510 -- using the fact that the expression must be of a discrete type.
1511 -- Moreover, the type this expression must not be a character literal
1512 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1514 -- If error already reported by Resolve, nothing more to do
1516 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1517 return;
1519 elsif Exp_Btype = Any_Character then
1520 Error_Msg_N
1521 ("character literal as case expression is ambiguous", Exp);
1522 return;
1524 elsif Ada_Version = Ada_83
1525 and then (Is_Generic_Type (Exp_Btype)
1526 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1527 then
1528 Error_Msg_N
1529 ("(Ada 83) case expression cannot be of a generic type", Exp);
1530 return;
1532 elsif not Core_Extensions_Allowed
1533 and then not Is_Discrete_Type (Exp_Type)
1534 then
1535 Error_Msg_N
1536 ("expression in case statement must be of a discrete_Type", Exp);
1537 return;
1538 end if;
1540 -- If the case expression is a formal object of mode in out, then treat
1541 -- it as having a nonstatic subtype by forcing use of the base type
1542 -- (which has to get passed to Check_Case_Choices below). Also use base
1543 -- type when the case expression is parenthesized.
1545 if Paren_Count (Exp) > 0
1546 or else (Is_Entity_Name (Exp)
1547 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1548 then
1549 Exp_Type := Exp_Btype;
1550 end if;
1552 -- Call instantiated procedures to analyze and check discrete choices
1554 Unblocked_Exit_Count := 0;
1556 Analyze_Choices (Alternatives (N), Exp_Type);
1557 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1559 if Is_General_Case_Statement then
1560 -- Work normally done in Process_Statements was deferred; do that
1561 -- deferred work now that Check_Choices has had a chance to create
1562 -- any needed pattern-match-binding declarations.
1563 declare
1564 Alt : Node_Id := First (Alternatives (N));
1565 begin
1566 while Present (Alt) loop
1567 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1568 Analyze_Statements (Statements (Alt));
1569 Next (Alt);
1570 end loop;
1571 end;
1572 end if;
1574 if Exp_Type = Universal_Integer and then not Others_Present then
1575 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1576 end if;
1578 -- If all our exits were blocked by unconditional transfers of control,
1579 -- then the entire CASE statement acts as an unconditional transfer of
1580 -- control, so treat it like one, and check unreachable code. Skip this
1581 -- test if we had serious errors preventing any statement analysis.
1583 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1584 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1585 Check_Unreachable_Code (N);
1586 else
1587 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1588 end if;
1590 -- If the expander is active it will detect the case of a statically
1591 -- determined single alternative and remove warnings for the case, but
1592 -- if we are not doing expansion, that circuit won't be active. Here we
1593 -- duplicate the effect of removing warnings in the same way, so that
1594 -- we will get the same set of warnings in -gnatc mode.
1596 if not Expander_Active
1597 and then Compile_Time_Known_Value (Expression (N))
1598 and then Serious_Errors_Detected = 0
1599 then
1600 declare
1601 Chosen : constant Node_Id := Find_Static_Alternative (N);
1602 Alt : Node_Id;
1604 begin
1605 Alt := First (Alternatives (N));
1606 while Present (Alt) loop
1607 if Alt /= Chosen then
1608 Remove_Warning_Messages (Statements (Alt));
1609 end if;
1611 Next (Alt);
1612 end loop;
1613 end;
1614 end if;
1615 end Analyze_Case_Statement;
1617 ----------------------------
1618 -- Analyze_Exit_Statement --
1619 ----------------------------
1621 -- If the exit includes a name, it must be the name of a currently open
1622 -- loop. Otherwise there must be an innermost open loop on the stack, to
1623 -- which the statement implicitly refers.
1625 -- Additionally, in SPARK mode:
1627 -- The exit can only name the closest enclosing loop;
1629 -- An exit with a when clause must be directly contained in a loop;
1631 -- An exit without a when clause must be directly contained in an
1632 -- if-statement with no elsif or else, which is itself directly contained
1633 -- in a loop. The exit must be the last statement in the if-statement.
1635 procedure Analyze_Exit_Statement (N : Node_Id) is
1636 Target : constant Node_Id := Name (N);
1637 Cond : constant Node_Id := Condition (N);
1638 Scope_Id : Entity_Id := Empty; -- initialize to prevent warning
1639 U_Name : Entity_Id;
1640 Kind : Entity_Kind;
1642 begin
1643 if No (Cond) then
1644 Check_Unreachable_Code (N);
1645 end if;
1647 if Present (Target) then
1648 Analyze (Target);
1649 U_Name := Entity (Target);
1651 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1652 Error_Msg_N ("invalid loop name in exit statement", N);
1653 return;
1655 else
1656 Set_Has_Exit (U_Name);
1657 end if;
1659 else
1660 U_Name := Empty;
1661 end if;
1663 for J in reverse 0 .. Scope_Stack.Last loop
1664 Scope_Id := Scope_Stack.Table (J).Entity;
1665 Kind := Ekind (Scope_Id);
1667 if Kind = E_Loop and then (No (Target) or else Scope_Id = U_Name) then
1668 Set_Has_Exit (Scope_Id);
1669 exit;
1671 elsif Kind = E_Block
1672 or else Kind = E_Loop
1673 or else Kind = E_Return_Statement
1674 then
1675 null;
1677 else
1678 Error_Msg_N
1679 ("cannot exit from program unit or accept statement", N);
1680 return;
1681 end if;
1682 end loop;
1684 -- Verify that if present the condition is a Boolean expression
1686 if Present (Cond) then
1687 Analyze_And_Resolve (Cond, Any_Boolean);
1688 Check_Unset_Reference (Cond);
1689 end if;
1691 -- Chain exit statement to associated loop entity
1693 Set_Next_Exit_Statement (N, First_Exit_Statement (Scope_Id));
1694 Set_First_Exit_Statement (Scope_Id, N);
1696 -- Since the exit may take us out of a loop, any previous assignment
1697 -- statement is not useless, so clear last assignment indications. It
1698 -- is OK to keep other current values, since if the exit statement
1699 -- does not exit, then the current values are still valid.
1701 Kill_Current_Values (Last_Assignment_Only => True);
1702 end Analyze_Exit_Statement;
1704 ----------------------------
1705 -- Analyze_Goto_Statement --
1706 ----------------------------
1708 procedure Analyze_Goto_Statement (N : Node_Id) is
1709 Label : constant Node_Id := Name (N);
1710 Scope_Id : Entity_Id;
1711 Label_Scope : Entity_Id;
1712 Label_Ent : Entity_Id;
1714 begin
1715 -- Actual semantic checks
1717 Check_Unreachable_Code (N);
1718 Kill_Current_Values (Last_Assignment_Only => True);
1720 Analyze (Label);
1721 Label_Ent := Entity (Label);
1723 -- Ignore previous error
1725 if Label_Ent = Any_Id then
1726 Check_Error_Detected;
1727 return;
1729 -- We just have a label as the target of a goto
1731 elsif Ekind (Label_Ent) /= E_Label then
1732 Error_Msg_N ("target of goto statement must be a label", Label);
1733 return;
1735 -- Check that the target of the goto is reachable according to Ada
1736 -- scoping rules. Note: the special gotos we generate for optimizing
1737 -- local handling of exceptions would violate these rules, but we mark
1738 -- such gotos as analyzed when built, so this code is never entered.
1740 elsif not Reachable (Label_Ent) then
1741 Error_Msg_N ("target of goto statement is not reachable", Label);
1742 return;
1743 end if;
1745 -- Here if goto passes initial validity checks
1747 Label_Scope := Enclosing_Scope (Label_Ent);
1749 for J in reverse 0 .. Scope_Stack.Last loop
1750 Scope_Id := Scope_Stack.Table (J).Entity;
1752 if Label_Scope = Scope_Id
1753 or else Ekind (Scope_Id) not in
1754 E_Block | E_Loop | E_Return_Statement
1755 then
1756 if Scope_Id /= Label_Scope then
1757 Error_Msg_N
1758 ("cannot exit from program unit or accept statement", N);
1759 end if;
1761 return;
1762 end if;
1763 end loop;
1765 raise Program_Error;
1766 end Analyze_Goto_Statement;
1768 ---------------------------------
1769 -- Analyze_Goto_When_Statement --
1770 ---------------------------------
1772 procedure Analyze_Goto_When_Statement (N : Node_Id) is
1773 begin
1774 -- Verify the condition is a Boolean expression
1776 Analyze_And_Resolve (Condition (N), Any_Boolean);
1777 Check_Unset_Reference (Condition (N));
1778 end Analyze_Goto_When_Statement;
1780 --------------------------
1781 -- Analyze_If_Statement --
1782 --------------------------
1784 -- A special complication arises in the analysis of if statements
1786 -- The expander has circuitry to completely delete code that it can tell
1787 -- will not be executed (as a result of compile time known conditions). In
1788 -- the analyzer, we ensure that code that will be deleted in this manner
1789 -- is analyzed but not expanded. This is obviously more efficient, but
1790 -- more significantly, difficulties arise if code is expanded and then
1791 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1792 -- generated in deleted code must be frozen from start, because the nodes
1793 -- on which they depend will not be available at the freeze point.
1795 procedure Analyze_If_Statement (N : Node_Id) is
1796 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1797 -- Recursively save value of this global, will be restored on exit
1799 Save_In_Deleted_Code : Boolean := In_Deleted_Code;
1801 Del : Boolean := False;
1802 -- This flag gets set True if a True condition has been found, which
1803 -- means that remaining ELSE/ELSIF parts are deleted.
1805 procedure Analyze_Cond_Then (Cnode : Node_Id);
1806 -- This is applied to either the N_If_Statement node itself or to an
1807 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1808 -- statements associated with it.
1810 -----------------------
1811 -- Analyze_Cond_Then --
1812 -----------------------
1814 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1815 Cond : constant Node_Id := Condition (Cnode);
1816 Tstm : constant List_Id := Then_Statements (Cnode);
1818 begin
1819 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1820 Analyze_And_Resolve (Cond, Any_Boolean);
1821 Check_Unset_Reference (Cond);
1822 Set_Current_Value_Condition (Cnode);
1824 -- If already deleting, then just analyze then statements
1826 if Del then
1827 Analyze_Statements (Tstm);
1829 -- Compile time known value, not deleting yet
1831 elsif Compile_Time_Known_Value (Cond) then
1832 Save_In_Deleted_Code := In_Deleted_Code;
1834 -- If condition is True, then analyze the THEN statements and set
1835 -- no expansion for ELSE and ELSIF parts.
1837 if Is_True (Expr_Value (Cond)) then
1838 Analyze_Statements (Tstm);
1839 Del := True;
1840 Expander_Mode_Save_And_Set (False);
1841 In_Deleted_Code := True;
1843 -- If condition is False, analyze THEN with expansion off
1845 else pragma Assert (Is_False (Expr_Value (Cond)));
1846 Expander_Mode_Save_And_Set (False);
1847 In_Deleted_Code := True;
1848 Analyze_Statements (Tstm);
1849 Expander_Mode_Restore;
1850 In_Deleted_Code := Save_In_Deleted_Code;
1851 end if;
1853 -- Not known at compile time, not deleting, normal analysis
1855 else
1856 Analyze_Statements (Tstm);
1857 end if;
1858 end Analyze_Cond_Then;
1860 -- Local variables
1862 E : Node_Id;
1863 -- For iterating over elsif parts
1865 -- Start of processing for Analyze_If_Statement
1867 begin
1868 -- Initialize exit count for else statements. If there is no else part,
1869 -- this count will stay non-zero reflecting the fact that the uncovered
1870 -- else case is an unblocked exit.
1872 Unblocked_Exit_Count := 1;
1873 Analyze_Cond_Then (N);
1875 -- Now to analyze the elsif parts if any are present
1877 E := First (Elsif_Parts (N));
1878 while Present (E) loop
1879 Analyze_Cond_Then (E);
1880 Next (E);
1881 end loop;
1883 if Present (Else_Statements (N)) then
1884 Analyze_Statements (Else_Statements (N));
1885 end if;
1887 -- If all our exits were blocked by unconditional transfers of control,
1888 -- then the entire IF statement acts as an unconditional transfer of
1889 -- control, so treat it like one, and check unreachable code.
1891 if Unblocked_Exit_Count = 0 then
1892 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1893 Check_Unreachable_Code (N);
1894 else
1895 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1896 end if;
1898 if Del then
1899 Expander_Mode_Restore;
1900 In_Deleted_Code := Save_In_Deleted_Code;
1901 end if;
1903 if not Expander_Active
1904 and then Compile_Time_Known_Value (Condition (N))
1905 and then Serious_Errors_Detected = 0
1906 then
1907 if Is_True (Expr_Value (Condition (N))) then
1908 Remove_Warning_Messages (Else_Statements (N));
1910 E := First (Elsif_Parts (N));
1911 while Present (E) loop
1912 Remove_Warning_Messages (Then_Statements (E));
1913 Next (E);
1914 end loop;
1916 else
1917 Remove_Warning_Messages (Then_Statements (N));
1918 end if;
1919 end if;
1921 -- Warn on redundant if statement that has no effect
1923 -- Note, we could also check empty ELSIF parts ???
1925 if Warn_On_Redundant_Constructs
1927 -- If statement must be from source
1929 and then Comes_From_Source (N)
1931 -- Condition must not have obvious side effect
1933 and then Has_No_Obvious_Side_Effects (Condition (N))
1935 -- No elsif parts of else part
1937 and then No (Elsif_Parts (N))
1938 and then No (Else_Statements (N))
1940 -- Then must be a single null statement
1942 and then List_Length (Then_Statements (N)) = 1
1943 then
1944 -- Go to original node, since we may have rewritten something as
1945 -- a null statement (e.g. a case we could figure the outcome of).
1947 declare
1948 T : constant Node_Id := First (Then_Statements (N));
1949 S : constant Node_Id := Original_Node (T);
1951 begin
1952 if Comes_From_Source (S) and then Nkind (S) = N_Null_Statement then
1953 Error_Msg_N ("if statement has no effect?r?", N);
1954 end if;
1955 end;
1956 end if;
1957 end Analyze_If_Statement;
1959 ----------------------------------------
1960 -- Analyze_Implicit_Label_Declaration --
1961 ----------------------------------------
1963 -- An implicit label declaration is generated in the innermost enclosing
1964 -- declarative part. This is done for labels, and block and loop names.
1966 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1967 Id : constant Node_Id := Defining_Identifier (N);
1968 begin
1969 Enter_Name (Id);
1970 Mutate_Ekind (Id, E_Label);
1971 Set_Etype (Id, Standard_Void_Type);
1972 Set_Enclosing_Scope (Id, Current_Scope);
1974 -- A label declared within a Ghost region becomes Ghost (SPARK RM
1975 -- 6.9(2)).
1977 if Ghost_Mode > None then
1978 Set_Is_Ghost_Entity (Id);
1979 end if;
1980 end Analyze_Implicit_Label_Declaration;
1982 ------------------------------
1983 -- Analyze_Iteration_Scheme --
1984 ------------------------------
1986 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1987 Cond : Node_Id;
1988 Iter_Spec : Node_Id;
1989 Loop_Spec : Node_Id;
1991 begin
1992 -- For an infinite loop, there is no iteration scheme
1994 if No (N) then
1995 return;
1996 end if;
1998 Cond := Condition (N);
1999 Iter_Spec := Iterator_Specification (N);
2000 Loop_Spec := Loop_Parameter_Specification (N);
2002 if Present (Cond) then
2003 Analyze_And_Resolve (Cond, Any_Boolean);
2004 Check_Unset_Reference (Cond);
2005 Set_Current_Value_Condition (N);
2007 elsif Present (Iter_Spec) then
2008 Analyze_Iterator_Specification (Iter_Spec);
2010 else
2011 Analyze_Loop_Parameter_Specification (Loop_Spec);
2012 end if;
2013 end Analyze_Iteration_Scheme;
2015 ------------------------------------
2016 -- Analyze_Iterator_Specification --
2017 ------------------------------------
2019 procedure Analyze_Iterator_Specification (N : Node_Id) is
2020 Def_Id : constant Node_Id := Defining_Identifier (N);
2021 Iter_Name : constant Node_Id := Name (N);
2022 Loc : constant Source_Ptr := Sloc (N);
2023 Subt : constant Node_Id := Subtype_Indication (N);
2025 Bas : Entity_Id := Empty; -- initialize to prevent warning
2026 Typ : Entity_Id;
2028 procedure Check_Reverse_Iteration (Typ : Entity_Id);
2029 -- For an iteration over a container, if the loop carries the Reverse
2030 -- indicator, verify that the container type has an Iterate aspect that
2031 -- implements the reversible iterator interface.
2033 procedure Check_Subtype_Definition (Comp_Type : Entity_Id);
2034 -- If a subtype indication is present, verify that it is consistent
2035 -- with the component type of the array or container name.
2036 -- In Ada 2022, the subtype indication may be an access definition,
2037 -- if the array or container has elements of an anonymous access type.
2039 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id;
2040 -- For containers with Iterator and related aspects, the cursor is
2041 -- obtained by locating an entity with the proper name in the scope
2042 -- of the type.
2044 -----------------------------
2045 -- Check_Reverse_Iteration --
2046 -----------------------------
2048 procedure Check_Reverse_Iteration (Typ : Entity_Id) is
2049 begin
2050 if Reverse_Present (N) then
2051 if Is_Array_Type (Typ)
2052 or else Is_Reversible_Iterator (Typ)
2053 or else
2054 (Has_Aspect (Typ, Aspect_Iterable)
2055 and then
2056 Present
2057 (Get_Iterable_Type_Primitive (Typ, Name_Previous)))
2058 then
2059 null;
2060 else
2061 Error_Msg_N
2062 ("container type does not support reverse iteration", N);
2063 end if;
2064 end if;
2065 end Check_Reverse_Iteration;
2067 -------------------------------
2068 -- Check_Subtype_Definition --
2069 -------------------------------
2071 procedure Check_Subtype_Definition (Comp_Type : Entity_Id) is
2072 begin
2073 if No (Subt) then
2074 return;
2075 end if;
2077 if Is_Anonymous_Access_Type (Entity (Subt)) then
2078 if not Is_Anonymous_Access_Type (Comp_Type) then
2079 Error_Msg_NE
2080 ("component type& is not an anonymous access",
2081 Subt, Comp_Type);
2083 elsif not Conforming_Types
2084 (Designated_Type (Entity (Subt)),
2085 Designated_Type (Comp_Type),
2086 Fully_Conformant)
2087 then
2088 Error_Msg_NE
2089 ("subtype indication does not match component type&",
2090 Subt, Comp_Type);
2091 end if;
2093 elsif not Covers (Base_Type (Bas), Comp_Type)
2094 or else not Subtypes_Statically_Match (Bas, Comp_Type)
2095 then
2096 if Is_Array_Type (Typ) then
2097 Error_Msg_NE
2098 ("subtype indication does not match component type&",
2099 Subt, Comp_Type);
2100 else
2101 Error_Msg_NE
2102 ("subtype indication does not match element type&",
2103 Subt, Comp_Type);
2104 end if;
2105 end if;
2106 end Check_Subtype_Definition;
2108 ---------------------
2109 -- Get_Cursor_Type --
2110 ---------------------
2112 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
2113 Ent : Entity_Id;
2115 begin
2116 -- If the iterator type is derived and it has an iterator interface
2117 -- type as an ancestor, then the cursor type is declared in the scope
2118 -- of that interface type.
2120 if Is_Derived_Type (Typ) then
2121 declare
2122 Iter_Iface : constant Entity_Id :=
2123 Iterator_Interface_Ancestor (Typ);
2125 begin
2126 if Present (Iter_Iface) then
2127 Ent := First_Entity (Scope (Iter_Iface));
2129 -- If there's not an iterator interface, then retrieve the
2130 -- scope associated with the parent type and start from its
2131 -- first entity.
2133 else
2134 Ent := First_Entity (Scope (Etype (Typ)));
2135 end if;
2136 end;
2138 else
2139 Ent := First_Entity (Scope (Typ));
2140 end if;
2142 while Present (Ent) loop
2143 exit when Chars (Ent) = Name_Cursor;
2144 Next_Entity (Ent);
2145 end loop;
2147 if No (Ent) then
2148 return Any_Type;
2149 end if;
2151 -- The cursor is the target of generated assignments in the
2152 -- loop, and cannot have a limited type.
2154 if Is_Limited_Type (Etype (Ent)) then
2155 Error_Msg_N ("cursor type cannot be limited", N);
2156 end if;
2158 return Etype (Ent);
2159 end Get_Cursor_Type;
2161 -- Start of processing for Analyze_Iterator_Specification
2163 begin
2164 Enter_Name (Def_Id);
2166 -- AI12-0151 specifies that when the subtype indication is present, it
2167 -- must statically match the type of the array or container element.
2168 -- To simplify this check, we introduce a subtype declaration with the
2169 -- given subtype indication when it carries a constraint, and rewrite
2170 -- the original as a reference to the created subtype entity.
2172 if Present (Subt) then
2173 if Nkind (Subt) = N_Subtype_Indication then
2174 declare
2175 S : constant Entity_Id := Make_Temporary (Sloc (Subt), 'S');
2176 Decl : constant Node_Id :=
2177 Make_Subtype_Declaration (Loc,
2178 Defining_Identifier => S,
2179 Subtype_Indication => New_Copy_Tree (Subt));
2180 begin
2181 Insert_Action (N, Decl);
2182 Analyze (Decl);
2183 Rewrite (Subt, New_Occurrence_Of (S, Sloc (Subt)));
2184 end;
2186 -- Ada 2022: the subtype definition may be for an anonymous
2187 -- access type.
2189 elsif Nkind (Subt) = N_Access_Definition then
2190 declare
2191 S : constant Entity_Id := Make_Temporary (Sloc (Subt), 'S');
2192 Decl : Node_Id;
2193 begin
2194 if Present (Subtype_Mark (Subt)) then
2195 Decl :=
2196 Make_Full_Type_Declaration (Loc,
2197 Defining_Identifier => S,
2198 Type_Definition =>
2199 Make_Access_To_Object_Definition (Loc,
2200 All_Present => True,
2201 Subtype_Indication =>
2202 New_Copy_Tree (Subtype_Mark (Subt))));
2204 else
2205 Decl :=
2206 Make_Full_Type_Declaration (Loc,
2207 Defining_Identifier => S,
2208 Type_Definition =>
2209 New_Copy_Tree
2210 (Access_To_Subprogram_Definition (Subt)));
2211 end if;
2213 Insert_Before (Parent (Parent (N)), Decl);
2214 Analyze (Decl);
2215 Freeze_Before (First (Statements (Parent (Parent (N)))), S);
2216 Rewrite (Subt, New_Occurrence_Of (S, Sloc (Subt)));
2217 end;
2218 else
2219 Analyze (Subt);
2220 end if;
2222 -- Save entity of subtype indication for subsequent check
2224 Bas := Entity (Subt);
2225 end if;
2227 Preanalyze_Range (Iter_Name);
2229 -- If the domain of iteration is a function call, make sure the function
2230 -- itself is frozen. This is an issue if this is a local expression
2231 -- function.
2233 if Nkind (Iter_Name) = N_Function_Call
2234 and then Is_Entity_Name (Name (Iter_Name))
2235 and then Full_Analysis
2236 and then (In_Assertion_Expr = 0 or else Assertions_Enabled)
2237 then
2238 Freeze_Before (N, Entity (Name (Iter_Name)));
2239 end if;
2241 -- Set the kind of the loop variable, which is not visible within the
2242 -- iterator name.
2244 Mutate_Ekind (Def_Id, E_Variable);
2245 Set_Is_Not_Self_Hidden (Def_Id);
2247 -- Provide a link between the iterator variable and the container, for
2248 -- subsequent use in cross-reference and modification information.
2250 if Of_Present (N) then
2251 Set_Related_Expression (Def_Id, Iter_Name);
2253 -- For a container, the iterator is specified through the aspect
2255 if not Is_Array_Type (Etype (Iter_Name)) then
2256 declare
2257 Iterator : constant Entity_Id :=
2258 Find_Value_Of_Aspect
2259 (Etype (Iter_Name), Aspect_Default_Iterator);
2261 I : Interp_Index;
2262 It : Interp;
2264 begin
2265 -- The domain of iteration must implement either the RM
2266 -- iterator interface, or the SPARK Iterable aspect.
2268 if No (Iterator) then
2269 if No (Find_Aspect (Etype (Iter_Name), Aspect_Iterable)) then
2270 Error_Msg_NE
2271 ("cannot iterate over&",
2272 N, Base_Type (Etype (Iter_Name)));
2273 return;
2274 end if;
2276 elsif not Is_Overloaded (Iterator) then
2277 Check_Reverse_Iteration (Etype (Iterator));
2279 -- If Iterator is overloaded, use reversible iterator if one is
2280 -- available.
2282 elsif Is_Overloaded (Iterator) then
2283 Get_First_Interp (Iterator, I, It);
2284 while Present (It.Nam) loop
2285 if Ekind (It.Nam) = E_Function
2286 and then Is_Reversible_Iterator (Etype (It.Nam))
2287 then
2288 Set_Etype (Iterator, It.Typ);
2289 Set_Entity (Iterator, It.Nam);
2290 exit;
2291 end if;
2293 Get_Next_Interp (I, It);
2294 end loop;
2296 Check_Reverse_Iteration (Etype (Iterator));
2297 end if;
2298 end;
2299 end if;
2300 end if;
2302 -- If the domain of iteration is an expression, create a declaration for
2303 -- it, so that finalization actions are introduced outside of the loop.
2304 -- The declaration must be a renaming (both in GNAT and GNATprove
2305 -- modes), because the body of the loop may assign to elements.
2307 if not Is_Entity_Name (Iter_Name)
2309 -- Do not perform this expansion in preanalysis
2311 and then Full_Analysis
2313 -- Do not perform this expansion when expansion is disabled, where the
2314 -- temporary may hide the transformation of a selected component into
2315 -- a prefixed function call, and references need to see the original
2316 -- expression.
2318 and then (Expander_Active or GNATprove_Mode)
2319 then
2320 declare
2321 Id : constant Entity_Id := Make_Temporary (Loc, 'R', Iter_Name);
2322 Decl : Node_Id;
2323 Act_S : Node_Id;
2325 begin
2327 -- If the domain of iteration is an array component that depends
2328 -- on a discriminant, create actual subtype for it. Preanalysis
2329 -- does not generate the actual subtype of a selected component.
2331 if Nkind (Iter_Name) = N_Selected_Component
2332 and then Is_Array_Type (Etype (Iter_Name))
2333 then
2334 Act_S :=
2335 Build_Actual_Subtype_Of_Component
2336 (Etype (Selector_Name (Iter_Name)), Iter_Name);
2337 Insert_Action (N, Act_S);
2339 if Present (Act_S) then
2340 Typ := Defining_Identifier (Act_S);
2341 else
2342 Typ := Etype (Iter_Name);
2343 end if;
2345 else
2346 Typ := Etype (Iter_Name);
2348 -- Verify that the expression produces an iterator
2350 if not Of_Present (N) and then not Is_Iterator (Typ)
2351 and then not Is_Array_Type (Typ)
2352 and then No (Find_Aspect (Typ, Aspect_Iterable))
2353 then
2354 Error_Msg_N
2355 ("expect object that implements iterator interface",
2356 Iter_Name);
2357 end if;
2358 end if;
2360 -- Protect against malformed iterator
2362 if Typ = Any_Type then
2363 Error_Msg_N ("invalid expression in loop iterator", Iter_Name);
2364 return;
2365 end if;
2367 if not Of_Present (N) then
2368 Check_Reverse_Iteration (Typ);
2369 end if;
2371 -- For an element iteration over a slice, we must complete
2372 -- the resolution and expansion of the slice bounds. These
2373 -- can be arbitrary expressions, and the preanalysis that
2374 -- was performed in preparation for the iteration may have
2375 -- generated an itype whose bounds must be fully expanded.
2376 -- We set the parent node to provide a proper insertion
2377 -- point for generated actions, if any.
2379 if Nkind (Iter_Name) = N_Slice
2380 and then Nkind (Discrete_Range (Iter_Name)) = N_Range
2381 and then not Analyzed (Discrete_Range (Iter_Name))
2382 then
2383 declare
2384 Indx : constant Node_Id :=
2385 Entity (First_Index (Etype (Iter_Name)));
2386 begin
2387 Set_Parent (Indx, Iter_Name);
2388 Resolve (Scalar_Range (Indx), Etype (Indx));
2389 end;
2390 end if;
2392 -- The name in the renaming declaration may be a function call.
2393 -- Indicate that it does not come from source, to suppress
2394 -- spurious warnings on renamings of parameterless functions,
2395 -- a common enough idiom in user-defined iterators.
2397 Decl :=
2398 Make_Object_Renaming_Declaration (Loc,
2399 Defining_Identifier => Id,
2400 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
2401 Name =>
2402 New_Copy_Tree (Iter_Name, New_Sloc => Loc));
2403 Set_Comes_From_Iterator (Decl);
2405 Insert_Actions (Parent (Parent (N)), New_List (Decl));
2406 Rewrite (Name (N), New_Occurrence_Of (Id, Loc));
2407 Analyze (Name (N));
2408 Set_Etype (Id, Typ);
2409 Set_Etype (Name (N), Typ);
2410 end;
2412 -- Container is an entity or an array with uncontrolled components, or
2413 -- else it is a container iterator given by a function call, typically
2414 -- called Iterate in the case of predefined containers, even though
2415 -- Iterate is not a reserved name. What matters is that the return type
2416 -- of the function is an iterator type.
2418 elsif Is_Entity_Name (Iter_Name) then
2419 Analyze (Iter_Name);
2421 if Nkind (Iter_Name) = N_Function_Call then
2422 declare
2423 C : constant Node_Id := Name (Iter_Name);
2424 I : Interp_Index;
2425 It : Interp;
2427 begin
2428 if not Is_Overloaded (Iter_Name) then
2429 Resolve (Iter_Name, Etype (C));
2431 else
2432 Get_First_Interp (C, I, It);
2433 while It.Typ /= Empty loop
2434 if Reverse_Present (N) then
2435 if Is_Reversible_Iterator (It.Typ) then
2436 Resolve (Iter_Name, It.Typ);
2437 exit;
2438 end if;
2440 elsif Is_Iterator (It.Typ) then
2441 Resolve (Iter_Name, It.Typ);
2442 exit;
2443 end if;
2445 Get_Next_Interp (I, It);
2446 end loop;
2447 end if;
2448 end;
2450 -- Domain of iteration is not overloaded
2452 else
2453 Resolve (Iter_Name);
2454 end if;
2456 if not Of_Present (N) then
2457 Check_Reverse_Iteration (Etype (Iter_Name));
2458 end if;
2459 end if;
2461 -- Get base type of container, for proper retrieval of Cursor type
2462 -- and primitive operations.
2464 Typ := Base_Type (Etype (Iter_Name));
2466 if Is_Array_Type (Typ) then
2467 if Of_Present (N) then
2468 Set_Etype (Def_Id, Component_Type (Typ));
2470 -- The loop variable is aliased if the array components are
2471 -- aliased. Likewise for the independent aspect.
2473 Set_Is_Aliased (Def_Id, Has_Aliased_Components (Typ));
2474 Set_Is_Independent (Def_Id, Has_Independent_Components (Typ));
2476 -- AI12-0047 stipulates that the domain (array or container)
2477 -- cannot be a component that depends on a discriminant if the
2478 -- enclosing object is mutable, to prevent a modification of the
2479 -- domain of iteration in the course of an iteration.
2481 -- If the object is an expression it has been captured in a
2482 -- temporary, so examine original node.
2484 if Nkind (Original_Node (Iter_Name)) = N_Selected_Component
2485 and then Is_Dependent_Component_Of_Mutable_Object
2486 (Original_Node (Iter_Name))
2487 then
2488 Error_Msg_N
2489 ("iterable name cannot be a discriminant-dependent "
2490 & "component of a mutable object", N);
2491 end if;
2493 Check_Subtype_Definition (Component_Type (Typ));
2495 -- Here we have a missing Range attribute
2497 else
2498 Error_Msg_N
2499 ("missing Range attribute in iteration over an array", N);
2501 -- In Ada 2012 mode, this may be an attempt at an iterator
2503 if Ada_Version >= Ada_2012 then
2504 Error_Msg_NE
2505 ("\if& is meant to designate an element of the array, use OF",
2506 N, Def_Id);
2507 end if;
2509 -- Prevent cascaded errors
2511 Mutate_Ekind (Def_Id, E_Loop_Parameter);
2512 Set_Etype (Def_Id, Etype (First_Index (Typ)));
2513 end if;
2515 -- Check for type error in iterator
2517 elsif Typ = Any_Type then
2518 return;
2520 -- Iteration over a container
2522 else
2523 Mutate_Ekind (Def_Id, E_Loop_Parameter);
2524 Set_Is_Not_Self_Hidden (Def_Id);
2525 Error_Msg_Ada_2012_Feature ("container iterator", Sloc (N));
2527 -- OF present
2529 if Of_Present (N) then
2530 if Has_Aspect (Typ, Aspect_Iterable) then
2531 declare
2532 Elt : constant Entity_Id :=
2533 Get_Iterable_Type_Primitive (Typ, Name_Element);
2534 begin
2535 if No (Elt) then
2536 Error_Msg_N
2537 ("missing Element primitive for iteration", N);
2538 else
2539 Set_Etype (Def_Id, Etype (Elt));
2540 Check_Reverse_Iteration (Typ);
2541 end if;
2542 end;
2544 Check_Subtype_Definition (Etype (Def_Id));
2546 -- For a predefined container, the type of the loop variable is
2547 -- the Iterator_Element aspect of the container type.
2549 else
2550 declare
2551 Element : constant Entity_Id :=
2552 Find_Value_Of_Aspect
2553 (Typ, Aspect_Iterator_Element);
2554 Iterator : constant Entity_Id :=
2555 Find_Value_Of_Aspect
2556 (Typ, Aspect_Default_Iterator);
2557 Orig_Iter_Name : constant Node_Id :=
2558 Original_Node (Iter_Name);
2559 Cursor_Type : Entity_Id;
2561 begin
2562 if No (Element) then
2563 Error_Msg_NE ("cannot iterate over&", N, Typ);
2564 return;
2566 else
2567 Set_Etype (Def_Id, Entity (Element));
2568 Cursor_Type := Get_Cursor_Type (Typ);
2569 pragma Assert (Present (Cursor_Type));
2571 Check_Subtype_Definition (Etype (Def_Id));
2573 -- If the container has a variable indexing aspect, the
2574 -- element is a variable and is modifiable in the loop.
2576 if Has_Aspect (Typ, Aspect_Variable_Indexing) then
2577 Mutate_Ekind (Def_Id, E_Variable);
2578 Set_Is_Not_Self_Hidden (Def_Id);
2579 end if;
2581 -- If the container is a constant, iterating over it
2582 -- requires a Constant_Indexing operation.
2584 if not Is_Variable (Iter_Name)
2585 and then not Has_Aspect (Typ, Aspect_Constant_Indexing)
2586 then
2587 Error_Msg_N
2588 ("iteration over constant container require "
2589 & "constant_indexing aspect", N);
2591 -- The Iterate function may have an in_out parameter,
2592 -- and a constant container is thus illegal.
2594 elsif Present (Iterator)
2595 and then Ekind (Entity (Iterator)) = E_Function
2596 and then Ekind (First_Formal (Entity (Iterator))) /=
2597 E_In_Parameter
2598 and then not Is_Variable (Iter_Name)
2599 then
2600 Error_Msg_N ("variable container expected", N);
2601 end if;
2603 -- Detect a case where the iterator denotes a component
2604 -- of a mutable object which depends on a discriminant.
2605 -- Note that the iterator may denote a function call in
2606 -- qualified form, in which case this check should not
2607 -- be performed.
2609 if Nkind (Orig_Iter_Name) = N_Selected_Component
2610 and then
2611 Present (Entity (Selector_Name (Orig_Iter_Name)))
2612 and then
2613 Ekind (Entity (Selector_Name (Orig_Iter_Name))) in
2614 E_Component | E_Discriminant
2615 and then Is_Dependent_Component_Of_Mutable_Object
2616 (Orig_Iter_Name)
2617 then
2618 Error_Msg_N
2619 ("container cannot be a discriminant-dependent "
2620 & "component of a mutable object", N);
2621 end if;
2622 end if;
2623 end;
2624 end if;
2626 -- IN iterator, domain is a range, a call to Iterate function,
2627 -- or an object/actual parameter of an iterator type.
2629 else
2630 -- If the type of the name is class-wide and its root type is a
2631 -- derived type, the primitive operations (First, Next, etc.) are
2632 -- those inherited by its specific type. Calls to these primitives
2633 -- will be dispatching.
2635 if Is_Class_Wide_Type (Typ)
2636 and then Is_Derived_Type (Etype (Typ))
2637 then
2638 Typ := Etype (Typ);
2639 end if;
2641 -- For an iteration of the form IN, the name must denote an
2642 -- iterator, typically the result of a call to Iterate. Give a
2643 -- useful error message when the name is a container by itself.
2645 -- The type may be a formal container type, which has to have
2646 -- an Iterable aspect detailing the required primitives.
2648 if Is_Entity_Name (Original_Node (Name (N)))
2649 and then not Is_Iterator (Typ)
2650 then
2651 if Has_Aspect (Typ, Aspect_Iterable) then
2652 null;
2654 elsif not Has_Aspect (Typ, Aspect_Iterator_Element) then
2655 Error_Msg_NE
2656 ("cannot iterate over&", Name (N), Typ);
2657 else
2658 Error_Msg_N
2659 ("name must be an iterator, not a container", Name (N));
2660 end if;
2662 if Has_Aspect (Typ, Aspect_Iterable) then
2663 null;
2664 else
2665 Error_Msg_NE
2666 ("\to iterate directly over the elements of a container, "
2667 & "write `of &`", Name (N), Original_Node (Name (N)));
2669 -- No point in continuing analysis of iterator spec
2671 return;
2672 end if;
2673 end if;
2675 -- If the name is a call (typically prefixed) to some Iterate
2676 -- function, it has been rewritten as an object declaration.
2677 -- If that object is a selected component, verify that it is not
2678 -- a component of an unconstrained mutable object.
2680 if Nkind (Iter_Name) = N_Identifier
2681 or else (not Expander_Active and Comes_From_Source (Iter_Name))
2682 then
2683 declare
2684 Orig_Node : constant Node_Id := Original_Node (Iter_Name);
2685 Iter_Kind : constant Node_Kind := Nkind (Orig_Node);
2686 Obj : Node_Id;
2688 begin
2689 if Iter_Kind = N_Selected_Component then
2690 Obj := Prefix (Orig_Node);
2692 elsif Iter_Kind = N_Function_Call then
2693 Obj := First_Actual (Orig_Node);
2695 -- If neither, the name comes from source
2697 else
2698 Obj := Iter_Name;
2699 end if;
2701 if Nkind (Obj) = N_Selected_Component
2702 and then Is_Dependent_Component_Of_Mutable_Object (Obj)
2703 then
2704 Error_Msg_N
2705 ("container cannot be a discriminant-dependent "
2706 & "component of a mutable object", N);
2707 end if;
2708 end;
2709 end if;
2711 -- The result type of Iterate function is the classwide type of
2712 -- the interface parent. We need the specific Cursor type defined
2713 -- in the container package. We obtain it by name for a predefined
2714 -- container, or through the Iterable aspect for a formal one.
2716 if Has_Aspect (Typ, Aspect_Iterable) then
2717 Set_Etype (Def_Id,
2718 Get_Cursor_Type
2719 (Parent (Find_Value_Of_Aspect (Typ, Aspect_Iterable)),
2720 Typ));
2722 else
2723 Set_Etype (Def_Id, Get_Cursor_Type (Typ));
2724 Check_Reverse_Iteration (Etype (Iter_Name));
2725 end if;
2727 end if;
2728 end if;
2730 -- Preanalyze the filter. Expansion will take place when enclosing
2731 -- loop is expanded.
2733 if Present (Iterator_Filter (N)) then
2734 Preanalyze_And_Resolve (Iterator_Filter (N), Standard_Boolean);
2735 end if;
2736 end Analyze_Iterator_Specification;
2738 -------------------
2739 -- Analyze_Label --
2740 -------------------
2742 -- Note: the semantic work required for analyzing labels (setting them as
2743 -- reachable) was done in a prepass through the statements in the block,
2744 -- so that forward gotos would be properly handled. See Analyze_Statements
2745 -- for further details. The only processing required here is to deal with
2746 -- optimizations that depend on an assumption of sequential control flow,
2747 -- since of course the occurrence of a label breaks this assumption.
2749 procedure Analyze_Label (N : Node_Id) is
2750 pragma Warnings (Off, N);
2751 begin
2752 Kill_Current_Values;
2753 end Analyze_Label;
2755 ------------------------------------------
2756 -- Analyze_Loop_Parameter_Specification --
2757 ------------------------------------------
2759 procedure Analyze_Loop_Parameter_Specification (N : Node_Id) is
2760 Loop_Nod : constant Node_Id := Parent (Parent (N));
2762 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
2763 -- If the bounds are given by a 'Range reference on a function call
2764 -- that returns a controlled array, introduce an explicit declaration
2765 -- to capture the bounds, so that the function result can be finalized
2766 -- in timely fashion.
2768 procedure Check_Predicate_Use (T : Entity_Id);
2769 -- Diagnose Attempt to iterate through non-static predicate. Note that
2770 -- a type with inherited predicates may have both static and dynamic
2771 -- forms. In this case it is not sufficient to check the static
2772 -- predicate function only, look for a dynamic predicate aspect as well.
2774 procedure Process_Bounds (R : Node_Id);
2775 -- If the iteration is given by a range, create temporaries and
2776 -- assignment statements block to capture the bounds and perform
2777 -- required finalization actions in case a bound includes a function
2778 -- call that uses the temporary stack. We first preanalyze a copy of
2779 -- the range in order to determine the expected type, and analyze and
2780 -- resolve the original bounds.
2782 --------------------------------------
2783 -- Check_Controlled_Array_Attribute --
2784 --------------------------------------
2786 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
2787 begin
2788 if Nkind (DS) = N_Attribute_Reference
2789 and then Is_Entity_Name (Prefix (DS))
2790 and then Ekind (Entity (Prefix (DS))) = E_Function
2791 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
2792 and then
2793 Is_Controlled (Component_Type (Etype (Entity (Prefix (DS)))))
2794 and then Expander_Active
2795 then
2796 declare
2797 Loc : constant Source_Ptr := Sloc (N);
2798 Arr : constant Entity_Id := Etype (Entity (Prefix (DS)));
2799 Indx : constant Entity_Id :=
2800 Base_Type (Etype (First_Index (Arr)));
2801 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
2802 Decl : Node_Id;
2804 begin
2805 Decl :=
2806 Make_Subtype_Declaration (Loc,
2807 Defining_Identifier => Subt,
2808 Subtype_Indication =>
2809 Make_Subtype_Indication (Loc,
2810 Subtype_Mark => New_Occurrence_Of (Indx, Loc),
2811 Constraint =>
2812 Make_Range_Constraint (Loc, Relocate_Node (DS))));
2813 Insert_Before (Loop_Nod, Decl);
2814 Analyze (Decl);
2816 Rewrite (DS,
2817 Make_Attribute_Reference (Loc,
2818 Prefix => New_Occurrence_Of (Subt, Loc),
2819 Attribute_Name => Attribute_Name (DS)));
2821 Analyze (DS);
2822 end;
2823 end if;
2824 end Check_Controlled_Array_Attribute;
2826 -------------------------
2827 -- Check_Predicate_Use --
2828 -------------------------
2830 procedure Check_Predicate_Use (T : Entity_Id) is
2831 begin
2832 -- A predicated subtype is illegal in loops and related constructs
2833 -- if the predicate is not static, or if it is a non-static subtype
2834 -- of a statically predicated subtype.
2836 if Is_Discrete_Type (T)
2837 and then Has_Predicates (T)
2838 and then (not Has_Static_Predicate (T)
2839 or else not Is_Static_Subtype (T)
2840 or else Has_Dynamic_Predicate_Aspect (T)
2841 or else Has_Ghost_Predicate_Aspect (T))
2842 then
2843 -- Seems a confusing message for the case of a static predicate
2844 -- with a non-static subtype???
2846 Bad_Predicated_Subtype_Use
2847 ("cannot use subtype& with non-static predicate for loop "
2848 & "iteration", Discrete_Subtype_Definition (N),
2849 T, Suggest_Static => True);
2851 elsif Inside_A_Generic
2852 and then Is_Generic_Formal (T)
2853 and then Is_Discrete_Type (T)
2854 then
2855 Set_No_Dynamic_Predicate_On_Actual (T);
2856 end if;
2857 end Check_Predicate_Use;
2859 --------------------
2860 -- Process_Bounds --
2861 --------------------
2863 procedure Process_Bounds (R : Node_Id) is
2864 Loc : constant Source_Ptr := Sloc (N);
2866 function One_Bound
2867 (Original_Bound : Node_Id;
2868 Analyzed_Bound : Node_Id;
2869 Typ : Entity_Id) return Node_Id;
2870 -- Capture value of bound and return captured value
2872 ---------------
2873 -- One_Bound --
2874 ---------------
2876 function One_Bound
2877 (Original_Bound : Node_Id;
2878 Analyzed_Bound : Node_Id;
2879 Typ : Entity_Id) return Node_Id
2881 Assign : Node_Id;
2882 Decl : Node_Id;
2883 Id : Entity_Id;
2885 begin
2886 -- If the bound is a constant or an object, no need for a separate
2887 -- declaration. If the bound is the result of previous expansion
2888 -- it is already analyzed and should not be modified. Note that
2889 -- the Bound will be resolved later, if needed, as part of the
2890 -- call to Make_Index (literal bounds may need to be resolved to
2891 -- type Integer).
2893 if Analyzed (Original_Bound) then
2894 return Original_Bound;
2896 elsif Nkind (Analyzed_Bound) in
2897 N_Integer_Literal | N_Character_Literal
2898 or else Is_Entity_Name (Analyzed_Bound)
2899 then
2900 Analyze_And_Resolve (Original_Bound, Typ);
2901 return Original_Bound;
2903 elsif Inside_Class_Condition_Preanalysis then
2904 Analyze_And_Resolve (Original_Bound, Typ);
2905 return Original_Bound;
2906 end if;
2908 -- Normally, the best approach is simply to generate a constant
2909 -- declaration that captures the bound. However, there is a nasty
2910 -- case where this is wrong. If the bound is complex, and has a
2911 -- possible use of the secondary stack, we need to generate a
2912 -- separate assignment statement to ensure the creation of a block
2913 -- which will release the secondary stack.
2915 -- We prefer the constant declaration, since it leaves us with a
2916 -- proper trace of the value, useful in optimizations that get rid
2917 -- of junk range checks.
2919 if not Has_Sec_Stack_Call (Analyzed_Bound) then
2920 Analyze_And_Resolve (Original_Bound, Typ);
2922 -- Ensure that the bound is valid. This check should not be
2923 -- generated when the range belongs to a quantified expression
2924 -- as the construct is still not expanded into its final form.
2926 if Nkind (Parent (R)) /= N_Loop_Parameter_Specification
2927 or else Nkind (Parent (Parent (R))) /= N_Quantified_Expression
2928 then
2929 Ensure_Valid (Original_Bound);
2930 end if;
2932 Force_Evaluation (Original_Bound);
2933 return Original_Bound;
2934 end if;
2936 Id := Make_Temporary (Loc, 'R', Original_Bound);
2938 -- Here we make a declaration with a separate assignment
2939 -- statement, and insert before loop header.
2941 Decl :=
2942 Make_Object_Declaration (Loc,
2943 Defining_Identifier => Id,
2944 Object_Definition => New_Occurrence_Of (Typ, Loc));
2946 Assign :=
2947 Make_Assignment_Statement (Loc,
2948 Name => New_Occurrence_Of (Id, Loc),
2949 Expression => Relocate_Node (Original_Bound));
2951 Insert_Actions (Loop_Nod, New_List (Decl, Assign));
2953 -- Now that this temporary variable is initialized we decorate it
2954 -- as safe-to-reevaluate to inform to the backend that no further
2955 -- asignment will be issued and hence it can be handled as side
2956 -- effect free. Note that this decoration must be done when the
2957 -- assignment has been analyzed because otherwise it will be
2958 -- rejected (see Analyze_Assignment).
2960 Set_Is_Safe_To_Reevaluate (Id);
2962 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
2964 if Nkind (Assign) = N_Assignment_Statement then
2965 return Expression (Assign);
2966 else
2967 return Original_Bound;
2968 end if;
2969 end One_Bound;
2971 Hi : constant Node_Id := High_Bound (R);
2972 Lo : constant Node_Id := Low_Bound (R);
2973 R_Copy : constant Node_Id := New_Copy_Tree (R);
2974 New_Hi : Node_Id;
2975 New_Lo : Node_Id;
2976 Typ : Entity_Id;
2978 -- Start of processing for Process_Bounds
2980 begin
2981 Set_Parent (R_Copy, Parent (R));
2982 Preanalyze_Range (R_Copy);
2983 Typ := Etype (R_Copy);
2985 -- If the type of the discrete range is Universal_Integer, then the
2986 -- bound's type must be resolved to Integer, and any object used to
2987 -- hold the bound must also have type Integer, unless the literal
2988 -- bounds are constant-folded expressions with a user-defined type.
2990 if Typ = Universal_Integer then
2991 if Nkind (Lo) = N_Integer_Literal
2992 and then Present (Etype (Lo))
2993 and then Scope (Etype (Lo)) /= Standard_Standard
2994 then
2995 Typ := Etype (Lo);
2997 elsif Nkind (Hi) = N_Integer_Literal
2998 and then Present (Etype (Hi))
2999 and then Scope (Etype (Hi)) /= Standard_Standard
3000 then
3001 Typ := Etype (Hi);
3003 else
3004 Typ := Standard_Integer;
3005 end if;
3006 end if;
3008 Set_Etype (R, Typ);
3010 New_Lo := One_Bound (Lo, Low_Bound (R_Copy), Typ);
3011 New_Hi := One_Bound (Hi, High_Bound (R_Copy), Typ);
3013 -- Propagate staticness to loop range itself, in case the
3014 -- corresponding subtype is static.
3016 if New_Lo /= Lo and then Is_OK_Static_Expression (New_Lo) then
3017 Rewrite (Low_Bound (R), New_Copy (New_Lo));
3018 end if;
3020 if New_Hi /= Hi and then Is_OK_Static_Expression (New_Hi) then
3021 Rewrite (High_Bound (R), New_Copy (New_Hi));
3022 end if;
3023 end Process_Bounds;
3025 -- Local variables
3027 DS : constant Node_Id := Discrete_Subtype_Definition (N);
3028 Id : constant Entity_Id := Defining_Identifier (N);
3030 DS_Copy : Node_Id;
3032 -- Start of processing for Analyze_Loop_Parameter_Specification
3034 begin
3035 Enter_Name (Id);
3037 -- We always consider the loop variable to be referenced, since the loop
3038 -- may be used just for counting purposes.
3040 Generate_Reference (Id, N, ' ');
3042 -- Check for the case of loop variable hiding a local variable (used
3043 -- later on to give a nice warning if the hidden variable is never
3044 -- assigned).
3046 declare
3047 H : constant Entity_Id := Homonym (Id);
3048 begin
3049 if Present (H)
3050 and then Ekind (H) = E_Variable
3051 and then Is_Discrete_Type (Etype (H))
3052 and then Enclosing_Dynamic_Scope (H) = Enclosing_Dynamic_Scope (Id)
3053 then
3054 Set_Hiding_Loop_Variable (H, Id);
3055 end if;
3056 end;
3058 -- Analyze the subtype definition and create temporaries for the bounds.
3059 -- Do not evaluate the range when preanalyzing a quantified expression
3060 -- because bounds expressed as function calls with side effects will be
3061 -- incorrectly replicated.
3063 if Nkind (DS) = N_Range
3064 and then Expander_Active
3065 and then Nkind (Parent (N)) /= N_Quantified_Expression
3066 then
3067 Process_Bounds (DS);
3069 -- Either the expander not active or the range of iteration is a subtype
3070 -- indication, an entity, or a function call that yields an aggregate or
3071 -- a container.
3073 else
3074 DS_Copy := New_Copy_Tree (DS);
3075 Set_Parent (DS_Copy, Parent (DS));
3076 Preanalyze_Range (DS_Copy);
3078 -- Ada 2012: If the domain of iteration is:
3080 -- a) a function call,
3081 -- b) an identifier that is not a type,
3082 -- c) an attribute reference 'Old (within a postcondition),
3083 -- d) an unchecked conversion or a qualified expression with
3084 -- the proper iterator type.
3086 -- then it is an iteration over a container. It was classified as
3087 -- a loop specification by the parser, and must be rewritten now
3088 -- to activate container iteration. The last case will occur within
3089 -- an expanded inlined call, where the expansion wraps an actual in
3090 -- an unchecked conversion when needed. The expression of the
3091 -- conversion is always an object.
3093 if Nkind (DS_Copy) = N_Function_Call
3095 or else (Is_Entity_Name (DS_Copy)
3096 and then not Is_Type (Entity (DS_Copy)))
3098 or else (Nkind (DS_Copy) = N_Attribute_Reference
3099 and then Attribute_Name (DS_Copy) in
3100 Name_Loop_Entry | Name_Old)
3102 or else Has_Aspect (Etype (DS_Copy), Aspect_Iterable)
3104 or else Nkind (DS_Copy) = N_Unchecked_Type_Conversion
3105 or else (Nkind (DS_Copy) = N_Qualified_Expression
3106 and then Is_Iterator (Etype (DS_Copy)))
3107 then
3108 -- This is an iterator specification. Rewrite it as such and
3109 -- analyze it to capture function calls that may require
3110 -- finalization actions.
3112 declare
3113 I_Spec : constant Node_Id :=
3114 Make_Iterator_Specification (Sloc (N),
3115 Defining_Identifier => Relocate_Node (Id),
3116 Name => DS_Copy,
3117 Subtype_Indication => Empty,
3118 Reverse_Present => Reverse_Present (N));
3119 Scheme : constant Node_Id := Parent (N);
3121 begin
3122 Set_Iterator_Specification (Scheme, I_Spec);
3123 Set_Loop_Parameter_Specification (Scheme, Empty);
3124 Set_Iterator_Filter (I_Spec,
3125 Relocate_Node (Iterator_Filter (N)));
3127 Analyze_Iterator_Specification (I_Spec);
3129 -- In a generic context, analyze the original domain of
3130 -- iteration, for name capture.
3132 if not Expander_Active then
3133 Analyze (DS);
3134 end if;
3136 -- Set kind of loop parameter, which may be used in the
3137 -- subsequent analysis of the condition in a quantified
3138 -- expression.
3140 Mutate_Ekind (Id, E_Loop_Parameter);
3141 return;
3142 end;
3144 -- Domain of iteration is not a function call, and is side-effect
3145 -- free.
3147 else
3148 -- A quantified expression that appears in a pre/post condition
3149 -- is preanalyzed several times. If the range is given by an
3150 -- attribute reference it is rewritten as a range, and this is
3151 -- done even with expansion disabled. If the type is already set
3152 -- do not reanalyze, because a range with static bounds may be
3153 -- typed Integer by default.
3155 if Nkind (Parent (N)) = N_Quantified_Expression
3156 and then Present (Etype (DS))
3157 then
3158 null;
3159 else
3160 Analyze (DS);
3161 end if;
3162 end if;
3163 end if;
3165 if DS = Error then
3166 return;
3167 end if;
3169 -- Some additional checks if we are iterating through a type
3171 if Is_Entity_Name (DS)
3172 and then Present (Entity (DS))
3173 and then Is_Type (Entity (DS))
3174 then
3175 -- The subtype indication may denote the completion of an incomplete
3176 -- type declaration.
3178 if Ekind (Entity (DS)) = E_Incomplete_Type then
3179 Set_Entity (DS, Get_Full_View (Entity (DS)));
3180 Set_Etype (DS, Entity (DS));
3181 end if;
3183 Check_Predicate_Use (Entity (DS));
3184 end if;
3186 -- Error if not discrete type
3188 if not Is_Discrete_Type (Etype (DS)) then
3189 Wrong_Type (DS, Any_Discrete);
3190 Set_Etype (DS, Any_Type);
3191 end if;
3193 Check_Controlled_Array_Attribute (DS);
3195 if Nkind (DS) = N_Subtype_Indication then
3196 Check_Predicate_Use (Entity (Subtype_Mark (DS)));
3197 end if;
3199 if Nkind (DS) not in N_Raise_xxx_Error then
3200 Make_Index (DS, N);
3201 end if;
3203 Mutate_Ekind (Id, E_Loop_Parameter);
3204 Set_Is_Not_Self_Hidden (Id);
3206 -- A quantified expression which appears in a pre- or post-condition may
3207 -- be analyzed multiple times. The analysis of the range creates several
3208 -- itypes which reside in different scopes depending on whether the pre-
3209 -- or post-condition has been expanded. Update the type of the loop
3210 -- variable to reflect the proper itype at each stage of analysis.
3212 -- Loop_Nod might not be present when we are preanalyzing a class-wide
3213 -- pre/postcondition since preanalysis occurs in a place unrelated to
3214 -- the actual code and the quantified expression may be the outermost
3215 -- expression of the class-wide condition.
3217 if No (Etype (Id))
3218 or else Etype (Id) = Any_Type
3219 or else
3220 (Present (Etype (Id))
3221 and then Is_Itype (Etype (Id))
3222 and then Present (Loop_Nod)
3223 and then Nkind (Parent (Loop_Nod)) = N_Expression_With_Actions
3224 and then Nkind (Original_Node (Parent (Loop_Nod))) =
3225 N_Quantified_Expression)
3226 then
3227 Set_Etype (Id, Etype (DS));
3228 end if;
3230 -- Treat a range as an implicit reference to the type, to inhibit
3231 -- spurious warnings.
3233 Generate_Reference (Base_Type (Etype (DS)), N, ' ');
3234 Set_Is_Known_Valid (Id, True);
3236 -- The loop is not a declarative part, so the loop variable must be
3237 -- frozen explicitly. Do not freeze while preanalyzing a quantified
3238 -- expression because the freeze node will not be inserted into the
3239 -- tree due to flag Is_Spec_Expression being set.
3241 if Nkind (Parent (N)) /= N_Quantified_Expression then
3242 declare
3243 Flist : constant List_Id := Freeze_Entity (Id, N);
3244 begin
3245 Insert_Actions (N, Flist);
3246 end;
3247 end if;
3249 -- Case where we have a range or a subtype, get type bounds
3251 if Nkind (DS) in N_Range | N_Subtype_Indication
3252 and then not Error_Posted (DS)
3253 and then Etype (DS) /= Any_Type
3254 and then Is_Discrete_Type (Etype (DS))
3255 then
3256 declare
3257 L : Node_Id;
3258 H : Node_Id;
3259 Null_Range : Boolean := False;
3261 begin
3262 if Nkind (DS) = N_Range then
3263 L := Low_Bound (DS);
3264 H := High_Bound (DS);
3265 else
3266 L :=
3267 Type_Low_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
3268 H :=
3269 Type_High_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
3270 end if;
3272 -- Check for null or possibly null range and issue warning. We
3273 -- suppress such messages in generic templates and instances,
3274 -- because in practice they tend to be dubious in these cases. The
3275 -- check applies as well to rewritten array element loops where a
3276 -- null range may be detected statically.
3278 if Compile_Time_Compare (L, H, Assume_Valid => True) = GT then
3279 if Compile_Time_Compare (L, H, Assume_Valid => False) = GT then
3280 -- Since we know the range of the loop is always null,
3281 -- set the appropriate flag to remove the loop entirely
3282 -- during expansion.
3284 Set_Is_Null_Loop (Loop_Nod);
3285 Null_Range := True;
3286 end if;
3288 -- Suppress the warning if inside a generic template or
3289 -- instance, since in practice they tend to be dubious in these
3290 -- cases since they can result from intended parameterization.
3292 if not Inside_A_Generic and then not In_Instance then
3294 -- Specialize msg if invalid values could make the loop
3295 -- non-null after all.
3297 if Null_Range then
3298 if Comes_From_Source (N) then
3299 Error_Msg_N
3300 ("??loop range is null, loop will not execute", DS);
3301 end if;
3303 -- Here is where the loop could execute because of
3304 -- invalid values, so issue appropriate message.
3306 elsif Comes_From_Source (N) then
3307 Error_Msg_N
3308 ("??loop range may be null, loop may not execute",
3309 DS);
3310 Error_Msg_N
3311 ("??can only execute if invalid values are present",
3312 DS);
3313 end if;
3314 end if;
3316 -- In either case, suppress warnings in the body of the loop,
3317 -- since it is likely that these warnings will be inappropriate
3318 -- if the loop never actually executes, which is likely.
3320 Set_Suppress_Loop_Warnings (Loop_Nod);
3322 -- The other case for a warning is a reverse loop where the
3323 -- upper bound is the integer literal zero or one, and the
3324 -- lower bound may exceed this value.
3326 -- For example, we have
3328 -- for J in reverse N .. 1 loop
3330 -- In practice, this is very likely to be a case of reversing
3331 -- the bounds incorrectly in the range.
3333 elsif Reverse_Present (N)
3334 and then Nkind (Original_Node (H)) = N_Integer_Literal
3335 and then
3336 (Intval (Original_Node (H)) = Uint_0
3337 or else
3338 Intval (Original_Node (H)) = Uint_1)
3339 then
3340 -- Lower bound may in fact be known and known not to exceed
3341 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
3343 if Compile_Time_Known_Value (L)
3344 and then Expr_Value (L) <= Expr_Value (H)
3345 then
3346 null;
3348 -- Otherwise warning is warranted
3350 else
3351 Error_Msg_N ("??loop range may be null", DS);
3352 Error_Msg_N ("\??bounds may be wrong way round", DS);
3353 end if;
3354 end if;
3356 -- Check if either bound is known to be outside the range of the
3357 -- loop parameter type, this is e.g. the case of a loop from
3358 -- 20..X where the type is 1..19.
3360 -- Such a loop is dubious since either it raises CE or it executes
3361 -- zero times, and that cannot be useful!
3363 if Etype (DS) /= Any_Type
3364 and then not Error_Posted (DS)
3365 and then Nkind (DS) = N_Subtype_Indication
3366 and then Nkind (Constraint (DS)) = N_Range_Constraint
3367 then
3368 declare
3369 LLo : constant Node_Id :=
3370 Low_Bound (Range_Expression (Constraint (DS)));
3371 LHi : constant Node_Id :=
3372 High_Bound (Range_Expression (Constraint (DS)));
3374 Bad_Bound : Node_Id := Empty;
3375 -- Suspicious loop bound
3377 begin
3378 -- At this stage L, H are the bounds of the type, and LLo
3379 -- Lhi are the low bound and high bound of the loop.
3381 if Compile_Time_Compare (LLo, L, Assume_Valid => True) = LT
3382 or else
3383 Compile_Time_Compare (LLo, H, Assume_Valid => True) = GT
3384 then
3385 Bad_Bound := LLo;
3386 end if;
3388 if Compile_Time_Compare (LHi, L, Assume_Valid => True) = LT
3389 or else
3390 Compile_Time_Compare (LHi, H, Assume_Valid => True) = GT
3391 then
3392 Bad_Bound := LHi;
3393 end if;
3395 if Present (Bad_Bound) then
3396 Error_Msg_N
3397 ("suspicious loop bound out of range of "
3398 & "loop subtype??", Bad_Bound);
3399 Error_Msg_N
3400 ("\loop executes zero times or raises "
3401 & "Constraint_Error??", Bad_Bound);
3402 end if;
3404 if Compile_Time_Compare (LLo, LHi, Assume_Valid => False)
3405 = GT
3406 then
3407 Error_Msg_N ("??constrained range is null",
3408 Constraint (DS));
3410 -- Additional constraints on modular types can be
3411 -- confusing, add more information.
3413 if Ekind (Etype (DS)) = E_Modular_Integer_Subtype then
3414 Error_Msg_Uint_1 := Intval (LLo);
3415 Error_Msg_Uint_2 := Intval (LHi);
3416 Error_Msg_NE ("\iterator has modular type &, " &
3417 "so the loop has bounds ^ ..^",
3418 Constraint (DS),
3419 Subtype_Mark (DS));
3420 end if;
3422 Set_Is_Null_Loop (Loop_Nod);
3423 Null_Range := True;
3425 -- Suppress other warnings about the body of the loop, as
3426 -- it will never execute.
3427 Set_Suppress_Loop_Warnings (Loop_Nod);
3428 end if;
3429 end;
3430 end if;
3432 -- This declare block is about warnings, if we get an exception while
3433 -- testing for warnings, we simply abandon the attempt silently. This
3434 -- most likely occurs as the result of a previous error, but might
3435 -- just be an obscure case we have missed. In either case, not giving
3436 -- the warning is perfectly acceptable.
3438 exception
3439 when others =>
3440 -- With debug flag K we will get an exception unless an error
3441 -- has already occurred (useful for debugging).
3443 if Debug_Flag_K then
3444 Check_Error_Detected;
3445 end if;
3446 end;
3447 end if;
3449 -- Preanalyze the filter. Expansion will take place when enclosing
3450 -- loop is expanded.
3452 if Present (Iterator_Filter (N)) then
3453 Preanalyze_And_Resolve (Iterator_Filter (N), Standard_Boolean);
3454 end if;
3455 end Analyze_Loop_Parameter_Specification;
3457 ----------------------------
3458 -- Analyze_Loop_Statement --
3459 ----------------------------
3461 procedure Analyze_Loop_Statement (N : Node_Id) is
3463 -- The following exception is raised by routine Prepare_Loop_Statement
3464 -- to avoid further analysis of a transformed loop.
3466 procedure Prepare_Loop_Statement
3467 (Iter : Node_Id;
3468 Stop_Processing : out Boolean);
3469 -- Determine whether loop statement N with iteration scheme Iter must be
3470 -- transformed prior to analysis, and if so, perform it.
3471 -- If Stop_Processing is set to True, should stop further processing.
3473 ----------------------------
3474 -- Prepare_Loop_Statement --
3475 ----------------------------
3477 procedure Prepare_Loop_Statement
3478 (Iter : Node_Id;
3479 Stop_Processing : out Boolean)
3481 function Has_Sec_Stack_Default_Iterator
3482 (Cont_Typ : Entity_Id) return Boolean;
3483 pragma Inline (Has_Sec_Stack_Default_Iterator);
3484 -- Determine whether container type Cont_Typ has a default iterator
3485 -- that requires secondary stack management.
3487 function Is_Sec_Stack_Iteration_Primitive
3488 (Cont_Typ : Entity_Id;
3489 Iter_Prim_Nam : Name_Id) return Boolean;
3490 pragma Inline (Is_Sec_Stack_Iteration_Primitive);
3491 -- Determine whether container type Cont_Typ has an iteration routine
3492 -- described by its name Iter_Prim_Nam that requires secondary stack
3493 -- management.
3495 function Is_Wrapped_In_Block (Stmt : Node_Id) return Boolean;
3496 pragma Inline (Is_Wrapped_In_Block);
3497 -- Determine whether arbitrary statement Stmt is the sole statement
3498 -- wrapped within some block, excluding pragmas.
3500 procedure Prepare_Iterator_Loop
3501 (Iter_Spec : Node_Id;
3502 Stop_Processing : out Boolean);
3503 pragma Inline (Prepare_Iterator_Loop);
3504 -- Prepare an iterator loop with iteration specification Iter_Spec
3505 -- for transformation if needed.
3506 -- If Stop_Processing is set to True, should stop further processing.
3508 procedure Prepare_Param_Spec_Loop
3509 (Param_Spec : Node_Id;
3510 Stop_Processing : out Boolean);
3511 pragma Inline (Prepare_Param_Spec_Loop);
3512 -- Prepare a discrete loop with parameter specification Param_Spec
3513 -- for transformation if needed.
3514 -- If Stop_Processing is set to True, should stop further processing.
3516 procedure Wrap_Loop_Statement (Manage_Sec_Stack : Boolean);
3517 pragma Inline (Wrap_Loop_Statement);
3518 -- Wrap loop statement N within a block. Flag Manage_Sec_Stack must
3519 -- be set when the block must mark and release the secondary stack.
3520 -- Should stop further processing after calling this procedure.
3522 ------------------------------------
3523 -- Has_Sec_Stack_Default_Iterator --
3524 ------------------------------------
3526 function Has_Sec_Stack_Default_Iterator
3527 (Cont_Typ : Entity_Id) return Boolean
3529 Def_Iter : constant Node_Id :=
3530 Find_Value_Of_Aspect
3531 (Cont_Typ, Aspect_Default_Iterator);
3532 begin
3533 return
3534 Present (Def_Iter)
3535 and then Present (Etype (Def_Iter))
3536 and then Requires_Transient_Scope (Etype (Def_Iter));
3537 end Has_Sec_Stack_Default_Iterator;
3539 --------------------------------------
3540 -- Is_Sec_Stack_Iteration_Primitive --
3541 --------------------------------------
3543 function Is_Sec_Stack_Iteration_Primitive
3544 (Cont_Typ : Entity_Id;
3545 Iter_Prim_Nam : Name_Id) return Boolean
3547 Iter_Prim : constant Entity_Id :=
3548 Get_Iterable_Type_Primitive
3549 (Cont_Typ, Iter_Prim_Nam);
3550 begin
3551 return
3552 Present (Iter_Prim)
3553 and then Requires_Transient_Scope (Etype (Iter_Prim));
3554 end Is_Sec_Stack_Iteration_Primitive;
3556 -------------------------
3557 -- Is_Wrapped_In_Block --
3558 -------------------------
3560 function Is_Wrapped_In_Block (Stmt : Node_Id) return Boolean is
3561 Blk_HSS : Node_Id;
3562 Blk_Id : Entity_Id;
3563 Blk_Stmt : Node_Id;
3565 begin
3566 Blk_Id := Current_Scope;
3568 -- The current context is a block. Inspect the statements of the
3569 -- block to determine whether it wraps Stmt.
3571 if Ekind (Blk_Id) = E_Block
3572 and then Present (Block_Node (Blk_Id))
3573 then
3574 Blk_HSS :=
3575 Handled_Statement_Sequence (Parent (Block_Node (Blk_Id)));
3577 -- Skip leading pragmas introduced for invariant and predicate
3578 -- checks.
3580 Blk_Stmt := First (Statements (Blk_HSS));
3581 while Present (Blk_Stmt)
3582 and then Nkind (Blk_Stmt) = N_Pragma
3583 loop
3584 Next (Blk_Stmt);
3585 end loop;
3587 return Blk_Stmt = Stmt and then No (Next (Blk_Stmt));
3588 end if;
3590 return False;
3591 end Is_Wrapped_In_Block;
3593 ---------------------------
3594 -- Prepare_Iterator_Loop --
3595 ---------------------------
3597 procedure Prepare_Iterator_Loop
3598 (Iter_Spec : Node_Id;
3599 Stop_Processing : out Boolean)
3601 Cont_Typ : Entity_Id;
3602 Nam : Node_Id;
3603 Nam_Copy : Node_Id;
3605 begin
3606 Stop_Processing := False;
3608 -- The iterator specification has syntactic errors. Transform the
3609 -- loop into an infinite loop in order to safely perform at least
3610 -- some minor analysis. This check must come first.
3612 if Error_Posted (Iter_Spec) then
3613 Set_Iteration_Scheme (N, Empty);
3614 Analyze (N);
3615 Stop_Processing := True;
3617 -- Nothing to do when the loop is already wrapped in a block
3619 elsif Is_Wrapped_In_Block (N) then
3620 null;
3622 -- Otherwise the iterator loop traverses an array or a container
3623 -- and appears in the form
3625 -- for Def_Id in [reverse] Iterator_Name loop
3626 -- for Def_Id [: Subtyp_Indic] of [reverse] Iterable_Name loop
3628 else
3629 -- Prepare a copy of the iterated name for preanalysis. The
3630 -- copy is semi inserted into the tree by setting its Parent
3631 -- pointer.
3633 Nam := Name (Iter_Spec);
3634 Nam_Copy := New_Copy_Tree (Nam);
3635 Set_Parent (Nam_Copy, Parent (Nam));
3637 -- Determine what the loop is iterating on
3639 Preanalyze_Range (Nam_Copy);
3640 Cont_Typ := Etype (Nam_Copy);
3642 -- The iterator loop is traversing an array. This case does not
3643 -- require any transformation, unless the name contains a call
3644 -- that returns on the secondary stack since we need to release
3645 -- the space allocated there.
3647 if Is_Array_Type (Cont_Typ)
3648 and then not Has_Sec_Stack_Call (Nam_Copy)
3649 then
3650 null;
3652 -- Otherwise unconditionally wrap the loop statement within
3653 -- a block. The expansion of iterator loops may relocate the
3654 -- iterator outside the loop, thus "leaking" its entity into
3655 -- the enclosing scope. Wrapping the loop statement allows
3656 -- for multiple iterator loops using the same iterator name
3657 -- to coexist within the same scope.
3659 -- The block must manage the secondary stack when the iterator
3660 -- loop is traversing a container using either
3662 -- * A default iterator obtained on the secondary stack
3664 -- * Call to Iterate where the iterator is returned on the
3665 -- secondary stack.
3667 -- * Combination of First, Next, and Has_Element where the
3668 -- first two return a cursor on the secondary stack.
3670 else
3671 Wrap_Loop_Statement
3672 (Manage_Sec_Stack =>
3673 Has_Sec_Stack_Default_Iterator (Cont_Typ)
3674 or else Has_Sec_Stack_Call (Nam_Copy)
3675 or else Is_Sec_Stack_Iteration_Primitive
3676 (Cont_Typ, Name_First)
3677 or else Is_Sec_Stack_Iteration_Primitive
3678 (Cont_Typ, Name_Next));
3679 Stop_Processing := True;
3680 end if;
3681 end if;
3682 end Prepare_Iterator_Loop;
3684 -----------------------------
3685 -- Prepare_Param_Spec_Loop --
3686 -----------------------------
3688 procedure Prepare_Param_Spec_Loop
3689 (Param_Spec : Node_Id;
3690 Stop_Processing : out Boolean)
3692 High : Node_Id;
3693 Low : Node_Id;
3694 Rng : Node_Id;
3695 Rng_Copy : Node_Id;
3696 Rng_Typ : Entity_Id;
3698 begin
3699 Stop_Processing := False;
3700 Rng := Discrete_Subtype_Definition (Param_Spec);
3702 -- Nothing to do when the loop is already wrapped in a block
3704 if Is_Wrapped_In_Block (N) then
3705 null;
3707 -- The parameter specification appears in the form
3709 -- for Def_Id in Subtype_Mark Constraint loop
3711 elsif Nkind (Rng) = N_Subtype_Indication
3712 and then Nkind (Range_Expression (Constraint (Rng))) = N_Range
3713 then
3714 Rng := Range_Expression (Constraint (Rng));
3716 -- Preanalyze the bounds of the range constraint, setting
3717 -- parent fields to associate the copied bounds with the range,
3718 -- allowing proper tree climbing during preanalysis.
3720 Low := New_Copy_Tree (Low_Bound (Rng));
3721 High := New_Copy_Tree (High_Bound (Rng));
3723 Set_Parent (Low, Rng);
3724 Set_Parent (High, Rng);
3726 Preanalyze (Low);
3727 Preanalyze (High);
3729 -- The bounds contain at least one function call that returns
3730 -- on the secondary stack. Note that the loop must be wrapped
3731 -- only when such a call exists.
3733 if Has_Sec_Stack_Call (Low) or else Has_Sec_Stack_Call (High)
3734 then
3735 Wrap_Loop_Statement (Manage_Sec_Stack => True);
3736 Stop_Processing := True;
3737 end if;
3739 -- Otherwise the parameter specification appears in the form
3741 -- for Def_Id in Range loop
3743 else
3744 -- Prepare a copy of the discrete range for preanalysis. The
3745 -- copy is semi inserted into the tree by setting its Parent
3746 -- pointer.
3748 Rng_Copy := New_Copy_Tree (Rng);
3749 Set_Parent (Rng_Copy, Parent (Rng));
3751 -- Determine what the loop is iterating on
3753 Preanalyze_Range (Rng_Copy);
3754 Rng_Typ := Etype (Rng_Copy);
3756 -- Wrap the loop statement within a block in order to manage
3757 -- the secondary stack when the discrete range is
3759 -- * Either a Forward_Iterator or a Reverse_Iterator
3761 -- * Function call whose return type requires finalization
3762 -- actions.
3764 -- ??? it is unclear why using Has_Sec_Stack_Call directly on
3765 -- the discrete range causes the freeze node of an itype to be
3766 -- in the wrong scope in complex assertion expressions.
3768 if Is_Iterator (Rng_Typ)
3769 or else (Nkind (Rng_Copy) = N_Function_Call
3770 and then Needs_Finalization (Rng_Typ))
3771 then
3772 Wrap_Loop_Statement (Manage_Sec_Stack => True);
3773 Stop_Processing := True;
3774 end if;
3775 end if;
3776 end Prepare_Param_Spec_Loop;
3778 -------------------------
3779 -- Wrap_Loop_Statement --
3780 -------------------------
3782 procedure Wrap_Loop_Statement (Manage_Sec_Stack : Boolean) is
3783 Loc : constant Source_Ptr := Sloc (N);
3785 Blk : Node_Id;
3786 Blk_Id : Entity_Id;
3788 begin
3789 Blk :=
3790 Make_Block_Statement (Loc,
3791 Declarations => New_List,
3792 Handled_Statement_Sequence =>
3793 Make_Handled_Sequence_Of_Statements (Loc,
3794 Statements => New_List (Relocate_Node (N))));
3796 Add_Block_Identifier (Blk, Blk_Id);
3797 Set_Uses_Sec_Stack (Blk_Id, Manage_Sec_Stack);
3799 Rewrite (N, Blk);
3800 Analyze (N);
3801 end Wrap_Loop_Statement;
3803 -- Local variables
3805 Iter_Spec : constant Node_Id := Iterator_Specification (Iter);
3806 Param_Spec : constant Node_Id := Loop_Parameter_Specification (Iter);
3808 -- Start of processing for Prepare_Loop_Statement
3810 begin
3811 Stop_Processing := False;
3813 if Present (Iter_Spec) then
3814 Prepare_Iterator_Loop (Iter_Spec, Stop_Processing);
3816 elsif Present (Param_Spec) then
3817 Prepare_Param_Spec_Loop (Param_Spec, Stop_Processing);
3818 end if;
3819 end Prepare_Loop_Statement;
3821 -- Local declarations
3823 Id : constant Node_Id := Identifier (N);
3824 Iter : constant Node_Id := Iteration_Scheme (N);
3825 Loc : constant Source_Ptr := Sloc (N);
3826 Ent : Entity_Id;
3827 Stmt : Node_Id;
3829 -- Start of processing for Analyze_Loop_Statement
3831 begin
3832 if Present (Id) then
3834 -- Make name visible, e.g. for use in exit statements. Loop labels
3835 -- are always considered to be referenced.
3837 Analyze (Id);
3838 Ent := Entity (Id);
3840 -- Guard against serious error (typically, a scope mismatch when
3841 -- semantic analysis is requested) by creating loop entity to
3842 -- continue analysis.
3844 if No (Ent) then
3845 if Total_Errors_Detected /= 0 then
3846 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3847 else
3848 raise Program_Error;
3849 end if;
3851 -- Verify that the loop name is hot hidden by an unrelated
3852 -- declaration in an inner scope.
3854 elsif Ekind (Ent) /= E_Label and then Ekind (Ent) /= E_Loop then
3855 Error_Msg_Sloc := Sloc (Ent);
3856 Error_Msg_N ("implicit label declaration for & is hidden#", Id);
3858 if Present (Homonym (Ent))
3859 and then Ekind (Homonym (Ent)) = E_Label
3860 then
3861 Set_Entity (Id, Ent);
3862 Mutate_Ekind (Ent, E_Loop);
3863 end if;
3865 else
3866 Generate_Reference (Ent, N, ' ');
3867 Generate_Definition (Ent);
3869 -- If we found a label, mark its type. If not, ignore it, since it
3870 -- means we have a conflicting declaration, which would already
3871 -- have been diagnosed at declaration time. Set Label_Construct
3872 -- of the implicit label declaration, which is not created by the
3873 -- parser for generic units.
3875 if Ekind (Ent) = E_Label then
3876 Reinit_Field_To_Zero (Ent, F_Enclosing_Scope);
3877 Reinit_Field_To_Zero (Ent, F_Reachable);
3878 Mutate_Ekind (Ent, E_Loop);
3880 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
3881 Set_Label_Construct (Parent (Ent), N);
3882 end if;
3883 end if;
3884 end if;
3886 -- Case of no identifier present. Create one and attach it to the
3887 -- loop statement for use as a scope and as a reference for later
3888 -- expansions. Indicate that the label does not come from source,
3889 -- and attach it to the loop statement so it is part of the tree,
3890 -- even without a full declaration.
3892 else
3893 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3894 Set_Etype (Ent, Standard_Void_Type);
3895 Set_Identifier (N, New_Occurrence_Of (Ent, Loc));
3896 Set_Parent (Ent, N);
3897 Set_Has_Created_Identifier (N);
3898 end if;
3900 -- Determine whether the loop statement must be transformed prior to
3901 -- analysis, and if so, perform it. This early modification is needed
3902 -- when:
3904 -- * The loop has an erroneous iteration scheme. In this case the
3905 -- loop is converted into an infinite loop in order to perform
3906 -- minor analysis.
3908 -- * The loop is an Ada 2012 iterator loop. In this case the loop is
3909 -- wrapped within a block to provide a local scope for the iterator.
3910 -- If the iterator specification requires the secondary stack in any
3911 -- way, the block is marked in order to manage it.
3913 -- * The loop is using a parameter specification where the discrete
3914 -- range requires the secondary stack. In this case the loop is
3915 -- wrapped within a block in order to manage the secondary stack.
3917 -- ??? This overlooks finalization: the loop may leave the secondary
3918 -- stack untouched, but its iterator or discrete range may need
3919 -- finalization, in which case the block is also required. Therefore
3920 -- the criterion must be based on Sem_Util.Requires_Transient_Scope,
3921 -- which happens to be what is currently implemented.
3923 if Present (Iter) then
3924 declare
3925 Stop_Processing : Boolean;
3926 begin
3927 Prepare_Loop_Statement (Iter, Stop_Processing);
3929 if Stop_Processing then
3930 return;
3931 end if;
3932 end;
3933 end if;
3935 -- Kill current values on entry to loop, since statements in the body of
3936 -- the loop may have been executed before the loop is entered. Similarly
3937 -- we kill values after the loop, since we do not know that the body of
3938 -- the loop was executed.
3940 Kill_Current_Values;
3941 Push_Scope (Ent);
3942 Analyze_Iteration_Scheme (Iter);
3944 -- Check for following case which merits a warning if the type E of is
3945 -- a multi-dimensional array (and no explicit subscript ranges present).
3947 -- for J in E'Range
3948 -- for K in E'Range
3950 if Present (Iter)
3951 and then Present (Loop_Parameter_Specification (Iter))
3952 then
3953 declare
3954 LPS : constant Node_Id := Loop_Parameter_Specification (Iter);
3955 DSD : constant Node_Id :=
3956 Original_Node (Discrete_Subtype_Definition (LPS));
3957 begin
3958 if Nkind (DSD) = N_Attribute_Reference
3959 and then Attribute_Name (DSD) = Name_Range
3960 and then No (Expressions (DSD))
3961 then
3962 declare
3963 Typ : constant Entity_Id := Etype (Prefix (DSD));
3964 begin
3965 if Is_Array_Type (Typ)
3966 and then Number_Dimensions (Typ) > 1
3967 and then Nkind (Parent (N)) = N_Loop_Statement
3968 and then Present (Iteration_Scheme (Parent (N)))
3969 then
3970 declare
3971 OIter : constant Node_Id :=
3972 Iteration_Scheme (Parent (N));
3973 OLPS : constant Node_Id :=
3974 Loop_Parameter_Specification (OIter);
3975 ODSD : constant Node_Id :=
3976 Original_Node (Discrete_Subtype_Definition (OLPS));
3977 begin
3978 if Nkind (ODSD) = N_Attribute_Reference
3979 and then Attribute_Name (ODSD) = Name_Range
3980 and then No (Expressions (ODSD))
3981 and then Etype (Prefix (ODSD)) = Typ
3982 then
3983 Error_Msg_Sloc := Sloc (ODSD);
3984 Error_Msg_N
3985 ("inner range same as outer range#??", DSD);
3986 end if;
3987 end;
3988 end if;
3989 end;
3990 end if;
3991 end;
3992 end if;
3994 -- Analyze the statements of the body except in the case of an Ada 2012
3995 -- iterator with the expander active. In this case the expander will do
3996 -- a rewrite of the loop into a while loop. We will then analyze the
3997 -- loop body when we analyze this while loop.
3999 -- We need to do this delay because if the container is for indefinite
4000 -- types the actual subtype of the components will only be determined
4001 -- when the cursor declaration is analyzed.
4003 -- If the expander is not active then we want to analyze the loop body
4004 -- now even in the Ada 2012 iterator case, since the rewriting will not
4005 -- be done. Insert the loop variable in the current scope, if not done
4006 -- when analysing the iteration scheme. Set its kind properly to detect
4007 -- improper uses in the loop body.
4009 -- In GNATprove mode, we do one of the above depending on the kind of
4010 -- loop. If it is an iterator over an array, then we do not analyze the
4011 -- loop now. We will analyze it after it has been rewritten by the
4012 -- special SPARK expansion which is activated in GNATprove mode. We need
4013 -- to do this so that other expansions that should occur in GNATprove
4014 -- mode take into account the specificities of the rewritten loop, in
4015 -- particular the introduction of a renaming (which needs to be
4016 -- expanded).
4018 -- In other cases in GNATprove mode then we want to analyze the loop
4019 -- body now, since no rewriting will occur. Within a generic the
4020 -- GNATprove mode is irrelevant, we must analyze the generic for
4021 -- non-local name capture.
4023 if Present (Iter)
4024 and then Present (Iterator_Specification (Iter))
4025 then
4026 if GNATprove_Mode
4027 and then Is_Iterator_Over_Array (Iterator_Specification (Iter))
4028 and then not Inside_A_Generic
4029 then
4030 null;
4032 elsif not Expander_Active then
4033 declare
4034 I_Spec : constant Node_Id := Iterator_Specification (Iter);
4035 Id : constant Entity_Id := Defining_Identifier (I_Spec);
4037 begin
4038 if Scope (Id) /= Current_Scope then
4039 Enter_Name (Id);
4040 end if;
4042 -- In an element iterator, the loop parameter is a variable if
4043 -- the domain of iteration (container or array) is a variable.
4045 if not Of_Present (I_Spec)
4046 or else not Is_Variable (Name (I_Spec))
4047 then
4048 Mutate_Ekind (Id, E_Loop_Parameter);
4049 end if;
4050 end;
4052 Analyze_Statements (Statements (N));
4053 end if;
4055 else
4056 -- Pre-Ada2012 for-loops and while loops
4058 Analyze_Statements (Statements (N));
4059 end if;
4061 -- If the loop has no side effects, mark it for removal.
4063 if Side_Effect_Free_Loop (N) then
4064 Set_Is_Null_Loop (N);
4065 end if;
4067 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
4068 -- the loop is transformed into a conditional block. Retrieve the loop.
4070 Stmt := N;
4072 if Subject_To_Loop_Entry_Attributes (Stmt) then
4073 Stmt := Find_Loop_In_Conditional_Block (Stmt);
4074 end if;
4076 -- Finish up processing for the loop. We kill all current values, since
4077 -- in general we don't know if the statements in the loop have been
4078 -- executed. We could do a bit better than this with a loop that we
4079 -- know will execute at least once, but it's not worth the trouble and
4080 -- the front end is not in the business of flow tracing.
4082 Process_End_Label (Stmt, 'e', Ent);
4083 End_Scope;
4084 Kill_Current_Values;
4086 -- Check for infinite loop. Skip check for generated code, since it
4087 -- justs waste time and makes debugging the routine called harder.
4089 -- Note that we have to wait till the body of the loop is fully analyzed
4090 -- before making this call, since Check_Infinite_Loop_Warning relies on
4091 -- being able to use semantic visibility information to find references.
4093 if Comes_From_Source (Stmt) then
4094 Check_Infinite_Loop_Warning (Stmt);
4095 end if;
4097 -- Code after loop is unreachable if the loop has no WHILE or FOR and
4098 -- contains no EXIT statements within the body of the loop.
4100 if No (Iter) and then not Has_Exit (Ent) then
4101 Check_Unreachable_Code (Stmt);
4102 end if;
4103 end Analyze_Loop_Statement;
4105 ----------------------------
4106 -- Analyze_Null_Statement --
4107 ----------------------------
4109 -- Note: the semantics of the null statement is implemented by a single
4110 -- null statement, too bad everything isn't as simple as this.
4112 procedure Analyze_Null_Statement (N : Node_Id) is
4113 pragma Warnings (Off, N);
4114 begin
4115 null;
4116 end Analyze_Null_Statement;
4118 -------------------------
4119 -- Analyze_Target_Name --
4120 -------------------------
4122 procedure Analyze_Target_Name (N : Node_Id) is
4123 procedure Report_Error;
4124 -- Complain about illegal use of target_name and rewrite it into unknown
4125 -- identifier.
4127 ------------------
4128 -- Report_Error --
4129 ------------------
4131 procedure Report_Error is
4132 begin
4133 Error_Msg_N
4134 ("must appear in the right-hand side of an assignment statement",
4136 Rewrite (N, New_Occurrence_Of (Any_Id, Sloc (N)));
4137 end Report_Error;
4139 -- Start of processing for Analyze_Target_Name
4141 begin
4142 -- A target name has the type of the left-hand side of the enclosing
4143 -- assignment.
4145 -- First, verify that the context is the right-hand side of an
4146 -- assignment statement.
4148 if No (Current_Assignment) then
4149 Report_Error;
4150 return;
4151 end if;
4153 declare
4154 Current : Node_Id := N;
4155 Context : Node_Id := Parent (N);
4156 begin
4157 while Present (Context) loop
4159 -- Check if target_name appears in the expression of the enclosing
4160 -- assignment.
4162 if Nkind (Context) = N_Assignment_Statement then
4163 if Current = Expression (Context) then
4164 pragma Assert (Context = Current_Assignment);
4165 Set_Etype (N, Etype (Name (Current_Assignment)));
4166 else
4167 Report_Error;
4168 end if;
4169 return;
4171 -- Prevent the search from going too far
4173 elsif Is_Body_Or_Package_Declaration (Context) then
4174 Report_Error;
4175 return;
4176 end if;
4178 Current := Context;
4179 Context := Parent (Context);
4180 end loop;
4182 Report_Error;
4183 end;
4184 end Analyze_Target_Name;
4186 ------------------------
4187 -- Analyze_Statements --
4188 ------------------------
4190 procedure Analyze_Statements (L : List_Id) is
4191 Lab : Entity_Id;
4192 S : Node_Id;
4194 begin
4195 -- The labels declared in the statement list are reachable from
4196 -- statements in the list. We do this as a prepass so that any goto
4197 -- statement will be properly flagged if its target is not reachable.
4198 -- This is not required, but is nice behavior.
4200 S := First (L);
4201 while Present (S) loop
4202 if Nkind (S) = N_Label then
4203 Analyze (Identifier (S));
4204 Lab := Entity (Identifier (S));
4206 -- If we found a label mark it as reachable
4208 if Ekind (Lab) = E_Label then
4209 Generate_Definition (Lab);
4210 Set_Reachable (Lab);
4212 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
4213 Set_Label_Construct (Parent (Lab), S);
4214 end if;
4216 -- If we failed to find a label, it means the implicit declaration
4217 -- of the label was hidden. A for-loop parameter can do this to
4218 -- a label with the same name inside the loop, since the implicit
4219 -- label declaration is in the innermost enclosing body or block
4220 -- statement.
4222 else
4223 Error_Msg_Sloc := Sloc (Lab);
4224 Error_Msg_N
4225 ("implicit label declaration for & is hidden#",
4226 Identifier (S));
4227 end if;
4228 end if;
4230 Next (S);
4231 end loop;
4233 -- Perform semantic analysis on all statements
4235 Conditional_Statements_Begin;
4237 S := First (L);
4238 while Present (S) loop
4239 Analyze (S);
4241 -- Remove dimension in all statements
4243 Remove_Dimension_In_Statement (S);
4244 Next (S);
4245 end loop;
4247 Conditional_Statements_End;
4249 -- Make labels unreachable. Visibility is not sufficient, because labels
4250 -- in one if-branch for example are not reachable from the other branch,
4251 -- even though their declarations are in the enclosing declarative part.
4253 S := First (L);
4254 while Present (S) loop
4255 if Nkind (S) = N_Label
4256 and then Ekind (Entity (Identifier (S))) = E_Label
4257 then
4258 Set_Reachable (Entity (Identifier (S)), False);
4259 end if;
4261 Next (S);
4262 end loop;
4263 end Analyze_Statements;
4265 ----------------------------
4266 -- Check_Unreachable_Code --
4267 ----------------------------
4269 procedure Check_Unreachable_Code (N : Node_Id) is
4271 function Is_Simple_Case (N : Node_Id) return Boolean;
4272 -- N is the condition of an if statement. True if N is simple enough
4273 -- that we should not set Unblocked_Exit_Count in the special case
4274 -- below.
4276 --------------------
4277 -- Is_Simple_Case --
4278 --------------------
4280 function Is_Simple_Case (N : Node_Id) return Boolean is
4281 begin
4282 return
4283 Is_Trivial_Boolean (N)
4284 or else
4285 (Comes_From_Source (N)
4286 and then Is_Static_Expression (N)
4287 and then Nkind (N) in N_Identifier | N_Expanded_Name
4288 and then Ekind (Entity (N)) = E_Constant)
4289 or else
4290 (not In_Instance
4291 and then Nkind (Original_Node (N)) = N_Op_Not
4292 and then Is_Simple_Case (Right_Opnd (Original_Node (N))));
4293 end Is_Simple_Case;
4295 Error_Node : Node_Id;
4296 Nxt : Node_Id;
4297 P : Node_Id;
4299 begin
4300 if Comes_From_Source (N) then
4301 Nxt := Original_Node (Next (N));
4303 -- Skip past pragmas
4305 while Nkind (Nxt) = N_Pragma loop
4306 Nxt := Original_Node (Next (Nxt));
4307 end loop;
4309 -- If a label follows us, then we never have dead code, since someone
4310 -- could branch to the label, so we just ignore it.
4312 if Nkind (Nxt) = N_Label then
4313 return;
4315 -- Otherwise see if we have a real statement following us
4317 elsif Comes_From_Source (Nxt)
4318 and then Is_Statement (Nxt)
4319 then
4320 -- Special very annoying exception. Ada RM 6.5(5) annoyingly
4321 -- requires functions to have at least one return statement, so
4322 -- don't complain about a simple return that follows a raise or a
4323 -- call to procedure with No_Return.
4325 if not (Present (Current_Subprogram)
4326 and then Ekind (Current_Subprogram) = E_Function
4327 and then (Nkind (N) in N_Raise_Statement
4328 or else
4329 (Nkind (N) = N_Procedure_Call_Statement
4330 and then Is_Entity_Name (Name (N))
4331 and then Present (Entity (Name (N)))
4332 and then No_Return (Entity (Name (N)))))
4333 and then Nkind (Nxt) = N_Simple_Return_Statement)
4334 then
4335 -- The rather strange shenanigans with the warning message
4336 -- here reflects the fact that Kill_Dead_Code is very good at
4337 -- removing warnings in deleted code, and this is one warning
4338 -- we would prefer NOT to have removed.
4340 Error_Node := Nxt;
4342 -- If we have unreachable code, analyze and remove the
4343 -- unreachable code, since it is useless and we don't want
4344 -- to generate junk warnings.
4346 -- We skip this step if we are not in code generation mode.
4348 -- This is the one case where we remove dead code in the
4349 -- semantics as opposed to the expander, and we do not want
4350 -- to remove code if we are not in code generation mode, since
4351 -- this messes up the tree or loses useful information for
4352 -- analysis tools such as CodePeer.
4354 -- Note that one might react by moving the whole circuit to
4355 -- exp_ch5, but then we lose the warning in -gnatc mode.
4357 if Operating_Mode = Generate_Code then
4358 loop
4359 declare
4360 Del : constant Node_Id := Next (N);
4361 -- Node to be possibly deleted
4362 begin
4363 -- Quit deleting when we have nothing more to delete
4364 -- or if we hit a label (since someone could transfer
4365 -- control to a label, so we should not delete it).
4367 exit when No (Del) or else Nkind (Del) = N_Label;
4369 -- Statement/declaration is to be deleted
4371 Analyze (Del);
4372 Kill_Dead_Code (Del);
4373 Remove (Del);
4374 end;
4375 end loop;
4377 -- If this is a function, we add "raise Program_Error;",
4378 -- because otherwise, we will get incorrect warnings about
4379 -- falling off the end of the function.
4381 declare
4382 Subp : constant Entity_Id := Current_Subprogram;
4383 begin
4384 if Present (Subp) and then Ekind (Subp) = E_Function then
4385 Insert_After_And_Analyze (N,
4386 Make_Raise_Program_Error (Sloc (Error_Node),
4387 Reason => PE_Missing_Return));
4388 end if;
4389 end;
4391 end if;
4393 -- Suppress the warning in instances, because a statement can
4394 -- be unreachable in some instances but not others.
4396 if not In_Instance then
4397 Error_Msg_N ("??unreachable code!", Error_Node);
4398 end if;
4399 end if;
4401 -- If the unconditional transfer of control instruction is the
4402 -- last statement of a sequence, then see if our parent is one of
4403 -- the constructs for which we count unblocked exits, and if so,
4404 -- adjust the count.
4406 else
4407 P := Parent (N);
4409 -- Statements in THEN part or ELSE part of IF statement
4411 if Nkind (P) = N_If_Statement then
4412 null;
4414 -- Statements in ELSIF part of an IF statement
4416 elsif Nkind (P) = N_Elsif_Part then
4417 P := Parent (P);
4418 pragma Assert (Nkind (P) = N_If_Statement);
4420 -- Statements in CASE statement alternative
4422 elsif Nkind (P) = N_Case_Statement_Alternative then
4423 P := Parent (P);
4424 pragma Assert (Nkind (P) = N_Case_Statement);
4426 -- Statements in body of block
4428 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
4429 and then Nkind (Parent (P)) = N_Block_Statement
4430 then
4431 -- The original loop is now placed inside a block statement
4432 -- due to the expansion of attribute 'Loop_Entry. Return as
4433 -- this is not a "real" block for the purposes of exit
4434 -- counting.
4436 if Nkind (N) = N_Loop_Statement
4437 and then Subject_To_Loop_Entry_Attributes (N)
4438 then
4439 return;
4440 end if;
4442 -- Statements in exception handler in a block
4444 elsif Nkind (P) = N_Exception_Handler
4445 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
4446 and then Nkind (Parent (Parent (P))) = N_Block_Statement
4447 then
4448 null;
4450 -- None of these cases, so return
4452 else
4453 return;
4454 end if;
4456 -- This was one of the cases we are looking for (i.e. the parent
4457 -- construct was IF, CASE or block). In most cases, we simply
4458 -- decrement the count. However, if the parent is something like:
4460 -- if cond then
4461 -- raise ...; -- or some other jump
4462 -- end if;
4464 -- where cond is an expression that is known-true at compile time,
4465 -- we can treat that as just the jump -- i.e. anything following
4466 -- the if statement is unreachable. We don't do this for simple
4467 -- cases like "if True" or "if Debug_Flag", because that causes
4468 -- too many warnings.
4470 if Nkind (P) = N_If_Statement
4471 and then Present (Then_Statements (P))
4472 and then No (Elsif_Parts (P))
4473 and then No (Else_Statements (P))
4474 and then Is_OK_Static_Expression (Condition (P))
4475 and then Is_True (Expr_Value (Condition (P)))
4476 and then not Is_Simple_Case (Condition (P))
4477 then
4478 pragma Assert (Unblocked_Exit_Count = 2);
4479 Unblocked_Exit_Count := 0;
4480 else
4481 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
4482 end if;
4483 end if;
4484 end if;
4485 end Check_Unreachable_Code;
4487 ------------------------
4488 -- Has_Sec_Stack_Call --
4489 ------------------------
4491 function Has_Sec_Stack_Call (N : Node_Id) return Boolean is
4492 function Check_Call (N : Node_Id) return Traverse_Result;
4493 -- Check if N is a function call which uses the secondary stack
4495 ----------------
4496 -- Check_Call --
4497 ----------------
4499 function Check_Call (N : Node_Id) return Traverse_Result is
4500 Nam : Node_Id;
4501 Subp : Entity_Id;
4502 Typ : Entity_Id;
4504 begin
4505 if Nkind (N) = N_Function_Call then
4506 Nam := Name (N);
4508 -- Obtain the subprogram being invoked
4510 loop
4511 if Nkind (Nam) = N_Explicit_Dereference then
4512 Nam := Prefix (Nam);
4514 elsif Nkind (Nam) = N_Selected_Component then
4515 Nam := Selector_Name (Nam);
4517 else
4518 exit;
4519 end if;
4520 end loop;
4522 Subp := Entity (Nam);
4524 if Present (Subp) then
4525 Typ := Etype (Subp);
4527 if Requires_Transient_Scope (Typ) then
4528 return Abandon;
4530 elsif Sec_Stack_Needed_For_Return (Subp) then
4531 return Abandon;
4532 end if;
4533 end if;
4534 end if;
4536 -- Continue traversing the tree
4538 return OK;
4539 end Check_Call;
4541 function Check_Calls is new Traverse_Func (Check_Call);
4543 -- Start of processing for Has_Sec_Stack_Call
4545 begin
4546 return Check_Calls (N) = Abandon;
4547 end Has_Sec_Stack_Call;
4549 ----------------------
4550 -- Preanalyze_Range --
4551 ----------------------
4553 procedure Preanalyze_Range (R_Copy : Node_Id) is
4554 Save_Analysis : constant Boolean := Full_Analysis;
4555 Typ : Entity_Id;
4557 begin
4558 Full_Analysis := False;
4559 Expander_Mode_Save_And_Set (False);
4561 -- In addition to the above we must explicitly suppress the generation
4562 -- of freeze nodes that might otherwise be generated during resolution
4563 -- of the range (e.g. if given by an attribute that will freeze its
4564 -- prefix).
4566 Set_Must_Not_Freeze (R_Copy);
4568 if Nkind (R_Copy) = N_Attribute_Reference then
4569 Set_Must_Not_Freeze (Prefix (R_Copy));
4570 end if;
4572 Analyze (R_Copy);
4574 if Nkind (R_Copy) in N_Subexpr and then Is_Overloaded (R_Copy) then
4576 -- Apply preference rules for range of predefined integer types, or
4577 -- check for array or iterable construct for "of" iterator, or
4578 -- diagnose true ambiguity.
4580 declare
4581 I : Interp_Index;
4582 It : Interp;
4583 Found : Entity_Id := Empty;
4585 begin
4586 Get_First_Interp (R_Copy, I, It);
4587 while Present (It.Typ) loop
4588 if Is_Discrete_Type (It.Typ) then
4589 if No (Found) then
4590 Found := It.Typ;
4591 else
4592 if Scope (Found) = Standard_Standard then
4593 null;
4595 elsif Scope (It.Typ) = Standard_Standard then
4596 Found := It.Typ;
4598 else
4599 -- Both of them are user-defined
4601 Error_Msg_N
4602 ("ambiguous bounds in range of iteration", R_Copy);
4603 Error_Msg_N ("\possible interpretations:", R_Copy);
4604 Error_Msg_NE ("\\}", R_Copy, Found);
4605 Error_Msg_NE ("\\}", R_Copy, It.Typ);
4606 exit;
4607 end if;
4608 end if;
4610 elsif Nkind (Parent (R_Copy)) = N_Iterator_Specification
4611 and then Of_Present (Parent (R_Copy))
4612 then
4613 if Is_Array_Type (It.Typ)
4614 or else Has_Aspect (It.Typ, Aspect_Iterator_Element)
4615 or else Has_Aspect (It.Typ, Aspect_Constant_Indexing)
4616 or else Has_Aspect (It.Typ, Aspect_Variable_Indexing)
4617 then
4618 if No (Found) then
4619 Found := It.Typ;
4620 Set_Etype (R_Copy, It.Typ);
4622 else
4623 Error_Msg_N ("ambiguous domain of iteration", R_Copy);
4624 end if;
4625 end if;
4626 end if;
4628 Get_Next_Interp (I, It);
4629 end loop;
4630 end;
4631 end if;
4633 -- Subtype mark in iteration scheme
4635 if Is_Entity_Name (R_Copy) and then Is_Type (Entity (R_Copy)) then
4636 null;
4638 -- Expression in range, or Ada 2012 iterator
4640 elsif Nkind (R_Copy) in N_Subexpr then
4641 Resolve (R_Copy);
4642 Typ := Etype (R_Copy);
4644 if Is_Discrete_Type (Typ) then
4645 null;
4647 -- Check that the resulting object is an iterable container
4649 elsif Has_Aspect (Typ, Aspect_Iterator_Element)
4650 or else Has_Aspect (Typ, Aspect_Constant_Indexing)
4651 or else Has_Aspect (Typ, Aspect_Variable_Indexing)
4652 then
4653 null;
4655 -- The expression may yield an implicit reference to an iterable
4656 -- container. Insert explicit dereference so that proper type is
4657 -- visible in the loop.
4659 elsif Has_Implicit_Dereference (Etype (R_Copy)) then
4660 Build_Explicit_Dereference
4661 (R_Copy, Get_Reference_Discriminant (Etype (R_Copy)));
4662 end if;
4663 end if;
4665 Expander_Mode_Restore;
4666 Full_Analysis := Save_Analysis;
4667 end Preanalyze_Range;
4669 end Sem_Ch5;