]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/exp_util.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / exp_util.adb
CommitLineData
70482933
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- E X P _ U T I L --
6-- --
7-- B o d y --
8-- --
e379beb5 9-- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
70482933
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
70482933
RK
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 --
b5c84c3c
RD
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. --
70482933
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
70482933
RK
23-- --
24------------------------------------------------------------------------------
25
2f7b7467 26with Aspects; use Aspects;
70482933 27with Atree; use Atree;
afbcdf5e 28with Casing; use Casing;
70482933 29with Checks; use Checks;
59e54267 30with Debug; use Debug;
70482933
RK
31with Einfo; use Einfo;
32with Elists; use Elists;
33with Errout; use Errout;
f44fe430 34with Exp_Aggr; use Exp_Aggr;
86cde7b1 35with Exp_Ch6; use Exp_Ch6;
70482933 36with Exp_Ch7; use Exp_Ch7;
241ebe89 37with Ghost; use Ghost;
70482933
RK
38with Inline; use Inline;
39with Itypes; use Itypes;
40with Lib; use Lib;
70482933
RK
41with Nlists; use Nlists;
42with Nmake; use Nmake;
43with Opt; use Opt;
44with Restrict; use Restrict;
6e937c1c 45with Rident; use Rident;
70482933 46with Sem; use Sem;
a4100e55 47with Sem_Aux; use Sem_Aux;
70482933 48with Sem_Ch8; use Sem_Ch8;
88fa9a24 49with Sem_Ch13; use Sem_Ch13;
70482933
RK
50with Sem_Eval; use Sem_Eval;
51with Sem_Res; use Sem_Res;
758c442c 52with Sem_Type; use Sem_Type;
70482933 53with Sem_Util; use Sem_Util;
fbf5a39b 54with Snames; use Snames;
70482933
RK
55with Stand; use Stand;
56with Stringt; use Stringt;
07fc65c4 57with Targparm; use Targparm;
70482933
RK
58with Tbuild; use Tbuild;
59with Ttypes; use Ttypes;
07fc65c4 60with Urealp; use Urealp;
70482933
RK
61with Validsw; use Validsw;
62
63package body Exp_Util is
64
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
68
69 function Build_Task_Array_Image
70 (Loc : Source_Ptr;
71 Id_Ref : Node_Id;
7bc1c7df 72 A_Type : Entity_Id;
bebbff91 73 Dyn : Boolean := False) return Node_Id;
273adcdf
AC
74 -- Build function to generate the image string for a task that is an array
75 -- component, concatenating the images of each index. To avoid storage
76 -- leaks, the string is built with successive slice assignments. The flag
77 -- Dyn indicates whether this is called for the initialization procedure of
78 -- an array of tasks, or for the name of a dynamically created task that is
79 -- assigned to an indexed component.
70482933
RK
80
81 function Build_Task_Image_Function
82 (Loc : Source_Ptr;
83 Decls : List_Id;
84 Stats : List_Id;
bebbff91 85 Res : Entity_Id) return Node_Id;
273adcdf
AC
86 -- Common processing for Task_Array_Image and Task_Record_Image. Build
87 -- function body that computes image.
70482933
RK
88
89 procedure Build_Task_Image_Prefix
90 (Loc : Source_Ptr;
91 Len : out Entity_Id;
92 Res : out Entity_Id;
93 Pos : out Entity_Id;
94 Prefix : Entity_Id;
95 Sum : Node_Id;
86cde7b1
RD
96 Decls : List_Id;
97 Stats : List_Id);
273adcdf
AC
98 -- Common processing for Task_Array_Image and Task_Record_Image. Create
99 -- local variables and assign prefix of name to result string.
70482933
RK
100
101 function Build_Task_Record_Image
102 (Loc : Source_Ptr;
103 Id_Ref : Node_Id;
bebbff91 104 Dyn : Boolean := False) return Node_Id;
273adcdf
AC
105 -- Build function to generate the image string for a task that is a record
106 -- component. Concatenate name of variable with that of selector. The flag
107 -- Dyn indicates whether this is called for the initialization procedure of
108 -- record with task components, or for a dynamically created task that is
109 -- assigned to a selected component.
70482933 110
08cd7c2f
AC
111 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
112 -- Force evaluation of bounds of a slice, which may be given by a range
113 -- or by a subtype indication with or without a constraint.
114
70482933 115 function Make_CW_Equivalent_Type
bebbff91
AC
116 (T : Entity_Id;
117 E : Node_Id) return Entity_Id;
70482933 118 -- T is a class-wide type entity, E is the initial expression node that
273adcdf
AC
119 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
120 -- returns the entity of the Equivalent type and inserts on the fly the
121 -- necessary declaration such as:
fbf5a39b 122 --
70482933
RK
123 -- type anon is record
124 -- _parent : Root_Type (T); constrained with E discriminants (if any)
125 -- Extension : String (1 .. expr to match size of E);
126 -- end record;
127 --
273adcdf
AC
128 -- This record is compatible with any object of the class of T thanks to
129 -- the first field and has the same size as E thanks to the second.
70482933
RK
130
131 function Make_Literal_Range
132 (Loc : Source_Ptr;
bebbff91 133 Literal_Typ : Entity_Id) return Node_Id;
70482933 134 -- Produce a Range node whose bounds are:
f91b40db 135 -- Low_Bound (Literal_Type) ..
86cde7b1 136 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
70482933 137 -- this is used for expanding declarations like X : String := "sdfgdfg";
86cde7b1
RD
138 --
139 -- If the index type of the target array is not integer, we generate:
140 -- Low_Bound (Literal_Type) ..
141 -- Literal_Type'Val
142 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
143 -- + (Length (Literal_Typ) -1))
70482933 144
b3b9865d
AC
145 function Make_Non_Empty_Check
146 (Loc : Source_Ptr;
147 N : Node_Id) return Node_Id;
148 -- Produce a boolean expression checking that the unidimensional array
149 -- node N is not empty.
150
70482933
RK
151 function New_Class_Wide_Subtype
152 (CW_Typ : Entity_Id;
bebbff91
AC
153 N : Node_Id) return Entity_Id;
154 -- Create an implicit subtype of CW_Typ attached to node N
70482933 155
87729e5a 156 function Requires_Cleanup_Actions
2ba7e31e 157 (L : List_Id;
fcf848c4 158 Lib_Level : Boolean;
2ba7e31e 159 Nested_Constructs : Boolean) return Boolean;
87729e5a
AC
160 -- Given a list L, determine whether it contains one of the following:
161 --
162 -- 1) controlled objects
163 -- 2) library-level tagged types
164 --
5f44f0d4
AC
165 -- Lib_Level is True when the list comes from a construct at the library
166 -- level, and False otherwise. Nested_Constructs is True when any nested
167 -- packages declared in L must be processed, and False otherwise.
87729e5a 168
4c318253
AC
169 -------------------------------------
170 -- Activate_Atomic_Synchronization --
171 -------------------------------------
172
173 procedure Activate_Atomic_Synchronization (N : Node_Id) is
174 Msg_Node : Node_Id;
175
176 begin
73fe1679 177 case Nkind (Parent (N)) is
73fe1679 178
6ec084f3
HK
179 -- Check for cases of appearing in the prefix of a construct where
180 -- we don't need atomic synchronization for this kind of usage.
181
182 when
183 -- Nothing to do if we are the prefix of an attribute, since we
184 -- do not want an atomic sync operation for things like 'Size.
185
186 N_Attribute_Reference |
187
188 -- The N_Reference node is like an attribute
73fe1679
AC
189
190 N_Reference |
191
6ec084f3
HK
192 -- Nothing to do for a reference to a component (or components)
193 -- of a composite object. Only reads and updates of the object
194 -- as a whole require atomic synchronization (RM C.6 (15)).
73fe1679
AC
195
196 N_Indexed_Component |
197 N_Selected_Component |
198 N_Slice =>
199
6ec084f3 200 -- For all the above cases, nothing to do if we are the prefix
73fe1679
AC
201
202 if Prefix (Parent (N)) = N then
203 return;
204 end if;
205
206 when others => null;
207 end case;
4c318253 208
6333ad3d 209 -- Nothing to do for the identifier in an object renaming declaration,
47b79f78 210 -- the renaming itself does not need atomic synchronization.
6333ad3d
AC
211
212 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
213 return;
214 end if;
215
4c318253
AC
216 -- Go ahead and set the flag
217
218 Set_Atomic_Sync_Required (N);
219
220 -- Generate info message if requested
221
222 if Warn_On_Atomic_Synchronization then
223 case Nkind (N) is
224 when N_Identifier =>
225 Msg_Node := N;
226
227 when N_Selected_Component | N_Expanded_Name =>
228 Msg_Node := Selector_Name (N);
229
230 when N_Explicit_Dereference | N_Indexed_Component =>
231 Msg_Node := Empty;
232
233 when others =>
234 pragma Assert (False);
235 return;
236 end case;
237
238 if Present (Msg_Node) then
324ac540 239 Error_Msg_N
2e57f88b 240 ("info: atomic synchronization set for &?N?", Msg_Node);
4c318253 241 else
324ac540 242 Error_Msg_N
2e57f88b 243 ("info: atomic synchronization set?N?", N);
4c318253
AC
244 end if;
245 end if;
246 end Activate_Atomic_Synchronization;
247
70482933
RK
248 ----------------------
249 -- Adjust_Condition --
250 ----------------------
251
252 procedure Adjust_Condition (N : Node_Id) is
253 begin
254 if No (N) then
255 return;
256 end if;
257
258 declare
259 Loc : constant Source_Ptr := Sloc (N);
260 T : constant Entity_Id := Etype (N);
261 Ti : Entity_Id;
262
263 begin
a2773bd3
AC
264 -- Defend against a call where the argument has no type, or has a
265 -- type that is not Boolean. This can occur because of prior errors.
70482933
RK
266
267 if No (T) or else not Is_Boolean_Type (T) then
268 return;
269 end if;
270
271 -- Apply validity checking if needed
272
273 if Validity_Checks_On and Validity_Check_Tests then
274 Ensure_Valid (N);
275 end if;
276
277 -- Immediate return if standard boolean, the most common case,
278 -- where nothing needs to be done.
279
280 if Base_Type (T) = Standard_Boolean then
281 return;
282 end if;
283
284 -- Case of zero/non-zero semantics or non-standard enumeration
285 -- representation. In each case, we rewrite the node as:
286
287 -- ityp!(N) /= False'Enum_Rep
288
273adcdf
AC
289 -- where ityp is an integer type with large enough size to hold any
290 -- value of type T.
70482933
RK
291
292 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
293 if Esize (T) <= Esize (Standard_Integer) then
294 Ti := Standard_Integer;
295 else
296 Ti := Standard_Long_Long_Integer;
297 end if;
298
299 Rewrite (N,
300 Make_Op_Ne (Loc,
301 Left_Opnd => Unchecked_Convert_To (Ti, N),
302 Right_Opnd =>
303 Make_Attribute_Reference (Loc,
304 Attribute_Name => Name_Enum_Rep,
305 Prefix =>
306 New_Occurrence_Of (First_Literal (T), Loc))));
307 Analyze_And_Resolve (N, Standard_Boolean);
308
309 else
310 Rewrite (N, Convert_To (Standard_Boolean, N));
311 Analyze_And_Resolve (N, Standard_Boolean);
312 end if;
313 end;
314 end Adjust_Condition;
315
316 ------------------------
317 -- Adjust_Result_Type --
318 ------------------------
319
320 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
321 begin
322 -- Ignore call if current type is not Standard.Boolean
323
324 if Etype (N) /= Standard_Boolean then
325 return;
326 end if;
327
328 -- If result is already of correct type, nothing to do. Note that
329 -- this will get the most common case where everything has a type
330 -- of Standard.Boolean.
331
332 if Base_Type (T) = Standard_Boolean then
333 return;
334
335 else
336 declare
337 KP : constant Node_Kind := Nkind (Parent (N));
338
339 begin
340 -- If result is to be used as a Condition in the syntax, no need
341 -- to convert it back, since if it was changed to Standard.Boolean
342 -- using Adjust_Condition, that is just fine for this usage.
343
344 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
345 return;
346
347 -- If result is an operand of another logical operation, no need
348 -- to reset its type, since Standard.Boolean is just fine, and
349 -- such operations always do Adjust_Condition on their operands.
350
ac7120ce
RD
351 elsif KP in N_Op_Boolean
352 or else KP in N_Short_Circuit
70482933
RK
353 or else KP = N_Op_Not
354 then
355 return;
356
273adcdf
AC
357 -- Otherwise we perform a conversion from the current type, which
358 -- must be Standard.Boolean, to the desired type.
70482933
RK
359
360 else
361 Set_Analyzed (N);
362 Rewrite (N, Convert_To (T, N));
363 Analyze_And_Resolve (N, T);
364 end if;
365 end;
366 end if;
367 end Adjust_Result_Type;
368
369 --------------------------
370 -- Append_Freeze_Action --
371 --------------------------
372
373 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
05350ac6 374 Fnode : Node_Id;
70482933
RK
375
376 begin
377 Ensure_Freeze_Node (T);
378 Fnode := Freeze_Node (T);
379
59e54267 380 if No (Actions (Fnode)) then
3a3af4c3
AC
381 Set_Actions (Fnode, New_List (N));
382 else
383 Append (N, Actions (Fnode));
70482933
RK
384 end if;
385
70482933
RK
386 end Append_Freeze_Action;
387
388 ---------------------------
389 -- Append_Freeze_Actions --
390 ---------------------------
391
392 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
3a3af4c3 393 Fnode : Node_Id;
70482933
RK
394
395 begin
396 if No (L) then
397 return;
3a3af4c3
AC
398 end if;
399
400 Ensure_Freeze_Node (T);
401 Fnode := Freeze_Node (T);
70482933 402
3a3af4c3
AC
403 if No (Actions (Fnode)) then
404 Set_Actions (Fnode, L);
70482933 405 else
3a3af4c3 406 Append_List (L, Actions (Fnode));
70482933
RK
407 end if;
408 end Append_Freeze_Actions;
409
df3e68b1
HK
410 ------------------------------------
411 -- Build_Allocate_Deallocate_Proc --
412 ------------------------------------
413
414 procedure Build_Allocate_Deallocate_Proc
415 (N : Node_Id;
416 Is_Allocate : Boolean)
417 is
ca5af305
AC
418 Desig_Typ : Entity_Id;
419 Expr : Node_Id;
420 Pool_Id : Entity_Id;
421 Proc_To_Call : Node_Id := Empty;
422 Ptr_Typ : Entity_Id;
df3e68b1
HK
423
424 function Find_Object (E : Node_Id) return Node_Id;
425 -- Given an arbitrary expression of an allocator, try to find an object
426 -- reference in it, otherwise return the original expression.
427
428 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
429 -- Determine whether subprogram Subp denotes a custom allocate or
430 -- deallocate.
431
432 -----------------
433 -- Find_Object --
434 -----------------
435
436 function Find_Object (E : Node_Id) return Node_Id is
2c1b72d7 437 Expr : Node_Id;
df3e68b1
HK
438
439 begin
440 pragma Assert (Is_Allocate);
441
2c1b72d7
AC
442 Expr := E;
443 loop
31d922e3
AC
444 if Nkind (Expr) = N_Explicit_Dereference then
445 Expr := Prefix (Expr);
446
447 elsif Nkind (Expr) = N_Qualified_Expression then
2c1b72d7 448 Expr := Expression (Expr);
df3e68b1 449
31d922e3
AC
450 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
451
452 -- When interface class-wide types are involved in allocation,
453 -- the expander introduces several levels of address arithmetic
454 -- to perform dispatch table displacement. In this scenario the
455 -- object appears as:
f3920a13 456
31d922e3 457 -- Tag_Ptr (Base_Address (<object>'Address))
f3920a13 458
31d922e3
AC
459 -- Detect this case and utilize the whole expression as the
460 -- "object" since it now points to the proper dispatch table.
461
462 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
463 exit;
464
465 -- Continue to strip the object
466
467 else
468 Expr := Expression (Expr);
469 end if;
2c1b72d7
AC
470
471 else
472 exit;
df3e68b1
HK
473 end if;
474 end loop;
475
476 return Expr;
477 end Find_Object;
478
479 ---------------------------------
480 -- Is_Allocate_Deallocate_Proc --
481 ---------------------------------
482
483 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
484 begin
485 -- Look for a subprogram body with only one statement which is a
d3f70b35 486 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
df3e68b1
HK
487
488 if Ekind (Subp) = E_Procedure
489 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
490 then
491 declare
492 HSS : constant Node_Id :=
493 Handled_Statement_Sequence (Parent (Parent (Subp)));
494 Proc : Entity_Id;
495
496 begin
497 if Present (Statements (HSS))
498 and then Nkind (First (Statements (HSS))) =
499 N_Procedure_Call_Statement
500 then
501 Proc := Entity (Name (First (Statements (HSS))));
502
503 return
d3f70b35
AC
504 Is_RTE (Proc, RE_Allocate_Any_Controlled)
505 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
df3e68b1
HK
506 end if;
507 end;
508 end if;
509
510 return False;
511 end Is_Allocate_Deallocate_Proc;
512
513 -- Start of processing for Build_Allocate_Deallocate_Proc
514
515 begin
ca5af305
AC
516 -- Obtain the attributes of the allocation / deallocation
517
518 if Nkind (N) = N_Free_Statement then
519 Expr := Expression (N);
520 Ptr_Typ := Base_Type (Etype (Expr));
521 Proc_To_Call := Procedure_To_Call (N);
522
523 else
524 if Nkind (N) = N_Object_Declaration then
525 Expr := Expression (N);
526 else
527 Expr := N;
528 end if;
529
f7bb41af
AC
530 -- In certain cases an allocator with a qualified expression may
531 -- be relocated and used as the initialization expression of a
532 -- temporary:
533
534 -- before:
535 -- Obj : Ptr_Typ := new Desig_Typ'(...);
536
537 -- after:
538 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
539 -- Obj : Ptr_Typ := Tmp;
540
541 -- Since the allocator is always marked as analyzed to avoid infinite
542 -- expansion, it will never be processed by this routine given that
543 -- the designated type needs finalization actions. Detect this case
544 -- and complete the expansion of the allocator.
545
546 if Nkind (Expr) = N_Identifier
547 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
548 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
549 then
550 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
551 return;
552 end if;
ca5af305 553
f7bb41af
AC
554 -- The allocator may have been rewritten into something else in which
555 -- case the expansion performed by this routine does not apply.
ca5af305 556
f7bb41af
AC
557 if Nkind (Expr) /= N_Allocator then
558 return;
ca5af305 559 end if;
f7bb41af
AC
560
561 Ptr_Typ := Base_Type (Etype (Expr));
562 Proc_To_Call := Procedure_To_Call (Expr);
ca5af305
AC
563 end if;
564
565 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
566 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
df3e68b1 567
ca5af305
AC
568 -- Handle concurrent types
569
570 if Is_Concurrent_Type (Desig_Typ)
571 and then Present (Corresponding_Record_Type (Desig_Typ))
572 then
573 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
574 end if;
575
576 -- Do not process allocations / deallocations without a pool
577
578 if No (Pool_Id) then
df3e68b1
HK
579 return;
580
ca5af305
AC
581 -- Do not process allocations on / deallocations from the secondary
582 -- stack.
583
584 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
585 return;
586
fc3819c9
AC
587 -- Optimize the case where we are using the default Global_Pool_Object,
588 -- and we don't need the heavy finalization machinery.
589
590 elsif Pool_Id = RTE (RE_Global_Pool_Object)
591 and then not Needs_Finalization (Desig_Typ)
592 then
593 return;
594
ca5af305
AC
595 -- Do not replicate the machinery if the allocator / free has already
596 -- been expanded and has a custom Allocate / Deallocate.
597
598 elsif Present (Proc_To_Call)
599 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
600 then
601 return;
602 end if;
603
604 if Needs_Finalization (Desig_Typ) then
605
606 -- Certain run-time configurations and targets do not provide support
607 -- for controlled types.
608
609 if Restriction_Active (No_Finalization) then
610 return;
611
612 -- Do nothing if the access type may never allocate / deallocate
613 -- objects.
614
615 elsif No_Pool_Assigned (Ptr_Typ) then
616 return;
ca5af305
AC
617 end if;
618
619 -- The allocation / deallocation of a controlled object must be
620 -- chained on / detached from a finalization master.
621
622 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
623
624 -- The only other kind of allocation / deallocation supported by this
625 -- routine is on / from a subpool.
df3e68b1
HK
626
627 elsif Nkind (Expr) = N_Allocator
ca5af305 628 and then No (Subpool_Handle_Name (Expr))
df3e68b1
HK
629 then
630 return;
631 end if;
632
633 declare
634 Loc : constant Source_Ptr := Sloc (N);
635 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
636 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
637 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
638 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
639
640 Actuals : List_Id;
d3f70b35
AC
641 Fin_Addr_Id : Entity_Id;
642 Fin_Mas_Act : Node_Id;
643 Fin_Mas_Id : Entity_Id;
df3e68b1 644 Proc_To_Call : Entity_Id;
ca5af305 645 Subpool : Node_Id := Empty;
df3e68b1
HK
646
647 begin
d3f70b35
AC
648 -- Step 1: Construct all the actuals for the call to library routine
649 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
df3e68b1 650
d3f70b35 651 -- a) Storage pool
df3e68b1 652
e4494292 653 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
df3e68b1 654
d3f70b35 655 if Is_Allocate then
df3e68b1 656
d3f70b35 657 -- b) Subpool
df3e68b1 658
ca5af305
AC
659 if Nkind (Expr) = N_Allocator then
660 Subpool := Subpool_Handle_Name (Expr);
661 end if;
662
4bb43ffb
AC
663 -- If a subpool is present it can be an arbitrary name, so make
664 -- the actual by copying the tree.
665
ca5af305 666 if Present (Subpool) then
4bb43ffb 667 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
d3f70b35
AC
668 else
669 Append_To (Actuals, Make_Null (Loc));
670 end if;
df3e68b1 671
d3f70b35
AC
672 -- c) Finalization master
673
674 if Needs_Finalization (Desig_Typ) then
ca5af305 675 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
e4494292 676 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
d3f70b35
AC
677
678 -- Handle the case where the master is actually a pointer to a
679 -- master. This case arises in build-in-place functions.
680
681 if Is_Access_Type (Etype (Fin_Mas_Id)) then
682 Append_To (Actuals, Fin_Mas_Act);
df3e68b1 683 else
d3f70b35
AC
684 Append_To (Actuals,
685 Make_Attribute_Reference (Loc,
686 Prefix => Fin_Mas_Act,
687 Attribute_Name => Name_Unrestricted_Access));
df3e68b1 688 end if;
d3f70b35
AC
689 else
690 Append_To (Actuals, Make_Null (Loc));
691 end if;
df3e68b1 692
d3f70b35 693 -- d) Finalize_Address
df3e68b1 694
60370fb1
AC
695 -- Primitive Finalize_Address is never generated in CodePeer mode
696 -- since it contains an Unchecked_Conversion.
df3e68b1 697
0e564ab4 698 if Needs_Finalization (Desig_Typ) and then not CodePeer_Mode then
760804f3 699 Fin_Addr_Id := Finalize_Address (Desig_Typ);
ca5af305
AC
700 pragma Assert (Present (Fin_Addr_Id));
701
d3f70b35
AC
702 Append_To (Actuals,
703 Make_Attribute_Reference (Loc,
e4494292 704 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
d3f70b35
AC
705 Attribute_Name => Name_Unrestricted_Access));
706 else
707 Append_To (Actuals, Make_Null (Loc));
708 end if;
709 end if;
df3e68b1 710
d3f70b35
AC
711 -- e) Address
712 -- f) Storage_Size
713 -- g) Alignment
df3e68b1 714
e4494292
RD
715 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
716 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
6bed26b5 717
033eaf85 718 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
e4494292 719 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
6bed26b5 720
f3296dd3 721 -- For deallocation of class-wide types we obtain the value of
6bed26b5
AC
722 -- alignment from the Type Specific Record of the deallocated object.
723 -- This is needed because the frontend expansion of class-wide types
724 -- into equivalent types confuses the backend.
725
726 else
727 -- Generate:
728 -- Obj.all'Alignment
729
730 -- ... because 'Alignment applied to class-wide types is expanded
731 -- into the code that reads the value of alignment from the TSD
732 -- (see Expand_N_Attribute_Reference)
733
734 Append_To (Actuals,
735 Unchecked_Convert_To (RTE (RE_Storage_Offset),
736 Make_Attribute_Reference (Loc,
033eaf85 737 Prefix =>
6bed26b5
AC
738 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
739 Attribute_Name => Name_Alignment)));
740 end if;
df3e68b1 741
d3f70b35 742 -- h) Is_Controlled
df3e68b1 743
d3f70b35 744 if Needs_Finalization (Desig_Typ) then
31d922e3
AC
745 declare
746 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
747 Flag_Expr : Node_Id;
748 Param : Node_Id;
749 Temp : Node_Id;
750
751 begin
752 if Is_Allocate then
753 Temp := Find_Object (Expression (Expr));
754 else
755 Temp := Expr;
756 end if;
df3e68b1 757
31d922e3
AC
758 -- Processing for allocations where the expression is a subtype
759 -- indication.
df3e68b1 760
31d922e3
AC
761 if Is_Allocate
762 and then Is_Entity_Name (Temp)
763 and then Is_Type (Entity (Temp))
764 then
765 Flag_Expr :=
e4494292 766 New_Occurrence_Of
f3920a13
AC
767 (Boolean_Literals
768 (Needs_Finalization (Entity (Temp))), Loc);
df3e68b1 769
31d922e3
AC
770 -- The allocation / deallocation of a class-wide object relies
771 -- on a runtime check to determine whether the object is truly
772 -- controlled or not. Depending on this check, the finalization
773 -- machinery will request or reclaim extra storage reserved for
774 -- a list header.
df3e68b1 775
31d922e3 776 elsif Is_Class_Wide_Type (Desig_Typ) then
df3e68b1 777
31d922e3
AC
778 -- Detect a special case where interface class-wide types
779 -- are involved as the object appears as:
f3920a13 780
31d922e3 781 -- Tag_Ptr (Base_Address (<object>'Address))
f3920a13 782
31d922e3 783 -- The expression already yields the proper tag, generate:
f3920a13 784
31d922e3
AC
785 -- Temp.all
786
787 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
788 Param :=
789 Make_Explicit_Dereference (Loc,
790 Prefix => Relocate_Node (Temp));
791
792 -- In the default case, obtain the tag of the object about
793 -- to be allocated / deallocated. Generate:
f3920a13 794
31d922e3 795 -- Temp'Tag
df3e68b1 796
d3f70b35 797 else
31d922e3
AC
798 Param :=
799 Make_Attribute_Reference (Loc,
800 Prefix => Relocate_Node (Temp),
801 Attribute_Name => Name_Tag);
d3f70b35
AC
802 end if;
803
31d922e3
AC
804 -- Generate:
805 -- Needs_Finalization (<Param>)
d3f70b35 806
31d922e3
AC
807 Flag_Expr :=
808 Make_Function_Call (Loc,
809 Name =>
e4494292 810 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
31d922e3 811 Parameter_Associations => New_List (Param));
d3f70b35 812
31d922e3 813 -- Processing for generic actuals
d3f70b35 814
31d922e3
AC
815 elsif Is_Generic_Actual_Type (Desig_Typ) then
816 Flag_Expr :=
e4494292 817 New_Occurrence_Of (Boolean_Literals
31d922e3 818 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
ca5af305 819
31d922e3
AC
820 -- The object does not require any specialized checks, it is
821 -- known to be controlled.
ca5af305 822
31d922e3 823 else
e4494292 824 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
31d922e3
AC
825 end if;
826
827 -- Create the temporary which represents the finalization state
828 -- of the expression. Generate:
829 --
830 -- F : constant Boolean := <Flag_Expr>;
831
832 Insert_Action (N,
833 Make_Object_Declaration (Loc,
834 Defining_Identifier => Flag_Id,
835 Constant_Present => True,
836 Object_Definition =>
e4494292 837 New_Occurrence_Of (Standard_Boolean, Loc),
31d922e3
AC
838 Expression => Flag_Expr));
839
e4494292 840 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
31d922e3
AC
841 end;
842
843 -- The object is not controlled
033eaf85 844
d3f70b35 845 else
e4494292 846 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
df3e68b1
HK
847 end if;
848
ca5af305
AC
849 -- i) On_Subpool
850
851 if Is_Allocate then
852 Append_To (Actuals,
e4494292 853 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
ca5af305
AC
854 end if;
855
d3f70b35
AC
856 -- Step 2: Build a wrapper Allocate / Deallocate which internally
857 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
858
df3e68b1
HK
859 -- Select the proper routine to call
860
861 if Is_Allocate then
d3f70b35 862 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
df3e68b1 863 else
d3f70b35 864 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
df3e68b1
HK
865 end if;
866
867 -- Create a custom Allocate / Deallocate routine which has identical
868 -- profile to that of System.Storage_Pools.
869
870 Insert_Action (N,
871 Make_Subprogram_Body (Loc,
872 Specification =>
873
874 -- procedure Pnn
875
876 Make_Procedure_Specification (Loc,
877 Defining_Unit_Name => Proc_Id,
878 Parameter_Specifications => New_List (
879
880 -- P : Root_Storage_Pool
881
882 Make_Parameter_Specification (Loc,
033eaf85 883 Defining_Identifier => Make_Temporary (Loc, 'P'),
df3e68b1 884 Parameter_Type =>
e4494292 885 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
df3e68b1
HK
886
887 -- A : [out] Address
888
889 Make_Parameter_Specification (Loc,
890 Defining_Identifier => Addr_Id,
033eaf85
AC
891 Out_Present => Is_Allocate,
892 Parameter_Type =>
e4494292 893 New_Occurrence_Of (RTE (RE_Address), Loc)),
df3e68b1
HK
894
895 -- S : Storage_Count
896
897 Make_Parameter_Specification (Loc,
898 Defining_Identifier => Size_Id,
033eaf85 899 Parameter_Type =>
e4494292 900 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
df3e68b1
HK
901
902 -- L : Storage_Count
903
904 Make_Parameter_Specification (Loc,
905 Defining_Identifier => Alig_Id,
033eaf85 906 Parameter_Type =>
e4494292 907 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
df3e68b1
HK
908
909 Declarations => No_List,
910
911 Handled_Statement_Sequence =>
912 Make_Handled_Sequence_Of_Statements (Loc,
913 Statements => New_List (
df3e68b1 914 Make_Procedure_Call_Statement (Loc,
e4494292 915 Name => New_Occurrence_Of (Proc_To_Call, Loc),
df3e68b1
HK
916 Parameter_Associations => Actuals)))));
917
918 -- The newly generated Allocate / Deallocate becomes the default
919 -- procedure to call when the back end processes the allocation /
920 -- deallocation.
921
922 if Is_Allocate then
923 Set_Procedure_To_Call (Expr, Proc_Id);
924 else
925 Set_Procedure_To_Call (N, Proc_Id);
926 end if;
927 end;
928 end Build_Allocate_Deallocate_Proc;
929
51b42ffa
AC
930 --------------------------
931 -- Build_Procedure_Form --
932 --------------------------
933
934 procedure Build_Procedure_Form (N : Node_Id) is
268aeaa9
AC
935 Loc : constant Source_Ptr := Sloc (N);
936 Subp : constant Entity_Id := Defining_Entity (N);
51b42ffa
AC
937
938 Func_Formal : Entity_Id;
939 Proc_Formals : List_Id;
17fd72ce 940 Proc_Decl : Node_Id;
51b42ffa
AC
941
942 begin
2a253c5b
AC
943 -- No action needed if this transformation was already done, or in case
944 -- of subprogram renaming declarations.
aeb98f1d 945
a14bbbb4
AC
946 if Nkind (Specification (N)) = N_Procedure_Specification
947 or else Nkind (N) = N_Subprogram_Renaming_Declaration
948 then
aeb98f1d
JM
949 return;
950 end if;
951
2a253c5b
AC
952 -- Ditto when dealing with an expression function, where both the
953 -- original expression and the generated declaration end up being
954 -- expanded here.
955
956 if Rewritten_For_C (Subp) then
957 return;
958 end if;
959
51b42ffa
AC
960 Proc_Formals := New_List;
961
962 -- Create a list of formal parameters with the same types as the
963 -- function.
964
965 Func_Formal := First_Formal (Subp);
966 while Present (Func_Formal) loop
967 Append_To (Proc_Formals,
968 Make_Parameter_Specification (Loc,
969 Defining_Identifier =>
51b42ffa
AC
970 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
971 Parameter_Type =>
972 New_Occurrence_Of (Etype (Func_Formal), Loc)));
973
974 Next_Formal (Func_Formal);
975 end loop;
976
977 -- Add an extra out parameter to carry the function result
978
979 Name_Len := 6;
980 Name_Buffer (1 .. Name_Len) := "RESULT";
981 Append_To (Proc_Formals,
982 Make_Parameter_Specification (Loc,
983 Defining_Identifier =>
984 Make_Defining_Identifier (Loc, Chars => Name_Find),
985 Out_Present => True,
986 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
987
988 -- The new procedure declaration is inserted immediately after the
989 -- function declaration. The processing in Build_Procedure_Body_Form
990 -- relies on this order.
991
17fd72ce 992 Proc_Decl :=
51b42ffa
AC
993 Make_Subprogram_Declaration (Loc,
994 Specification =>
995 Make_Procedure_Specification (Loc,
996 Defining_Unit_Name =>
997 Make_Defining_Identifier (Loc, Chars (Subp)),
17fd72ce
ES
998 Parameter_Specifications => Proc_Formals));
999
1000 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
1001
1002 -- Entity of procedure must remain invisible so that it does not
1003 -- overload subsequent references to the original function.
1004
1005 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
51b42ffa 1006
888be6b1
AC
1007 -- Mark the function as having a procedure form and link the function
1008 -- and its internally built procedure.
51b42ffa
AC
1009
1010 Set_Rewritten_For_C (Subp);
888be6b1
AC
1011 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
1012 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
51b42ffa
AC
1013 end Build_Procedure_Form;
1014
70482933
RK
1015 ------------------------
1016 -- Build_Runtime_Call --
1017 ------------------------
1018
1019 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
1020 begin
fbf5a39b
AC
1021 -- If entity is not available, we can skip making the call (this avoids
1022 -- junk duplicated error messages in a number of cases).
1023
1024 if not RTE_Available (RE) then
1025 return Make_Null_Statement (Loc);
1026 else
1027 return
1028 Make_Procedure_Call_Statement (Loc,
e4494292 1029 Name => New_Occurrence_Of (RTE (RE), Loc));
fbf5a39b 1030 end if;
70482933
RK
1031 end Build_Runtime_Call;
1032
8e888920
AC
1033 ------------------------
1034 -- Build_SS_Mark_Call --
1035 ------------------------
1036
1037 function Build_SS_Mark_Call
1038 (Loc : Source_Ptr;
1039 Mark : Entity_Id) return Node_Id
1040 is
1041 begin
1042 -- Generate:
1043 -- Mark : constant Mark_Id := SS_Mark;
1044
1045 return
1046 Make_Object_Declaration (Loc,
1047 Defining_Identifier => Mark,
1048 Constant_Present => True,
1049 Object_Definition =>
1050 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
1051 Expression =>
1052 Make_Function_Call (Loc,
1053 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
1054 end Build_SS_Mark_Call;
1055
1056 ---------------------------
1057 -- Build_SS_Release_Call --
1058 ---------------------------
1059
1060 function Build_SS_Release_Call
1061 (Loc : Source_Ptr;
1062 Mark : Entity_Id) return Node_Id
1063 is
1064 begin
1065 -- Generate:
1066 -- SS_Release (Mark);
1067
1068 return
1069 Make_Procedure_Call_Statement (Loc,
1070 Name =>
1071 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
1072 Parameter_Associations => New_List (
1073 New_Occurrence_Of (Mark, Loc)));
1074 end Build_SS_Release_Call;
1075
15ce9ca2
AC
1076 ----------------------------
1077 -- Build_Task_Array_Image --
1078 ----------------------------
70482933
RK
1079
1080 -- This function generates the body for a function that constructs the
1081 -- image string for a task that is an array component. The function is
fbf5a39b 1082 -- local to the init proc for the array type, and is called for each one
70482933
RK
1083 -- of the components. The constructed image has the form of an indexed
1084 -- component, whose prefix is the outer variable of the array type.
3b42c566 1085 -- The n-dimensional array type has known indexes Index, Index2...
273adcdf 1086
fbf5a39b 1087 -- Id_Ref is an indexed component form created by the enclosing init proc.
3b42c566 1088 -- Its successive indexes are Val1, Val2, ... which are the loop variables
fbf5a39b 1089 -- in the loops that call the individual task init proc on each component.
70482933
RK
1090
1091 -- The generated function has the following structure:
1092
fbf5a39b
AC
1093 -- function F return String is
1094 -- Pref : string renames Task_Name;
1095 -- T1 : String := Index1'Image (Val1);
70482933 1096 -- ...
fbf5a39b
AC
1097 -- Tn : String := indexn'image (Valn);
1098 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
70482933 1099 -- -- Len includes commas and the end parentheses.
fbf5a39b
AC
1100 -- Res : String (1..Len);
1101 -- Pos : Integer := Pref'Length;
70482933
RK
1102 --
1103 -- begin
7bc1c7df 1104 -- Res (1 .. Pos) := Pref;
70482933
RK
1105 -- Pos := Pos + 1;
1106 -- Res (Pos) := '(';
1107 -- Pos := Pos + 1;
1108 -- Res (Pos .. Pos + T1'Length - 1) := T1;
1109 -- Pos := Pos + T1'Length;
1110 -- Res (Pos) := '.';
1111 -- Pos := Pos + 1;
1112 -- ...
1113 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
1114 -- Res (Len) := ')';
1115 --
fbf5a39b 1116 -- return Res;
70482933
RK
1117 -- end F;
1118 --
273adcdf
AC
1119 -- Needless to say, multidimensional arrays of tasks are rare enough that
1120 -- the bulkiness of this code is not really a concern.
70482933
RK
1121
1122 function Build_Task_Array_Image
1123 (Loc : Source_Ptr;
1124 Id_Ref : Node_Id;
7bc1c7df 1125 A_Type : Entity_Id;
bebbff91 1126 Dyn : Boolean := False) return Node_Id
70482933
RK
1127 is
1128 Dims : constant Nat := Number_Dimensions (A_Type);
bebbff91 1129 -- Number of dimensions for array of tasks
70482933
RK
1130
1131 Temps : array (1 .. Dims) of Entity_Id;
bebbff91 1132 -- Array of temporaries to hold string for each index
70482933
RK
1133
1134 Indx : Node_Id;
1135 -- Index expression
1136
1137 Len : Entity_Id;
1138 -- Total length of generated name
1139
1140 Pos : Entity_Id;
1141 -- Running index for substring assignments
1142
092ef350 1143 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
70482933
RK
1144 -- Name of enclosing variable, prefix of resulting name
1145
1146 Res : Entity_Id;
1147 -- String to hold result
1148
1149 Val : Node_Id;
3b42c566 1150 -- Value of successive indexes
70482933
RK
1151
1152 Sum : Node_Id;
1153 -- Expression to compute total size of string
1154
1155 T : Entity_Id;
1156 -- Entity for name at one index position
1157
86cde7b1
RD
1158 Decls : constant List_Id := New_List;
1159 Stats : constant List_Id := New_List;
70482933
RK
1160
1161 begin
273adcdf
AC
1162 -- For a dynamic task, the name comes from the target variable. For a
1163 -- static one it is a formal of the enclosing init proc.
7bc1c7df
ES
1164
1165 if Dyn then
1166 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
fbf5a39b
AC
1167 Append_To (Decls,
1168 Make_Object_Declaration (Loc,
1169 Defining_Identifier => Pref,
1170 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1171 Expression =>
bebbff91
AC
1172 Make_String_Literal (Loc,
1173 Strval => String_From_Name_Buffer)));
fbf5a39b 1174
7bc1c7df 1175 else
fbf5a39b
AC
1176 Append_To (Decls,
1177 Make_Object_Renaming_Declaration (Loc,
1178 Defining_Identifier => Pref,
1179 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1180 Name => Make_Identifier (Loc, Name_uTask_Name)));
7bc1c7df 1181 end if;
70482933 1182
70482933
RK
1183 Indx := First_Index (A_Type);
1184 Val := First (Expressions (Id_Ref));
1185
1186 for J in 1 .. Dims loop
092ef350 1187 T := Make_Temporary (Loc, 'T');
70482933
RK
1188 Temps (J) := T;
1189
1190 Append_To (Decls,
18a2ad5d
AC
1191 Make_Object_Declaration (Loc,
1192 Defining_Identifier => T,
1193 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1194 Expression =>
1195 Make_Attribute_Reference (Loc,
1196 Attribute_Name => Name_Image,
1197 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
1198 Expressions => New_List (New_Copy_Tree (Val)))));
70482933
RK
1199
1200 Next_Index (Indx);
1201 Next (Val);
1202 end loop;
1203
1204 Sum := Make_Integer_Literal (Loc, Dims + 1);
1205
1206 Sum :=
1207 Make_Op_Add (Loc,
1208 Left_Opnd => Sum,
1209 Right_Opnd =>
18a2ad5d
AC
1210 Make_Attribute_Reference (Loc,
1211 Attribute_Name => Name_Length,
1212 Prefix => New_Occurrence_Of (Pref, Loc),
1213 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
70482933
RK
1214
1215 for J in 1 .. Dims loop
1216 Sum :=
18a2ad5d
AC
1217 Make_Op_Add (Loc,
1218 Left_Opnd => Sum,
70482933 1219 Right_Opnd =>
18a2ad5d
AC
1220 Make_Attribute_Reference (Loc,
1221 Attribute_Name => Name_Length,
1222 Prefix =>
70482933 1223 New_Occurrence_Of (Temps (J), Loc),
18a2ad5d 1224 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
70482933
RK
1225 end loop;
1226
7bc1c7df 1227 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
70482933
RK
1228
1229 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
1230
1231 Append_To (Stats,
18a2ad5d
AC
1232 Make_Assignment_Statement (Loc,
1233 Name =>
1234 Make_Indexed_Component (Loc,
1235 Prefix => New_Occurrence_Of (Res, Loc),
70482933 1236 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
18a2ad5d
AC
1237 Expression =>
1238 Make_Character_Literal (Loc,
1239 Chars => Name_Find,
1240 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
70482933
RK
1241
1242 Append_To (Stats,
18a2ad5d
AC
1243 Make_Assignment_Statement (Loc,
1244 Name => New_Occurrence_Of (Pos, Loc),
1245 Expression =>
1246 Make_Op_Add (Loc,
1247 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1248 Right_Opnd => Make_Integer_Literal (Loc, 1))));
70482933
RK
1249
1250 for J in 1 .. Dims loop
1251
1252 Append_To (Stats,
18a2ad5d
AC
1253 Make_Assignment_Statement (Loc,
1254 Name =>
1255 Make_Slice (Loc,
1256 Prefix => New_Occurrence_Of (Res, Loc),
70482933
RK
1257 Discrete_Range =>
1258 Make_Range (Loc,
18a2ad5d
AC
1259 Low_Bound => New_Occurrence_Of (Pos, Loc),
1260 High_Bound =>
1261 Make_Op_Subtract (Loc,
1262 Left_Opnd =>
1263 Make_Op_Add (Loc,
1264 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1265 Right_Opnd =>
1266 Make_Attribute_Reference (Loc,
1267 Attribute_Name => Name_Length,
1268 Prefix =>
1269 New_Occurrence_Of (Temps (J), Loc),
1270 Expressions =>
1271 New_List (Make_Integer_Literal (Loc, 1)))),
70482933
RK
1272 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
1273
1274 Expression => New_Occurrence_Of (Temps (J), Loc)));
1275
1276 if J < Dims then
1277 Append_To (Stats,
1278 Make_Assignment_Statement (Loc,
18a2ad5d 1279 Name => New_Occurrence_Of (Pos, Loc),
70482933
RK
1280 Expression =>
1281 Make_Op_Add (Loc,
18a2ad5d 1282 Left_Opnd => New_Occurrence_Of (Pos, Loc),
70482933
RK
1283 Right_Opnd =>
1284 Make_Attribute_Reference (Loc,
1285 Attribute_Name => Name_Length,
18a2ad5d
AC
1286 Prefix => New_Occurrence_Of (Temps (J), Loc),
1287 Expressions =>
1288 New_List (Make_Integer_Literal (Loc, 1))))));
70482933
RK
1289
1290 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
1291
1292 Append_To (Stats,
18a2ad5d
AC
1293 Make_Assignment_Statement (Loc,
1294 Name => Make_Indexed_Component (Loc,
1295 Prefix => New_Occurrence_Of (Res, Loc),
1296 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1297 Expression =>
1298 Make_Character_Literal (Loc,
1299 Chars => Name_Find,
1300 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
70482933
RK
1301
1302 Append_To (Stats,
1303 Make_Assignment_Statement (Loc,
18a2ad5d 1304 Name => New_Occurrence_Of (Pos, Loc),
70482933
RK
1305 Expression =>
1306 Make_Op_Add (Loc,
18a2ad5d 1307 Left_Opnd => New_Occurrence_Of (Pos, Loc),
70482933
RK
1308 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1309 end if;
1310 end loop;
1311
1312 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
1313
1314 Append_To (Stats,
18a2ad5d
AC
1315 Make_Assignment_Statement (Loc,
1316 Name =>
1317 Make_Indexed_Component (Loc,
1318 Prefix => New_Occurrence_Of (Res, Loc),
70482933
RK
1319 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
1320 Expression =>
1321 Make_Character_Literal (Loc,
18a2ad5d
AC
1322 Chars => Name_Find,
1323 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
70482933
RK
1324 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1325 end Build_Task_Array_Image;
1326
1327 ----------------------------
1328 -- Build_Task_Image_Decls --
1329 ----------------------------
1330
1331 function Build_Task_Image_Decls
05350ac6
BD
1332 (Loc : Source_Ptr;
1333 Id_Ref : Node_Id;
1334 A_Type : Entity_Id;
1335 In_Init_Proc : Boolean := False) return List_Id
70482933 1336 is
fbf5a39b 1337 Decls : constant List_Id := New_List;
7bc1c7df
ES
1338 T_Id : Entity_Id := Empty;
1339 Decl : Node_Id;
7bc1c7df
ES
1340 Expr : Node_Id := Empty;
1341 Fun : Node_Id := Empty;
1342 Is_Dyn : constant Boolean :=
fbf5a39b
AC
1343 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
1344 and then
1345 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
70482933
RK
1346
1347 begin
fbf5a39b
AC
1348 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
1349 -- generate a dummy declaration only.
70482933 1350
6e937c1c 1351 if Restriction_Active (No_Implicit_Heap_Allocations)
fbf5a39b
AC
1352 or else Global_Discard_Names
1353 then
092ef350 1354 T_Id := Make_Temporary (Loc, 'J');
fbf5a39b 1355 Name_Len := 0;
70482933
RK
1356
1357 return
1358 New_List (
1359 Make_Object_Declaration (Loc,
1360 Defining_Identifier => T_Id,
fbf5a39b
AC
1361 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1362 Expression =>
bebbff91
AC
1363 Make_String_Literal (Loc,
1364 Strval => String_From_Name_Buffer)));
70482933
RK
1365
1366 else
1367 if Nkind (Id_Ref) = N_Identifier
1368 or else Nkind (Id_Ref) = N_Defining_Identifier
1369 then
523456db 1370 -- For a simple variable, the image of the task is built from
273adcdf
AC
1371 -- the name of the variable. To avoid possible conflict with the
1372 -- anonymous type created for a single protected object, add a
1373 -- numeric suffix.
70482933
RK
1374
1375 T_Id :=
1376 Make_Defining_Identifier (Loc,
523456db 1377 New_External_Name (Chars (Id_Ref), 'T', 1));
70482933
RK
1378
1379 Get_Name_String (Chars (Id_Ref));
1380
bebbff91
AC
1381 Expr :=
1382 Make_String_Literal (Loc,
1383 Strval => String_From_Name_Buffer);
70482933
RK
1384
1385 elsif Nkind (Id_Ref) = N_Selected_Component then
1386 T_Id :=
1387 Make_Defining_Identifier (Loc,
fbf5a39b 1388 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
07fc65c4 1389 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
70482933
RK
1390
1391 elsif Nkind (Id_Ref) = N_Indexed_Component then
1392 T_Id :=
1393 Make_Defining_Identifier (Loc,
fbf5a39b 1394 New_External_Name (Chars (A_Type), 'N'));
70482933 1395
7bc1c7df 1396 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
70482933
RK
1397 end if;
1398 end if;
1399
1400 if Present (Fun) then
1401 Append (Fun, Decls);
fbf5a39b
AC
1402 Expr := Make_Function_Call (Loc,
1403 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
05350ac6 1404
535a8637 1405 if not In_Init_Proc then
05350ac6
BD
1406 Set_Uses_Sec_Stack (Defining_Entity (Fun));
1407 end if;
70482933
RK
1408 end if;
1409
1410 Decl := Make_Object_Declaration (Loc,
1411 Defining_Identifier => T_Id,
fbf5a39b
AC
1412 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1413 Constant_Present => True,
1414 Expression => Expr);
70482933
RK
1415
1416 Append (Decl, Decls);
1417 return Decls;
1418 end Build_Task_Image_Decls;
1419
1420 -------------------------------
1421 -- Build_Task_Image_Function --
1422 -------------------------------
1423
1424 function Build_Task_Image_Function
1425 (Loc : Source_Ptr;
1426 Decls : List_Id;
1427 Stats : List_Id;
bebbff91 1428 Res : Entity_Id) return Node_Id
70482933
RK
1429 is
1430 Spec : Node_Id;
1431
1432 begin
1433 Append_To (Stats,
86cde7b1 1434 Make_Simple_Return_Statement (Loc,
fbf5a39b
AC
1435 Expression => New_Occurrence_Of (Res, Loc)));
1436
1437 Spec := Make_Function_Specification (Loc,
092ef350
RD
1438 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
1439 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
fbf5a39b 1440
273adcdf
AC
1441 -- Calls to 'Image use the secondary stack, which must be cleaned up
1442 -- after the task name is built.
fbf5a39b 1443
70482933
RK
1444 return Make_Subprogram_Body (Loc,
1445 Specification => Spec,
1446 Declarations => Decls,
1447 Handled_Statement_Sequence =>
fbf5a39b 1448 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
70482933
RK
1449 end Build_Task_Image_Function;
1450
1451 -----------------------------
1452 -- Build_Task_Image_Prefix --
1453 -----------------------------
1454
1455 procedure Build_Task_Image_Prefix
1456 (Loc : Source_Ptr;
1457 Len : out Entity_Id;
1458 Res : out Entity_Id;
1459 Pos : out Entity_Id;
1460 Prefix : Entity_Id;
1461 Sum : Node_Id;
86cde7b1
RD
1462 Decls : List_Id;
1463 Stats : List_Id)
70482933
RK
1464 is
1465 begin
092ef350 1466 Len := Make_Temporary (Loc, 'L', Sum);
70482933
RK
1467
1468 Append_To (Decls,
1469 Make_Object_Declaration (Loc,
1470 Defining_Identifier => Len,
092ef350
RD
1471 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
1472 Expression => Sum));
70482933 1473
092ef350 1474 Res := Make_Temporary (Loc, 'R');
70482933
RK
1475
1476 Append_To (Decls,
1477 Make_Object_Declaration (Loc,
1478 Defining_Identifier => Res,
1479 Object_Definition =>
1480 Make_Subtype_Indication (Loc,
1481 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1482 Constraint =>
1483 Make_Index_Or_Discriminant_Constraint (Loc,
1484 Constraints =>
1485 New_List (
1486 Make_Range (Loc,
1487 Low_Bound => Make_Integer_Literal (Loc, 1),
1488 High_Bound => New_Occurrence_Of (Len, Loc)))))));
1489
f90d14ac
AC
1490 -- Indicate that the result is an internal temporary, so it does not
1491 -- receive a bogus initialization when declaration is expanded. This
1492 -- is both efficient, and prevents anomalies in the handling of
1493 -- dynamic objects on the secondary stack.
1494
1495 Set_Is_Internal (Res);
092ef350 1496 Pos := Make_Temporary (Loc, 'P');
70482933
RK
1497
1498 Append_To (Decls,
1499 Make_Object_Declaration (Loc,
1500 Defining_Identifier => Pos,
092ef350 1501 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
70482933
RK
1502
1503 -- Pos := Prefix'Length;
1504
1505 Append_To (Stats,
1506 Make_Assignment_Statement (Loc,
1507 Name => New_Occurrence_Of (Pos, Loc),
1508 Expression =>
1509 Make_Attribute_Reference (Loc,
1510 Attribute_Name => Name_Length,
092ef350
RD
1511 Prefix => New_Occurrence_Of (Prefix, Loc),
1512 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
70482933
RK
1513
1514 -- Res (1 .. Pos) := Prefix;
1515
1516 Append_To (Stats,
092ef350
RD
1517 Make_Assignment_Statement (Loc,
1518 Name =>
1519 Make_Slice (Loc,
1520 Prefix => New_Occurrence_Of (Res, Loc),
70482933
RK
1521 Discrete_Range =>
1522 Make_Range (Loc,
092ef350 1523 Low_Bound => Make_Integer_Literal (Loc, 1),
70482933
RK
1524 High_Bound => New_Occurrence_Of (Pos, Loc))),
1525
092ef350 1526 Expression => New_Occurrence_Of (Prefix, Loc)));
70482933
RK
1527
1528 Append_To (Stats,
1529 Make_Assignment_Statement (Loc,
092ef350 1530 Name => New_Occurrence_Of (Pos, Loc),
70482933
RK
1531 Expression =>
1532 Make_Op_Add (Loc,
092ef350 1533 Left_Opnd => New_Occurrence_Of (Pos, Loc),
70482933
RK
1534 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1535 end Build_Task_Image_Prefix;
1536
1537 -----------------------------
1538 -- Build_Task_Record_Image --
1539 -----------------------------
1540
1541 function Build_Task_Record_Image
1542 (Loc : Source_Ptr;
1543 Id_Ref : Node_Id;
bebbff91 1544 Dyn : Boolean := False) return Node_Id
70482933
RK
1545 is
1546 Len : Entity_Id;
1547 -- Total length of generated name
1548
1549 Pos : Entity_Id;
1550 -- Index into result
1551
1552 Res : Entity_Id;
1553 -- String to hold result
1554
092ef350 1555 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
70482933
RK
1556 -- Name of enclosing variable, prefix of resulting name
1557
1558 Sum : Node_Id;
bebbff91 1559 -- Expression to compute total size of string
70482933
RK
1560
1561 Sel : Entity_Id;
1562 -- Entity for selector name
1563
86cde7b1
RD
1564 Decls : constant List_Id := New_List;
1565 Stats : constant List_Id := New_List;
70482933
RK
1566
1567 begin
aa9a7dd7
AC
1568 -- For a dynamic task, the name comes from the target variable. For a
1569 -- static one it is a formal of the enclosing init proc.
7bc1c7df
ES
1570
1571 if Dyn then
1572 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
fbf5a39b
AC
1573 Append_To (Decls,
1574 Make_Object_Declaration (Loc,
1575 Defining_Identifier => Pref,
1576 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1577 Expression =>
bebbff91
AC
1578 Make_String_Literal (Loc,
1579 Strval => String_From_Name_Buffer)));
fbf5a39b 1580
7bc1c7df 1581 else
fbf5a39b
AC
1582 Append_To (Decls,
1583 Make_Object_Renaming_Declaration (Loc,
1584 Defining_Identifier => Pref,
1585 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1586 Name => Make_Identifier (Loc, Name_uTask_Name)));
7bc1c7df 1587 end if;
70482933 1588
092ef350 1589 Sel := Make_Temporary (Loc, 'S');
70482933
RK
1590
1591 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1592
1593 Append_To (Decls,
1594 Make_Object_Declaration (Loc,
1595 Defining_Identifier => Sel,
092ef350
RD
1596 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1597 Expression =>
bebbff91
AC
1598 Make_String_Literal (Loc,
1599 Strval => String_From_Name_Buffer)));
70482933
RK
1600
1601 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1602
1603 Sum :=
1604 Make_Op_Add (Loc,
1605 Left_Opnd => Sum,
1606 Right_Opnd =>
1607 Make_Attribute_Reference (Loc,
1608 Attribute_Name => Name_Length,
1609 Prefix =>
7bc1c7df 1610 New_Occurrence_Of (Pref, Loc),
70482933
RK
1611 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1612
7bc1c7df 1613 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
70482933
RK
1614
1615 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1616
1617 -- Res (Pos) := '.';
1618
1619 Append_To (Stats,
1620 Make_Assignment_Statement (Loc,
1621 Name => Make_Indexed_Component (Loc,
1622 Prefix => New_Occurrence_Of (Res, Loc),
1623 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1624 Expression =>
1625 Make_Character_Literal (Loc,
1626 Chars => Name_Find,
1627 Char_Literal_Value =>
82c80734 1628 UI_From_Int (Character'Pos ('.')))));
70482933
RK
1629
1630 Append_To (Stats,
1631 Make_Assignment_Statement (Loc,
1632 Name => New_Occurrence_Of (Pos, Loc),
1633 Expression =>
1634 Make_Op_Add (Loc,
1635 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1636 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1637
1638 -- Res (Pos .. Len) := Selector;
1639
1640 Append_To (Stats,
1641 Make_Assignment_Statement (Loc,
1642 Name => Make_Slice (Loc,
1643 Prefix => New_Occurrence_Of (Res, Loc),
1644 Discrete_Range =>
1645 Make_Range (Loc,
1646 Low_Bound => New_Occurrence_Of (Pos, Loc),
1647 High_Bound => New_Occurrence_Of (Len, Loc))),
1648 Expression => New_Occurrence_Of (Sel, Loc)));
1649
1650 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1651 end Build_Task_Record_Image;
1652
d26d790d
AC
1653 -----------------------------
1654 -- Check_Float_Op_Overflow --
1655 -----------------------------
1656
1657 procedure Check_Float_Op_Overflow (N : Node_Id) is
1658 begin
1659 -- Return if no check needed
1660
bb304287
AC
1661 if not Is_Floating_Point_Type (Etype (N))
1662 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
d26d790d 1663
af6478c8 1664 -- In CodePeer_Mode, rely on the overflow check flag being set instead
bb304287 1665 -- and do not expand the code for float overflow checking.
e943fe8a 1666
af6478c8
AC
1667 or else CodePeer_Mode
1668 then
1669 return;
e943fe8a
AC
1670 end if;
1671
d26d790d
AC
1672 -- Otherwise we replace the expression by
1673
1674 -- do Tnn : constant ftype := expression;
1675 -- constraint_error when not Tnn'Valid;
1676 -- in Tnn;
1677
1678 declare
1679 Loc : constant Source_Ptr := Sloc (N);
1680 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
1681 Typ : constant Entity_Id := Etype (N);
1682
1683 begin
bb304287
AC
1684 -- Turn off the Do_Overflow_Check flag, since we are doing that work
1685 -- right here. We also set the node as analyzed to prevent infinite
1686 -- recursion from repeating the operation in the expansion.
d26d790d 1687
bb304287
AC
1688 Set_Do_Overflow_Check (N, False);
1689 Set_Analyzed (N, True);
d26d790d
AC
1690
1691 -- Do the rewrite to include the check
1692
1693 Rewrite (N,
1694 Make_Expression_With_Actions (Loc,
1695 Actions => New_List (
1696 Make_Object_Declaration (Loc,
1697 Defining_Identifier => Tnn,
1698 Object_Definition => New_Occurrence_Of (Typ, Loc),
1699 Constant_Present => True,
1700 Expression => Relocate_Node (N)),
1701 Make_Raise_Constraint_Error (Loc,
1702 Condition =>
1703 Make_Op_Not (Loc,
1704 Right_Opnd =>
1705 Make_Attribute_Reference (Loc,
1706 Prefix => New_Occurrence_Of (Tnn, Loc),
1707 Attribute_Name => Name_Valid)),
1708 Reason => CE_Overflow_Check_Failed)),
1709 Expression => New_Occurrence_Of (Tnn, Loc)));
1710
1711 Analyze_And_Resolve (N, Typ);
1712 end;
1713 end Check_Float_Op_Overflow;
1714
91b1417d
AC
1715 ----------------------------------
1716 -- Component_May_Be_Bit_Aligned --
1717 ----------------------------------
1718
1719 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
c97c0163 1720 UT : Entity_Id;
6fb4cdde 1721
91b1417d 1722 begin
dc7c0c4d
AC
1723 -- If no component clause, then everything is fine, since the back end
1724 -- never bit-misaligns by default, even if there is a pragma Packed for
1725 -- the record.
91b1417d 1726
c97c0163 1727 if No (Comp) or else No (Component_Clause (Comp)) then
91b1417d
AC
1728 return False;
1729 end if;
1730
c97c0163
AC
1731 UT := Underlying_Type (Etype (Comp));
1732
91b1417d
AC
1733 -- It is only array and record types that cause trouble
1734
0e564ab4 1735 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
91b1417d
AC
1736 return False;
1737
6fb4cdde
AC
1738 -- If we know that we have a small (64 bits or less) record or small
1739 -- bit-packed array, then everything is fine, since the back end can
1740 -- handle these cases correctly.
91b1417d
AC
1741
1742 elsif Esize (Comp) <= 64
0e564ab4 1743 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
91b1417d
AC
1744 then
1745 return False;
1746
dc7c0c4d
AC
1747 -- Otherwise if the component is not byte aligned, we know we have the
1748 -- nasty unaligned case.
91b1417d
AC
1749
1750 elsif Normalized_First_Bit (Comp) /= Uint_0
1751 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1752 then
1753 return True;
1754
1755 -- If we are large and byte aligned, then OK at this level
1756
1757 else
1758 return False;
1759 end if;
1760 end Component_May_Be_Bit_Aligned;
1761
1e3ed0fc
RD
1762 ----------------------------------------
1763 -- Containing_Package_With_Ext_Axioms --
1764 ----------------------------------------
1765
1766 function Containing_Package_With_Ext_Axioms
2cc7967f
AC
1767 (E : Entity_Id) return Entity_Id
1768 is
1e3ed0fc 1769 begin
1e3ed0fc
RD
1770 -- E is the package or generic package which is externally axiomatized
1771
2cc7967f 1772 if Ekind_In (E, E_Generic_Package, E_Package)
1e3ed0fc
RD
1773 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
1774 then
1775 return E;
1776 end if;
1777
aff557c7 1778 -- If E's scope is axiomatized, E is axiomatized
1e3ed0fc 1779
aff557c7 1780 if Present (Scope (E)) then
e379beb5
AC
1781 declare
1782 First_Ax_Parent_Scope : constant Entity_Id :=
1783 Containing_Package_With_Ext_Axioms (Scope (E));
1784 begin
1785 if Present (First_Ax_Parent_Scope) then
1786 return First_Ax_Parent_Scope;
1787 end if;
1788 end;
aff557c7
AC
1789 end if;
1790
1791 -- Otherwise, if E is a package instance, it is axiomatized if the
1792 -- corresponding generic package is axiomatized.
1e3ed0fc 1793
aff557c7 1794 if Ekind (E) = E_Package then
e379beb5
AC
1795 declare
1796 Par : constant Node_Id := Parent (E);
1797 Decl : Node_Id;
2cc7967f 1798
e379beb5
AC
1799 begin
1800 if Nkind (Par) = N_Defining_Program_Unit_Name then
1801 Decl := Parent (Par);
1802 else
1803 Decl := Par;
1804 end if;
aff557c7 1805
e379beb5
AC
1806 if Present (Generic_Parent (Decl)) then
1807 return
1808 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
1809 end if;
1810 end;
aff557c7
AC
1811 end if;
1812
1813 return Empty;
1e3ed0fc
RD
1814 end Containing_Package_With_Ext_Axioms;
1815
1816 -------------------------------
1817 -- Convert_To_Actual_Subtype --
1818 -------------------------------
1819
1820 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1821 Act_ST : Entity_Id;
1822
1823 begin
1824 Act_ST := Get_Actual_Subtype (Exp);
1825
1826 if Act_ST = Etype (Exp) then
1827 return;
1828 else
1829 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1830 Analyze_And_Resolve (Exp, Act_ST);
1831 end if;
1832 end Convert_To_Actual_Subtype;
1833
1923d2d6
JM
1834 -----------------------------------
1835 -- Corresponding_Runtime_Package --
1836 -----------------------------------
1837
1838 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1839 Pkg_Id : RTU_Id := RTU_Null;
1840
1841 begin
1842 pragma Assert (Is_Concurrent_Type (Typ));
1843
1844 if Ekind (Typ) in Protected_Kind then
1845 if Has_Entries (Typ)
65df5b71
HK
1846
1847 -- A protected type without entries that covers an interface and
1848 -- overrides the abstract routines with protected procedures is
1849 -- considered equivalent to a protected type with entries in the
f3d0f304 1850 -- context of dispatching select statements. It is sufficient to
65df5b71
HK
1851 -- check for the presence of an interface list in the declaration
1852 -- node to recognize this case.
1853
1854 or else Present (Interface_List (Parent (Typ)))
27a8f150
AC
1855
1856 -- Protected types with interrupt handlers (when not using a
1857 -- restricted profile) are also considered equivalent to
1858 -- protected types with entries. The types which are used
1859 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
1860 -- are derived from Protection_Entries.
1861
1862 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
1863 or else Has_Interrupt_Handler (Typ)
1923d2d6
JM
1864 then
1865 if Abort_Allowed
1866 or else Restriction_Active (No_Entry_Queue) = False
a54ffd6c 1867 or else Restriction_Active (No_Select_Statements) = False
1923d2d6
JM
1868 or else Number_Entries (Typ) > 1
1869 or else (Has_Attach_Handler (Typ)
dc36a7e3 1870 and then not Restricted_Profile)
1923d2d6
JM
1871 then
1872 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1873 else
1874 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1875 end if;
1876
1877 else
1878 Pkg_Id := System_Tasking_Protected_Objects;
1879 end if;
1880 end if;
1881
1882 return Pkg_Id;
1883 end Corresponding_Runtime_Package;
1884
70482933
RK
1885 -----------------------------------
1886 -- Current_Sem_Unit_Declarations --
1887 -----------------------------------
1888
1889 function Current_Sem_Unit_Declarations return List_Id is
1890 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1891 Decls : List_Id;
1892
1893 begin
1894 -- If the current unit is a package body, locate the visible
1895 -- declarations of the package spec.
1896
1897 if Nkind (U) = N_Package_Body then
1898 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1899 end if;
1900
1901 if Nkind (U) = N_Package_Declaration then
1902 U := Specification (U);
1903 Decls := Visible_Declarations (U);
1904
1905 if No (Decls) then
1906 Decls := New_List;
1907 Set_Visible_Declarations (U, Decls);
1908 end if;
1909
1910 else
1911 Decls := Declarations (U);
1912
1913 if No (Decls) then
1914 Decls := New_List;
1915 Set_Declarations (U, Decls);
1916 end if;
1917 end if;
1918
1919 return Decls;
1920 end Current_Sem_Unit_Declarations;
1921
1922 -----------------------
1923 -- Duplicate_Subexpr --
1924 -----------------------
1925
1926 function Duplicate_Subexpr
a43f6434
AC
1927 (Exp : Node_Id;
1928 Name_Req : Boolean := False;
1929 Renaming_Req : Boolean := False) return Node_Id
70482933
RK
1930 is
1931 begin
a43f6434 1932 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
70482933
RK
1933 return New_Copy_Tree (Exp);
1934 end Duplicate_Subexpr;
1935
8cbb664e
MG
1936 ---------------------------------
1937 -- Duplicate_Subexpr_No_Checks --
1938 ---------------------------------
1939
1940 function Duplicate_Subexpr_No_Checks
2934b84a
AC
1941 (Exp : Node_Id;
1942 Name_Req : Boolean := False;
1943 Renaming_Req : Boolean := False;
1944 Related_Id : Entity_Id := Empty;
1945 Is_Low_Bound : Boolean := False;
1946 Is_High_Bound : Boolean := False) return Node_Id
8cbb664e
MG
1947 is
1948 New_Exp : Node_Id;
a43f6434 1949
8cbb664e 1950 begin
2934b84a
AC
1951 Remove_Side_Effects
1952 (Exp => Exp,
1953 Name_Req => Name_Req,
1954 Renaming_Req => Renaming_Req,
1955 Related_Id => Related_Id,
1956 Is_Low_Bound => Is_Low_Bound,
1957 Is_High_Bound => Is_High_Bound);
1958
8cbb664e
MG
1959 New_Exp := New_Copy_Tree (Exp);
1960 Remove_Checks (New_Exp);
1961 return New_Exp;
1962 end Duplicate_Subexpr_No_Checks;
1963
1964 -----------------------------------
1965 -- Duplicate_Subexpr_Move_Checks --
1966 -----------------------------------
1967
1968 function Duplicate_Subexpr_Move_Checks
a43f6434
AC
1969 (Exp : Node_Id;
1970 Name_Req : Boolean := False;
1971 Renaming_Req : Boolean := False) return Node_Id
8cbb664e
MG
1972 is
1973 New_Exp : Node_Id;
a43f6434 1974
8cbb664e 1975 begin
a43f6434 1976 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
8cbb664e
MG
1977 New_Exp := New_Copy_Tree (Exp);
1978 Remove_Checks (Exp);
1979 return New_Exp;
1980 end Duplicate_Subexpr_Move_Checks;
1981
70482933
RK
1982 --------------------
1983 -- Ensure_Defined --
1984 --------------------
1985
1986 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1987 IR : Node_Id;
70482933
RK
1988
1989 begin
aa9a7dd7
AC
1990 -- An itype reference must only be created if this is a local itype, so
1991 -- that gigi can elaborate it on the proper objstack.
86cde7b1 1992
0e564ab4 1993 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
70482933
RK
1994 IR := Make_Itype_Reference (Sloc (N));
1995 Set_Itype (IR, Typ);
86cde7b1 1996 Insert_Action (N, IR);
70482933
RK
1997 end if;
1998 end Ensure_Defined;
1999
c42e6724
HK
2000 --------------------
2001 -- Entry_Names_OK --
2002 --------------------
2003
2004 function Entry_Names_OK return Boolean is
2005 begin
2006 return
2007 not Restricted_Profile
2008 and then not Global_Discard_Names
2009 and then not Restriction_Active (No_Implicit_Heap_Allocations)
2010 and then not Restriction_Active (No_Local_Allocators);
2011 end Entry_Names_OK;
2012
cc570be6
AC
2013 -------------------
2014 -- Evaluate_Name --
2015 -------------------
2016
2017 procedure Evaluate_Name (Nam : Node_Id) is
2018 K : constant Node_Kind := Nkind (Nam);
2019
2020 begin
2021 -- For an explicit dereference, we simply force the evaluation of the
2022 -- name expression. The dereference provides a value that is the address
2023 -- for the renamed object, and it is precisely this value that we want
2024 -- to preserve.
2025
2026 if K = N_Explicit_Dereference then
2027 Force_Evaluation (Prefix (Nam));
2028
2029 -- For a selected component, we simply evaluate the prefix
2030
2031 elsif K = N_Selected_Component then
2032 Evaluate_Name (Prefix (Nam));
2033
2034 -- For an indexed component, or an attribute reference, we evaluate the
2035 -- prefix, which is itself a name, recursively, and then force the
2036 -- evaluation of all the subscripts (or attribute expressions).
2037
2038 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
2039 Evaluate_Name (Prefix (Nam));
2040
2041 declare
2042 E : Node_Id;
2043
2044 begin
2045 E := First (Expressions (Nam));
2046 while Present (E) loop
2047 Force_Evaluation (E);
2048
2049 if Original_Node (E) /= E then
2050 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
2051 end if;
2052
2053 Next (E);
2054 end loop;
2055 end;
2056
2057 -- For a slice, we evaluate the prefix, as for the indexed component
2058 -- case and then, if there is a range present, either directly or as the
2059 -- constraint of a discrete subtype indication, we evaluate the two
2060 -- bounds of this range.
2061
2062 elsif K = N_Slice then
2063 Evaluate_Name (Prefix (Nam));
08cd7c2f 2064 Evaluate_Slice_Bounds (Nam);
cc570be6
AC
2065
2066 -- For a type conversion, the expression of the conversion must be the
2067 -- name of an object, and we simply need to evaluate this name.
2068
2069 elsif K = N_Type_Conversion then
2070 Evaluate_Name (Expression (Nam));
2071
2072 -- For a function call, we evaluate the call
2073
2074 elsif K = N_Function_Call then
2075 Force_Evaluation (Nam);
2076
2077 -- The remaining cases are direct name, operator symbol and character
2078 -- literal. In all these cases, we do nothing, since we want to
2079 -- reevaluate each time the renamed object is used.
2080
2081 else
2082 return;
2083 end if;
2084 end Evaluate_Name;
2085
08cd7c2f
AC
2086 ---------------------------
2087 -- Evaluate_Slice_Bounds --
2088 ---------------------------
2089
2090 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
2091 DR : constant Node_Id := Discrete_Range (Slice);
2092 Constr : Node_Id;
2093 Rexpr : Node_Id;
2094
2095 begin
2096 if Nkind (DR) = N_Range then
2097 Force_Evaluation (Low_Bound (DR));
2098 Force_Evaluation (High_Bound (DR));
2099
2100 elsif Nkind (DR) = N_Subtype_Indication then
2101 Constr := Constraint (DR);
2102
2103 if Nkind (Constr) = N_Range_Constraint then
2104 Rexpr := Range_Expression (Constr);
2105
2106 Force_Evaluation (Low_Bound (Rexpr));
2107 Force_Evaluation (High_Bound (Rexpr));
2108 end if;
2109 end if;
2110 end Evaluate_Slice_Bounds;
2111
70482933
RK
2112 ---------------------
2113 -- Evolve_And_Then --
2114 ---------------------
2115
2116 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
2117 begin
2118 if No (Cond) then
2119 Cond := Cond1;
2120 else
2121 Cond :=
2122 Make_And_Then (Sloc (Cond1),
2123 Left_Opnd => Cond,
2124 Right_Opnd => Cond1);
2125 end if;
2126 end Evolve_And_Then;
2127
2128 --------------------
2129 -- Evolve_Or_Else --
2130 --------------------
2131
2132 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
2133 begin
2134 if No (Cond) then
2135 Cond := Cond1;
2136 else
2137 Cond :=
2138 Make_Or_Else (Sloc (Cond1),
2139 Left_Opnd => Cond,
2140 Right_Opnd => Cond1);
2141 end if;
2142 end Evolve_Or_Else;
2143
9e92ad49
AC
2144 -----------------------------------------
2145 -- Expand_Static_Predicates_In_Choices --
2146 -----------------------------------------
2147
2148 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
2149 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
2150
2151 Choices : constant List_Id := Discrete_Choices (N);
2152
2153 Choice : Node_Id;
2154 Next_C : Node_Id;
2155 P : Node_Id;
2156 C : Node_Id;
2157
2158 begin
2159 Choice := First (Choices);
2160 while Present (Choice) loop
2161 Next_C := Next (Choice);
2162
2163 -- Check for name of subtype with static predicate
2164
2165 if Is_Entity_Name (Choice)
2166 and then Is_Type (Entity (Choice))
2167 and then Has_Predicates (Entity (Choice))
2168 then
2169 -- Loop through entries in predicate list, converting to choices
2170 -- and inserting in the list before the current choice. Note that
2171 -- if the list is empty, corresponding to a False predicate, then
2172 -- no choices are inserted.
2173
60f908dd 2174 P := First (Static_Discrete_Predicate (Entity (Choice)));
9e92ad49
AC
2175 while Present (P) loop
2176
2177 -- If low bound and high bounds are equal, copy simple choice
2178
2179 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
2180 C := New_Copy (Low_Bound (P));
2181
2182 -- Otherwise copy a range
2183
2184 else
2185 C := New_Copy (P);
2186 end if;
2187
2188 -- Change Sloc to referencing choice (rather than the Sloc of
15918371 2189 -- the predicate declaration element itself).
9e92ad49
AC
2190
2191 Set_Sloc (C, Sloc (Choice));
2192 Insert_Before (Choice, C);
2193 Next (P);
2194 end loop;
2195
2196 -- Delete the predicated entry
2197
2198 Remove (Choice);
2199 end if;
2200
2201 -- Move to next choice to check
2202
2203 Choice := Next_C;
2204 end loop;
2205 end Expand_Static_Predicates_In_Choices;
2206
70482933
RK
2207 ------------------------------
2208 -- Expand_Subtype_From_Expr --
2209 ------------------------------
2210
2211 -- This function is applicable for both static and dynamic allocation of
2212 -- objects which are constrained by an initial expression. Basically it
2213 -- transforms an unconstrained subtype indication into a constrained one.
273adcdf 2214
70482933 2215 -- The expression may also be transformed in certain cases in order to
05350ac6
BD
2216 -- avoid multiple evaluation. In the static allocation case, the general
2217 -- scheme is:
70482933
RK
2218
2219 -- Val : T := Expr;
2220
2221 -- is transformed into
2222
2223 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
2224 --
2225 -- Here are the main cases :
2226 --
2227 -- <if Expr is a Slice>
2228 -- Val : T ([Index_Subtype (Expr)]) := Expr;
2229 --
2230 -- <elsif Expr is a String Literal>
2231 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
2232 --
2233 -- <elsif Expr is Constrained>
2234 -- subtype T is Type_Of_Expr
2235 -- Val : T := Expr;
2236 --
2237 -- <elsif Expr is an entity_name>
638e383e 2238 -- Val : T (constraints taken from Expr) := Expr;
70482933
RK
2239 --
2240 -- <else>
2241 -- type Axxx is access all T;
2242 -- Rval : Axxx := Expr'ref;
638e383e 2243 -- Val : T (constraints taken from Rval) := Rval.all;
70482933
RK
2244
2245 -- ??? note: when the Expression is allocated in the secondary stack
2246 -- we could use it directly instead of copying it by declaring
2247 -- Val : T (...) renames Rval.all
2248
2249 procedure Expand_Subtype_From_Expr
2250 (N : Node_Id;
2251 Unc_Type : Entity_Id;
2252 Subtype_Indic : Node_Id;
d9307840
HK
2253 Exp : Node_Id;
2254 Related_Id : Entity_Id := Empty)
70482933
RK
2255 is
2256 Loc : constant Source_Ptr := Sloc (N);
2257 Exp_Typ : constant Entity_Id := Etype (Exp);
2258 T : Entity_Id;
2259
2260 begin
2261 -- In general we cannot build the subtype if expansion is disabled,
2262 -- because internal entities may not have been defined. However, to
f2e7ec10
AC
2263 -- avoid some cascaded errors, we try to continue when the expression is
2264 -- an array (or string), because it is safe to compute the bounds. It is
2265 -- in fact required to do so even in a generic context, because there
2266 -- may be constants that depend on the bounds of a string literal, both
2267 -- standard string types and more generally arrays of characters.
70482933 2268
ebb6b0bd 2269 -- In GNATprove mode, these extra subtypes are not needed
f5da7a97 2270
ebb6b0bd 2271 if GNATprove_Mode then
70482933
RK
2272 return;
2273 end if;
2274
ebb6b0bd
AC
2275 if not Expander_Active
2276 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
2277 then
f5da7a97
YM
2278 return;
2279 end if;
2280
70482933
RK
2281 if Nkind (Exp) = N_Slice then
2282 declare
2283 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
2284
2285 begin
2286 Rewrite (Subtype_Indic,
2287 Make_Subtype_Indication (Loc,
e4494292 2288 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
70482933
RK
2289 Constraint =>
2290 Make_Index_Or_Discriminant_Constraint (Loc,
2291 Constraints => New_List
e4494292 2292 (New_Occurrence_Of (Slice_Type, Loc)))));
70482933 2293
e14c931f 2294 -- This subtype indication may be used later for constraint checks
70482933
RK
2295 -- we better make sure that if a variable was used as a bound of
2296 -- of the original slice, its value is frozen.
2297
08cd7c2f 2298 Evaluate_Slice_Bounds (Exp);
70482933
RK
2299 end;
2300
2301 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2302 Rewrite (Subtype_Indic,
2303 Make_Subtype_Indication (Loc,
e4494292 2304 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
70482933
RK
2305 Constraint =>
2306 Make_Index_Or_Discriminant_Constraint (Loc,
2307 Constraints => New_List (
2308 Make_Literal_Range (Loc,
f91b40db 2309 Literal_Typ => Exp_Typ)))));
70482933 2310
9a7049fd 2311 -- If the type of the expression is an internally generated type it
872c2f37
RD
2312 -- may not be necessary to create a new subtype. However there are two
2313 -- exceptions: references to the current instances, and aliased array
2314 -- object declarations for which the backend needs to create a template.
9a7049fd 2315
70482933
RK
2316 elsif Is_Constrained (Exp_Typ)
2317 and then not Is_Class_Wide_Type (Unc_Type)
9a7049fd
AC
2318 and then
2319 (Nkind (N) /= N_Object_Declaration
872c2f37
RD
2320 or else not Is_Entity_Name (Expression (N))
2321 or else not Comes_From_Source (Entity (Expression (N)))
2322 or else not Is_Array_Type (Exp_Typ)
2323 or else not Aliased_Present (N))
70482933
RK
2324 then
2325 if Is_Itype (Exp_Typ) then
2326
758c442c 2327 -- Within an initialization procedure, a selected component
273adcdf
AC
2328 -- denotes a component of the enclosing record, and it appears as
2329 -- an actual in a call to its own initialization procedure. If
2330 -- this component depends on the outer discriminant, we must
758c442c 2331 -- generate the proper actual subtype for it.
70482933 2332
758c442c
GD
2333 if Nkind (Exp) = N_Selected_Component
2334 and then Within_Init_Proc
2335 then
2336 declare
2337 Decl : constant Node_Id :=
2338 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2339 begin
2340 if Present (Decl) then
2341 Insert_Action (N, Decl);
2342 T := Defining_Identifier (Decl);
2343 else
2344 T := Exp_Typ;
2345 end if;
2346 end;
2347
9a7049fd 2348 -- No need to generate a new subtype
758c442c
GD
2349
2350 else
2351 T := Exp_Typ;
2352 end if;
70482933
RK
2353
2354 else
092ef350 2355 T := Make_Temporary (Loc, 'T');
70482933
RK
2356
2357 Insert_Action (N,
2358 Make_Subtype_Declaration (Loc,
2359 Defining_Identifier => T,
e4494292 2360 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
70482933 2361
273adcdf
AC
2362 -- This type is marked as an itype even though it has an explicit
2363 -- declaration since otherwise Is_Generic_Actual_Type can get
2364 -- set, resulting in the generation of spurious errors. (See
2365 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
70482933
RK
2366
2367 Set_Is_Itype (T);
2368 Set_Associated_Node_For_Itype (T, Exp);
2369 end if;
2370
e4494292 2371 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
70482933 2372
0a69df7c 2373 -- Nothing needs to be done for private types with unknown discriminants
3f5bb1b8
AC
2374 -- if the underlying type is not an unconstrained composite type or it
2375 -- is an unchecked union.
70482933
RK
2376
2377 elsif Is_Private_Type (Unc_Type)
2378 and then Has_Unknown_Discriminants (Unc_Type)
2379 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
0a69df7c
AC
2380 or else Is_Constrained (Underlying_Type (Unc_Type))
2381 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
70482933
RK
2382 then
2383 null;
2384
58a9d876
AC
2385 -- Case of derived type with unknown discriminants where the parent type
2386 -- also has unknown discriminants.
f4d379b8
HK
2387
2388 elsif Is_Record_Type (Unc_Type)
2389 and then not Is_Class_Wide_Type (Unc_Type)
2390 and then Has_Unknown_Discriminants (Unc_Type)
2391 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2392 then
58a9d876
AC
2393 -- Nothing to be done if no underlying record view available
2394
2395 if No (Underlying_Record_View (Unc_Type)) then
2396 null;
2397
2398 -- Otherwise use the Underlying_Record_View to create the proper
2399 -- constrained subtype for an object of a derived type with unknown
2400 -- discriminants.
2401
2402 else
2403 Remove_Side_Effects (Exp);
2404 Rewrite (Subtype_Indic,
2405 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2406 end if;
f4d379b8 2407
0e41a941
AC
2408 -- Renamings of class-wide interface types require no equivalent
2409 -- constrained type declarations because we only need to reference
df3e68b1
HK
2410 -- the tag component associated with the interface. The same is
2411 -- presumably true for class-wide types in general, so this test
2412 -- is broadened to include all class-wide renamings, which also
2413 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2414 -- (Is this really correct, or are there some cases of class-wide
2415 -- renamings that require action in this procedure???)
0e41a941
AC
2416
2417 elsif Present (N)
2418 and then Nkind (N) = N_Object_Renaming_Declaration
df3e68b1 2419 and then Is_Class_Wide_Type (Unc_Type)
0e41a941 2420 then
0e41a941
AC
2421 null;
2422
885c4871 2423 -- In Ada 95 nothing to be done if the type of the expression is limited
aa9a7dd7
AC
2424 -- because in this case the expression cannot be copied, and its use can
2425 -- only be by reference.
10b93b2e 2426
885c4871 2427 -- In Ada 2005 the context can be an object declaration whose expression
0712790c
ES
2428 -- is a function that returns in place. If the nominal subtype has
2429 -- unknown discriminants, the call still provides constraints on the
2430 -- object, and we have to create an actual subtype from it.
2431
2432 -- If the type is class-wide, the expression is dynamically tagged and
2433 -- we do not create an actual subtype either. Ditto for an interface.
0187b60e
AC
2434 -- For now this applies only if the type is immutably limited, and the
2435 -- function being called is build-in-place. This will have to be revised
2436 -- when build-in-place functions are generalized to other types.
0712790c 2437
51245e2d 2438 elsif Is_Limited_View (Exp_Typ)
0712790c
ES
2439 and then
2440 (Is_Class_Wide_Type (Exp_Typ)
2441 or else Is_Interface (Exp_Typ)
2442 or else not Has_Unknown_Discriminants (Exp_Typ)
2443 or else not Is_Composite_Type (Unc_Type))
2444 then
2445 null;
2446
86cde7b1
RD
2447 -- For limited objects initialized with build in place function calls,
2448 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2449 -- node in the expression initializing the object, which breaks the
2450 -- circuitry that detects and adds the additional arguments to the
2451 -- called function.
2452
2453 elsif Is_Build_In_Place_Function_Call (Exp) then
2454 null;
2455
70482933
RK
2456 else
2457 Remove_Side_Effects (Exp);
2458 Rewrite (Subtype_Indic,
d9307840 2459 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
70482933
RK
2460 end if;
2461 end Expand_Subtype_From_Expr;
2462
760804f3
AC
2463 ----------------------
2464 -- Finalize_Address --
2465 ----------------------
2466
2467 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
2468 Utyp : Entity_Id := Typ;
2469
2470 begin
2471 -- Handle protected class-wide or task class-wide types
2472
2473 if Is_Class_Wide_Type (Utyp) then
2474 if Is_Concurrent_Type (Root_Type (Utyp)) then
2475 Utyp := Root_Type (Utyp);
2476
2477 elsif Is_Private_Type (Root_Type (Utyp))
2478 and then Present (Full_View (Root_Type (Utyp)))
2479 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
2480 then
2481 Utyp := Full_View (Root_Type (Utyp));
2482 end if;
2483 end if;
2484
2485 -- Handle private types
2486
2487 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
2488 Utyp := Full_View (Utyp);
2489 end if;
2490
2491 -- Handle protected and task types
2492
2493 if Is_Concurrent_Type (Utyp)
2494 and then Present (Corresponding_Record_Type (Utyp))
2495 then
2496 Utyp := Corresponding_Record_Type (Utyp);
2497 end if;
2498
2499 Utyp := Underlying_Type (Base_Type (Utyp));
2500
2501 -- Deal with untagged derivation of private views. If the parent is
2502 -- now known to be protected, the finalization routine is the one
2503 -- defined on the corresponding record of the ancestor (corresponding
2504 -- records do not automatically inherit operations, but maybe they
2505 -- should???)
2506
2507 if Is_Untagged_Derivation (Typ) then
2508 if Is_Protected_Type (Typ) then
2509 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
46413d9e 2510
760804f3
AC
2511 else
2512 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
2513
2514 if Is_Protected_Type (Utyp) then
2515 Utyp := Corresponding_Record_Type (Utyp);
2516 end if;
2517 end if;
2518 end if;
2519
2520 -- If the underlying_type is a subtype, we are dealing with the
2521 -- completion of a private type. We need to access the base type and
2522 -- generate a conversion to it.
2523
2524 if Utyp /= Base_Type (Utyp) then
2525 pragma Assert (Is_Private_Type (Typ));
2526
2527 Utyp := Base_Type (Utyp);
2528 end if;
2529
2530 -- When dealing with an internally built full view for a type with
2531 -- unknown discriminants, use the original record type.
2532
2533 if Is_Underlying_Record_View (Utyp) then
2534 Utyp := Etype (Utyp);
2535 end if;
2536
2537 return TSS (Utyp, TSS_Finalize_Address);
2538 end Finalize_Address;
2539
758c442c 2540 ------------------------
f4d379b8 2541 -- Find_Interface_ADT --
758c442c
GD
2542 ------------------------
2543
3ca505dc
JM
2544 function Find_Interface_ADT
2545 (T : Entity_Id;
ac4d6407 2546 Iface : Entity_Id) return Elmt_Id
3ca505dc 2547 is
ce2b6ba5
JM
2548 ADT : Elmt_Id;
2549 Typ : Entity_Id := T;
3ca505dc
JM
2550
2551 begin
dee4682a
JM
2552 pragma Assert (Is_Interface (Iface));
2553
3ca505dc
JM
2554 -- Handle private types
2555
0e564ab4 2556 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
3ca505dc
JM
2557 Typ := Full_View (Typ);
2558 end if;
2559
2560 -- Handle access types
2561
2562 if Is_Access_Type (Typ) then
841dd0f5 2563 Typ := Designated_Type (Typ);
3ca505dc
JM
2564 end if;
2565
2566 -- Handle task and protected types implementing interfaces
2567
dee4682a 2568 if Is_Concurrent_Type (Typ) then
3ca505dc
JM
2569 Typ := Corresponding_Record_Type (Typ);
2570 end if;
2571
dee4682a
JM
2572 pragma Assert
2573 (not Is_Class_Wide_Type (Typ)
2574 and then Ekind (Typ) /= E_Incomplete_Type);
2575
4ac2477e 2576 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
ce2b6ba5
JM
2577 return First_Elmt (Access_Disp_Table (Typ));
2578
2579 else
872c2f37 2580 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
ce2b6ba5
JM
2581 while Present (ADT)
2582 and then Present (Related_Type (Node (ADT)))
2583 and then Related_Type (Node (ADT)) /= Iface
4ac2477e
JM
2584 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2585 Use_Full_View => True)
ce2b6ba5
JM
2586 loop
2587 Next_Elmt (ADT);
2588 end loop;
2589
2590 pragma Assert (Present (Related_Type (Node (ADT))));
2591 return ADT;
2592 end if;
3ca505dc
JM
2593 end Find_Interface_ADT;
2594
2595 ------------------------
2596 -- Find_Interface_Tag --
2597 ------------------------
2598
2599 function Find_Interface_Tag
dee4682a
JM
2600 (T : Entity_Id;
2601 Iface : Entity_Id) return Entity_Id
758c442c 2602 is
3ca505dc 2603 AI_Tag : Entity_Id;
dee4682a 2604 Found : Boolean := False;
3ca505dc 2605 Typ : Entity_Id := T;
758c442c 2606
59e54267 2607 procedure Find_Tag (Typ : Entity_Id);
3ca505dc 2608 -- Internal subprogram used to recursively climb to the ancestors
758c442c 2609
ea985d95
RD
2610 --------------
2611 -- Find_Tag --
2612 --------------
758c442c 2613
59e54267 2614 procedure Find_Tag (Typ : Entity_Id) is
758c442c
GD
2615 AI_Elmt : Elmt_Id;
2616 AI : Node_Id;
2617
2618 begin
0e41a941
AC
2619 -- This routine does not handle the case in which the interface is an
2620 -- ancestor of Typ. That case is handled by the enclosing subprogram.
758c442c 2621
0e41a941 2622 pragma Assert (Typ /= Iface);
758c442c 2623
f4d379b8
HK
2624 -- Climb to the root type handling private types
2625
ce2b6ba5 2626 if Present (Full_View (Etype (Typ))) then
f4d379b8
HK
2627 if Full_View (Etype (Typ)) /= Typ then
2628 Find_Tag (Full_View (Etype (Typ)));
2629 end if;
758c442c 2630
f4d379b8 2631 elsif Etype (Typ) /= Typ then
3ca505dc 2632 Find_Tag (Etype (Typ));
758c442c
GD
2633 end if;
2634
2635 -- Traverse the list of interfaces implemented by the type
2636
2637 if not Found
ce2b6ba5
JM
2638 and then Present (Interfaces (Typ))
2639 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
758c442c 2640 then
10b93b2e 2641 -- Skip the tag associated with the primary table
758c442c 2642
ce2b6ba5
JM
2643 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2644 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2645 pragma Assert (Present (AI_Tag));
758c442c 2646
ce2b6ba5 2647 AI_Elmt := First_Elmt (Interfaces (Typ));
758c442c
GD
2648 while Present (AI_Elmt) loop
2649 AI := Node (AI_Elmt);
2650
4ac2477e
JM
2651 if AI = Iface
2652 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2653 then
758c442c
GD
2654 Found := True;
2655 return;
2656 end if;
2657
2658 AI_Tag := Next_Tag_Component (AI_Tag);
2659 Next_Elmt (AI_Elmt);
758c442c
GD
2660 end loop;
2661 end if;
3ca505dc
JM
2662 end Find_Tag;
2663
2664 -- Start of processing for Find_Interface_Tag
758c442c
GD
2665
2666 begin
f4d379b8
HK
2667 pragma Assert (Is_Interface (Iface));
2668
3ca505dc 2669 -- Handle access types
758c442c 2670
3ca505dc 2671 if Is_Access_Type (Typ) then
841dd0f5 2672 Typ := Designated_Type (Typ);
3ca505dc 2673 end if;
758c442c 2674
c6ad817f 2675 -- Handle class-wide types
758c442c 2676
c6ad817f
JM
2677 if Is_Class_Wide_Type (Typ) then
2678 Typ := Root_Type (Typ);
3ca505dc
JM
2679 end if;
2680
c6ad817f
JM
2681 -- Handle private types
2682
0e564ab4 2683 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
c6ad817f 2684 Typ := Full_View (Typ);
10b93b2e
HK
2685 end if;
2686
2687 -- Handle entities from the limited view
2688
2689 if Ekind (Typ) = E_Incomplete_Type then
2690 pragma Assert (Present (Non_Limited_View (Typ)));
2691 Typ := Non_Limited_View (Typ);
2692 end if;
2693
c6ad817f
JM
2694 -- Handle task and protected types implementing interfaces
2695
2696 if Is_Concurrent_Type (Typ) then
2697 Typ := Corresponding_Record_Type (Typ);
2698 end if;
2699
0e41a941
AC
2700 -- If the interface is an ancestor of the type, then it shared the
2701 -- primary dispatch table.
2702
4ac2477e 2703 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
0e41a941
AC
2704 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2705 return First_Tag_Component (Typ);
2706
2707 -- Otherwise we need to search for its associated tag component
2708
2709 else
2710 Find_Tag (Typ);
2711 pragma Assert (Found);
2712 return AI_Tag;
2713 end if;
ce2b6ba5 2714 end Find_Interface_Tag;
ea985d95 2715
ca811241
BD
2716 ---------------------------
2717 -- Find_Optional_Prim_Op --
2718 ---------------------------
70482933 2719
ca811241
BD
2720 function Find_Optional_Prim_Op
2721 (T : Entity_Id; Name : Name_Id) return Entity_Id
2722 is
70482933
RK
2723 Prim : Elmt_Id;
2724 Typ : Entity_Id := T;
59e54267 2725 Op : Entity_Id;
70482933
RK
2726
2727 begin
2728 if Is_Class_Wide_Type (Typ) then
2729 Typ := Root_Type (Typ);
2730 end if;
2731
2732 Typ := Underlying_Type (Typ);
2733
59e54267
ES
2734 -- Loop through primitive operations
2735
70482933 2736 Prim := First_Elmt (Primitive_Operations (Typ));
59e54267
ES
2737 while Present (Prim) loop
2738 Op := Node (Prim);
2739
2740 -- We can retrieve primitive operations by name if it is an internal
2741 -- name. For equality we must check that both of its operands have
2742 -- the same type, to avoid confusion with user-defined equalities
2743 -- than may have a non-symmetric signature.
2744
2745 exit when Chars (Op) = Name
2746 and then
2747 (Name /= Name_Op_Eq
0e564ab4 2748 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
59e54267 2749
70482933 2750 Next_Elmt (Prim);
70482933
RK
2751 end loop;
2752
ca811241
BD
2753 return Node (Prim); -- Empty if not found
2754 end Find_Optional_Prim_Op;
70482933 2755
ca811241
BD
2756 ---------------------------
2757 -- Find_Optional_Prim_Op --
2758 ---------------------------
dee4682a 2759
ca811241 2760 function Find_Optional_Prim_Op
fbf5a39b
AC
2761 (T : Entity_Id;
2762 Name : TSS_Name_Type) return Entity_Id
2763 is
df3e68b1
HK
2764 Inher_Op : Entity_Id := Empty;
2765 Own_Op : Entity_Id := Empty;
2766 Prim_Elmt : Elmt_Id;
2767 Prim_Id : Entity_Id;
2768 Typ : Entity_Id := T;
fbf5a39b
AC
2769
2770 begin
2771 if Is_Class_Wide_Type (Typ) then
2772 Typ := Root_Type (Typ);
2773 end if;
2774
2775 Typ := Underlying_Type (Typ);
2776
df3e68b1
HK
2777 -- This search is based on the assertion that the dispatching version
2778 -- of the TSS routine always precedes the real primitive.
6a4d72a6 2779
df3e68b1
HK
2780 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2781 while Present (Prim_Elmt) loop
2782 Prim_Id := Node (Prim_Elmt);
7813a510 2783
df3e68b1
HK
2784 if Is_TSS (Prim_Id, Name) then
2785 if Present (Alias (Prim_Id)) then
2786 Inher_Op := Prim_Id;
2787 else
2788 Own_Op := Prim_Id;
2789 end if;
6a4d72a6 2790 end if;
df3e68b1
HK
2791
2792 Next_Elmt (Prim_Elmt);
fbf5a39b
AC
2793 end loop;
2794
df3e68b1
HK
2795 if Present (Own_Op) then
2796 return Own_Op;
2797 elsif Present (Inher_Op) then
2798 return Inher_Op;
2799 else
ca811241
BD
2800 return Empty;
2801 end if;
2802 end Find_Optional_Prim_Op;
2803
2804 ------------------
2805 -- Find_Prim_Op --
2806 ------------------
2807
2808 function Find_Prim_Op
2809 (T : Entity_Id; Name : Name_Id) return Entity_Id
2810 is
2811 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
2812 begin
2813 if No (Result) then
2814 raise Program_Error;
2815 end if;
2816
2817 return Result;
2818 end Find_Prim_Op;
2819
2820 ------------------
2821 -- Find_Prim_Op --
2822 ------------------
2823
2824 function Find_Prim_Op
2825 (T : Entity_Id;
2826 Name : TSS_Name_Type) return Entity_Id
2827 is
2828 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
2829 begin
2830 if No (Result) then
df3e68b1
HK
2831 raise Program_Error;
2832 end if;
ca811241
BD
2833
2834 return Result;
fbf5a39b
AC
2835 end Find_Prim_Op;
2836
65df5b71
HK
2837 ----------------------------
2838 -- Find_Protection_Object --
2839 ----------------------------
2840
2841 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2842 S : Entity_Id;
2843
2844 begin
2845 S := Scop;
2846 while Present (S) loop
0e564ab4 2847 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
65df5b71
HK
2848 and then Present (Protection_Object (S))
2849 then
2850 return Protection_Object (S);
2851 end if;
2852
2853 S := Scope (S);
2854 end loop;
2855
2856 -- If we do not find a Protection object in the scope chain, then
2857 -- something has gone wrong, most likely the object was never created.
2858
2859 raise Program_Error;
2860 end Find_Protection_Object;
2861
df3e68b1
HK
2862 --------------------------
2863 -- Find_Protection_Type --
2864 --------------------------
2865
2866 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2867 Comp : Entity_Id;
2868 Typ : Entity_Id := Conc_Typ;
2869
2870 begin
2871 if Is_Concurrent_Type (Typ) then
2872 Typ := Corresponding_Record_Type (Typ);
2873 end if;
2874
e0c32166
AC
2875 -- Since restriction violations are not considered serious errors, the
2876 -- expander remains active, but may leave the corresponding record type
2877 -- malformed. In such cases, component _object is not available so do
2878 -- not look for it.
2879
2880 if not Analyzed (Typ) then
2881 return Empty;
2882 end if;
2883
df3e68b1
HK
2884 Comp := First_Component (Typ);
2885 while Present (Comp) loop
2886 if Chars (Comp) = Name_uObject then
2887 return Base_Type (Etype (Comp));
2888 end if;
2889
2890 Next_Component (Comp);
2891 end loop;
2892
2893 -- The corresponding record of a protected type should always have an
2894 -- _object field.
2895
2896 raise Program_Error;
2897 end Find_Protection_Type;
2898
e59243fa
AC
2899 -----------------------
2900 -- Find_Hook_Context --
2901 -----------------------
2902
2903 function Find_Hook_Context (N : Node_Id) return Node_Id is
2904 Par : Node_Id;
2905 Top : Node_Id;
2906
2907 Wrapped_Node : Node_Id;
2908 -- Note: if we are in a transient scope, we want to reuse it as
2909 -- the context for actions insertion, if possible. But if N is itself
2910 -- part of the stored actions for the current transient scope,
2911 -- then we need to insert at the appropriate (inner) location in
2912 -- the not as an action on Node_To_Be_Wrapped.
2913
2914 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
2915
2916 begin
2917 -- When the node is inside a case/if expression, the lifetime of any
2918 -- temporary controlled object is extended. Find a suitable insertion
2919 -- node by locating the topmost case or if expressions.
2920
2921 if In_Cond_Expr then
2922 Par := N;
2923 Top := N;
2924 while Present (Par) loop
2925 if Nkind_In (Original_Node (Par), N_Case_Expression,
2926 N_If_Expression)
2927 then
2928 Top := Par;
2929
2930 -- Prevent the search from going too far
2931
2932 elsif Is_Body_Or_Package_Declaration (Par) then
2933 exit;
2934 end if;
2935
2936 Par := Parent (Par);
2937 end loop;
2938
2939 -- The topmost case or if expression is now recovered, but it may
2940 -- still not be the correct place to add generated code. Climb to
2941 -- find a parent that is part of a declarative or statement list,
2942 -- and is not a list of actuals in a call.
2943
2944 Par := Top;
2945 while Present (Par) loop
2946 if Is_List_Member (Par)
2947 and then not Nkind_In (Par, N_Component_Association,
2948 N_Discriminant_Association,
2949 N_Parameter_Association,
2950 N_Pragma_Argument_Association)
3386e3ae
AC
2951 and then not Nkind_In (Parent (Par), N_Function_Call,
2952 N_Procedure_Call_Statement,
2953 N_Entry_Call_Statement)
e59243fa
AC
2954
2955 then
2956 return Par;
2957
2958 -- Prevent the search from going too far
2959
2960 elsif Is_Body_Or_Package_Declaration (Par) then
2961 exit;
2962 end if;
2963
2964 Par := Parent (Par);
2965 end loop;
2966
2967 return Par;
2968
2969 else
2970 Par := N;
2971 while Present (Par) loop
2972
2973 -- Keep climbing past various operators
2974
2975 if Nkind (Parent (Par)) in N_Op
2976 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
2977 then
2978 Par := Parent (Par);
2979 else
2980 exit;
2981 end if;
2982 end loop;
2983
2984 Top := Par;
2985
2986 -- The node may be located in a pragma in which case return the
2987 -- pragma itself:
2988
2989 -- pragma Precondition (... and then Ctrl_Func_Call ...);
2990
2991 -- Similar case occurs when the node is related to an object
2992 -- declaration or assignment:
2993
2994 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
2995
2996 -- Another case to consider is when the node is part of a return
2997 -- statement:
2998
2999 -- return ... and then Ctrl_Func_Call ...;
3000
3001 -- Another case is when the node acts as a formal in a procedure
3002 -- call statement:
3003
3004 -- Proc (... and then Ctrl_Func_Call ...);
3005
3006 if Scope_Is_Transient then
3007 Wrapped_Node := Node_To_Be_Wrapped;
3008 else
3009 Wrapped_Node := Empty;
3010 end if;
3011
3012 while Present (Par) loop
3013 if Par = Wrapped_Node
3014 or else Nkind_In (Par, N_Assignment_Statement,
3015 N_Object_Declaration,
3016 N_Pragma,
3017 N_Procedure_Call_Statement,
3018 N_Simple_Return_Statement)
3019 then
3020 return Par;
3021
3022 -- Prevent the search from going too far
3023
3024 elsif Is_Body_Or_Package_Declaration (Par) then
3025 exit;
3026 end if;
3027
3028 Par := Parent (Par);
3029 end loop;
3030
3031 -- Return the topmost short circuit operator
3032
3033 return Top;
3034 end if;
3035 end Find_Hook_Context;
3036
cd2c6027
AC
3037 ------------------------------
3038 -- Following_Address_Clause --
3039 ------------------------------
3040
cd2c6027 3041 function Following_Address_Clause (D : Node_Id) return Node_Id is
f2d9ae20
AC
3042 Id : constant Entity_Id := Defining_Identifier (D);
3043 Result : Node_Id;
3044 Par : Node_Id;
3045
3046 function Check_Decls (D : Node_Id) return Node_Id;
3047 -- This internal function differs from the main function in that it
3048 -- gets called to deal with a following package private part, and
3049 -- it checks declarations starting with D (the main function checks
3050 -- declarations following D). If D is Empty, then Empty is returned.
3051
3052 -----------------
3053 -- Check_Decls --
3054 -----------------
3055
3056 function Check_Decls (D : Node_Id) return Node_Id is
3057 Decl : Node_Id;
3058
3059 begin
3060 Decl := D;
3061 while Present (Decl) loop
3062 if Nkind (Decl) = N_At_Clause
3063 and then Chars (Identifier (Decl)) = Chars (Id)
3064 then
3065 return Decl;
3066
3067 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3068 and then Chars (Decl) = Name_Address
3069 and then Chars (Name (Decl)) = Chars (Id)
3070 then
3071 return Decl;
3072 end if;
3073
3074 Next (Decl);
3075 end loop;
3076
3077 -- Otherwise not found, return Empty
3078
3079 return Empty;
3080 end Check_Decls;
3081
3082 -- Start of processing for Following_Address_Clause
cd2c6027
AC
3083
3084 begin
572f38e4 3085 -- If parser detected no address clause for the identifier in question,
de4ac038 3086 -- then the answer is a quick NO, without the need for a search.
572f38e4 3087
a921e83c 3088 if not Get_Name_Table_Boolean1 (Chars (Id)) then
572f38e4
AC
3089 return Empty;
3090 end if;
3091
3092 -- Otherwise search current declarative unit
3093
f2d9ae20 3094 Result := Check_Decls (Next (D));
cd2c6027 3095
f2d9ae20
AC
3096 if Present (Result) then
3097 return Result;
3098 end if;
cd2c6027 3099
f2d9ae20 3100 -- Check for possible package private part following
cd2c6027 3101
f2d9ae20
AC
3102 Par := Parent (D);
3103
3104 if Nkind (Par) = N_Package_Specification
3105 and then Visible_Declarations (Par) = List_Containing (D)
3106 and then Present (Private_Declarations (Par))
3107 then
3108 -- Private part present, check declarations there
3109
3110 return Check_Decls (First (Private_Declarations (Par)));
3111
3112 else
3113 -- No private part, clause not found, return Empty
3114
3115 return Empty;
3116 end if;
cd2c6027
AC
3117 end Following_Address_Clause;
3118
70482933
RK
3119 ----------------------
3120 -- Force_Evaluation --
3121 ----------------------
3122
28c7180f
RD
3123 procedure Force_Evaluation
3124 (Exp : Node_Id;
3125 Name_Req : Boolean := False;
3126 Related_Id : Entity_Id := Empty;
3127 Is_Low_Bound : Boolean := False;
89d3b1a1
AC
3128 Is_High_Bound : Boolean := False;
3129 Mode : Force_Evaluation_Mode := Relaxed)
28c7180f 3130 is
70482933 3131 begin
28c7180f 3132 Remove_Side_Effects
494a7e45
AC
3133 (Exp => Exp,
3134 Name_Req => Name_Req,
3135 Variable_Ref => True,
3136 Renaming_Req => False,
3137 Related_Id => Related_Id,
3138 Is_Low_Bound => Is_Low_Bound,
3139 Is_High_Bound => Is_High_Bound,
89d3b1a1
AC
3140 Check_Side_Effects =>
3141 Is_Static_Expression (Exp)
3142 or else Mode = Relaxed);
70482933
RK
3143 end Force_Evaluation;
3144
afbcdf5e
AC
3145 ---------------------------------
3146 -- Fully_Qualified_Name_String --
3147 ---------------------------------
3148
72267417
AC
3149 function Fully_Qualified_Name_String
3150 (E : Entity_Id;
3151 Append_NUL : Boolean := True) return String_Id
3152 is
afbcdf5e
AC
3153 procedure Internal_Full_Qualified_Name (E : Entity_Id);
3154 -- Compute recursively the qualified name without NUL at the end, adding
3155 -- it to the currently started string being generated
3156
3157 ----------------------------------
3158 -- Internal_Full_Qualified_Name --
3159 ----------------------------------
3160
3161 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
3162 Ent : Entity_Id;
3163
3164 begin
3165 -- Deal properly with child units
3166
3167 if Nkind (E) = N_Defining_Program_Unit_Name then
3168 Ent := Defining_Identifier (E);
3169 else
3170 Ent := E;
3171 end if;
3172
3173 -- Compute qualification recursively (only "Standard" has no scope)
3174
3175 if Present (Scope (Scope (Ent))) then
3176 Internal_Full_Qualified_Name (Scope (Ent));
3177 Store_String_Char (Get_Char_Code ('.'));
3178 end if;
3179
3180 -- Every entity should have a name except some expanded blocks
3181 -- don't bother about those.
3182
3183 if Chars (Ent) = No_Name then
3184 return;
3185 end if;
3186
3187 -- Generates the entity name in upper case
3188
3189 Get_Decoded_Name_String (Chars (Ent));
3190 Set_All_Upper_Case;
3191 Store_String_Chars (Name_Buffer (1 .. Name_Len));
3192 return;
3193 end Internal_Full_Qualified_Name;
3194
3195 -- Start of processing for Full_Qualified_Name
3196
3197 begin
3198 Start_String;
3199 Internal_Full_Qualified_Name (E);
9d5598bf 3200
72267417
AC
3201 if Append_NUL then
3202 Store_String_Char (Get_Char_Code (ASCII.NUL));
3203 end if;
9d5598bf 3204
afbcdf5e
AC
3205 return End_String;
3206 end Fully_Qualified_Name_String;
3207
70482933
RK
3208 ------------------------
3209 -- Generate_Poll_Call --
3210 ------------------------
3211
3212 procedure Generate_Poll_Call (N : Node_Id) is
3213 begin
3214 -- No poll call if polling not active
3215
3216 if not Polling_Required then
3217 return;
3218
3219 -- Otherwise generate require poll call
3220
3221 else
3222 Insert_Before_And_Analyze (N,
3223 Make_Procedure_Call_Statement (Sloc (N),
3224 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
3225 end if;
3226 end Generate_Poll_Call;
3227
fbf5a39b
AC
3228 ---------------------------------
3229 -- Get_Current_Value_Condition --
3230 ---------------------------------
3231
05350ac6
BD
3232 -- Note: the implementation of this procedure is very closely tied to the
3233 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
3234 -- interpret Current_Value fields set by the Set procedure, so the two
3235 -- procedures need to be closely coordinated.
3236
fbf5a39b
AC
3237 procedure Get_Current_Value_Condition
3238 (Var : Node_Id;
3239 Op : out Node_Kind;
3240 Val : out Node_Id)
3241 is
59e54267
ES
3242 Loc : constant Source_Ptr := Sloc (Var);
3243 Ent : constant Entity_Id := Entity (Var);
fbf5a39b 3244
05350ac6
BD
3245 procedure Process_Current_Value_Condition
3246 (N : Node_Id;
3247 S : Boolean);
3248 -- N is an expression which holds either True (S = True) or False (S =
3249 -- False) in the condition. This procedure digs out the expression and
3250 -- if it refers to Ent, sets Op and Val appropriately.
3251
3252 -------------------------------------
3253 -- Process_Current_Value_Condition --
3254 -------------------------------------
3255
3256 procedure Process_Current_Value_Condition
3257 (N : Node_Id;
3258 S : Boolean)
3259 is
064f4527
TQ
3260 Cond : Node_Id;
3261 Prev_Cond : Node_Id;
3262 Sens : Boolean;
05350ac6
BD
3263
3264 begin
3265 Cond := N;
3266 Sens := S;
3267
064f4527
TQ
3268 loop
3269 Prev_Cond := Cond;
05350ac6 3270
064f4527
TQ
3271 -- Deal with NOT operators, inverting sense
3272
3273 while Nkind (Cond) = N_Op_Not loop
3274 Cond := Right_Opnd (Cond);
3275 Sens := not Sens;
3276 end loop;
3277
3278 -- Deal with conversions, qualifications, and expressions with
3279 -- actions.
3280
3281 while Nkind_In (Cond,
3282 N_Type_Conversion,
3283 N_Qualified_Expression,
3284 N_Expression_With_Actions)
3285 loop
3286 Cond := Expression (Cond);
3287 end loop;
3288
3289 exit when Cond = Prev_Cond;
05350ac6
BD
3290 end loop;
3291
3292 -- Deal with AND THEN and AND cases
3293
0e564ab4
AC
3294 if Nkind_In (Cond, N_And_Then, N_Op_And) then
3295
aa9a7dd7
AC
3296 -- Don't ever try to invert a condition that is of the form of an
3297 -- AND or AND THEN (since we are not doing sufficiently general
3298 -- processing to allow this).
05350ac6
BD
3299
3300 if Sens = False then
3301 Op := N_Empty;
3302 Val := Empty;
3303 return;
3304 end if;
3305
3306 -- Recursively process AND and AND THEN branches
3307
3308 Process_Current_Value_Condition (Left_Opnd (Cond), True);
3309
3310 if Op /= N_Empty then
3311 return;
3312 end if;
3313
3314 Process_Current_Value_Condition (Right_Opnd (Cond), True);
3315 return;
3316
3317 -- Case of relational operator
3318
3319 elsif Nkind (Cond) in N_Op_Compare then
3320 Op := Nkind (Cond);
3321
3322 -- Invert sense of test if inverted test
3323
3324 if Sens = False then
3325 case Op is
3326 when N_Op_Eq => Op := N_Op_Ne;
3327 when N_Op_Ne => Op := N_Op_Eq;
3328 when N_Op_Lt => Op := N_Op_Ge;
3329 when N_Op_Gt => Op := N_Op_Le;
3330 when N_Op_Le => Op := N_Op_Gt;
3331 when N_Op_Ge => Op := N_Op_Lt;
3332 when others => raise Program_Error;
3333 end case;
3334 end if;
3335
3336 -- Case of entity op value
3337
3338 if Is_Entity_Name (Left_Opnd (Cond))
3339 and then Ent = Entity (Left_Opnd (Cond))
3340 and then Compile_Time_Known_Value (Right_Opnd (Cond))
3341 then
3342 Val := Right_Opnd (Cond);
3343
3344 -- Case of value op entity
3345
3346 elsif Is_Entity_Name (Right_Opnd (Cond))
3347 and then Ent = Entity (Right_Opnd (Cond))
3348 and then Compile_Time_Known_Value (Left_Opnd (Cond))
3349 then
3350 Val := Left_Opnd (Cond);
3351
3352 -- We are effectively swapping operands
3353
3354 case Op is
3355 when N_Op_Eq => null;
3356 when N_Op_Ne => null;
3357 when N_Op_Lt => Op := N_Op_Gt;
3358 when N_Op_Gt => Op := N_Op_Lt;
3359 when N_Op_Le => Op := N_Op_Ge;
3360 when N_Op_Ge => Op := N_Op_Le;
3361 when others => raise Program_Error;
3362 end case;
3363
3364 else
3365 Op := N_Empty;
3366 end if;
3367
3368 return;
3369
064f4527
TQ
3370 elsif Nkind_In (Cond,
3371 N_Type_Conversion,
3372 N_Qualified_Expression,
3373 N_Expression_With_Actions)
3374 then
3375 Cond := Expression (Cond);
3376
3377 -- Case of Boolean variable reference, return as though the
3378 -- reference had said var = True.
05350ac6
BD
3379
3380 else
0e564ab4 3381 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
05350ac6
BD
3382 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
3383
3384 if Sens = False then
3385 Op := N_Op_Ne;
3386 else
3387 Op := N_Op_Eq;
3388 end if;
3389 end if;
3390 end if;
3391 end Process_Current_Value_Condition;
3392
3393 -- Start of processing for Get_Current_Value_Condition
3394
fbf5a39b
AC
3395 begin
3396 Op := N_Empty;
3397 Val := Empty;
3398
59e54267 3399 -- Immediate return, nothing doing, if this is not an object
fbf5a39b 3400
59e54267
ES
3401 if Ekind (Ent) not in Object_Kind then
3402 return;
3403 end if;
fbf5a39b 3404
59e54267 3405 -- Otherwise examine current value
fbf5a39b 3406
59e54267
ES
3407 declare
3408 CV : constant Node_Id := Current_Value (Ent);
3409 Sens : Boolean;
3410 Stm : Node_Id;
fbf5a39b 3411
59e54267
ES
3412 begin
3413 -- If statement. Condition is known true in THEN section, known False
3414 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
fbf5a39b 3415
59e54267 3416 if Nkind (CV) = N_If_Statement then
fbf5a39b 3417
59e54267 3418 -- Before start of IF statement
fbf5a39b 3419
59e54267
ES
3420 if Loc < Sloc (CV) then
3421 return;
fbf5a39b 3422
59e54267 3423 -- After end of IF statement
fbf5a39b 3424
59e54267
ES
3425 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
3426 return;
3427 end if;
fbf5a39b 3428
59e54267
ES
3429 -- At this stage we know that we are within the IF statement, but
3430 -- unfortunately, the tree does not record the SLOC of the ELSE so
3431 -- we cannot use a simple SLOC comparison to distinguish between
3432 -- the then/else statements, so we have to climb the tree.
fbf5a39b 3433
59e54267
ES
3434 declare
3435 N : Node_Id;
fbf5a39b 3436
59e54267
ES
3437 begin
3438 N := Parent (Var);
3439 while Parent (N) /= CV loop
3440 N := Parent (N);
fbf5a39b 3441
59e54267
ES
3442 -- If we fall off the top of the tree, then that's odd, but
3443 -- perhaps it could occur in some error situation, and the
3444 -- safest response is simply to assume that the outcome of
3445 -- the condition is unknown. No point in bombing during an
3446 -- attempt to optimize things.
fbf5a39b 3447
59e54267
ES
3448 if No (N) then
3449 return;
3450 end if;
3451 end loop;
fbf5a39b 3452
59e54267
ES
3453 -- Now we have N pointing to a node whose parent is the IF
3454 -- statement in question, so now we can tell if we are within
3455 -- the THEN statements.
fbf5a39b 3456
59e54267
ES
3457 if Is_List_Member (N)
3458 and then List_Containing (N) = Then_Statements (CV)
3459 then
3460 Sens := True;
fbf5a39b 3461
05350ac6
BD
3462 -- If the variable reference does not come from source, we
3463 -- cannot reliably tell whether it appears in the else part.
16b05213 3464 -- In particular, if it appears in generated code for a node
05350ac6
BD
3465 -- that requires finalization, it may be attached to a list
3466 -- that has not been yet inserted into the code. For now,
3467 -- treat it as unknown.
3468
3469 elsif not Comes_From_Source (N) then
3470 return;
3471
3472 -- Otherwise we must be in ELSIF or ELSE part
fbf5a39b 3473
59e54267
ES
3474 else
3475 Sens := False;
3476 end if;
3477 end;
fbf5a39b 3478
59e54267 3479 -- ELSIF part. Condition is known true within the referenced
aa9a7dd7
AC
3480 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
3481 -- and unknown before the ELSE part or after the IF statement.
fbf5a39b 3482
59e54267 3483 elsif Nkind (CV) = N_Elsif_Part then
196379c6
ES
3484
3485 -- if the Elsif_Part had condition_actions, the elsif has been
3486 -- rewritten as a nested if, and the original elsif_part is
3487 -- detached from the tree, so there is no way to obtain useful
3488 -- information on the current value of the variable.
3489 -- Can this be improved ???
3490
3491 if No (Parent (CV)) then
3492 return;
3493 end if;
3494
59e54267 3495 Stm := Parent (CV);
fbf5a39b 3496
a0a10853
AC
3497 -- If the tree has been otherwise rewritten there is nothing
3498 -- else to be done either.
3499
3500 if Nkind (Stm) /= N_If_Statement then
3501 return;
3502 end if;
3503
59e54267 3504 -- Before start of ELSIF part
fbf5a39b 3505
59e54267
ES
3506 if Loc < Sloc (CV) then
3507 return;
fbf5a39b 3508
59e54267 3509 -- After end of IF statement
fbf5a39b 3510
59e54267
ES
3511 elsif Loc >= Sloc (Stm) +
3512 Text_Ptr (UI_To_Int (End_Span (Stm)))
3513 then
3514 return;
3515 end if;
fbf5a39b 3516
59e54267
ES
3517 -- Again we lack the SLOC of the ELSE, so we need to climb the
3518 -- tree to see if we are within the ELSIF part in question.
fbf5a39b 3519
59e54267
ES
3520 declare
3521 N : Node_Id;
fbf5a39b 3522
59e54267
ES
3523 begin
3524 N := Parent (Var);
3525 while Parent (N) /= Stm loop
3526 N := Parent (N);
fbf5a39b 3527
59e54267
ES
3528 -- If we fall off the top of the tree, then that's odd, but
3529 -- perhaps it could occur in some error situation, and the
3530 -- safest response is simply to assume that the outcome of
3531 -- the condition is unknown. No point in bombing during an
3532 -- attempt to optimize things.
fbf5a39b 3533
59e54267
ES
3534 if No (N) then
3535 return;
3536 end if;
3537 end loop;
fbf5a39b 3538
59e54267
ES
3539 -- Now we have N pointing to a node whose parent is the IF
3540 -- statement in question, so see if is the ELSIF part we want.
3541 -- the THEN statements.
fbf5a39b 3542
59e54267
ES
3543 if N = CV then
3544 Sens := True;
fbf5a39b 3545
e14c931f 3546 -- Otherwise we must be in subsequent ELSIF or ELSE part
fbf5a39b 3547
59e54267
ES
3548 else
3549 Sens := False;
3550 end if;
3551 end;
fbf5a39b 3552
05350ac6
BD
3553 -- Iteration scheme of while loop. The condition is known to be
3554 -- true within the body of the loop.
59e54267 3555
05350ac6
BD
3556 elsif Nkind (CV) = N_Iteration_Scheme then
3557 declare
3558 Loop_Stmt : constant Node_Id := Parent (CV);
fbf5a39b 3559
05350ac6
BD
3560 begin
3561 -- Before start of body of loop
fbf5a39b 3562
05350ac6
BD
3563 if Loc < Sloc (Loop_Stmt) then
3564 return;
fbf5a39b 3565
05350ac6 3566 -- After end of LOOP statement
59e54267 3567
05350ac6
BD
3568 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
3569 return;
59e54267 3570
05350ac6 3571 -- We are within the body of the loop
59e54267 3572
05350ac6
BD
3573 else
3574 Sens := True;
3575 end if;
3576 end;
fbf5a39b 3577
05350ac6 3578 -- All other cases of Current_Value settings
fbf5a39b 3579
05350ac6
BD
3580 else
3581 return;
59e54267 3582 end if;
05350ac6
BD
3583
3584 -- If we fall through here, then we have a reportable condition, Sens
3585 -- is True if the condition is true and False if it needs inverting.
3586
3587 Process_Current_Value_Condition (Condition (CV), Sens);
59e54267 3588 end;
fbf5a39b
AC
3589 end Get_Current_Value_Condition;
3590
9eea4346
GB
3591 ---------------------
3592 -- Get_Stream_Size --
3593 ---------------------
3594
3595 function Get_Stream_Size (E : Entity_Id) return Uint is
3596 begin
3597 -- If we have a Stream_Size clause for this type use it
3598
3599 if Has_Stream_Size_Clause (E) then
3600 return Static_Integer (Expression (Stream_Size_Clause (E)));
3601
3602 -- Otherwise the Stream_Size if the size of the type
3603
3604 else
3605 return Esize (E);
3606 end if;
3607 end Get_Stream_Size;
3608
df3e68b1
HK
3609 ---------------------------
3610 -- Has_Access_Constraint --
3611 ---------------------------
3612
3613 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3614 Disc : Entity_Id;
3615 T : constant Entity_Id := Etype (E);
3616
3617 begin
0e564ab4 3618 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
df3e68b1
HK
3619 Disc := First_Discriminant (T);
3620 while Present (Disc) loop
3621 if Is_Access_Type (Etype (Disc)) then
3622 return True;
3623 end if;
3624
3625 Next_Discriminant (Disc);
3626 end loop;
3627
3628 return False;
3629 else
3630 return False;
3631 end if;
3632 end Has_Access_Constraint;
3633
fce54763
AC
3634 -----------------------------------------------------
3635 -- Has_Annotate_Pragma_For_External_Axiomatization --
3636 -----------------------------------------------------
3637
3638 function Has_Annotate_Pragma_For_External_Axiomatization
3639 (E : Entity_Id) return Boolean
3640 is
fce54763
AC
3641 function Is_Annotate_Pragma_For_External_Axiomatization
3642 (N : Node_Id) return Boolean;
3643 -- Returns whether N is
3644 -- pragma Annotate (GNATprove, External_Axiomatization);
3645
3646 ----------------------------------------------------
3647 -- Is_Annotate_Pragma_For_External_Axiomatization --
3648 ----------------------------------------------------
3649
3650 -- The general form of pragma Annotate is
3651
3652 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
3653 -- ARG ::= NAME | EXPRESSION
3654
3655 -- The first two arguments are by convention intended to refer to an
3656 -- external tool and a tool-specific function. These arguments are
3657 -- not analyzed.
3658
3659 -- The following is used to annotate a package specification which
3660 -- GNATprove should treat specially, because the axiomatization of
3661 -- this unit is given by the user instead of being automatically
3662 -- generated.
3663
3664 -- pragma Annotate (GNATprove, External_Axiomatization);
3665
3666 function Is_Annotate_Pragma_For_External_Axiomatization
fd7215d7
AC
3667 (N : Node_Id) return Boolean
3668 is
3669 Name_GNATprove : constant String :=
3670 "gnatprove";
fce54763 3671 Name_External_Axiomatization : constant String :=
fd7215d7
AC
3672 "external_axiomatization";
3673 -- Special names
3674
fce54763
AC
3675 begin
3676 if Nkind (N) = N_Pragma
3677 and then Get_Pragma_Id (Pragma_Name (N)) = Pragma_Annotate
3678 and then List_Length (Pragma_Argument_Associations (N)) = 2
3679 then
3680 declare
3681 Arg1 : constant Node_Id :=
fd7215d7 3682 First (Pragma_Argument_Associations (N));
fce54763
AC
3683 Arg2 : constant Node_Id := Next (Arg1);
3684 Nam1 : Name_Id;
3685 Nam2 : Name_Id;
fd7215d7 3686
fce54763
AC
3687 begin
3688 -- Fill in Name_Buffer with Name_GNATprove first, and then with
3689 -- Name_External_Axiomatization so that Name_Find returns the
3690 -- corresponding name. This takes care of all possible casings.
3691
3692 Name_Len := 0;
3693 Add_Str_To_Name_Buffer (Name_GNATprove);
3694 Nam1 := Name_Find;
3695
3696 Name_Len := 0;
3697 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
3698 Nam2 := Name_Find;
3699
3700 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
fd7215d7
AC
3701 and then
3702 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
fce54763
AC
3703 end;
3704
3705 else
3706 return False;
3707 end if;
3708 end Is_Annotate_Pragma_For_External_Axiomatization;
3709
fd7215d7
AC
3710 -- Local variables
3711
3712 Decl : Node_Id;
fce54763
AC
3713 Vis_Decls : List_Id;
3714 N : Node_Id;
3715
fd7215d7
AC
3716 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
3717
fce54763
AC
3718 begin
3719 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
3720 Decl := Parent (Parent (E));
3721 else
3722 Decl := Parent (E);
3723 end if;
3724
3725 Vis_Decls := Visible_Declarations (Decl);
3726
3727 N := First (Vis_Decls);
3728 while Present (N) loop
3729
3730 -- Skip declarations generated by the frontend. Skip all pragmas
3731 -- that are not the desired Annotate pragma. Stop the search on
3732 -- the first non-pragma source declaration.
3733
3734 if Comes_From_Source (N) then
3735 if Nkind (N) = N_Pragma then
3736 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
3737 return True;
3738 end if;
3739 else
3740 return False;
3741 end if;
3742 end if;
3743
3744 Next (N);
3745 end loop;
3746
3747 return False;
3748 end Has_Annotate_Pragma_For_External_Axiomatization;
3749
70482933
RK
3750 --------------------
3751 -- Homonym_Number --
3752 --------------------
3753
3754 function Homonym_Number (Subp : Entity_Id) return Nat is
3755 Count : Nat;
3756 Hom : Entity_Id;
3757
3758 begin
3759 Count := 1;
3760 Hom := Homonym (Subp);
3761 while Present (Hom) loop
3762 if Scope (Hom) = Scope (Subp) then
3763 Count := Count + 1;
3764 end if;
3765
3766 Hom := Homonym (Hom);
3767 end loop;
3768
3769 return Count;
3770 end Homonym_Number;
3771
df3e68b1
HK
3772 -----------------------------------
3773 -- In_Library_Level_Package_Body --
3774 -----------------------------------
3775
3776 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3777 begin
3778 -- First determine whether the entity appears at the library level, then
3779 -- look at the containing unit.
3780
3781 if Is_Library_Level_Entity (Id) then
3782 declare
3783 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3784
3785 begin
3786 return Nkind (Unit (Container)) = N_Package_Body;
3787 end;
3788 end if;
3789
3790 return False;
3791 end In_Library_Level_Package_Body;
3792
70482933
RK
3793 ------------------------------
3794 -- In_Unconditional_Context --
3795 ------------------------------
3796
3797 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3798 P : Node_Id;
3799
3800 begin
3801 P := Node;
3802 while Present (P) loop
3803 case Nkind (P) is
3804 when N_Subprogram_Body =>
3805 return True;
3806
3807 when N_If_Statement =>
3808 return False;
3809
3810 when N_Loop_Statement =>
3811 return False;
3812
3813 when N_Case_Statement =>
3814 return False;
3815
3816 when others =>
3817 P := Parent (P);
3818 end case;
3819 end loop;
3820
3821 return False;
3822 end In_Unconditional_Context;
3823
3824 -------------------
3825 -- Insert_Action --
3826 -------------------
3827
3828 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3829 begin
3830 if Present (Ins_Action) then
3831 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3832 end if;
3833 end Insert_Action;
3834
3835 -- Version with check(s) suppressed
3836
3837 procedure Insert_Action
3838 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3839 is
3840 begin
3841 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3842 end Insert_Action;
3843
df3e68b1
HK
3844 -------------------------
3845 -- Insert_Action_After --
3846 -------------------------
3847
3848 procedure Insert_Action_After
3849 (Assoc_Node : Node_Id;
3850 Ins_Action : Node_Id)
3851 is
3852 begin
3853 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3854 end Insert_Action_After;
3855
70482933
RK
3856 --------------------
3857 -- Insert_Actions --
3858 --------------------
3859
3860 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3861 N : Node_Id;
3862 P : Node_Id;
3863
3864 Wrapped_Node : Node_Id := Empty;
3865
3866 begin
3867 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3868 return;
3869 end if;
3870
65df5b71
HK
3871 -- Ignore insert of actions from inside default expression (or other
3872 -- similar "spec expression") in the special spec-expression analyze
3873 -- mode. Any insertions at this point have no relevance, since we are
3874 -- only doing the analyze to freeze the types of any static expressions.
3875 -- See section "Handling of Default Expressions" in the spec of package
3876 -- Sem for further details.
70482933 3877
65df5b71 3878 if In_Spec_Expression then
70482933
RK
3879 return;
3880 end if;
3881
3882 -- If the action derives from stuff inside a record, then the actions
3883 -- are attached to the current scope, to be inserted and analyzed on
273adcdf
AC
3884 -- exit from the scope. The reason for this is that we may also be
3885 -- generating freeze actions at the same time, and they must eventually
3886 -- be elaborated in the correct order.
70482933
RK
3887
3888 if Is_Record_Type (Current_Scope)
3889 and then not Is_Frozen (Current_Scope)
3890 then
3891 if No (Scope_Stack.Table
7fcd29e0 3892 (Scope_Stack.Last).Pending_Freeze_Actions)
70482933
RK
3893 then
3894 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3895 Ins_Actions;
3896 else
3897 Append_List
3898 (Ins_Actions,
3899 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3900 end if;
3901
3902 return;
3903 end if;
3904
3905 -- We now intend to climb up the tree to find the right point to
273adcdf
AC
3906 -- insert the actions. We start at Assoc_Node, unless this node is a
3907 -- subexpression in which case we start with its parent. We do this for
3908 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3909 -- itself one of the special nodes like N_And_Then, then we assume that
3910 -- an initial request to insert actions for such a node does not expect
3911 -- the actions to get deposited in the node for later handling when the
3912 -- node is expanded, since clearly the node is being dealt with by the
3913 -- caller. Note that in the subexpression case, N is always the child we
3914 -- came from.
3915
9bdc432a
AC
3916 -- N_Raise_xxx_Error is an annoying special case, it is a statement
3917 -- if it has type Standard_Void_Type, and a subexpression otherwise.
3918 -- Procedure calls, and similarly procedure attribute references, are
3919 -- also statements.
70482933
RK
3920
3921 if Nkind (Assoc_Node) in N_Subexpr
a52e6d7e 3922 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
70482933 3923 or else Etype (Assoc_Node) /= Standard_Void_Type)
a52e6d7e 3924 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
70482933 3925 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
c5c780e6
HK
3926 or else not Is_Procedure_Attribute_Name
3927 (Attribute_Name (Assoc_Node)))
70482933 3928 then
a52e6d7e
AC
3929 N := Assoc_Node;
3930 P := Parent (Assoc_Node);
70482933 3931
273adcdf
AC
3932 -- Non-subexpression case. Note that N is initially Empty in this case
3933 -- (N is only guaranteed Non-Empty in the subexpr case).
70482933
RK
3934
3935 else
70482933 3936 N := Empty;
a52e6d7e 3937 P := Assoc_Node;
70482933
RK
3938 end if;
3939
3940 -- Capture root of the transient scope
3941
3942 if Scope_Is_Transient then
05350ac6 3943 Wrapped_Node := Node_To_Be_Wrapped;
70482933
RK
3944 end if;
3945
3946 loop
3947 pragma Assert (Present (P));
3948
a52e6d7e
AC
3949 -- Make sure that inserted actions stay in the transient scope
3950
3951 if Present (Wrapped_Node) and then N = Wrapped_Node then
3952 Store_Before_Actions_In_Scope (Ins_Actions);
3953 return;
3954 end if;
3955
70482933
RK
3956 case Nkind (P) is
3957
3958 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3959 -- in the Actions field of the right operand. They will be moved
3960 -- out further when the AND THEN or OR ELSE operator is expanded.
3961 -- Nothing special needs to be done for the left operand since
3962 -- in that case the actions are executed unconditionally.
3963
ac7120ce 3964 when N_Short_Circuit =>
70482933 3965 if N = Right_Opnd (P) then
ac4d6407
RD
3966
3967 -- We are now going to either append the actions to the
3968 -- actions field of the short-circuit operation. We will
3969 -- also analyze the actions now.
3970
3971 -- This analysis is really too early, the proper thing would
3972 -- be to just park them there now, and only analyze them if
3973 -- we find we really need them, and to it at the proper
3974 -- final insertion point. However attempting to this proved
3975 -- tricky, so for now we just kill current values before and
3976 -- after the analyze call to make sure we avoid peculiar
3977 -- optimizations from this out of order insertion.
3978
3979 Kill_Current_Values;
3980
2e70d415
AC
3981 -- If P has already been expanded, we can't park new actions
3982 -- on it, so we need to expand them immediately, introducing
3983 -- an Expression_With_Actions. N can't be an expression
3984 -- with actions, or else then the actions would have been
3985 -- inserted at an inner level.
3986
3987 if Analyzed (P) then
3988 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
3989 Rewrite (N,
3990 Make_Expression_With_Actions (Sloc (N),
3991 Actions => Ins_Actions,
3992 Expression => Relocate_Node (N)));
3993 Analyze_And_Resolve (N);
3994
3995 elsif Present (Actions (P)) then
70482933 3996 Insert_List_After_And_Analyze
ac4d6407 3997 (Last (Actions (P)), Ins_Actions);
70482933
RK
3998 else
3999 Set_Actions (P, Ins_Actions);
4000 Analyze_List (Actions (P));
4001 end if;
4002
ac4d6407
RD
4003 Kill_Current_Values;
4004
70482933
RK
4005 return;
4006 end if;
4007
9b16cb57
RD
4008 -- Then or Else dependent expression of an if expression. Add
4009 -- actions to Then_Actions or Else_Actions field as appropriate.
4010 -- The actions will be moved further out when the if is expanded.
70482933 4011
9b16cb57 4012 when N_If_Expression =>
70482933
RK
4013 declare
4014 ThenX : constant Node_Id := Next (First (Expressions (P)));
4015 ElseX : constant Node_Id := Next (ThenX);
4016
4017 begin
aa9a7dd7
AC
4018 -- If the enclosing expression is already analyzed, as
4019 -- is the case for nested elaboration checks, insert the
4020 -- conditional further out.
70482933 4021
aa9a7dd7
AC
4022 if Analyzed (P) then
4023 null;
4024
4025 -- Actions belong to the then expression, temporarily place
9b16cb57
RD
4026 -- them as Then_Actions of the if expression. They will be
4027 -- moved to the proper place later when the if expression
4028 -- is expanded.
aa9a7dd7
AC
4029
4030 elsif N = ThenX then
70482933
RK
4031 if Present (Then_Actions (P)) then
4032 Insert_List_After_And_Analyze
4033 (Last (Then_Actions (P)), Ins_Actions);
4034 else
4035 Set_Then_Actions (P, Ins_Actions);
4036 Analyze_List (Then_Actions (P));
4037 end if;
4038
4039 return;
4040
9b16cb57
RD
4041 -- Actions belong to the else expression, temporarily place
4042 -- them as Else_Actions of the if expression. They will be
4043 -- moved to the proper place later when the if expression
4044 -- is expanded.
70482933
RK
4045
4046 elsif N = ElseX then
4047 if Present (Else_Actions (P)) then
4048 Insert_List_After_And_Analyze
4049 (Last (Else_Actions (P)), Ins_Actions);
4050 else
4051 Set_Else_Actions (P, Ins_Actions);
4052 Analyze_List (Else_Actions (P));
4053 end if;
4054
4055 return;
4056
4057 -- Actions belong to the condition. In this case they are
4058 -- unconditionally executed, and so we can continue the
4059 -- search for the proper insert point.
4060
4061 else
4062 null;
4063 end if;
4064 end;
4065
aa9a7dd7
AC
4066 -- Alternative of case expression, we place the action in the
4067 -- Actions field of the case expression alternative, this will
4068 -- be handled when the case expression is expanded.
19d846a0
RD
4069
4070 when N_Case_Expression_Alternative =>
4071 if Present (Actions (P)) then
4072 Insert_List_After_And_Analyze
4073 (Last (Actions (P)), Ins_Actions);
4074 else
4075 Set_Actions (P, Ins_Actions);
1c54829e 4076 Analyze_List (Actions (P));
19d846a0
RD
4077 end if;
4078
4079 return;
4080
a52e6d7e
AC
4081 -- Case of appearing within an Expressions_With_Actions node. When
4082 -- the new actions come from the expression of the expression with
4083 -- actions, they must be added to the existing actions. The other
4084 -- alternative is when the new actions are related to one of the
2e70d415 4085 -- existing actions of the expression with actions, and should
f5f6d8d7
AC
4086 -- never reach here: if actions are inserted on a statement
4087 -- within the Actions of an expression with actions, or on some
2e70d415
AC
4088 -- sub-expression of such a statement, then the outermost proper
4089 -- insertion point is right before the statement, and we should
4090 -- never climb up as far as the N_Expression_With_Actions itself.
955871d3
AC
4091
4092 when N_Expression_With_Actions =>
a52e6d7e 4093 if N = Expression (P) then
064f4527
TQ
4094 if Is_Empty_List (Actions (P)) then
4095 Append_List_To (Actions (P), Ins_Actions);
4096 Analyze_List (Actions (P));
4097 else
4098 Insert_List_After_And_Analyze
4099 (Last (Actions (P)), Ins_Actions);
4100 end if;
f5f6d8d7 4101
72e9f2b9 4102 return;
2e70d415
AC
4103
4104 else
4105 raise Program_Error;
72e9f2b9 4106 end if;
955871d3 4107
70482933
RK
4108 -- Case of appearing in the condition of a while expression or
4109 -- elsif. We insert the actions into the Condition_Actions field.
4110 -- They will be moved further out when the while loop or elsif
4111 -- is analyzed.
4112
4113 when N_Iteration_Scheme |
4114 N_Elsif_Part
4115 =>
4116 if N = Condition (P) then
4117 if Present (Condition_Actions (P)) then
4118 Insert_List_After_And_Analyze
4119 (Last (Condition_Actions (P)), Ins_Actions);
4120 else
4121 Set_Condition_Actions (P, Ins_Actions);
4122
aa9a7dd7
AC
4123 -- Set the parent of the insert actions explicitly. This
4124 -- is not a syntactic field, but we need the parent field
4125 -- set, in particular so that freeze can understand that
4126 -- it is dealing with condition actions, and properly
4127 -- insert the freezing actions.
70482933
RK
4128
4129 Set_Parent (Ins_Actions, P);
4130 Analyze_List (Condition_Actions (P));
4131 end if;
4132
4133 return;
4134 end if;
4135
bebbff91 4136 -- Statements, declarations, pragmas, representation clauses
70482933
RK
4137
4138 when
4139 -- Statements
4140
4141 N_Procedure_Call_Statement |
4142 N_Statement_Other_Than_Procedure_Call |
4143
4144 -- Pragmas
4145
4146 N_Pragma |
4147
4148 -- Representation_Clause
4149
4150 N_At_Clause |
4151 N_Attribute_Definition_Clause |
4152 N_Enumeration_Representation_Clause |
4153 N_Record_Representation_Clause |
4154
4155 -- Declarations
4156
4157 N_Abstract_Subprogram_Declaration |
4158 N_Entry_Body |
4159 N_Exception_Declaration |
4160 N_Exception_Renaming_Declaration |
b0186f71 4161 N_Expression_Function |
82c80734
RD
4162 N_Formal_Abstract_Subprogram_Declaration |
4163 N_Formal_Concrete_Subprogram_Declaration |
70482933 4164 N_Formal_Object_Declaration |
70482933
RK
4165 N_Formal_Type_Declaration |
4166 N_Full_Type_Declaration |
4167 N_Function_Instantiation |
4168 N_Generic_Function_Renaming_Declaration |
4169 N_Generic_Package_Declaration |
4170 N_Generic_Package_Renaming_Declaration |
4171 N_Generic_Procedure_Renaming_Declaration |
4172 N_Generic_Subprogram_Declaration |
4173 N_Implicit_Label_Declaration |
4174 N_Incomplete_Type_Declaration |
4175 N_Number_Declaration |
4176 N_Object_Declaration |
4177 N_Object_Renaming_Declaration |
4178 N_Package_Body |
4179 N_Package_Body_Stub |
4180 N_Package_Declaration |
4181 N_Package_Instantiation |
4182 N_Package_Renaming_Declaration |
4183 N_Private_Extension_Declaration |
4184 N_Private_Type_Declaration |
4185 N_Procedure_Instantiation |
19590d70 4186 N_Protected_Body |
70482933
RK
4187 N_Protected_Body_Stub |
4188 N_Protected_Type_Declaration |
4189 N_Single_Task_Declaration |
4190 N_Subprogram_Body |
4191 N_Subprogram_Body_Stub |
4192 N_Subprogram_Declaration |
4193 N_Subprogram_Renaming_Declaration |
4194 N_Subtype_Declaration |
4195 N_Task_Body |
4196 N_Task_Body_Stub |
4197 N_Task_Type_Declaration |
4198
8c889ae4
AC
4199 -- Use clauses can appear in lists of declarations
4200
4201 N_Use_Package_Clause |
4202 N_Use_Type_Clause |
4203
70482933
RK
4204 -- Freeze entity behaves like a declaration or statement
4205
3cd4a210
AC
4206 N_Freeze_Entity |
4207 N_Freeze_Generic_Entity
70482933
RK
4208 =>
4209 -- Do not insert here if the item is not a list member (this
4210 -- happens for example with a triggering statement, and the
4211 -- proper approach is to insert before the entire select).
4212
4213 if not Is_List_Member (P) then
4214 null;
4215
4216 -- Do not insert if parent of P is an N_Component_Association
05350ac6
BD
4217 -- node (i.e. we are in the context of an N_Aggregate or
4218 -- N_Extension_Aggregate node. In this case we want to insert
4219 -- before the entire aggregate.
70482933
RK
4220
4221 elsif Nkind (Parent (P)) = N_Component_Association then
4222 null;
4223
273adcdf
AC
4224 -- Do not insert if the parent of P is either an N_Variant node
4225 -- or an N_Record_Definition node, meaning in either case that
4226 -- P is a member of a component list, and that therefore the
4227 -- actions should be inserted outside the complete record
4228 -- declaration.
70482933 4229
0e564ab4 4230 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
70482933
RK
4231 null;
4232
4233 -- Do not insert freeze nodes within the loop generated for
4234 -- an aggregate, because they may be elaborated too late for
4235 -- subsequent use in the back end: within a package spec the
4236 -- loop is part of the elaboration procedure and is only
4237 -- elaborated during the second pass.
aa9a7dd7 4238
273adcdf
AC
4239 -- If the loop comes from source, or the entity is local to the
4240 -- loop itself it must remain within.
70482933
RK
4241
4242 elsif Nkind (Parent (P)) = N_Loop_Statement
4243 and then not Comes_From_Source (Parent (P))
4244 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
4245 and then
4246 Scope (Entity (First (Ins_Actions))) /= Current_Scope
4247 then
4248 null;
4249
4250 -- Otherwise we can go ahead and do the insertion
4251
05350ac6 4252 elsif P = Wrapped_Node then
70482933
RK
4253 Store_Before_Actions_In_Scope (Ins_Actions);
4254 return;
4255
4256 else
4257 Insert_List_Before_And_Analyze (P, Ins_Actions);
4258 return;
4259 end if;
4260
aa9a7dd7
AC
4261 -- A special case, N_Raise_xxx_Error can act either as a statement
4262 -- or a subexpression. We tell the difference by looking at the
4263 -- Etype. It is set to Standard_Void_Type in the statement case.
70482933
RK
4264
4265 when
4266 N_Raise_xxx_Error =>
4267 if Etype (P) = Standard_Void_Type then
9fe696a3 4268 if P = Wrapped_Node then
70482933
RK
4269 Store_Before_Actions_In_Scope (Ins_Actions);
4270 else
4271 Insert_List_Before_And_Analyze (P, Ins_Actions);
4272 end if;
4273
4274 return;
4275
4276 -- In the subexpression case, keep climbing
4277
4278 else
4279 null;
4280 end if;
4281
4282 -- If a component association appears within a loop created for
4283 -- an array aggregate, attach the actions to the association so
4284 -- they can be subsequently inserted within the loop. For other
fbf5a39b
AC
4285 -- component associations insert outside of the aggregate. For
4286 -- an association that will generate a loop, its Loop_Actions
4287 -- attribute is already initialized (see exp_aggr.adb).
70482933
RK
4288
4289 -- The list of loop_actions can in turn generate additional ones,
4290 -- that are inserted before the associated node. If the associated
4291 -- node is outside the aggregate, the new actions are collected
4292 -- at the end of the loop actions, to respect the order in which
4293 -- they are to be elaborated.
4294
4295 when
4296 N_Component_Association =>
4297 if Nkind (Parent (P)) = N_Aggregate
fbf5a39b 4298 and then Present (Loop_Actions (P))
70482933 4299 then
fbf5a39b 4300 if Is_Empty_List (Loop_Actions (P)) then
70482933
RK
4301 Set_Loop_Actions (P, Ins_Actions);
4302 Analyze_List (Ins_Actions);
4303
4304 else
4305 declare
bebbff91 4306 Decl : Node_Id;
70482933
RK
4307
4308 begin
aa9a7dd7
AC
4309 -- Check whether these actions were generated by a
4310 -- declaration that is part of the loop_ actions
4311 -- for the component_association.
70482933 4312
bebbff91 4313 Decl := Assoc_Node;
70482933
RK
4314 while Present (Decl) loop
4315 exit when Parent (Decl) = P
4316 and then Is_List_Member (Decl)
4317 and then
4318 List_Containing (Decl) = Loop_Actions (P);
4319 Decl := Parent (Decl);
4320 end loop;
4321
4322 if Present (Decl) then
4323 Insert_List_Before_And_Analyze
4324 (Decl, Ins_Actions);
4325 else
4326 Insert_List_After_And_Analyze
4327 (Last (Loop_Actions (P)), Ins_Actions);
4328 end if;
4329 end;
4330 end if;
4331
4332 return;
4333
4334 else
4335 null;
4336 end if;
4337
4338 -- Another special case, an attribute denoting a procedure call
4339
4340 when
4341 N_Attribute_Reference =>
4342 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
4343 if P = Wrapped_Node then
4344 Store_Before_Actions_In_Scope (Ins_Actions);
4345 else
4346 Insert_List_Before_And_Analyze (P, Ins_Actions);
4347 end if;
4348
4349 return;
4350
4351 -- In the subexpression case, keep climbing
4352
4353 else
4354 null;
4355 end if;
4356
dac3bede
YM
4357 -- A contract node should not belong to the tree
4358
4359 when N_Contract =>
4360 raise Program_Error;
4361
70482933
RK
4362 -- For all other node types, keep climbing tree
4363
4364 when
4365 N_Abortable_Part |
4366 N_Accept_Alternative |
4367 N_Access_Definition |
4368 N_Access_Function_Definition |
4369 N_Access_Procedure_Definition |
4370 N_Access_To_Object_Definition |
4371 N_Aggregate |
4372 N_Allocator |
313d6f2c 4373 N_Aspect_Specification |
19d846a0 4374 N_Case_Expression |
70482933
RK
4375 N_Case_Statement_Alternative |
4376 N_Character_Literal |
4377 N_Compilation_Unit |
4378 N_Compilation_Unit_Aux |
4379 N_Component_Clause |
4380 N_Component_Declaration |
a397db96 4381 N_Component_Definition |
70482933
RK
4382 N_Component_List |
4383 N_Constrained_Array_Definition |
4384 N_Decimal_Fixed_Point_Definition |
4385 N_Defining_Character_Literal |
4386 N_Defining_Identifier |
4387 N_Defining_Operator_Symbol |
4388 N_Defining_Program_Unit_Name |
4389 N_Delay_Alternative |
4390 N_Delta_Constraint |
4391 N_Derived_Type_Definition |
4392 N_Designator |
4393 N_Digits_Constraint |
4394 N_Discriminant_Association |
4395 N_Discriminant_Specification |
4396 N_Empty |
4397 N_Entry_Body_Formal_Part |
4398 N_Entry_Call_Alternative |
4399 N_Entry_Declaration |
4400 N_Entry_Index_Specification |
4401 N_Enumeration_Type_Definition |
4402 N_Error |
4403 N_Exception_Handler |
4404 N_Expanded_Name |
4405 N_Explicit_Dereference |
4406 N_Extension_Aggregate |
4407 N_Floating_Point_Definition |
4408 N_Formal_Decimal_Fixed_Point_Definition |
4409 N_Formal_Derived_Type_Definition |
4410 N_Formal_Discrete_Type_Definition |
4411 N_Formal_Floating_Point_Definition |
4412 N_Formal_Modular_Type_Definition |
4413 N_Formal_Ordinary_Fixed_Point_Definition |
4414 N_Formal_Package_Declaration |
4415 N_Formal_Private_Type_Definition |
d3cb4cc0 4416 N_Formal_Incomplete_Type_Definition |
70482933
RK
4417 N_Formal_Signed_Integer_Type_Definition |
4418 N_Function_Call |
4419 N_Function_Specification |
4420 N_Generic_Association |
4421 N_Handled_Sequence_Of_Statements |
4422 N_Identifier |
4423 N_In |
4424 N_Index_Or_Discriminant_Constraint |
4425 N_Indexed_Component |
4426 N_Integer_Literal |
57d62f0c 4427 N_Iterator_Specification |
70482933
RK
4428 N_Itype_Reference |
4429 N_Label |
4430 N_Loop_Parameter_Specification |
4431 N_Mod_Clause |
4432 N_Modular_Type_Definition |
4433 N_Not_In |
4434 N_Null |
4435 N_Op_Abs |
4436 N_Op_Add |
4437 N_Op_And |
4438 N_Op_Concat |
4439 N_Op_Divide |
4440 N_Op_Eq |
4441 N_Op_Expon |
4442 N_Op_Ge |
4443 N_Op_Gt |
4444 N_Op_Le |
4445 N_Op_Lt |
4446 N_Op_Minus |
4447 N_Op_Mod |
4448 N_Op_Multiply |
4449 N_Op_Ne |
4450 N_Op_Not |
4451 N_Op_Or |
4452 N_Op_Plus |
4453 N_Op_Rem |
4454 N_Op_Rotate_Left |
4455 N_Op_Rotate_Right |
4456 N_Op_Shift_Left |
4457 N_Op_Shift_Right |
4458 N_Op_Shift_Right_Arithmetic |
4459 N_Op_Subtract |
4460 N_Op_Xor |
4461 N_Operator_Symbol |
4462 N_Ordinary_Fixed_Point_Definition |
4463 N_Others_Choice |
4464 N_Package_Specification |
4465 N_Parameter_Association |
4466 N_Parameter_Specification |
dee4682a
JM
4467 N_Pop_Constraint_Error_Label |
4468 N_Pop_Program_Error_Label |
4469 N_Pop_Storage_Error_Label |
70482933
RK
4470 N_Pragma_Argument_Association |
4471 N_Procedure_Specification |
70482933 4472 N_Protected_Definition |
dee4682a
JM
4473 N_Push_Constraint_Error_Label |
4474 N_Push_Program_Error_Label |
4475 N_Push_Storage_Error_Label |
70482933 4476 N_Qualified_Expression |
a961aa79 4477 N_Quantified_Expression |
c8d63650 4478 N_Raise_Expression |
70482933
RK
4479 N_Range |
4480 N_Range_Constraint |
4481 N_Real_Literal |
4482 N_Real_Range_Specification |
4483 N_Record_Definition |
4484 N_Reference |
327503f1
JM
4485 N_SCIL_Dispatch_Table_Tag_Init |
4486 N_SCIL_Dispatching_Call |
82878151 4487 N_SCIL_Membership_Test |
70482933
RK
4488 N_Selected_Component |
4489 N_Signed_Integer_Type_Definition |
4490 N_Single_Protected_Declaration |
4491 N_Slice |
4492 N_String_Literal |
70482933
RK
4493 N_Subtype_Indication |
4494 N_Subunit |
4495 N_Task_Definition |
4496 N_Terminate_Alternative |
4497 N_Triggering_Alternative |
4498 N_Type_Conversion |
4499 N_Unchecked_Expression |
4500 N_Unchecked_Type_Conversion |
4501 N_Unconstrained_Array_Definition |
4502 N_Unused_At_End |
4503 N_Unused_At_Start |
70482933
RK
4504 N_Variant |
4505 N_Variant_Part |
4506 N_Validate_Unchecked_Conversion |
0712790c 4507 N_With_Clause
70482933
RK
4508 =>
4509 null;
4510
4511 end case;
4512
70482933
RK
4513 -- If we fall through above tests, keep climbing tree
4514
4515 N := P;
4516
4517 if Nkind (Parent (N)) = N_Subunit then
4518
aa9a7dd7
AC
4519 -- This is the proper body corresponding to a stub. Insertion must
4520 -- be done at the point of the stub, which is in the declarative
4521 -- part of the parent unit.
70482933
RK
4522
4523 P := Corresponding_Stub (Parent (N));
4524
4525 else
4526 P := Parent (N);
4527 end if;
4528 end loop;
70482933
RK
4529 end Insert_Actions;
4530
4531 -- Version with check(s) suppressed
4532
4533 procedure Insert_Actions
0712790c
ES
4534 (Assoc_Node : Node_Id;
4535 Ins_Actions : List_Id;
4536 Suppress : Check_Id)
70482933
RK
4537 is
4538 begin
4539 if Suppress = All_Checks then
4540 declare
a7f1b24f 4541 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
70482933 4542 begin
a7f1b24f 4543 Scope_Suppress.Suppress := (others => True);
70482933 4544 Insert_Actions (Assoc_Node, Ins_Actions);
a7f1b24f 4545 Scope_Suppress.Suppress := Sva;
70482933
RK
4546 end;
4547
4548 else
4549 declare
3217f71e 4550 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
70482933 4551 begin
3217f71e 4552 Scope_Suppress.Suppress (Suppress) := True;
70482933 4553 Insert_Actions (Assoc_Node, Ins_Actions);
3217f71e 4554 Scope_Suppress.Suppress (Suppress) := Svg;
70482933
RK
4555 end;
4556 end if;
4557 end Insert_Actions;
4558
4559 --------------------------
4560 -- Insert_Actions_After --
4561 --------------------------
4562
4563 procedure Insert_Actions_After
4564 (Assoc_Node : Node_Id;
4565 Ins_Actions : List_Id)
4566 is
4567 begin
0e564ab4 4568 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
70482933
RK
4569 Store_After_Actions_In_Scope (Ins_Actions);
4570 else
4571 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
4572 end if;
4573 end Insert_Actions_After;
4574
e03f7ccf
AC
4575 ------------------------
4576 -- Insert_Declaration --
4577 ------------------------
4578
4579 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
4580 P : Node_Id;
4581
4582 begin
4583 pragma Assert (Nkind (N) in N_Subexpr);
4584
4585 -- Climb until we find a procedure or a package
4586
adb252d8 4587 P := N;
e03f7ccf 4588 loop
adb252d8
AC
4589 pragma Assert (Present (Parent (P)));
4590 P := Parent (P);
4591
e03f7ccf
AC
4592 if Is_List_Member (P) then
4593 exit when Nkind_In (Parent (P), N_Package_Specification,
e03f7ccf
AC
4594 N_Subprogram_Body);
4595
4596 -- Special handling for handled sequence of statements, we must
4597 -- insert in the statements not the exception handlers!
4598
4599 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
4600 P := First (Statements (Parent (P)));
4601 exit;
4602 end if;
4603 end if;
e03f7ccf
AC
4604 end loop;
4605
4606 -- Now do the insertion
4607
4608 Insert_Before (P, Decl);
4609 Analyze (Decl);
4610 end Insert_Declaration;
4611
70482933
RK
4612 ---------------------------------
4613 -- Insert_Library_Level_Action --
4614 ---------------------------------
4615
4616 procedure Insert_Library_Level_Action (N : Node_Id) is
4617 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4618
4619 begin
0712790c
ES
4620 Push_Scope (Cunit_Entity (Main_Unit));
4621 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
70482933
RK
4622
4623 if No (Actions (Aux)) then
4624 Set_Actions (Aux, New_List (N));
4625 else
4626 Append (N, Actions (Aux));
4627 end if;
4628
4629 Analyze (N);
4630 Pop_Scope;
4631 end Insert_Library_Level_Action;
4632
4633 ----------------------------------
4634 -- Insert_Library_Level_Actions --
4635 ----------------------------------
4636
4637 procedure Insert_Library_Level_Actions (L : List_Id) is
4638 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4639
4640 begin
4641 if Is_Non_Empty_List (L) then
0712790c
ES
4642 Push_Scope (Cunit_Entity (Main_Unit));
4643 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
70482933
RK
4644
4645 if No (Actions (Aux)) then
4646 Set_Actions (Aux, L);
4647 Analyze_List (L);
4648 else
4649 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
4650 end if;
4651
4652 Pop_Scope;
4653 end if;
4654 end Insert_Library_Level_Actions;
4655
4656 ----------------------
4657 -- Inside_Init_Proc --
4658 ----------------------
4659
4660 function Inside_Init_Proc return Boolean is
4661 S : Entity_Id;
4662
4663 begin
4664 S := Current_Scope;
0e564ab4 4665 while Present (S) and then S /= Standard_Standard loop
fbf5a39b 4666 if Is_Init_Proc (S) then
70482933
RK
4667 return True;
4668 else
4669 S := Scope (S);
4670 end if;
4671 end loop;
4672
4673 return False;
4674 end Inside_Init_Proc;
4675
fbf5a39b
AC
4676 ----------------------------
4677 -- Is_All_Null_Statements --
4678 ----------------------------
4679
4680 function Is_All_Null_Statements (L : List_Id) return Boolean is
4681 Stm : Node_Id;
4682
4683 begin
4684 Stm := First (L);
4685 while Present (Stm) loop
4686 if Nkind (Stm) /= N_Null_Statement then
4687 return False;
4688 end if;
4689
4690 Next (Stm);
4691 end loop;
4692
4693 return True;
4694 end Is_All_Null_Statements;
4695
a429e6b3
AC
4696 --------------------------------------------------
4697 -- Is_Displacement_Of_Object_Or_Function_Result --
4698 --------------------------------------------------
aab08130 4699
a429e6b3 4700 function Is_Displacement_Of_Object_Or_Function_Result
aab08130
AC
4701 (Obj_Id : Entity_Id) return Boolean
4702 is
a429e6b3 4703 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
36295779
AC
4704 -- Determine if particular node denotes a controlled function call. The
4705 -- call may have been heavily expanded.
aab08130
AC
4706
4707 function Is_Displace_Call (N : Node_Id) return Boolean;
4708 -- Determine whether a particular node is a call to Ada.Tags.Displace.
4709 -- The call might be nested within other actions such as conversions.
4710
a429e6b3
AC
4711 function Is_Source_Object (N : Node_Id) return Boolean;
4712 -- Determine whether a particular node denotes a source object
4713
4714 ---------------------------------
4715 -- Is_Controlled_Function_Call --
4716 ---------------------------------
aab08130 4717
a429e6b3
AC
4718 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
4719 Expr : Node_Id := Original_Node (N);
f307415a 4720
aab08130 4721 begin
36295779 4722 -- When a function call appears in Object.Operation format, the
34557478
HK
4723 -- original representation has several possible forms depending on
4724 -- the availability and form of actual parameters:
bb012790 4725
a8a42b93
AC
4726 -- Obj.Func N_Selected_Component
4727 -- Obj.Func (Actual) N_Indexed_Component
4728 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
4729 -- N_Selected_Component
36295779 4730
a8a42b93
AC
4731 case Nkind (Expr) is
4732 when N_Function_Call =>
4733 Expr := Name (Expr);
4734
4735 -- Check for "Obj.Func (Formal => Actual)" case
4736
4737 if Nkind (Expr) = N_Selected_Component then
4738 Expr := Selector_Name (Expr);
4739 end if;
4740
4741 -- "Obj.Func (Actual)" case
4742
4743 when N_Indexed_Component =>
36295779 4744 Expr := Prefix (Expr);
f307415a 4745
a8a42b93
AC
4746 if Nkind (Expr) = N_Selected_Component then
4747 Expr := Selector_Name (Expr);
4748 end if;
4749
4750 -- "Obj.Func" case
4751
4752 when N_Selected_Component =>
36295779 4753 Expr := Selector_Name (Expr);
a8a42b93
AC
4754
4755 when others => null;
4756 end case;
f307415a 4757
aab08130 4758 return
f307415a
AC
4759 Nkind_In (Expr, N_Expanded_Name, N_Identifier)
4760 and then Ekind (Entity (Expr)) = E_Function
4761 and then Needs_Finalization (Etype (Entity (Expr)));
a429e6b3 4762 end Is_Controlled_Function_Call;
aab08130
AC
4763
4764 ----------------------
4765 -- Is_Displace_Call --
4766 ----------------------
4767
4768 function Is_Displace_Call (N : Node_Id) return Boolean is
4769 Call : Node_Id := N;
4770
4771 begin
4772 -- Strip various actions which may precede a call to Displace
4773
4774 loop
4775 if Nkind (Call) = N_Explicit_Dereference then
4776 Call := Prefix (Call);
4777
4778 elsif Nkind_In (Call, N_Type_Conversion,
4779 N_Unchecked_Type_Conversion)
4780 then
4781 Call := Expression (Call);
9aa04cc7 4782
aab08130
AC
4783 else
4784 exit;
4785 end if;
4786 end loop;
4787
4788 return
a429e6b3
AC
4789 Present (Call)
4790 and then Nkind (Call) = N_Function_Call
aab08130
AC
4791 and then Is_RTE (Entity (Name (Call)), RE_Displace);
4792 end Is_Displace_Call;
4793
a429e6b3
AC
4794 ----------------------
4795 -- Is_Source_Object --
4796 ----------------------
4797
4798 function Is_Source_Object (N : Node_Id) return Boolean is
4799 begin
4800 return
4801 Present (N)
4802 and then Nkind (N) in N_Has_Entity
4803 and then Is_Object (Entity (N))
4804 and then Comes_From_Source (N);
4805 end Is_Source_Object;
4806
aab08130
AC
4807 -- Local variables
4808
4809 Decl : constant Node_Id := Parent (Obj_Id);
4810 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4811 Orig_Decl : constant Node_Id := Original_Node (Decl);
4812
a429e6b3 4813 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
aab08130
AC
4814
4815 begin
a429e6b3 4816 -- Case 1:
aab08130 4817
a429e6b3 4818 -- Obj : CW_Type := Function_Call (...);
aab08130 4819
a429e6b3 4820 -- rewritten into:
aab08130 4821
a429e6b3
AC
4822 -- Tmp : ... := Function_Call (...)'reference;
4823 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
aab08130 4824
a429e6b3
AC
4825 -- where the return type of the function and the class-wide type require
4826 -- dispatch table pointer displacement.
4827
4828 -- Case 2:
4829
4830 -- Obj : CW_Type := Src_Obj;
4831
4832 -- rewritten into:
4833
4834 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4835
4836 -- where the type of the source object and the class-wide type require
aab08130
AC
4837 -- dispatch table pointer displacement.
4838
4839 return
4840 Nkind (Decl) = N_Object_Renaming_Declaration
4841 and then Nkind (Orig_Decl) = N_Object_Declaration
4842 and then Comes_From_Source (Orig_Decl)
aab08130 4843 and then Is_Class_Wide_Type (Obj_Typ)
a429e6b3
AC
4844 and then Is_Displace_Call (Renamed_Object (Obj_Id))
4845 and then
4846 (Is_Controlled_Function_Call (Expression (Orig_Decl))
36504e5f 4847 or else Is_Source_Object (Expression (Orig_Decl)));
a429e6b3 4848 end Is_Displacement_Of_Object_Or_Function_Result;
aab08130 4849
df3e68b1
HK
4850 ------------------------------
4851 -- Is_Finalizable_Transient --
4852 ------------------------------
4853
4854 function Is_Finalizable_Transient
4855 (Decl : Node_Id;
4856 Rel_Node : Node_Id) return Boolean
4857 is
f7bb41af
AC
4858 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4859 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
df3e68b1
HK
4860
4861 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4862 -- Determine whether transient object Trans_Id is initialized either
4863 -- by a function call which returns an access type or simply renames
4864 -- another pointer.
4865
4866 function Initialized_By_Aliased_BIP_Func_Call
4867 (Trans_Id : Entity_Id) return Boolean;
4868 -- Determine whether transient object Trans_Id is initialized by a
4869 -- build-in-place function call where the BIPalloc parameter is of
4870 -- value 1 and BIPaccess is not null. This case creates an aliasing
4871 -- between the returned value and the value denoted by BIPaccess.
4872
f7bb41af 4873 function Is_Aliased
df3e68b1
HK
4874 (Trans_Id : Entity_Id;
4875 First_Stmt : Node_Id) return Boolean;
f7bb41af
AC
4876 -- Determine whether transient object Trans_Id has been renamed or
4877 -- aliased through 'reference in the statement list starting from
4878 -- First_Stmt.
4879
4880 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4881 -- Determine whether transient object Trans_Id is allocated on the heap
df3e68b1 4882
2f7b7467
AC
4883 function Is_Iterated_Container
4884 (Trans_Id : Entity_Id;
4885 First_Stmt : Node_Id) return Boolean;
4886 -- Determine whether transient object Trans_Id denotes a container which
4887 -- is in the process of being iterated in the statement list starting
4888 -- from First_Stmt.
4889
df3e68b1
HK
4890 ---------------------------
4891 -- Initialized_By_Access --
4892 ---------------------------
4893
4894 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4895 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4896
4897 begin
4898 return
4899 Present (Expr)
4900 and then Nkind (Expr) /= N_Reference
4901 and then Is_Access_Type (Etype (Expr));
4902 end Initialized_By_Access;
4903
4904 ------------------------------------------
4905 -- Initialized_By_Aliased_BIP_Func_Call --
4906 ------------------------------------------
4907
4908 function Initialized_By_Aliased_BIP_Func_Call
4909 (Trans_Id : Entity_Id) return Boolean
4910 is
4911 Call : Node_Id := Expression (Parent (Trans_Id));
4912
4913 begin
4914 -- Build-in-place calls usually appear in 'reference format
4915
4916 if Nkind (Call) = N_Reference then
4917 Call := Prefix (Call);
4918 end if;
4919
4920 if Is_Build_In_Place_Function_Call (Call) then
4921 declare
4922 Access_Nam : Name_Id := No_Name;
4923 Access_OK : Boolean := False;
4924 Actual : Node_Id;
4925 Alloc_Nam : Name_Id := No_Name;
4926 Alloc_OK : Boolean := False;
4927 Formal : Node_Id;
4928 Func_Id : Entity_Id;
4929 Param : Node_Id;
4930
4931 begin
4932 -- Examine all parameter associations of the function call
4933
4934 Param := First (Parameter_Associations (Call));
4935 while Present (Param) loop
4936 if Nkind (Param) = N_Parameter_Association
4937 and then Nkind (Selector_Name (Param)) = N_Identifier
4938 then
4939 Actual := Explicit_Actual_Parameter (Param);
4940 Formal := Selector_Name (Param);
4941
4942 -- Construct the names of formals BIPaccess and BIPalloc
4943 -- using the function name retrieved from an arbitrary
4944 -- formal.
4945
4946 if Access_Nam = No_Name
4947 and then Alloc_Nam = No_Name
4948 and then Present (Entity (Formal))
4949 then
4950 Func_Id := Scope (Entity (Formal));
4951
4952 Access_Nam :=
4953 New_External_Name (Chars (Func_Id),
4954 BIP_Formal_Suffix (BIP_Object_Access));
4955
4956 Alloc_Nam :=
4957 New_External_Name (Chars (Func_Id),
4958 BIP_Formal_Suffix (BIP_Alloc_Form));
4959 end if;
4960
4961 -- A match for BIPaccess => Temp has been found
4962
4963 if Chars (Formal) = Access_Nam
4964 and then Nkind (Actual) /= N_Null
4965 then
4966 Access_OK := True;
4967 end if;
4968
4969 -- A match for BIPalloc => 1 has been found
4970
4971 if Chars (Formal) = Alloc_Nam
4972 and then Nkind (Actual) = N_Integer_Literal
4973 and then Intval (Actual) = Uint_1
4974 then
4975 Alloc_OK := True;
4976 end if;
4977 end if;
4978
4979 Next (Param);
4980 end loop;
4981
0e564ab4 4982 return Access_OK and Alloc_OK;
df3e68b1
HK
4983 end;
4984 end if;
4985
4986 return False;
4987 end Initialized_By_Aliased_BIP_Func_Call;
4988
df3e68b1 4989 ----------------
f7bb41af 4990 -- Is_Aliased --
df3e68b1
HK
4991 ----------------
4992
f7bb41af 4993 function Is_Aliased
df3e68b1
HK
4994 (Trans_Id : Entity_Id;
4995 First_Stmt : Node_Id) return Boolean
4996 is
c5f5123f 4997 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
df3e68b1
HK
4998 -- Given an object renaming declaration, retrieve the entity of the
4999 -- renamed name. Return Empty if the renamed name is anything other
5000 -- than a variable or a constant.
5001
c5f5123f
AC
5002 -------------------------
5003 -- Find_Renamed_Object --
5004 -------------------------
df3e68b1 5005
c5f5123f
AC
5006 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
5007 Ren_Obj : Node_Id := Empty;
df3e68b1 5008
c5f5123f
AC
5009 function Find_Object (N : Node_Id) return Traverse_Result;
5010 -- Try to detect an object which is either a constant or a
5011 -- variable.
0180fd26 5012
c5f5123f
AC
5013 -----------------
5014 -- Find_Object --
5015 -----------------
5016
5017 function Find_Object (N : Node_Id) return Traverse_Result is
5018 begin
5019 -- Stop the search once a constant or a variable has been
5020 -- detected.
5021
5022 if Nkind (N) = N_Identifier
5023 and then Present (Entity (N))
5024 and then Ekind_In (Entity (N), E_Constant, E_Variable)
0180fd26 5025 then
c5f5123f
AC
5026 Ren_Obj := Entity (N);
5027 return Abandon;
df3e68b1 5028 end if;
df3e68b1 5029
c5f5123f
AC
5030 return OK;
5031 end Find_Object;
5032
5033 procedure Search is new Traverse_Proc (Find_Object);
5034
5035 -- Local variables
5036
5037 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
5038
5039 -- Start of processing for Find_Renamed_Object
5040
5041 begin
5042 -- Actions related to dispatching calls may appear as renamings of
5043 -- tags. Do not process this type of renaming because it does not
5044 -- use the actual value of the object.
5045
5046 if not Is_RTE (Typ, RE_Tag_Ptr) then
5047 Search (Name (Ren_Decl));
df3e68b1
HK
5048 end if;
5049
c5f5123f
AC
5050 return Ren_Obj;
5051 end Find_Renamed_Object;
df3e68b1 5052
f7bb41af 5053 -- Local variables
df3e68b1 5054
f7bb41af
AC
5055 Expr : Node_Id;
5056 Ren_Obj : Entity_Id;
5057 Stmt : Node_Id;
df3e68b1 5058
f7bb41af 5059 -- Start of processing for Is_Aliased
df3e68b1 5060
f7bb41af 5061 begin
4b17187f
AC
5062 -- A controlled transient object is not considered aliased when it
5063 -- appears inside an expression_with_actions node even when there are
5064 -- explicit aliases of it:
5065
5066 -- do
5067 -- Trans_Id : Ctrl_Typ ...; -- controlled transient object
5068 -- Alias : ... := Trans_Id; -- object is aliased
5069 -- Val : constant Boolean :=
5070 -- ... Alias ...; -- aliasing ends
5071 -- <finalize Trans_Id> -- object safe to finalize
5072 -- in Val end;
5073
5074 -- Expansion ensures that all aliases are encapsulated in the actions
5075 -- list and do not leak to the expression by forcing the evaluation
5076 -- of the expression.
5077
5078 if Nkind (Rel_Node) = N_Expression_With_Actions then
5079 return False;
f7bb41af 5080
4b17187f
AC
5081 -- Otherwise examine the statements after the controlled transient
5082 -- object and look for various forms of aliasing.
df3e68b1 5083
4b17187f
AC
5084 else
5085 Stmt := First_Stmt;
5086 while Present (Stmt) loop
5087 if Nkind (Stmt) = N_Object_Declaration then
5088 Expr := Expression (Stmt);
5089
5090 -- Aliasing of the form:
5091 -- Obj : ... := Trans_Id'reference;
5092
5093 if Present (Expr)
5094 and then Nkind (Expr) = N_Reference
5095 and then Nkind (Prefix (Expr)) = N_Identifier
5096 and then Entity (Prefix (Expr)) = Trans_Id
5097 then
5098 return True;
5099 end if;
5100
5101 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
5102 Ren_Obj := Find_Renamed_Object (Stmt);
5103
5104 -- Aliasing of the form:
5105 -- Obj : ... renames ... Trans_Id ...;
5106
5107 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
5108 return True;
5109 end if;
df3e68b1 5110 end if;
df3e68b1 5111
4b17187f
AC
5112 Next (Stmt);
5113 end loop;
df3e68b1 5114
4b17187f
AC
5115 return False;
5116 end if;
f7bb41af
AC
5117 end Is_Aliased;
5118
5119 ------------------
5120 -- Is_Allocated --
5121 ------------------
5122
5123 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
5124 Expr : constant Node_Id := Expression (Parent (Trans_Id));
f7bb41af
AC
5125 begin
5126 return
5127 Is_Access_Type (Etype (Trans_Id))
5128 and then Present (Expr)
5129 and then Nkind (Expr) = N_Allocator;
5130 end Is_Allocated;
df3e68b1 5131
2f7b7467
AC
5132 ---------------------------
5133 -- Is_Iterated_Container --
5134 ---------------------------
5135
5136 function Is_Iterated_Container
5137 (Trans_Id : Entity_Id;
5138 First_Stmt : Node_Id) return Boolean
5139 is
5140 Aspect : Node_Id;
5141 Call : Node_Id;
5142 Iter : Entity_Id;
5143 Param : Node_Id;
5144 Stmt : Node_Id;
5145 Typ : Entity_Id;
5146
5147 begin
5148 -- It is not possible to iterate over containers in non-Ada 2012 code
5149
5150 if Ada_Version < Ada_2012 then
5151 return False;
5152 end if;
5153
5154 Typ := Etype (Trans_Id);
5155
5156 -- Handle access type created for secondary stack use
5157
5158 if Is_Access_Type (Typ) then
5159 Typ := Designated_Type (Typ);
5160 end if;
5161
46de64ca
AC
5162 -- Look for aspect Default_Iterator. It may be part of a type
5163 -- declaration for a container, or inherited from a base type
5164 -- or parent type.
2f7b7467 5165
46de64ca 5166 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
2f7b7467 5167
46de64ca
AC
5168 if Present (Aspect) then
5169 Iter := Entity (Aspect);
2f7b7467 5170
46de64ca
AC
5171 -- Examine the statements following the container object and
5172 -- look for a call to the default iterate routine where the
5173 -- first parameter is the transient. Such a call appears as:
2f7b7467 5174
46de64ca
AC
5175 -- It : Access_To_CW_Iterator :=
5176 -- Iterate (Tran_Id.all, ...)'reference;
2f7b7467 5177
46de64ca
AC
5178 Stmt := First_Stmt;
5179 while Present (Stmt) loop
2f7b7467 5180
46de64ca
AC
5181 -- Detect an object declaration which is initialized by a
5182 -- secondary stack function call.
2f7b7467 5183
46de64ca
AC
5184 if Nkind (Stmt) = N_Object_Declaration
5185 and then Present (Expression (Stmt))
5186 and then Nkind (Expression (Stmt)) = N_Reference
c5c780e6 5187 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
46de64ca
AC
5188 then
5189 Call := Prefix (Expression (Stmt));
2f7b7467 5190
46de64ca
AC
5191 -- The call must invoke the default iterate routine of
5192 -- the container and the transient object must appear as
5193 -- the first actual parameter. Skip any calls whose names
5194 -- are not entities.
2f7b7467 5195
46de64ca
AC
5196 if Is_Entity_Name (Name (Call))
5197 and then Entity (Name (Call)) = Iter
5198 and then Present (Parameter_Associations (Call))
5199 then
5200 Param := First (Parameter_Associations (Call));
2f7b7467 5201
46de64ca
AC
5202 if Nkind (Param) = N_Explicit_Dereference
5203 and then Entity (Prefix (Param)) = Trans_Id
5204 then
5205 return True;
2f7b7467
AC
5206 end if;
5207 end if;
46de64ca 5208 end if;
2f7b7467 5209
46de64ca
AC
5210 Next (Stmt);
5211 end loop;
2f7b7467
AC
5212 end if;
5213
5214 return False;
5215 end Is_Iterated_Container;
5216
4b17187f
AC
5217 -- Local variables
5218
5219 Desig : Entity_Id := Obj_Typ;
5220
df3e68b1
HK
5221 -- Start of processing for Is_Finalizable_Transient
5222
5223 begin
5224 -- Handle access types
5225
5226 if Is_Access_Type (Desig) then
5227 Desig := Available_View (Designated_Type (Desig));
5228 end if;
5229
5230 return
5231 Ekind_In (Obj_Id, E_Constant, E_Variable)
5232 and then Needs_Finalization (Desig)
5233 and then Requires_Transient_Scope (Desig)
5234 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
5235
2c17ca0a
AC
5236 -- Do not consider renamed or 'reference-d transient objects because
5237 -- the act of renaming extends the object's lifetime.
f7bb41af
AC
5238
5239 and then not Is_Aliased (Obj_Id, Decl)
5240
2c17ca0a
AC
5241 -- Do not consider transient objects allocated on the heap since
5242 -- they are attached to a finalization master.
df3e68b1
HK
5243
5244 and then not Is_Allocated (Obj_Id)
5245
2c17ca0a 5246 -- If the transient object is a pointer, check that it is not
7c4d86c9 5247 -- initialized by a function that returns a pointer or acts as a
2c17ca0a 5248 -- renaming of another pointer.
df3e68b1
HK
5249
5250 and then
5251 (not Is_Access_Type (Obj_Typ)
5252 or else not Initialized_By_Access (Obj_Id))
5253
2c17ca0a
AC
5254 -- Do not consider transient objects which act as indirect aliases
5255 -- of build-in-place function results.
df3e68b1 5256
2d395256
AC
5257 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
5258
2c17ca0a 5259 -- Do not consider conversions of tags to class-wide types
2d395256 5260
aab08130 5261 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
2f7b7467 5262
4b17187f
AC
5263 -- Do not consider iterators because those are treated as normal
5264 -- controlled objects and are processed by the usual finalization
5265 -- machinery. This avoids the double finalization of an iterator.
5266
5267 and then not Is_Iterator (Desig)
5268
2f7b7467
AC
5269 -- Do not consider containers in the context of iterator loops. Such
5270 -- transient objects must exist for as long as the loop is around,
5271 -- otherwise any operation carried out by the iterator will fail.
5272
5273 and then not Is_Iterated_Container (Obj_Id, Decl);
df3e68b1
HK
5274 end Is_Finalizable_Transient;
5275
6fb4cdde
AC
5276 ---------------------------------
5277 -- Is_Fully_Repped_Tagged_Type --
5278 ---------------------------------
5279
5280 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
5281 U : constant Entity_Id := Underlying_Type (T);
5282 Comp : Entity_Id;
5283
5284 begin
5285 if No (U) or else not Is_Tagged_Type (U) then
5286 return False;
5287 elsif Has_Discriminants (U) then
5288 return False;
5289 elsif not Has_Specified_Layout (U) then
5290 return False;
5291 end if;
5292
5293 -- Here we have a tagged type, see if it has any unlayed out fields
5294 -- other than a possible tag and parent fields. If so, we return False.
5295
5296 Comp := First_Component (U);
5297 while Present (Comp) loop
5298 if not Is_Tag (Comp)
5299 and then Chars (Comp) /= Name_uParent
5300 and then No (Component_Clause (Comp))
5301 then
5302 return False;
5303 else
5304 Next_Component (Comp);
5305 end if;
5306 end loop;
5307
5308 -- All components are layed out
5309
5310 return True;
5311 end Is_Fully_Repped_Tagged_Type;
5312
86cde7b1
RD
5313 ----------------------------------
5314 -- Is_Library_Level_Tagged_Type --
5315 ----------------------------------
5316
5317 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
5318 begin
0e564ab4 5319 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
86cde7b1
RD
5320 end Is_Library_Level_Tagged_Type;
5321
df3e68b1
HK
5322 --------------------------
5323 -- Is_Non_BIP_Func_Call --
5324 --------------------------
5325
5326 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
5327 begin
5328 -- The expected call is of the format
5329 --
5330 -- Func_Call'reference
5331
5332 return
5333 Nkind (Expr) = N_Reference
5334 and then Nkind (Prefix (Expr)) = N_Function_Call
5335 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
5336 end Is_Non_BIP_Func_Call;
5337
7b966a95
AC
5338 ------------------------------------
5339 -- Is_Object_Access_BIP_Func_Call --
5340 ------------------------------------
5341
5342 function Is_Object_Access_BIP_Func_Call
5343 (Expr : Node_Id;
5344 Obj_Id : Entity_Id) return Boolean
5345 is
5346 Access_Nam : Name_Id := No_Name;
5347 Actual : Node_Id;
5348 Call : Node_Id;
5349 Formal : Node_Id;
5350 Param : Node_Id;
5351
5352 begin
5353 -- Build-in-place calls usually appear in 'reference format. Note that
5354 -- the accessibility check machinery may add an extra 'reference due to
5355 -- side effect removal.
5356
5357 Call := Expr;
5358 while Nkind (Call) = N_Reference loop
5359 Call := Prefix (Call);
5360 end loop;
5361
5362 if Nkind_In (Call, N_Qualified_Expression,
5363 N_Unchecked_Type_Conversion)
5364 then
5365 Call := Expression (Call);
5366 end if;
5367
5368 if Is_Build_In_Place_Function_Call (Call) then
5369
5370 -- Examine all parameter associations of the function call
5371
5372 Param := First (Parameter_Associations (Call));
5373 while Present (Param) loop
5374 if Nkind (Param) = N_Parameter_Association
5375 and then Nkind (Selector_Name (Param)) = N_Identifier
5376 then
5377 Formal := Selector_Name (Param);
5378 Actual := Explicit_Actual_Parameter (Param);
5379
5380 -- Construct the name of formal BIPaccess. It is much easier to
5381 -- extract the name of the function using an arbitrary formal's
5382 -- scope rather than the Name field of Call.
5383
5384 if Access_Nam = No_Name and then Present (Entity (Formal)) then
5385 Access_Nam :=
5386 New_External_Name
5387 (Chars (Scope (Entity (Formal))),
5388 BIP_Formal_Suffix (BIP_Object_Access));
5389 end if;
5390
5391 -- A match for BIPaccess => Obj_Id'Unrestricted_Access has been
5392 -- found.
5393
5394 if Chars (Formal) = Access_Nam
5395 and then Nkind (Actual) = N_Attribute_Reference
5396 and then Attribute_Name (Actual) = Name_Unrestricted_Access
5397 and then Nkind (Prefix (Actual)) = N_Identifier
5398 and then Entity (Prefix (Actual)) = Obj_Id
5399 then
5400 return True;
5401 end if;
5402 end if;
5403
5404 Next (Param);
5405 end loop;
5406 end if;
5407
5408 return False;
5409 end Is_Object_Access_BIP_Func_Call;
5410
fbf5a39b
AC
5411 ----------------------------------
5412 -- Is_Possibly_Unaligned_Object --
5413 ----------------------------------
5414
f44fe430
RD
5415 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
5416 T : constant Entity_Id := Etype (N);
5417
fbf5a39b 5418 begin
f44fe430 5419 -- If renamed object, apply test to underlying object
fbf5a39b 5420
f44fe430
RD
5421 if Is_Entity_Name (N)
5422 and then Is_Object (Entity (N))
5423 and then Present (Renamed_Object (Entity (N)))
5424 then
5425 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
fbf5a39b
AC
5426 end if;
5427
273adcdf
AC
5428 -- Tagged and controlled types and aliased types are always aligned, as
5429 -- are concurrent types.
fbf5a39b 5430
f44fe430
RD
5431 if Is_Aliased (T)
5432 or else Has_Controlled_Component (T)
5433 or else Is_Concurrent_Type (T)
5434 or else Is_Tagged_Type (T)
5435 or else Is_Controlled (T)
fbf5a39b 5436 then
f44fe430 5437 return False;
fbf5a39b
AC
5438 end if;
5439
5440 -- If this is an element of a packed array, may be unaligned
5441
f44fe430 5442 if Is_Ref_To_Bit_Packed_Array (N) then
fbf5a39b
AC
5443 return True;
5444 end if;
5445
1adaea16 5446 -- Case of indexed component reference: test whether prefix is unaligned
fbf5a39b 5447
1adaea16
AC
5448 if Nkind (N) = N_Indexed_Component then
5449 return Is_Possibly_Unaligned_Object (Prefix (N));
5450
5451 -- Case of selected component reference
5452
5453 elsif Nkind (N) = N_Selected_Component then
f44fe430
RD
5454 declare
5455 P : constant Node_Id := Prefix (N);
5456 C : constant Entity_Id := Entity (Selector_Name (N));
5457 M : Nat;
5458 S : Nat;
fbf5a39b 5459
f44fe430
RD
5460 begin
5461 -- If component reference is for an array with non-static bounds,
273adcdf 5462 -- then it is always aligned: we can only process unaligned arrays
2c17ca0a 5463 -- with static bounds (more precisely compile time known bounds).
fbf5a39b 5464
f44fe430
RD
5465 if Is_Array_Type (T)
5466 and then not Compile_Time_Known_Bounds (T)
5467 then
5468 return False;
5469 end if;
fbf5a39b 5470
f44fe430 5471 -- If component is aliased, it is definitely properly aligned
fbf5a39b 5472
f44fe430
RD
5473 if Is_Aliased (C) then
5474 return False;
5475 end if;
5476
5477 -- If component is for a type implemented as a scalar, and the
5478 -- record is packed, and the component is other than the first
5479 -- component of the record, then the component may be unaligned.
5480
5481 if Is_Packed (Etype (P))
8adcacef
RD
5482 and then Represented_As_Scalar (Etype (C))
5483 and then First_Entity (Scope (C)) /= C
f44fe430
RD
5484 then
5485 return True;
5486 end if;
5487
5488 -- Compute maximum possible alignment for T
5489
5490 -- If alignment is known, then that settles things
5491
5492 if Known_Alignment (T) then
5493 M := UI_To_Int (Alignment (T));
5494
5495 -- If alignment is not known, tentatively set max alignment
5496
5497 else
5498 M := Ttypes.Maximum_Alignment;
5499
5500 -- We can reduce this if the Esize is known since the default
5501 -- alignment will never be more than the smallest power of 2
5502 -- that does not exceed this Esize value.
5503
5504 if Known_Esize (T) then
5505 S := UI_To_Int (Esize (T));
5506
5507 while (M / 2) >= S loop
5508 M := M / 2;
5509 end loop;
5510 end if;
5511 end if;
5512
87dc09cb
AC
5513 -- The following code is historical, it used to be present but it
5514 -- is too cautious, because the front-end does not know the proper
5515 -- default alignments for the target. Also, if the alignment is
a90bd866 5516 -- not known, the front end can't know in any case. If a copy is
87dc09cb
AC
5517 -- needed, the back-end will take care of it. This whole section
5518 -- including this comment can be removed later ???
5519
f44fe430
RD
5520 -- If the component reference is for a record that has a specified
5521 -- alignment, and we either know it is too small, or cannot tell,
87dc09cb
AC
5522 -- then the component may be unaligned.
5523
2c17ca0a
AC
5524 -- What is the following commented out code ???
5525
87dc09cb
AC
5526 -- if Known_Alignment (Etype (P))
5527 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
5528 -- and then M > Alignment (Etype (P))
5529 -- then
5530 -- return True;
5531 -- end if;
f44fe430
RD
5532
5533 -- Case of component clause present which may specify an
5534 -- unaligned position.
5535
5536 if Present (Component_Clause (C)) then
5537
5538 -- Otherwise we can do a test to make sure that the actual
5539 -- start position in the record, and the length, are both
5540 -- consistent with the required alignment. If not, we know
5541 -- that we are unaligned.
5542
5543 declare
5544 Align_In_Bits : constant Nat := M * System_Storage_Unit;
5545 begin
5546 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
5547 or else Esize (C) mod Align_In_Bits /= 0
5548 then
5549 return True;
5550 end if;
5551 end;
5552 end if;
5553
5554 -- Otherwise, for a component reference, test prefix
5555
5556 return Is_Possibly_Unaligned_Object (P);
5557 end;
fbf5a39b
AC
5558
5559 -- If not a component reference, must be aligned
5560
5561 else
5562 return False;
5563 end if;
5564 end Is_Possibly_Unaligned_Object;
5565
5566 ---------------------------------
5567 -- Is_Possibly_Unaligned_Slice --
5568 ---------------------------------
5569
f44fe430 5570 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
fbf5a39b 5571 begin
0712790c 5572 -- Go to renamed object
246d2ceb 5573
f44fe430
RD
5574 if Is_Entity_Name (N)
5575 and then Is_Object (Entity (N))
5576 and then Present (Renamed_Object (Entity (N)))
fbf5a39b 5577 then
f44fe430 5578 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
fbf5a39b
AC
5579 end if;
5580
246d2ceb 5581 -- The reference must be a slice
fbf5a39b 5582
f44fe430 5583 if Nkind (N) /= N_Slice then
246d2ceb 5584 return False;
fbf5a39b
AC
5585 end if;
5586
246d2ceb
AC
5587 -- We only need to worry if the target has strict alignment
5588
5589 if not Target_Strict_Alignment then
fbf5a39b
AC
5590 return False;
5591 end if;
5592
5593 -- If it is a slice, then look at the array type being sliced
5594
5595 declare
f44fe430 5596 Sarr : constant Node_Id := Prefix (N);
246d2ceb
AC
5597 -- Prefix of the slice, i.e. the array being sliced
5598
f44fe430 5599 Styp : constant Entity_Id := Etype (Prefix (N));
246d2ceb
AC
5600 -- Type of the array being sliced
5601
5602 Pref : Node_Id;
5603 Ptyp : Entity_Id;
fbf5a39b
AC
5604
5605 begin
246d2ceb
AC
5606 -- The problems arise if the array object that is being sliced
5607 -- is a component of a record or array, and we cannot guarantee
5608 -- the alignment of the array within its containing object.
fbf5a39b 5609
246d2ceb
AC
5610 -- To investigate this, we look at successive prefixes to see
5611 -- if we have a worrisome indexed or selected component.
fbf5a39b 5612
246d2ceb
AC
5613 Pref := Sarr;
5614 loop
5615 -- Case of array is part of an indexed component reference
fbf5a39b 5616
246d2ceb
AC
5617 if Nkind (Pref) = N_Indexed_Component then
5618 Ptyp := Etype (Prefix (Pref));
5619
273adcdf
AC
5620 -- The only problematic case is when the array is packed, in
5621 -- which case we really know nothing about the alignment of
5622 -- individual components.
246d2ceb
AC
5623
5624 if Is_Bit_Packed_Array (Ptyp) then
5625 return True;
5626 end if;
5627
5628 -- Case of array is part of a selected component reference
5629
5630 elsif Nkind (Pref) = N_Selected_Component then
5631 Ptyp := Etype (Prefix (Pref));
5632
5633 -- We are definitely in trouble if the record in question
5634 -- has an alignment, and either we know this alignment is
273adcdf
AC
5635 -- inconsistent with the alignment of the slice, or we don't
5636 -- know what the alignment of the slice should be.
246d2ceb
AC
5637
5638 if Known_Alignment (Ptyp)
5639 and then (Unknown_Alignment (Styp)
0e564ab4 5640 or else Alignment (Styp) > Alignment (Ptyp))
246d2ceb
AC
5641 then
5642 return True;
5643 end if;
5644
5645 -- We are in potential trouble if the record type is packed.
5646 -- We could special case when we know that the array is the
5647 -- first component, but that's not such a simple case ???
5648
5649 if Is_Packed (Ptyp) then
5650 return True;
5651 end if;
5652
5653 -- We are in trouble if there is a component clause, and
5654 -- either we do not know the alignment of the slice, or
5655 -- the alignment of the slice is inconsistent with the
5656 -- bit position specified by the component clause.
5657
5658 declare
5659 Field : constant Entity_Id := Entity (Selector_Name (Pref));
5660 begin
5661 if Present (Component_Clause (Field))
5662 and then
5663 (Unknown_Alignment (Styp)
5664 or else
5665 (Component_Bit_Offset (Field) mod
5666 (System_Storage_Unit * Alignment (Styp))) /= 0)
5667 then
5668 return True;
5669 end if;
5670 end;
5671
273adcdf
AC
5672 -- For cases other than selected or indexed components we know we
5673 -- are OK, since no issues arise over alignment.
246d2ceb
AC
5674
5675 else
5676 return False;
5677 end if;
5678
5679 -- We processed an indexed component or selected component
5680 -- reference that looked safe, so keep checking prefixes.
5681
5682 Pref := Prefix (Pref);
5683 end loop;
fbf5a39b
AC
5684 end;
5685 end Is_Possibly_Unaligned_Slice;
5686
df3e68b1
HK
5687 -------------------------------
5688 -- Is_Related_To_Func_Return --
5689 -------------------------------
5690
5691 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
5692 Expr : constant Node_Id := Related_Expression (Id);
df3e68b1
HK
5693 begin
5694 return
5695 Present (Expr)
5696 and then Nkind (Expr) = N_Explicit_Dereference
5697 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
5698 end Is_Related_To_Func_Return;
5699
70482933
RK
5700 --------------------------------
5701 -- Is_Ref_To_Bit_Packed_Array --
5702 --------------------------------
5703
f44fe430 5704 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
70482933
RK
5705 Result : Boolean;
5706 Expr : Node_Id;
5707
5708 begin
f44fe430
RD
5709 if Is_Entity_Name (N)
5710 and then Is_Object (Entity (N))
5711 and then Present (Renamed_Object (Entity (N)))
fbf5a39b 5712 then
f44fe430 5713 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
fbf5a39b
AC
5714 end if;
5715
0e564ab4 5716 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
f44fe430 5717 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
70482933
RK
5718 Result := True;
5719 else
f44fe430 5720 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
70482933
RK
5721 end if;
5722
f44fe430
RD
5723 if Result and then Nkind (N) = N_Indexed_Component then
5724 Expr := First (Expressions (N));
70482933
RK
5725 while Present (Expr) loop
5726 Force_Evaluation (Expr);
5727 Next (Expr);
5728 end loop;
5729 end if;
5730
5731 return Result;
5732
5733 else
5734 return False;
5735 end if;
5736 end Is_Ref_To_Bit_Packed_Array;
5737
5738 --------------------------------
fbf5a39b 5739 -- Is_Ref_To_Bit_Packed_Slice --
70482933
RK
5740 --------------------------------
5741
f44fe430 5742 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
70482933 5743 begin
ea985d95
RD
5744 if Nkind (N) = N_Type_Conversion then
5745 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
5746
5747 elsif Is_Entity_Name (N)
f44fe430
RD
5748 and then Is_Object (Entity (N))
5749 and then Present (Renamed_Object (Entity (N)))
fbf5a39b 5750 then
f44fe430 5751 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
fbf5a39b 5752
ea985d95 5753 elsif Nkind (N) = N_Slice
f44fe430 5754 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
70482933
RK
5755 then
5756 return True;
5757
0e564ab4 5758 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
f44fe430 5759 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
70482933
RK
5760
5761 else
5762 return False;
5763 end if;
5764 end Is_Ref_To_Bit_Packed_Slice;
5765
5766 -----------------------
5767 -- Is_Renamed_Object --
5768 -----------------------
5769
5770 function Is_Renamed_Object (N : Node_Id) return Boolean is
5771 Pnod : constant Node_Id := Parent (N);
5772 Kind : constant Node_Kind := Nkind (Pnod);
70482933
RK
5773 begin
5774 if Kind = N_Object_Renaming_Declaration then
5775 return True;
6fb4cdde 5776 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
70482933 5777 return Is_Renamed_Object (Pnod);
70482933
RK
5778 else
5779 return False;
5780 end if;
5781 end Is_Renamed_Object;
5782
cdc96e3e
AC
5783 --------------------------------------
5784 -- Is_Secondary_Stack_BIP_Func_Call --
5785 --------------------------------------
5786
5787 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
7b966a95
AC
5788 Alloc_Nam : Name_Id := No_Name;
5789 Actual : Node_Id;
5790 Call : Node_Id := Expr;
5791 Formal : Node_Id;
5792 Param : Node_Id;
cdc96e3e
AC
5793
5794 begin
e0adfeb4
AC
5795 -- Build-in-place calls usually appear in 'reference format. Note that
5796 -- the accessibility check machinery may add an extra 'reference due to
5797 -- side effect removal.
cdc96e3e 5798
e0adfeb4 5799 while Nkind (Call) = N_Reference loop
cdc96e3e 5800 Call := Prefix (Call);
e0adfeb4 5801 end loop;
cdc96e3e
AC
5802
5803 if Nkind_In (Call, N_Qualified_Expression,
5804 N_Unchecked_Type_Conversion)
5805 then
5806 Call := Expression (Call);
5807 end if;
5808
5809 if Is_Build_In_Place_Function_Call (Call) then
cdc96e3e 5810
7b966a95 5811 -- Examine all parameter associations of the function call
cdc96e3e 5812
7b966a95
AC
5813 Param := First (Parameter_Associations (Call));
5814 while Present (Param) loop
5815 if Nkind (Param) = N_Parameter_Association
5816 and then Nkind (Selector_Name (Param)) = N_Identifier
5817 then
5818 Formal := Selector_Name (Param);
5819 Actual := Explicit_Actual_Parameter (Param);
5820
5821 -- Construct the name of formal BIPalloc. It is much easier to
5822 -- extract the name of the function using an arbitrary formal's
5823 -- scope rather than the Name field of Call.
5824
5825 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
5826 Alloc_Nam :=
5827 New_External_Name
5828 (Chars (Scope (Entity (Formal))),
5829 BIP_Formal_Suffix (BIP_Alloc_Form));
5830 end if;
cdc96e3e 5831
7b966a95 5832 -- A match for BIPalloc => 2 has been found
cdc96e3e 5833
7b966a95
AC
5834 if Chars (Formal) = Alloc_Nam
5835 and then Nkind (Actual) = N_Integer_Literal
5836 and then Intval (Actual) = Uint_2
5837 then
5838 return True;
cdc96e3e 5839 end if;
7b966a95 5840 end if;
cdc96e3e 5841
7b966a95
AC
5842 Next (Param);
5843 end loop;
cdc96e3e
AC
5844 end if;
5845
5846 return False;
5847 end Is_Secondary_Stack_BIP_Func_Call;
5848
aab08130
AC
5849 -------------------------------------
5850 -- Is_Tag_To_Class_Wide_Conversion --
5851 -------------------------------------
2d395256 5852
aab08130
AC
5853 function Is_Tag_To_Class_Wide_Conversion
5854 (Obj_Id : Entity_Id) return Boolean
5855 is
2d395256
AC
5856 Expr : constant Node_Id := Expression (Parent (Obj_Id));
5857
5858 begin
5859 return
5860 Is_Class_Wide_Type (Etype (Obj_Id))
5861 and then Present (Expr)
5862 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5863 and then Etype (Expression (Expr)) = RTE (RE_Tag);
aab08130 5864 end Is_Tag_To_Class_Wide_Conversion;
2d395256 5865
70482933
RK
5866 ----------------------------
5867 -- Is_Untagged_Derivation --
5868 ----------------------------
5869
5870 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5871 begin
5872 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5873 or else
7b966a95
AC
5874 (Is_Private_Type (T) and then Present (Full_View (T))
5875 and then not Is_Tagged_Type (Full_View (T))
5876 and then Is_Derived_Type (Full_View (T))
5877 and then Etype (Full_View (T)) /= T);
70482933
RK
5878 end Is_Untagged_Derivation;
5879
65df5b71
HK
5880 ---------------------------
5881 -- Is_Volatile_Reference --
5882 ---------------------------
5883
5884 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5885 begin
bb012790
AC
5886 -- Only source references are to be treated as volatile, internally
5887 -- generated stuff cannot have volatile external effects.
5888
5889 if not Comes_From_Source (N) then
5890 return False;
5891
5892 -- Never true for reference to a type
5893
5894 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
5895 return False;
5896
57d08392
AC
5897 -- Never true for a compile time known constant
5898
5899 elsif Compile_Time_Known_Value (N) then
5900 return False;
5901
bb012790
AC
5902 -- True if object reference with volatile type
5903
5904 elsif Is_Volatile_Object (N) then
65df5b71
HK
5905 return True;
5906
bb012790
AC
5907 -- True if reference to volatile entity
5908
65df5b71
HK
5909 elsif Is_Entity_Name (N) then
5910 return Treat_As_Volatile (Entity (N));
5911
bb012790
AC
5912 -- True for slice of volatile array
5913
65df5b71
HK
5914 elsif Nkind (N) = N_Slice then
5915 return Is_Volatile_Reference (Prefix (N));
5916
bb012790
AC
5917 -- True if volatile component
5918
65df5b71
HK
5919 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5920 if (Is_Entity_Name (Prefix (N))
0e564ab4 5921 and then Has_Volatile_Components (Entity (Prefix (N))))
65df5b71 5922 or else (Present (Etype (Prefix (N)))
0e564ab4 5923 and then Has_Volatile_Components (Etype (Prefix (N))))
65df5b71
HK
5924 then
5925 return True;
5926 else
5927 return Is_Volatile_Reference (Prefix (N));
5928 end if;
5929
bb012790
AC
5930 -- Otherwise false
5931
65df5b71
HK
5932 else
5933 return False;
5934 end if;
5935 end Is_Volatile_Reference;
5936
70482933
RK
5937 --------------------
5938 -- Kill_Dead_Code --
5939 --------------------
5940
05350ac6 5941 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3acdda2d
AC
5942 W : Boolean := Warn;
5943 -- Set False if warnings suppressed
5944
70482933
RK
5945 begin
5946 if Present (N) then
70482933
RK
5947 Remove_Warning_Messages (N);
5948
3acdda2d
AC
5949 -- Generate warning if appropriate
5950
5951 if W then
5952
5953 -- We suppress the warning if this code is under control of an
5954 -- if statement, whose condition is a simple identifier, and
5955 -- either we are in an instance, or warnings off is set for this
5956 -- identifier. The reason for killing it in the instance case is
5957 -- that it is common and reasonable for code to be deleted in
5958 -- instances for various reasons.
5959
02bb0765
AC
5960 -- Could we use Is_Statically_Unevaluated here???
5961
3acdda2d
AC
5962 if Nkind (Parent (N)) = N_If_Statement then
5963 declare
5964 C : constant Node_Id := Condition (Parent (N));
5965 begin
5966 if Nkind (C) = N_Identifier
5967 and then
5968 (In_Instance
5969 or else (Present (Entity (C))
0e564ab4 5970 and then Has_Warnings_Off (Entity (C))))
3acdda2d
AC
5971 then
5972 W := False;
5973 end if;
5974 end;
5975 end if;
5976
5977 -- Generate warning if not suppressed
5978
5979 if W then
ed2233dc 5980 Error_Msg_F
685bc70f 5981 ("?t?this code can never be executed and has been deleted!",
324ac540 5982 N);
3acdda2d 5983 end if;
05350ac6
BD
5984 end if;
5985
07fc65c4 5986 -- Recurse into block statements and bodies to process declarations
3acdda2d 5987 -- and statements.
70482933 5988
07fc65c4
GB
5989 if Nkind (N) = N_Block_Statement
5990 or else Nkind (N) = N_Subprogram_Body
5991 or else Nkind (N) = N_Package_Body
5992 then
569f538b
TQ
5993 Kill_Dead_Code (Declarations (N), False);
5994 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
70482933 5995
07fc65c4
GB
5996 if Nkind (N) = N_Subprogram_Body then
5997 Set_Is_Eliminated (Defining_Entity (N));
5998 end if;
5999
f44fe430
RD
6000 elsif Nkind (N) = N_Package_Declaration then
6001 Kill_Dead_Code (Visible_Declarations (Specification (N)));
6002 Kill_Dead_Code (Private_Declarations (Specification (N)));
6003
569f538b 6004 -- ??? After this point, Delete_Tree has been called on all
273adcdf
AC
6005 -- declarations in Specification (N), so references to entities
6006 -- therein look suspicious.
569f538b 6007
f44fe430
RD
6008 declare
6009 E : Entity_Id := First_Entity (Defining_Entity (N));
02bb0765 6010
f44fe430
RD
6011 begin
6012 while Present (E) loop
6013 if Ekind (E) = E_Operator then
6014 Set_Is_Eliminated (E);
6015 end if;
6016
6017 Next_Entity (E);
6018 end loop;
6019 end;
6020
273adcdf
AC
6021 -- Recurse into composite statement to kill individual statements in
6022 -- particular instantiations.
70482933
RK
6023
6024 elsif Nkind (N) = N_If_Statement then
6025 Kill_Dead_Code (Then_Statements (N));
02bb0765 6026 Kill_Dead_Code (Elsif_Parts (N));
70482933
RK
6027 Kill_Dead_Code (Else_Statements (N));
6028
6029 elsif Nkind (N) = N_Loop_Statement then
6030 Kill_Dead_Code (Statements (N));
6031
6032 elsif Nkind (N) = N_Case_Statement then
6033 declare
bebbff91 6034 Alt : Node_Id;
70482933 6035 begin
bebbff91 6036 Alt := First (Alternatives (N));
70482933
RK
6037 while Present (Alt) loop
6038 Kill_Dead_Code (Statements (Alt));
6039 Next (Alt);
6040 end loop;
6041 end;
6042
fbf5a39b
AC
6043 elsif Nkind (N) = N_Case_Statement_Alternative then
6044 Kill_Dead_Code (Statements (N));
6045
70482933
RK
6046 -- Deal with dead instances caused by deleting instantiations
6047
6048 elsif Nkind (N) in N_Generic_Instantiation then
6049 Remove_Dead_Instance (N);
6050 end if;
70482933
RK
6051 end if;
6052 end Kill_Dead_Code;
6053
6054 -- Case where argument is a list of nodes to be killed
6055
05350ac6 6056 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
70482933 6057 N : Node_Id;
05350ac6 6058 W : Boolean;
02bb0765 6059
70482933 6060 begin
05350ac6 6061 W := Warn;
02bb0765 6062
70482933 6063 if Is_Non_Empty_List (L) then
ac4d6407
RD
6064 N := First (L);
6065 while Present (N) loop
05350ac6
BD
6066 Kill_Dead_Code (N, W);
6067 W := False;
ac4d6407 6068 Next (N);
70482933
RK
6069 end loop;
6070 end if;
6071 end Kill_Dead_Code;
6072
6073 ------------------------
6074 -- Known_Non_Negative --
6075 ------------------------
6076
6077 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
6078 begin
0e564ab4 6079 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
70482933
RK
6080 return True;
6081
6082 else
6083 declare
6084 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
70482933
RK
6085 begin
6086 return
6087 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
6088 end;
6089 end if;
6090 end Known_Non_Negative;
6091
fbf5a39b
AC
6092 --------------------
6093 -- Known_Non_Null --
6094 --------------------
07fc65c4 6095
fbf5a39b
AC
6096 function Known_Non_Null (N : Node_Id) return Boolean is
6097 begin
59e54267 6098 -- Checks for case where N is an entity reference
07fc65c4 6099
59e54267
ES
6100 if Is_Entity_Name (N) and then Present (Entity (N)) then
6101 declare
6102 E : constant Entity_Id := Entity (N);
6103 Op : Node_Kind;
6104 Val : Node_Id;
07fc65c4 6105
59e54267
ES
6106 begin
6107 -- First check if we are in decisive conditional
07fc65c4 6108
59e54267 6109 Get_Current_Value_Condition (N, Op, Val);
07fc65c4 6110
86cde7b1 6111 if Known_Null (Val) then
59e54267
ES
6112 if Op = N_Op_Eq then
6113 return False;
6114 elsif Op = N_Op_Ne then
6115 return True;
6116 end if;
6117 end if;
07fc65c4 6118
59e54267 6119 -- If OK to do replacement, test Is_Known_Non_Null flag
07fc65c4 6120
59e54267
ES
6121 if OK_To_Do_Constant_Replacement (E) then
6122 return Is_Known_Non_Null (E);
6123
6124 -- Otherwise if not safe to do replacement, then say so
6125
6126 else
6127 return False;
6128 end if;
6129 end;
07fc65c4 6130
fbf5a39b 6131 -- True if access attribute
07fc65c4 6132
fbf5a39b 6133 elsif Nkind (N) = N_Attribute_Reference
b69cd36a
AC
6134 and then Nam_In (Attribute_Name (N), Name_Access,
6135 Name_Unchecked_Access,
6136 Name_Unrestricted_Access)
fbf5a39b
AC
6137 then
6138 return True;
07fc65c4 6139
fbf5a39b 6140 -- True if allocator
07fc65c4 6141
fbf5a39b
AC
6142 elsif Nkind (N) = N_Allocator then
6143 return True;
07fc65c4 6144
fbf5a39b 6145 -- For a conversion, true if expression is known non-null
07fc65c4 6146
fbf5a39b
AC
6147 elsif Nkind (N) = N_Type_Conversion then
6148 return Known_Non_Null (Expression (N));
07fc65c4 6149
59e54267
ES
6150 -- Above are all cases where the value could be determined to be
6151 -- non-null. In all other cases, we don't know, so return False.
07fc65c4 6152
59e54267
ES
6153 else
6154 return False;
6155 end if;
6156 end Known_Non_Null;
6157
6158 ----------------
6159 -- Known_Null --
6160 ----------------
6161
6162 function Known_Null (N : Node_Id) return Boolean is
6163 begin
6164 -- Checks for case where N is an entity reference
6165
6166 if Is_Entity_Name (N) and then Present (Entity (N)) then
fbf5a39b 6167 declare
59e54267 6168 E : constant Entity_Id := Entity (N);
fbf5a39b
AC
6169 Op : Node_Kind;
6170 Val : Node_Id;
07fc65c4 6171
fbf5a39b 6172 begin
86cde7b1
RD
6173 -- Constant null value is for sure null
6174
6175 if Ekind (E) = E_Constant
6176 and then Known_Null (Constant_Value (E))
6177 then
6178 return True;
6179 end if;
6180
59e54267
ES
6181 -- First check if we are in decisive conditional
6182
fbf5a39b 6183 Get_Current_Value_Condition (N, Op, Val);
59e54267 6184
86cde7b1 6185 if Known_Null (Val) then
59e54267
ES
6186 if Op = N_Op_Eq then
6187 return True;
6188 elsif Op = N_Op_Ne then
6189 return False;
6190 end if;
6191 end if;
6192
6193 -- If OK to do replacement, test Is_Known_Null flag
6194
6195 if OK_To_Do_Constant_Replacement (E) then
6196 return Is_Known_Null (E);
6197
6198 -- Otherwise if not safe to do replacement, then say so
6199
6200 else
6201 return False;
6202 end if;
fbf5a39b
AC
6203 end;
6204
59e54267
ES
6205 -- True if explicit reference to null
6206
6207 elsif Nkind (N) = N_Null then
6208 return True;
6209
6210 -- For a conversion, true if expression is known null
6211
6212 elsif Nkind (N) = N_Type_Conversion then
6213 return Known_Null (Expression (N));
6214
6215 -- Above are all cases where the value could be determined to be null.
6216 -- In all other cases, we don't know, so return False.
fbf5a39b
AC
6217
6218 else
6219 return False;
6220 end if;
59e54267 6221 end Known_Null;
07fc65c4 6222
70482933
RK
6223 -----------------------------
6224 -- Make_CW_Equivalent_Type --
6225 -----------------------------
6226
6fb4cdde
AC
6227 -- Create a record type used as an equivalent of any member of the class
6228 -- which takes its size from exp.
70482933
RK
6229
6230 -- Generate the following code:
6231
6232 -- type Equiv_T is record
e14c931f 6233 -- _parent : T (List of discriminant constraints taken from Exp);
fbf5a39b 6234 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
70482933 6235 -- end Equiv_T;
fbf5a39b
AC
6236 --
6237 -- ??? Note that this type does not guarantee same alignment as all
6238 -- derived types
70482933
RK
6239
6240 function Make_CW_Equivalent_Type
bebbff91
AC
6241 (T : Entity_Id;
6242 E : Node_Id) return Entity_Id
70482933
RK
6243 is
6244 Loc : constant Source_Ptr := Sloc (E);
6245 Root_Typ : constant Entity_Id := Root_Type (T);
fbf5a39b 6246 List_Def : constant List_Id := Empty_List;
0712790c 6247 Comp_List : constant List_Id := New_List;
70482933
RK
6248 Equiv_Type : Entity_Id;
6249 Range_Type : Entity_Id;
6250 Str_Type : Entity_Id;
70482933
RK
6251 Constr_Root : Entity_Id;
6252 Sizexpr : Node_Id;
6253
6254 begin
3e2399ba
AC
6255 -- If the root type is already constrained, there are no discriminants
6256 -- in the expression.
6257
6258 if not Has_Discriminants (Root_Typ)
6259 or else Is_Constrained (Root_Typ)
6260 then
70482933 6261 Constr_Root := Root_Typ;
ed09416f
AC
6262
6263 -- At this point in the expansion, non-limited view of the type
6264 -- must be available, otherwise the error will be reported later.
6265
6266 if From_Limited_With (Constr_Root)
6267 and then Present (Non_Limited_View (Constr_Root))
6268 then
6269 Constr_Root := Non_Limited_View (Constr_Root);
6270 end if;
6271
70482933 6272 else
092ef350 6273 Constr_Root := Make_Temporary (Loc, 'R');
70482933
RK
6274
6275 -- subtype cstr__n is T (List of discr constraints taken from Exp)
6276
6277 Append_To (List_Def,
6278 Make_Subtype_Declaration (Loc,
6279 Defining_Identifier => Constr_Root,
092ef350 6280 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
70482933
RK
6281 end if;
6282
0712790c 6283 -- Generate the range subtype declaration
70482933 6284
092ef350 6285 Range_Type := Make_Temporary (Loc, 'G');
70482933 6286
0712790c 6287 if not Is_Interface (Root_Typ) then
6fb4cdde 6288
0712790c
ES
6289 -- subtype rg__xx is
6290 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
6291
6292 Sizexpr :=
6293 Make_Op_Subtract (Loc,
6294 Left_Opnd =>
6295 Make_Attribute_Reference (Loc,
6296 Prefix =>
6297 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6298 Attribute_Name => Name_Size),
6299 Right_Opnd =>
6300 Make_Attribute_Reference (Loc,
e4494292 6301 Prefix => New_Occurrence_Of (Constr_Root, Loc),
0712790c
ES
6302 Attribute_Name => Name_Object_Size));
6303 else
6304 -- subtype rg__xx is
6305 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
6306
6307 Sizexpr :=
6308 Make_Attribute_Reference (Loc,
6309 Prefix =>
6310 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6311 Attribute_Name => Name_Size);
6312 end if;
70482933
RK
6313
6314 Set_Paren_Count (Sizexpr, 1);
6315
6316 Append_To (List_Def,
6317 Make_Subtype_Declaration (Loc,
6318 Defining_Identifier => Range_Type,
6319 Subtype_Indication =>
6320 Make_Subtype_Indication (Loc,
e4494292 6321 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
70482933
RK
6322 Constraint => Make_Range_Constraint (Loc,
6323 Range_Expression =>
6324 Make_Range (Loc,
6325 Low_Bound => Make_Integer_Literal (Loc, 1),
6326 High_Bound =>
6327 Make_Op_Divide (Loc,
6328 Left_Opnd => Sizexpr,
6329 Right_Opnd => Make_Integer_Literal (Loc,
6330 Intval => System_Storage_Unit)))))));
6331
6332 -- subtype str__nn is Storage_Array (rg__x);
6333
092ef350 6334 Str_Type := Make_Temporary (Loc, 'S');
70482933
RK
6335 Append_To (List_Def,
6336 Make_Subtype_Declaration (Loc,
6337 Defining_Identifier => Str_Type,
6338 Subtype_Indication =>
6339 Make_Subtype_Indication (Loc,
e4494292 6340 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
70482933
RK
6341 Constraint =>
6342 Make_Index_Or_Discriminant_Constraint (Loc,
6343 Constraints =>
e4494292 6344 New_List (New_Occurrence_Of (Range_Type, Loc))))));
70482933
RK
6345
6346 -- type Equiv_T is record
0712790c 6347 -- [ _parent : Tnn; ]
70482933
RK
6348 -- E : Str_Type;
6349 -- end Equiv_T;
6350
092ef350 6351 Equiv_Type := Make_Temporary (Loc, 'T');
70482933
RK
6352 Set_Ekind (Equiv_Type, E_Record_Type);
6353 Set_Parent_Subtype (Equiv_Type, Constr_Root);
6354
80fa4617
EB
6355 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
6356 -- treatment for this type. In particular, even though _parent's type
6357 -- is a controlled type or contains controlled components, we do not
6358 -- want to set Has_Controlled_Component on it to avoid making it gain
6359 -- an unwanted _controller component.
6360
6361 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
6362
f3296dd3 6363 -- A class-wide equivalent type does not require initialization
3dddb11e
ES
6364
6365 Set_Suppress_Initialization (Equiv_Type);
6366
0712790c
ES
6367 if not Is_Interface (Root_Typ) then
6368 Append_To (Comp_List,
6369 Make_Component_Declaration (Loc,
3dddb11e 6370 Defining_Identifier =>
0712790c
ES
6371 Make_Defining_Identifier (Loc, Name_uParent),
6372 Component_Definition =>
6373 Make_Component_Definition (Loc,
6374 Aliased_Present => False,
e4494292 6375 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
0712790c
ES
6376 end if;
6377
6378 Append_To (Comp_List,
6379 Make_Component_Declaration (Loc,
092ef350 6380 Defining_Identifier => Make_Temporary (Loc, 'C'),
0712790c
ES
6381 Component_Definition =>
6382 Make_Component_Definition (Loc,
6383 Aliased_Present => False,
e4494292 6384 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
0712790c 6385
70482933
RK
6386 Append_To (List_Def,
6387 Make_Full_Type_Declaration (Loc,
6388 Defining_Identifier => Equiv_Type,
3dddb11e 6389 Type_Definition =>
70482933 6390 Make_Record_Definition (Loc,
3dddb11e 6391 Component_List =>
0712790c
ES
6392 Make_Component_List (Loc,
6393 Component_Items => Comp_List,
6394 Variant_Part => Empty))));
6395
273adcdf
AC
6396 -- Suppress all checks during the analysis of the expanded code to avoid
6397 -- the generation of spurious warnings under ZFP run-time.
0712790c
ES
6398
6399 Insert_Actions (E, List_Def, Suppress => All_Checks);
70482933
RK
6400 return Equiv_Type;
6401 end Make_CW_Equivalent_Type;
6402
e606088a
AC
6403 -------------------------
6404 -- Make_Invariant_Call --
6405 -------------------------
6406
6407 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
3ddfabe3
AC
6408 Loc : constant Source_Ptr := Sloc (Expr);
6409 Typ : constant Entity_Id := Base_Type (Etype (Expr));
6410 Proc_Id : Entity_Id;
e606088a
AC
6411
6412 begin
3ddfabe3 6413 pragma Assert (Has_Invariants (Typ));
2995860f 6414
3ddfabe3
AC
6415 Proc_Id := Invariant_Procedure (Typ);
6416 pragma Assert (Present (Proc_Id));
2995860f 6417
f1c80977
AC
6418 return
6419 Make_Procedure_Call_Statement (Loc,
3ddfabe3 6420 Name => New_Occurrence_Of (Proc_Id, Loc),
f1c80977 6421 Parameter_Associations => New_List (Relocate_Node (Expr)));
e606088a
AC
6422 end Make_Invariant_Call;
6423
70482933
RK
6424 ------------------------
6425 -- Make_Literal_Range --
6426 ------------------------
6427
6428 function Make_Literal_Range
6429 (Loc : Source_Ptr;
bebbff91 6430 Literal_Typ : Entity_Id) return Node_Id
70482933 6431 is
86cde7b1
RD
6432 Lo : constant Node_Id :=
6433 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
6434 Index : constant Entity_Id := Etype (Lo);
6435
6436 Hi : Node_Id;
6437 Length_Expr : constant Node_Id :=
6438 Make_Op_Subtract (Loc,
6439 Left_Opnd =>
6440 Make_Integer_Literal (Loc,
6441 Intval => String_Literal_Length (Literal_Typ)),
6442 Right_Opnd =>
6443 Make_Integer_Literal (Loc, 1));
f91b40db 6444
70482933 6445 begin
f91b40db
GB
6446 Set_Analyzed (Lo, False);
6447
86cde7b1
RD
6448 if Is_Integer_Type (Index) then
6449 Hi :=
6450 Make_Op_Add (Loc,
6451 Left_Opnd => New_Copy_Tree (Lo),
6452 Right_Opnd => Length_Expr);
6453 else
6454 Hi :=
6455 Make_Attribute_Reference (Loc,
6456 Attribute_Name => Name_Val,
6457 Prefix => New_Occurrence_Of (Index, Loc),
6458 Expressions => New_List (
6459 Make_Op_Add (Loc,
6460 Left_Opnd =>
6461 Make_Attribute_Reference (Loc,
6462 Attribute_Name => Name_Pos,
6463 Prefix => New_Occurrence_Of (Index, Loc),
6464 Expressions => New_List (New_Copy_Tree (Lo))),
6465 Right_Opnd => Length_Expr)));
6466 end if;
6467
70482933
RK
6468 return
6469 Make_Range (Loc,
86cde7b1
RD
6470 Low_Bound => Lo,
6471 High_Bound => Hi);
70482933
RK
6472 end Make_Literal_Range;
6473
b3b9865d
AC
6474 --------------------------
6475 -- Make_Non_Empty_Check --
6476 --------------------------
6477
6478 function Make_Non_Empty_Check
6479 (Loc : Source_Ptr;
6480 N : Node_Id) return Node_Id
6481 is
6482 begin
6483 return
6484 Make_Op_Ne (Loc,
6485 Left_Opnd =>
6486 Make_Attribute_Reference (Loc,
6487 Attribute_Name => Name_Length,
6488 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
6489 Right_Opnd =>
6490 Make_Integer_Literal (Loc, 0));
6491 end Make_Non_Empty_Check;
6492
4818e7b9
RD
6493 -------------------------
6494 -- Make_Predicate_Call --
6495 -------------------------
6496
6497 function Make_Predicate_Call
6498 (Typ : Entity_Id;
fc142f63
AC
6499 Expr : Node_Id;
6500 Mem : Boolean := False) return Node_Id
4818e7b9 6501 is
241ebe89
HK
6502 Loc : constant Source_Ptr := Sloc (Expr);
6503 Call : Node_Id;
6504 PFM : Entity_Id;
6505
1af4455a 6506 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4818e7b9
RD
6507
6508 begin
6509 pragma Assert (Present (Predicate_Function (Typ)));
6510
1af4455a
HK
6511 -- The related type may be subject to pragma Ghost. Set the mode now to
6512 -- ensure that the call is properly marked as Ghost.
241ebe89
HK
6513
6514 Set_Ghost_Mode_From_Entity (Typ);
6515
fc142f63
AC
6516 -- Call special membership version if requested and available
6517
6518 if Mem then
241ebe89
HK
6519 PFM := Predicate_Function_M (Typ);
6520
6521 if Present (PFM) then
6522 Call :=
6523 Make_Function_Call (Loc,
6524 Name => New_Occurrence_Of (PFM, Loc),
6525 Parameter_Associations => New_List (Relocate_Node (Expr)));
6526
1af4455a 6527 Ghost_Mode := Save_Ghost_Mode;
241ebe89
HK
6528 return Call;
6529 end if;
fc142f63
AC
6530 end if;
6531
6532 -- Case of calling normal predicate function
6533
241ebe89
HK
6534 Call :=
6535 Make_Function_Call (Loc,
6536 Name =>
6537 New_Occurrence_Of (Predicate_Function (Typ), Loc),
6538 Parameter_Associations => New_List (Relocate_Node (Expr)));
6539
1af4455a 6540 Ghost_Mode := Save_Ghost_Mode;
241ebe89 6541 return Call;
4818e7b9
RD
6542 end Make_Predicate_Call;
6543
6544 --------------------------
6545 -- Make_Predicate_Check --
6546 --------------------------
6547
6548 function Make_Predicate_Check
6549 (Typ : Entity_Id;
6550 Expr : Node_Id) return Node_Id
6551 is
88fa9a24
ES
6552 procedure Replace_Subtype_Reference (N : Node_Id);
6553 -- Replace current occurrences of the subtype to which a dynamic
6554 -- predicate applies, by the expression that triggers a predicate
6555 -- check. This is needed for aspect Predicate_Failure, for which
6556 -- we do not generate a wrapper procedure, but simply modify the
6557 -- expression for the pragma of the predicate check.
6558
6559 --------------------------------
6560 -- Replace_Subtype_Reference --
6561 --------------------------------
6562
6563 procedure Replace_Subtype_Reference (N : Node_Id) is
6564 begin
6565 Rewrite (N, New_Copy_Tree (Expr));
6566
6567 -- We want to treat the node as if it comes from source, so
6568 -- that ASIS will not ignore it.
6569
6570 Set_Comes_From_Source (N, True);
6571 end Replace_Subtype_Reference;
6572
6573 procedure Replace_Subtype_References is
6574 new Replace_Type_References_Generic (Replace_Subtype_Reference);
6575
6576 -- Local variables
6577
6578 Loc : constant Source_Ptr := Sloc (Expr);
6579 Arg_List : List_Id;
6580 Fail_Expr : Node_Id;
6581 Nam : Name_Id;
6582
6583 -- Start of processing for Make_Predicate_Check
4818e7b9
RD
6584
6585 begin
48bb06a7
AC
6586 -- If predicate checks are suppressed, then return a null statement. For
6587 -- this call, we check only the scope setting. If the caller wants to
6588 -- check a specific entity's setting, they must do it manually.
f1c80977
AC
6589
6590 if Predicate_Checks_Suppressed (Empty) then
6591 return Make_Null_Statement (Loc);
6592 end if;
6593
8e1e62e3
AC
6594 -- Do not generate a check within an internal subprogram (stream
6595 -- functions and the like, including including predicate functions).
6596
6597 if Within_Internal_Subprogram then
6598 return Make_Null_Statement (Loc);
6599 end if;
6600
aab45d22 6601 -- Compute proper name to use, we need to get this right so that the
16d3a853 6602 -- right set of check policies apply to the Check pragma we are making.
aab45d22
AC
6603
6604 if Has_Dynamic_Predicate_Aspect (Typ) then
6605 Nam := Name_Dynamic_Predicate;
6606 elsif Has_Static_Predicate_Aspect (Typ) then
6607 Nam := Name_Static_Predicate;
6608 else
6609 Nam := Name_Predicate;
6610 end if;
6611
a2c314c7
AC
6612 Arg_List := New_List (
6613 Make_Pragma_Argument_Association (Loc,
6614 Expression => Make_Identifier (Loc, Nam)),
6615 Make_Pragma_Argument_Association (Loc,
6616 Expression => Make_Predicate_Call (Typ, Expr)));
6617
88fa9a24
ES
6618 -- If subtype has Predicate_Failure defined, add the correponding
6619 -- expression as an additional pragma parameter, after replacing
6620 -- current instances with the expression being checked.
6621
a2c314c7 6622 if Has_Aspect (Typ, Aspect_Predicate_Failure) then
88fa9a24
ES
6623 Fail_Expr :=
6624 New_Copy_Tree
6625 (Expression (Find_Aspect (Typ, Aspect_Predicate_Failure)));
6626 Replace_Subtype_References (Fail_Expr, Typ);
6627
a2c314c7
AC
6628 Append_To (Arg_List,
6629 Make_Pragma_Argument_Association (Loc,
88fa9a24 6630 Expression => Fail_Expr));
a2c314c7
AC
6631 end if;
6632
4818e7b9
RD
6633 return
6634 Make_Pragma (Loc,
7675ad4f 6635 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
a2c314c7 6636 Pragma_Argument_Associations => Arg_List);
4818e7b9
RD
6637 end Make_Predicate_Check;
6638
70482933
RK
6639 ----------------------------
6640 -- Make_Subtype_From_Expr --
6641 ----------------------------
6642
e14c931f
RW
6643 -- 1. If Expr is an unconstrained array expression, creates
6644 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
70482933
RK
6645
6646 -- 2. If Expr is a unconstrained discriminated type expression, creates
6647 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
6648
f3296dd3 6649 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
70482933
RK
6650
6651 function Make_Subtype_From_Expr
d9307840
HK
6652 (E : Node_Id;
6653 Unc_Typ : Entity_Id;
6654 Related_Id : Entity_Id := Empty) return Node_Id
70482933 6655 is
fbf5a39b 6656 List_Constr : constant List_Id := New_List;
d18b1548 6657 Loc : constant Source_Ptr := Sloc (E);
70482933 6658 D : Entity_Id;
d18b1548
AC
6659 Full_Exp : Node_Id;
6660 Full_Subtyp : Entity_Id;
6661 High_Bound : Entity_Id;
6662 Index_Typ : Entity_Id;
6663 Low_Bound : Entity_Id;
6664 Priv_Subtyp : Entity_Id;
6665 Utyp : Entity_Id;
70482933
RK
6666
6667 begin
6668 if Is_Private_Type (Unc_Typ)
6669 and then Has_Unknown_Discriminants (Unc_Typ)
6670 then
2f54ef3d
AC
6671 -- The caller requests a unique external name for both the private
6672 -- and the full subtype.
d9307840
HK
6673
6674 if Present (Related_Id) then
6675 Full_Subtyp :=
6676 Make_Defining_Identifier (Loc,
6677 Chars => New_External_Name (Chars (Related_Id), 'C'));
6678 Priv_Subtyp :=
6679 Make_Defining_Identifier (Loc,
6680 Chars => New_External_Name (Chars (Related_Id), 'P'));
6681
6682 else
6683 Full_Subtyp := Make_Temporary (Loc, 'C');
6684 Priv_Subtyp := Make_Temporary (Loc, 'P');
6685 end if;
6686
d18b1548
AC
6687 -- Prepare the subtype completion. Use the base type to find the
6688 -- underlying type because the type may be a generic actual or an
6689 -- explicit subtype.
70482933 6690
d9307840
HK
6691 Utyp := Underlying_Type (Base_Type (Unc_Typ));
6692
6693 Full_Exp :=
092ef350 6694 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
70482933
RK
6695 Set_Parent (Full_Exp, Parent (E));
6696
70482933
RK
6697 Insert_Action (E,
6698 Make_Subtype_Declaration (Loc,
6699 Defining_Identifier => Full_Subtyp,
6700 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
6701
6702 -- Define the dummy private subtype
6703
6704 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
ea985d95 6705 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
70482933
RK
6706 Set_Scope (Priv_Subtyp, Full_Subtyp);
6707 Set_Is_Constrained (Priv_Subtyp);
6708 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
6709 Set_Is_Itype (Priv_Subtyp);
6710 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
6711
6712 if Is_Tagged_Type (Priv_Subtyp) then
6713 Set_Class_Wide_Type
6714 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
ef2a63ba
JM
6715 Set_Direct_Primitive_Operations (Priv_Subtyp,
6716 Direct_Primitive_Operations (Unc_Typ));
70482933
RK
6717 end if;
6718
6719 Set_Full_View (Priv_Subtyp, Full_Subtyp);
6720
e4494292 6721 return New_Occurrence_Of (Priv_Subtyp, Loc);
70482933
RK
6722
6723 elsif Is_Array_Type (Unc_Typ) then
d18b1548 6724 Index_Typ := First_Index (Unc_Typ);
70482933 6725 for J in 1 .. Number_Dimensions (Unc_Typ) loop
d18b1548
AC
6726
6727 -- Capture the bounds of each index constraint in case the context
6728 -- is an object declaration of an unconstrained type initialized
6729 -- by a function call:
6730
6731 -- Obj : Unconstr_Typ := Func_Call;
6732
6733 -- This scenario requires secondary scope management and the index
6734 -- constraint cannot depend on the temporary used to capture the
6735 -- result of the function call.
6736
6737 -- SS_Mark;
6738 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
6739 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
6740 -- Obj : S := Temp.all;
6741 -- SS_Release; -- Temp is gone at this point, bounds of S are
6742 -- -- non existent.
6743
d18b1548 6744 -- Generate:
3fbbbd1e 6745 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
d18b1548
AC
6746
6747 Low_Bound := Make_Temporary (Loc, 'B');
6748 Insert_Action (E,
6749 Make_Object_Declaration (Loc,
6750 Defining_Identifier => Low_Bound,
6751 Object_Definition =>
6752 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
3fbbbd1e 6753 Constant_Present => True,
d18b1548 6754 Expression =>
70482933 6755 Make_Attribute_Reference (Loc,
d18b1548 6756 Prefix => Duplicate_Subexpr_No_Checks (E),
70482933 6757 Attribute_Name => Name_First,
d18b1548
AC
6758 Expressions => New_List (
6759 Make_Integer_Literal (Loc, J)))));
8cbb664e 6760
d18b1548 6761 -- Generate:
3fbbbd1e 6762 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
d18b1548
AC
6763
6764 High_Bound := Make_Temporary (Loc, 'B');
6765 Insert_Action (E,
6766 Make_Object_Declaration (Loc,
6767 Defining_Identifier => High_Bound,
6768 Object_Definition =>
6769 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
3fbbbd1e 6770 Constant_Present => True,
d18b1548 6771 Expression =>
70482933 6772 Make_Attribute_Reference (Loc,
8cbb664e 6773 Prefix => Duplicate_Subexpr_No_Checks (E),
70482933
RK
6774 Attribute_Name => Name_Last,
6775 Expressions => New_List (
6776 Make_Integer_Literal (Loc, J)))));
d18b1548
AC
6777
6778 Append_To (List_Constr,
6779 Make_Range (Loc,
6780 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
6781 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
6782
6783 Index_Typ := Next_Index (Index_Typ);
70482933
RK
6784 end loop;
6785
6786 elsif Is_Class_Wide_Type (Unc_Typ) then
6787 declare
6788 CW_Subtype : Entity_Id;
6789 EQ_Typ : Entity_Id := Empty;
6790
6791 begin
535a8637 6792 -- A class-wide equivalent type is not needed on VM targets
0712790c 6793 -- because the VM back-ends handle the class-wide object
44d6a706 6794 -- initialization itself (and doesn't need or want the
70482933
RK
6795 -- additional intermediate type to handle the assignment).
6796
1f110335 6797 if Expander_Active and then Tagged_Type_Expansion then
22cb89b5 6798
f3296dd3
AC
6799 -- If this is the class-wide type of a completion that is a
6800 -- record subtype, set the type of the class-wide type to be
273adcdf
AC
6801 -- the full base type, for use in the expanded code for the
6802 -- equivalent type. Should this be done earlier when the
6803 -- completion is analyzed ???
22cb89b5
AC
6804
6805 if Is_Private_Type (Etype (Unc_Typ))
6806 and then
6807 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
6808 then
6809 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
6810 end if;
6811
70482933
RK
6812 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
6813 end if;
6814
6815 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
6816 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
6817 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
6818
6819 return New_Occurrence_Of (CW_Subtype, Loc);
6820 end;
6821
ea985d95 6822 -- Indefinite record type with discriminants
fbf5a39b 6823
70482933
RK
6824 else
6825 D := First_Discriminant (Unc_Typ);
fbf5a39b 6826 while Present (D) loop
70482933
RK
6827 Append_To (List_Constr,
6828 Make_Selected_Component (Loc,
8cbb664e 6829 Prefix => Duplicate_Subexpr_No_Checks (E),
e4494292 6830 Selector_Name => New_Occurrence_Of (D, Loc)));
70482933
RK
6831
6832 Next_Discriminant (D);
6833 end loop;
6834 end if;
6835
6836 return
6837 Make_Subtype_Indication (Loc,
e4494292 6838 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
70482933
RK
6839 Constraint =>
6840 Make_Index_Or_Discriminant_Constraint (Loc,
6841 Constraints => List_Constr));
6842 end Make_Subtype_From_Expr;
6843
e03f7ccf
AC
6844 ----------------------------
6845 -- Matching_Standard_Type --
6846 ----------------------------
6847
6848 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
6849 pragma Assert (Is_Scalar_Type (Typ));
6850 Siz : constant Uint := Esize (Typ);
6851
6852 begin
adb252d8 6853 -- Floating-point cases
e03f7ccf
AC
6854
6855 if Is_Floating_Point_Type (Typ) then
6856 if Siz <= Esize (Standard_Short_Float) then
6857 return Standard_Short_Float;
6858 elsif Siz <= Esize (Standard_Float) then
6859 return Standard_Float;
6860 elsif Siz <= Esize (Standard_Long_Float) then
6861 return Standard_Long_Float;
6862 elsif Siz <= Esize (Standard_Long_Long_Float) then
6863 return Standard_Long_Long_Float;
6864 else
6865 raise Program_Error;
6866 end if;
6867
6868 -- Integer cases (includes fixed-point types)
6869
adb252d8 6870 -- Unsigned integer cases (includes normal enumeration types)
e03f7ccf
AC
6871
6872 elsif Is_Unsigned_Type (Typ) then
6873 if Siz <= Esize (Standard_Short_Short_Unsigned) then
6874 return Standard_Short_Short_Unsigned;
6875 elsif Siz <= Esize (Standard_Short_Unsigned) then
6876 return Standard_Short_Unsigned;
6877 elsif Siz <= Esize (Standard_Unsigned) then
6878 return Standard_Unsigned;
6879 elsif Siz <= Esize (Standard_Long_Unsigned) then
6880 return Standard_Long_Unsigned;
6881 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
6882 return Standard_Long_Long_Unsigned;
6883 else
6884 raise Program_Error;
6885 end if;
6886
adb252d8 6887 -- Signed integer cases
e03f7ccf
AC
6888
6889 else
6890 if Siz <= Esize (Standard_Short_Short_Integer) then
6891 return Standard_Short_Short_Integer;
6892 elsif Siz <= Esize (Standard_Short_Integer) then
6893 return Standard_Short_Integer;
6894 elsif Siz <= Esize (Standard_Integer) then
6895 return Standard_Integer;
6896 elsif Siz <= Esize (Standard_Long_Integer) then
6897 return Standard_Long_Integer;
6898 elsif Siz <= Esize (Standard_Long_Long_Integer) then
6899 return Standard_Long_Long_Integer;
6900 else
6901 raise Program_Error;
6902 end if;
6903 end if;
6904 end Matching_Standard_Type;
6905
70482933
RK
6906 -----------------------------
6907 -- May_Generate_Large_Temp --
6908 -----------------------------
6909
273adcdf
AC
6910 -- At the current time, the only types that we return False for (i.e. where
6911 -- we decide we know they cannot generate large temps) are ones where we
6912 -- know the size is 256 bits or less at compile time, and we are still not
6913 -- doing a thorough job on arrays and records ???
70482933
RK
6914
6915 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
6916 begin
7324bf49 6917 if not Size_Known_At_Compile_Time (Typ) then
70482933
RK
6918 return False;
6919
6920 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
6921 return False;
6922
8ca597af
RD
6923 elsif Is_Array_Type (Typ)
6924 and then Present (Packed_Array_Impl_Type (Typ))
6925 then
6926 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
70482933
RK
6927
6928 -- We could do more here to find other small types ???
6929
6930 else
6931 return True;
6932 end if;
6933 end May_Generate_Large_Temp;
6934
df3e68b1
HK
6935 ------------------------
6936 -- Needs_Finalization --
6937 ------------------------
6938
6939 function Needs_Finalization (T : Entity_Id) return Boolean is
6940 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
6941 -- If type is not frozen yet, check explicitly among its components,
6942 -- because the Has_Controlled_Component flag is not necessarily set.
6943
6944 -----------------------------------
6945 -- Has_Some_Controlled_Component --
6946 -----------------------------------
6947
6948 function Has_Some_Controlled_Component
6949 (Rec : Entity_Id) return Boolean
6950 is
6951 Comp : Entity_Id;
6952
6953 begin
6954 if Has_Controlled_Component (Rec) then
6955 return True;
6956
6957 elsif not Is_Frozen (Rec) then
6958 if Is_Record_Type (Rec) then
6959 Comp := First_Entity (Rec);
6960
6961 while Present (Comp) loop
6962 if not Is_Type (Comp)
6963 and then Needs_Finalization (Etype (Comp))
6964 then
6965 return True;
6966 end if;
6967
6968 Next_Entity (Comp);
6969 end loop;
6970
6971 return False;
6972
df3e68b1 6973 else
24d2fbbe
BD
6974 return
6975 Is_Array_Type (Rec)
6976 and then Needs_Finalization (Component_Type (Rec));
df3e68b1
HK
6977 end if;
6978 else
6979 return False;
6980 end if;
6981 end Has_Some_Controlled_Component;
6982
6983 -- Start of processing for Needs_Finalization
6984
6985 begin
f553e7bc
AC
6986 -- Certain run-time configurations and targets do not provide support
6987 -- for controlled types.
df3e68b1 6988
f553e7bc
AC
6989 if Restriction_Active (No_Finalization) then
6990 return False;
df3e68b1 6991
535a8637
AC
6992 -- C++ types are not considered controlled. It is assumed that the
6993 -- non-Ada side will handle their clean up.
ca5af305 6994
535a8637 6995 elsif Convention (T) = Convention_CPP then
ca5af305
AC
6996 return False;
6997
c8593453
AC
6998 -- Never needs finalization if Disable_Controlled set
6999
7000 elsif Disable_Controlled (T) then
7001 return False;
7002
db99c46e
AC
7003 elsif Is_Class_Wide_Type (T) and then Disable_Controlled (Etype (T)) then
7004 return False;
7005
f553e7bc
AC
7006 else
7007 -- Class-wide types are treated as controlled because derivations
7008 -- from the root type can introduce controlled components.
7009
c8593453 7010 return Is_Class_Wide_Type (T)
f553e7bc 7011 or else Is_Controlled (T)
f553e7bc
AC
7012 or else Has_Some_Controlled_Component (T)
7013 or else
7014 (Is_Concurrent_Type (T)
0e564ab4
AC
7015 and then Present (Corresponding_Record_Type (T))
7016 and then Needs_Finalization (Corresponding_Record_Type (T)));
f553e7bc 7017 end if;
df3e68b1
HK
7018 end Needs_Finalization;
7019
0d901290
AC
7020 ----------------------------
7021 -- Needs_Constant_Address --
7022 ----------------------------
7023
7024 function Needs_Constant_Address
7025 (Decl : Node_Id;
7026 Typ : Entity_Id) return Boolean
7027 is
7028 begin
7029
273adcdf
AC
7030 -- If we have no initialization of any kind, then we don't need to place
7031 -- any restrictions on the address clause, because the object will be
7032 -- elaborated after the address clause is evaluated. This happens if the
7033 -- declaration has no initial expression, or the type has no implicit
7034 -- initialization, or the object is imported.
0d901290 7035
273adcdf
AC
7036 -- The same holds for all initialized scalar types and all access types.
7037 -- Packed bit arrays of size up to 64 are represented using a modular
7038 -- type with an initialization (to zero) and can be processed like other
7039 -- initialized scalar types.
0d901290
AC
7040
7041 -- If the type is controlled, code to attach the object to a
273adcdf
AC
7042 -- finalization chain is generated at the point of declaration, and
7043 -- therefore the elaboration of the object cannot be delayed: the
7044 -- address expression must be a constant.
0d901290
AC
7045
7046 if No (Expression (Decl))
7047 and then not Needs_Finalization (Typ)
7048 and then
7049 (not Has_Non_Null_Base_Init_Proc (Typ)
7050 or else Is_Imported (Defining_Identifier (Decl)))
7051 then
7052 return False;
7053
7054 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
7055 or else Is_Access_Type (Typ)
7056 or else
7057 (Is_Bit_Packed_Array (Typ)
8ca597af 7058 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
0d901290
AC
7059 then
7060 return False;
7061
7062 else
7063
7064 -- Otherwise, we require the address clause to be constant because
7065 -- the call to the initialization procedure (or the attach code) has
7066 -- to happen at the point of the declaration.
7067
273adcdf
AC
7068 -- Actually the IP call has been moved to the freeze actions anyway,
7069 -- so maybe we can relax this restriction???
0d901290
AC
7070
7071 return True;
7072 end if;
7073 end Needs_Constant_Address;
7074
70482933
RK
7075 ----------------------------
7076 -- New_Class_Wide_Subtype --
7077 ----------------------------
7078
7079 function New_Class_Wide_Subtype
7080 (CW_Typ : Entity_Id;
bebbff91 7081 N : Node_Id) return Entity_Id
70482933 7082 is
fbf5a39b
AC
7083 Res : constant Entity_Id := Create_Itype (E_Void, N);
7084 Res_Name : constant Name_Id := Chars (Res);
7085 Res_Scope : constant Entity_Id := Scope (Res);
70482933
RK
7086
7087 begin
7088 Copy_Node (CW_Typ, Res);
05350ac6 7089 Set_Comes_From_Source (Res, False);
70482933
RK
7090 Set_Sloc (Res, Sloc (N));
7091 Set_Is_Itype (Res);
7092 Set_Associated_Node_For_Itype (Res, N);
7093 Set_Is_Public (Res, False); -- By default, may be changed below.
7094 Set_Public_Status (Res);
7095 Set_Chars (Res, Res_Name);
7096 Set_Scope (Res, Res_Scope);
7097 Set_Ekind (Res, E_Class_Wide_Subtype);
7098 Set_Next_Entity (Res, Empty);
7099 Set_Etype (Res, Base_Type (CW_Typ));
cbae498b 7100 Set_Is_Frozen (Res, False);
70482933
RK
7101 Set_Freeze_Node (Res, Empty);
7102 return (Res);
7103 end New_Class_Wide_Subtype;
7104
0712790c
ES
7105 --------------------------------
7106 -- Non_Limited_Designated_Type --
7107 ---------------------------------
7108
7109 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
7110 Desig : constant Entity_Id := Designated_Type (T);
7111 begin
47346923 7112 if Has_Non_Limited_View (Desig) then
0712790c
ES
7113 return Non_Limited_View (Desig);
7114 else
7115 return Desig;
7116 end if;
7117 end Non_Limited_Designated_Type;
7118
59e54267
ES
7119 -----------------------------------
7120 -- OK_To_Do_Constant_Replacement --
7121 -----------------------------------
7122
7123 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
7124 ES : constant Entity_Id := Scope (E);
7125 CS : Entity_Id;
7126
7127 begin
7128 -- Do not replace statically allocated objects, because they may be
7129 -- modified outside the current scope.
7130
7131 if Is_Statically_Allocated (E) then
7132 return False;
7133
7134 -- Do not replace aliased or volatile objects, since we don't know what
7135 -- else might change the value.
7136
7137 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
7138 return False;
7139
7140 -- Debug flag -gnatdM disconnects this optimization
7141
7142 elsif Debug_Flag_MM then
7143 return False;
7144
7145 -- Otherwise check scopes
7146
7147 else
59e54267
ES
7148 CS := Current_Scope;
7149
7150 loop
7151 -- If we are in right scope, replacement is safe
7152
7153 if CS = ES then
7154 return True;
7155
7156 -- Packages do not affect the determination of safety
7157
7158 elsif Ekind (CS) = E_Package then
59e54267 7159 exit when CS = Standard_Standard;
05350ac6 7160 CS := Scope (CS);
59e54267
ES
7161
7162 -- Blocks do not affect the determination of safety
7163
7164 elsif Ekind (CS) = E_Block then
7165 CS := Scope (CS);
7166
05350ac6
BD
7167 -- Loops do not affect the determination of safety. Note that we
7168 -- kill all current values on entry to a loop, so we are just
7169 -- talking about processing within a loop here.
7170
7171 elsif Ekind (CS) = E_Loop then
7172 CS := Scope (CS);
7173
59e54267
ES
7174 -- Otherwise, the reference is dubious, and we cannot be sure that
7175 -- it is safe to do the replacement.
7176
7177 else
7178 exit;
7179 end if;
7180 end loop;
7181
7182 return False;
7183 end if;
7184 end OK_To_Do_Constant_Replacement;
7185
0712790c
ES
7186 ------------------------------------
7187 -- Possible_Bit_Aligned_Component --
7188 ------------------------------------
7189
7190 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
7191 begin
184a23e9
HK
7192 -- Do not process an unanalyzed node because it is not yet decorated and
7193 -- most checks performed below will fail.
7194
7195 if not Analyzed (N) then
7196 return False;
7197 end if;
7198
0712790c
ES
7199 case Nkind (N) is
7200
7201 -- Case of indexed component
7202
7203 when N_Indexed_Component =>
7204 declare
7205 P : constant Node_Id := Prefix (N);
7206 Ptyp : constant Entity_Id := Etype (P);
7207
7208 begin
7209 -- If we know the component size and it is less than 64, then
dc7c0c4d
AC
7210 -- we are definitely OK. The back end always does assignment of
7211 -- misaligned small objects correctly.
0712790c
ES
7212
7213 if Known_Static_Component_Size (Ptyp)
7214 and then Component_Size (Ptyp) <= 64
7215 then
7216 return False;
7217
7218 -- Otherwise, we need to test the prefix, to see if we are
7219 -- indexing from a possibly unaligned component.
7220
7221 else
7222 return Possible_Bit_Aligned_Component (P);
7223 end if;
7224 end;
7225
7226 -- Case of selected component
7227
7228 when N_Selected_Component =>
7229 declare
7230 P : constant Node_Id := Prefix (N);
7231 Comp : constant Entity_Id := Entity (Selector_Name (N));
7232
7233 begin
7234 -- If there is no component clause, then we are in the clear
7235 -- since the back end will never misalign a large component
7236 -- unless it is forced to do so. In the clear means we need
7237 -- only the recursive test on the prefix.
7238
7239 if Component_May_Be_Bit_Aligned (Comp) then
7240 return True;
7241 else
7242 return Possible_Bit_Aligned_Component (P);
7243 end if;
7244 end;
7245
65df5b71 7246 -- For a slice, test the prefix, if that is possibly misaligned,
a90bd866 7247 -- then for sure the slice is.
65df5b71
HK
7248
7249 when N_Slice =>
7250 return Possible_Bit_Aligned_Component (Prefix (N));
7251
83de674b
AC
7252 -- For an unchecked conversion, check whether the expression may
7253 -- be bit-aligned.
7254
7255 when N_Unchecked_Type_Conversion =>
7256 return Possible_Bit_Aligned_Component (Expression (N));
7257
65df5b71
HK
7258 -- If we have none of the above, it means that we have fallen off the
7259 -- top testing prefixes recursively, and we now have a stand alone
469fba4a
AC
7260 -- object, where we don't have a problem, unless this is a renaming,
7261 -- in which case we need to look into the renamed object.
0712790c
ES
7262
7263 when others =>
469fba4a
AC
7264 if Is_Entity_Name (N)
7265 and then Present (Renamed_Object (Entity (N)))
7266 then
7267 return
7268 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
7269 else
7270 return False;
7271 end if;
0712790c
ES
7272
7273 end case;
7274 end Possible_Bit_Aligned_Component;
7275
2ba7e31e
AC
7276 -----------------------------------------------
7277 -- Process_Statements_For_Controlled_Objects --
7278 -----------------------------------------------
7279
7280 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
7281 Loc : constant Source_Ptr := Sloc (N);
7282
7283 function Are_Wrapped (L : List_Id) return Boolean;
7284 -- Determine whether list L contains only one statement which is a block
7285
7edfb4c6
HK
7286 function Wrap_Statements_In_Block
7287 (L : List_Id;
7288 Scop : Entity_Id := Current_Scope) return Node_Id;
2ba7e31e 7289 -- Given a list of statements L, wrap it in a block statement and return
7edfb4c6
HK
7290 -- the generated node. Scop is either the current scope or the scope of
7291 -- the context (if applicable).
2ba7e31e
AC
7292
7293 -----------------
7294 -- Are_Wrapped --
7295 -----------------
7296
7297 function Are_Wrapped (L : List_Id) return Boolean is
7298 Stmt : constant Node_Id := First (L);
2ba7e31e
AC
7299 begin
7300 return
7301 Present (Stmt)
7302 and then No (Next (Stmt))
7303 and then Nkind (Stmt) = N_Block_Statement;
7304 end Are_Wrapped;
7305
7306 ------------------------------
7307 -- Wrap_Statements_In_Block --
7308 ------------------------------
7309
7edfb4c6
HK
7310 function Wrap_Statements_In_Block
7311 (L : List_Id;
7312 Scop : Entity_Id := Current_Scope) return Node_Id
7313 is
7314 Block_Id : Entity_Id;
7315 Block_Nod : Node_Id;
7316 Iter_Loop : Entity_Id;
7317
2ba7e31e 7318 begin
7edfb4c6 7319 Block_Nod :=
2ba7e31e 7320 Make_Block_Statement (Loc,
7edfb4c6 7321 Declarations => No_List,
2ba7e31e
AC
7322 Handled_Statement_Sequence =>
7323 Make_Handled_Sequence_Of_Statements (Loc,
7324 Statements => L));
7edfb4c6
HK
7325
7326 -- Create a label for the block in case the block needs to manage the
7327 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
7328
7329 Add_Block_Identifier (Block_Nod, Block_Id);
7330
7331 -- When wrapping the statements of an iterator loop, check whether
7332 -- the loop requires secondary stack management and if so, propagate
3b8481cb 7333 -- the appropriate flags to the block. This ensures that the cursor
c624298a 7334 -- is properly cleaned up at each iteration of the loop.
7edfb4c6
HK
7335
7336 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
7337
3b8481cb 7338 if Present (Iter_Loop) then
c624298a
AC
7339 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
7340
7341 -- Secondary stack reclamation is suppressed when the associated
7342 -- iterator loop contains a return statement which uses the stack.
7343
3b8481cb
AC
7344 Set_Sec_Stack_Needed_For_Return
7345 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
7edfb4c6
HK
7346 end if;
7347
7348 return Block_Nod;
2ba7e31e
AC
7349 end Wrap_Statements_In_Block;
7350
25b589cb
AC
7351 -- Local variables
7352
7353 Block : Node_Id;
7354
2ba7e31e
AC
7355 -- Start of processing for Process_Statements_For_Controlled_Objects
7356
7357 begin
25b589cb
AC
7358 -- Whenever a non-handled statement list is wrapped in a block, the
7359 -- block must be explicitly analyzed to redecorate all entities in the
7360 -- list and ensure that a finalizer is properly built.
7361
2ba7e31e 7362 case Nkind (N) is
15d8a51d
AC
7363 when N_Elsif_Part |
7364 N_If_Statement |
7365 N_Conditional_Entry_Call |
7366 N_Selective_Accept =>
2ba7e31e
AC
7367
7368 -- Check the "then statements" for elsif parts and if statements
7369
15d8a51d 7370 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
2ba7e31e
AC
7371 and then not Is_Empty_List (Then_Statements (N))
7372 and then not Are_Wrapped (Then_Statements (N))
7373 and then Requires_Cleanup_Actions
7374 (Then_Statements (N), False, False)
7375 then
25b589cb
AC
7376 Block := Wrap_Statements_In_Block (Then_Statements (N));
7377 Set_Then_Statements (N, New_List (Block));
7378
7379 Analyze (Block);
2ba7e31e
AC
7380 end if;
7381
7382 -- Check the "else statements" for conditional entry calls, if
7383 -- statements and selective accepts.
7384
7385 if Nkind_In (N, N_Conditional_Entry_Call,
7386 N_If_Statement,
7387 N_Selective_Accept)
7388 and then not Is_Empty_List (Else_Statements (N))
7389 and then not Are_Wrapped (Else_Statements (N))
7390 and then Requires_Cleanup_Actions
7391 (Else_Statements (N), False, False)
7392 then
25b589cb
AC
7393 Block := Wrap_Statements_In_Block (Else_Statements (N));
7394 Set_Else_Statements (N, New_List (Block));
7395
7396 Analyze (Block);
2ba7e31e
AC
7397 end if;
7398
7399 when N_Abortable_Part |
7400 N_Accept_Alternative |
7401 N_Case_Statement_Alternative |
7402 N_Delay_Alternative |
7403 N_Entry_Call_Alternative |
7404 N_Exception_Handler |
7405 N_Loop_Statement |
7406 N_Triggering_Alternative =>
7407
7408 if not Is_Empty_List (Statements (N))
7409 and then not Are_Wrapped (Statements (N))
7410 and then Requires_Cleanup_Actions (Statements (N), False, False)
7411 then
7edfb4c6
HK
7412 if Nkind (N) = N_Loop_Statement
7413 and then Present (Identifier (N))
7414 then
7415 Block :=
7416 Wrap_Statements_In_Block
7417 (L => Statements (N),
7418 Scop => Entity (Identifier (N)));
7419 else
7420 Block := Wrap_Statements_In_Block (Statements (N));
7421 end if;
25b589cb 7422
7edfb4c6 7423 Set_Statements (N, New_List (Block));
25b589cb 7424 Analyze (Block);
2ba7e31e
AC
7425 end if;
7426
02bb0765 7427 when others =>
2ba7e31e
AC
7428 null;
7429 end case;
7430 end Process_Statements_For_Controlled_Objects;
7431
2c9f8c0a
AC
7432 ------------------
7433 -- Power_Of_Two --
7434 ------------------
7435
7436 function Power_Of_Two (N : Node_Id) return Nat is
7437 Typ : constant Entity_Id := Etype (N);
7438 pragma Assert (Is_Integer_Type (Typ));
02bb0765 7439
2c9f8c0a
AC
7440 Siz : constant Nat := UI_To_Int (Esize (Typ));
7441 Val : Uint;
7442
7443 begin
7444 if not Compile_Time_Known_Value (N) then
7445 return 0;
7446
7447 else
7448 Val := Expr_Value (N);
7449 for J in 1 .. Siz - 1 loop
7450 if Val = Uint_2 ** J then
7451 return J;
7452 end if;
7453 end loop;
7454
7455 return 0;
7456 end if;
7457 end Power_Of_Two;
7458
3a3af4c3
AC
7459 ----------------------
7460 -- Remove_Init_Call --
7461 ----------------------
7462
7463 function Remove_Init_Call
7464 (Var : Entity_Id;
7465 Rep_Clause : Node_Id) return Node_Id
7466 is
7467 Par : constant Node_Id := Parent (Var);
7468 Typ : constant Entity_Id := Etype (Var);
7469
7470 Init_Proc : Entity_Id;
7471 -- Initialization procedure for Typ
7472
7473 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
7474 -- Look for init call for Var starting at From and scanning the
7475 -- enclosing list until Rep_Clause or the end of the list is reached.
7476
7477 ----------------------------
7478 -- Find_Init_Call_In_List --
7479 ----------------------------
7480
7481 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
7482 Init_Call : Node_Id;
ae05cdd6 7483
3a3af4c3
AC
7484 begin
7485 Init_Call := From;
3a3af4c3
AC
7486 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
7487 if Nkind (Init_Call) = N_Procedure_Call_Statement
7488 and then Is_Entity_Name (Name (Init_Call))
7489 and then Entity (Name (Init_Call)) = Init_Proc
7490 then
7491 return Init_Call;
7492 end if;
7493
7494 Next (Init_Call);
7495 end loop;
7496
7497 return Empty;
7498 end Find_Init_Call_In_List;
7499
7500 Init_Call : Node_Id;
7501
7502 -- Start of processing for Find_Init_Call
7503
7504 begin
7505 if Present (Initialization_Statements (Var)) then
7506 Init_Call := Initialization_Statements (Var);
7507 Set_Initialization_Statements (Var, Empty);
7508
7509 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
7510
7511 -- No init proc for the type, so obviously no call to be found
7512
7513 return Empty;
7514
7515 else
7516 -- We might be able to handle other cases below by just properly
7517 -- setting Initialization_Statements at the point where the init proc
7518 -- call is generated???
7519
7520 Init_Proc := Base_Init_Proc (Typ);
7521
7522 -- First scan the list containing the declaration of Var
7523
7524 Init_Call := Find_Init_Call_In_List (From => Next (Par));
7525
7526 -- If not found, also look on Var's freeze actions list, if any,
7527 -- since the init call may have been moved there (case of an address
7528 -- clause applying to Var).
7529
7530 if No (Init_Call) and then Present (Freeze_Node (Var)) then
7531 Init_Call :=
7532 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
7533 end if;
7534
7535 -- If the initialization call has actuals that use the secondary
7536 -- stack, the call may have been wrapped into a temporary block, in
7537 -- which case the block itself has to be removed.
7538
7539 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
7540 declare
7541 Blk : constant Node_Id := Next (Par);
7542 begin
7543 if Present
7544 (Find_Init_Call_In_List
7545 (First (Statements (Handled_Statement_Sequence (Blk)))))
7546 then
7547 Init_Call := Blk;
7548 end if;
7549 end;
7550 end if;
7551 end if;
7552
7553 if Present (Init_Call) then
7554 Remove (Init_Call);
7555 end if;
7556 return Init_Call;
7557 end Remove_Init_Call;
7558
70482933
RK
7559 -------------------------
7560 -- Remove_Side_Effects --
7561 -------------------------
7562
7563 procedure Remove_Side_Effects
89d3b1a1
AC
7564 (Exp : Node_Id;
7565 Name_Req : Boolean := False;
7566 Renaming_Req : Boolean := False;
7567 Variable_Ref : Boolean := False;
7568 Related_Id : Entity_Id := Empty;
7569 Is_Low_Bound : Boolean := False;
7570 Is_High_Bound : Boolean := False;
7571 Check_Side_Effects : Boolean := True)
70482933 7572 is
2934b84a
AC
7573 function Build_Temporary
7574 (Loc : Source_Ptr;
7575 Id : Character;
7576 Related_Nod : Node_Id := Empty) return Entity_Id;
09edc2c2
AC
7577 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
7578 -- is present (xxx is taken from the Chars field of Related_Nod),
7579 -- otherwise it generates an internal temporary.
2934b84a 7580
22e89283
AC
7581 function Is_Name_Reference (N : Node_Id) return Boolean;
7582 -- Determine if the tree referenced by N represents a name. This is
7583 -- similar to Is_Object_Reference but returns true only if N can be
7584 -- renamed without the need for a temporary, the typical example of
7585 -- an object not in this category being a function call.
7586
2934b84a
AC
7587 ---------------------
7588 -- Build_Temporary --
7589 ---------------------
7590
7591 function Build_Temporary
7592 (Loc : Source_Ptr;
7593 Id : Character;
7594 Related_Nod : Node_Id := Empty) return Entity_Id
7595 is
7596 Temp_Nam : Name_Id;
7597
7598 begin
7599 -- The context requires an external symbol
7600
7601 if Present (Related_Id) then
7602 if Is_Low_Bound then
7603 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
7604 else pragma Assert (Is_High_Bound);
7605 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
7606 end if;
7607
7608 return Make_Defining_Identifier (Loc, Temp_Nam);
7609
7610 -- Otherwise generate an internal temporary
7611
7612 else
7613 return Make_Temporary (Loc, Id, Related_Nod);
7614 end if;
7615 end Build_Temporary;
7616
22e89283
AC
7617 -----------------------
7618 -- Is_Name_Reference --
7619 -----------------------
7620
7621 function Is_Name_Reference (N : Node_Id) return Boolean is
7622 begin
7623 if Is_Entity_Name (N) then
7624 return Present (Entity (N)) and then Is_Object (Entity (N));
7625 end if;
7626
7627 case Nkind (N) is
7628 when N_Indexed_Component | N_Slice =>
7629 return
7630 Is_Name_Reference (Prefix (N))
7631 or else Is_Access_Type (Etype (Prefix (N)));
7632
7633 -- Attributes 'Input, 'Old and 'Result produce objects
7634
7635 when N_Attribute_Reference =>
7636 return
7637 Nam_In
7638 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
7639
7640 when N_Selected_Component =>
7641 return
7642 Is_Name_Reference (Selector_Name (N))
7643 and then
7644 (Is_Name_Reference (Prefix (N))
7645 or else Is_Access_Type (Etype (Prefix (N))));
7646
7647 when N_Explicit_Dereference =>
7648 return True;
7649
7650 -- A view conversion of a tagged name is a name reference
7651
7652 when N_Type_Conversion =>
7653 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
7654 and then Is_Tagged_Type (Etype (Expression (N)))
7655 and then Is_Name_Reference (Expression (N));
7656
7657 -- An unchecked type conversion is considered to be a name if
7658 -- the operand is a name (this construction arises only as a
7659 -- result of expansion activities).
7660
7661 when N_Unchecked_Type_Conversion =>
7662 return Is_Name_Reference (Expression (N));
7663
7664 when others =>
7665 return False;
7666 end case;
7667 end Is_Name_Reference;
7668
2934b84a
AC
7669 -- Local variables
7670
3217f71e
AC
7671 Loc : constant Source_Ptr := Sloc (Exp);
7672 Exp_Type : constant Entity_Id := Etype (Exp);
7673 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
70482933 7674 Def_Id : Entity_Id;
cc570be6
AC
7675 E : Node_Id;
7676 New_Exp : Node_Id;
7677 Ptr_Typ_Decl : Node_Id;
70482933
RK
7678 Ref_Type : Entity_Id;
7679 Res : Node_Id;
70482933 7680
2934b84a
AC
7681 -- Start of processing for Remove_Side_Effects
7682
70482933 7683 begin
f5da7a97
YM
7684 -- Handle cases in which there is nothing to do. In GNATprove mode,
7685 -- removal of side effects is useful for the light expansion of
a6ce7e76
AC
7686 -- renamings. This removal should only occur when not inside a
7687 -- generic and not doing a pre-analysis.
c269a1f5 7688
a6ce7e76
AC
7689 if not Expander_Active
7690 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
7691 then
cae81f17 7692 return;
5328a91d 7693 end if;
cae81f17
JM
7694
7695 -- Cannot generate temporaries if the invocation to remove side effects
7696 -- was issued too early and the type of the expression is not resolved
7697 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
7698 -- Remove_Side_Effects).
7699
bb012790 7700 if No (Exp_Type) or else Ekind (Exp_Type) = E_Access_Attribute_Type then
cae81f17
JM
7701 return;
7702
7703 -- No action needed for side-effect free expressions
70482933 7704
89d3b1a1
AC
7705 elsif Check_Side_Effects
7706 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
7707 then
70482933
RK
7708 return;
7709 end if;
7710
22e89283 7711 -- The remaining processing is done with all checks suppressed
67b8ac46
AC
7712
7713 -- Note: from now on, don't use return statements, instead do a goto
7714 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
70482933 7715
a7f1b24f 7716 Scope_Suppress.Suppress := (others => True);
70482933 7717
34da9c98
EB
7718 -- If this is an elementary or a small not by-reference record type, and
7719 -- we need to capture the value, just make a constant; this is cheap and
7720 -- objects of both kinds of types can be bit aligned, so it might not be
7721 -- possible to generate a reference to them. Likewise if this is not a
7722 -- name reference, except for a type conversion because we would enter
7723 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
7724 -- type has predicates (and type conversions need a specific treatment
7725 -- anyway, see below). Also do it if we have a volatile reference and
7726 -- Name_Req is not set (see comments for Side_Effect_Free).
7727
7728 if (Is_Elementary_Type (Exp_Type)
7729 or else (Is_Record_Type (Exp_Type)
7730 and then Known_Static_RM_Size (Exp_Type)
7731 and then RM_Size (Exp_Type) <= 64
7732 and then not Has_Discriminants (Exp_Type)
7733 and then not Is_By_Reference_Type (Exp_Type)))
d9e0a587 7734 and then (Variable_Ref
22e89283
AC
7735 or else (not Is_Name_Reference (Exp)
7736 and then Nkind (Exp) /= N_Type_Conversion)
365c8496
RD
7737 or else (not Name_Req
7738 and then Is_Volatile_Reference (Exp)))
d9e0a587 7739 then
2934b84a 7740 Def_Id := Build_Temporary (Loc, 'R', Exp);
d9e0a587 7741 Set_Etype (Def_Id, Exp_Type);
e4494292 7742 Res := New_Occurrence_Of (Def_Id, Loc);
d9e0a587 7743
273adcdf
AC
7744 -- If the expression is a packed reference, it must be reanalyzed and
7745 -- expanded, depending on context. This is the case for actuals where
7746 -- a constraint check may capture the actual before expansion of the
7747 -- call is complete.
8cce3d75
AC
7748
7749 if Nkind (Exp) = N_Indexed_Component
7750 and then Is_Packed (Etype (Prefix (Exp)))
7751 then
7752 Set_Analyzed (Exp, False);
7753 Set_Analyzed (Prefix (Exp), False);
7754 end if;
7755
a43f6434
AC
7756 -- Generate:
7757 -- Rnn : Exp_Type renames Expr;
7758
7759 if Renaming_Req then
7760 E :=
7761 Make_Object_Renaming_Declaration (Loc,
7762 Defining_Identifier => Def_Id,
7763 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7764 Name => Relocate_Node (Exp));
7765
7766 -- Generate:
7767 -- Rnn : constant Exp_Type := Expr;
7768
7769 else
7770 E :=
7771 Make_Object_Declaration (Loc,
7772 Defining_Identifier => Def_Id,
7773 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7774 Constant_Present => True,
7775 Expression => Relocate_Node (Exp));
7776
7777 Set_Assignment_OK (E);
7778 end if;
d9e0a587 7779
d9e0a587
EB
7780 Insert_Action (Exp, E);
7781
273adcdf 7782 -- If the expression has the form v.all then we can just capture the
bb012790
AC
7783 -- pointer, and then do an explicit dereference on the result, but
7784 -- this is not right if this is a volatile reference.
70482933 7785
bb012790
AC
7786 elsif Nkind (Exp) = N_Explicit_Dereference
7787 and then not Is_Volatile_Reference (Exp)
7788 then
2934b84a 7789 Def_Id := Build_Temporary (Loc, 'R', Exp);
70482933 7790 Res :=
e4494292 7791 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
70482933
RK
7792
7793 Insert_Action (Exp,
7794 Make_Object_Declaration (Loc,
7795 Defining_Identifier => Def_Id,
7796 Object_Definition =>
e4494292 7797 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
70482933
RK
7798 Constant_Present => True,
7799 Expression => Relocate_Node (Prefix (Exp))));
7800
273adcdf
AC
7801 -- Similar processing for an unchecked conversion of an expression of
7802 -- the form v.all, where we want the same kind of treatment.
fbf5a39b
AC
7803
7804 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7805 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
7806 then
8adcacef 7807 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
67b8ac46 7808 goto Leave;
fbf5a39b 7809
70482933
RK
7810 -- If this is a type conversion, leave the type conversion and remove
7811 -- the side effects in the expression. This is important in several
273adcdf
AC
7812 -- circumstances: for change of representations, and also when this is a
7813 -- view conversion to a smaller object, where gigi can end up creating
65df5b71 7814 -- its own temporary of the wrong size.
c9a4817d 7815
59e54267 7816 elsif Nkind (Exp) = N_Type_Conversion then
8adcacef 7817 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
6905a049
AC
7818
7819 -- Generating C code the type conversion of an access to constrained
7820 -- array type into an access to unconstrained array type involves
7821 -- initializing a fat pointer and the expression must be free of
7822 -- side effects to safely compute its bounds.
7823
7824 if Generate_C_Code
7825 and then Is_Access_Type (Etype (Exp))
7826 and then Is_Array_Type (Designated_Type (Etype (Exp)))
7827 and then not Is_Constrained (Designated_Type (Etype (Exp)))
7828 then
7829 Def_Id := Build_Temporary (Loc, 'R', Exp);
7830 Set_Etype (Def_Id, Exp_Type);
7831 Res := New_Occurrence_Of (Def_Id, Loc);
7832
7833 Insert_Action (Exp,
7834 Make_Object_Declaration (Loc,
7835 Defining_Identifier => Def_Id,
7836 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7837 Constant_Present => True,
7838 Expression => Relocate_Node (Exp)));
7839 else
7840 goto Leave;
7841 end if;
70482933 7842
d9e0a587
EB
7843 -- If this is an unchecked conversion that Gigi can't handle, make
7844 -- a copy or a use a renaming to capture the value.
7845
7846 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7847 and then not Safe_Unchecked_Type_Conversion (Exp)
7848 then
048e5cef 7849 if CW_Or_Has_Controlled_Part (Exp_Type) then
d9e0a587
EB
7850
7851 -- Use a renaming to capture the expression, rather than create
7852 -- a controlled temporary.
7853
2934b84a
AC
7854 Def_Id := Build_Temporary (Loc, 'R', Exp);
7855 Res := New_Occurrence_Of (Def_Id, Loc);
d9e0a587
EB
7856
7857 Insert_Action (Exp,
7858 Make_Object_Renaming_Declaration (Loc,
7859 Defining_Identifier => Def_Id,
e4494292 7860 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
d9e0a587
EB
7861 Name => Relocate_Node (Exp)));
7862
7863 else
2934b84a 7864 Def_Id := Build_Temporary (Loc, 'R', Exp);
d9e0a587 7865 Set_Etype (Def_Id, Exp_Type);
2934b84a 7866 Res := New_Occurrence_Of (Def_Id, Loc);
d9e0a587
EB
7867
7868 E :=
7869 Make_Object_Declaration (Loc,
7870 Defining_Identifier => Def_Id,
e4494292 7871 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
d9e0a587
EB
7872 Constant_Present => not Is_Variable (Exp),
7873 Expression => Relocate_Node (Exp));
7874
7875 Set_Assignment_OK (E);
7876 Insert_Action (Exp, E);
7877 end if;
7878
22e89283 7879 -- For expressions that denote names, we can use a renaming scheme.
18a2ad5d
AC
7880 -- This is needed for correctness in the case of a volatile object of
7881 -- a non-volatile type because the Make_Reference call of the "default"
273adcdf 7882 -- approach would generate an illegal access value (an access value
bb012790 7883 -- cannot designate such an object - see Analyze_Reference).
18a2ad5d 7884
22e89283 7885 elsif Is_Name_Reference (Exp)
bb012790
AC
7886
7887 -- We skip using this scheme if we have an object of a volatile
7888 -- type and we do not have Name_Req set true (see comments for
7889 -- Side_Effect_Free).
7890
676e8420 7891 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
70482933 7892 then
2934b84a 7893 Def_Id := Build_Temporary (Loc, 'R', Exp);
22e89283 7894 Res := New_Occurrence_Of (Def_Id, Loc);
70482933 7895
22e89283
AC
7896 Insert_Action (Exp,
7897 Make_Object_Renaming_Declaration (Loc,
7898 Defining_Identifier => Def_Id,
7899 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7900 Name => Relocate_Node (Exp)));
70482933 7901
273adcdf
AC
7902 -- If this is a packed reference, or a selected component with
7903 -- a non-standard representation, a reference to the temporary
7904 -- will be replaced by a copy of the original expression (see
65df5b71 7905 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
59e54267
ES
7906 -- elaborated by gigi, and is of course not to be replaced in-line
7907 -- by the expression it renames, which would defeat the purpose of
7908 -- removing the side-effect.
7909
0e564ab4 7910 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
59e54267
ES
7911 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
7912 then
7913 null;
7914 else
7915 Set_Is_Renaming_Of_Object (Def_Id, False);
7916 end if;
70482933 7917
22e89283
AC
7918 -- Avoid generating a variable-sized temporary, by generating the
7919 -- reference just for the function call. The transformation could be
7920 -- refined to apply only when the array component is constrained by a
7921 -- discriminant???
7922
7923 elsif Nkind (Exp) = N_Selected_Component
7924 and then Nkind (Prefix (Exp)) = N_Function_Call
7925 and then Is_Array_Type (Exp_Type)
7926 then
7927 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
7928 goto Leave;
7929
7930 -- Otherwise we generate a reference to the expression
70482933
RK
7931
7932 else
06b599fd
YM
7933 -- An expression which is in SPARK mode is considered side effect
7934 -- free if the resulting value is captured by a variable or a
c7518e6f 7935 -- constant.
c269a1f5 7936
c7518e6f 7937 if GNATprove_Mode
06b599fd
YM
7938 and then Nkind (Parent (Exp)) = N_Object_Declaration
7939 then
67b8ac46 7940 goto Leave;
c7518e6f
AC
7941
7942 -- When generating C code we cannot consider side effect free object
7943 -- declarations that have discriminants and are initialized by means
7944 -- of a function call since on this target there is no secondary
7945 -- stack to store the return value and the expander may generate an
7946 -- extra call to the function to compute the discriminant value. In
7947 -- addition, for targets that have secondary stack, the expansion of
7948 -- functions with side effects involves the generation of an access
7949 -- type to capture the return value stored in the secondary stack;
7950 -- by contrast when generating C code such expansion generates an
7951 -- internal object declaration (no access type involved) which must
7952 -- be identified here to avoid entering into a never-ending loop
7953 -- generating internal object declarations.
7954
7955 elsif Generate_C_Code
7956 and then Nkind (Parent (Exp)) = N_Object_Declaration
7957 and then
7958 (Nkind (Exp) /= N_Function_Call
7959 or else not Has_Discriminants (Exp_Type)
7960 or else Is_Internal_Name
7961 (Chars (Defining_Identifier (Parent (Exp)))))
7962 then
7963 goto Leave;
c269a1f5
AC
7964 end if;
7965
01957849
AC
7966 -- Special processing for function calls that return a limited type.
7967 -- We need to build a declaration that will enable build-in-place
7968 -- expansion of the call. This is not done if the context is already
7969 -- an object declaration, to prevent infinite recursion.
65df5b71
HK
7970
7971 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
7972 -- to accommodate functions returning limited objects by reference.
7973
cc570be6
AC
7974 if Ada_Version >= Ada_2005
7975 and then Nkind (Exp) = N_Function_Call
51245e2d 7976 and then Is_Limited_View (Etype (Exp))
01957849 7977 and then Nkind (Parent (Exp)) /= N_Object_Declaration
65df5b71
HK
7978 then
7979 declare
faf387e1 7980 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
65df5b71
HK
7981 Decl : Node_Id;
7982
7983 begin
7984 Decl :=
7985 Make_Object_Declaration (Loc,
7986 Defining_Identifier => Obj,
7987 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7988 Expression => Relocate_Node (Exp));
327503f1 7989
65df5b71
HK
7990 Insert_Action (Exp, Decl);
7991 Set_Etype (Obj, Exp_Type);
7992 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
67b8ac46 7993 goto Leave;
65df5b71
HK
7994 end;
7995 end if;
7996
2934b84a 7997 Def_Id := Build_Temporary (Loc, 'R', Exp);
70482933 7998
c269a1f5
AC
7999 -- The regular expansion of functions with side effects involves the
8000 -- generation of an access type to capture the return value found on
06b599fd 8001 -- the secondary stack. Since SPARK (and why) cannot process access
c269a1f5
AC
8002 -- types, use a different approach which ignores the secondary stack
8003 -- and "copies" the returned object.
7b52257c
AC
8004 -- When generating C code, no need for a 'reference since the
8005 -- secondary stack is not supported.
cc570be6 8006
7b52257c 8007 if GNATprove_Mode or Generate_C_Code then
e4494292 8008 Res := New_Occurrence_Of (Def_Id, Loc);
c269a1f5
AC
8009 Ref_Type := Exp_Type;
8010
8011 -- Regular expansion utilizing an access type and 'reference
cc570be6 8012
c269a1f5
AC
8013 else
8014 Res :=
8015 Make_Explicit_Dereference (Loc,
e4494292 8016 Prefix => New_Occurrence_Of (Def_Id, Loc));
b2ab8c33 8017
c269a1f5
AC
8018 -- Generate:
8019 -- type Ann is access all <Exp_Type>;
cc570be6 8020
c269a1f5
AC
8021 Ref_Type := Make_Temporary (Loc, 'A');
8022
8023 Ptr_Typ_Decl :=
8024 Make_Full_Type_Declaration (Loc,
8025 Defining_Identifier => Ref_Type,
8026 Type_Definition =>
8027 Make_Access_To_Object_Definition (Loc,
8028 All_Present => True,
8029 Subtype_Indication =>
e4494292 8030 New_Occurrence_Of (Exp_Type, Loc)));
c269a1f5
AC
8031
8032 Insert_Action (Exp, Ptr_Typ_Decl);
8033 end if;
cc570be6
AC
8034
8035 E := Exp;
70482933
RK
8036 if Nkind (E) = N_Explicit_Dereference then
8037 New_Exp := Relocate_Node (Prefix (E));
365c8496 8038
70482933
RK
8039 else
8040 E := Relocate_Node (E);
cc570be6 8041
7b52257c
AC
8042 -- Do not generate a 'reference in SPARK mode or C generation
8043 -- since the access type is not created in the first place.
cc570be6 8044
7b52257c 8045 if GNATprove_Mode or Generate_C_Code then
cc570be6 8046 New_Exp := E;
03e1048e
AC
8047
8048 -- Otherwise generate reference, marking the value as non-null
8049 -- since we know it cannot be null and we don't want a check.
8050
cc570be6
AC
8051 else
8052 New_Exp := Make_Reference (Loc, E);
74cab21a 8053 Set_Is_Known_Non_Null (Def_Id);
cc570be6 8054 end if;
70482933
RK
8055 end if;
8056
f44fe430
RD
8057 if Is_Delayed_Aggregate (E) then
8058
8059 -- The expansion of nested aggregates is delayed until the
8060 -- enclosing aggregate is expanded. As aggregates are often
273adcdf
AC
8061 -- qualified, the predicate applies to qualified expressions as
8062 -- well, indicating that the enclosing aggregate has not been
8063 -- expanded yet. At this point the aggregate is part of a
8064 -- stand-alone declaration, and must be fully expanded.
f44fe430
RD
8065
8066 if Nkind (E) = N_Qualified_Expression then
8067 Set_Expansion_Delayed (Expression (E), False);
8068 Set_Analyzed (Expression (E), False);
8069 else
8070 Set_Expansion_Delayed (E, False);
8071 end if;
8072
70482933
RK
8073 Set_Analyzed (E, False);
8074 end if;
8075
c7518e6f
AC
8076 -- Generating C code of object declarations that have discriminants
8077 -- and are initialized by means of a function call we propagate the
8078 -- discriminants of the parent type to the internally built object.
8079 -- This is needed to avoid generating an extra call to the called
8080 -- function.
8081
8082 -- For example, if we generate here the following declaration, it
8083 -- will be expanded later adding an extra call to evaluate the value
8084 -- of the discriminant (needed to compute the size of the object).
8085 --
8086 -- type Rec (D : Integer) is ...
8087 -- Obj : constant Rec := SomeFunc;
8088
8089 if Generate_C_Code
8090 and then Nkind (Parent (Exp)) = N_Object_Declaration
8091 and then Has_Discriminants (Exp_Type)
8092 and then Nkind (Exp) = N_Function_Call
8093 then
8094 Insert_Action (Exp,
8095 Make_Object_Declaration (Loc,
8096 Defining_Identifier => Def_Id,
8097 Object_Definition => New_Copy_Tree
8098 (Object_Definition (Parent (Exp))),
8099 Constant_Present => True,
8100 Expression => New_Exp));
8101 else
8102 Insert_Action (Exp,
8103 Make_Object_Declaration (Loc,
8104 Defining_Identifier => Def_Id,
8105 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
8106 Constant_Present => True,
8107 Expression => New_Exp));
8108 end if;
70482933
RK
8109 end if;
8110
273adcdf
AC
8111 -- Preserve the Assignment_OK flag in all copies, since at least one
8112 -- copy may be used in a context where this flag must be set (otherwise
8113 -- why would the flag be set in the first place).
70482933
RK
8114
8115 Set_Assignment_OK (Res, Assignment_OK (Exp));
8116
8117 -- Finally rewrite the original expression and we are done
8118
8119 Rewrite (Exp, Res);
8120 Analyze_And_Resolve (Exp, Exp_Type);
67b8ac46
AC
8121
8122 <<Leave>>
70482933
RK
8123 Scope_Suppress := Svg_Suppress;
8124 end Remove_Side_Effects;
8125
f44fe430
RD
8126 ---------------------------
8127 -- Represented_As_Scalar --
8128 ---------------------------
8129
8130 function Represented_As_Scalar (T : Entity_Id) return Boolean is
8131 UT : constant Entity_Id := Underlying_Type (T);
8132 begin
8133 return Is_Scalar_Type (UT)
8134 or else (Is_Bit_Packed_Array (UT)
8ca597af 8135 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
f44fe430
RD
8136 end Represented_As_Scalar;
8137
87729e5a
AC
8138 ------------------------------
8139 -- Requires_Cleanup_Actions --
8140 ------------------------------
8141
fcf848c4
AC
8142 function Requires_Cleanup_Actions
8143 (N : Node_Id;
8144 Lib_Level : Boolean) return Boolean
8145 is
5f44f0d4
AC
8146 At_Lib_Level : constant Boolean :=
8147 Lib_Level
8148 and then Nkind_In (N, N_Package_Body,
8149 N_Package_Specification);
fcf848c4
AC
8150 -- N is at the library level if the top-most context is a package and
8151 -- the path taken to reach N does not inlcude non-package constructs.
87729e5a
AC
8152
8153 begin
8154 case Nkind (N) is
8155 when N_Accept_Statement |
8156 N_Block_Statement |
8157 N_Entry_Body |
8158 N_Package_Body |
8159 N_Protected_Body |
8160 N_Subprogram_Body |
8161 N_Task_Body =>
8162 return
fcf848c4 8163 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
87729e5a 8164 or else
5f44f0d4
AC
8165 (Present (Handled_Statement_Sequence (N))
8166 and then
8167 Requires_Cleanup_Actions
8168 (Statements (Handled_Statement_Sequence (N)),
8169 At_Lib_Level, True));
87729e5a
AC
8170
8171 when N_Package_Specification =>
8172 return
2ba7e31e 8173 Requires_Cleanup_Actions
fcf848c4 8174 (Visible_Declarations (N), At_Lib_Level, True)
2ba7e31e
AC
8175 or else
8176 Requires_Cleanup_Actions
fcf848c4 8177 (Private_Declarations (N), At_Lib_Level, True);
87729e5a
AC
8178
8179 when others =>
8180 return False;
8181 end case;
8182 end Requires_Cleanup_Actions;
8183
8184 ------------------------------
8185 -- Requires_Cleanup_Actions --
8186 ------------------------------
8187
8188 function Requires_Cleanup_Actions
2ba7e31e 8189 (L : List_Id;
fcf848c4 8190 Lib_Level : Boolean;
2ba7e31e 8191 Nested_Constructs : Boolean) return Boolean
87729e5a
AC
8192 is
8193 Decl : Node_Id;
8194 Expr : Node_Id;
8195 Obj_Id : Entity_Id;
8196 Obj_Typ : Entity_Id;
8197 Pack_Id : Entity_Id;
8198 Typ : Entity_Id;
8199
8200 begin
8201 if No (L)
8202 or else Is_Empty_List (L)
8203 then
8204 return False;
8205 end if;
8206
8207 Decl := First (L);
8208 while Present (Decl) loop
8209
8210 -- Library-level tagged types
8211
8212 if Nkind (Decl) = N_Full_Type_Declaration then
8213 Typ := Defining_Identifier (Decl);
8214
8636f52f
HK
8215 -- Ignored Ghost types do not need any cleanup actions because
8216 -- they will not appear in the final tree.
8217
8218 if Is_Ignored_Ghost_Entity (Typ) then
8219 null;
8220
8221 elsif Is_Tagged_Type (Typ)
87729e5a
AC
8222 and then Is_Library_Level_Entity (Typ)
8223 and then Convention (Typ) = Convention_Ada
8224 and then Present (Access_Disp_Table (Typ))
8225 and then RTE_Available (RE_Unregister_Tag)
87729e5a 8226 and then not Is_Abstract_Type (Typ)
8636f52f 8227 and then not No_Run_Time_Mode
87729e5a
AC
8228 then
8229 return True;
8230 end if;
8231
8232 -- Regular object declarations
8233
8234 elsif Nkind (Decl) = N_Object_Declaration then
8235 Obj_Id := Defining_Identifier (Decl);
8236 Obj_Typ := Base_Type (Etype (Obj_Id));
8237 Expr := Expression (Decl);
8238
8239 -- Bypass any form of processing for objects which have their
8240 -- finalization disabled. This applies only to objects at the
8241 -- library level.
8242
fcf848c4 8243 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
87729e5a
AC
8244 null;
8245
8246 -- Transient variables are treated separately in order to minimize
8247 -- the size of the generated code. See Exp_Ch7.Process_Transient_
8248 -- Objects.
8249
8250 elsif Is_Processed_Transient (Obj_Id) then
8251 null;
8252
8636f52f
HK
8253 -- Ignored Ghost objects do not need any cleanup actions because
8254 -- they will not appear in the final tree.
8255
8256 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
8257 null;
8258
1f8f3e6e
AC
8259 -- The expansion of iterator loops generates an object declaration
8260 -- where the Ekind is explicitly set to loop parameter. This is to
8261 -- ensure that the loop parameter behaves as a constant from user
8262 -- code point of view. Such object are never controlled and do not
8263 -- require cleanup actions. An iterator loop over a container of
8264 -- controlled objects does not produce such object declarations.
8265
8266 elsif Ekind (Obj_Id) = E_Loop_Parameter then
8267 return False;
8268
87729e5a 8269 -- The object is of the form:
3386e3ae 8270 -- Obj : [constant] Typ [:= Expr];
87729e5a 8271 --
3386e3ae
AC
8272 -- Do not process tag-to-class-wide conversions because they do
8273 -- not yield an object. Do not process the incomplete view of a
8274 -- deferred constant. Note that an object initialized by means
8275 -- of a build-in-place function call may appear as a deferred
8276 -- constant after expansion activities. These kinds of objects
8277 -- must be finalized.
87729e5a
AC
8278
8279 elsif not Is_Imported (Obj_Id)
8280 and then Needs_Finalization (Obj_Typ)
aab08130 8281 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
3386e3ae
AC
8282 and then not (Ekind (Obj_Id) = E_Constant
8283 and then not Has_Completion (Obj_Id)
8284 and then No (BIP_Initialization_Call (Obj_Id)))
87729e5a
AC
8285 then
8286 return True;
8287
8288 -- The object is of the form:
8289 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
8290 --
8291 -- Obj : Access_Typ :=
cdc96e3e 8292 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
87729e5a
AC
8293
8294 elsif Is_Access_Type (Obj_Typ)
8295 and then Needs_Finalization
8296 (Available_View (Designated_Type (Obj_Typ)))
8297 and then Present (Expr)
8298 and then
cdc96e3e
AC
8299 (Is_Secondary_Stack_BIP_Func_Call (Expr)
8300 or else
8301 (Is_Non_BIP_Func_Call (Expr)
8302 and then not Is_Related_To_Func_Return (Obj_Id)))
87729e5a
AC
8303 then
8304 return True;
8305
8306 -- Processing for "hook" objects generated for controlled
8307 -- transients declared inside an Expression_With_Actions.
8308
8309 elsif Is_Access_Type (Obj_Typ)
3cebd1c0
AC
8310 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
8311 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
c5c780e6 8312 N_Object_Declaration
3cebd1c0
AC
8313 then
8314 return True;
8315
9b16cb57
RD
8316 -- Processing for intermediate results of if expressions where
8317 -- one of the alternatives uses a controlled function call.
3cebd1c0
AC
8318
8319 elsif Is_Access_Type (Obj_Typ)
8320 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
8321 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
9ab5d86b 8322 N_Defining_Identifier
3cebd1c0
AC
8323 and then Present (Expr)
8324 and then Nkind (Expr) = N_Null
87729e5a
AC
8325 then
8326 return True;
8327
8328 -- Simple protected objects which use type System.Tasking.
8329 -- Protected_Objects.Protection to manage their locks should be
8330 -- treated as controlled since they require manual cleanup.
8331
8332 elsif Ekind (Obj_Id) = E_Variable
c5c780e6
HK
8333 and then (Is_Simple_Protected_Type (Obj_Typ)
8334 or else Has_Simple_Protected_Object (Obj_Typ))
87729e5a
AC
8335 then
8336 return True;
8337 end if;
8338
8339 -- Specific cases of object renamings
8340
aab08130 8341 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
87729e5a
AC
8342 Obj_Id := Defining_Identifier (Decl);
8343 Obj_Typ := Base_Type (Etype (Obj_Id));
8344
8345 -- Bypass any form of processing for objects which have their
8346 -- finalization disabled. This applies only to objects at the
8347 -- library level.
8348
fcf848c4 8349 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
87729e5a
AC
8350 null;
8351
8636f52f
HK
8352 -- Ignored Ghost object renamings do not need any cleanup actions
8353 -- because they will not appear in the final tree.
8354
8355 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
8356 null;
8357
87729e5a
AC
8358 -- Return object of a build-in-place function. This case is
8359 -- recognized and marked by the expansion of an extended return
8360 -- statement (see Expand_N_Extended_Return_Statement).
8361
8362 elsif Needs_Finalization (Obj_Typ)
8363 and then Is_Return_Object (Obj_Id)
3cebd1c0 8364 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
87729e5a
AC
8365 then
8366 return True;
aab08130 8367
a429e6b3
AC
8368 -- Detect a case where a source object has been initialized by
8369 -- a controlled function call or another object which was later
8370 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
aab08130 8371
a429e6b3
AC
8372 -- Obj1 : CW_Type := Src_Obj;
8373 -- Obj2 : CW_Type := Function_Call (...);
aab08130 8374
a429e6b3
AC
8375 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
8376 -- Tmp : ... := Function_Call (...)'reference;
8377 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
aab08130 8378
a429e6b3 8379 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
aab08130 8380 return True;
87729e5a
AC
8381 end if;
8382
d3f70b35
AC
8383 -- Inspect the freeze node of an access-to-controlled type and look
8384 -- for a delayed finalization master. This case arises when the
8385 -- freeze actions are inserted at a later time than the expansion of
8386 -- the context. Since Build_Finalizer is never called on a single
8387 -- construct twice, the master will be ultimately left out and never
8388 -- finalized. This is also needed for freeze actions of designated
8389 -- types themselves, since in some cases the finalization master is
8390 -- associated with a designated type's freeze node rather than that
8391 -- of the access type (see handling for freeze actions in
8392 -- Build_Finalization_Master).
87729e5a
AC
8393
8394 elsif Nkind (Decl) = N_Freeze_Entity
8395 and then Present (Actions (Decl))
8396 then
8397 Typ := Entity (Decl);
8398
8636f52f
HK
8399 -- Freeze nodes for ignored Ghost types do not need cleanup
8400 -- actions because they will never appear in the final tree.
8401
8402 if Is_Ignored_Ghost_Entity (Typ) then
8403 null;
8404
8405 elsif ((Is_Access_Type (Typ)
8406 and then not Is_Access_Subprogram_Type (Typ)
8407 and then Needs_Finalization
8408 (Available_View (Designated_Type (Typ))))
8409 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
8f66cda7 8410 and then Requires_Cleanup_Actions
fcf848c4 8411 (Actions (Decl), Lib_Level, Nested_Constructs)
87729e5a
AC
8412 then
8413 return True;
8414 end if;
8415
8416 -- Nested package declarations
8417
2ba7e31e
AC
8418 elsif Nested_Constructs
8419 and then Nkind (Decl) = N_Package_Declaration
8420 then
8636f52f 8421 Pack_Id := Defining_Entity (Decl);
87729e5a 8422
8636f52f
HK
8423 -- Do not inspect an ignored Ghost package because all code found
8424 -- within will not appear in the final tree.
87729e5a 8425
8636f52f
HK
8426 if Is_Ignored_Ghost_Entity (Pack_Id) then
8427 null;
8428
8429 elsif Ekind (Pack_Id) /= E_Generic_Package
8430 and then Requires_Cleanup_Actions
8431 (Specification (Decl), Lib_Level)
87729e5a
AC
8432 then
8433 return True;
8434 end if;
8435
8436 -- Nested package bodies
8437
0e564ab4 8438 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
87729e5a 8439
8636f52f
HK
8440 -- Do not inspect an ignored Ghost package body because all code
8441 -- found within will not appear in the final tree.
8442
8443 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
8444 null;
8445
8446 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
fcf848c4 8447 and then Requires_Cleanup_Actions (Decl, Lib_Level)
87729e5a
AC
8448 then
8449 return True;
8450 end if;
8636f52f
HK
8451
8452 elsif Nkind (Decl) = N_Block_Statement
8453 and then
8454
8455 -- Handle a rare case caused by a controlled transient variable
8456 -- created as part of a record init proc. The variable is wrapped
8457 -- in a block, but the block is not associated with a transient
8458 -- scope.
8459
8460 (Inside_Init_Proc
8461
8462 -- Handle the case where the original context has been wrapped in
8463 -- a block to avoid interference between exception handlers and
8464 -- At_End handlers. Treat the block as transparent and process its
8465 -- contents.
8466
8467 or else Is_Finalization_Wrapper (Decl))
8468 then
8469 if Requires_Cleanup_Actions (Decl, Lib_Level) then
8470 return True;
8471 end if;
87729e5a
AC
8472 end if;
8473
8474 Next (Decl);
8475 end loop;
8476
8477 return False;
8478 end Requires_Cleanup_Actions;
8479
70482933
RK
8480 ------------------------------------
8481 -- Safe_Unchecked_Type_Conversion --
8482 ------------------------------------
8483
273adcdf
AC
8484 -- Note: this function knows quite a bit about the exact requirements of
8485 -- Gigi with respect to unchecked type conversions, and its code must be
8486 -- coordinated with any changes in Gigi in this area.
70482933
RK
8487
8488 -- The above requirements should be documented in Sinfo ???
8489
8490 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
8491 Otyp : Entity_Id;
8492 Ityp : Entity_Id;
8493 Oalign : Uint;
8494 Ialign : Uint;
8495 Pexp : constant Node_Id := Parent (Exp);
8496
8497 begin
8498 -- If the expression is the RHS of an assignment or object declaration
0cc1540d 8499 -- we are always OK because there will always be a target.
70482933
RK
8500
8501 -- Object renaming declarations, (generated for view conversions of
8502 -- actuals in inlined calls), like object declarations, provide an
8503 -- explicit type, and are safe as well.
8504
8505 if (Nkind (Pexp) = N_Assignment_Statement
8506 and then Expression (Pexp) = Exp)
0e564ab4
AC
8507 or else Nkind_In (Pexp, N_Object_Declaration,
8508 N_Object_Renaming_Declaration)
70482933
RK
8509 then
8510 return True;
8511
273adcdf
AC
8512 -- If the expression is the prefix of an N_Selected_Component we should
8513 -- also be OK because GCC knows to look inside the conversion except if
8514 -- the type is discriminated. We assume that we are OK anyway if the
8515 -- type is not set yet or if it is controlled since we can't afford to
8516 -- introduce a temporary in this case.
70482933
RK
8517
8518 elsif Nkind (Pexp) = N_Selected_Component
0e564ab4 8519 and then Prefix (Pexp) = Exp
70482933
RK
8520 then
8521 if No (Etype (Pexp)) then
8522 return True;
8523 else
8524 return
8525 not Has_Discriminants (Etype (Pexp))
8526 or else Is_Constrained (Etype (Pexp));
8527 end if;
8528 end if;
8529
273adcdf
AC
8530 -- Set the output type, this comes from Etype if it is set, otherwise we
8531 -- take it from the subtype mark, which we assume was already fully
8532 -- analyzed.
70482933
RK
8533
8534 if Present (Etype (Exp)) then
8535 Otyp := Etype (Exp);
8536 else
8537 Otyp := Entity (Subtype_Mark (Exp));
8538 end if;
8539
0cc1540d
AC
8540 -- The input type always comes from the expression, and we assume this
8541 -- is indeed always analyzed, so we can simply get the Etype.
70482933
RK
8542
8543 Ityp := Etype (Expression (Exp));
8544
8545 -- Initialize alignments to unknown so far
8546
8547 Oalign := No_Uint;
8548 Ialign := No_Uint;
8549
273adcdf
AC
8550 -- Replace a concurrent type by its corresponding record type and each
8551 -- type by its underlying type and do the tests on those. The original
8552 -- type may be a private type whose completion is a concurrent type, so
8553 -- find the underlying type first.
70482933
RK
8554
8555 if Present (Underlying_Type (Otyp)) then
8556 Otyp := Underlying_Type (Otyp);
8557 end if;
8558
8559 if Present (Underlying_Type (Ityp)) then
8560 Ityp := Underlying_Type (Ityp);
8561 end if;
8562
8563 if Is_Concurrent_Type (Otyp) then
8564 Otyp := Corresponding_Record_Type (Otyp);
8565 end if;
8566
8567 if Is_Concurrent_Type (Ityp) then
8568 Ityp := Corresponding_Record_Type (Ityp);
8569 end if;
8570
8571 -- If the base types are the same, we know there is no problem since
8572 -- this conversion will be a noop.
8573
8574 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
8575 return True;
8576
6cdb2c6e
AC
8577 -- Same if this is an upwards conversion of an untagged type, and there
8578 -- are no constraints involved (could be more general???)
8579
8580 elsif Etype (Ityp) = Otyp
8581 and then not Is_Tagged_Type (Ityp)
8582 and then not Has_Discriminants (Ityp)
8583 and then No (First_Rep_Item (Base_Type (Ityp)))
8584 then
8585 return True;
8586
273adcdf
AC
8587 -- If the expression has an access type (object or subprogram) we assume
8588 -- that the conversion is safe, because the size of the target is safe,
8589 -- even if it is a record (which might be treated as having unknown size
8590 -- at this point).
4da17013
AC
8591
8592 elsif Is_Access_Type (Ityp) then
8593 return True;
8594
273adcdf
AC
8595 -- If the size of output type is known at compile time, there is never
8596 -- a problem. Note that unconstrained records are considered to be of
8597 -- known size, but we can't consider them that way here, because we are
8598 -- talking about the actual size of the object.
70482933 8599
273adcdf
AC
8600 -- We also make sure that in addition to the size being known, we do not
8601 -- have a case which might generate an embarrassingly large temp in
8602 -- stack checking mode.
70482933
RK
8603
8604 elsif Size_Known_At_Compile_Time (Otyp)
7324bf49
AC
8605 and then
8606 (not Stack_Checking_Enabled
0e564ab4 8607 or else not May_Generate_Large_Temp (Otyp))
70482933
RK
8608 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
8609 then
8610 return True;
8611
0cc1540d
AC
8612 -- If either type is tagged, then we know the alignment is OK so Gigi
8613 -- will be able to use pointer punning.
70482933
RK
8614
8615 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
8616 return True;
8617
273adcdf
AC
8618 -- If either type is a limited record type, we cannot do a copy, so say
8619 -- safe since there's nothing else we can do.
70482933
RK
8620
8621 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
8622 return True;
8623
8624 -- Conversions to and from packed array types are always ignored and
8625 -- hence are safe.
8626
8ca597af
RD
8627 elsif Is_Packed_Array_Impl_Type (Otyp)
8628 or else Is_Packed_Array_Impl_Type (Ityp)
70482933
RK
8629 then
8630 return True;
8631 end if;
8632
8633 -- The only other cases known to be safe is if the input type's
8634 -- alignment is known to be at least the maximum alignment for the
8635 -- target or if both alignments are known and the output type's
273adcdf 8636 -- alignment is no stricter than the input's. We can use the component
ff7b374b 8637 -- type alignment for an array if a type is an unpacked array type.
70482933
RK
8638
8639 if Present (Alignment_Clause (Otyp)) then
8640 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
8641
8642 elsif Is_Array_Type (Otyp)
8643 and then Present (Alignment_Clause (Component_Type (Otyp)))
8644 then
8645 Oalign := Expr_Value (Expression (Alignment_Clause
8646 (Component_Type (Otyp))));
8647 end if;
8648
8649 if Present (Alignment_Clause (Ityp)) then
8650 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
8651
8652 elsif Is_Array_Type (Ityp)
8653 and then Present (Alignment_Clause (Component_Type (Ityp)))
8654 then
8655 Ialign := Expr_Value (Expression (Alignment_Clause
8656 (Component_Type (Ityp))));
8657 end if;
8658
8659 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
8660 return True;
8661
c5c780e6
HK
8662 elsif Ialign /= No_Uint
8663 and then Oalign /= No_Uint
70482933
RK
8664 and then Ialign <= Oalign
8665 then
8666 return True;
8667
bebbff91 8668 -- Otherwise, Gigi cannot handle this and we must make a temporary
70482933
RK
8669
8670 else
8671 return False;
8672 end if;
70482933
RK
8673 end Safe_Unchecked_Type_Conversion;
8674
05350ac6
BD
8675 ---------------------------------
8676 -- Set_Current_Value_Condition --
8677 ---------------------------------
8678
8679 -- Note: the implementation of this procedure is very closely tied to the
8680 -- implementation of Get_Current_Value_Condition. Here we set required
8681 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
8682 -- them, so they must have a consistent view.
8683
8684 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
8685
8686 procedure Set_Entity_Current_Value (N : Node_Id);
8687 -- If N is an entity reference, where the entity is of an appropriate
8688 -- kind, then set the current value of this entity to Cnode, unless
8689 -- there is already a definite value set there.
8690
8691 procedure Set_Expression_Current_Value (N : Node_Id);
8692 -- If N is of an appropriate form, sets an appropriate entry in current
8693 -- value fields of relevant entities. Multiple entities can be affected
8694 -- in the case of an AND or AND THEN.
8695
8696 ------------------------------
8697 -- Set_Entity_Current_Value --
8698 ------------------------------
8699
8700 procedure Set_Entity_Current_Value (N : Node_Id) is
8701 begin
8702 if Is_Entity_Name (N) then
8703 declare
8704 Ent : constant Entity_Id := Entity (N);
8705
8706 begin
8707 -- Don't capture if not safe to do so
8708
8709 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
8710 return;
8711 end if;
8712
273adcdf
AC
8713 -- Here we have a case where the Current_Value field may need
8714 -- to be set. We set it if it is not already set to a compile
8715 -- time expression value.
05350ac6
BD
8716
8717 -- Note that this represents a decision that one condition
273adcdf
AC
8718 -- blots out another previous one. That's certainly right if
8719 -- they occur at the same level. If the second one is nested,
8720 -- then the decision is neither right nor wrong (it would be
8721 -- equally OK to leave the outer one in place, or take the new
8722 -- inner one. Really we should record both, but our data
8723 -- structures are not that elaborate.
05350ac6
BD
8724
8725 if Nkind (Current_Value (Ent)) not in N_Subexpr then
8726 Set_Current_Value (Ent, Cnode);
8727 end if;
8728 end;
8729 end if;
8730 end Set_Entity_Current_Value;
8731
8732 ----------------------------------
8733 -- Set_Expression_Current_Value --
8734 ----------------------------------
8735
8736 procedure Set_Expression_Current_Value (N : Node_Id) is
8737 Cond : Node_Id;
8738
8739 begin
8740 Cond := N;
8741
8742 -- Loop to deal with (ignore for now) any NOT operators present. The
8743 -- presence of NOT operators will be handled properly when we call
8744 -- Get_Current_Value_Condition.
8745
8746 while Nkind (Cond) = N_Op_Not loop
8747 Cond := Right_Opnd (Cond);
8748 end loop;
8749
8750 -- For an AND or AND THEN, recursively process operands
8751
8752 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
8753 Set_Expression_Current_Value (Left_Opnd (Cond));
8754 Set_Expression_Current_Value (Right_Opnd (Cond));
8755 return;
8756 end if;
8757
8758 -- Check possible relational operator
8759
8760 if Nkind (Cond) in N_Op_Compare then
8761 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
8762 Set_Entity_Current_Value (Left_Opnd (Cond));
8763 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
8764 Set_Entity_Current_Value (Right_Opnd (Cond));
8765 end if;
8766
064f4527
TQ
8767 elsif Nkind_In (Cond,
8768 N_Type_Conversion,
8769 N_Qualified_Expression,
8770 N_Expression_With_Actions)
8771 then
8772 Set_Expression_Current_Value (Expression (Cond));
8773
8774 -- Check possible boolean variable reference
05350ac6
BD
8775
8776 else
8777 Set_Entity_Current_Value (Cond);
8778 end if;
8779 end Set_Expression_Current_Value;
8780
8781 -- Start of processing for Set_Current_Value_Condition
8782
8783 begin
8784 Set_Expression_Current_Value (Condition (Cnode));
8785 end Set_Current_Value_Condition;
8786
70482933
RK
8787 --------------------------
8788 -- Set_Elaboration_Flag --
8789 --------------------------
8790
8791 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
8792 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 8793 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
70482933
RK
8794 Asn : Node_Id;
8795
8796 begin
fbf5a39b 8797 if Present (Ent) then
70482933
RK
8798
8799 -- Nothing to do if at the compilation unit level, because in this
8800 -- case the flag is set by the binder generated elaboration routine.
8801
8802 if Nkind (Parent (N)) = N_Compilation_Unit then
8803 null;
8804
8805 -- Here we do need to generate an assignment statement
8806
8807 else
8808 Check_Restriction (No_Elaboration_Code, N);
8809 Asn :=
8810 Make_Assignment_Statement (Loc,
fbf5a39b 8811 Name => New_Occurrence_Of (Ent, Loc),
824e9320 8812 Expression => Make_Integer_Literal (Loc, Uint_1));
70482933
RK
8813
8814 if Nkind (Parent (N)) = N_Subunit then
8815 Insert_After (Corresponding_Stub (Parent (N)), Asn);
8816 else
8817 Insert_After (N, Asn);
8818 end if;
8819
8820 Analyze (Asn);
fbf5a39b 8821
65df5b71
HK
8822 -- Kill current value indication. This is necessary because the
8823 -- tests of this flag are inserted out of sequence and must not
8824 -- pick up bogus indications of the wrong constant value.
fbf5a39b
AC
8825
8826 Set_Current_Value (Ent, Empty);
113a9fb6
AC
8827
8828 -- If the subprogram is in the current declarative part and
8829 -- 'access has been applied to it, generate an elaboration
8830 -- check at the beginning of the declarations of the body.
8831
8832 if Nkind (N) = N_Subprogram_Body
8833 and then Address_Taken (Spec_Id)
8834 and then
8835 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
8836 then
8837 declare
8838 Loc : constant Source_Ptr := Sloc (N);
8839 Decls : constant List_Id := Declarations (N);
8840 Chk : Node_Id;
8841
8842 begin
8843 -- No need to generate this check if first entry in the
8844 -- declaration list is a raise of Program_Error now.
8845
8846 if Present (Decls)
8847 and then Nkind (First (Decls)) = N_Raise_Program_Error
8848 then
8849 return;
8850 end if;
8851
8852 -- Otherwise generate the check
8853
8854 Chk :=
8855 Make_Raise_Program_Error (Loc,
8856 Condition =>
8857 Make_Op_Eq (Loc,
8858 Left_Opnd => New_Occurrence_Of (Ent, Loc),
8859 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
8860 Reason => PE_Access_Before_Elaboration);
8861
8862 if No (Decls) then
8863 Set_Declarations (N, New_List (Chk));
8864 else
8865 Prepend (Chk, Decls);
8866 end if;
8867
8868 Analyze (Chk);
8869 end;
8870 end if;
70482933
RK
8871 end if;
8872 end if;
8873 end Set_Elaboration_Flag;
8874
59e54267
ES
8875 ----------------------------
8876 -- Set_Renamed_Subprogram --
8877 ----------------------------
8878
8879 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
8880 begin
8881 -- If input node is an identifier, we can just reset it
8882
8883 if Nkind (N) = N_Identifier then
8884 Set_Chars (N, Chars (E));
8885 Set_Entity (N, E);
8886
8887 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
8888
8889 else
8890 declare
8891 CS : constant Boolean := Comes_From_Source (N);
8892 begin
7675ad4f 8893 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
59e54267
ES
8894 Set_Entity (N, E);
8895 Set_Comes_From_Source (N, CS);
8896 Set_Analyzed (N, True);
8897 end;
8898 end if;
8899 end Set_Renamed_Subprogram;
8900
adb252d8
AC
8901 ----------------------
8902 -- Side_Effect_Free --
8903 ----------------------
8904
8905 function Side_Effect_Free
8906 (N : Node_Id;
8907 Name_Req : Boolean := False;
8908 Variable_Ref : Boolean := False) return Boolean
8909 is
e7cff5af
RD
8910 Typ : constant Entity_Id := Etype (N);
8911 -- Result type of the expression
8912
adb252d8
AC
8913 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
8914 -- The argument N is a construct where the Prefix is dereferenced if it
8915 -- is an access type and the result is a variable. The call returns True
8916 -- if the construct is side effect free (not considering side effects in
8917 -- other than the prefix which are to be tested by the caller).
8918
8919 function Within_In_Parameter (N : Node_Id) return Boolean;
8920 -- Determines if N is a subcomponent of a composite in-parameter. If so,
8921 -- N is not side-effect free when the actual is global and modifiable
8922 -- indirectly from within a subprogram, because it may be passed by
8923 -- reference. The front-end must be conservative here and assume that
8924 -- this may happen with any array or record type. On the other hand, we
8925 -- cannot create temporaries for all expressions for which this
8926 -- condition is true, for various reasons that might require clearing up
8927 -- ??? For example, discriminant references that appear out of place, or
8928 -- spurious type errors with class-wide expressions. As a result, we
8929 -- limit the transformation to loop bounds, which is so far the only
8930 -- case that requires it.
8931
8932 -----------------------------
8933 -- Safe_Prefixed_Reference --
8934 -----------------------------
8935
8936 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
8937 begin
8938 -- If prefix is not side effect free, definitely not safe
8939
8940 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
8941 return False;
8942
8943 -- If the prefix is of an access type that is not access-to-constant,
8944 -- then this construct is a variable reference, which means it is to
8945 -- be considered to have side effects if Variable_Ref is set True.
8946
8947 elsif Is_Access_Type (Etype (Prefix (N)))
8948 and then not Is_Access_Constant (Etype (Prefix (N)))
8949 and then Variable_Ref
8950 then
8951 -- Exception is a prefix that is the result of a previous removal
8952 -- of side-effects.
8953
8954 return Is_Entity_Name (Prefix (N))
8955 and then not Comes_From_Source (Prefix (N))
8956 and then Ekind (Entity (Prefix (N))) = E_Constant
8957 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
8958
8959 -- If the prefix is an explicit dereference then this construct is a
8960 -- variable reference, which means it is to be considered to have
8961 -- side effects if Variable_Ref is True.
8962
8963 -- We do NOT exclude dereferences of access-to-constant types because
8964 -- we handle them as constant view of variables.
8965
8966 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
8967 and then Variable_Ref
8968 then
8969 return False;
8970
8971 -- Note: The following test is the simplest way of solving a complex
8972 -- problem uncovered by the following test (Side effect on loop bound
8973 -- that is a subcomponent of a global variable:
8974
8975 -- with Text_Io; use Text_Io;
8976 -- procedure Tloop is
8977 -- type X is
8978 -- record
8979 -- V : Natural := 4;
8980 -- S : String (1..5) := (others => 'a');
8981 -- end record;
8982 -- X1 : X;
8983
8984 -- procedure Modi;
8985
8986 -- generic
8987 -- with procedure Action;
8988 -- procedure Loop_G (Arg : X; Msg : String)
8989
8990 -- procedure Loop_G (Arg : X; Msg : String) is
8991 -- begin
8992 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
8993 -- & Natural'Image (Arg.V));
8994 -- for Index in 1 .. Arg.V loop
8995 -- Text_Io.Put_Line
8996 -- (Natural'Image (Index) & " " & Arg.S (Index));
8997 -- if Index > 2 then
8998 -- Modi;
8999 -- end if;
9000 -- end loop;
9001 -- Put_Line ("end loop_g " & Msg);
9002 -- end;
9003
9004 -- procedure Loop1 is new Loop_G (Modi);
9005 -- procedure Modi is
9006 -- begin
9007 -- X1.V := 1;
9008 -- Loop1 (X1, "from modi");
9009 -- end;
9010 --
9011 -- begin
9012 -- Loop1 (X1, "initial");
9013 -- end;
9014
9015 -- The output of the above program should be:
9016
9017 -- begin loop_g initial will loop till: 4
9018 -- 1 a
9019 -- 2 a
9020 -- 3 a
9021 -- begin loop_g from modi will loop till: 1
9022 -- 1 a
9023 -- end loop_g from modi
9024 -- 4 a
9025 -- begin loop_g from modi will loop till: 1
9026 -- 1 a
9027 -- end loop_g from modi
9028 -- end loop_g initial
9029
9030 -- If a loop bound is a subcomponent of a global variable, a
9031 -- modification of that variable within the loop may incorrectly
9032 -- affect the execution of the loop.
9033
9034 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
9035 and then Within_In_Parameter (Prefix (N))
9036 and then Variable_Ref
9037 then
9038 return False;
9039
9040 -- All other cases are side effect free
9041
9042 else
9043 return True;
9044 end if;
9045 end Safe_Prefixed_Reference;
9046
9047 -------------------------
9048 -- Within_In_Parameter --
9049 -------------------------
9050
9051 function Within_In_Parameter (N : Node_Id) return Boolean is
9052 begin
9053 if not Comes_From_Source (N) then
9054 return False;
9055
9056 elsif Is_Entity_Name (N) then
9057 return Ekind (Entity (N)) = E_In_Parameter;
9058
9059 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
9060 return Within_In_Parameter (Prefix (N));
9061
9062 else
9063 return False;
9064 end if;
9065 end Within_In_Parameter;
9066
9067 -- Start of processing for Side_Effect_Free
9068
9069 begin
bb012790
AC
9070 -- If volatile reference, always consider it to have side effects
9071
9072 if Is_Volatile_Reference (N) then
9073 return False;
9074 end if;
9075
adb252d8
AC
9076 -- Note on checks that could raise Constraint_Error. Strictly, if we
9077 -- take advantage of 11.6, these checks do not count as side effects.
9078 -- However, we would prefer to consider that they are side effects,
9079 -- since the backend CSE does not work very well on expressions which
9080 -- can raise Constraint_Error. On the other hand if we don't consider
9081 -- them to be side effect free, then we get some awkward expansions
9082 -- in -gnato mode, resulting in code insertions at a point where we
9083 -- do not have a clear model for performing the insertions.
9084
9085 -- Special handling for entity names
9086
9087 if Is_Entity_Name (N) then
9088
bb012790
AC
9089 -- A type reference is always side effect free
9090
9091 if Is_Type (Entity (N)) then
9092 return True;
9093
adb252d8
AC
9094 -- Variables are considered to be a side effect if Variable_Ref
9095 -- is set or if we have a volatile reference and Name_Req is off.
9096 -- If Name_Req is True then we can't help returning a name which
9097 -- effectively allows multiple references in any case.
9098
bb012790 9099 elsif Is_Variable (N, Use_Original_Node => False) then
adb252d8
AC
9100 return not Variable_Ref
9101 and then (not Is_Volatile_Reference (N) or else Name_Req);
9102
9103 -- Any other entity (e.g. a subtype name) is definitely side
9104 -- effect free.
9105
9106 else
9107 return True;
9108 end if;
9109
9110 -- A value known at compile time is always side effect free
9111
9112 elsif Compile_Time_Known_Value (N) then
9113 return True;
9114
9115 -- A variable renaming is not side-effect free, because the renaming
9116 -- will function like a macro in the front-end in some cases, and an
9117 -- assignment can modify the component designated by N, so we need to
9118 -- create a temporary for it.
9119
9120 -- The guard testing for Entity being present is needed at least in
9121 -- the case of rewritten predicate expressions, and may well also be
9122 -- appropriate elsewhere. Obviously we can't go testing the entity
9123 -- field if it does not exist, so it's reasonable to say that this is
9124 -- not the renaming case if it does not exist.
9125
9126 elsif Is_Entity_Name (Original_Node (N))
9127 and then Present (Entity (Original_Node (N)))
9128 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
9129 and then Ekind (Entity (Original_Node (N))) /= E_Constant
9130 then
9131 declare
9132 RO : constant Node_Id :=
9133 Renamed_Object (Entity (Original_Node (N)));
9134
9135 begin
9136 -- If the renamed object is an indexed component, or an
9137 -- explicit dereference, then the designated object could
9138 -- be modified by an assignment.
9139
9140 if Nkind_In (RO, N_Indexed_Component,
9141 N_Explicit_Dereference)
9142 then
9143 return False;
9144
9145 -- A selected component must have a safe prefix
9146
9147 elsif Nkind (RO) = N_Selected_Component then
9148 return Safe_Prefixed_Reference (RO);
9149
9150 -- In all other cases, designated object cannot be changed so
9151 -- we are side effect free.
9152
9153 else
9154 return True;
9155 end if;
9156 end;
9157
9158 -- Remove_Side_Effects generates an object renaming declaration to
9159 -- capture the expression of a class-wide expression. In VM targets
9160 -- the frontend performs no expansion for dispatching calls to
9161 -- class- wide types since they are handled by the VM. Hence, we must
9162 -- locate here if this node corresponds to a previous invocation of
9163 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
9164
535a8637 9165 elsif not Tagged_Type_Expansion
f145ece7
AC
9166 and then not Comes_From_Source (N)
9167 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
9168 and then Is_Class_Wide_Type (Typ)
adb252d8
AC
9169 then
9170 return True;
6905a049
AC
9171
9172 -- Generating C the type conversion of an access to constrained array
9173 -- type into an access to unconstrained array type involves initializing
9174 -- a fat pointer and the expression cannot be assumed to be free of side
9175 -- effects since it must referenced several times to compute its bounds.
9176
9177 elsif Generate_C_Code
9178 and then Nkind (N) = N_Type_Conversion
9179 and then Is_Access_Type (Typ)
9180 and then Is_Array_Type (Designated_Type (Typ))
9181 and then not Is_Constrained (Designated_Type (Typ))
9182 then
9183 return False;
adb252d8
AC
9184 end if;
9185
9186 -- For other than entity names and compile time known values,
9187 -- check the node kind for special processing.
9188
9189 case Nkind (N) is
9190
9191 -- An attribute reference is side effect free if its expressions
9192 -- are side effect free and its prefix is side effect free or
9193 -- is an entity reference.
9194
9195 -- Is this right? what about x'first where x is a variable???
9196
9197 when N_Attribute_Reference =>
9198 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
9199 and then Attribute_Name (N) /= Name_Input
9200 and then (Is_Entity_Name (Prefix (N))
9201 or else Side_Effect_Free
9202 (Prefix (N), Name_Req, Variable_Ref));
9203
9204 -- A binary operator is side effect free if and both operands are
9205 -- side effect free. For this purpose binary operators include
9206 -- membership tests and short circuit forms.
9207
9208 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
9209 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
9210 and then
9211 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
9212
9213 -- An explicit dereference is side effect free only if it is
9214 -- a side effect free prefixed reference.
9215
9216 when N_Explicit_Dereference =>
9217 return Safe_Prefixed_Reference (N);
9218
9219 -- An expression with action is side effect free if its expression
9220 -- is side effect free and it has no actions.
9221
9222 when N_Expression_With_Actions =>
9223 return Is_Empty_List (Actions (N))
9224 and then
9225 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9226
9227 -- A call to _rep_to_pos is side effect free, since we generate
9228 -- this pure function call ourselves. Moreover it is critically
9229 -- important to make this exception, since otherwise we can have
9230 -- discriminants in array components which don't look side effect
9231 -- free in the case of an array whose index type is an enumeration
9232 -- type with an enumeration rep clause.
9233
9234 -- All other function calls are not side effect free
9235
9236 when N_Function_Call =>
9237 return Nkind (Name (N)) = N_Identifier
9238 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
9239 and then
9240 Side_Effect_Free
9241 (First (Parameter_Associations (N)), Name_Req, Variable_Ref);
9242
e7cff5af
RD
9243 -- An IF expression is side effect free if it's of a scalar type, and
9244 -- all its components are all side effect free (conditions and then
9245 -- actions and else actions). We restrict to scalar types, since it
9246 -- is annoying to deal with things like (if A then B else C)'First
9247 -- where the type involved is a string type.
a767d69b 9248
e7cff5af
RD
9249 when N_If_Expression =>
9250 return Is_Scalar_Type (Typ)
9251 and then
9252 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref);
a767d69b 9253
adb252d8
AC
9254 -- An indexed component is side effect free if it is a side
9255 -- effect free prefixed reference and all the indexing
9256 -- expressions are side effect free.
9257
9258 when N_Indexed_Component =>
9259 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
9260 and then Safe_Prefixed_Reference (N);
9261
9262 -- A type qualification is side effect free if the expression
9263 -- is side effect free.
9264
9265 when N_Qualified_Expression =>
9266 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9267
9268 -- A selected component is side effect free only if it is a side
22e89283 9269 -- effect free prefixed reference.
adb252d8
AC
9270
9271 when N_Selected_Component =>
22e89283 9272 return Safe_Prefixed_Reference (N);
adb252d8
AC
9273
9274 -- A range is side effect free if the bounds are side effect free
9275
9276 when N_Range =>
9277 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
c5c780e6 9278 and then
adb252d8
AC
9279 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
9280
9281 -- A slice is side effect free if it is a side effect free
9282 -- prefixed reference and the bounds are side effect free.
9283
9284 when N_Slice =>
9285 return Side_Effect_Free
9286 (Discrete_Range (N), Name_Req, Variable_Ref)
9287 and then Safe_Prefixed_Reference (N);
9288
9289 -- A type conversion is side effect free if the expression to be
9290 -- converted is side effect free.
9291
9292 when N_Type_Conversion =>
9293 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9294
9295 -- A unary operator is side effect free if the operand
9296 -- is side effect free.
9297
9298 when N_Unary_Op =>
9299 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
9300
9301 -- An unchecked type conversion is side effect free only if it
9302 -- is safe and its argument is side effect free.
9303
9304 when N_Unchecked_Type_Conversion =>
9305 return Safe_Unchecked_Type_Conversion (N)
9306 and then
9307 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9308
9309 -- An unchecked expression is side effect free if its expression
9310 -- is side effect free.
9311
9312 when N_Unchecked_Expression =>
9313 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9314
9315 -- A literal is side effect free
9316
9317 when N_Character_Literal |
9318 N_Integer_Literal |
9319 N_Real_Literal |
9320 N_String_Literal =>
9321 return True;
9322
9323 -- We consider that anything else has side effects. This is a bit
9324 -- crude, but we are pretty close for most common cases, and we
9325 -- are certainly correct (i.e. we never return True when the
9326 -- answer should be False).
9327
9328 when others =>
9329 return False;
9330 end case;
9331 end Side_Effect_Free;
9332
9333 -- A list is side effect free if all elements of the list are side
9334 -- effect free.
9335
9336 function Side_Effect_Free
9337 (L : List_Id;
9338 Name_Req : Boolean := False;
9339 Variable_Ref : Boolean := False) return Boolean
9340 is
9341 N : Node_Id;
9342
9343 begin
9344 if L = No_List or else L = Error_List then
9345 return True;
9346
9347 else
9348 N := First (L);
9349 while Present (N) loop
9350 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
9351 return False;
9352 else
9353 Next (N);
9354 end if;
9355 end loop;
9356
9357 return True;
9358 end if;
9359 end Side_Effect_Free;
9360
65df5b71
HK
9361 ----------------------------------
9362 -- Silly_Boolean_Array_Not_Test --
9363 ----------------------------------
9364
9365 -- This procedure implements an odd and silly test. We explicitly check
9366 -- for the case where the 'First of the component type is equal to the
9367 -- 'Last of this component type, and if this is the case, we make sure
9368 -- that constraint error is raised. The reason is that the NOT is bound
9369 -- to cause CE in this case, and we will not otherwise catch it.
9370
b3b9865d
AC
9371 -- No such check is required for AND and OR, since for both these cases
9372 -- False op False = False, and True op True = True. For the XOR case,
9373 -- see Silly_Boolean_Array_Xor_Test.
9374
273adcdf
AC
9375 -- Believe it or not, this was reported as a bug. Note that nearly always,
9376 -- the test will evaluate statically to False, so the code will be
9377 -- statically removed, and no extra overhead caused.
65df5b71
HK
9378
9379 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
9380 Loc : constant Source_Ptr := Sloc (N);
9381 CT : constant Entity_Id := Component_Type (T);
9382
9383 begin
b3b9865d
AC
9384 -- The check we install is
9385
9386 -- constraint_error when
9387 -- component_type'first = component_type'last
9388 -- and then array_type'Length /= 0)
9389
9390 -- We need the last guard because we don't want to raise CE for empty
9391 -- arrays since no out of range values result. (Empty arrays with a
9392 -- component type of True .. True -- very useful -- even the ACATS
a90bd866 9393 -- does not test that marginal case).
b3b9865d 9394
65df5b71
HK
9395 Insert_Action (N,
9396 Make_Raise_Constraint_Error (Loc,
9397 Condition =>
b3b9865d 9398 Make_And_Then (Loc,
65df5b71 9399 Left_Opnd =>
b3b9865d
AC
9400 Make_Op_Eq (Loc,
9401 Left_Opnd =>
9402 Make_Attribute_Reference (Loc,
9403 Prefix => New_Occurrence_Of (CT, Loc),
9404 Attribute_Name => Name_First),
9405
9406 Right_Opnd =>
9407 Make_Attribute_Reference (Loc,
9408 Prefix => New_Occurrence_Of (CT, Loc),
9409 Attribute_Name => Name_Last)),
9410
9411 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
65df5b71
HK
9412 Reason => CE_Range_Check_Failed));
9413 end Silly_Boolean_Array_Not_Test;
9414
9415 ----------------------------------
9416 -- Silly_Boolean_Array_Xor_Test --
9417 ----------------------------------
9418
9419 -- This procedure implements an odd and silly test. We explicitly check
9420 -- for the XOR case where the component type is True .. True, since this
9421 -- will raise constraint error. A special check is required since CE
f17889b3 9422 -- will not be generated otherwise (cf Expand_Packed_Not).
65df5b71
HK
9423
9424 -- No such check is required for AND and OR, since for both these cases
b3b9865d
AC
9425 -- False op False = False, and True op True = True, and no check is
9426 -- required for the case of False .. False, since False xor False = False.
9427 -- See also Silly_Boolean_Array_Not_Test
65df5b71
HK
9428
9429 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
9430 Loc : constant Source_Ptr := Sloc (N);
9431 CT : constant Entity_Id := Component_Type (T);
65df5b71
HK
9432
9433 begin
f17889b3
RD
9434 -- The check we install is
9435
9436 -- constraint_error when
9437 -- Boolean (component_type'First)
9438 -- and then Boolean (component_type'Last)
9439 -- and then array_type'Length /= 0)
9440
9441 -- We need the last guard because we don't want to raise CE for empty
9442 -- arrays since no out of range values result (Empty arrays with a
9443 -- component type of True .. True -- very useful -- even the ACATS
a90bd866 9444 -- does not test that marginal case).
f17889b3 9445
65df5b71
HK
9446 Insert_Action (N,
9447 Make_Raise_Constraint_Error (Loc,
9448 Condition =>
f17889b3 9449 Make_And_Then (Loc,
65df5b71 9450 Left_Opnd =>
f17889b3 9451 Make_And_Then (Loc,
65df5b71 9452 Left_Opnd =>
f17889b3
RD
9453 Convert_To (Standard_Boolean,
9454 Make_Attribute_Reference (Loc,
9455 Prefix => New_Occurrence_Of (CT, Loc),
9456 Attribute_Name => Name_First)),
65df5b71
HK
9457
9458 Right_Opnd =>
f17889b3
RD
9459 Convert_To (Standard_Boolean,
9460 Make_Attribute_Reference (Loc,
9461 Prefix => New_Occurrence_Of (CT, Loc),
9462 Attribute_Name => Name_Last))),
65df5b71 9463
b3b9865d 9464 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
65df5b71
HK
9465 Reason => CE_Range_Check_Failed));
9466 end Silly_Boolean_Array_Xor_Test;
9467
fbf5a39b
AC
9468 --------------------------
9469 -- Target_Has_Fixed_Ops --
9470 --------------------------
9471
9472 Integer_Sized_Small : Ureal;
273adcdf 9473 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
a90bd866 9474 -- called (we don't want to compute it more than once).
fbf5a39b
AC
9475
9476 Long_Integer_Sized_Small : Ureal;
273adcdf
AC
9477 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
9478 -- is called (we don't want to compute it more than once)
fbf5a39b
AC
9479
9480 First_Time_For_THFO : Boolean := True;
9481 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
9482
9483 function Target_Has_Fixed_Ops
9484 (Left_Typ : Entity_Id;
9485 Right_Typ : Entity_Id;
bebbff91 9486 Result_Typ : Entity_Id) return Boolean
fbf5a39b
AC
9487 is
9488 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
9489 -- Return True if the given type is a fixed-point type with a small
9490 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
273adcdf
AC
9491 -- an absolute value less than 1.0. This is currently limited to
9492 -- fixed-point types that map to Integer or Long_Integer.
fbf5a39b
AC
9493
9494 ------------------------
9495 -- Is_Fractional_Type --
9496 ------------------------
9497
9498 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
9499 begin
9500 if Esize (Typ) = Standard_Integer_Size then
9501 return Small_Value (Typ) = Integer_Sized_Small;
9502
9503 elsif Esize (Typ) = Standard_Long_Integer_Size then
9504 return Small_Value (Typ) = Long_Integer_Sized_Small;
9505
9506 else
9507 return False;
9508 end if;
9509 end Is_Fractional_Type;
9510
9511 -- Start of processing for Target_Has_Fixed_Ops
9512
9513 begin
9514 -- Return False if Fractional_Fixed_Ops_On_Target is false
9515
9516 if not Fractional_Fixed_Ops_On_Target then
9517 return False;
9518 end if;
9519
9520 -- Here the target has Fractional_Fixed_Ops, if first time, compute
9521 -- standard constants used by Is_Fractional_Type.
9522
9523 if First_Time_For_THFO then
9524 First_Time_For_THFO := False;
9525
9526 Integer_Sized_Small :=
9527 UR_From_Components
9528 (Num => Uint_1,
9529 Den => UI_From_Int (Standard_Integer_Size - 1),
9530 Rbase => 2);
9531
9532 Long_Integer_Sized_Small :=
9533 UR_From_Components
9534 (Num => Uint_1,
9535 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
9536 Rbase => 2);
9537 end if;
9538
273adcdf
AC
9539 -- Return True if target supports fixed-by-fixed multiply/divide for
9540 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
9541 -- and result types are equivalent fractional types.
fbf5a39b
AC
9542
9543 return Is_Fractional_Type (Base_Type (Left_Typ))
9544 and then Is_Fractional_Type (Base_Type (Right_Typ))
9545 and then Is_Fractional_Type (Base_Type (Result_Typ))
9546 and then Esize (Left_Typ) = Esize (Right_Typ)
9547 and then Esize (Left_Typ) = Esize (Result_Typ);
9548 end Target_Has_Fixed_Ops;
9549
91b1417d
AC
9550 ------------------------------------------
9551 -- Type_May_Have_Bit_Aligned_Components --
9552 ------------------------------------------
9553
9554 function Type_May_Have_Bit_Aligned_Components
9555 (Typ : Entity_Id) return Boolean
9556 is
9557 begin
9558 -- Array type, check component type
9559
9560 if Is_Array_Type (Typ) then
9561 return
9562 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
9563
9564 -- Record type, check components
9565
9566 elsif Is_Record_Type (Typ) then
9567 declare
9568 E : Entity_Id;
9569
9570 begin
dee4682a 9571 E := First_Component_Or_Discriminant (Typ);
91b1417d 9572 while Present (E) loop
dee4682a
JM
9573 if Component_May_Be_Bit_Aligned (E)
9574 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
91b1417d 9575 then
dee4682a 9576 return True;
91b1417d
AC
9577 end if;
9578
dee4682a 9579 Next_Component_Or_Discriminant (E);
91b1417d
AC
9580 end loop;
9581
9582 return False;
9583 end;
9584
9585 -- Type other than array or record is always OK
9586
9587 else
9588 return False;
9589 end if;
9590 end Type_May_Have_Bit_Aligned_Components;
9591
4c7e0990
AC
9592 ----------------------------------
9593 -- Within_Case_Or_If_Expression --
9594 ----------------------------------
9595
9596 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
9597 Par : Node_Id;
9598
9599 begin
b2c28399
AC
9600 -- Locate an enclosing case or if expression. Note that these constructs
9601 -- can be expanded into Expression_With_Actions, hence the test of the
9602 -- original node.
4c7e0990 9603
b2c28399 9604 Par := Parent (N);
4c7e0990
AC
9605 while Present (Par) loop
9606 if Nkind_In (Original_Node (Par), N_Case_Expression,
9607 N_If_Expression)
9608 then
9609 return True;
9610
9611 -- Prevent the search from going too far
9612
a7e68e7f 9613 elsif Is_Body_Or_Package_Declaration (Par) then
4c7e0990
AC
9614 return False;
9615 end if;
9616
9617 Par := Parent (Par);
9618 end loop;
9619
9620 return False;
9621 end Within_Case_Or_If_Expression;
9622
8e1e62e3
AC
9623 --------------------------------
9624 -- Within_Internal_Subprogram --
9625 --------------------------------
9626
9627 function Within_Internal_Subprogram return Boolean is
9628 S : Entity_Id;
9629
9630 begin
9631 S := Current_Scope;
9632 while Present (S) and then not Is_Subprogram (S) loop
9633 S := Scope (S);
9634 end loop;
9635
9636 return Present (S)
9637 and then Get_TSS_Name (S) /= TSS_Null
a2c314c7
AC
9638 and then not Is_Predicate_Function (S)
9639 and then not Is_Predicate_Function_M (S);
8e1e62e3
AC
9640 end Within_Internal_Subprogram;
9641
70482933 9642end Exp_Util;