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