Make vect_model_store_cost take a vec_load_store_type
[official-gcc.git] / gcc / ada / sem_ch5.adb
blob205f414c290dfba5270241df79b023d7b4e80e22
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
556 -- In certain cases involving user-defined concatenation operators,
557 -- we need to resolve the right-hand side before transforming the
558 -- assignment.
560 case Nkind (Unqual_Conv (Rhs)) is
561 when N_Function_Call =>
562 declare
563 Actual : Node_Id :=
564 First (Parameter_Associations (Unqual_Conv (Rhs)));
565 Actual_Exp : Node_Id;
567 begin
568 while Present (Actual) loop
569 if Nkind (Actual) = N_Parameter_Association then
570 Actual_Exp := Explicit_Actual_Parameter (Actual);
571 else
572 Actual_Exp := Actual;
573 end if;
575 if Nkind (Actual_Exp) = N_Op_Concat then
576 Resolve (Rhs, T1);
577 exit;
578 end if;
580 Next (Actual);
581 end loop;
582 end;
584 when N_Attribute_Reference
585 | N_Expanded_Name
586 | N_Identifier
587 | N_Op
589 null;
591 when others =>
592 raise Program_Error;
593 end case;
595 Transform_BIP_Assignment (Typ => T1);
596 end if;
598 pragma Assert (not Should_Transform_BIP_Assignment (Typ => T1));
600 -- The resulting assignment type is T1, so now we will resolve the left
601 -- hand side of the assignment using this determined type.
603 Resolve (Lhs, T1);
605 -- Cases where Lhs is not a variable. In an instance or an inlined body
606 -- no need for further check because assignment was legal in template.
608 if In_Inlined_Body then
609 null;
611 elsif not Is_Variable (Lhs) then
613 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
614 -- protected object.
616 declare
617 Ent : Entity_Id;
618 S : Entity_Id;
620 begin
621 if Ada_Version >= Ada_2005 then
623 -- Handle chains of renamings
625 Ent := Lhs;
626 while Nkind (Ent) in N_Has_Entity
627 and then Present (Entity (Ent))
628 and then Present (Renamed_Object (Entity (Ent)))
629 loop
630 Ent := Renamed_Object (Entity (Ent));
631 end loop;
633 if (Nkind (Ent) = N_Attribute_Reference
634 and then Attribute_Name (Ent) = Name_Priority)
636 -- Renamings of the attribute Priority applied to protected
637 -- objects have been previously expanded into calls to the
638 -- Get_Ceiling run-time subprogram.
640 or else Is_Expanded_Priority_Attribute (Ent)
641 then
642 -- The enclosing subprogram cannot be a protected function
644 S := Current_Scope;
645 while not (Is_Subprogram (S)
646 and then Convention (S) = Convention_Protected)
647 and then S /= Standard_Standard
648 loop
649 S := Scope (S);
650 end loop;
652 if Ekind (S) = E_Function
653 and then Convention (S) = Convention_Protected
654 then
655 Error_Msg_N
656 ("protected function cannot modify protected object",
657 Lhs);
658 end if;
660 -- Changes of the ceiling priority of the protected object
661 -- are only effective if the Ceiling_Locking policy is in
662 -- effect (AARM D.5.2 (5/2)).
664 if Locking_Policy /= 'C' then
665 Error_Msg_N
666 ("assignment to the attribute PRIORITY has no effect??",
667 Lhs);
668 Error_Msg_N
669 ("\since no Locking_Policy has been specified??", Lhs);
670 end if;
672 goto Leave;
673 end if;
674 end if;
675 end;
677 Diagnose_Non_Variable_Lhs (Lhs);
678 goto Leave;
680 -- Error of assigning to limited type. We do however allow this in
681 -- certain cases where the front end generates the assignments.
683 elsif Is_Limited_Type (T1)
684 and then not Assignment_OK (Lhs)
685 and then not Assignment_OK (Original_Node (Lhs))
686 then
687 -- CPP constructors can only be called in declarations
689 if Is_CPP_Constructor_Call (Rhs) then
690 Error_Msg_N ("invalid use of 'C'P'P constructor", Rhs);
691 else
692 Error_Msg_N
693 ("left hand of assignment must not be limited type", Lhs);
694 Explain_Limited_Type (T1, Lhs);
695 end if;
697 goto Leave;
699 -- A class-wide type may be a limited view. This illegal case is not
700 -- caught by previous checks.
702 elsif Ekind (T1) = E_Class_Wide_Type and then From_Limited_With (T1) then
703 Error_Msg_NE ("invalid use of limited view of&", Lhs, T1);
704 goto Leave;
706 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
707 -- abstract. This is only checked when the assignment Comes_From_Source,
708 -- because in some cases the expander generates such assignments (such
709 -- in the _assign operation for an abstract type).
711 elsif Is_Abstract_Type (T1) and then Comes_From_Source (N) then
712 Error_Msg_N
713 ("target of assignment operation must not be abstract", Lhs);
714 end if;
716 -- Resolution may have updated the subtype, in case the left-hand side
717 -- is a private protected component. Use the correct subtype to avoid
718 -- scoping issues in the back-end.
720 T1 := Etype (Lhs);
722 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
723 -- type. For example:
725 -- limited with P;
726 -- package Pkg is
727 -- type Acc is access P.T;
728 -- end Pkg;
730 -- with Pkg; use Acc;
731 -- procedure Example is
732 -- A, B : Acc;
733 -- begin
734 -- A.all := B.all; -- ERROR
735 -- end Example;
737 if Nkind (Lhs) = N_Explicit_Dereference
738 and then Ekind (T1) = E_Incomplete_Type
739 then
740 Error_Msg_N ("invalid use of incomplete type", Lhs);
741 Kill_Lhs;
742 goto Leave;
743 end if;
745 -- Now we can complete the resolution of the right hand side
747 Set_Assignment_Type (Lhs, T1);
749 -- If the target of the assignment is an entity of a mutable type and
750 -- the expression is a conditional expression, its alternatives can be
751 -- of different subtypes of the nominal type of the LHS, so they must be
752 -- resolved with the base type, given that their subtype may differ from
753 -- that of the target mutable object.
755 if Is_Entity_Name (Lhs)
756 and then Ekind_In (Entity (Lhs), E_In_Out_Parameter,
757 E_Out_Parameter,
758 E_Variable)
759 and then Is_Composite_Type (T1)
760 and then not Is_Constrained (Etype (Entity (Lhs)))
761 and then Nkind_In (Rhs, N_If_Expression, N_Case_Expression)
762 then
763 Resolve (Rhs, Base_Type (T1));
765 else
766 Resolve (Rhs, T1);
767 end if;
769 -- This is the point at which we check for an unset reference
771 Check_Unset_Reference (Rhs);
772 Check_Unprotected_Access (Lhs, Rhs);
774 -- Remaining steps are skipped if Rhs was syntactically in error
776 if Rhs = Error then
777 Kill_Lhs;
778 goto Leave;
779 end if;
781 T2 := Etype (Rhs);
783 if not Covers (T1, T2) then
784 Wrong_Type (Rhs, Etype (Lhs));
785 Kill_Lhs;
786 goto Leave;
787 end if;
789 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
790 -- types, use the non-limited view if available
792 if Nkind (Rhs) = N_Explicit_Dereference
793 and then Is_Tagged_Type (T2)
794 and then Has_Non_Limited_View (T2)
795 then
796 T2 := Non_Limited_View (T2);
797 end if;
799 Set_Assignment_Type (Rhs, T2);
801 if Total_Errors_Detected /= 0 then
802 if No (T1) then
803 T1 := Any_Type;
804 end if;
806 if No (T2) then
807 T2 := Any_Type;
808 end if;
809 end if;
811 if T1 = Any_Type or else T2 = Any_Type then
812 Kill_Lhs;
813 goto Leave;
814 end if;
816 -- If the rhs is class-wide or dynamically tagged, then require the lhs
817 -- to be class-wide. The case where the rhs is a dynamically tagged call
818 -- to a dispatching operation with a controlling access result is
819 -- excluded from this check, since the target has an access type (and
820 -- no tag propagation occurs in that case).
822 if (Is_Class_Wide_Type (T2)
823 or else (Is_Dynamically_Tagged (Rhs)
824 and then not Is_Access_Type (T1)))
825 and then not Is_Class_Wide_Type (T1)
826 then
827 Error_Msg_N ("dynamically tagged expression not allowed!", Rhs);
829 elsif Is_Class_Wide_Type (T1)
830 and then not Is_Class_Wide_Type (T2)
831 and then not Is_Tag_Indeterminate (Rhs)
832 and then not Is_Dynamically_Tagged (Rhs)
833 then
834 Error_Msg_N ("dynamically tagged expression required!", Rhs);
835 end if;
837 -- Propagate the tag from a class-wide target to the rhs when the rhs
838 -- is a tag-indeterminate call.
840 if Is_Tag_Indeterminate (Rhs) then
841 if Is_Class_Wide_Type (T1) then
842 Propagate_Tag (Lhs, Rhs);
844 elsif Nkind (Rhs) = N_Function_Call
845 and then Is_Entity_Name (Name (Rhs))
846 and then Is_Abstract_Subprogram (Entity (Name (Rhs)))
847 then
848 Error_Msg_N
849 ("call to abstract function must be dispatching", Name (Rhs));
851 elsif Nkind (Rhs) = N_Qualified_Expression
852 and then Nkind (Expression (Rhs)) = N_Function_Call
853 and then Is_Entity_Name (Name (Expression (Rhs)))
854 and then
855 Is_Abstract_Subprogram (Entity (Name (Expression (Rhs))))
856 then
857 Error_Msg_N
858 ("call to abstract function must be dispatching",
859 Name (Expression (Rhs)));
860 end if;
861 end if;
863 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
864 -- apply an implicit conversion of the rhs to that type to force
865 -- appropriate static and run-time accessibility checks. This applies
866 -- as well to anonymous access-to-subprogram types that are component
867 -- subtypes or formal parameters.
869 if Ada_Version >= Ada_2005 and then Is_Access_Type (T1) then
870 if Is_Local_Anonymous_Access (T1)
871 or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type
873 -- Handle assignment to an Ada 2012 stand-alone object
874 -- of an anonymous access type.
876 or else (Ekind (T1) = E_Anonymous_Access_Type
877 and then Nkind (Associated_Node_For_Itype (T1)) =
878 N_Object_Declaration)
880 then
881 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
882 Analyze_And_Resolve (Rhs, T1);
883 end if;
884 end if;
886 -- Ada 2005 (AI-231): Assignment to not null variable
888 if Ada_Version >= Ada_2005
889 and then Can_Never_Be_Null (T1)
890 and then not Assignment_OK (Lhs)
891 then
892 -- Case where we know the right hand side is null
894 if Known_Null (Rhs) then
895 Apply_Compile_Time_Constraint_Error
896 (N => Rhs,
897 Msg =>
898 "(Ada 2005) null not allowed in null-excluding objects??",
899 Reason => CE_Null_Not_Allowed);
901 -- We still mark this as a possible modification, that's necessary
902 -- to reset Is_True_Constant, and desirable for xref purposes.
904 Note_Possible_Modification (Lhs, Sure => True);
905 goto Leave;
907 -- If we know the right hand side is non-null, then we convert to the
908 -- target type, since we don't need a run time check in that case.
910 elsif not Can_Never_Be_Null (T2) then
911 Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
912 Analyze_And_Resolve (Rhs, T1);
913 end if;
914 end if;
916 if Is_Scalar_Type (T1) then
917 Apply_Scalar_Range_Check (Rhs, Etype (Lhs));
919 -- For array types, verify that lengths match. If the right hand side
920 -- is a function call that has been inlined, the assignment has been
921 -- rewritten as a block, and the constraint check will be applied to the
922 -- assignment within the block.
924 elsif Is_Array_Type (T1)
925 and then (Nkind (Rhs) /= N_Type_Conversion
926 or else Is_Constrained (Etype (Rhs)))
927 and then (Nkind (Rhs) /= N_Function_Call
928 or else Nkind (N) /= N_Block_Statement)
929 then
930 -- Assignment verifies that the length of the Lsh and Rhs are equal,
931 -- but of course the indexes do not have to match. If the right-hand
932 -- side is a type conversion to an unconstrained type, a length check
933 -- is performed on the expression itself during expansion. In rare
934 -- cases, the redundant length check is computed on an index type
935 -- with a different representation, triggering incorrect code in the
936 -- back end.
938 Apply_Length_Check (Rhs, Etype (Lhs));
940 else
941 -- Discriminant checks are applied in the course of expansion
943 null;
944 end if;
946 -- Note: modifications of the Lhs may only be recorded after
947 -- checks have been applied.
949 Note_Possible_Modification (Lhs, Sure => True);
951 -- ??? a real accessibility check is needed when ???
953 -- Post warning for redundant assignment or variable to itself
955 if Warn_On_Redundant_Constructs
957 -- We only warn for source constructs
959 and then Comes_From_Source (N)
961 -- Where the object is the same on both sides
963 and then Same_Object (Lhs, Original_Node (Rhs))
965 -- But exclude the case where the right side was an operation that
966 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
967 -- don't want to warn in such a case, since it is reasonable to write
968 -- such expressions especially when K is defined symbolically in some
969 -- other package.
971 and then Nkind (Original_Node (Rhs)) not in N_Op
972 then
973 if Nkind (Lhs) in N_Has_Entity then
974 Error_Msg_NE -- CODEFIX
975 ("?r?useless assignment of & to itself!", N, Entity (Lhs));
976 else
977 Error_Msg_N -- CODEFIX
978 ("?r?useless assignment of object to itself!", N);
979 end if;
980 end if;
982 -- Check for non-allowed composite assignment
984 if not Support_Composite_Assign_On_Target
985 and then (Is_Array_Type (T1) or else Is_Record_Type (T1))
986 and then (not Has_Size_Clause (T1) or else Esize (T1) > 64)
987 then
988 Error_Msg_CRT ("composite assignment", N);
989 end if;
991 -- Check elaboration warning for left side if not in elab code
993 if Legacy_Elaboration_Checks
994 and not In_Subprogram_Or_Concurrent_Unit
995 then
996 Check_Elab_Assign (Lhs);
997 end if;
999 -- Save the scenario for later examination by the ABE Processing phase
1001 Record_Elaboration_Scenario (N);
1003 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
1004 -- assignment is a source assignment in the extended main source unit.
1005 -- We are not interested in any reference information outside this
1006 -- context, or in compiler generated assignment statements.
1008 if Comes_From_Source (N)
1009 and then In_Extended_Main_Source_Unit (Lhs)
1010 then
1011 Set_Referenced_Modified (Lhs, Out_Param => False);
1012 end if;
1014 -- RM 7.3.2 (12/3): An assignment to a view conversion (from a type to
1015 -- one of its ancestors) requires an invariant check. Apply check only
1016 -- if expression comes from source, otherwise it will be applied when
1017 -- value is assigned to source entity. This is not done in GNATprove
1018 -- mode, as GNATprove handles invariant checks itself.
1020 if Nkind (Lhs) = N_Type_Conversion
1021 and then Has_Invariants (Etype (Expression (Lhs)))
1022 and then Comes_From_Source (Expression (Lhs))
1023 and then not GNATprove_Mode
1024 then
1025 Insert_After (N, Make_Invariant_Call (Expression (Lhs)));
1026 end if;
1028 -- Final step. If left side is an entity, then we may be able to reset
1029 -- the current tracked values to new safe values. We only have something
1030 -- to do if the left side is an entity name, and expansion has not
1031 -- modified the node into something other than an assignment, and of
1032 -- course we only capture values if it is safe to do so.
1034 if Is_Entity_Name (Lhs)
1035 and then Nkind (N) = N_Assignment_Statement
1036 then
1037 declare
1038 Ent : constant Entity_Id := Entity (Lhs);
1040 begin
1041 if Safe_To_Capture_Value (N, Ent) then
1043 -- If simple variable on left side, warn if this assignment
1044 -- blots out another one (rendering it useless). We only do
1045 -- this for source assignments, otherwise we can generate bogus
1046 -- warnings when an assignment is rewritten as another
1047 -- assignment, and gets tied up with itself.
1049 -- There may have been a previous reference to a component of
1050 -- the variable, which in general removes the Last_Assignment
1051 -- field of the variable to indicate a relevant use of the
1052 -- previous assignment. However, if the assignment is to a
1053 -- subcomponent the reference may not have registered, because
1054 -- it is not possible to determine whether the context is an
1055 -- assignment. In those cases we generate a Deferred_Reference,
1056 -- to be used at the end of compilation to generate the right
1057 -- kind of reference, and we suppress a potential warning for
1058 -- a useless assignment, which might be premature. This may
1059 -- lose a warning in rare cases, but seems preferable to a
1060 -- misleading warning.
1062 if Warn_On_Modified_Unread
1063 and then Is_Assignable (Ent)
1064 and then Comes_From_Source (N)
1065 and then In_Extended_Main_Source_Unit (Ent)
1066 and then not Has_Deferred_Reference (Ent)
1067 then
1068 Warn_On_Useless_Assignment (Ent, N);
1069 end if;
1071 -- If we are assigning an access type and the left side is an
1072 -- entity, then make sure that the Is_Known_[Non_]Null flags
1073 -- properly reflect the state of the entity after assignment.
1075 if Is_Access_Type (T1) then
1076 if Known_Non_Null (Rhs) then
1077 Set_Is_Known_Non_Null (Ent, True);
1079 elsif Known_Null (Rhs)
1080 and then not Can_Never_Be_Null (Ent)
1081 then
1082 Set_Is_Known_Null (Ent, True);
1084 else
1085 Set_Is_Known_Null (Ent, False);
1087 if not Can_Never_Be_Null (Ent) then
1088 Set_Is_Known_Non_Null (Ent, False);
1089 end if;
1090 end if;
1092 -- For discrete types, we may be able to set the current value
1093 -- if the value is known at compile time.
1095 elsif Is_Discrete_Type (T1)
1096 and then Compile_Time_Known_Value (Rhs)
1097 then
1098 Set_Current_Value (Ent, Rhs);
1099 else
1100 Set_Current_Value (Ent, Empty);
1101 end if;
1103 -- If not safe to capture values, kill them
1105 else
1106 Kill_Lhs;
1107 end if;
1108 end;
1109 end if;
1111 -- If assigning to an object in whole or in part, note location of
1112 -- assignment in case no one references value. We only do this for
1113 -- source assignments, otherwise we can generate bogus warnings when an
1114 -- assignment is rewritten as another assignment, and gets tied up with
1115 -- itself.
1117 declare
1118 Ent : constant Entity_Id := Get_Enclosing_Object (Lhs);
1119 begin
1120 if Present (Ent)
1121 and then Safe_To_Capture_Value (N, Ent)
1122 and then Nkind (N) = N_Assignment_Statement
1123 and then Warn_On_Modified_Unread
1124 and then Is_Assignable (Ent)
1125 and then Comes_From_Source (N)
1126 and then In_Extended_Main_Source_Unit (Ent)
1127 then
1128 Set_Last_Assignment (Ent, Lhs);
1129 end if;
1130 end;
1132 Analyze_Dimension (N);
1134 <<Leave>>
1135 Restore_Ghost_Mode (Saved_GM);
1137 -- If the right-hand side contains target names, expansion has been
1138 -- disabled to prevent expansion that might move target names out of
1139 -- the context of the assignment statement. Restore the expander mode
1140 -- now so that assignment statement can be properly expanded.
1142 if Nkind (N) = N_Assignment_Statement then
1143 if Has_Target_Names (N) then
1144 Expander_Mode_Restore;
1145 Full_Analysis := Save_Full_Analysis;
1146 end if;
1148 pragma Assert (not Should_Transform_BIP_Assignment (Typ => T1));
1149 end if;
1150 end Analyze_Assignment;
1152 -----------------------------
1153 -- Analyze_Block_Statement --
1154 -----------------------------
1156 procedure Analyze_Block_Statement (N : Node_Id) is
1157 procedure Install_Return_Entities (Scop : Entity_Id);
1158 -- Install all entities of return statement scope Scop in the visibility
1159 -- chain except for the return object since its entity is reused in a
1160 -- renaming.
1162 -----------------------------
1163 -- Install_Return_Entities --
1164 -----------------------------
1166 procedure Install_Return_Entities (Scop : Entity_Id) is
1167 Id : Entity_Id;
1169 begin
1170 Id := First_Entity (Scop);
1171 while Present (Id) loop
1173 -- Do not install the return object
1175 if not Ekind_In (Id, E_Constant, E_Variable)
1176 or else not Is_Return_Object (Id)
1177 then
1178 Install_Entity (Id);
1179 end if;
1181 Next_Entity (Id);
1182 end loop;
1183 end Install_Return_Entities;
1185 -- Local constants and variables
1187 Decls : constant List_Id := Declarations (N);
1188 Id : constant Node_Id := Identifier (N);
1189 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1191 Is_BIP_Return_Statement : Boolean;
1193 -- Start of processing for Analyze_Block_Statement
1195 begin
1196 -- In SPARK mode, we reject block statements. Note that the case of
1197 -- block statements generated by the expander is fine.
1199 if Nkind (Original_Node (N)) = N_Block_Statement then
1200 Check_SPARK_05_Restriction ("block statement is not allowed", N);
1201 end if;
1203 -- If no handled statement sequence is present, things are really messed
1204 -- up, and we just return immediately (defence against previous errors).
1206 if No (HSS) then
1207 Check_Error_Detected;
1208 return;
1209 end if;
1211 -- Detect whether the block is actually a rewritten return statement of
1212 -- a build-in-place function.
1214 Is_BIP_Return_Statement :=
1215 Present (Id)
1216 and then Present (Entity (Id))
1217 and then Ekind (Entity (Id)) = E_Return_Statement
1218 and then Is_Build_In_Place_Function
1219 (Return_Applies_To (Entity (Id)));
1221 -- Normal processing with HSS present
1223 declare
1224 EH : constant List_Id := Exception_Handlers (HSS);
1225 Ent : Entity_Id := Empty;
1226 S : Entity_Id;
1228 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1229 -- Recursively save value of this global, will be restored on exit
1231 begin
1232 -- Initialize unblocked exit count for statements of begin block
1233 -- plus one for each exception handler that is present.
1235 Unblocked_Exit_Count := 1;
1237 if Present (EH) then
1238 Unblocked_Exit_Count := Unblocked_Exit_Count + List_Length (EH);
1239 end if;
1241 -- If a label is present analyze it and mark it as referenced
1243 if Present (Id) then
1244 Analyze (Id);
1245 Ent := Entity (Id);
1247 -- An error defense. If we have an identifier, but no entity, then
1248 -- something is wrong. If previous errors, then just remove the
1249 -- identifier and continue, otherwise raise an exception.
1251 if No (Ent) then
1252 Check_Error_Detected;
1253 Set_Identifier (N, Empty);
1255 else
1256 Set_Ekind (Ent, E_Block);
1257 Generate_Reference (Ent, N, ' ');
1258 Generate_Definition (Ent);
1260 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
1261 Set_Label_Construct (Parent (Ent), N);
1262 end if;
1263 end if;
1264 end if;
1266 -- If no entity set, create a label entity
1268 if No (Ent) then
1269 Ent := New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
1270 Set_Identifier (N, New_Occurrence_Of (Ent, Sloc (N)));
1271 Set_Parent (Ent, N);
1272 end if;
1274 Set_Etype (Ent, Standard_Void_Type);
1275 Set_Block_Node (Ent, Identifier (N));
1276 Push_Scope (Ent);
1278 -- The block served as an extended return statement. Ensure that any
1279 -- entities created during the analysis and expansion of the return
1280 -- object declaration are once again visible.
1282 if Is_BIP_Return_Statement then
1283 Install_Return_Entities (Ent);
1284 end if;
1286 if Present (Decls) then
1287 Analyze_Declarations (Decls);
1288 Check_Completion;
1289 Inspect_Deferred_Constant_Completion (Decls);
1290 end if;
1292 Analyze (HSS);
1293 Process_End_Label (HSS, 'e', Ent);
1295 -- If exception handlers are present, then we indicate that enclosing
1296 -- scopes contain a block with handlers. We only need to mark non-
1297 -- generic scopes.
1299 if Present (EH) then
1300 S := Scope (Ent);
1301 loop
1302 Set_Has_Nested_Block_With_Handler (S);
1303 exit when Is_Overloadable (S)
1304 or else Ekind (S) = E_Package
1305 or else Is_Generic_Unit (S);
1306 S := Scope (S);
1307 end loop;
1308 end if;
1310 Check_References (Ent);
1311 Update_Use_Clause_Chain;
1312 End_Scope;
1314 if Unblocked_Exit_Count = 0 then
1315 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1316 Check_Unreachable_Code (N);
1317 else
1318 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1319 end if;
1320 end;
1321 end Analyze_Block_Statement;
1323 --------------------------------
1324 -- Analyze_Compound_Statement --
1325 --------------------------------
1327 procedure Analyze_Compound_Statement (N : Node_Id) is
1328 begin
1329 Analyze_List (Actions (N));
1330 end Analyze_Compound_Statement;
1332 ----------------------------
1333 -- Analyze_Case_Statement --
1334 ----------------------------
1336 procedure Analyze_Case_Statement (N : Node_Id) is
1337 Exp : Node_Id;
1338 Exp_Type : Entity_Id;
1339 Exp_Btype : Entity_Id;
1340 Last_Choice : Nat;
1342 Others_Present : Boolean;
1343 -- Indicates if Others was present
1345 pragma Warnings (Off, Last_Choice);
1346 -- Don't care about assigned value
1348 Statements_Analyzed : Boolean := False;
1349 -- Set True if at least some statement sequences get analyzed. If False
1350 -- on exit, means we had a serious error that prevented full analysis of
1351 -- the case statement, and as a result it is not a good idea to output
1352 -- warning messages about unreachable code.
1354 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1355 -- Recursively save value of this global, will be restored on exit
1357 procedure Non_Static_Choice_Error (Choice : Node_Id);
1358 -- Error routine invoked by the generic instantiation below when the
1359 -- case statement has a non static choice.
1361 procedure Process_Statements (Alternative : Node_Id);
1362 -- Analyzes the statements associated with a case alternative. Needed
1363 -- by instantiation below.
1365 package Analyze_Case_Choices is new
1366 Generic_Analyze_Choices
1367 (Process_Associated_Node => Process_Statements);
1368 use Analyze_Case_Choices;
1369 -- Instantiation of the generic choice analysis package
1371 package Check_Case_Choices is new
1372 Generic_Check_Choices
1373 (Process_Empty_Choice => No_OP,
1374 Process_Non_Static_Choice => Non_Static_Choice_Error,
1375 Process_Associated_Node => No_OP);
1376 use Check_Case_Choices;
1377 -- Instantiation of the generic choice processing package
1379 -----------------------------
1380 -- Non_Static_Choice_Error --
1381 -----------------------------
1383 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1384 begin
1385 Flag_Non_Static_Expr
1386 ("choice given in case statement is not static!", Choice);
1387 end Non_Static_Choice_Error;
1389 ------------------------
1390 -- Process_Statements --
1391 ------------------------
1393 procedure Process_Statements (Alternative : Node_Id) is
1394 Choices : constant List_Id := Discrete_Choices (Alternative);
1395 Ent : Entity_Id;
1397 begin
1398 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1399 Statements_Analyzed := True;
1401 -- An interesting optimization. If the case statement expression
1402 -- is a simple entity, then we can set the current value within an
1403 -- alternative if the alternative has one possible value.
1405 -- case N is
1406 -- when 1 => alpha
1407 -- when 2 | 3 => beta
1408 -- when others => gamma
1410 -- Here we know that N is initially 1 within alpha, but for beta and
1411 -- gamma, we do not know anything more about the initial value.
1413 if Is_Entity_Name (Exp) then
1414 Ent := Entity (Exp);
1416 if Ekind_In (Ent, E_Variable,
1417 E_In_Out_Parameter,
1418 E_Out_Parameter)
1419 then
1420 if List_Length (Choices) = 1
1421 and then Nkind (First (Choices)) in N_Subexpr
1422 and then Compile_Time_Known_Value (First (Choices))
1423 then
1424 Set_Current_Value (Entity (Exp), First (Choices));
1425 end if;
1427 Analyze_Statements (Statements (Alternative));
1429 -- After analyzing the case, set the current value to empty
1430 -- since we won't know what it is for the next alternative
1431 -- (unless reset by this same circuit), or after the case.
1433 Set_Current_Value (Entity (Exp), Empty);
1434 return;
1435 end if;
1436 end if;
1438 -- Case where expression is not an entity name of a variable
1440 Analyze_Statements (Statements (Alternative));
1441 end Process_Statements;
1443 -- Start of processing for Analyze_Case_Statement
1445 begin
1446 Unblocked_Exit_Count := 0;
1447 Exp := Expression (N);
1448 Analyze (Exp);
1450 -- The expression must be of any discrete type. In rare cases, the
1451 -- expander constructs a case statement whose expression has a private
1452 -- type whose full view is discrete. This can happen when generating
1453 -- a stream operation for a variant type after the type is frozen,
1454 -- when the partial of view of the type of the discriminant is private.
1455 -- In that case, use the full view to analyze case alternatives.
1457 if not Is_Overloaded (Exp)
1458 and then not Comes_From_Source (N)
1459 and then Is_Private_Type (Etype (Exp))
1460 and then Present (Full_View (Etype (Exp)))
1461 and then Is_Discrete_Type (Full_View (Etype (Exp)))
1462 then
1463 Resolve (Exp, Etype (Exp));
1464 Exp_Type := Full_View (Etype (Exp));
1466 else
1467 Analyze_And_Resolve (Exp, Any_Discrete);
1468 Exp_Type := Etype (Exp);
1469 end if;
1471 Check_Unset_Reference (Exp);
1472 Exp_Btype := Base_Type (Exp_Type);
1474 -- The expression must be of a discrete type which must be determinable
1475 -- independently of the context in which the expression occurs, but
1476 -- using the fact that the expression must be of a discrete type.
1477 -- Moreover, the type this expression must not be a character literal
1478 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1480 -- If error already reported by Resolve, nothing more to do
1482 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
1483 return;
1485 elsif Exp_Btype = Any_Character then
1486 Error_Msg_N
1487 ("character literal as case expression is ambiguous", Exp);
1488 return;
1490 elsif Ada_Version = Ada_83
1491 and then (Is_Generic_Type (Exp_Btype)
1492 or else Is_Generic_Type (Root_Type (Exp_Btype)))
1493 then
1494 Error_Msg_N
1495 ("(Ada 83) case expression cannot be of a generic type", Exp);
1496 return;
1497 end if;
1499 -- If the case expression is a formal object of mode in out, then treat
1500 -- it as having a nonstatic subtype by forcing use of the base type
1501 -- (which has to get passed to Check_Case_Choices below). Also use base
1502 -- type when the case expression is parenthesized.
1504 if Paren_Count (Exp) > 0
1505 or else (Is_Entity_Name (Exp)
1506 and then Ekind (Entity (Exp)) = E_Generic_In_Out_Parameter)
1507 then
1508 Exp_Type := Exp_Btype;
1509 end if;
1511 -- Call instantiated procedures to analyzwe and check discrete choices
1513 Analyze_Choices (Alternatives (N), Exp_Type);
1514 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
1516 -- Case statement with single OTHERS alternative not allowed in SPARK
1518 if Others_Present and then List_Length (Alternatives (N)) = 1 then
1519 Check_SPARK_05_Restriction
1520 ("OTHERS as unique case alternative is not allowed", N);
1521 end if;
1523 if Exp_Type = Universal_Integer and then not Others_Present then
1524 Error_Msg_N ("case on universal integer requires OTHERS choice", Exp);
1525 end if;
1527 -- If all our exits were blocked by unconditional transfers of control,
1528 -- then the entire CASE statement acts as an unconditional transfer of
1529 -- control, so treat it like one, and check unreachable code. Skip this
1530 -- test if we had serious errors preventing any statement analysis.
1532 if Unblocked_Exit_Count = 0 and then Statements_Analyzed then
1533 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1534 Check_Unreachable_Code (N);
1535 else
1536 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1537 end if;
1539 -- If the expander is active it will detect the case of a statically
1540 -- determined single alternative and remove warnings for the case, but
1541 -- if we are not doing expansion, that circuit won't be active. Here we
1542 -- duplicate the effect of removing warnings in the same way, so that
1543 -- we will get the same set of warnings in -gnatc mode.
1545 if not Expander_Active
1546 and then Compile_Time_Known_Value (Expression (N))
1547 and then Serious_Errors_Detected = 0
1548 then
1549 declare
1550 Chosen : constant Node_Id := Find_Static_Alternative (N);
1551 Alt : Node_Id;
1553 begin
1554 Alt := First (Alternatives (N));
1555 while Present (Alt) loop
1556 if Alt /= Chosen then
1557 Remove_Warning_Messages (Statements (Alt));
1558 end if;
1560 Next (Alt);
1561 end loop;
1562 end;
1563 end if;
1564 end Analyze_Case_Statement;
1566 ----------------------------
1567 -- Analyze_Exit_Statement --
1568 ----------------------------
1570 -- If the exit includes a name, it must be the name of a currently open
1571 -- loop. Otherwise there must be an innermost open loop on the stack, to
1572 -- which the statement implicitly refers.
1574 -- Additionally, in SPARK mode:
1576 -- The exit can only name the closest enclosing loop;
1578 -- An exit with a when clause must be directly contained in a loop;
1580 -- An exit without a when clause must be directly contained in an
1581 -- if-statement with no elsif or else, which is itself directly contained
1582 -- in a loop. The exit must be the last statement in the if-statement.
1584 procedure Analyze_Exit_Statement (N : Node_Id) is
1585 Target : constant Node_Id := Name (N);
1586 Cond : constant Node_Id := Condition (N);
1587 Scope_Id : Entity_Id := Empty; -- initialize to prevent warning
1588 U_Name : Entity_Id;
1589 Kind : Entity_Kind;
1591 begin
1592 if No (Cond) then
1593 Check_Unreachable_Code (N);
1594 end if;
1596 if Present (Target) then
1597 Analyze (Target);
1598 U_Name := Entity (Target);
1600 if not In_Open_Scopes (U_Name) or else Ekind (U_Name) /= E_Loop then
1601 Error_Msg_N ("invalid loop name in exit statement", N);
1602 return;
1604 else
1605 if Has_Loop_In_Inner_Open_Scopes (U_Name) then
1606 Check_SPARK_05_Restriction
1607 ("exit label must name the closest enclosing loop", N);
1608 end if;
1610 Set_Has_Exit (U_Name);
1611 end if;
1613 else
1614 U_Name := Empty;
1615 end if;
1617 for J in reverse 0 .. Scope_Stack.Last loop
1618 Scope_Id := Scope_Stack.Table (J).Entity;
1619 Kind := Ekind (Scope_Id);
1621 if Kind = E_Loop and then (No (Target) or else Scope_Id = U_Name) then
1622 Set_Has_Exit (Scope_Id);
1623 exit;
1625 elsif Kind = E_Block
1626 or else Kind = E_Loop
1627 or else Kind = E_Return_Statement
1628 then
1629 null;
1631 else
1632 Error_Msg_N
1633 ("cannot exit from program unit or accept statement", N);
1634 return;
1635 end if;
1636 end loop;
1638 -- Verify that if present the condition is a Boolean expression
1640 if Present (Cond) then
1641 Analyze_And_Resolve (Cond, Any_Boolean);
1642 Check_Unset_Reference (Cond);
1643 end if;
1645 -- In SPARK mode, verify that the exit statement respects the SPARK
1646 -- restrictions.
1648 if Present (Cond) then
1649 if Nkind (Parent (N)) /= N_Loop_Statement then
1650 Check_SPARK_05_Restriction
1651 ("exit with when clause must be directly in loop", N);
1652 end if;
1654 else
1655 if Nkind (Parent (N)) /= N_If_Statement then
1656 if Nkind (Parent (N)) = N_Elsif_Part then
1657 Check_SPARK_05_Restriction
1658 ("exit must be in IF without ELSIF", N);
1659 else
1660 Check_SPARK_05_Restriction ("exit must be directly in IF", N);
1661 end if;
1663 elsif Nkind (Parent (Parent (N))) /= N_Loop_Statement then
1664 Check_SPARK_05_Restriction
1665 ("exit must be in IF directly in loop", N);
1667 -- First test the presence of ELSE, so that an exit in an ELSE leads
1668 -- to an error mentioning the ELSE.
1670 elsif Present (Else_Statements (Parent (N))) then
1671 Check_SPARK_05_Restriction ("exit must be in IF without ELSE", N);
1673 -- An exit in an ELSIF does not reach here, as it would have been
1674 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1676 elsif Present (Elsif_Parts (Parent (N))) then
1677 Check_SPARK_05_Restriction ("exit must be in IF without ELSIF", N);
1678 end if;
1679 end if;
1681 -- Chain exit statement to associated loop entity
1683 Set_Next_Exit_Statement (N, First_Exit_Statement (Scope_Id));
1684 Set_First_Exit_Statement (Scope_Id, N);
1686 -- Since the exit may take us out of a loop, any previous assignment
1687 -- statement is not useless, so clear last assignment indications. It
1688 -- is OK to keep other current values, since if the exit statement
1689 -- does not exit, then the current values are still valid.
1691 Kill_Current_Values (Last_Assignment_Only => True);
1692 end Analyze_Exit_Statement;
1694 ----------------------------
1695 -- Analyze_Goto_Statement --
1696 ----------------------------
1698 procedure Analyze_Goto_Statement (N : Node_Id) is
1699 Label : constant Node_Id := Name (N);
1700 Scope_Id : Entity_Id;
1701 Label_Scope : Entity_Id;
1702 Label_Ent : Entity_Id;
1704 begin
1705 Check_SPARK_05_Restriction ("goto statement is not allowed", N);
1707 -- Actual semantic checks
1709 Check_Unreachable_Code (N);
1710 Kill_Current_Values (Last_Assignment_Only => True);
1712 Analyze (Label);
1713 Label_Ent := Entity (Label);
1715 -- Ignore previous error
1717 if Label_Ent = Any_Id then
1718 Check_Error_Detected;
1719 return;
1721 -- We just have a label as the target of a goto
1723 elsif Ekind (Label_Ent) /= E_Label then
1724 Error_Msg_N ("target of goto statement must be a label", Label);
1725 return;
1727 -- Check that the target of the goto is reachable according to Ada
1728 -- scoping rules. Note: the special gotos we generate for optimizing
1729 -- local handling of exceptions would violate these rules, but we mark
1730 -- such gotos as analyzed when built, so this code is never entered.
1732 elsif not Reachable (Label_Ent) then
1733 Error_Msg_N ("target of goto statement is not reachable", Label);
1734 return;
1735 end if;
1737 -- Here if goto passes initial validity checks
1739 Label_Scope := Enclosing_Scope (Label_Ent);
1741 for J in reverse 0 .. Scope_Stack.Last loop
1742 Scope_Id := Scope_Stack.Table (J).Entity;
1744 if Label_Scope = Scope_Id
1745 or else not Ekind_In (Scope_Id, E_Block, E_Loop, E_Return_Statement)
1746 then
1747 if Scope_Id /= Label_Scope then
1748 Error_Msg_N
1749 ("cannot exit from program unit or accept statement", N);
1750 end if;
1752 return;
1753 end if;
1754 end loop;
1756 raise Program_Error;
1757 end Analyze_Goto_Statement;
1759 --------------------------
1760 -- Analyze_If_Statement --
1761 --------------------------
1763 -- A special complication arises in the analysis of if statements
1765 -- The expander has circuitry to completely delete code that it can tell
1766 -- will not be executed (as a result of compile time known conditions). In
1767 -- the analyzer, we ensure that code that will be deleted in this manner
1768 -- is analyzed but not expanded. This is obviously more efficient, but
1769 -- more significantly, difficulties arise if code is expanded and then
1770 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1771 -- generated in deleted code must be frozen from start, because the nodes
1772 -- on which they depend will not be available at the freeze point.
1774 procedure Analyze_If_Statement (N : Node_Id) is
1775 E : Node_Id;
1777 Save_Unblocked_Exit_Count : constant Nat := Unblocked_Exit_Count;
1778 -- Recursively save value of this global, will be restored on exit
1780 Save_In_Deleted_Code : Boolean;
1782 Del : Boolean := False;
1783 -- This flag gets set True if a True condition has been found, which
1784 -- means that remaining ELSE/ELSIF parts are deleted.
1786 procedure Analyze_Cond_Then (Cnode : Node_Id);
1787 -- This is applied to either the N_If_Statement node itself or to an
1788 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1789 -- statements associated with it.
1791 -----------------------
1792 -- Analyze_Cond_Then --
1793 -----------------------
1795 procedure Analyze_Cond_Then (Cnode : Node_Id) is
1796 Cond : constant Node_Id := Condition (Cnode);
1797 Tstm : constant List_Id := Then_Statements (Cnode);
1799 begin
1800 Unblocked_Exit_Count := Unblocked_Exit_Count + 1;
1801 Analyze_And_Resolve (Cond, Any_Boolean);
1802 Check_Unset_Reference (Cond);
1803 Set_Current_Value_Condition (Cnode);
1805 -- If already deleting, then just analyze then statements
1807 if Del then
1808 Analyze_Statements (Tstm);
1810 -- Compile time known value, not deleting yet
1812 elsif Compile_Time_Known_Value (Cond) then
1813 Save_In_Deleted_Code := In_Deleted_Code;
1815 -- If condition is True, then analyze the THEN statements and set
1816 -- no expansion for ELSE and ELSIF parts.
1818 if Is_True (Expr_Value (Cond)) then
1819 Analyze_Statements (Tstm);
1820 Del := True;
1821 Expander_Mode_Save_And_Set (False);
1822 In_Deleted_Code := True;
1824 -- If condition is False, analyze THEN with expansion off
1826 else -- Is_False (Expr_Value (Cond))
1827 Expander_Mode_Save_And_Set (False);
1828 In_Deleted_Code := True;
1829 Analyze_Statements (Tstm);
1830 Expander_Mode_Restore;
1831 In_Deleted_Code := Save_In_Deleted_Code;
1832 end if;
1834 -- Not known at compile time, not deleting, normal analysis
1836 else
1837 Analyze_Statements (Tstm);
1838 end if;
1839 end Analyze_Cond_Then;
1841 -- Start of processing for Analyze_If_Statement
1843 begin
1844 -- Initialize exit count for else statements. If there is no else part,
1845 -- this count will stay non-zero reflecting the fact that the uncovered
1846 -- else case is an unblocked exit.
1848 Unblocked_Exit_Count := 1;
1849 Analyze_Cond_Then (N);
1851 -- Now to analyze the elsif parts if any are present
1853 if Present (Elsif_Parts (N)) then
1854 E := First (Elsif_Parts (N));
1855 while Present (E) loop
1856 Analyze_Cond_Then (E);
1857 Next (E);
1858 end loop;
1859 end if;
1861 if Present (Else_Statements (N)) then
1862 Analyze_Statements (Else_Statements (N));
1863 end if;
1865 -- If all our exits were blocked by unconditional transfers of control,
1866 -- then the entire IF statement acts as an unconditional transfer of
1867 -- control, so treat it like one, and check unreachable code.
1869 if Unblocked_Exit_Count = 0 then
1870 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1871 Check_Unreachable_Code (N);
1872 else
1873 Unblocked_Exit_Count := Save_Unblocked_Exit_Count;
1874 end if;
1876 if Del then
1877 Expander_Mode_Restore;
1878 In_Deleted_Code := Save_In_Deleted_Code;
1879 end if;
1881 if not Expander_Active
1882 and then Compile_Time_Known_Value (Condition (N))
1883 and then Serious_Errors_Detected = 0
1884 then
1885 if Is_True (Expr_Value (Condition (N))) then
1886 Remove_Warning_Messages (Else_Statements (N));
1888 if Present (Elsif_Parts (N)) then
1889 E := First (Elsif_Parts (N));
1890 while Present (E) loop
1891 Remove_Warning_Messages (Then_Statements (E));
1892 Next (E);
1893 end loop;
1894 end if;
1896 else
1897 Remove_Warning_Messages (Then_Statements (N));
1898 end if;
1899 end if;
1901 -- Warn on redundant if statement that has no effect
1903 -- Note, we could also check empty ELSIF parts ???
1905 if Warn_On_Redundant_Constructs
1907 -- If statement must be from source
1909 and then Comes_From_Source (N)
1911 -- Condition must not have obvious side effect
1913 and then Has_No_Obvious_Side_Effects (Condition (N))
1915 -- No elsif parts of else part
1917 and then No (Elsif_Parts (N))
1918 and then No (Else_Statements (N))
1920 -- Then must be a single null statement
1922 and then List_Length (Then_Statements (N)) = 1
1923 then
1924 -- Go to original node, since we may have rewritten something as
1925 -- a null statement (e.g. a case we could figure the outcome of).
1927 declare
1928 T : constant Node_Id := First (Then_Statements (N));
1929 S : constant Node_Id := Original_Node (T);
1931 begin
1932 if Comes_From_Source (S) and then Nkind (S) = N_Null_Statement then
1933 Error_Msg_N ("if statement has no effect?r?", N);
1934 end if;
1935 end;
1936 end if;
1937 end Analyze_If_Statement;
1939 ----------------------------------------
1940 -- Analyze_Implicit_Label_Declaration --
1941 ----------------------------------------
1943 -- An implicit label declaration is generated in the innermost enclosing
1944 -- declarative part. This is done for labels, and block and loop names.
1946 -- Note: any changes in this routine may need to be reflected in
1947 -- Analyze_Label_Entity.
1949 procedure Analyze_Implicit_Label_Declaration (N : Node_Id) is
1950 Id : constant Node_Id := Defining_Identifier (N);
1951 begin
1952 Enter_Name (Id);
1953 Set_Ekind (Id, E_Label);
1954 Set_Etype (Id, Standard_Void_Type);
1955 Set_Enclosing_Scope (Id, Current_Scope);
1956 end Analyze_Implicit_Label_Declaration;
1958 ------------------------------
1959 -- Analyze_Iteration_Scheme --
1960 ------------------------------
1962 procedure Analyze_Iteration_Scheme (N : Node_Id) is
1963 Cond : Node_Id;
1964 Iter_Spec : Node_Id;
1965 Loop_Spec : Node_Id;
1967 begin
1968 -- For an infinite loop, there is no iteration scheme
1970 if No (N) then
1971 return;
1972 end if;
1974 Cond := Condition (N);
1975 Iter_Spec := Iterator_Specification (N);
1976 Loop_Spec := Loop_Parameter_Specification (N);
1978 if Present (Cond) then
1979 Analyze_And_Resolve (Cond, Any_Boolean);
1980 Check_Unset_Reference (Cond);
1981 Set_Current_Value_Condition (N);
1983 elsif Present (Iter_Spec) then
1984 Analyze_Iterator_Specification (Iter_Spec);
1986 else
1987 Analyze_Loop_Parameter_Specification (Loop_Spec);
1988 end if;
1989 end Analyze_Iteration_Scheme;
1991 ------------------------------------
1992 -- Analyze_Iterator_Specification --
1993 ------------------------------------
1995 procedure Analyze_Iterator_Specification (N : Node_Id) is
1996 procedure Check_Reverse_Iteration (Typ : Entity_Id);
1997 -- For an iteration over a container, if the loop carries the Reverse
1998 -- indicator, verify that the container type has an Iterate aspect that
1999 -- implements the reversible iterator interface.
2001 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id;
2002 -- For containers with Iterator and related aspects, the cursor is
2003 -- obtained by locating an entity with the proper name in the scope
2004 -- of the type.
2006 -----------------------------
2007 -- Check_Reverse_Iteration --
2008 -----------------------------
2010 procedure Check_Reverse_Iteration (Typ : Entity_Id) is
2011 begin
2012 if Reverse_Present (N) then
2013 if Is_Array_Type (Typ)
2014 or else Is_Reversible_Iterator (Typ)
2015 or else
2016 (Present (Find_Aspect (Typ, Aspect_Iterable))
2017 and then
2018 Present
2019 (Get_Iterable_Type_Primitive (Typ, Name_Previous)))
2020 then
2021 null;
2022 else
2023 Error_Msg_NE
2024 ("container type does not support reverse iteration", N, Typ);
2025 end if;
2026 end if;
2027 end Check_Reverse_Iteration;
2029 ---------------------
2030 -- Get_Cursor_Type --
2031 ---------------------
2033 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
2034 Ent : Entity_Id;
2036 begin
2037 -- If iterator type is derived, the cursor is declared in the scope
2038 -- of the parent type.
2040 if Is_Derived_Type (Typ) then
2041 Ent := First_Entity (Scope (Etype (Typ)));
2042 else
2043 Ent := First_Entity (Scope (Typ));
2044 end if;
2046 while Present (Ent) loop
2047 exit when Chars (Ent) = Name_Cursor;
2048 Next_Entity (Ent);
2049 end loop;
2051 if No (Ent) then
2052 return Any_Type;
2053 end if;
2055 -- The cursor is the target of generated assignments in the
2056 -- loop, and cannot have a limited type.
2058 if Is_Limited_Type (Etype (Ent)) then
2059 Error_Msg_N ("cursor type cannot be limited", N);
2060 end if;
2062 return Etype (Ent);
2063 end Get_Cursor_Type;
2065 -- Local variables
2067 Def_Id : constant Node_Id := Defining_Identifier (N);
2068 Iter_Name : constant Node_Id := Name (N);
2069 Loc : constant Source_Ptr := Sloc (N);
2070 Subt : constant Node_Id := Subtype_Indication (N);
2072 Bas : Entity_Id := Empty; -- initialize to prevent warning
2073 Typ : Entity_Id;
2075 -- Start of processing for Analyze_Iterator_Specification
2077 begin
2078 Enter_Name (Def_Id);
2080 -- AI12-0151 specifies that when the subtype indication is present, it
2081 -- must statically match the type of the array or container element.
2082 -- To simplify this check, we introduce a subtype declaration with the
2083 -- given subtype indication when it carries a constraint, and rewrite
2084 -- the original as a reference to the created subtype entity.
2086 if Present (Subt) then
2087 if Nkind (Subt) = N_Subtype_Indication then
2088 declare
2089 S : constant Entity_Id := Make_Temporary (Sloc (Subt), 'S');
2090 Decl : constant Node_Id :=
2091 Make_Subtype_Declaration (Loc,
2092 Defining_Identifier => S,
2093 Subtype_Indication => New_Copy_Tree (Subt));
2094 begin
2095 Insert_Before (Parent (Parent (N)), Decl);
2096 Analyze (Decl);
2097 Rewrite (Subt, New_Occurrence_Of (S, Sloc (Subt)));
2098 end;
2099 else
2100 Analyze (Subt);
2101 end if;
2103 -- Save entity of subtype indication for subsequent check
2105 Bas := Entity (Subt);
2106 end if;
2108 Preanalyze_Range (Iter_Name);
2110 -- Set the kind of the loop variable, which is not visible within the
2111 -- iterator name.
2113 Set_Ekind (Def_Id, E_Variable);
2115 -- Provide a link between the iterator variable and the container, for
2116 -- subsequent use in cross-reference and modification information.
2118 if Of_Present (N) then
2119 Set_Related_Expression (Def_Id, Iter_Name);
2121 -- For a container, the iterator is specified through the aspect
2123 if not Is_Array_Type (Etype (Iter_Name)) then
2124 declare
2125 Iterator : constant Entity_Id :=
2126 Find_Value_Of_Aspect
2127 (Etype (Iter_Name), Aspect_Default_Iterator);
2129 I : Interp_Index;
2130 It : Interp;
2132 begin
2133 if No (Iterator) then
2134 null; -- error reported below
2136 elsif not Is_Overloaded (Iterator) then
2137 Check_Reverse_Iteration (Etype (Iterator));
2139 -- If Iterator is overloaded, use reversible iterator if one is
2140 -- available.
2142 elsif Is_Overloaded (Iterator) then
2143 Get_First_Interp (Iterator, I, It);
2144 while Present (It.Nam) loop
2145 if Ekind (It.Nam) = E_Function
2146 and then Is_Reversible_Iterator (Etype (It.Nam))
2147 then
2148 Set_Etype (Iterator, It.Typ);
2149 Set_Entity (Iterator, It.Nam);
2150 exit;
2151 end if;
2153 Get_Next_Interp (I, It);
2154 end loop;
2156 Check_Reverse_Iteration (Etype (Iterator));
2157 end if;
2158 end;
2159 end if;
2160 end if;
2162 -- If the domain of iteration is an expression, create a declaration for
2163 -- it, so that finalization actions are introduced outside of the loop.
2164 -- The declaration must be a renaming because the body of the loop may
2165 -- assign to elements.
2167 if not Is_Entity_Name (Iter_Name)
2169 -- When the context is a quantified expression, the renaming
2170 -- declaration is delayed until the expansion phase if we are
2171 -- doing expansion.
2173 and then (Nkind (Parent (N)) /= N_Quantified_Expression
2174 or else Operating_Mode = Check_Semantics)
2176 -- Do not perform this expansion for ASIS and when expansion is
2177 -- disabled, where the temporary may hide the transformation of a
2178 -- selected component into a prefixed function call, and references
2179 -- need to see the original expression.
2181 and then Expander_Active
2182 then
2183 declare
2184 Id : constant Entity_Id := Make_Temporary (Loc, 'R', Iter_Name);
2185 Decl : Node_Id;
2186 Act_S : Node_Id;
2188 begin
2190 -- If the domain of iteration is an array component that depends
2191 -- on a discriminant, create actual subtype for it. Pre-analysis
2192 -- does not generate the actual subtype of a selected component.
2194 if Nkind (Iter_Name) = N_Selected_Component
2195 and then Is_Array_Type (Etype (Iter_Name))
2196 then
2197 Act_S :=
2198 Build_Actual_Subtype_Of_Component
2199 (Etype (Selector_Name (Iter_Name)), Iter_Name);
2200 Insert_Action (N, Act_S);
2202 if Present (Act_S) then
2203 Typ := Defining_Identifier (Act_S);
2204 else
2205 Typ := Etype (Iter_Name);
2206 end if;
2208 else
2209 Typ := Etype (Iter_Name);
2211 -- Verify that the expression produces an iterator
2213 if not Of_Present (N) and then not Is_Iterator (Typ)
2214 and then not Is_Array_Type (Typ)
2215 and then No (Find_Aspect (Typ, Aspect_Iterable))
2216 then
2217 Error_Msg_N
2218 ("expect object that implements iterator interface",
2219 Iter_Name);
2220 end if;
2221 end if;
2223 -- Protect against malformed iterator
2225 if Typ = Any_Type then
2226 Error_Msg_N ("invalid expression in loop iterator", Iter_Name);
2227 return;
2228 end if;
2230 if not Of_Present (N) then
2231 Check_Reverse_Iteration (Typ);
2232 end if;
2234 -- The name in the renaming declaration may be a function call.
2235 -- Indicate that it does not come from source, to suppress
2236 -- spurious warnings on renamings of parameterless functions,
2237 -- a common enough idiom in user-defined iterators.
2239 Decl :=
2240 Make_Object_Renaming_Declaration (Loc,
2241 Defining_Identifier => Id,
2242 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
2243 Name =>
2244 New_Copy_Tree (Iter_Name, New_Sloc => Loc));
2246 Insert_Actions (Parent (Parent (N)), New_List (Decl));
2247 Rewrite (Name (N), New_Occurrence_Of (Id, Loc));
2248 Set_Etype (Id, Typ);
2249 Set_Etype (Name (N), Typ);
2250 end;
2252 -- Container is an entity or an array with uncontrolled components, or
2253 -- else it is a container iterator given by a function call, typically
2254 -- called Iterate in the case of predefined containers, even though
2255 -- Iterate is not a reserved name. What matters is that the return type
2256 -- of the function is an iterator type.
2258 elsif Is_Entity_Name (Iter_Name) then
2259 Analyze (Iter_Name);
2261 if Nkind (Iter_Name) = N_Function_Call then
2262 declare
2263 C : constant Node_Id := Name (Iter_Name);
2264 I : Interp_Index;
2265 It : Interp;
2267 begin
2268 if not Is_Overloaded (Iter_Name) then
2269 Resolve (Iter_Name, Etype (C));
2271 else
2272 Get_First_Interp (C, I, It);
2273 while It.Typ /= Empty loop
2274 if Reverse_Present (N) then
2275 if Is_Reversible_Iterator (It.Typ) then
2276 Resolve (Iter_Name, It.Typ);
2277 exit;
2278 end if;
2280 elsif Is_Iterator (It.Typ) then
2281 Resolve (Iter_Name, It.Typ);
2282 exit;
2283 end if;
2285 Get_Next_Interp (I, It);
2286 end loop;
2287 end if;
2288 end;
2290 -- Domain of iteration is not overloaded
2292 else
2293 Resolve (Iter_Name, Etype (Iter_Name));
2294 end if;
2296 if not Of_Present (N) then
2297 Check_Reverse_Iteration (Etype (Iter_Name));
2298 end if;
2299 end if;
2301 -- Get base type of container, for proper retrieval of Cursor type
2302 -- and primitive operations.
2304 Typ := Base_Type (Etype (Iter_Name));
2306 if Is_Array_Type (Typ) then
2307 if Of_Present (N) then
2308 Set_Etype (Def_Id, Component_Type (Typ));
2310 -- The loop variable is aliased if the array components are
2311 -- aliased.
2313 Set_Is_Aliased (Def_Id, Has_Aliased_Components (Typ));
2315 -- AI12-0047 stipulates that the domain (array or container)
2316 -- cannot be a component that depends on a discriminant if the
2317 -- enclosing object is mutable, to prevent a modification of the
2318 -- dowmain of iteration in the course of an iteration.
2320 -- If the object is an expression it has been captured in a
2321 -- temporary, so examine original node.
2323 if Nkind (Original_Node (Iter_Name)) = N_Selected_Component
2324 and then Is_Dependent_Component_Of_Mutable_Object
2325 (Original_Node (Iter_Name))
2326 then
2327 Error_Msg_N
2328 ("iterable name cannot be a discriminant-dependent "
2329 & "component of a mutable object", N);
2330 end if;
2332 if Present (Subt)
2333 and then
2334 (Base_Type (Bas) /= Base_Type (Component_Type (Typ))
2335 or else
2336 not Subtypes_Statically_Match (Bas, Component_Type (Typ)))
2337 then
2338 Error_Msg_N
2339 ("subtype indication does not match component type", Subt);
2340 end if;
2342 -- Here we have a missing Range attribute
2344 else
2345 Error_Msg_N
2346 ("missing Range attribute in iteration over an array", N);
2348 -- In Ada 2012 mode, this may be an attempt at an iterator
2350 if Ada_Version >= Ada_2012 then
2351 Error_Msg_NE
2352 ("\if& is meant to designate an element of the array, use OF",
2353 N, Def_Id);
2354 end if;
2356 -- Prevent cascaded errors
2358 Set_Ekind (Def_Id, E_Loop_Parameter);
2359 Set_Etype (Def_Id, Etype (First_Index (Typ)));
2360 end if;
2362 -- Check for type error in iterator
2364 elsif Typ = Any_Type then
2365 return;
2367 -- Iteration over a container
2369 else
2370 Set_Ekind (Def_Id, E_Loop_Parameter);
2371 Error_Msg_Ada_2012_Feature ("container iterator", Sloc (N));
2373 -- OF present
2375 if Of_Present (N) then
2376 if Has_Aspect (Typ, Aspect_Iterable) then
2377 declare
2378 Elt : constant Entity_Id :=
2379 Get_Iterable_Type_Primitive (Typ, Name_Element);
2380 begin
2381 if No (Elt) then
2382 Error_Msg_N
2383 ("missing Element primitive for iteration", N);
2384 else
2385 Set_Etype (Def_Id, Etype (Elt));
2386 Check_Reverse_Iteration (Typ);
2387 end if;
2388 end;
2390 -- For a predefined container, The type of the loop variable is
2391 -- the Iterator_Element aspect of the container type.
2393 else
2394 declare
2395 Element : constant Entity_Id :=
2396 Find_Value_Of_Aspect
2397 (Typ, Aspect_Iterator_Element);
2398 Iterator : constant Entity_Id :=
2399 Find_Value_Of_Aspect
2400 (Typ, Aspect_Default_Iterator);
2401 Orig_Iter_Name : constant Node_Id :=
2402 Original_Node (Iter_Name);
2403 Cursor_Type : Entity_Id;
2405 begin
2406 if No (Element) then
2407 Error_Msg_NE ("cannot iterate over&", N, Typ);
2408 return;
2410 else
2411 Set_Etype (Def_Id, Entity (Element));
2412 Cursor_Type := Get_Cursor_Type (Typ);
2413 pragma Assert (Present (Cursor_Type));
2415 -- If subtype indication was given, verify that it covers
2416 -- the element type of the container.
2418 if Present (Subt)
2419 and then (not Covers (Bas, Etype (Def_Id))
2420 or else not Subtypes_Statically_Match
2421 (Bas, Etype (Def_Id)))
2422 then
2423 Error_Msg_N
2424 ("subtype indication does not match element type",
2425 Subt);
2426 end if;
2428 -- If the container has a variable indexing aspect, the
2429 -- element is a variable and is modifiable in the loop.
2431 if Has_Aspect (Typ, Aspect_Variable_Indexing) then
2432 Set_Ekind (Def_Id, E_Variable);
2433 end if;
2435 -- If the container is a constant, iterating over it
2436 -- requires a Constant_Indexing operation.
2438 if not Is_Variable (Iter_Name)
2439 and then not Has_Aspect (Typ, Aspect_Constant_Indexing)
2440 then
2441 Error_Msg_N
2442 ("iteration over constant container require "
2443 & "constant_indexing aspect", N);
2445 -- The Iterate function may have an in_out parameter,
2446 -- and a constant container is thus illegal.
2448 elsif Present (Iterator)
2449 and then Ekind (Entity (Iterator)) = E_Function
2450 and then Ekind (First_Formal (Entity (Iterator))) /=
2451 E_In_Parameter
2452 and then not Is_Variable (Iter_Name)
2453 then
2454 Error_Msg_N ("variable container expected", N);
2455 end if;
2457 -- Detect a case where the iterator denotes a component
2458 -- of a mutable object which depends on a discriminant.
2459 -- Note that the iterator may denote a function call in
2460 -- qualified form, in which case this check should not
2461 -- be performed.
2463 if Nkind (Orig_Iter_Name) = N_Selected_Component
2464 and then
2465 Present (Entity (Selector_Name (Orig_Iter_Name)))
2466 and then Ekind_In
2467 (Entity (Selector_Name (Orig_Iter_Name)),
2468 E_Component,
2469 E_Discriminant)
2470 and then Is_Dependent_Component_Of_Mutable_Object
2471 (Orig_Iter_Name)
2472 then
2473 Error_Msg_N
2474 ("container cannot be a discriminant-dependent "
2475 & "component of a mutable object", N);
2476 end if;
2477 end if;
2478 end;
2479 end if;
2481 -- IN iterator, domain is a range, or a call to Iterate function
2483 else
2484 -- For an iteration of the form IN, the name must denote an
2485 -- iterator, typically the result of a call to Iterate. Give a
2486 -- useful error message when the name is a container by itself.
2488 -- The type may be a formal container type, which has to have
2489 -- an Iterable aspect detailing the required primitives.
2491 if Is_Entity_Name (Original_Node (Name (N)))
2492 and then not Is_Iterator (Typ)
2493 then
2494 if Has_Aspect (Typ, Aspect_Iterable) then
2495 null;
2497 elsif not Has_Aspect (Typ, Aspect_Iterator_Element) then
2498 Error_Msg_NE
2499 ("cannot iterate over&", Name (N), Typ);
2500 else
2501 Error_Msg_N
2502 ("name must be an iterator, not a container", Name (N));
2503 end if;
2505 if Has_Aspect (Typ, Aspect_Iterable) then
2506 null;
2507 else
2508 Error_Msg_NE
2509 ("\to iterate directly over the elements of a container, "
2510 & "write `of &`", Name (N), Original_Node (Name (N)));
2512 -- No point in continuing analysis of iterator spec
2514 return;
2515 end if;
2516 end if;
2518 -- If the name is a call (typically prefixed) to some Iterate
2519 -- function, it has been rewritten as an object declaration.
2520 -- If that object is a selected component, verify that it is not
2521 -- a component of an unconstrained mutable object.
2523 if Nkind (Iter_Name) = N_Identifier
2524 or else (not Expander_Active and Comes_From_Source (Iter_Name))
2525 then
2526 declare
2527 Orig_Node : constant Node_Id := Original_Node (Iter_Name);
2528 Iter_Kind : constant Node_Kind := Nkind (Orig_Node);
2529 Obj : Node_Id;
2531 begin
2532 if Iter_Kind = N_Selected_Component then
2533 Obj := Prefix (Orig_Node);
2535 elsif Iter_Kind = N_Function_Call then
2536 Obj := First_Actual (Orig_Node);
2538 -- If neither, the name comes from source
2540 else
2541 Obj := Iter_Name;
2542 end if;
2544 if Nkind (Obj) = N_Selected_Component
2545 and then Is_Dependent_Component_Of_Mutable_Object (Obj)
2546 then
2547 Error_Msg_N
2548 ("container cannot be a discriminant-dependent "
2549 & "component of a mutable object", N);
2550 end if;
2551 end;
2552 end if;
2554 -- The result type of Iterate function is the classwide type of
2555 -- the interface parent. We need the specific Cursor type defined
2556 -- in the container package. We obtain it by name for a predefined
2557 -- container, or through the Iterable aspect for a formal one.
2559 if Has_Aspect (Typ, Aspect_Iterable) then
2560 Set_Etype (Def_Id,
2561 Get_Cursor_Type
2562 (Parent (Find_Value_Of_Aspect (Typ, Aspect_Iterable)),
2563 Typ));
2565 else
2566 Set_Etype (Def_Id, Get_Cursor_Type (Typ));
2567 Check_Reverse_Iteration (Etype (Iter_Name));
2568 end if;
2570 end if;
2571 end if;
2572 end Analyze_Iterator_Specification;
2574 -------------------
2575 -- Analyze_Label --
2576 -------------------
2578 -- Note: the semantic work required for analyzing labels (setting them as
2579 -- reachable) was done in a prepass through the statements in the block,
2580 -- so that forward gotos would be properly handled. See Analyze_Statements
2581 -- for further details. The only processing required here is to deal with
2582 -- optimizations that depend on an assumption of sequential control flow,
2583 -- since of course the occurrence of a label breaks this assumption.
2585 procedure Analyze_Label (N : Node_Id) is
2586 pragma Warnings (Off, N);
2587 begin
2588 Kill_Current_Values;
2589 end Analyze_Label;
2591 --------------------------
2592 -- Analyze_Label_Entity --
2593 --------------------------
2595 procedure Analyze_Label_Entity (E : Entity_Id) is
2596 begin
2597 Set_Ekind (E, E_Label);
2598 Set_Etype (E, Standard_Void_Type);
2599 Set_Enclosing_Scope (E, Current_Scope);
2600 Set_Reachable (E, True);
2601 end Analyze_Label_Entity;
2603 ------------------------------------------
2604 -- Analyze_Loop_Parameter_Specification --
2605 ------------------------------------------
2607 procedure Analyze_Loop_Parameter_Specification (N : Node_Id) is
2608 Loop_Nod : constant Node_Id := Parent (Parent (N));
2610 procedure Check_Controlled_Array_Attribute (DS : Node_Id);
2611 -- If the bounds are given by a 'Range reference on a function call
2612 -- that returns a controlled array, introduce an explicit declaration
2613 -- to capture the bounds, so that the function result can be finalized
2614 -- in timely fashion.
2616 procedure Check_Predicate_Use (T : Entity_Id);
2617 -- Diagnose Attempt to iterate through non-static predicate. Note that
2618 -- a type with inherited predicates may have both static and dynamic
2619 -- forms. In this case it is not sufficent to check the static predicate
2620 -- function only, look for a dynamic predicate aspect as well.
2622 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean;
2623 -- N is the node for an arbitrary construct. This function searches the
2624 -- construct N to see if any expressions within it contain function
2625 -- calls that use the secondary stack, returning True if any such call
2626 -- is found, and False otherwise.
2628 procedure Process_Bounds (R : Node_Id);
2629 -- If the iteration is given by a range, create temporaries and
2630 -- assignment statements block to capture the bounds and perform
2631 -- required finalization actions in case a bound includes a function
2632 -- call that uses the temporary stack. We first pre-analyze a copy of
2633 -- the range in order to determine the expected type, and analyze and
2634 -- resolve the original bounds.
2636 --------------------------------------
2637 -- Check_Controlled_Array_Attribute --
2638 --------------------------------------
2640 procedure Check_Controlled_Array_Attribute (DS : Node_Id) is
2641 begin
2642 if Nkind (DS) = N_Attribute_Reference
2643 and then Is_Entity_Name (Prefix (DS))
2644 and then Ekind (Entity (Prefix (DS))) = E_Function
2645 and then Is_Array_Type (Etype (Entity (Prefix (DS))))
2646 and then
2647 Is_Controlled (Component_Type (Etype (Entity (Prefix (DS)))))
2648 and then Expander_Active
2649 then
2650 declare
2651 Loc : constant Source_Ptr := Sloc (N);
2652 Arr : constant Entity_Id := Etype (Entity (Prefix (DS)));
2653 Indx : constant Entity_Id :=
2654 Base_Type (Etype (First_Index (Arr)));
2655 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
2656 Decl : Node_Id;
2658 begin
2659 Decl :=
2660 Make_Subtype_Declaration (Loc,
2661 Defining_Identifier => Subt,
2662 Subtype_Indication =>
2663 Make_Subtype_Indication (Loc,
2664 Subtype_Mark => New_Occurrence_Of (Indx, Loc),
2665 Constraint =>
2666 Make_Range_Constraint (Loc, Relocate_Node (DS))));
2667 Insert_Before (Loop_Nod, Decl);
2668 Analyze (Decl);
2670 Rewrite (DS,
2671 Make_Attribute_Reference (Loc,
2672 Prefix => New_Occurrence_Of (Subt, Loc),
2673 Attribute_Name => Attribute_Name (DS)));
2675 Analyze (DS);
2676 end;
2677 end if;
2678 end Check_Controlled_Array_Attribute;
2680 -------------------------
2681 -- Check_Predicate_Use --
2682 -------------------------
2684 procedure Check_Predicate_Use (T : Entity_Id) is
2685 begin
2686 -- A predicated subtype is illegal in loops and related constructs
2687 -- if the predicate is not static, or if it is a non-static subtype
2688 -- of a statically predicated subtype.
2690 if Is_Discrete_Type (T)
2691 and then Has_Predicates (T)
2692 and then (not Has_Static_Predicate (T)
2693 or else not Is_Static_Subtype (T)
2694 or else Has_Dynamic_Predicate_Aspect (T))
2695 then
2696 -- Seems a confusing message for the case of a static predicate
2697 -- with a non-static subtype???
2699 Bad_Predicated_Subtype_Use
2700 ("cannot use subtype& with non-static predicate for loop "
2701 & "iteration", Discrete_Subtype_Definition (N),
2702 T, Suggest_Static => True);
2704 elsif Inside_A_Generic
2705 and then Is_Generic_Formal (T)
2706 and then Is_Discrete_Type (T)
2707 then
2708 Set_No_Dynamic_Predicate_On_Actual (T);
2709 end if;
2710 end Check_Predicate_Use;
2712 ------------------------------------
2713 -- Has_Call_Using_Secondary_Stack --
2714 ------------------------------------
2716 function Has_Call_Using_Secondary_Stack (N : Node_Id) return Boolean is
2718 function Check_Call (N : Node_Id) return Traverse_Result;
2719 -- Check if N is a function call which uses the secondary stack
2721 ----------------
2722 -- Check_Call --
2723 ----------------
2725 function Check_Call (N : Node_Id) return Traverse_Result is
2726 Nam : Node_Id;
2727 Subp : Entity_Id;
2728 Return_Typ : Entity_Id;
2730 begin
2731 if Nkind (N) = N_Function_Call then
2732 Nam := Name (N);
2734 -- Call using access to subprogram with explicit dereference
2736 if Nkind (Nam) = N_Explicit_Dereference then
2737 Subp := Etype (Nam);
2739 -- Call using a selected component notation or Ada 2005 object
2740 -- operation notation
2742 elsif Nkind (Nam) = N_Selected_Component then
2743 Subp := Entity (Selector_Name (Nam));
2745 -- Common case
2747 else
2748 Subp := Entity (Nam);
2749 end if;
2751 Return_Typ := Etype (Subp);
2753 if Is_Composite_Type (Return_Typ)
2754 and then not Is_Constrained (Return_Typ)
2755 then
2756 return Abandon;
2758 elsif Sec_Stack_Needed_For_Return (Subp) then
2759 return Abandon;
2760 end if;
2761 end if;
2763 -- Continue traversing the tree
2765 return OK;
2766 end Check_Call;
2768 function Check_Calls is new Traverse_Func (Check_Call);
2770 -- Start of processing for Has_Call_Using_Secondary_Stack
2772 begin
2773 return Check_Calls (N) = Abandon;
2774 end Has_Call_Using_Secondary_Stack;
2776 --------------------
2777 -- Process_Bounds --
2778 --------------------
2780 procedure Process_Bounds (R : Node_Id) is
2781 Loc : constant Source_Ptr := Sloc (N);
2783 function One_Bound
2784 (Original_Bound : Node_Id;
2785 Analyzed_Bound : Node_Id;
2786 Typ : Entity_Id) return Node_Id;
2787 -- Capture value of bound and return captured value
2789 ---------------
2790 -- One_Bound --
2791 ---------------
2793 function One_Bound
2794 (Original_Bound : Node_Id;
2795 Analyzed_Bound : Node_Id;
2796 Typ : Entity_Id) return Node_Id
2798 Assign : Node_Id;
2799 Decl : Node_Id;
2800 Id : Entity_Id;
2802 begin
2803 -- If the bound is a constant or an object, no need for a separate
2804 -- declaration. If the bound is the result of previous expansion
2805 -- it is already analyzed and should not be modified. Note that
2806 -- the Bound will be resolved later, if needed, as part of the
2807 -- call to Make_Index (literal bounds may need to be resolved to
2808 -- type Integer).
2810 if Analyzed (Original_Bound) then
2811 return Original_Bound;
2813 elsif Nkind_In (Analyzed_Bound, N_Integer_Literal,
2814 N_Character_Literal)
2815 or else Is_Entity_Name (Analyzed_Bound)
2816 then
2817 Analyze_And_Resolve (Original_Bound, Typ);
2818 return Original_Bound;
2819 end if;
2821 -- Normally, the best approach is simply to generate a constant
2822 -- declaration that captures the bound. However, there is a nasty
2823 -- case where this is wrong. If the bound is complex, and has a
2824 -- possible use of the secondary stack, we need to generate a
2825 -- separate assignment statement to ensure the creation of a block
2826 -- which will release the secondary stack.
2828 -- We prefer the constant declaration, since it leaves us with a
2829 -- proper trace of the value, useful in optimizations that get rid
2830 -- of junk range checks.
2832 if not Has_Call_Using_Secondary_Stack (Analyzed_Bound) then
2833 Analyze_And_Resolve (Original_Bound, Typ);
2835 -- Ensure that the bound is valid. This check should not be
2836 -- generated when the range belongs to a quantified expression
2837 -- as the construct is still not expanded into its final form.
2839 if Nkind (Parent (R)) /= N_Loop_Parameter_Specification
2840 or else Nkind (Parent (Parent (R))) /= N_Quantified_Expression
2841 then
2842 Ensure_Valid (Original_Bound);
2843 end if;
2845 Force_Evaluation (Original_Bound);
2846 return Original_Bound;
2847 end if;
2849 Id := Make_Temporary (Loc, 'R', Original_Bound);
2851 -- Here we make a declaration with a separate assignment
2852 -- statement, and insert before loop header.
2854 Decl :=
2855 Make_Object_Declaration (Loc,
2856 Defining_Identifier => Id,
2857 Object_Definition => New_Occurrence_Of (Typ, Loc));
2859 Assign :=
2860 Make_Assignment_Statement (Loc,
2861 Name => New_Occurrence_Of (Id, Loc),
2862 Expression => Relocate_Node (Original_Bound));
2864 Insert_Actions (Loop_Nod, New_List (Decl, Assign));
2866 -- Now that this temporary variable is initialized we decorate it
2867 -- as safe-to-reevaluate to inform to the backend that no further
2868 -- asignment will be issued and hence it can be handled as side
2869 -- effect free. Note that this decoration must be done when the
2870 -- assignment has been analyzed because otherwise it will be
2871 -- rejected (see Analyze_Assignment).
2873 Set_Is_Safe_To_Reevaluate (Id);
2875 Rewrite (Original_Bound, New_Occurrence_Of (Id, Loc));
2877 if Nkind (Assign) = N_Assignment_Statement then
2878 return Expression (Assign);
2879 else
2880 return Original_Bound;
2881 end if;
2882 end One_Bound;
2884 Hi : constant Node_Id := High_Bound (R);
2885 Lo : constant Node_Id := Low_Bound (R);
2886 R_Copy : constant Node_Id := New_Copy_Tree (R);
2887 New_Hi : Node_Id;
2888 New_Lo : Node_Id;
2889 Typ : Entity_Id;
2891 -- Start of processing for Process_Bounds
2893 begin
2894 Set_Parent (R_Copy, Parent (R));
2895 Preanalyze_Range (R_Copy);
2896 Typ := Etype (R_Copy);
2898 -- If the type of the discrete range is Universal_Integer, then the
2899 -- bound's type must be resolved to Integer, and any object used to
2900 -- hold the bound must also have type Integer, unless the literal
2901 -- bounds are constant-folded expressions with a user-defined type.
2903 if Typ = Universal_Integer then
2904 if Nkind (Lo) = N_Integer_Literal
2905 and then Present (Etype (Lo))
2906 and then Scope (Etype (Lo)) /= Standard_Standard
2907 then
2908 Typ := Etype (Lo);
2910 elsif Nkind (Hi) = N_Integer_Literal
2911 and then Present (Etype (Hi))
2912 and then Scope (Etype (Hi)) /= Standard_Standard
2913 then
2914 Typ := Etype (Hi);
2916 else
2917 Typ := Standard_Integer;
2918 end if;
2919 end if;
2921 Set_Etype (R, Typ);
2923 New_Lo := One_Bound (Lo, Low_Bound (R_Copy), Typ);
2924 New_Hi := One_Bound (Hi, High_Bound (R_Copy), Typ);
2926 -- Propagate staticness to loop range itself, in case the
2927 -- corresponding subtype is static.
2929 if New_Lo /= Lo and then Is_OK_Static_Expression (New_Lo) then
2930 Rewrite (Low_Bound (R), New_Copy (New_Lo));
2931 end if;
2933 if New_Hi /= Hi and then Is_OK_Static_Expression (New_Hi) then
2934 Rewrite (High_Bound (R), New_Copy (New_Hi));
2935 end if;
2936 end Process_Bounds;
2938 -- Local variables
2940 DS : constant Node_Id := Discrete_Subtype_Definition (N);
2941 Id : constant Entity_Id := Defining_Identifier (N);
2943 DS_Copy : Node_Id;
2945 -- Start of processing for Analyze_Loop_Parameter_Specification
2947 begin
2948 Enter_Name (Id);
2950 -- We always consider the loop variable to be referenced, since the loop
2951 -- may be used just for counting purposes.
2953 Generate_Reference (Id, N, ' ');
2955 -- Check for the case of loop variable hiding a local variable (used
2956 -- later on to give a nice warning if the hidden variable is never
2957 -- assigned).
2959 declare
2960 H : constant Entity_Id := Homonym (Id);
2961 begin
2962 if Present (H)
2963 and then Ekind (H) = E_Variable
2964 and then Is_Discrete_Type (Etype (H))
2965 and then Enclosing_Dynamic_Scope (H) = Enclosing_Dynamic_Scope (Id)
2966 then
2967 Set_Hiding_Loop_Variable (H, Id);
2968 end if;
2969 end;
2971 -- Loop parameter specification must include subtype mark in SPARK
2973 if Nkind (DS) = N_Range then
2974 Check_SPARK_05_Restriction
2975 ("loop parameter specification must include subtype mark", N);
2976 end if;
2978 -- Analyze the subtype definition and create temporaries for the bounds.
2979 -- Do not evaluate the range when preanalyzing a quantified expression
2980 -- because bounds expressed as function calls with side effects will be
2981 -- incorrectly replicated.
2983 if Nkind (DS) = N_Range
2984 and then Expander_Active
2985 and then Nkind (Parent (N)) /= N_Quantified_Expression
2986 then
2987 Process_Bounds (DS);
2989 -- Either the expander not active or the range of iteration is a subtype
2990 -- indication, an entity, or a function call that yields an aggregate or
2991 -- a container.
2993 else
2994 DS_Copy := New_Copy_Tree (DS);
2995 Set_Parent (DS_Copy, Parent (DS));
2996 Preanalyze_Range (DS_Copy);
2998 -- Ada 2012: If the domain of iteration is:
3000 -- a) a function call,
3001 -- b) an identifier that is not a type,
3002 -- c) an attribute reference 'Old (within a postcondition),
3003 -- d) an unchecked conversion or a qualified expression with
3004 -- the proper iterator type.
3006 -- then it is an iteration over a container. It was classified as
3007 -- a loop specification by the parser, and must be rewritten now
3008 -- to activate container iteration. The last case will occur within
3009 -- an expanded inlined call, where the expansion wraps an actual in
3010 -- an unchecked conversion when needed. The expression of the
3011 -- conversion is always an object.
3013 if Nkind (DS_Copy) = N_Function_Call
3015 or else (Is_Entity_Name (DS_Copy)
3016 and then not Is_Type (Entity (DS_Copy)))
3018 or else (Nkind (DS_Copy) = N_Attribute_Reference
3019 and then Nam_In (Attribute_Name (DS_Copy),
3020 Name_Loop_Entry, Name_Old))
3022 or else Has_Aspect (Etype (DS_Copy), Aspect_Iterable)
3024 or else Nkind (DS_Copy) = N_Unchecked_Type_Conversion
3025 or else (Nkind (DS_Copy) = N_Qualified_Expression
3026 and then Is_Iterator (Etype (DS_Copy)))
3027 then
3028 -- This is an iterator specification. Rewrite it as such and
3029 -- analyze it to capture function calls that may require
3030 -- finalization actions.
3032 declare
3033 I_Spec : constant Node_Id :=
3034 Make_Iterator_Specification (Sloc (N),
3035 Defining_Identifier => Relocate_Node (Id),
3036 Name => DS_Copy,
3037 Subtype_Indication => Empty,
3038 Reverse_Present => Reverse_Present (N));
3039 Scheme : constant Node_Id := Parent (N);
3041 begin
3042 Set_Iterator_Specification (Scheme, I_Spec);
3043 Set_Loop_Parameter_Specification (Scheme, Empty);
3044 Analyze_Iterator_Specification (I_Spec);
3046 -- In a generic context, analyze the original domain of
3047 -- iteration, for name capture.
3049 if not Expander_Active then
3050 Analyze (DS);
3051 end if;
3053 -- Set kind of loop parameter, which may be used in the
3054 -- subsequent analysis of the condition in a quantified
3055 -- expression.
3057 Set_Ekind (Id, E_Loop_Parameter);
3058 return;
3059 end;
3061 -- Domain of iteration is not a function call, and is side-effect
3062 -- free.
3064 else
3065 -- A quantified expression that appears in a pre/post condition
3066 -- is pre-analyzed several times. If the range is given by an
3067 -- attribute reference it is rewritten as a range, and this is
3068 -- done even with expansion disabled. If the type is already set
3069 -- do not reanalyze, because a range with static bounds may be
3070 -- typed Integer by default.
3072 if Nkind (Parent (N)) = N_Quantified_Expression
3073 and then Present (Etype (DS))
3074 then
3075 null;
3076 else
3077 Analyze (DS);
3078 end if;
3079 end if;
3080 end if;
3082 if DS = Error then
3083 return;
3084 end if;
3086 -- Some additional checks if we are iterating through a type
3088 if Is_Entity_Name (DS)
3089 and then Present (Entity (DS))
3090 and then Is_Type (Entity (DS))
3091 then
3092 -- The subtype indication may denote the completion of an incomplete
3093 -- type declaration.
3095 if Ekind (Entity (DS)) = E_Incomplete_Type then
3096 Set_Entity (DS, Get_Full_View (Entity (DS)));
3097 Set_Etype (DS, Entity (DS));
3098 end if;
3100 Check_Predicate_Use (Entity (DS));
3101 end if;
3103 -- Error if not discrete type
3105 if not Is_Discrete_Type (Etype (DS)) then
3106 Wrong_Type (DS, Any_Discrete);
3107 Set_Etype (DS, Any_Type);
3108 end if;
3110 Check_Controlled_Array_Attribute (DS);
3112 if Nkind (DS) = N_Subtype_Indication then
3113 Check_Predicate_Use (Entity (Subtype_Mark (DS)));
3114 end if;
3116 Make_Index (DS, N, In_Iter_Schm => True);
3117 Set_Ekind (Id, E_Loop_Parameter);
3119 -- A quantified expression which appears in a pre- or post-condition may
3120 -- be analyzed multiple times. The analysis of the range creates several
3121 -- itypes which reside in different scopes depending on whether the pre-
3122 -- or post-condition has been expanded. Update the type of the loop
3123 -- variable to reflect the proper itype at each stage of analysis.
3125 if No (Etype (Id))
3126 or else Etype (Id) = Any_Type
3127 or else
3128 (Present (Etype (Id))
3129 and then Is_Itype (Etype (Id))
3130 and then Nkind (Parent (Loop_Nod)) = N_Expression_With_Actions
3131 and then Nkind (Original_Node (Parent (Loop_Nod))) =
3132 N_Quantified_Expression)
3133 then
3134 Set_Etype (Id, Etype (DS));
3135 end if;
3137 -- Treat a range as an implicit reference to the type, to inhibit
3138 -- spurious warnings.
3140 Generate_Reference (Base_Type (Etype (DS)), N, ' ');
3141 Set_Is_Known_Valid (Id, True);
3143 -- The loop is not a declarative part, so the loop variable must be
3144 -- frozen explicitly. Do not freeze while preanalyzing a quantified
3145 -- expression because the freeze node will not be inserted into the
3146 -- tree due to flag Is_Spec_Expression being set.
3148 if Nkind (Parent (N)) /= N_Quantified_Expression then
3149 declare
3150 Flist : constant List_Id := Freeze_Entity (Id, N);
3151 begin
3152 if Is_Non_Empty_List (Flist) then
3153 Insert_Actions (N, Flist);
3154 end if;
3155 end;
3156 end if;
3158 -- Case where we have a range or a subtype, get type bounds
3160 if Nkind_In (DS, N_Range, N_Subtype_Indication)
3161 and then not Error_Posted (DS)
3162 and then Etype (DS) /= Any_Type
3163 and then Is_Discrete_Type (Etype (DS))
3164 then
3165 declare
3166 L : Node_Id;
3167 H : Node_Id;
3169 begin
3170 if Nkind (DS) = N_Range then
3171 L := Low_Bound (DS);
3172 H := High_Bound (DS);
3173 else
3174 L :=
3175 Type_Low_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
3176 H :=
3177 Type_High_Bound (Underlying_Type (Etype (Subtype_Mark (DS))));
3178 end if;
3180 -- Check for null or possibly null range and issue warning. We
3181 -- suppress such messages in generic templates and instances,
3182 -- because in practice they tend to be dubious in these cases. The
3183 -- check applies as well to rewritten array element loops where a
3184 -- null range may be detected statically.
3186 if Compile_Time_Compare (L, H, Assume_Valid => True) = GT then
3188 -- Suppress the warning if inside a generic template or
3189 -- instance, since in practice they tend to be dubious in these
3190 -- cases since they can result from intended parameterization.
3192 if not Inside_A_Generic and then not In_Instance then
3194 -- Specialize msg if invalid values could make the loop
3195 -- non-null after all.
3197 if Compile_Time_Compare
3198 (L, H, Assume_Valid => False) = GT
3199 then
3200 -- Since we know the range of the loop is null, set the
3201 -- appropriate flag to remove the loop entirely during
3202 -- expansion.
3204 Set_Is_Null_Loop (Loop_Nod);
3206 if Comes_From_Source (N) then
3207 Error_Msg_N
3208 ("??loop range is null, loop will not execute", DS);
3209 end if;
3211 -- Here is where the loop could execute because of
3212 -- invalid values, so issue appropriate message and in
3213 -- this case we do not set the Is_Null_Loop flag since
3214 -- the loop may execute.
3216 elsif Comes_From_Source (N) then
3217 Error_Msg_N
3218 ("??loop range may be null, loop may not execute",
3219 DS);
3220 Error_Msg_N
3221 ("??can only execute if invalid values are present",
3222 DS);
3223 end if;
3224 end if;
3226 -- In either case, suppress warnings in the body of the loop,
3227 -- since it is likely that these warnings will be inappropriate
3228 -- if the loop never actually executes, which is likely.
3230 Set_Suppress_Loop_Warnings (Loop_Nod);
3232 -- The other case for a warning is a reverse loop where the
3233 -- upper bound is the integer literal zero or one, and the
3234 -- lower bound may exceed this value.
3236 -- For example, we have
3238 -- for J in reverse N .. 1 loop
3240 -- In practice, this is very likely to be a case of reversing
3241 -- the bounds incorrectly in the range.
3243 elsif Reverse_Present (N)
3244 and then Nkind (Original_Node (H)) = N_Integer_Literal
3245 and then
3246 (Intval (Original_Node (H)) = Uint_0
3247 or else
3248 Intval (Original_Node (H)) = Uint_1)
3249 then
3250 -- Lower bound may in fact be known and known not to exceed
3251 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
3253 if Compile_Time_Known_Value (L)
3254 and then Expr_Value (L) <= Expr_Value (H)
3255 then
3256 null;
3258 -- Otherwise warning is warranted
3260 else
3261 Error_Msg_N ("??loop range may be null", DS);
3262 Error_Msg_N ("\??bounds may be wrong way round", DS);
3263 end if;
3264 end if;
3266 -- Check if either bound is known to be outside the range of the
3267 -- loop parameter type, this is e.g. the case of a loop from
3268 -- 20..X where the type is 1..19.
3270 -- Such a loop is dubious since either it raises CE or it executes
3271 -- zero times, and that cannot be useful!
3273 if Etype (DS) /= Any_Type
3274 and then not Error_Posted (DS)
3275 and then Nkind (DS) = N_Subtype_Indication
3276 and then Nkind (Constraint (DS)) = N_Range_Constraint
3277 then
3278 declare
3279 LLo : constant Node_Id :=
3280 Low_Bound (Range_Expression (Constraint (DS)));
3281 LHi : constant Node_Id :=
3282 High_Bound (Range_Expression (Constraint (DS)));
3284 Bad_Bound : Node_Id := Empty;
3285 -- Suspicious loop bound
3287 begin
3288 -- At this stage L, H are the bounds of the type, and LLo
3289 -- Lhi are the low bound and high bound of the loop.
3291 if Compile_Time_Compare (LLo, L, Assume_Valid => True) = LT
3292 or else
3293 Compile_Time_Compare (LLo, H, Assume_Valid => True) = GT
3294 then
3295 Bad_Bound := LLo;
3296 end if;
3298 if Compile_Time_Compare (LHi, L, Assume_Valid => True) = LT
3299 or else
3300 Compile_Time_Compare (LHi, H, Assume_Valid => True) = GT
3301 then
3302 Bad_Bound := LHi;
3303 end if;
3305 if Present (Bad_Bound) then
3306 Error_Msg_N
3307 ("suspicious loop bound out of range of "
3308 & "loop subtype??", Bad_Bound);
3309 Error_Msg_N
3310 ("\loop executes zero times or raises "
3311 & "Constraint_Error??", Bad_Bound);
3312 end if;
3313 end;
3314 end if;
3316 -- This declare block is about warnings, if we get an exception while
3317 -- testing for warnings, we simply abandon the attempt silently. This
3318 -- most likely occurs as the result of a previous error, but might
3319 -- just be an obscure case we have missed. In either case, not giving
3320 -- the warning is perfectly acceptable.
3322 exception
3323 when others => null;
3324 end;
3325 end if;
3327 -- A loop parameter cannot be effectively volatile (SPARK RM 7.1.3(4)).
3328 -- This check is relevant only when SPARK_Mode is on as it is not a
3329 -- standard Ada legality check.
3331 if SPARK_Mode = On and then Is_Effectively_Volatile (Id) then
3332 Error_Msg_N ("loop parameter cannot be volatile", Id);
3333 end if;
3334 end Analyze_Loop_Parameter_Specification;
3336 ----------------------------
3337 -- Analyze_Loop_Statement --
3338 ----------------------------
3340 procedure Analyze_Loop_Statement (N : Node_Id) is
3342 function Is_Container_Iterator (Iter : Node_Id) return Boolean;
3343 -- Given a loop iteration scheme, determine whether it is an Ada 2012
3344 -- container iteration.
3346 function Is_Wrapped_In_Block (N : Node_Id) return Boolean;
3347 -- Determine whether loop statement N has been wrapped in a block to
3348 -- capture finalization actions that may be generated for container
3349 -- iterators. Prevents infinite recursion when block is analyzed.
3350 -- Routine is a noop if loop is single statement within source block.
3352 ---------------------------
3353 -- Is_Container_Iterator --
3354 ---------------------------
3356 function Is_Container_Iterator (Iter : Node_Id) return Boolean is
3357 begin
3358 -- Infinite loop
3360 if No (Iter) then
3361 return False;
3363 -- While loop
3365 elsif Present (Condition (Iter)) then
3366 return False;
3368 -- for Def_Id in [reverse] Name loop
3369 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
3371 elsif Present (Iterator_Specification (Iter)) then
3372 declare
3373 Nam : constant Node_Id := Name (Iterator_Specification (Iter));
3374 Nam_Copy : Node_Id;
3376 begin
3377 Nam_Copy := New_Copy_Tree (Nam);
3378 Set_Parent (Nam_Copy, Parent (Nam));
3379 Preanalyze_Range (Nam_Copy);
3381 -- The only two options here are iteration over a container or
3382 -- an array.
3384 return not Is_Array_Type (Etype (Nam_Copy));
3385 end;
3387 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
3389 else
3390 declare
3391 LP : constant Node_Id := Loop_Parameter_Specification (Iter);
3392 DS : constant Node_Id := Discrete_Subtype_Definition (LP);
3393 DS_Copy : Node_Id;
3395 begin
3396 DS_Copy := New_Copy_Tree (DS);
3397 Set_Parent (DS_Copy, Parent (DS));
3398 Preanalyze_Range (DS_Copy);
3400 -- Check for a call to Iterate () or an expression with
3401 -- an iterator type.
3403 return
3404 (Nkind (DS_Copy) = N_Function_Call
3405 and then Needs_Finalization (Etype (DS_Copy)))
3406 or else Is_Iterator (Etype (DS_Copy));
3407 end;
3408 end if;
3409 end Is_Container_Iterator;
3411 -------------------------
3412 -- Is_Wrapped_In_Block --
3413 -------------------------
3415 function Is_Wrapped_In_Block (N : Node_Id) return Boolean is
3416 HSS : Node_Id;
3417 Stat : Node_Id;
3419 begin
3421 -- Check if current scope is a block that is not a transient block.
3423 if Ekind (Current_Scope) /= E_Block
3424 or else No (Block_Node (Current_Scope))
3425 then
3426 return False;
3428 else
3429 HSS :=
3430 Handled_Statement_Sequence (Parent (Block_Node (Current_Scope)));
3432 -- Skip leading pragmas that may be introduced for invariant and
3433 -- predicate checks.
3435 Stat := First (Statements (HSS));
3436 while Present (Stat) and then Nkind (Stat) = N_Pragma loop
3437 Stat := Next (Stat);
3438 end loop;
3440 return Stat = N and then No (Next (Stat));
3441 end if;
3442 end Is_Wrapped_In_Block;
3444 -- Local declarations
3446 Id : constant Node_Id := Identifier (N);
3447 Iter : constant Node_Id := Iteration_Scheme (N);
3448 Loc : constant Source_Ptr := Sloc (N);
3449 Ent : Entity_Id;
3450 Stmt : Node_Id;
3452 -- Start of processing for Analyze_Loop_Statement
3454 begin
3455 if Present (Id) then
3457 -- Make name visible, e.g. for use in exit statements. Loop labels
3458 -- are always considered to be referenced.
3460 Analyze (Id);
3461 Ent := Entity (Id);
3463 -- Guard against serious error (typically, a scope mismatch when
3464 -- semantic analysis is requested) by creating loop entity to
3465 -- continue analysis.
3467 if No (Ent) then
3468 if Total_Errors_Detected /= 0 then
3469 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3470 else
3471 raise Program_Error;
3472 end if;
3474 -- Verify that the loop name is hot hidden by an unrelated
3475 -- declaration in an inner scope.
3477 elsif Ekind (Ent) /= E_Label and then Ekind (Ent) /= E_Loop then
3478 Error_Msg_Sloc := Sloc (Ent);
3479 Error_Msg_N ("implicit label declaration for & is hidden#", Id);
3481 if Present (Homonym (Ent))
3482 and then Ekind (Homonym (Ent)) = E_Label
3483 then
3484 Set_Entity (Id, Ent);
3485 Set_Ekind (Ent, E_Loop);
3486 end if;
3488 else
3489 Generate_Reference (Ent, N, ' ');
3490 Generate_Definition (Ent);
3492 -- If we found a label, mark its type. If not, ignore it, since it
3493 -- means we have a conflicting declaration, which would already
3494 -- have been diagnosed at declaration time. Set Label_Construct
3495 -- of the implicit label declaration, which is not created by the
3496 -- parser for generic units.
3498 if Ekind (Ent) = E_Label then
3499 Set_Ekind (Ent, E_Loop);
3501 if Nkind (Parent (Ent)) = N_Implicit_Label_Declaration then
3502 Set_Label_Construct (Parent (Ent), N);
3503 end if;
3504 end if;
3505 end if;
3507 -- Case of no identifier present. Create one and attach it to the
3508 -- loop statement for use as a scope and as a reference for later
3509 -- expansions. Indicate that the label does not come from source,
3510 -- and attach it to the loop statement so it is part of the tree,
3511 -- even without a full declaration.
3513 else
3514 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
3515 Set_Etype (Ent, Standard_Void_Type);
3516 Set_Identifier (N, New_Occurrence_Of (Ent, Loc));
3517 Set_Parent (Ent, N);
3518 Set_Has_Created_Identifier (N);
3519 end if;
3521 -- If the iterator specification has a syntactic error, transform
3522 -- construct into an infinite loop to prevent a crash and perform
3523 -- some analysis.
3525 if Present (Iter)
3526 and then Present (Iterator_Specification (Iter))
3527 and then Error_Posted (Iterator_Specification (Iter))
3528 then
3529 Set_Iteration_Scheme (N, Empty);
3530 Analyze (N);
3531 return;
3532 end if;
3534 -- Iteration over a container in Ada 2012 involves the creation of a
3535 -- controlled iterator object. Wrap the loop in a block to ensure the
3536 -- timely finalization of the iterator and release of container locks.
3537 -- The same applies to the use of secondary stack when obtaining an
3538 -- iterator.
3540 if Ada_Version >= Ada_2012
3541 and then Is_Container_Iterator (Iter)
3542 and then not Is_Wrapped_In_Block (N)
3543 then
3544 declare
3545 Block_Nod : Node_Id;
3546 Block_Id : Entity_Id;
3548 begin
3549 Block_Nod :=
3550 Make_Block_Statement (Loc,
3551 Declarations => New_List,
3552 Handled_Statement_Sequence =>
3553 Make_Handled_Sequence_Of_Statements (Loc,
3554 Statements => New_List (Relocate_Node (N))));
3556 Add_Block_Identifier (Block_Nod, Block_Id);
3558 -- The expansion of iterator loops generates an iterator in order
3559 -- to traverse the elements of a container:
3561 -- Iter : <iterator type> := Iterate (Container)'reference;
3563 -- The iterator is controlled and returned on the secondary stack.
3564 -- The analysis of the call to Iterate establishes a transient
3565 -- scope to deal with the secondary stack management, but never
3566 -- really creates a physical block as this would kill the iterator
3567 -- too early (see Wrap_Transient_Declaration). To address this
3568 -- case, mark the generated block as needing secondary stack
3569 -- management.
3571 Set_Uses_Sec_Stack (Block_Id);
3573 Rewrite (N, Block_Nod);
3574 Analyze (N);
3575 return;
3576 end;
3577 end if;
3579 -- Kill current values on entry to loop, since statements in the body of
3580 -- the loop may have been executed before the loop is entered. Similarly
3581 -- we kill values after the loop, since we do not know that the body of
3582 -- the loop was executed.
3584 Kill_Current_Values;
3585 Push_Scope (Ent);
3586 Analyze_Iteration_Scheme (Iter);
3588 -- Check for following case which merits a warning if the type E of is
3589 -- a multi-dimensional array (and no explicit subscript ranges present).
3591 -- for J in E'Range
3592 -- for K in E'Range
3594 if Present (Iter)
3595 and then Present (Loop_Parameter_Specification (Iter))
3596 then
3597 declare
3598 LPS : constant Node_Id := Loop_Parameter_Specification (Iter);
3599 DSD : constant Node_Id :=
3600 Original_Node (Discrete_Subtype_Definition (LPS));
3601 begin
3602 if Nkind (DSD) = N_Attribute_Reference
3603 and then Attribute_Name (DSD) = Name_Range
3604 and then No (Expressions (DSD))
3605 then
3606 declare
3607 Typ : constant Entity_Id := Etype (Prefix (DSD));
3608 begin
3609 if Is_Array_Type (Typ)
3610 and then Number_Dimensions (Typ) > 1
3611 and then Nkind (Parent (N)) = N_Loop_Statement
3612 and then Present (Iteration_Scheme (Parent (N)))
3613 then
3614 declare
3615 OIter : constant Node_Id :=
3616 Iteration_Scheme (Parent (N));
3617 OLPS : constant Node_Id :=
3618 Loop_Parameter_Specification (OIter);
3619 ODSD : constant Node_Id :=
3620 Original_Node (Discrete_Subtype_Definition (OLPS));
3621 begin
3622 if Nkind (ODSD) = N_Attribute_Reference
3623 and then Attribute_Name (ODSD) = Name_Range
3624 and then No (Expressions (ODSD))
3625 and then Etype (Prefix (ODSD)) = Typ
3626 then
3627 Error_Msg_Sloc := Sloc (ODSD);
3628 Error_Msg_N
3629 ("inner range same as outer range#??", DSD);
3630 end if;
3631 end;
3632 end if;
3633 end;
3634 end if;
3635 end;
3636 end if;
3638 -- Analyze the statements of the body except in the case of an Ada 2012
3639 -- iterator with the expander active. In this case the expander will do
3640 -- a rewrite of the loop into a while loop. We will then analyze the
3641 -- loop body when we analyze this while loop.
3643 -- We need to do this delay because if the container is for indefinite
3644 -- types the actual subtype of the components will only be determined
3645 -- when the cursor declaration is analyzed.
3647 -- If the expander is not active then we want to analyze the loop body
3648 -- now even in the Ada 2012 iterator case, since the rewriting will not
3649 -- be done. Insert the loop variable in the current scope, if not done
3650 -- when analysing the iteration scheme. Set its kind properly to detect
3651 -- improper uses in the loop body.
3653 -- In GNATprove mode, we do one of the above depending on the kind of
3654 -- loop. If it is an iterator over an array, then we do not analyze the
3655 -- loop now. We will analyze it after it has been rewritten by the
3656 -- special SPARK expansion which is activated in GNATprove mode. We need
3657 -- to do this so that other expansions that should occur in GNATprove
3658 -- mode take into account the specificities of the rewritten loop, in
3659 -- particular the introduction of a renaming (which needs to be
3660 -- expanded).
3662 -- In other cases in GNATprove mode then we want to analyze the loop
3663 -- body now, since no rewriting will occur. Within a generic the
3664 -- GNATprove mode is irrelevant, we must analyze the generic for
3665 -- non-local name capture.
3667 if Present (Iter)
3668 and then Present (Iterator_Specification (Iter))
3669 then
3670 if GNATprove_Mode
3671 and then Is_Iterator_Over_Array (Iterator_Specification (Iter))
3672 and then not Inside_A_Generic
3673 then
3674 null;
3676 elsif not Expander_Active then
3677 declare
3678 I_Spec : constant Node_Id := Iterator_Specification (Iter);
3679 Id : constant Entity_Id := Defining_Identifier (I_Spec);
3681 begin
3682 if Scope (Id) /= Current_Scope then
3683 Enter_Name (Id);
3684 end if;
3686 -- In an element iterator, The loop parameter is a variable if
3687 -- the domain of iteration (container or array) is a variable.
3689 if not Of_Present (I_Spec)
3690 or else not Is_Variable (Name (I_Spec))
3691 then
3692 Set_Ekind (Id, E_Loop_Parameter);
3693 end if;
3694 end;
3696 Analyze_Statements (Statements (N));
3697 end if;
3699 else
3700 -- Pre-Ada2012 for-loops and while loops
3702 Analyze_Statements (Statements (N));
3703 end if;
3705 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
3706 -- the loop is transformed into a conditional block. Retrieve the loop.
3708 Stmt := N;
3710 if Subject_To_Loop_Entry_Attributes (Stmt) then
3711 Stmt := Find_Loop_In_Conditional_Block (Stmt);
3712 end if;
3714 -- Finish up processing for the loop. We kill all current values, since
3715 -- in general we don't know if the statements in the loop have been
3716 -- executed. We could do a bit better than this with a loop that we
3717 -- know will execute at least once, but it's not worth the trouble and
3718 -- the front end is not in the business of flow tracing.
3720 Process_End_Label (Stmt, 'e', Ent);
3721 End_Scope;
3722 Kill_Current_Values;
3724 -- Check for infinite loop. Skip check for generated code, since it
3725 -- justs waste time and makes debugging the routine called harder.
3727 -- Note that we have to wait till the body of the loop is fully analyzed
3728 -- before making this call, since Check_Infinite_Loop_Warning relies on
3729 -- being able to use semantic visibility information to find references.
3731 if Comes_From_Source (Stmt) then
3732 Check_Infinite_Loop_Warning (Stmt);
3733 end if;
3735 -- Code after loop is unreachable if the loop has no WHILE or FOR and
3736 -- contains no EXIT statements within the body of the loop.
3738 if No (Iter) and then not Has_Exit (Ent) then
3739 Check_Unreachable_Code (Stmt);
3740 end if;
3741 end Analyze_Loop_Statement;
3743 ----------------------------
3744 -- Analyze_Null_Statement --
3745 ----------------------------
3747 -- Note: the semantics of the null statement is implemented by a single
3748 -- null statement, too bad everything isn't as simple as this.
3750 procedure Analyze_Null_Statement (N : Node_Id) is
3751 pragma Warnings (Off, N);
3752 begin
3753 null;
3754 end Analyze_Null_Statement;
3756 -------------------------
3757 -- Analyze_Target_Name --
3758 -------------------------
3760 procedure Analyze_Target_Name (N : Node_Id) is
3761 begin
3762 -- A target name has the type of the left-hand side of the enclosing
3763 -- assignment.
3765 Set_Etype (N, Etype (Name (Current_Assignment)));
3766 end Analyze_Target_Name;
3768 ------------------------
3769 -- Analyze_Statements --
3770 ------------------------
3772 procedure Analyze_Statements (L : List_Id) is
3773 Lab : Entity_Id;
3774 S : Node_Id;
3776 begin
3777 -- The labels declared in the statement list are reachable from
3778 -- statements in the list. We do this as a prepass so that any goto
3779 -- statement will be properly flagged if its target is not reachable.
3780 -- This is not required, but is nice behavior.
3782 S := First (L);
3783 while Present (S) loop
3784 if Nkind (S) = N_Label then
3785 Analyze (Identifier (S));
3786 Lab := Entity (Identifier (S));
3788 -- If we found a label mark it as reachable
3790 if Ekind (Lab) = E_Label then
3791 Generate_Definition (Lab);
3792 Set_Reachable (Lab);
3794 if Nkind (Parent (Lab)) = N_Implicit_Label_Declaration then
3795 Set_Label_Construct (Parent (Lab), S);
3796 end if;
3798 -- If we failed to find a label, it means the implicit declaration
3799 -- of the label was hidden. A for-loop parameter can do this to
3800 -- a label with the same name inside the loop, since the implicit
3801 -- label declaration is in the innermost enclosing body or block
3802 -- statement.
3804 else
3805 Error_Msg_Sloc := Sloc (Lab);
3806 Error_Msg_N
3807 ("implicit label declaration for & is hidden#",
3808 Identifier (S));
3809 end if;
3810 end if;
3812 Next (S);
3813 end loop;
3815 -- Perform semantic analysis on all statements
3817 Conditional_Statements_Begin;
3819 S := First (L);
3820 while Present (S) loop
3821 Analyze (S);
3823 -- Remove dimension in all statements
3825 Remove_Dimension_In_Statement (S);
3826 Next (S);
3827 end loop;
3829 Conditional_Statements_End;
3831 -- Make labels unreachable. Visibility is not sufficient, because labels
3832 -- in one if-branch for example are not reachable from the other branch,
3833 -- even though their declarations are in the enclosing declarative part.
3835 S := First (L);
3836 while Present (S) loop
3837 if Nkind (S) = N_Label then
3838 Set_Reachable (Entity (Identifier (S)), False);
3839 end if;
3841 Next (S);
3842 end loop;
3843 end Analyze_Statements;
3845 ----------------------------
3846 -- Check_Unreachable_Code --
3847 ----------------------------
3849 procedure Check_Unreachable_Code (N : Node_Id) is
3850 Error_Node : Node_Id;
3851 P : Node_Id;
3853 begin
3854 if Is_List_Member (N) and then Comes_From_Source (N) then
3855 declare
3856 Nxt : Node_Id;
3858 begin
3859 Nxt := Original_Node (Next (N));
3861 -- Skip past pragmas
3863 while Nkind (Nxt) = N_Pragma loop
3864 Nxt := Original_Node (Next (Nxt));
3865 end loop;
3867 -- If a label follows us, then we never have dead code, since
3868 -- someone could branch to the label, so we just ignore it, unless
3869 -- we are in formal mode where goto statements are not allowed.
3871 if Nkind (Nxt) = N_Label
3872 and then not Restriction_Check_Required (SPARK_05)
3873 then
3874 return;
3876 -- Otherwise see if we have a real statement following us
3878 elsif Present (Nxt)
3879 and then Comes_From_Source (Nxt)
3880 and then Is_Statement (Nxt)
3881 then
3882 -- Special very annoying exception. If we have a return that
3883 -- follows a raise, then we allow it without a warning, since
3884 -- the Ada RM annoyingly requires a useless return here.
3886 if Nkind (Original_Node (N)) /= N_Raise_Statement
3887 or else Nkind (Nxt) /= N_Simple_Return_Statement
3888 then
3889 -- The rather strange shenanigans with the warning message
3890 -- here reflects the fact that Kill_Dead_Code is very good
3891 -- at removing warnings in deleted code, and this is one
3892 -- warning we would prefer NOT to have removed.
3894 Error_Node := Nxt;
3896 -- If we have unreachable code, analyze and remove the
3897 -- unreachable code, since it is useless and we don't
3898 -- want to generate junk warnings.
3900 -- We skip this step if we are not in code generation mode
3901 -- or CodePeer mode.
3903 -- This is the one case where we remove dead code in the
3904 -- semantics as opposed to the expander, and we do not want
3905 -- to remove code if we are not in code generation mode,
3906 -- since this messes up the ASIS trees or loses useful
3907 -- information in the CodePeer tree.
3909 -- Note that one might react by moving the whole circuit to
3910 -- exp_ch5, but then we lose the warning in -gnatc mode.
3912 if Operating_Mode = Generate_Code
3913 and then not CodePeer_Mode
3914 then
3915 loop
3916 Nxt := Next (N);
3918 -- Quit deleting when we have nothing more to delete
3919 -- or if we hit a label (since someone could transfer
3920 -- control to a label, so we should not delete it).
3922 exit when No (Nxt) or else Nkind (Nxt) = N_Label;
3924 -- Statement/declaration is to be deleted
3926 Analyze (Nxt);
3927 Remove (Nxt);
3928 Kill_Dead_Code (Nxt);
3929 end loop;
3930 end if;
3932 -- Now issue the warning (or error in formal mode)
3934 if Restriction_Check_Required (SPARK_05) then
3935 Check_SPARK_05_Restriction
3936 ("unreachable code is not allowed", Error_Node);
3937 else
3938 Error_Msg
3939 ("??unreachable code!", Sloc (Error_Node), Error_Node);
3940 end if;
3941 end if;
3943 -- If the unconditional transfer of control instruction is the
3944 -- last statement of a sequence, then see if our parent is one of
3945 -- the constructs for which we count unblocked exits, and if so,
3946 -- adjust the count.
3948 else
3949 P := Parent (N);
3951 -- Statements in THEN part or ELSE part of IF statement
3953 if Nkind (P) = N_If_Statement then
3954 null;
3956 -- Statements in ELSIF part of an IF statement
3958 elsif Nkind (P) = N_Elsif_Part then
3959 P := Parent (P);
3960 pragma Assert (Nkind (P) = N_If_Statement);
3962 -- Statements in CASE statement alternative
3964 elsif Nkind (P) = N_Case_Statement_Alternative then
3965 P := Parent (P);
3966 pragma Assert (Nkind (P) = N_Case_Statement);
3968 -- Statements in body of block
3970 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
3971 and then Nkind (Parent (P)) = N_Block_Statement
3972 then
3973 -- The original loop is now placed inside a block statement
3974 -- due to the expansion of attribute 'Loop_Entry. Return as
3975 -- this is not a "real" block for the purposes of exit
3976 -- counting.
3978 if Nkind (N) = N_Loop_Statement
3979 and then Subject_To_Loop_Entry_Attributes (N)
3980 then
3981 return;
3982 end if;
3984 -- Statements in exception handler in a block
3986 elsif Nkind (P) = N_Exception_Handler
3987 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
3988 and then Nkind (Parent (Parent (P))) = N_Block_Statement
3989 then
3990 null;
3992 -- None of these cases, so return
3994 else
3995 return;
3996 end if;
3998 -- This was one of the cases we are looking for (i.e. the
3999 -- parent construct was IF, CASE or block) so decrement count.
4001 Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
4002 end if;
4003 end;
4004 end if;
4005 end Check_Unreachable_Code;
4007 ----------------------
4008 -- Preanalyze_Range --
4009 ----------------------
4011 procedure Preanalyze_Range (R_Copy : Node_Id) is
4012 Save_Analysis : constant Boolean := Full_Analysis;
4013 Typ : Entity_Id;
4015 begin
4016 Full_Analysis := False;
4017 Expander_Mode_Save_And_Set (False);
4019 Analyze (R_Copy);
4021 if Nkind (R_Copy) in N_Subexpr and then Is_Overloaded (R_Copy) then
4023 -- Apply preference rules for range of predefined integer types, or
4024 -- check for array or iterable construct for "of" iterator, or
4025 -- diagnose true ambiguity.
4027 declare
4028 I : Interp_Index;
4029 It : Interp;
4030 Found : Entity_Id := Empty;
4032 begin
4033 Get_First_Interp (R_Copy, I, It);
4034 while Present (It.Typ) loop
4035 if Is_Discrete_Type (It.Typ) then
4036 if No (Found) then
4037 Found := It.Typ;
4038 else
4039 if Scope (Found) = Standard_Standard then
4040 null;
4042 elsif Scope (It.Typ) = Standard_Standard then
4043 Found := It.Typ;
4045 else
4046 -- Both of them are user-defined
4048 Error_Msg_N
4049 ("ambiguous bounds in range of iteration", R_Copy);
4050 Error_Msg_N ("\possible interpretations:", R_Copy);
4051 Error_Msg_NE ("\\} ", R_Copy, Found);
4052 Error_Msg_NE ("\\} ", R_Copy, It.Typ);
4053 exit;
4054 end if;
4055 end if;
4057 elsif Nkind (Parent (R_Copy)) = N_Iterator_Specification
4058 and then Of_Present (Parent (R_Copy))
4059 then
4060 if Is_Array_Type (It.Typ)
4061 or else Has_Aspect (It.Typ, Aspect_Iterator_Element)
4062 or else Has_Aspect (It.Typ, Aspect_Constant_Indexing)
4063 or else Has_Aspect (It.Typ, Aspect_Variable_Indexing)
4064 then
4065 if No (Found) then
4066 Found := It.Typ;
4067 Set_Etype (R_Copy, It.Typ);
4069 else
4070 Error_Msg_N ("ambiguous domain of iteration", R_Copy);
4071 end if;
4072 end if;
4073 end if;
4075 Get_Next_Interp (I, It);
4076 end loop;
4077 end;
4078 end if;
4080 -- Subtype mark in iteration scheme
4082 if Is_Entity_Name (R_Copy) and then Is_Type (Entity (R_Copy)) then
4083 null;
4085 -- Expression in range, or Ada 2012 iterator
4087 elsif Nkind (R_Copy) in N_Subexpr then
4088 Resolve (R_Copy);
4089 Typ := Etype (R_Copy);
4091 if Is_Discrete_Type (Typ) then
4092 null;
4094 -- Check that the resulting object is an iterable container
4096 elsif Has_Aspect (Typ, Aspect_Iterator_Element)
4097 or else Has_Aspect (Typ, Aspect_Constant_Indexing)
4098 or else Has_Aspect (Typ, Aspect_Variable_Indexing)
4099 then
4100 null;
4102 -- The expression may yield an implicit reference to an iterable
4103 -- container. Insert explicit dereference so that proper type is
4104 -- visible in the loop.
4106 elsif Has_Implicit_Dereference (Etype (R_Copy)) then
4107 declare
4108 Disc : Entity_Id;
4110 begin
4111 Disc := First_Discriminant (Typ);
4112 while Present (Disc) loop
4113 if Has_Implicit_Dereference (Disc) then
4114 Build_Explicit_Dereference (R_Copy, Disc);
4115 exit;
4116 end if;
4118 Next_Discriminant (Disc);
4119 end loop;
4120 end;
4122 end if;
4123 end if;
4125 Expander_Mode_Restore;
4126 Full_Analysis := Save_Analysis;
4127 end Preanalyze_Range;
4129 end Sem_Ch5;