]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/exp_util.adb
2012-10-04 Vincent Celier <celier@adacore.com>
[thirdparty/gcc.git] / gcc / ada / exp_util.adb
CommitLineData
ee6ba406 1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- E X P _ U T I L --
6-- --
7-- B o d y --
8-- --
6daf2e31 9-- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
ee6ba406 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- --
80df182a 13-- ware Foundation; either version 3, or (at your option) any later ver- --
ee6ba406 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 --
80df182a 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. --
ee6ba406 20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
e78e8c8e 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
ee6ba406 23-- --
24------------------------------------------------------------------------------
25
2fc9b6cd 26with Aspects; use Aspects;
ee6ba406 27with Atree; use Atree;
c6a30f24 28with Casing; use Casing;
ee6ba406 29with Checks; use Checks;
35889f1e 30with Debug; use Debug;
ee6ba406 31with Einfo; use Einfo;
32with Elists; use Elists;
33with Errout; use Errout;
ba40b4af 34with Exp_Aggr; use Exp_Aggr;
0cd40f50 35with Exp_Ch6; use Exp_Ch6;
ee6ba406 36with Exp_Ch7; use Exp_Ch7;
ee6ba406 37with Inline; use Inline;
38with Itypes; use Itypes;
39with Lib; use Lib;
ee6ba406 40with Nlists; use Nlists;
41with Nmake; use Nmake;
42with Opt; use Opt;
43with Restrict; use Restrict;
1e16c51c 44with Rident; use Rident;
ee6ba406 45with Sem; use Sem;
d60c9ff7 46with Sem_Aux; use Sem_Aux;
ee6ba406 47with Sem_Ch8; use Sem_Ch8;
48with Sem_Eval; use Sem_Eval;
5b5df4a9 49with Sem_Prag; use Sem_Prag;
ee6ba406 50with Sem_Res; use Sem_Res;
aad6babd 51with Sem_Type; use Sem_Type;
ee6ba406 52with Sem_Util; use Sem_Util;
9dfe12ae 53with Snames; use Snames;
ee6ba406 54with Stand; use Stand;
55with Stringt; use Stringt;
f15731c4 56with Targparm; use Targparm;
ee6ba406 57with Tbuild; use Tbuild;
58with Ttypes; use Ttypes;
f15731c4 59with Urealp; use Urealp;
ee6ba406 60with Validsw; use Validsw;
61
62package body Exp_Util is
63
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
67
68 function Build_Task_Array_Image
69 (Loc : Source_Ptr;
70 Id_Ref : Node_Id;
71b30311 71 A_Type : Entity_Id;
23e83313 72 Dyn : Boolean := False) return Node_Id;
dc74650f 73 -- Build function to generate the image string for a task that is an array
74 -- component, concatenating the images of each index. To avoid storage
75 -- leaks, the string is built with successive slice assignments. The flag
76 -- Dyn indicates whether this is called for the initialization procedure of
77 -- an array of tasks, or for the name of a dynamically created task that is
78 -- assigned to an indexed component.
ee6ba406 79
80 function Build_Task_Image_Function
81 (Loc : Source_Ptr;
82 Decls : List_Id;
83 Stats : List_Id;
23e83313 84 Res : Entity_Id) return Node_Id;
dc74650f 85 -- Common processing for Task_Array_Image and Task_Record_Image. Build
86 -- function body that computes image.
ee6ba406 87
88 procedure Build_Task_Image_Prefix
89 (Loc : Source_Ptr;
90 Len : out Entity_Id;
91 Res : out Entity_Id;
92 Pos : out Entity_Id;
93 Prefix : Entity_Id;
94 Sum : Node_Id;
0cd40f50 95 Decls : List_Id;
96 Stats : List_Id);
dc74650f 97 -- Common processing for Task_Array_Image and Task_Record_Image. Create
98 -- local variables and assign prefix of name to result string.
ee6ba406 99
100 function Build_Task_Record_Image
101 (Loc : Source_Ptr;
102 Id_Ref : Node_Id;
23e83313 103 Dyn : Boolean := False) return Node_Id;
dc74650f 104 -- Build function to generate the image string for a task that is a record
105 -- component. Concatenate name of variable with that of selector. The flag
106 -- Dyn indicates whether this is called for the initialization procedure of
107 -- record with task components, or for a dynamically created task that is
108 -- assigned to a selected component.
ee6ba406 109
110 function Make_CW_Equivalent_Type
23e83313 111 (T : Entity_Id;
112 E : Node_Id) return Entity_Id;
ee6ba406 113 -- T is a class-wide type entity, E is the initial expression node that
dc74650f 114 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
115 -- returns the entity of the Equivalent type and inserts on the fly the
116 -- necessary declaration such as:
9dfe12ae 117 --
ee6ba406 118 -- type anon is record
119 -- _parent : Root_Type (T); constrained with E discriminants (if any)
120 -- Extension : String (1 .. expr to match size of E);
121 -- end record;
122 --
dc74650f 123 -- This record is compatible with any object of the class of T thanks to
124 -- the first field and has the same size as E thanks to the second.
ee6ba406 125
126 function Make_Literal_Range
127 (Loc : Source_Ptr;
23e83313 128 Literal_Typ : Entity_Id) return Node_Id;
ee6ba406 129 -- Produce a Range node whose bounds are:
db1260ab 130 -- Low_Bound (Literal_Type) ..
0cd40f50 131 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
ee6ba406 132 -- this is used for expanding declarations like X : String := "sdfgdfg";
0cd40f50 133 --
134 -- If the index type of the target array is not integer, we generate:
135 -- Low_Bound (Literal_Type) ..
136 -- Literal_Type'Val
137 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
138 -- + (Length (Literal_Typ) -1))
ee6ba406 139
2b523281 140 function Make_Non_Empty_Check
141 (Loc : Source_Ptr;
142 N : Node_Id) return Node_Id;
143 -- Produce a boolean expression checking that the unidimensional array
144 -- node N is not empty.
145
ee6ba406 146 function New_Class_Wide_Subtype
147 (CW_Typ : Entity_Id;
23e83313 148 N : Node_Id) return Entity_Id;
149 -- Create an implicit subtype of CW_Typ attached to node N
ee6ba406 150
fd68eaab 151 function Requires_Cleanup_Actions
f239f5be 152 (L : List_Id;
cae6218b 153 Lib_Level : Boolean;
f239f5be 154 Nested_Constructs : Boolean) return Boolean;
fd68eaab 155 -- Given a list L, determine whether it contains one of the following:
156 --
157 -- 1) controlled objects
158 -- 2) library-level tagged types
159 --
0a4dc67b 160 -- Lib_Level is True when the list comes from a construct at the library
161 -- level, and False otherwise. Nested_Constructs is True when any nested
162 -- packages declared in L must be processed, and False otherwise.
fd68eaab 163
b444f81d 164 -------------------------------------
165 -- Activate_Atomic_Synchronization --
166 -------------------------------------
167
168 procedure Activate_Atomic_Synchronization (N : Node_Id) is
169 Msg_Node : Node_Id;
170
171 begin
11700d57 172 case Nkind (Parent (N)) is
11700d57 173
7f9f76a0 174 -- Check for cases of appearing in the prefix of a construct where
175 -- we don't need atomic synchronization for this kind of usage.
176
177 when
178 -- Nothing to do if we are the prefix of an attribute, since we
179 -- do not want an atomic sync operation for things like 'Size.
180
181 N_Attribute_Reference |
182
183 -- The N_Reference node is like an attribute
11700d57 184
185 N_Reference |
186
7f9f76a0 187 -- Nothing to do for a reference to a component (or components)
188 -- of a composite object. Only reads and updates of the object
189 -- as a whole require atomic synchronization (RM C.6 (15)).
11700d57 190
191 N_Indexed_Component |
192 N_Selected_Component |
193 N_Slice =>
194
7f9f76a0 195 -- For all the above cases, nothing to do if we are the prefix
11700d57 196
197 if Prefix (Parent (N)) = N then
198 return;
199 end if;
200
201 when others => null;
202 end case;
b444f81d 203
204 -- Go ahead and set the flag
205
206 Set_Atomic_Sync_Required (N);
207
208 -- Generate info message if requested
209
210 if Warn_On_Atomic_Synchronization then
211 case Nkind (N) is
212 when N_Identifier =>
213 Msg_Node := N;
214
215 when N_Selected_Component | N_Expanded_Name =>
216 Msg_Node := Selector_Name (N);
217
218 when N_Explicit_Dereference | N_Indexed_Component =>
219 Msg_Node := Empty;
220
221 when others =>
222 pragma Assert (False);
223 return;
224 end case;
225
226 if Present (Msg_Node) then
227 Error_Msg_N ("?info: atomic synchronization set for &", Msg_Node);
228 else
229 Error_Msg_N ("?info: atomic synchronization set", N);
230 end if;
231 end if;
232 end Activate_Atomic_Synchronization;
233
ee6ba406 234 ----------------------
235 -- Adjust_Condition --
236 ----------------------
237
238 procedure Adjust_Condition (N : Node_Id) is
239 begin
240 if No (N) then
241 return;
242 end if;
243
244 declare
245 Loc : constant Source_Ptr := Sloc (N);
246 T : constant Entity_Id := Etype (N);
247 Ti : Entity_Id;
248
249 begin
ffd8b3a5 250 -- Defend against a call where the argument has no type, or has a
251 -- type that is not Boolean. This can occur because of prior errors.
ee6ba406 252
253 if No (T) or else not Is_Boolean_Type (T) then
254 return;
255 end if;
256
257 -- Apply validity checking if needed
258
259 if Validity_Checks_On and Validity_Check_Tests then
260 Ensure_Valid (N);
261 end if;
262
263 -- Immediate return if standard boolean, the most common case,
264 -- where nothing needs to be done.
265
266 if Base_Type (T) = Standard_Boolean then
267 return;
268 end if;
269
270 -- Case of zero/non-zero semantics or non-standard enumeration
271 -- representation. In each case, we rewrite the node as:
272
273 -- ityp!(N) /= False'Enum_Rep
274
dc74650f 275 -- where ityp is an integer type with large enough size to hold any
276 -- value of type T.
ee6ba406 277
278 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
279 if Esize (T) <= Esize (Standard_Integer) then
280 Ti := Standard_Integer;
281 else
282 Ti := Standard_Long_Long_Integer;
283 end if;
284
285 Rewrite (N,
286 Make_Op_Ne (Loc,
287 Left_Opnd => Unchecked_Convert_To (Ti, N),
288 Right_Opnd =>
289 Make_Attribute_Reference (Loc,
290 Attribute_Name => Name_Enum_Rep,
291 Prefix =>
292 New_Occurrence_Of (First_Literal (T), Loc))));
293 Analyze_And_Resolve (N, Standard_Boolean);
294
295 else
296 Rewrite (N, Convert_To (Standard_Boolean, N));
297 Analyze_And_Resolve (N, Standard_Boolean);
298 end if;
299 end;
300 end Adjust_Condition;
301
302 ------------------------
303 -- Adjust_Result_Type --
304 ------------------------
305
306 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
307 begin
308 -- Ignore call if current type is not Standard.Boolean
309
310 if Etype (N) /= Standard_Boolean then
311 return;
312 end if;
313
314 -- If result is already of correct type, nothing to do. Note that
315 -- this will get the most common case where everything has a type
316 -- of Standard.Boolean.
317
318 if Base_Type (T) = Standard_Boolean then
319 return;
320
321 else
322 declare
323 KP : constant Node_Kind := Nkind (Parent (N));
324
325 begin
326 -- If result is to be used as a Condition in the syntax, no need
327 -- to convert it back, since if it was changed to Standard.Boolean
328 -- using Adjust_Condition, that is just fine for this usage.
329
330 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
331 return;
332
333 -- If result is an operand of another logical operation, no need
334 -- to reset its type, since Standard.Boolean is just fine, and
335 -- such operations always do Adjust_Condition on their operands.
336
7b31b357 337 elsif KP in N_Op_Boolean
338 or else KP in N_Short_Circuit
ee6ba406 339 or else KP = N_Op_Not
340 then
341 return;
342
dc74650f 343 -- Otherwise we perform a conversion from the current type, which
344 -- must be Standard.Boolean, to the desired type.
ee6ba406 345
346 else
347 Set_Analyzed (N);
348 Rewrite (N, Convert_To (T, N));
349 Analyze_And_Resolve (N, T);
350 end if;
351 end;
352 end if;
353 end Adjust_Result_Type;
354
355 --------------------------
356 -- Append_Freeze_Action --
357 --------------------------
358
359 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
014e9448 360 Fnode : Node_Id;
ee6ba406 361
362 begin
363 Ensure_Freeze_Node (T);
364 Fnode := Freeze_Node (T);
365
35889f1e 366 if No (Actions (Fnode)) then
ee6ba406 367 Set_Actions (Fnode, New_List);
368 end if;
369
370 Append (N, Actions (Fnode));
371 end Append_Freeze_Action;
372
373 ---------------------------
374 -- Append_Freeze_Actions --
375 ---------------------------
376
377 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
378 Fnode : constant Node_Id := Freeze_Node (T);
379
380 begin
381 if No (L) then
382 return;
383
384 else
385 if No (Actions (Fnode)) then
386 Set_Actions (Fnode, L);
ee6ba406 387 else
388 Append_List (L, Actions (Fnode));
389 end if;
ee6ba406 390 end if;
391 end Append_Freeze_Actions;
392
bb3b440a 393 ------------------------------------
394 -- Build_Allocate_Deallocate_Proc --
395 ------------------------------------
396
397 procedure Build_Allocate_Deallocate_Proc
398 (N : Node_Id;
399 Is_Allocate : Boolean)
400 is
53c179ea 401 Desig_Typ : Entity_Id;
402 Expr : Node_Id;
403 Pool_Id : Entity_Id;
404 Proc_To_Call : Node_Id := Empty;
405 Ptr_Typ : Entity_Id;
bb3b440a 406
57acff55 407 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id;
408 -- Locate TSS primitive Finalize_Address in type Typ
409
bb3b440a 410 function Find_Object (E : Node_Id) return Node_Id;
411 -- Given an arbitrary expression of an allocator, try to find an object
412 -- reference in it, otherwise return the original expression.
413
414 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
415 -- Determine whether subprogram Subp denotes a custom allocate or
416 -- deallocate.
417
57acff55 418 ---------------------------
419 -- Find_Finalize_Address --
420 ---------------------------
421
422 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id is
423 Utyp : Entity_Id := Typ;
424
425 begin
53c179ea 426 -- Handle protected class-wide or task class-wide types
427
428 if Is_Class_Wide_Type (Utyp) then
429 if Is_Concurrent_Type (Root_Type (Utyp)) then
430 Utyp := Root_Type (Utyp);
431
432 elsif Is_Private_Type (Root_Type (Utyp))
433 and then Present (Full_View (Root_Type (Utyp)))
434 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
435 then
436 Utyp := Full_View (Root_Type (Utyp));
437 end if;
438 end if;
439
440 -- Handle private types
441
57acff55 442 if Is_Private_Type (Utyp)
443 and then Present (Full_View (Utyp))
444 then
445 Utyp := Full_View (Utyp);
446 end if;
447
53c179ea 448 -- Handle protected and task types
449
450 if Is_Concurrent_Type (Utyp)
451 and then Present (Corresponding_Record_Type (Utyp))
452 then
57acff55 453 Utyp := Corresponding_Record_Type (Utyp);
454 end if;
455
456 Utyp := Underlying_Type (Base_Type (Utyp));
457
458 -- Deal with non-tagged derivation of private views. If the parent is
459 -- now known to be protected, the finalization routine is the one
460 -- defined on the corresponding record of the ancestor (corresponding
461 -- records do not automatically inherit operations, but maybe they
462 -- should???)
463
464 if Is_Untagged_Derivation (Typ) then
465 if Is_Protected_Type (Typ) then
466 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
467 else
468 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
469
470 if Is_Protected_Type (Utyp) then
471 Utyp := Corresponding_Record_Type (Utyp);
472 end if;
473 end if;
474 end if;
475
476 -- If the underlying_type is a subtype, we are dealing with the
477 -- completion of a private type. We need to access the base type and
478 -- generate a conversion to it.
479
480 if Utyp /= Base_Type (Utyp) then
481 pragma Assert (Is_Private_Type (Typ));
482
483 Utyp := Base_Type (Utyp);
484 end if;
485
0b0eb1f6 486 -- When dealing with an internally built full view for a type with
487 -- unknown discriminants, use the original record type.
488
489 if Is_Underlying_Record_View (Utyp) then
490 Utyp := Etype (Utyp);
491 end if;
492
57acff55 493 return TSS (Utyp, TSS_Finalize_Address);
494 end Find_Finalize_Address;
495
bb3b440a 496 -----------------
497 -- Find_Object --
498 -----------------
499
500 function Find_Object (E : Node_Id) return Node_Id is
b3190af0 501 Expr : Node_Id;
bb3b440a 502
503 begin
504 pragma Assert (Is_Allocate);
505
b3190af0 506 Expr := E;
507 loop
bb3b440a 508 if Nkind_In (Expr, N_Qualified_Expression,
509 N_Unchecked_Type_Conversion)
510 then
b3190af0 511 Expr := Expression (Expr);
bb3b440a 512
513 elsif Nkind (Expr) = N_Explicit_Dereference then
b3190af0 514 Expr := Prefix (Expr);
515
516 else
517 exit;
bb3b440a 518 end if;
519 end loop;
520
521 return Expr;
522 end Find_Object;
523
524 ---------------------------------
525 -- Is_Allocate_Deallocate_Proc --
526 ---------------------------------
527
528 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
529 begin
530 -- Look for a subprogram body with only one statement which is a
57acff55 531 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
bb3b440a 532
533 if Ekind (Subp) = E_Procedure
534 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
535 then
536 declare
537 HSS : constant Node_Id :=
538 Handled_Statement_Sequence (Parent (Parent (Subp)));
539 Proc : Entity_Id;
540
541 begin
542 if Present (Statements (HSS))
543 and then Nkind (First (Statements (HSS))) =
544 N_Procedure_Call_Statement
545 then
546 Proc := Entity (Name (First (Statements (HSS))));
547
548 return
57acff55 549 Is_RTE (Proc, RE_Allocate_Any_Controlled)
550 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
bb3b440a 551 end if;
552 end;
553 end if;
554
555 return False;
556 end Is_Allocate_Deallocate_Proc;
557
558 -- Start of processing for Build_Allocate_Deallocate_Proc
559
560 begin
045fdaf6 561 -- Do not perform this expansion in Alfa mode because it is not
562 -- necessary.
563
564 if Alfa_Mode then
565 return;
566 end if;
567
53c179ea 568 -- Obtain the attributes of the allocation / deallocation
569
570 if Nkind (N) = N_Free_Statement then
571 Expr := Expression (N);
572 Ptr_Typ := Base_Type (Etype (Expr));
573 Proc_To_Call := Procedure_To_Call (N);
574
575 else
576 if Nkind (N) = N_Object_Declaration then
577 Expr := Expression (N);
578 else
579 Expr := N;
580 end if;
581
8e636ab7 582 -- In certain cases an allocator with a qualified expression may
583 -- be relocated and used as the initialization expression of a
584 -- temporary:
585
586 -- before:
587 -- Obj : Ptr_Typ := new Desig_Typ'(...);
588
589 -- after:
590 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
591 -- Obj : Ptr_Typ := Tmp;
592
593 -- Since the allocator is always marked as analyzed to avoid infinite
594 -- expansion, it will never be processed by this routine given that
595 -- the designated type needs finalization actions. Detect this case
596 -- and complete the expansion of the allocator.
597
598 if Nkind (Expr) = N_Identifier
599 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
600 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
601 then
602 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
603 return;
604 end if;
53c179ea 605
8e636ab7 606 -- The allocator may have been rewritten into something else in which
607 -- case the expansion performed by this routine does not apply.
53c179ea 608
8e636ab7 609 if Nkind (Expr) /= N_Allocator then
610 return;
53c179ea 611 end if;
8e636ab7 612
613 Ptr_Typ := Base_Type (Etype (Expr));
614 Proc_To_Call := Procedure_To_Call (Expr);
53c179ea 615 end if;
616
617 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
618 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
bb3b440a 619
53c179ea 620 -- Handle concurrent types
621
622 if Is_Concurrent_Type (Desig_Typ)
623 and then Present (Corresponding_Record_Type (Desig_Typ))
624 then
625 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
626 end if;
627
628 -- Do not process allocations / deallocations without a pool
629
630 if No (Pool_Id) then
bb3b440a 631 return;
632
53c179ea 633 -- Do not process allocations on / deallocations from the secondary
634 -- stack.
635
636 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
637 return;
638
639 -- Do not replicate the machinery if the allocator / free has already
640 -- been expanded and has a custom Allocate / Deallocate.
641
642 elsif Present (Proc_To_Call)
643 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
644 then
645 return;
646 end if;
647
648 if Needs_Finalization (Desig_Typ) then
649
650 -- Certain run-time configurations and targets do not provide support
651 -- for controlled types.
652
653 if Restriction_Active (No_Finalization) then
654 return;
655
656 -- Do nothing if the access type may never allocate / deallocate
657 -- objects.
658
659 elsif No_Pool_Assigned (Ptr_Typ) then
660 return;
661
662 -- Access-to-controlled types are not supported on .NET/JVM since
663 -- these targets cannot support pools and address arithmetic.
664
665 elsif VM_Target /= No_VM then
666 return;
667 end if;
668
669 -- The allocation / deallocation of a controlled object must be
670 -- chained on / detached from a finalization master.
671
672 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
673
674 -- The only other kind of allocation / deallocation supported by this
675 -- routine is on / from a subpool.
bb3b440a 676
677 elsif Nkind (Expr) = N_Allocator
53c179ea 678 and then No (Subpool_Handle_Name (Expr))
bb3b440a 679 then
680 return;
681 end if;
682
683 declare
684 Loc : constant Source_Ptr := Sloc (N);
685 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
686 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
687 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
688 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
689
690 Actuals : List_Id;
57acff55 691 Fin_Addr_Id : Entity_Id;
692 Fin_Mas_Act : Node_Id;
693 Fin_Mas_Id : Entity_Id;
bb3b440a 694 Proc_To_Call : Entity_Id;
53c179ea 695 Subpool : Node_Id := Empty;
bb3b440a 696
697 begin
57acff55 698 -- Step 1: Construct all the actuals for the call to library routine
699 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
bb3b440a 700
57acff55 701 -- a) Storage pool
bb3b440a 702
53c179ea 703 Actuals := New_List (New_Reference_To (Pool_Id, Loc));
bb3b440a 704
57acff55 705 if Is_Allocate then
bb3b440a 706
57acff55 707 -- b) Subpool
bb3b440a 708
53c179ea 709 if Nkind (Expr) = N_Allocator then
710 Subpool := Subpool_Handle_Name (Expr);
711 end if;
712
57c2f209 713 -- If a subpool is present it can be an arbitrary name, so make
714 -- the actual by copying the tree.
715
53c179ea 716 if Present (Subpool) then
57c2f209 717 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
57acff55 718 else
719 Append_To (Actuals, Make_Null (Loc));
720 end if;
bb3b440a 721
57acff55 722 -- c) Finalization master
723
724 if Needs_Finalization (Desig_Typ) then
53c179ea 725 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
57acff55 726 Fin_Mas_Act := New_Reference_To (Fin_Mas_Id, Loc);
727
728 -- Handle the case where the master is actually a pointer to a
729 -- master. This case arises in build-in-place functions.
730
731 if Is_Access_Type (Etype (Fin_Mas_Id)) then
732 Append_To (Actuals, Fin_Mas_Act);
bb3b440a 733 else
57acff55 734 Append_To (Actuals,
735 Make_Attribute_Reference (Loc,
736 Prefix => Fin_Mas_Act,
737 Attribute_Name => Name_Unrestricted_Access));
bb3b440a 738 end if;
57acff55 739 else
740 Append_To (Actuals, Make_Null (Loc));
741 end if;
bb3b440a 742
57acff55 743 -- d) Finalize_Address
bb3b440a 744
962f9261 745 -- Primitive Finalize_Address is never generated in CodePeer mode
746 -- since it contains an Unchecked_Conversion.
bb3b440a 747
962f9261 748 if Needs_Finalization (Desig_Typ)
749 and then not CodePeer_Mode
750 then
751 Fin_Addr_Id := Find_Finalize_Address (Desig_Typ);
53c179ea 752 pragma Assert (Present (Fin_Addr_Id));
753
57acff55 754 Append_To (Actuals,
755 Make_Attribute_Reference (Loc,
756 Prefix => New_Reference_To (Fin_Addr_Id, Loc),
757 Attribute_Name => Name_Unrestricted_Access));
758 else
759 Append_To (Actuals, Make_Null (Loc));
760 end if;
761 end if;
bb3b440a 762
57acff55 763 -- e) Address
764 -- f) Storage_Size
765 -- g) Alignment
bb3b440a 766
57acff55 767 Append_To (Actuals, New_Reference_To (Addr_Id, Loc));
768 Append_To (Actuals, New_Reference_To (Size_Id, Loc));
208fd589 769
41331dcf 770 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
208fd589 771 Append_To (Actuals, New_Reference_To (Alig_Id, Loc));
772
773 -- For deallocation of class wide types we obtain the value of
774 -- alignment from the Type Specific Record of the deallocated object.
775 -- This is needed because the frontend expansion of class-wide types
776 -- into equivalent types confuses the backend.
777
778 else
779 -- Generate:
780 -- Obj.all'Alignment
781
782 -- ... because 'Alignment applied to class-wide types is expanded
783 -- into the code that reads the value of alignment from the TSD
784 -- (see Expand_N_Attribute_Reference)
785
786 Append_To (Actuals,
787 Unchecked_Convert_To (RTE (RE_Storage_Offset),
788 Make_Attribute_Reference (Loc,
41331dcf 789 Prefix =>
208fd589 790 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
791 Attribute_Name => Name_Alignment)));
792 end if;
bb3b440a 793
57acff55 794 -- h) Is_Controlled
bb3b440a 795
57acff55 796 -- Generate a run-time check to determine whether a class-wide object
797 -- is truly controlled.
bb3b440a 798
57acff55 799 if Needs_Finalization (Desig_Typ) then
800 if Is_Class_Wide_Type (Desig_Typ)
801 or else Is_Generic_Actual_Type (Desig_Typ)
802 then
803 declare
804 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
805 Flag_Expr : Node_Id;
806 Param : Node_Id;
807 Temp : Node_Id;
bb3b440a 808
57acff55 809 begin
810 if Is_Allocate then
811 Temp := Find_Object (Expression (Expr));
bb3b440a 812 else
57acff55 813 Temp := Expr;
bb3b440a 814 end if;
815
57acff55 816 -- Processing for generic actuals
bb3b440a 817
57acff55 818 if Is_Generic_Actual_Type (Desig_Typ) then
819 Flag_Expr :=
820 New_Reference_To (Boolean_Literals
821 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
bb3b440a 822
57acff55 823 -- Processing for subtype indications
bb3b440a 824
57acff55 825 elsif Nkind (Temp) in N_Has_Entity
826 and then Is_Type (Entity (Temp))
827 then
828 Flag_Expr :=
829 New_Reference_To (Boolean_Literals
830 (Needs_Finalization (Entity (Temp))), Loc);
bb3b440a 831
57acff55 832 -- Generate a runtime check to test the controlled state of
833 -- an object for the purposes of allocation / deallocation.
bb3b440a 834
57acff55 835 else
836 -- The following case arises when allocating through an
837 -- interface class-wide type, generate:
838 --
839 -- Temp.all
840
841 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
842 Param :=
843 Make_Explicit_Dereference (Loc,
844 Prefix =>
845 Relocate_Node (Temp));
846
847 -- Generate:
848 -- Temp'Tag
849
850 else
851 Param :=
852 Make_Attribute_Reference (Loc,
853 Prefix =>
854 Relocate_Node (Temp),
855 Attribute_Name => Name_Tag);
856 end if;
857
858 -- Generate:
859 -- Needs_Finalization (<Param>)
860
861 Flag_Expr :=
862 Make_Function_Call (Loc,
863 Name =>
864 New_Reference_To (RTE (RE_Needs_Finalization), Loc),
865 Parameter_Associations => New_List (Param));
866 end if;
867
868 -- Create the temporary which represents the finalization
869 -- state of the expression. Generate:
870 --
871 -- F : constant Boolean := <Flag_Expr>;
872
873 Insert_Action (N,
874 Make_Object_Declaration (Loc,
875 Defining_Identifier => Flag_Id,
876 Constant_Present => True,
877 Object_Definition =>
878 New_Reference_To (Standard_Boolean, Loc),
879 Expression => Flag_Expr));
880
881 -- The flag acts as the last actual
882
883 Append_To (Actuals, New_Reference_To (Flag_Id, Loc));
884 end;
53c179ea 885
886 -- The object is statically known to be controlled
887
888 else
889 Append_To (Actuals, New_Reference_To (Standard_True, Loc));
57acff55 890 end if;
41331dcf 891
57acff55 892 else
893 Append_To (Actuals, New_Reference_To (Standard_False, Loc));
bb3b440a 894 end if;
895
53c179ea 896 -- i) On_Subpool
897
898 if Is_Allocate then
899 Append_To (Actuals,
900 New_Reference_To (Boolean_Literals (Present (Subpool)), Loc));
901 end if;
902
57acff55 903 -- Step 2: Build a wrapper Allocate / Deallocate which internally
904 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
905
bb3b440a 906 -- Select the proper routine to call
907
908 if Is_Allocate then
57acff55 909 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
bb3b440a 910 else
57acff55 911 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
bb3b440a 912 end if;
913
914 -- Create a custom Allocate / Deallocate routine which has identical
915 -- profile to that of System.Storage_Pools.
916
917 Insert_Action (N,
918 Make_Subprogram_Body (Loc,
919 Specification =>
920
921 -- procedure Pnn
922
923 Make_Procedure_Specification (Loc,
924 Defining_Unit_Name => Proc_Id,
925 Parameter_Specifications => New_List (
926
927 -- P : Root_Storage_Pool
928
929 Make_Parameter_Specification (Loc,
41331dcf 930 Defining_Identifier => Make_Temporary (Loc, 'P'),
bb3b440a 931 Parameter_Type =>
932 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc)),
933
934 -- A : [out] Address
935
936 Make_Parameter_Specification (Loc,
937 Defining_Identifier => Addr_Id,
41331dcf 938 Out_Present => Is_Allocate,
939 Parameter_Type =>
bb3b440a 940 New_Reference_To (RTE (RE_Address), Loc)),
941
942 -- S : Storage_Count
943
944 Make_Parameter_Specification (Loc,
945 Defining_Identifier => Size_Id,
41331dcf 946 Parameter_Type =>
bb3b440a 947 New_Reference_To (RTE (RE_Storage_Count), Loc)),
948
949 -- L : Storage_Count
950
951 Make_Parameter_Specification (Loc,
952 Defining_Identifier => Alig_Id,
41331dcf 953 Parameter_Type =>
bb3b440a 954 New_Reference_To (RTE (RE_Storage_Count), Loc)))),
955
956 Declarations => No_List,
957
958 Handled_Statement_Sequence =>
959 Make_Handled_Sequence_Of_Statements (Loc,
960 Statements => New_List (
bb3b440a 961 Make_Procedure_Call_Statement (Loc,
41331dcf 962 Name => New_Reference_To (Proc_To_Call, Loc),
bb3b440a 963 Parameter_Associations => Actuals)))));
964
965 -- The newly generated Allocate / Deallocate becomes the default
966 -- procedure to call when the back end processes the allocation /
967 -- deallocation.
968
969 if Is_Allocate then
970 Set_Procedure_To_Call (Expr, Proc_Id);
971 else
972 Set_Procedure_To_Call (N, Proc_Id);
973 end if;
974 end;
975 end Build_Allocate_Deallocate_Proc;
976
ee6ba406 977 ------------------------
978 -- Build_Runtime_Call --
979 ------------------------
980
981 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
982 begin
9dfe12ae 983 -- If entity is not available, we can skip making the call (this avoids
984 -- junk duplicated error messages in a number of cases).
985
986 if not RTE_Available (RE) then
987 return Make_Null_Statement (Loc);
988 else
989 return
990 Make_Procedure_Call_Statement (Loc,
991 Name => New_Reference_To (RTE (RE), Loc));
992 end if;
ee6ba406 993 end Build_Runtime_Call;
994
5c99c290 995 ----------------------------
996 -- Build_Task_Array_Image --
997 ----------------------------
ee6ba406 998
999 -- This function generates the body for a function that constructs the
1000 -- image string for a task that is an array component. The function is
9dfe12ae 1001 -- local to the init proc for the array type, and is called for each one
ee6ba406 1002 -- of the components. The constructed image has the form of an indexed
1003 -- component, whose prefix is the outer variable of the array type.
1d00a8ce 1004 -- The n-dimensional array type has known indexes Index, Index2...
dc74650f 1005
9dfe12ae 1006 -- Id_Ref is an indexed component form created by the enclosing init proc.
1d00a8ce 1007 -- Its successive indexes are Val1, Val2, ... which are the loop variables
9dfe12ae 1008 -- in the loops that call the individual task init proc on each component.
ee6ba406 1009
1010 -- The generated function has the following structure:
1011
9dfe12ae 1012 -- function F return String is
1013 -- Pref : string renames Task_Name;
1014 -- T1 : String := Index1'Image (Val1);
ee6ba406 1015 -- ...
9dfe12ae 1016 -- Tn : String := indexn'image (Valn);
1017 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
ee6ba406 1018 -- -- Len includes commas and the end parentheses.
9dfe12ae 1019 -- Res : String (1..Len);
1020 -- Pos : Integer := Pref'Length;
ee6ba406 1021 --
1022 -- begin
71b30311 1023 -- Res (1 .. Pos) := Pref;
ee6ba406 1024 -- Pos := Pos + 1;
1025 -- Res (Pos) := '(';
1026 -- Pos := Pos + 1;
1027 -- Res (Pos .. Pos + T1'Length - 1) := T1;
1028 -- Pos := Pos + T1'Length;
1029 -- Res (Pos) := '.';
1030 -- Pos := Pos + 1;
1031 -- ...
1032 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
1033 -- Res (Len) := ')';
1034 --
9dfe12ae 1035 -- return Res;
ee6ba406 1036 -- end F;
1037 --
dc74650f 1038 -- Needless to say, multidimensional arrays of tasks are rare enough that
1039 -- the bulkiness of this code is not really a concern.
ee6ba406 1040
1041 function Build_Task_Array_Image
1042 (Loc : Source_Ptr;
1043 Id_Ref : Node_Id;
71b30311 1044 A_Type : Entity_Id;
23e83313 1045 Dyn : Boolean := False) return Node_Id
ee6ba406 1046 is
1047 Dims : constant Nat := Number_Dimensions (A_Type);
23e83313 1048 -- Number of dimensions for array of tasks
ee6ba406 1049
1050 Temps : array (1 .. Dims) of Entity_Id;
23e83313 1051 -- Array of temporaries to hold string for each index
ee6ba406 1052
1053 Indx : Node_Id;
1054 -- Index expression
1055
1056 Len : Entity_Id;
1057 -- Total length of generated name
1058
1059 Pos : Entity_Id;
1060 -- Running index for substring assignments
1061
11deeeb6 1062 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
ee6ba406 1063 -- Name of enclosing variable, prefix of resulting name
1064
1065 Res : Entity_Id;
1066 -- String to hold result
1067
1068 Val : Node_Id;
1d00a8ce 1069 -- Value of successive indexes
ee6ba406 1070
1071 Sum : Node_Id;
1072 -- Expression to compute total size of string
1073
1074 T : Entity_Id;
1075 -- Entity for name at one index position
1076
0cd40f50 1077 Decls : constant List_Id := New_List;
1078 Stats : constant List_Id := New_List;
ee6ba406 1079
1080 begin
dc74650f 1081 -- For a dynamic task, the name comes from the target variable. For a
1082 -- static one it is a formal of the enclosing init proc.
71b30311 1083
1084 if Dyn then
1085 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
9dfe12ae 1086 Append_To (Decls,
1087 Make_Object_Declaration (Loc,
1088 Defining_Identifier => Pref,
1089 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1090 Expression =>
23e83313 1091 Make_String_Literal (Loc,
1092 Strval => String_From_Name_Buffer)));
9dfe12ae 1093
71b30311 1094 else
9dfe12ae 1095 Append_To (Decls,
1096 Make_Object_Renaming_Declaration (Loc,
1097 Defining_Identifier => Pref,
1098 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1099 Name => Make_Identifier (Loc, Name_uTask_Name)));
71b30311 1100 end if;
ee6ba406 1101
ee6ba406 1102 Indx := First_Index (A_Type);
1103 Val := First (Expressions (Id_Ref));
1104
1105 for J in 1 .. Dims loop
11deeeb6 1106 T := Make_Temporary (Loc, 'T');
ee6ba406 1107 Temps (J) := T;
1108
1109 Append_To (Decls,
1110 Make_Object_Declaration (Loc,
1111 Defining_Identifier => T,
1112 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1113 Expression =>
1114 Make_Attribute_Reference (Loc,
1115 Attribute_Name => Name_Image,
11deeeb6 1116 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
1117 Expressions => New_List (New_Copy_Tree (Val)))));
ee6ba406 1118
1119 Next_Index (Indx);
1120 Next (Val);
1121 end loop;
1122
1123 Sum := Make_Integer_Literal (Loc, Dims + 1);
1124
1125 Sum :=
1126 Make_Op_Add (Loc,
1127 Left_Opnd => Sum,
1128 Right_Opnd =>
1129 Make_Attribute_Reference (Loc,
1130 Attribute_Name => Name_Length,
1131 Prefix =>
71b30311 1132 New_Occurrence_Of (Pref, Loc),
ee6ba406 1133 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1134
1135 for J in 1 .. Dims loop
1136 Sum :=
1137 Make_Op_Add (Loc,
1138 Left_Opnd => Sum,
1139 Right_Opnd =>
1140 Make_Attribute_Reference (Loc,
1141 Attribute_Name => Name_Length,
1142 Prefix =>
1143 New_Occurrence_Of (Temps (J), Loc),
1144 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1145 end loop;
1146
71b30311 1147 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
ee6ba406 1148
1149 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
1150
1151 Append_To (Stats,
1152 Make_Assignment_Statement (Loc,
1153 Name => Make_Indexed_Component (Loc,
1154 Prefix => New_Occurrence_Of (Res, Loc),
1155 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1156 Expression =>
1157 Make_Character_Literal (Loc,
1158 Chars => Name_Find,
1159 Char_Literal_Value =>
7189d17f 1160 UI_From_Int (Character'Pos ('(')))));
ee6ba406 1161
1162 Append_To (Stats,
1163 Make_Assignment_Statement (Loc,
1164 Name => New_Occurrence_Of (Pos, Loc),
1165 Expression =>
1166 Make_Op_Add (Loc,
1167 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1168 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1169
1170 for J in 1 .. Dims loop
1171
1172 Append_To (Stats,
1173 Make_Assignment_Statement (Loc,
1174 Name => Make_Slice (Loc,
1175 Prefix => New_Occurrence_Of (Res, Loc),
1176 Discrete_Range =>
1177 Make_Range (Loc,
1178 Low_Bound => New_Occurrence_Of (Pos, Loc),
1179 High_Bound => Make_Op_Subtract (Loc,
1180 Left_Opnd =>
1181 Make_Op_Add (Loc,
1182 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1183 Right_Opnd =>
1184 Make_Attribute_Reference (Loc,
1185 Attribute_Name => Name_Length,
1186 Prefix =>
1187 New_Occurrence_Of (Temps (J), Loc),
1188 Expressions =>
1189 New_List (Make_Integer_Literal (Loc, 1)))),
1190 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
1191
1192 Expression => New_Occurrence_Of (Temps (J), Loc)));
1193
1194 if J < Dims then
1195 Append_To (Stats,
1196 Make_Assignment_Statement (Loc,
1197 Name => New_Occurrence_Of (Pos, Loc),
1198 Expression =>
1199 Make_Op_Add (Loc,
1200 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1201 Right_Opnd =>
1202 Make_Attribute_Reference (Loc,
1203 Attribute_Name => Name_Length,
1204 Prefix => New_Occurrence_Of (Temps (J), Loc),
1205 Expressions =>
1206 New_List (Make_Integer_Literal (Loc, 1))))));
1207
1208 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
1209
1210 Append_To (Stats,
1211 Make_Assignment_Statement (Loc,
1212 Name => Make_Indexed_Component (Loc,
1213 Prefix => New_Occurrence_Of (Res, Loc),
1214 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1215 Expression =>
1216 Make_Character_Literal (Loc,
1217 Chars => Name_Find,
1218 Char_Literal_Value =>
7189d17f 1219 UI_From_Int (Character'Pos (',')))));
ee6ba406 1220
1221 Append_To (Stats,
1222 Make_Assignment_Statement (Loc,
1223 Name => New_Occurrence_Of (Pos, Loc),
1224 Expression =>
1225 Make_Op_Add (Loc,
1226 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1227 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1228 end if;
1229 end loop;
1230
1231 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
1232
1233 Append_To (Stats,
1234 Make_Assignment_Statement (Loc,
1235 Name => Make_Indexed_Component (Loc,
1236 Prefix => New_Occurrence_Of (Res, Loc),
1237 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
1238 Expression =>
1239 Make_Character_Literal (Loc,
1240 Chars => Name_Find,
1241 Char_Literal_Value =>
7189d17f 1242 UI_From_Int (Character'Pos (')')))));
ee6ba406 1243 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1244 end Build_Task_Array_Image;
1245
1246 ----------------------------
1247 -- Build_Task_Image_Decls --
1248 ----------------------------
1249
1250 function Build_Task_Image_Decls
014e9448 1251 (Loc : Source_Ptr;
1252 Id_Ref : Node_Id;
1253 A_Type : Entity_Id;
1254 In_Init_Proc : Boolean := False) return List_Id
ee6ba406 1255 is
9dfe12ae 1256 Decls : constant List_Id := New_List;
71b30311 1257 T_Id : Entity_Id := Empty;
1258 Decl : Node_Id;
71b30311 1259 Expr : Node_Id := Empty;
1260 Fun : Node_Id := Empty;
1261 Is_Dyn : constant Boolean :=
9dfe12ae 1262 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
1263 and then
1264 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
ee6ba406 1265
1266 begin
9dfe12ae 1267 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
1268 -- generate a dummy declaration only.
ee6ba406 1269
1e16c51c 1270 if Restriction_Active (No_Implicit_Heap_Allocations)
9dfe12ae 1271 or else Global_Discard_Names
1272 then
11deeeb6 1273 T_Id := Make_Temporary (Loc, 'J');
9dfe12ae 1274 Name_Len := 0;
ee6ba406 1275
1276 return
1277 New_List (
1278 Make_Object_Declaration (Loc,
1279 Defining_Identifier => T_Id,
9dfe12ae 1280 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1281 Expression =>
23e83313 1282 Make_String_Literal (Loc,
1283 Strval => String_From_Name_Buffer)));
ee6ba406 1284
1285 else
1286 if Nkind (Id_Ref) = N_Identifier
1287 or else Nkind (Id_Ref) = N_Defining_Identifier
1288 then
8f71d067 1289 -- For a simple variable, the image of the task is built from
dc74650f 1290 -- the name of the variable. To avoid possible conflict with the
1291 -- anonymous type created for a single protected object, add a
1292 -- numeric suffix.
ee6ba406 1293
1294 T_Id :=
1295 Make_Defining_Identifier (Loc,
8f71d067 1296 New_External_Name (Chars (Id_Ref), 'T', 1));
ee6ba406 1297
1298 Get_Name_String (Chars (Id_Ref));
1299
23e83313 1300 Expr :=
1301 Make_String_Literal (Loc,
1302 Strval => String_From_Name_Buffer);
ee6ba406 1303
1304 elsif Nkind (Id_Ref) = N_Selected_Component then
1305 T_Id :=
1306 Make_Defining_Identifier (Loc,
9dfe12ae 1307 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
f15731c4 1308 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
ee6ba406 1309
1310 elsif Nkind (Id_Ref) = N_Indexed_Component then
1311 T_Id :=
1312 Make_Defining_Identifier (Loc,
9dfe12ae 1313 New_External_Name (Chars (A_Type), 'N'));
ee6ba406 1314
71b30311 1315 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
ee6ba406 1316 end if;
1317 end if;
1318
1319 if Present (Fun) then
1320 Append (Fun, Decls);
9dfe12ae 1321 Expr := Make_Function_Call (Loc,
1322 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
014e9448 1323
37808946 1324 if not In_Init_Proc and then VM_Target = No_VM then
014e9448 1325 Set_Uses_Sec_Stack (Defining_Entity (Fun));
1326 end if;
ee6ba406 1327 end if;
1328
1329 Decl := Make_Object_Declaration (Loc,
1330 Defining_Identifier => T_Id,
9dfe12ae 1331 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1332 Constant_Present => True,
1333 Expression => Expr);
ee6ba406 1334
1335 Append (Decl, Decls);
1336 return Decls;
1337 end Build_Task_Image_Decls;
1338
1339 -------------------------------
1340 -- Build_Task_Image_Function --
1341 -------------------------------
1342
1343 function Build_Task_Image_Function
1344 (Loc : Source_Ptr;
1345 Decls : List_Id;
1346 Stats : List_Id;
23e83313 1347 Res : Entity_Id) return Node_Id
ee6ba406 1348 is
1349 Spec : Node_Id;
1350
1351 begin
1352 Append_To (Stats,
0cd40f50 1353 Make_Simple_Return_Statement (Loc,
9dfe12ae 1354 Expression => New_Occurrence_Of (Res, Loc)));
1355
1356 Spec := Make_Function_Specification (Loc,
11deeeb6 1357 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
1358 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
9dfe12ae 1359
dc74650f 1360 -- Calls to 'Image use the secondary stack, which must be cleaned up
1361 -- after the task name is built.
9dfe12ae 1362
ee6ba406 1363 return Make_Subprogram_Body (Loc,
1364 Specification => Spec,
1365 Declarations => Decls,
1366 Handled_Statement_Sequence =>
9dfe12ae 1367 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
ee6ba406 1368 end Build_Task_Image_Function;
1369
1370 -----------------------------
1371 -- Build_Task_Image_Prefix --
1372 -----------------------------
1373
1374 procedure Build_Task_Image_Prefix
1375 (Loc : Source_Ptr;
1376 Len : out Entity_Id;
1377 Res : out Entity_Id;
1378 Pos : out Entity_Id;
1379 Prefix : Entity_Id;
1380 Sum : Node_Id;
0cd40f50 1381 Decls : List_Id;
1382 Stats : List_Id)
ee6ba406 1383 is
1384 begin
11deeeb6 1385 Len := Make_Temporary (Loc, 'L', Sum);
ee6ba406 1386
1387 Append_To (Decls,
1388 Make_Object_Declaration (Loc,
1389 Defining_Identifier => Len,
11deeeb6 1390 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
1391 Expression => Sum));
ee6ba406 1392
11deeeb6 1393 Res := Make_Temporary (Loc, 'R');
ee6ba406 1394
1395 Append_To (Decls,
1396 Make_Object_Declaration (Loc,
1397 Defining_Identifier => Res,
1398 Object_Definition =>
1399 Make_Subtype_Indication (Loc,
1400 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1401 Constraint =>
1402 Make_Index_Or_Discriminant_Constraint (Loc,
1403 Constraints =>
1404 New_List (
1405 Make_Range (Loc,
1406 Low_Bound => Make_Integer_Literal (Loc, 1),
1407 High_Bound => New_Occurrence_Of (Len, Loc)))))));
1408
11deeeb6 1409 Pos := Make_Temporary (Loc, 'P');
ee6ba406 1410
1411 Append_To (Decls,
1412 Make_Object_Declaration (Loc,
1413 Defining_Identifier => Pos,
11deeeb6 1414 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
ee6ba406 1415
1416 -- Pos := Prefix'Length;
1417
1418 Append_To (Stats,
1419 Make_Assignment_Statement (Loc,
1420 Name => New_Occurrence_Of (Pos, Loc),
1421 Expression =>
1422 Make_Attribute_Reference (Loc,
1423 Attribute_Name => Name_Length,
11deeeb6 1424 Prefix => New_Occurrence_Of (Prefix, Loc),
1425 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
ee6ba406 1426
1427 -- Res (1 .. Pos) := Prefix;
1428
1429 Append_To (Stats,
11deeeb6 1430 Make_Assignment_Statement (Loc,
1431 Name =>
1432 Make_Slice (Loc,
1433 Prefix => New_Occurrence_Of (Res, Loc),
ee6ba406 1434 Discrete_Range =>
1435 Make_Range (Loc,
11deeeb6 1436 Low_Bound => Make_Integer_Literal (Loc, 1),
ee6ba406 1437 High_Bound => New_Occurrence_Of (Pos, Loc))),
1438
11deeeb6 1439 Expression => New_Occurrence_Of (Prefix, Loc)));
ee6ba406 1440
1441 Append_To (Stats,
1442 Make_Assignment_Statement (Loc,
11deeeb6 1443 Name => New_Occurrence_Of (Pos, Loc),
ee6ba406 1444 Expression =>
1445 Make_Op_Add (Loc,
11deeeb6 1446 Left_Opnd => New_Occurrence_Of (Pos, Loc),
ee6ba406 1447 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1448 end Build_Task_Image_Prefix;
1449
1450 -----------------------------
1451 -- Build_Task_Record_Image --
1452 -----------------------------
1453
1454 function Build_Task_Record_Image
1455 (Loc : Source_Ptr;
1456 Id_Ref : Node_Id;
23e83313 1457 Dyn : Boolean := False) return Node_Id
ee6ba406 1458 is
1459 Len : Entity_Id;
1460 -- Total length of generated name
1461
1462 Pos : Entity_Id;
1463 -- Index into result
1464
1465 Res : Entity_Id;
1466 -- String to hold result
1467
11deeeb6 1468 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
ee6ba406 1469 -- Name of enclosing variable, prefix of resulting name
1470
1471 Sum : Node_Id;
23e83313 1472 -- Expression to compute total size of string
ee6ba406 1473
1474 Sel : Entity_Id;
1475 -- Entity for selector name
1476
0cd40f50 1477 Decls : constant List_Id := New_List;
1478 Stats : constant List_Id := New_List;
ee6ba406 1479
1480 begin
f5fbfaa6 1481 -- For a dynamic task, the name comes from the target variable. For a
1482 -- static one it is a formal of the enclosing init proc.
71b30311 1483
1484 if Dyn then
1485 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
9dfe12ae 1486 Append_To (Decls,
1487 Make_Object_Declaration (Loc,
1488 Defining_Identifier => Pref,
1489 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1490 Expression =>
23e83313 1491 Make_String_Literal (Loc,
1492 Strval => String_From_Name_Buffer)));
9dfe12ae 1493
71b30311 1494 else
9dfe12ae 1495 Append_To (Decls,
1496 Make_Object_Renaming_Declaration (Loc,
1497 Defining_Identifier => Pref,
1498 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1499 Name => Make_Identifier (Loc, Name_uTask_Name)));
71b30311 1500 end if;
ee6ba406 1501
11deeeb6 1502 Sel := Make_Temporary (Loc, 'S');
ee6ba406 1503
1504 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1505
1506 Append_To (Decls,
1507 Make_Object_Declaration (Loc,
1508 Defining_Identifier => Sel,
11deeeb6 1509 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1510 Expression =>
23e83313 1511 Make_String_Literal (Loc,
1512 Strval => String_From_Name_Buffer)));
ee6ba406 1513
1514 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1515
1516 Sum :=
1517 Make_Op_Add (Loc,
1518 Left_Opnd => Sum,
1519 Right_Opnd =>
1520 Make_Attribute_Reference (Loc,
1521 Attribute_Name => Name_Length,
1522 Prefix =>
71b30311 1523 New_Occurrence_Of (Pref, Loc),
ee6ba406 1524 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1525
71b30311 1526 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
ee6ba406 1527
1528 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1529
1530 -- Res (Pos) := '.';
1531
1532 Append_To (Stats,
1533 Make_Assignment_Statement (Loc,
1534 Name => Make_Indexed_Component (Loc,
1535 Prefix => New_Occurrence_Of (Res, Loc),
1536 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1537 Expression =>
1538 Make_Character_Literal (Loc,
1539 Chars => Name_Find,
1540 Char_Literal_Value =>
7189d17f 1541 UI_From_Int (Character'Pos ('.')))));
ee6ba406 1542
1543 Append_To (Stats,
1544 Make_Assignment_Statement (Loc,
1545 Name => New_Occurrence_Of (Pos, Loc),
1546 Expression =>
1547 Make_Op_Add (Loc,
1548 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1549 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1550
1551 -- Res (Pos .. Len) := Selector;
1552
1553 Append_To (Stats,
1554 Make_Assignment_Statement (Loc,
1555 Name => Make_Slice (Loc,
1556 Prefix => New_Occurrence_Of (Res, Loc),
1557 Discrete_Range =>
1558 Make_Range (Loc,
1559 Low_Bound => New_Occurrence_Of (Pos, Loc),
1560 High_Bound => New_Occurrence_Of (Len, Loc))),
1561 Expression => New_Occurrence_Of (Sel, Loc)));
1562
1563 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1564 end Build_Task_Record_Image;
1565
5c61a0ff 1566 ----------------------------------
1567 -- Component_May_Be_Bit_Aligned --
1568 ----------------------------------
1569
1570 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
325f3586 1571 UT : Entity_Id;
d2b860b4 1572
5c61a0ff 1573 begin
3925b986 1574 -- If no component clause, then everything is fine, since the back end
1575 -- never bit-misaligns by default, even if there is a pragma Packed for
1576 -- the record.
5c61a0ff 1577
325f3586 1578 if No (Comp) or else No (Component_Clause (Comp)) then
5c61a0ff 1579 return False;
1580 end if;
1581
325f3586 1582 UT := Underlying_Type (Etype (Comp));
1583
5c61a0ff 1584 -- It is only array and record types that cause trouble
1585
7d4e1171 1586 if not Is_Record_Type (UT)
1587 and then not Is_Array_Type (UT)
5c61a0ff 1588 then
1589 return False;
1590
d2b860b4 1591 -- If we know that we have a small (64 bits or less) record or small
1592 -- bit-packed array, then everything is fine, since the back end can
1593 -- handle these cases correctly.
5c61a0ff 1594
1595 elsif Esize (Comp) <= 64
7d4e1171 1596 and then (Is_Record_Type (UT)
1597 or else Is_Bit_Packed_Array (UT))
5c61a0ff 1598 then
1599 return False;
1600
3925b986 1601 -- Otherwise if the component is not byte aligned, we know we have the
1602 -- nasty unaligned case.
5c61a0ff 1603
1604 elsif Normalized_First_Bit (Comp) /= Uint_0
1605 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1606 then
1607 return True;
1608
1609 -- If we are large and byte aligned, then OK at this level
1610
1611 else
1612 return False;
1613 end if;
1614 end Component_May_Be_Bit_Aligned;
1615
acf97c11 1616 -----------------------------------
1617 -- Corresponding_Runtime_Package --
1618 -----------------------------------
1619
1620 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1621 Pkg_Id : RTU_Id := RTU_Null;
1622
1623 begin
1624 pragma Assert (Is_Concurrent_Type (Typ));
1625
1626 if Ekind (Typ) in Protected_Kind then
1627 if Has_Entries (Typ)
57993a53 1628
1629 -- A protected type without entries that covers an interface and
1630 -- overrides the abstract routines with protected procedures is
1631 -- considered equivalent to a protected type with entries in the
2c145f84 1632 -- context of dispatching select statements. It is sufficient to
57993a53 1633 -- check for the presence of an interface list in the declaration
1634 -- node to recognize this case.
1635
1636 or else Present (Interface_List (Parent (Typ)))
134520e8 1637 or else
1638 (((Has_Attach_Handler (Typ) and then not Restricted_Profile)
fe0961a8 1639 or else Has_Interrupt_Handler (Typ))
134520e8 1640 and then not Restriction_Active (No_Dynamic_Attachment))
acf97c11 1641 then
1642 if Abort_Allowed
1643 or else Restriction_Active (No_Entry_Queue) = False
1644 or else Number_Entries (Typ) > 1
1645 or else (Has_Attach_Handler (Typ)
fe0961a8 1646 and then not Restricted_Profile)
acf97c11 1647 then
1648 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1649 else
1650 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1651 end if;
1652
1653 else
1654 Pkg_Id := System_Tasking_Protected_Objects;
1655 end if;
1656 end if;
1657
1658 return Pkg_Id;
1659 end Corresponding_Runtime_Package;
1660
ee6ba406 1661 -------------------------------
1662 -- Convert_To_Actual_Subtype --
1663 -------------------------------
1664
1665 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1666 Act_ST : Entity_Id;
1667
1668 begin
1669 Act_ST := Get_Actual_Subtype (Exp);
1670
1671 if Act_ST = Etype (Exp) then
1672 return;
ee6ba406 1673 else
fe0961a8 1674 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
ee6ba406 1675 Analyze_And_Resolve (Exp, Act_ST);
1676 end if;
1677 end Convert_To_Actual_Subtype;
1678
1679 -----------------------------------
1680 -- Current_Sem_Unit_Declarations --
1681 -----------------------------------
1682
1683 function Current_Sem_Unit_Declarations return List_Id is
1684 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1685 Decls : List_Id;
1686
1687 begin
1688 -- If the current unit is a package body, locate the visible
1689 -- declarations of the package spec.
1690
1691 if Nkind (U) = N_Package_Body then
1692 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1693 end if;
1694
1695 if Nkind (U) = N_Package_Declaration then
1696 U := Specification (U);
1697 Decls := Visible_Declarations (U);
1698
1699 if No (Decls) then
1700 Decls := New_List;
1701 Set_Visible_Declarations (U, Decls);
1702 end if;
1703
1704 else
1705 Decls := Declarations (U);
1706
1707 if No (Decls) then
1708 Decls := New_List;
1709 Set_Declarations (U, Decls);
1710 end if;
1711 end if;
1712
1713 return Decls;
1714 end Current_Sem_Unit_Declarations;
1715
1716 -----------------------
1717 -- Duplicate_Subexpr --
1718 -----------------------
1719
1720 function Duplicate_Subexpr
1721 (Exp : Node_Id;
23e83313 1722 Name_Req : Boolean := False) return Node_Id
ee6ba406 1723 is
1724 begin
1725 Remove_Side_Effects (Exp, Name_Req);
1726 return New_Copy_Tree (Exp);
1727 end Duplicate_Subexpr;
1728
226494a3 1729 ---------------------------------
1730 -- Duplicate_Subexpr_No_Checks --
1731 ---------------------------------
1732
1733 function Duplicate_Subexpr_No_Checks
1734 (Exp : Node_Id;
23e83313 1735 Name_Req : Boolean := False) return Node_Id
226494a3 1736 is
1737 New_Exp : Node_Id;
1738
1739 begin
1740 Remove_Side_Effects (Exp, Name_Req);
1741 New_Exp := New_Copy_Tree (Exp);
1742 Remove_Checks (New_Exp);
1743 return New_Exp;
1744 end Duplicate_Subexpr_No_Checks;
1745
1746 -----------------------------------
1747 -- Duplicate_Subexpr_Move_Checks --
1748 -----------------------------------
1749
1750 function Duplicate_Subexpr_Move_Checks
1751 (Exp : Node_Id;
23e83313 1752 Name_Req : Boolean := False) return Node_Id
226494a3 1753 is
1754 New_Exp : Node_Id;
226494a3 1755 begin
1756 Remove_Side_Effects (Exp, Name_Req);
1757 New_Exp := New_Copy_Tree (Exp);
1758 Remove_Checks (Exp);
1759 return New_Exp;
1760 end Duplicate_Subexpr_Move_Checks;
1761
ee6ba406 1762 --------------------
1763 -- Ensure_Defined --
1764 --------------------
1765
1766 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1767 IR : Node_Id;
ee6ba406 1768
1769 begin
f5fbfaa6 1770 -- An itype reference must only be created if this is a local itype, so
1771 -- that gigi can elaborate it on the proper objstack.
0cd40f50 1772
1773 if Is_Itype (Typ)
236f09e1 1774 and then Scope (Typ) = Current_Scope
0cd40f50 1775 then
ee6ba406 1776 IR := Make_Itype_Reference (Sloc (N));
1777 Set_Itype (IR, Typ);
0cd40f50 1778 Insert_Action (N, IR);
ee6ba406 1779 end if;
1780 end Ensure_Defined;
1781
f89cc618 1782 --------------------
1783 -- Entry_Names_OK --
1784 --------------------
1785
1786 function Entry_Names_OK return Boolean is
1787 begin
1788 return
1789 not Restricted_Profile
1790 and then not Global_Discard_Names
1791 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1792 and then not Restriction_Active (No_Local_Allocators);
1793 end Entry_Names_OK;
1794
54665c5c 1795 -------------------
1796 -- Evaluate_Name --
1797 -------------------
1798
1799 procedure Evaluate_Name (Nam : Node_Id) is
1800 K : constant Node_Kind := Nkind (Nam);
1801
1802 begin
1803 -- For an explicit dereference, we simply force the evaluation of the
1804 -- name expression. The dereference provides a value that is the address
1805 -- for the renamed object, and it is precisely this value that we want
1806 -- to preserve.
1807
1808 if K = N_Explicit_Dereference then
1809 Force_Evaluation (Prefix (Nam));
1810
1811 -- For a selected component, we simply evaluate the prefix
1812
1813 elsif K = N_Selected_Component then
1814 Evaluate_Name (Prefix (Nam));
1815
1816 -- For an indexed component, or an attribute reference, we evaluate the
1817 -- prefix, which is itself a name, recursively, and then force the
1818 -- evaluation of all the subscripts (or attribute expressions).
1819
1820 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
1821 Evaluate_Name (Prefix (Nam));
1822
1823 declare
1824 E : Node_Id;
1825
1826 begin
1827 E := First (Expressions (Nam));
1828 while Present (E) loop
1829 Force_Evaluation (E);
1830
1831 if Original_Node (E) /= E then
1832 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
1833 end if;
1834
1835 Next (E);
1836 end loop;
1837 end;
1838
1839 -- For a slice, we evaluate the prefix, as for the indexed component
1840 -- case and then, if there is a range present, either directly or as the
1841 -- constraint of a discrete subtype indication, we evaluate the two
1842 -- bounds of this range.
1843
1844 elsif K = N_Slice then
1845 Evaluate_Name (Prefix (Nam));
1846
1847 declare
1848 DR : constant Node_Id := Discrete_Range (Nam);
1849 Constr : Node_Id;
1850 Rexpr : Node_Id;
1851
1852 begin
1853 if Nkind (DR) = N_Range then
1854 Force_Evaluation (Low_Bound (DR));
1855 Force_Evaluation (High_Bound (DR));
1856
1857 elsif Nkind (DR) = N_Subtype_Indication then
1858 Constr := Constraint (DR);
1859
1860 if Nkind (Constr) = N_Range_Constraint then
1861 Rexpr := Range_Expression (Constr);
1862
1863 Force_Evaluation (Low_Bound (Rexpr));
1864 Force_Evaluation (High_Bound (Rexpr));
1865 end if;
1866 end if;
1867 end;
1868
1869 -- For a type conversion, the expression of the conversion must be the
1870 -- name of an object, and we simply need to evaluate this name.
1871
1872 elsif K = N_Type_Conversion then
1873 Evaluate_Name (Expression (Nam));
1874
1875 -- For a function call, we evaluate the call
1876
1877 elsif K = N_Function_Call then
1878 Force_Evaluation (Nam);
1879
1880 -- The remaining cases are direct name, operator symbol and character
1881 -- literal. In all these cases, we do nothing, since we want to
1882 -- reevaluate each time the renamed object is used.
1883
1884 else
1885 return;
1886 end if;
1887 end Evaluate_Name;
1888
ee6ba406 1889 ---------------------
1890 -- Evolve_And_Then --
1891 ---------------------
1892
1893 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1894 begin
1895 if No (Cond) then
1896 Cond := Cond1;
1897 else
1898 Cond :=
1899 Make_And_Then (Sloc (Cond1),
1900 Left_Opnd => Cond,
1901 Right_Opnd => Cond1);
1902 end if;
1903 end Evolve_And_Then;
1904
1905 --------------------
1906 -- Evolve_Or_Else --
1907 --------------------
1908
1909 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1910 begin
1911 if No (Cond) then
1912 Cond := Cond1;
1913 else
1914 Cond :=
1915 Make_Or_Else (Sloc (Cond1),
1916 Left_Opnd => Cond,
1917 Right_Opnd => Cond1);
1918 end if;
1919 end Evolve_Or_Else;
1920
1921 ------------------------------
1922 -- Expand_Subtype_From_Expr --
1923 ------------------------------
1924
1925 -- This function is applicable for both static and dynamic allocation of
1926 -- objects which are constrained by an initial expression. Basically it
1927 -- transforms an unconstrained subtype indication into a constrained one.
dc74650f 1928
ee6ba406 1929 -- The expression may also be transformed in certain cases in order to
014e9448 1930 -- avoid multiple evaluation. In the static allocation case, the general
1931 -- scheme is:
ee6ba406 1932
1933 -- Val : T := Expr;
1934
1935 -- is transformed into
1936
1937 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1938 --
1939 -- Here are the main cases :
1940 --
1941 -- <if Expr is a Slice>
1942 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1943 --
1944 -- <elsif Expr is a String Literal>
1945 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1946 --
1947 -- <elsif Expr is Constrained>
1948 -- subtype T is Type_Of_Expr
1949 -- Val : T := Expr;
1950 --
1951 -- <elsif Expr is an entity_name>
da253936 1952 -- Val : T (constraints taken from Expr) := Expr;
ee6ba406 1953 --
1954 -- <else>
1955 -- type Axxx is access all T;
1956 -- Rval : Axxx := Expr'ref;
da253936 1957 -- Val : T (constraints taken from Rval) := Rval.all;
ee6ba406 1958
1959 -- ??? note: when the Expression is allocated in the secondary stack
1960 -- we could use it directly instead of copying it by declaring
1961 -- Val : T (...) renames Rval.all
1962
1963 procedure Expand_Subtype_From_Expr
1964 (N : Node_Id;
1965 Unc_Type : Entity_Id;
1966 Subtype_Indic : Node_Id;
1967 Exp : Node_Id)
1968 is
1969 Loc : constant Source_Ptr := Sloc (N);
1970 Exp_Typ : constant Entity_Id := Etype (Exp);
1971 T : Entity_Id;
1972
1973 begin
1974 -- In general we cannot build the subtype if expansion is disabled,
1975 -- because internal entities may not have been defined. However, to
bef6f73c 1976 -- avoid some cascaded errors, we try to continue when the expression is
1977 -- an array (or string), because it is safe to compute the bounds. It is
1978 -- in fact required to do so even in a generic context, because there
1979 -- may be constants that depend on the bounds of a string literal, both
1980 -- standard string types and more generally arrays of characters.
ee6ba406 1981
1982 if not Expander_Active
1983 and then (No (Etype (Exp))
bef6f73c 1984 or else not Is_String_Type (Etype (Exp)))
ee6ba406 1985 then
1986 return;
1987 end if;
1988
1989 if Nkind (Exp) = N_Slice then
1990 declare
1991 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1992
1993 begin
1994 Rewrite (Subtype_Indic,
1995 Make_Subtype_Indication (Loc,
1996 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1997 Constraint =>
1998 Make_Index_Or_Discriminant_Constraint (Loc,
1999 Constraints => New_List
2000 (New_Reference_To (Slice_Type, Loc)))));
2001
526aedbb 2002 -- This subtype indication may be used later for constraint checks
ee6ba406 2003 -- we better make sure that if a variable was used as a bound of
2004 -- of the original slice, its value is frozen.
2005
2006 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
2007 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
2008 end;
2009
2010 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2011 Rewrite (Subtype_Indic,
2012 Make_Subtype_Indication (Loc,
2013 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
2014 Constraint =>
2015 Make_Index_Or_Discriminant_Constraint (Loc,
2016 Constraints => New_List (
2017 Make_Literal_Range (Loc,
db1260ab 2018 Literal_Typ => Exp_Typ)))));
ee6ba406 2019
2020 elsif Is_Constrained (Exp_Typ)
2021 and then not Is_Class_Wide_Type (Unc_Type)
2022 then
2023 if Is_Itype (Exp_Typ) then
2024
aad6babd 2025 -- Within an initialization procedure, a selected component
dc74650f 2026 -- denotes a component of the enclosing record, and it appears as
2027 -- an actual in a call to its own initialization procedure. If
2028 -- this component depends on the outer discriminant, we must
aad6babd 2029 -- generate the proper actual subtype for it.
ee6ba406 2030
aad6babd 2031 if Nkind (Exp) = N_Selected_Component
2032 and then Within_Init_Proc
2033 then
2034 declare
2035 Decl : constant Node_Id :=
2036 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2037 begin
2038 if Present (Decl) then
2039 Insert_Action (N, Decl);
2040 T := Defining_Identifier (Decl);
2041 else
2042 T := Exp_Typ;
2043 end if;
2044 end;
2045
2046 -- No need to generate a new one (new what???)
2047
2048 else
2049 T := Exp_Typ;
2050 end if;
ee6ba406 2051
2052 else
11deeeb6 2053 T := Make_Temporary (Loc, 'T');
ee6ba406 2054
2055 Insert_Action (N,
2056 Make_Subtype_Declaration (Loc,
2057 Defining_Identifier => T,
2058 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
2059
dc74650f 2060 -- This type is marked as an itype even though it has an explicit
2061 -- declaration since otherwise Is_Generic_Actual_Type can get
2062 -- set, resulting in the generation of spurious errors. (See
2063 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
ee6ba406 2064
2065 Set_Is_Itype (T);
2066 Set_Associated_Node_For_Itype (T, Exp);
2067 end if;
2068
2069 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
2070
0b16c8b7 2071 -- Nothing needs to be done for private types with unknown discriminants
3e7bf7ba 2072 -- if the underlying type is not an unconstrained composite type or it
2073 -- is an unchecked union.
ee6ba406 2074
2075 elsif Is_Private_Type (Unc_Type)
2076 and then Has_Unknown_Discriminants (Unc_Type)
2077 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
0b16c8b7 2078 or else Is_Constrained (Underlying_Type (Unc_Type))
2079 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
ee6ba406 2080 then
2081 null;
2082
bfb2f922 2083 -- Case of derived type with unknown discriminants where the parent type
2084 -- also has unknown discriminants.
76a1c25b 2085
2086 elsif Is_Record_Type (Unc_Type)
2087 and then not Is_Class_Wide_Type (Unc_Type)
2088 and then Has_Unknown_Discriminants (Unc_Type)
2089 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2090 then
bfb2f922 2091 -- Nothing to be done if no underlying record view available
2092
2093 if No (Underlying_Record_View (Unc_Type)) then
2094 null;
2095
2096 -- Otherwise use the Underlying_Record_View to create the proper
2097 -- constrained subtype for an object of a derived type with unknown
2098 -- discriminants.
2099
2100 else
2101 Remove_Side_Effects (Exp);
2102 Rewrite (Subtype_Indic,
2103 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2104 end if;
76a1c25b 2105
dffd0a90 2106 -- Renamings of class-wide interface types require no equivalent
2107 -- constrained type declarations because we only need to reference
bb3b440a 2108 -- the tag component associated with the interface. The same is
2109 -- presumably true for class-wide types in general, so this test
2110 -- is broadened to include all class-wide renamings, which also
2111 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2112 -- (Is this really correct, or are there some cases of class-wide
2113 -- renamings that require action in this procedure???)
dffd0a90 2114
2115 elsif Present (N)
2116 and then Nkind (N) = N_Object_Renaming_Declaration
bb3b440a 2117 and then Is_Class_Wide_Type (Unc_Type)
dffd0a90 2118 then
dffd0a90 2119 null;
2120
b2df433c 2121 -- In Ada 95 nothing to be done if the type of the expression is limited
f5fbfaa6 2122 -- because in this case the expression cannot be copied, and its use can
2123 -- only be by reference.
d62940bf 2124
b2df433c 2125 -- In Ada 2005 the context can be an object declaration whose expression
37808946 2126 -- is a function that returns in place. If the nominal subtype has
2127 -- unknown discriminants, the call still provides constraints on the
2128 -- object, and we have to create an actual subtype from it.
2129
2130 -- If the type is class-wide, the expression is dynamically tagged and
2131 -- we do not create an actual subtype either. Ditto for an interface.
e2ce06be 2132 -- For now this applies only if the type is immutably limited, and the
2133 -- function being called is build-in-place. This will have to be revised
2134 -- when build-in-place functions are generalized to other types.
37808946 2135
e2ce06be 2136 elsif Is_Immutably_Limited_Type (Exp_Typ)
37808946 2137 and then
2138 (Is_Class_Wide_Type (Exp_Typ)
2139 or else Is_Interface (Exp_Typ)
2140 or else not Has_Unknown_Discriminants (Exp_Typ)
2141 or else not Is_Composite_Type (Unc_Type))
2142 then
2143 null;
2144
0cd40f50 2145 -- For limited objects initialized with build in place function calls,
2146 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2147 -- node in the expression initializing the object, which breaks the
2148 -- circuitry that detects and adds the additional arguments to the
2149 -- called function.
2150
2151 elsif Is_Build_In_Place_Function_Call (Exp) then
2152 null;
2153
ee6ba406 2154 else
2155 Remove_Side_Effects (Exp);
2156 Rewrite (Subtype_Indic,
2157 Make_Subtype_From_Expr (Exp, Unc_Type));
2158 end if;
2159 end Expand_Subtype_From_Expr;
2160
51ad5ad2 2161 --------------------
2162 -- Find_Init_Call --
2163 --------------------
2164
2165 function Find_Init_Call
2166 (Var : Entity_Id;
2167 Rep_Clause : Node_Id) return Node_Id
2168 is
2169 Typ : constant Entity_Id := Etype (Var);
2170
2171 Init_Proc : Entity_Id;
2172 -- Initialization procedure for Typ
2173
2174 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
2175 -- Look for init call for Var starting at From and scanning the
2176 -- enclosing list until Rep_Clause or the end of the list is reached.
2177
2178 ----------------------------
2179 -- Find_Init_Call_In_List --
2180 ----------------------------
2181
2182 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
2183 Init_Call : Node_Id;
2184 begin
2185 Init_Call := From;
2186
2187 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
2188 if Nkind (Init_Call) = N_Procedure_Call_Statement
bb3b440a 2189 and then Is_Entity_Name (Name (Init_Call))
2190 and then Entity (Name (Init_Call)) = Init_Proc
51ad5ad2 2191 then
2192 return Init_Call;
2193 end if;
bb3b440a 2194
51ad5ad2 2195 Next (Init_Call);
2196 end loop;
2197
2198 return Empty;
2199 end Find_Init_Call_In_List;
2200
2201 Init_Call : Node_Id;
2202
2203 -- Start of processing for Find_Init_Call
2204
2205 begin
2206 if not Has_Non_Null_Base_Init_Proc (Typ) then
2207 -- No init proc for the type, so obviously no call to be found
2208
2209 return Empty;
2210 end if;
2211
2212 Init_Proc := Base_Init_Proc (Typ);
2213
2214 -- First scan the list containing the declaration of Var
2215
2216 Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
2217
2218 -- If not found, also look on Var's freeze actions list, if any, since
2219 -- the init call may have been moved there (case of an address clause
2220 -- applying to Var).
2221
2222 if No (Init_Call) and then Present (Freeze_Node (Var)) then
bb3b440a 2223 Init_Call :=
2224 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
51ad5ad2 2225 end if;
2226
2227 return Init_Call;
2228 end Find_Init_Call;
2229
aad6babd 2230 ------------------------
76a1c25b 2231 -- Find_Interface_ADT --
aad6babd 2232 ------------------------
2233
e1c20931 2234 function Find_Interface_ADT
2235 (T : Entity_Id;
d34432fa 2236 Iface : Entity_Id) return Elmt_Id
e1c20931 2237 is
a652dd51 2238 ADT : Elmt_Id;
2239 Typ : Entity_Id := T;
e1c20931 2240
2241 begin
343d35dc 2242 pragma Assert (Is_Interface (Iface));
2243
e1c20931 2244 -- Handle private types
2245
2246 if Has_Private_Declaration (Typ)
2247 and then Present (Full_View (Typ))
2248 then
2249 Typ := Full_View (Typ);
2250 end if;
2251
2252 -- Handle access types
2253
2254 if Is_Access_Type (Typ) then
d972a221 2255 Typ := Designated_Type (Typ);
e1c20931 2256 end if;
2257
2258 -- Handle task and protected types implementing interfaces
2259
343d35dc 2260 if Is_Concurrent_Type (Typ) then
e1c20931 2261 Typ := Corresponding_Record_Type (Typ);
2262 end if;
2263
343d35dc 2264 pragma Assert
2265 (not Is_Class_Wide_Type (Typ)
2266 and then Ekind (Typ) /= E_Incomplete_Type);
2267
cb4af01d 2268 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
a652dd51 2269 return First_Elmt (Access_Disp_Table (Typ));
2270
2271 else
2272 ADT :=
2273 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2274 while Present (ADT)
2275 and then Present (Related_Type (Node (ADT)))
2276 and then Related_Type (Node (ADT)) /= Iface
cb4af01d 2277 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2278 Use_Full_View => True)
a652dd51 2279 loop
2280 Next_Elmt (ADT);
2281 end loop;
2282
2283 pragma Assert (Present (Related_Type (Node (ADT))));
2284 return ADT;
2285 end if;
e1c20931 2286 end Find_Interface_ADT;
2287
2288 ------------------------
2289 -- Find_Interface_Tag --
2290 ------------------------
2291
2292 function Find_Interface_Tag
343d35dc 2293 (T : Entity_Id;
2294 Iface : Entity_Id) return Entity_Id
aad6babd 2295 is
e1c20931 2296 AI_Tag : Entity_Id;
343d35dc 2297 Found : Boolean := False;
e1c20931 2298 Typ : Entity_Id := T;
aad6babd 2299
35889f1e 2300 procedure Find_Tag (Typ : Entity_Id);
e1c20931 2301 -- Internal subprogram used to recursively climb to the ancestors
aad6babd 2302
041a8137 2303 --------------
2304 -- Find_Tag --
2305 --------------
aad6babd 2306
35889f1e 2307 procedure Find_Tag (Typ : Entity_Id) is
aad6babd 2308 AI_Elmt : Elmt_Id;
2309 AI : Node_Id;
2310
2311 begin
dffd0a90 2312 -- This routine does not handle the case in which the interface is an
2313 -- ancestor of Typ. That case is handled by the enclosing subprogram.
aad6babd 2314
dffd0a90 2315 pragma Assert (Typ /= Iface);
aad6babd 2316
76a1c25b 2317 -- Climb to the root type handling private types
2318
a652dd51 2319 if Present (Full_View (Etype (Typ))) then
76a1c25b 2320 if Full_View (Etype (Typ)) /= Typ then
2321 Find_Tag (Full_View (Etype (Typ)));
2322 end if;
aad6babd 2323
76a1c25b 2324 elsif Etype (Typ) /= Typ then
e1c20931 2325 Find_Tag (Etype (Typ));
aad6babd 2326 end if;
2327
2328 -- Traverse the list of interfaces implemented by the type
2329
2330 if not Found
a652dd51 2331 and then Present (Interfaces (Typ))
2332 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
aad6babd 2333 then
d62940bf 2334 -- Skip the tag associated with the primary table
aad6babd 2335
a652dd51 2336 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2337 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2338 pragma Assert (Present (AI_Tag));
aad6babd 2339
a652dd51 2340 AI_Elmt := First_Elmt (Interfaces (Typ));
aad6babd 2341 while Present (AI_Elmt) loop
2342 AI := Node (AI_Elmt);
2343
cb4af01d 2344 if AI = Iface
2345 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2346 then
aad6babd 2347 Found := True;
2348 return;
2349 end if;
2350
2351 AI_Tag := Next_Tag_Component (AI_Tag);
2352 Next_Elmt (AI_Elmt);
aad6babd 2353 end loop;
2354 end if;
e1c20931 2355 end Find_Tag;
2356
2357 -- Start of processing for Find_Interface_Tag
aad6babd 2358
2359 begin
76a1c25b 2360 pragma Assert (Is_Interface (Iface));
2361
e1c20931 2362 -- Handle access types
aad6babd 2363
e1c20931 2364 if Is_Access_Type (Typ) then
d972a221 2365 Typ := Designated_Type (Typ);
e1c20931 2366 end if;
aad6babd 2367
21ead1c6 2368 -- Handle class-wide types
aad6babd 2369
21ead1c6 2370 if Is_Class_Wide_Type (Typ) then
2371 Typ := Root_Type (Typ);
e1c20931 2372 end if;
2373
21ead1c6 2374 -- Handle private types
2375
2376 if Has_Private_Declaration (Typ)
2377 and then Present (Full_View (Typ))
2378 then
2379 Typ := Full_View (Typ);
d62940bf 2380 end if;
2381
2382 -- Handle entities from the limited view
2383
2384 if Ekind (Typ) = E_Incomplete_Type then
2385 pragma Assert (Present (Non_Limited_View (Typ)));
2386 Typ := Non_Limited_View (Typ);
2387 end if;
2388
21ead1c6 2389 -- Handle task and protected types implementing interfaces
2390
2391 if Is_Concurrent_Type (Typ) then
2392 Typ := Corresponding_Record_Type (Typ);
2393 end if;
2394
dffd0a90 2395 -- If the interface is an ancestor of the type, then it shared the
2396 -- primary dispatch table.
2397
cb4af01d 2398 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
dffd0a90 2399 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2400 return First_Tag_Component (Typ);
2401
2402 -- Otherwise we need to search for its associated tag component
2403
2404 else
2405 Find_Tag (Typ);
2406 pragma Assert (Found);
2407 return AI_Tag;
2408 end if;
a652dd51 2409 end Find_Interface_Tag;
041a8137 2410
ee6ba406 2411 ------------------
2412 -- Find_Prim_Op --
2413 ------------------
2414
2415 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
2416 Prim : Elmt_Id;
2417 Typ : Entity_Id := T;
35889f1e 2418 Op : Entity_Id;
ee6ba406 2419
2420 begin
2421 if Is_Class_Wide_Type (Typ) then
2422 Typ := Root_Type (Typ);
2423 end if;
2424
2425 Typ := Underlying_Type (Typ);
2426
35889f1e 2427 -- Loop through primitive operations
2428
ee6ba406 2429 Prim := First_Elmt (Primitive_Operations (Typ));
35889f1e 2430 while Present (Prim) loop
2431 Op := Node (Prim);
2432
2433 -- We can retrieve primitive operations by name if it is an internal
2434 -- name. For equality we must check that both of its operands have
2435 -- the same type, to avoid confusion with user-defined equalities
2436 -- than may have a non-symmetric signature.
2437
2438 exit when Chars (Op) = Name
2439 and then
2440 (Name /= Name_Op_Eq
30592778 2441 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
35889f1e 2442
ee6ba406 2443 Next_Elmt (Prim);
bd507c5d 2444
9c35ffe7 2445 -- Raise Program_Error if no primitive found
2446
bd507c5d 2447 if No (Prim) then
2448 raise Program_Error;
2449 end if;
ee6ba406 2450 end loop;
2451
2452 return Node (Prim);
2453 end Find_Prim_Op;
2454
343d35dc 2455 ------------------
2456 -- Find_Prim_Op --
2457 ------------------
2458
9dfe12ae 2459 function Find_Prim_Op
2460 (T : Entity_Id;
2461 Name : TSS_Name_Type) return Entity_Id
2462 is
bb3b440a 2463 Inher_Op : Entity_Id := Empty;
2464 Own_Op : Entity_Id := Empty;
2465 Prim_Elmt : Elmt_Id;
2466 Prim_Id : Entity_Id;
2467 Typ : Entity_Id := T;
9dfe12ae 2468
2469 begin
2470 if Is_Class_Wide_Type (Typ) then
2471 Typ := Root_Type (Typ);
2472 end if;
2473
2474 Typ := Underlying_Type (Typ);
2475
bb3b440a 2476 -- This search is based on the assertion that the dispatching version
2477 -- of the TSS routine always precedes the real primitive.
bd507c5d 2478
bb3b440a 2479 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2480 while Present (Prim_Elmt) loop
2481 Prim_Id := Node (Prim_Elmt);
9c35ffe7 2482
bb3b440a 2483 if Is_TSS (Prim_Id, Name) then
2484 if Present (Alias (Prim_Id)) then
2485 Inher_Op := Prim_Id;
2486 else
2487 Own_Op := Prim_Id;
2488 end if;
bd507c5d 2489 end if;
bb3b440a 2490
2491 Next_Elmt (Prim_Elmt);
9dfe12ae 2492 end loop;
2493
bb3b440a 2494 if Present (Own_Op) then
2495 return Own_Op;
2496 elsif Present (Inher_Op) then
2497 return Inher_Op;
2498 else
2499 raise Program_Error;
2500 end if;
9dfe12ae 2501 end Find_Prim_Op;
2502
57993a53 2503 ----------------------------
2504 -- Find_Protection_Object --
2505 ----------------------------
2506
2507 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2508 S : Entity_Id;
2509
2510 begin
2511 S := Scop;
2512 while Present (S) loop
2513 if (Ekind (S) = E_Entry
2514 or else Ekind (S) = E_Entry_Family
2515 or else Ekind (S) = E_Function
2516 or else Ekind (S) = E_Procedure)
2517 and then Present (Protection_Object (S))
2518 then
2519 return Protection_Object (S);
2520 end if;
2521
2522 S := Scope (S);
2523 end loop;
2524
2525 -- If we do not find a Protection object in the scope chain, then
2526 -- something has gone wrong, most likely the object was never created.
2527
2528 raise Program_Error;
2529 end Find_Protection_Object;
2530
bb3b440a 2531 --------------------------
2532 -- Find_Protection_Type --
2533 --------------------------
2534
2535 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2536 Comp : Entity_Id;
2537 Typ : Entity_Id := Conc_Typ;
2538
2539 begin
2540 if Is_Concurrent_Type (Typ) then
2541 Typ := Corresponding_Record_Type (Typ);
2542 end if;
2543
06eec79f 2544 -- Since restriction violations are not considered serious errors, the
2545 -- expander remains active, but may leave the corresponding record type
2546 -- malformed. In such cases, component _object is not available so do
2547 -- not look for it.
2548
2549 if not Analyzed (Typ) then
2550 return Empty;
2551 end if;
2552
bb3b440a 2553 Comp := First_Component (Typ);
2554 while Present (Comp) loop
2555 if Chars (Comp) = Name_uObject then
2556 return Base_Type (Etype (Comp));
2557 end if;
2558
2559 Next_Component (Comp);
2560 end loop;
2561
2562 -- The corresponding record of a protected type should always have an
2563 -- _object field.
2564
2565 raise Program_Error;
2566 end Find_Protection_Type;
2567
ee6ba406 2568 ----------------------
2569 -- Force_Evaluation --
2570 ----------------------
2571
2572 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
2573 begin
1735f911 2574 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
ee6ba406 2575 end Force_Evaluation;
2576
c6a30f24 2577 ---------------------------------
2578 -- Fully_Qualified_Name_String --
2579 ---------------------------------
2580
2581 function Fully_Qualified_Name_String (E : Entity_Id) return String_Id is
2582 procedure Internal_Full_Qualified_Name (E : Entity_Id);
2583 -- Compute recursively the qualified name without NUL at the end, adding
2584 -- it to the currently started string being generated
2585
2586 ----------------------------------
2587 -- Internal_Full_Qualified_Name --
2588 ----------------------------------
2589
2590 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
2591 Ent : Entity_Id;
2592
2593 begin
2594 -- Deal properly with child units
2595
2596 if Nkind (E) = N_Defining_Program_Unit_Name then
2597 Ent := Defining_Identifier (E);
2598 else
2599 Ent := E;
2600 end if;
2601
2602 -- Compute qualification recursively (only "Standard" has no scope)
2603
2604 if Present (Scope (Scope (Ent))) then
2605 Internal_Full_Qualified_Name (Scope (Ent));
2606 Store_String_Char (Get_Char_Code ('.'));
2607 end if;
2608
2609 -- Every entity should have a name except some expanded blocks
2610 -- don't bother about those.
2611
2612 if Chars (Ent) = No_Name then
2613 return;
2614 end if;
2615
2616 -- Generates the entity name in upper case
2617
2618 Get_Decoded_Name_String (Chars (Ent));
2619 Set_All_Upper_Case;
2620 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2621 return;
2622 end Internal_Full_Qualified_Name;
2623
2624 -- Start of processing for Full_Qualified_Name
2625
2626 begin
2627 Start_String;
2628 Internal_Full_Qualified_Name (E);
2629 Store_String_Char (Get_Char_Code (ASCII.NUL));
2630 return End_String;
2631 end Fully_Qualified_Name_String;
2632
ee6ba406 2633 ------------------------
2634 -- Generate_Poll_Call --
2635 ------------------------
2636
2637 procedure Generate_Poll_Call (N : Node_Id) is
2638 begin
2639 -- No poll call if polling not active
2640
2641 if not Polling_Required then
2642 return;
2643
2644 -- Otherwise generate require poll call
2645
2646 else
2647 Insert_Before_And_Analyze (N,
2648 Make_Procedure_Call_Statement (Sloc (N),
2649 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
2650 end if;
2651 end Generate_Poll_Call;
2652
9dfe12ae 2653 ---------------------------------
2654 -- Get_Current_Value_Condition --
2655 ---------------------------------
2656
014e9448 2657 -- Note: the implementation of this procedure is very closely tied to the
2658 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
2659 -- interpret Current_Value fields set by the Set procedure, so the two
2660 -- procedures need to be closely coordinated.
2661
9dfe12ae 2662 procedure Get_Current_Value_Condition
2663 (Var : Node_Id;
2664 Op : out Node_Kind;
2665 Val : out Node_Id)
2666 is
35889f1e 2667 Loc : constant Source_Ptr := Sloc (Var);
2668 Ent : constant Entity_Id := Entity (Var);
9dfe12ae 2669
014e9448 2670 procedure Process_Current_Value_Condition
2671 (N : Node_Id;
2672 S : Boolean);
2673 -- N is an expression which holds either True (S = True) or False (S =
2674 -- False) in the condition. This procedure digs out the expression and
2675 -- if it refers to Ent, sets Op and Val appropriately.
2676
2677 -------------------------------------
2678 -- Process_Current_Value_Condition --
2679 -------------------------------------
2680
2681 procedure Process_Current_Value_Condition
2682 (N : Node_Id;
2683 S : Boolean)
2684 is
2685 Cond : Node_Id;
2686 Sens : Boolean;
2687
2688 begin
2689 Cond := N;
2690 Sens := S;
2691
2692 -- Deal with NOT operators, inverting sense
2693
2694 while Nkind (Cond) = N_Op_Not loop
2695 Cond := Right_Opnd (Cond);
2696 Sens := not Sens;
2697 end loop;
2698
2699 -- Deal with AND THEN and AND cases
2700
2701 if Nkind (Cond) = N_And_Then
2702 or else Nkind (Cond) = N_Op_And
2703 then
f5fbfaa6 2704 -- Don't ever try to invert a condition that is of the form of an
2705 -- AND or AND THEN (since we are not doing sufficiently general
2706 -- processing to allow this).
014e9448 2707
2708 if Sens = False then
2709 Op := N_Empty;
2710 Val := Empty;
2711 return;
2712 end if;
2713
2714 -- Recursively process AND and AND THEN branches
2715
2716 Process_Current_Value_Condition (Left_Opnd (Cond), True);
2717
2718 if Op /= N_Empty then
2719 return;
2720 end if;
2721
2722 Process_Current_Value_Condition (Right_Opnd (Cond), True);
2723 return;
2724
2725 -- Case of relational operator
2726
2727 elsif Nkind (Cond) in N_Op_Compare then
2728 Op := Nkind (Cond);
2729
2730 -- Invert sense of test if inverted test
2731
2732 if Sens = False then
2733 case Op is
2734 when N_Op_Eq => Op := N_Op_Ne;
2735 when N_Op_Ne => Op := N_Op_Eq;
2736 when N_Op_Lt => Op := N_Op_Ge;
2737 when N_Op_Gt => Op := N_Op_Le;
2738 when N_Op_Le => Op := N_Op_Gt;
2739 when N_Op_Ge => Op := N_Op_Lt;
2740 when others => raise Program_Error;
2741 end case;
2742 end if;
2743
2744 -- Case of entity op value
2745
2746 if Is_Entity_Name (Left_Opnd (Cond))
2747 and then Ent = Entity (Left_Opnd (Cond))
2748 and then Compile_Time_Known_Value (Right_Opnd (Cond))
2749 then
2750 Val := Right_Opnd (Cond);
2751
2752 -- Case of value op entity
2753
2754 elsif Is_Entity_Name (Right_Opnd (Cond))
2755 and then Ent = Entity (Right_Opnd (Cond))
2756 and then Compile_Time_Known_Value (Left_Opnd (Cond))
2757 then
2758 Val := Left_Opnd (Cond);
2759
2760 -- We are effectively swapping operands
2761
2762 case Op is
2763 when N_Op_Eq => null;
2764 when N_Op_Ne => null;
2765 when N_Op_Lt => Op := N_Op_Gt;
2766 when N_Op_Gt => Op := N_Op_Lt;
2767 when N_Op_Le => Op := N_Op_Ge;
2768 when N_Op_Ge => Op := N_Op_Le;
2769 when others => raise Program_Error;
2770 end case;
2771
2772 else
2773 Op := N_Empty;
2774 end if;
2775
2776 return;
2777
2778 -- Case of Boolean variable reference, return as though the
2779 -- reference had said var = True.
2780
2781 else
2782 if Is_Entity_Name (Cond)
2783 and then Ent = Entity (Cond)
2784 then
2785 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
2786
2787 if Sens = False then
2788 Op := N_Op_Ne;
2789 else
2790 Op := N_Op_Eq;
2791 end if;
2792 end if;
2793 end if;
2794 end Process_Current_Value_Condition;
2795
2796 -- Start of processing for Get_Current_Value_Condition
2797
9dfe12ae 2798 begin
2799 Op := N_Empty;
2800 Val := Empty;
2801
35889f1e 2802 -- Immediate return, nothing doing, if this is not an object
9dfe12ae 2803
35889f1e 2804 if Ekind (Ent) not in Object_Kind then
2805 return;
2806 end if;
9dfe12ae 2807
35889f1e 2808 -- Otherwise examine current value
9dfe12ae 2809
35889f1e 2810 declare
2811 CV : constant Node_Id := Current_Value (Ent);
2812 Sens : Boolean;
2813 Stm : Node_Id;
9dfe12ae 2814
35889f1e 2815 begin
2816 -- If statement. Condition is known true in THEN section, known False
2817 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
9dfe12ae 2818
35889f1e 2819 if Nkind (CV) = N_If_Statement then
9dfe12ae 2820
35889f1e 2821 -- Before start of IF statement
9dfe12ae 2822
35889f1e 2823 if Loc < Sloc (CV) then
2824 return;
9dfe12ae 2825
35889f1e 2826 -- After end of IF statement
9dfe12ae 2827
35889f1e 2828 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
2829 return;
2830 end if;
9dfe12ae 2831
35889f1e 2832 -- At this stage we know that we are within the IF statement, but
2833 -- unfortunately, the tree does not record the SLOC of the ELSE so
2834 -- we cannot use a simple SLOC comparison to distinguish between
2835 -- the then/else statements, so we have to climb the tree.
9dfe12ae 2836
35889f1e 2837 declare
2838 N : Node_Id;
9dfe12ae 2839
35889f1e 2840 begin
2841 N := Parent (Var);
2842 while Parent (N) /= CV loop
2843 N := Parent (N);
9dfe12ae 2844
35889f1e 2845 -- If we fall off the top of the tree, then that's odd, but
2846 -- perhaps it could occur in some error situation, and the
2847 -- safest response is simply to assume that the outcome of
2848 -- the condition is unknown. No point in bombing during an
2849 -- attempt to optimize things.
9dfe12ae 2850
35889f1e 2851 if No (N) then
2852 return;
2853 end if;
2854 end loop;
9dfe12ae 2855
35889f1e 2856 -- Now we have N pointing to a node whose parent is the IF
2857 -- statement in question, so now we can tell if we are within
2858 -- the THEN statements.
9dfe12ae 2859
35889f1e 2860 if Is_List_Member (N)
2861 and then List_Containing (N) = Then_Statements (CV)
2862 then
2863 Sens := True;
9dfe12ae 2864
014e9448 2865 -- If the variable reference does not come from source, we
2866 -- cannot reliably tell whether it appears in the else part.
bf3e1520 2867 -- In particular, if it appears in generated code for a node
014e9448 2868 -- that requires finalization, it may be attached to a list
2869 -- that has not been yet inserted into the code. For now,
2870 -- treat it as unknown.
2871
2872 elsif not Comes_From_Source (N) then
2873 return;
2874
2875 -- Otherwise we must be in ELSIF or ELSE part
9dfe12ae 2876
35889f1e 2877 else
2878 Sens := False;
2879 end if;
2880 end;
9dfe12ae 2881
35889f1e 2882 -- ELSIF part. Condition is known true within the referenced
f5fbfaa6 2883 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
2884 -- and unknown before the ELSE part or after the IF statement.
9dfe12ae 2885
35889f1e 2886 elsif Nkind (CV) = N_Elsif_Part then
56854dfd 2887
2888 -- if the Elsif_Part had condition_actions, the elsif has been
2889 -- rewritten as a nested if, and the original elsif_part is
2890 -- detached from the tree, so there is no way to obtain useful
2891 -- information on the current value of the variable.
2892 -- Can this be improved ???
2893
2894 if No (Parent (CV)) then
2895 return;
2896 end if;
2897
35889f1e 2898 Stm := Parent (CV);
9dfe12ae 2899
35889f1e 2900 -- Before start of ELSIF part
9dfe12ae 2901
35889f1e 2902 if Loc < Sloc (CV) then
2903 return;
9dfe12ae 2904
35889f1e 2905 -- After end of IF statement
9dfe12ae 2906
35889f1e 2907 elsif Loc >= Sloc (Stm) +
2908 Text_Ptr (UI_To_Int (End_Span (Stm)))
2909 then
2910 return;
2911 end if;
9dfe12ae 2912
35889f1e 2913 -- Again we lack the SLOC of the ELSE, so we need to climb the
2914 -- tree to see if we are within the ELSIF part in question.
9dfe12ae 2915
35889f1e 2916 declare
2917 N : Node_Id;
9dfe12ae 2918
35889f1e 2919 begin
2920 N := Parent (Var);
2921 while Parent (N) /= Stm loop
2922 N := Parent (N);
9dfe12ae 2923
35889f1e 2924 -- If we fall off the top of the tree, then that's odd, but
2925 -- perhaps it could occur in some error situation, and the
2926 -- safest response is simply to assume that the outcome of
2927 -- the condition is unknown. No point in bombing during an
2928 -- attempt to optimize things.
9dfe12ae 2929
35889f1e 2930 if No (N) then
2931 return;
2932 end if;
2933 end loop;
9dfe12ae 2934
35889f1e 2935 -- Now we have N pointing to a node whose parent is the IF
2936 -- statement in question, so see if is the ELSIF part we want.
2937 -- the THEN statements.
9dfe12ae 2938
35889f1e 2939 if N = CV then
2940 Sens := True;
9dfe12ae 2941
526aedbb 2942 -- Otherwise we must be in subsequent ELSIF or ELSE part
9dfe12ae 2943
35889f1e 2944 else
2945 Sens := False;
2946 end if;
2947 end;
9dfe12ae 2948
014e9448 2949 -- Iteration scheme of while loop. The condition is known to be
2950 -- true within the body of the loop.
35889f1e 2951
014e9448 2952 elsif Nkind (CV) = N_Iteration_Scheme then
2953 declare
2954 Loop_Stmt : constant Node_Id := Parent (CV);
9dfe12ae 2955
014e9448 2956 begin
2957 -- Before start of body of loop
9dfe12ae 2958
014e9448 2959 if Loc < Sloc (Loop_Stmt) then
2960 return;
9dfe12ae 2961
014e9448 2962 -- After end of LOOP statement
35889f1e 2963
014e9448 2964 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2965 return;
35889f1e 2966
014e9448 2967 -- We are within the body of the loop
35889f1e 2968
014e9448 2969 else
2970 Sens := True;
2971 end if;
2972 end;
9dfe12ae 2973
014e9448 2974 -- All other cases of Current_Value settings
9dfe12ae 2975
014e9448 2976 else
2977 return;
35889f1e 2978 end if;
014e9448 2979
2980 -- If we fall through here, then we have a reportable condition, Sens
2981 -- is True if the condition is true and False if it needs inverting.
2982
2983 Process_Current_Value_Condition (Condition (CV), Sens);
35889f1e 2984 end;
9dfe12ae 2985 end Get_Current_Value_Condition;
2986
3430bf31 2987 ---------------------
2988 -- Get_Stream_Size --
2989 ---------------------
2990
2991 function Get_Stream_Size (E : Entity_Id) return Uint is
2992 begin
2993 -- If we have a Stream_Size clause for this type use it
2994
2995 if Has_Stream_Size_Clause (E) then
2996 return Static_Integer (Expression (Stream_Size_Clause (E)));
2997
2998 -- Otherwise the Stream_Size if the size of the type
2999
3000 else
3001 return Esize (E);
3002 end if;
3003 end Get_Stream_Size;
3004
bb3b440a 3005 ---------------------------
3006 -- Has_Access_Constraint --
3007 ---------------------------
3008
3009 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3010 Disc : Entity_Id;
3011 T : constant Entity_Id := Etype (E);
3012
3013 begin
3014 if Has_Per_Object_Constraint (E)
3015 and then Has_Discriminants (T)
3016 then
3017 Disc := First_Discriminant (T);
3018 while Present (Disc) loop
3019 if Is_Access_Type (Etype (Disc)) then
3020 return True;
3021 end if;
3022
3023 Next_Discriminant (Disc);
3024 end loop;
3025
3026 return False;
3027 else
3028 return False;
3029 end if;
3030 end Has_Access_Constraint;
3031
bb3b440a 3032 ----------------------------------
3033 -- Has_Following_Address_Clause --
3034 ----------------------------------
950e1932 3035
3036 -- Should this function check the private part in a package ???
3037
3038 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
3039 Id : constant Entity_Id := Defining_Identifier (D);
3040 Decl : Node_Id;
3041
3042 begin
3043 Decl := Next (D);
3044 while Present (Decl) loop
3045 if Nkind (Decl) = N_At_Clause
3046 and then Chars (Identifier (Decl)) = Chars (Id)
3047 then
3048 return True;
3049
3050 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3051 and then Chars (Decl) = Name_Address
3052 and then Chars (Name (Decl)) = Chars (Id)
3053 then
3054 return True;
3055 end if;
3056
3057 Next (Decl);
3058 end loop;
3059
3060 return False;
3061 end Has_Following_Address_Clause;
3062
ee6ba406 3063 --------------------
3064 -- Homonym_Number --
3065 --------------------
3066
3067 function Homonym_Number (Subp : Entity_Id) return Nat is
3068 Count : Nat;
3069 Hom : Entity_Id;
3070
3071 begin
3072 Count := 1;
3073 Hom := Homonym (Subp);
3074 while Present (Hom) loop
3075 if Scope (Hom) = Scope (Subp) then
3076 Count := Count + 1;
3077 end if;
3078
3079 Hom := Homonym (Hom);
3080 end loop;
3081
3082 return Count;
3083 end Homonym_Number;
3084
bb3b440a 3085 -----------------------------------
3086 -- In_Library_Level_Package_Body --
3087 -----------------------------------
3088
3089 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3090 begin
3091 -- First determine whether the entity appears at the library level, then
3092 -- look at the containing unit.
3093
3094 if Is_Library_Level_Entity (Id) then
3095 declare
3096 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3097
3098 begin
3099 return Nkind (Unit (Container)) = N_Package_Body;
3100 end;
3101 end if;
3102
3103 return False;
3104 end In_Library_Level_Package_Body;
3105
ee6ba406 3106 ------------------------------
3107 -- In_Unconditional_Context --
3108 ------------------------------
3109
3110 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3111 P : Node_Id;
3112
3113 begin
3114 P := Node;
3115 while Present (P) loop
3116 case Nkind (P) is
3117 when N_Subprogram_Body =>
3118 return True;
3119
3120 when N_If_Statement =>
3121 return False;
3122
3123 when N_Loop_Statement =>
3124 return False;
3125
3126 when N_Case_Statement =>
3127 return False;
3128
3129 when others =>
3130 P := Parent (P);
3131 end case;
3132 end loop;
3133
3134 return False;
3135 end In_Unconditional_Context;
3136
3137 -------------------
3138 -- Insert_Action --
3139 -------------------
3140
3141 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3142 begin
3143 if Present (Ins_Action) then
3144 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3145 end if;
3146 end Insert_Action;
3147
3148 -- Version with check(s) suppressed
3149
3150 procedure Insert_Action
3151 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3152 is
3153 begin
3154 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3155 end Insert_Action;
3156
bb3b440a 3157 -------------------------
3158 -- Insert_Action_After --
3159 -------------------------
3160
3161 procedure Insert_Action_After
3162 (Assoc_Node : Node_Id;
3163 Ins_Action : Node_Id)
3164 is
3165 begin
3166 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3167 end Insert_Action_After;
3168
ee6ba406 3169 --------------------
3170 -- Insert_Actions --
3171 --------------------
3172
3173 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3174 N : Node_Id;
3175 P : Node_Id;
3176
3177 Wrapped_Node : Node_Id := Empty;
3178
3179 begin
3180 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3181 return;
3182 end if;
3183
57993a53 3184 -- Ignore insert of actions from inside default expression (or other
3185 -- similar "spec expression") in the special spec-expression analyze
3186 -- mode. Any insertions at this point have no relevance, since we are
3187 -- only doing the analyze to freeze the types of any static expressions.
3188 -- See section "Handling of Default Expressions" in the spec of package
3189 -- Sem for further details.
ee6ba406 3190
57993a53 3191 if In_Spec_Expression then
ee6ba406 3192 return;
3193 end if;
3194
3195 -- If the action derives from stuff inside a record, then the actions
3196 -- are attached to the current scope, to be inserted and analyzed on
dc74650f 3197 -- exit from the scope. The reason for this is that we may also be
3198 -- generating freeze actions at the same time, and they must eventually
3199 -- be elaborated in the correct order.
ee6ba406 3200
3201 if Is_Record_Type (Current_Scope)
3202 and then not Is_Frozen (Current_Scope)
3203 then
3204 if No (Scope_Stack.Table
3205 (Scope_Stack.Last).Pending_Freeze_Actions)
3206 then
3207 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3208 Ins_Actions;
3209 else
3210 Append_List
3211 (Ins_Actions,
3212 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3213 end if;
3214
3215 return;
3216 end if;
3217
3218 -- We now intend to climb up the tree to find the right point to
dc74650f 3219 -- insert the actions. We start at Assoc_Node, unless this node is a
3220 -- subexpression in which case we start with its parent. We do this for
3221 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3222 -- itself one of the special nodes like N_And_Then, then we assume that
3223 -- an initial request to insert actions for such a node does not expect
3224 -- the actions to get deposited in the node for later handling when the
3225 -- node is expanded, since clearly the node is being dealt with by the
3226 -- caller. Note that in the subexpression case, N is always the child we
3227 -- came from.
3228
3229 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3230 -- it has type Standard_Void_Type, and a subexpression otherwise.
ee6ba406 3231 -- otherwise. Procedure attribute references are also statements.
3232
3233 if Nkind (Assoc_Node) in N_Subexpr
3234 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
3235 or else Etype (Assoc_Node) /= Standard_Void_Type)
3236 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3237 or else
3238 not Is_Procedure_Attribute_Name
3239 (Attribute_Name (Assoc_Node)))
3240 then
9dfe12ae 3241 P := Assoc_Node; -- ??? does not agree with above!
ee6ba406 3242 N := Parent (Assoc_Node);
3243
dc74650f 3244 -- Non-subexpression case. Note that N is initially Empty in this case
3245 -- (N is only guaranteed Non-Empty in the subexpr case).
ee6ba406 3246
3247 else
3248 P := Assoc_Node;
3249 N := Empty;
3250 end if;
3251
3252 -- Capture root of the transient scope
3253
3254 if Scope_Is_Transient then
014e9448 3255 Wrapped_Node := Node_To_Be_Wrapped;
ee6ba406 3256 end if;
3257
3258 loop
3259 pragma Assert (Present (P));
3260
3261 case Nkind (P) is
3262
3263 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3264 -- in the Actions field of the right operand. They will be moved
3265 -- out further when the AND THEN or OR ELSE operator is expanded.
3266 -- Nothing special needs to be done for the left operand since
3267 -- in that case the actions are executed unconditionally.
3268
7b31b357 3269 when N_Short_Circuit =>
ee6ba406 3270 if N = Right_Opnd (P) then
d34432fa 3271
3272 -- We are now going to either append the actions to the
3273 -- actions field of the short-circuit operation. We will
3274 -- also analyze the actions now.
3275
3276 -- This analysis is really too early, the proper thing would
3277 -- be to just park them there now, and only analyze them if
3278 -- we find we really need them, and to it at the proper
3279 -- final insertion point. However attempting to this proved
3280 -- tricky, so for now we just kill current values before and
3281 -- after the analyze call to make sure we avoid peculiar
3282 -- optimizations from this out of order insertion.
3283
3284 Kill_Current_Values;
3285
ee6ba406 3286 if Present (Actions (P)) then
3287 Insert_List_After_And_Analyze
d34432fa 3288 (Last (Actions (P)), Ins_Actions);
ee6ba406 3289 else
3290 Set_Actions (P, Ins_Actions);
3291 Analyze_List (Actions (P));
3292 end if;
3293
d34432fa 3294 Kill_Current_Values;
3295
ee6ba406 3296 return;
3297 end if;
3298
92f1631f 3299 -- Then or Else dependent expression of an if expression. Add
3300 -- actions to Then_Actions or Else_Actions field as appropriate.
3301 -- The actions will be moved further out when the if is expanded.
ee6ba406 3302
92f1631f 3303 when N_If_Expression =>
ee6ba406 3304 declare
3305 ThenX : constant Node_Id := Next (First (Expressions (P)));
3306 ElseX : constant Node_Id := Next (ThenX);
3307
3308 begin
f5fbfaa6 3309 -- If the enclosing expression is already analyzed, as
3310 -- is the case for nested elaboration checks, insert the
3311 -- conditional further out.
ee6ba406 3312
f5fbfaa6 3313 if Analyzed (P) then
3314 null;
3315
3316 -- Actions belong to the then expression, temporarily place
92f1631f 3317 -- them as Then_Actions of the if expression. They will be
3318 -- moved to the proper place later when the if expression
3319 -- is expanded.
f5fbfaa6 3320
3321 elsif N = ThenX then
ee6ba406 3322 if Present (Then_Actions (P)) then
3323 Insert_List_After_And_Analyze
3324 (Last (Then_Actions (P)), Ins_Actions);
3325 else
3326 Set_Then_Actions (P, Ins_Actions);
3327 Analyze_List (Then_Actions (P));
3328 end if;
3329
3330 return;
3331
92f1631f 3332 -- Actions belong to the else expression, temporarily place
3333 -- them as Else_Actions of the if expression. They will be
3334 -- moved to the proper place later when the if expression
3335 -- is expanded.
ee6ba406 3336
3337 elsif N = ElseX then
3338 if Present (Else_Actions (P)) then
3339 Insert_List_After_And_Analyze
3340 (Last (Else_Actions (P)), Ins_Actions);
3341 else
3342 Set_Else_Actions (P, Ins_Actions);
3343 Analyze_List (Else_Actions (P));
3344 end if;
3345
3346 return;
3347
3348 -- Actions belong to the condition. In this case they are
3349 -- unconditionally executed, and so we can continue the
3350 -- search for the proper insert point.
3351
3352 else
3353 null;
3354 end if;
3355 end;
3356
f5fbfaa6 3357 -- Alternative of case expression, we place the action in the
3358 -- Actions field of the case expression alternative, this will
3359 -- be handled when the case expression is expanded.
e977c0cf 3360
3361 when N_Case_Expression_Alternative =>
3362 if Present (Actions (P)) then
3363 Insert_List_After_And_Analyze
3364 (Last (Actions (P)), Ins_Actions);
3365 else
3366 Set_Actions (P, Ins_Actions);
ddf1337b 3367 Analyze_List (Actions (P));
e977c0cf 3368 end if;
3369
3370 return;
3371
6b73a73b 3372 -- Case of appearing within an Expressions_With_Actions node. We
7800b920 3373 -- prepend the actions to the list of actions already there, if
3374 -- the node has not been analyzed yet. Otherwise find insertion
3375 -- location further up the tree.
6b73a73b 3376
3377 when N_Expression_With_Actions =>
7800b920 3378 if not Analyzed (P) then
3379 Prepend_List (Ins_Actions, Actions (P));
3380 return;
3381 end if;
6b73a73b 3382
ee6ba406 3383 -- Case of appearing in the condition of a while expression or
3384 -- elsif. We insert the actions into the Condition_Actions field.
3385 -- They will be moved further out when the while loop or elsif
3386 -- is analyzed.
3387
3388 when N_Iteration_Scheme |
3389 N_Elsif_Part
3390 =>
3391 if N = Condition (P) then
3392 if Present (Condition_Actions (P)) then
3393 Insert_List_After_And_Analyze
3394 (Last (Condition_Actions (P)), Ins_Actions);
3395 else
3396 Set_Condition_Actions (P, Ins_Actions);
3397
f5fbfaa6 3398 -- Set the parent of the insert actions explicitly. This
3399 -- is not a syntactic field, but we need the parent field
3400 -- set, in particular so that freeze can understand that
3401 -- it is dealing with condition actions, and properly
3402 -- insert the freezing actions.
ee6ba406 3403
3404 Set_Parent (Ins_Actions, P);
3405 Analyze_List (Condition_Actions (P));
3406 end if;
3407
3408 return;
3409 end if;
3410
23e83313 3411 -- Statements, declarations, pragmas, representation clauses
ee6ba406 3412
3413 when
3414 -- Statements
3415
3416 N_Procedure_Call_Statement |
3417 N_Statement_Other_Than_Procedure_Call |
3418
3419 -- Pragmas
3420
3421 N_Pragma |
3422
3423 -- Representation_Clause
3424
3425 N_At_Clause |
3426 N_Attribute_Definition_Clause |
3427 N_Enumeration_Representation_Clause |
3428 N_Record_Representation_Clause |
3429
3430 -- Declarations
3431
3432 N_Abstract_Subprogram_Declaration |
3433 N_Entry_Body |
3434 N_Exception_Declaration |
3435 N_Exception_Renaming_Declaration |
1cea7a8f 3436 N_Expression_Function |
7189d17f 3437 N_Formal_Abstract_Subprogram_Declaration |
3438 N_Formal_Concrete_Subprogram_Declaration |
ee6ba406 3439 N_Formal_Object_Declaration |
ee6ba406 3440 N_Formal_Type_Declaration |
3441 N_Full_Type_Declaration |
3442 N_Function_Instantiation |
3443 N_Generic_Function_Renaming_Declaration |
3444 N_Generic_Package_Declaration |
3445 N_Generic_Package_Renaming_Declaration |
3446 N_Generic_Procedure_Renaming_Declaration |
3447 N_Generic_Subprogram_Declaration |
3448 N_Implicit_Label_Declaration |
3449 N_Incomplete_Type_Declaration |
3450 N_Number_Declaration |
3451 N_Object_Declaration |
3452 N_Object_Renaming_Declaration |
3453 N_Package_Body |
3454 N_Package_Body_Stub |
3455 N_Package_Declaration |
3456 N_Package_Instantiation |
3457 N_Package_Renaming_Declaration |
3458 N_Private_Extension_Declaration |
3459 N_Private_Type_Declaration |
3460 N_Procedure_Instantiation |
236f09e1 3461 N_Protected_Body |
ee6ba406 3462 N_Protected_Body_Stub |
3463 N_Protected_Type_Declaration |
3464 N_Single_Task_Declaration |
3465 N_Subprogram_Body |
3466 N_Subprogram_Body_Stub |
3467 N_Subprogram_Declaration |
3468 N_Subprogram_Renaming_Declaration |
3469 N_Subtype_Declaration |
3470 N_Task_Body |
3471 N_Task_Body_Stub |
3472 N_Task_Type_Declaration |
3473
20486e0b 3474 -- Use clauses can appear in lists of declarations
3475
3476 N_Use_Package_Clause |
3477 N_Use_Type_Clause |
3478
ee6ba406 3479 -- Freeze entity behaves like a declaration or statement
3480
3481 N_Freeze_Entity
3482 =>
3483 -- Do not insert here if the item is not a list member (this
3484 -- happens for example with a triggering statement, and the
3485 -- proper approach is to insert before the entire select).
3486
3487 if not Is_List_Member (P) then
3488 null;
3489
3490 -- Do not insert if parent of P is an N_Component_Association
014e9448 3491 -- node (i.e. we are in the context of an N_Aggregate or
3492 -- N_Extension_Aggregate node. In this case we want to insert
3493 -- before the entire aggregate.
ee6ba406 3494
3495 elsif Nkind (Parent (P)) = N_Component_Association then
3496 null;
3497
dc74650f 3498 -- Do not insert if the parent of P is either an N_Variant node
3499 -- or an N_Record_Definition node, meaning in either case that
3500 -- P is a member of a component list, and that therefore the
3501 -- actions should be inserted outside the complete record
3502 -- declaration.
ee6ba406 3503
3504 elsif Nkind (Parent (P)) = N_Variant
3505 or else Nkind (Parent (P)) = N_Record_Definition
3506 then
3507 null;
3508
3509 -- Do not insert freeze nodes within the loop generated for
3510 -- an aggregate, because they may be elaborated too late for
3511 -- subsequent use in the back end: within a package spec the
3512 -- loop is part of the elaboration procedure and is only
3513 -- elaborated during the second pass.
f5fbfaa6 3514
dc74650f 3515 -- If the loop comes from source, or the entity is local to the
3516 -- loop itself it must remain within.
ee6ba406 3517
3518 elsif Nkind (Parent (P)) = N_Loop_Statement
3519 and then not Comes_From_Source (Parent (P))
3520 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
3521 and then
3522 Scope (Entity (First (Ins_Actions))) /= Current_Scope
3523 then
3524 null;
3525
3526 -- Otherwise we can go ahead and do the insertion
3527
014e9448 3528 elsif P = Wrapped_Node then
ee6ba406 3529 Store_Before_Actions_In_Scope (Ins_Actions);
3530 return;
3531
3532 else
3533 Insert_List_Before_And_Analyze (P, Ins_Actions);
3534 return;
3535 end if;
3536
f5fbfaa6 3537 -- A special case, N_Raise_xxx_Error can act either as a statement
3538 -- or a subexpression. We tell the difference by looking at the
3539 -- Etype. It is set to Standard_Void_Type in the statement case.
ee6ba406 3540
3541 when
3542 N_Raise_xxx_Error =>
3543 if Etype (P) = Standard_Void_Type then
3544 if P = Wrapped_Node then
3545 Store_Before_Actions_In_Scope (Ins_Actions);
3546 else
3547 Insert_List_Before_And_Analyze (P, Ins_Actions);
3548 end if;
3549
3550 return;
3551
3552 -- In the subexpression case, keep climbing
3553
3554 else
3555 null;
3556 end if;
3557
3558 -- If a component association appears within a loop created for
3559 -- an array aggregate, attach the actions to the association so
3560 -- they can be subsequently inserted within the loop. For other
9dfe12ae 3561 -- component associations insert outside of the aggregate. For
3562 -- an association that will generate a loop, its Loop_Actions
3563 -- attribute is already initialized (see exp_aggr.adb).
ee6ba406 3564
3565 -- The list of loop_actions can in turn generate additional ones,
3566 -- that are inserted before the associated node. If the associated
3567 -- node is outside the aggregate, the new actions are collected
3568 -- at the end of the loop actions, to respect the order in which
3569 -- they are to be elaborated.
3570
3571 when
3572 N_Component_Association =>
3573 if Nkind (Parent (P)) = N_Aggregate
9dfe12ae 3574 and then Present (Loop_Actions (P))
ee6ba406 3575 then
9dfe12ae 3576 if Is_Empty_List (Loop_Actions (P)) then
ee6ba406 3577 Set_Loop_Actions (P, Ins_Actions);
3578 Analyze_List (Ins_Actions);
3579
3580 else
3581 declare
23e83313 3582 Decl : Node_Id;
ee6ba406 3583
3584 begin
f5fbfaa6 3585 -- Check whether these actions were generated by a
3586 -- declaration that is part of the loop_ actions
3587 -- for the component_association.
ee6ba406 3588
23e83313 3589 Decl := Assoc_Node;
ee6ba406 3590 while Present (Decl) loop
3591 exit when Parent (Decl) = P
3592 and then Is_List_Member (Decl)
3593 and then
3594 List_Containing (Decl) = Loop_Actions (P);
3595 Decl := Parent (Decl);
3596 end loop;
3597
3598 if Present (Decl) then
3599 Insert_List_Before_And_Analyze
3600 (Decl, Ins_Actions);
3601 else
3602 Insert_List_After_And_Analyze
3603 (Last (Loop_Actions (P)), Ins_Actions);
3604 end if;
3605 end;
3606 end if;
3607
3608 return;
3609
3610 else
3611 null;
3612 end if;
3613
3614 -- Another special case, an attribute denoting a procedure call
3615
3616 when
3617 N_Attribute_Reference =>
3618 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
3619 if P = Wrapped_Node then
3620 Store_Before_Actions_In_Scope (Ins_Actions);
3621 else
3622 Insert_List_Before_And_Analyze (P, Ins_Actions);
3623 end if;
3624
3625 return;
3626
3627 -- In the subexpression case, keep climbing
3628
3629 else
3630 null;
3631 end if;
3632
6c545057 3633 -- A contract node should not belong to the tree
3634
3635 when N_Contract =>
3636 raise Program_Error;
3637
ee6ba406 3638 -- For all other node types, keep climbing tree
3639
3640 when
3641 N_Abortable_Part |
3642 N_Accept_Alternative |
3643 N_Access_Definition |
3644 N_Access_Function_Definition |
3645 N_Access_Procedure_Definition |
3646 N_Access_To_Object_Definition |
3647 N_Aggregate |
3648 N_Allocator |
f7479d84 3649 N_Aspect_Specification |
e977c0cf 3650 N_Case_Expression |
ee6ba406 3651 N_Case_Statement_Alternative |
3652 N_Character_Literal |
3653 N_Compilation_Unit |
3654 N_Compilation_Unit_Aux |
3655 N_Component_Clause |
3656 N_Component_Declaration |
b5ff3ed8 3657 N_Component_Definition |
ee6ba406 3658 N_Component_List |
3659 N_Constrained_Array_Definition |
3660 N_Decimal_Fixed_Point_Definition |
3661 N_Defining_Character_Literal |
3662 N_Defining_Identifier |
3663 N_Defining_Operator_Symbol |
3664 N_Defining_Program_Unit_Name |
3665 N_Delay_Alternative |
3666 N_Delta_Constraint |
3667 N_Derived_Type_Definition |
3668 N_Designator |
3669 N_Digits_Constraint |
3670 N_Discriminant_Association |
3671 N_Discriminant_Specification |
3672 N_Empty |
3673 N_Entry_Body_Formal_Part |
3674 N_Entry_Call_Alternative |
3675 N_Entry_Declaration |
3676 N_Entry_Index_Specification |
3677 N_Enumeration_Type_Definition |
3678 N_Error |
3679 N_Exception_Handler |
3680 N_Expanded_Name |
3681 N_Explicit_Dereference |
3682 N_Extension_Aggregate |
3683 N_Floating_Point_Definition |
3684 N_Formal_Decimal_Fixed_Point_Definition |
3685 N_Formal_Derived_Type_Definition |
3686 N_Formal_Discrete_Type_Definition |
3687 N_Formal_Floating_Point_Definition |
3688 N_Formal_Modular_Type_Definition |
3689 N_Formal_Ordinary_Fixed_Point_Definition |
3690 N_Formal_Package_Declaration |
3691 N_Formal_Private_Type_Definition |
9c0fda18 3692 N_Formal_Incomplete_Type_Definition |
ee6ba406 3693 N_Formal_Signed_Integer_Type_Definition |
3694 N_Function_Call |
3695 N_Function_Specification |
3696 N_Generic_Association |
3697 N_Handled_Sequence_Of_Statements |
3698 N_Identifier |
3699 N_In |
3700 N_Index_Or_Discriminant_Constraint |
3701 N_Indexed_Component |
3702 N_Integer_Literal |
f37e6e70 3703 N_Iterator_Specification |
ee6ba406 3704 N_Itype_Reference |
3705 N_Label |
3706 N_Loop_Parameter_Specification |
3707 N_Mod_Clause |
3708 N_Modular_Type_Definition |
3709 N_Not_In |
3710 N_Null |
3711 N_Op_Abs |
3712 N_Op_Add |
3713 N_Op_And |
3714 N_Op_Concat |
3715 N_Op_Divide |
3716 N_Op_Eq |
3717 N_Op_Expon |
3718 N_Op_Ge |
3719 N_Op_Gt |
3720 N_Op_Le |
3721 N_Op_Lt |
3722 N_Op_Minus |
3723 N_Op_Mod |
3724 N_Op_Multiply |
3725 N_Op_Ne |
3726 N_Op_Not |
3727 N_Op_Or |
3728 N_Op_Plus |
3729 N_Op_Rem |
3730 N_Op_Rotate_Left |
3731 N_Op_Rotate_Right |
3732 N_Op_Shift_Left |
3733 N_Op_Shift_Right |
3734 N_Op_Shift_Right_Arithmetic |
3735 N_Op_Subtract |
3736 N_Op_Xor |
3737 N_Operator_Symbol |
3738 N_Ordinary_Fixed_Point_Definition |
3739 N_Others_Choice |
3740 N_Package_Specification |
3741 N_Parameter_Association |
3742 N_Parameter_Specification |
343d35dc 3743 N_Pop_Constraint_Error_Label |
3744 N_Pop_Program_Error_Label |
3745 N_Pop_Storage_Error_Label |
ee6ba406 3746 N_Pragma_Argument_Association |
3747 N_Procedure_Specification |
ee6ba406 3748 N_Protected_Definition |
343d35dc 3749 N_Push_Constraint_Error_Label |
3750 N_Push_Program_Error_Label |
3751 N_Push_Storage_Error_Label |
ee6ba406 3752 N_Qualified_Expression |
39d4bf68 3753 N_Quantified_Expression |
ee6ba406 3754 N_Range |
3755 N_Range_Constraint |
3756 N_Real_Literal |
3757 N_Real_Range_Specification |
3758 N_Record_Definition |
3759 N_Reference |
d215f619 3760 N_SCIL_Dispatch_Table_Tag_Init |
3761 N_SCIL_Dispatching_Call |
3feedf2a 3762 N_SCIL_Membership_Test |
ee6ba406 3763 N_Selected_Component |
3764 N_Signed_Integer_Type_Definition |
3765 N_Single_Protected_Declaration |
3766 N_Slice |
3767 N_String_Literal |
3768 N_Subprogram_Info |
3769 N_Subtype_Indication |
3770 N_Subunit |
3771 N_Task_Definition |
3772 N_Terminate_Alternative |
3773 N_Triggering_Alternative |
3774 N_Type_Conversion |
3775 N_Unchecked_Expression |
3776 N_Unchecked_Type_Conversion |
3777 N_Unconstrained_Array_Definition |
3778 N_Unused_At_End |
3779 N_Unused_At_Start |
ee6ba406 3780 N_Variant |
3781 N_Variant_Part |
3782 N_Validate_Unchecked_Conversion |
37808946 3783 N_With_Clause
ee6ba406 3784 =>
3785 null;
3786
3787 end case;
3788
3789 -- Make sure that inserted actions stay in the transient scope
3790
3791 if P = Wrapped_Node then
3792 Store_Before_Actions_In_Scope (Ins_Actions);
3793 return;
3794 end if;
3795
3796 -- If we fall through above tests, keep climbing tree
3797
3798 N := P;
3799
3800 if Nkind (Parent (N)) = N_Subunit then
3801
f5fbfaa6 3802 -- This is the proper body corresponding to a stub. Insertion must
3803 -- be done at the point of the stub, which is in the declarative
3804 -- part of the parent unit.
ee6ba406 3805
3806 P := Corresponding_Stub (Parent (N));
3807
3808 else
3809 P := Parent (N);
3810 end if;
3811 end loop;
ee6ba406 3812 end Insert_Actions;
3813
3814 -- Version with check(s) suppressed
3815
3816 procedure Insert_Actions
37808946 3817 (Assoc_Node : Node_Id;
3818 Ins_Actions : List_Id;
3819 Suppress : Check_Id)
ee6ba406 3820 is
3821 begin
3822 if Suppress = All_Checks then
3823 declare
fafc6b97 3824 Svg : constant Suppress_Record := Scope_Suppress;
ee6ba406 3825 begin
fafc6b97 3826 Scope_Suppress := Suppress_All;
ee6ba406 3827 Insert_Actions (Assoc_Node, Ins_Actions);
3828 Scope_Suppress := Svg;
3829 end;
3830
3831 else
3832 declare
fafc6b97 3833 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
ee6ba406 3834 begin
fafc6b97 3835 Scope_Suppress.Suppress (Suppress) := True;
ee6ba406 3836 Insert_Actions (Assoc_Node, Ins_Actions);
fafc6b97 3837 Scope_Suppress.Suppress (Suppress) := Svg;
ee6ba406 3838 end;
3839 end if;
3840 end Insert_Actions;
3841
3842 --------------------------
3843 -- Insert_Actions_After --
3844 --------------------------
3845
3846 procedure Insert_Actions_After
3847 (Assoc_Node : Node_Id;
3848 Ins_Actions : List_Id)
3849 is
3850 begin
3851 if Scope_Is_Transient
3852 and then Assoc_Node = Node_To_Be_Wrapped
3853 then
3854 Store_After_Actions_In_Scope (Ins_Actions);
3855 else
3856 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
3857 end if;
3858 end Insert_Actions_After;
3859
3860 ---------------------------------
3861 -- Insert_Library_Level_Action --
3862 ---------------------------------
3863
3864 procedure Insert_Library_Level_Action (N : Node_Id) is
3865 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3866
3867 begin
37808946 3868 Push_Scope (Cunit_Entity (Main_Unit));
3869 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
ee6ba406 3870
3871 if No (Actions (Aux)) then
3872 Set_Actions (Aux, New_List (N));
3873 else
3874 Append (N, Actions (Aux));
3875 end if;
3876
3877 Analyze (N);
3878 Pop_Scope;
3879 end Insert_Library_Level_Action;
3880
3881 ----------------------------------
3882 -- Insert_Library_Level_Actions --
3883 ----------------------------------
3884
3885 procedure Insert_Library_Level_Actions (L : List_Id) is
3886 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3887
3888 begin
3889 if Is_Non_Empty_List (L) then
37808946 3890 Push_Scope (Cunit_Entity (Main_Unit));
3891 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
ee6ba406 3892
3893 if No (Actions (Aux)) then
3894 Set_Actions (Aux, L);
3895 Analyze_List (L);
3896 else
3897 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
3898 end if;
3899
3900 Pop_Scope;
3901 end if;
3902 end Insert_Library_Level_Actions;
3903
3904 ----------------------
3905 -- Inside_Init_Proc --
3906 ----------------------
3907
3908 function Inside_Init_Proc return Boolean is
3909 S : Entity_Id;
3910
3911 begin
3912 S := Current_Scope;
9dfe12ae 3913 while Present (S)
3914 and then S /= Standard_Standard
3915 loop
3916 if Is_Init_Proc (S) then
ee6ba406 3917 return True;
3918 else
3919 S := Scope (S);
3920 end if;
3921 end loop;
3922
3923 return False;
3924 end Inside_Init_Proc;
3925
9dfe12ae 3926 ----------------------------
3927 -- Is_All_Null_Statements --
3928 ----------------------------
3929
3930 function Is_All_Null_Statements (L : List_Id) return Boolean is
3931 Stm : Node_Id;
3932
3933 begin
3934 Stm := First (L);
3935 while Present (Stm) loop
3936 if Nkind (Stm) /= N_Null_Statement then
3937 return False;
3938 end if;
3939
3940 Next (Stm);
3941 end loop;
3942
3943 return True;
3944 end Is_All_Null_Statements;
3945
849f127a 3946 --------------------------------------------------
3947 -- Is_Displacement_Of_Object_Or_Function_Result --
3948 --------------------------------------------------
3ad9c375 3949
849f127a 3950 function Is_Displacement_Of_Object_Or_Function_Result
3ad9c375 3951 (Obj_Id : Entity_Id) return Boolean
3952 is
849f127a 3953 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7a19298b 3954 -- Determine if particular node denotes a controlled function call
3ad9c375 3955
3956 function Is_Displace_Call (N : Node_Id) return Boolean;
3957 -- Determine whether a particular node is a call to Ada.Tags.Displace.
3958 -- The call might be nested within other actions such as conversions.
3959
849f127a 3960 function Is_Source_Object (N : Node_Id) return Boolean;
3961 -- Determine whether a particular node denotes a source object
3962
3963 ---------------------------------
3964 -- Is_Controlled_Function_Call --
3965 ---------------------------------
3ad9c375 3966
849f127a 3967 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
3968 Expr : Node_Id := Original_Node (N);
ad3ac5aa 3969
3ad9c375 3970 begin
ad3ac5aa 3971 if Nkind (Expr) = N_Function_Call then
3972 Expr := Name (Expr);
3973 end if;
3974
469bbc18 3975 -- The function call may appear in object.operation format
ad3ac5aa 3976
469bbc18 3977 if Nkind (Expr) = N_Selected_Component then
3978 Expr := Selector_Name (Expr);
3979 end if;
ad3ac5aa 3980
3ad9c375 3981 return
ad3ac5aa 3982 Nkind_In (Expr, N_Expanded_Name, N_Identifier)
3983 and then Ekind (Entity (Expr)) = E_Function
3984 and then Needs_Finalization (Etype (Entity (Expr)));
849f127a 3985 end Is_Controlled_Function_Call;
3ad9c375 3986
3987 ----------------------
3988 -- Is_Displace_Call --
3989 ----------------------
3990
3991 function Is_Displace_Call (N : Node_Id) return Boolean is
3992 Call : Node_Id := N;
3993
3994 begin
3995 -- Strip various actions which may precede a call to Displace
3996
3997 loop
3998 if Nkind (Call) = N_Explicit_Dereference then
3999 Call := Prefix (Call);
4000
4001 elsif Nkind_In (Call, N_Type_Conversion,
4002 N_Unchecked_Type_Conversion)
4003 then
4004 Call := Expression (Call);
ad6b44d8 4005
3ad9c375 4006 else
4007 exit;
4008 end if;
4009 end loop;
4010
4011 return
849f127a 4012 Present (Call)
4013 and then Nkind (Call) = N_Function_Call
3ad9c375 4014 and then Is_RTE (Entity (Name (Call)), RE_Displace);
4015 end Is_Displace_Call;
4016
849f127a 4017 ----------------------
4018 -- Is_Source_Object --
4019 ----------------------
4020
4021 function Is_Source_Object (N : Node_Id) return Boolean is
4022 begin
4023 return
4024 Present (N)
4025 and then Nkind (N) in N_Has_Entity
4026 and then Is_Object (Entity (N))
4027 and then Comes_From_Source (N);
4028 end Is_Source_Object;
4029
3ad9c375 4030 -- Local variables
4031
4032 Decl : constant Node_Id := Parent (Obj_Id);
4033 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4034 Orig_Decl : constant Node_Id := Original_Node (Decl);
4035
849f127a 4036 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
3ad9c375 4037
4038 begin
849f127a 4039 -- Case 1:
3ad9c375 4040
849f127a 4041 -- Obj : CW_Type := Function_Call (...);
3ad9c375 4042
849f127a 4043 -- rewritten into:
3ad9c375 4044
849f127a 4045 -- Tmp : ... := Function_Call (...)'reference;
4046 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
3ad9c375 4047
849f127a 4048 -- where the return type of the function and the class-wide type require
4049 -- dispatch table pointer displacement.
4050
4051 -- Case 2:
4052
4053 -- Obj : CW_Type := Src_Obj;
4054
4055 -- rewritten into:
4056
4057 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4058
4059 -- where the type of the source object and the class-wide type require
3ad9c375 4060 -- dispatch table pointer displacement.
4061
4062 return
4063 Nkind (Decl) = N_Object_Renaming_Declaration
4064 and then Nkind (Orig_Decl) = N_Object_Declaration
4065 and then Comes_From_Source (Orig_Decl)
3ad9c375 4066 and then Is_Class_Wide_Type (Obj_Typ)
849f127a 4067 and then Is_Displace_Call (Renamed_Object (Obj_Id))
4068 and then
4069 (Is_Controlled_Function_Call (Expression (Orig_Decl))
7a19298b 4070 or else Is_Source_Object (Expression (Orig_Decl)));
849f127a 4071 end Is_Displacement_Of_Object_Or_Function_Result;
3ad9c375 4072
bb3b440a 4073 ------------------------------
4074 -- Is_Finalizable_Transient --
4075 ------------------------------
4076
4077 function Is_Finalizable_Transient
4078 (Decl : Node_Id;
4079 Rel_Node : Node_Id) return Boolean
4080 is
8e636ab7 4081 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4082 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4083 Desig : Entity_Id := Obj_Typ;
bb3b440a 4084
4085 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4086 -- Determine whether transient object Trans_Id is initialized either
4087 -- by a function call which returns an access type or simply renames
4088 -- another pointer.
4089
4090 function Initialized_By_Aliased_BIP_Func_Call
4091 (Trans_Id : Entity_Id) return Boolean;
4092 -- Determine whether transient object Trans_Id is initialized by a
4093 -- build-in-place function call where the BIPalloc parameter is of
4094 -- value 1 and BIPaccess is not null. This case creates an aliasing
4095 -- between the returned value and the value denoted by BIPaccess.
4096
8e636ab7 4097 function Is_Aliased
bb3b440a 4098 (Trans_Id : Entity_Id;
4099 First_Stmt : Node_Id) return Boolean;
8e636ab7 4100 -- Determine whether transient object Trans_Id has been renamed or
4101 -- aliased through 'reference in the statement list starting from
4102 -- First_Stmt.
4103
4104 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4105 -- Determine whether transient object Trans_Id is allocated on the heap
bb3b440a 4106
2fc9b6cd 4107 function Is_Iterated_Container
4108 (Trans_Id : Entity_Id;
4109 First_Stmt : Node_Id) return Boolean;
4110 -- Determine whether transient object Trans_Id denotes a container which
4111 -- is in the process of being iterated in the statement list starting
4112 -- from First_Stmt.
4113
bb3b440a 4114 ---------------------------
4115 -- Initialized_By_Access --
4116 ---------------------------
4117
4118 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4119 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4120
4121 begin
4122 return
4123 Present (Expr)
4124 and then Nkind (Expr) /= N_Reference
4125 and then Is_Access_Type (Etype (Expr));
4126 end Initialized_By_Access;
4127
4128 ------------------------------------------
4129 -- Initialized_By_Aliased_BIP_Func_Call --
4130 ------------------------------------------
4131
4132 function Initialized_By_Aliased_BIP_Func_Call
4133 (Trans_Id : Entity_Id) return Boolean
4134 is
4135 Call : Node_Id := Expression (Parent (Trans_Id));
4136
4137 begin
4138 -- Build-in-place calls usually appear in 'reference format
4139
4140 if Nkind (Call) = N_Reference then
4141 Call := Prefix (Call);
4142 end if;
4143
4144 if Is_Build_In_Place_Function_Call (Call) then
4145 declare
4146 Access_Nam : Name_Id := No_Name;
4147 Access_OK : Boolean := False;
4148 Actual : Node_Id;
4149 Alloc_Nam : Name_Id := No_Name;
4150 Alloc_OK : Boolean := False;
4151 Formal : Node_Id;
4152 Func_Id : Entity_Id;
4153 Param : Node_Id;
4154
4155 begin
4156 -- Examine all parameter associations of the function call
4157
4158 Param := First (Parameter_Associations (Call));
4159 while Present (Param) loop
4160 if Nkind (Param) = N_Parameter_Association
4161 and then Nkind (Selector_Name (Param)) = N_Identifier
4162 then
4163 Actual := Explicit_Actual_Parameter (Param);
4164 Formal := Selector_Name (Param);
4165
4166 -- Construct the names of formals BIPaccess and BIPalloc
4167 -- using the function name retrieved from an arbitrary
4168 -- formal.
4169
4170 if Access_Nam = No_Name
4171 and then Alloc_Nam = No_Name
4172 and then Present (Entity (Formal))
4173 then
4174 Func_Id := Scope (Entity (Formal));
4175
4176 Access_Nam :=
4177 New_External_Name (Chars (Func_Id),
4178 BIP_Formal_Suffix (BIP_Object_Access));
4179
4180 Alloc_Nam :=
4181 New_External_Name (Chars (Func_Id),
4182 BIP_Formal_Suffix (BIP_Alloc_Form));
4183 end if;
4184
4185 -- A match for BIPaccess => Temp has been found
4186
4187 if Chars (Formal) = Access_Nam
4188 and then Nkind (Actual) /= N_Null
4189 then
4190 Access_OK := True;
4191 end if;
4192
4193 -- A match for BIPalloc => 1 has been found
4194
4195 if Chars (Formal) = Alloc_Nam
4196 and then Nkind (Actual) = N_Integer_Literal
4197 and then Intval (Actual) = Uint_1
4198 then
4199 Alloc_OK := True;
4200 end if;
4201 end if;
4202
4203 Next (Param);
4204 end loop;
4205
4206 return Access_OK and then Alloc_OK;
4207 end;
4208 end if;
4209
4210 return False;
4211 end Initialized_By_Aliased_BIP_Func_Call;
4212
bb3b440a 4213 ----------------
8e636ab7 4214 -- Is_Aliased --
bb3b440a 4215 ----------------
4216
8e636ab7 4217 function Is_Aliased
bb3b440a 4218 (Trans_Id : Entity_Id;
4219 First_Stmt : Node_Id) return Boolean
4220 is
229c2354 4221 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
bb3b440a 4222 -- Given an object renaming declaration, retrieve the entity of the
4223 -- renamed name. Return Empty if the renamed name is anything other
4224 -- than a variable or a constant.
4225
229c2354 4226 -------------------------
4227 -- Find_Renamed_Object --
4228 -------------------------
bb3b440a 4229
229c2354 4230 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4231 Ren_Obj : Node_Id := Empty;
bb3b440a 4232
229c2354 4233 function Find_Object (N : Node_Id) return Traverse_Result;
4234 -- Try to detect an object which is either a constant or a
4235 -- variable.
2e2a6452 4236
229c2354 4237 -----------------
4238 -- Find_Object --
4239 -----------------
4240
4241 function Find_Object (N : Node_Id) return Traverse_Result is
4242 begin
4243 -- Stop the search once a constant or a variable has been
4244 -- detected.
4245
4246 if Nkind (N) = N_Identifier
4247 and then Present (Entity (N))
4248 and then Ekind_In (Entity (N), E_Constant, E_Variable)
2e2a6452 4249 then
229c2354 4250 Ren_Obj := Entity (N);
4251 return Abandon;
bb3b440a 4252 end if;
bb3b440a 4253
229c2354 4254 return OK;
4255 end Find_Object;
4256
4257 procedure Search is new Traverse_Proc (Find_Object);
4258
4259 -- Local variables
4260
4261 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4262
4263 -- Start of processing for Find_Renamed_Object
4264
4265 begin
4266 -- Actions related to dispatching calls may appear as renamings of
4267 -- tags. Do not process this type of renaming because it does not
4268 -- use the actual value of the object.
4269
4270 if not Is_RTE (Typ, RE_Tag_Ptr) then
4271 Search (Name (Ren_Decl));
bb3b440a 4272 end if;
4273
229c2354 4274 return Ren_Obj;
4275 end Find_Renamed_Object;
bb3b440a 4276
8e636ab7 4277 -- Local variables
bb3b440a 4278
8e636ab7 4279 Expr : Node_Id;
4280 Ren_Obj : Entity_Id;
4281 Stmt : Node_Id;
bb3b440a 4282
8e636ab7 4283 -- Start of processing for Is_Aliased
bb3b440a 4284
8e636ab7 4285 begin
bb3b440a 4286 Stmt := First_Stmt;
4287 while Present (Stmt) loop
8e636ab7 4288 if Nkind (Stmt) = N_Object_Declaration then
4289 Expr := Expression (Stmt);
4290
4291 if Present (Expr)
4292 and then Nkind (Expr) = N_Reference
4293 and then Nkind (Prefix (Expr)) = N_Identifier
4294 and then Entity (Prefix (Expr)) = Trans_Id
4295 then
4296 return True;
4297 end if;
4298
4299 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
229c2354 4300 Ren_Obj := Find_Renamed_Object (Stmt);
bb3b440a 4301
4302 if Present (Ren_Obj)
4303 and then Ren_Obj = Trans_Id
4304 then
4305 return True;
4306 end if;
4307 end if;
4308
4309 Next (Stmt);
4310 end loop;
4311
4312 return False;
8e636ab7 4313 end Is_Aliased;
4314
4315 ------------------
4316 -- Is_Allocated --
4317 ------------------
4318
4319 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
4320 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8e636ab7 4321 begin
4322 return
4323 Is_Access_Type (Etype (Trans_Id))
4324 and then Present (Expr)
4325 and then Nkind (Expr) = N_Allocator;
4326 end Is_Allocated;
bb3b440a 4327
2fc9b6cd 4328 ---------------------------
4329 -- Is_Iterated_Container --
4330 ---------------------------
4331
4332 function Is_Iterated_Container
4333 (Trans_Id : Entity_Id;
4334 First_Stmt : Node_Id) return Boolean
4335 is
4336 Aspect : Node_Id;
4337 Call : Node_Id;
4338 Iter : Entity_Id;
4339 Param : Node_Id;
4340 Stmt : Node_Id;
4341 Typ : Entity_Id;
4342
4343 begin
4344 -- It is not possible to iterate over containers in non-Ada 2012 code
4345
4346 if Ada_Version < Ada_2012 then
4347 return False;
4348 end if;
4349
4350 Typ := Etype (Trans_Id);
4351
4352 -- Handle access type created for secondary stack use
4353
4354 if Is_Access_Type (Typ) then
4355 Typ := Designated_Type (Typ);
4356 end if;
4357
4358 -- Look for aspect Default_Iterator
4359
4360 if Has_Aspects (Parent (Typ)) then
4361 Aspect := Find_Aspect (Typ, Aspect_Default_Iterator);
4362
4363 if Present (Aspect) then
4364 Iter := Entity (Aspect);
4365
4366 -- Examine the statements following the container object and
4367 -- look for a call to the default iterate routine where the
4368 -- first parameter is the transient. Such a call appears as:
4369
4370 -- It : Access_To_CW_Iterator :=
4371 -- Iterate (Tran_Id.all, ...)'reference;
4372
4373 Stmt := First_Stmt;
4374 while Present (Stmt) loop
4375
4376 -- Detect an object declaration which is initialized by a
4377 -- secondary stack function call.
4378
4379 if Nkind (Stmt) = N_Object_Declaration
4380 and then Present (Expression (Stmt))
4381 and then Nkind (Expression (Stmt)) = N_Reference
4382 and then Nkind (Prefix (Expression (Stmt))) =
4383 N_Function_Call
4384 then
4385 Call := Prefix (Expression (Stmt));
4386
4387 -- The call must invoke the default iterate routine of
4388 -- the container and the transient object must appear as
6daf2e31 4389 -- the first actual parameter. Skip any calls whose names
4390 -- are not entities.
2fc9b6cd 4391
6daf2e31 4392 if Is_Entity_Name (Name (Call))
4393 and then Entity (Name (Call)) = Iter
2fc9b6cd 4394 and then Present (Parameter_Associations (Call))
4395 then
4396 Param := First (Parameter_Associations (Call));
4397
4398 if Nkind (Param) = N_Explicit_Dereference
4399 and then Entity (Prefix (Param)) = Trans_Id
4400 then
4401 return True;
4402 end if;
4403 end if;
4404 end if;
4405
4406 Next (Stmt);
4407 end loop;
4408 end if;
4409 end if;
4410
4411 return False;
4412 end Is_Iterated_Container;
4413
bb3b440a 4414 -- Start of processing for Is_Finalizable_Transient
4415
4416 begin
4417 -- Handle access types
4418
4419 if Is_Access_Type (Desig) then
4420 Desig := Available_View (Designated_Type (Desig));
4421 end if;
4422
4423 return
4424 Ekind_In (Obj_Id, E_Constant, E_Variable)
4425 and then Needs_Finalization (Desig)
4426 and then Requires_Transient_Scope (Desig)
4427 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
4428
e4caa3ff 4429 -- Do not consider renamed or 'reference-d transient objects because
4430 -- the act of renaming extends the object's lifetime.
8e636ab7 4431
4432 and then not Is_Aliased (Obj_Id, Decl)
4433
e4caa3ff 4434 -- Do not consider transient objects allocated on the heap since
4435 -- they are attached to a finalization master.
bb3b440a 4436
4437 and then not Is_Allocated (Obj_Id)
4438
e4caa3ff 4439 -- If the transient object is a pointer, check that it is not
4440 -- initialized by a function which returns a pointer or acts as a
4441 -- renaming of another pointer.
bb3b440a 4442
4443 and then
4444 (not Is_Access_Type (Obj_Typ)
4445 or else not Initialized_By_Access (Obj_Id))
4446
e4caa3ff 4447 -- Do not consider transient objects which act as indirect aliases
4448 -- of build-in-place function results.
bb3b440a 4449
a273015d 4450 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
4451
e4caa3ff 4452 -- Do not consider conversions of tags to class-wide types
a273015d 4453
3ad9c375 4454 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
2fc9b6cd 4455
4456 -- Do not consider containers in the context of iterator loops. Such
4457 -- transient objects must exist for as long as the loop is around,
4458 -- otherwise any operation carried out by the iterator will fail.
4459
4460 and then not Is_Iterated_Container (Obj_Id, Decl);
bb3b440a 4461 end Is_Finalizable_Transient;
4462
d2b860b4 4463 ---------------------------------
4464 -- Is_Fully_Repped_Tagged_Type --
4465 ---------------------------------
4466
4467 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
4468 U : constant Entity_Id := Underlying_Type (T);
4469 Comp : Entity_Id;
4470
4471 begin
4472 if No (U) or else not Is_Tagged_Type (U) then
4473 return False;
4474 elsif Has_Discriminants (U) then
4475 return False;
4476 elsif not Has_Specified_Layout (U) then
4477 return False;
4478 end if;
4479
4480 -- Here we have a tagged type, see if it has any unlayed out fields
4481 -- other than a possible tag and parent fields. If so, we return False.
4482
4483 Comp := First_Component (U);
4484 while Present (Comp) loop
4485 if not Is_Tag (Comp)
4486 and then Chars (Comp) /= Name_uParent
4487 and then No (Component_Clause (Comp))
4488 then
4489 return False;
4490 else
4491 Next_Component (Comp);
4492 end if;
4493 end loop;
4494
4495 -- All components are layed out
4496
4497 return True;
4498 end Is_Fully_Repped_Tagged_Type;
4499
0cd40f50 4500 ----------------------------------
4501 -- Is_Library_Level_Tagged_Type --
4502 ----------------------------------
4503
4504 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
4505 begin
4506 return Is_Tagged_Type (Typ)
4507 and then Is_Library_Level_Entity (Typ);
4508 end Is_Library_Level_Tagged_Type;
4509
bb3b440a 4510 --------------------------
4511 -- Is_Non_BIP_Func_Call --
4512 --------------------------
4513
4514 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
4515 begin
4516 -- The expected call is of the format
4517 --
4518 -- Func_Call'reference
4519
4520 return
4521 Nkind (Expr) = N_Reference
4522 and then Nkind (Prefix (Expr)) = N_Function_Call
4523 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
4524 end Is_Non_BIP_Func_Call;
4525
9dfe12ae 4526 ----------------------------------
4527 -- Is_Possibly_Unaligned_Object --
4528 ----------------------------------
4529
ba40b4af 4530 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
4531 T : constant Entity_Id := Etype (N);
4532
9dfe12ae 4533 begin
ba40b4af 4534 -- If renamed object, apply test to underlying object
9dfe12ae 4535
ba40b4af 4536 if Is_Entity_Name (N)
4537 and then Is_Object (Entity (N))
4538 and then Present (Renamed_Object (Entity (N)))
4539 then
4540 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
9dfe12ae 4541 end if;
4542
dc74650f 4543 -- Tagged and controlled types and aliased types are always aligned, as
4544 -- are concurrent types.
9dfe12ae 4545
ba40b4af 4546 if Is_Aliased (T)
4547 or else Has_Controlled_Component (T)
4548 or else Is_Concurrent_Type (T)
4549 or else Is_Tagged_Type (T)
4550 or else Is_Controlled (T)
9dfe12ae 4551 then
ba40b4af 4552 return False;
9dfe12ae 4553 end if;
4554
4555 -- If this is an element of a packed array, may be unaligned
4556
ba40b4af 4557 if Is_Ref_To_Bit_Packed_Array (N) then
9dfe12ae 4558 return True;
4559 end if;
4560
8f979dbe 4561 -- Case of indexed component reference: test whether prefix is unaligned
9dfe12ae 4562
8f979dbe 4563 if Nkind (N) = N_Indexed_Component then
4564 return Is_Possibly_Unaligned_Object (Prefix (N));
4565
4566 -- Case of selected component reference
4567
4568 elsif Nkind (N) = N_Selected_Component then
ba40b4af 4569 declare
4570 P : constant Node_Id := Prefix (N);
4571 C : constant Entity_Id := Entity (Selector_Name (N));
4572 M : Nat;
4573 S : Nat;
9dfe12ae 4574
ba40b4af 4575 begin
4576 -- If component reference is for an array with non-static bounds,
dc74650f 4577 -- then it is always aligned: we can only process unaligned arrays
e4caa3ff 4578 -- with static bounds (more precisely compile time known bounds).
9dfe12ae 4579
ba40b4af 4580 if Is_Array_Type (T)
4581 and then not Compile_Time_Known_Bounds (T)
4582 then
4583 return False;
4584 end if;
9dfe12ae 4585
ba40b4af 4586 -- If component is aliased, it is definitely properly aligned
9dfe12ae 4587
ba40b4af 4588 if Is_Aliased (C) then
4589 return False;
4590 end if;
4591
4592 -- If component is for a type implemented as a scalar, and the
4593 -- record is packed, and the component is other than the first
4594 -- component of the record, then the component may be unaligned.
4595
4596 if Is_Packed (Etype (P))
35403645 4597 and then Represented_As_Scalar (Etype (C))
4598 and then First_Entity (Scope (C)) /= C
ba40b4af 4599 then
4600 return True;
4601 end if;
4602
4603 -- Compute maximum possible alignment for T
4604
4605 -- If alignment is known, then that settles things
4606
4607 if Known_Alignment (T) then
4608 M := UI_To_Int (Alignment (T));
4609
4610 -- If alignment is not known, tentatively set max alignment
4611
4612 else
4613 M := Ttypes.Maximum_Alignment;
4614
4615 -- We can reduce this if the Esize is known since the default
4616 -- alignment will never be more than the smallest power of 2
4617 -- that does not exceed this Esize value.
4618
4619 if Known_Esize (T) then
4620 S := UI_To_Int (Esize (T));
4621
4622 while (M / 2) >= S loop
4623 M := M / 2;
4624 end loop;
4625 end if;
4626 end if;
4627
925c2ba1 4628 -- The following code is historical, it used to be present but it
4629 -- is too cautious, because the front-end does not know the proper
4630 -- default alignments for the target. Also, if the alignment is
4631 -- not known, the front end can't know in any case! If a copy is
4632 -- needed, the back-end will take care of it. This whole section
4633 -- including this comment can be removed later ???
4634
ba40b4af 4635 -- If the component reference is for a record that has a specified
4636 -- alignment, and we either know it is too small, or cannot tell,
925c2ba1 4637 -- then the component may be unaligned.
4638
e4caa3ff 4639 -- What is the following commented out code ???
4640
925c2ba1 4641 -- if Known_Alignment (Etype (P))
4642 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
4643 -- and then M > Alignment (Etype (P))
4644 -- then
4645 -- return True;
4646 -- end if;
ba40b4af 4647
4648 -- Case of component clause present which may specify an
4649 -- unaligned position.
4650
4651 if Present (Component_Clause (C)) then
4652
4653 -- Otherwise we can do a test to make sure that the actual
4654 -- start position in the record, and the length, are both
4655 -- consistent with the required alignment. If not, we know
4656 -- that we are unaligned.
4657
4658 declare
4659 Align_In_Bits : constant Nat := M * System_Storage_Unit;
4660 begin
4661 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
4662 or else Esize (C) mod Align_In_Bits /= 0
4663 then
4664 return True;
4665 end if;
4666 end;
4667 end if;
4668
4669 -- Otherwise, for a component reference, test prefix
4670
4671 return Is_Possibly_Unaligned_Object (P);
4672 end;
9dfe12ae 4673
4674 -- If not a component reference, must be aligned
4675
4676 else
4677 return False;
4678 end if;
4679 end Is_Possibly_Unaligned_Object;
4680
4681 ---------------------------------
4682 -- Is_Possibly_Unaligned_Slice --
4683 ---------------------------------
4684
ba40b4af 4685 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
9dfe12ae 4686 begin
37808946 4687 -- Go to renamed object
c54e9270 4688
ba40b4af 4689 if Is_Entity_Name (N)
4690 and then Is_Object (Entity (N))
4691 and then Present (Renamed_Object (Entity (N)))
9dfe12ae 4692 then
ba40b4af 4693 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
9dfe12ae 4694 end if;
4695
c54e9270 4696 -- The reference must be a slice
9dfe12ae 4697
ba40b4af 4698 if Nkind (N) /= N_Slice then
c54e9270 4699 return False;
9dfe12ae 4700 end if;
4701
c54e9270 4702 -- Always assume the worst for a nested record component with a
4703 -- component clause, which gigi/gcc does not appear to handle well.
4704 -- It is not clear why this special test is needed at all ???
9dfe12ae 4705
ba40b4af 4706 if Nkind (Prefix (N)) = N_Selected_Component
4707 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
c54e9270 4708 and then
ba40b4af 4709 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
c54e9270 4710 then
4711 return True;
4712 end if;
4713
4714 -- We only need to worry if the target has strict alignment
4715
4716 if not Target_Strict_Alignment then
9dfe12ae 4717 return False;
4718 end if;
4719
4720 -- If it is a slice, then look at the array type being sliced
4721
4722 declare
ba40b4af 4723 Sarr : constant Node_Id := Prefix (N);
c54e9270 4724 -- Prefix of the slice, i.e. the array being sliced
4725
ba40b4af 4726 Styp : constant Entity_Id := Etype (Prefix (N));
c54e9270 4727 -- Type of the array being sliced
4728
4729 Pref : Node_Id;
4730 Ptyp : Entity_Id;
9dfe12ae 4731
4732 begin
c54e9270 4733 -- The problems arise if the array object that is being sliced
4734 -- is a component of a record or array, and we cannot guarantee
4735 -- the alignment of the array within its containing object.
9dfe12ae 4736
c54e9270 4737 -- To investigate this, we look at successive prefixes to see
4738 -- if we have a worrisome indexed or selected component.
9dfe12ae 4739
c54e9270 4740 Pref := Sarr;
4741 loop
4742 -- Case of array is part of an indexed component reference
9dfe12ae 4743
c54e9270 4744 if Nkind (Pref) = N_Indexed_Component then
4745 Ptyp := Etype (Prefix (Pref));
4746
dc74650f 4747 -- The only problematic case is when the array is packed, in
4748 -- which case we really know nothing about the alignment of
4749 -- individual components.
c54e9270 4750
4751 if Is_Bit_Packed_Array (Ptyp) then
4752 return True;
4753 end if;
4754
4755 -- Case of array is part of a selected component reference
4756
4757 elsif Nkind (Pref) = N_Selected_Component then
4758 Ptyp := Etype (Prefix (Pref));
4759
4760 -- We are definitely in trouble if the record in question
4761 -- has an alignment, and either we know this alignment is
dc74650f 4762 -- inconsistent with the alignment of the slice, or we don't
4763 -- know what the alignment of the slice should be.
c54e9270 4764
4765 if Known_Alignment (Ptyp)
4766 and then (Unknown_Alignment (Styp)
4767 or else Alignment (Styp) > Alignment (Ptyp))
4768 then
4769 return True;
4770 end if;
4771
4772 -- We are in potential trouble if the record type is packed.
4773 -- We could special case when we know that the array is the
4774 -- first component, but that's not such a simple case ???
4775
4776 if Is_Packed (Ptyp) then
4777 return True;
4778 end if;
4779
4780 -- We are in trouble if there is a component clause, and
4781 -- either we do not know the alignment of the slice, or
4782 -- the alignment of the slice is inconsistent with the
4783 -- bit position specified by the component clause.
4784
4785 declare
4786 Field : constant Entity_Id := Entity (Selector_Name (Pref));
4787 begin
4788 if Present (Component_Clause (Field))
4789 and then
4790 (Unknown_Alignment (Styp)
4791 or else
4792 (Component_Bit_Offset (Field) mod
4793 (System_Storage_Unit * Alignment (Styp))) /= 0)
4794 then
4795 return True;
4796 end if;
4797 end;
4798
dc74650f 4799 -- For cases other than selected or indexed components we know we
4800 -- are OK, since no issues arise over alignment.
c54e9270 4801
4802 else
4803 return False;
4804 end if;
4805
4806 -- We processed an indexed component or selected component
4807 -- reference that looked safe, so keep checking prefixes.
4808
4809 Pref := Prefix (Pref);
4810 end loop;
9dfe12ae 4811 end;
4812 end Is_Possibly_Unaligned_Slice;
4813
bb3b440a 4814 -------------------------------
4815 -- Is_Related_To_Func_Return --
4816 -------------------------------
4817
4818 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
4819 Expr : constant Node_Id := Related_Expression (Id);
bb3b440a 4820 begin
4821 return
4822 Present (Expr)
4823 and then Nkind (Expr) = N_Explicit_Dereference
4824 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
4825 end Is_Related_To_Func_Return;
4826
ee6ba406 4827 --------------------------------
4828 -- Is_Ref_To_Bit_Packed_Array --
4829 --------------------------------
4830
ba40b4af 4831 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
ee6ba406 4832 Result : Boolean;
4833 Expr : Node_Id;
4834
4835 begin
ba40b4af 4836 if Is_Entity_Name (N)
4837 and then Is_Object (Entity (N))
4838 and then Present (Renamed_Object (Entity (N)))
9dfe12ae 4839 then
ba40b4af 4840 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
9dfe12ae 4841 end if;
4842
ba40b4af 4843 if Nkind (N) = N_Indexed_Component
ee6ba406 4844 or else
ba40b4af 4845 Nkind (N) = N_Selected_Component
ee6ba406 4846 then
ba40b4af 4847 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
ee6ba406 4848 Result := True;
4849 else
ba40b4af 4850 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
ee6ba406 4851 end if;
4852
ba40b4af 4853 if Result and then Nkind (N) = N_Indexed_Component then
4854 Expr := First (Expressions (N));
ee6ba406 4855 while Present (Expr) loop
4856 Force_Evaluation (Expr);
4857 Next (Expr);
4858 end loop;
4859 end if;
4860
4861 return Result;
4862
4863 else
4864 return False;
4865 end if;
4866 end Is_Ref_To_Bit_Packed_Array;
4867
4868 --------------------------------
9dfe12ae 4869 -- Is_Ref_To_Bit_Packed_Slice --
ee6ba406 4870 --------------------------------
4871
ba40b4af 4872 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
ee6ba406 4873 begin
041a8137 4874 if Nkind (N) = N_Type_Conversion then
4875 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
4876
4877 elsif Is_Entity_Name (N)
ba40b4af 4878 and then Is_Object (Entity (N))
4879 and then Present (Renamed_Object (Entity (N)))
9dfe12ae 4880 then
ba40b4af 4881 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
9dfe12ae 4882
041a8137 4883 elsif Nkind (N) = N_Slice
ba40b4af 4884 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
ee6ba406 4885 then
4886 return True;
4887
ba40b4af 4888 elsif Nkind (N) = N_Indexed_Component
ee6ba406 4889 or else
ba40b4af 4890 Nkind (N) = N_Selected_Component
ee6ba406 4891 then
ba40b4af 4892 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
ee6ba406 4893
4894 else
4895 return False;
4896 end if;
4897 end Is_Ref_To_Bit_Packed_Slice;
4898
4899 -----------------------
4900 -- Is_Renamed_Object --
4901 -----------------------
4902
4903 function Is_Renamed_Object (N : Node_Id) return Boolean is
4904 Pnod : constant Node_Id := Parent (N);
4905 Kind : constant Node_Kind := Nkind (Pnod);
ee6ba406 4906 begin
4907 if Kind = N_Object_Renaming_Declaration then
4908 return True;
d2b860b4 4909 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
ee6ba406 4910 return Is_Renamed_Object (Pnod);
ee6ba406 4911 else
4912 return False;
4913 end if;
4914 end Is_Renamed_Object;
4915
14980c45 4916 --------------------------------------
4917 -- Is_Secondary_Stack_BIP_Func_Call --
4918 --------------------------------------
4919
4920 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
4921 Call : Node_Id := Expr;
4922
4923 begin
81dd521a 4924 -- Build-in-place calls usually appear in 'reference format. Note that
4925 -- the accessibility check machinery may add an extra 'reference due to
4926 -- side effect removal.
14980c45 4927
81dd521a 4928 while Nkind (Call) = N_Reference loop
14980c45 4929 Call := Prefix (Call);
81dd521a 4930 end loop;
14980c45 4931
4932 if Nkind_In (Call, N_Qualified_Expression,
4933 N_Unchecked_Type_Conversion)
4934 then
4935 Call := Expression (Call);
4936 end if;
4937
4938 if Is_Build_In_Place_Function_Call (Call) then
4939 declare
4940 Access_Nam : Name_Id := No_Name;
4941 Actual : Node_Id;
4942 Param : Node_Id;
4943 Formal : Node_Id;
4944
4945 begin
4946 -- Examine all parameter associations of the function call
4947
4948 Param := First (Parameter_Associations (Call));
4949 while Present (Param) loop
4950 if Nkind (Param) = N_Parameter_Association
4951 and then Nkind (Selector_Name (Param)) = N_Identifier
4952 then
4953 Formal := Selector_Name (Param);
4954 Actual := Explicit_Actual_Parameter (Param);
4955
4956 -- Construct the name of formal BIPalloc. It is much easier
4957 -- to extract the name of the function using an arbitrary
4958 -- formal's scope rather than the Name field of Call.
4959
4960 if Access_Nam = No_Name
4961 and then Present (Entity (Formal))
4962 then
4963 Access_Nam :=
4964 New_External_Name
4965 (Chars (Scope (Entity (Formal))),
4966 BIP_Formal_Suffix (BIP_Alloc_Form));
4967 end if;
4968
4969 -- A match for BIPalloc => 2 has been found
4970
4971 if Chars (Formal) = Access_Nam
4972 and then Nkind (Actual) = N_Integer_Literal
4973 and then Intval (Actual) = Uint_2
4974 then
4975 return True;
4976 end if;
4977 end if;
4978
4979 Next (Param);
4980 end loop;
4981 end;
4982 end if;
4983
4984 return False;
4985 end Is_Secondary_Stack_BIP_Func_Call;
4986
3ad9c375 4987 -------------------------------------
4988 -- Is_Tag_To_Class_Wide_Conversion --
4989 -------------------------------------
a273015d 4990
3ad9c375 4991 function Is_Tag_To_Class_Wide_Conversion
4992 (Obj_Id : Entity_Id) return Boolean
4993 is
a273015d 4994 Expr : constant Node_Id := Expression (Parent (Obj_Id));
4995
4996 begin
4997 return
4998 Is_Class_Wide_Type (Etype (Obj_Id))
4999 and then Present (Expr)
5000 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5001 and then Etype (Expression (Expr)) = RTE (RE_Tag);
3ad9c375 5002 end Is_Tag_To_Class_Wide_Conversion;
a273015d 5003
ee6ba406 5004 ----------------------------
5005 -- Is_Untagged_Derivation --
5006 ----------------------------
5007
5008 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5009 begin
5010 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5011 or else
5012 (Is_Private_Type (T) and then Present (Full_View (T))
5013 and then not Is_Tagged_Type (Full_View (T))
5014 and then Is_Derived_Type (Full_View (T))
5015 and then Etype (Full_View (T)) /= T);
ee6ba406 5016 end Is_Untagged_Derivation;
5017
57993a53 5018 ---------------------------
5019 -- Is_Volatile_Reference --
5020 ---------------------------
5021
5022 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5023 begin
5024 if Nkind (N) in N_Has_Etype
5025 and then Present (Etype (N))
5026 and then Treat_As_Volatile (Etype (N))
5027 then
5028 return True;
5029
5030 elsif Is_Entity_Name (N) then
5031 return Treat_As_Volatile (Entity (N));
5032
5033 elsif Nkind (N) = N_Slice then
5034 return Is_Volatile_Reference (Prefix (N));
5035
5036 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5037 if (Is_Entity_Name (Prefix (N))
5038 and then Has_Volatile_Components (Entity (Prefix (N))))
5039 or else (Present (Etype (Prefix (N)))
5040 and then Has_Volatile_Components (Etype (Prefix (N))))
5041 then
5042 return True;
5043 else
5044 return Is_Volatile_Reference (Prefix (N));
5045 end if;
5046
5047 else
5048 return False;
5049 end if;
5050 end Is_Volatile_Reference;
5051
c6b238e7 5052 --------------------------
5053 -- Is_VM_By_Copy_Actual --
5054 --------------------------
5055
5056 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5057 begin
5058 return VM_Target /= No_VM
098d3082 5059 and then (Nkind (N) = N_Slice
5060 or else
5061 (Nkind (N) = N_Identifier
5062 and then Present (Renamed_Object (Entity (N)))
5063 and then Nkind (Renamed_Object (Entity (N)))
5064 = N_Slice));
c6b238e7 5065 end Is_VM_By_Copy_Actual;
5066
ee6ba406 5067 --------------------
5068 -- Kill_Dead_Code --
5069 --------------------
5070
014e9448 5071 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
d36a3269 5072 W : Boolean := Warn;
5073 -- Set False if warnings suppressed
5074
ee6ba406 5075 begin
5076 if Present (N) then
ee6ba406 5077 Remove_Warning_Messages (N);
5078
d36a3269 5079 -- Generate warning if appropriate
5080
5081 if W then
5082
5083 -- We suppress the warning if this code is under control of an
5084 -- if statement, whose condition is a simple identifier, and
5085 -- either we are in an instance, or warnings off is set for this
5086 -- identifier. The reason for killing it in the instance case is
5087 -- that it is common and reasonable for code to be deleted in
5088 -- instances for various reasons.
5089
5090 if Nkind (Parent (N)) = N_If_Statement then
5091 declare
5092 C : constant Node_Id := Condition (Parent (N));
5093 begin
5094 if Nkind (C) = N_Identifier
5095 and then
5096 (In_Instance
5097 or else (Present (Entity (C))
5098 and then Has_Warnings_Off (Entity (C))))
5099 then
5100 W := False;
5101 end if;
5102 end;
5103 end if;
5104
5105 -- Generate warning if not suppressed
5106
5107 if W then
503f7fd3 5108 Error_Msg_F
d36a3269 5109 ("?this code can never be executed and has been deleted!", N);
5110 end if;
014e9448 5111 end if;
5112
f15731c4 5113 -- Recurse into block statements and bodies to process declarations
d36a3269 5114 -- and statements.
ee6ba406 5115
f15731c4 5116 if Nkind (N) = N_Block_Statement
5117 or else Nkind (N) = N_Subprogram_Body
5118 or else Nkind (N) = N_Package_Body
5119 then
4536414b 5120 Kill_Dead_Code (Declarations (N), False);
5121 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
ee6ba406 5122
f15731c4 5123 if Nkind (N) = N_Subprogram_Body then
5124 Set_Is_Eliminated (Defining_Entity (N));
5125 end if;
5126
ba40b4af 5127 elsif Nkind (N) = N_Package_Declaration then
5128 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5129 Kill_Dead_Code (Private_Declarations (Specification (N)));
5130
4536414b 5131 -- ??? After this point, Delete_Tree has been called on all
dc74650f 5132 -- declarations in Specification (N), so references to entities
5133 -- therein look suspicious.
4536414b 5134
ba40b4af 5135 declare
5136 E : Entity_Id := First_Entity (Defining_Entity (N));
5137 begin
5138 while Present (E) loop
5139 if Ekind (E) = E_Operator then
5140 Set_Is_Eliminated (E);
5141 end if;
5142
5143 Next_Entity (E);
5144 end loop;
5145 end;
5146
dc74650f 5147 -- Recurse into composite statement to kill individual statements in
5148 -- particular instantiations.
ee6ba406 5149
5150 elsif Nkind (N) = N_If_Statement then
5151 Kill_Dead_Code (Then_Statements (N));
5152 Kill_Dead_Code (Elsif_Parts (N));
5153 Kill_Dead_Code (Else_Statements (N));
5154
5155 elsif Nkind (N) = N_Loop_Statement then
5156 Kill_Dead_Code (Statements (N));
5157
5158 elsif Nkind (N) = N_Case_Statement then
5159 declare
23e83313 5160 Alt : Node_Id;
ee6ba406 5161 begin
23e83313 5162 Alt := First (Alternatives (N));
ee6ba406 5163 while Present (Alt) loop
5164 Kill_Dead_Code (Statements (Alt));
5165 Next (Alt);
5166 end loop;
5167 end;
5168
9dfe12ae 5169 elsif Nkind (N) = N_Case_Statement_Alternative then
5170 Kill_Dead_Code (Statements (N));
5171
ee6ba406 5172 -- Deal with dead instances caused by deleting instantiations
5173
5174 elsif Nkind (N) in N_Generic_Instantiation then
5175 Remove_Dead_Instance (N);
5176 end if;
ee6ba406 5177 end if;
5178 end Kill_Dead_Code;
5179
5180 -- Case where argument is a list of nodes to be killed
5181
014e9448 5182 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
ee6ba406 5183 N : Node_Id;
014e9448 5184 W : Boolean;
ee6ba406 5185 begin
014e9448 5186 W := Warn;
ee6ba406 5187 if Is_Non_Empty_List (L) then
d34432fa 5188 N := First (L);
5189 while Present (N) loop
014e9448 5190 Kill_Dead_Code (N, W);
5191 W := False;
d34432fa 5192 Next (N);
ee6ba406 5193 end loop;
5194 end if;
5195 end Kill_Dead_Code;
5196
5197 ------------------------
5198 -- Known_Non_Negative --
5199 ------------------------
5200
5201 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5202 begin
5203 if Is_OK_Static_Expression (Opnd)
5204 and then Expr_Value (Opnd) >= 0
5205 then
5206 return True;
5207
5208 else
5209 declare
5210 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5211
5212 begin
5213 return
5214 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
5215 end;
5216 end if;
5217 end Known_Non_Negative;
5218
9dfe12ae 5219 --------------------
5220 -- Known_Non_Null --
5221 --------------------
f15731c4 5222
9dfe12ae 5223 function Known_Non_Null (N : Node_Id) return Boolean is
5224 begin
35889f1e 5225 -- Checks for case where N is an entity reference
f15731c4 5226
35889f1e 5227 if Is_Entity_Name (N) and then Present (Entity (N)) then
5228 declare
5229 E : constant Entity_Id := Entity (N);
5230 Op : Node_Kind;
5231 Val : Node_Id;
f15731c4 5232
35889f1e 5233 begin
5234 -- First check if we are in decisive conditional
f15731c4 5235
35889f1e 5236 Get_Current_Value_Condition (N, Op, Val);
f15731c4 5237
0cd40f50 5238 if Known_Null (Val) then
35889f1e 5239 if Op = N_Op_Eq then
5240 return False;
5241 elsif Op = N_Op_Ne then
5242 return True;
5243 end if;
5244 end if;
f15731c4 5245
35889f1e 5246 -- If OK to do replacement, test Is_Known_Non_Null flag
f15731c4 5247
35889f1e 5248 if OK_To_Do_Constant_Replacement (E) then
5249 return Is_Known_Non_Null (E);
5250
5251 -- Otherwise if not safe to do replacement, then say so
5252
5253 else
5254 return False;
5255 end if;
5256 end;
f15731c4 5257
9dfe12ae 5258 -- True if access attribute
f15731c4 5259
9dfe12ae 5260 elsif Nkind (N) = N_Attribute_Reference
5261 and then (Attribute_Name (N) = Name_Access
5262 or else
5263 Attribute_Name (N) = Name_Unchecked_Access
5264 or else
5265 Attribute_Name (N) = Name_Unrestricted_Access)
5266 then
5267 return True;
f15731c4 5268
9dfe12ae 5269 -- True if allocator
f15731c4 5270
9dfe12ae 5271 elsif Nkind (N) = N_Allocator then
5272 return True;
f15731c4 5273
9dfe12ae 5274 -- For a conversion, true if expression is known non-null
f15731c4 5275
9dfe12ae 5276 elsif Nkind (N) = N_Type_Conversion then
5277 return Known_Non_Null (Expression (N));
f15731c4 5278
35889f1e 5279 -- Above are all cases where the value could be determined to be
5280 -- non-null. In all other cases, we don't know, so return False.
f15731c4 5281
35889f1e 5282 else
5283 return False;
5284 end if;
5285 end Known_Non_Null;
5286
5287 ----------------
5288 -- Known_Null --
5289 ----------------
5290
5291 function Known_Null (N : Node_Id) return Boolean is
5292 begin
5293 -- Checks for case where N is an entity reference
5294
5295 if Is_Entity_Name (N) and then Present (Entity (N)) then
9dfe12ae 5296 declare
35889f1e 5297 E : constant Entity_Id := Entity (N);
9dfe12ae 5298 Op : Node_Kind;
5299 Val : Node_Id;
f15731c4 5300
9dfe12ae 5301 begin
0cd40f50 5302 -- Constant null value is for sure null
5303
5304 if Ekind (E) = E_Constant
5305 and then Known_Null (Constant_Value (E))
5306 then
5307 return True;
5308 end if;
5309
35889f1e 5310 -- First check if we are in decisive conditional
5311
9dfe12ae 5312 Get_Current_Value_Condition (N, Op, Val);
35889f1e 5313
0cd40f50 5314 if Known_Null (Val) then
35889f1e 5315 if Op = N_Op_Eq then
5316 return True;
5317 elsif Op = N_Op_Ne then
5318 return False;
5319 end if;
5320 end if;
5321
5322 -- If OK to do replacement, test Is_Known_Null flag
5323
5324 if OK_To_Do_Constant_Replacement (E) then
5325 return Is_Known_Null (E);
5326
5327 -- Otherwise if not safe to do replacement, then say so
5328
5329 else
5330 return False;
5331 end if;
9dfe12ae 5332 end;
5333
35889f1e 5334 -- True if explicit reference to null
5335
5336 elsif Nkind (N) = N_Null then
5337 return True;
5338
5339 -- For a conversion, true if expression is known null
5340
5341 elsif Nkind (N) = N_Type_Conversion then
5342 return Known_Null (Expression (N));
5343
5344 -- Above are all cases where the value could be determined to be null.
5345 -- In all other cases, we don't know, so return False.
9dfe12ae 5346
5347 else
5348 return False;
5349 end if;
35889f1e 5350 end Known_Null;
f15731c4 5351
ee6ba406 5352 -----------------------------
5353 -- Make_CW_Equivalent_Type --
5354 -----------------------------
5355
d2b860b4 5356 -- Create a record type used as an equivalent of any member of the class
5357 -- which takes its size from exp.
ee6ba406 5358
5359 -- Generate the following code:
5360
5361 -- type Equiv_T is record
526aedbb 5362 -- _parent : T (List of discriminant constraints taken from Exp);
9dfe12ae 5363 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
ee6ba406 5364 -- end Equiv_T;
9dfe12ae 5365 --
5366 -- ??? Note that this type does not guarantee same alignment as all
5367 -- derived types
ee6ba406 5368
5369 function Make_CW_Equivalent_Type
23e83313 5370 (T : Entity_Id;
5371 E : Node_Id) return Entity_Id
ee6ba406 5372 is
5373 Loc : constant Source_Ptr := Sloc (E);
5374 Root_Typ : constant Entity_Id := Root_Type (T);
9dfe12ae 5375 List_Def : constant List_Id := Empty_List;
37808946 5376 Comp_List : constant List_Id := New_List;
ee6ba406 5377 Equiv_Type : Entity_Id;
5378 Range_Type : Entity_Id;
5379 Str_Type : Entity_Id;
ee6ba406 5380 Constr_Root : Entity_Id;
5381 Sizexpr : Node_Id;
5382
5383 begin
716fc478 5384 -- If the root type is already constrained, there are no discriminants
5385 -- in the expression.
5386
5387 if not Has_Discriminants (Root_Typ)
5388 or else Is_Constrained (Root_Typ)
5389 then
ee6ba406 5390 Constr_Root := Root_Typ;
5391 else
11deeeb6 5392 Constr_Root := Make_Temporary (Loc, 'R');
ee6ba406 5393
5394 -- subtype cstr__n is T (List of discr constraints taken from Exp)
5395
5396 Append_To (List_Def,
5397 Make_Subtype_Declaration (Loc,
5398 Defining_Identifier => Constr_Root,
11deeeb6 5399 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
ee6ba406 5400 end if;
5401
37808946 5402 -- Generate the range subtype declaration
ee6ba406 5403
11deeeb6 5404 Range_Type := Make_Temporary (Loc, 'G');
ee6ba406 5405
37808946 5406 if not Is_Interface (Root_Typ) then
d2b860b4 5407
37808946 5408 -- subtype rg__xx is
5409 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
5410
5411 Sizexpr :=
5412 Make_Op_Subtract (Loc,
5413 Left_Opnd =>
5414 Make_Attribute_Reference (Loc,
5415 Prefix =>
5416 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5417 Attribute_Name => Name_Size),
5418 Right_Opnd =>
5419 Make_Attribute_Reference (Loc,
5420 Prefix => New_Reference_To (Constr_Root, Loc),
5421 Attribute_Name => Name_Object_Size));
5422 else
5423 -- subtype rg__xx is
5424 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
5425
5426 Sizexpr :=
5427 Make_Attribute_Reference (Loc,
5428 Prefix =>
5429 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5430 Attribute_Name => Name_Size);
5431 end if;
ee6ba406 5432
5433 Set_Paren_Count (Sizexpr, 1);
5434
5435 Append_To (List_Def,
5436 Make_Subtype_Declaration (Loc,
5437 Defining_Identifier => Range_Type,
5438 Subtype_Indication =>
5439 Make_Subtype_Indication (Loc,
5440 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
5441 Constraint => Make_Range_Constraint (Loc,
5442 Range_Expression =>
5443 Make_Range (Loc,
5444 Low_Bound => Make_Integer_Literal (Loc, 1),
5445 High_Bound =>
5446 Make_Op_Divide (Loc,
5447 Left_Opnd => Sizexpr,
5448 Right_Opnd => Make_Integer_Literal (Loc,
5449 Intval => System_Storage_Unit)))))));
5450
5451 -- subtype str__nn is Storage_Array (rg__x);
5452
11deeeb6 5453 Str_Type := Make_Temporary (Loc, 'S');
ee6ba406 5454 Append_To (List_Def,
5455 Make_Subtype_Declaration (Loc,
5456 Defining_Identifier => Str_Type,
5457 Subtype_Indication =>
5458 Make_Subtype_Indication (Loc,
5459 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
5460 Constraint =>
5461 Make_Index_Or_Discriminant_Constraint (Loc,
5462 Constraints =>
5463 New_List (New_Reference_To (Range_Type, Loc))))));
5464
5465 -- type Equiv_T is record
37808946 5466 -- [ _parent : Tnn; ]
ee6ba406 5467 -- E : Str_Type;
5468 -- end Equiv_T;
5469
11deeeb6 5470 Equiv_Type := Make_Temporary (Loc, 'T');
ee6ba406 5471 Set_Ekind (Equiv_Type, E_Record_Type);
5472 Set_Parent_Subtype (Equiv_Type, Constr_Root);
5473
aab874ce 5474 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
5475 -- treatment for this type. In particular, even though _parent's type
5476 -- is a controlled type or contains controlled components, we do not
5477 -- want to set Has_Controlled_Component on it to avoid making it gain
5478 -- an unwanted _controller component.
5479
5480 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
5481
37808946 5482 if not Is_Interface (Root_Typ) then
5483 Append_To (Comp_List,
5484 Make_Component_Declaration (Loc,
5485 Defining_Identifier =>
5486 Make_Defining_Identifier (Loc, Name_uParent),
5487 Component_Definition =>
5488 Make_Component_Definition (Loc,
5489 Aliased_Present => False,
5490 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
5491 end if;
5492
5493 Append_To (Comp_List,
5494 Make_Component_Declaration (Loc,
11deeeb6 5495 Defining_Identifier => Make_Temporary (Loc, 'C'),
37808946 5496 Component_Definition =>
5497 Make_Component_Definition (Loc,
5498 Aliased_Present => False,
5499 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
5500
ee6ba406 5501 Append_To (List_Def,
5502 Make_Full_Type_Declaration (Loc,
5503 Defining_Identifier => Equiv_Type,
ee6ba406 5504 Type_Definition =>
5505 Make_Record_Definition (Loc,
37808946 5506 Component_List =>
5507 Make_Component_List (Loc,
5508 Component_Items => Comp_List,
5509 Variant_Part => Empty))));
5510
dc74650f 5511 -- Suppress all checks during the analysis of the expanded code to avoid
5512 -- the generation of spurious warnings under ZFP run-time.
37808946 5513
5514 Insert_Actions (E, List_Def, Suppress => All_Checks);
ee6ba406 5515 return Equiv_Type;
5516 end Make_CW_Equivalent_Type;
5517
5b5df4a9 5518 -------------------------
5519 -- Make_Invariant_Call --
5520 -------------------------
5521
5522 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
5523 Loc : constant Source_Ptr := Sloc (Expr);
5524 Typ : constant Entity_Id := Etype (Expr);
5525
5526 begin
f54f1dff 5527 pragma Assert
5528 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
5529
5b5df4a9 5530 if Check_Enabled (Name_Invariant)
5531 or else
5532 Check_Enabled (Name_Assertion)
5533 then
5534 return
5535 Make_Procedure_Call_Statement (Loc,
5536 Name =>
5537 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
5538 Parameter_Associations => New_List (Relocate_Node (Expr)));
5539
5540 else
5541 return
5542 Make_Null_Statement (Loc);
5543 end if;
5544 end Make_Invariant_Call;
5545
ee6ba406 5546 ------------------------
5547 -- Make_Literal_Range --
5548 ------------------------
5549
5550 function Make_Literal_Range
5551 (Loc : Source_Ptr;
23e83313 5552 Literal_Typ : Entity_Id) return Node_Id
ee6ba406 5553 is
0cd40f50 5554 Lo : constant Node_Id :=
5555 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
5556 Index : constant Entity_Id := Etype (Lo);
5557
5558 Hi : Node_Id;
5559 Length_Expr : constant Node_Id :=
5560 Make_Op_Subtract (Loc,
5561 Left_Opnd =>
5562 Make_Integer_Literal (Loc,
5563 Intval => String_Literal_Length (Literal_Typ)),
5564 Right_Opnd =>
5565 Make_Integer_Literal (Loc, 1));
db1260ab 5566
ee6ba406 5567 begin
db1260ab 5568 Set_Analyzed (Lo, False);
5569
0cd40f50 5570 if Is_Integer_Type (Index) then
5571 Hi :=
5572 Make_Op_Add (Loc,
5573 Left_Opnd => New_Copy_Tree (Lo),
5574 Right_Opnd => Length_Expr);
5575 else
5576 Hi :=
5577 Make_Attribute_Reference (Loc,
5578 Attribute_Name => Name_Val,
5579 Prefix => New_Occurrence_Of (Index, Loc),
5580 Expressions => New_List (
5581 Make_Op_Add (Loc,
5582 Left_Opnd =>
5583 Make_Attribute_Reference (Loc,
5584 Attribute_Name => Name_Pos,
5585 Prefix => New_Occurrence_Of (Index, Loc),
5586 Expressions => New_List (New_Copy_Tree (Lo))),
5587 Right_Opnd => Length_Expr)));
5588 end if;
5589
ee6ba406 5590 return
5591 Make_Range (Loc,
0cd40f50 5592 Low_Bound => Lo,
5593 High_Bound => Hi);
ee6ba406 5594 end Make_Literal_Range;
5595
2b523281 5596 --------------------------
5597 -- Make_Non_Empty_Check --
5598 --------------------------
5599
5600 function Make_Non_Empty_Check
5601 (Loc : Source_Ptr;
5602 N : Node_Id) return Node_Id
5603 is
5604 begin
5605 return
5606 Make_Op_Ne (Loc,
5607 Left_Opnd =>
5608 Make_Attribute_Reference (Loc,
5609 Attribute_Name => Name_Length,
5610 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
5611 Right_Opnd =>
5612 Make_Integer_Literal (Loc, 0));
5613 end Make_Non_Empty_Check;
5614
4aed5405 5615 -------------------------
5616 -- Make_Predicate_Call --
5617 -------------------------
5618
5619 function Make_Predicate_Call
5620 (Typ : Entity_Id;
5621 Expr : Node_Id) return Node_Id
5622 is
5623 Loc : constant Source_Ptr := Sloc (Expr);
5624
5625 begin
5626 pragma Assert (Present (Predicate_Function (Typ)));
5627
5628 return
5629 Make_Function_Call (Loc,
5630 Name =>
5631 New_Occurrence_Of (Predicate_Function (Typ), Loc),
5632 Parameter_Associations => New_List (Relocate_Node (Expr)));
5633 end Make_Predicate_Call;
5634
5635 --------------------------
5636 -- Make_Predicate_Check --
5637 --------------------------
5638
5639 function Make_Predicate_Check
5640 (Typ : Entity_Id;
5641 Expr : Node_Id) return Node_Id
5642 is
5643 Loc : constant Source_Ptr := Sloc (Expr);
5644
5645 begin
5646 return
5647 Make_Pragma (Loc,
55868293 5648 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
4aed5405 5649 Pragma_Argument_Associations => New_List (
5650 Make_Pragma_Argument_Association (Loc,
55868293 5651 Expression => Make_Identifier (Loc, Name_Predicate)),
4aed5405 5652 Make_Pragma_Argument_Association (Loc,
5653 Expression => Make_Predicate_Call (Typ, Expr))));
5654 end Make_Predicate_Check;
5655
ee6ba406 5656 ----------------------------
5657 -- Make_Subtype_From_Expr --
5658 ----------------------------
5659
526aedbb 5660 -- 1. If Expr is an unconstrained array expression, creates
5661 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
ee6ba406 5662
5663 -- 2. If Expr is a unconstrained discriminated type expression, creates
5664 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
5665
5666 -- 3. If Expr is class-wide, creates an implicit class wide subtype
5667
5668 function Make_Subtype_From_Expr
5669 (E : Node_Id;
23e83313 5670 Unc_Typ : Entity_Id) return Node_Id
ee6ba406 5671 is
5672 Loc : constant Source_Ptr := Sloc (E);
9dfe12ae 5673 List_Constr : constant List_Id := New_List;
ee6ba406 5674 D : Entity_Id;
5675
5676 Full_Subtyp : Entity_Id;
5677 Priv_Subtyp : Entity_Id;
5678 Utyp : Entity_Id;
5679 Full_Exp : Node_Id;
5680
5681 begin
5682 if Is_Private_Type (Unc_Typ)
5683 and then Has_Unknown_Discriminants (Unc_Typ)
5684 then
9dfe12ae 5685 -- Prepare the subtype completion, Go to base type to
041a8137 5686 -- find underlying type, because the type may be a generic
5687 -- actual or an explicit subtype.
ee6ba406 5688
9dfe12ae 5689 Utyp := Underlying_Type (Base_Type (Unc_Typ));
11deeeb6 5690 Full_Subtyp := Make_Temporary (Loc, 'C');
226494a3 5691 Full_Exp :=
11deeeb6 5692 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
ee6ba406 5693 Set_Parent (Full_Exp, Parent (E));
5694
11deeeb6 5695 Priv_Subtyp := Make_Temporary (Loc, 'P');
ee6ba406 5696
5697 Insert_Action (E,
5698 Make_Subtype_Declaration (Loc,
5699 Defining_Identifier => Full_Subtyp,
5700 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
5701
5702 -- Define the dummy private subtype
5703
5704 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
041a8137 5705 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
ee6ba406 5706 Set_Scope (Priv_Subtyp, Full_Subtyp);
5707 Set_Is_Constrained (Priv_Subtyp);
5708 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
5709 Set_Is_Itype (Priv_Subtyp);
5710 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
5711
5712 if Is_Tagged_Type (Priv_Subtyp) then
5713 Set_Class_Wide_Type
5714 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9431b9db 5715 Set_Direct_Primitive_Operations (Priv_Subtyp,
5716 Direct_Primitive_Operations (Unc_Typ));
ee6ba406 5717 end if;
5718
5719 Set_Full_View (Priv_Subtyp, Full_Subtyp);
5720
5721 return New_Reference_To (Priv_Subtyp, Loc);
5722
5723 elsif Is_Array_Type (Unc_Typ) then
5724 for J in 1 .. Number_Dimensions (Unc_Typ) loop
5725 Append_To (List_Constr,
5726 Make_Range (Loc,
5727 Low_Bound =>
5728 Make_Attribute_Reference (Loc,
226494a3 5729 Prefix => Duplicate_Subexpr_No_Checks (E),
ee6ba406 5730 Attribute_Name => Name_First,
5731 Expressions => New_List (
5732 Make_Integer_Literal (Loc, J))),
226494a3 5733
ee6ba406 5734 High_Bound =>
5735 Make_Attribute_Reference (Loc,
226494a3 5736 Prefix => Duplicate_Subexpr_No_Checks (E),
ee6ba406 5737 Attribute_Name => Name_Last,
5738 Expressions => New_List (
5739 Make_Integer_Literal (Loc, J)))));
5740 end loop;
5741
5742 elsif Is_Class_Wide_Type (Unc_Typ) then
5743 declare
5744 CW_Subtype : Entity_Id;
5745 EQ_Typ : Entity_Id := Empty;
5746
5747 begin
37808946 5748 -- A class-wide equivalent type is not needed when VM_Target
5749 -- because the VM back-ends handle the class-wide object
3d069ad4 5750 -- initialization itself (and doesn't need or want the
ee6ba406 5751 -- additional intermediate type to handle the assignment).
5752
662256db 5753 if Expander_Active and then Tagged_Type_Expansion then
5c8da411 5754
dc74650f 5755 -- If this is the class_wide type of a completion that is a
5756 -- record subtype, set the type of the class_wide type to be
5757 -- the full base type, for use in the expanded code for the
5758 -- equivalent type. Should this be done earlier when the
5759 -- completion is analyzed ???
5c8da411 5760
5761 if Is_Private_Type (Etype (Unc_Typ))
5762 and then
5763 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
5764 then
5765 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
5766 end if;
5767
ee6ba406 5768 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
5769 end if;
5770
5771 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
5772 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
5773 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
5774
5775 return New_Occurrence_Of (CW_Subtype, Loc);
5776 end;
5777
041a8137 5778 -- Indefinite record type with discriminants
9dfe12ae 5779
ee6ba406 5780 else
5781 D := First_Discriminant (Unc_Typ);
9dfe12ae 5782 while Present (D) loop
ee6ba406 5783 Append_To (List_Constr,
5784 Make_Selected_Component (Loc,
226494a3 5785 Prefix => Duplicate_Subexpr_No_Checks (E),
ee6ba406 5786 Selector_Name => New_Reference_To (D, Loc)));
5787
5788 Next_Discriminant (D);
5789 end loop;
5790 end if;
5791
5792 return
5793 Make_Subtype_Indication (Loc,
5794 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
5795 Constraint =>
5796 Make_Index_Or_Discriminant_Constraint (Loc,
5797 Constraints => List_Constr));
5798 end Make_Subtype_From_Expr;
5799
5800 -----------------------------
5801 -- May_Generate_Large_Temp --
5802 -----------------------------
5803
dc74650f 5804 -- At the current time, the only types that we return False for (i.e. where
5805 -- we decide we know they cannot generate large temps) are ones where we
5806 -- know the size is 256 bits or less at compile time, and we are still not
5807 -- doing a thorough job on arrays and records ???
ee6ba406 5808
5809 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
5810 begin
5329ca64 5811 if not Size_Known_At_Compile_Time (Typ) then
ee6ba406 5812 return False;
5813
5814 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
5815 return False;
5816
5817 elsif Is_Array_Type (Typ)
5818 and then Present (Packed_Array_Type (Typ))
5819 then
5820 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
5821
5822 -- We could do more here to find other small types ???
5823
5824 else
5825 return True;
5826 end if;
5827 end May_Generate_Large_Temp;
5828
bb3b440a 5829 ------------------------
5830 -- Needs_Finalization --
5831 ------------------------
5832
5833 function Needs_Finalization (T : Entity_Id) return Boolean is
5834 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
5835 -- If type is not frozen yet, check explicitly among its components,
5836 -- because the Has_Controlled_Component flag is not necessarily set.
5837
5838 -----------------------------------
5839 -- Has_Some_Controlled_Component --
5840 -----------------------------------
5841
5842 function Has_Some_Controlled_Component
5843 (Rec : Entity_Id) return Boolean
5844 is
5845 Comp : Entity_Id;
5846
5847 begin
5848 if Has_Controlled_Component (Rec) then
5849 return True;
5850
5851 elsif not Is_Frozen (Rec) then
5852 if Is_Record_Type (Rec) then
5853 Comp := First_Entity (Rec);
5854
5855 while Present (Comp) loop
5856 if not Is_Type (Comp)
5857 and then Needs_Finalization (Etype (Comp))
5858 then
5859 return True;
5860 end if;
5861
5862 Next_Entity (Comp);
5863 end loop;
5864
5865 return False;
5866
5867 elsif Is_Array_Type (Rec) then
5868 return Needs_Finalization (Component_Type (Rec));
5869
5870 else
5871 return Has_Controlled_Component (Rec);
5872 end if;
5873 else
5874 return False;
5875 end if;
5876 end Has_Some_Controlled_Component;
5877
5878 -- Start of processing for Needs_Finalization
5879
5880 begin
a08d6ad4 5881 -- Certain run-time configurations and targets do not provide support
5882 -- for controlled types.
bb3b440a 5883
a08d6ad4 5884 if Restriction_Active (No_Finalization) then
5885 return False;
bb3b440a 5886
53c179ea 5887 -- C, C++, CIL and Java types are not considered controlled. It is
5888 -- assumed that the non-Ada side will handle their clean up.
5889
5890 elsif Convention (T) = Convention_C
5891 or else Convention (T) = Convention_CIL
5892 or else Convention (T) = Convention_CPP
5893 or else Convention (T) = Convention_Java
5894 then
5895 return False;
5896
a08d6ad4 5897 else
5898 -- Class-wide types are treated as controlled because derivations
5899 -- from the root type can introduce controlled components.
5900
5901 return
5902 Is_Class_Wide_Type (T)
5903 or else Is_Controlled (T)
5904 or else Has_Controlled_Component (T)
5905 or else Has_Some_Controlled_Component (T)
5906 or else
5907 (Is_Concurrent_Type (T)
bb3b440a 5908 and then Present (Corresponding_Record_Type (T))
5909 and then Needs_Finalization (Corresponding_Record_Type (T)));
a08d6ad4 5910 end if;
bb3b440a 5911 end Needs_Finalization;
5912
64427fe6 5913 ----------------------------
5914 -- Needs_Constant_Address --
5915 ----------------------------
5916
5917 function Needs_Constant_Address
5918 (Decl : Node_Id;
5919 Typ : Entity_Id) return Boolean
5920 is
5921 begin
5922
dc74650f 5923 -- If we have no initialization of any kind, then we don't need to place
5924 -- any restrictions on the address clause, because the object will be
5925 -- elaborated after the address clause is evaluated. This happens if the
5926 -- declaration has no initial expression, or the type has no implicit
5927 -- initialization, or the object is imported.
64427fe6 5928
dc74650f 5929 -- The same holds for all initialized scalar types and all access types.
5930 -- Packed bit arrays of size up to 64 are represented using a modular
5931 -- type with an initialization (to zero) and can be processed like other
5932 -- initialized scalar types.
64427fe6 5933
5934 -- If the type is controlled, code to attach the object to a
dc74650f 5935 -- finalization chain is generated at the point of declaration, and
5936 -- therefore the elaboration of the object cannot be delayed: the
5937 -- address expression must be a constant.
64427fe6 5938
5939 if No (Expression (Decl))
5940 and then not Needs_Finalization (Typ)
5941 and then
5942 (not Has_Non_Null_Base_Init_Proc (Typ)
5943 or else Is_Imported (Defining_Identifier (Decl)))
5944 then
5945 return False;
5946
5947 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
5948 or else Is_Access_Type (Typ)
5949 or else
5950 (Is_Bit_Packed_Array (Typ)
5951 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
5952 then
5953 return False;
5954
5955 else
5956
5957 -- Otherwise, we require the address clause to be constant because
5958 -- the call to the initialization procedure (or the attach code) has
5959 -- to happen at the point of the declaration.
5960
dc74650f 5961 -- Actually the IP call has been moved to the freeze actions anyway,
5962 -- so maybe we can relax this restriction???
64427fe6 5963
5964 return True;
5965 end if;
5966 end Needs_Constant_Address;
5967
ee6ba406 5968 ----------------------------
5969 -- New_Class_Wide_Subtype --
5970 ----------------------------
5971
5972 function New_Class_Wide_Subtype
5973 (CW_Typ : Entity_Id;
23e83313 5974 N : Node_Id) return Entity_Id
ee6ba406 5975 is
9dfe12ae 5976 Res : constant Entity_Id := Create_Itype (E_Void, N);
5977 Res_Name : constant Name_Id := Chars (Res);
5978 Res_Scope : constant Entity_Id := Scope (Res);
ee6ba406 5979
5980 begin
5981 Copy_Node (CW_Typ, Res);
014e9448 5982 Set_Comes_From_Source (Res, False);
ee6ba406 5983 Set_Sloc (Res, Sloc (N));
5984 Set_Is_Itype (Res);
5985 Set_Associated_Node_For_Itype (Res, N);
5986 Set_Is_Public (Res, False); -- By default, may be changed below.
5987 Set_Public_Status (Res);
5988 Set_Chars (Res, Res_Name);
5989 Set_Scope (Res, Res_Scope);
5990 Set_Ekind (Res, E_Class_Wide_Subtype);
5991 Set_Next_Entity (Res, Empty);
5992 Set_Etype (Res, Base_Type (CW_Typ));
d58b12c2 5993 Set_Is_Frozen (Res, False);
ee6ba406 5994 Set_Freeze_Node (Res, Empty);
5995 return (Res);
5996 end New_Class_Wide_Subtype;
5997
37808946 5998 --------------------------------
5999 -- Non_Limited_Designated_Type --
6000 ---------------------------------
6001
6002 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
6003 Desig : constant Entity_Id := Designated_Type (T);
6004 begin
6005 if Ekind (Desig) = E_Incomplete_Type
6006 and then Present (Non_Limited_View (Desig))
6007 then
6008 return Non_Limited_View (Desig);
6009 else
6010 return Desig;
6011 end if;
6012 end Non_Limited_Designated_Type;
6013
35889f1e 6014 -----------------------------------
6015 -- OK_To_Do_Constant_Replacement --
6016 -----------------------------------
6017
6018 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
6019 ES : constant Entity_Id := Scope (E);
6020 CS : Entity_Id;
6021
6022 begin
6023 -- Do not replace statically allocated objects, because they may be
6024 -- modified outside the current scope.
6025
6026 if Is_Statically_Allocated (E) then
6027 return False;
6028
6029 -- Do not replace aliased or volatile objects, since we don't know what
6030 -- else might change the value.
6031
6032 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
6033 return False;
6034
6035 -- Debug flag -gnatdM disconnects this optimization
6036
6037 elsif Debug_Flag_MM then
6038 return False;
6039
6040 -- Otherwise check scopes
6041
6042 else
35889f1e 6043 CS := Current_Scope;
6044
6045 loop
6046 -- If we are in right scope, replacement is safe
6047
6048 if CS = ES then
6049 return True;
6050
6051 -- Packages do not affect the determination of safety
6052
6053 elsif Ekind (CS) = E_Package then
35889f1e 6054 exit when CS = Standard_Standard;
014e9448 6055 CS := Scope (CS);
35889f1e 6056
6057 -- Blocks do not affect the determination of safety
6058
6059 elsif Ekind (CS) = E_Block then
6060 CS := Scope (CS);
6061
014e9448 6062 -- Loops do not affect the determination of safety. Note that we
6063 -- kill all current values on entry to a loop, so we are just
6064 -- talking about processing within a loop here.
6065
6066 elsif Ekind (CS) = E_Loop then
6067 CS := Scope (CS);
6068
35889f1e 6069 -- Otherwise, the reference is dubious, and we cannot be sure that
6070 -- it is safe to do the replacement.
6071
6072 else
6073 exit;
6074 end if;
6075 end loop;
6076
6077 return False;
6078 end if;
6079 end OK_To_Do_Constant_Replacement;
6080
37808946 6081 ------------------------------------
6082 -- Possible_Bit_Aligned_Component --
6083 ------------------------------------
6084
6085 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
6086 begin
6087 case Nkind (N) is
6088
6089 -- Case of indexed component
6090
6091 when N_Indexed_Component =>
6092 declare
6093 P : constant Node_Id := Prefix (N);
6094 Ptyp : constant Entity_Id := Etype (P);
6095
6096 begin
6097 -- If we know the component size and it is less than 64, then
3925b986 6098 -- we are definitely OK. The back end always does assignment of
6099 -- misaligned small objects correctly.
37808946 6100
6101 if Known_Static_Component_Size (Ptyp)
6102 and then Component_Size (Ptyp) <= 64
6103 then
6104 return False;
6105
6106 -- Otherwise, we need to test the prefix, to see if we are
6107 -- indexing from a possibly unaligned component.
6108
6109 else
6110 return Possible_Bit_Aligned_Component (P);
6111 end if;
6112 end;
6113
6114 -- Case of selected component
6115
6116 when N_Selected_Component =>
6117 declare
6118 P : constant Node_Id := Prefix (N);
6119 Comp : constant Entity_Id := Entity (Selector_Name (N));
6120
6121 begin
6122 -- If there is no component clause, then we are in the clear
6123 -- since the back end will never misalign a large component
6124 -- unless it is forced to do so. In the clear means we need
6125 -- only the recursive test on the prefix.
6126
6127 if Component_May_Be_Bit_Aligned (Comp) then
6128 return True;
6129 else
6130 return Possible_Bit_Aligned_Component (P);
6131 end if;
6132 end;
6133
57993a53 6134 -- For a slice, test the prefix, if that is possibly misaligned,
6135 -- then for sure the slice is!
6136
6137 when N_Slice =>
6138 return Possible_Bit_Aligned_Component (Prefix (N));
6139
ac892341 6140 -- For an unchecked conversion, check whether the expression may
6141 -- be bit-aligned.
6142
6143 when N_Unchecked_Type_Conversion =>
6144 return Possible_Bit_Aligned_Component (Expression (N));
6145
57993a53 6146 -- If we have none of the above, it means that we have fallen off the
6147 -- top testing prefixes recursively, and we now have a stand alone
6148 -- object, where we don't have a problem.
37808946 6149
6150 when others =>
6151 return False;
6152
6153 end case;
6154 end Possible_Bit_Aligned_Component;
6155
f239f5be 6156 -----------------------------------------------
6157 -- Process_Statements_For_Controlled_Objects --
6158 -----------------------------------------------
6159
6160 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
6161 Loc : constant Source_Ptr := Sloc (N);
6162
6163 function Are_Wrapped (L : List_Id) return Boolean;
6164 -- Determine whether list L contains only one statement which is a block
6165
6166 function Wrap_Statements_In_Block (L : List_Id) return Node_Id;
6167 -- Given a list of statements L, wrap it in a block statement and return
6168 -- the generated node.
6169
6170 -----------------
6171 -- Are_Wrapped --
6172 -----------------
6173
6174 function Are_Wrapped (L : List_Id) return Boolean is
6175 Stmt : constant Node_Id := First (L);
f239f5be 6176 begin
6177 return
6178 Present (Stmt)
6179 and then No (Next (Stmt))
6180 and then Nkind (Stmt) = N_Block_Statement;
6181 end Are_Wrapped;
6182
6183 ------------------------------
6184 -- Wrap_Statements_In_Block --
6185 ------------------------------
6186
6187 function Wrap_Statements_In_Block (L : List_Id) return Node_Id is
6188 begin
6189 return
6190 Make_Block_Statement (Loc,
6191 Declarations => No_List,
6192 Handled_Statement_Sequence =>
6193 Make_Handled_Sequence_Of_Statements (Loc,
6194 Statements => L));
6195 end Wrap_Statements_In_Block;
6196
0da7785c 6197 -- Local variables
6198
6199 Block : Node_Id;
6200
f239f5be 6201 -- Start of processing for Process_Statements_For_Controlled_Objects
6202
6203 begin
0da7785c 6204 -- Whenever a non-handled statement list is wrapped in a block, the
6205 -- block must be explicitly analyzed to redecorate all entities in the
6206 -- list and ensure that a finalizer is properly built.
6207
f239f5be 6208 case Nkind (N) is
f53dda32 6209 when N_Elsif_Part |
6210 N_If_Statement |
6211 N_Conditional_Entry_Call |
6212 N_Selective_Accept =>
f239f5be 6213
6214 -- Check the "then statements" for elsif parts and if statements
6215
f53dda32 6216 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
f239f5be 6217 and then not Is_Empty_List (Then_Statements (N))
6218 and then not Are_Wrapped (Then_Statements (N))
6219 and then Requires_Cleanup_Actions
6220 (Then_Statements (N), False, False)
6221 then
0da7785c 6222 Block := Wrap_Statements_In_Block (Then_Statements (N));
6223 Set_Then_Statements (N, New_List (Block));
6224
6225 Analyze (Block);
f239f5be 6226 end if;
6227
6228 -- Check the "else statements" for conditional entry calls, if
6229 -- statements and selective accepts.
6230
6231 if Nkind_In (N, N_Conditional_Entry_Call,
6232 N_If_Statement,
6233 N_Selective_Accept)
6234 and then not Is_Empty_List (Else_Statements (N))
6235 and then not Are_Wrapped (Else_Statements (N))
6236 and then Requires_Cleanup_Actions
6237 (Else_Statements (N), False, False)
6238 then
0da7785c 6239 Block := Wrap_Statements_In_Block (Else_Statements (N));
6240 Set_Else_Statements (N, New_List (Block));
6241
6242 Analyze (Block);
f239f5be 6243 end if;
6244
6245 when N_Abortable_Part |
6246 N_Accept_Alternative |
6247 N_Case_Statement_Alternative |
6248 N_Delay_Alternative |
6249 N_Entry_Call_Alternative |
6250 N_Exception_Handler |
6251 N_Loop_Statement |
6252 N_Triggering_Alternative =>
6253
6254 if not Is_Empty_List (Statements (N))
6255 and then not Are_Wrapped (Statements (N))
6256 and then Requires_Cleanup_Actions (Statements (N), False, False)
6257 then
0da7785c 6258 Block := Wrap_Statements_In_Block (Statements (N));
6259 Set_Statements (N, New_List (Block));
6260
6261 Analyze (Block);
f239f5be 6262 end if;
6263
6264 when others =>
6265 null;
6266 end case;
6267 end Process_Statements_For_Controlled_Objects;
6268
ee6ba406 6269 -------------------------
6270 -- Remove_Side_Effects --
6271 -------------------------
6272
6273 procedure Remove_Side_Effects
6274 (Exp : Node_Id;
6275 Name_Req : Boolean := False;
6276 Variable_Ref : Boolean := False)
6277 is
fafc6b97 6278 Loc : constant Source_Ptr := Sloc (Exp);
6279 Exp_Type : constant Entity_Id := Etype (Exp);
6280 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
ee6ba406 6281 Def_Id : Entity_Id;
54665c5c 6282 E : Node_Id;
6283 New_Exp : Node_Id;
6284 Ptr_Typ_Decl : Node_Id;
ee6ba406 6285 Ref_Type : Entity_Id;
6286 Res : Node_Id;
ee6ba406 6287
6288 function Side_Effect_Free (N : Node_Id) return Boolean;
35889f1e 6289 -- Determines if the tree N represents an expression that is known not
6290 -- to have side effects, and for which no processing is required.
ee6ba406 6291
6292 function Side_Effect_Free (L : List_Id) return Boolean;
6293 -- Determines if all elements of the list L are side effect free
6294
9dfe12ae 6295 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
35889f1e 6296 -- The argument N is a construct where the Prefix is dereferenced if it
6297 -- is an access type and the result is a variable. The call returns True
6298 -- if the construct is side effect free (not considering side effects in
6299 -- other than the prefix which are to be tested by the caller).
9dfe12ae 6300
6301 function Within_In_Parameter (N : Node_Id) return Boolean;
35889f1e 6302 -- Determines if N is a subcomponent of a composite in-parameter. If so,
6303 -- N is not side-effect free when the actual is global and modifiable
6304 -- indirectly from within a subprogram, because it may be passed by
6305 -- reference. The front-end must be conservative here and assume that
6306 -- this may happen with any array or record type. On the other hand, we
6307 -- cannot create temporaries for all expressions for which this
6308 -- condition is true, for various reasons that might require clearing up
526aedbb 6309 -- ??? For example, discriminant references that appear out of place, or
35889f1e 6310 -- spurious type errors with class-wide expressions. As a result, we
6311 -- limit the transformation to loop bounds, which is so far the only
6312 -- case that requires it.
9dfe12ae 6313
6314 -----------------------------
6315 -- Safe_Prefixed_Reference --
6316 -----------------------------
6317
6318 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
6319 begin
6320 -- If prefix is not side effect free, definitely not safe
ee6ba406 6321
9dfe12ae 6322 if not Side_Effect_Free (Prefix (N)) then
6323 return False;
ee6ba406 6324
9dfe12ae 6325 -- If the prefix is of an access type that is not access-to-constant,
6326 -- then this construct is a variable reference, which means it is to
6dbb316f 6327 -- be considered to have side effects if Variable_Ref is set True.
9dfe12ae 6328
6329 elsif Is_Access_Type (Etype (Prefix (N)))
ee6ba406 6330 and then not Is_Access_Constant (Etype (Prefix (N)))
9dfe12ae 6331 and then Variable_Ref
6332 then
6dbb316f 6333 -- Exception is a prefix that is the result of a previous removal
6334 -- of side-effects.
6335
6336 return Is_Entity_Name (Prefix (N))
6337 and then not Comes_From_Source (Prefix (N))
6338 and then Ekind (Entity (Prefix (N))) = E_Constant
6339 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
9dfe12ae 6340
ad8b87c8 6341 -- If the prefix is an explicit dereference then this construct is a
6342 -- variable reference, which means it is to be considered to have
6343 -- side effects if Variable_Ref is True.
6344
6345 -- We do NOT exclude dereferences of access-to-constant types because
6346 -- we handle them as constant view of variables.
9d0f093f 6347
9d0f093f 6348 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
9d0f093f 6349 and then Variable_Ref
6350 then
d900a3fb 6351 return False;
9d0f093f 6352
245e87df 6353 -- Note: The following test is the simplest way of solving a complex
6354 -- problem uncovered by the following test (Side effect on loop bound
6355 -- that is a subcomponent of a global variable:
6356
6357 -- with Text_Io; use Text_Io;
6358 -- procedure Tloop is
6359 -- type X is
6360 -- record
6361 -- V : Natural := 4;
6362 -- S : String (1..5) := (others => 'a');
6363 -- end record;
6364 -- X1 : X;
6365
6366 -- procedure Modi;
6367
6368 -- generic
6369 -- with procedure Action;
6370 -- procedure Loop_G (Arg : X; Msg : String)
6371
6372 -- procedure Loop_G (Arg : X; Msg : String) is
6373 -- begin
6374 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
6375 -- & Natural'Image (Arg.V));
6376 -- for Index in 1 .. Arg.V loop
6377 -- Text_Io.Put_Line
6378 -- (Natural'Image (Index) & " " & Arg.S (Index));
6379 -- if Index > 2 then
6380 -- Modi;
6381 -- end if;
6382 -- end loop;
6383 -- Put_Line ("end loop_g " & Msg);
6384 -- end;
6385
6386 -- procedure Loop1 is new Loop_G (Modi);
6387 -- procedure Modi is
6388 -- begin
6389 -- X1.V := 1;
6390 -- Loop1 (X1, "from modi");
6391 -- end;
6392 --
6393 -- begin
6394 -- Loop1 (X1, "initial");
6395 -- end;
6396
6397 -- The output of the above program should be:
6398
6399 -- begin loop_g initial will loop till: 4
6400 -- 1 a
6401 -- 2 a
6402 -- 3 a
6403 -- begin loop_g from modi will loop till: 1
6404 -- 1 a
6405 -- end loop_g from modi
6406 -- 4 a
6407 -- begin loop_g from modi will loop till: 1
6408 -- 1 a
6409 -- end loop_g from modi
6410 -- end loop_g initial
6411
6412 -- If a loop bound is a subcomponent of a global variable, a
6413 -- modification of that variable within the loop may incorrectly
6414 -- affect the execution of the loop.
9dfe12ae 6415
d900a3fb 6416 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
6417 and then Within_In_Parameter (Prefix (N))
6418 and then Variable_Ref
9dfe12ae 6419 then
6420 return False;
6421
6422 -- All other cases are side effect free
6423
6424 else
6425 return True;
6426 end if;
6427 end Safe_Prefixed_Reference;
ee6ba406 6428
6429 ----------------------
6430 -- Side_Effect_Free --
6431 ----------------------
6432
6433 function Side_Effect_Free (N : Node_Id) return Boolean is
ee6ba406 6434 begin
7836f2d6 6435 -- Note on checks that could raise Constraint_Error. Strictly, if we
6436 -- take advantage of 11.6, these checks do not count as side effects.
6437 -- However, we would prefer to consider that they are side effects,
6438 -- since the backend CSE does not work very well on expressions which
6439 -- can raise Constraint_Error. On the other hand if we don't consider
6440 -- them to be side effect free, then we get some awkward expansions
6441 -- in -gnato mode, resulting in code insertions at a point where we
6442 -- do not have a clear model for performing the insertions.
ee6ba406 6443
9dfe12ae 6444 -- Special handling for entity names
ee6ba406 6445
9dfe12ae 6446 if Is_Entity_Name (N) then
ee6ba406 6447
9dfe12ae 6448 -- Variables are considered to be a side effect if Variable_Ref
57993a53 6449 -- is set or if we have a volatile reference and Name_Req is off.
9dfe12ae 6450 -- If Name_Req is True then we can't help returning a name which
6451 -- effectively allows multiple references in any case.
ee6ba406 6452
87bdc21d 6453 if Is_Variable (N, Use_Original_Node => False) then
9dfe12ae 6454 return not Variable_Ref
57993a53 6455 and then (not Is_Volatile_Reference (N) or else Name_Req);
9dfe12ae 6456
6457 -- Any other entity (e.g. a subtype name) is definitely side
6458 -- effect free.
ee6ba406 6459
6460 else
6461 return True;
6462 end if;
6463
6464 -- A value known at compile time is always side effect free
6465
6466 elsif Compile_Time_Known_Value (N) then
6467 return True;
37808946 6468
dc74650f 6469 -- A variable renaming is not side-effect free, because the renaming
6470 -- will function like a macro in the front-end in some cases, and an
6471 -- assignment can modify the component designated by N, so we need to
6472 -- create a temporary for it.
37808946 6473
dc74650f 6474 -- The guard testing for Entity being present is needed at least in
6475 -- the case of rewritten predicate expressions, and may well also be
2072eaa9 6476 -- appropriate elsewhere. Obviously we can't go testing the entity
dc74650f 6477 -- field if it does not exist, so it's reasonable to say that this is
6478 -- not the renaming case if it does not exist.
2072eaa9 6479
37808946 6480 elsif Is_Entity_Name (Original_Node (N))
2072eaa9 6481 and then Present (Entity (Original_Node (N)))
37808946 6482 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
6483 and then Ekind (Entity (Original_Node (N))) /= E_Constant
6484 then
6485 return False;
9b1a6ae8 6486
6487 -- Remove_Side_Effects generates an object renaming declaration to
6488 -- capture the expression of a class-wide expression. In VM targets
6489 -- the frontend performs no expansion for dispatching calls to
dc74650f 6490 -- class- wide types since they are handled by the VM. Hence, we must
9b1a6ae8 6491 -- locate here if this node corresponds to a previous invocation of
6492 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
6493
6494 elsif VM_Target /= No_VM
6495 and then not Comes_From_Source (N)
9b1a6ae8 6496 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
c2ed7507 6497 and then Is_Class_Wide_Type (Etype (N))
9b1a6ae8 6498 then
6499 return True;
9dfe12ae 6500 end if;
ee6ba406 6501
9dfe12ae 6502 -- For other than entity names and compile time known values,
6503 -- check the node kind for special processing.
ee6ba406 6504
9dfe12ae 6505 case Nkind (N) is
6506
6507 -- An attribute reference is side effect free if its expressions
6508 -- are side effect free and its prefix is side effect free or
6509 -- is an entity reference.
6510
6511 -- Is this right? what about x'first where x is a variable???
6512
6513 when N_Attribute_Reference =>
6514 return Side_Effect_Free (Expressions (N))
35889f1e 6515 and then Attribute_Name (N) /= Name_Input
9dfe12ae 6516 and then (Is_Entity_Name (Prefix (N))
6517 or else Side_Effect_Free (Prefix (N)));
6518
dc74650f 6519 -- A binary operator is side effect free if and both operands are
6520 -- side effect free. For this purpose binary operators include
54665c5c 6521 -- membership tests and short circuit forms.
9dfe12ae 6522
7b31b357 6523 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
9dfe12ae 6524 return Side_Effect_Free (Left_Opnd (N))
7b31b357 6525 and then
6526 Side_Effect_Free (Right_Opnd (N));
9dfe12ae 6527
6528 -- An explicit dereference is side effect free only if it is
6529 -- a side effect free prefixed reference.
6530
6531 when N_Explicit_Dereference =>
6532 return Safe_Prefixed_Reference (N);
6533
6534 -- A call to _rep_to_pos is side effect free, since we generate
6535 -- this pure function call ourselves. Moreover it is critically
dc74650f 6536 -- important to make this exception, since otherwise we can have
6537 -- discriminants in array components which don't look side effect
6538 -- free in the case of an array whose index type is an enumeration
6539 -- type with an enumeration rep clause.
9dfe12ae 6540
6541 -- All other function calls are not side effect free
6542
6543 when N_Function_Call =>
6544 return Nkind (Name (N)) = N_Identifier
6545 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
6546 and then
6547 Side_Effect_Free (First (Parameter_Associations (N)));
ee6ba406 6548
9dfe12ae 6549 -- An indexed component is side effect free if it is a side
6550 -- effect free prefixed reference and all the indexing
6551 -- expressions are side effect free.
ee6ba406 6552
9dfe12ae 6553 when N_Indexed_Component =>
6554 return Side_Effect_Free (Expressions (N))
6555 and then Safe_Prefixed_Reference (N);
ee6ba406 6556
9dfe12ae 6557 -- A type qualification is side effect free if the expression
6558 -- is side effect free.
ee6ba406 6559
9dfe12ae 6560 when N_Qualified_Expression =>
ee6ba406 6561 return Side_Effect_Free (Expression (N));
ee6ba406 6562
dc74650f 6563 -- A selected component is side effect free only if it is a side
6564 -- effect free prefixed reference. If it designates a component
6565 -- with a rep. clause it must be treated has having a potential
6566 -- side effect, because it may be modified through a renaming, and
6567 -- a subsequent use of the renaming as a macro will yield the
6568 -- wrong value. This complex interaction between renaming and
6569 -- removing side effects is a reminder that the latter has become
6570 -- a headache to maintain, and that it should be removed in favor
6571 -- of the gcc mechanism to capture values ???
ee6ba406 6572
9dfe12ae 6573 when N_Selected_Component =>
0cd40f50 6574 if Nkind (Parent (N)) = N_Explicit_Dereference
6575 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
6576 then
6577 return False;
6578 else
6579 return Safe_Prefixed_Reference (N);
6580 end if;
ee6ba406 6581
9dfe12ae 6582 -- A range is side effect free if the bounds are side effect free
ee6ba406 6583
9dfe12ae 6584 when N_Range =>
6585 return Side_Effect_Free (Low_Bound (N))
6586 and then Side_Effect_Free (High_Bound (N));
ee6ba406 6587
9dfe12ae 6588 -- A slice is side effect free if it is a side effect free
6589 -- prefixed reference and the bounds are side effect free.
ee6ba406 6590
9dfe12ae 6591 when N_Slice =>
6592 return Side_Effect_Free (Discrete_Range (N))
6593 and then Safe_Prefixed_Reference (N);
ee6ba406 6594
0cd40f50 6595 -- A type conversion is side effect free if the expression to be
6596 -- converted is side effect free.
ee6ba406 6597
9dfe12ae 6598 when N_Type_Conversion =>
6599 return Side_Effect_Free (Expression (N));
6600
6601 -- A unary operator is side effect free if the operand
6602 -- is side effect free.
6603
6604 when N_Unary_Op =>
6605 return Side_Effect_Free (Right_Opnd (N));
6606
6607 -- An unchecked type conversion is side effect free only if it
6608 -- is safe and its argument is side effect free.
6609
6610 when N_Unchecked_Type_Conversion =>
6611 return Safe_Unchecked_Type_Conversion (N)
6612 and then Side_Effect_Free (Expression (N));
6613
6614 -- An unchecked expression is side effect free if its expression
6615 -- is side effect free.
6616
6617 when N_Unchecked_Expression =>
6618 return Side_Effect_Free (Expression (N));
6619
8b20af99 6620 -- A literal is side effect free
6621
6622 when N_Character_Literal |
6623 N_Integer_Literal |
6624 N_Real_Literal |
cdc6fa44 6625 N_String_Literal =>
8b20af99 6626 return True;
6627
9dfe12ae 6628 -- We consider that anything else has side effects. This is a bit
6629 -- crude, but we are pretty close for most common cases, and we
6630 -- are certainly correct (i.e. we never return True when the
6631 -- answer should be False).
6632
6633 when others =>
6634 return False;
6635 end case;
ee6ba406 6636 end Side_Effect_Free;
6637
dc74650f 6638 -- A list is side effect free if all elements of the list are side
6639 -- effect free.
9dfe12ae 6640
ee6ba406 6641 function Side_Effect_Free (L : List_Id) return Boolean is
6642 N : Node_Id;
6643
6644 begin
6645 if L = No_List or else L = Error_List then
6646 return True;
6647
6648 else
6649 N := First (L);
ee6ba406 6650 while Present (N) loop
6651 if not Side_Effect_Free (N) then
6652 return False;
6653 else
6654 Next (N);
6655 end if;
6656 end loop;
6657
6658 return True;
6659 end if;
6660 end Side_Effect_Free;
6661
9dfe12ae 6662 -------------------------
6663 -- Within_In_Parameter --
6664 -------------------------
6665
6666 function Within_In_Parameter (N : Node_Id) return Boolean is
6667 begin
6668 if not Comes_From_Source (N) then
6669 return False;
6670
6671 elsif Is_Entity_Name (N) then
0cd40f50 6672 return Ekind (Entity (N)) = E_In_Parameter;
9dfe12ae 6673
6674 elsif Nkind (N) = N_Indexed_Component
6675 or else Nkind (N) = N_Selected_Component
6676 then
6677 return Within_In_Parameter (Prefix (N));
6678 else
6679
6680 return False;
6681 end if;
6682 end Within_In_Parameter;
6683
ee6ba406 6684 -- Start of processing for Remove_Side_Effects
6685
6686 begin
96af89e4 6687 -- Handle cases in which there is nothing to do
6688
6689 if not Expander_Active then
6dbb316f 6690 return;
c0538fdb 6691 end if;
6dbb316f 6692
6693 -- Cannot generate temporaries if the invocation to remove side effects
6694 -- was issued too early and the type of the expression is not resolved
6695 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
6696 -- Remove_Side_Effects).
6697
c0538fdb 6698 if No (Exp_Type)
6dbb316f 6699 or else Ekind (Exp_Type) = E_Access_Attribute_Type
6700 then
6701 return;
6702
6703 -- No action needed for side-effect free expressions
ee6ba406 6704
6dbb316f 6705 elsif Side_Effect_Free (Exp) then
ee6ba406 6706 return;
6707 end if;
6708
9dfe12ae 6709 -- All this must not have any checks
ee6ba406 6710
fafc6b97 6711 Scope_Suppress := Suppress_All;
ee6ba406 6712
0cd40f50 6713 -- If it is a scalar type and we need to capture the value, just make
79d59c5e 6714 -- a copy. Likewise for a function call, an attribute reference, an
0f6fd0ec 6715 -- allocator, or an operator. And if we have a volatile reference and
79d59c5e 6716 -- Name_Req is not set (see comments above for Side_Effect_Free).
1735f911 6717
6718 if Is_Elementary_Type (Exp_Type)
6719 and then (Variable_Ref
6720 or else Nkind (Exp) = N_Function_Call
57993a53 6721 or else Nkind (Exp) = N_Attribute_Reference
79d59c5e 6722 or else Nkind (Exp) = N_Allocator
0cd40f50 6723 or else Nkind (Exp) in N_Op
57993a53 6724 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
1735f911 6725 then
1a8bc727 6726 Def_Id := Make_Temporary (Loc, 'R', Exp);
1735f911 6727 Set_Etype (Def_Id, Exp_Type);
6728 Res := New_Reference_To (Def_Id, Loc);
6729
dc74650f 6730 -- If the expression is a packed reference, it must be reanalyzed and
6731 -- expanded, depending on context. This is the case for actuals where
6732 -- a constraint check may capture the actual before expansion of the
6733 -- call is complete.
5e797030 6734
6735 if Nkind (Exp) = N_Indexed_Component
6736 and then Is_Packed (Etype (Prefix (Exp)))
6737 then
6738 Set_Analyzed (Exp, False);
6739 Set_Analyzed (Prefix (Exp), False);
6740 end if;
6741
1735f911 6742 E :=
6743 Make_Object_Declaration (Loc,
6744 Defining_Identifier => Def_Id,
6745 Object_Definition => New_Reference_To (Exp_Type, Loc),
6746 Constant_Present => True,
6747 Expression => Relocate_Node (Exp));
6748
6749 Set_Assignment_OK (E);
6750 Insert_Action (Exp, E);
6751
dc74650f 6752 -- If the expression has the form v.all then we can just capture the
6753 -- pointer, and then do an explicit dereference on the result.
ee6ba406 6754
1735f911 6755 elsif Nkind (Exp) = N_Explicit_Dereference then
1a8bc727 6756 Def_Id := Make_Temporary (Loc, 'R', Exp);
ee6ba406 6757 Res :=
6758 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
6759
6760 Insert_Action (Exp,
6761 Make_Object_Declaration (Loc,
6762 Defining_Identifier => Def_Id,
6763 Object_Definition =>
6764 New_Reference_To (Etype (Prefix (Exp)), Loc),
6765 Constant_Present => True,
6766 Expression => Relocate_Node (Prefix (Exp))));
6767
dc74650f 6768 -- Similar processing for an unchecked conversion of an expression of
6769 -- the form v.all, where we want the same kind of treatment.
9dfe12ae 6770
6771 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6772 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
6773 then
35403645 6774 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
9dfe12ae 6775 Scope_Suppress := Svg_Suppress;
6776 return;
6777
ee6ba406 6778 -- If this is a type conversion, leave the type conversion and remove
6779 -- the side effects in the expression. This is important in several
dc74650f 6780 -- circumstances: for change of representations, and also when this is a
6781 -- view conversion to a smaller object, where gigi can end up creating
57993a53 6782 -- its own temporary of the wrong size.
6af1bdbc 6783
35889f1e 6784 elsif Nkind (Exp) = N_Type_Conversion then
35403645 6785 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
ee6ba406 6786 Scope_Suppress := Svg_Suppress;
6787 return;
6788
1735f911 6789 -- If this is an unchecked conversion that Gigi can't handle, make
6790 -- a copy or a use a renaming to capture the value.
6791
6792 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6793 and then not Safe_Unchecked_Type_Conversion (Exp)
6794 then
45851103 6795 if CW_Or_Has_Controlled_Part (Exp_Type) then
1735f911 6796
6797 -- Use a renaming to capture the expression, rather than create
6798 -- a controlled temporary.
6799
1a8bc727 6800 Def_Id := Make_Temporary (Loc, 'R', Exp);
1735f911 6801 Res := New_Reference_To (Def_Id, Loc);
6802
6803 Insert_Action (Exp,
6804 Make_Object_Renaming_Declaration (Loc,
6805 Defining_Identifier => Def_Id,
6806 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
6807 Name => Relocate_Node (Exp)));
6808
6809 else
1a8bc727 6810 Def_Id := Make_Temporary (Loc, 'R', Exp);
1735f911 6811 Set_Etype (Def_Id, Exp_Type);
6812 Res := New_Reference_To (Def_Id, Loc);
6813
6814 E :=
6815 Make_Object_Declaration (Loc,
6816 Defining_Identifier => Def_Id,
6817 Object_Definition => New_Reference_To (Exp_Type, Loc),
6818 Constant_Present => not Is_Variable (Exp),
6819 Expression => Relocate_Node (Exp));
6820
6821 Set_Assignment_OK (E);
6822 Insert_Action (Exp, E);
6823 end if;
6824
ee6ba406 6825 -- For expressions that denote objects, we can use a renaming scheme.
dc74650f 6826 -- This is needed for correctness in the case of a volatile object of a
6827 -- non-volatile type because the Make_Reference call of the "default"
6828 -- approach would generate an illegal access value (an access value
6829 -- cannot designate such an object - see Analyze_Reference). We skip
6830 -- using this scheme if we have an object of a volatile type and we do
6831 -- not have Name_Req set true (see comments above for Side_Effect_Free).
9dfe12ae 6832
ee6ba406 6833 elsif Is_Object_Reference (Exp)
6834 and then Nkind (Exp) /= N_Function_Call
69a5377d 6835 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
ee6ba406 6836 then
1a8bc727 6837 Def_Id := Make_Temporary (Loc, 'R', Exp);
ee6ba406 6838
6839 if Nkind (Exp) = N_Selected_Component
6840 and then Nkind (Prefix (Exp)) = N_Function_Call
35889f1e 6841 and then Is_Array_Type (Exp_Type)
ee6ba406 6842 then
6843 -- Avoid generating a variable-sized temporary, by generating
6844 -- the renaming declaration just for the function call. The
6845 -- transformation could be refined to apply only when the array
6846 -- component is constrained by a discriminant???
6847
6848 Res :=
6849 Make_Selected_Component (Loc,
6850 Prefix => New_Occurrence_Of (Def_Id, Loc),
6851 Selector_Name => Selector_Name (Exp));
6852
6853 Insert_Action (Exp,
6854 Make_Object_Renaming_Declaration (Loc,
6855 Defining_Identifier => Def_Id,
6856 Subtype_Mark =>
6857 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
6858 Name => Relocate_Node (Prefix (Exp))));
9dfe12ae 6859
ee6ba406 6860 else
6861 Res := New_Reference_To (Def_Id, Loc);
6862
6863 Insert_Action (Exp,
6864 Make_Object_Renaming_Declaration (Loc,
6865 Defining_Identifier => Def_Id,
6866 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
6867 Name => Relocate_Node (Exp)));
6868 end if;
6869
dc74650f 6870 -- If this is a packed reference, or a selected component with
6871 -- a non-standard representation, a reference to the temporary
6872 -- will be replaced by a copy of the original expression (see
57993a53 6873 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
35889f1e 6874 -- elaborated by gigi, and is of course not to be replaced in-line
6875 -- by the expression it renames, which would defeat the purpose of
6876 -- removing the side-effect.
6877
6878 if (Nkind (Exp) = N_Selected_Component
6879 or else Nkind (Exp) = N_Indexed_Component)
6880 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
6881 then
6882 null;
6883 else
6884 Set_Is_Renaming_Of_Object (Def_Id, False);
6885 end if;
ee6ba406 6886
6887 -- Otherwise we generate a reference to the value
6888
6889 else
96af89e4 6890 -- An expression which is in Alfa mode is considered side effect free
6891 -- if the resulting value is captured by a variable or a constant.
6892
6893 if Alfa_Mode
6894 and then Nkind (Parent (Exp)) = N_Object_Declaration
6895 then
6896 return;
6897 end if;
6898
7f2cf564 6899 -- Special processing for function calls that return a limited type.
6900 -- We need to build a declaration that will enable build-in-place
6901 -- expansion of the call. This is not done if the context is already
6902 -- an object declaration, to prevent infinite recursion.
57993a53 6903
6904 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
6905 -- to accommodate functions returning limited objects by reference.
6906
54665c5c 6907 if Ada_Version >= Ada_2005
6908 and then Nkind (Exp) = N_Function_Call
172f8d3a 6909 and then Is_Immutably_Limited_Type (Etype (Exp))
7f2cf564 6910 and then Nkind (Parent (Exp)) /= N_Object_Declaration
57993a53 6911 then
6912 declare
1a8bc727 6913 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
57993a53 6914 Decl : Node_Id;
6915
6916 begin
6917 Decl :=
6918 Make_Object_Declaration (Loc,
6919 Defining_Identifier => Obj,
6920 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
6921 Expression => Relocate_Node (Exp));
d215f619 6922
57993a53 6923 Insert_Action (Exp, Decl);
6924 Set_Etype (Obj, Exp_Type);
6925 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
6926 return;
6927 end;
6928 end if;
6929
54665c5c 6930 Def_Id := Make_Temporary (Loc, 'R', Exp);
6931 Set_Etype (Def_Id, Exp_Type);
ee6ba406 6932
96af89e4 6933 -- The regular expansion of functions with side effects involves the
6934 -- generation of an access type to capture the return value found on
6935 -- the secondary stack. Since Alfa (and why) cannot process access
6936 -- types, use a different approach which ignores the secondary stack
6937 -- and "copies" the returned object.
54665c5c 6938
96af89e4 6939 if Alfa_Mode then
6940 Res := New_Reference_To (Def_Id, Loc);
6941 Ref_Type := Exp_Type;
6942
6943 -- Regular expansion utilizing an access type and 'reference
54665c5c 6944
96af89e4 6945 else
6946 Res :=
6947 Make_Explicit_Dereference (Loc,
6948 Prefix => New_Reference_To (Def_Id, Loc));
52e56d2d 6949
96af89e4 6950 -- Generate:
6951 -- type Ann is access all <Exp_Type>;
54665c5c 6952
96af89e4 6953 Ref_Type := Make_Temporary (Loc, 'A');
6954
6955 Ptr_Typ_Decl :=
6956 Make_Full_Type_Declaration (Loc,
6957 Defining_Identifier => Ref_Type,
6958 Type_Definition =>
6959 Make_Access_To_Object_Definition (Loc,
6960 All_Present => True,
6961 Subtype_Indication =>
6962 New_Reference_To (Exp_Type, Loc)));
6963
6964 Insert_Action (Exp, Ptr_Typ_Decl);
6965 end if;
54665c5c 6966
6967 E := Exp;
ee6ba406 6968 if Nkind (E) = N_Explicit_Dereference then
6969 New_Exp := Relocate_Node (Prefix (E));
6970 else
6971 E := Relocate_Node (E);
54665c5c 6972
6973 -- Do not generate a 'reference in Alfa mode since the access type
6974 -- is not created in the first place.
6975
6976 if Alfa_Mode then
6977 New_Exp := E;
23056195 6978
6979 -- Otherwise generate reference, marking the value as non-null
6980 -- since we know it cannot be null and we don't want a check.
6981
54665c5c 6982 else
6983 New_Exp := Make_Reference (Loc, E);
472ea160 6984 Set_Is_Known_Non_Null (Def_Id);
54665c5c 6985 end if;
ee6ba406 6986 end if;
6987
ba40b4af 6988 if Is_Delayed_Aggregate (E) then
6989
6990 -- The expansion of nested aggregates is delayed until the
6991 -- enclosing aggregate is expanded. As aggregates are often
dc74650f 6992 -- qualified, the predicate applies to qualified expressions as
6993 -- well, indicating that the enclosing aggregate has not been
6994 -- expanded yet. At this point the aggregate is part of a
6995 -- stand-alone declaration, and must be fully expanded.
ba40b4af 6996
6997 if Nkind (E) = N_Qualified_Expression then
6998 Set_Expansion_Delayed (Expression (E), False);
6999 Set_Analyzed (Expression (E), False);
7000 else
7001 Set_Expansion_Delayed (E, False);
7002 end if;
7003
ee6ba406 7004 Set_Analyzed (E, False);
7005 end if;
7006
7007 Insert_Action (Exp,
7008 Make_Object_Declaration (Loc,
7009 Defining_Identifier => Def_Id,
7010 Object_Definition => New_Reference_To (Ref_Type, Loc),
64427fe6 7011 Constant_Present => True,
ee6ba406 7012 Expression => New_Exp));
7013 end if;
7014
dc74650f 7015 -- Preserve the Assignment_OK flag in all copies, since at least one
7016 -- copy may be used in a context where this flag must be set (otherwise
7017 -- why would the flag be set in the first place).
ee6ba406 7018
7019 Set_Assignment_OK (Res, Assignment_OK (Exp));
7020
7021 -- Finally rewrite the original expression and we are done
7022
7023 Rewrite (Exp, Res);
7024 Analyze_And_Resolve (Exp, Exp_Type);
7025 Scope_Suppress := Svg_Suppress;
7026 end Remove_Side_Effects;
7027
ba40b4af 7028 ---------------------------
7029 -- Represented_As_Scalar --
7030 ---------------------------
7031
7032 function Represented_As_Scalar (T : Entity_Id) return Boolean is
7033 UT : constant Entity_Id := Underlying_Type (T);
7034 begin
7035 return Is_Scalar_Type (UT)
7036 or else (Is_Bit_Packed_Array (UT)
7037 and then Is_Scalar_Type (Packed_Array_Type (UT)));
7038 end Represented_As_Scalar;
7039
fd68eaab 7040 ------------------------------
7041 -- Requires_Cleanup_Actions --
7042 ------------------------------
7043
cae6218b 7044 function Requires_Cleanup_Actions
7045 (N : Node_Id;
7046 Lib_Level : Boolean) return Boolean
7047 is
0a4dc67b 7048 At_Lib_Level : constant Boolean :=
7049 Lib_Level
7050 and then Nkind_In (N, N_Package_Body,
7051 N_Package_Specification);
cae6218b 7052 -- N is at the library level if the top-most context is a package and
7053 -- the path taken to reach N does not inlcude non-package constructs.
fd68eaab 7054
7055 begin
7056 case Nkind (N) is
7057 when N_Accept_Statement |
7058 N_Block_Statement |
7059 N_Entry_Body |
7060 N_Package_Body |
7061 N_Protected_Body |
7062 N_Subprogram_Body |
7063 N_Task_Body =>
7064 return
cae6218b 7065 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
fd68eaab 7066 or else
0a4dc67b 7067 (Present (Handled_Statement_Sequence (N))
7068 and then
7069 Requires_Cleanup_Actions
7070 (Statements (Handled_Statement_Sequence (N)),
7071 At_Lib_Level, True));
fd68eaab 7072
7073 when N_Package_Specification =>
7074 return
f239f5be 7075 Requires_Cleanup_Actions
cae6218b 7076 (Visible_Declarations (N), At_Lib_Level, True)
f239f5be 7077 or else
7078 Requires_Cleanup_Actions
cae6218b 7079 (Private_Declarations (N), At_Lib_Level, True);
fd68eaab 7080
7081 when others =>
7082 return False;
7083 end case;
7084 end Requires_Cleanup_Actions;
7085
7086 ------------------------------
7087 -- Requires_Cleanup_Actions --
7088 ------------------------------
7089
7090 function Requires_Cleanup_Actions
f239f5be 7091 (L : List_Id;
cae6218b 7092 Lib_Level : Boolean;
f239f5be 7093 Nested_Constructs : Boolean) return Boolean
fd68eaab 7094 is
7095 Decl : Node_Id;
7096 Expr : Node_Id;
7097 Obj_Id : Entity_Id;
7098 Obj_Typ : Entity_Id;
7099 Pack_Id : Entity_Id;
7100 Typ : Entity_Id;
7101
7102 begin
7103 if No (L)
7104 or else Is_Empty_List (L)
7105 then
7106 return False;
7107 end if;
7108
7109 Decl := First (L);
7110 while Present (Decl) loop
7111
7112 -- Library-level tagged types
7113
7114 if Nkind (Decl) = N_Full_Type_Declaration then
7115 Typ := Defining_Identifier (Decl);
7116
7117 if Is_Tagged_Type (Typ)
7118 and then Is_Library_Level_Entity (Typ)
7119 and then Convention (Typ) = Convention_Ada
7120 and then Present (Access_Disp_Table (Typ))
7121 and then RTE_Available (RE_Unregister_Tag)
7122 and then not No_Run_Time_Mode
7123 and then not Is_Abstract_Type (Typ)
7124 then
7125 return True;
7126 end if;
7127
7128 -- Regular object declarations
7129
7130 elsif Nkind (Decl) = N_Object_Declaration then
7131 Obj_Id := Defining_Identifier (Decl);
7132 Obj_Typ := Base_Type (Etype (Obj_Id));
7133 Expr := Expression (Decl);
7134
7135 -- Bypass any form of processing for objects which have their
7136 -- finalization disabled. This applies only to objects at the
7137 -- library level.
7138
cae6218b 7139 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
fd68eaab 7140 null;
7141
7142 -- Transient variables are treated separately in order to minimize
7143 -- the size of the generated code. See Exp_Ch7.Process_Transient_
7144 -- Objects.
7145
7146 elsif Is_Processed_Transient (Obj_Id) then
7147 null;
7148
7149 -- The object is of the form:
7150 -- Obj : Typ [:= Expr];
7151 --
7152 -- Do not process the incomplete view of a deferred constant. Do
7153 -- not consider tag-to-class-wide conversions.
7154
7155 elsif not Is_Imported (Obj_Id)
7156 and then Needs_Finalization (Obj_Typ)
7157 and then not (Ekind (Obj_Id) = E_Constant
7158 and then not Has_Completion (Obj_Id))
3ad9c375 7159 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
fd68eaab 7160 then
7161 return True;
7162
7163 -- The object is of the form:
7164 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
7165 --
7166 -- Obj : Access_Typ :=
14980c45 7167 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
fd68eaab 7168
7169 elsif Is_Access_Type (Obj_Typ)
7170 and then Needs_Finalization
7171 (Available_View (Designated_Type (Obj_Typ)))
7172 and then Present (Expr)
7173 and then
14980c45 7174 (Is_Secondary_Stack_BIP_Func_Call (Expr)
7175 or else
7176 (Is_Non_BIP_Func_Call (Expr)
7177 and then not Is_Related_To_Func_Return (Obj_Id)))
fd68eaab 7178 then
7179 return True;
7180
7181 -- Processing for "hook" objects generated for controlled
7182 -- transients declared inside an Expression_With_Actions.
7183
7184 elsif Is_Access_Type (Obj_Typ)
714e7f2d 7185 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7186 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
caf125ce 7187 N_Object_Declaration
fd68eaab 7188 and then Is_Finalizable_Transient
714e7f2d 7189 (Status_Flag_Or_Transient_Decl (Obj_Id), Decl)
7190 then
7191 return True;
7192
92f1631f 7193 -- Processing for intermediate results of if expressions where
7194 -- one of the alternatives uses a controlled function call.
714e7f2d 7195
7196 elsif Is_Access_Type (Obj_Typ)
7197 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7198 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7199 N_Defining_Identifier
7200 and then Present (Expr)
7201 and then Nkind (Expr) = N_Null
fd68eaab 7202 then
7203 return True;
7204
7205 -- Simple protected objects which use type System.Tasking.
7206 -- Protected_Objects.Protection to manage their locks should be
7207 -- treated as controlled since they require manual cleanup.
7208
7209 elsif Ekind (Obj_Id) = E_Variable
7210 and then
7211 (Is_Simple_Protected_Type (Obj_Typ)
7212 or else Has_Simple_Protected_Object (Obj_Typ))
7213 then
7214 return True;
7215 end if;
7216
7217 -- Specific cases of object renamings
7218
3ad9c375 7219 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
fd68eaab 7220 Obj_Id := Defining_Identifier (Decl);
7221 Obj_Typ := Base_Type (Etype (Obj_Id));
7222
7223 -- Bypass any form of processing for objects which have their
7224 -- finalization disabled. This applies only to objects at the
7225 -- library level.
7226
cae6218b 7227 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
fd68eaab 7228 null;
7229
7230 -- Return object of a build-in-place function. This case is
7231 -- recognized and marked by the expansion of an extended return
7232 -- statement (see Expand_N_Extended_Return_Statement).
7233
7234 elsif Needs_Finalization (Obj_Typ)
7235 and then Is_Return_Object (Obj_Id)
714e7f2d 7236 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
fd68eaab 7237 then
7238 return True;
3ad9c375 7239
849f127a 7240 -- Detect a case where a source object has been initialized by
7241 -- a controlled function call or another object which was later
7242 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
3ad9c375 7243
849f127a 7244 -- Obj1 : CW_Type := Src_Obj;
7245 -- Obj2 : CW_Type := Function_Call (...);
3ad9c375 7246
849f127a 7247 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7248 -- Tmp : ... := Function_Call (...)'reference;
7249 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
3ad9c375 7250
849f127a 7251 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
3ad9c375 7252 return True;
fd68eaab 7253 end if;
7254
57acff55 7255 -- Inspect the freeze node of an access-to-controlled type and look
7256 -- for a delayed finalization master. This case arises when the
7257 -- freeze actions are inserted at a later time than the expansion of
7258 -- the context. Since Build_Finalizer is never called on a single
7259 -- construct twice, the master will be ultimately left out and never
7260 -- finalized. This is also needed for freeze actions of designated
7261 -- types themselves, since in some cases the finalization master is
7262 -- associated with a designated type's freeze node rather than that
7263 -- of the access type (see handling for freeze actions in
7264 -- Build_Finalization_Master).
fd68eaab 7265
7266 elsif Nkind (Decl) = N_Freeze_Entity
7267 and then Present (Actions (Decl))
7268 then
7269 Typ := Entity (Decl);
7270
568b0f6a 7271 if ((Is_Access_Type (Typ)
141d591a 7272 and then not Is_Access_Subprogram_Type (Typ)
7273 and then Needs_Finalization
7274 (Available_View (Designated_Type (Typ))))
57acff55 7275 or else
7276 (Is_Type (Typ)
7277 and then Needs_Finalization (Typ)))
568b0f6a 7278 and then Requires_Cleanup_Actions
cae6218b 7279 (Actions (Decl), Lib_Level, Nested_Constructs)
fd68eaab 7280 then
7281 return True;
7282 end if;
7283
7284 -- Nested package declarations
7285
f239f5be 7286 elsif Nested_Constructs
7287 and then Nkind (Decl) = N_Package_Declaration
7288 then
fd68eaab 7289 Pack_Id := Defining_Unit_Name (Specification (Decl));
7290
7291 if Nkind (Pack_Id) = N_Defining_Program_Unit_Name then
7292 Pack_Id := Defining_Identifier (Pack_Id);
7293 end if;
7294
7295 if Ekind (Pack_Id) /= E_Generic_Package
cae6218b 7296 and then Requires_Cleanup_Actions
7297 (Specification (Decl), Lib_Level)
fd68eaab 7298 then
7299 return True;
7300 end if;
7301
7302 -- Nested package bodies
7303
f239f5be 7304 elsif Nested_Constructs
7305 and then Nkind (Decl) = N_Package_Body
7306 then
fd68eaab 7307 Pack_Id := Corresponding_Spec (Decl);
7308
7309 if Ekind (Pack_Id) /= E_Generic_Package
cae6218b 7310 and then Requires_Cleanup_Actions (Decl, Lib_Level)
fd68eaab 7311 then
7312 return True;
7313 end if;
7314 end if;
7315
7316 Next (Decl);
7317 end loop;
7318
7319 return False;
7320 end Requires_Cleanup_Actions;
7321
ee6ba406 7322 ------------------------------------
7323 -- Safe_Unchecked_Type_Conversion --
7324 ------------------------------------
7325
dc74650f 7326 -- Note: this function knows quite a bit about the exact requirements of
7327 -- Gigi with respect to unchecked type conversions, and its code must be
7328 -- coordinated with any changes in Gigi in this area.
ee6ba406 7329
7330 -- The above requirements should be documented in Sinfo ???
7331
7332 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
7333 Otyp : Entity_Id;
7334 Ityp : Entity_Id;
7335 Oalign : Uint;
7336 Ialign : Uint;
7337 Pexp : constant Node_Id := Parent (Exp);
7338
7339 begin
7340 -- If the expression is the RHS of an assignment or object declaration
7341 -- we are always OK because there will always be a target.
7342
7343 -- Object renaming declarations, (generated for view conversions of
7344 -- actuals in inlined calls), like object declarations, provide an
7345 -- explicit type, and are safe as well.
7346
7347 if (Nkind (Pexp) = N_Assignment_Statement
7348 and then Expression (Pexp) = Exp)
7349 or else Nkind (Pexp) = N_Object_Declaration
7350 or else Nkind (Pexp) = N_Object_Renaming_Declaration
7351 then
7352 return True;
7353
dc74650f 7354 -- If the expression is the prefix of an N_Selected_Component we should
7355 -- also be OK because GCC knows to look inside the conversion except if
7356 -- the type is discriminated. We assume that we are OK anyway if the
7357 -- type is not set yet or if it is controlled since we can't afford to
7358 -- introduce a temporary in this case.
ee6ba406 7359
7360 elsif Nkind (Pexp) = N_Selected_Component
7361 and then Prefix (Pexp) = Exp
7362 then
7363 if No (Etype (Pexp)) then
7364 return True;
7365 else
7366 return
7367 not Has_Discriminants (Etype (Pexp))
7368 or else Is_Constrained (Etype (Pexp));
7369 end if;
7370 end if;
7371
dc74650f 7372 -- Set the output type, this comes from Etype if it is set, otherwise we
7373 -- take it from the subtype mark, which we assume was already fully
7374 -- analyzed.
ee6ba406 7375
7376 if Present (Etype (Exp)) then
7377 Otyp := Etype (Exp);
7378 else
7379 Otyp := Entity (Subtype_Mark (Exp));
7380 end if;
7381
7382 -- The input type always comes from the expression, and we assume
7383 -- this is indeed always analyzed, so we can simply get the Etype.
7384
7385 Ityp := Etype (Expression (Exp));
7386
7387 -- Initialize alignments to unknown so far
7388
7389 Oalign := No_Uint;
7390 Ialign := No_Uint;
7391
dc74650f 7392 -- Replace a concurrent type by its corresponding record type and each
7393 -- type by its underlying type and do the tests on those. The original
7394 -- type may be a private type whose completion is a concurrent type, so
7395 -- find the underlying type first.
ee6ba406 7396
7397 if Present (Underlying_Type (Otyp)) then
7398 Otyp := Underlying_Type (Otyp);
7399 end if;
7400
7401 if Present (Underlying_Type (Ityp)) then
7402 Ityp := Underlying_Type (Ityp);
7403 end if;
7404
7405 if Is_Concurrent_Type (Otyp) then
7406 Otyp := Corresponding_Record_Type (Otyp);
7407 end if;
7408
7409 if Is_Concurrent_Type (Ityp) then
7410 Ityp := Corresponding_Record_Type (Ityp);
7411 end if;
7412
7413 -- If the base types are the same, we know there is no problem since
7414 -- this conversion will be a noop.
7415
7416 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
7417 return True;
7418
284faf8b 7419 -- Same if this is an upwards conversion of an untagged type, and there
7420 -- are no constraints involved (could be more general???)
7421
7422 elsif Etype (Ityp) = Otyp
7423 and then not Is_Tagged_Type (Ityp)
7424 and then not Has_Discriminants (Ityp)
7425 and then No (First_Rep_Item (Base_Type (Ityp)))
7426 then
7427 return True;
7428
dc74650f 7429 -- If the expression has an access type (object or subprogram) we assume
7430 -- that the conversion is safe, because the size of the target is safe,
7431 -- even if it is a record (which might be treated as having unknown size
7432 -- at this point).
b6b96867 7433
7434 elsif Is_Access_Type (Ityp) then
7435 return True;
7436
dc74650f 7437 -- If the size of output type is known at compile time, there is never
7438 -- a problem. Note that unconstrained records are considered to be of
7439 -- known size, but we can't consider them that way here, because we are
7440 -- talking about the actual size of the object.
ee6ba406 7441
dc74650f 7442 -- We also make sure that in addition to the size being known, we do not
7443 -- have a case which might generate an embarrassingly large temp in
7444 -- stack checking mode.
ee6ba406 7445
7446 elsif Size_Known_At_Compile_Time (Otyp)
5329ca64 7447 and then
7448 (not Stack_Checking_Enabled
7449 or else not May_Generate_Large_Temp (Otyp))
ee6ba406 7450 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
7451 then
7452 return True;
7453
7454 -- If either type is tagged, then we know the alignment is OK so
7455 -- Gigi will be able to use pointer punning.
7456
7457 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
7458 return True;
7459
dc74650f 7460 -- If either type is a limited record type, we cannot do a copy, so say
7461 -- safe since there's nothing else we can do.
ee6ba406 7462
7463 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
7464 return True;
7465
7466 -- Conversions to and from packed array types are always ignored and
7467 -- hence are safe.
7468
7469 elsif Is_Packed_Array_Type (Otyp)
7470 or else Is_Packed_Array_Type (Ityp)
7471 then
7472 return True;
7473 end if;
7474
7475 -- The only other cases known to be safe is if the input type's
7476 -- alignment is known to be at least the maximum alignment for the
7477 -- target or if both alignments are known and the output type's
dc74650f 7478 -- alignment is no stricter than the input's. We can use the component
7479 -- type alignement for an array if a type is an unpacked array type.
ee6ba406 7480
7481 if Present (Alignment_Clause (Otyp)) then
7482 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
7483
7484 elsif Is_Array_Type (Otyp)
7485 and then Present (Alignment_Clause (Component_Type (Otyp)))
7486 then
7487 Oalign := Expr_Value (Expression (Alignment_Clause
7488 (Component_Type (Otyp))));
7489 end if;
7490
7491 if Present (Alignment_Clause (Ityp)) then
7492 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
7493
7494 elsif Is_Array_Type (Ityp)
7495 and then Present (Alignment_Clause (Component_Type (Ityp)))
7496 then
7497 Ialign := Expr_Value (Expression (Alignment_Clause
7498 (Component_Type (Ityp))));
7499 end if;
7500
7501 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
7502 return True;
7503
7504 elsif Ialign /= No_Uint and then Oalign /= No_Uint
7505 and then Ialign <= Oalign
7506 then
7507 return True;
7508
23e83313 7509 -- Otherwise, Gigi cannot handle this and we must make a temporary
ee6ba406 7510
7511 else
7512 return False;
7513 end if;
ee6ba406 7514 end Safe_Unchecked_Type_Conversion;
7515
014e9448 7516 ---------------------------------
7517 -- Set_Current_Value_Condition --
7518 ---------------------------------
7519
7520 -- Note: the implementation of this procedure is very closely tied to the
7521 -- implementation of Get_Current_Value_Condition. Here we set required
7522 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
7523 -- them, so they must have a consistent view.
7524
7525 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
7526
7527 procedure Set_Entity_Current_Value (N : Node_Id);
7528 -- If N is an entity reference, where the entity is of an appropriate
7529 -- kind, then set the current value of this entity to Cnode, unless
7530 -- there is already a definite value set there.
7531
7532 procedure Set_Expression_Current_Value (N : Node_Id);
7533 -- If N is of an appropriate form, sets an appropriate entry in current
7534 -- value fields of relevant entities. Multiple entities can be affected
7535 -- in the case of an AND or AND THEN.
7536
7537 ------------------------------
7538 -- Set_Entity_Current_Value --
7539 ------------------------------
7540
7541 procedure Set_Entity_Current_Value (N : Node_Id) is
7542 begin
7543 if Is_Entity_Name (N) then
7544 declare
7545 Ent : constant Entity_Id := Entity (N);
7546
7547 begin
7548 -- Don't capture if not safe to do so
7549
7550 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
7551 return;
7552 end if;
7553
dc74650f 7554 -- Here we have a case where the Current_Value field may need
7555 -- to be set. We set it if it is not already set to a compile
7556 -- time expression value.
014e9448 7557
7558 -- Note that this represents a decision that one condition
dc74650f 7559 -- blots out another previous one. That's certainly right if
7560 -- they occur at the same level. If the second one is nested,
7561 -- then the decision is neither right nor wrong (it would be
7562 -- equally OK to leave the outer one in place, or take the new
7563 -- inner one. Really we should record both, but our data
7564 -- structures are not that elaborate.
014e9448 7565
7566 if Nkind (Current_Value (Ent)) not in N_Subexpr then
7567 Set_Current_Value (Ent, Cnode);
7568 end if;
7569 end;
7570 end if;
7571 end Set_Entity_Current_Value;
7572
7573 ----------------------------------
7574 -- Set_Expression_Current_Value --
7575 ----------------------------------
7576
7577 procedure Set_Expression_Current_Value (N : Node_Id) is
7578 Cond : Node_Id;
7579
7580 begin
7581 Cond := N;
7582
7583 -- Loop to deal with (ignore for now) any NOT operators present. The
7584 -- presence of NOT operators will be handled properly when we call
7585 -- Get_Current_Value_Condition.
7586
7587 while Nkind (Cond) = N_Op_Not loop
7588 Cond := Right_Opnd (Cond);
7589 end loop;
7590
7591 -- For an AND or AND THEN, recursively process operands
7592
7593 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
7594 Set_Expression_Current_Value (Left_Opnd (Cond));
7595 Set_Expression_Current_Value (Right_Opnd (Cond));
7596 return;
7597 end if;
7598
7599 -- Check possible relational operator
7600
7601 if Nkind (Cond) in N_Op_Compare then
7602 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
7603 Set_Entity_Current_Value (Left_Opnd (Cond));
7604 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
7605 Set_Entity_Current_Value (Right_Opnd (Cond));
7606 end if;
7607
7608 -- Check possible boolean variable reference
7609
7610 else
7611 Set_Entity_Current_Value (Cond);
7612 end if;
7613 end Set_Expression_Current_Value;
7614
7615 -- Start of processing for Set_Current_Value_Condition
7616
7617 begin
7618 Set_Expression_Current_Value (Condition (Cnode));
7619 end Set_Current_Value_Condition;
7620
ee6ba406 7621 --------------------------
7622 -- Set_Elaboration_Flag --
7623 --------------------------
7624
7625 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
7626 Loc : constant Source_Ptr := Sloc (N);
9dfe12ae 7627 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
ee6ba406 7628 Asn : Node_Id;
7629
7630 begin
9dfe12ae 7631 if Present (Ent) then
ee6ba406 7632
7633 -- Nothing to do if at the compilation unit level, because in this
7634 -- case the flag is set by the binder generated elaboration routine.
7635
7636 if Nkind (Parent (N)) = N_Compilation_Unit then
7637 null;
7638
7639 -- Here we do need to generate an assignment statement
7640
7641 else
7642 Check_Restriction (No_Elaboration_Code, N);
7643 Asn :=
7644 Make_Assignment_Statement (Loc,
9dfe12ae 7645 Name => New_Occurrence_Of (Ent, Loc),
c04fff3e 7646 Expression => Make_Integer_Literal (Loc, Uint_1));
ee6ba406 7647
7648 if Nkind (Parent (N)) = N_Subunit then
7649 Insert_After (Corresponding_Stub (Parent (N)), Asn);
7650 else
7651 Insert_After (N, Asn);
7652 end if;
7653
7654 Analyze (Asn);
9dfe12ae 7655
57993a53 7656 -- Kill current value indication. This is necessary because the
7657 -- tests of this flag are inserted out of sequence and must not
7658 -- pick up bogus indications of the wrong constant value.
9dfe12ae 7659
7660 Set_Current_Value (Ent, Empty);
ee6ba406 7661 end if;
7662 end if;
7663 end Set_Elaboration_Flag;
7664
35889f1e 7665 ----------------------------
7666 -- Set_Renamed_Subprogram --
7667 ----------------------------
7668
7669 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
7670 begin
7671 -- If input node is an identifier, we can just reset it
7672
7673 if Nkind (N) = N_Identifier then
7674 Set_Chars (N, Chars (E));
7675 Set_Entity (N, E);
7676
7677 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
7678
7679 else
7680 declare
7681 CS : constant Boolean := Comes_From_Source (N);
7682 begin
55868293 7683 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
35889f1e 7684 Set_Entity (N, E);
7685 Set_Comes_From_Source (N, CS);
7686 Set_Analyzed (N, True);
7687 end;
7688 end if;
7689 end Set_Renamed_Subprogram;
7690
57993a53 7691 ----------------------------------
7692 -- Silly_Boolean_Array_Not_Test --
7693 ----------------------------------
7694
7695 -- This procedure implements an odd and silly test. We explicitly check
7696 -- for the case where the 'First of the component type is equal to the
7697 -- 'Last of this component type, and if this is the case, we make sure
7698 -- that constraint error is raised. The reason is that the NOT is bound
7699 -- to cause CE in this case, and we will not otherwise catch it.
7700
2b523281 7701 -- No such check is required for AND and OR, since for both these cases
7702 -- False op False = False, and True op True = True. For the XOR case,
7703 -- see Silly_Boolean_Array_Xor_Test.
7704
dc74650f 7705 -- Believe it or not, this was reported as a bug. Note that nearly always,
7706 -- the test will evaluate statically to False, so the code will be
7707 -- statically removed, and no extra overhead caused.
57993a53 7708
7709 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
7710 Loc : constant Source_Ptr := Sloc (N);
7711 CT : constant Entity_Id := Component_Type (T);
7712
7713 begin
2b523281 7714 -- The check we install is
7715
7716 -- constraint_error when
7717 -- component_type'first = component_type'last
7718 -- and then array_type'Length /= 0)
7719
7720 -- We need the last guard because we don't want to raise CE for empty
7721 -- arrays since no out of range values result. (Empty arrays with a
7722 -- component type of True .. True -- very useful -- even the ACATS
7723 -- does not test that marginal case!)
7724
57993a53 7725 Insert_Action (N,
7726 Make_Raise_Constraint_Error (Loc,
7727 Condition =>
2b523281 7728 Make_And_Then (Loc,
57993a53 7729 Left_Opnd =>
2b523281 7730 Make_Op_Eq (Loc,
7731 Left_Opnd =>
7732 Make_Attribute_Reference (Loc,
7733 Prefix => New_Occurrence_Of (CT, Loc),
7734 Attribute_Name => Name_First),
7735
7736 Right_Opnd =>
7737 Make_Attribute_Reference (Loc,
7738 Prefix => New_Occurrence_Of (CT, Loc),
7739 Attribute_Name => Name_Last)),
7740
7741 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
57993a53 7742 Reason => CE_Range_Check_Failed));
7743 end Silly_Boolean_Array_Not_Test;
7744
7745 ----------------------------------
7746 -- Silly_Boolean_Array_Xor_Test --
7747 ----------------------------------
7748
7749 -- This procedure implements an odd and silly test. We explicitly check
7750 -- for the XOR case where the component type is True .. True, since this
7751 -- will raise constraint error. A special check is required since CE
bfd188a4 7752 -- will not be generated otherwise (cf Expand_Packed_Not).
57993a53 7753
7754 -- No such check is required for AND and OR, since for both these cases
2b523281 7755 -- False op False = False, and True op True = True, and no check is
7756 -- required for the case of False .. False, since False xor False = False.
7757 -- See also Silly_Boolean_Array_Not_Test
57993a53 7758
7759 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
7760 Loc : constant Source_Ptr := Sloc (N);
7761 CT : constant Entity_Id := Component_Type (T);
57993a53 7762
7763 begin
bfd188a4 7764 -- The check we install is
7765
7766 -- constraint_error when
7767 -- Boolean (component_type'First)
7768 -- and then Boolean (component_type'Last)
7769 -- and then array_type'Length /= 0)
7770
7771 -- We need the last guard because we don't want to raise CE for empty
7772 -- arrays since no out of range values result (Empty arrays with a
7773 -- component type of True .. True -- very useful -- even the ACATS
7774 -- does not test that marginal case!).
7775
57993a53 7776 Insert_Action (N,
7777 Make_Raise_Constraint_Error (Loc,
7778 Condition =>
bfd188a4 7779 Make_And_Then (Loc,
57993a53 7780 Left_Opnd =>
bfd188a4 7781 Make_And_Then (Loc,
57993a53 7782 Left_Opnd =>
bfd188a4 7783 Convert_To (Standard_Boolean,
7784 Make_Attribute_Reference (Loc,
7785 Prefix => New_Occurrence_Of (CT, Loc),
7786 Attribute_Name => Name_First)),
57993a53 7787
7788 Right_Opnd =>
bfd188a4 7789 Convert_To (Standard_Boolean,
7790 Make_Attribute_Reference (Loc,
7791 Prefix => New_Occurrence_Of (CT, Loc),
7792 Attribute_Name => Name_Last))),
57993a53 7793
2b523281 7794 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
57993a53 7795 Reason => CE_Range_Check_Failed));
7796 end Silly_Boolean_Array_Xor_Test;
7797
9dfe12ae 7798 --------------------------
7799 -- Target_Has_Fixed_Ops --
7800 --------------------------
7801
7802 Integer_Sized_Small : Ureal;
dc74650f 7803 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
7804 -- called (we don't want to compute it more than once!)
9dfe12ae 7805
7806 Long_Integer_Sized_Small : Ureal;
dc74650f 7807 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
7808 -- is called (we don't want to compute it more than once)
9dfe12ae 7809
7810 First_Time_For_THFO : Boolean := True;
7811 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
7812
7813 function Target_Has_Fixed_Ops
7814 (Left_Typ : Entity_Id;
7815 Right_Typ : Entity_Id;
23e83313 7816 Result_Typ : Entity_Id) return Boolean
9dfe12ae 7817 is
7818 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
7819 -- Return True if the given type is a fixed-point type with a small
7820 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
dc74650f 7821 -- an absolute value less than 1.0. This is currently limited to
7822 -- fixed-point types that map to Integer or Long_Integer.
9dfe12ae 7823
7824 ------------------------
7825 -- Is_Fractional_Type --
7826 ------------------------
7827
7828 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
7829 begin
7830 if Esize (Typ) = Standard_Integer_Size then
7831 return Small_Value (Typ) = Integer_Sized_Small;
7832
7833 elsif Esize (Typ) = Standard_Long_Integer_Size then
7834 return Small_Value (Typ) = Long_Integer_Sized_Small;
7835
7836 else
7837 return False;
7838 end if;
7839 end Is_Fractional_Type;
7840
7841 -- Start of processing for Target_Has_Fixed_Ops
7842
7843 begin
7844 -- Return False if Fractional_Fixed_Ops_On_Target is false
7845
7846 if not Fractional_Fixed_Ops_On_Target then
7847 return False;
7848 end if;
7849
7850 -- Here the target has Fractional_Fixed_Ops, if first time, compute
7851 -- standard constants used by Is_Fractional_Type.
7852
7853 if First_Time_For_THFO then
7854 First_Time_For_THFO := False;
7855
7856 Integer_Sized_Small :=
7857 UR_From_Components
7858 (Num => Uint_1,
7859 Den => UI_From_Int (Standard_Integer_Size - 1),
7860 Rbase => 2);
7861
7862 Long_Integer_Sized_Small :=
7863 UR_From_Components
7864 (Num => Uint_1,
7865 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
7866 Rbase => 2);
7867 end if;
7868
dc74650f 7869 -- Return True if target supports fixed-by-fixed multiply/divide for
7870 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
7871 -- and result types are equivalent fractional types.
9dfe12ae 7872
7873 return Is_Fractional_Type (Base_Type (Left_Typ))
7874 and then Is_Fractional_Type (Base_Type (Right_Typ))
7875 and then Is_Fractional_Type (Base_Type (Result_Typ))
7876 and then Esize (Left_Typ) = Esize (Right_Typ)
7877 and then Esize (Left_Typ) = Esize (Result_Typ);
7878 end Target_Has_Fixed_Ops;
7879
5c61a0ff 7880 ------------------------------------------
7881 -- Type_May_Have_Bit_Aligned_Components --
7882 ------------------------------------------
7883
7884 function Type_May_Have_Bit_Aligned_Components
7885 (Typ : Entity_Id) return Boolean
7886 is
7887 begin
7888 -- Array type, check component type
7889
7890 if Is_Array_Type (Typ) then
7891 return
7892 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
7893
7894 -- Record type, check components
7895
7896 elsif Is_Record_Type (Typ) then
7897 declare
7898 E : Entity_Id;
7899
7900 begin
343d35dc 7901 E := First_Component_Or_Discriminant (Typ);
5c61a0ff 7902 while Present (E) loop
343d35dc 7903 if Component_May_Be_Bit_Aligned (E)
7904 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5c61a0ff 7905 then
343d35dc 7906 return True;
5c61a0ff 7907 end if;
7908
343d35dc 7909 Next_Component_Or_Discriminant (E);
5c61a0ff 7910 end loop;
7911
7912 return False;
7913 end;
7914
7915 -- Type other than array or record is always OK
7916
7917 else
7918 return False;
7919 end if;
7920 end Type_May_Have_Bit_Aligned_Components;
7921
ee6ba406 7922 ----------------------------
7923 -- Wrap_Cleanup_Procedure --
7924 ----------------------------
7925
7926 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
7927 Loc : constant Source_Ptr := Sloc (N);
7928 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
7929 Stmts : constant List_Id := Statements (Stseq);
7930
7931 begin
7932 if Abort_Allowed then
7933 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7934 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7935 end if;
7936 end Wrap_Cleanup_Procedure;
7937
7938end Exp_Util;