Fix typos in riscv register save/restore.
[official-gcc.git] / gcc / ada / sem_ch5.adb
blobe7fc14983d69e77a278cbcc9c8ce691a6032b6df
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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Ghost; use Ghost;
36 with Lib; use Lib;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Sem; use Sem;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Case; use Sem_Case;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Dim; use Sem_Dim;
51 with Sem_Disp; use Sem_Disp;
52 with Sem_Elab; use Sem_Elab;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Res; use Sem_Res;
55 with Sem_Type; use Sem_Type;
56 with Sem_Util; use Sem_Util;
57 with Sem_Warn; use Sem_Warn;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Sinfo; use Sinfo;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
65 package body Sem_Ch5 is
67 Current_Assignment : Node_Id := Empty;
68 -- This variable holds the node for an assignment that contains target
69 -- names. The corresponding flag has been set by the parser, and when
70 -- set the analysis of the RHS must be done with all expansion disabled,
71 -- because the assignment is reanalyzed after expansion has replaced all
72 -- occurrences of the target name appropriately.
74 Unblocked_Exit_Count : Nat := 0;
75 -- This variable is used when processing if statements, case statements,
76 -- and block statements. It counts the number of exit points that are not
77 -- blocked by unconditional transfer instructions: for IF and CASE, these
78 -- are the branches of the conditional; for a block, they are the statement
79 -- sequence of the block, and the statement sequences of any exception
80 -- handlers that are part of the block. When processing is complete, if
81 -- this count is zero, it means that control cannot fall through the IF,
82 -- CASE or block statement. This is used for the generation of warning
83 -- messages. This variable is recursively saved on entry to processing the
84 -- construct, and restored on exit.
86 procedure Preanalyze_Range (R_Copy : Node_Id);
87 -- Determine expected type of range or domain of iteration of Ada 2012
88 -- loop by analyzing separate copy. Do the analysis and resolution of the
89 -- copy of the bound(s) with expansion disabled, to prevent the generation
90 -- of finalization actions. This prevents memory leaks when the bounds
91 -- contain calls to functions returning controlled arrays or when the
92 -- domain of iteration is a container.
94 ------------------------
95 -- Analyze_Assignment --
96 ------------------------
98 -- WARNING: This routine manages Ghost regions. Return statements must be
99 -- replaced by gotos which jump to the end of the routine and restore the
100 -- Ghost mode.
102 procedure Analyze_Assignment (N : Node_Id) is
103 Lhs : constant Node_Id := Name (N);
104 Rhs : Node_Id := Expression (N);
106 procedure Diagnose_Non_Variable_Lhs (N : Node_Id);
107 -- N is the node for the left hand side of an assignment, and it is not
108 -- a variable. This routine issues an appropriate diagnostic.
110 procedure Kill_Lhs;
111 -- This is called to kill current value settings of a simple variable
112 -- on the left hand side. We call it if we find any error in analyzing
113 -- the assignment, and at the end of processing before setting any new
114 -- current values in place.
116 procedure Set_Assignment_Type
117 (Opnd : Node_Id;
118 Opnd_Type : in out Entity_Id);
119 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
120 -- nominal subtype. This procedure is used to deal with cases where the
121 -- nominal subtype must be replaced by the actual subtype.
123 procedure Transform_BIP_Assignment (Typ : Entity_Id);
124 function Should_Transform_BIP_Assignment
125 (Typ : Entity_Id) return Boolean;
126 -- If the right-hand side of an assignment statement is a build-in-place
127 -- call we cannot build in place, so we insert a temp initialized with
128 -- the call, and transform the assignment statement to copy the temp.
129 -- Transform_BIP_Assignment does the tranformation, and
130 -- Should_Transform_BIP_Assignment determines whether we should.
131 -- The same goes for qualified expressions and conversions whose
132 -- operand is such a call.
134 -- This is only for nonlimited types; assignment statements are illegal
135 -- for limited types, but are generated internally for aggregates and
136 -- init procs. These limited-type are not really assignment statements
137 -- -- conceptually, they are initializations, so should not be
138 -- transformed.
140 -- Similarly, for nonlimited types, aggregates and init procs generate
141 -- assignment statements that are really initializations. These are
142 -- marked No_Ctrl_Actions.
144 -------------------------------
145 -- Diagnose_Non_Variable_Lhs --
146 -------------------------------
148 procedure Diagnose_Non_Variable_Lhs (N : Node_Id) is
149 begin
150 -- Not worth posting another error if left hand side already flagged
151 -- as being illegal in some respect.
153 if Error_Posted (N) then
154 return;
156 -- Some special bad cases of entity names
158 elsif Is_Entity_Name (N) then
159 declare
160 Ent : constant Entity_Id := Entity (N);
162 begin
163 if Ekind (Ent) = E_In_Parameter then
164 Error_Msg_N
165 ("assignment to IN mode parameter not allowed", N);
166 return;
168 -- Renamings of protected private components are turned into
169 -- constants when compiling a protected function. In the case
170 -- of single protected types, the private component appears
171 -- directly.
173 elsif (Is_Prival (Ent)
174 and then
175 (Ekind (Current_Scope) = E_Function
176 or else Ekind (Enclosing_Dynamic_Scope
177 (Current_Scope)) = E_Function))
178 or else
179 (Ekind (Ent) = E_Component
180 and then Is_Protected_Type (Scope (Ent)))
181 then
182 Error_Msg_N
183 ("protected function cannot modify protected object", N);
184 return;
186 elsif Ekind (Ent) = E_Loop_Parameter then
187 Error_Msg_N ("assignment to loop parameter not allowed", N);
188 return;
189 end if;
190 end;
192 -- For indexed components, test prefix if it is in array. We do not
193 -- want to recurse for cases where the prefix is a pointer, since we
194 -- may get a message confusing the pointer and what it references.
196 elsif Nkind (N) = N_Indexed_Component
197 and then Is_Array_Type (Etype (Prefix (N)))
198 then
199 Diagnose_Non_Variable_Lhs (Prefix (N));
200 return;
202 -- Another special case for assignment to discriminant
204 elsif Nkind (N) = N_Selected_Component then
205 if Present (Entity (Selector_Name (N)))
206 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
207 then
208 Error_Msg_N ("assignment to discriminant not allowed", N);
209 return;
211 -- For selection from record, diagnose prefix, but note that again
212 -- we only do this for a record, not e.g. for a pointer.
214 elsif Is_Record_Type (Etype (Prefix (N))) then
215 Diagnose_Non_Variable_Lhs (Prefix (N));
216 return;
217 end if;
218 end if;
220 -- If we fall through, we have no special message to issue
222 Error_Msg_N ("left hand side of assignment must be a variable", N);
223 end Diagnose_Non_Variable_Lhs;
225 --------------
226 -- Kill_Lhs --
227 --------------
229 procedure Kill_Lhs is
230 begin
231 if Is_Entity_Name (Lhs) then
232 declare
233 Ent : constant Entity_Id := Entity (Lhs);
234 begin
235 if Present (Ent) then
236 Kill_Current_Values (Ent);
237 end if;
238 end;
239 end if;
240 end Kill_Lhs;
242 -------------------------
243 -- Set_Assignment_Type --
244 -------------------------
246 procedure Set_Assignment_Type
247 (Opnd : Node_Id;
248 Opnd_Type : in out Entity_Id)
250 Decl : Node_Id;
252 begin
253 Require_Entity (Opnd);
255 -- If the assignment operand is an in-out or out parameter, then we
256 -- get the actual subtype (needed for the unconstrained case). If the
257 -- operand is the actual in an entry declaration, then within the
258 -- accept statement it is replaced with a local renaming, which may
259 -- also have an actual subtype.
261 if Is_Entity_Name (Opnd)
262 and then (Ekind (Entity (Opnd)) = E_Out_Parameter
263 or else Ekind_In (Entity (Opnd),
264 E_In_Out_Parameter,
265 E_Generic_In_Out_Parameter)
266 or else
267 (Ekind (Entity (Opnd)) = E_Variable
268 and then Nkind (Parent (Entity (Opnd))) =
269 N_Object_Renaming_Declaration
270 and then Nkind (Parent (Parent (Entity (Opnd)))) =
271 N_Accept_Statement))
272 then
273 Opnd_Type := Get_Actual_Subtype (Opnd);
275 -- If assignment operand is a component reference, then we get the
276 -- actual subtype of the component for the unconstrained case.
278 elsif Nkind_In (Opnd, N_Selected_Component, N_Explicit_Dereference)
279 and then not Is_Unchecked_Union (Opnd_Type)
280 then
281 Decl := Build_Actual_Subtype_Of_Component (Opnd_Type, Opnd);
283 if Present (Decl) then
284 Insert_Action (N, Decl);
285 Mark_Rewrite_Insertion (Decl);
286 Analyze (Decl);
287 Opnd_Type := Defining_Identifier (Decl);
288 Set_Etype (Opnd, Opnd_Type);
289 Freeze_Itype (Opnd_Type, N);
291 elsif Is_Constrained (Etype (Opnd)) then
292 Opnd_Type := Etype (Opnd);
293 end if;
295 -- For slice, use the constrained subtype created for the slice
297 elsif Nkind (Opnd) = N_Slice then
298 Opnd_Type := Etype (Opnd);
299 end if;
300 end Set_Assignment_Type;
302 -------------------------------------
303 -- Should_Transform_BIP_Assignment --
304 -------------------------------------
306 function Should_Transform_BIP_Assignment
307 (Typ : Entity_Id) return Boolean
309 Result : Boolean;
311 begin
312 if Expander_Active
313 and then not Is_Limited_View (Typ)
314 and then Is_Build_In_Place_Result_Type (Typ)
315 and then not No_Ctrl_Actions (N)
316 then
317 -- This function is called early, before name resolution is
318 -- complete, so we have to deal with things that might turn into
319 -- function calls later. N_Function_Call and N_Op nodes are the
320 -- obvious case. An N_Identifier or N_Expanded_Name is a
321 -- parameterless function call if it denotes a function.
322 -- Finally, an attribute reference can be a function call.
324 case Nkind (Unqual_Conv (Rhs)) is
325 when N_Function_Call
326 | N_Op
328 Result := True;
330 when N_Expanded_Name
331 | N_Identifier
333 case Ekind (Entity (Unqual_Conv (Rhs))) is
334 when E_Function
335 | E_Operator
337 Result := True;
339 when others =>
340 Result := False;
341 end case;
343 when N_Attribute_Reference =>
344 Result := Attribute_Name (Unqual_Conv (Rhs)) = Name_Input;
345 -- T'Input will turn into a call whose result type is T
347 when others =>
348 Result := False;
349 end case;
350 else
351 Result := False;
352 end if;
354 return Result;
355 end Should_Transform_BIP_Assignment;
357 ------------------------------
358 -- Transform_BIP_Assignment --
359 ------------------------------
361 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
363 -- Tranform "X : [constant] T := F (...);" into:
365 -- Temp : constant T := F (...);
366 -- X := Temp;
368 Loc : constant Source_Ptr := Sloc (N);
369 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', Rhs);
370 Obj_Decl : constant Node_Id :=
371 Make_Object_Declaration (Loc,
372 Defining_Identifier => Def_Id,
373 Constant_Present => True,
374 Object_Definition => New_Occurrence_Of (Typ, Loc),
375 Expression => Rhs,
376 Has_Init_Expression => True);
378 begin
379 Set_Etype (Def_Id, Typ);
380 Set_Expression (N, New_Occurrence_Of (Def_Id, Loc));
382 -- At this point, Rhs is no longer equal to Expression (N), so:
384 Rhs := Expression (N);
386 Insert_Action (N, Obj_Decl);
387 end Transform_BIP_Assignment;
389 -- Local variables
391 T1 : Entity_Id;
392 T2 : Entity_Id;
394 Save_Full_Analysis : Boolean := False;
395 -- Force initialization to facilitate static analysis
397 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
398 -- Save the Ghost mode to restore on exit
400 -- Start of processing for Analyze_Assignment
402 begin
403 Mark_Coextensions (N, Rhs);
405 -- Preserve relevant elaboration-related attributes of the context which
406 -- are no longer available or very expensive to recompute once analysis,
407 -- resolution, and expansion are over.
409 Mark_Elaboration_Attributes
410 (N_Id => N,
411 Checks => True,
412 Modes => True);
414 -- Analyze the target of the assignment first in case the expression
415 -- contains references to Ghost entities. The checks that verify the
416 -- proper use of a Ghost entity need to know the enclosing context.
418 Analyze (Lhs);
420 -- An assignment statement is Ghost when the left hand side denotes a
421 -- Ghost entity. Set the mode now to ensure that any nodes generated
422 -- during analysis and expansion are properly marked as Ghost.
424 if Has_Target_Names (N) then
425 Current_Assignment := N;
426 Expander_Mode_Save_And_Set (False);
427 Save_Full_Analysis := Full_Analysis;
428 Full_Analysis := False;
429 else
430 Current_Assignment := Empty;
431 end if;
433 Mark_And_Set_Ghost_Assignment (N);
434 Analyze (Rhs);
436 -- Ensure that we never do an assignment on a variable marked as
437 -- Is_Safe_To_Reevaluate.
439 pragma Assert
440 (not Is_Entity_Name (Lhs)
441 or else Ekind (Entity (Lhs)) /= E_Variable
442 or else not Is_Safe_To_Reevaluate (Entity (Lhs)));
444 -- Start type analysis for assignment
446 T1 := Etype (Lhs);
448 -- In the most general case, both Lhs and Rhs can be overloaded, and we
449 -- must compute the intersection of the possible types on each side.
451 if Is_Overloaded (Lhs) then
452 declare
453 I : Interp_Index;
454 It : Interp;
456 begin
457 T1 := Any_Type;
458 Get_First_Interp (Lhs, I, It);
460 while Present (It.Typ) loop
462 -- An indexed component with generalized indexing is always
463 -- overloaded with the corresponding dereference. Discard the
464 -- interpretation that yields a reference type, which is not
465 -- assignable.
467 if Nkind (Lhs) = N_Indexed_Component
468 and then Present (Generalized_Indexing (Lhs))
469 and then Has_Implicit_Dereference (It.Typ)
470 then
471 null;
473 -- This may be a call to a parameterless function through an
474 -- implicit dereference, so discard interpretation as well.
476 elsif Is_Entity_Name (Lhs)
477 and then Has_Implicit_Dereference (It.Typ)
478 then
479 null;
481 elsif Has_Compatible_Type (Rhs, It.Typ) then
482 if T1 = Any_Type then
483 T1 := It.Typ;
484 else
485 -- An explicit dereference is overloaded if the prefix
486 -- is. Try to remove the ambiguity on the prefix, the
487 -- error will be posted there if the ambiguity is real.
489 if Nkind (Lhs) = N_Explicit_Dereference then
490 declare
491 PI : Interp_Index;
492 PI1 : Interp_Index := 0;
493 PIt : Interp;
494 Found : Boolean;
496 begin
497 Found := False;
498 Get_First_Interp (Prefix (Lhs), PI, PIt);
500 while Present (PIt.Typ) loop
501 if Is_Access_Type (PIt.Typ)
502 and then Has_Compatible_Type
503 (Rhs, Designated_Type (PIt.Typ))
504 then
505 if Found then
506 PIt :=
507 Disambiguate (Prefix (Lhs),
508 PI1, PI, Any_Type);
510 if PIt = No_Interp then
511 Error_Msg_N
512 ("ambiguous left-hand side in "
513 & "assignment", Lhs);
514 exit;
515 else
516 Resolve (Prefix (Lhs), PIt.Typ);
517 end if;
519 exit;
520 else
521 Found := True;
522 PI1 := PI;
523 end if;
524 end if;
526 Get_Next_Interp (PI, PIt);
527 end loop;
528 end;
530 else
531 Error_Msg_N
532 ("ambiguous left-hand side in assignment", Lhs);
533 exit;
534 end if;
535 end if;
536 end if;
538 Get_Next_Interp (I, It);
539 end loop;
540 end;
542 if T1 = Any_Type then
543 Error_Msg_N
544 ("no valid types for left-hand side for assignment", Lhs);
545 Kill_Lhs;
546 goto Leave;
547 end if;
548 end if;
550 -- Deal with build-in-place calls for nonlimited types. We don't do this
551 -- later, because resolving the rhs tranforms it incorrectly for build-
552 -- in-place.
554 if Should_Transform_BIP_Assignment (Typ => T1) then
555 -- In certain cases involving user-defined concatenation operators,
556 -- we need to resolve the right-hand side before transforming the
557 -- assignment.
559 case Nkind (Unqual_Conv (Rhs)) is
560 when N_Function_Call =>
561 declare
562 Actual : Node_Id :=
563 First (Parameter_Associations (Unqual_Conv (Rhs)));
564 Actual_Exp : Node_Id;
566 begin
567 while Present (Actual) loop
568 if Nkind (Actual) = N_Parameter_Association then
569 Actual_Exp := Explicit_Actual_Parameter (Actual);
570 else
571 Actual_Exp := Actual;
572 end if;
574 if Nkind (Actual_Exp) = N_Op_Concat then
575 Resolve (Rhs, T1);
576 exit;
577 end if;
579 Next (Actual);
580 end loop;
581 end;
583 when N_Op
584 | N_Expanded_Name
585 | N_Identifier
586 | N_Attribute_Reference
588 null;
590 when others =>
591 raise Program_Error;
592 end case;
594 Transform_BIP_Assignment (Typ => T1);
595 end if;
597 pragma Assert (not Should_Transform_BIP_Assignment (Typ => T1));
599 -- The resulting assignment type is T1, so now we will resolve the left
600 -- hand side of the assignment using this determined type.
602 Resolve (Lhs, T1);
604 -- Cases where Lhs is not a variable. In an instance or an inlined body
605 -- no need for further check because assignment was legal in template.
607 if In_Inlined_Body then
608 null;
610 elsif not Is_Variable (Lhs) then
612 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
613 -- protected object.
615 declare
616 Ent : Entity_Id;
617 S : Entity_Id;
619 begin
620 if Ada_Version >= Ada_2005 then
622 -- Handle chains of renamings
624 Ent := Lhs;
625 while Nkind (Ent) in N_Has_Entity
626 and then Present (Entity (Ent))
627 and then Present (Renamed_Object (Entity (Ent)))
628 loop
629 Ent := Renamed_Object (Entity (Ent));
630 end loop;
632 if (Nkind (Ent) = N_Attribute_Reference
633 and then Attribute_Name (Ent) = Name_Priority)
635 -- Renamings of the attribute Priority applied to protected
636 -- objects have been previously expanded into calls to the
637 -- Get_Ceiling run-time subprogram.
639 or else Is_Expanded_Priority_Attribute (Ent)
640 then
641 -- The enclosing subprogram cannot be a protected function
643 S := Current_Scope;
644 while not (Is_Subprogram (S)
645 and then Convention (S) = Convention_Protected)
646 and then S /= Standard_Standard
647 loop
648 S := Scope (S);
649 end loop;
651 if Ekind (S) = E_Function
652 and then Convention (S) = Convention_Protected
653 then
654 Error_Msg_N
655 ("protected function cannot modify protected object",
656 Lhs);
657 end if;
659 -- Changes of the ceiling priority of the protected object
660 -- are only effective if the Ceiling_Locking policy is in
661 -- effect (AARM D.5.2 (5/2)).
663 if Locking_Policy /= 'C' then
664 Error_Msg_N
665 ("assignment to the attribute PRIORITY has no effect??",
666 Lhs);
667 Error_Msg_N
668 ("\since no Locking_Policy has been specified??", Lhs);
669 end if;
671 goto Leave;
672 end if;
673 end if;
674 end;
676 Diagnose_Non_Variable_Lhs (Lhs);
677 goto Leave;
679 -- Error of assigning to limited type. We do however allow this in
680 -- certain cases where the front end generates the assignments.
682 elsif Is_Limited_Type (T1)
683 and then not Assignment_OK (Lhs)
684 and then not Assignment_OK (Original_Node (Lhs))
685 then
686 -- CPP constructors can only be called in declarations
688 if Is_CPP_Constructor_Call (Rhs) then
689 Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
690 else
691 Error_Msg_N
692 ("left hand of assignment must not be limited type", Lhs);
693 Explain_Limited_Type (T1, Lhs);
694 end if;
696 goto Leave;
698 -- A class-wide type may be a limited view. This illegal case is not
699 -- caught by previous checks.
701 elsif Ekind (T1) = E_Class_Wide_Type and then From_Limited_With (T1) then
702 Error_Msg_NE ("invalid use of limited view of&", Lhs, T1);
703 goto Leave;
705 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
706 -- abstract. This is only checked when the assignment Comes_From_Source,
707 -- because in some cases the expander generates such assignments (such
708 -- in the _assign operation for an abstract type).
710 elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
711 Error_Msg_N
712 ("target of assignment operation must not be abstract", Lhs);
713 end if;
715 -- Resolution may have updated the subtype, in case the left-hand side
716 -- is a private protected component. Use the correct subtype to avoid
717 -- scoping issues in the back-end.
719 T1 := Etype (Lhs);
721 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
722 -- type. For example:
724 -- limited with P;
725 -- package Pkg is
726 -- type Acc is access P.T;
727 -- end Pkg;
729 -- with Pkg; use Acc;
730 -- procedure Example is
731 -- A, B : Acc;
732 -- begin
733 -- A.all := B.all; -- ERROR
734 -- end Example;
736 if Nkind (Lhs) = N_Explicit_Dereference
737 and then Ekind (T1) = E_Incomplete_Type
738 then
739 Error_Msg_N ("invalid use of incomplete type", Lhs);
740 Kill_Lhs;
741 goto Leave;
742 end if;
744 -- Now we can complete the resolution of the right hand side
746 Set_Assignment_Type (Lhs, T1);
748 -- If the target of the assignment is an entity of a mutable type and
749 -- the expression is a conditional expression, its alternatives can be
750 -- of different subtypes of the nominal type of the LHS, so they must be
751 -- resolved with the base type, given that their subtype may differ from
752 -- that of the target mutable object.
754 if Is_Entity_Name (Lhs)
755 and then Ekind_In (Entity (Lhs), E_In_Out_Parameter,
756 E_Out_Parameter,
757 E_Variable)
758 and then Is_Composite_Type (T1)
759 and then not Is_Constrained (Etype (Entity (Lhs)))
760 and then Nkind_In (Rhs, N_If_Expression, N_Case_Expression)
761 then
762 Resolve (Rhs, Base_Type (T1));
764 else
765 Resolve (Rhs, T1);
766 end if;
768 -- This is the point at which we check for an unset reference
770 Check_Unset_Reference (Rhs);
771 Check_Unprotected_Access (Lhs, Rhs);
773 -- Remaining steps are skipped if Rhs was syntactically in error
775 if Rhs = Error then
776 Kill_Lhs;
777 goto Leave;
778 end if;
780 T2 := Etype (Rhs);
782 if not Covers (T1, T2) then
783 Wrong_Type (Rhs, Etype (Lhs));
784 Kill_Lhs;
785 goto Leave;
786 end if;
788 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
789 -- types, use the non-limited view if available
791 if Nkind (Rhs) = N_Explicit_Dereference
792 and then Is_Tagged_Type (T2)
793 and then Has_Non_Limited_View (T2)
794 then
795 T2 := Non_Limited_View (T2);
796 end if;
798 Set_Assignment_Type (Rhs, T2);
800 if Total_Errors_Detected /= 0 then
801 if No (T1) then
802 T1 := Any_Type;
803 end if;
805 if No (T2) then
806 T2 := Any_Type;
807 end if;
808 end if;
810 if T1 = Any_Type or else T2 = Any_Type then
811 Kill_Lhs;
812 goto Leave;
813 end if;
815 -- If the rhs is class-wide or dynamically tagged, then require the lhs
816 -- to be class-wide. The case where the rhs is a dynamically tagged call
817 -- to a dispatching operation with a controlling access result is
818 -- excluded from this check, since the target has an access type (and
819 -- no tag propagation occurs in that case).
821 if (Is_Class_Wide_Type (T2)
822 or else (Is_Dynamically_Tagged (Rhs)
823 and then not Is_Access_Type (T1)))
824 and then not Is_Class_Wide_Type (T1)
825 then
826 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
828 elsif Is_Class_Wide_Type (T1)
829 and then not Is_Class_Wide_Type (T2)
830 and then not Is_Tag_Indeterminate (Rhs)
831 and then not Is_Dynamically_Tagged (Rhs)
832 then
833 Error_Msg_N ("dynamically tagged expression required!", Rhs);
834 end if;
836 -- Propagate the tag from a class-wide target to the rhs when the rhs
837 -- is a tag-indeterminate call.
839 if Is_Tag_Indeterminate (Rhs) then
840 if Is_Class_Wide_Type (T1) then
841 Propagate_Tag (Lhs, Rhs);
843 elsif Nkind (Rhs) = N_Function_Call
844 and then Is_Entity_Name (Name (Rhs))
845 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
846 then
847 Error_Msg_N
848 ("call to abstract function must be dispatching", Name (Rhs));
850 elsif Nkind (Rhs) = N_Qualified_Expression
851 and then Nkind (Expression (Rhs)) = N_Function_Call
852 and then Is_Entity_Name (Name (Expression (Rhs)))
853 and then
854 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
855 then
856 Error_Msg_N
857 ("call to abstract function must be dispatching",
858 Name (Expression (Rhs)));
859 end if;
860 end if;
862 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
863 -- apply an implicit conversion of the rhs to that type to force
864 -- appropriate static and run-time accessibility checks. This applies
865 -- as well to anonymous access-to-subprogram types that are component
866 -- subtypes or formal parameters.
868 if Ada_Version >= Ada_2005 and then Is_Access_Type (T1) then
869 if Is_Local_Anonymous_Access (T1)
870 or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
872 -- Handle assignment to an Ada 2012 stand-alone object
873 -- of an anonymous access type.
875 or else (Ekind (T1) = E_Anonymous_Access_Type
876 and then Nkind (Associated_Node_For_Itype (T1)) =
877 N_Object_Declaration)
879 then
880 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
881 Analyze_And_Resolve (Rhs, T1);
882 end if;
883 end if;
885 -- Ada 2005 (AI-231): Assignment to not null variable
887 if Ada_Version >= Ada_2005
888 and then Can_Never_Be_Null (T1)
889 and then not Assignment_OK (Lhs)
890 then
891 -- Case where we know the right hand side is null
893 if Known_Null (Rhs) then
894 Apply_Compile_Time_Constraint_Error
895 (N => Rhs,
896 Msg =>
897 "(Ada 2005) null not allowed in null-excluding objects??",
898 Reason => CE_Null_Not_Allowed);
900 -- We still mark this as a possible modification, that's necessary
901 -- to reset Is_True_Constant, and desirable for xref purposes.
903 Note_Possible_Modification (Lhs, Sure => True);
904 goto Leave;
906 -- If we know the right hand side is non-null, then we convert to the
907 -- target type, since we don't need a run time check in that case.
909 elsif not Can_Never_Be_Null (T2) then
910 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
911 Analyze_And_Resolve (Rhs, T1);
912 end if;
913 end if;
915 if Is_Scalar_Type (T1) then
916 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
918 -- For array types, verify that lengths match. If the right hand side
919 -- is a function call that has been inlined, the assignment has been
920 -- rewritten as a block, and the constraint check will be applied to the
921 -- assignment within the block.
923 elsif Is_Array_Type (T1)
924 and then (Nkind (Rhs) /= N_Type_Conversion
925 or else Is_Constrained (Etype (Rhs)))
926 and then (Nkind (Rhs) /= N_Function_Call
927 or else Nkind (N) /= N_Block_Statement)
928 then
929 -- Assignment verifies that the length of the Lsh and Rhs are equal,
930 -- but of course the indexes do not have to match. If the right-hand
931 -- side is a type conversion to an unconstrained type, a length check
932 -- is performed on the expression itself during expansion. In rare
933 -- cases, the redundant length check is computed on an index type
934 -- with a different representation, triggering incorrect code in the
935 -- back end.
937 Apply_Length_Check (Rhs, Etype (Lhs));
939 else
940 -- Discriminant checks are applied in the course of expansion
942 null;
943 end if;
945 -- Note: modifications of the Lhs may only be recorded after
946 -- checks have been applied.
948 Note_Possible_Modification (Lhs, Sure => True);
950 -- ??? a real accessibility check is needed when ???
952 -- Post warning for redundant assignment or variable to itself
954 if Warn_On_Redundant_Constructs
956 -- We only warn for source constructs
958 and then Comes_From_Source (N)
960 -- Where the object is the same on both sides
962 and then Same_Object (Lhs, Original_Node (Rhs))
964 -- But exclude the case where the right side was an operation that
965 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
966 -- don't want to warn in such a case, since it is reasonable to write
967 -- such expressions especially when K is defined symbolically in some
968 -- other package.
970 and then Nkind (Original_Node (Rhs)) not in N_Op
971 then
972 if Nkind (Lhs) in N_Has_Entity then
973 Error_Msg_NE -- CODEFIX
974 ("?r?useless assignment of & to itself!", N, Entity (Lhs));
975 else
976 Error_Msg_N -- CODEFIX
977 ("?r?useless assignment of object to itself!", N);
978 end if;
979 end if;
981 -- Check for non-allowed composite assignment
983 if not Support_Composite_Assign_On_Target
984 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
985 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
986 then
987 Error_Msg_CRT ("composite assignment", N);
988 end if;
990 -- Save the scenario for later examination by the ABE Processing phase
992 Record_Elaboration_Scenario (N);
994 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
995 -- assignment is a source assignment in the extended main source unit.
996 -- We are not interested in any reference information outside this
997 -- context, or in compiler generated assignment statements.
999 if Comes_From_Source (N)
1000 and then In_Extended_Main_Source_Unit (Lhs)
1001 then
1002 Set_Referenced_Modified (Lhs, Out_Param => False);
1003 end if;
1005 -- RM 7.3.2 (12/3): An assignment to a view conversion (from a type to
1006 -- one of its ancestors) requires an invariant check. Apply check only
1007 -- if expression comes from source, otherwise it will be applied when
1008 -- value is assigned to source entity. This is not done in GNATprove
1009 -- mode, as GNATprove handles invariant checks itself.
1011 if Nkind (Lhs) = N_Type_Conversion
1012 and then Has_Invariants (Etype (Expression (Lhs)))
1013 and then Comes_From_Source (Expression (Lhs))
1014 and then not GNATprove_Mode
1015 then
1016 Insert_After (N, Make_Invariant_Call (Expression (Lhs)));
1017 end if;
1019 -- Final step. If left side is an entity, then we may be able to reset
1020 -- the current tracked values to new safe values. We only have something
1021 -- to do if the left side is an entity name, and expansion has not
1022 -- modified the node into something other than an assignment, and of
1023 -- course we only capture values if it is safe to do so.
1025 if Is_Entity_Name (Lhs)
1026 and then Nkind (N) = N_Assignment_Statement
1027 then
1028 declare
1029 Ent : constant Entity_Id := Entity (Lhs);
1031 begin
1032 if Safe_To_Capture_Value (N, Ent) then
1034 -- If simple variable on left side, warn if this assignment
1035 -- blots out another one (rendering it useless). We only do
1036 -- this for source assignments, otherwise we can generate bogus
1037 -- warnings when an assignment is rewritten as another
1038 -- assignment, and gets tied up with itself.
1040 -- There may have been a previous reference to a component of
1041 -- the variable, which in general removes the Last_Assignment
1042 -- field of the variable to indicate a relevant use of the
1043 -- previous assignment. However, if the assignment is to a
1044 -- subcomponent the reference may not have registered, because
1045 -- it is not possible to determine whether the context is an
1046 -- assignment. In those cases we generate a Deferred_Reference,
1047 -- to be used at the end of compilation to generate the right
1048 -- kind of reference, and we suppress a potential warning for
1049 -- a useless assignment, which might be premature. This may
1050 -- lose a warning in rare cases, but seems preferable to a
1051 -- misleading warning.
1053 if Warn_On_Modified_Unread
1054 and then Is_Assignable (Ent)
1055 and then Comes_From_Source (N)
1056 and then In_Extended_Main_Source_Unit (Ent)
1057 and then not Has_Deferred_Reference (Ent)
1058 then
1059 Warn_On_Useless_Assignment (Ent, N);
1060 end if;
1062 -- If we are assigning an access type and the left side is an
1063 -- entity, then make sure that the Is_Known_[Non_]Null flags
1064 -- properly reflect the state of the entity after assignment.
1066 if Is_Access_Type (T1) then
1067 if Known_Non_Null (Rhs) then
1068 Set_Is_Known_Non_Null (Ent, True);
1070 elsif Known_Null (Rhs)
1071 and then not Can_Never_Be_Null (Ent)
1072 then
1073 Set_Is_Known_Null (Ent, True);
1075 else
1076 Set_Is_Known_Null (Ent, False);
1078 if not Can_Never_Be_Null (Ent) then
1079 Set_Is_Known_Non_Null (Ent, False);
1080 end if;
1081 end if;
1083 -- For discrete types, we may be able to set the current value
1084 -- if the value is known at compile time.
1086 elsif Is_Discrete_Type (T1)
1087 and then Compile_Time_Known_Value (Rhs)
1088 then
1089 Set_Current_Value (Ent, Rhs);
1090 else
1091 Set_Current_Value (Ent, Empty);
1092 end if;
1094 -- If not safe to capture values, kill them
1096 else
1097 Kill_Lhs;
1098 end if;
1099 end;
1100 end if;
1102 -- If assigning to an object in whole or in part, note location of
1103 -- assignment in case no one references value. We only do this for
1104 -- source assignments, otherwise we can generate bogus warnings when an
1105 -- assignment is rewritten as another assignment, and gets tied up with
1106 -- itself.
1108 declare
1109 Ent : constant Entity_Id := Get_Enclosing_Object (Lhs);
1110 begin
1111 if Present (Ent)
1112 and then Safe_To_Capture_Value (N, Ent)
1113 and then Nkind (N) = N_Assignment_Statement
1114 and then Warn_On_Modified_Unread
1115 and then Is_Assignable (Ent)
1116 and then Comes_From_Source (N)
1117 and then In_Extended_Main_Source_Unit (Ent)
1118 then
1119 Set_Last_Assignment (Ent, Lhs);
1120 end if;
1121 end;
1123 Analyze_Dimension (N);
1125 <<Leave>>
1126 Restore_Ghost_Mode (Saved_GM);
1128 -- If the right-hand side contains target names, expansion has been
1129 -- disabled to prevent expansion that might move target names out of
1130 -- the context of the assignment statement. Restore the expander mode
1131 -- now so that assignment statement can be properly expanded.
1133 if Nkind (N) = N_Assignment_Statement then
1134 if Has_Target_Names (N) then
1135 Expander_Mode_Restore;
1136 Full_Analysis := Save_Full_Analysis;
1137 end if;
1139 pragma Assert (not Should_Transform_BIP_Assignment (Typ => T1));
1140 end if;
1141 end Analyze_Assignment;
1143 -----------------------------
1144 -- Analyze_Block_Statement --
1145 -----------------------------
1147 procedure Analyze_Block_Statement (N : Node_Id) is
1148 procedure Install_Return_Entities (Scop : Entity_Id);
1149 -- Install all entities of return statement scope Scop in the visibility
1150 -- chain except for the return object since its entity is reused in a
1151 -- renaming.
1153 -----------------------------
1154 -- Install_Return_Entities --
1155 -----------------------------
1157 procedure Install_Return_Entities (Scop : Entity_Id) is
1158 Id : Entity_Id;
1160 begin
1161 Id := First_Entity (Scop);
1162 while Present (Id) loop
1164 -- Do not install the return object
1166 if not Ekind_In (Id, E_Constant, E_Variable)
1167 or else not Is_Return_Object (Id)
1168 then
1169 Install_Entity (Id);
1170 end if;
1172 Next_Entity (Id);
1173 end loop;
1174 end Install_Return_Entities;
1176 -- Local constants and variables
1178 Decls : constant List_Id := Declarations (N);
1179 Id : constant Node_Id := Identifier (N);
1180 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1182 Is_BIP_Return_Statement : Boolean;
1184 -- Start of processing for Analyze_Block_Statement
1186 begin
1187 -- In SPARK mode, we reject block statements. Note that the case of
1188 -- block statements generated by the expander is fine.
1190 if Nkind (Original_Node (N)) = N_Block_Statement then
1191 Check_SPARK_05_Restriction ("block statement is not allowed", N);
1192 end if;
1194 -- If no handled statement sequence is present, things are really messed
1195 -- up, and we just return immediately (defence against previous errors).
1197 if No (HSS) then
1198 Check_Error_Detected;
1199 return;
1200 end if;
1202 -- Detect whether the block is actually a rewritten return statement of
1203 -- a build-in-place function.
1205 Is_BIP_Return_Statement :=
1206 Present (Id)
1207 and then Present (Entity (Id))
1208 and then Ekind (Entity (Id)) = E_Return_Statement
1209 and then Is_Build_In_Place_Function
1210 (Return_Applies_To (Entity (Id)));
1212 -- Normal processing with HSS present
1214 declare
1215 EH : constant List_Id := Exception_Handlers (HSS);
1216 Ent : Entity_Id := Empty;
1217 S : Entity_Id;
1219 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1220 -- Recursively save value of this global, will be restored on exit
1222 begin
1223 -- Initialize unblocked exit count for statements of begin block
1224 -- plus one for each exception handler that is present.
1226 Unblocked_Exit_Count := 1;
1228 if Present (EH) then
1229 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
1230 end if;
1232 -- If a label is present analyze it and mark it as referenced
1234 if Present (Id) then
1235 Analyze (Id);
1236 Ent := Entity (Id);
1238 -- An error defense. If we have an identifier, but no entity, then
1239 -- something is wrong. If previous errors, then just remove the
1240 -- identifier and continue, otherwise raise an exception.
1242 if No (Ent) then
1243 Check_Error_Detected;
1244 Set_Identifier (N, Empty);
1246 else
1247 Set_Ekind (Ent, E_Block);
1248 Generate_Reference (Ent, N, ' ');
1249 Generate_Definition (Ent);
1251 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1252 Set_Label_Construct (Parent (Ent), N);
1253 end if;
1254 end if;
1255 end if;
1257 -- If no entity set, create a label entity
1259 if No (Ent) then
1260 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
1261 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
1262 Set_Parent (Ent, N);
1263 end if;
1265 Set_Etype (Ent, Standard_Void_Type);
1266 Set_Block_Node (Ent, Identifier (N));
1267 Push_Scope (Ent);
1269 -- The block served as an extended return statement. Ensure that any
1270 -- entities created during the analysis and expansion of the return
1271 -- object declaration are once again visible.
1273 if Is_BIP_Return_Statement then
1274 Install_Return_Entities (Ent);
1275 end if;
1277 if Present (Decls) then
1278 Analyze_Declarations (Decls);
1279 Check_Completion;
1280 Inspect_Deferred_Constant_Completion (Decls);
1281 end if;
1283 Analyze (HSS);
1284 Process_End_Label (HSS, 'e', Ent);
1286 -- If exception handlers are present, then we indicate that enclosing
1287 -- scopes contain a block with handlers. We only need to mark non-
1288 -- generic scopes.
1290 if Present (EH) then
1291 S := Scope (Ent);
1292 loop
1293 Set_Has_Nested_Block_With_Handler (S);
1294 exit when Is_Overloadable (S)
1295 or else Ekind (S) = E_Package
1296 or else Is_Generic_Unit (S);
1297 S := Scope (S);
1298 end loop;
1299 end if;
1301 Check_References (Ent);
1302 Update_Use_Clause_Chain;
1303 End_Scope;
1305 if Unblocked_Exit_Count = 0 then
1306 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1307 Check_Unreachable_Code (N);
1308 else
1309 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1310 end if;
1311 end;
1312 end Analyze_Block_Statement;
1314 --------------------------------
1315 -- Analyze_Compound_Statement --
1316 --------------------------------
1318 procedure Analyze_Compound_Statement (N : Node_Id) is
1319 begin
1320 Analyze_List (Actions (N));
1321 end Analyze_Compound_Statement;
1323 ----------------------------
1324 -- Analyze_Case_Statement --
1325 ----------------------------
1327 procedure Analyze_Case_Statement (N : Node_Id) is
1328 Exp : Node_Id;
1329 Exp_Type : Entity_Id;
1330 Exp_Btype : Entity_Id;
1331 Last_Choice : Nat;
1333 Others_Present : Boolean;
1334 -- Indicates if Others was present
1336 pragma Warnings (Off, Last_Choice);
1337 -- Don't care about assigned value
1339 Statements_Analyzed : Boolean := False;
1340 -- Set True if at least some statement sequences get analyzed. If False
1341 -- on exit, means we had a serious error that prevented full analysis of
1342 -- the case statement, and as a result it is not a good idea to output
1343 -- warning messages about unreachable code.
1345 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1346 -- Recursively save value of this global, will be restored on exit
1348 procedure Non_Static_Choice_Error (Choice : Node_Id);
1349 -- Error routine invoked by the generic instantiation below when the
1350 -- case statement has a non static choice.
1352 procedure Process_Statements (Alternative : Node_Id);
1353 -- Analyzes the statements associated with a case alternative. Needed
1354 -- by instantiation below.
1356 package Analyze_Case_Choices is new
1357 Generic_Analyze_Choices
1358 (Process_Associated_Node => Process_Statements);
1359 use Analyze_Case_Choices;
1360 -- Instantiation of the generic choice analysis package
1362 package Check_Case_Choices is new
1363 Generic_Check_Choices
1364 (Process_Empty_Choice => No_OP,
1365 Process_Non_Static_Choice => Non_Static_Choice_Error,
1366 Process_Associated_Node => No_OP);
1367 use Check_Case_Choices;
1368 -- Instantiation of the generic choice processing package
1370 -----------------------------
1371 -- Non_Static_Choice_Error --
1372 -----------------------------
1374 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1375 begin
1376 Flag_Non_Static_Expr
1377 ("choice given in case statement is not static!", Choice);
1378 end Non_Static_Choice_Error;
1380 ------------------------
1381 -- Process_Statements --
1382 ------------------------
1384 procedure Process_Statements (Alternative : Node_Id) is
1385 Choices : constant List_Id := Discrete_Choices (Alternative);
1386 Ent : Entity_Id;
1388 begin
1389 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1390 Statements_Analyzed := True;
1392 -- An interesting optimization. If the case statement expression
1393 -- is a simple entity, then we can set the current value within an
1394 -- alternative if the alternative has one possible value.
1396 -- case N is
1397 -- when 1 => alpha
1398 -- when 2 | 3 => beta
1399 -- when others => gamma
1401 -- Here we know that N is initially 1 within alpha, but for beta and
1402 -- gamma, we do not know anything more about the initial value.
1404 if Is_Entity_Name (Exp) then
1405 Ent := Entity (Exp);
1407 if Ekind_In (Ent, E_Variable,
1408 E_In_Out_Parameter,
1409 E_Out_Parameter)
1410 then
1411 if List_Length (Choices) = 1
1412 and then Nkind (First (Choices)) in N_Subexpr
1413 and then Compile_Time_Known_Value (First (Choices))
1414 then
1415 Set_Current_Value (Entity (Exp), First (Choices));
1416 end if;
1418 Analyze_Statements (Statements (Alternative));
1420 -- After analyzing the case, set the current value to empty
1421 -- since we won't know what it is for the next alternative
1422 -- (unless reset by this same circuit), or after the case.
1424 Set_Current_Value (Entity (Exp), Empty);
1425 return;
1426 end if;
1427 end if;
1429 -- Case where expression is not an entity name of a variable
1431 Analyze_Statements (Statements (Alternative));
1432 end Process_Statements;
1434 -- Start of processing for Analyze_Case_Statement
1436 begin
1437 Unblocked_Exit_Count := 0;
1438 Exp := Expression (N);
1439 Analyze (Exp);
1441 -- The expression must be of any discrete type. In rare cases, the
1442 -- expander constructs a case statement whose expression has a private
1443 -- type whose full view is discrete. This can happen when generating
1444 -- a stream operation for a variant type after the type is frozen,
1445 -- when the partial of view of the type of the discriminant is private.
1446 -- In that case, use the full view to analyze case alternatives.
1448 if not Is_Overloaded (Exp)
1449 and then not Comes_From_Source (N)
1450 and then Is_Private_Type (Etype (Exp))
1451 and then Present (Full_View (Etype (Exp)))
1452 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1453 then
1454 Resolve (Exp, Etype (Exp));
1455 Exp_Type := Full_View (Etype (Exp));
1457 else
1458 Analyze_And_Resolve (Exp, Any_Discrete);
1459 Exp_Type := Etype (Exp);
1460 end if;
1462 Check_Unset_Reference (Exp);
1463 Exp_Btype := Base_Type (Exp_Type);
1465 -- The expression must be of a discrete type which must be determinable
1466 -- independently of the context in which the expression occurs, but
1467 -- using the fact that the expression must be of a discrete type.
1468 -- Moreover, the type this expression must not be a character literal
1469 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1471 -- If error already reported by Resolve, nothing more to do
1473 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1474 return;
1476 elsif Exp_Btype = Any_Character then
1477 Error_Msg_N
1478 ("character literal as case expression is ambiguous", Exp);
1479 return;
1481 elsif Ada_Version = Ada_83
1482 and then (Is_Generic_Type (Exp_Btype)
1483 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1484 then
1485 Error_Msg_N
1486 ("(Ada 83) case expression cannot be of a generic type", Exp);
1487 return;
1488 end if;
1490 -- If the case expression is a formal object of mode in out, then treat
1491 -- it as having a nonstatic subtype by forcing use of the base type
1492 -- (which has to get passed to Check_Case_Choices below). Also use base
1493 -- type when the case expression is parenthesized.
1495 if Paren_Count (Exp) > 0
1496 or else (Is_Entity_Name (Exp)
1497 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1498 then
1499 Exp_Type := Exp_Btype;
1500 end if;
1502 -- Call instantiated procedures to analyzwe and check discrete choices
1504 Analyze_Choices (Alternatives (N), Exp_Type);
1505 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1507 -- Case statement with single OTHERS alternative not allowed in SPARK
1509 if Others_Present and then List_Length (Alternatives (N)) = 1 then
1510 Check_SPARK_05_Restriction
1511 ("OTHERS as unique case alternative is not allowed", N);
1512 end if;
1514 if Exp_Type = Universal_Integer and then not Others_Present then
1515 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1516 end if;
1518 -- If all our exits were blocked by unconditional transfers of control,
1519 -- then the entire CASE statement acts as an unconditional transfer of
1520 -- control, so treat it like one, and check unreachable code. Skip this
1521 -- test if we had serious errors preventing any statement analysis.
1523 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1524 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1525 Check_Unreachable_Code (N);
1526 else
1527 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1528 end if;
1530 -- If the expander is active it will detect the case of a statically
1531 -- determined single alternative and remove warnings for the case, but
1532 -- if we are not doing expansion, that circuit won't be active. Here we
1533 -- duplicate the effect of removing warnings in the same way, so that
1534 -- we will get the same set of warnings in -gnatc mode.
1536 if not Expander_Active
1537 and then Compile_Time_Known_Value (Expression (N))
1538 and then Serious_Errors_Detected = 0
1539 then
1540 declare
1541 Chosen : constant Node_Id := Find_Static_Alternative (N);
1542 Alt : Node_Id;
1544 begin
1545 Alt := First (Alternatives (N));
1546 while Present (Alt) loop
1547 if Alt /= Chosen then
1548 Remove_Warning_Messages (Statements (Alt));
1549 end if;
1551 Next (Alt);
1552 end loop;
1553 end;
1554 end if;
1555 end Analyze_Case_Statement;
1557 ----------------------------
1558 -- Analyze_Exit_Statement --
1559 ----------------------------
1561 -- If the exit includes a name, it must be the name of a currently open
1562 -- loop. Otherwise there must be an innermost open loop on the stack, to
1563 -- which the statement implicitly refers.
1565 -- Additionally, in SPARK mode:
1567 -- The exit can only name the closest enclosing loop;
1569 -- An exit with a when clause must be directly contained in a loop;
1571 -- An exit without a when clause must be directly contained in an
1572 -- if-statement with no elsif or else, which is itself directly contained
1573 -- in a loop. The exit must be the last statement in the if-statement.
1575 procedure Analyze_Exit_Statement (N : Node_Id) is
1576 Target : constant Node_Id := Name (N);
1577 Cond : constant Node_Id := Condition (N);
1578 Scope_Id : Entity_Id := Empty; -- initialize to prevent warning
1579 U_Name : Entity_Id;
1580 Kind : Entity_Kind;
1582 begin
1583 if No (Cond) then
1584 Check_Unreachable_Code (N);
1585 end if;
1587 if Present (Target) then
1588 Analyze (Target);
1589 U_Name := Entity (Target);
1591 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1592 Error_Msg_N ("invalid loop name in exit statement", N);
1593 return;
1595 else
1596 if Has_Loop_In_Inner_Open_Scopes (U_Name) then
1597 Check_SPARK_05_Restriction
1598 ("exit label must name the closest enclosing loop", N);
1599 end if;
1601 Set_Has_Exit (U_Name);
1602 end if;
1604 else
1605 U_Name := Empty;
1606 end if;
1608 for J in reverse 0 .. Scope_Stack.Last loop
1609 Scope_Id := Scope_Stack.Table (J).Entity;
1610 Kind := Ekind (Scope_Id);
1612 if Kind = E_Loop and then (No (Target) or else Scope_Id = U_Name) then
1613 Set_Has_Exit (Scope_Id);
1614 exit;
1616 elsif Kind = E_Block
1617 or else Kind = E_Loop
1618 or else Kind = E_Return_Statement
1619 then
1620 null;
1622 else
1623 Error_Msg_N
1624 ("cannot exit from program unit or accept statement", N);
1625 return;
1626 end if;
1627 end loop;
1629 -- Verify that if present the condition is a Boolean expression
1631 if Present (Cond) then
1632 Analyze_And_Resolve (Cond, Any_Boolean);
1633 Check_Unset_Reference (Cond);
1634 end if;
1636 -- In SPARK mode, verify that the exit statement respects the SPARK
1637 -- restrictions.
1639 if Present (Cond) then
1640 if Nkind (Parent (N)) /= N_Loop_Statement then
1641 Check_SPARK_05_Restriction
1642 ("exit with when clause must be directly in loop", N);
1643 end if;
1645 else
1646 if Nkind (Parent (N)) /= N_If_Statement then
1647 if Nkind (Parent (N)) = N_Elsif_Part then
1648 Check_SPARK_05_Restriction
1649 ("exit must be in IF without ELSIF", N);
1650 else
1651 Check_SPARK_05_Restriction ("exit must be directly in IF", N);
1652 end if;
1654 elsif Nkind (Parent (Parent (N))) /= N_Loop_Statement then
1655 Check_SPARK_05_Restriction
1656 ("exit must be in IF directly in loop", N);
1658 -- First test the presence of ELSE, so that an exit in an ELSE leads
1659 -- to an error mentioning the ELSE.
1661 elsif Present (Else_Statements (Parent (N))) then
1662 Check_SPARK_05_Restriction ("exit must be in IF without ELSE", N);
1664 -- An exit in an ELSIF does not reach here, as it would have been
1665 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1667 elsif Present (Elsif_Parts (Parent (N))) then
1668 Check_SPARK_05_Restriction ("exit must be in IF without ELSIF", N);
1669 end if;
1670 end if;
1672 -- Chain exit statement to associated loop entity
1674 Set_Next_Exit_Statement (N, First_Exit_Statement (Scope_Id));
1675 Set_First_Exit_Statement (Scope_Id, N);
1677 -- Since the exit may take us out of a loop, any previous assignment
1678 -- statement is not useless, so clear last assignment indications. It
1679 -- is OK to keep other current values, since if the exit statement
1680 -- does not exit, then the current values are still valid.
1682 Kill_Current_Values (Last_Assignment_Only => True);
1683 end Analyze_Exit_Statement;
1685 ----------------------------
1686 -- Analyze_Goto_Statement --
1687 ----------------------------
1689 procedure Analyze_Goto_Statement (N : Node_Id) is
1690 Label : constant Node_Id := Name (N);
1691 Scope_Id : Entity_Id;
1692 Label_Scope : Entity_Id;
1693 Label_Ent : Entity_Id;
1695 begin
1696 Check_SPARK_05_Restriction ("goto statement is not allowed", N);
1698 -- Actual semantic checks
1700 Check_Unreachable_Code (N);
1701 Kill_Current_Values (Last_Assignment_Only => True);
1703 Analyze (Label);
1704 Label_Ent := Entity (Label);
1706 -- Ignore previous error
1708 if Label_Ent = Any_Id then
1709 Check_Error_Detected;
1710 return;
1712 -- We just have a label as the target of a goto
1714 elsif Ekind (Label_Ent) /= E_Label then
1715 Error_Msg_N ("target of goto statement must be a label", Label);
1716 return;
1718 -- Check that the target of the goto is reachable according to Ada
1719 -- scoping rules. Note: the special gotos we generate for optimizing
1720 -- local handling of exceptions would violate these rules, but we mark
1721 -- such gotos as analyzed when built, so this code is never entered.
1723 elsif not Reachable (Label_Ent) then
1724 Error_Msg_N ("target of goto statement is not reachable", Label);
1725 return;
1726 end if;
1728 -- Here if goto passes initial validity checks
1730 Label_Scope := Enclosing_Scope (Label_Ent);
1732 for J in reverse 0 .. Scope_Stack.Last loop
1733 Scope_Id := Scope_Stack.Table (J).Entity;
1735 if Label_Scope = Scope_Id
1736 or else not Ekind_In (Scope_Id, E_Block, E_Loop, E_Return_Statement)
1737 then
1738 if Scope_Id /= Label_Scope then
1739 Error_Msg_N
1740 ("cannot exit from program unit or accept statement", N);
1741 end if;
1743 return;
1744 end if;
1745 end loop;
1747 raise Program_Error;
1748 end Analyze_Goto_Statement;
1750 --------------------------
1751 -- Analyze_If_Statement --
1752 --------------------------
1754 -- A special complication arises in the analysis of if statements
1756 -- The expander has circuitry to completely delete code that it can tell
1757 -- will not be executed (as a result of compile time known conditions). In
1758 -- the analyzer, we ensure that code that will be deleted in this manner
1759 -- is analyzed but not expanded. This is obviously more efficient, but
1760 -- more significantly, difficulties arise if code is expanded and then
1761 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1762 -- generated in deleted code must be frozen from start, because the nodes
1763 -- on which they depend will not be available at the freeze point.
1765 procedure Analyze_If_Statement (N : Node_Id) is
1766 E : Node_Id;
1768 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1769 -- Recursively save value of this global, will be restored on exit
1771 Save_In_Deleted_Code : Boolean;
1773 Del : Boolean := False;
1774 -- This flag gets set True if a True condition has been found, which
1775 -- means that remaining ELSE/ELSIF parts are deleted.
1777 procedure Analyze_Cond_Then (Cnode : Node_Id);
1778 -- This is applied to either the N_If_Statement node itself or to an
1779 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1780 -- statements associated with it.
1782 -----------------------
1783 -- Analyze_Cond_Then --
1784 -----------------------
1786 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1787 Cond : constant Node_Id := Condition (Cnode);
1788 Tstm : constant List_Id := Then_Statements (Cnode);
1790 begin
1791 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1792 Analyze_And_Resolve (Cond, Any_Boolean);
1793 Check_Unset_Reference (Cond);
1794 Set_Current_Value_Condition (Cnode);
1796 -- If already deleting, then just analyze then statements
1798 if Del then
1799 Analyze_Statements (Tstm);
1801 -- Compile time known value, not deleting yet
1803 elsif Compile_Time_Known_Value (Cond) then
1804 Save_In_Deleted_Code := In_Deleted_Code;
1806 -- If condition is True, then analyze the THEN statements and set
1807 -- no expansion for ELSE and ELSIF parts.
1809 if Is_True (Expr_Value (Cond)) then
1810 Analyze_Statements (Tstm);
1811 Del := True;
1812 Expander_Mode_Save_And_Set (False);
1813 In_Deleted_Code := True;
1815 -- If condition is False, analyze THEN with expansion off
1817 else -- Is_False (Expr_Value (Cond))
1818 Expander_Mode_Save_And_Set (False);
1819 In_Deleted_Code := True;
1820 Analyze_Statements (Tstm);
1821 Expander_Mode_Restore;
1822 In_Deleted_Code := Save_In_Deleted_Code;
1823 end if;
1825 -- Not known at compile time, not deleting, normal analysis
1827 else
1828 Analyze_Statements (Tstm);
1829 end if;
1830 end Analyze_Cond_Then;
1832 -- Start of processing for Analyze_If_Statement
1834 begin
1835 -- Initialize exit count for else statements. If there is no else part,
1836 -- this count will stay non-zero reflecting the fact that the uncovered
1837 -- else case is an unblocked exit.
1839 Unblocked_Exit_Count := 1;
1840 Analyze_Cond_Then (N);
1842 -- Now to analyze the elsif parts if any are present
1844 if Present (Elsif_Parts (N)) then
1845 E := First (Elsif_Parts (N));
1846 while Present (E) loop
1847 Analyze_Cond_Then (E);
1848 Next (E);
1849 end loop;
1850 end if;
1852 if Present (Else_Statements (N)) then
1853 Analyze_Statements (Else_Statements (N));
1854 end if;
1856 -- If all our exits were blocked by unconditional transfers of control,
1857 -- then the entire IF statement acts as an unconditional transfer of
1858 -- control, so treat it like one, and check unreachable code.
1860 if Unblocked_Exit_Count = 0 then
1861 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1862 Check_Unreachable_Code (N);
1863 else
1864 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1865 end if;
1867 if Del then
1868 Expander_Mode_Restore;
1869 In_Deleted_Code := Save_In_Deleted_Code;
1870 end if;
1872 if not Expander_Active
1873 and then Compile_Time_Known_Value (Condition (N))
1874 and then Serious_Errors_Detected = 0
1875 then
1876 if Is_True (Expr_Value (Condition (N))) then
1877 Remove_Warning_Messages (Else_Statements (N));
1879 if Present (Elsif_Parts (N)) then
1880 E := First (Elsif_Parts (N));
1881 while Present (E) loop
1882 Remove_Warning_Messages (Then_Statements (E));
1883 Next (E);
1884 end loop;
1885 end if;
1887 else
1888 Remove_Warning_Messages (Then_Statements (N));
1889 end if;
1890 end if;
1892 -- Warn on redundant if statement that has no effect
1894 -- Note, we could also check empty ELSIF parts ???
1896 if Warn_On_Redundant_Constructs
1898 -- If statement must be from source
1900 and then Comes_From_Source (N)
1902 -- Condition must not have obvious side effect
1904 and then Has_No_Obvious_Side_Effects (Condition (N))
1906 -- No elsif parts of else part
1908 and then No (Elsif_Parts (N))
1909 and then No (Else_Statements (N))
1911 -- Then must be a single null statement
1913 and then List_Length (Then_Statements (N)) = 1
1914 then
1915 -- Go to original node, since we may have rewritten something as
1916 -- a null statement (e.g. a case we could figure the outcome of).
1918 declare
1919 T : constant Node_Id := First (Then_Statements (N));
1920 S : constant Node_Id := Original_Node (T);
1922 begin
1923 if Comes_From_Source (S) and then Nkind (S) = N_Null_Statement then
1924 Error_Msg_N ("if statement has no effect?r?", N);
1925 end if;
1926 end;
1927 end if;
1928 end Analyze_If_Statement;
1930 ----------------------------------------
1931 -- Analyze_Implicit_Label_Declaration --
1932 ----------------------------------------
1934 -- An implicit label declaration is generated in the innermost enclosing
1935 -- declarative part. This is done for labels, and block and loop names.
1937 -- Note: any changes in this routine may need to be reflected in
1938 -- Analyze_Label_Entity.
1940 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1941 Id : constant Node_Id := Defining_Identifier (N);
1942 begin
1943 Enter_Name (Id);
1944 Set_Ekind (Id, E_Label);
1945 Set_Etype (Id, Standard_Void_Type);
1946 Set_Enclosing_Scope (Id, Current_Scope);
1947 end Analyze_Implicit_Label_Declaration;
1949 ------------------------------
1950 -- Analyze_Iteration_Scheme --
1951 ------------------------------
1953 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1954 Cond : Node_Id;
1955 Iter_Spec : Node_Id;
1956 Loop_Spec : Node_Id;
1958 begin
1959 -- For an infinite loop, there is no iteration scheme
1961 if No (N) then
1962 return;
1963 end if;
1965 Cond := Condition (N);
1966 Iter_Spec := Iterator_Specification (N);
1967 Loop_Spec := Loop_Parameter_Specification (N);
1969 if Present (Cond) then
1970 Analyze_And_Resolve (Cond, Any_Boolean);
1971 Check_Unset_Reference (Cond);
1972 Set_Current_Value_Condition (N);
1974 elsif Present (Iter_Spec) then
1975 Analyze_Iterator_Specification (Iter_Spec);
1977 else
1978 Analyze_Loop_Parameter_Specification (Loop_Spec);
1979 end if;
1980 end Analyze_Iteration_Scheme;
1982 ------------------------------------
1983 -- Analyze_Iterator_Specification --
1984 ------------------------------------
1986 procedure Analyze_Iterator_Specification (N : Node_Id) is
1987 procedure Check_Reverse_Iteration (Typ : Entity_Id);
1988 -- For an iteration over a container, if the loop carries the Reverse
1989 -- indicator, verify that the container type has an Iterate aspect that
1990 -- implements the reversible iterator interface.
1992 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id;
1993 -- For containers with Iterator and related aspects, the cursor is
1994 -- obtained by locating an entity with the proper name in the scope
1995 -- of the type.
1997 -----------------------------
1998 -- Check_Reverse_Iteration --
1999 -----------------------------
2001 procedure Check_Reverse_Iteration (Typ : Entity_Id) is
2002 begin
2003 if Reverse_Present (N) then
2004 if Is_Array_Type (Typ)
2005 or else Is_Reversible_Iterator (Typ)
2006 or else
2007 (Present (Find_Aspect (Typ, Aspect_Iterable))
2008 and then
2009 Present
2010 (Get_Iterable_Type_Primitive (Typ, Name_Previous)))
2011 then
2012 null;
2013 else
2014 Error_Msg_NE
2015 ("container type does not support reverse iteration", N, Typ);
2016 end if;
2017 end if;
2018 end Check_Reverse_Iteration;
2020 ---------------------
2021 -- Get_Cursor_Type --
2022 ---------------------
2024 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
2025 Ent : Entity_Id;
2027 begin
2028 -- If iterator type is derived, the cursor is declared in the scope
2029 -- of the parent type.
2031 if Is_Derived_Type (Typ) then
2032 Ent := First_Entity (Scope (Etype (Typ)));
2033 else
2034 Ent := First_Entity (Scope (Typ));
2035 end if;
2037 while Present (Ent) loop
2038 exit when Chars (Ent) = Name_Cursor;
2039 Next_Entity (Ent);
2040 end loop;
2042 if No (Ent) then
2043 return Any_Type;
2044 end if;
2046 -- The cursor is the target of generated assignments in the
2047 -- loop, and cannot have a limited type.
2049 if Is_Limited_Type (Etype (Ent)) then
2050 Error_Msg_N ("cursor type cannot be limited", N);
2051 end if;
2053 return Etype (Ent);
2054 end Get_Cursor_Type;
2056 -- Local variables
2058 Def_Id : constant Node_Id := Defining_Identifier (N);
2059 Iter_Name : constant Node_Id := Name (N);
2060 Loc : constant Source_Ptr := Sloc (N);
2061 Subt : constant Node_Id := Subtype_Indication (N);
2063 Bas : Entity_Id := Empty; -- initialize to prevent warning
2064 Typ : Entity_Id;
2066 -- Start of processing for Analyze_Iterator_Specification
2068 begin
2069 Enter_Name (Def_Id);
2071 -- AI12-0151 specifies that when the subtype indication is present, it
2072 -- must statically match the type of the array or container element.
2073 -- To simplify this check, we introduce a subtype declaration with the
2074 -- given subtype indication when it carries a constraint, and rewrite
2075 -- the original as a reference to the created subtype entity.
2077 if Present (Subt) then
2078 if Nkind (Subt) = N_Subtype_Indication then
2079 declare
2080 S : constant Entity_Id := Make_Temporary (Sloc (Subt), 'S');
2081 Decl : constant Node_Id :=
2082 Make_Subtype_Declaration (Loc,
2083 Defining_Identifier => S,
2084 Subtype_Indication => New_Copy_Tree (Subt));
2085 begin
2086 Insert_Before (Parent (Parent (N)), Decl);
2087 Analyze (Decl);
2088 Rewrite (Subt, New_Occurrence_Of (S, Sloc (Subt)));
2089 end;
2090 else
2091 Analyze (Subt);
2092 end if;
2094 -- Save entity of subtype indication for subsequent check
2096 Bas := Entity (Subt);
2097 end if;
2099 Preanalyze_Range (Iter_Name);
2101 -- Set the kind of the loop variable, which is not visible within the
2102 -- iterator name.
2104 Set_Ekind (Def_Id, E_Variable);
2106 -- Provide a link between the iterator variable and the container, for
2107 -- subsequent use in cross-reference and modification information.
2109 if Of_Present (N) then
2110 Set_Related_Expression (Def_Id, Iter_Name);
2112 -- For a container, the iterator is specified through the aspect
2114 if not Is_Array_Type (Etype (Iter_Name)) then
2115 declare
2116 Iterator : constant Entity_Id :=
2117 Find_Value_Of_Aspect
2118 (Etype (Iter_Name), Aspect_Default_Iterator);
2120 I : Interp_Index;
2121 It : Interp;
2123 begin
2124 if No (Iterator) then
2125 null; -- error reported below
2127 elsif not Is_Overloaded (Iterator) then
2128 Check_Reverse_Iteration (Etype (Iterator));
2130 -- If Iterator is overloaded, use reversible iterator if one is
2131 -- available.
2133 elsif Is_Overloaded (Iterator) then
2134 Get_First_Interp (Iterator, I, It);
2135 while Present (It.Nam) loop
2136 if Ekind (It.Nam) = E_Function
2137 and then Is_Reversible_Iterator (Etype (It.Nam))
2138 then
2139 Set_Etype (Iterator, It.Typ);
2140 Set_Entity (Iterator, It.Nam);
2141 exit;
2142 end if;
2144 Get_Next_Interp (I, It);
2145 end loop;
2147 Check_Reverse_Iteration (Etype (Iterator));
2148 end if;
2149 end;
2150 end if;
2151 end if;
2153 -- If the domain of iteration is an expression, create a declaration for
2154 -- it, so that finalization actions are introduced outside of the loop.
2155 -- The declaration must be a renaming because the body of the loop may
2156 -- assign to elements.
2158 if not Is_Entity_Name (Iter_Name)
2160 -- When the context is a quantified expression, the renaming
2161 -- declaration is delayed until the expansion phase if we are
2162 -- doing expansion.
2164 and then (Nkind (Parent (N)) /= N_Quantified_Expression
2165 or else Operating_Mode = Check_Semantics)
2167 -- Do not perform this expansion for ASIS and when expansion is
2168 -- disabled, where the temporary may hide the transformation of a
2169 -- selected component into a prefixed function call, and references
2170 -- need to see the original expression.
2172 and then Expander_Active
2173 then
2174 declare
2175 Id : constant Entity_Id := Make_Temporary (Loc, 'R', Iter_Name);
2176 Decl : Node_Id;
2177 Act_S : Node_Id;
2179 begin
2181 -- If the domain of iteration is an array component that depends
2182 -- on a discriminant, create actual subtype for it. Pre-analysis
2183 -- does not generate the actual subtype of a selected component.
2185 if Nkind (Iter_Name) = N_Selected_Component
2186 and then Is_Array_Type (Etype (Iter_Name))
2187 then
2188 Act_S :=
2189 Build_Actual_Subtype_Of_Component
2190 (Etype (Selector_Name (Iter_Name)), Iter_Name);
2191 Insert_Action (N, Act_S);
2193 if Present (Act_S) then
2194 Typ := Defining_Identifier (Act_S);
2195 else
2196 Typ := Etype (Iter_Name);
2197 end if;
2199 else
2200 Typ := Etype (Iter_Name);
2202 -- Verify that the expression produces an iterator
2204 if not Of_Present (N) and then not Is_Iterator (Typ)
2205 and then not Is_Array_Type (Typ)
2206 and then No (Find_Aspect (Typ, Aspect_Iterable))
2207 then
2208 Error_Msg_N
2209 ("expect object that implements iterator interface",
2210 Iter_Name);
2211 end if;
2212 end if;
2214 -- Protect against malformed iterator
2216 if Typ = Any_Type then
2217 Error_Msg_N ("invalid expression in loop iterator", Iter_Name);
2218 return;
2219 end if;
2221 if not Of_Present (N) then
2222 Check_Reverse_Iteration (Typ);
2223 end if;
2225 -- The name in the renaming declaration may be a function call.
2226 -- Indicate that it does not come from source, to suppress
2227 -- spurious warnings on renamings of parameterless functions,
2228 -- a common enough idiom in user-defined iterators.
2230 Decl :=
2231 Make_Object_Renaming_Declaration (Loc,
2232 Defining_Identifier => Id,
2233 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
2234 Name =>
2235 New_Copy_Tree (Iter_Name, New_Sloc => Loc));
2237 Insert_Actions (Parent (Parent (N)), New_List (Decl));
2238 Rewrite (Name (N), New_Occurrence_Of (Id, Loc));
2239 Set_Etype (Id, Typ);
2240 Set_Etype (Name (N), Typ);
2241 end;
2243 -- Container is an entity or an array with uncontrolled components, or
2244 -- else it is a container iterator given by a function call, typically
2245 -- called Iterate in the case of predefined containers, even though
2246 -- Iterate is not a reserved name. What matters is that the return type
2247 -- of the function is an iterator type.
2249 elsif Is_Entity_Name (Iter_Name) then
2250 Analyze (Iter_Name);
2252 if Nkind (Iter_Name) = N_Function_Call then
2253 declare
2254 C : constant Node_Id := Name (Iter_Name);
2255 I : Interp_Index;
2256 It : Interp;
2258 begin
2259 if not Is_Overloaded (Iter_Name) then
2260 Resolve (Iter_Name, Etype (C));
2262 else
2263 Get_First_Interp (C, I, It);
2264 while It.Typ /= Empty loop
2265 if Reverse_Present (N) then
2266 if Is_Reversible_Iterator (It.Typ) then
2267 Resolve (Iter_Name, It.Typ);
2268 exit;
2269 end if;
2271 elsif Is_Iterator (It.Typ) then
2272 Resolve (Iter_Name, It.Typ);
2273 exit;
2274 end if;
2276 Get_Next_Interp (I, It);
2277 end loop;
2278 end if;
2279 end;
2281 -- Domain of iteration is not overloaded
2283 else
2284 Resolve (Iter_Name, Etype (Iter_Name));
2285 end if;
2287 if not Of_Present (N) then
2288 Check_Reverse_Iteration (Etype (Iter_Name));
2289 end if;
2290 end if;
2292 -- Get base type of container, for proper retrieval of Cursor type
2293 -- and primitive operations.
2295 Typ := Base_Type (Etype (Iter_Name));
2297 if Is_Array_Type (Typ) then
2298 if Of_Present (N) then
2299 Set_Etype (Def_Id, Component_Type (Typ));
2301 -- The loop variable is aliased if the array components are
2302 -- aliased.
2304 Set_Is_Aliased (Def_Id, Has_Aliased_Components (Typ));
2306 -- AI12-0047 stipulates that the domain (array or container)
2307 -- cannot be a component that depends on a discriminant if the
2308 -- enclosing object is mutable, to prevent a modification of the
2309 -- dowmain of iteration in the course of an iteration.
2311 -- If the object is an expression it has been captured in a
2312 -- temporary, so examine original node.
2314 if Nkind (Original_Node (Iter_Name)) = N_Selected_Component
2315 and then Is_Dependent_Component_Of_Mutable_Object
2316 (Original_Node (Iter_Name))
2317 then
2318 Error_Msg_N
2319 ("iterable name cannot be a discriminant-dependent "
2320 & "component of a mutable object", N);
2321 end if;
2323 if Present (Subt)
2324 and then
2325 (Base_Type (Bas) /= Base_Type (Component_Type (Typ))
2326 or else
2327 not Subtypes_Statically_Match (Bas, Component_Type (Typ)))
2328 then
2329 Error_Msg_N
2330 ("subtype indication does not match component type", Subt);
2331 end if;
2333 -- Here we have a missing Range attribute
2335 else
2336 Error_Msg_N
2337 ("missing Range attribute in iteration over an array", N);
2339 -- In Ada 2012 mode, this may be an attempt at an iterator
2341 if Ada_Version >= Ada_2012 then
2342 Error_Msg_NE
2343 ("\if& is meant to designate an element of the array, use OF",
2344 N, Def_Id);
2345 end if;
2347 -- Prevent cascaded errors
2349 Set_Ekind (Def_Id, E_Loop_Parameter);
2350 Set_Etype (Def_Id, Etype (First_Index (Typ)));
2351 end if;
2353 -- Check for type error in iterator
2355 elsif Typ = Any_Type then
2356 return;
2358 -- Iteration over a container
2360 else
2361 Set_Ekind (Def_Id, E_Loop_Parameter);
2362 Error_Msg_Ada_2012_Feature ("container iterator", Sloc (N));
2364 -- OF present
2366 if Of_Present (N) then
2367 if Has_Aspect (Typ, Aspect_Iterable) then
2368 declare
2369 Elt : constant Entity_Id :=
2370 Get_Iterable_Type_Primitive (Typ, Name_Element);
2371 begin
2372 if No (Elt) then
2373 Error_Msg_N
2374 ("missing Element primitive for iteration", N);
2375 else
2376 Set_Etype (Def_Id, Etype (Elt));
2377 Check_Reverse_Iteration (Typ);
2378 end if;
2379 end;
2381 -- For a predefined container, The type of the loop variable is
2382 -- the Iterator_Element aspect of the container type.
2384 else
2385 declare
2386 Element : constant Entity_Id :=
2387 Find_Value_Of_Aspect
2388 (Typ, Aspect_Iterator_Element);
2389 Iterator : constant Entity_Id :=
2390 Find_Value_Of_Aspect
2391 (Typ, Aspect_Default_Iterator);
2392 Orig_Iter_Name : constant Node_Id :=
2393 Original_Node (Iter_Name);
2394 Cursor_Type : Entity_Id;
2396 begin
2397 if No (Element) then
2398 Error_Msg_NE ("cannot iterate over&", N, Typ);
2399 return;
2401 else
2402 Set_Etype (Def_Id, Entity (Element));
2403 Cursor_Type := Get_Cursor_Type (Typ);
2404 pragma Assert (Present (Cursor_Type));
2406 -- If subtype indication was given, verify that it covers
2407 -- the element type of the container.
2409 if Present (Subt)
2410 and then (not Covers (Bas, Etype (Def_Id))
2411 or else not Subtypes_Statically_Match
2412 (Bas, Etype (Def_Id)))
2413 then
2414 Error_Msg_N
2415 ("subtype indication does not match element type",
2416 Subt);
2417 end if;
2419 -- If the container has a variable indexing aspect, the
2420 -- element is a variable and is modifiable in the loop.
2422 if Has_Aspect (Typ, Aspect_Variable_Indexing) then
2423 Set_Ekind (Def_Id, E_Variable);
2424 end if;
2426 -- If the container is a constant, iterating over it
2427 -- requires a Constant_Indexing operation.
2429 if not Is_Variable (Iter_Name)
2430 and then not Has_Aspect (Typ, Aspect_Constant_Indexing)
2431 then
2432 Error_Msg_N
2433 ("iteration over constant container require "
2434 & "constant_indexing aspect", N);
2436 -- The Iterate function may have an in_out parameter,
2437 -- and a constant container is thus illegal.
2439 elsif Present (Iterator)
2440 and then Ekind (Entity (Iterator)) = E_Function
2441 and then Ekind (First_Formal (Entity (Iterator))) /=
2442 E_In_Parameter
2443 and then not Is_Variable (Iter_Name)
2444 then
2445 Error_Msg_N ("variable container expected", N);
2446 end if;
2448 -- Detect a case where the iterator denotes a component
2449 -- of a mutable object which depends on a discriminant.
2450 -- Note that the iterator may denote a function call in
2451 -- qualified form, in which case this check should not
2452 -- be performed.
2454 if Nkind (Orig_Iter_Name) = N_Selected_Component
2455 and then
2456 Present (Entity (Selector_Name (Orig_Iter_Name)))
2457 and then Ekind_In
2458 (Entity (Selector_Name (Orig_Iter_Name)),
2459 E_Component,
2460 E_Discriminant)
2461 and then Is_Dependent_Component_Of_Mutable_Object
2462 (Orig_Iter_Name)
2463 then
2464 Error_Msg_N
2465 ("container cannot be a discriminant-dependent "
2466 & "component of a mutable object", N);
2467 end if;
2468 end if;
2469 end;
2470 end if;
2472 -- IN iterator, domain is a range, or a call to Iterate function
2474 else
2475 -- For an iteration of the form IN, the name must denote an
2476 -- iterator, typically the result of a call to Iterate. Give a
2477 -- useful error message when the name is a container by itself.
2479 -- The type may be a formal container type, which has to have
2480 -- an Iterable aspect detailing the required primitives.
2482 if Is_Entity_Name (Original_Node (Name (N)))
2483 and then not Is_Iterator (Typ)
2484 then
2485 if Has_Aspect (Typ, Aspect_Iterable) then
2486 null;
2488 elsif not Has_Aspect (Typ, Aspect_Iterator_Element) then
2489 Error_Msg_NE
2490 ("cannot iterate over&", Name (N), Typ);
2491 else
2492 Error_Msg_N
2493 ("name must be an iterator, not a container", Name (N));
2494 end if;
2496 if Has_Aspect (Typ, Aspect_Iterable) then
2497 null;
2498 else
2499 Error_Msg_NE
2500 ("\to iterate directly over the elements of a container, "
2501 & "write `of &`", Name (N), Original_Node (Name (N)));
2503 -- No point in continuing analysis of iterator spec
2505 return;
2506 end if;
2507 end if;
2509 -- If the name is a call (typically prefixed) to some Iterate
2510 -- function, it has been rewritten as an object declaration.
2511 -- If that object is a selected component, verify that it is not
2512 -- a component of an unconstrained mutable object.
2514 if Nkind (Iter_Name) = N_Identifier
2515 or else (not Expander_Active and Comes_From_Source (Iter_Name))
2516 then
2517 declare
2518 Orig_Node : constant Node_Id := Original_Node (Iter_Name);
2519 Iter_Kind : constant Node_Kind := Nkind (Orig_Node);
2520 Obj : Node_Id;
2522 begin
2523 if Iter_Kind = N_Selected_Component then
2524 Obj := Prefix (Orig_Node);
2526 elsif Iter_Kind = N_Function_Call then
2527 Obj := First_Actual (Orig_Node);
2529 -- If neither, the name comes from source
2531 else
2532 Obj := Iter_Name;
2533 end if;
2535 if Nkind (Obj) = N_Selected_Component
2536 and then Is_Dependent_Component_Of_Mutable_Object (Obj)
2537 then
2538 Error_Msg_N
2539 ("container cannot be a discriminant-dependent "
2540 & "component of a mutable object", N);
2541 end if;
2542 end;
2543 end if;
2545 -- The result type of Iterate function is the classwide type of
2546 -- the interface parent. We need the specific Cursor type defined
2547 -- in the container package. We obtain it by name for a predefined
2548 -- container, or through the Iterable aspect for a formal one.
2550 if Has_Aspect (Typ, Aspect_Iterable) then
2551 Set_Etype (Def_Id,
2552 Get_Cursor_Type
2553 (Parent (Find_Value_Of_Aspect (Typ, Aspect_Iterable)),
2554 Typ));
2556 else
2557 Set_Etype (Def_Id, Get_Cursor_Type (Typ));
2558 Check_Reverse_Iteration (Etype (Iter_Name));
2559 end if;
2561 end if;
2562 end if;
2563 end Analyze_Iterator_Specification;
2565 -------------------
2566 -- Analyze_Label --
2567 -------------------
2569 -- Note: the semantic work required for analyzing labels (setting them as
2570 -- reachable) was done in a prepass through the statements in the block,
2571 -- so that forward gotos would be properly handled. See Analyze_Statements
2572 -- for further details. The only processing required here is to deal with
2573 -- optimizations that depend on an assumption of sequential control flow,
2574 -- since of course the occurrence of a label breaks this assumption.
2576 procedure Analyze_Label (N : Node_Id) is
2577 pragma Warnings (Off, N);
2578 begin
2579 Kill_Current_Values;
2580 end Analyze_Label;
2582 --------------------------
2583 -- Analyze_Label_Entity --
2584 --------------------------
2586 procedure Analyze_Label_Entity (E : Entity_Id) is
2587 begin
2588 Set_Ekind (E, E_Label);
2589 Set_Etype (E, Standard_Void_Type);
2590 Set_Enclosing_Scope (E, Current_Scope);
2591 Set_Reachable (E, True);
2592 end Analyze_Label_Entity;
2594 ------------------------------------------
2595 -- Analyze_Loop_Parameter_Specification --
2596 ------------------------------------------
2598 procedure Analyze_Loop_Parameter_Specification (N : Node_Id) is
2599 Loop_Nod : constant Node_Id := Parent (Parent (N));
2601 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
2602 -- If the bounds are given by a 'Range reference on a function call
2603 -- that returns a controlled array, introduce an explicit declaration
2604 -- to capture the bounds, so that the function result can be finalized
2605 -- in timely fashion.
2607 procedure Check_Predicate_Use (T : Entity_Id);
2608 -- Diagnose Attempt to iterate through non-static predicate. Note that
2609 -- a type with inherited predicates may have both static and dynamic
2610 -- forms. In this case it is not sufficent to check the static predicate
2611 -- function only, look for a dynamic predicate aspect as well.
2613 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean;
2614 -- N is the node for an arbitrary construct. This function searches the
2615 -- construct N to see if any expressions within it contain function
2616 -- calls that use the secondary stack, returning True if any such call
2617 -- is found, and False otherwise.
2619 procedure Process_Bounds (R : Node_Id);
2620 -- If the iteration is given by a range, create temporaries and
2621 -- assignment statements block to capture the bounds and perform
2622 -- required finalization actions in case a bound includes a function
2623 -- call that uses the temporary stack. We first pre-analyze a copy of
2624 -- the range in order to determine the expected type, and analyze and
2625 -- resolve the original bounds.
2627 --------------------------------------
2628 -- Check_Controlled_Array_Attribute --
2629 --------------------------------------
2631 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
2632 begin
2633 if Nkind (DS) = N_Attribute_Reference
2634 and then Is_Entity_Name (Prefix (DS))
2635 and then Ekind (Entity (Prefix (DS))) = E_Function
2636 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
2637 and then
2638 Is_Controlled (Component_Type (Etype (Entity (Prefix (DS)))))
2639 and then Expander_Active
2640 then
2641 declare
2642 Loc : constant Source_Ptr := Sloc (N);
2643 Arr : constant Entity_Id := Etype (Entity (Prefix (DS)));
2644 Indx : constant Entity_Id :=
2645 Base_Type (Etype (First_Index (Arr)));
2646 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
2647 Decl : Node_Id;
2649 begin
2650 Decl :=
2651 Make_Subtype_Declaration (Loc,
2652 Defining_Identifier => Subt,
2653 Subtype_Indication =>
2654 Make_Subtype_Indication (Loc,
2655 Subtype_Mark => New_Occurrence_Of (Indx, Loc),
2656 Constraint =>
2657 Make_Range_Constraint (Loc, Relocate_Node (DS))));
2658 Insert_Before (Loop_Nod, Decl);
2659 Analyze (Decl);
2661 Rewrite (DS,
2662 Make_Attribute_Reference (Loc,
2663 Prefix => New_Occurrence_Of (Subt, Loc),
2664 Attribute_Name => Attribute_Name (DS)));
2666 Analyze (DS);
2667 end;
2668 end if;
2669 end Check_Controlled_Array_Attribute;
2671 -------------------------
2672 -- Check_Predicate_Use --
2673 -------------------------
2675 procedure Check_Predicate_Use (T : Entity_Id) is
2676 begin
2677 -- A predicated subtype is illegal in loops and related constructs
2678 -- if the predicate is not static, or if it is a non-static subtype
2679 -- of a statically predicated subtype.
2681 if Is_Discrete_Type (T)
2682 and then Has_Predicates (T)
2683 and then (not Has_Static_Predicate (T)
2684 or else not Is_Static_Subtype (T)
2685 or else Has_Dynamic_Predicate_Aspect (T))
2686 then
2687 -- Seems a confusing message for the case of a static predicate
2688 -- with a non-static subtype???
2690 Bad_Predicated_Subtype_Use
2691 ("cannot use subtype& with non-static predicate for loop "
2692 & "iteration", Discrete_Subtype_Definition (N),
2693 T, Suggest_Static => True);
2695 elsif Inside_A_Generic
2696 and then Is_Generic_Formal (T)
2697 and then Is_Discrete_Type (T)
2698 then
2699 Set_No_Dynamic_Predicate_On_Actual (T);
2700 end if;
2701 end Check_Predicate_Use;
2703 ------------------------------------
2704 -- Has_Call_Using_Secondary_Stack --
2705 ------------------------------------
2707 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean is
2709 function Check_Call (N : Node_Id) return Traverse_Result;
2710 -- Check if N is a function call which uses the secondary stack
2712 ----------------
2713 -- Check_Call --
2714 ----------------
2716 function Check_Call (N : Node_Id) return Traverse_Result is
2717 Nam : Node_Id;
2718 Subp : Entity_Id;
2719 Return_Typ : Entity_Id;
2721 begin
2722 if Nkind (N) = N_Function_Call then
2723 Nam := Name (N);
2725 -- Call using access to subprogram with explicit dereference
2727 if Nkind (Nam) = N_Explicit_Dereference then
2728 Subp := Etype (Nam);
2730 -- Call using a selected component notation or Ada 2005 object
2731 -- operation notation
2733 elsif Nkind (Nam) = N_Selected_Component then
2734 Subp := Entity (Selector_Name (Nam));
2736 -- Common case
2738 else
2739 Subp := Entity (Nam);
2740 end if;
2742 Return_Typ := Etype (Subp);
2744 if Is_Composite_Type (Return_Typ)
2745 and then not Is_Constrained (Return_Typ)
2746 then
2747 return Abandon;
2749 elsif Sec_Stack_Needed_For_Return (Subp) then
2750 return Abandon;
2751 end if;
2752 end if;
2754 -- Continue traversing the tree
2756 return OK;
2757 end Check_Call;
2759 function Check_Calls is new Traverse_Func (Check_Call);
2761 -- Start of processing for Has_Call_Using_Secondary_Stack
2763 begin
2764 return Check_Calls (N) = Abandon;
2765 end Has_Call_Using_Secondary_Stack;
2767 --------------------
2768 -- Process_Bounds --
2769 --------------------
2771 procedure Process_Bounds (R : Node_Id) is
2772 Loc : constant Source_Ptr := Sloc (N);
2774 function One_Bound
2775 (Original_Bound : Node_Id;
2776 Analyzed_Bound : Node_Id;
2777 Typ : Entity_Id) return Node_Id;
2778 -- Capture value of bound and return captured value
2780 ---------------
2781 -- One_Bound --
2782 ---------------
2784 function One_Bound
2785 (Original_Bound : Node_Id;
2786 Analyzed_Bound : Node_Id;
2787 Typ : Entity_Id) return Node_Id
2789 Assign : Node_Id;
2790 Decl : Node_Id;
2791 Id : Entity_Id;
2793 begin
2794 -- If the bound is a constant or an object, no need for a separate
2795 -- declaration. If the bound is the result of previous expansion
2796 -- it is already analyzed and should not be modified. Note that
2797 -- the Bound will be resolved later, if needed, as part of the
2798 -- call to Make_Index (literal bounds may need to be resolved to
2799 -- type Integer).
2801 if Analyzed (Original_Bound) then
2802 return Original_Bound;
2804 elsif Nkind_In (Analyzed_Bound, N_Integer_Literal,
2805 N_Character_Literal)
2806 or else Is_Entity_Name (Analyzed_Bound)
2807 then
2808 Analyze_And_Resolve (Original_Bound, Typ);
2809 return Original_Bound;
2810 end if;
2812 -- Normally, the best approach is simply to generate a constant
2813 -- declaration that captures the bound. However, there is a nasty
2814 -- case where this is wrong. If the bound is complex, and has a
2815 -- possible use of the secondary stack, we need to generate a
2816 -- separate assignment statement to ensure the creation of a block
2817 -- which will release the secondary stack.
2819 -- We prefer the constant declaration, since it leaves us with a
2820 -- proper trace of the value, useful in optimizations that get rid
2821 -- of junk range checks.
2823 if not Has_Call_Using_Secondary_Stack (Analyzed_Bound) then
2824 Analyze_And_Resolve (Original_Bound, Typ);
2826 -- Ensure that the bound is valid. This check should not be
2827 -- generated when the range belongs to a quantified expression
2828 -- as the construct is still not expanded into its final form.
2830 if Nkind (Parent (R)) /= N_Loop_Parameter_Specification
2831 or else Nkind (Parent (Parent (R))) /= N_Quantified_Expression
2832 then
2833 Ensure_Valid (Original_Bound);
2834 end if;
2836 Force_Evaluation (Original_Bound);
2837 return Original_Bound;
2838 end if;
2840 Id := Make_Temporary (Loc, 'R', Original_Bound);
2842 -- Here we make a declaration with a separate assignment
2843 -- statement, and insert before loop header.
2845 Decl :=
2846 Make_Object_Declaration (Loc,
2847 Defining_Identifier => Id,
2848 Object_Definition => New_Occurrence_Of (Typ, Loc));
2850 Assign :=
2851 Make_Assignment_Statement (Loc,
2852 Name => New_Occurrence_Of (Id, Loc),
2853 Expression => Relocate_Node (Original_Bound));
2855 Insert_Actions (Loop_Nod, New_List (Decl, Assign));
2857 -- Now that this temporary variable is initialized we decorate it
2858 -- as safe-to-reevaluate to inform to the backend that no further
2859 -- asignment will be issued and hence it can be handled as side
2860 -- effect free. Note that this decoration must be done when the
2861 -- assignment has been analyzed because otherwise it will be
2862 -- rejected (see Analyze_Assignment).
2864 Set_Is_Safe_To_Reevaluate (Id);
2866 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
2868 if Nkind (Assign) = N_Assignment_Statement then
2869 return Expression (Assign);
2870 else
2871 return Original_Bound;
2872 end if;
2873 end One_Bound;
2875 Hi : constant Node_Id := High_Bound (R);
2876 Lo : constant Node_Id := Low_Bound (R);
2877 R_Copy : constant Node_Id := New_Copy_Tree (R);
2878 New_Hi : Node_Id;
2879 New_Lo : Node_Id;
2880 Typ : Entity_Id;
2882 -- Start of processing for Process_Bounds
2884 begin
2885 Set_Parent (R_Copy, Parent (R));
2886 Preanalyze_Range (R_Copy);
2887 Typ := Etype (R_Copy);
2889 -- If the type of the discrete range is Universal_Integer, then the
2890 -- bound's type must be resolved to Integer, and any object used to
2891 -- hold the bound must also have type Integer, unless the literal
2892 -- bounds are constant-folded expressions with a user-defined type.
2894 if Typ = Universal_Integer then
2895 if Nkind (Lo) = N_Integer_Literal
2896 and then Present (Etype (Lo))
2897 and then Scope (Etype (Lo)) /= Standard_Standard
2898 then
2899 Typ := Etype (Lo);
2901 elsif Nkind (Hi) = N_Integer_Literal
2902 and then Present (Etype (Hi))
2903 and then Scope (Etype (Hi)) /= Standard_Standard
2904 then
2905 Typ := Etype (Hi);
2907 else
2908 Typ := Standard_Integer;
2909 end if;
2910 end if;
2912 Set_Etype (R, Typ);
2914 New_Lo := One_Bound (Lo, Low_Bound (R_Copy), Typ);
2915 New_Hi := One_Bound (Hi, High_Bound (R_Copy), Typ);
2917 -- Propagate staticness to loop range itself, in case the
2918 -- corresponding subtype is static.
2920 if New_Lo /= Lo and then Is_OK_Static_Expression (New_Lo) then
2921 Rewrite (Low_Bound (R), New_Copy (New_Lo));
2922 end if;
2924 if New_Hi /= Hi and then Is_OK_Static_Expression (New_Hi) then
2925 Rewrite (High_Bound (R), New_Copy (New_Hi));
2926 end if;
2927 end Process_Bounds;
2929 -- Local variables
2931 DS : constant Node_Id := Discrete_Subtype_Definition (N);
2932 Id : constant Entity_Id := Defining_Identifier (N);
2934 DS_Copy : Node_Id;
2936 -- Start of processing for Analyze_Loop_Parameter_Specification
2938 begin
2939 Enter_Name (Id);
2941 -- We always consider the loop variable to be referenced, since the loop
2942 -- may be used just for counting purposes.
2944 Generate_Reference (Id, N, ' ');
2946 -- Check for the case of loop variable hiding a local variable (used
2947 -- later on to give a nice warning if the hidden variable is never
2948 -- assigned).
2950 declare
2951 H : constant Entity_Id := Homonym (Id);
2952 begin
2953 if Present (H)
2954 and then Ekind (H) = E_Variable
2955 and then Is_Discrete_Type (Etype (H))
2956 and then Enclosing_Dynamic_Scope (H) = Enclosing_Dynamic_Scope (Id)
2957 then
2958 Set_Hiding_Loop_Variable (H, Id);
2959 end if;
2960 end;
2962 -- Loop parameter specification must include subtype mark in SPARK
2964 if Nkind (DS) = N_Range then
2965 Check_SPARK_05_Restriction
2966 ("loop parameter specification must include subtype mark", N);
2967 end if;
2969 -- Analyze the subtype definition and create temporaries for the bounds.
2970 -- Do not evaluate the range when preanalyzing a quantified expression
2971 -- because bounds expressed as function calls with side effects will be
2972 -- incorrectly replicated.
2974 if Nkind (DS) = N_Range
2975 and then Expander_Active
2976 and then Nkind (Parent (N)) /= N_Quantified_Expression
2977 then
2978 Process_Bounds (DS);
2980 -- Either the expander not active or the range of iteration is a subtype
2981 -- indication, an entity, or a function call that yields an aggregate or
2982 -- a container.
2984 else
2985 DS_Copy := New_Copy_Tree (DS);
2986 Set_Parent (DS_Copy, Parent (DS));
2987 Preanalyze_Range (DS_Copy);
2989 -- Ada 2012: If the domain of iteration is:
2991 -- a) a function call,
2992 -- b) an identifier that is not a type,
2993 -- c) an attribute reference 'Old (within a postcondition),
2994 -- d) an unchecked conversion or a qualified expression with
2995 -- the proper iterator type.
2997 -- then it is an iteration over a container. It was classified as
2998 -- a loop specification by the parser, and must be rewritten now
2999 -- to activate container iteration. The last case will occur within
3000 -- an expanded inlined call, where the expansion wraps an actual in
3001 -- an unchecked conversion when needed. The expression of the
3002 -- conversion is always an object.
3004 if Nkind (DS_Copy) = N_Function_Call
3006 or else (Is_Entity_Name (DS_Copy)
3007 and then not Is_Type (Entity (DS_Copy)))
3009 or else (Nkind (DS_Copy) = N_Attribute_Reference
3010 and then Nam_In (Attribute_Name (DS_Copy),
3011 Name_Loop_Entry, Name_Old))
3013 or else Has_Aspect (Etype (DS_Copy), Aspect_Iterable)
3015 or else Nkind (DS_Copy) = N_Unchecked_Type_Conversion
3016 or else (Nkind (DS_Copy) = N_Qualified_Expression
3017 and then Is_Iterator (Etype (DS_Copy)))
3018 then
3019 -- This is an iterator specification. Rewrite it as such and
3020 -- analyze it to capture function calls that may require
3021 -- finalization actions.
3023 declare
3024 I_Spec : constant Node_Id :=
3025 Make_Iterator_Specification (Sloc (N),
3026 Defining_Identifier => Relocate_Node (Id),
3027 Name => DS_Copy,
3028 Subtype_Indication => Empty,
3029 Reverse_Present => Reverse_Present (N));
3030 Scheme : constant Node_Id := Parent (N);
3032 begin
3033 Set_Iterator_Specification (Scheme, I_Spec);
3034 Set_Loop_Parameter_Specification (Scheme, Empty);
3035 Analyze_Iterator_Specification (I_Spec);
3037 -- In a generic context, analyze the original domain of
3038 -- iteration, for name capture.
3040 if not Expander_Active then
3041 Analyze (DS);
3042 end if;
3044 -- Set kind of loop parameter, which may be used in the
3045 -- subsequent analysis of the condition in a quantified
3046 -- expression.
3048 Set_Ekind (Id, E_Loop_Parameter);
3049 return;
3050 end;
3052 -- Domain of iteration is not a function call, and is side-effect
3053 -- free.
3055 else
3056 -- A quantified expression that appears in a pre/post condition
3057 -- is pre-analyzed several times. If the range is given by an
3058 -- attribute reference it is rewritten as a range, and this is
3059 -- done even with expansion disabled. If the type is already set
3060 -- do not reanalyze, because a range with static bounds may be
3061 -- typed Integer by default.
3063 if Nkind (Parent (N)) = N_Quantified_Expression
3064 and then Present (Etype (DS))
3065 then
3066 null;
3067 else
3068 Analyze (DS);
3069 end if;
3070 end if;
3071 end if;
3073 if DS = Error then
3074 return;
3075 end if;
3077 -- Some additional checks if we are iterating through a type
3079 if Is_Entity_Name (DS)
3080 and then Present (Entity (DS))
3081 and then Is_Type (Entity (DS))
3082 then
3083 -- The subtype indication may denote the completion of an incomplete
3084 -- type declaration.
3086 if Ekind (Entity (DS)) = E_Incomplete_Type then
3087 Set_Entity (DS, Get_Full_View (Entity (DS)));
3088 Set_Etype (DS, Entity (DS));
3089 end if;
3091 Check_Predicate_Use (Entity (DS));
3092 end if;
3094 -- Error if not discrete type
3096 if not Is_Discrete_Type (Etype (DS)) then
3097 Wrong_Type (DS, Any_Discrete);
3098 Set_Etype (DS, Any_Type);
3099 end if;
3101 Check_Controlled_Array_Attribute (DS);
3103 if Nkind (DS) = N_Subtype_Indication then
3104 Check_Predicate_Use (Entity (Subtype_Mark (DS)));
3105 end if;
3107 Make_Index (DS, N, In_Iter_Schm => True);
3108 Set_Ekind (Id, E_Loop_Parameter);
3110 -- A quantified expression which appears in a pre- or post-condition may
3111 -- be analyzed multiple times. The analysis of the range creates several
3112 -- itypes which reside in different scopes depending on whether the pre-
3113 -- or post-condition has been expanded. Update the type of the loop
3114 -- variable to reflect the proper itype at each stage of analysis.
3116 if No (Etype (Id))
3117 or else Etype (Id) = Any_Type
3118 or else
3119 (Present (Etype (Id))
3120 and then Is_Itype (Etype (Id))
3121 and then Nkind (Parent (Loop_Nod)) = N_Expression_With_Actions
3122 and then Nkind (Original_Node (Parent (Loop_Nod))) =
3123 N_Quantified_Expression)
3124 then
3125 Set_Etype (Id, Etype (DS));
3126 end if;
3128 -- Treat a range as an implicit reference to the type, to inhibit
3129 -- spurious warnings.
3131 Generate_Reference (Base_Type (Etype (DS)), N, ' ');
3132 Set_Is_Known_Valid (Id, True);
3134 -- The loop is not a declarative part, so the loop variable must be
3135 -- frozen explicitly. Do not freeze while preanalyzing a quantified
3136 -- expression because the freeze node will not be inserted into the
3137 -- tree due to flag Is_Spec_Expression being set.
3139 if Nkind (Parent (N)) /= N_Quantified_Expression then
3140 declare
3141 Flist : constant List_Id := Freeze_Entity (Id, N);
3142 begin
3143 if Is_Non_Empty_List (Flist) then
3144 Insert_Actions (N, Flist);
3145 end if;
3146 end;
3147 end if;
3149 -- Case where we have a range or a subtype, get type bounds
3151 if Nkind_In (DS, N_Range, N_Subtype_Indication)
3152 and then not Error_Posted (DS)
3153 and then Etype (DS) /= Any_Type
3154 and then Is_Discrete_Type (Etype (DS))
3155 then
3156 declare
3157 L : Node_Id;
3158 H : Node_Id;
3160 begin
3161 if Nkind (DS) = N_Range then
3162 L := Low_Bound (DS);
3163 H := High_Bound (DS);
3164 else
3165 L :=
3166 Type_Low_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
3167 H :=
3168 Type_High_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
3169 end if;
3171 -- Check for null or possibly null range and issue warning. We
3172 -- suppress such messages in generic templates and instances,
3173 -- because in practice they tend to be dubious in these cases. The
3174 -- check applies as well to rewritten array element loops where a
3175 -- null range may be detected statically.
3177 if Compile_Time_Compare (L, H, Assume_Valid => True) = GT then
3179 -- Suppress the warning if inside a generic template or
3180 -- instance, since in practice they tend to be dubious in these
3181 -- cases since they can result from intended parameterization.
3183 if not Inside_A_Generic and then not In_Instance then
3185 -- Specialize msg if invalid values could make the loop
3186 -- non-null after all.
3188 if Compile_Time_Compare
3189 (L, H, Assume_Valid => False) = GT
3190 then
3191 -- Since we know the range of the loop is null, set the
3192 -- appropriate flag to remove the loop entirely during
3193 -- expansion.
3195 Set_Is_Null_Loop (Loop_Nod);
3197 if Comes_From_Source (N) then
3198 Error_Msg_N
3199 ("??loop range is null, loop will not execute", DS);
3200 end if;
3202 -- Here is where the loop could execute because of
3203 -- invalid values, so issue appropriate message and in
3204 -- this case we do not set the Is_Null_Loop flag since
3205 -- the loop may execute.
3207 elsif Comes_From_Source (N) then
3208 Error_Msg_N
3209 ("??loop range may be null, loop may not execute",
3210 DS);
3211 Error_Msg_N
3212 ("??can only execute if invalid values are present",
3213 DS);
3214 end if;
3215 end if;
3217 -- In either case, suppress warnings in the body of the loop,
3218 -- since it is likely that these warnings will be inappropriate
3219 -- if the loop never actually executes, which is likely.
3221 Set_Suppress_Loop_Warnings (Loop_Nod);
3223 -- The other case for a warning is a reverse loop where the
3224 -- upper bound is the integer literal zero or one, and the
3225 -- lower bound may exceed this value.
3227 -- For example, we have
3229 -- for J in reverse N .. 1 loop
3231 -- In practice, this is very likely to be a case of reversing
3232 -- the bounds incorrectly in the range.
3234 elsif Reverse_Present (N)
3235 and then Nkind (Original_Node (H)) = N_Integer_Literal
3236 and then
3237 (Intval (Original_Node (H)) = Uint_0
3238 or else
3239 Intval (Original_Node (H)) = Uint_1)
3240 then
3241 -- Lower bound may in fact be known and known not to exceed
3242 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
3244 if Compile_Time_Known_Value (L)
3245 and then Expr_Value (L) <= Expr_Value (H)
3246 then
3247 null;
3249 -- Otherwise warning is warranted
3251 else
3252 Error_Msg_N ("??loop range may be null", DS);
3253 Error_Msg_N ("\??bounds may be wrong way round", DS);
3254 end if;
3255 end if;
3257 -- Check if either bound is known to be outside the range of the
3258 -- loop parameter type, this is e.g. the case of a loop from
3259 -- 20..X where the type is 1..19.
3261 -- Such a loop is dubious since either it raises CE or it executes
3262 -- zero times, and that cannot be useful!
3264 if Etype (DS) /= Any_Type
3265 and then not Error_Posted (DS)
3266 and then Nkind (DS) = N_Subtype_Indication
3267 and then Nkind (Constraint (DS)) = N_Range_Constraint
3268 then
3269 declare
3270 LLo : constant Node_Id :=
3271 Low_Bound (Range_Expression (Constraint (DS)));
3272 LHi : constant Node_Id :=
3273 High_Bound (Range_Expression (Constraint (DS)));
3275 Bad_Bound : Node_Id := Empty;
3276 -- Suspicious loop bound
3278 begin
3279 -- At this stage L, H are the bounds of the type, and LLo
3280 -- Lhi are the low bound and high bound of the loop.
3282 if Compile_Time_Compare (LLo, L, Assume_Valid => True) = LT
3283 or else
3284 Compile_Time_Compare (LLo, H, Assume_Valid => True) = GT
3285 then
3286 Bad_Bound := LLo;
3287 end if;
3289 if Compile_Time_Compare (LHi, L, Assume_Valid => True) = LT
3290 or else
3291 Compile_Time_Compare (LHi, H, Assume_Valid => True) = GT
3292 then
3293 Bad_Bound := LHi;
3294 end if;
3296 if Present (Bad_Bound) then
3297 Error_Msg_N
3298 ("suspicious loop bound out of range of "
3299 & "loop subtype??", Bad_Bound);
3300 Error_Msg_N
3301 ("\loop executes zero times or raises "
3302 & "Constraint_Error??", Bad_Bound);
3303 end if;
3304 end;
3305 end if;
3307 -- This declare block is about warnings, if we get an exception while
3308 -- testing for warnings, we simply abandon the attempt silently. This
3309 -- most likely occurs as the result of a previous error, but might
3310 -- just be an obscure case we have missed. In either case, not giving
3311 -- the warning is perfectly acceptable.
3313 exception
3314 when others => null;
3315 end;
3316 end if;
3318 -- A loop parameter cannot be effectively volatile (SPARK RM 7.1.3(4)).
3319 -- This check is relevant only when SPARK_Mode is on as it is not a
3320 -- standard Ada legality check.
3322 if SPARK_Mode = On and then Is_Effectively_Volatile (Id) then
3323 Error_Msg_N ("loop parameter cannot be volatile", Id);
3324 end if;
3325 end Analyze_Loop_Parameter_Specification;
3327 ----------------------------
3328 -- Analyze_Loop_Statement --
3329 ----------------------------
3331 procedure Analyze_Loop_Statement (N : Node_Id) is
3333 function Is_Container_Iterator (Iter : Node_Id) return Boolean;
3334 -- Given a loop iteration scheme, determine whether it is an Ada 2012
3335 -- container iteration.
3337 function Is_Wrapped_In_Block (N : Node_Id) return Boolean;
3338 -- Determine whether loop statement N has been wrapped in a block to
3339 -- capture finalization actions that may be generated for container
3340 -- iterators. Prevents infinite recursion when block is analyzed.
3341 -- Routine is a noop if loop is single statement within source block.
3343 ---------------------------
3344 -- Is_Container_Iterator --
3345 ---------------------------
3347 function Is_Container_Iterator (Iter : Node_Id) return Boolean is
3348 begin
3349 -- Infinite loop
3351 if No (Iter) then
3352 return False;
3354 -- While loop
3356 elsif Present (Condition (Iter)) then
3357 return False;
3359 -- for Def_Id in [reverse] Name loop
3360 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
3362 elsif Present (Iterator_Specification (Iter)) then
3363 declare
3364 Nam : constant Node_Id := Name (Iterator_Specification (Iter));
3365 Nam_Copy : Node_Id;
3367 begin
3368 Nam_Copy := New_Copy_Tree (Nam);
3369 Set_Parent (Nam_Copy, Parent (Nam));
3370 Preanalyze_Range (Nam_Copy);
3372 -- The only two options here are iteration over a container or
3373 -- an array.
3375 return not Is_Array_Type (Etype (Nam_Copy));
3376 end;
3378 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
3380 else
3381 declare
3382 LP : constant Node_Id := Loop_Parameter_Specification (Iter);
3383 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
3384 DS_Copy : Node_Id;
3386 begin
3387 DS_Copy := New_Copy_Tree (DS);
3388 Set_Parent (DS_Copy, Parent (DS));
3389 Preanalyze_Range (DS_Copy);
3391 -- Check for a call to Iterate () or an expression with
3392 -- an iterator type.
3394 return
3395 (Nkind (DS_Copy) = N_Function_Call
3396 and then Needs_Finalization (Etype (DS_Copy)))
3397 or else Is_Iterator (Etype (DS_Copy));
3398 end;
3399 end if;
3400 end Is_Container_Iterator;
3402 -------------------------
3403 -- Is_Wrapped_In_Block --
3404 -------------------------
3406 function Is_Wrapped_In_Block (N : Node_Id) return Boolean is
3407 HSS : Node_Id;
3408 Stat : Node_Id;
3410 begin
3412 -- Check if current scope is a block that is not a transient block.
3414 if Ekind (Current_Scope) /= E_Block
3415 or else No (Block_Node (Current_Scope))
3416 then
3417 return False;
3419 else
3420 HSS :=
3421 Handled_Statement_Sequence (Parent (Block_Node (Current_Scope)));
3423 -- Skip leading pragmas that may be introduced for invariant and
3424 -- predicate checks.
3426 Stat := First (Statements (HSS));
3427 while Present (Stat) and then Nkind (Stat) = N_Pragma loop
3428 Stat := Next (Stat);
3429 end loop;
3431 return Stat = N and then No (Next (Stat));
3432 end if;
3433 end Is_Wrapped_In_Block;
3435 -- Local declarations
3437 Id : constant Node_Id := Identifier (N);
3438 Iter : constant Node_Id := Iteration_Scheme (N);
3439 Loc : constant Source_Ptr := Sloc (N);
3440 Ent : Entity_Id;
3441 Stmt : Node_Id;
3443 -- Start of processing for Analyze_Loop_Statement
3445 begin
3446 if Present (Id) then
3448 -- Make name visible, e.g. for use in exit statements. Loop labels
3449 -- are always considered to be referenced.
3451 Analyze (Id);
3452 Ent := Entity (Id);
3454 -- Guard against serious error (typically, a scope mismatch when
3455 -- semantic analysis is requested) by creating loop entity to
3456 -- continue analysis.
3458 if No (Ent) then
3459 if Total_Errors_Detected /= 0 then
3460 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3461 else
3462 raise Program_Error;
3463 end if;
3465 -- Verify that the loop name is hot hidden by an unrelated
3466 -- declaration in an inner scope.
3468 elsif Ekind (Ent) /= E_Label and then Ekind (Ent) /= E_Loop then
3469 Error_Msg_Sloc := Sloc (Ent);
3470 Error_Msg_N ("implicit label declaration for & is hidden#", Id);
3472 if Present (Homonym (Ent))
3473 and then Ekind (Homonym (Ent)) = E_Label
3474 then
3475 Set_Entity (Id, Ent);
3476 Set_Ekind (Ent, E_Loop);
3477 end if;
3479 else
3480 Generate_Reference (Ent, N, ' ');
3481 Generate_Definition (Ent);
3483 -- If we found a label, mark its type. If not, ignore it, since it
3484 -- means we have a conflicting declaration, which would already
3485 -- have been diagnosed at declaration time. Set Label_Construct
3486 -- of the implicit label declaration, which is not created by the
3487 -- parser for generic units.
3489 if Ekind (Ent) = E_Label then
3490 Set_Ekind (Ent, E_Loop);
3492 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
3493 Set_Label_Construct (Parent (Ent), N);
3494 end if;
3495 end if;
3496 end if;
3498 -- Case of no identifier present. Create one and attach it to the
3499 -- loop statement for use as a scope and as a reference for later
3500 -- expansions. Indicate that the label does not come from source,
3501 -- and attach it to the loop statement so it is part of the tree,
3502 -- even without a full declaration.
3504 else
3505 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3506 Set_Etype (Ent, Standard_Void_Type);
3507 Set_Identifier (N, New_Occurrence_Of (Ent, Loc));
3508 Set_Parent (Ent, N);
3509 Set_Has_Created_Identifier (N);
3510 end if;
3512 -- If the iterator specification has a syntactic error, transform
3513 -- construct into an infinite loop to prevent a crash and perform
3514 -- some analysis.
3516 if Present (Iter)
3517 and then Present (Iterator_Specification (Iter))
3518 and then Error_Posted (Iterator_Specification (Iter))
3519 then
3520 Set_Iteration_Scheme (N, Empty);
3521 Analyze (N);
3522 return;
3523 end if;
3525 -- Iteration over a container in Ada 2012 involves the creation of a
3526 -- controlled iterator object. Wrap the loop in a block to ensure the
3527 -- timely finalization of the iterator and release of container locks.
3528 -- The same applies to the use of secondary stack when obtaining an
3529 -- iterator.
3531 if Ada_Version >= Ada_2012
3532 and then Is_Container_Iterator (Iter)
3533 and then not Is_Wrapped_In_Block (N)
3534 then
3535 declare
3536 Block_Nod : Node_Id;
3537 Block_Id : Entity_Id;
3539 begin
3540 Block_Nod :=
3541 Make_Block_Statement (Loc,
3542 Declarations => New_List,
3543 Handled_Statement_Sequence =>
3544 Make_Handled_Sequence_Of_Statements (Loc,
3545 Statements => New_List (Relocate_Node (N))));
3547 Add_Block_Identifier (Block_Nod, Block_Id);
3549 -- The expansion of iterator loops generates an iterator in order
3550 -- to traverse the elements of a container:
3552 -- Iter : <iterator type> := Iterate (Container)'reference;
3554 -- The iterator is controlled and returned on the secondary stack.
3555 -- The analysis of the call to Iterate establishes a transient
3556 -- scope to deal with the secondary stack management, but never
3557 -- really creates a physical block as this would kill the iterator
3558 -- too early (see Wrap_Transient_Declaration). To address this
3559 -- case, mark the generated block as needing secondary stack
3560 -- management.
3562 Set_Uses_Sec_Stack (Block_Id);
3564 Rewrite (N, Block_Nod);
3565 Analyze (N);
3566 return;
3567 end;
3568 end if;
3570 -- Kill current values on entry to loop, since statements in the body of
3571 -- the loop may have been executed before the loop is entered. Similarly
3572 -- we kill values after the loop, since we do not know that the body of
3573 -- the loop was executed.
3575 Kill_Current_Values;
3576 Push_Scope (Ent);
3577 Analyze_Iteration_Scheme (Iter);
3579 -- Check for following case which merits a warning if the type E of is
3580 -- a multi-dimensional array (and no explicit subscript ranges present).
3582 -- for J in E'Range
3583 -- for K in E'Range
3585 if Present (Iter)
3586 and then Present (Loop_Parameter_Specification (Iter))
3587 then
3588 declare
3589 LPS : constant Node_Id := Loop_Parameter_Specification (Iter);
3590 DSD : constant Node_Id :=
3591 Original_Node (Discrete_Subtype_Definition (LPS));
3592 begin
3593 if Nkind (DSD) = N_Attribute_Reference
3594 and then Attribute_Name (DSD) = Name_Range
3595 and then No (Expressions (DSD))
3596 then
3597 declare
3598 Typ : constant Entity_Id := Etype (Prefix (DSD));
3599 begin
3600 if Is_Array_Type (Typ)
3601 and then Number_Dimensions (Typ) > 1
3602 and then Nkind (Parent (N)) = N_Loop_Statement
3603 and then Present (Iteration_Scheme (Parent (N)))
3604 then
3605 declare
3606 OIter : constant Node_Id :=
3607 Iteration_Scheme (Parent (N));
3608 OLPS : constant Node_Id :=
3609 Loop_Parameter_Specification (OIter);
3610 ODSD : constant Node_Id :=
3611 Original_Node (Discrete_Subtype_Definition (OLPS));
3612 begin
3613 if Nkind (ODSD) = N_Attribute_Reference
3614 and then Attribute_Name (ODSD) = Name_Range
3615 and then No (Expressions (ODSD))
3616 and then Etype (Prefix (ODSD)) = Typ
3617 then
3618 Error_Msg_Sloc := Sloc (ODSD);
3619 Error_Msg_N
3620 ("inner range same as outer range#??", DSD);
3621 end if;
3622 end;
3623 end if;
3624 end;
3625 end if;
3626 end;
3627 end if;
3629 -- Analyze the statements of the body except in the case of an Ada 2012
3630 -- iterator with the expander active. In this case the expander will do
3631 -- a rewrite of the loop into a while loop. We will then analyze the
3632 -- loop body when we analyze this while loop.
3634 -- We need to do this delay because if the container is for indefinite
3635 -- types the actual subtype of the components will only be determined
3636 -- when the cursor declaration is analyzed.
3638 -- If the expander is not active then we want to analyze the loop body
3639 -- now even in the Ada 2012 iterator case, since the rewriting will not
3640 -- be done. Insert the loop variable in the current scope, if not done
3641 -- when analysing the iteration scheme. Set its kind properly to detect
3642 -- improper uses in the loop body.
3644 -- In GNATprove mode, we do one of the above depending on the kind of
3645 -- loop. If it is an iterator over an array, then we do not analyze the
3646 -- loop now. We will analyze it after it has been rewritten by the
3647 -- special SPARK expansion which is activated in GNATprove mode. We need
3648 -- to do this so that other expansions that should occur in GNATprove
3649 -- mode take into account the specificities of the rewritten loop, in
3650 -- particular the introduction of a renaming (which needs to be
3651 -- expanded).
3653 -- In other cases in GNATprove mode then we want to analyze the loop
3654 -- body now, since no rewriting will occur. Within a generic the
3655 -- GNATprove mode is irrelevant, we must analyze the generic for
3656 -- non-local name capture.
3658 if Present (Iter)
3659 and then Present (Iterator_Specification (Iter))
3660 then
3661 if GNATprove_Mode
3662 and then Is_Iterator_Over_Array (Iterator_Specification (Iter))
3663 and then not Inside_A_Generic
3664 then
3665 null;
3667 elsif not Expander_Active then
3668 declare
3669 I_Spec : constant Node_Id := Iterator_Specification (Iter);
3670 Id : constant Entity_Id := Defining_Identifier (I_Spec);
3672 begin
3673 if Scope (Id) /= Current_Scope then
3674 Enter_Name (Id);
3675 end if;
3677 -- In an element iterator, The loop parameter is a variable if
3678 -- the domain of iteration (container or array) is a variable.
3680 if not Of_Present (I_Spec)
3681 or else not Is_Variable (Name (I_Spec))
3682 then
3683 Set_Ekind (Id, E_Loop_Parameter);
3684 end if;
3685 end;
3687 Analyze_Statements (Statements (N));
3688 end if;
3690 else
3691 -- Pre-Ada2012 for-loops and while loops
3693 Analyze_Statements (Statements (N));
3694 end if;
3696 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
3697 -- the loop is transformed into a conditional block. Retrieve the loop.
3699 Stmt := N;
3701 if Subject_To_Loop_Entry_Attributes (Stmt) then
3702 Stmt := Find_Loop_In_Conditional_Block (Stmt);
3703 end if;
3705 -- Finish up processing for the loop. We kill all current values, since
3706 -- in general we don't know if the statements in the loop have been
3707 -- executed. We could do a bit better than this with a loop that we
3708 -- know will execute at least once, but it's not worth the trouble and
3709 -- the front end is not in the business of flow tracing.
3711 Process_End_Label (Stmt, 'e', Ent);
3712 End_Scope;
3713 Kill_Current_Values;
3715 -- Check for infinite loop. Skip check for generated code, since it
3716 -- justs waste time and makes debugging the routine called harder.
3718 -- Note that we have to wait till the body of the loop is fully analyzed
3719 -- before making this call, since Check_Infinite_Loop_Warning relies on
3720 -- being able to use semantic visibility information to find references.
3722 if Comes_From_Source (Stmt) then
3723 Check_Infinite_Loop_Warning (Stmt);
3724 end if;
3726 -- Code after loop is unreachable if the loop has no WHILE or FOR and
3727 -- contains no EXIT statements within the body of the loop.
3729 if No (Iter) and then not Has_Exit (Ent) then
3730 Check_Unreachable_Code (Stmt);
3731 end if;
3732 end Analyze_Loop_Statement;
3734 ----------------------------
3735 -- Analyze_Null_Statement --
3736 ----------------------------
3738 -- Note: the semantics of the null statement is implemented by a single
3739 -- null statement, too bad everything isn't as simple as this.
3741 procedure Analyze_Null_Statement (N : Node_Id) is
3742 pragma Warnings (Off, N);
3743 begin
3744 null;
3745 end Analyze_Null_Statement;
3747 -------------------------
3748 -- Analyze_Target_Name --
3749 -------------------------
3751 procedure Analyze_Target_Name (N : Node_Id) is
3752 begin
3753 -- A target name has the type of the left-hand side of the enclosing
3754 -- assignment.
3756 Set_Etype (N, Etype (Name (Current_Assignment)));
3757 end Analyze_Target_Name;
3759 ------------------------
3760 -- Analyze_Statements --
3761 ------------------------
3763 procedure Analyze_Statements (L : List_Id) is
3764 Lab : Entity_Id;
3765 S : Node_Id;
3767 begin
3768 -- The labels declared in the statement list are reachable from
3769 -- statements in the list. We do this as a prepass so that any goto
3770 -- statement will be properly flagged if its target is not reachable.
3771 -- This is not required, but is nice behavior.
3773 S := First (L);
3774 while Present (S) loop
3775 if Nkind (S) = N_Label then
3776 Analyze (Identifier (S));
3777 Lab := Entity (Identifier (S));
3779 -- If we found a label mark it as reachable
3781 if Ekind (Lab) = E_Label then
3782 Generate_Definition (Lab);
3783 Set_Reachable (Lab);
3785 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
3786 Set_Label_Construct (Parent (Lab), S);
3787 end if;
3789 -- If we failed to find a label, it means the implicit declaration
3790 -- of the label was hidden. A for-loop parameter can do this to
3791 -- a label with the same name inside the loop, since the implicit
3792 -- label declaration is in the innermost enclosing body or block
3793 -- statement.
3795 else
3796 Error_Msg_Sloc := Sloc (Lab);
3797 Error_Msg_N
3798 ("implicit label declaration for & is hidden#",
3799 Identifier (S));
3800 end if;
3801 end if;
3803 Next (S);
3804 end loop;
3806 -- Perform semantic analysis on all statements
3808 Conditional_Statements_Begin;
3810 S := First (L);
3811 while Present (S) loop
3812 Analyze (S);
3814 -- Remove dimension in all statements
3816 Remove_Dimension_In_Statement (S);
3817 Next (S);
3818 end loop;
3820 Conditional_Statements_End;
3822 -- Make labels unreachable. Visibility is not sufficient, because labels
3823 -- in one if-branch for example are not reachable from the other branch,
3824 -- even though their declarations are in the enclosing declarative part.
3826 S := First (L);
3827 while Present (S) loop
3828 if Nkind (S) = N_Label then
3829 Set_Reachable (Entity (Identifier (S)), False);
3830 end if;
3832 Next (S);
3833 end loop;
3834 end Analyze_Statements;
3836 ----------------------------
3837 -- Check_Unreachable_Code --
3838 ----------------------------
3840 procedure Check_Unreachable_Code (N : Node_Id) is
3841 Error_Node : Node_Id;
3842 P : Node_Id;
3844 begin
3845 if Is_List_Member (N) and then Comes_From_Source (N) then
3846 declare
3847 Nxt : Node_Id;
3849 begin
3850 Nxt := Original_Node (Next (N));
3852 -- Skip past pragmas
3854 while Nkind (Nxt) = N_Pragma loop
3855 Nxt := Original_Node (Next (Nxt));
3856 end loop;
3858 -- If a label follows us, then we never have dead code, since
3859 -- someone could branch to the label, so we just ignore it, unless
3860 -- we are in formal mode where goto statements are not allowed.
3862 if Nkind (Nxt) = N_Label
3863 and then not Restriction_Check_Required (SPARK_05)
3864 then
3865 return;
3867 -- Otherwise see if we have a real statement following us
3869 elsif Present (Nxt)
3870 and then Comes_From_Source (Nxt)
3871 and then Is_Statement (Nxt)
3872 then
3873 -- Special very annoying exception. If we have a return that
3874 -- follows a raise, then we allow it without a warning, since
3875 -- the Ada RM annoyingly requires a useless return here.
3877 if Nkind (Original_Node (N)) /= N_Raise_Statement
3878 or else Nkind (Nxt) /= N_Simple_Return_Statement
3879 then
3880 -- The rather strange shenanigans with the warning message
3881 -- here reflects the fact that Kill_Dead_Code is very good
3882 -- at removing warnings in deleted code, and this is one
3883 -- warning we would prefer NOT to have removed.
3885 Error_Node := Nxt;
3887 -- If we have unreachable code, analyze and remove the
3888 -- unreachable code, since it is useless and we don't
3889 -- want to generate junk warnings.
3891 -- We skip this step if we are not in code generation mode
3892 -- or CodePeer mode.
3894 -- This is the one case where we remove dead code in the
3895 -- semantics as opposed to the expander, and we do not want
3896 -- to remove code if we are not in code generation mode,
3897 -- since this messes up the ASIS trees or loses useful
3898 -- information in the CodePeer tree.
3900 -- Note that one might react by moving the whole circuit to
3901 -- exp_ch5, but then we lose the warning in -gnatc mode.
3903 if Operating_Mode = Generate_Code
3904 and then not CodePeer_Mode
3905 then
3906 loop
3907 Nxt := Next (N);
3909 -- Quit deleting when we have nothing more to delete
3910 -- or if we hit a label (since someone could transfer
3911 -- control to a label, so we should not delete it).
3913 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
3915 -- Statement/declaration is to be deleted
3917 Analyze (Nxt);
3918 Remove (Nxt);
3919 Kill_Dead_Code (Nxt);
3920 end loop;
3921 end if;
3923 -- Now issue the warning (or error in formal mode)
3925 if Restriction_Check_Required (SPARK_05) then
3926 Check_SPARK_05_Restriction
3927 ("unreachable code is not allowed", Error_Node);
3928 else
3929 Error_Msg
3930 ("??unreachable code!", Sloc (Error_Node), Error_Node);
3931 end if;
3932 end if;
3934 -- If the unconditional transfer of control instruction is the
3935 -- last statement of a sequence, then see if our parent is one of
3936 -- the constructs for which we count unblocked exits, and if so,
3937 -- adjust the count.
3939 else
3940 P := Parent (N);
3942 -- Statements in THEN part or ELSE part of IF statement
3944 if Nkind (P) = N_If_Statement then
3945 null;
3947 -- Statements in ELSIF part of an IF statement
3949 elsif Nkind (P) = N_Elsif_Part then
3950 P := Parent (P);
3951 pragma Assert (Nkind (P) = N_If_Statement);
3953 -- Statements in CASE statement alternative
3955 elsif Nkind (P) = N_Case_Statement_Alternative then
3956 P := Parent (P);
3957 pragma Assert (Nkind (P) = N_Case_Statement);
3959 -- Statements in body of block
3961 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
3962 and then Nkind (Parent (P)) = N_Block_Statement
3963 then
3964 -- The original loop is now placed inside a block statement
3965 -- due to the expansion of attribute 'Loop_Entry. Return as
3966 -- this is not a "real" block for the purposes of exit
3967 -- counting.
3969 if Nkind (N) = N_Loop_Statement
3970 and then Subject_To_Loop_Entry_Attributes (N)
3971 then
3972 return;
3973 end if;
3975 -- Statements in exception handler in a block
3977 elsif Nkind (P) = N_Exception_Handler
3978 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
3979 and then Nkind (Parent (Parent (P))) = N_Block_Statement
3980 then
3981 null;
3983 -- None of these cases, so return
3985 else
3986 return;
3987 end if;
3989 -- This was one of the cases we are looking for (i.e. the
3990 -- parent construct was IF, CASE or block) so decrement count.
3992 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
3993 end if;
3994 end;
3995 end if;
3996 end Check_Unreachable_Code;
3998 ----------------------
3999 -- Preanalyze_Range --
4000 ----------------------
4002 procedure Preanalyze_Range (R_Copy : Node_Id) is
4003 Save_Analysis : constant Boolean := Full_Analysis;
4004 Typ : Entity_Id;
4006 begin
4007 Full_Analysis := False;
4008 Expander_Mode_Save_And_Set (False);
4010 Analyze (R_Copy);
4012 if Nkind (R_Copy) in N_Subexpr and then Is_Overloaded (R_Copy) then
4014 -- Apply preference rules for range of predefined integer types, or
4015 -- check for array or iterable construct for "of" iterator, or
4016 -- diagnose true ambiguity.
4018 declare
4019 I : Interp_Index;
4020 It : Interp;
4021 Found : Entity_Id := Empty;
4023 begin
4024 Get_First_Interp (R_Copy, I, It);
4025 while Present (It.Typ) loop
4026 if Is_Discrete_Type (It.Typ) then
4027 if No (Found) then
4028 Found := It.Typ;
4029 else
4030 if Scope (Found) = Standard_Standard then
4031 null;
4033 elsif Scope (It.Typ) = Standard_Standard then
4034 Found := It.Typ;
4036 else
4037 -- Both of them are user-defined
4039 Error_Msg_N
4040 ("ambiguous bounds in range of iteration", R_Copy);
4041 Error_Msg_N ("\possible interpretations:", R_Copy);
4042 Error_Msg_NE ("\\} ", R_Copy, Found);
4043 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
4044 exit;
4045 end if;
4046 end if;
4048 elsif Nkind (Parent (R_Copy)) = N_Iterator_Specification
4049 and then Of_Present (Parent (R_Copy))
4050 then
4051 if Is_Array_Type (It.Typ)
4052 or else Has_Aspect (It.Typ, Aspect_Iterator_Element)
4053 or else Has_Aspect (It.Typ, Aspect_Constant_Indexing)
4054 or else Has_Aspect (It.Typ, Aspect_Variable_Indexing)
4055 then
4056 if No (Found) then
4057 Found := It.Typ;
4058 Set_Etype (R_Copy, It.Typ);
4060 else
4061 Error_Msg_N ("ambiguous domain of iteration", R_Copy);
4062 end if;
4063 end if;
4064 end if;
4066 Get_Next_Interp (I, It);
4067 end loop;
4068 end;
4069 end if;
4071 -- Subtype mark in iteration scheme
4073 if Is_Entity_Name (R_Copy) and then Is_Type (Entity (R_Copy)) then
4074 null;
4076 -- Expression in range, or Ada 2012 iterator
4078 elsif Nkind (R_Copy) in N_Subexpr then
4079 Resolve (R_Copy);
4080 Typ := Etype (R_Copy);
4082 if Is_Discrete_Type (Typ) then
4083 null;
4085 -- Check that the resulting object is an iterable container
4087 elsif Has_Aspect (Typ, Aspect_Iterator_Element)
4088 or else Has_Aspect (Typ, Aspect_Constant_Indexing)
4089 or else Has_Aspect (Typ, Aspect_Variable_Indexing)
4090 then
4091 null;
4093 -- The expression may yield an implicit reference to an iterable
4094 -- container. Insert explicit dereference so that proper type is
4095 -- visible in the loop.
4097 elsif Has_Implicit_Dereference (Etype (R_Copy)) then
4098 declare
4099 Disc : Entity_Id;
4101 begin
4102 Disc := First_Discriminant (Typ);
4103 while Present (Disc) loop
4104 if Has_Implicit_Dereference (Disc) then
4105 Build_Explicit_Dereference (R_Copy, Disc);
4106 exit;
4107 end if;
4109 Next_Discriminant (Disc);
4110 end loop;
4111 end;
4113 end if;
4114 end if;
4116 Expander_Mode_Restore;
4117 Full_Analysis := Save_Analysis;
4118 end Preanalyze_Range;
4120 end Sem_Ch5;