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