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