]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_util.adb
[Ada] Get rid of secondary stack for controlled components
[thirdparty/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-2022, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Aggr; use Exp_Aggr;
37 with Exp_Ch6; use Exp_Ch6;
38 with Exp_Ch7; use Exp_Ch7;
39 with Exp_Ch11; use Exp_Ch11;
40 with Freeze; use Freeze;
41 with Ghost; use Ghost;
42 with Inline; use Inline;
43 with Itypes; use Itypes;
44 with Lib; use Lib;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch12; use Sem_Ch12;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Type; use Sem_Type;
62 with Sem_Util; use Sem_Util;
63 with Sinfo.Utils; use Sinfo.Utils;
64 with Snames; use Snames;
65 with Stand; use Stand;
66 with Stringt; use Stringt;
67 with Tbuild; use Tbuild;
68 with Ttypes; use Ttypes;
69 with Validsw; use Validsw;
70
71 with GNAT.HTable;
72 package body Exp_Util is
73
74 ---------------------------------------------------------
75 -- Handling of inherited class-wide pre/postconditions --
76 ---------------------------------------------------------
77
78 -- Following AI12-0113, the expression for a class-wide condition is
79 -- transformed for a subprogram that inherits it, by replacing calls
80 -- to primitive operations of the original controlling type into the
81 -- corresponding overriding operations of the derived type. The following
82 -- hash table manages this mapping, and is expanded on demand whenever
83 -- such inherited expression needs to be constructed.
84
85 -- The mapping is also used to check whether an inherited operation has
86 -- a condition that depends on overridden operations. For such an
87 -- operation we must create a wrapper that is then treated as a normal
88 -- overriding. In SPARK mode such operations are illegal.
89
90 -- For a given root type there may be several type extensions with their
91 -- own overriding operations, so at various times a given operation of
92 -- the root will be mapped into different overridings. The root type is
93 -- also mapped into the current type extension to indicate that its
94 -- operations are mapped into the overriding operations of that current
95 -- type extension.
96
97 -- The contents of the map are as follows:
98
99 -- Key Value
100
101 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
102 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
103 -- Discriminant (Entity_Id) Expression (Node_Id)
104 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
105 -- Type (Entity_Id) Type (Entity_Id)
106
107 Type_Map_Size : constant := 511;
108
109 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
110 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
111
112 package Type_Map is new GNAT.HTable.Simple_HTable
113 (Header_Num => Type_Map_Header,
114 Key => Entity_Id,
115 Element => Node_Or_Entity_Id,
116 No_element => Empty,
117 Hash => Type_Map_Hash,
118 Equal => "=");
119
120 -----------------------
121 -- Local Subprograms --
122 -----------------------
123
124 function Build_Task_Array_Image
125 (Loc : Source_Ptr;
126 Id_Ref : Node_Id;
127 A_Type : Entity_Id;
128 Dyn : Boolean := False) return Node_Id;
129 -- Build function to generate the image string for a task that is an array
130 -- component, concatenating the images of each index. To avoid storage
131 -- leaks, the string is built with successive slice assignments. The flag
132 -- Dyn indicates whether this is called for the initialization procedure of
133 -- an array of tasks, or for the name of a dynamically created task that is
134 -- assigned to an indexed component.
135
136 function Build_Task_Image_Function
137 (Loc : Source_Ptr;
138 Decls : List_Id;
139 Stats : List_Id;
140 Res : Entity_Id) return Node_Id;
141 -- Common processing for Task_Array_Image and Task_Record_Image. Build
142 -- function body that computes image.
143
144 procedure Build_Task_Image_Prefix
145 (Loc : Source_Ptr;
146 Len : out Entity_Id;
147 Res : out Entity_Id;
148 Pos : out Entity_Id;
149 Prefix : Entity_Id;
150 Sum : Node_Id;
151 Decls : List_Id;
152 Stats : List_Id);
153 -- Common processing for Task_Array_Image and Task_Record_Image. Create
154 -- local variables and assign prefix of name to result string.
155
156 function Build_Task_Record_Image
157 (Loc : Source_Ptr;
158 Id_Ref : Node_Id;
159 Dyn : Boolean := False) return Node_Id;
160 -- Build function to generate the image string for a task that is a record
161 -- component. Concatenate name of variable with that of selector. The flag
162 -- Dyn indicates whether this is called for the initialization procedure of
163 -- record with task components, or for a dynamically created task that is
164 -- assigned to a selected component.
165
166 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
167 -- Force evaluation of bounds of a slice, which may be given by a range
168 -- or by a subtype indication with or without a constraint.
169
170 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
171 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
172 -- an assertion expression that should be verified at run time.
173
174 function Is_Uninitialized_Aggregate
175 (Exp : Node_Id;
176 T : Entity_Id) return Boolean;
177 -- Determine whether an array aggregate used in an object declaration
178 -- is uninitialized, when the aggregate is declared with a box and
179 -- the component type has no default value. Such an aggregate can be
180 -- optimized away to prevent the copying of uninitialized data, and
181 -- the bounds of the aggregate can be propagated directly to the
182 -- object declaration.
183
184 function Make_CW_Equivalent_Type
185 (T : Entity_Id;
186 E : Node_Id) return Entity_Id;
187 -- T is a class-wide type entity, E is the initial expression node that
188 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
189 -- returns the entity of the Equivalent type and inserts on the fly the
190 -- necessary declaration such as:
191 --
192 -- type anon is record
193 -- _parent : Root_Type (T); constrained with E discriminants (if any)
194 -- Extension : String (1 .. expr to match size of E);
195 -- end record;
196 --
197 -- This record is compatible with any object of the class of T thanks to
198 -- the first field and has the same size as E thanks to the second.
199
200 function Make_Literal_Range
201 (Loc : Source_Ptr;
202 Literal_Typ : Entity_Id) return Node_Id;
203 -- Produce a Range node whose bounds are:
204 -- Low_Bound (Literal_Type) ..
205 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
206 -- this is used for expanding declarations like X : String := "sdfgdfg";
207 --
208 -- If the index type of the target array is not integer, we generate:
209 -- Low_Bound (Literal_Type) ..
210 -- Literal_Type'Val
211 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
212 -- + (Length (Literal_Typ) -1))
213
214 function Make_Non_Empty_Check
215 (Loc : Source_Ptr;
216 N : Node_Id) return Node_Id;
217 -- Produce a boolean expression checking that the unidimensional array
218 -- node N is not empty.
219
220 function New_Class_Wide_Subtype
221 (CW_Typ : Entity_Id;
222 N : Node_Id) return Entity_Id;
223 -- Create an implicit subtype of CW_Typ attached to node N
224
225 function Requires_Cleanup_Actions
226 (L : List_Id;
227 Lib_Level : Boolean;
228 Nested_Constructs : Boolean) return Boolean;
229 -- Given a list L, determine whether it contains one of the following:
230 --
231 -- 1) controlled objects
232 -- 2) library-level tagged types
233 --
234 -- Lib_Level is True when the list comes from a construct at the library
235 -- level, and False otherwise. Nested_Constructs is True when any nested
236 -- packages declared in L must be processed, and False otherwise.
237
238 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean;
239 -- Return True if the evaluation of the given attribute is considered
240 -- side-effect free, independently of its prefix and expressions.
241
242 -------------------------------------
243 -- Activate_Atomic_Synchronization --
244 -------------------------------------
245
246 procedure Activate_Atomic_Synchronization (N : Node_Id) is
247 Msg_Node : Node_Id;
248
249 begin
250 case Nkind (Parent (N)) is
251
252 -- Check for cases of appearing in the prefix of a construct where we
253 -- don't need atomic synchronization for this kind of usage.
254
255 when
256 -- Nothing to do if we are the prefix of an attribute, since we
257 -- do not want an atomic sync operation for things like 'Size.
258
259 N_Attribute_Reference
260
261 -- The N_Reference node is like an attribute
262
263 | N_Reference
264
265 -- Nothing to do for a reference to a component (or components)
266 -- of a composite object. Only reads and updates of the object
267 -- as a whole require atomic synchronization (RM C.6 (15)).
268
269 | N_Indexed_Component
270 | N_Selected_Component
271 | N_Slice
272 =>
273 -- For all the above cases, nothing to do if we are the prefix
274
275 if Prefix (Parent (N)) = N then
276 return;
277 end if;
278
279 when others =>
280 null;
281 end case;
282
283 -- Nothing to do for the identifier in an object renaming declaration,
284 -- the renaming itself does not need atomic synchronization.
285
286 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
287 return;
288 end if;
289
290 -- Go ahead and set the flag
291
292 Set_Atomic_Sync_Required (N);
293
294 -- Generate info message if requested
295
296 if Warn_On_Atomic_Synchronization then
297 case Nkind (N) is
298 when N_Identifier =>
299 Msg_Node := N;
300
301 when N_Expanded_Name
302 | N_Selected_Component
303 =>
304 Msg_Node := Selector_Name (N);
305
306 when N_Explicit_Dereference
307 | N_Indexed_Component
308 =>
309 Msg_Node := Empty;
310
311 when others =>
312 pragma Assert (False);
313 return;
314 end case;
315
316 if Present (Msg_Node) then
317 Error_Msg_N
318 ("info: atomic synchronization set for &?.n?", Msg_Node);
319 else
320 Error_Msg_N
321 ("info: atomic synchronization set?.n?", N);
322 end if;
323 end if;
324 end Activate_Atomic_Synchronization;
325
326 ----------------------
327 -- Adjust_Condition --
328 ----------------------
329
330 procedure Adjust_Condition (N : Node_Id) is
331
332 function Is_Hardbool_Type (T : Entity_Id) return Boolean;
333 -- Return True iff T is a type annotated with the
334 -- Machine_Attribute pragma "hardbool".
335
336 ----------------------
337 -- Is_Hardbool_Type --
338 ----------------------
339
340 function Is_Hardbool_Type (T : Entity_Id) return Boolean is
341
342 function Find_Hardbool_Pragma
343 (Id : Entity_Id) return Node_Id;
344 -- Return a Rep_Item associated with entity Id that
345 -- corresponds to the Hardbool Machine_Attribute pragma, if
346 -- any, or Empty otherwise.
347
348 function Pragma_Arg_To_String (Item : Node_Id) return String is
349 (To_String (Strval (Expr_Value_S (Item))));
350 -- Return the pragma argument Item as a String
351
352 function Hardbool_Pragma_P (Item : Node_Id) return Boolean is
353 (Nkind (Item) = N_Pragma
354 and then
355 Pragma_Name (Item) = Name_Machine_Attribute
356 and then
357 Pragma_Arg_To_String
358 (Get_Pragma_Arg
359 (Next (First (Pragma_Argument_Associations (Item)))))
360 = "hardbool");
361 -- Return True iff representation Item is a "hardbool"
362 -- Machine_Attribute pragma.
363
364 --------------------------
365 -- Find_Hardbool_Pragma --
366 --------------------------
367
368 function Find_Hardbool_Pragma
369 (Id : Entity_Id) return Node_Id
370 is
371 Item : Node_Id;
372
373 begin
374 if not Has_Gigi_Rep_Item (Id) then
375 return Empty;
376 end if;
377
378 Item := First_Rep_Item (Id);
379 while Present (Item) loop
380 if Hardbool_Pragma_P (Item) then
381 return Item;
382 end if;
383 Item := Next_Rep_Item (Item);
384 end loop;
385
386 return Empty;
387 end Find_Hardbool_Pragma;
388
389 -- Start of processing for Is_Hardbool_Type
390
391 begin
392 return Present (Find_Hardbool_Pragma (T));
393 end Is_Hardbool_Type;
394
395 -- Start of processing for Adjust_Condition
396
397 begin
398 if No (N) then
399 return;
400 end if;
401
402 declare
403 Loc : constant Source_Ptr := Sloc (N);
404 T : constant Entity_Id := Etype (N);
405
406 begin
407 -- Defend against a call where the argument has no type, or has a
408 -- type that is not Boolean. This can occur because of prior errors.
409
410 if No (T) or else not Is_Boolean_Type (T) then
411 return;
412 end if;
413
414 -- Apply validity checking if needed
415
416 if Validity_Checks_On
417 and then
418 (Validity_Check_Tests or else Is_Hardbool_Type (T))
419 then
420 Ensure_Valid (N);
421 end if;
422
423 -- Immediate return if standard boolean, the most common case,
424 -- where nothing needs to be done.
425
426 if Base_Type (T) = Standard_Boolean then
427 return;
428 end if;
429
430 -- Case of zero/nonzero semantics or nonstandard enumeration
431 -- representation. In each case, we rewrite the node as:
432
433 -- ityp!(N) /= False'Enum_Rep
434
435 -- where ityp is an integer type with large enough size to hold any
436 -- value of type T.
437
438 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
439 Rewrite (N,
440 Make_Op_Ne (Loc,
441 Left_Opnd =>
442 Unchecked_Convert_To
443 (Integer_Type_For (Esize (T), Uns => False), N),
444 Right_Opnd =>
445 Make_Attribute_Reference (Loc,
446 Attribute_Name => Name_Enum_Rep,
447 Prefix =>
448 New_Occurrence_Of (First_Literal (T), Loc))));
449 Analyze_And_Resolve (N, Standard_Boolean);
450
451 else
452 Rewrite (N, Convert_To (Standard_Boolean, N));
453 Analyze_And_Resolve (N, Standard_Boolean);
454 end if;
455 end;
456 end Adjust_Condition;
457
458 ------------------------
459 -- Adjust_Result_Type --
460 ------------------------
461
462 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
463 begin
464 -- Ignore call if current type is not Standard.Boolean
465
466 if Etype (N) /= Standard_Boolean then
467 return;
468 end if;
469
470 -- If result is already of correct type, nothing to do. Note that
471 -- this will get the most common case where everything has a type
472 -- of Standard.Boolean.
473
474 if Base_Type (T) = Standard_Boolean then
475 return;
476
477 else
478 declare
479 KP : constant Node_Kind := Nkind (Parent (N));
480
481 begin
482 -- If result is to be used as a Condition in the syntax, no need
483 -- to convert it back, since if it was changed to Standard.Boolean
484 -- using Adjust_Condition, that is just fine for this usage.
485
486 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
487 return;
488
489 -- If result is an operand of another logical operation, no need
490 -- to reset its type, since Standard.Boolean is just fine, and
491 -- such operations always do Adjust_Condition on their operands.
492
493 elsif KP in N_Op_Boolean
494 or else KP in N_Short_Circuit
495 or else KP = N_Op_Not
496 or else (KP in N_Type_Conversion
497 | N_Unchecked_Type_Conversion
498 and then Is_Boolean_Type (Etype (Parent (N))))
499 then
500 return;
501
502 -- Otherwise we perform a conversion from the current type, which
503 -- must be Standard.Boolean, to the desired type. Use the base
504 -- type to prevent spurious constraint checks that are extraneous
505 -- to the transformation. The type and its base have the same
506 -- representation, standard or otherwise.
507
508 else
509 Set_Analyzed (N);
510 Rewrite (N, Convert_To (Base_Type (T), N));
511 Analyze_And_Resolve (N, Base_Type (T));
512 end if;
513 end;
514 end if;
515 end Adjust_Result_Type;
516
517 --------------------------
518 -- Append_Freeze_Action --
519 --------------------------
520
521 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
522 Fnode : Node_Id;
523
524 begin
525 Ensure_Freeze_Node (T);
526 Fnode := Freeze_Node (T);
527
528 if No (Actions (Fnode)) then
529 Set_Actions (Fnode, New_List (N));
530 else
531 Append (N, Actions (Fnode));
532 end if;
533 end Append_Freeze_Action;
534
535 ---------------------------
536 -- Append_Freeze_Actions --
537 ---------------------------
538
539 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
540 Fnode : Node_Id;
541
542 begin
543 if No (L) then
544 return;
545 end if;
546
547 Ensure_Freeze_Node (T);
548 Fnode := Freeze_Node (T);
549
550 if No (Actions (Fnode)) then
551 Set_Actions (Fnode, L);
552 else
553 Append_List (L, Actions (Fnode));
554 end if;
555 end Append_Freeze_Actions;
556
557 ----------------------------------------
558 -- Attribute_Constrained_Static_Value --
559 ----------------------------------------
560
561 function Attribute_Constrained_Static_Value (Pref : Node_Id) return Boolean
562 is
563 Ptyp : constant Entity_Id := Etype (Pref);
564 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
565
566 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
567 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
568 -- view of an aliased object whose subtype is constrained.
569
570 ---------------------------------
571 -- Is_Constrained_Aliased_View --
572 ---------------------------------
573
574 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
575 E : Entity_Id;
576
577 begin
578 if Is_Entity_Name (Obj) then
579 E := Entity (Obj);
580
581 if Present (Renamed_Object (E)) then
582 return Is_Constrained_Aliased_View (Renamed_Object (E));
583 else
584 return Is_Aliased (E) and then Is_Constrained (Etype (E));
585 end if;
586
587 else
588 return Is_Aliased_View (Obj)
589 and then
590 (Is_Constrained (Etype (Obj))
591 or else
592 (Nkind (Obj) = N_Explicit_Dereference
593 and then
594 not Object_Type_Has_Constrained_Partial_View
595 (Typ => Base_Type (Etype (Obj)),
596 Scop => Current_Scope)));
597 end if;
598 end Is_Constrained_Aliased_View;
599
600 -- Start of processing for Attribute_Constrained_Static_Value
601
602 begin
603 -- We are in a case where the attribute is known statically, and
604 -- implicit dereferences have been rewritten.
605
606 pragma Assert
607 (not (Present (Formal_Ent)
608 and then Ekind (Formal_Ent) /= E_Constant
609 and then Present (Extra_Constrained (Formal_Ent)))
610 and then
611 not (Is_Access_Type (Etype (Pref))
612 and then (not Is_Entity_Name (Pref)
613 or else Is_Object (Entity (Pref))))
614 and then
615 not (Nkind (Pref) = N_Identifier
616 and then Ekind (Entity (Pref)) = E_Variable
617 and then Present (Extra_Constrained (Entity (Pref)))));
618
619 if Is_Entity_Name (Pref) then
620 declare
621 Ent : constant Entity_Id := Entity (Pref);
622 Res : Boolean;
623
624 begin
625 -- (RM J.4) obsolescent cases
626
627 if Is_Type (Ent) then
628
629 -- Private type
630
631 if Is_Private_Type (Ent) then
632 Res := not Has_Discriminants (Ent)
633 or else Is_Constrained (Ent);
634
635 -- It not a private type, must be a generic actual type
636 -- that corresponded to a private type. We know that this
637 -- correspondence holds, since otherwise the reference
638 -- within the generic template would have been illegal.
639
640 else
641 if Is_Composite_Type (Underlying_Type (Ent)) then
642 Res := Is_Constrained (Ent);
643 else
644 Res := True;
645 end if;
646 end if;
647
648 else
649
650 -- If the prefix is not a variable or is aliased, then
651 -- definitely true; if it's a formal parameter without an
652 -- associated extra formal, then treat it as constrained.
653
654 -- Ada 2005 (AI-363): An aliased prefix must be known to be
655 -- constrained in order to set the attribute to True.
656
657 if not Is_Variable (Pref)
658 or else Present (Formal_Ent)
659 or else (Ada_Version < Ada_2005
660 and then Is_Aliased_View (Pref))
661 or else (Ada_Version >= Ada_2005
662 and then Is_Constrained_Aliased_View (Pref))
663 then
664 Res := True;
665
666 -- Variable case, look at type to see if it is constrained.
667 -- Note that the one case where this is not accurate (the
668 -- procedure formal case), has been handled above.
669
670 -- We use the Underlying_Type here (and below) in case the
671 -- type is private without discriminants, but the full type
672 -- has discriminants. This case is illegal, but we generate
673 -- it internally for passing to the Extra_Constrained
674 -- parameter.
675
676 else
677 -- In Ada 2012, test for case of a limited tagged type,
678 -- in which case the attribute is always required to
679 -- return True. The underlying type is tested, to make
680 -- sure we also return True for cases where there is an
681 -- unconstrained object with an untagged limited partial
682 -- view which has defaulted discriminants (such objects
683 -- always produce a False in earlier versions of
684 -- Ada). (Ada 2012: AI05-0214)
685
686 Res :=
687 Is_Constrained (Underlying_Type (Etype (Ent)))
688 or else
689 (Ada_Version >= Ada_2012
690 and then Is_Tagged_Type (Underlying_Type (Ptyp))
691 and then Is_Limited_Type (Ptyp));
692 end if;
693 end if;
694
695 return Res;
696 end;
697
698 -- Prefix is not an entity name. These are also cases where we can
699 -- always tell at compile time by looking at the form and type of the
700 -- prefix. If an explicit dereference of an object with constrained
701 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
702 -- underlying type is a limited tagged type, then Constrained is
703 -- required to always return True (Ada 2012: AI05-0214).
704
705 else
706 return not Is_Variable (Pref)
707 or else
708 (Nkind (Pref) = N_Explicit_Dereference
709 and then
710 not Object_Type_Has_Constrained_Partial_View
711 (Typ => Base_Type (Ptyp),
712 Scop => Current_Scope))
713 or else Is_Constrained (Underlying_Type (Ptyp))
714 or else (Ada_Version >= Ada_2012
715 and then Is_Tagged_Type (Underlying_Type (Ptyp))
716 and then Is_Limited_Type (Ptyp));
717 end if;
718 end Attribute_Constrained_Static_Value;
719
720 ------------------------------------
721 -- Build_Allocate_Deallocate_Proc --
722 ------------------------------------
723
724 procedure Build_Allocate_Deallocate_Proc
725 (N : Node_Id;
726 Is_Allocate : Boolean)
727 is
728 function Find_Object (E : Node_Id) return Node_Id;
729 -- Given an arbitrary expression of an allocator, try to find an object
730 -- reference in it, otherwise return the original expression.
731
732 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
733 -- Determine whether subprogram Subp denotes a custom allocate or
734 -- deallocate.
735
736 -----------------
737 -- Find_Object --
738 -----------------
739
740 function Find_Object (E : Node_Id) return Node_Id is
741 Expr : Node_Id;
742
743 begin
744 pragma Assert (Is_Allocate);
745
746 Expr := E;
747 loop
748 if Nkind (Expr) = N_Explicit_Dereference then
749 Expr := Prefix (Expr);
750
751 elsif Nkind (Expr) = N_Qualified_Expression then
752 Expr := Expression (Expr);
753
754 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
755
756 -- When interface class-wide types are involved in allocation,
757 -- the expander introduces several levels of address arithmetic
758 -- to perform dispatch table displacement. In this scenario the
759 -- object appears as:
760
761 -- Tag_Ptr (Base_Address (<object>'Address))
762
763 -- Detect this case and utilize the whole expression as the
764 -- "object" since it now points to the proper dispatch table.
765
766 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
767 exit;
768
769 -- Continue to strip the object
770
771 else
772 Expr := Expression (Expr);
773 end if;
774
775 else
776 exit;
777 end if;
778 end loop;
779
780 return Expr;
781 end Find_Object;
782
783 ---------------------------------
784 -- Is_Allocate_Deallocate_Proc --
785 ---------------------------------
786
787 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
788 begin
789 -- Look for a subprogram body with only one statement which is a
790 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
791
792 if Ekind (Subp) = E_Procedure
793 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
794 then
795 declare
796 HSS : constant Node_Id :=
797 Handled_Statement_Sequence (Parent (Parent (Subp)));
798 Proc : Entity_Id;
799
800 begin
801 if Present (Statements (HSS))
802 and then Nkind (First (Statements (HSS))) =
803 N_Procedure_Call_Statement
804 then
805 Proc := Entity (Name (First (Statements (HSS))));
806
807 return
808 Is_RTE (Proc, RE_Allocate_Any_Controlled)
809 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
810 end if;
811 end;
812 end if;
813
814 return False;
815 end Is_Allocate_Deallocate_Proc;
816
817 -- Local variables
818
819 Desig_Typ : Entity_Id;
820 Expr : Node_Id;
821 Needs_Fin : Boolean;
822 Pool_Id : Entity_Id;
823 Proc_To_Call : Node_Id := Empty;
824 Ptr_Typ : Entity_Id;
825 Use_Secondary_Stack_Pool : Boolean;
826
827 -- Start of processing for Build_Allocate_Deallocate_Proc
828
829 begin
830 -- Obtain the attributes of the allocation / deallocation
831
832 if Nkind (N) = N_Free_Statement then
833 Expr := Expression (N);
834 Ptr_Typ := Base_Type (Etype (Expr));
835 Proc_To_Call := Procedure_To_Call (N);
836
837 else
838 if Nkind (N) = N_Object_Declaration then
839 Expr := Expression (N);
840 else
841 Expr := N;
842 end if;
843
844 -- In certain cases an allocator with a qualified expression may
845 -- be relocated and used as the initialization expression of a
846 -- temporary:
847
848 -- before:
849 -- Obj : Ptr_Typ := new Desig_Typ'(...);
850
851 -- after:
852 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
853 -- Obj : Ptr_Typ := Tmp;
854
855 -- Since the allocator is always marked as analyzed to avoid infinite
856 -- expansion, it will never be processed by this routine given that
857 -- the designated type needs finalization actions. Detect this case
858 -- and complete the expansion of the allocator.
859
860 if Nkind (Expr) = N_Identifier
861 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
862 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
863 then
864 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
865 return;
866 end if;
867
868 -- The allocator may have been rewritten into something else in which
869 -- case the expansion performed by this routine does not apply.
870
871 if Nkind (Expr) /= N_Allocator then
872 return;
873 end if;
874
875 Ptr_Typ := Base_Type (Etype (Expr));
876 Proc_To_Call := Procedure_To_Call (Expr);
877 end if;
878
879 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
880 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
881
882 -- Handle concurrent types
883
884 if Is_Concurrent_Type (Desig_Typ)
885 and then Present (Corresponding_Record_Type (Desig_Typ))
886 then
887 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
888 end if;
889
890 Use_Secondary_Stack_Pool :=
891 Is_RTE (Pool_Id, RE_SS_Pool)
892 or else (Nkind (Expr) = N_Allocator
893 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool));
894
895 -- Do not process allocations / deallocations without a pool
896
897 if No (Pool_Id) then
898 return;
899
900 -- Do not process allocations from the return stack
901
902 elsif Is_RTE (Pool_Id, RE_RS_Pool) then
903 return;
904
905 -- Do not process allocations on / deallocations from the secondary
906 -- stack, except for access types used to implement indirect temps.
907
908 elsif Use_Secondary_Stack_Pool
909 and then not Old_Attr_Util.Indirect_Temps
910 .Is_Access_Type_For_Indirect_Temp (Ptr_Typ)
911 then
912 return;
913
914 -- Optimize the case where we are using the default Global_Pool_Object,
915 -- and we don't need the heavy finalization machinery.
916
917 elsif Is_RTE (Pool_Id, RE_Global_Pool_Object)
918 and then not Needs_Finalization (Desig_Typ)
919 then
920 return;
921
922 -- Do not replicate the machinery if the allocator / free has already
923 -- been expanded and has a custom Allocate / Deallocate.
924
925 elsif Present (Proc_To_Call)
926 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
927 then
928 return;
929 end if;
930
931 -- Finalization actions are required when the object to be allocated or
932 -- deallocated needs these actions and the associated access type is not
933 -- subject to pragma No_Heap_Finalization.
934
935 Needs_Fin :=
936 Needs_Finalization (Desig_Typ)
937 and then not No_Heap_Finalization (Ptr_Typ);
938
939 if Needs_Fin then
940
941 -- Do nothing if the access type may never allocate / deallocate
942 -- objects.
943
944 if No_Pool_Assigned (Ptr_Typ) then
945 return;
946 end if;
947
948 -- The allocation / deallocation of a controlled object must be
949 -- chained on / detached from a finalization master.
950
951 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
952
953 -- The only other kind of allocation / deallocation supported by this
954 -- routine is on / from a subpool.
955
956 elsif Nkind (Expr) = N_Allocator
957 and then No (Subpool_Handle_Name (Expr))
958 then
959 return;
960 end if;
961
962 declare
963 Loc : constant Source_Ptr := Sloc (N);
964 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
965 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
966 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
967 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
968
969 Actuals : List_Id;
970 Alloc_Nod : Node_Id := Empty;
971 Alloc_Expr : Node_Id := Empty;
972 Fin_Addr_Id : Entity_Id;
973 Fin_Mas_Act : Node_Id;
974 Fin_Mas_Id : Entity_Id;
975 Proc_To_Call : Entity_Id;
976 Subpool : Node_Id := Empty;
977
978 begin
979 -- When we are building an allocator procedure, extract the allocator
980 -- node for later processing and calculation of alignment.
981
982 if Is_Allocate then
983
984 if Nkind (Expr) = N_Allocator then
985 Alloc_Nod := Expr;
986
987 -- When Expr is an object declaration we have to examine its
988 -- expression.
989
990 elsif Nkind (Expr) = N_Object_Declaration
991 and then Nkind (Expression (Expr)) = N_Allocator
992 then
993 Alloc_Nod := Expression (Expr);
994
995 -- Otherwise, we raise an error because we should have found one
996
997 else
998 raise Program_Error;
999 end if;
1000
1001 -- Extract the qualified expression if there is one from the
1002 -- allocator.
1003
1004 if Nkind (Expression (Alloc_Nod)) = N_Qualified_Expression then
1005 Alloc_Expr := Expression (Alloc_Nod);
1006 end if;
1007 end if;
1008
1009 -- Step 1: Construct all the actuals for the call to library routine
1010 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
1011
1012 -- a) Storage pool
1013
1014 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
1015
1016 if Is_Allocate then
1017
1018 -- b) Subpool
1019
1020 if Nkind (Expr) = N_Allocator then
1021 Subpool := Subpool_Handle_Name (Expr);
1022 end if;
1023
1024 -- If a subpool is present it can be an arbitrary name, so make
1025 -- the actual by copying the tree.
1026
1027 if Present (Subpool) then
1028 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
1029 else
1030 Append_To (Actuals, Make_Null (Loc));
1031 end if;
1032
1033 -- c) Finalization master
1034
1035 if Needs_Fin then
1036 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
1037 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
1038
1039 -- Handle the case where the master is actually a pointer to a
1040 -- master. This case arises in build-in-place functions.
1041
1042 if Is_Access_Type (Etype (Fin_Mas_Id)) then
1043 Append_To (Actuals, Fin_Mas_Act);
1044 else
1045 Append_To (Actuals,
1046 Make_Attribute_Reference (Loc,
1047 Prefix => Fin_Mas_Act,
1048 Attribute_Name => Name_Unrestricted_Access));
1049 end if;
1050 else
1051 Append_To (Actuals, Make_Null (Loc));
1052 end if;
1053
1054 -- d) Finalize_Address
1055
1056 -- Primitive Finalize_Address is never generated in CodePeer mode
1057 -- since it contains an Unchecked_Conversion.
1058
1059 if Needs_Fin and then not CodePeer_Mode then
1060 Fin_Addr_Id := Finalize_Address (Desig_Typ);
1061 pragma Assert (Present (Fin_Addr_Id));
1062
1063 Append_To (Actuals,
1064 Make_Attribute_Reference (Loc,
1065 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
1066 Attribute_Name => Name_Unrestricted_Access));
1067 else
1068 Append_To (Actuals, Make_Null (Loc));
1069 end if;
1070 end if;
1071
1072 -- e) Address
1073 -- f) Storage_Size
1074 -- g) Alignment
1075
1076 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
1077 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
1078
1079 -- Class-wide allocations without expressions and non-class-wide
1080 -- allocations can be performed without getting the alignment from
1081 -- the type's Type Specific Record.
1082
1083 if ((Is_Allocate and then No (Alloc_Expr))
1084 or else
1085 not Is_Class_Wide_Type (Desig_Typ))
1086 and then not Use_Secondary_Stack_Pool
1087 then
1088 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
1089
1090 -- For operations on class-wide types we obtain the value of
1091 -- alignment from the Type Specific Record of the relevant object.
1092 -- This is needed because the frontend expansion of class-wide types
1093 -- into equivalent types confuses the back end.
1094
1095 else
1096 -- Generate:
1097 -- Obj.all'Alignment
1098 -- or
1099 -- Alloc_Expr'Alignment
1100
1101 -- ... because 'Alignment applied to class-wide types is expanded
1102 -- into the code that reads the value of alignment from the TSD
1103 -- (see Expand_N_Attribute_Reference)
1104
1105 -- In the Use_Secondary_Stack_Pool case, Alig_Id is not
1106 -- passed in and therefore must not be referenced.
1107
1108 Append_To (Actuals,
1109 Unchecked_Convert_To (RTE (RE_Storage_Offset),
1110 Make_Attribute_Reference (Loc,
1111 Prefix =>
1112 (if No (Alloc_Expr) then
1113 Make_Explicit_Dereference (Loc, Relocate_Node (Expr))
1114 else
1115 Relocate_Node (Expression (Alloc_Expr))),
1116 Attribute_Name => Name_Alignment)));
1117 end if;
1118
1119 -- h) Is_Controlled
1120
1121 if Needs_Fin then
1122 Is_Controlled : declare
1123 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
1124 Flag_Expr : Node_Id;
1125 Param : Node_Id;
1126 Pref : Node_Id;
1127 Temp : Node_Id;
1128
1129 begin
1130 if Is_Allocate then
1131 Temp := Find_Object (Expression (Expr));
1132 else
1133 Temp := Expr;
1134 end if;
1135
1136 -- Processing for allocations where the expression is a subtype
1137 -- indication.
1138
1139 if Is_Allocate
1140 and then Is_Entity_Name (Temp)
1141 and then Is_Type (Entity (Temp))
1142 then
1143 Flag_Expr :=
1144 New_Occurrence_Of
1145 (Boolean_Literals
1146 (Needs_Finalization (Entity (Temp))), Loc);
1147
1148 -- The allocation / deallocation of a class-wide object relies
1149 -- on a runtime check to determine whether the object is truly
1150 -- controlled or not. Depending on this check, the finalization
1151 -- machinery will request or reclaim extra storage reserved for
1152 -- a list header.
1153
1154 elsif Is_Class_Wide_Type (Desig_Typ) then
1155
1156 -- Detect a special case where interface class-wide types
1157 -- are involved as the object appears as:
1158
1159 -- Tag_Ptr (Base_Address (<object>'Address))
1160
1161 -- The expression already yields the proper tag, generate:
1162
1163 -- Temp.all
1164
1165 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
1166 Param :=
1167 Make_Explicit_Dereference (Loc,
1168 Prefix => Relocate_Node (Temp));
1169
1170 -- In the default case, obtain the tag of the object about
1171 -- to be allocated / deallocated. Generate:
1172
1173 -- Temp'Tag
1174
1175 -- If the object is an unchecked conversion (typically to
1176 -- an access to class-wide type), we must preserve the
1177 -- conversion to ensure that the object is seen as tagged
1178 -- in the code that follows.
1179
1180 else
1181 Pref := Temp;
1182
1183 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
1184 then
1185 Pref := Parent (Pref);
1186 end if;
1187
1188 Param :=
1189 Make_Attribute_Reference (Loc,
1190 Prefix => Relocate_Node (Pref),
1191 Attribute_Name => Name_Tag);
1192 end if;
1193
1194 -- Generate:
1195 -- Needs_Finalization (<Param>)
1196
1197 Flag_Expr :=
1198 Make_Function_Call (Loc,
1199 Name =>
1200 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
1201 Parameter_Associations => New_List (Param));
1202
1203 -- Processing for generic actuals
1204
1205 elsif Is_Generic_Actual_Type (Desig_Typ) then
1206 Flag_Expr :=
1207 New_Occurrence_Of (Boolean_Literals
1208 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
1209
1210 -- The object does not require any specialized checks, it is
1211 -- known to be controlled.
1212
1213 else
1214 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
1215 end if;
1216
1217 -- Create the temporary which represents the finalization state
1218 -- of the expression. Generate:
1219 --
1220 -- F : constant Boolean := <Flag_Expr>;
1221
1222 Insert_Action (N,
1223 Make_Object_Declaration (Loc,
1224 Defining_Identifier => Flag_Id,
1225 Constant_Present => True,
1226 Object_Definition =>
1227 New_Occurrence_Of (Standard_Boolean, Loc),
1228 Expression => Flag_Expr));
1229
1230 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
1231 end Is_Controlled;
1232
1233 -- The object is not controlled
1234
1235 else
1236 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
1237 end if;
1238
1239 -- i) On_Subpool
1240
1241 if Is_Allocate then
1242 Append_To (Actuals,
1243 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
1244 end if;
1245
1246 -- Step 2: Build a wrapper Allocate / Deallocate which internally
1247 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
1248
1249 -- Select the proper routine to call
1250
1251 if Is_Allocate then
1252 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
1253 else
1254 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
1255 end if;
1256
1257 -- Create a custom Allocate / Deallocate routine which has identical
1258 -- profile to that of System.Storage_Pools.
1259
1260 declare
1261 -- P : Root_Storage_Pool
1262 function Pool_Param return Node_Id is (
1263 Make_Parameter_Specification (Loc,
1264 Defining_Identifier => Make_Temporary (Loc, 'P'),
1265 Parameter_Type =>
1266 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)));
1267
1268 -- A : [out] Address
1269 function Address_Param return Node_Id is (
1270 Make_Parameter_Specification (Loc,
1271 Defining_Identifier => Addr_Id,
1272 Out_Present => Is_Allocate,
1273 Parameter_Type =>
1274 New_Occurrence_Of (RTE (RE_Address), Loc)));
1275
1276 -- S : Storage_Count
1277 function Size_Param return Node_Id is (
1278 Make_Parameter_Specification (Loc,
1279 Defining_Identifier => Size_Id,
1280 Parameter_Type =>
1281 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1282
1283 -- L : Storage_Count
1284 function Alignment_Param return Node_Id is (
1285 Make_Parameter_Specification (Loc,
1286 Defining_Identifier => Alig_Id,
1287 Parameter_Type =>
1288 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1289
1290 Formal_Params : List_Id;
1291 begin
1292 if Use_Secondary_Stack_Pool then
1293 -- Gigi expects a different profile in the Secondary_Stack_Pool
1294 -- case. There must be no uses of the two missing formals
1295 -- (i.e., Pool_Param and Alignment_Param) in this case.
1296 Formal_Params := New_List (Address_Param, Size_Param);
1297 else
1298 Formal_Params := New_List (
1299 Pool_Param, Address_Param, Size_Param, Alignment_Param);
1300 end if;
1301
1302 Insert_Action (N,
1303 Make_Subprogram_Body (Loc,
1304 Specification =>
1305 -- procedure Pnn
1306 Make_Procedure_Specification (Loc,
1307 Defining_Unit_Name => Proc_Id,
1308 Parameter_Specifications => Formal_Params),
1309
1310 Declarations => No_List,
1311
1312 Handled_Statement_Sequence =>
1313 Make_Handled_Sequence_Of_Statements (Loc,
1314 Statements => New_List (
1315 Make_Procedure_Call_Statement (Loc,
1316 Name =>
1317 New_Occurrence_Of (Proc_To_Call, Loc),
1318 Parameter_Associations => Actuals)))),
1319 Suppress => All_Checks);
1320 end;
1321
1322 -- The newly generated Allocate / Deallocate becomes the default
1323 -- procedure to call when the back end processes the allocation /
1324 -- deallocation.
1325
1326 if Is_Allocate then
1327 Set_Procedure_To_Call (Expr, Proc_Id);
1328 else
1329 Set_Procedure_To_Call (N, Proc_Id);
1330 end if;
1331 end;
1332 end Build_Allocate_Deallocate_Proc;
1333
1334 -------------------------------
1335 -- Build_Abort_Undefer_Block --
1336 -------------------------------
1337
1338 function Build_Abort_Undefer_Block
1339 (Loc : Source_Ptr;
1340 Stmts : List_Id;
1341 Context : Node_Id) return Node_Id
1342 is
1343 Exceptions_OK : constant Boolean :=
1344 not Restriction_Active (No_Exception_Propagation);
1345
1346 AUD : Entity_Id;
1347 Blk : Node_Id;
1348 Blk_Id : Entity_Id;
1349 HSS : Node_Id;
1350
1351 begin
1352 -- The block should be generated only when undeferring abort in the
1353 -- context of a potential exception.
1354
1355 pragma Assert (Abort_Allowed and Exceptions_OK);
1356
1357 -- Generate:
1358 -- begin
1359 -- <Stmts>
1360 -- at end
1361 -- Abort_Undefer_Direct;
1362 -- end;
1363
1364 AUD := RTE (RE_Abort_Undefer_Direct);
1365
1366 HSS :=
1367 Make_Handled_Sequence_Of_Statements (Loc,
1368 Statements => Stmts,
1369 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1370
1371 Blk :=
1372 Make_Block_Statement (Loc,
1373 Handled_Statement_Sequence => HSS);
1374 Set_Is_Abort_Block (Blk);
1375
1376 Add_Block_Identifier (Blk, Blk_Id);
1377 Expand_At_End_Handler (HSS, Blk_Id);
1378
1379 -- Present the Abort_Undefer_Direct function to the back end to inline
1380 -- the call to the routine.
1381
1382 Add_Inlined_Body (AUD, Context);
1383
1384 return Blk;
1385 end Build_Abort_Undefer_Block;
1386
1387 ---------------------------------
1388 -- Build_Class_Wide_Expression --
1389 ---------------------------------
1390
1391 procedure Build_Class_Wide_Expression
1392 (Pragma_Or_Expr : Node_Id;
1393 Subp : Entity_Id;
1394 Par_Subp : Entity_Id;
1395 Adjust_Sloc : Boolean)
1396 is
1397 function Replace_Entity (N : Node_Id) return Traverse_Result;
1398 -- Replace reference to formal of inherited operation or to primitive
1399 -- operation of root type, with corresponding entity for derived type,
1400 -- when constructing the class-wide condition of an overriding
1401 -- subprogram.
1402
1403 --------------------
1404 -- Replace_Entity --
1405 --------------------
1406
1407 function Replace_Entity (N : Node_Id) return Traverse_Result is
1408 New_E : Entity_Id;
1409
1410 begin
1411 if Adjust_Sloc then
1412 Adjust_Inherited_Pragma_Sloc (N);
1413 end if;
1414
1415 if Nkind (N) in N_Identifier | N_Expanded_Name | N_Operator_Symbol
1416 and then Present (Entity (N))
1417 and then
1418 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1419 and then
1420 (Nkind (Parent (N)) /= N_Attribute_Reference
1421 or else Attribute_Name (Parent (N)) /= Name_Class)
1422 then
1423 -- The replacement does not apply to dispatching calls within the
1424 -- condition, but only to calls whose static tag is that of the
1425 -- parent type.
1426
1427 if Is_Subprogram (Entity (N))
1428 and then Nkind (Parent (N)) = N_Function_Call
1429 and then Present (Controlling_Argument (Parent (N)))
1430 then
1431 return OK;
1432 end if;
1433
1434 -- Determine whether entity has a renaming
1435
1436 New_E := Type_Map.Get (Entity (N));
1437
1438 if Present (New_E) then
1439 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1440 end if;
1441
1442 -- Update type of function call node, which should be the same as
1443 -- the function's return type.
1444
1445 if Is_Subprogram (Entity (N))
1446 and then Nkind (Parent (N)) = N_Function_Call
1447 then
1448 Set_Etype (Parent (N), Etype (Entity (N)));
1449 end if;
1450
1451 -- The whole expression will be reanalyzed
1452
1453 elsif Nkind (N) in N_Has_Etype then
1454 Set_Analyzed (N, False);
1455 end if;
1456
1457 return OK;
1458 end Replace_Entity;
1459
1460 procedure Replace_Condition_Entities is
1461 new Traverse_Proc (Replace_Entity);
1462
1463 -- Local variables
1464
1465 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Subp);
1466 Subp_Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1467
1468 -- Start of processing for Build_Class_Wide_Expression
1469
1470 begin
1471 pragma Assert (Par_Typ /= Subp_Typ);
1472
1473 Update_Primitives_Mapping (Par_Subp, Subp);
1474 Map_Formals (Par_Subp, Subp);
1475 Replace_Condition_Entities (Pragma_Or_Expr);
1476 end Build_Class_Wide_Expression;
1477
1478 --------------------
1479 -- Build_DIC_Call --
1480 --------------------
1481
1482 function Build_DIC_Call
1483 (Loc : Source_Ptr;
1484 Obj_Name : Node_Id;
1485 Typ : Entity_Id) return Node_Id
1486 is
1487 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1488 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1489
1490 begin
1491 -- The DIC procedure has a null body if assertions are disabled or
1492 -- Assertion_Policy Ignore is in effect. In that case, it would be
1493 -- nice to generate a null statement instead of a call to the DIC
1494 -- procedure, but doing that seems to interfere with the determination
1495 -- of ECRs (early call regions) in SPARK. ???
1496
1497 return
1498 Make_Procedure_Call_Statement (Loc,
1499 Name => New_Occurrence_Of (Proc_Id, Loc),
1500 Parameter_Associations => New_List (
1501 Unchecked_Convert_To (Formal_Typ, Obj_Name)));
1502 end Build_DIC_Call;
1503
1504 ------------------------------
1505 -- Build_DIC_Procedure_Body --
1506 ------------------------------
1507
1508 -- WARNING: This routine manages Ghost regions. Return statements must be
1509 -- replaced by gotos which jump to the end of the routine and restore the
1510 -- Ghost mode.
1511
1512 procedure Build_DIC_Procedure_Body
1513 (Typ : Entity_Id;
1514 Partial_DIC : Boolean := False)
1515 is
1516 Pragmas_Seen : Elist_Id := No_Elist;
1517 -- This list contains all DIC pragmas processed so far. The list is used
1518 -- to avoid redundant Default_Initial_Condition checks.
1519
1520 procedure Add_DIC_Check
1521 (DIC_Prag : Node_Id;
1522 DIC_Expr : Node_Id;
1523 Stmts : in out List_Id);
1524 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1525 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1526 -- is added to list Stmts.
1527
1528 procedure Add_Inherited_DIC
1529 (DIC_Prag : Node_Id;
1530 Par_Typ : Entity_Id;
1531 Deriv_Typ : Entity_Id;
1532 Stmts : in out List_Id);
1533 -- Add a runtime check to verify the assertion expression of inherited
1534 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1535 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1536 -- pragma. All generated code is added to list Stmts.
1537
1538 procedure Add_Inherited_Tagged_DIC
1539 (DIC_Prag : Node_Id;
1540 Expr : Node_Id;
1541 Stmts : in out List_Id);
1542 -- Add a runtime check to verify assertion expression DIC_Expr of
1543 -- inherited pragma DIC_Prag. This routine applies class-wide pre-
1544 -- and postcondition-like runtime semantics to the check. Expr is
1545 -- the assertion expression after substitution has been performed
1546 -- (via Replace_References). All generated code is added to list Stmts.
1547
1548 procedure Add_Inherited_DICs
1549 (T : Entity_Id;
1550 Priv_Typ : Entity_Id;
1551 Full_Typ : Entity_Id;
1552 Obj_Id : Entity_Id;
1553 Checks : in out List_Id);
1554 -- Generate a DIC check for each inherited Default_Initial_Condition
1555 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
1556 -- the partial and full view of the parent type. Obj_Id denotes the
1557 -- entity of the _object formal parameter of the DIC procedure. All
1558 -- created checks are added to list Checks.
1559
1560 procedure Add_Own_DIC
1561 (DIC_Prag : Node_Id;
1562 DIC_Typ : Entity_Id;
1563 Obj_Id : Entity_Id;
1564 Stmts : in out List_Id);
1565 -- Add a runtime check to verify the assertion expression of pragma
1566 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. Obj_Id is the
1567 -- object to substitute in the assertion expression for any references
1568 -- to the current instance of the type All generated code is added to
1569 -- list Stmts.
1570
1571 procedure Add_Parent_DICs
1572 (T : Entity_Id;
1573 Obj_Id : Entity_Id;
1574 Checks : in out List_Id);
1575 -- Generate a Default_Initial_Condition check for each inherited DIC
1576 -- aspect coming from all parent types of type T. Obj_Id denotes the
1577 -- entity of the _object formal parameter of the DIC procedure. All
1578 -- created checks are added to list Checks.
1579
1580 -------------------
1581 -- Add_DIC_Check --
1582 -------------------
1583
1584 procedure Add_DIC_Check
1585 (DIC_Prag : Node_Id;
1586 DIC_Expr : Node_Id;
1587 Stmts : in out List_Id)
1588 is
1589 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1590 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1591
1592 begin
1593 -- The DIC pragma is ignored, nothing left to do
1594
1595 if Is_Ignored (DIC_Prag) then
1596 null;
1597
1598 -- Otherwise the DIC expression must be checked at run time.
1599 -- Generate:
1600
1601 -- pragma Check (<Nam>, <DIC_Expr>);
1602
1603 else
1604 Append_New_To (Stmts,
1605 Make_Pragma (Loc,
1606 Pragma_Identifier =>
1607 Make_Identifier (Loc, Name_Check),
1608
1609 Pragma_Argument_Associations => New_List (
1610 Make_Pragma_Argument_Association (Loc,
1611 Expression => Make_Identifier (Loc, Nam)),
1612
1613 Make_Pragma_Argument_Association (Loc,
1614 Expression => DIC_Expr))));
1615 end if;
1616
1617 -- Add the pragma to the list of processed pragmas
1618
1619 Append_New_Elmt (DIC_Prag, Pragmas_Seen);
1620 end Add_DIC_Check;
1621
1622 -----------------------
1623 -- Add_Inherited_DIC --
1624 -----------------------
1625
1626 procedure Add_Inherited_DIC
1627 (DIC_Prag : Node_Id;
1628 Par_Typ : Entity_Id;
1629 Deriv_Typ : Entity_Id;
1630 Stmts : in out List_Id)
1631 is
1632 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1633 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1634 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1635 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1636 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1637
1638 begin
1639 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1640
1641 -- Verify the inherited DIC assertion expression by calling the DIC
1642 -- procedure of the parent type.
1643
1644 -- Generate:
1645 -- <Par_Typ>DIC (Par_Typ (_object));
1646
1647 Append_New_To (Stmts,
1648 Make_Procedure_Call_Statement (Loc,
1649 Name => New_Occurrence_Of (Par_Proc, Loc),
1650 Parameter_Associations => New_List (
1651 Convert_To
1652 (Typ => Etype (Par_Obj),
1653 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1654 end Add_Inherited_DIC;
1655
1656 ------------------------------
1657 -- Add_Inherited_Tagged_DIC --
1658 ------------------------------
1659
1660 procedure Add_Inherited_Tagged_DIC
1661 (DIC_Prag : Node_Id;
1662 Expr : Node_Id;
1663 Stmts : in out List_Id)
1664 is
1665 begin
1666 -- Once the DIC assertion expression is fully processed, add a check
1667 -- to the statements of the DIC procedure.
1668
1669 Add_DIC_Check
1670 (DIC_Prag => DIC_Prag,
1671 DIC_Expr => Expr,
1672 Stmts => Stmts);
1673 end Add_Inherited_Tagged_DIC;
1674
1675 ------------------------
1676 -- Add_Inherited_DICs --
1677 ------------------------
1678
1679 procedure Add_Inherited_DICs
1680 (T : Entity_Id;
1681 Priv_Typ : Entity_Id;
1682 Full_Typ : Entity_Id;
1683 Obj_Id : Entity_Id;
1684 Checks : in out List_Id)
1685 is
1686 Deriv_Typ : Entity_Id;
1687 Expr : Node_Id;
1688 Prag : Node_Id;
1689 Prag_Expr : Node_Id;
1690 Prag_Expr_Arg : Node_Id;
1691 Prag_Typ : Node_Id;
1692 Prag_Typ_Arg : Node_Id;
1693
1694 Par_Proc : Entity_Id;
1695 -- The "partial" invariant procedure of Par_Typ
1696
1697 Par_Typ : Entity_Id;
1698 -- The suitable view of the parent type used in the substitution of
1699 -- type attributes.
1700
1701 begin
1702 if not Present (Priv_Typ) and then not Present (Full_Typ) then
1703 return;
1704 end if;
1705
1706 -- When the type inheriting the class-wide invariant is a concurrent
1707 -- type, use the corresponding record type because it contains all
1708 -- primitive operations of the concurrent type and allows for proper
1709 -- substitution.
1710
1711 if Is_Concurrent_Type (T) then
1712 Deriv_Typ := Corresponding_Record_Type (T);
1713 else
1714 Deriv_Typ := T;
1715 end if;
1716
1717 pragma Assert (Present (Deriv_Typ));
1718
1719 -- Determine which rep item chain to use. Precedence is given to that
1720 -- of the parent type's partial view since it usually carries all the
1721 -- class-wide invariants.
1722
1723 if Present (Priv_Typ) then
1724 Prag := First_Rep_Item (Priv_Typ);
1725 else
1726 Prag := First_Rep_Item (Full_Typ);
1727 end if;
1728
1729 while Present (Prag) loop
1730 if Nkind (Prag) = N_Pragma
1731 and then Pragma_Name (Prag) = Name_Default_Initial_Condition
1732 then
1733 -- Nothing to do if the pragma was already processed
1734
1735 if Contains (Pragmas_Seen, Prag) then
1736 return;
1737 end if;
1738
1739 -- Extract arguments of the Default_Initial_Condition pragma
1740
1741 Prag_Expr_Arg := First (Pragma_Argument_Associations (Prag));
1742 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
1743
1744 -- Pick up the implicit second argument of the pragma, which
1745 -- indicates the type that the pragma applies to.
1746
1747 Prag_Typ_Arg := Next (Prag_Expr_Arg);
1748 if Present (Prag_Typ_Arg) then
1749 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
1750 else
1751 Prag_Typ := Empty;
1752 end if;
1753
1754 -- The pragma applies to the partial view of the parent type
1755
1756 if Present (Priv_Typ)
1757 and then Present (Prag_Typ)
1758 and then Entity (Prag_Typ) = Priv_Typ
1759 then
1760 Par_Typ := Priv_Typ;
1761
1762 -- The pragma applies to the full view of the parent type
1763
1764 elsif Present (Full_Typ)
1765 and then Present (Prag_Typ)
1766 and then Entity (Prag_Typ) = Full_Typ
1767 then
1768 Par_Typ := Full_Typ;
1769
1770 -- Otherwise the pragma does not belong to the parent type and
1771 -- should not be considered.
1772
1773 else
1774 return;
1775 end if;
1776
1777 -- Substitute references in the DIC expression that are related
1778 -- to the partial type with corresponding references related to
1779 -- the derived type (call to Replace_References below).
1780
1781 Expr := New_Copy_Tree (Prag_Expr);
1782
1783 Par_Proc := Partial_DIC_Procedure (Par_Typ);
1784
1785 -- If there's not a partial DIC procedure (such as when a
1786 -- full type doesn't have its own DIC, but is inherited from
1787 -- a type with DIC), get the full DIC procedure.
1788
1789 if not Present (Par_Proc) then
1790 Par_Proc := DIC_Procedure (Par_Typ);
1791 end if;
1792
1793 Replace_References
1794 (Expr => Expr,
1795 Par_Typ => Par_Typ,
1796 Deriv_Typ => Deriv_Typ,
1797 Par_Obj => First_Formal (Par_Proc),
1798 Deriv_Obj => Obj_Id);
1799
1800 -- Why are there different actions depending on whether T is
1801 -- tagged? Can these be unified? ???
1802
1803 if Is_Tagged_Type (T) then
1804 Add_Inherited_Tagged_DIC
1805 (DIC_Prag => Prag,
1806 Expr => Expr,
1807 Stmts => Checks);
1808
1809 else
1810 Add_Inherited_DIC
1811 (DIC_Prag => Prag,
1812 Par_Typ => Par_Typ,
1813 Deriv_Typ => Deriv_Typ,
1814 Stmts => Checks);
1815 end if;
1816
1817 -- Leave as soon as we get a DIC pragma, since we'll visit
1818 -- the pragmas of the parents, so will get to any "inherited"
1819 -- pragmas that way.
1820
1821 return;
1822 end if;
1823
1824 Next_Rep_Item (Prag);
1825 end loop;
1826 end Add_Inherited_DICs;
1827
1828 -----------------
1829 -- Add_Own_DIC --
1830 -----------------
1831
1832 procedure Add_Own_DIC
1833 (DIC_Prag : Node_Id;
1834 DIC_Typ : Entity_Id;
1835 Obj_Id : Entity_Id;
1836 Stmts : in out List_Id)
1837 is
1838 DIC_Args : constant List_Id :=
1839 Pragma_Argument_Associations (DIC_Prag);
1840 DIC_Arg : constant Node_Id := First (DIC_Args);
1841 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1842 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1843
1844 -- Local variables
1845
1846 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1847
1848 Expr : Node_Id;
1849
1850 -- Start of processing for Add_Own_DIC
1851
1852 begin
1853 pragma Assert (Present (DIC_Expr));
1854 Expr := New_Copy_Tree (DIC_Expr);
1855
1856 -- Perform the following substitution:
1857
1858 -- * Replace the current instance of DIC_Typ with a reference to
1859 -- the _object formal parameter of the DIC procedure.
1860
1861 Replace_Type_References
1862 (Expr => Expr,
1863 Typ => DIC_Typ,
1864 Obj_Id => Obj_Id);
1865
1866 -- Preanalyze the DIC expression to detect errors and at the same
1867 -- time capture the visibility of the proper package part.
1868
1869 Set_Parent (Expr, Typ_Decl);
1870 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1871
1872 -- Save a copy of the expression with all replacements and analysis
1873 -- already taken place in case a derived type inherits the pragma.
1874 -- The copy will be used as the foundation of the derived type's own
1875 -- version of the DIC assertion expression.
1876
1877 if Is_Tagged_Type (DIC_Typ) then
1878 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1879 end if;
1880
1881 -- If the pragma comes from an aspect specification, replace the
1882 -- saved expression because all type references must be substituted
1883 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1884 -- routines.
1885
1886 if Present (DIC_Asp) then
1887 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1888 end if;
1889
1890 -- Once the DIC assertion expression is fully processed, add a check
1891 -- to the statements of the DIC procedure (unless the type is an
1892 -- abstract type, in which case we don't want the possibility of
1893 -- generating a call to an abstract function of the type; such DIC
1894 -- procedures can never be called in any case, so not generating the
1895 -- check at all is OK).
1896
1897 if not Is_Abstract_Type (DIC_Typ) or else GNATprove_Mode then
1898 Add_DIC_Check
1899 (DIC_Prag => DIC_Prag,
1900 DIC_Expr => Expr,
1901 Stmts => Stmts);
1902 end if;
1903 end Add_Own_DIC;
1904
1905 ---------------------
1906 -- Add_Parent_DICs --
1907 ---------------------
1908
1909 procedure Add_Parent_DICs
1910 (T : Entity_Id;
1911 Obj_Id : Entity_Id;
1912 Checks : in out List_Id)
1913 is
1914 Dummy_1 : Entity_Id;
1915 Dummy_2 : Entity_Id;
1916
1917 Curr_Typ : Entity_Id;
1918 -- The entity of the current type being examined
1919
1920 Full_Typ : Entity_Id;
1921 -- The full view of Par_Typ
1922
1923 Par_Typ : Entity_Id;
1924 -- The entity of the parent type
1925
1926 Priv_Typ : Entity_Id;
1927 -- The partial view of Par_Typ
1928
1929 Op_Node : Elmt_Id;
1930 Par_Prim : Entity_Id;
1931 Prim : Entity_Id;
1932
1933 begin
1934 -- Map the overridden primitive to the overriding one; required by
1935 -- Replace_References (called by Add_Inherited_DICs) to handle calls
1936 -- to parent primitives.
1937
1938 Op_Node := First_Elmt (Primitive_Operations (T));
1939 while Present (Op_Node) loop
1940 Prim := Node (Op_Node);
1941
1942 if Present (Overridden_Operation (Prim))
1943 and then Comes_From_Source (Prim)
1944 then
1945 Par_Prim := Overridden_Operation (Prim);
1946
1947 -- Create a mapping of the form:
1948 -- parent type primitive -> derived type primitive
1949
1950 Type_Map.Set (Par_Prim, Prim);
1951 end if;
1952
1953 Next_Elmt (Op_Node);
1954 end loop;
1955
1956 -- Climb the parent type chain
1957
1958 Curr_Typ := T;
1959 loop
1960 -- Do not consider subtypes, as they inherit the DICs from their
1961 -- base types.
1962
1963 Par_Typ := Base_Type (Etype (Base_Type (Curr_Typ)));
1964
1965 -- Stop the climb once the root of the parent chain is
1966 -- reached.
1967
1968 exit when Curr_Typ = Par_Typ;
1969
1970 -- Process the DICs of the parent type
1971
1972 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
1973
1974 -- Only try to inherit a DIC pragma from the parent type Par_Typ
1975 -- if it Has_Own_DIC pragma. The loop will proceed up the parent
1976 -- chain to find all types that have their own DIC.
1977
1978 if Has_Own_DIC (Par_Typ) then
1979 Add_Inherited_DICs
1980 (T => T,
1981 Priv_Typ => Priv_Typ,
1982 Full_Typ => Full_Typ,
1983 Obj_Id => Obj_Id,
1984 Checks => Checks);
1985 end if;
1986
1987 Curr_Typ := Par_Typ;
1988 end loop;
1989 end Add_Parent_DICs;
1990
1991 -- Local variables
1992
1993 Loc : constant Source_Ptr := Sloc (Typ);
1994
1995 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1996 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1997 -- Save the Ghost-related attributes to restore on exit
1998
1999 DIC_Prag : Node_Id;
2000 DIC_Typ : Entity_Id;
2001 Dummy_1 : Entity_Id;
2002 Dummy_2 : Entity_Id;
2003 Proc_Body : Node_Id;
2004 Proc_Body_Id : Entity_Id;
2005 Proc_Decl : Node_Id;
2006 Proc_Id : Entity_Id;
2007 Stmts : List_Id := No_List;
2008
2009 CRec_Typ : Entity_Id := Empty;
2010 -- The corresponding record type of Full_Typ
2011
2012 Full_Typ : Entity_Id := Empty;
2013 -- The full view of the working type
2014
2015 Obj_Id : Entity_Id := Empty;
2016 -- The _object formal parameter of the invariant procedure
2017
2018 Part_Proc : Entity_Id := Empty;
2019 -- The entity of the "partial" invariant procedure
2020
2021 Priv_Typ : Entity_Id := Empty;
2022 -- The partial view of the working type
2023
2024 Work_Typ : Entity_Id;
2025 -- The working type
2026
2027 -- Start of processing for Build_DIC_Procedure_Body
2028
2029 begin
2030 Work_Typ := Base_Type (Typ);
2031
2032 -- Do not process class-wide types as these are Itypes, but lack a first
2033 -- subtype (see below).
2034
2035 if Is_Class_Wide_Type (Work_Typ) then
2036 return;
2037
2038 -- Do not process the underlying full view of a private type. There is
2039 -- no way to get back to the partial view, plus the body will be built
2040 -- by the full view or the base type.
2041
2042 elsif Is_Underlying_Full_View (Work_Typ) then
2043 return;
2044
2045 -- Use the first subtype when dealing with various base types
2046
2047 elsif Is_Itype (Work_Typ) then
2048 Work_Typ := First_Subtype (Work_Typ);
2049
2050 -- The input denotes the corresponding record type of a protected or a
2051 -- task type. Work with the concurrent type because the corresponding
2052 -- record type may not be visible to clients of the type.
2053
2054 elsif Ekind (Work_Typ) = E_Record_Type
2055 and then Is_Concurrent_Record_Type (Work_Typ)
2056 then
2057 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2058 end if;
2059
2060 -- The working type may be subject to pragma Ghost. Set the mode now to
2061 -- ensure that the DIC procedure is properly marked as Ghost.
2062
2063 Set_Ghost_Mode (Work_Typ);
2064
2065 -- The working type must be either define a DIC pragma of its own or
2066 -- inherit one from a parent type.
2067
2068 pragma Assert (Has_DIC (Work_Typ));
2069
2070 -- Recover the type which defines the DIC pragma. This is either the
2071 -- working type itself or a parent type when the pragma is inherited.
2072
2073 DIC_Typ := Find_DIC_Type (Work_Typ);
2074 pragma Assert (Present (DIC_Typ));
2075
2076 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2077 pragma Assert (Present (DIC_Prag));
2078
2079 -- Nothing to do if pragma DIC appears without an argument or its sole
2080 -- argument is "null".
2081
2082 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2083 goto Leave;
2084 end if;
2085
2086 -- Obtain both views of the type
2087
2088 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy_1, CRec_Typ);
2089
2090 -- The caller requests a body for the partial DIC procedure
2091
2092 if Partial_DIC then
2093 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2094
2095 -- The "full" DIC procedure body was already created
2096
2097 -- Create a declaration for the "partial" DIC procedure if it
2098 -- is not available.
2099
2100 if No (Proc_Id) then
2101 Build_DIC_Procedure_Declaration
2102 (Typ => Work_Typ,
2103 Partial_DIC => True);
2104
2105 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2106 end if;
2107
2108 -- The caller requests a body for the "full" DIC procedure
2109
2110 else
2111 Proc_Id := DIC_Procedure (Work_Typ);
2112 Part_Proc := Partial_DIC_Procedure (Work_Typ);
2113
2114 -- Create a declaration for the "full" DIC procedure if it is
2115 -- not available.
2116
2117 if No (Proc_Id) then
2118 Build_DIC_Procedure_Declaration (Work_Typ);
2119 Proc_Id := DIC_Procedure (Work_Typ);
2120 end if;
2121 end if;
2122
2123 -- At this point there should be a DIC procedure declaration
2124
2125 pragma Assert (Present (Proc_Id));
2126 Proc_Decl := Unit_Declaration_Node (Proc_Id);
2127
2128 -- Nothing to do if the DIC procedure already has a body
2129
2130 if Present (Corresponding_Body (Proc_Decl)) then
2131 goto Leave;
2132 end if;
2133
2134 -- Emulate the environment of the DIC procedure by installing its scope
2135 -- and formal parameters.
2136
2137 Push_Scope (Proc_Id);
2138 Install_Formals (Proc_Id);
2139
2140 Obj_Id := First_Formal (Proc_Id);
2141 pragma Assert (Present (Obj_Id));
2142
2143 -- The "partial" DIC procedure verifies the DICs of the partial view
2144 -- only.
2145
2146 if Partial_DIC then
2147 pragma Assert (Present (Priv_Typ));
2148
2149 if Has_Own_DIC (Work_Typ) then -- If we're testing this then maybe
2150 Add_Own_DIC -- we shouldn't be calling Find_DIC_Typ above???
2151 (DIC_Prag => DIC_Prag,
2152 DIC_Typ => DIC_Typ, -- Should this just be Work_Typ???
2153 Obj_Id => Obj_Id,
2154 Stmts => Stmts);
2155 end if;
2156
2157 -- Otherwise, the "full" DIC procedure verifies the DICs inherited from
2158 -- parent types, as well as indirectly verifying the DICs of the partial
2159 -- view by calling the "partial" DIC procedure.
2160
2161 else
2162 -- Check the DIC of the partial view by calling the "partial" DIC
2163 -- procedure, unless the partial DIC body is empty. Generate:
2164
2165 -- <Work_Typ>Partial_DIC (_object);
2166
2167 if Present (Part_Proc) and then not Has_Null_Body (Part_Proc) then
2168 Append_New_To (Stmts,
2169 Make_Procedure_Call_Statement (Loc,
2170 Name => New_Occurrence_Of (Part_Proc, Loc),
2171 Parameter_Associations => New_List (
2172 New_Occurrence_Of (Obj_Id, Loc))));
2173 end if;
2174
2175 -- Process inherited Default_Initial_Conditions for all parent types
2176
2177 Add_Parent_DICs (Work_Typ, Obj_Id, Stmts);
2178 end if;
2179
2180 End_Scope;
2181
2182 -- Produce an empty completing body in the following cases:
2183 -- * Assertions are disabled
2184 -- * The DIC Assertion_Policy is Ignore
2185
2186 if No (Stmts) then
2187 Stmts := New_List (Make_Null_Statement (Loc));
2188 end if;
2189
2190 -- Generate:
2191 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
2192 -- begin
2193 -- <Stmts>
2194 -- end <Work_Typ>DIC;
2195
2196 Proc_Body :=
2197 Make_Subprogram_Body (Loc,
2198 Specification =>
2199 Copy_Subprogram_Spec (Parent (Proc_Id)),
2200 Declarations => Empty_List,
2201 Handled_Statement_Sequence =>
2202 Make_Handled_Sequence_Of_Statements (Loc,
2203 Statements => Stmts));
2204 Proc_Body_Id := Defining_Entity (Proc_Body);
2205
2206 -- Perform minor decoration in case the body is not analyzed
2207
2208 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
2209 Set_Etype (Proc_Body_Id, Standard_Void_Type);
2210 Set_Scope (Proc_Body_Id, Current_Scope);
2211 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
2212 Set_SPARK_Pragma_Inherited
2213 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
2214
2215 -- Link both spec and body to avoid generating duplicates
2216
2217 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
2218 Set_Corresponding_Spec (Proc_Body, Proc_Id);
2219
2220 -- The body should not be inserted into the tree when the context
2221 -- is a generic unit because it is not part of the template.
2222 -- Note that the body must still be generated in order to resolve the
2223 -- DIC assertion expression.
2224
2225 if Inside_A_Generic then
2226 null;
2227
2228 -- Semi-insert the body into the tree for GNATprove by setting its
2229 -- Parent field. This allows for proper upstream tree traversals.
2230
2231 elsif GNATprove_Mode then
2232 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
2233
2234 -- Otherwise the body is part of the freezing actions of the working
2235 -- type.
2236
2237 else
2238 Append_Freeze_Action (Work_Typ, Proc_Body);
2239 end if;
2240
2241 <<Leave>>
2242 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2243 end Build_DIC_Procedure_Body;
2244
2245 -------------------------------------
2246 -- Build_DIC_Procedure_Declaration --
2247 -------------------------------------
2248
2249 -- WARNING: This routine manages Ghost regions. Return statements must be
2250 -- replaced by gotos which jump to the end of the routine and restore the
2251 -- Ghost mode.
2252
2253 procedure Build_DIC_Procedure_Declaration
2254 (Typ : Entity_Id;
2255 Partial_DIC : Boolean := False)
2256 is
2257 Loc : constant Source_Ptr := Sloc (Typ);
2258
2259 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2260 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2261 -- Save the Ghost-related attributes to restore on exit
2262
2263 DIC_Prag : Node_Id;
2264 DIC_Typ : Entity_Id;
2265 Proc_Decl : Node_Id;
2266 Proc_Id : Entity_Id;
2267 Proc_Nam : Name_Id;
2268 Typ_Decl : Node_Id;
2269
2270 CRec_Typ : Entity_Id;
2271 -- The corresponding record type of Full_Typ
2272
2273 Full_Typ : Entity_Id;
2274 -- The full view of working type
2275
2276 Obj_Id : Entity_Id;
2277 -- The _object formal parameter of the DIC procedure
2278
2279 Priv_Typ : Entity_Id;
2280 -- The partial view of working type
2281
2282 UFull_Typ : Entity_Id;
2283 -- The underlying full view of Full_Typ
2284
2285 Work_Typ : Entity_Id;
2286 -- The working type
2287
2288 begin
2289 Work_Typ := Base_Type (Typ);
2290
2291 -- Do not process class-wide types as these are Itypes, but lack a first
2292 -- subtype (see below).
2293
2294 if Is_Class_Wide_Type (Work_Typ) then
2295 return;
2296
2297 -- Do not process the underlying full view of a private type. There is
2298 -- no way to get back to the partial view, plus the body will be built
2299 -- by the full view or the base type.
2300
2301 elsif Is_Underlying_Full_View (Work_Typ) then
2302 return;
2303
2304 -- Use the first subtype when dealing with various base types
2305
2306 elsif Is_Itype (Work_Typ) then
2307 Work_Typ := First_Subtype (Work_Typ);
2308
2309 -- The input denotes the corresponding record type of a protected or a
2310 -- task type. Work with the concurrent type because the corresponding
2311 -- record type may not be visible to clients of the type.
2312
2313 elsif Ekind (Work_Typ) = E_Record_Type
2314 and then Is_Concurrent_Record_Type (Work_Typ)
2315 then
2316 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2317 end if;
2318
2319 -- The working type may be subject to pragma Ghost. Set the mode now to
2320 -- ensure that the DIC procedure is properly marked as Ghost.
2321
2322 Set_Ghost_Mode (Work_Typ);
2323
2324 -- The type must be either subject to a DIC pragma or inherit one from a
2325 -- parent type.
2326
2327 pragma Assert (Has_DIC (Work_Typ));
2328
2329 -- Recover the type which defines the DIC pragma. This is either the
2330 -- working type itself or a parent type when the pragma is inherited.
2331
2332 DIC_Typ := Find_DIC_Type (Work_Typ);
2333 pragma Assert (Present (DIC_Typ));
2334
2335 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2336 pragma Assert (Present (DIC_Prag));
2337
2338 -- Nothing to do if pragma DIC appears without an argument or its sole
2339 -- argument is "null".
2340
2341 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2342 goto Leave;
2343 end if;
2344
2345 -- Nothing to do if the type already has a "partial" DIC procedure
2346
2347 if Partial_DIC then
2348 if Present (Partial_DIC_Procedure (Work_Typ)) then
2349 goto Leave;
2350 end if;
2351
2352 -- Nothing to do if the type already has a "full" DIC procedure
2353
2354 elsif Present (DIC_Procedure (Work_Typ)) then
2355 goto Leave;
2356 end if;
2357
2358 -- The caller requests the declaration of the "partial" DIC procedure
2359
2360 if Partial_DIC then
2361 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_DIC");
2362
2363 -- Otherwise the caller requests the declaration of the "full" DIC
2364 -- procedure.
2365
2366 else
2367 Proc_Nam := New_External_Name (Chars (Work_Typ), "DIC");
2368 end if;
2369
2370 Proc_Id :=
2371 Make_Defining_Identifier (Loc, Chars => Proc_Nam);
2372
2373 -- Perform minor decoration in case the declaration is not analyzed
2374
2375 Mutate_Ekind (Proc_Id, E_Procedure);
2376 Set_Etype (Proc_Id, Standard_Void_Type);
2377 Set_Is_DIC_Procedure (Proc_Id);
2378 Set_Scope (Proc_Id, Current_Scope);
2379 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
2380 Set_SPARK_Pragma_Inherited (Proc_Id);
2381
2382 Set_DIC_Procedure (Work_Typ, Proc_Id);
2383
2384 -- The DIC procedure requires debug info when the assertion expression
2385 -- is subject to Source Coverage Obligations.
2386
2387 if Generate_SCO then
2388 Set_Debug_Info_Needed (Proc_Id);
2389 end if;
2390
2391 -- Obtain all views of the input type
2392
2393 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
2394
2395 -- Associate the DIC procedure and various flags with all views
2396
2397 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
2398 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
2399 Propagate_DIC_Attributes (UFull_Typ, From_Typ => Work_Typ);
2400 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
2401
2402 -- The declaration of the DIC procedure must be inserted after the
2403 -- declaration of the partial view as this allows for proper external
2404 -- visibility.
2405
2406 if Present (Priv_Typ) then
2407 Typ_Decl := Declaration_Node (Priv_Typ);
2408
2409 -- Derived types with the full view as parent do not have a partial
2410 -- view. Insert the DIC procedure after the derived type.
2411
2412 else
2413 Typ_Decl := Declaration_Node (Full_Typ);
2414 end if;
2415
2416 -- The type should have a declarative node
2417
2418 pragma Assert (Present (Typ_Decl));
2419
2420 -- Create the formal parameter which emulates the variable-like behavior
2421 -- of the type's current instance.
2422
2423 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
2424
2425 -- Perform minor decoration in case the declaration is not analyzed
2426
2427 Mutate_Ekind (Obj_Id, E_In_Parameter);
2428 Set_Etype (Obj_Id, Work_Typ);
2429 Set_Scope (Obj_Id, Proc_Id);
2430
2431 Set_First_Entity (Proc_Id, Obj_Id);
2432 Set_Last_Entity (Proc_Id, Obj_Id);
2433
2434 -- Generate:
2435 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
2436
2437 Proc_Decl :=
2438 Make_Subprogram_Declaration (Loc,
2439 Specification =>
2440 Make_Procedure_Specification (Loc,
2441 Defining_Unit_Name => Proc_Id,
2442 Parameter_Specifications => New_List (
2443 Make_Parameter_Specification (Loc,
2444 Defining_Identifier => Obj_Id,
2445 Parameter_Type =>
2446 New_Occurrence_Of (Work_Typ, Loc)))));
2447
2448 -- The declaration should not be inserted into the tree when the context
2449 -- is a generic unit because it is not part of the template.
2450
2451 if Inside_A_Generic then
2452 null;
2453
2454 -- Semi-insert the declaration into the tree for GNATprove by setting
2455 -- its Parent field. This allows for proper upstream tree traversals.
2456
2457 elsif GNATprove_Mode then
2458 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2459
2460 -- Otherwise insert the declaration
2461
2462 else
2463 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2464 end if;
2465
2466 <<Leave>>
2467 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2468 end Build_DIC_Procedure_Declaration;
2469
2470 ------------------------------------
2471 -- Build_Invariant_Procedure_Body --
2472 ------------------------------------
2473
2474 -- WARNING: This routine manages Ghost regions. Return statements must be
2475 -- replaced by gotos which jump to the end of the routine and restore the
2476 -- Ghost mode.
2477
2478 procedure Build_Invariant_Procedure_Body
2479 (Typ : Entity_Id;
2480 Partial_Invariant : Boolean := False)
2481 is
2482 Loc : constant Source_Ptr := Sloc (Typ);
2483
2484 Pragmas_Seen : Elist_Id := No_Elist;
2485 -- This list contains all invariant pragmas processed so far. The list
2486 -- is used to avoid generating redundant invariant checks.
2487
2488 Produced_Check : Boolean := False;
2489 -- This flag tracks whether the type has produced at least one invariant
2490 -- check. The flag is used as a sanity check at the end of the routine.
2491
2492 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2493 -- intentionally unnested to avoid deep indentation of code.
2494
2495 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2496 -- they emit checks, loops (for arrays) and case statements (for record
2497 -- variant parts) only when there are invariants to verify. This keeps
2498 -- the body of the invariant procedure free of useless code.
2499
2500 procedure Add_Array_Component_Invariants
2501 (T : Entity_Id;
2502 Obj_Id : Entity_Id;
2503 Checks : in out List_Id);
2504 -- Generate an invariant check for each component of array type T.
2505 -- Obj_Id denotes the entity of the _object formal parameter of the
2506 -- invariant procedure. All created checks are added to list Checks.
2507
2508 procedure Add_Inherited_Invariants
2509 (T : Entity_Id;
2510 Priv_Typ : Entity_Id;
2511 Full_Typ : Entity_Id;
2512 Obj_Id : Entity_Id;
2513 Checks : in out List_Id);
2514 -- Generate an invariant check for each inherited class-wide invariant
2515 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2516 -- the partial and full view of the parent type. Obj_Id denotes the
2517 -- entity of the _object formal parameter of the invariant procedure.
2518 -- All created checks are added to list Checks.
2519
2520 procedure Add_Interface_Invariants
2521 (T : Entity_Id;
2522 Obj_Id : Entity_Id;
2523 Checks : in out List_Id);
2524 -- Generate an invariant check for each inherited class-wide invariant
2525 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2526 -- entity of the _object formal parameter of the invariant procedure.
2527 -- All created checks are added to list Checks.
2528
2529 procedure Add_Invariant_Check
2530 (Prag : Node_Id;
2531 Expr : Node_Id;
2532 Checks : in out List_Id;
2533 Inherited : Boolean := False);
2534 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2535 -- verify assertion expression Expr of pragma Prag. All generated code
2536 -- is added to list Checks. Flag Inherited should be set when the pragma
2537 -- is inherited from a parent or interface type.
2538
2539 procedure Add_Own_Invariants
2540 (T : Entity_Id;
2541 Obj_Id : Entity_Id;
2542 Checks : in out List_Id;
2543 Priv_Item : Node_Id := Empty);
2544 -- Generate an invariant check for each invariant found for type T.
2545 -- Obj_Id denotes the entity of the _object formal parameter of the
2546 -- invariant procedure. All created checks are added to list Checks.
2547 -- Priv_Item denotes the first rep item of the private type.
2548
2549 procedure Add_Parent_Invariants
2550 (T : Entity_Id;
2551 Obj_Id : Entity_Id;
2552 Checks : in out List_Id);
2553 -- Generate an invariant check for each inherited class-wide invariant
2554 -- coming from all parent types of type T. Obj_Id denotes the entity of
2555 -- the _object formal parameter of the invariant procedure. All created
2556 -- checks are added to list Checks.
2557
2558 procedure Add_Record_Component_Invariants
2559 (T : Entity_Id;
2560 Obj_Id : Entity_Id;
2561 Checks : in out List_Id);
2562 -- Generate an invariant check for each component of record type T.
2563 -- Obj_Id denotes the entity of the _object formal parameter of the
2564 -- invariant procedure. All created checks are added to list Checks.
2565
2566 ------------------------------------
2567 -- Add_Array_Component_Invariants --
2568 ------------------------------------
2569
2570 procedure Add_Array_Component_Invariants
2571 (T : Entity_Id;
2572 Obj_Id : Entity_Id;
2573 Checks : in out List_Id)
2574 is
2575 Comp_Typ : constant Entity_Id := Component_Type (T);
2576 Dims : constant Pos := Number_Dimensions (T);
2577
2578 procedure Process_Array_Component
2579 (Indices : List_Id;
2580 Comp_Checks : in out List_Id);
2581 -- Generate an invariant check for an array component identified by
2582 -- the indices in list Indices. All created checks are added to list
2583 -- Comp_Checks.
2584
2585 procedure Process_One_Dimension
2586 (Dim : Pos;
2587 Indices : List_Id;
2588 Dim_Checks : in out List_Id);
2589 -- Generate a loop over the Nth dimension Dim of an array type. List
2590 -- Indices contains all array indices for the dimension. All created
2591 -- checks are added to list Dim_Checks.
2592
2593 -----------------------------
2594 -- Process_Array_Component --
2595 -----------------------------
2596
2597 procedure Process_Array_Component
2598 (Indices : List_Id;
2599 Comp_Checks : in out List_Id)
2600 is
2601 Proc_Id : Entity_Id;
2602
2603 begin
2604 if Has_Invariants (Comp_Typ) then
2605
2606 -- In GNATprove mode, the component invariants are checked by
2607 -- other means. They should not be added to the array type
2608 -- invariant procedure, so that the procedure can be used to
2609 -- check the array type invariants if any.
2610
2611 if GNATprove_Mode then
2612 null;
2613
2614 else
2615 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2616
2617 -- The component type should have an invariant procedure
2618 -- if it has invariants of its own or inherits class-wide
2619 -- invariants from parent or interface types.
2620
2621 pragma Assert (Present (Proc_Id));
2622
2623 -- Generate:
2624 -- <Comp_Typ>Invariant (_object (<Indices>));
2625
2626 -- The invariant procedure has a null body if assertions are
2627 -- disabled or Assertion_Policy Ignore is in effect.
2628
2629 if not Has_Null_Body (Proc_Id) then
2630 Append_New_To (Comp_Checks,
2631 Make_Procedure_Call_Statement (Loc,
2632 Name =>
2633 New_Occurrence_Of (Proc_Id, Loc),
2634 Parameter_Associations => New_List (
2635 Make_Indexed_Component (Loc,
2636 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2637 Expressions => New_Copy_List (Indices)))));
2638 end if;
2639 end if;
2640
2641 Produced_Check := True;
2642 end if;
2643 end Process_Array_Component;
2644
2645 ---------------------------
2646 -- Process_One_Dimension --
2647 ---------------------------
2648
2649 procedure Process_One_Dimension
2650 (Dim : Pos;
2651 Indices : List_Id;
2652 Dim_Checks : in out List_Id)
2653 is
2654 Comp_Checks : List_Id := No_List;
2655 Index : Entity_Id;
2656
2657 begin
2658 -- Generate the invariant checks for the array component after all
2659 -- dimensions have produced their respective loops.
2660
2661 if Dim > Dims then
2662 Process_Array_Component
2663 (Indices => Indices,
2664 Comp_Checks => Dim_Checks);
2665
2666 -- Otherwise create a loop for the current dimension
2667
2668 else
2669 -- Create a new loop variable for each dimension
2670
2671 Index :=
2672 Make_Defining_Identifier (Loc,
2673 Chars => New_External_Name ('I', Dim));
2674 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2675
2676 Process_One_Dimension
2677 (Dim => Dim + 1,
2678 Indices => Indices,
2679 Dim_Checks => Comp_Checks);
2680
2681 -- Generate:
2682 -- for I<Dim> in _object'Range (<Dim>) loop
2683 -- <Comp_Checks>
2684 -- end loop;
2685
2686 -- Note that the invariant procedure may have a null body if
2687 -- assertions are disabled or Assertion_Policy Ignore is in
2688 -- effect.
2689
2690 if Present (Comp_Checks) then
2691 Append_New_To (Dim_Checks,
2692 Make_Implicit_Loop_Statement (T,
2693 Identifier => Empty,
2694 Iteration_Scheme =>
2695 Make_Iteration_Scheme (Loc,
2696 Loop_Parameter_Specification =>
2697 Make_Loop_Parameter_Specification (Loc,
2698 Defining_Identifier => Index,
2699 Discrete_Subtype_Definition =>
2700 Make_Attribute_Reference (Loc,
2701 Prefix =>
2702 New_Occurrence_Of (Obj_Id, Loc),
2703 Attribute_Name => Name_Range,
2704 Expressions => New_List (
2705 Make_Integer_Literal (Loc, Dim))))),
2706 Statements => Comp_Checks));
2707 end if;
2708 end if;
2709 end Process_One_Dimension;
2710
2711 -- Start of processing for Add_Array_Component_Invariants
2712
2713 begin
2714 Process_One_Dimension
2715 (Dim => 1,
2716 Indices => New_List,
2717 Dim_Checks => Checks);
2718 end Add_Array_Component_Invariants;
2719
2720 ------------------------------
2721 -- Add_Inherited_Invariants --
2722 ------------------------------
2723
2724 procedure Add_Inherited_Invariants
2725 (T : Entity_Id;
2726 Priv_Typ : Entity_Id;
2727 Full_Typ : Entity_Id;
2728 Obj_Id : Entity_Id;
2729 Checks : in out List_Id)
2730 is
2731 Deriv_Typ : Entity_Id;
2732 Expr : Node_Id;
2733 Prag : Node_Id;
2734 Prag_Expr : Node_Id;
2735 Prag_Expr_Arg : Node_Id;
2736 Prag_Typ : Node_Id;
2737 Prag_Typ_Arg : Node_Id;
2738
2739 Par_Proc : Entity_Id;
2740 -- The "partial" invariant procedure of Par_Typ
2741
2742 Par_Typ : Entity_Id;
2743 -- The suitable view of the parent type used in the substitution of
2744 -- type attributes.
2745
2746 begin
2747 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2748 return;
2749 end if;
2750
2751 -- When the type inheriting the class-wide invariant is a concurrent
2752 -- type, use the corresponding record type because it contains all
2753 -- primitive operations of the concurrent type and allows for proper
2754 -- substitution.
2755
2756 if Is_Concurrent_Type (T) then
2757 Deriv_Typ := Corresponding_Record_Type (T);
2758 else
2759 Deriv_Typ := T;
2760 end if;
2761
2762 pragma Assert (Present (Deriv_Typ));
2763
2764 -- Determine which rep item chain to use. Precedence is given to that
2765 -- of the parent type's partial view since it usually carries all the
2766 -- class-wide invariants.
2767
2768 if Present (Priv_Typ) then
2769 Prag := First_Rep_Item (Priv_Typ);
2770 else
2771 Prag := First_Rep_Item (Full_Typ);
2772 end if;
2773
2774 while Present (Prag) loop
2775 if Nkind (Prag) = N_Pragma
2776 and then Pragma_Name (Prag) = Name_Invariant
2777 then
2778 -- Nothing to do if the pragma was already processed
2779
2780 if Contains (Pragmas_Seen, Prag) then
2781 return;
2782
2783 -- Nothing to do when the caller requests the processing of all
2784 -- inherited class-wide invariants, but the pragma does not
2785 -- fall in this category.
2786
2787 elsif not Class_Present (Prag) then
2788 return;
2789 end if;
2790
2791 -- Extract the arguments of the invariant pragma
2792
2793 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2794 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2795 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2796 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2797
2798 -- The pragma applies to the partial view of the parent type
2799
2800 if Present (Priv_Typ)
2801 and then Entity (Prag_Typ) = Priv_Typ
2802 then
2803 Par_Typ := Priv_Typ;
2804
2805 -- The pragma applies to the full view of the parent type
2806
2807 elsif Present (Full_Typ)
2808 and then Entity (Prag_Typ) = Full_Typ
2809 then
2810 Par_Typ := Full_Typ;
2811
2812 -- Otherwise the pragma does not belong to the parent type and
2813 -- should not be considered.
2814
2815 else
2816 return;
2817 end if;
2818
2819 -- Perform the following substitutions:
2820
2821 -- * Replace a reference to the _object parameter of the
2822 -- parent type's partial invariant procedure with a
2823 -- reference to the _object parameter of the derived
2824 -- type's full invariant procedure.
2825
2826 -- * Replace a reference to a discriminant of the parent type
2827 -- with a suitable value from the point of view of the
2828 -- derived type.
2829
2830 -- * Replace a call to an overridden parent primitive with a
2831 -- call to the overriding derived type primitive.
2832
2833 -- * Replace a call to an inherited parent primitive with a
2834 -- call to the internally-generated inherited derived type
2835 -- primitive.
2836
2837 Expr := New_Copy_Tree (Prag_Expr);
2838
2839 -- The parent type must have a "partial" invariant procedure
2840 -- because class-wide invariants are captured exclusively by
2841 -- it.
2842
2843 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2844 pragma Assert (Present (Par_Proc));
2845
2846 Replace_References
2847 (Expr => Expr,
2848 Par_Typ => Par_Typ,
2849 Deriv_Typ => Deriv_Typ,
2850 Par_Obj => First_Formal (Par_Proc),
2851 Deriv_Obj => Obj_Id);
2852
2853 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2854 end if;
2855
2856 Next_Rep_Item (Prag);
2857 end loop;
2858 end Add_Inherited_Invariants;
2859
2860 ------------------------------
2861 -- Add_Interface_Invariants --
2862 ------------------------------
2863
2864 procedure Add_Interface_Invariants
2865 (T : Entity_Id;
2866 Obj_Id : Entity_Id;
2867 Checks : in out List_Id)
2868 is
2869 Iface_Elmt : Elmt_Id;
2870 Ifaces : Elist_Id;
2871
2872 begin
2873 -- Generate an invariant check for each class-wide invariant coming
2874 -- from all interfaces implemented by type T.
2875
2876 if Is_Tagged_Type (T) then
2877 Collect_Interfaces (T, Ifaces);
2878
2879 -- Process the class-wide invariants of all implemented interfaces
2880
2881 Iface_Elmt := First_Elmt (Ifaces);
2882 while Present (Iface_Elmt) loop
2883
2884 -- The Full_Typ parameter is intentionally left Empty because
2885 -- interfaces are treated as the partial view of a private type
2886 -- in order to achieve uniformity with the general case.
2887
2888 Add_Inherited_Invariants
2889 (T => T,
2890 Priv_Typ => Node (Iface_Elmt),
2891 Full_Typ => Empty,
2892 Obj_Id => Obj_Id,
2893 Checks => Checks);
2894
2895 Next_Elmt (Iface_Elmt);
2896 end loop;
2897 end if;
2898 end Add_Interface_Invariants;
2899
2900 -------------------------
2901 -- Add_Invariant_Check --
2902 -------------------------
2903
2904 procedure Add_Invariant_Check
2905 (Prag : Node_Id;
2906 Expr : Node_Id;
2907 Checks : in out List_Id;
2908 Inherited : Boolean := False)
2909 is
2910 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2911 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2912 Ploc : constant Source_Ptr := Sloc (Prag);
2913 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2914
2915 Assoc : List_Id;
2916 Str : String_Id;
2917
2918 begin
2919 -- The invariant is ignored, nothing left to do
2920
2921 if Is_Ignored (Prag) then
2922 null;
2923
2924 -- Otherwise the invariant is checked. Build a pragma Check to verify
2925 -- the expression at run time.
2926
2927 else
2928 Assoc := New_List (
2929 Make_Pragma_Argument_Association (Ploc,
2930 Expression => Make_Identifier (Ploc, Nam)),
2931 Make_Pragma_Argument_Association (Ploc,
2932 Expression => Expr));
2933
2934 -- Handle the String argument (if any)
2935
2936 if Present (Str_Arg) then
2937 Str := Strval (Get_Pragma_Arg (Str_Arg));
2938
2939 -- When inheriting an invariant, modify the message from
2940 -- "failed invariant" to "failed inherited invariant".
2941
2942 if Inherited then
2943 String_To_Name_Buffer (Str);
2944
2945 if Name_Buffer (1 .. 16) = "failed invariant" then
2946 Insert_Str_In_Name_Buffer ("inherited ", 8);
2947 Str := String_From_Name_Buffer;
2948 end if;
2949 end if;
2950
2951 Append_To (Assoc,
2952 Make_Pragma_Argument_Association (Ploc,
2953 Expression => Make_String_Literal (Ploc, Str)));
2954 end if;
2955
2956 -- Generate:
2957 -- pragma Check (<Nam>, <Expr>, <Str>);
2958
2959 Append_New_To (Checks,
2960 Make_Pragma (Ploc,
2961 Chars => Name_Check,
2962 Pragma_Argument_Associations => Assoc));
2963 end if;
2964
2965 -- Output an info message when inheriting an invariant and the
2966 -- listing option is enabled.
2967
2968 if Inherited and Opt.List_Inherited_Aspects then
2969 Error_Msg_Sloc := Sloc (Prag);
2970 Error_Msg_N
2971 ("info: & inherits `Invariant''Class` aspect from #?.l?", Typ);
2972 end if;
2973
2974 -- Add the pragma to the list of processed pragmas
2975
2976 Append_New_Elmt (Prag, Pragmas_Seen);
2977 Produced_Check := True;
2978 end Add_Invariant_Check;
2979
2980 ---------------------------
2981 -- Add_Parent_Invariants --
2982 ---------------------------
2983
2984 procedure Add_Parent_Invariants
2985 (T : Entity_Id;
2986 Obj_Id : Entity_Id;
2987 Checks : in out List_Id)
2988 is
2989 Dummy_1 : Entity_Id;
2990 Dummy_2 : Entity_Id;
2991
2992 Curr_Typ : Entity_Id;
2993 -- The entity of the current type being examined
2994
2995 Full_Typ : Entity_Id;
2996 -- The full view of Par_Typ
2997
2998 Par_Typ : Entity_Id;
2999 -- The entity of the parent type
3000
3001 Priv_Typ : Entity_Id;
3002 -- The partial view of Par_Typ
3003
3004 begin
3005 -- Do not process array types because they cannot have true parent
3006 -- types. This also prevents the generation of a duplicate invariant
3007 -- check when the input type is an array base type because its Etype
3008 -- denotes the first subtype, both of which share the same component
3009 -- type.
3010
3011 if Is_Array_Type (T) then
3012 return;
3013 end if;
3014
3015 -- Climb the parent type chain
3016
3017 Curr_Typ := T;
3018 loop
3019 -- Do not consider subtypes as they inherit the invariants
3020 -- from their base types.
3021
3022 Par_Typ := Base_Type (Etype (Curr_Typ));
3023
3024 -- Stop the climb once the root of the parent chain is
3025 -- reached.
3026
3027 exit when Curr_Typ = Par_Typ;
3028
3029 -- Process the class-wide invariants of the parent type
3030
3031 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
3032
3033 -- Process the elements of an array type
3034
3035 if Is_Array_Type (Full_Typ) then
3036 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
3037
3038 -- Process the components of a record type
3039
3040 elsif Ekind (Full_Typ) = E_Record_Type then
3041 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
3042 end if;
3043
3044 Add_Inherited_Invariants
3045 (T => T,
3046 Priv_Typ => Priv_Typ,
3047 Full_Typ => Full_Typ,
3048 Obj_Id => Obj_Id,
3049 Checks => Checks);
3050
3051 Curr_Typ := Par_Typ;
3052 end loop;
3053 end Add_Parent_Invariants;
3054
3055 ------------------------
3056 -- Add_Own_Invariants --
3057 ------------------------
3058
3059 procedure Add_Own_Invariants
3060 (T : Entity_Id;
3061 Obj_Id : Entity_Id;
3062 Checks : in out List_Id;
3063 Priv_Item : Node_Id := Empty)
3064 is
3065 Expr : Node_Id;
3066 Prag : Node_Id;
3067 Prag_Asp : Node_Id;
3068 Prag_Expr : Node_Id;
3069 Prag_Expr_Arg : Node_Id;
3070 Prag_Typ : Node_Id;
3071 Prag_Typ_Arg : Node_Id;
3072
3073 begin
3074 if not Present (T) then
3075 return;
3076 end if;
3077
3078 Prag := First_Rep_Item (T);
3079 while Present (Prag) loop
3080 if Nkind (Prag) = N_Pragma
3081 and then Pragma_Name (Prag) = Name_Invariant
3082 then
3083 -- Stop the traversal of the rep item chain once a specific
3084 -- item is encountered.
3085
3086 if Present (Priv_Item) and then Prag = Priv_Item then
3087 exit;
3088 end if;
3089
3090 -- Nothing to do if the pragma was already processed
3091
3092 if Contains (Pragmas_Seen, Prag) then
3093 return;
3094 end if;
3095
3096 -- Extract the arguments of the invariant pragma
3097
3098 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
3099 Prag_Expr_Arg := Next (Prag_Typ_Arg);
3100 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
3101 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
3102 Prag_Asp := Corresponding_Aspect (Prag);
3103
3104 -- Verify the pragma belongs to T, otherwise the pragma applies
3105 -- to a parent type in which case it will be processed later by
3106 -- Add_Parent_Invariants or Add_Interface_Invariants.
3107
3108 if Entity (Prag_Typ) /= T then
3109 return;
3110 end if;
3111
3112 Expr := New_Copy_Tree (Prag_Expr);
3113
3114 -- Substitute all references to type T with references to the
3115 -- _object formal parameter.
3116
3117 Replace_Type_References (Expr, T, Obj_Id);
3118
3119 -- Preanalyze the invariant expression to detect errors and at
3120 -- the same time capture the visibility of the proper package
3121 -- part.
3122
3123 Set_Parent (Expr, Parent (Prag_Expr));
3124 Preanalyze_Assert_Expression (Expr, Any_Boolean);
3125
3126 -- Save a copy of the expression when T is tagged to detect
3127 -- errors and capture the visibility of the proper package part
3128 -- for the generation of inherited type invariants.
3129
3130 if Is_Tagged_Type (T) then
3131 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
3132 end if;
3133
3134 -- If the pragma comes from an aspect specification, replace
3135 -- the saved expression because all type references must be
3136 -- substituted for the call to Preanalyze_Spec_Expression in
3137 -- Check_Aspect_At_xxx routines.
3138
3139 if Present (Prag_Asp) then
3140 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
3141 end if;
3142
3143 Add_Invariant_Check (Prag, Expr, Checks);
3144 end if;
3145
3146 Next_Rep_Item (Prag);
3147 end loop;
3148 end Add_Own_Invariants;
3149
3150 -------------------------------------
3151 -- Add_Record_Component_Invariants --
3152 -------------------------------------
3153
3154 procedure Add_Record_Component_Invariants
3155 (T : Entity_Id;
3156 Obj_Id : Entity_Id;
3157 Checks : in out List_Id)
3158 is
3159 procedure Process_Component_List
3160 (Comp_List : Node_Id;
3161 CL_Checks : in out List_Id);
3162 -- Generate invariant checks for all record components found in
3163 -- component list Comp_List, including variant parts. All created
3164 -- checks are added to list CL_Checks.
3165
3166 procedure Process_Record_Component
3167 (Comp_Id : Entity_Id;
3168 Comp_Checks : in out List_Id);
3169 -- Generate an invariant check for a record component identified by
3170 -- Comp_Id. All created checks are added to list Comp_Checks.
3171
3172 ----------------------------
3173 -- Process_Component_List --
3174 ----------------------------
3175
3176 procedure Process_Component_List
3177 (Comp_List : Node_Id;
3178 CL_Checks : in out List_Id)
3179 is
3180 Comp : Node_Id;
3181 Var : Node_Id;
3182 Var_Alts : List_Id := No_List;
3183 Var_Checks : List_Id := No_List;
3184 Var_Stmts : List_Id;
3185
3186 Produced_Variant_Check : Boolean := False;
3187 -- This flag tracks whether the component has produced at least
3188 -- one invariant check.
3189
3190 begin
3191 -- Traverse the component items
3192
3193 Comp := First (Component_Items (Comp_List));
3194 while Present (Comp) loop
3195 if Nkind (Comp) = N_Component_Declaration then
3196
3197 -- Generate the component invariant check
3198
3199 Process_Record_Component
3200 (Comp_Id => Defining_Entity (Comp),
3201 Comp_Checks => CL_Checks);
3202 end if;
3203
3204 Next (Comp);
3205 end loop;
3206
3207 -- Traverse the variant part
3208
3209 if Present (Variant_Part (Comp_List)) then
3210 Var := First (Variants (Variant_Part (Comp_List)));
3211 while Present (Var) loop
3212 Var_Checks := No_List;
3213
3214 -- Generate invariant checks for all components and variant
3215 -- parts that qualify.
3216
3217 Process_Component_List
3218 (Comp_List => Component_List (Var),
3219 CL_Checks => Var_Checks);
3220
3221 -- The components of the current variant produced at least
3222 -- one invariant check.
3223
3224 if Present (Var_Checks) then
3225 Var_Stmts := Var_Checks;
3226 Produced_Variant_Check := True;
3227
3228 -- Otherwise there are either no components with invariants,
3229 -- assertions are disabled, or Assertion_Policy Ignore is in
3230 -- effect.
3231
3232 else
3233 Var_Stmts := New_List (Make_Null_Statement (Loc));
3234 end if;
3235
3236 Append_New_To (Var_Alts,
3237 Make_Case_Statement_Alternative (Loc,
3238 Discrete_Choices =>
3239 New_Copy_List (Discrete_Choices (Var)),
3240 Statements => Var_Stmts));
3241
3242 Next (Var);
3243 end loop;
3244
3245 -- Create a case statement which verifies the invariant checks
3246 -- of a particular component list depending on the discriminant
3247 -- values only when there is at least one real invariant check.
3248
3249 if Produced_Variant_Check then
3250 Append_New_To (CL_Checks,
3251 Make_Case_Statement (Loc,
3252 Expression =>
3253 Make_Selected_Component (Loc,
3254 Prefix => New_Occurrence_Of (Obj_Id, Loc),
3255 Selector_Name =>
3256 New_Occurrence_Of
3257 (Entity (Name (Variant_Part (Comp_List))), Loc)),
3258 Alternatives => Var_Alts));
3259 end if;
3260 end if;
3261 end Process_Component_List;
3262
3263 ------------------------------
3264 -- Process_Record_Component --
3265 ------------------------------
3266
3267 procedure Process_Record_Component
3268 (Comp_Id : Entity_Id;
3269 Comp_Checks : in out List_Id)
3270 is
3271 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
3272 Proc_Id : Entity_Id;
3273
3274 Produced_Component_Check : Boolean := False;
3275 -- This flag tracks whether the component has produced at least
3276 -- one invariant check.
3277
3278 begin
3279 -- Nothing to do for internal component _parent. Note that it is
3280 -- not desirable to check whether the component comes from source
3281 -- because protected type components are relocated to an internal
3282 -- corresponding record, but still need processing.
3283
3284 if Chars (Comp_Id) = Name_uParent then
3285 return;
3286 end if;
3287
3288 -- Verify the invariant of the component. Note that an access
3289 -- type may have an invariant when it acts as the full view of a
3290 -- private type and the invariant appears on the partial view. In
3291 -- this case verify the access value itself.
3292
3293 if Has_Invariants (Comp_Typ) then
3294
3295 -- In GNATprove mode, the component invariants are checked by
3296 -- other means. They should not be added to the record type
3297 -- invariant procedure, so that the procedure can be used to
3298 -- check the record type invariants if any.
3299
3300 if GNATprove_Mode then
3301 null;
3302
3303 else
3304 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
3305
3306 -- The component type should have an invariant procedure
3307 -- if it has invariants of its own or inherits class-wide
3308 -- invariants from parent or interface types.
3309
3310 pragma Assert (Present (Proc_Id));
3311
3312 -- Generate:
3313 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
3314
3315 -- Note that the invariant procedure may have a null body if
3316 -- assertions are disabled or Assertion_Policy Ignore is in
3317 -- effect.
3318
3319 if not Has_Null_Body (Proc_Id) then
3320 Append_New_To (Comp_Checks,
3321 Make_Procedure_Call_Statement (Loc,
3322 Name =>
3323 New_Occurrence_Of (Proc_Id, Loc),
3324 Parameter_Associations => New_List (
3325 Make_Selected_Component (Loc,
3326 Prefix =>
3327 Unchecked_Convert_To
3328 (T, New_Occurrence_Of (Obj_Id, Loc)),
3329 Selector_Name =>
3330 New_Occurrence_Of (Comp_Id, Loc)))));
3331 end if;
3332 end if;
3333
3334 Produced_Check := True;
3335 Produced_Component_Check := True;
3336 end if;
3337
3338 if Produced_Component_Check and then Has_Unchecked_Union (T) then
3339 Error_Msg_NE
3340 ("invariants cannot be checked on components of "
3341 & "unchecked_union type &??", Comp_Id, T);
3342 end if;
3343 end Process_Record_Component;
3344
3345 -- Local variables
3346
3347 Comps : Node_Id;
3348 Def : Node_Id;
3349
3350 -- Start of processing for Add_Record_Component_Invariants
3351
3352 begin
3353 -- An untagged derived type inherits the components of its parent
3354 -- type. In order to avoid creating redundant invariant checks, do
3355 -- not process the components now. Instead wait until the ultimate
3356 -- parent of the untagged derivation chain is reached.
3357
3358 if not Is_Untagged_Derivation (T) then
3359 Def := Type_Definition (Parent (T));
3360
3361 if Nkind (Def) = N_Derived_Type_Definition then
3362 Def := Record_Extension_Part (Def);
3363 end if;
3364
3365 pragma Assert (Nkind (Def) = N_Record_Definition);
3366 Comps := Component_List (Def);
3367
3368 if Present (Comps) then
3369 Process_Component_List
3370 (Comp_List => Comps,
3371 CL_Checks => Checks);
3372 end if;
3373 end if;
3374 end Add_Record_Component_Invariants;
3375
3376 -- Local variables
3377
3378 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3379 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3380 -- Save the Ghost-related attributes to restore on exit
3381
3382 Dummy : Entity_Id;
3383 Priv_Item : Node_Id;
3384 Proc_Body : Node_Id;
3385 Proc_Body_Id : Entity_Id;
3386 Proc_Decl : Node_Id;
3387 Proc_Id : Entity_Id;
3388 Stmts : List_Id := No_List;
3389
3390 CRec_Typ : Entity_Id := Empty;
3391 -- The corresponding record type of Full_Typ
3392
3393 Full_Proc : Entity_Id := Empty;
3394 -- The entity of the "full" invariant procedure
3395
3396 Full_Typ : Entity_Id := Empty;
3397 -- The full view of the working type
3398
3399 Obj_Id : Entity_Id := Empty;
3400 -- The _object formal parameter of the invariant procedure
3401
3402 Part_Proc : Entity_Id := Empty;
3403 -- The entity of the "partial" invariant procedure
3404
3405 Priv_Typ : Entity_Id := Empty;
3406 -- The partial view of the working type
3407
3408 Work_Typ : Entity_Id := Empty;
3409 -- The working type
3410
3411 -- Start of processing for Build_Invariant_Procedure_Body
3412
3413 begin
3414 Work_Typ := Typ;
3415
3416 -- Do not process the underlying full view of a private type. There is
3417 -- no way to get back to the partial view, plus the body will be built
3418 -- by the full view or the base type.
3419
3420 if Is_Underlying_Full_View (Work_Typ) then
3421 return;
3422
3423 -- The input type denotes the implementation base type of a constrained
3424 -- array type. Work with the first subtype as all invariant pragmas are
3425 -- on its rep item chain.
3426
3427 elsif Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3428 Work_Typ := First_Subtype (Work_Typ);
3429
3430 -- The input type denotes the corresponding record type of a protected
3431 -- or task type. Work with the concurrent type because the corresponding
3432 -- record type may not be visible to clients of the type.
3433
3434 elsif Ekind (Work_Typ) = E_Record_Type
3435 and then Is_Concurrent_Record_Type (Work_Typ)
3436 then
3437 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3438 end if;
3439
3440 -- The working type may be subject to pragma Ghost. Set the mode now to
3441 -- ensure that the invariant procedure is properly marked as Ghost.
3442
3443 Set_Ghost_Mode (Work_Typ);
3444
3445 -- The type must either have invariants of its own, inherit class-wide
3446 -- invariants from parent types or interfaces, or be an array or record
3447 -- type whose components have invariants.
3448
3449 pragma Assert (Has_Invariants (Work_Typ));
3450
3451 -- Interfaces are treated as the partial view of a private type in order
3452 -- to achieve uniformity with the general case.
3453
3454 if Is_Interface (Work_Typ) then
3455 Priv_Typ := Work_Typ;
3456
3457 -- Otherwise obtain both views of the type
3458
3459 else
3460 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3461 end if;
3462
3463 -- The caller requests a body for the partial invariant procedure
3464
3465 if Partial_Invariant then
3466 Full_Proc := Invariant_Procedure (Work_Typ);
3467 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3468
3469 -- The "full" invariant procedure body was already created
3470
3471 if Present (Full_Proc)
3472 and then Present
3473 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3474 then
3475 -- This scenario happens only when the type is an untagged
3476 -- derivation from a private parent and the underlying full
3477 -- view was processed before the partial view.
3478
3479 pragma Assert
3480 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3481
3482 -- Nothing to do because the processing of the underlying full
3483 -- view already checked the invariants of the partial view.
3484
3485 goto Leave;
3486 end if;
3487
3488 -- Create a declaration for the "partial" invariant procedure if it
3489 -- is not available.
3490
3491 if No (Proc_Id) then
3492 Build_Invariant_Procedure_Declaration
3493 (Typ => Work_Typ,
3494 Partial_Invariant => True);
3495
3496 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3497 end if;
3498
3499 -- The caller requests a body for the "full" invariant procedure
3500
3501 else
3502 Proc_Id := Invariant_Procedure (Work_Typ);
3503 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3504
3505 -- Create a declaration for the "full" invariant procedure if it is
3506 -- not available.
3507
3508 if No (Proc_Id) then
3509 Build_Invariant_Procedure_Declaration (Work_Typ);
3510 Proc_Id := Invariant_Procedure (Work_Typ);
3511 end if;
3512 end if;
3513
3514 -- At this point there should be an invariant procedure declaration
3515
3516 pragma Assert (Present (Proc_Id));
3517 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3518
3519 -- Nothing to do if the invariant procedure already has a body
3520
3521 if Present (Corresponding_Body (Proc_Decl)) then
3522 goto Leave;
3523 end if;
3524
3525 -- Emulate the environment of the invariant procedure by installing its
3526 -- scope and formal parameters. Note that this is not needed, but having
3527 -- the scope installed helps with the detection of invariant-related
3528 -- errors.
3529
3530 Push_Scope (Proc_Id);
3531 Install_Formals (Proc_Id);
3532
3533 Obj_Id := First_Formal (Proc_Id);
3534 pragma Assert (Present (Obj_Id));
3535
3536 -- The "partial" invariant procedure verifies the invariants of the
3537 -- partial view only.
3538
3539 if Partial_Invariant then
3540 pragma Assert (Present (Priv_Typ));
3541
3542 Add_Own_Invariants
3543 (T => Priv_Typ,
3544 Obj_Id => Obj_Id,
3545 Checks => Stmts);
3546
3547 -- Otherwise the "full" invariant procedure verifies the invariants of
3548 -- the full view, all array or record components, as well as class-wide
3549 -- invariants inherited from parent types or interfaces. In addition, it
3550 -- indirectly verifies the invariants of the partial view by calling the
3551 -- "partial" invariant procedure.
3552
3553 else
3554 pragma Assert (Present (Full_Typ));
3555
3556 -- Check the invariants of the partial view by calling the "partial"
3557 -- invariant procedure. Generate:
3558
3559 -- <Work_Typ>Partial_Invariant (_object);
3560
3561 if Present (Part_Proc) then
3562 Append_New_To (Stmts,
3563 Make_Procedure_Call_Statement (Loc,
3564 Name => New_Occurrence_Of (Part_Proc, Loc),
3565 Parameter_Associations => New_List (
3566 New_Occurrence_Of (Obj_Id, Loc))));
3567
3568 Produced_Check := True;
3569 end if;
3570
3571 Priv_Item := Empty;
3572
3573 -- Derived subtypes do not have a partial view
3574
3575 if Present (Priv_Typ) then
3576
3577 -- The processing of the "full" invariant procedure intentionally
3578 -- skips the partial view because a) this may result in changes of
3579 -- visibility and b) lead to duplicate checks. However, when the
3580 -- full view is the underlying full view of an untagged derived
3581 -- type whose parent type is private, partial invariants appear on
3582 -- the rep item chain of the partial view only.
3583
3584 -- package Pack_1 is
3585 -- type Root ... is private;
3586 -- private
3587 -- <full view of Root>
3588 -- end Pack_1;
3589
3590 -- with Pack_1;
3591 -- package Pack_2 is
3592 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3593 -- <underlying full view of Child>
3594 -- end Pack_2;
3595
3596 -- As a result, the processing of the full view must also consider
3597 -- all invariants of the partial view.
3598
3599 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3600 null;
3601
3602 -- Otherwise the invariants of the partial view are ignored
3603
3604 else
3605 -- Note that the rep item chain is shared between the partial
3606 -- and full views of a type. To avoid processing the invariants
3607 -- of the partial view, signal the logic to stop when the first
3608 -- rep item of the partial view has been reached.
3609
3610 Priv_Item := First_Rep_Item (Priv_Typ);
3611
3612 -- Ignore the invariants of the partial view by eliminating the
3613 -- view.
3614
3615 Priv_Typ := Empty;
3616 end if;
3617 end if;
3618
3619 -- Process the invariants of the full view and in certain cases those
3620 -- of the partial view. This also handles any invariants on array or
3621 -- record components.
3622
3623 Add_Own_Invariants
3624 (T => Priv_Typ,
3625 Obj_Id => Obj_Id,
3626 Checks => Stmts,
3627 Priv_Item => Priv_Item);
3628
3629 Add_Own_Invariants
3630 (T => Full_Typ,
3631 Obj_Id => Obj_Id,
3632 Checks => Stmts,
3633 Priv_Item => Priv_Item);
3634
3635 -- Process the elements of an array type
3636
3637 if Is_Array_Type (Full_Typ) then
3638 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3639
3640 -- Process the components of a record type
3641
3642 elsif Ekind (Full_Typ) = E_Record_Type then
3643 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3644
3645 -- Process the components of a corresponding record
3646
3647 elsif Present (CRec_Typ) then
3648 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3649 end if;
3650
3651 -- Process the inherited class-wide invariants of all parent types.
3652 -- This also handles any invariants on record components.
3653
3654 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3655
3656 -- Process the inherited class-wide invariants of all implemented
3657 -- interface types.
3658
3659 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3660 end if;
3661
3662 End_Scope;
3663
3664 -- At this point there should be at least one invariant check. If this
3665 -- is not the case, then the invariant-related flags were not properly
3666 -- set, or there is a missing invariant procedure on one of the array
3667 -- or record components.
3668
3669 pragma Assert (Produced_Check);
3670
3671 -- Account for the case where assertions are disabled or all invariant
3672 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3673 -- empty body.
3674
3675 if No (Stmts) then
3676 Stmts := New_List (Make_Null_Statement (Loc));
3677 end if;
3678
3679 -- Generate:
3680 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3681 -- begin
3682 -- <Stmts>
3683 -- end <Work_Typ>[Partial_]Invariant;
3684
3685 Proc_Body :=
3686 Make_Subprogram_Body (Loc,
3687 Specification =>
3688 Copy_Subprogram_Spec (Parent (Proc_Id)),
3689 Declarations => Empty_List,
3690 Handled_Statement_Sequence =>
3691 Make_Handled_Sequence_Of_Statements (Loc,
3692 Statements => Stmts));
3693 Proc_Body_Id := Defining_Entity (Proc_Body);
3694
3695 -- Perform minor decoration in case the body is not analyzed
3696
3697 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
3698 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3699 Set_Scope (Proc_Body_Id, Current_Scope);
3700
3701 -- Link both spec and body to avoid generating duplicates
3702
3703 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3704 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3705
3706 -- The body should not be inserted into the tree when the context is
3707 -- a generic unit because it is not part of the template. Note
3708 -- that the body must still be generated in order to resolve the
3709 -- invariants.
3710
3711 if Inside_A_Generic then
3712 null;
3713
3714 -- Semi-insert the body into the tree for GNATprove by setting its
3715 -- Parent field. This allows for proper upstream tree traversals.
3716
3717 elsif GNATprove_Mode then
3718 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3719
3720 -- Otherwise the body is part of the freezing actions of the type
3721
3722 else
3723 Append_Freeze_Action (Work_Typ, Proc_Body);
3724 end if;
3725
3726 <<Leave>>
3727 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3728 end Build_Invariant_Procedure_Body;
3729
3730 -------------------------------------------
3731 -- Build_Invariant_Procedure_Declaration --
3732 -------------------------------------------
3733
3734 -- WARNING: This routine manages Ghost regions. Return statements must be
3735 -- replaced by gotos which jump to the end of the routine and restore the
3736 -- Ghost mode.
3737
3738 procedure Build_Invariant_Procedure_Declaration
3739 (Typ : Entity_Id;
3740 Partial_Invariant : Boolean := False)
3741 is
3742 Loc : constant Source_Ptr := Sloc (Typ);
3743
3744 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3745 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3746 -- Save the Ghost-related attributes to restore on exit
3747
3748 Proc_Decl : Node_Id;
3749 Proc_Id : Entity_Id;
3750 Proc_Nam : Name_Id;
3751 Typ_Decl : Node_Id;
3752
3753 CRec_Typ : Entity_Id;
3754 -- The corresponding record type of Full_Typ
3755
3756 Full_Typ : Entity_Id;
3757 -- The full view of working type
3758
3759 Obj_Id : Entity_Id;
3760 -- The _object formal parameter of the invariant procedure
3761
3762 Obj_Typ : Entity_Id;
3763 -- The type of the _object formal parameter
3764
3765 Priv_Typ : Entity_Id;
3766 -- The partial view of working type
3767
3768 UFull_Typ : Entity_Id;
3769 -- The underlying full view of Full_Typ
3770
3771 Work_Typ : Entity_Id;
3772 -- The working type
3773
3774 begin
3775 Work_Typ := Typ;
3776
3777 -- The input type denotes the implementation base type of a constrained
3778 -- array type. Work with the first subtype as all invariant pragmas are
3779 -- on its rep item chain.
3780
3781 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3782 Work_Typ := First_Subtype (Work_Typ);
3783
3784 -- The input denotes the corresponding record type of a protected or a
3785 -- task type. Work with the concurrent type because the corresponding
3786 -- record type may not be visible to clients of the type.
3787
3788 elsif Ekind (Work_Typ) = E_Record_Type
3789 and then Is_Concurrent_Record_Type (Work_Typ)
3790 then
3791 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3792 end if;
3793
3794 -- The working type may be subject to pragma Ghost. Set the mode now to
3795 -- ensure that the invariant procedure is properly marked as Ghost.
3796
3797 Set_Ghost_Mode (Work_Typ);
3798
3799 -- The type must either have invariants of its own, inherit class-wide
3800 -- invariants from parent or interface types, or be an array or record
3801 -- type whose components have invariants.
3802
3803 pragma Assert (Has_Invariants (Work_Typ));
3804
3805 -- Nothing to do if the type already has a "partial" invariant procedure
3806
3807 if Partial_Invariant then
3808 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3809 goto Leave;
3810 end if;
3811
3812 -- Nothing to do if the type already has a "full" invariant procedure
3813
3814 elsif Present (Invariant_Procedure (Work_Typ)) then
3815 goto Leave;
3816 end if;
3817
3818 -- The caller requests the declaration of the "partial" invariant
3819 -- procedure.
3820
3821 if Partial_Invariant then
3822 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3823
3824 -- Otherwise the caller requests the declaration of the "full" invariant
3825 -- procedure.
3826
3827 else
3828 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3829 end if;
3830
3831 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3832
3833 -- Perform minor decoration in case the declaration is not analyzed
3834
3835 Mutate_Ekind (Proc_Id, E_Procedure);
3836 Set_Etype (Proc_Id, Standard_Void_Type);
3837 Set_Scope (Proc_Id, Current_Scope);
3838
3839 if Partial_Invariant then
3840 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3841 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3842 else
3843 Set_Is_Invariant_Procedure (Proc_Id);
3844 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3845 end if;
3846
3847 -- The invariant procedure requires debug info when the invariants are
3848 -- subject to Source Coverage Obligations.
3849
3850 if Generate_SCO then
3851 Set_Debug_Info_Needed (Proc_Id);
3852 end if;
3853
3854 -- Obtain all views of the input type
3855
3856 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
3857
3858 -- Associate the invariant procedure and various flags with all views
3859
3860 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3861 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3862 Propagate_Invariant_Attributes (UFull_Typ, From_Typ => Work_Typ);
3863 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3864
3865 -- The declaration of the invariant procedure is inserted after the
3866 -- declaration of the partial view as this allows for proper external
3867 -- visibility.
3868
3869 if Present (Priv_Typ) then
3870 Typ_Decl := Declaration_Node (Priv_Typ);
3871
3872 -- Anonymous arrays in object declarations have no explicit declaration
3873 -- so use the related object declaration as the insertion point.
3874
3875 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3876 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3877
3878 -- Derived types with the full view as parent do not have a partial
3879 -- view. Insert the invariant procedure after the derived type.
3880
3881 else
3882 Typ_Decl := Declaration_Node (Full_Typ);
3883 end if;
3884
3885 -- The type should have a declarative node
3886
3887 pragma Assert (Present (Typ_Decl));
3888
3889 -- Create the formal parameter which emulates the variable-like behavior
3890 -- of the current type instance.
3891
3892 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3893
3894 -- When generating an invariant procedure declaration for an abstract
3895 -- type (including interfaces), use the class-wide type as the _object
3896 -- type. This has several desirable effects:
3897
3898 -- * The invariant procedure does not become a primitive of the type.
3899 -- This eliminates the need to either special case the treatment of
3900 -- invariant procedures, or to make it a predefined primitive and
3901 -- force every derived type to potentially provide an empty body.
3902
3903 -- * The invariant procedure does not need to be declared as abstract.
3904 -- This allows for a proper body, which in turn avoids redundant
3905 -- processing of the same invariants for types with multiple views.
3906
3907 -- * The class-wide type allows for calls to abstract primitives
3908 -- within a nonabstract subprogram. The calls are treated as
3909 -- dispatching and require additional processing when they are
3910 -- remapped to call primitives of derived types. See routine
3911 -- Replace_References for details.
3912
3913 if Is_Abstract_Type (Work_Typ) then
3914 Obj_Typ := Class_Wide_Type (Work_Typ);
3915 else
3916 Obj_Typ := Work_Typ;
3917 end if;
3918
3919 -- Perform minor decoration in case the declaration is not analyzed
3920
3921 Mutate_Ekind (Obj_Id, E_In_Parameter);
3922 Set_Etype (Obj_Id, Obj_Typ);
3923 Set_Scope (Obj_Id, Proc_Id);
3924
3925 Set_First_Entity (Proc_Id, Obj_Id);
3926 Set_Last_Entity (Proc_Id, Obj_Id);
3927
3928 -- Generate:
3929 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3930
3931 Proc_Decl :=
3932 Make_Subprogram_Declaration (Loc,
3933 Specification =>
3934 Make_Procedure_Specification (Loc,
3935 Defining_Unit_Name => Proc_Id,
3936 Parameter_Specifications => New_List (
3937 Make_Parameter_Specification (Loc,
3938 Defining_Identifier => Obj_Id,
3939 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3940
3941 -- The declaration should not be inserted into the tree when the context
3942 -- is a generic unit because it is not part of the template.
3943
3944 if Inside_A_Generic then
3945 null;
3946
3947 -- Semi-insert the declaration into the tree for GNATprove by setting
3948 -- its Parent field. This allows for proper upstream tree traversals.
3949
3950 elsif GNATprove_Mode then
3951 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3952
3953 -- Otherwise insert the declaration
3954
3955 else
3956 pragma Assert (Present (Typ_Decl));
3957 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3958 end if;
3959
3960 <<Leave>>
3961 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3962 end Build_Invariant_Procedure_Declaration;
3963
3964 --------------------------
3965 -- Build_Procedure_Form --
3966 --------------------------
3967
3968 procedure Build_Procedure_Form (N : Node_Id) is
3969 Loc : constant Source_Ptr := Sloc (N);
3970 Subp : constant Entity_Id := Defining_Entity (N);
3971
3972 Func_Formal : Entity_Id;
3973 Proc_Formals : List_Id;
3974 Proc_Decl : Node_Id;
3975
3976 begin
3977 -- No action needed if this transformation was already done, or in case
3978 -- of subprogram renaming declarations.
3979
3980 if Nkind (Specification (N)) = N_Procedure_Specification
3981 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3982 then
3983 return;
3984 end if;
3985
3986 -- Ditto when dealing with an expression function, where both the
3987 -- original expression and the generated declaration end up being
3988 -- expanded here.
3989
3990 if Rewritten_For_C (Subp) then
3991 return;
3992 end if;
3993
3994 Proc_Formals := New_List;
3995
3996 -- Create a list of formal parameters with the same types as the
3997 -- function.
3998
3999 Func_Formal := First_Formal (Subp);
4000 while Present (Func_Formal) loop
4001 Append_To (Proc_Formals,
4002 Make_Parameter_Specification (Loc,
4003 Defining_Identifier =>
4004 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
4005 Parameter_Type =>
4006 New_Occurrence_Of (Etype (Func_Formal), Loc)));
4007
4008 Next_Formal (Func_Formal);
4009 end loop;
4010
4011 -- Add an extra out parameter to carry the function result
4012
4013 Append_To (Proc_Formals,
4014 Make_Parameter_Specification (Loc,
4015 Defining_Identifier =>
4016 Make_Defining_Identifier (Loc, Name_UP_RESULT),
4017 Out_Present => True,
4018 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
4019
4020 -- The new procedure declaration is inserted before the function
4021 -- declaration. The processing in Build_Procedure_Body_Form relies on
4022 -- this order. Note that we insert before because in the case of a
4023 -- function body with no separate spec, we do not want to insert the
4024 -- new spec after the body which will later get rewritten.
4025
4026 Proc_Decl :=
4027 Make_Subprogram_Declaration (Loc,
4028 Specification =>
4029 Make_Procedure_Specification (Loc,
4030 Defining_Unit_Name =>
4031 Make_Defining_Identifier (Loc, Chars (Subp)),
4032 Parameter_Specifications => Proc_Formals));
4033
4034 Insert_Before_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
4035
4036 -- Entity of procedure must remain invisible so that it does not
4037 -- overload subsequent references to the original function.
4038
4039 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
4040
4041 -- Mark the function as having a procedure form and link the function
4042 -- and its internally built procedure.
4043
4044 Set_Rewritten_For_C (Subp);
4045 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
4046 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
4047 end Build_Procedure_Form;
4048
4049 ------------------------
4050 -- Build_Runtime_Call --
4051 ------------------------
4052
4053 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
4054 begin
4055 -- If entity is not available, we can skip making the call (this avoids
4056 -- junk duplicated error messages in a number of cases).
4057
4058 if not RTE_Available (RE) then
4059 return Make_Null_Statement (Loc);
4060 else
4061 return
4062 Make_Procedure_Call_Statement (Loc,
4063 Name => New_Occurrence_Of (RTE (RE), Loc));
4064 end if;
4065 end Build_Runtime_Call;
4066
4067 ------------------------
4068 -- Build_SS_Mark_Call --
4069 ------------------------
4070
4071 function Build_SS_Mark_Call
4072 (Loc : Source_Ptr;
4073 Mark : Entity_Id) return Node_Id
4074 is
4075 begin
4076 -- Generate:
4077 -- Mark : constant Mark_Id := SS_Mark;
4078
4079 return
4080 Make_Object_Declaration (Loc,
4081 Defining_Identifier => Mark,
4082 Constant_Present => True,
4083 Object_Definition =>
4084 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
4085 Expression =>
4086 Make_Function_Call (Loc,
4087 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
4088 end Build_SS_Mark_Call;
4089
4090 ---------------------------
4091 -- Build_SS_Release_Call --
4092 ---------------------------
4093
4094 function Build_SS_Release_Call
4095 (Loc : Source_Ptr;
4096 Mark : Entity_Id) return Node_Id
4097 is
4098 begin
4099 -- Generate:
4100 -- SS_Release (Mark);
4101
4102 return
4103 Make_Procedure_Call_Statement (Loc,
4104 Name =>
4105 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
4106 Parameter_Associations => New_List (
4107 New_Occurrence_Of (Mark, Loc)));
4108 end Build_SS_Release_Call;
4109
4110 ----------------------------
4111 -- Build_Task_Array_Image --
4112 ----------------------------
4113
4114 -- This function generates the body for a function that constructs the
4115 -- image string for a task that is an array component. The function is
4116 -- local to the init proc for the array type, and is called for each one
4117 -- of the components. The constructed image has the form of an indexed
4118 -- component, whose prefix is the outer variable of the array type.
4119 -- The n-dimensional array type has known indexes Index, Index2...
4120
4121 -- Id_Ref is an indexed component form created by the enclosing init proc.
4122 -- Its successive indexes are Val1, Val2, ... which are the loop variables
4123 -- in the loops that call the individual task init proc on each component.
4124
4125 -- The generated function has the following structure:
4126
4127 -- function F return String is
4128 -- Pref : String renames Task_Name;
4129 -- T1 : constant String := Index1'Image (Val1);
4130 -- ...
4131 -- Tn : constant String := Indexn'Image (Valn);
4132 -- Len : constant Integer :=
4133 -- Pref'Length + T1'Length + ... + Tn'Length + n + 1;
4134 -- -- Len includes commas and the end parentheses
4135 --
4136 -- Res : String (1 .. Len);
4137 -- Pos : Integer := Pref'Length;
4138 --
4139 -- begin
4140 -- Res (1 .. Pos) := Pref;
4141 -- Pos := Pos + 1;
4142 -- Res (Pos) := '(';
4143 -- Pos := Pos + 1;
4144 -- Res (Pos .. Pos + T1'Length - 1) := T1;
4145 -- Pos := Pos + T1'Length;
4146 -- Res (Pos) := '.';
4147 -- Pos := Pos + 1;
4148 -- ...
4149 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
4150 -- Res (Len) := ')';
4151 --
4152 -- return Res;
4153 -- end F;
4154 --
4155 -- Needless to say, multidimensional arrays of tasks are rare enough that
4156 -- the bulkiness of this code is not really a concern.
4157
4158 function Build_Task_Array_Image
4159 (Loc : Source_Ptr;
4160 Id_Ref : Node_Id;
4161 A_Type : Entity_Id;
4162 Dyn : Boolean := False) return Node_Id
4163 is
4164 Dims : constant Nat := Number_Dimensions (A_Type);
4165 -- Number of dimensions for array of tasks
4166
4167 Temps : array (1 .. Dims) of Entity_Id;
4168 -- Array of temporaries to hold string for each index
4169
4170 Indx : Node_Id;
4171 -- Index expression
4172
4173 Len : Entity_Id;
4174 -- Total length of generated name
4175
4176 Pos : Entity_Id;
4177 -- Running index for substring assignments
4178
4179 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4180 -- Name of enclosing variable, prefix of resulting name
4181
4182 Res : Entity_Id;
4183 -- String to hold result
4184
4185 Val : Node_Id;
4186 -- Value of successive indexes
4187
4188 Sum : Node_Id;
4189 -- Expression to compute total size of string
4190
4191 T : Entity_Id;
4192 -- Entity for name at one index position
4193
4194 Decls : constant List_Id := New_List;
4195 Stats : constant List_Id := New_List;
4196
4197 begin
4198 -- For a dynamic task, the name comes from the target variable. For a
4199 -- static one it is a formal of the enclosing init proc.
4200
4201 if Dyn then
4202 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4203 Append_To (Decls,
4204 Make_Object_Declaration (Loc,
4205 Defining_Identifier => Pref,
4206 Constant_Present => True,
4207 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4208 Expression =>
4209 Make_String_Literal (Loc,
4210 Strval => String_From_Name_Buffer)));
4211
4212 else
4213 Append_To (Decls,
4214 Make_Object_Renaming_Declaration (Loc,
4215 Defining_Identifier => Pref,
4216 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4217 Name => Make_Identifier (Loc, Name_uTask_Name)));
4218 end if;
4219
4220 Indx := First_Index (A_Type);
4221 Val := First (Expressions (Id_Ref));
4222
4223 for J in 1 .. Dims loop
4224 T := Make_Temporary (Loc, 'T');
4225 Temps (J) := T;
4226
4227 Append_To (Decls,
4228 Make_Object_Declaration (Loc,
4229 Defining_Identifier => T,
4230 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4231 Constant_Present => True,
4232 Expression =>
4233 Make_Attribute_Reference (Loc,
4234 Attribute_Name => Name_Image,
4235 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
4236 Expressions => New_List (New_Copy_Tree (Val)))));
4237
4238 Next_Index (Indx);
4239 Next (Val);
4240 end loop;
4241
4242 Sum := Make_Integer_Literal (Loc, Dims + 1);
4243
4244 Sum :=
4245 Make_Op_Add (Loc,
4246 Left_Opnd => Sum,
4247 Right_Opnd =>
4248 Make_Attribute_Reference (Loc,
4249 Attribute_Name => Name_Length,
4250 Prefix => New_Occurrence_Of (Pref, Loc),
4251 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4252
4253 for J in 1 .. Dims loop
4254 Sum :=
4255 Make_Op_Add (Loc,
4256 Left_Opnd => Sum,
4257 Right_Opnd =>
4258 Make_Attribute_Reference (Loc,
4259 Attribute_Name => Name_Length,
4260 Prefix =>
4261 New_Occurrence_Of (Temps (J), Loc),
4262 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4263 end loop;
4264
4265 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4266
4267 Set_Character_Literal_Name (Get_Char_Code ('('));
4268
4269 Append_To (Stats,
4270 Make_Assignment_Statement (Loc,
4271 Name =>
4272 Make_Indexed_Component (Loc,
4273 Prefix => New_Occurrence_Of (Res, Loc),
4274 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4275 Expression =>
4276 Make_Character_Literal (Loc,
4277 Chars => Name_Find,
4278 Char_Literal_Value => UI_From_CC (Get_Char_Code ('(')))));
4279
4280 Append_To (Stats,
4281 Make_Assignment_Statement (Loc,
4282 Name => New_Occurrence_Of (Pos, Loc),
4283 Expression =>
4284 Make_Op_Add (Loc,
4285 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4286 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4287
4288 for J in 1 .. Dims loop
4289
4290 Append_To (Stats,
4291 Make_Assignment_Statement (Loc,
4292 Name =>
4293 Make_Slice (Loc,
4294 Prefix => New_Occurrence_Of (Res, Loc),
4295 Discrete_Range =>
4296 Make_Range (Loc,
4297 Low_Bound => New_Occurrence_Of (Pos, Loc),
4298 High_Bound =>
4299 Make_Op_Subtract (Loc,
4300 Left_Opnd =>
4301 Make_Op_Add (Loc,
4302 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4303 Right_Opnd =>
4304 Make_Attribute_Reference (Loc,
4305 Attribute_Name => Name_Length,
4306 Prefix =>
4307 New_Occurrence_Of (Temps (J), Loc),
4308 Expressions =>
4309 New_List (Make_Integer_Literal (Loc, 1)))),
4310 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
4311
4312 Expression => New_Occurrence_Of (Temps (J), Loc)));
4313
4314 if J < Dims then
4315 Append_To (Stats,
4316 Make_Assignment_Statement (Loc,
4317 Name => New_Occurrence_Of (Pos, Loc),
4318 Expression =>
4319 Make_Op_Add (Loc,
4320 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4321 Right_Opnd =>
4322 Make_Attribute_Reference (Loc,
4323 Attribute_Name => Name_Length,
4324 Prefix => New_Occurrence_Of (Temps (J), Loc),
4325 Expressions =>
4326 New_List (Make_Integer_Literal (Loc, 1))))));
4327
4328 Set_Character_Literal_Name (Get_Char_Code (','));
4329
4330 Append_To (Stats,
4331 Make_Assignment_Statement (Loc,
4332 Name => Make_Indexed_Component (Loc,
4333 Prefix => New_Occurrence_Of (Res, Loc),
4334 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4335 Expression =>
4336 Make_Character_Literal (Loc,
4337 Chars => Name_Find,
4338 Char_Literal_Value => UI_From_CC (Get_Char_Code (',')))));
4339
4340 Append_To (Stats,
4341 Make_Assignment_Statement (Loc,
4342 Name => New_Occurrence_Of (Pos, Loc),
4343 Expression =>
4344 Make_Op_Add (Loc,
4345 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4346 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4347 end if;
4348 end loop;
4349
4350 Set_Character_Literal_Name (Get_Char_Code (')'));
4351
4352 Append_To (Stats,
4353 Make_Assignment_Statement (Loc,
4354 Name =>
4355 Make_Indexed_Component (Loc,
4356 Prefix => New_Occurrence_Of (Res, Loc),
4357 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
4358 Expression =>
4359 Make_Character_Literal (Loc,
4360 Chars => Name_Find,
4361 Char_Literal_Value => UI_From_CC (Get_Char_Code (')')))));
4362 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4363 end Build_Task_Array_Image;
4364
4365 ----------------------------
4366 -- Build_Task_Image_Decls --
4367 ----------------------------
4368
4369 function Build_Task_Image_Decls
4370 (Loc : Source_Ptr;
4371 Id_Ref : Node_Id;
4372 A_Type : Entity_Id;
4373 In_Init_Proc : Boolean := False) return List_Id
4374 is
4375 Decls : constant List_Id := New_List;
4376 T_Id : Entity_Id := Empty;
4377 Decl : Node_Id;
4378 Expr : Node_Id := Empty;
4379 Fun : Node_Id := Empty;
4380 Is_Dyn : constant Boolean :=
4381 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
4382 and then
4383 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
4384
4385 Component_Suffix_Index : constant Int :=
4386 (if In_Init_Proc then -1 else 0);
4387 -- If an init proc calls Build_Task_Image_Decls twice for its
4388 -- _Parent component (to split early/late initialization), we don't
4389 -- want two decls with the same name. Hence, the -1 suffix.
4390
4391 begin
4392 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
4393 -- generate a dummy declaration only.
4394
4395 if Restriction_Active (No_Implicit_Heap_Allocations)
4396 or else Global_Discard_Names
4397 then
4398 T_Id := Make_Temporary (Loc, 'J');
4399 Name_Len := 0;
4400
4401 return
4402 New_List (
4403 Make_Object_Declaration (Loc,
4404 Defining_Identifier => T_Id,
4405 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4406 Expression =>
4407 Make_String_Literal (Loc,
4408 Strval => String_From_Name_Buffer)));
4409
4410 else
4411 if Nkind (Id_Ref) = N_Identifier
4412 or else Nkind (Id_Ref) = N_Defining_Identifier
4413 then
4414 -- For a simple variable, the image of the task is built from
4415 -- the name of the variable. To avoid possible conflict with the
4416 -- anonymous type created for a single protected object, add a
4417 -- numeric suffix.
4418
4419 T_Id :=
4420 Make_Defining_Identifier (Loc,
4421 New_External_Name (Chars (Id_Ref), 'T', 1));
4422
4423 Get_Name_String (Chars (Id_Ref));
4424
4425 Expr :=
4426 Make_String_Literal (Loc,
4427 Strval => String_From_Name_Buffer);
4428
4429 elsif Nkind (Id_Ref) = N_Selected_Component then
4430 T_Id :=
4431 Make_Defining_Identifier (Loc,
4432 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T',
4433 Suffix_Index => Component_Suffix_Index));
4434 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
4435
4436 elsif Nkind (Id_Ref) = N_Indexed_Component then
4437 T_Id :=
4438 Make_Defining_Identifier (Loc,
4439 New_External_Name (Chars (A_Type), 'N'));
4440
4441 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
4442 end if;
4443 end if;
4444
4445 if Present (Fun) then
4446 Append (Fun, Decls);
4447 Expr := Make_Function_Call (Loc,
4448 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
4449
4450 if not In_Init_Proc then
4451 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4452 end if;
4453 end if;
4454
4455 Decl := Make_Object_Declaration (Loc,
4456 Defining_Identifier => T_Id,
4457 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4458 Constant_Present => True,
4459 Expression => Expr);
4460
4461 Append (Decl, Decls);
4462 return Decls;
4463 end Build_Task_Image_Decls;
4464
4465 -------------------------------
4466 -- Build_Task_Image_Function --
4467 -------------------------------
4468
4469 function Build_Task_Image_Function
4470 (Loc : Source_Ptr;
4471 Decls : List_Id;
4472 Stats : List_Id;
4473 Res : Entity_Id) return Node_Id
4474 is
4475 Spec : Node_Id;
4476
4477 begin
4478 Append_To (Stats,
4479 Make_Simple_Return_Statement (Loc,
4480 Expression => New_Occurrence_Of (Res, Loc)));
4481
4482 Spec := Make_Function_Specification (Loc,
4483 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4484 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4485
4486 -- Calls to 'Image use the secondary stack, which must be cleaned up
4487 -- after the task name is built.
4488
4489 return Make_Subprogram_Body (Loc,
4490 Specification => Spec,
4491 Declarations => Decls,
4492 Handled_Statement_Sequence =>
4493 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4494 end Build_Task_Image_Function;
4495
4496 -----------------------------
4497 -- Build_Task_Image_Prefix --
4498 -----------------------------
4499
4500 procedure Build_Task_Image_Prefix
4501 (Loc : Source_Ptr;
4502 Len : out Entity_Id;
4503 Res : out Entity_Id;
4504 Pos : out Entity_Id;
4505 Prefix : Entity_Id;
4506 Sum : Node_Id;
4507 Decls : List_Id;
4508 Stats : List_Id)
4509 is
4510 begin
4511 Len := Make_Temporary (Loc, 'L', Sum);
4512
4513 Append_To (Decls,
4514 Make_Object_Declaration (Loc,
4515 Defining_Identifier => Len,
4516 Constant_Present => True,
4517 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4518 Expression => Sum));
4519
4520 Res := Make_Temporary (Loc, 'R');
4521
4522 Append_To (Decls,
4523 Make_Object_Declaration (Loc,
4524 Defining_Identifier => Res,
4525 Object_Definition =>
4526 Make_Subtype_Indication (Loc,
4527 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4528 Constraint =>
4529 Make_Index_Or_Discriminant_Constraint (Loc,
4530 Constraints =>
4531 New_List (
4532 Make_Range (Loc,
4533 Low_Bound => Make_Integer_Literal (Loc, 1),
4534 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4535
4536 -- Indicate that the result is an internal temporary, so it does not
4537 -- receive a bogus initialization when declaration is expanded. This
4538 -- is both efficient, and prevents anomalies in the handling of
4539 -- dynamic objects on the secondary stack.
4540
4541 Set_Is_Internal (Res);
4542 Pos := Make_Temporary (Loc, 'P');
4543
4544 Append_To (Decls,
4545 Make_Object_Declaration (Loc,
4546 Defining_Identifier => Pos,
4547 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4548
4549 -- Pos := Prefix'Length;
4550
4551 Append_To (Stats,
4552 Make_Assignment_Statement (Loc,
4553 Name => New_Occurrence_Of (Pos, Loc),
4554 Expression =>
4555 Make_Attribute_Reference (Loc,
4556 Attribute_Name => Name_Length,
4557 Prefix => New_Occurrence_Of (Prefix, Loc),
4558 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4559
4560 -- Res (1 .. Pos) := Prefix;
4561
4562 Append_To (Stats,
4563 Make_Assignment_Statement (Loc,
4564 Name =>
4565 Make_Slice (Loc,
4566 Prefix => New_Occurrence_Of (Res, Loc),
4567 Discrete_Range =>
4568 Make_Range (Loc,
4569 Low_Bound => Make_Integer_Literal (Loc, 1),
4570 High_Bound => New_Occurrence_Of (Pos, Loc))),
4571
4572 Expression => New_Occurrence_Of (Prefix, Loc)));
4573
4574 Append_To (Stats,
4575 Make_Assignment_Statement (Loc,
4576 Name => New_Occurrence_Of (Pos, Loc),
4577 Expression =>
4578 Make_Op_Add (Loc,
4579 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4580 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4581 end Build_Task_Image_Prefix;
4582
4583 -----------------------------
4584 -- Build_Task_Record_Image --
4585 -----------------------------
4586
4587 function Build_Task_Record_Image
4588 (Loc : Source_Ptr;
4589 Id_Ref : Node_Id;
4590 Dyn : Boolean := False) return Node_Id
4591 is
4592 Len : Entity_Id;
4593 -- Total length of generated name
4594
4595 Pos : Entity_Id;
4596 -- Index into result
4597
4598 Res : Entity_Id;
4599 -- String to hold result
4600
4601 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4602 -- Name of enclosing variable, prefix of resulting name
4603
4604 Sum : Node_Id;
4605 -- Expression to compute total size of string
4606
4607 Sel : Entity_Id;
4608 -- Entity for selector name
4609
4610 Decls : constant List_Id := New_List;
4611 Stats : constant List_Id := New_List;
4612
4613 begin
4614 -- For a dynamic task, the name comes from the target variable. For a
4615 -- static one it is a formal of the enclosing init proc.
4616
4617 if Dyn then
4618 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4619 Append_To (Decls,
4620 Make_Object_Declaration (Loc,
4621 Defining_Identifier => Pref,
4622 Constant_Present => True,
4623 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4624 Expression =>
4625 Make_String_Literal (Loc,
4626 Strval => String_From_Name_Buffer)));
4627
4628 else
4629 Append_To (Decls,
4630 Make_Object_Renaming_Declaration (Loc,
4631 Defining_Identifier => Pref,
4632 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4633 Name => Make_Identifier (Loc, Name_uTask_Name)));
4634 end if;
4635
4636 Sel := Make_Temporary (Loc, 'S');
4637
4638 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4639
4640 Append_To (Decls,
4641 Make_Object_Declaration (Loc,
4642 Defining_Identifier => Sel,
4643 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4644 Expression =>
4645 Make_String_Literal (Loc,
4646 Strval => String_From_Name_Buffer)));
4647
4648 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4649
4650 Sum :=
4651 Make_Op_Add (Loc,
4652 Left_Opnd => Sum,
4653 Right_Opnd =>
4654 Make_Attribute_Reference (Loc,
4655 Attribute_Name => Name_Length,
4656 Prefix =>
4657 New_Occurrence_Of (Pref, Loc),
4658 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4659
4660 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4661
4662 Set_Character_Literal_Name (Get_Char_Code ('.'));
4663
4664 -- Res (Pos) := '.';
4665
4666 Append_To (Stats,
4667 Make_Assignment_Statement (Loc,
4668 Name => Make_Indexed_Component (Loc,
4669 Prefix => New_Occurrence_Of (Res, Loc),
4670 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4671 Expression =>
4672 Make_Character_Literal (Loc,
4673 Chars => Name_Find,
4674 Char_Literal_Value =>
4675 UI_From_CC (Get_Char_Code ('.')))));
4676
4677 Append_To (Stats,
4678 Make_Assignment_Statement (Loc,
4679 Name => New_Occurrence_Of (Pos, Loc),
4680 Expression =>
4681 Make_Op_Add (Loc,
4682 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4683 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4684
4685 -- Res (Pos .. Len) := Selector;
4686
4687 Append_To (Stats,
4688 Make_Assignment_Statement (Loc,
4689 Name => Make_Slice (Loc,
4690 Prefix => New_Occurrence_Of (Res, Loc),
4691 Discrete_Range =>
4692 Make_Range (Loc,
4693 Low_Bound => New_Occurrence_Of (Pos, Loc),
4694 High_Bound => New_Occurrence_Of (Len, Loc))),
4695 Expression => New_Occurrence_Of (Sel, Loc)));
4696
4697 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4698 end Build_Task_Record_Image;
4699
4700 ---------------------------------------
4701 -- Build_Transient_Object_Statements --
4702 ---------------------------------------
4703
4704 procedure Build_Transient_Object_Statements
4705 (Obj_Decl : Node_Id;
4706 Fin_Call : out Node_Id;
4707 Hook_Assign : out Node_Id;
4708 Hook_Clear : out Node_Id;
4709 Hook_Decl : out Node_Id;
4710 Ptr_Decl : out Node_Id;
4711 Finalize_Obj : Boolean := True)
4712 is
4713 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4714 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4715 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4716
4717 Desig_Typ : Entity_Id;
4718 Hook_Expr : Node_Id;
4719 Hook_Id : Entity_Id;
4720 Obj_Ref : Node_Id;
4721 Ptr_Typ : Entity_Id;
4722
4723 begin
4724 -- Recover the type of the object
4725
4726 Desig_Typ := Obj_Typ;
4727
4728 if Is_Access_Type (Desig_Typ) then
4729 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4730 end if;
4731
4732 -- Create an access type which provides a reference to the transient
4733 -- object. Generate:
4734
4735 -- type Ptr_Typ is access all Desig_Typ;
4736
4737 Ptr_Typ := Make_Temporary (Loc, 'A');
4738 Mutate_Ekind (Ptr_Typ, E_General_Access_Type);
4739 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4740
4741 Ptr_Decl :=
4742 Make_Full_Type_Declaration (Loc,
4743 Defining_Identifier => Ptr_Typ,
4744 Type_Definition =>
4745 Make_Access_To_Object_Definition (Loc,
4746 All_Present => True,
4747 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4748
4749 -- Create a temporary check which acts as a hook to the transient
4750 -- object. Generate:
4751
4752 -- Hook : Ptr_Typ := null;
4753
4754 Hook_Id := Make_Temporary (Loc, 'T');
4755 Mutate_Ekind (Hook_Id, E_Variable);
4756 Set_Etype (Hook_Id, Ptr_Typ);
4757
4758 Hook_Decl :=
4759 Make_Object_Declaration (Loc,
4760 Defining_Identifier => Hook_Id,
4761 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4762 Expression => Make_Null (Loc));
4763
4764 -- Mark the temporary as a hook. This signals the machinery in
4765 -- Build_Finalizer to recognize this special case.
4766
4767 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4768
4769 -- Hook the transient object to the temporary. Generate:
4770
4771 -- Hook := Ptr_Typ (Obj_Id);
4772 -- <or>
4773 -- Hool := Obj_Id'Unrestricted_Access;
4774
4775 if Is_Access_Type (Obj_Typ) then
4776 Hook_Expr :=
4777 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4778 else
4779 Hook_Expr :=
4780 Make_Attribute_Reference (Loc,
4781 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4782 Attribute_Name => Name_Unrestricted_Access);
4783 end if;
4784
4785 Hook_Assign :=
4786 Make_Assignment_Statement (Loc,
4787 Name => New_Occurrence_Of (Hook_Id, Loc),
4788 Expression => Hook_Expr);
4789
4790 -- Crear the hook prior to finalizing the object. Generate:
4791
4792 -- Hook := null;
4793
4794 Hook_Clear :=
4795 Make_Assignment_Statement (Loc,
4796 Name => New_Occurrence_Of (Hook_Id, Loc),
4797 Expression => Make_Null (Loc));
4798
4799 -- Finalize the object. Generate:
4800
4801 -- [Deep_]Finalize (Obj_Ref[.all]);
4802
4803 if Finalize_Obj then
4804 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4805
4806 if Is_Access_Type (Obj_Typ) then
4807 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4808 Set_Etype (Obj_Ref, Desig_Typ);
4809 end if;
4810
4811 Fin_Call :=
4812 Make_Final_Call
4813 (Obj_Ref => Obj_Ref,
4814 Typ => Desig_Typ);
4815
4816 -- Otherwise finalize the hook. Generate:
4817
4818 -- [Deep_]Finalize (Hook.all);
4819
4820 else
4821 Fin_Call :=
4822 Make_Final_Call (
4823 Obj_Ref =>
4824 Make_Explicit_Dereference (Loc,
4825 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4826 Typ => Desig_Typ);
4827 end if;
4828 end Build_Transient_Object_Statements;
4829
4830 -----------------------------
4831 -- Check_Float_Op_Overflow --
4832 -----------------------------
4833
4834 procedure Check_Float_Op_Overflow (N : Node_Id) is
4835 begin
4836 -- Return if no check needed
4837
4838 if not Is_Floating_Point_Type (Etype (N))
4839 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4840
4841 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4842 -- and do not expand the code for float overflow checking.
4843
4844 or else CodePeer_Mode
4845 then
4846 return;
4847 end if;
4848
4849 -- Otherwise we replace the expression by
4850
4851 -- do Tnn : constant ftype := expression;
4852 -- constraint_error when not Tnn'Valid;
4853 -- in Tnn;
4854
4855 declare
4856 Loc : constant Source_Ptr := Sloc (N);
4857 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4858 Typ : constant Entity_Id := Etype (N);
4859
4860 begin
4861 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4862 -- right here. We also set the node as analyzed to prevent infinite
4863 -- recursion from repeating the operation in the expansion.
4864
4865 Set_Do_Overflow_Check (N, False);
4866 Set_Analyzed (N, True);
4867
4868 -- Do the rewrite to include the check
4869
4870 Rewrite (N,
4871 Make_Expression_With_Actions (Loc,
4872 Actions => New_List (
4873 Make_Object_Declaration (Loc,
4874 Defining_Identifier => Tnn,
4875 Object_Definition => New_Occurrence_Of (Typ, Loc),
4876 Constant_Present => True,
4877 Expression => Relocate_Node (N)),
4878 Make_Raise_Constraint_Error (Loc,
4879 Condition =>
4880 Make_Op_Not (Loc,
4881 Right_Opnd =>
4882 Make_Attribute_Reference (Loc,
4883 Prefix => New_Occurrence_Of (Tnn, Loc),
4884 Attribute_Name => Name_Valid)),
4885 Reason => CE_Overflow_Check_Failed)),
4886 Expression => New_Occurrence_Of (Tnn, Loc)));
4887
4888 Analyze_And_Resolve (N, Typ);
4889 end;
4890 end Check_Float_Op_Overflow;
4891
4892 ----------------------------------
4893 -- Component_May_Be_Bit_Aligned --
4894 ----------------------------------
4895
4896 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4897 UT : Entity_Id;
4898
4899 begin
4900 -- If no component clause, then everything is fine, since the back end
4901 -- never misaligns from byte boundaries by default, even if there is a
4902 -- pragma Pack for the record.
4903
4904 if No (Comp) or else No (Component_Clause (Comp)) then
4905 return False;
4906 end if;
4907
4908 UT := Underlying_Type (Etype (Comp));
4909
4910 -- It is only array and record types that cause trouble
4911
4912 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4913 return False;
4914
4915 -- If we know that we have a small (at most the maximum integer size)
4916 -- record or bit-packed array, then everything is fine, since the back
4917 -- end can handle these cases correctly.
4918
4919 elsif Known_Esize (Comp)
4920 and then Esize (Comp) <= System_Max_Integer_Size
4921 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4922 then
4923 return False;
4924
4925 elsif not Known_Normalized_First_Bit (Comp) then
4926 return True;
4927
4928 -- Otherwise if the component is not byte aligned, we know we have the
4929 -- nasty unaligned case.
4930
4931 elsif Normalized_First_Bit (Comp) /= Uint_0
4932 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4933 then
4934 return True;
4935
4936 -- If we are large and byte aligned, then OK at this level
4937
4938 else
4939 return False;
4940 end if;
4941 end Component_May_Be_Bit_Aligned;
4942
4943 -------------------------------
4944 -- Convert_To_Actual_Subtype --
4945 -------------------------------
4946
4947 procedure Convert_To_Actual_Subtype (Exp : Node_Id) is
4948 Act_ST : Entity_Id;
4949
4950 begin
4951 Act_ST := Get_Actual_Subtype (Exp);
4952
4953 if Act_ST = Etype (Exp) then
4954 return;
4955 else
4956 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4957 Analyze_And_Resolve (Exp, Act_ST);
4958 end if;
4959 end Convert_To_Actual_Subtype;
4960
4961 -----------------------------------
4962 -- Corresponding_Runtime_Package --
4963 -----------------------------------
4964
4965 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4966 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4967 -- Return True if protected type T has one entry and the maximum queue
4968 -- length is one.
4969
4970 --------------------------------
4971 -- Has_One_Entry_And_No_Queue --
4972 --------------------------------
4973
4974 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4975 Item : Entity_Id;
4976 Is_First : Boolean := True;
4977
4978 begin
4979 Item := First_Entity (T);
4980 while Present (Item) loop
4981 if Is_Entry (Item) then
4982
4983 -- The protected type has more than one entry
4984
4985 if not Is_First then
4986 return False;
4987 end if;
4988
4989 -- The queue length is not one
4990
4991 if not Restriction_Active (No_Entry_Queue)
4992 and then Get_Max_Queue_Length (Item) /= Uint_1
4993 then
4994 return False;
4995 end if;
4996
4997 Is_First := False;
4998 end if;
4999
5000 Next_Entity (Item);
5001 end loop;
5002
5003 return True;
5004 end Has_One_Entry_And_No_Queue;
5005
5006 -- Local variables
5007
5008 Pkg_Id : RTU_Id := RTU_Null;
5009
5010 -- Start of processing for Corresponding_Runtime_Package
5011
5012 begin
5013 pragma Assert (Is_Concurrent_Type (Typ));
5014
5015 if Is_Protected_Type (Typ) then
5016 if Has_Entries (Typ)
5017
5018 -- A protected type without entries that covers an interface and
5019 -- overrides the abstract routines with protected procedures is
5020 -- considered equivalent to a protected type with entries in the
5021 -- context of dispatching select statements. It is sufficient to
5022 -- check for the presence of an interface list in the declaration
5023 -- node to recognize this case.
5024
5025 or else Present (Interface_List (Parent (Typ)))
5026
5027 -- Protected types with interrupt handlers (when not using a
5028 -- restricted profile) are also considered equivalent to
5029 -- protected types with entries. The types which are used
5030 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
5031 -- are derived from Protection_Entries.
5032
5033 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
5034 or else Has_Interrupt_Handler (Typ)
5035 then
5036 if Abort_Allowed
5037 or else Restriction_Active (No_Select_Statements) = False
5038 or else not Has_One_Entry_And_No_Queue (Typ)
5039 or else (Has_Attach_Handler (Typ)
5040 and then not Restricted_Profile)
5041 then
5042 Pkg_Id := System_Tasking_Protected_Objects_Entries;
5043 else
5044 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
5045 end if;
5046
5047 else
5048 Pkg_Id := System_Tasking_Protected_Objects;
5049 end if;
5050 end if;
5051
5052 return Pkg_Id;
5053 end Corresponding_Runtime_Package;
5054
5055 -----------------------------------
5056 -- Current_Sem_Unit_Declarations --
5057 -----------------------------------
5058
5059 function Current_Sem_Unit_Declarations return List_Id is
5060 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
5061 Decls : List_Id;
5062
5063 begin
5064 -- If the current unit is a package body, locate the visible
5065 -- declarations of the package spec.
5066
5067 if Nkind (U) = N_Package_Body then
5068 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
5069 end if;
5070
5071 if Nkind (U) = N_Package_Declaration then
5072 U := Specification (U);
5073 Decls := Visible_Declarations (U);
5074
5075 if No (Decls) then
5076 Decls := New_List;
5077 Set_Visible_Declarations (U, Decls);
5078 end if;
5079
5080 else
5081 Decls := Declarations (U);
5082
5083 if No (Decls) then
5084 Decls := New_List;
5085 Set_Declarations (U, Decls);
5086 end if;
5087 end if;
5088
5089 return Decls;
5090 end Current_Sem_Unit_Declarations;
5091
5092 -----------------------
5093 -- Duplicate_Subexpr --
5094 -----------------------
5095
5096 function Duplicate_Subexpr
5097 (Exp : Node_Id;
5098 Name_Req : Boolean := False;
5099 Renaming_Req : Boolean := False) return Node_Id
5100 is
5101 begin
5102 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5103 return New_Copy_Tree (Exp);
5104 end Duplicate_Subexpr;
5105
5106 ---------------------------------
5107 -- Duplicate_Subexpr_No_Checks --
5108 ---------------------------------
5109
5110 function Duplicate_Subexpr_No_Checks
5111 (Exp : Node_Id;
5112 Name_Req : Boolean := False;
5113 Renaming_Req : Boolean := False;
5114 Related_Id : Entity_Id := Empty;
5115 Is_Low_Bound : Boolean := False;
5116 Is_High_Bound : Boolean := False) return Node_Id
5117 is
5118 New_Exp : Node_Id;
5119
5120 begin
5121 Remove_Side_Effects
5122 (Exp => Exp,
5123 Name_Req => Name_Req,
5124 Renaming_Req => Renaming_Req,
5125 Related_Id => Related_Id,
5126 Is_Low_Bound => Is_Low_Bound,
5127 Is_High_Bound => Is_High_Bound);
5128
5129 New_Exp := New_Copy_Tree (Exp);
5130 Remove_Checks (New_Exp);
5131 return New_Exp;
5132 end Duplicate_Subexpr_No_Checks;
5133
5134 -----------------------------------
5135 -- Duplicate_Subexpr_Move_Checks --
5136 -----------------------------------
5137
5138 function Duplicate_Subexpr_Move_Checks
5139 (Exp : Node_Id;
5140 Name_Req : Boolean := False;
5141 Renaming_Req : Boolean := False) return Node_Id
5142 is
5143 New_Exp : Node_Id;
5144
5145 begin
5146 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5147 New_Exp := New_Copy_Tree (Exp);
5148 Remove_Checks (Exp);
5149 return New_Exp;
5150 end Duplicate_Subexpr_Move_Checks;
5151
5152 -------------------------
5153 -- Enclosing_Init_Proc --
5154 -------------------------
5155
5156 function Enclosing_Init_Proc return Entity_Id is
5157 S : Entity_Id;
5158
5159 begin
5160 S := Current_Scope;
5161 while Present (S) and then S /= Standard_Standard loop
5162 if Is_Init_Proc (S) then
5163 return S;
5164 else
5165 S := Scope (S);
5166 end if;
5167 end loop;
5168
5169 return Empty;
5170 end Enclosing_Init_Proc;
5171
5172 --------------------
5173 -- Ensure_Defined --
5174 --------------------
5175
5176 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
5177 IR : Node_Id;
5178
5179 begin
5180 -- An itype reference must only be created if this is a local itype, so
5181 -- that gigi can elaborate it on the proper objstack.
5182
5183 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
5184 IR := Make_Itype_Reference (Sloc (N));
5185 Set_Itype (IR, Typ);
5186 Insert_Action (N, IR);
5187 end if;
5188 end Ensure_Defined;
5189
5190 --------------------
5191 -- Entry_Names_OK --
5192 --------------------
5193
5194 function Entry_Names_OK return Boolean is
5195 begin
5196 return
5197 not Restricted_Profile
5198 and then not Global_Discard_Names
5199 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5200 and then not Restriction_Active (No_Local_Allocators);
5201 end Entry_Names_OK;
5202
5203 -------------------
5204 -- Evaluate_Name --
5205 -------------------
5206
5207 procedure Evaluate_Name (Nam : Node_Id) is
5208 begin
5209 case Nkind (Nam) is
5210 -- For an aggregate, force its evaluation
5211
5212 when N_Aggregate =>
5213 Force_Evaluation (Nam);
5214
5215 -- For an attribute reference or an indexed component, evaluate the
5216 -- prefix, which is itself a name, recursively, and then force the
5217 -- evaluation of all the subscripts (or attribute expressions).
5218
5219 when N_Attribute_Reference
5220 | N_Indexed_Component
5221 =>
5222 Evaluate_Name (Prefix (Nam));
5223
5224 declare
5225 E : Node_Id;
5226
5227 begin
5228 E := First (Expressions (Nam));
5229 while Present (E) loop
5230 Force_Evaluation (E);
5231
5232 if Is_Rewrite_Substitution (E) then
5233 Set_Do_Range_Check
5234 (E, Do_Range_Check (Original_Node (E)));
5235 end if;
5236
5237 Next (E);
5238 end loop;
5239 end;
5240
5241 -- For an explicit dereference, we simply force the evaluation of
5242 -- the name expression. The dereference provides a value that is the
5243 -- address for the renamed object, and it is precisely this value
5244 -- that we want to preserve.
5245
5246 when N_Explicit_Dereference =>
5247 Force_Evaluation (Prefix (Nam));
5248
5249 -- For a function call, we evaluate the call; same for an operator
5250
5251 when N_Function_Call
5252 | N_Op
5253 =>
5254 Force_Evaluation (Nam);
5255
5256 -- For a qualified expression, we evaluate the expression
5257
5258 when N_Qualified_Expression =>
5259 Evaluate_Name (Expression (Nam));
5260
5261 -- For a selected component, we simply evaluate the prefix
5262
5263 when N_Selected_Component =>
5264 Evaluate_Name (Prefix (Nam));
5265
5266 -- For a slice, we evaluate the prefix, as for the indexed component
5267 -- case and then, if there is a range present, either directly or as
5268 -- the constraint of a discrete subtype indication, we evaluate the
5269 -- two bounds of this range.
5270
5271 when N_Slice =>
5272 Evaluate_Name (Prefix (Nam));
5273 Evaluate_Slice_Bounds (Nam);
5274
5275 -- For a type conversion, the expression of the conversion must be
5276 -- the name of an object, and we simply need to evaluate this name.
5277
5278 when N_Type_Conversion =>
5279 Evaluate_Name (Expression (Nam));
5280
5281 -- The remaining cases are direct name and character literal. In all
5282 -- these cases, we do nothing, since we want to reevaluate each time
5283 -- the renamed object is used. ??? There are more remaining cases, at
5284 -- least in the GNATprove_Mode, where this routine is called in more
5285 -- contexts than in GNAT.
5286
5287 when others =>
5288 null;
5289 end case;
5290 end Evaluate_Name;
5291
5292 ---------------------------
5293 -- Evaluate_Slice_Bounds --
5294 ---------------------------
5295
5296 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
5297 DR : constant Node_Id := Discrete_Range (Slice);
5298 Constr : Node_Id;
5299 Rexpr : Node_Id;
5300
5301 begin
5302 if Nkind (DR) = N_Range then
5303 Force_Evaluation (Low_Bound (DR));
5304 Force_Evaluation (High_Bound (DR));
5305
5306 elsif Nkind (DR) = N_Subtype_Indication then
5307 Constr := Constraint (DR);
5308
5309 if Nkind (Constr) = N_Range_Constraint then
5310 Rexpr := Range_Expression (Constr);
5311
5312 Force_Evaluation (Low_Bound (Rexpr));
5313 Force_Evaluation (High_Bound (Rexpr));
5314 end if;
5315 end if;
5316 end Evaluate_Slice_Bounds;
5317
5318 ---------------------
5319 -- Evolve_And_Then --
5320 ---------------------
5321
5322 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
5323 begin
5324 if No (Cond) then
5325 Cond := Cond1;
5326 else
5327 Cond :=
5328 Make_And_Then (Sloc (Cond1),
5329 Left_Opnd => Cond,
5330 Right_Opnd => Cond1);
5331 end if;
5332 end Evolve_And_Then;
5333
5334 --------------------
5335 -- Evolve_Or_Else --
5336 --------------------
5337
5338 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
5339 begin
5340 if No (Cond) then
5341 Cond := Cond1;
5342 else
5343 Cond :=
5344 Make_Or_Else (Sloc (Cond1),
5345 Left_Opnd => Cond,
5346 Right_Opnd => Cond1);
5347 end if;
5348 end Evolve_Or_Else;
5349
5350 -------------------------------
5351 -- Expand_Sliding_Conversion --
5352 -------------------------------
5353
5354 procedure Expand_Sliding_Conversion (N : Node_Id; Arr_Typ : Entity_Id) is
5355
5356 pragma Assert (Is_Array_Type (Arr_Typ)
5357 and then not Is_Constrained (Arr_Typ)
5358 and then Is_Fixed_Lower_Bound_Array_Subtype (Arr_Typ));
5359
5360 Constraints : List_Id;
5361 Index : Node_Id := First_Index (Arr_Typ);
5362 Loc : constant Source_Ptr := Sloc (N);
5363 Subt_Decl : Node_Id;
5364 Subt : Entity_Id;
5365 Subt_Low : Node_Id;
5366 Subt_High : Node_Id;
5367
5368 Act_Subt : Entity_Id;
5369 Act_Index : Node_Id;
5370 Act_Low : Node_Id;
5371 Act_High : Node_Id;
5372 Adjust_Incr : Node_Id;
5373 Dimension : Int := 0;
5374 All_FLBs_Match : Boolean := True;
5375
5376 begin
5377 -- This procedure is called during semantic analysis, and we only expand
5378 -- a sliding conversion when Expander_Active, to avoid doing it during
5379 -- preanalysis (which can lead to problems with the target subtype not
5380 -- getting properly expanded during later full analysis). Also, sliding
5381 -- should never be needed for string literals, because their bounds are
5382 -- determined directly based on the fixed lower bound of Arr_Typ and
5383 -- their length.
5384
5385 if Expander_Active and then Nkind (N) /= N_String_Literal then
5386 Constraints := New_List;
5387
5388 Act_Subt := Get_Actual_Subtype (N);
5389 Act_Index := First_Index (Act_Subt);
5390
5391 -- Loop over the indexes of the fixed-lower-bound array type or
5392 -- subtype to build up an index constraint for constructing the
5393 -- subtype that will be the target of a conversion of the array
5394 -- object that may need a sliding conversion.
5395
5396 while Present (Index) loop
5397 pragma Assert (Present (Act_Index));
5398
5399 Dimension := Dimension + 1;
5400
5401 Get_Index_Bounds (Act_Index, Act_Low, Act_High);
5402
5403 -- If Index defines a normal unconstrained range (range <>),
5404 -- then we will simply use the bounds of the actual subtype's
5405 -- corresponding index range.
5406
5407 if not Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index)) then
5408 Subt_Low := Act_Low;
5409 Subt_High := Act_High;
5410
5411 -- Otherwise, a range will be created with a low bound given by
5412 -- the fixed lower bound of the array subtype's index, and with
5413 -- high bound given by (Actual'Length + fixed lower bound - 1).
5414
5415 else
5416 if Nkind (Index) = N_Subtype_Indication then
5417 Subt_Low :=
5418 New_Copy_Tree
5419 (Low_Bound (Range_Expression (Constraint (Index))));
5420 else
5421 pragma Assert (Nkind (Index) = N_Range);
5422
5423 Subt_Low := New_Copy_Tree (Low_Bound (Index));
5424 end if;
5425
5426 -- If either we have a nonstatic lower bound, or the target and
5427 -- source subtypes are statically known to have unequal lower
5428 -- bounds, then we will need to make a subtype conversion to
5429 -- slide the bounds. However, if all of the indexes' lower
5430 -- bounds are static and known to be equal (the common case),
5431 -- then no conversion will be needed, and we'll end up not
5432 -- creating the subtype or the conversion (though we still
5433 -- build up the index constraint, which will simply be unused).
5434
5435 if not (Compile_Time_Known_Value (Subt_Low)
5436 and then Compile_Time_Known_Value (Act_Low))
5437 or else Expr_Value (Subt_Low) /= Expr_Value (Act_Low)
5438 then
5439 All_FLBs_Match := False;
5440 end if;
5441
5442 -- Apply 'Pos to lower bound, which may be of an enumeration
5443 -- type, before subtracting.
5444
5445 Adjust_Incr :=
5446 Make_Op_Subtract (Loc,
5447 Make_Attribute_Reference (Loc,
5448 Prefix =>
5449 New_Occurrence_Of (Etype (Act_Index), Loc),
5450 Attribute_Name =>
5451 Name_Pos,
5452 Expressions =>
5453 New_List (New_Copy_Tree (Subt_Low))),
5454 Make_Integer_Literal (Loc, 1));
5455
5456 -- Apply 'Val to the result of adding the increment to the
5457 -- length, to handle indexes of enumeration types.
5458
5459 Subt_High :=
5460 Make_Attribute_Reference (Loc,
5461 Prefix =>
5462 New_Occurrence_Of (Etype (Act_Index), Loc),
5463 Attribute_Name =>
5464 Name_Val,
5465 Expressions =>
5466 New_List (Make_Op_Add (Loc,
5467 Make_Attribute_Reference (Loc,
5468 Prefix =>
5469 New_Occurrence_Of (Act_Subt, Loc),
5470 Attribute_Name =>
5471 Name_Length,
5472 Expressions =>
5473 New_List
5474 (Make_Integer_Literal
5475 (Loc, Dimension))),
5476 Adjust_Incr)));
5477 end if;
5478
5479 Append (Make_Range (Loc, Subt_Low, Subt_High), Constraints);
5480
5481 Next (Index);
5482 Next (Act_Index);
5483 end loop;
5484
5485 -- If for each index with a fixed lower bound (FLB), the lower bound
5486 -- of the corresponding index of the actual subtype is statically
5487 -- known be equal to the FLB, then a sliding conversion isn't needed
5488 -- at all, so just return without building a subtype or conversion.
5489
5490 if All_FLBs_Match then
5491 return;
5492 end if;
5493
5494 -- A sliding conversion is needed, so create the target subtype using
5495 -- the index constraint created above, and rewrite the expression
5496 -- as a conversion to that subtype.
5497
5498 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
5499 Set_Is_Internal (Subt);
5500
5501 Subt_Decl :=
5502 Make_Subtype_Declaration (Loc,
5503 Defining_Identifier => Subt,
5504 Subtype_Indication =>
5505 Make_Subtype_Indication (Loc,
5506 Subtype_Mark =>
5507 New_Occurrence_Of (Arr_Typ, Loc),
5508 Constraint =>
5509 Make_Index_Or_Discriminant_Constraint (Loc,
5510 Constraints => Constraints)));
5511
5512 Mark_Rewrite_Insertion (Subt_Decl);
5513
5514 -- The actual subtype is an Itype, so we analyze the declaration,
5515 -- but do not attach it to the tree.
5516
5517 Set_Parent (Subt_Decl, N);
5518 Set_Is_Itype (Subt);
5519 Analyze (Subt_Decl, Suppress => All_Checks);
5520 Set_Associated_Node_For_Itype (Subt, N);
5521 Set_Has_Delayed_Freeze (Subt, False);
5522
5523 -- We need to freeze the actual subtype immediately. This is needed
5524 -- because otherwise this Itype will not get frozen at all, and it is
5525 -- always safe to freeze on creation because any associated types
5526 -- must be frozen at this point.
5527
5528 Freeze_Itype (Subt, N);
5529
5530 Rewrite (N,
5531 Make_Type_Conversion (Loc,
5532 Subtype_Mark =>
5533 New_Occurrence_Of (Subt, Loc),
5534 Expression => Relocate_Node (N)));
5535 Analyze (N);
5536 end if;
5537 end Expand_Sliding_Conversion;
5538
5539 -----------------------------------------
5540 -- Expand_Static_Predicates_In_Choices --
5541 -----------------------------------------
5542
5543 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
5544 pragma Assert (Nkind (N) in N_Case_Statement_Alternative | N_Variant);
5545
5546 Choices : List_Id := Discrete_Choices (N);
5547
5548 Choice : Node_Id;
5549 Next_C : Node_Id;
5550 P : Node_Id;
5551 C : Node_Id;
5552
5553 begin
5554 -- If this is an "others" alternative, we need to process any static
5555 -- predicates in its Others_Discrete_Choices.
5556
5557 if Nkind (First (Choices)) = N_Others_Choice then
5558 Choices := Others_Discrete_Choices (First (Choices));
5559 end if;
5560
5561 Choice := First (Choices);
5562 while Present (Choice) loop
5563 Next_C := Next (Choice);
5564
5565 -- Check for name of subtype with static predicate
5566
5567 if Is_Entity_Name (Choice)
5568 and then Is_Type (Entity (Choice))
5569 and then Has_Predicates (Entity (Choice))
5570 then
5571 -- Loop through entries in predicate list, converting to choices
5572 -- and inserting in the list before the current choice. Note that
5573 -- if the list is empty, corresponding to a False predicate, then
5574 -- no choices are inserted.
5575
5576 P := First (Static_Discrete_Predicate (Entity (Choice)));
5577 while Present (P) loop
5578
5579 -- If low bound and high bounds are equal, copy simple choice
5580
5581 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
5582 C := New_Copy (Low_Bound (P));
5583
5584 -- Otherwise copy a range
5585
5586 else
5587 C := New_Copy (P);
5588 end if;
5589
5590 -- Change Sloc to referencing choice (rather than the Sloc of
5591 -- the predicate declaration element itself).
5592
5593 Set_Sloc (C, Sloc (Choice));
5594 Insert_Before (Choice, C);
5595 Next (P);
5596 end loop;
5597
5598 -- Delete the predicated entry
5599
5600 Remove (Choice);
5601 end if;
5602
5603 -- Move to next choice to check
5604
5605 Choice := Next_C;
5606 end loop;
5607
5608 Set_Has_SP_Choice (N, False);
5609 end Expand_Static_Predicates_In_Choices;
5610
5611 ------------------------------
5612 -- Expand_Subtype_From_Expr --
5613 ------------------------------
5614
5615 -- This function is applicable for both static and dynamic allocation of
5616 -- objects which are constrained by an initial expression. Basically it
5617 -- transforms an unconstrained subtype indication into a constrained one.
5618
5619 -- The expression may also be transformed in certain cases in order to
5620 -- avoid multiple evaluation. In the static allocation case, the general
5621 -- scheme is:
5622
5623 -- Val : T := Expr;
5624
5625 -- is transformed into
5626
5627 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5628 --
5629 -- Here are the main cases :
5630 --
5631 -- <if Expr is a Slice>
5632 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5633 --
5634 -- <elsif Expr is a String Literal>
5635 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5636 --
5637 -- <elsif Expr is Constrained>
5638 -- subtype T is Type_Of_Expr
5639 -- Val : T := Expr;
5640 --
5641 -- <elsif Expr is an entity_name>
5642 -- Val : T (constraints taken from Expr) := Expr;
5643 --
5644 -- <else>
5645 -- type Axxx is access all T;
5646 -- Rval : Axxx := Expr'ref;
5647 -- Val : T (constraints taken from Rval) := Rval.all;
5648
5649 -- ??? note: when the Expression is allocated in the secondary stack
5650 -- we could use it directly instead of copying it by declaring
5651 -- Val : T (...) renames Rval.all
5652
5653 procedure Expand_Subtype_From_Expr
5654 (N : Node_Id;
5655 Unc_Type : Entity_Id;
5656 Subtype_Indic : Node_Id;
5657 Exp : Node_Id;
5658 Related_Id : Entity_Id := Empty)
5659 is
5660 Loc : constant Source_Ptr := Sloc (N);
5661 Exp_Typ : constant Entity_Id := Etype (Exp);
5662 T : Entity_Id;
5663
5664 begin
5665 -- In general we cannot build the subtype if expansion is disabled,
5666 -- because internal entities may not have been defined. However, to
5667 -- avoid some cascaded errors, we try to continue when the expression is
5668 -- an array (or string), because it is safe to compute the bounds. It is
5669 -- in fact required to do so even in a generic context, because there
5670 -- may be constants that depend on the bounds of a string literal, both
5671 -- standard string types and more generally arrays of characters.
5672
5673 -- In GNATprove mode, these extra subtypes are not needed, unless Exp is
5674 -- a static expression. In that case, the subtype will be constrained
5675 -- while the original type might be unconstrained, so expanding the type
5676 -- is necessary both for passing legality checks in GNAT and for precise
5677 -- analysis in GNATprove.
5678
5679 if GNATprove_Mode and then not Is_Static_Expression (Exp) then
5680 return;
5681 end if;
5682
5683 if not Expander_Active
5684 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5685 then
5686 return;
5687 end if;
5688
5689 if Nkind (Exp) = N_Slice then
5690 declare
5691 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5692
5693 begin
5694 Rewrite (Subtype_Indic,
5695 Make_Subtype_Indication (Loc,
5696 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5697 Constraint =>
5698 Make_Index_Or_Discriminant_Constraint (Loc,
5699 Constraints => New_List
5700 (New_Occurrence_Of (Slice_Type, Loc)))));
5701
5702 -- This subtype indication may be used later for constraint checks
5703 -- we better make sure that if a variable was used as a bound of
5704 -- the original slice, its value is frozen.
5705
5706 Evaluate_Slice_Bounds (Exp);
5707 end;
5708
5709 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5710 Rewrite (Subtype_Indic,
5711 Make_Subtype_Indication (Loc,
5712 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5713 Constraint =>
5714 Make_Index_Or_Discriminant_Constraint (Loc,
5715 Constraints => New_List (
5716 Make_Literal_Range (Loc,
5717 Literal_Typ => Exp_Typ)))));
5718
5719 -- If the type of the expression is an internally generated type it
5720 -- may not be necessary to create a new subtype. However there are two
5721 -- exceptions: references to the current instances, and aliased array
5722 -- object declarations for which the back end has to create a template.
5723
5724 elsif Is_Constrained (Exp_Typ)
5725 and then not Is_Class_Wide_Type (Unc_Type)
5726 and then
5727 (Nkind (N) /= N_Object_Declaration
5728 or else not Is_Entity_Name (Expression (N))
5729 or else not Comes_From_Source (Entity (Expression (N)))
5730 or else not Is_Array_Type (Exp_Typ)
5731 or else not Aliased_Present (N))
5732 then
5733 if Is_Itype (Exp_Typ) then
5734
5735 -- Within an initialization procedure, a selected component
5736 -- denotes a component of the enclosing record, and it appears as
5737 -- an actual in a call to its own initialization procedure. If
5738 -- this component depends on the outer discriminant, we must
5739 -- generate the proper actual subtype for it.
5740
5741 if Nkind (Exp) = N_Selected_Component
5742 and then Within_Init_Proc
5743 then
5744 declare
5745 Decl : constant Node_Id :=
5746 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5747 begin
5748 if Present (Decl) then
5749 Insert_Action (N, Decl);
5750 T := Defining_Identifier (Decl);
5751 else
5752 T := Exp_Typ;
5753 end if;
5754 end;
5755
5756 -- No need to generate a new subtype
5757
5758 else
5759 T := Exp_Typ;
5760 end if;
5761
5762 else
5763 T := Make_Temporary (Loc, 'T');
5764
5765 Insert_Action (N,
5766 Make_Subtype_Declaration (Loc,
5767 Defining_Identifier => T,
5768 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5769
5770 -- This type is marked as an itype even though it has an explicit
5771 -- declaration since otherwise Is_Generic_Actual_Type can get
5772 -- set, resulting in the generation of spurious errors. (See
5773 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5774
5775 Set_Is_Itype (T);
5776 Set_Associated_Node_For_Itype (T, Exp);
5777 end if;
5778
5779 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5780
5781 -- Nothing needs to be done for private types with unknown discriminants
5782 -- if the underlying type is not an unconstrained composite type or it
5783 -- is an unchecked union.
5784
5785 elsif Is_Private_Type (Unc_Type)
5786 and then Has_Unknown_Discriminants (Unc_Type)
5787 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5788 or else Is_Constrained (Underlying_Type (Unc_Type))
5789 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5790 then
5791 null;
5792
5793 -- Case of derived type with unknown discriminants where the parent type
5794 -- also has unknown discriminants.
5795
5796 elsif Is_Record_Type (Unc_Type)
5797 and then not Is_Class_Wide_Type (Unc_Type)
5798 and then Has_Unknown_Discriminants (Unc_Type)
5799 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5800 then
5801 -- Nothing to be done if no underlying record view available
5802
5803 -- If this is a limited type derived from a type with unknown
5804 -- discriminants, do not expand either, so that subsequent expansion
5805 -- of the call can add build-in-place parameters to call.
5806
5807 if No (Underlying_Record_View (Unc_Type))
5808 or else Is_Limited_Type (Unc_Type)
5809 then
5810 null;
5811
5812 -- Otherwise use the Underlying_Record_View to create the proper
5813 -- constrained subtype for an object of a derived type with unknown
5814 -- discriminants.
5815
5816 else
5817 Remove_Side_Effects (Exp);
5818 Rewrite (Subtype_Indic,
5819 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5820 end if;
5821
5822 -- Renamings of class-wide interface types require no equivalent
5823 -- constrained type declarations because we only need to reference
5824 -- the tag component associated with the interface. The same is
5825 -- presumably true for class-wide types in general, so this test
5826 -- is broadened to include all class-wide renamings, which also
5827 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5828 -- (Is this really correct, or are there some cases of class-wide
5829 -- renamings that require action in this procedure???)
5830
5831 elsif Present (N)
5832 and then Nkind (N) = N_Object_Renaming_Declaration
5833 and then Is_Class_Wide_Type (Unc_Type)
5834 then
5835 null;
5836
5837 -- In Ada 95 nothing to be done if the type of the expression is limited
5838 -- because in this case the expression cannot be copied, and its use can
5839 -- only be by reference.
5840
5841 -- In Ada 2005 the context can be an object declaration whose expression
5842 -- is a function that returns in place. If the nominal subtype has
5843 -- unknown discriminants, the call still provides constraints on the
5844 -- object, and we have to create an actual subtype from it.
5845
5846 -- If the type is class-wide, the expression is dynamically tagged and
5847 -- we do not create an actual subtype either. Ditto for an interface.
5848 -- For now this applies only if the type is immutably limited, and the
5849 -- function being called is build-in-place. This will have to be revised
5850 -- when build-in-place functions are generalized to other types.
5851
5852 elsif Is_Limited_View (Exp_Typ)
5853 and then
5854 (Is_Class_Wide_Type (Exp_Typ)
5855 or else Is_Interface (Exp_Typ)
5856 or else not Has_Unknown_Discriminants (Exp_Typ)
5857 or else not Is_Composite_Type (Unc_Type))
5858 then
5859 null;
5860
5861 -- For limited objects initialized with build-in-place function calls,
5862 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5863 -- node in the expression initializing the object, which breaks the
5864 -- circuitry that detects and adds the additional arguments to the
5865 -- called function.
5866
5867 elsif Is_Build_In_Place_Function_Call (Exp) then
5868 null;
5869
5870 -- If the expression is an uninitialized aggregate, no need to build
5871 -- a subtype from the expression, because this may require the use of
5872 -- dynamic memory to create the object.
5873
5874 elsif Is_Uninitialized_Aggregate (Exp, Exp_Typ) then
5875 Rewrite (Subtype_Indic, New_Occurrence_Of (Etype (Exp), Sloc (N)));
5876 if Nkind (N) = N_Object_Declaration then
5877 Set_Expression (N, Empty);
5878 Set_No_Initialization (N);
5879 end if;
5880
5881 else
5882 Remove_Side_Effects (Exp);
5883 Rewrite (Subtype_Indic,
5884 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5885 end if;
5886 end Expand_Subtype_From_Expr;
5887
5888 ---------------------------------------------
5889 -- Expression_Contains_Primitives_Calls_Of --
5890 ---------------------------------------------
5891
5892 function Expression_Contains_Primitives_Calls_Of
5893 (Expr : Node_Id;
5894 Typ : Entity_Id) return Boolean
5895 is
5896 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5897
5898 Calls_OK : Boolean := False;
5899 -- This flag is set to True when expression Expr contains at least one
5900 -- call to a nondispatching primitive function of Typ.
5901
5902 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5903 -- Search for nondispatching calls to primitive functions of type Typ
5904
5905 ----------------------------
5906 -- Search_Primitive_Calls --
5907 ----------------------------
5908
5909 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5910 Disp_Typ : Entity_Id;
5911 Subp : Entity_Id;
5912
5913 begin
5914 -- Detect a function call that could denote a nondispatching
5915 -- primitive of the input type.
5916
5917 if Nkind (N) = N_Function_Call
5918 and then Is_Entity_Name (Name (N))
5919 then
5920 Subp := Entity (Name (N));
5921
5922 -- Do not consider function calls with a controlling argument, as
5923 -- those are always dispatching calls.
5924
5925 if Is_Dispatching_Operation (Subp)
5926 and then No (Controlling_Argument (N))
5927 then
5928 Disp_Typ := Find_Dispatching_Type (Subp);
5929
5930 -- To qualify as a suitable primitive, the dispatching type of
5931 -- the function must be the input type.
5932
5933 if Present (Disp_Typ)
5934 and then Unique_Entity (Disp_Typ) = U_Typ
5935 then
5936 Calls_OK := True;
5937
5938 -- There is no need to continue the traversal, as one such
5939 -- call suffices.
5940
5941 return Abandon;
5942 end if;
5943 end if;
5944 end if;
5945
5946 return OK;
5947 end Search_Primitive_Calls;
5948
5949 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5950
5951 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5952
5953 begin
5954 Search_Calls (Expr);
5955 return Calls_OK;
5956 end Expression_Contains_Primitives_Calls_Of;
5957
5958 ----------------------
5959 -- Finalize_Address --
5960 ----------------------
5961
5962 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5963 Btyp : constant Entity_Id := Base_Type (Typ);
5964 Utyp : Entity_Id := Typ;
5965
5966 begin
5967 -- Handle protected class-wide or task class-wide types
5968
5969 if Is_Class_Wide_Type (Utyp) then
5970 if Is_Concurrent_Type (Root_Type (Utyp)) then
5971 Utyp := Root_Type (Utyp);
5972
5973 elsif Is_Private_Type (Root_Type (Utyp))
5974 and then Present (Full_View (Root_Type (Utyp)))
5975 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5976 then
5977 Utyp := Full_View (Root_Type (Utyp));
5978 end if;
5979 end if;
5980
5981 -- Handle private types
5982
5983 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5984 Utyp := Full_View (Utyp);
5985 end if;
5986
5987 -- Handle protected and task types
5988
5989 if Is_Concurrent_Type (Utyp)
5990 and then Present (Corresponding_Record_Type (Utyp))
5991 then
5992 Utyp := Corresponding_Record_Type (Utyp);
5993 end if;
5994
5995 Utyp := Underlying_Type (Base_Type (Utyp));
5996
5997 -- Deal with untagged derivation of private views. If the parent is
5998 -- now known to be protected, the finalization routine is the one
5999 -- defined on the corresponding record of the ancestor (corresponding
6000 -- records do not automatically inherit operations, but maybe they
6001 -- should???)
6002
6003 if Is_Untagged_Derivation (Btyp) then
6004 if Is_Protected_Type (Btyp) then
6005 Utyp := Corresponding_Record_Type (Root_Type (Btyp));
6006
6007 else
6008 Utyp := Underlying_Type (Root_Type (Btyp));
6009
6010 if Is_Protected_Type (Utyp) then
6011 Utyp := Corresponding_Record_Type (Utyp);
6012 end if;
6013 end if;
6014 end if;
6015
6016 -- If the underlying_type is a subtype, we are dealing with the
6017 -- completion of a private type. We need to access the base type and
6018 -- generate a conversion to it.
6019
6020 if Utyp /= Base_Type (Utyp) then
6021 pragma Assert (Is_Private_Type (Typ));
6022
6023 Utyp := Base_Type (Utyp);
6024 end if;
6025
6026 -- When dealing with an internally built full view for a type with
6027 -- unknown discriminants, use the original record type.
6028
6029 if Is_Underlying_Record_View (Utyp) then
6030 Utyp := Etype (Utyp);
6031 end if;
6032
6033 return TSS (Utyp, TSS_Finalize_Address);
6034 end Finalize_Address;
6035
6036 ------------------------
6037 -- Find_Interface_ADT --
6038 ------------------------
6039
6040 function Find_Interface_ADT
6041 (T : Entity_Id;
6042 Iface : Entity_Id) return Elmt_Id
6043 is
6044 ADT : Elmt_Id;
6045 Typ : Entity_Id := T;
6046
6047 begin
6048 pragma Assert (Is_Interface (Iface));
6049
6050 -- Handle private types
6051
6052 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6053 Typ := Full_View (Typ);
6054 end if;
6055
6056 -- Handle access types
6057
6058 if Is_Access_Type (Typ) then
6059 Typ := Designated_Type (Typ);
6060 end if;
6061
6062 -- Handle task and protected types implementing interfaces
6063
6064 if Is_Concurrent_Type (Typ) then
6065 Typ := Corresponding_Record_Type (Typ);
6066 end if;
6067
6068 pragma Assert
6069 (not Is_Class_Wide_Type (Typ)
6070 and then Ekind (Typ) /= E_Incomplete_Type);
6071
6072 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6073 return First_Elmt (Access_Disp_Table (Typ));
6074
6075 else
6076 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
6077 while Present (ADT)
6078 and then Present (Related_Type (Node (ADT)))
6079 and then Related_Type (Node (ADT)) /= Iface
6080 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
6081 Use_Full_View => True)
6082 loop
6083 Next_Elmt (ADT);
6084 end loop;
6085
6086 pragma Assert (Present (Related_Type (Node (ADT))));
6087 return ADT;
6088 end if;
6089 end Find_Interface_ADT;
6090
6091 ------------------------
6092 -- Find_Interface_Tag --
6093 ------------------------
6094
6095 function Find_Interface_Tag
6096 (T : Entity_Id;
6097 Iface : Entity_Id) return Entity_Id
6098 is
6099 AI_Tag : Entity_Id := Empty;
6100 Found : Boolean := False;
6101 Typ : Entity_Id := T;
6102
6103 procedure Find_Tag (Typ : Entity_Id);
6104 -- Internal subprogram used to recursively climb to the ancestors
6105
6106 --------------
6107 -- Find_Tag --
6108 --------------
6109
6110 procedure Find_Tag (Typ : Entity_Id) is
6111 AI_Elmt : Elmt_Id;
6112 AI : Node_Id;
6113
6114 begin
6115 -- This routine does not handle the case in which the interface is an
6116 -- ancestor of Typ. That case is handled by the enclosing subprogram.
6117
6118 pragma Assert (Typ /= Iface);
6119
6120 -- Climb to the root type handling private types
6121
6122 if Present (Full_View (Etype (Typ))) then
6123 if Full_View (Etype (Typ)) /= Typ then
6124 Find_Tag (Full_View (Etype (Typ)));
6125 end if;
6126
6127 elsif Etype (Typ) /= Typ then
6128 Find_Tag (Etype (Typ));
6129 end if;
6130
6131 -- Traverse the list of interfaces implemented by the type
6132
6133 if not Found
6134 and then Present (Interfaces (Typ))
6135 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
6136 then
6137 -- Skip the tag associated with the primary table
6138
6139 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
6140 pragma Assert (Present (AI_Tag));
6141
6142 AI_Elmt := First_Elmt (Interfaces (Typ));
6143 while Present (AI_Elmt) loop
6144 AI := Node (AI_Elmt);
6145
6146 if AI = Iface
6147 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
6148 then
6149 Found := True;
6150 return;
6151 end if;
6152
6153 AI_Tag := Next_Tag_Component (AI_Tag);
6154 Next_Elmt (AI_Elmt);
6155 end loop;
6156 end if;
6157 end Find_Tag;
6158
6159 -- Start of processing for Find_Interface_Tag
6160
6161 begin
6162 pragma Assert (Is_Interface (Iface));
6163
6164 -- Handle access types
6165
6166 if Is_Access_Type (Typ) then
6167 Typ := Designated_Type (Typ);
6168 end if;
6169
6170 -- Handle class-wide types
6171
6172 if Is_Class_Wide_Type (Typ) then
6173 Typ := Root_Type (Typ);
6174 end if;
6175
6176 -- Handle private types
6177
6178 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6179 Typ := Full_View (Typ);
6180 end if;
6181
6182 -- Handle entities from the limited view
6183
6184 if Ekind (Typ) = E_Incomplete_Type then
6185 pragma Assert (Present (Non_Limited_View (Typ)));
6186 Typ := Non_Limited_View (Typ);
6187 end if;
6188
6189 -- Handle task and protected types implementing interfaces
6190
6191 if Is_Concurrent_Type (Typ) then
6192 Typ := Corresponding_Record_Type (Typ);
6193 end if;
6194
6195 -- If the interface is an ancestor of the type, then it shared the
6196 -- primary dispatch table.
6197
6198 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6199 return First_Tag_Component (Typ);
6200
6201 -- Otherwise we need to search for its associated tag component
6202
6203 else
6204 Find_Tag (Typ);
6205 return AI_Tag;
6206 end if;
6207 end Find_Interface_Tag;
6208
6209 ---------------------------
6210 -- Find_Optional_Prim_Op --
6211 ---------------------------
6212
6213 function Find_Optional_Prim_Op
6214 (T : Entity_Id; Name : Name_Id) return Entity_Id
6215 is
6216 Prim : Elmt_Id;
6217 Typ : Entity_Id := T;
6218 Op : Entity_Id;
6219
6220 begin
6221 if Is_Class_Wide_Type (Typ) then
6222 Typ := Root_Type (Typ);
6223 end if;
6224
6225 Typ := Underlying_Type (Typ);
6226
6227 -- Loop through primitive operations
6228
6229 Prim := First_Elmt (Primitive_Operations (Typ));
6230 while Present (Prim) loop
6231 Op := Node (Prim);
6232
6233 -- We can retrieve primitive operations by name if it is an internal
6234 -- name. For equality we must check that both of its operands have
6235 -- the same type, to avoid confusion with user-defined equalities
6236 -- than may have a asymmetric signature.
6237
6238 exit when Chars (Op) = Name
6239 and then
6240 (Name /= Name_Op_Eq
6241 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
6242
6243 Next_Elmt (Prim);
6244 end loop;
6245
6246 return Node (Prim); -- Empty if not found
6247 end Find_Optional_Prim_Op;
6248
6249 ---------------------------
6250 -- Find_Optional_Prim_Op --
6251 ---------------------------
6252
6253 function Find_Optional_Prim_Op
6254 (T : Entity_Id;
6255 Name : TSS_Name_Type) return Entity_Id
6256 is
6257 Inher_Op : Entity_Id := Empty;
6258 Own_Op : Entity_Id := Empty;
6259 Prim_Elmt : Elmt_Id;
6260 Prim_Id : Entity_Id;
6261 Typ : Entity_Id := T;
6262
6263 begin
6264 if Is_Class_Wide_Type (Typ) then
6265 Typ := Root_Type (Typ);
6266 end if;
6267
6268 Typ := Underlying_Type (Typ);
6269
6270 -- This search is based on the assertion that the dispatching version
6271 -- of the TSS routine always precedes the real primitive.
6272
6273 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6274 while Present (Prim_Elmt) loop
6275 Prim_Id := Node (Prim_Elmt);
6276
6277 if Is_TSS (Prim_Id, Name) then
6278 if Present (Alias (Prim_Id)) then
6279 Inher_Op := Prim_Id;
6280 else
6281 Own_Op := Prim_Id;
6282 end if;
6283 end if;
6284
6285 Next_Elmt (Prim_Elmt);
6286 end loop;
6287
6288 if Present (Own_Op) then
6289 return Own_Op;
6290 elsif Present (Inher_Op) then
6291 return Inher_Op;
6292 else
6293 return Empty;
6294 end if;
6295 end Find_Optional_Prim_Op;
6296
6297 ------------------
6298 -- Find_Prim_Op --
6299 ------------------
6300
6301 function Find_Prim_Op
6302 (T : Entity_Id; Name : Name_Id) return Entity_Id
6303 is
6304 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6305 begin
6306 if No (Result) then
6307 raise Program_Error;
6308 end if;
6309
6310 return Result;
6311 end Find_Prim_Op;
6312
6313 ------------------
6314 -- Find_Prim_Op --
6315 ------------------
6316
6317 function Find_Prim_Op
6318 (T : Entity_Id;
6319 Name : TSS_Name_Type) return Entity_Id
6320 is
6321 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6322 begin
6323 if No (Result) then
6324 raise Program_Error;
6325 end if;
6326
6327 return Result;
6328 end Find_Prim_Op;
6329
6330 ----------------------------
6331 -- Find_Protection_Object --
6332 ----------------------------
6333
6334 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
6335 S : Entity_Id;
6336
6337 begin
6338 S := Scop;
6339 while Present (S) loop
6340 if Ekind (S) in E_Entry | E_Entry_Family | E_Function | E_Procedure
6341 and then Present (Protection_Object (S))
6342 then
6343 return Protection_Object (S);
6344 end if;
6345
6346 S := Scope (S);
6347 end loop;
6348
6349 -- If we do not find a Protection object in the scope chain, then
6350 -- something has gone wrong, most likely the object was never created.
6351
6352 raise Program_Error;
6353 end Find_Protection_Object;
6354
6355 --------------------------
6356 -- Find_Protection_Type --
6357 --------------------------
6358
6359 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
6360 Comp : Entity_Id;
6361 Typ : Entity_Id := Conc_Typ;
6362
6363 begin
6364 if Is_Concurrent_Type (Typ) then
6365 Typ := Corresponding_Record_Type (Typ);
6366 end if;
6367
6368 -- Since restriction violations are not considered serious errors, the
6369 -- expander remains active, but may leave the corresponding record type
6370 -- malformed. In such cases, component _object is not available so do
6371 -- not look for it.
6372
6373 if not Analyzed (Typ) then
6374 return Empty;
6375 end if;
6376
6377 Comp := First_Component (Typ);
6378 while Present (Comp) loop
6379 if Chars (Comp) = Name_uObject then
6380 return Base_Type (Etype (Comp));
6381 end if;
6382
6383 Next_Component (Comp);
6384 end loop;
6385
6386 -- The corresponding record of a protected type should always have an
6387 -- _object field.
6388
6389 raise Program_Error;
6390 end Find_Protection_Type;
6391
6392 function Find_Storage_Op
6393 (Typ : Entity_Id;
6394 Nam : Name_Id) return Entity_Id
6395 is
6396 use Sem_Util.Storage_Model_Support;
6397
6398 begin
6399 if Has_Storage_Model_Type_Aspect (Typ) then
6400 declare
6401 SMT_Op : constant Entity_Id :=
6402 Get_Storage_Model_Type_Entity (Typ, Nam);
6403 begin
6404 if not Present (SMT_Op) then
6405 raise Program_Error;
6406 else
6407 return SMT_Op;
6408 end if;
6409 end;
6410
6411 -- Otherwise we assume that Typ is a descendant of Root_Storage_Pool
6412
6413 else
6414 return Find_Prim_Op (Typ, Nam);
6415 end if;
6416 end Find_Storage_Op;
6417
6418 -----------------------
6419 -- Find_Hook_Context --
6420 -----------------------
6421
6422 function Find_Hook_Context (N : Node_Id) return Node_Id is
6423 Par : Node_Id;
6424 Top : Node_Id;
6425
6426 Wrapped_Node : Node_Id;
6427 -- Note: if we are in a transient scope, we want to reuse it as
6428 -- the context for actions insertion, if possible. But if N is itself
6429 -- part of the stored actions for the current transient scope,
6430 -- then we need to insert at the appropriate (inner) location in
6431 -- the not as an action on Node_To_Be_Wrapped.
6432
6433 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
6434
6435 begin
6436 -- When the node is inside a case/if expression, the lifetime of any
6437 -- temporary controlled object is extended. Find a suitable insertion
6438 -- node by locating the topmost case or if expressions.
6439
6440 if In_Cond_Expr then
6441 Par := N;
6442 Top := N;
6443 while Present (Par) loop
6444 if Nkind (Original_Node (Par)) in
6445 N_Case_Expression | N_If_Expression
6446 then
6447 Top := Par;
6448
6449 -- Prevent the search from going too far
6450
6451 elsif Is_Body_Or_Package_Declaration (Par) then
6452 exit;
6453 end if;
6454
6455 Par := Parent (Par);
6456 end loop;
6457
6458 -- The topmost case or if expression is now recovered, but it may
6459 -- still not be the correct place to add generated code. Climb to
6460 -- find a parent that is part of a declarative or statement list,
6461 -- and is not a list of actuals in a call.
6462
6463 Par := Top;
6464 while Present (Par) loop
6465 if Is_List_Member (Par)
6466 and then Nkind (Par) not in N_Component_Association
6467 | N_Discriminant_Association
6468 | N_Parameter_Association
6469 | N_Pragma_Argument_Association
6470 | N_Aggregate
6471 | N_Delta_Aggregate
6472 | N_Extension_Aggregate
6473 and then Nkind (Parent (Par)) not in N_Function_Call
6474 | N_Procedure_Call_Statement
6475 | N_Entry_Call_Statement
6476
6477 then
6478 return Par;
6479
6480 -- Prevent the search from going too far
6481
6482 elsif Is_Body_Or_Package_Declaration (Par) then
6483 exit;
6484 end if;
6485
6486 Par := Parent (Par);
6487 end loop;
6488
6489 return Par;
6490
6491 else
6492 Par := N;
6493 while Present (Par) loop
6494
6495 -- Keep climbing past various operators
6496
6497 if Nkind (Parent (Par)) in N_Op
6498 or else Nkind (Parent (Par)) in N_And_Then | N_Or_Else
6499 then
6500 Par := Parent (Par);
6501 else
6502 exit;
6503 end if;
6504 end loop;
6505
6506 Top := Par;
6507
6508 -- The node may be located in a pragma in which case return the
6509 -- pragma itself:
6510
6511 -- pragma Precondition (... and then Ctrl_Func_Call ...);
6512
6513 -- Similar case occurs when the node is related to an object
6514 -- declaration or assignment:
6515
6516 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
6517
6518 -- Another case to consider is when the node is part of a return
6519 -- statement:
6520
6521 -- return ... and then Ctrl_Func_Call ...;
6522
6523 -- Another case is when the node acts as a formal in a procedure
6524 -- call statement:
6525
6526 -- Proc (... and then Ctrl_Func_Call ...);
6527
6528 if Scope_Is_Transient then
6529 Wrapped_Node := Node_To_Be_Wrapped;
6530 else
6531 Wrapped_Node := Empty;
6532 end if;
6533
6534 while Present (Par) loop
6535 if Par = Wrapped_Node
6536 or else Nkind (Par) in N_Assignment_Statement
6537 | N_Object_Declaration
6538 | N_Pragma
6539 | N_Procedure_Call_Statement
6540 | N_Simple_Return_Statement
6541 then
6542 return Par;
6543
6544 -- Prevent the search from going too far
6545
6546 elsif Is_Body_Or_Package_Declaration (Par) then
6547 exit;
6548 end if;
6549
6550 Par := Parent (Par);
6551 end loop;
6552
6553 -- Return the topmost short circuit operator
6554
6555 return Top;
6556 end if;
6557 end Find_Hook_Context;
6558
6559 ------------------------------
6560 -- Following_Address_Clause --
6561 ------------------------------
6562
6563 function Following_Address_Clause (D : Node_Id) return Node_Id is
6564 Id : constant Entity_Id := Defining_Identifier (D);
6565 Result : Node_Id;
6566 Par : Node_Id;
6567
6568 function Check_Decls (D : Node_Id) return Node_Id;
6569 -- This internal function differs from the main function in that it
6570 -- gets called to deal with a following package private part, and
6571 -- it checks declarations starting with D (the main function checks
6572 -- declarations following D). If D is Empty, then Empty is returned.
6573
6574 -----------------
6575 -- Check_Decls --
6576 -----------------
6577
6578 function Check_Decls (D : Node_Id) return Node_Id is
6579 Decl : Node_Id;
6580
6581 begin
6582 Decl := D;
6583 while Present (Decl) loop
6584 if Nkind (Decl) = N_At_Clause
6585 and then Chars (Identifier (Decl)) = Chars (Id)
6586 then
6587 return Decl;
6588
6589 elsif Nkind (Decl) = N_Attribute_Definition_Clause
6590 and then Chars (Decl) = Name_Address
6591 and then Chars (Name (Decl)) = Chars (Id)
6592 then
6593 return Decl;
6594 end if;
6595
6596 Next (Decl);
6597 end loop;
6598
6599 -- Otherwise not found, return Empty
6600
6601 return Empty;
6602 end Check_Decls;
6603
6604 -- Start of processing for Following_Address_Clause
6605
6606 begin
6607 -- If parser detected no address clause for the identifier in question,
6608 -- then the answer is a quick NO, without the need for a search.
6609
6610 if not Get_Name_Table_Boolean1 (Chars (Id)) then
6611 return Empty;
6612 end if;
6613
6614 -- Otherwise search current declarative unit
6615
6616 Result := Check_Decls (Next (D));
6617
6618 if Present (Result) then
6619 return Result;
6620 end if;
6621
6622 -- Check for possible package private part following
6623
6624 Par := Parent (D);
6625
6626 if Nkind (Par) = N_Package_Specification
6627 and then Visible_Declarations (Par) = List_Containing (D)
6628 and then Present (Private_Declarations (Par))
6629 then
6630 -- Private part present, check declarations there
6631
6632 return Check_Decls (First (Private_Declarations (Par)));
6633
6634 else
6635 -- No private part, clause not found, return Empty
6636
6637 return Empty;
6638 end if;
6639 end Following_Address_Clause;
6640
6641 ----------------------
6642 -- Force_Evaluation --
6643 ----------------------
6644
6645 procedure Force_Evaluation
6646 (Exp : Node_Id;
6647 Name_Req : Boolean := False;
6648 Related_Id : Entity_Id := Empty;
6649 Is_Low_Bound : Boolean := False;
6650 Is_High_Bound : Boolean := False;
6651 Discr_Number : Int := 0;
6652 Mode : Force_Evaluation_Mode := Relaxed)
6653 is
6654 begin
6655 Remove_Side_Effects
6656 (Exp => Exp,
6657 Name_Req => Name_Req,
6658 Variable_Ref => True,
6659 Renaming_Req => False,
6660 Related_Id => Related_Id,
6661 Is_Low_Bound => Is_Low_Bound,
6662 Is_High_Bound => Is_High_Bound,
6663 Discr_Number => Discr_Number,
6664 Check_Side_Effects =>
6665 Is_Static_Expression (Exp)
6666 or else Mode = Relaxed);
6667 end Force_Evaluation;
6668
6669 ---------------------------------
6670 -- Fully_Qualified_Name_String --
6671 ---------------------------------
6672
6673 function Fully_Qualified_Name_String
6674 (E : Entity_Id;
6675 Append_NUL : Boolean := True) return String_Id
6676 is
6677 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6678 -- Compute recursively the qualified name without NUL at the end, adding
6679 -- it to the currently started string being generated
6680
6681 ----------------------------------
6682 -- Internal_Full_Qualified_Name --
6683 ----------------------------------
6684
6685 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6686 Ent : Entity_Id;
6687
6688 begin
6689 -- Deal properly with child units
6690
6691 if Nkind (E) = N_Defining_Program_Unit_Name then
6692 Ent := Defining_Identifier (E);
6693 else
6694 Ent := E;
6695 end if;
6696
6697 -- Compute qualification recursively (only "Standard" has no scope)
6698
6699 if Present (Scope (Scope (Ent))) then
6700 Internal_Full_Qualified_Name (Scope (Ent));
6701 Store_String_Char (Get_Char_Code ('.'));
6702 end if;
6703
6704 -- Every entity should have a name except some expanded blocks
6705 -- don't bother about those.
6706
6707 if Chars (Ent) = No_Name then
6708 return;
6709 end if;
6710
6711 -- Generates the entity name in upper case
6712
6713 Get_Decoded_Name_String (Chars (Ent));
6714 Set_Casing (All_Upper_Case);
6715 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6716 return;
6717 end Internal_Full_Qualified_Name;
6718
6719 -- Start of processing for Full_Qualified_Name
6720
6721 begin
6722 Start_String;
6723 Internal_Full_Qualified_Name (E);
6724
6725 if Append_NUL then
6726 Store_String_Char (Get_Char_Code (ASCII.NUL));
6727 end if;
6728
6729 return End_String;
6730 end Fully_Qualified_Name_String;
6731
6732 ---------------------------------
6733 -- Get_Current_Value_Condition --
6734 ---------------------------------
6735
6736 -- Note: the implementation of this procedure is very closely tied to the
6737 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6738 -- interpret Current_Value fields set by the Set procedure, so the two
6739 -- procedures need to be closely coordinated.
6740
6741 procedure Get_Current_Value_Condition
6742 (Var : Node_Id;
6743 Op : out Node_Kind;
6744 Val : out Node_Id)
6745 is
6746 Loc : constant Source_Ptr := Sloc (Var);
6747 Ent : constant Entity_Id := Entity (Var);
6748
6749 procedure Process_Current_Value_Condition (N : Node_Id; S : Boolean);
6750 -- N is an expression which holds either True (S = True) or False (S =
6751 -- False) in the condition. This procedure digs out the expression and
6752 -- if it refers to Ent, sets Op and Val appropriately.
6753
6754 -------------------------------------
6755 -- Process_Current_Value_Condition --
6756 -------------------------------------
6757
6758 procedure Process_Current_Value_Condition
6759 (N : Node_Id;
6760 S : Boolean)
6761 is
6762 Cond : Node_Id;
6763 Prev_Cond : Node_Id;
6764 Sens : Boolean;
6765
6766 begin
6767 Cond := N;
6768 Sens := S;
6769
6770 loop
6771 Prev_Cond := Cond;
6772
6773 -- Deal with NOT operators, inverting sense
6774
6775 while Nkind (Cond) = N_Op_Not loop
6776 Cond := Right_Opnd (Cond);
6777 Sens := not Sens;
6778 end loop;
6779
6780 -- Deal with conversions, qualifications, and expressions with
6781 -- actions.
6782
6783 while Nkind (Cond) in N_Type_Conversion
6784 | N_Qualified_Expression
6785 | N_Expression_With_Actions
6786 loop
6787 Cond := Expression (Cond);
6788 end loop;
6789
6790 exit when Cond = Prev_Cond;
6791 end loop;
6792
6793 -- Deal with AND THEN and AND cases
6794
6795 if Nkind (Cond) in N_And_Then | N_Op_And then
6796
6797 -- Don't ever try to invert a condition that is of the form of an
6798 -- AND or AND THEN (since we are not doing sufficiently general
6799 -- processing to allow this).
6800
6801 if Sens = False then
6802 Op := N_Empty;
6803 Val := Empty;
6804 return;
6805 end if;
6806
6807 -- Recursively process AND and AND THEN branches
6808
6809 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6810 pragma Assert (Op'Valid);
6811
6812 if Op /= N_Empty then
6813 return;
6814 end if;
6815
6816 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6817 return;
6818
6819 -- Case of relational operator
6820
6821 elsif Nkind (Cond) in N_Op_Compare then
6822 Op := Nkind (Cond);
6823
6824 -- Invert sense of test if inverted test
6825
6826 if Sens = False then
6827 case Op is
6828 when N_Op_Eq => Op := N_Op_Ne;
6829 when N_Op_Ne => Op := N_Op_Eq;
6830 when N_Op_Lt => Op := N_Op_Ge;
6831 when N_Op_Gt => Op := N_Op_Le;
6832 when N_Op_Le => Op := N_Op_Gt;
6833 when N_Op_Ge => Op := N_Op_Lt;
6834 when others => raise Program_Error;
6835 end case;
6836 end if;
6837
6838 -- Case of entity op value
6839
6840 if Is_Entity_Name (Left_Opnd (Cond))
6841 and then Ent = Entity (Left_Opnd (Cond))
6842 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6843 then
6844 Val := Right_Opnd (Cond);
6845
6846 -- Case of value op entity
6847
6848 elsif Is_Entity_Name (Right_Opnd (Cond))
6849 and then Ent = Entity (Right_Opnd (Cond))
6850 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6851 then
6852 Val := Left_Opnd (Cond);
6853
6854 -- We are effectively swapping operands
6855
6856 case Op is
6857 when N_Op_Eq => null;
6858 when N_Op_Ne => null;
6859 when N_Op_Lt => Op := N_Op_Gt;
6860 when N_Op_Gt => Op := N_Op_Lt;
6861 when N_Op_Le => Op := N_Op_Ge;
6862 when N_Op_Ge => Op := N_Op_Le;
6863 when others => raise Program_Error;
6864 end case;
6865
6866 else
6867 Op := N_Empty;
6868 end if;
6869
6870 return;
6871
6872 elsif Nkind (Cond) in N_Type_Conversion
6873 | N_Qualified_Expression
6874 | N_Expression_With_Actions
6875 then
6876 Cond := Expression (Cond);
6877
6878 -- Case of Boolean variable reference, return as though the
6879 -- reference had said var = True.
6880
6881 else
6882 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6883 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6884
6885 if Sens = False then
6886 Op := N_Op_Ne;
6887 else
6888 Op := N_Op_Eq;
6889 end if;
6890 end if;
6891 end if;
6892 end Process_Current_Value_Condition;
6893
6894 -- Start of processing for Get_Current_Value_Condition
6895
6896 begin
6897 Op := N_Empty;
6898 Val := Empty;
6899
6900 -- Immediate return, nothing doing, if this is not an object
6901
6902 if not Is_Object (Ent) then
6903 return;
6904 end if;
6905
6906 -- In GNATprove mode we don't want to use current value optimizer, in
6907 -- particular for loop invariant expressions and other assertions that
6908 -- act as cut points for proof. The optimizer often folds expressions
6909 -- into True/False where they trivially follow from the previous
6910 -- assignments, but this deprives proof from the information needed to
6911 -- discharge checks that are beyond the scope of the value optimizer.
6912
6913 if GNATprove_Mode then
6914 return;
6915 end if;
6916
6917 -- Otherwise examine current value
6918
6919 declare
6920 CV : constant Node_Id := Current_Value (Ent);
6921 Sens : Boolean;
6922 Stm : Node_Id;
6923
6924 begin
6925 -- If statement. Condition is known true in THEN section, known False
6926 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6927
6928 if Nkind (CV) = N_If_Statement then
6929
6930 -- Before start of IF statement
6931
6932 if Loc < Sloc (CV) then
6933 return;
6934
6935 -- After end of IF statement
6936
6937 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6938 return;
6939 end if;
6940
6941 -- At this stage we know that we are within the IF statement, but
6942 -- unfortunately, the tree does not record the SLOC of the ELSE so
6943 -- we cannot use a simple SLOC comparison to distinguish between
6944 -- the then/else statements, so we have to climb the tree.
6945
6946 declare
6947 N : Node_Id;
6948
6949 begin
6950 N := Parent (Var);
6951 while Parent (N) /= CV loop
6952 N := Parent (N);
6953
6954 -- If we fall off the top of the tree, then that's odd, but
6955 -- perhaps it could occur in some error situation, and the
6956 -- safest response is simply to assume that the outcome of
6957 -- the condition is unknown. No point in bombing during an
6958 -- attempt to optimize things.
6959
6960 if No (N) then
6961 return;
6962 end if;
6963 end loop;
6964
6965 -- Now we have N pointing to a node whose parent is the IF
6966 -- statement in question, so now we can tell if we are within
6967 -- the THEN statements.
6968
6969 if Is_List_Member (N)
6970 and then List_Containing (N) = Then_Statements (CV)
6971 then
6972 Sens := True;
6973
6974 -- If the variable reference does not come from source, we
6975 -- cannot reliably tell whether it appears in the else part.
6976 -- In particular, if it appears in generated code for a node
6977 -- that requires finalization, it may be attached to a list
6978 -- that has not been yet inserted into the code. For now,
6979 -- treat it as unknown.
6980
6981 elsif not Comes_From_Source (N) then
6982 return;
6983
6984 -- Otherwise we must be in ELSIF or ELSE part
6985
6986 else
6987 Sens := False;
6988 end if;
6989 end;
6990
6991 -- ELSIF part. Condition is known true within the referenced
6992 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6993 -- and unknown before the ELSE part or after the IF statement.
6994
6995 elsif Nkind (CV) = N_Elsif_Part then
6996
6997 -- if the Elsif_Part had condition_actions, the elsif has been
6998 -- rewritten as a nested if, and the original elsif_part is
6999 -- detached from the tree, so there is no way to obtain useful
7000 -- information on the current value of the variable.
7001 -- Can this be improved ???
7002
7003 if No (Parent (CV)) then
7004 return;
7005 end if;
7006
7007 Stm := Parent (CV);
7008
7009 -- If the tree has been otherwise rewritten there is nothing
7010 -- else to be done either.
7011
7012 if Nkind (Stm) /= N_If_Statement then
7013 return;
7014 end if;
7015
7016 -- Before start of ELSIF part
7017
7018 if Loc < Sloc (CV) then
7019 return;
7020
7021 -- After end of IF statement
7022
7023 elsif Loc >= Sloc (Stm) +
7024 Text_Ptr (UI_To_Int (End_Span (Stm)))
7025 then
7026 return;
7027 end if;
7028
7029 -- Again we lack the SLOC of the ELSE, so we need to climb the
7030 -- tree to see if we are within the ELSIF part in question.
7031
7032 declare
7033 N : Node_Id;
7034
7035 begin
7036 N := Parent (Var);
7037 while Parent (N) /= Stm loop
7038 N := Parent (N);
7039
7040 -- If we fall off the top of the tree, then that's odd, but
7041 -- perhaps it could occur in some error situation, and the
7042 -- safest response is simply to assume that the outcome of
7043 -- the condition is unknown. No point in bombing during an
7044 -- attempt to optimize things.
7045
7046 if No (N) then
7047 return;
7048 end if;
7049 end loop;
7050
7051 -- Now we have N pointing to a node whose parent is the IF
7052 -- statement in question, so see if is the ELSIF part we want.
7053 -- the THEN statements.
7054
7055 if N = CV then
7056 Sens := True;
7057
7058 -- Otherwise we must be in subsequent ELSIF or ELSE part
7059
7060 else
7061 Sens := False;
7062 end if;
7063 end;
7064
7065 -- Iteration scheme of while loop. The condition is known to be
7066 -- true within the body of the loop.
7067
7068 elsif Nkind (CV) = N_Iteration_Scheme then
7069 declare
7070 Loop_Stmt : constant Node_Id := Parent (CV);
7071
7072 begin
7073 -- Before start of body of loop
7074
7075 if Loc < Sloc (Loop_Stmt) then
7076 return;
7077
7078 -- After end of LOOP statement
7079
7080 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
7081 return;
7082
7083 -- We are within the body of the loop
7084
7085 else
7086 Sens := True;
7087 end if;
7088 end;
7089
7090 -- All other cases of Current_Value settings
7091
7092 else
7093 return;
7094 end if;
7095
7096 -- If we fall through here, then we have a reportable condition, Sens
7097 -- is True if the condition is true and False if it needs inverting.
7098
7099 Process_Current_Value_Condition (Condition (CV), Sens);
7100 end;
7101 end Get_Current_Value_Condition;
7102
7103 -----------------------
7104 -- Get_Index_Subtype --
7105 -----------------------
7106
7107 function Get_Index_Subtype (N : Node_Id) return Entity_Id is
7108 P_Type : Entity_Id := Etype (Prefix (N));
7109 Indx : Node_Id;
7110 J : Int;
7111
7112 begin
7113 if Is_Access_Type (P_Type) then
7114 P_Type := Designated_Type (P_Type);
7115 end if;
7116
7117 if No (Expressions (N)) then
7118 J := 1;
7119 else
7120 J := UI_To_Int (Expr_Value (First (Expressions (N))));
7121 end if;
7122
7123 Indx := First_Index (P_Type);
7124 while J > 1 loop
7125 Next_Index (Indx);
7126 J := J - 1;
7127 end loop;
7128
7129 return Etype (Indx);
7130 end Get_Index_Subtype;
7131
7132 -----------------------
7133 -- Get_Mapped_Entity --
7134 -----------------------
7135
7136 function Get_Mapped_Entity (E : Entity_Id) return Entity_Id is
7137 begin
7138 return Type_Map.Get (E);
7139 end Get_Mapped_Entity;
7140
7141 ---------------------
7142 -- Get_Stream_Size --
7143 ---------------------
7144
7145 function Get_Stream_Size (E : Entity_Id) return Uint is
7146 begin
7147 -- If we have a Stream_Size clause for this type use it
7148
7149 if Has_Stream_Size_Clause (E) then
7150 return Static_Integer (Expression (Stream_Size_Clause (E)));
7151
7152 -- Otherwise the Stream_Size is the size of the type
7153
7154 else
7155 return Esize (E);
7156 end if;
7157 end Get_Stream_Size;
7158
7159 ---------------------------
7160 -- Has_Access_Constraint --
7161 ---------------------------
7162
7163 function Has_Access_Constraint (E : Entity_Id) return Boolean is
7164 Disc : Entity_Id;
7165 T : constant Entity_Id := Etype (E);
7166
7167 begin
7168 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
7169 Disc := First_Discriminant (T);
7170 while Present (Disc) loop
7171 if Is_Access_Type (Etype (Disc)) then
7172 return True;
7173 end if;
7174
7175 Next_Discriminant (Disc);
7176 end loop;
7177
7178 return False;
7179 else
7180 return False;
7181 end if;
7182 end Has_Access_Constraint;
7183
7184 --------------------
7185 -- Homonym_Number --
7186 --------------------
7187
7188 function Homonym_Number (Subp : Entity_Id) return Pos is
7189 Hom : Entity_Id := Homonym (Subp);
7190 Count : Pos := 1;
7191
7192 begin
7193 while Present (Hom) loop
7194 if Scope (Hom) = Scope (Subp) then
7195 Count := Count + 1;
7196 end if;
7197
7198 Hom := Homonym (Hom);
7199 end loop;
7200
7201 return Count;
7202 end Homonym_Number;
7203
7204 -----------------------------------
7205 -- In_Library_Level_Package_Body --
7206 -----------------------------------
7207
7208 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
7209 begin
7210 -- First determine whether the entity appears at the library level, then
7211 -- look at the containing unit.
7212
7213 if Is_Library_Level_Entity (Id) then
7214 declare
7215 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
7216
7217 begin
7218 return Nkind (Unit (Container)) = N_Package_Body;
7219 end;
7220 end if;
7221
7222 return False;
7223 end In_Library_Level_Package_Body;
7224
7225 ------------------------------
7226 -- In_Unconditional_Context --
7227 ------------------------------
7228
7229 function In_Unconditional_Context (Node : Node_Id) return Boolean is
7230 P : Node_Id;
7231
7232 begin
7233 P := Node;
7234 while Present (P) loop
7235 case Nkind (P) is
7236 when N_Subprogram_Body => return True;
7237 when N_If_Statement => return False;
7238 when N_Loop_Statement => return False;
7239 when N_Case_Statement => return False;
7240 when others => P := Parent (P);
7241 end case;
7242 end loop;
7243
7244 return False;
7245 end In_Unconditional_Context;
7246
7247 -------------------
7248 -- Insert_Action --
7249 -------------------
7250
7251 procedure Insert_Action
7252 (Assoc_Node : Node_Id;
7253 Ins_Action : Node_Id;
7254 Spec_Expr_OK : Boolean := False)
7255 is
7256 begin
7257 if Present (Ins_Action) then
7258 Insert_Actions
7259 (Assoc_Node => Assoc_Node,
7260 Ins_Actions => New_List (Ins_Action),
7261 Spec_Expr_OK => Spec_Expr_OK);
7262 end if;
7263 end Insert_Action;
7264
7265 -- Version with check(s) suppressed
7266
7267 procedure Insert_Action
7268 (Assoc_Node : Node_Id;
7269 Ins_Action : Node_Id;
7270 Suppress : Check_Id;
7271 Spec_Expr_OK : Boolean := False)
7272 is
7273 begin
7274 Insert_Actions
7275 (Assoc_Node => Assoc_Node,
7276 Ins_Actions => New_List (Ins_Action),
7277 Suppress => Suppress,
7278 Spec_Expr_OK => Spec_Expr_OK);
7279 end Insert_Action;
7280
7281 -------------------------
7282 -- Insert_Action_After --
7283 -------------------------
7284
7285 procedure Insert_Action_After
7286 (Assoc_Node : Node_Id;
7287 Ins_Action : Node_Id)
7288 is
7289 begin
7290 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
7291 end Insert_Action_After;
7292
7293 --------------------
7294 -- Insert_Actions --
7295 --------------------
7296
7297 procedure Insert_Actions
7298 (Assoc_Node : Node_Id;
7299 Ins_Actions : List_Id;
7300 Spec_Expr_OK : Boolean := False)
7301 is
7302 N : Node_Id;
7303 P : Node_Id;
7304
7305 Wrapped_Node : Node_Id := Empty;
7306
7307 begin
7308 if Is_Empty_List (Ins_Actions) then
7309 return;
7310 end if;
7311
7312 -- Insert the action when the context is "Handling of Default and Per-
7313 -- Object Expressions" only when requested by the caller.
7314
7315 if Spec_Expr_OK then
7316 null;
7317
7318 -- Ignore insert of actions from inside default expression (or other
7319 -- similar "spec expression") in the special spec-expression analyze
7320 -- mode. Any insertions at this point have no relevance, since we are
7321 -- only doing the analyze to freeze the types of any static expressions.
7322 -- See section "Handling of Default and Per-Object Expressions" in the
7323 -- spec of package Sem for further details.
7324
7325 elsif In_Spec_Expression then
7326 return;
7327 end if;
7328
7329 -- If the action derives from stuff inside a record, then the actions
7330 -- are attached to the current scope, to be inserted and analyzed on
7331 -- exit from the scope. The reason for this is that we may also be
7332 -- generating freeze actions at the same time, and they must eventually
7333 -- be elaborated in the correct order.
7334
7335 if Is_Record_Type (Current_Scope)
7336 and then not Is_Frozen (Current_Scope)
7337 then
7338 if No (Scope_Stack.Table
7339 (Scope_Stack.Last).Pending_Freeze_Actions)
7340 then
7341 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
7342 Ins_Actions;
7343 else
7344 Append_List
7345 (Ins_Actions,
7346 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
7347 end if;
7348
7349 return;
7350 end if;
7351
7352 -- We now intend to climb up the tree to find the right point to
7353 -- insert the actions. We start at Assoc_Node, unless this node is a
7354 -- subexpression in which case we start with its parent. We do this for
7355 -- two reasons. First it speeds things up. Second, if Assoc_Node is
7356 -- itself one of the special nodes like N_And_Then, then we assume that
7357 -- an initial request to insert actions for such a node does not expect
7358 -- the actions to get deposited in the node for later handling when the
7359 -- node is expanded, since clearly the node is being dealt with by the
7360 -- caller. Note that in the subexpression case, N is always the child we
7361 -- came from.
7362
7363 -- N_Raise_xxx_Error is an annoying special case, it is a statement
7364 -- if it has type Standard_Void_Type, and a subexpression otherwise.
7365 -- Procedure calls, and similarly procedure attribute references, are
7366 -- also statements.
7367
7368 if Nkind (Assoc_Node) in N_Subexpr
7369 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
7370 or else Etype (Assoc_Node) /= Standard_Void_Type)
7371 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
7372 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
7373 or else not Is_Procedure_Attribute_Name
7374 (Attribute_Name (Assoc_Node)))
7375 then
7376 N := Assoc_Node;
7377 P := Parent (Assoc_Node);
7378
7379 -- Nonsubexpression case. Note that N is initially Empty in this case
7380 -- (N is only guaranteed non-Empty in the subexpr case).
7381
7382 else
7383 N := Empty;
7384 P := Assoc_Node;
7385 end if;
7386
7387 -- Capture root of the transient scope
7388
7389 if Scope_Is_Transient then
7390 Wrapped_Node := Node_To_Be_Wrapped;
7391 end if;
7392
7393 loop
7394 pragma Assert (Present (P));
7395
7396 -- Make sure that inserted actions stay in the transient scope
7397
7398 if Present (Wrapped_Node) and then N = Wrapped_Node then
7399 Store_Before_Actions_In_Scope (Ins_Actions);
7400 return;
7401 end if;
7402
7403 case Nkind (P) is
7404
7405 -- Case of right operand of AND THEN or OR ELSE. Put the actions
7406 -- in the Actions field of the right operand. They will be moved
7407 -- out further when the AND THEN or OR ELSE operator is expanded.
7408 -- Nothing special needs to be done for the left operand since
7409 -- in that case the actions are executed unconditionally.
7410
7411 when N_Short_Circuit =>
7412 if N = Right_Opnd (P) then
7413
7414 -- We are now going to either append the actions to the
7415 -- actions field of the short-circuit operation. We will
7416 -- also analyze the actions now.
7417
7418 -- This analysis is really too early, the proper thing would
7419 -- be to just park them there now, and only analyze them if
7420 -- we find we really need them, and to it at the proper
7421 -- final insertion point. However attempting to this proved
7422 -- tricky, so for now we just kill current values before and
7423 -- after the analyze call to make sure we avoid peculiar
7424 -- optimizations from this out of order insertion.
7425
7426 Kill_Current_Values;
7427
7428 -- If P has already been expanded, we can't park new actions
7429 -- on it, so we need to expand them immediately, introducing
7430 -- an Expression_With_Actions. N can't be an expression
7431 -- with actions, or else then the actions would have been
7432 -- inserted at an inner level.
7433
7434 if Analyzed (P) then
7435 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
7436 Rewrite (N,
7437 Make_Expression_With_Actions (Sloc (N),
7438 Actions => Ins_Actions,
7439 Expression => Relocate_Node (N)));
7440 Analyze_And_Resolve (N);
7441
7442 elsif Present (Actions (P)) then
7443 Insert_List_After_And_Analyze
7444 (Last (Actions (P)), Ins_Actions);
7445 else
7446 Set_Actions (P, Ins_Actions);
7447 Analyze_List (Actions (P));
7448 end if;
7449
7450 Kill_Current_Values;
7451
7452 return;
7453 end if;
7454
7455 -- Then or Else dependent expression of an if expression. Add
7456 -- actions to Then_Actions or Else_Actions field as appropriate.
7457 -- The actions will be moved further out when the if is expanded.
7458
7459 when N_If_Expression =>
7460 declare
7461 ThenX : constant Node_Id := Next (First (Expressions (P)));
7462 ElseX : constant Node_Id := Next (ThenX);
7463
7464 begin
7465 -- If the enclosing expression is already analyzed, as
7466 -- is the case for nested elaboration checks, insert the
7467 -- conditional further out.
7468
7469 if Analyzed (P) then
7470 null;
7471
7472 -- Actions belong to the then expression, temporarily place
7473 -- them as Then_Actions of the if expression. They will be
7474 -- moved to the proper place later when the if expression is
7475 -- expanded.
7476
7477 elsif N = ThenX then
7478 if Present (Then_Actions (P)) then
7479 Insert_List_After_And_Analyze
7480 (Last (Then_Actions (P)), Ins_Actions);
7481 else
7482 Set_Then_Actions (P, Ins_Actions);
7483 Analyze_List (Then_Actions (P));
7484 end if;
7485
7486 return;
7487
7488 -- Else_Actions is treated the same as Then_Actions above
7489
7490 elsif N = ElseX then
7491 if Present (Else_Actions (P)) then
7492 Insert_List_After_And_Analyze
7493 (Last (Else_Actions (P)), Ins_Actions);
7494 else
7495 Set_Else_Actions (P, Ins_Actions);
7496 Analyze_List (Else_Actions (P));
7497 end if;
7498
7499 return;
7500
7501 -- Actions belong to the condition. In this case they are
7502 -- unconditionally executed, and so we can continue the
7503 -- search for the proper insert point.
7504
7505 else
7506 null;
7507 end if;
7508 end;
7509
7510 -- Alternative of case expression, we place the action in the
7511 -- Actions field of the case expression alternative, this will
7512 -- be handled when the case expression is expanded.
7513
7514 when N_Case_Expression_Alternative =>
7515 if Present (Actions (P)) then
7516 Insert_List_After_And_Analyze
7517 (Last (Actions (P)), Ins_Actions);
7518 else
7519 Set_Actions (P, Ins_Actions);
7520 Analyze_List (Actions (P));
7521 end if;
7522
7523 return;
7524
7525 -- Case of appearing within an Expressions_With_Actions node. When
7526 -- the new actions come from the expression of the expression with
7527 -- actions, they must be added to the existing actions. The other
7528 -- alternative is when the new actions are related to one of the
7529 -- existing actions of the expression with actions, and should
7530 -- never reach here: if actions are inserted on a statement
7531 -- within the Actions of an expression with actions, or on some
7532 -- subexpression of such a statement, then the outermost proper
7533 -- insertion point is right before the statement, and we should
7534 -- never climb up as far as the N_Expression_With_Actions itself.
7535
7536 when N_Expression_With_Actions =>
7537 if N = Expression (P) then
7538 if Is_Empty_List (Actions (P)) then
7539 Append_List_To (Actions (P), Ins_Actions);
7540 Analyze_List (Actions (P));
7541 else
7542 Insert_List_After_And_Analyze
7543 (Last (Actions (P)), Ins_Actions);
7544 end if;
7545
7546 return;
7547
7548 else
7549 raise Program_Error;
7550 end if;
7551
7552 -- Case of appearing in the condition of a while expression or
7553 -- elsif. We insert the actions into the Condition_Actions field.
7554 -- They will be moved further out when the while loop or elsif
7555 -- is analyzed.
7556
7557 when N_Elsif_Part
7558 | N_Iteration_Scheme
7559 =>
7560 if Present (Condition (P)) and then N = Condition (P) then
7561 if Present (Condition_Actions (P)) then
7562 Insert_List_After_And_Analyze
7563 (Last (Condition_Actions (P)), Ins_Actions);
7564 else
7565 Set_Condition_Actions (P, Ins_Actions);
7566
7567 -- Set the parent of the insert actions explicitly. This
7568 -- is not a syntactic field, but we need the parent field
7569 -- set, in particular so that freeze can understand that
7570 -- it is dealing with condition actions, and properly
7571 -- insert the freezing actions.
7572
7573 Set_Parent (Ins_Actions, P);
7574 Analyze_List (Condition_Actions (P));
7575 end if;
7576
7577 return;
7578 end if;
7579
7580 -- Statements, declarations, pragmas, representation clauses
7581
7582 when
7583 -- Statements
7584
7585 N_Procedure_Call_Statement
7586 | N_Statement_Other_Than_Procedure_Call
7587
7588 -- Pragmas
7589
7590 | N_Pragma
7591
7592 -- Representation_Clause
7593
7594 | N_At_Clause
7595 | N_Attribute_Definition_Clause
7596 | N_Enumeration_Representation_Clause
7597 | N_Record_Representation_Clause
7598
7599 -- Declarations
7600
7601 | N_Abstract_Subprogram_Declaration
7602 | N_Entry_Body
7603 | N_Exception_Declaration
7604 | N_Exception_Renaming_Declaration
7605 | N_Expression_Function
7606 | N_Formal_Abstract_Subprogram_Declaration
7607 | N_Formal_Concrete_Subprogram_Declaration
7608 | N_Formal_Object_Declaration
7609 | N_Formal_Type_Declaration
7610 | N_Full_Type_Declaration
7611 | N_Function_Instantiation
7612 | N_Generic_Function_Renaming_Declaration
7613 | N_Generic_Package_Declaration
7614 | N_Generic_Package_Renaming_Declaration
7615 | N_Generic_Procedure_Renaming_Declaration
7616 | N_Generic_Subprogram_Declaration
7617 | N_Implicit_Label_Declaration
7618 | N_Incomplete_Type_Declaration
7619 | N_Number_Declaration
7620 | N_Object_Declaration
7621 | N_Object_Renaming_Declaration
7622 | N_Package_Body
7623 | N_Package_Body_Stub
7624 | N_Package_Declaration
7625 | N_Package_Instantiation
7626 | N_Package_Renaming_Declaration
7627 | N_Private_Extension_Declaration
7628 | N_Private_Type_Declaration
7629 | N_Procedure_Instantiation
7630 | N_Protected_Body
7631 | N_Protected_Body_Stub
7632 | N_Single_Task_Declaration
7633 | N_Subprogram_Body
7634 | N_Subprogram_Body_Stub
7635 | N_Subprogram_Declaration
7636 | N_Subprogram_Renaming_Declaration
7637 | N_Subtype_Declaration
7638 | N_Task_Body
7639 | N_Task_Body_Stub
7640
7641 -- Use clauses can appear in lists of declarations
7642
7643 | N_Use_Package_Clause
7644 | N_Use_Type_Clause
7645
7646 -- Freeze entity behaves like a declaration or statement
7647
7648 | N_Freeze_Entity
7649 | N_Freeze_Generic_Entity
7650 =>
7651 -- Do not insert here if the item is not a list member (this
7652 -- happens for example with a triggering statement, and the
7653 -- proper approach is to insert before the entire select).
7654
7655 if not Is_List_Member (P) then
7656 null;
7657
7658 -- Do not insert if parent of P is an N_Component_Association
7659 -- node (i.e. we are in the context of an N_Aggregate or
7660 -- N_Extension_Aggregate node. In this case we want to insert
7661 -- before the entire aggregate.
7662
7663 elsif Nkind (Parent (P)) = N_Component_Association then
7664 null;
7665
7666 -- Do not insert if the parent of P is either an N_Variant node
7667 -- or an N_Record_Definition node, meaning in either case that
7668 -- P is a member of a component list, and that therefore the
7669 -- actions should be inserted outside the complete record
7670 -- declaration.
7671
7672 elsif Nkind (Parent (P)) in N_Variant | N_Record_Definition then
7673 null;
7674
7675 -- Do not insert freeze nodes within the loop generated for
7676 -- an aggregate, because they may be elaborated too late for
7677 -- subsequent use in the back end: within a package spec the
7678 -- loop is part of the elaboration procedure and is only
7679 -- elaborated during the second pass.
7680
7681 -- If the loop comes from source, or the entity is local to the
7682 -- loop itself it must remain within.
7683
7684 elsif Nkind (Parent (P)) = N_Loop_Statement
7685 and then not Comes_From_Source (Parent (P))
7686 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7687 and then
7688 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7689 then
7690 null;
7691
7692 -- Otherwise we can go ahead and do the insertion
7693
7694 elsif P = Wrapped_Node then
7695 Store_Before_Actions_In_Scope (Ins_Actions);
7696 return;
7697
7698 else
7699 Insert_List_Before_And_Analyze (P, Ins_Actions);
7700 return;
7701 end if;
7702
7703 -- the expansion of Task and protected type declarations can
7704 -- create declarations for temporaries which, like other actions
7705 -- are inserted and analyzed before the current declaraation.
7706 -- However, the current scope is the synchronized type, and
7707 -- for unnesting it is critical that the proper scope for these
7708 -- generated entities be the enclosing one.
7709
7710 when N_Task_Type_Declaration
7711 | N_Protected_Type_Declaration =>
7712
7713 Push_Scope (Scope (Current_Scope));
7714 Insert_List_Before_And_Analyze (P, Ins_Actions);
7715 Pop_Scope;
7716 return;
7717
7718 -- A special case, N_Raise_xxx_Error can act either as a statement
7719 -- or a subexpression. We tell the difference by looking at the
7720 -- Etype. It is set to Standard_Void_Type in the statement case.
7721
7722 when N_Raise_xxx_Error =>
7723 if Etype (P) = Standard_Void_Type then
7724 if P = Wrapped_Node then
7725 Store_Before_Actions_In_Scope (Ins_Actions);
7726 else
7727 Insert_List_Before_And_Analyze (P, Ins_Actions);
7728 end if;
7729
7730 return;
7731
7732 -- In the subexpression case, keep climbing
7733
7734 else
7735 null;
7736 end if;
7737
7738 -- If a component association appears within a loop created for
7739 -- an array aggregate, attach the actions to the association so
7740 -- they can be subsequently inserted within the loop. For other
7741 -- component associations insert outside of the aggregate. For
7742 -- an association that will generate a loop, its Loop_Actions
7743 -- attribute is already initialized (see exp_aggr.adb).
7744
7745 -- The list of Loop_Actions can in turn generate additional ones,
7746 -- that are inserted before the associated node. If the associated
7747 -- node is outside the aggregate, the new actions are collected
7748 -- at the end of the Loop_Actions, to respect the order in which
7749 -- they are to be elaborated.
7750
7751 when N_Component_Association
7752 | N_Iterated_Component_Association
7753 | N_Iterated_Element_Association
7754 =>
7755 if Nkind (Parent (P)) in N_Aggregate | N_Delta_Aggregate
7756
7757 -- We must not climb up out of an N_Iterated_xxx_Association
7758 -- because the actions might contain references to the loop
7759 -- parameter, except if we come from the Discrete_Choices of
7760 -- N_Iterated_Component_Association which cannot contain any.
7761 -- But it turns out that setting the Loop_Actions field in
7762 -- the case of an N_Component_Association when the field was
7763 -- not already set can lead to gigi assertion failures that
7764 -- are presumably due to malformed trees, so don't do that.
7765
7766 and then (Nkind (P) /= N_Iterated_Component_Association
7767 or else not Is_List_Member (N)
7768 or else
7769 List_Containing (N) /= Discrete_Choices (P))
7770 and then (Nkind (P) /= N_Component_Association
7771 or else Present (Loop_Actions (P)))
7772 then
7773 if Is_Empty_List (Loop_Actions (P)) then
7774 Set_Loop_Actions (P, Ins_Actions);
7775 Analyze_List (Ins_Actions);
7776 else
7777 declare
7778 Decl : Node_Id;
7779
7780 begin
7781 -- Check whether these actions were generated by a
7782 -- declaration that is part of the Loop_Actions for
7783 -- the component_association.
7784
7785 Decl := Assoc_Node;
7786 while Present (Decl) loop
7787 exit when Parent (Decl) = P
7788 and then Is_List_Member (Decl)
7789 and then
7790 List_Containing (Decl) = Loop_Actions (P);
7791 Decl := Parent (Decl);
7792 end loop;
7793
7794 if Present (Decl) then
7795 Insert_List_Before_And_Analyze
7796 (Decl, Ins_Actions);
7797 else
7798 Insert_List_After_And_Analyze
7799 (Last (Loop_Actions (P)), Ins_Actions);
7800 end if;
7801 end;
7802 end if;
7803
7804 return;
7805
7806 else
7807 null;
7808 end if;
7809
7810 -- Special case: an attribute denoting a procedure call
7811
7812 when N_Attribute_Reference =>
7813 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7814 if P = Wrapped_Node then
7815 Store_Before_Actions_In_Scope (Ins_Actions);
7816 else
7817 Insert_List_Before_And_Analyze (P, Ins_Actions);
7818 end if;
7819
7820 return;
7821
7822 -- In the subexpression case, keep climbing
7823
7824 else
7825 null;
7826 end if;
7827
7828 -- Special case: a marker
7829
7830 when N_Call_Marker
7831 | N_Variable_Reference_Marker
7832 =>
7833 if Is_List_Member (P) then
7834 Insert_List_Before_And_Analyze (P, Ins_Actions);
7835 return;
7836 end if;
7837
7838 -- A contract node should not belong to the tree
7839
7840 when N_Contract =>
7841 raise Program_Error;
7842
7843 -- For all other node types, keep climbing tree
7844
7845 when N_Abortable_Part
7846 | N_Accept_Alternative
7847 | N_Access_Definition
7848 | N_Access_Function_Definition
7849 | N_Access_Procedure_Definition
7850 | N_Access_To_Object_Definition
7851 | N_Aggregate
7852 | N_Allocator
7853 | N_Aspect_Specification
7854 | N_Case_Expression
7855 | N_Case_Statement_Alternative
7856 | N_Character_Literal
7857 | N_Compilation_Unit
7858 | N_Compilation_Unit_Aux
7859 | N_Component_Clause
7860 | N_Component_Declaration
7861 | N_Component_Definition
7862 | N_Component_List
7863 | N_Constrained_Array_Definition
7864 | N_Decimal_Fixed_Point_Definition
7865 | N_Defining_Character_Literal
7866 | N_Defining_Identifier
7867 | N_Defining_Operator_Symbol
7868 | N_Defining_Program_Unit_Name
7869 | N_Delay_Alternative
7870 | N_Delta_Aggregate
7871 | N_Delta_Constraint
7872 | N_Derived_Type_Definition
7873 | N_Designator
7874 | N_Digits_Constraint
7875 | N_Discriminant_Association
7876 | N_Discriminant_Specification
7877 | N_Empty
7878 | N_Entry_Body_Formal_Part
7879 | N_Entry_Call_Alternative
7880 | N_Entry_Declaration
7881 | N_Entry_Index_Specification
7882 | N_Enumeration_Type_Definition
7883 | N_Error
7884 | N_Exception_Handler
7885 | N_Expanded_Name
7886 | N_Explicit_Dereference
7887 | N_Extension_Aggregate
7888 | N_Floating_Point_Definition
7889 | N_Formal_Decimal_Fixed_Point_Definition
7890 | N_Formal_Derived_Type_Definition
7891 | N_Formal_Discrete_Type_Definition
7892 | N_Formal_Floating_Point_Definition
7893 | N_Formal_Modular_Type_Definition
7894 | N_Formal_Ordinary_Fixed_Point_Definition
7895 | N_Formal_Package_Declaration
7896 | N_Formal_Private_Type_Definition
7897 | N_Formal_Incomplete_Type_Definition
7898 | N_Formal_Signed_Integer_Type_Definition
7899 | N_Function_Call
7900 | N_Function_Specification
7901 | N_Generic_Association
7902 | N_Handled_Sequence_Of_Statements
7903 | N_Identifier
7904 | N_In
7905 | N_Index_Or_Discriminant_Constraint
7906 | N_Indexed_Component
7907 | N_Integer_Literal
7908 | N_Iterator_Specification
7909 | N_Itype_Reference
7910 | N_Label
7911 | N_Loop_Parameter_Specification
7912 | N_Mod_Clause
7913 | N_Modular_Type_Definition
7914 | N_Not_In
7915 | N_Null
7916 | N_Op_Abs
7917 | N_Op_Add
7918 | N_Op_And
7919 | N_Op_Concat
7920 | N_Op_Divide
7921 | N_Op_Eq
7922 | N_Op_Expon
7923 | N_Op_Ge
7924 | N_Op_Gt
7925 | N_Op_Le
7926 | N_Op_Lt
7927 | N_Op_Minus
7928 | N_Op_Mod
7929 | N_Op_Multiply
7930 | N_Op_Ne
7931 | N_Op_Not
7932 | N_Op_Or
7933 | N_Op_Plus
7934 | N_Op_Rem
7935 | N_Op_Rotate_Left
7936 | N_Op_Rotate_Right
7937 | N_Op_Shift_Left
7938 | N_Op_Shift_Right
7939 | N_Op_Shift_Right_Arithmetic
7940 | N_Op_Subtract
7941 | N_Op_Xor
7942 | N_Operator_Symbol
7943 | N_Ordinary_Fixed_Point_Definition
7944 | N_Others_Choice
7945 | N_Package_Specification
7946 | N_Parameter_Association
7947 | N_Parameter_Specification
7948 | N_Pop_Constraint_Error_Label
7949 | N_Pop_Program_Error_Label
7950 | N_Pop_Storage_Error_Label
7951 | N_Pragma_Argument_Association
7952 | N_Procedure_Specification
7953 | N_Protected_Definition
7954 | N_Push_Constraint_Error_Label
7955 | N_Push_Program_Error_Label
7956 | N_Push_Storage_Error_Label
7957 | N_Qualified_Expression
7958 | N_Quantified_Expression
7959 | N_Raise_Expression
7960 | N_Range
7961 | N_Range_Constraint
7962 | N_Real_Literal
7963 | N_Real_Range_Specification
7964 | N_Record_Definition
7965 | N_Reference
7966 | N_SCIL_Dispatch_Table_Tag_Init
7967 | N_SCIL_Dispatching_Call
7968 | N_SCIL_Membership_Test
7969 | N_Selected_Component
7970 | N_Signed_Integer_Type_Definition
7971 | N_Single_Protected_Declaration
7972 | N_Slice
7973 | N_String_Literal
7974 | N_Subtype_Indication
7975 | N_Subunit
7976 | N_Target_Name
7977 | N_Task_Definition
7978 | N_Terminate_Alternative
7979 | N_Triggering_Alternative
7980 | N_Type_Conversion
7981 | N_Unchecked_Expression
7982 | N_Unchecked_Type_Conversion
7983 | N_Unconstrained_Array_Definition
7984 | N_Unused_At_End
7985 | N_Unused_At_Start
7986 | N_Variant
7987 | N_Variant_Part
7988 | N_Validate_Unchecked_Conversion
7989 | N_With_Clause
7990 =>
7991 null;
7992 end case;
7993
7994 -- If we fall through above tests, keep climbing tree
7995
7996 N := P;
7997
7998 if Nkind (Parent (N)) = N_Subunit then
7999
8000 -- This is the proper body corresponding to a stub. Insertion must
8001 -- be done at the point of the stub, which is in the declarative
8002 -- part of the parent unit.
8003
8004 P := Corresponding_Stub (Parent (N));
8005
8006 else
8007 P := Parent (N);
8008 end if;
8009 end loop;
8010 end Insert_Actions;
8011
8012 -- Version with check(s) suppressed
8013
8014 procedure Insert_Actions
8015 (Assoc_Node : Node_Id;
8016 Ins_Actions : List_Id;
8017 Suppress : Check_Id;
8018 Spec_Expr_OK : Boolean := False)
8019 is
8020 begin
8021 if Suppress = All_Checks then
8022 declare
8023 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
8024 begin
8025 Scope_Suppress.Suppress := (others => True);
8026 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8027 Scope_Suppress.Suppress := Sva;
8028 end;
8029
8030 else
8031 declare
8032 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
8033 begin
8034 Scope_Suppress.Suppress (Suppress) := True;
8035 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8036 Scope_Suppress.Suppress (Suppress) := Svg;
8037 end;
8038 end if;
8039 end Insert_Actions;
8040
8041 --------------------------
8042 -- Insert_Actions_After --
8043 --------------------------
8044
8045 procedure Insert_Actions_After
8046 (Assoc_Node : Node_Id;
8047 Ins_Actions : List_Id)
8048 is
8049 begin
8050 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
8051 Store_After_Actions_In_Scope (Ins_Actions);
8052 else
8053 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
8054 end if;
8055 end Insert_Actions_After;
8056
8057 ---------------------------------
8058 -- Insert_Library_Level_Action --
8059 ---------------------------------
8060
8061 procedure Insert_Library_Level_Action (N : Node_Id) is
8062 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8063
8064 begin
8065 Push_Scope (Cunit_Entity (Current_Sem_Unit));
8066 -- And not Main_Unit as previously. If the main unit is a body,
8067 -- the scope needed to analyze the actions is the entity of the
8068 -- corresponding declaration.
8069
8070 if No (Actions (Aux)) then
8071 Set_Actions (Aux, New_List (N));
8072 else
8073 Append (N, Actions (Aux));
8074 end if;
8075
8076 Analyze (N);
8077 Pop_Scope;
8078 end Insert_Library_Level_Action;
8079
8080 ----------------------------------
8081 -- Insert_Library_Level_Actions --
8082 ----------------------------------
8083
8084 procedure Insert_Library_Level_Actions (L : List_Id) is
8085 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8086
8087 begin
8088 if Is_Non_Empty_List (L) then
8089 Push_Scope (Cunit_Entity (Main_Unit));
8090 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
8091
8092 if No (Actions (Aux)) then
8093 Set_Actions (Aux, L);
8094 Analyze_List (L);
8095 else
8096 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
8097 end if;
8098
8099 Pop_Scope;
8100 end if;
8101 end Insert_Library_Level_Actions;
8102
8103 ----------------------
8104 -- Inside_Init_Proc --
8105 ----------------------
8106
8107 function Inside_Init_Proc return Boolean is
8108 begin
8109 return Present (Enclosing_Init_Proc);
8110 end Inside_Init_Proc;
8111
8112 ----------------------
8113 -- Integer_Type_For --
8114 ----------------------
8115
8116 function Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id is
8117 begin
8118 pragma Assert (S <= System_Max_Integer_Size);
8119
8120 -- This is the canonical 32-bit type
8121
8122 if S <= Standard_Integer_Size then
8123 if Uns then
8124 return Standard_Unsigned;
8125 else
8126 return Standard_Integer;
8127 end if;
8128
8129 -- This is the canonical 64-bit type
8130
8131 elsif S <= Standard_Long_Long_Integer_Size then
8132 if Uns then
8133 return Standard_Long_Long_Unsigned;
8134 else
8135 return Standard_Long_Long_Integer;
8136 end if;
8137
8138 -- This is the canonical 128-bit type
8139
8140 elsif S <= Standard_Long_Long_Long_Integer_Size then
8141 if Uns then
8142 return Standard_Long_Long_Long_Unsigned;
8143 else
8144 return Standard_Long_Long_Long_Integer;
8145 end if;
8146
8147 else
8148 raise Program_Error;
8149 end if;
8150 end Integer_Type_For;
8151
8152 --------------------------------------------------
8153 -- Is_Displacement_Of_Object_Or_Function_Result --
8154 --------------------------------------------------
8155
8156 function Is_Displacement_Of_Object_Or_Function_Result
8157 (Obj_Id : Entity_Id) return Boolean
8158 is
8159 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
8160 -- Determine whether node N denotes a controlled function call
8161
8162 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
8163 -- Determine whether node N denotes a generalized indexing form which
8164 -- involves a controlled result.
8165
8166 function Is_Displace_Call (N : Node_Id) return Boolean;
8167 -- Determine whether node N denotes a call to Ada.Tags.Displace
8168
8169 function Is_Source_Object (N : Node_Id) return Boolean;
8170 -- Determine whether a particular node denotes a source object
8171
8172 function Strip (N : Node_Id) return Node_Id;
8173 -- Examine arbitrary node N by stripping various indirections and return
8174 -- the "real" node.
8175
8176 ---------------------------------
8177 -- Is_Controlled_Function_Call --
8178 ---------------------------------
8179
8180 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
8181 Expr : Node_Id;
8182
8183 begin
8184 -- When a function call appears in Object.Operation format, the
8185 -- original representation has several possible forms depending on
8186 -- the availability and form of actual parameters:
8187
8188 -- Obj.Func N_Selected_Component
8189 -- Obj.Func (Actual) N_Indexed_Component
8190 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
8191 -- N_Selected_Component
8192
8193 Expr := Original_Node (N);
8194 loop
8195 if Nkind (Expr) = N_Function_Call then
8196 Expr := Name (Expr);
8197
8198 -- "Obj.Func (Actual)" case
8199
8200 elsif Nkind (Expr) = N_Indexed_Component then
8201 Expr := Prefix (Expr);
8202
8203 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
8204
8205 elsif Nkind (Expr) = N_Selected_Component then
8206 Expr := Selector_Name (Expr);
8207
8208 else
8209 exit;
8210 end if;
8211 end loop;
8212
8213 return
8214 Nkind (Expr) in N_Has_Entity
8215 and then Present (Entity (Expr))
8216 and then Ekind (Entity (Expr)) = E_Function
8217 and then Needs_Finalization (Etype (Entity (Expr)));
8218 end Is_Controlled_Function_Call;
8219
8220 ----------------------------
8221 -- Is_Controlled_Indexing --
8222 ----------------------------
8223
8224 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
8225 Expr : constant Node_Id := Original_Node (N);
8226
8227 begin
8228 return
8229 Nkind (Expr) = N_Indexed_Component
8230 and then Present (Generalized_Indexing (Expr))
8231 and then Needs_Finalization (Etype (Expr));
8232 end Is_Controlled_Indexing;
8233
8234 ----------------------
8235 -- Is_Displace_Call --
8236 ----------------------
8237
8238 function Is_Displace_Call (N : Node_Id) return Boolean is
8239 Call : constant Node_Id := Strip (N);
8240
8241 begin
8242 return
8243 Present (Call)
8244 and then Nkind (Call) = N_Function_Call
8245 and then Nkind (Name (Call)) in N_Has_Entity
8246 and then Is_RTE (Entity (Name (Call)), RE_Displace);
8247 end Is_Displace_Call;
8248
8249 ----------------------
8250 -- Is_Source_Object --
8251 ----------------------
8252
8253 function Is_Source_Object (N : Node_Id) return Boolean is
8254 Obj : constant Node_Id := Strip (N);
8255
8256 begin
8257 return
8258 Present (Obj)
8259 and then Comes_From_Source (Obj)
8260 and then Nkind (Obj) in N_Has_Entity
8261 and then Is_Object (Entity (Obj));
8262 end Is_Source_Object;
8263
8264 -----------
8265 -- Strip --
8266 -----------
8267
8268 function Strip (N : Node_Id) return Node_Id is
8269 Result : Node_Id;
8270
8271 begin
8272 Result := N;
8273 loop
8274 if Nkind (Result) = N_Explicit_Dereference then
8275 Result := Prefix (Result);
8276
8277 elsif Nkind (Result) in
8278 N_Type_Conversion | N_Unchecked_Type_Conversion
8279 then
8280 Result := Expression (Result);
8281
8282 else
8283 exit;
8284 end if;
8285 end loop;
8286
8287 return Result;
8288 end Strip;
8289
8290 -- Local variables
8291
8292 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
8293 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8294 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
8295 Orig_Expr : Node_Id;
8296
8297 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
8298
8299 begin
8300 -- Case 1:
8301
8302 -- Obj : CW_Type := Function_Call (...);
8303
8304 -- is rewritten into:
8305
8306 -- Temp : ... := Function_Call (...)'reference;
8307 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
8308
8309 -- where the return type of the function and the class-wide type require
8310 -- dispatch table pointer displacement.
8311
8312 -- Case 2:
8313
8314 -- Obj : CW_Type := Container (...);
8315
8316 -- is rewritten into:
8317
8318 -- Temp : ... := Function_Call (Container, ...)'reference;
8319 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
8320
8321 -- where the container element type and the class-wide type require
8322 -- dispatch table pointer dispacement.
8323
8324 -- Case 3:
8325
8326 -- Obj : CW_Type := Src_Obj;
8327
8328 -- is rewritten into:
8329
8330 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
8331
8332 -- where the type of the source object and the class-wide type require
8333 -- dispatch table pointer displacement.
8334
8335 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
8336 and then Is_Class_Wide_Type (Obj_Typ)
8337 and then Is_Displace_Call (Renamed_Object (Obj_Id))
8338 and then Nkind (Orig_Decl) = N_Object_Declaration
8339 and then Comes_From_Source (Orig_Decl)
8340 then
8341 Orig_Expr := Expression (Orig_Decl);
8342
8343 return
8344 Is_Controlled_Function_Call (Orig_Expr)
8345 or else Is_Controlled_Indexing (Orig_Expr)
8346 or else Is_Source_Object (Orig_Expr);
8347 end if;
8348
8349 return False;
8350 end Is_Displacement_Of_Object_Or_Function_Result;
8351
8352 ------------------------------
8353 -- Is_Finalizable_Transient --
8354 ------------------------------
8355
8356 function Is_Finalizable_Transient
8357 (Decl : Node_Id;
8358 Rel_Node : Node_Id) return Boolean
8359 is
8360 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
8361 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8362
8363 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
8364 -- Determine whether transient object Trans_Id is initialized either
8365 -- by a function call which returns an access type or simply renames
8366 -- another pointer.
8367
8368 function Initialized_By_Aliased_BIP_Func_Call
8369 (Trans_Id : Entity_Id) return Boolean;
8370 -- Determine whether transient object Trans_Id is initialized by a
8371 -- build-in-place function call where the BIPalloc parameter is of
8372 -- value 1 and BIPaccess is not null. This case creates an aliasing
8373 -- between the returned value and the value denoted by BIPaccess.
8374
8375 function Is_Aliased
8376 (Trans_Id : Entity_Id;
8377 First_Stmt : Node_Id) return Boolean;
8378 -- Determine whether transient object Trans_Id has been renamed or
8379 -- aliased through 'reference in the statement list starting from
8380 -- First_Stmt.
8381
8382 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
8383 -- Determine whether transient object Trans_Id is allocated on the heap
8384
8385 function Is_Iterated_Container
8386 (Trans_Id : Entity_Id;
8387 First_Stmt : Node_Id) return Boolean;
8388 -- Determine whether transient object Trans_Id denotes a container which
8389 -- is in the process of being iterated in the statement list starting
8390 -- from First_Stmt.
8391
8392 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean;
8393 -- Return True if N is directly part of a build-in-place return
8394 -- statement.
8395
8396 ---------------------------
8397 -- Initialized_By_Access --
8398 ---------------------------
8399
8400 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
8401 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8402
8403 begin
8404 return
8405 Present (Expr)
8406 and then Nkind (Expr) /= N_Reference
8407 and then Is_Access_Type (Etype (Expr));
8408 end Initialized_By_Access;
8409
8410 ------------------------------------------
8411 -- Initialized_By_Aliased_BIP_Func_Call --
8412 ------------------------------------------
8413
8414 function Initialized_By_Aliased_BIP_Func_Call
8415 (Trans_Id : Entity_Id) return Boolean
8416 is
8417 Call : Node_Id := Expression (Parent (Trans_Id));
8418
8419 begin
8420 -- Build-in-place calls usually appear in 'reference format
8421
8422 if Nkind (Call) = N_Reference then
8423 Call := Prefix (Call);
8424 end if;
8425
8426 Call := Unqual_Conv (Call);
8427
8428 if Is_Build_In_Place_Function_Call (Call) then
8429 declare
8430 Access_Nam : Name_Id := No_Name;
8431 Access_OK : Boolean := False;
8432 Actual : Node_Id;
8433 Alloc_Nam : Name_Id := No_Name;
8434 Alloc_OK : Boolean := False;
8435 Formal : Node_Id;
8436 Func_Id : Entity_Id;
8437 Param : Node_Id;
8438
8439 begin
8440 -- Examine all parameter associations of the function call
8441
8442 Param := First (Parameter_Associations (Call));
8443 while Present (Param) loop
8444 if Nkind (Param) = N_Parameter_Association
8445 and then Nkind (Selector_Name (Param)) = N_Identifier
8446 then
8447 Actual := Explicit_Actual_Parameter (Param);
8448 Formal := Selector_Name (Param);
8449
8450 -- Construct the names of formals BIPaccess and BIPalloc
8451 -- using the function name retrieved from an arbitrary
8452 -- formal.
8453
8454 if Access_Nam = No_Name
8455 and then Alloc_Nam = No_Name
8456 and then Present (Entity (Formal))
8457 then
8458 Func_Id := Scope (Entity (Formal));
8459
8460 Access_Nam :=
8461 New_External_Name (Chars (Func_Id),
8462 BIP_Formal_Suffix (BIP_Object_Access));
8463
8464 Alloc_Nam :=
8465 New_External_Name (Chars (Func_Id),
8466 BIP_Formal_Suffix (BIP_Alloc_Form));
8467 end if;
8468
8469 -- A match for BIPaccess => Temp has been found
8470
8471 if Chars (Formal) = Access_Nam
8472 and then Nkind (Actual) /= N_Null
8473 then
8474 Access_OK := True;
8475 end if;
8476
8477 -- A match for BIPalloc => 1 has been found
8478
8479 if Chars (Formal) = Alloc_Nam
8480 and then Nkind (Actual) = N_Integer_Literal
8481 and then Intval (Actual) = Uint_1
8482 then
8483 Alloc_OK := True;
8484 end if;
8485 end if;
8486
8487 Next (Param);
8488 end loop;
8489
8490 return Access_OK and Alloc_OK;
8491 end;
8492 end if;
8493
8494 return False;
8495 end Initialized_By_Aliased_BIP_Func_Call;
8496
8497 ----------------
8498 -- Is_Aliased --
8499 ----------------
8500
8501 function Is_Aliased
8502 (Trans_Id : Entity_Id;
8503 First_Stmt : Node_Id) return Boolean
8504 is
8505 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
8506 -- Given an object renaming declaration, retrieve the entity of the
8507 -- renamed name. Return Empty if the renamed name is anything other
8508 -- than a variable or a constant.
8509
8510 -------------------------
8511 -- Find_Renamed_Object --
8512 -------------------------
8513
8514 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
8515 Ren_Obj : Node_Id := Empty;
8516
8517 function Find_Object (N : Node_Id) return Traverse_Result;
8518 -- Try to detect an object which is either a constant or a
8519 -- variable.
8520
8521 -----------------
8522 -- Find_Object --
8523 -----------------
8524
8525 function Find_Object (N : Node_Id) return Traverse_Result is
8526 begin
8527 -- Stop the search once a constant or a variable has been
8528 -- detected.
8529
8530 if Nkind (N) = N_Identifier
8531 and then Present (Entity (N))
8532 and then Ekind (Entity (N)) in E_Constant | E_Variable
8533 then
8534 Ren_Obj := Entity (N);
8535 return Abandon;
8536 end if;
8537
8538 return OK;
8539 end Find_Object;
8540
8541 procedure Search is new Traverse_Proc (Find_Object);
8542
8543 -- Local variables
8544
8545 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
8546
8547 -- Start of processing for Find_Renamed_Object
8548
8549 begin
8550 -- Actions related to dispatching calls may appear as renamings of
8551 -- tags. Do not process this type of renaming because it does not
8552 -- use the actual value of the object.
8553
8554 if not Is_RTE (Typ, RE_Tag_Ptr) then
8555 Search (Name (Ren_Decl));
8556 end if;
8557
8558 return Ren_Obj;
8559 end Find_Renamed_Object;
8560
8561 -- Local variables
8562
8563 Expr : Node_Id;
8564 Ren_Obj : Entity_Id;
8565 Stmt : Node_Id;
8566
8567 -- Start of processing for Is_Aliased
8568
8569 begin
8570 -- A controlled transient object is not considered aliased when it
8571 -- appears inside an expression_with_actions node even when there are
8572 -- explicit aliases of it:
8573
8574 -- do
8575 -- Trans_Id : Ctrl_Typ ...; -- transient object
8576 -- Alias : ... := Trans_Id; -- object is aliased
8577 -- Val : constant Boolean :=
8578 -- ... Alias ...; -- aliasing ends
8579 -- <finalize Trans_Id> -- object safe to finalize
8580 -- in Val end;
8581
8582 -- Expansion ensures that all aliases are encapsulated in the actions
8583 -- list and do not leak to the expression by forcing the evaluation
8584 -- of the expression.
8585
8586 if Nkind (Rel_Node) = N_Expression_With_Actions then
8587 return False;
8588
8589 -- Otherwise examine the statements after the controlled transient
8590 -- object and look for various forms of aliasing.
8591
8592 else
8593 Stmt := First_Stmt;
8594 while Present (Stmt) loop
8595 if Nkind (Stmt) = N_Object_Declaration then
8596 Expr := Expression (Stmt);
8597
8598 -- Aliasing of the form:
8599 -- Obj : ... := Trans_Id'reference;
8600
8601 if Present (Expr)
8602 and then Nkind (Expr) = N_Reference
8603 and then Nkind (Prefix (Expr)) = N_Identifier
8604 and then Entity (Prefix (Expr)) = Trans_Id
8605 then
8606 return True;
8607 end if;
8608
8609 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8610 Ren_Obj := Find_Renamed_Object (Stmt);
8611
8612 -- Aliasing of the form:
8613 -- Obj : ... renames ... Trans_Id ...;
8614
8615 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8616 return True;
8617 end if;
8618 end if;
8619
8620 Next (Stmt);
8621 end loop;
8622
8623 return False;
8624 end if;
8625 end Is_Aliased;
8626
8627 ------------------
8628 -- Is_Allocated --
8629 ------------------
8630
8631 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8632 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8633 begin
8634 return
8635 Is_Access_Type (Etype (Trans_Id))
8636 and then Present (Expr)
8637 and then Nkind (Expr) = N_Allocator;
8638 end Is_Allocated;
8639
8640 ---------------------------
8641 -- Is_Iterated_Container --
8642 ---------------------------
8643
8644 function Is_Iterated_Container
8645 (Trans_Id : Entity_Id;
8646 First_Stmt : Node_Id) return Boolean
8647 is
8648 Aspect : Node_Id;
8649 Call : Node_Id;
8650 Iter : Entity_Id;
8651 Param : Node_Id;
8652 Stmt : Node_Id;
8653 Typ : Entity_Id;
8654
8655 begin
8656 -- It is not possible to iterate over containers in non-Ada 2012 code
8657
8658 if Ada_Version < Ada_2012 then
8659 return False;
8660 end if;
8661
8662 Typ := Etype (Trans_Id);
8663
8664 -- Handle access type created for secondary stack use
8665
8666 if Is_Access_Type (Typ) then
8667 Typ := Designated_Type (Typ);
8668 end if;
8669
8670 -- Look for aspect Default_Iterator. It may be part of a type
8671 -- declaration for a container, or inherited from a base type
8672 -- or parent type.
8673
8674 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8675
8676 if Present (Aspect) then
8677 Iter := Entity (Aspect);
8678
8679 -- Examine the statements following the container object and
8680 -- look for a call to the default iterate routine where the
8681 -- first parameter is the transient. Such a call appears as:
8682
8683 -- It : Access_To_CW_Iterator :=
8684 -- Iterate (Tran_Id.all, ...)'reference;
8685
8686 Stmt := First_Stmt;
8687 while Present (Stmt) loop
8688
8689 -- Detect an object declaration which is initialized by a
8690 -- secondary stack function call.
8691
8692 if Nkind (Stmt) = N_Object_Declaration
8693 and then Present (Expression (Stmt))
8694 and then Nkind (Expression (Stmt)) = N_Reference
8695 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8696 then
8697 Call := Prefix (Expression (Stmt));
8698
8699 -- The call must invoke the default iterate routine of
8700 -- the container and the transient object must appear as
8701 -- the first actual parameter. Skip any calls whose names
8702 -- are not entities.
8703
8704 if Is_Entity_Name (Name (Call))
8705 and then Entity (Name (Call)) = Iter
8706 and then Present (Parameter_Associations (Call))
8707 then
8708 Param := First (Parameter_Associations (Call));
8709
8710 if Nkind (Param) = N_Explicit_Dereference
8711 and then Entity (Prefix (Param)) = Trans_Id
8712 then
8713 return True;
8714 end if;
8715 end if;
8716 end if;
8717
8718 Next (Stmt);
8719 end loop;
8720 end if;
8721
8722 return False;
8723 end Is_Iterated_Container;
8724
8725 -------------------------------------
8726 -- Is_Part_Of_BIP_Return_Statement --
8727 -------------------------------------
8728
8729 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean is
8730 Subp : constant Entity_Id := Current_Subprogram;
8731 Context : Node_Id;
8732 begin
8733 -- First check if N is part of a BIP function
8734
8735 if No (Subp)
8736 or else not Is_Build_In_Place_Function (Subp)
8737 then
8738 return False;
8739 end if;
8740
8741 -- Then check whether N is a complete part of a return statement
8742 -- Should we consider other node kinds to go up the tree???
8743
8744 Context := N;
8745 loop
8746 case Nkind (Context) is
8747 when N_Expression_With_Actions => Context := Parent (Context);
8748 when N_Simple_Return_Statement => return True;
8749 when others => return False;
8750 end case;
8751 end loop;
8752 end Is_Part_Of_BIP_Return_Statement;
8753
8754 -- Local variables
8755
8756 Desig : Entity_Id := Obj_Typ;
8757
8758 -- Start of processing for Is_Finalizable_Transient
8759
8760 begin
8761 -- Handle access types
8762
8763 if Is_Access_Type (Desig) then
8764 Desig := Available_View (Designated_Type (Desig));
8765 end if;
8766
8767 return
8768 Ekind (Obj_Id) in E_Constant | E_Variable
8769 and then Needs_Finalization (Desig)
8770 and then Requires_Transient_Scope (Desig)
8771 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8772 and then not Is_Part_Of_BIP_Return_Statement (Rel_Node)
8773
8774 -- Do not consider a transient object that was already processed
8775
8776 and then not Is_Finalized_Transient (Obj_Id)
8777
8778 -- Do not consider renamed or 'reference-d transient objects because
8779 -- the act of renaming extends the object's lifetime.
8780
8781 and then not Is_Aliased (Obj_Id, Decl)
8782
8783 -- Do not consider transient objects allocated on the heap since
8784 -- they are attached to a finalization master.
8785
8786 and then not Is_Allocated (Obj_Id)
8787
8788 -- If the transient object is a pointer, check that it is not
8789 -- initialized by a function that returns a pointer or acts as a
8790 -- renaming of another pointer.
8791
8792 and then not
8793 (Is_Access_Type (Obj_Typ) and then Initialized_By_Access (Obj_Id))
8794
8795 -- Do not consider transient objects which act as indirect aliases
8796 -- of build-in-place function results.
8797
8798 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8799
8800 -- Do not consider conversions of tags to class-wide types
8801
8802 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8803
8804 -- Do not consider iterators because those are treated as normal
8805 -- controlled objects and are processed by the usual finalization
8806 -- machinery. This avoids the double finalization of an iterator.
8807
8808 and then not Is_Iterator (Desig)
8809
8810 -- Do not consider containers in the context of iterator loops. Such
8811 -- transient objects must exist for as long as the loop is around,
8812 -- otherwise any operation carried out by the iterator will fail.
8813
8814 and then not Is_Iterated_Container (Obj_Id, Decl);
8815 end Is_Finalizable_Transient;
8816
8817 ---------------------------------
8818 -- Is_Fully_Repped_Tagged_Type --
8819 ---------------------------------
8820
8821 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8822 U : constant Entity_Id := Underlying_Type (T);
8823 Comp : Entity_Id;
8824
8825 begin
8826 if No (U) or else not Is_Tagged_Type (U) then
8827 return False;
8828 elsif Has_Discriminants (U) then
8829 return False;
8830 elsif not Has_Specified_Layout (U) then
8831 return False;
8832 end if;
8833
8834 -- Here we have a tagged type, see if it has any component (other than
8835 -- tag and parent) with no component_clause. If so, we return False.
8836
8837 Comp := First_Component (U);
8838 while Present (Comp) loop
8839 if not Is_Tag (Comp)
8840 and then Chars (Comp) /= Name_uParent
8841 and then No (Component_Clause (Comp))
8842 then
8843 return False;
8844 else
8845 Next_Component (Comp);
8846 end if;
8847 end loop;
8848
8849 -- All components have clauses
8850
8851 return True;
8852 end Is_Fully_Repped_Tagged_Type;
8853
8854 ----------------------------------
8855 -- Is_Library_Level_Tagged_Type --
8856 ----------------------------------
8857
8858 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8859 begin
8860 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8861 end Is_Library_Level_Tagged_Type;
8862
8863 --------------------------
8864 -- Is_Non_BIP_Func_Call --
8865 --------------------------
8866
8867 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8868 begin
8869 -- The expected call is of the format
8870 --
8871 -- Func_Call'reference
8872
8873 return
8874 Nkind (Expr) = N_Reference
8875 and then Nkind (Prefix (Expr)) = N_Function_Call
8876 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8877 end Is_Non_BIP_Func_Call;
8878
8879 ----------------------------------
8880 -- Is_Possibly_Unaligned_Object --
8881 ----------------------------------
8882
8883 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8884 T : constant Entity_Id := Etype (N);
8885
8886 begin
8887 -- If renamed object, apply test to underlying object
8888
8889 if Is_Entity_Name (N)
8890 and then Is_Object (Entity (N))
8891 and then Present (Renamed_Object (Entity (N)))
8892 then
8893 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8894 end if;
8895
8896 -- Tagged and controlled types and aliased types are always aligned, as
8897 -- are concurrent types.
8898
8899 if Is_Aliased (T)
8900 or else Has_Controlled_Component (T)
8901 or else Is_Concurrent_Type (T)
8902 or else Is_Tagged_Type (T)
8903 or else Is_Controlled (T)
8904 then
8905 return False;
8906 end if;
8907
8908 -- If this is an element of a packed array, may be unaligned
8909
8910 if Is_Ref_To_Bit_Packed_Array (N) then
8911 return True;
8912 end if;
8913
8914 -- Case of indexed component reference: test whether prefix is unaligned
8915
8916 if Nkind (N) = N_Indexed_Component then
8917 return Is_Possibly_Unaligned_Object (Prefix (N));
8918
8919 -- Case of selected component reference
8920
8921 elsif Nkind (N) = N_Selected_Component then
8922 declare
8923 P : constant Node_Id := Prefix (N);
8924 C : constant Entity_Id := Entity (Selector_Name (N));
8925 M : Nat;
8926 S : Nat;
8927
8928 begin
8929 -- If component reference is for an array with nonstatic bounds,
8930 -- then it is always aligned: we can only process unaligned arrays
8931 -- with static bounds (more precisely compile time known bounds).
8932
8933 if Is_Array_Type (T)
8934 and then not Compile_Time_Known_Bounds (T)
8935 then
8936 return False;
8937 end if;
8938
8939 -- If component is aliased, it is definitely properly aligned
8940
8941 if Is_Aliased (C) then
8942 return False;
8943 end if;
8944
8945 -- If component is for a type implemented as a scalar, and the
8946 -- record is packed, and the component is other than the first
8947 -- component of the record, then the component may be unaligned.
8948
8949 if Is_Packed (Etype (P))
8950 and then Represented_As_Scalar (Etype (C))
8951 and then First_Entity (Scope (C)) /= C
8952 then
8953 return True;
8954 end if;
8955
8956 -- Compute maximum possible alignment for T
8957
8958 -- If alignment is known, then that settles things
8959
8960 if Known_Alignment (T) then
8961 M := UI_To_Int (Alignment (T));
8962
8963 -- If alignment is not known, tentatively set max alignment
8964
8965 else
8966 M := Ttypes.Maximum_Alignment;
8967
8968 -- We can reduce this if the Esize is known since the default
8969 -- alignment will never be more than the smallest power of 2
8970 -- that does not exceed this Esize value.
8971
8972 if Known_Esize (T) then
8973 S := UI_To_Int (Esize (T));
8974
8975 while (M / 2) >= S loop
8976 M := M / 2;
8977 end loop;
8978 end if;
8979 end if;
8980
8981 -- Case of component clause present which may specify an
8982 -- unaligned position.
8983
8984 if Present (Component_Clause (C)) then
8985
8986 -- Otherwise we can do a test to make sure that the actual
8987 -- start position in the record, and the length, are both
8988 -- consistent with the required alignment. If not, we know
8989 -- that we are unaligned.
8990
8991 declare
8992 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8993 Comp : Entity_Id;
8994
8995 begin
8996 Comp := C;
8997
8998 -- For a component inherited in a record extension, the
8999 -- clause is inherited but position and size are not set.
9000
9001 if Is_Base_Type (Etype (P))
9002 and then Is_Tagged_Type (Etype (P))
9003 and then Present (Original_Record_Component (Comp))
9004 then
9005 Comp := Original_Record_Component (Comp);
9006 end if;
9007
9008 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
9009 or else Esize (Comp) mod Align_In_Bits /= 0
9010 then
9011 return True;
9012 end if;
9013 end;
9014 end if;
9015
9016 -- Otherwise, for a component reference, test prefix
9017
9018 return Is_Possibly_Unaligned_Object (P);
9019 end;
9020
9021 -- If not a component reference, must be aligned
9022
9023 else
9024 return False;
9025 end if;
9026 end Is_Possibly_Unaligned_Object;
9027
9028 ---------------------------------
9029 -- Is_Possibly_Unaligned_Slice --
9030 ---------------------------------
9031
9032 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
9033 begin
9034 -- Go to renamed object
9035
9036 if Is_Entity_Name (N)
9037 and then Is_Object (Entity (N))
9038 and then Present (Renamed_Object (Entity (N)))
9039 then
9040 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
9041 end if;
9042
9043 -- The reference must be a slice
9044
9045 if Nkind (N) /= N_Slice then
9046 return False;
9047 end if;
9048
9049 -- If it is a slice, then look at the array type being sliced
9050
9051 declare
9052 Sarr : constant Node_Id := Prefix (N);
9053 -- Prefix of the slice, i.e. the array being sliced
9054
9055 Styp : constant Entity_Id := Etype (Prefix (N));
9056 -- Type of the array being sliced
9057
9058 Pref : Node_Id;
9059 Ptyp : Entity_Id;
9060
9061 begin
9062 -- The problems arise if the array object that is being sliced
9063 -- is a component of a record or array, and we cannot guarantee
9064 -- the alignment of the array within its containing object.
9065
9066 -- To investigate this, we look at successive prefixes to see
9067 -- if we have a worrisome indexed or selected component.
9068
9069 Pref := Sarr;
9070 loop
9071 -- Case of array is part of an indexed component reference
9072
9073 if Nkind (Pref) = N_Indexed_Component then
9074 Ptyp := Etype (Prefix (Pref));
9075
9076 -- The only problematic case is when the array is packed, in
9077 -- which case we really know nothing about the alignment of
9078 -- individual components.
9079
9080 if Is_Bit_Packed_Array (Ptyp) then
9081 return True;
9082 end if;
9083
9084 -- Case of array is part of a selected component reference
9085
9086 elsif Nkind (Pref) = N_Selected_Component then
9087 Ptyp := Etype (Prefix (Pref));
9088
9089 -- We are definitely in trouble if the record in question
9090 -- has an alignment, and either we know this alignment is
9091 -- inconsistent with the alignment of the slice, or we don't
9092 -- know what the alignment of the slice should be. But this
9093 -- really matters only if the target has strict alignment.
9094
9095 if Target_Strict_Alignment
9096 and then Known_Alignment (Ptyp)
9097 and then (not Known_Alignment (Styp)
9098 or else Alignment (Styp) > Alignment (Ptyp))
9099 then
9100 return True;
9101 end if;
9102
9103 -- We are in potential trouble if the record type is packed.
9104 -- We could special case when we know that the array is the
9105 -- first component, but that's not such a simple case ???
9106
9107 if Is_Packed (Ptyp) then
9108 return True;
9109 end if;
9110
9111 -- We are in trouble if there is a component clause, and
9112 -- either we do not know the alignment of the slice, or
9113 -- the alignment of the slice is inconsistent with the
9114 -- bit position specified by the component clause.
9115
9116 declare
9117 Field : constant Entity_Id := Entity (Selector_Name (Pref));
9118 begin
9119 if Present (Component_Clause (Field))
9120 and then
9121 (not Known_Alignment (Styp)
9122 or else
9123 (Component_Bit_Offset (Field) mod
9124 (System_Storage_Unit * Alignment (Styp))) /= 0)
9125 then
9126 return True;
9127 end if;
9128 end;
9129
9130 -- For cases other than selected or indexed components we know we
9131 -- are OK, since no issues arise over alignment.
9132
9133 else
9134 return False;
9135 end if;
9136
9137 -- We processed an indexed component or selected component
9138 -- reference that looked safe, so keep checking prefixes.
9139
9140 Pref := Prefix (Pref);
9141 end loop;
9142 end;
9143 end Is_Possibly_Unaligned_Slice;
9144
9145 -------------------------------
9146 -- Is_Related_To_Func_Return --
9147 -------------------------------
9148
9149 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
9150 Expr : constant Node_Id := Related_Expression (Id);
9151 begin
9152 -- In the case of a function with a class-wide result that returns
9153 -- a call to a function with a specific result, we introduce a
9154 -- type conversion for the return expression. We do not want that
9155 -- type conversion to influence the result of this function.
9156
9157 return
9158 Present (Expr)
9159 and then Nkind (Unqual_Conv (Expr)) = N_Explicit_Dereference
9160 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
9161 end Is_Related_To_Func_Return;
9162
9163 --------------------------------
9164 -- Is_Ref_To_Bit_Packed_Array --
9165 --------------------------------
9166
9167 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
9168 Result : Boolean;
9169 Expr : Node_Id;
9170
9171 begin
9172 if Is_Entity_Name (N)
9173 and then Is_Object (Entity (N))
9174 and then Present (Renamed_Object (Entity (N)))
9175 then
9176 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
9177 end if;
9178
9179 if Nkind (N) in N_Indexed_Component | N_Selected_Component then
9180 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
9181 Result := True;
9182 else
9183 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
9184 end if;
9185
9186 if Result and then Nkind (N) = N_Indexed_Component then
9187 Expr := First (Expressions (N));
9188 while Present (Expr) loop
9189 Force_Evaluation (Expr);
9190 Next (Expr);
9191 end loop;
9192 end if;
9193
9194 return Result;
9195
9196 else
9197 return False;
9198 end if;
9199 end Is_Ref_To_Bit_Packed_Array;
9200
9201 --------------------------------
9202 -- Is_Ref_To_Bit_Packed_Slice --
9203 --------------------------------
9204
9205 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
9206 begin
9207 if Nkind (N) = N_Type_Conversion then
9208 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
9209
9210 elsif Is_Entity_Name (N)
9211 and then Is_Object (Entity (N))
9212 and then Present (Renamed_Object (Entity (N)))
9213 then
9214 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
9215
9216 elsif Nkind (N) = N_Slice
9217 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
9218 then
9219 return True;
9220
9221 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9222 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
9223
9224 else
9225 return False;
9226 end if;
9227 end Is_Ref_To_Bit_Packed_Slice;
9228
9229 -----------------------
9230 -- Is_Renamed_Object --
9231 -----------------------
9232
9233 function Is_Renamed_Object (N : Node_Id) return Boolean is
9234 Pnod : constant Node_Id := Parent (N);
9235 Kind : constant Node_Kind := Nkind (Pnod);
9236 begin
9237 if Kind = N_Object_Renaming_Declaration then
9238 return True;
9239 elsif Kind in N_Indexed_Component | N_Selected_Component then
9240 return Is_Renamed_Object (Pnod);
9241 else
9242 return False;
9243 end if;
9244 end Is_Renamed_Object;
9245
9246 --------------------------------------
9247 -- Is_Secondary_Stack_BIP_Func_Call --
9248 --------------------------------------
9249
9250 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
9251 Actual : Node_Id;
9252 Call : Node_Id := Expr;
9253 Formal : Node_Id;
9254 Param : Node_Id;
9255
9256 begin
9257 -- Build-in-place calls usually appear in 'reference format. Note that
9258 -- the accessibility check machinery may add an extra 'reference due to
9259 -- side effect removal.
9260
9261 while Nkind (Call) = N_Reference loop
9262 Call := Prefix (Call);
9263 end loop;
9264
9265 Call := Unqual_Conv (Call);
9266
9267 if Is_Build_In_Place_Function_Call (Call) then
9268
9269 -- Examine all parameter associations of the function call
9270
9271 Param := First (Parameter_Associations (Call));
9272 while Present (Param) loop
9273 if Nkind (Param) = N_Parameter_Association then
9274 Formal := Selector_Name (Param);
9275 Actual := Explicit_Actual_Parameter (Param);
9276
9277 -- A match for BIPalloc => 2 has been found
9278
9279 if Is_Build_In_Place_Entity (Formal)
9280 and then BIP_Suffix_Kind (Formal) = BIP_Alloc_Form
9281 and then Nkind (Actual) = N_Integer_Literal
9282 and then Intval (Actual) = Uint_2
9283 then
9284 return True;
9285 end if;
9286 end if;
9287
9288 Next (Param);
9289 end loop;
9290 end if;
9291
9292 return False;
9293 end Is_Secondary_Stack_BIP_Func_Call;
9294
9295 -------------------------------------
9296 -- Is_Tag_To_Class_Wide_Conversion --
9297 -------------------------------------
9298
9299 function Is_Tag_To_Class_Wide_Conversion
9300 (Obj_Id : Entity_Id) return Boolean
9301 is
9302 Expr : constant Node_Id := Expression (Parent (Obj_Id));
9303
9304 begin
9305 return
9306 Is_Class_Wide_Type (Etype (Obj_Id))
9307 and then Present (Expr)
9308 and then Nkind (Expr) = N_Unchecked_Type_Conversion
9309 and then Is_RTE (Etype (Expression (Expr)), RE_Tag);
9310 end Is_Tag_To_Class_Wide_Conversion;
9311
9312 --------------------------------
9313 -- Is_Uninitialized_Aggregate --
9314 --------------------------------
9315
9316 function Is_Uninitialized_Aggregate
9317 (Exp : Node_Id;
9318 T : Entity_Id) return Boolean
9319 is
9320 Comp : Node_Id;
9321 Comp_Type : Entity_Id;
9322 Typ : Entity_Id;
9323
9324 begin
9325 if Nkind (Exp) /= N_Aggregate then
9326 return False;
9327 end if;
9328
9329 Preanalyze_And_Resolve (Exp, T);
9330 Typ := Etype (Exp);
9331
9332 if No (Typ)
9333 or else Ekind (Typ) /= E_Array_Subtype
9334 or else Present (Expressions (Exp))
9335 or else No (Component_Associations (Exp))
9336 then
9337 return False;
9338 else
9339 Comp_Type := Component_Type (Typ);
9340 Comp := First (Component_Associations (Exp));
9341
9342 if not Box_Present (Comp)
9343 or else Present (Next (Comp))
9344 then
9345 return False;
9346 end if;
9347
9348 return Is_Scalar_Type (Comp_Type)
9349 and then No (Default_Aspect_Component_Value (Typ));
9350 end if;
9351 end Is_Uninitialized_Aggregate;
9352
9353 ----------------------------
9354 -- Is_Untagged_Derivation --
9355 ----------------------------
9356
9357 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
9358 begin
9359 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
9360 or else
9361 (Is_Private_Type (T) and then Present (Full_View (T))
9362 and then not Is_Tagged_Type (Full_View (T))
9363 and then Is_Derived_Type (Full_View (T))
9364 and then Etype (Full_View (T)) /= T);
9365 end Is_Untagged_Derivation;
9366
9367 ------------------------------------
9368 -- Is_Untagged_Private_Derivation --
9369 ------------------------------------
9370
9371 function Is_Untagged_Private_Derivation
9372 (Priv_Typ : Entity_Id;
9373 Full_Typ : Entity_Id) return Boolean
9374 is
9375 begin
9376 return
9377 Present (Priv_Typ)
9378 and then Is_Untagged_Derivation (Priv_Typ)
9379 and then Is_Private_Type (Etype (Priv_Typ))
9380 and then Present (Full_Typ)
9381 and then Is_Itype (Full_Typ);
9382 end Is_Untagged_Private_Derivation;
9383
9384 ------------------------------
9385 -- Is_Verifiable_DIC_Pragma --
9386 ------------------------------
9387
9388 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
9389 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9390
9391 begin
9392 -- To qualify as verifiable, a DIC pragma must have a non-null argument
9393
9394 return
9395 Present (Args)
9396
9397 -- If there are args, but the first arg is Empty, then treat the
9398 -- pragma the same as having no args (there may be a second arg that
9399 -- is an implicitly added type arg, and Empty is a placeholder).
9400
9401 and then Present (Get_Pragma_Arg (First (Args)))
9402
9403 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
9404 end Is_Verifiable_DIC_Pragma;
9405
9406 ---------------------------
9407 -- Is_Volatile_Reference --
9408 ---------------------------
9409
9410 function Is_Volatile_Reference (N : Node_Id) return Boolean is
9411 begin
9412 -- Only source references are to be treated as volatile, internally
9413 -- generated stuff cannot have volatile external effects.
9414
9415 if not Comes_From_Source (N) then
9416 return False;
9417
9418 -- Never true for reference to a type
9419
9420 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9421 return False;
9422
9423 -- Never true for a compile time known constant
9424
9425 elsif Compile_Time_Known_Value (N) then
9426 return False;
9427
9428 -- True if object reference with volatile type
9429
9430 elsif Is_Volatile_Object_Ref (N) then
9431 return True;
9432
9433 -- True if reference to volatile entity
9434
9435 elsif Is_Entity_Name (N) then
9436 return Treat_As_Volatile (Entity (N));
9437
9438 -- True for slice of volatile array
9439
9440 elsif Nkind (N) = N_Slice then
9441 return Is_Volatile_Reference (Prefix (N));
9442
9443 -- True if volatile component
9444
9445 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9446 if (Is_Entity_Name (Prefix (N))
9447 and then Has_Volatile_Components (Entity (Prefix (N))))
9448 or else (Present (Etype (Prefix (N)))
9449 and then Has_Volatile_Components (Etype (Prefix (N))))
9450 then
9451 return True;
9452 else
9453 return Is_Volatile_Reference (Prefix (N));
9454 end if;
9455
9456 -- Otherwise false
9457
9458 else
9459 return False;
9460 end if;
9461 end Is_Volatile_Reference;
9462
9463 --------------------
9464 -- Kill_Dead_Code --
9465 --------------------
9466
9467 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
9468 W : Boolean := Warn;
9469 -- Set False if warnings suppressed
9470
9471 begin
9472 if Present (N) then
9473 Remove_Warning_Messages (N);
9474
9475 -- Update the internal structures of the ABE mechanism in case the
9476 -- dead node is an elaboration scenario.
9477
9478 Kill_Elaboration_Scenario (N);
9479
9480 -- Generate warning if appropriate
9481
9482 if W then
9483
9484 -- We suppress the warning if this code is under control of an
9485 -- if/case statement and either
9486 -- a) we are in an instance and the condition/selector
9487 -- has a statically known value; or
9488 -- b) the condition/selector is a simple identifier and
9489 -- warnings off is set for this identifier.
9490 -- Dead code is common and reasonable in instances, so we don't
9491 -- want a warning in that case.
9492
9493 declare
9494 C : Node_Id := Empty;
9495 begin
9496 if Nkind (Parent (N)) = N_If_Statement then
9497 C := Condition (Parent (N));
9498 elsif Nkind (Parent (N)) = N_Case_Statement_Alternative then
9499 C := Expression (Parent (Parent (N)));
9500 end if;
9501
9502 if Present (C) then
9503 if (In_Instance and Compile_Time_Known_Value (C))
9504 or else (Nkind (C) = N_Identifier
9505 and then Present (Entity (C))
9506 and then Has_Warnings_Off (Entity (C)))
9507 then
9508 W := False;
9509 end if;
9510 end if;
9511 end;
9512
9513 -- Generate warning if not suppressed
9514
9515 if W then
9516 Error_Msg_F
9517 ("?t?this code can never be executed and has been deleted!",
9518 N);
9519 end if;
9520 end if;
9521
9522 -- Recurse into block statements and bodies to process declarations
9523 -- and statements.
9524
9525 if Nkind (N) = N_Block_Statement
9526 or else Nkind (N) = N_Subprogram_Body
9527 or else Nkind (N) = N_Package_Body
9528 then
9529 Kill_Dead_Code (Declarations (N), False);
9530 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
9531
9532 if Nkind (N) = N_Subprogram_Body then
9533 Set_Is_Eliminated (Defining_Entity (N));
9534 end if;
9535
9536 elsif Nkind (N) = N_Package_Declaration then
9537 Kill_Dead_Code (Visible_Declarations (Specification (N)));
9538 Kill_Dead_Code (Private_Declarations (Specification (N)));
9539
9540 -- ??? After this point, Delete_Tree has been called on all
9541 -- declarations in Specification (N), so references to entities
9542 -- therein look suspicious.
9543
9544 declare
9545 E : Entity_Id := First_Entity (Defining_Entity (N));
9546
9547 begin
9548 while Present (E) loop
9549 if Ekind (E) = E_Operator then
9550 Set_Is_Eliminated (E);
9551 end if;
9552
9553 Next_Entity (E);
9554 end loop;
9555 end;
9556
9557 -- Recurse into composite statement to kill individual statements in
9558 -- particular instantiations.
9559
9560 elsif Nkind (N) = N_If_Statement then
9561 Kill_Dead_Code (Then_Statements (N));
9562 Kill_Dead_Code (Elsif_Parts (N));
9563 Kill_Dead_Code (Else_Statements (N));
9564
9565 elsif Nkind (N) = N_Loop_Statement then
9566 Kill_Dead_Code (Statements (N));
9567
9568 elsif Nkind (N) = N_Case_Statement then
9569 declare
9570 Alt : Node_Id;
9571 begin
9572 Alt := First (Alternatives (N));
9573 while Present (Alt) loop
9574 Kill_Dead_Code (Statements (Alt));
9575 Next (Alt);
9576 end loop;
9577 end;
9578
9579 elsif Nkind (N) = N_Case_Statement_Alternative then
9580 Kill_Dead_Code (Statements (N));
9581
9582 -- Deal with dead instances caused by deleting instantiations
9583
9584 elsif Nkind (N) in N_Generic_Instantiation then
9585 Remove_Dead_Instance (N);
9586 end if;
9587 end if;
9588 end Kill_Dead_Code;
9589
9590 -- Case where argument is a list of nodes to be killed
9591
9592 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
9593 N : Node_Id;
9594 W : Boolean;
9595
9596 begin
9597 W := Warn;
9598
9599 N := First (L);
9600 while Present (N) loop
9601 Kill_Dead_Code (N, W);
9602 W := False;
9603 Next (N);
9604 end loop;
9605 end Kill_Dead_Code;
9606
9607 -----------------------------
9608 -- Make_CW_Equivalent_Type --
9609 -----------------------------
9610
9611 -- Create a record type used as an equivalent of any member of the class
9612 -- which takes its size from exp.
9613
9614 -- Generate the following code:
9615
9616 -- type Equiv_T is record
9617 -- _parent : T (List of discriminant constraints taken from Exp);
9618 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
9619 -- end Equiv_T;
9620 --
9621 -- Note that this type does not guarantee same alignment as all derived
9622 -- types.
9623 --
9624 -- Note: for the freezing circuitry, this looks like a record extension,
9625 -- and so we need to make sure that the scalar storage order is the same
9626 -- as that of the parent type. (This does not change anything for the
9627 -- representation of the extension part.)
9628
9629 function Make_CW_Equivalent_Type
9630 (T : Entity_Id;
9631 E : Node_Id) return Entity_Id
9632 is
9633 Loc : constant Source_Ptr := Sloc (E);
9634 Root_Typ : constant Entity_Id := Root_Type (T);
9635 Root_Utyp : constant Entity_Id := Underlying_Type (Root_Typ);
9636 List_Def : constant List_Id := Empty_List;
9637 Comp_List : constant List_Id := New_List;
9638 Equiv_Type : Entity_Id;
9639 Range_Type : Entity_Id;
9640 Str_Type : Entity_Id;
9641 Constr_Root : Entity_Id;
9642 Sizexpr : Node_Id;
9643
9644 begin
9645 -- If the root type is already constrained, there are no discriminants
9646 -- in the expression.
9647
9648 if not Has_Discriminants (Root_Typ)
9649 or else Is_Constrained (Root_Typ)
9650 then
9651 Constr_Root := Root_Typ;
9652
9653 -- At this point in the expansion, nonlimited view of the type
9654 -- must be available, otherwise the error will be reported later.
9655
9656 if From_Limited_With (Constr_Root)
9657 and then Present (Non_Limited_View (Constr_Root))
9658 then
9659 Constr_Root := Non_Limited_View (Constr_Root);
9660 end if;
9661
9662 else
9663 Constr_Root := Make_Temporary (Loc, 'R');
9664
9665 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9666
9667 Append_To (List_Def,
9668 Make_Subtype_Declaration (Loc,
9669 Defining_Identifier => Constr_Root,
9670 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9671 end if;
9672
9673 -- Generate the range subtype declaration
9674
9675 Range_Type := Make_Temporary (Loc, 'G');
9676
9677 if not Is_Interface (Root_Typ) then
9678
9679 -- subtype rg__xx is
9680 -- Storage_Offset range 1 .. (Expr'size - typ'object_size)
9681 -- / Storage_Unit
9682
9683 Sizexpr :=
9684 Make_Op_Subtract (Loc,
9685 Left_Opnd =>
9686 Make_Attribute_Reference (Loc,
9687 Prefix =>
9688 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9689 Attribute_Name => Name_Size),
9690 Right_Opnd =>
9691 Make_Attribute_Reference (Loc,
9692 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9693 Attribute_Name => Name_Object_Size));
9694 else
9695 -- subtype rg__xx is
9696 -- Storage_Offset range 1 .. (Expr'size - Ada.Tags.Tag'object_size)
9697 -- / Storage_Unit
9698
9699 Sizexpr :=
9700 Make_Op_Subtract (Loc,
9701 Left_Opnd =>
9702 Make_Attribute_Reference (Loc,
9703 Prefix =>
9704 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9705 Attribute_Name => Name_Size),
9706 Right_Opnd =>
9707 Make_Attribute_Reference (Loc,
9708 Prefix => New_Occurrence_Of (RTE (RE_Tag), Loc),
9709 Attribute_Name => Name_Object_Size));
9710 end if;
9711
9712 Set_Paren_Count (Sizexpr, 1);
9713
9714 Append_To (List_Def,
9715 Make_Subtype_Declaration (Loc,
9716 Defining_Identifier => Range_Type,
9717 Subtype_Indication =>
9718 Make_Subtype_Indication (Loc,
9719 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9720 Constraint => Make_Range_Constraint (Loc,
9721 Range_Expression =>
9722 Make_Range (Loc,
9723 Low_Bound => Make_Integer_Literal (Loc, 1),
9724 High_Bound =>
9725 Make_Op_Divide (Loc,
9726 Left_Opnd => Sizexpr,
9727 Right_Opnd => Make_Integer_Literal (Loc,
9728 Intval => System_Storage_Unit)))))));
9729
9730 -- subtype str__nn is Storage_Array (rg__x);
9731
9732 Str_Type := Make_Temporary (Loc, 'S');
9733 Append_To (List_Def,
9734 Make_Subtype_Declaration (Loc,
9735 Defining_Identifier => Str_Type,
9736 Subtype_Indication =>
9737 Make_Subtype_Indication (Loc,
9738 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9739 Constraint =>
9740 Make_Index_Or_Discriminant_Constraint (Loc,
9741 Constraints =>
9742 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9743
9744 -- type Equiv_T is record
9745 -- _Parent : Snn; -- not interface
9746 -- _Tag : Ada.Tags.Tag -- interface
9747 -- Cnn : Str_Type;
9748 -- end Equiv_T;
9749
9750 Equiv_Type := Make_Temporary (Loc, 'T');
9751 Mutate_Ekind (Equiv_Type, E_Record_Type);
9752
9753 if not Is_Interface (Root_Typ) then
9754 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9755 end if;
9756
9757 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9758 -- treatment for this type. In particular, even though _parent's type
9759 -- is a controlled type or contains controlled components, we do not
9760 -- want to set Has_Controlled_Component on it to avoid making it gain
9761 -- an unwanted _controller component.
9762
9763 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9764
9765 -- A class-wide equivalent type does not require initialization
9766
9767 Set_Suppress_Initialization (Equiv_Type);
9768
9769 if not Is_Interface (Root_Typ) then
9770 Append_To (Comp_List,
9771 Make_Component_Declaration (Loc,
9772 Defining_Identifier =>
9773 Make_Defining_Identifier (Loc, Name_uParent),
9774 Component_Definition =>
9775 Make_Component_Definition (Loc,
9776 Aliased_Present => False,
9777 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9778
9779 Set_Reverse_Storage_Order
9780 (Equiv_Type, Reverse_Storage_Order (Base_Type (Root_Utyp)));
9781 Set_Reverse_Bit_Order
9782 (Equiv_Type, Reverse_Bit_Order (Base_Type (Root_Utyp)));
9783
9784 else
9785 Append_To (Comp_List,
9786 Make_Component_Declaration (Loc,
9787 Defining_Identifier =>
9788 Make_Defining_Identifier (Loc, Name_uTag),
9789 Component_Definition =>
9790 Make_Component_Definition (Loc,
9791 Aliased_Present => False,
9792 Subtype_Indication =>
9793 New_Occurrence_Of (RTE (RE_Tag), Loc))));
9794 end if;
9795
9796 Append_To (Comp_List,
9797 Make_Component_Declaration (Loc,
9798 Defining_Identifier => Make_Temporary (Loc, 'C'),
9799 Component_Definition =>
9800 Make_Component_Definition (Loc,
9801 Aliased_Present => False,
9802 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9803
9804 Append_To (List_Def,
9805 Make_Full_Type_Declaration (Loc,
9806 Defining_Identifier => Equiv_Type,
9807 Type_Definition =>
9808 Make_Record_Definition (Loc,
9809 Component_List =>
9810 Make_Component_List (Loc,
9811 Component_Items => Comp_List,
9812 Variant_Part => Empty))));
9813
9814 -- Suppress all checks during the analysis of the expanded code to avoid
9815 -- the generation of spurious warnings under ZFP run-time.
9816
9817 Insert_Actions (E, List_Def, Suppress => All_Checks);
9818
9819 -- In the case of an interface type mark the tag for First_Tag_Component
9820
9821 if Is_Interface (Root_Typ) then
9822 Set_Is_Tag (First_Entity (Equiv_Type));
9823 end if;
9824
9825 return Equiv_Type;
9826 end Make_CW_Equivalent_Type;
9827
9828 -------------------------
9829 -- Make_Invariant_Call --
9830 -------------------------
9831
9832 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9833 Loc : constant Source_Ptr := Sloc (Expr);
9834 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9835 pragma Assert (Has_Invariants (Typ));
9836 Proc_Id : constant Entity_Id := Invariant_Procedure (Typ);
9837 pragma Assert (Present (Proc_Id));
9838 begin
9839 -- The invariant procedure has a null body if assertions are disabled or
9840 -- Assertion_Policy Ignore is in effect. In that case, generate a null
9841 -- statement instead of a call to the invariant procedure.
9842
9843 if Has_Null_Body (Proc_Id) then
9844 return Make_Null_Statement (Loc);
9845 else
9846 return
9847 Make_Procedure_Call_Statement (Loc,
9848 Name => New_Occurrence_Of (Proc_Id, Loc),
9849 Parameter_Associations => New_List (Relocate_Node (Expr)));
9850 end if;
9851 end Make_Invariant_Call;
9852
9853 ------------------------
9854 -- Make_Literal_Range --
9855 ------------------------
9856
9857 function Make_Literal_Range
9858 (Loc : Source_Ptr;
9859 Literal_Typ : Entity_Id) return Node_Id
9860 is
9861 Lo : constant Node_Id :=
9862 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9863 Index : constant Entity_Id := Etype (Lo);
9864 Length_Expr : constant Node_Id :=
9865 Make_Op_Subtract (Loc,
9866 Left_Opnd =>
9867 Make_Integer_Literal (Loc,
9868 Intval => String_Literal_Length (Literal_Typ)),
9869 Right_Opnd => Make_Integer_Literal (Loc, 1));
9870
9871 Hi : Node_Id;
9872
9873 begin
9874 Set_Analyzed (Lo, False);
9875
9876 if Is_Integer_Type (Index) then
9877 Hi :=
9878 Make_Op_Add (Loc,
9879 Left_Opnd => New_Copy_Tree (Lo),
9880 Right_Opnd => Length_Expr);
9881 else
9882 Hi :=
9883 Make_Attribute_Reference (Loc,
9884 Attribute_Name => Name_Val,
9885 Prefix => New_Occurrence_Of (Index, Loc),
9886 Expressions => New_List (
9887 Make_Op_Add (Loc,
9888 Left_Opnd =>
9889 Make_Attribute_Reference (Loc,
9890 Attribute_Name => Name_Pos,
9891 Prefix => New_Occurrence_Of (Index, Loc),
9892 Expressions => New_List (New_Copy_Tree (Lo))),
9893 Right_Opnd => Length_Expr)));
9894 end if;
9895
9896 return
9897 Make_Range (Loc,
9898 Low_Bound => Lo,
9899 High_Bound => Hi);
9900 end Make_Literal_Range;
9901
9902 --------------------------
9903 -- Make_Non_Empty_Check --
9904 --------------------------
9905
9906 function Make_Non_Empty_Check
9907 (Loc : Source_Ptr;
9908 N : Node_Id) return Node_Id
9909 is
9910 begin
9911 return
9912 Make_Op_Ne (Loc,
9913 Left_Opnd =>
9914 Make_Attribute_Reference (Loc,
9915 Attribute_Name => Name_Length,
9916 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9917 Right_Opnd =>
9918 Make_Integer_Literal (Loc, 0));
9919 end Make_Non_Empty_Check;
9920
9921 -------------------------
9922 -- Make_Predicate_Call --
9923 -------------------------
9924
9925 -- WARNING: This routine manages Ghost regions. Return statements must be
9926 -- replaced by gotos which jump to the end of the routine and restore the
9927 -- Ghost mode.
9928
9929 function Make_Predicate_Call
9930 (Typ : Entity_Id;
9931 Expr : Node_Id;
9932 Mem : Boolean := False) return Node_Id
9933 is
9934 Loc : constant Source_Ptr := Sloc (Expr);
9935
9936 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9937 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9938 -- Save the Ghost-related attributes to restore on exit
9939
9940 Call : Node_Id;
9941 Func_Id : Entity_Id;
9942
9943 begin
9944 Func_Id := Predicate_Function (Typ);
9945 pragma Assert (Present (Func_Id));
9946
9947 -- The related type may be subject to pragma Ghost. Set the mode now to
9948 -- ensure that the call is properly marked as Ghost.
9949
9950 Set_Ghost_Mode (Typ);
9951
9952 -- Call special membership version if requested and available
9953
9954 if Mem and then Present (Predicate_Function_M (Typ)) then
9955 Func_Id := Predicate_Function_M (Typ);
9956 end if;
9957
9958 -- Case of calling normal predicate function
9959
9960 -- If the type is tagged, the expression may be class-wide, in which
9961 -- case it has to be converted to its root type, given that the
9962 -- generated predicate function is not dispatching. The conversion is
9963 -- type-safe and does not need validation, which matters when private
9964 -- extensions are involved.
9965
9966 if Is_Tagged_Type (Typ) then
9967 Call :=
9968 Make_Function_Call (Loc,
9969 Name => New_Occurrence_Of (Func_Id, Loc),
9970 Parameter_Associations =>
9971 New_List (OK_Convert_To (Typ, Relocate_Node (Expr))));
9972 else
9973 Call :=
9974 Make_Function_Call (Loc,
9975 Name => New_Occurrence_Of (Func_Id, Loc),
9976 Parameter_Associations => New_List (Relocate_Node (Expr)));
9977 end if;
9978
9979 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9980
9981 return Call;
9982 end Make_Predicate_Call;
9983
9984 --------------------------
9985 -- Make_Predicate_Check --
9986 --------------------------
9987
9988 function Make_Predicate_Check
9989 (Typ : Entity_Id;
9990 Expr : Node_Id) return Node_Id
9991 is
9992 Loc : constant Source_Ptr := Sloc (Expr);
9993
9994 procedure Add_Failure_Expression (Args : List_Id);
9995 -- Add the failure expression of pragma Predicate_Failure (if any) to
9996 -- list Args.
9997
9998 ----------------------------
9999 -- Add_Failure_Expression --
10000 ----------------------------
10001
10002 procedure Add_Failure_Expression (Args : List_Id) is
10003 function Failure_Expression return Node_Id;
10004 pragma Inline (Failure_Expression);
10005 -- Find aspect or pragma Predicate_Failure that applies to type Typ
10006 -- and return its expression. Return Empty if no such annotation is
10007 -- available.
10008
10009 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean;
10010 pragma Inline (Is_OK_PF_Aspect);
10011 -- Determine whether aspect Asp is a suitable Predicate_Failure
10012 -- aspect that applies to type Typ.
10013
10014 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean;
10015 pragma Inline (Is_OK_PF_Pragma);
10016 -- Determine whether pragma Prag is a suitable Predicate_Failure
10017 -- pragma that applies to type Typ.
10018
10019 procedure Replace_Subtype_Reference (N : Node_Id);
10020 -- Replace the current instance of type Typ denoted by N with
10021 -- expression Expr.
10022
10023 ------------------------
10024 -- Failure_Expression --
10025 ------------------------
10026
10027 function Failure_Expression return Node_Id is
10028 Item : Node_Id;
10029
10030 begin
10031 -- The management of the rep item chain involves "inheritance" of
10032 -- parent type chains. If a parent [sub]type is already subject to
10033 -- pragma Predicate_Failure, then the pragma will also appear in
10034 -- the chain of the child [sub]type, which in turn may possess a
10035 -- pragma of its own. Avoid order-dependent issues by inspecting
10036 -- the rep item chain directly. Note that routine Get_Pragma may
10037 -- return a parent pragma.
10038
10039 Item := First_Rep_Item (Typ);
10040 while Present (Item) loop
10041
10042 -- Predicate_Failure appears as an aspect
10043
10044 if Nkind (Item) = N_Aspect_Specification
10045 and then Is_OK_PF_Aspect (Item)
10046 then
10047 return Expression (Item);
10048
10049 -- Predicate_Failure appears as a pragma
10050
10051 elsif Nkind (Item) = N_Pragma
10052 and then Is_OK_PF_Pragma (Item)
10053 then
10054 return
10055 Get_Pragma_Arg
10056 (Next (First (Pragma_Argument_Associations (Item))));
10057 end if;
10058
10059 Next_Rep_Item (Item);
10060 end loop;
10061
10062 return Empty;
10063 end Failure_Expression;
10064
10065 ---------------------
10066 -- Is_OK_PF_Aspect --
10067 ---------------------
10068
10069 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean is
10070 begin
10071 -- To qualify, the aspect must apply to the type subjected to the
10072 -- predicate check.
10073
10074 return
10075 Chars (Identifier (Asp)) = Name_Predicate_Failure
10076 and then Present (Entity (Asp))
10077 and then Entity (Asp) = Typ;
10078 end Is_OK_PF_Aspect;
10079
10080 ---------------------
10081 -- Is_OK_PF_Pragma --
10082 ---------------------
10083
10084 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean is
10085 Args : constant List_Id := Pragma_Argument_Associations (Prag);
10086 Typ_Arg : Node_Id;
10087
10088 begin
10089 -- Nothing to do when the pragma does not denote Predicate_Failure
10090
10091 if Pragma_Name (Prag) /= Name_Predicate_Failure then
10092 return False;
10093
10094 -- Nothing to do when the pragma lacks arguments, in which case it
10095 -- is illegal.
10096
10097 elsif Is_Empty_List (Args) then
10098 return False;
10099 end if;
10100
10101 Typ_Arg := Get_Pragma_Arg (First (Args));
10102
10103 -- To qualify, the local name argument of the pragma must denote
10104 -- the type subjected to the predicate check.
10105
10106 return
10107 Is_Entity_Name (Typ_Arg)
10108 and then Present (Entity (Typ_Arg))
10109 and then Entity (Typ_Arg) = Typ;
10110 end Is_OK_PF_Pragma;
10111
10112 --------------------------------
10113 -- Replace_Subtype_Reference --
10114 --------------------------------
10115
10116 procedure Replace_Subtype_Reference (N : Node_Id) is
10117 begin
10118 Rewrite (N, New_Copy_Tree (Expr));
10119 end Replace_Subtype_Reference;
10120
10121 procedure Replace_Subtype_References is
10122 new Replace_Type_References_Generic (Replace_Subtype_Reference);
10123
10124 -- Local variables
10125
10126 PF_Expr : constant Node_Id := Failure_Expression;
10127 Expr : Node_Id;
10128
10129 -- Start of processing for Add_Failure_Expression
10130
10131 begin
10132 if Present (PF_Expr) then
10133
10134 -- Replace any occurrences of the current instance of the type
10135 -- with the object subjected to the predicate check.
10136
10137 Expr := New_Copy_Tree (PF_Expr);
10138 Replace_Subtype_References (Expr, Typ);
10139
10140 -- The failure expression appears as the third argument of the
10141 -- Check pragma.
10142
10143 Append_To (Args,
10144 Make_Pragma_Argument_Association (Loc,
10145 Expression => Expr));
10146 end if;
10147 end Add_Failure_Expression;
10148
10149 -- Local variables
10150
10151 Args : List_Id;
10152 Nam : Name_Id;
10153
10154 -- Start of processing for Make_Predicate_Check
10155
10156 begin
10157 -- If predicate checks are suppressed, then return a null statement. For
10158 -- this call, we check only the scope setting. If the caller wants to
10159 -- check a specific entity's setting, they must do it manually.
10160
10161 if Predicate_Checks_Suppressed (Empty) then
10162 return Make_Null_Statement (Loc);
10163 end if;
10164
10165 -- Do not generate a check within stream functions and the like.
10166
10167 if not Predicate_Check_In_Scope (Expr) then
10168 return Make_Null_Statement (Loc);
10169 end if;
10170
10171 -- Compute proper name to use, we need to get this right so that the
10172 -- right set of check policies apply to the Check pragma we are making.
10173
10174 if Has_Dynamic_Predicate_Aspect (Typ) then
10175 Nam := Name_Dynamic_Predicate;
10176 elsif Has_Static_Predicate_Aspect (Typ) then
10177 Nam := Name_Static_Predicate;
10178 else
10179 Nam := Name_Predicate;
10180 end if;
10181
10182 Args := New_List (
10183 Make_Pragma_Argument_Association (Loc,
10184 Expression => Make_Identifier (Loc, Nam)),
10185 Make_Pragma_Argument_Association (Loc,
10186 Expression => Make_Predicate_Call (Typ, Expr)));
10187
10188 -- If the subtype is subject to pragma Predicate_Failure, add the
10189 -- failure expression as an additional parameter.
10190
10191 Add_Failure_Expression (Args);
10192
10193 return
10194 Make_Pragma (Loc,
10195 Chars => Name_Check,
10196 Pragma_Argument_Associations => Args);
10197 end Make_Predicate_Check;
10198
10199 ----------------------------
10200 -- Make_Subtype_From_Expr --
10201 ----------------------------
10202
10203 -- 1. If Expr is an unconstrained array expression, creates
10204 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
10205
10206 -- 2. If Expr is a unconstrained discriminated type expression, creates
10207 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
10208
10209 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
10210
10211 function Make_Subtype_From_Expr
10212 (E : Node_Id;
10213 Unc_Typ : Entity_Id;
10214 Related_Id : Entity_Id := Empty) return Node_Id
10215 is
10216 List_Constr : constant List_Id := New_List;
10217 Loc : constant Source_Ptr := Sloc (E);
10218 D : Entity_Id;
10219 Full_Exp : Node_Id;
10220 Full_Subtyp : Entity_Id;
10221 High_Bound : Entity_Id;
10222 Index_Typ : Entity_Id;
10223 Low_Bound : Entity_Id;
10224 Priv_Subtyp : Entity_Id;
10225 Utyp : Entity_Id;
10226
10227 begin
10228 if Is_Private_Type (Unc_Typ)
10229 and then Has_Unknown_Discriminants (Unc_Typ)
10230 then
10231 -- The caller requests a unique external name for both the private
10232 -- and the full subtype.
10233
10234 if Present (Related_Id) then
10235 Full_Subtyp :=
10236 Make_Defining_Identifier (Loc,
10237 Chars => New_External_Name (Chars (Related_Id), 'C'));
10238 Priv_Subtyp :=
10239 Make_Defining_Identifier (Loc,
10240 Chars => New_External_Name (Chars (Related_Id), 'P'));
10241
10242 else
10243 Full_Subtyp := Make_Temporary (Loc, 'C');
10244 Priv_Subtyp := Make_Temporary (Loc, 'P');
10245 end if;
10246
10247 -- Prepare the subtype completion. Use the base type to find the
10248 -- underlying type because the type may be a generic actual or an
10249 -- explicit subtype.
10250
10251 Utyp := Underlying_Type (Base_Type (Unc_Typ));
10252
10253 Full_Exp :=
10254 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
10255 Set_Parent (Full_Exp, Parent (E));
10256
10257 Insert_Action (E,
10258 Make_Subtype_Declaration (Loc,
10259 Defining_Identifier => Full_Subtyp,
10260 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
10261
10262 -- Define the dummy private subtype
10263
10264 Mutate_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
10265 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
10266 Set_Scope (Priv_Subtyp, Full_Subtyp);
10267 Set_Is_Constrained (Priv_Subtyp);
10268 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
10269 Set_Is_Itype (Priv_Subtyp);
10270 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
10271
10272 if Is_Tagged_Type (Priv_Subtyp) then
10273 Set_Class_Wide_Type
10274 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
10275 Set_Direct_Primitive_Operations (Priv_Subtyp,
10276 Direct_Primitive_Operations (Unc_Typ));
10277 end if;
10278
10279 Set_Full_View (Priv_Subtyp, Full_Subtyp);
10280
10281 return New_Occurrence_Of (Priv_Subtyp, Loc);
10282
10283 elsif Is_Array_Type (Unc_Typ) then
10284 Index_Typ := First_Index (Unc_Typ);
10285 for J in 1 .. Number_Dimensions (Unc_Typ) loop
10286
10287 -- Capture the bounds of each index constraint in case the context
10288 -- is an object declaration of an unconstrained type initialized
10289 -- by a function call:
10290
10291 -- Obj : Unconstr_Typ := Func_Call;
10292
10293 -- This scenario requires secondary scope management and the index
10294 -- constraint cannot depend on the temporary used to capture the
10295 -- result of the function call.
10296
10297 -- SS_Mark;
10298 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
10299 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
10300 -- Obj : S := Temp.all;
10301 -- SS_Release; -- Temp is gone at this point, bounds of S are
10302 -- -- non existent.
10303
10304 -- Generate:
10305 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
10306
10307 Low_Bound := Make_Temporary (Loc, 'B');
10308 Insert_Action (E,
10309 Make_Object_Declaration (Loc,
10310 Defining_Identifier => Low_Bound,
10311 Object_Definition =>
10312 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10313 Constant_Present => True,
10314 Expression =>
10315 Make_Attribute_Reference (Loc,
10316 Prefix => Duplicate_Subexpr_No_Checks (E),
10317 Attribute_Name => Name_First,
10318 Expressions => New_List (
10319 Make_Integer_Literal (Loc, J)))));
10320
10321 -- Generate:
10322 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
10323
10324 High_Bound := Make_Temporary (Loc, 'B');
10325 Insert_Action (E,
10326 Make_Object_Declaration (Loc,
10327 Defining_Identifier => High_Bound,
10328 Object_Definition =>
10329 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10330 Constant_Present => True,
10331 Expression =>
10332 Make_Attribute_Reference (Loc,
10333 Prefix => Duplicate_Subexpr_No_Checks (E),
10334 Attribute_Name => Name_Last,
10335 Expressions => New_List (
10336 Make_Integer_Literal (Loc, J)))));
10337
10338 Append_To (List_Constr,
10339 Make_Range (Loc,
10340 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
10341 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
10342
10343 Next_Index (Index_Typ);
10344 end loop;
10345
10346 elsif Is_Class_Wide_Type (Unc_Typ) then
10347 declare
10348 CW_Subtype : Entity_Id;
10349 EQ_Typ : Entity_Id := Empty;
10350
10351 begin
10352 -- A class-wide equivalent type is not needed on VM targets
10353 -- because the VM back-ends handle the class-wide object
10354 -- initialization itself (and doesn't need or want the
10355 -- additional intermediate type to handle the assignment).
10356
10357 if Expander_Active and then Tagged_Type_Expansion then
10358
10359 -- If this is the class-wide type of a completion that is a
10360 -- record subtype, set the type of the class-wide type to be
10361 -- the full base type, for use in the expanded code for the
10362 -- equivalent type. Should this be done earlier when the
10363 -- completion is analyzed ???
10364
10365 if Is_Private_Type (Etype (Unc_Typ))
10366 and then
10367 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
10368 then
10369 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
10370 end if;
10371
10372 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
10373 end if;
10374
10375 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
10376 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
10377 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
10378
10379 return New_Occurrence_Of (CW_Subtype, Loc);
10380 end;
10381
10382 -- Indefinite record type with discriminants
10383
10384 else
10385 D := First_Discriminant (Unc_Typ);
10386 while Present (D) loop
10387 Append_To (List_Constr,
10388 Make_Selected_Component (Loc,
10389 Prefix => Duplicate_Subexpr_No_Checks (E),
10390 Selector_Name => New_Occurrence_Of (D, Loc)));
10391
10392 Next_Discriminant (D);
10393 end loop;
10394 end if;
10395
10396 return
10397 Make_Subtype_Indication (Loc,
10398 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
10399 Constraint =>
10400 Make_Index_Or_Discriminant_Constraint (Loc,
10401 Constraints => List_Constr));
10402 end Make_Subtype_From_Expr;
10403
10404 -----------------------------
10405 -- Make_Variant_Comparison --
10406 -----------------------------
10407
10408 function Make_Variant_Comparison
10409 (Loc : Source_Ptr;
10410 Typ : Entity_Id;
10411 Mode : Name_Id;
10412 Curr_Val : Node_Id;
10413 Old_Val : Node_Id) return Node_Id
10414 is
10415 function Big_Integer_Lt return Entity_Id;
10416 -- Returns the entity of the predefined "<" function from
10417 -- Ada.Numerics.Big_Numbers.Big_Integers.
10418
10419 --------------------
10420 -- Big_Integer_Lt --
10421 --------------------
10422
10423 function Big_Integer_Lt return Entity_Id is
10424 Big_Integers : constant Entity_Id :=
10425 RTU_Entity (Ada_Numerics_Big_Numbers_Big_Integers);
10426
10427 E : Entity_Id := First_Entity (Big_Integers);
10428
10429 begin
10430 while Present (E) loop
10431 if Chars (E) = Name_Op_Lt then
10432 return E;
10433 end if;
10434 Next_Entity (E);
10435 end loop;
10436
10437 raise Program_Error;
10438 end Big_Integer_Lt;
10439
10440 -- Start of processing for Make_Variant_Comparison
10441
10442 begin
10443 if Mode = Name_Increases then
10444 return Make_Op_Gt (Loc, Curr_Val, Old_Val);
10445
10446 else pragma Assert (Mode = Name_Decreases);
10447
10448 -- For discrete expressions use the "<" operator
10449
10450 if Is_Discrete_Type (Typ) then
10451 return Make_Op_Lt (Loc, Curr_Val, Old_Val);
10452
10453 -- For Big_Integer expressions use the "<" function, because the
10454 -- operator on private type might not be visible and won't be
10455 -- resolved.
10456
10457 else pragma Assert (Is_RTE (Base_Type (Typ), RE_Big_Integer));
10458 return
10459 Make_Function_Call (Loc,
10460 Name =>
10461 New_Occurrence_Of (Big_Integer_Lt, Loc),
10462 Parameter_Associations =>
10463 New_List (Curr_Val, Old_Val));
10464 end if;
10465 end if;
10466 end Make_Variant_Comparison;
10467
10468 -----------------
10469 -- Map_Formals --
10470 -----------------
10471
10472 procedure Map_Formals
10473 (Parent_Subp : Entity_Id;
10474 Derived_Subp : Entity_Id;
10475 Force_Update : Boolean := False)
10476 is
10477 Par_Formal : Entity_Id := First_Formal (Parent_Subp);
10478 Subp_Formal : Entity_Id := First_Formal (Derived_Subp);
10479
10480 begin
10481 if Force_Update then
10482 Type_Map.Set (Parent_Subp, Derived_Subp);
10483 end if;
10484
10485 -- At this stage either we are under regular processing and the caller
10486 -- has previously ensured that these primitives are already mapped (by
10487 -- means of calling previously to Update_Primitives_Mapping), or we are
10488 -- processing a late-overriding primitive and Force_Update updated above
10489 -- the mapping of these primitives.
10490
10491 while Present (Par_Formal) and then Present (Subp_Formal) loop
10492 Type_Map.Set (Par_Formal, Subp_Formal);
10493 Next_Formal (Par_Formal);
10494 Next_Formal (Subp_Formal);
10495 end loop;
10496 end Map_Formals;
10497
10498 ---------------
10499 -- Map_Types --
10500 ---------------
10501
10502 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
10503
10504 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
10505 -- avoid deep indentation of code.
10506
10507 -- NOTE: Routines which deal with discriminant mapping operate on the
10508 -- [underlying/record] full view of various types because those views
10509 -- contain all discriminants and stored constraints.
10510
10511 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
10512 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
10513 -- overriding chain starting from Prim whose dispatching type is parent
10514 -- type Par_Typ and add a mapping between the result and primitive Prim.
10515
10516 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
10517 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
10518 -- the inheritance or overriding chain of subprogram Subp. Return Empty
10519 -- if no such primitive is available.
10520
10521 function Build_Chain
10522 (Par_Typ : Entity_Id;
10523 Deriv_Typ : Entity_Id) return Elist_Id;
10524 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
10525 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
10526 -- list has the form:
10527 --
10528 -- head tail
10529 -- v v
10530 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
10531 --
10532 -- Note that Par_Typ is not part of the resulting derivation chain
10533
10534 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
10535 -- Return the view of type Typ which could potentially contains either
10536 -- the discriminants or stored constraints of the type.
10537
10538 function Find_Discriminant_Value
10539 (Discr : Entity_Id;
10540 Par_Typ : Entity_Id;
10541 Deriv_Typ : Entity_Id;
10542 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
10543 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
10544 -- in the derivation chain starting from parent type Par_Typ leading to
10545 -- derived type Deriv_Typ. The returned value is one of the following:
10546 --
10547 -- * An entity which is either a discriminant or a nondiscriminant
10548 -- name, and renames/constraints Discr.
10549 --
10550 -- * An expression which constraints Discr
10551 --
10552 -- Typ_Elmt is an element of the derivation chain created by routine
10553 -- Build_Chain and denotes the current ancestor being examined.
10554
10555 procedure Map_Discriminants
10556 (Par_Typ : Entity_Id;
10557 Deriv_Typ : Entity_Id);
10558 -- Map each discriminant of type Par_Typ to a meaningful constraint
10559 -- from the point of view of type Deriv_Typ.
10560
10561 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
10562 -- Map each primitive of type Par_Typ to a corresponding primitive of
10563 -- type Deriv_Typ.
10564
10565 -------------------
10566 -- Add_Primitive --
10567 -------------------
10568
10569 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
10570 Par_Prim : Entity_Id;
10571
10572 begin
10573 -- Inspect the inheritance chain through the Alias attribute and the
10574 -- overriding chain through the Overridden_Operation looking for an
10575 -- ancestor primitive with the appropriate dispatching type.
10576
10577 Par_Prim := Prim;
10578 while Present (Par_Prim) loop
10579 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
10580 Par_Prim := Ancestor_Primitive (Par_Prim);
10581 end loop;
10582
10583 -- Create a mapping of the form:
10584
10585 -- parent type primitive -> derived type primitive
10586
10587 if Present (Par_Prim) then
10588 Type_Map.Set (Par_Prim, Prim);
10589 end if;
10590 end Add_Primitive;
10591
10592 ------------------------
10593 -- Ancestor_Primitive --
10594 ------------------------
10595
10596 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
10597 Inher_Prim : constant Entity_Id := Alias (Subp);
10598 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
10599
10600 begin
10601 -- The current subprogram overrides an ancestor primitive
10602
10603 if Present (Over_Prim) then
10604 return Over_Prim;
10605
10606 -- The current subprogram is an internally generated alias of an
10607 -- inherited ancestor primitive.
10608
10609 elsif Present (Inher_Prim) then
10610 -- It is possible that an internally generated alias could be
10611 -- set to a subprogram which overrides the same aliased primitive,
10612 -- so return Empty in this case.
10613
10614 if Ancestor_Primitive (Inher_Prim) = Subp then
10615 return Empty;
10616 end if;
10617
10618 return Inher_Prim;
10619
10620 -- Otherwise the current subprogram is the root of the inheritance or
10621 -- overriding chain.
10622
10623 else
10624 return Empty;
10625 end if;
10626 end Ancestor_Primitive;
10627
10628 -----------------
10629 -- Build_Chain --
10630 -----------------
10631
10632 function Build_Chain
10633 (Par_Typ : Entity_Id;
10634 Deriv_Typ : Entity_Id) return Elist_Id
10635 is
10636 Anc_Typ : Entity_Id;
10637 Chain : Elist_Id;
10638 Curr_Typ : Entity_Id;
10639
10640 begin
10641 Chain := New_Elmt_List;
10642
10643 -- Add the derived type to the derivation chain
10644
10645 Prepend_Elmt (Deriv_Typ, Chain);
10646
10647 -- Examine all ancestors starting from the derived type climbing
10648 -- towards parent type Par_Typ.
10649
10650 Curr_Typ := Deriv_Typ;
10651 loop
10652 -- Handle the case where the current type is a record which
10653 -- derives from a subtype.
10654
10655 -- subtype Sub_Typ is Par_Typ ...
10656 -- type Deriv_Typ is Sub_Typ ...
10657
10658 if Ekind (Curr_Typ) = E_Record_Type
10659 and then Present (Parent_Subtype (Curr_Typ))
10660 then
10661 Anc_Typ := Parent_Subtype (Curr_Typ);
10662
10663 -- Handle the case where the current type is a record subtype of
10664 -- another subtype.
10665
10666 -- subtype Sub_Typ1 is Par_Typ ...
10667 -- subtype Sub_Typ2 is Sub_Typ1 ...
10668
10669 elsif Ekind (Curr_Typ) = E_Record_Subtype
10670 and then Present (Cloned_Subtype (Curr_Typ))
10671 then
10672 Anc_Typ := Cloned_Subtype (Curr_Typ);
10673
10674 -- Otherwise use the direct parent type
10675
10676 else
10677 Anc_Typ := Etype (Curr_Typ);
10678 end if;
10679
10680 -- Use the first subtype when dealing with itypes
10681
10682 if Is_Itype (Anc_Typ) then
10683 Anc_Typ := First_Subtype (Anc_Typ);
10684 end if;
10685
10686 -- Work with the view which contains the discriminants and stored
10687 -- constraints.
10688
10689 Anc_Typ := Discriminated_View (Anc_Typ);
10690
10691 -- Stop the climb when either the parent type has been reached or
10692 -- there are no more ancestors left to examine.
10693
10694 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
10695
10696 Prepend_Unique_Elmt (Anc_Typ, Chain);
10697 Curr_Typ := Anc_Typ;
10698 end loop;
10699
10700 return Chain;
10701 end Build_Chain;
10702
10703 ------------------------
10704 -- Discriminated_View --
10705 ------------------------
10706
10707 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
10708 T : Entity_Id;
10709
10710 begin
10711 T := Typ;
10712
10713 -- Use the [underlying] full view when dealing with private types
10714 -- because the view contains all inherited discriminants or stored
10715 -- constraints.
10716
10717 if Is_Private_Type (T) then
10718 if Present (Underlying_Full_View (T)) then
10719 T := Underlying_Full_View (T);
10720
10721 elsif Present (Full_View (T)) then
10722 T := Full_View (T);
10723 end if;
10724 end if;
10725
10726 -- Use the underlying record view when the type is an extenstion of
10727 -- a parent type with unknown discriminants because the view contains
10728 -- all inherited discriminants or stored constraints.
10729
10730 if Ekind (T) = E_Record_Type
10731 and then Present (Underlying_Record_View (T))
10732 then
10733 T := Underlying_Record_View (T);
10734 end if;
10735
10736 return T;
10737 end Discriminated_View;
10738
10739 -----------------------------
10740 -- Find_Discriminant_Value --
10741 -----------------------------
10742
10743 function Find_Discriminant_Value
10744 (Discr : Entity_Id;
10745 Par_Typ : Entity_Id;
10746 Deriv_Typ : Entity_Id;
10747 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
10748 is
10749 Discr_Pos : constant Uint := Discriminant_Number (Discr);
10750 Typ : constant Entity_Id := Node (Typ_Elmt);
10751
10752 function Find_Constraint_Value
10753 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
10754 -- Given constraint Constr, find what it denotes. This is either:
10755 --
10756 -- * An entity which is either a discriminant or a name
10757 --
10758 -- * An expression
10759
10760 ---------------------------
10761 -- Find_Constraint_Value --
10762 ---------------------------
10763
10764 function Find_Constraint_Value
10765 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
10766 is
10767 begin
10768 if Nkind (Constr) in N_Entity then
10769
10770 -- The constraint denotes a discriminant of the curren type
10771 -- which renames the ancestor discriminant:
10772
10773 -- vv
10774 -- type Typ (D1 : ...; DN : ...) is
10775 -- new Anc (Discr => D1) with ...
10776 -- ^^
10777
10778 if Ekind (Constr) = E_Discriminant then
10779
10780 -- The discriminant belongs to derived type Deriv_Typ. This
10781 -- is the final value for the ancestor discriminant as the
10782 -- derivations chain has been fully exhausted.
10783
10784 if Typ = Deriv_Typ then
10785 return Constr;
10786
10787 -- Otherwise the discriminant may be renamed or constrained
10788 -- at a lower level. Continue looking down the derivation
10789 -- chain.
10790
10791 else
10792 return
10793 Find_Discriminant_Value
10794 (Discr => Constr,
10795 Par_Typ => Par_Typ,
10796 Deriv_Typ => Deriv_Typ,
10797 Typ_Elmt => Next_Elmt (Typ_Elmt));
10798 end if;
10799
10800 -- Otherwise the constraint denotes a reference to some name
10801 -- which results in a Stored discriminant:
10802
10803 -- vvvv
10804 -- Name : ...;
10805 -- type Typ (D1 : ...; DN : ...) is
10806 -- new Anc (Discr => Name) with ...
10807 -- ^^^^
10808
10809 -- Return the name as this is the proper constraint of the
10810 -- discriminant.
10811
10812 else
10813 return Constr;
10814 end if;
10815
10816 -- The constraint denotes a reference to a name
10817
10818 elsif Is_Entity_Name (Constr) then
10819 return Find_Constraint_Value (Entity (Constr));
10820
10821 -- Otherwise the current constraint is an expression which yields
10822 -- a Stored discriminant:
10823
10824 -- type Typ (D1 : ...; DN : ...) is
10825 -- new Anc (Discr => <expression>) with ...
10826 -- ^^^^^^^^^^
10827
10828 -- Return the expression as this is the proper constraint of the
10829 -- discriminant.
10830
10831 else
10832 return Constr;
10833 end if;
10834 end Find_Constraint_Value;
10835
10836 -- Local variables
10837
10838 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10839
10840 Constr_Elmt : Elmt_Id;
10841 Pos : Uint;
10842 Typ_Discr : Entity_Id;
10843
10844 -- Start of processing for Find_Discriminant_Value
10845
10846 begin
10847 -- The algorithm for finding the value of a discriminant works as
10848 -- follows. First, it recreates the derivation chain from Par_Typ
10849 -- to Deriv_Typ as a list:
10850
10851 -- Par_Typ (shown for completeness)
10852 -- v
10853 -- Ancestor_N <-- head of chain
10854 -- v
10855 -- Ancestor_1
10856 -- v
10857 -- Deriv_Typ <-- tail of chain
10858
10859 -- The algorithm then traces the fate of a parent discriminant down
10860 -- the derivation chain. At each derivation level, the discriminant
10861 -- may be either inherited or constrained.
10862
10863 -- 1) Discriminant is inherited: there are two cases, depending on
10864 -- which type is inheriting.
10865
10866 -- 1.1) Deriv_Typ is inheriting:
10867
10868 -- type Ancestor (D_1 : ...) is tagged ...
10869 -- type Deriv_Typ is new Ancestor ...
10870
10871 -- In this case the inherited discriminant is the final value of
10872 -- the parent discriminant because the end of the derivation chain
10873 -- has been reached.
10874
10875 -- 1.2) Some other type is inheriting:
10876
10877 -- type Ancestor_1 (D_1 : ...) is tagged ...
10878 -- type Ancestor_2 is new Ancestor_1 ...
10879
10880 -- In this case the algorithm continues to trace the fate of the
10881 -- inherited discriminant down the derivation chain because it may
10882 -- be further inherited or constrained.
10883
10884 -- 2) Discriminant is constrained: there are three cases, depending
10885 -- on what the constraint is.
10886
10887 -- 2.1) The constraint is another discriminant (aka renaming):
10888
10889 -- type Ancestor_1 (D_1 : ...) is tagged ...
10890 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10891
10892 -- In this case the constraining discriminant becomes the one to
10893 -- track down the derivation chain. The algorithm already knows
10894 -- that D_2 constrains D_1, therefore if the algorithm finds the
10895 -- value of D_2, then this would also be the value for D_1.
10896
10897 -- 2.2) The constraint is a name (aka Stored):
10898
10899 -- Name : ...
10900 -- type Ancestor_1 (D_1 : ...) is tagged ...
10901 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10902
10903 -- In this case the name is the final value of D_1 because the
10904 -- discriminant cannot be further constrained.
10905
10906 -- 2.3) The constraint is an expression (aka Stored):
10907
10908 -- type Ancestor_1 (D_1 : ...) is tagged ...
10909 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10910
10911 -- Similar to 2.2, the expression is the final value of D_1
10912
10913 Pos := Uint_1;
10914
10915 -- When a derived type constrains its parent type, all constaints
10916 -- appear in the Stored_Constraint list. Examine the list looking
10917 -- for a positional match.
10918
10919 if Present (Constrs) then
10920 Constr_Elmt := First_Elmt (Constrs);
10921 while Present (Constr_Elmt) loop
10922
10923 -- The position of the current constraint matches that of the
10924 -- ancestor discriminant.
10925
10926 if Pos = Discr_Pos then
10927 return Find_Constraint_Value (Node (Constr_Elmt));
10928 end if;
10929
10930 Next_Elmt (Constr_Elmt);
10931 Pos := Pos + 1;
10932 end loop;
10933
10934 -- Otherwise the derived type does not constraint its parent type in
10935 -- which case it inherits the parent discriminants.
10936
10937 else
10938 Typ_Discr := First_Discriminant (Typ);
10939 while Present (Typ_Discr) loop
10940
10941 -- The position of the current discriminant matches that of the
10942 -- ancestor discriminant.
10943
10944 if Pos = Discr_Pos then
10945 return Find_Constraint_Value (Typ_Discr);
10946 end if;
10947
10948 Next_Discriminant (Typ_Discr);
10949 Pos := Pos + 1;
10950 end loop;
10951 end if;
10952
10953 -- A discriminant must always have a corresponding value. This is
10954 -- either another discriminant, a name, or an expression. If this
10955 -- point is reached, them most likely the derivation chain employs
10956 -- the wrong views of types.
10957
10958 pragma Assert (False);
10959
10960 return Empty;
10961 end Find_Discriminant_Value;
10962
10963 -----------------------
10964 -- Map_Discriminants --
10965 -----------------------
10966
10967 procedure Map_Discriminants
10968 (Par_Typ : Entity_Id;
10969 Deriv_Typ : Entity_Id)
10970 is
10971 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10972
10973 Discr : Entity_Id;
10974 Discr_Val : Node_Or_Entity_Id;
10975
10976 begin
10977 -- Examine each discriminant of parent type Par_Typ and find a
10978 -- suitable value for it from the point of view of derived type
10979 -- Deriv_Typ.
10980
10981 if Has_Discriminants (Par_Typ) then
10982 Discr := First_Discriminant (Par_Typ);
10983 while Present (Discr) loop
10984 Discr_Val :=
10985 Find_Discriminant_Value
10986 (Discr => Discr,
10987 Par_Typ => Par_Typ,
10988 Deriv_Typ => Deriv_Typ,
10989 Typ_Elmt => First_Elmt (Deriv_Chain));
10990
10991 -- Create a mapping of the form:
10992
10993 -- parent type discriminant -> value
10994
10995 Type_Map.Set (Discr, Discr_Val);
10996
10997 Next_Discriminant (Discr);
10998 end loop;
10999 end if;
11000 end Map_Discriminants;
11001
11002 --------------------
11003 -- Map_Primitives --
11004 --------------------
11005
11006 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
11007 Deriv_Prim : Entity_Id;
11008 Par_Prim : Entity_Id;
11009 Par_Prims : Elist_Id;
11010 Prim_Elmt : Elmt_Id;
11011
11012 begin
11013 -- Inspect the primitives of the derived type and determine whether
11014 -- they relate to the primitives of the parent type. If there is a
11015 -- meaningful relation, create a mapping of the form:
11016
11017 -- parent type primitive -> derived type primitive
11018
11019 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
11020 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
11021 while Present (Prim_Elmt) loop
11022 Deriv_Prim := Node (Prim_Elmt);
11023
11024 if Is_Subprogram (Deriv_Prim)
11025 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
11026 then
11027 Add_Primitive (Deriv_Prim, Par_Typ);
11028 end if;
11029
11030 Next_Elmt (Prim_Elmt);
11031 end loop;
11032 end if;
11033
11034 -- If the parent operation is an interface operation, the overriding
11035 -- indicator is not present. Instead, we get from the interface
11036 -- operation the primitive of the current type that implements it.
11037
11038 if Is_Interface (Par_Typ) then
11039 Par_Prims := Collect_Primitive_Operations (Par_Typ);
11040
11041 if Present (Par_Prims) then
11042 Prim_Elmt := First_Elmt (Par_Prims);
11043
11044 while Present (Prim_Elmt) loop
11045 Par_Prim := Node (Prim_Elmt);
11046 Deriv_Prim :=
11047 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
11048
11049 if Present (Deriv_Prim) then
11050 Type_Map.Set (Par_Prim, Deriv_Prim);
11051 end if;
11052
11053 Next_Elmt (Prim_Elmt);
11054 end loop;
11055 end if;
11056 end if;
11057 end Map_Primitives;
11058
11059 -- Start of processing for Map_Types
11060
11061 begin
11062 -- Nothing to do if there are no types to work with
11063
11064 if No (Parent_Type) or else No (Derived_Type) then
11065 return;
11066
11067 -- Nothing to do if the mapping already exists
11068
11069 elsif Type_Map.Get (Parent_Type) = Derived_Type then
11070 return;
11071
11072 -- Nothing to do if both types are not tagged. Note that untagged types
11073 -- do not have primitive operations and their discriminants are already
11074 -- handled by gigi.
11075
11076 elsif not Is_Tagged_Type (Parent_Type)
11077 or else not Is_Tagged_Type (Derived_Type)
11078 then
11079 return;
11080 end if;
11081
11082 -- Create a mapping of the form
11083
11084 -- parent type -> derived type
11085
11086 -- to prevent any subsequent attempts to produce the same relations
11087
11088 Type_Map.Set (Parent_Type, Derived_Type);
11089
11090 -- Create mappings of the form
11091
11092 -- parent type discriminant -> derived type discriminant
11093 -- <or>
11094 -- parent type discriminant -> constraint
11095
11096 -- Note that mapping of discriminants breaks privacy because it needs to
11097 -- work with those views which contains the discriminants and any stored
11098 -- constraints.
11099
11100 Map_Discriminants
11101 (Par_Typ => Discriminated_View (Parent_Type),
11102 Deriv_Typ => Discriminated_View (Derived_Type));
11103
11104 -- Create mappings of the form
11105
11106 -- parent type primitive -> derived type primitive
11107
11108 Map_Primitives
11109 (Par_Typ => Parent_Type,
11110 Deriv_Typ => Derived_Type);
11111 end Map_Types;
11112
11113 ----------------------------
11114 -- Matching_Standard_Type --
11115 ----------------------------
11116
11117 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
11118 pragma Assert (Is_Scalar_Type (Typ));
11119 Siz : constant Uint := Esize (Typ);
11120
11121 begin
11122 -- Floating-point cases
11123
11124 if Is_Floating_Point_Type (Typ) then
11125 if Siz <= Esize (Standard_Short_Float) then
11126 return Standard_Short_Float;
11127 elsif Siz <= Esize (Standard_Float) then
11128 return Standard_Float;
11129 elsif Siz <= Esize (Standard_Long_Float) then
11130 return Standard_Long_Float;
11131 elsif Siz <= Esize (Standard_Long_Long_Float) then
11132 return Standard_Long_Long_Float;
11133 else
11134 raise Program_Error;
11135 end if;
11136
11137 -- Integer cases (includes fixed-point types)
11138
11139 -- Unsigned integer cases (includes normal enumeration types)
11140
11141 else
11142 return Small_Integer_Type_For (Siz, Is_Unsigned_Type (Typ));
11143 end if;
11144 end Matching_Standard_Type;
11145
11146 -----------------------------
11147 -- May_Generate_Large_Temp --
11148 -----------------------------
11149
11150 -- At the current time, the only types that we return False for (i.e. where
11151 -- we decide we know they cannot generate large temps) are ones where we
11152 -- know the size is 256 bits or less at compile time, and we are still not
11153 -- doing a thorough job on arrays and records.
11154
11155 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
11156 begin
11157 if not Size_Known_At_Compile_Time (Typ) then
11158 return False;
11159 end if;
11160
11161 if Known_Esize (Typ) and then Esize (Typ) <= 256 then
11162 return False;
11163 end if;
11164
11165 if Is_Array_Type (Typ)
11166 and then Present (Packed_Array_Impl_Type (Typ))
11167 then
11168 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
11169 end if;
11170
11171 return True;
11172 end May_Generate_Large_Temp;
11173
11174 --------------------------------------------
11175 -- Needs_Conditional_Null_Excluding_Check --
11176 --------------------------------------------
11177
11178 function Needs_Conditional_Null_Excluding_Check
11179 (Typ : Entity_Id) return Boolean
11180 is
11181 begin
11182 return
11183 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
11184 end Needs_Conditional_Null_Excluding_Check;
11185
11186 ----------------------------
11187 -- Needs_Constant_Address --
11188 ----------------------------
11189
11190 function Needs_Constant_Address
11191 (Decl : Node_Id;
11192 Typ : Entity_Id) return Boolean
11193 is
11194 begin
11195 -- If we have no initialization of any kind, then we don't need to place
11196 -- any restrictions on the address clause, because the object will be
11197 -- elaborated after the address clause is evaluated. This happens if the
11198 -- declaration has no initial expression, or the type has no implicit
11199 -- initialization, or the object is imported.
11200
11201 -- The same holds for all initialized scalar types and all access types.
11202 -- Packed bit array types of size up to the maximum integer size are
11203 -- represented using a modular type with an initialization (to zero) and
11204 -- can be processed like other initialized scalar types.
11205
11206 -- If the type is controlled, code to attach the object to a
11207 -- finalization chain is generated at the point of declaration, and
11208 -- therefore the elaboration of the object cannot be delayed: the
11209 -- address expression must be a constant.
11210
11211 if No (Expression (Decl))
11212 and then not Needs_Finalization (Typ)
11213 and then
11214 (not Has_Non_Null_Base_Init_Proc (Typ)
11215 or else Is_Imported (Defining_Identifier (Decl)))
11216 then
11217 return False;
11218
11219 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
11220 or else Is_Access_Type (Typ)
11221 or else
11222 (Is_Bit_Packed_Array (Typ)
11223 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
11224 then
11225 return False;
11226
11227 else
11228 -- Otherwise, we require the address clause to be constant because
11229 -- the call to the initialization procedure (or the attach code) has
11230 -- to happen at the point of the declaration.
11231
11232 -- Actually the IP call has been moved to the freeze actions anyway,
11233 -- so maybe we can relax this restriction???
11234
11235 return True;
11236 end if;
11237 end Needs_Constant_Address;
11238
11239 ----------------------------
11240 -- New_Class_Wide_Subtype --
11241 ----------------------------
11242
11243 function New_Class_Wide_Subtype
11244 (CW_Typ : Entity_Id;
11245 N : Node_Id) return Entity_Id
11246 is
11247 Res : constant Entity_Id := Create_Itype (E_Void, N);
11248
11249 -- Capture relevant attributes of the class-wide subtype which must be
11250 -- restored after the copy.
11251
11252 Res_Chars : constant Name_Id := Chars (Res);
11253 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
11254 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
11255 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
11256 Res_Scope : constant Entity_Id := Scope (Res);
11257
11258 begin
11259 Copy_Node (CW_Typ, Res);
11260
11261 -- Restore the relevant attributes of the class-wide subtype
11262
11263 Set_Chars (Res, Res_Chars);
11264 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
11265 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
11266 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
11267 Set_Scope (Res, Res_Scope);
11268
11269 -- Decorate the class-wide subtype
11270
11271 Set_Associated_Node_For_Itype (Res, N);
11272 Set_Comes_From_Source (Res, False);
11273 Mutate_Ekind (Res, E_Class_Wide_Subtype);
11274 Set_Etype (Res, Base_Type (CW_Typ));
11275 Set_Freeze_Node (Res, Empty);
11276 Set_Is_Frozen (Res, False);
11277 Set_Is_Itype (Res);
11278 Set_Is_Public (Res, False);
11279 Set_Next_Entity (Res, Empty);
11280 Set_Prev_Entity (Res, Empty);
11281 Set_Sloc (Res, Sloc (N));
11282
11283 Set_Public_Status (Res);
11284
11285 return Res;
11286 end New_Class_Wide_Subtype;
11287
11288 -----------------------------------
11289 -- OK_To_Do_Constant_Replacement --
11290 -----------------------------------
11291
11292 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
11293 ES : constant Entity_Id := Scope (E);
11294 CS : Entity_Id;
11295
11296 begin
11297 -- Do not replace statically allocated objects, because they may be
11298 -- modified outside the current scope.
11299
11300 if Is_Statically_Allocated (E) then
11301 return False;
11302
11303 -- Do not replace aliased or volatile objects, since we don't know what
11304 -- else might change the value.
11305
11306 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
11307 return False;
11308
11309 -- Debug flag -gnatdM disconnects this optimization
11310
11311 elsif Debug_Flag_MM then
11312 return False;
11313
11314 -- Otherwise check scopes
11315
11316 else
11317 CS := Current_Scope;
11318
11319 loop
11320 -- If we are in right scope, replacement is safe
11321
11322 if CS = ES then
11323 return True;
11324
11325 -- Packages do not affect the determination of safety
11326
11327 elsif Ekind (CS) = E_Package then
11328 exit when CS = Standard_Standard;
11329 CS := Scope (CS);
11330
11331 -- Blocks do not affect the determination of safety
11332
11333 elsif Ekind (CS) = E_Block then
11334 CS := Scope (CS);
11335
11336 -- Loops do not affect the determination of safety. Note that we
11337 -- kill all current values on entry to a loop, so we are just
11338 -- talking about processing within a loop here.
11339
11340 elsif Ekind (CS) = E_Loop then
11341 CS := Scope (CS);
11342
11343 -- Otherwise, the reference is dubious, and we cannot be sure that
11344 -- it is safe to do the replacement.
11345
11346 else
11347 exit;
11348 end if;
11349 end loop;
11350
11351 return False;
11352 end if;
11353 end OK_To_Do_Constant_Replacement;
11354
11355 ------------------------------------
11356 -- Possible_Bit_Aligned_Component --
11357 ------------------------------------
11358
11359 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
11360 begin
11361 -- Do not process an unanalyzed node because it is not yet decorated and
11362 -- most checks performed below will fail.
11363
11364 if not Analyzed (N) then
11365 return False;
11366 end if;
11367
11368 -- There are never alignment issues in CodePeer mode
11369
11370 if CodePeer_Mode then
11371 return False;
11372 end if;
11373
11374 case Nkind (N) is
11375
11376 -- Case of indexed component
11377
11378 when N_Indexed_Component =>
11379 declare
11380 P : constant Node_Id := Prefix (N);
11381 Ptyp : constant Entity_Id := Etype (P);
11382
11383 begin
11384 -- If we know the component size and it is not larger than the
11385 -- maximum integer size, then we are OK. The back end does the
11386 -- assignment of small misaligned objects correctly.
11387
11388 if Known_Static_Component_Size (Ptyp)
11389 and then Component_Size (Ptyp) <= System_Max_Integer_Size
11390 then
11391 return False;
11392
11393 -- Otherwise, we need to test the prefix, to see if we are
11394 -- indexing from a possibly unaligned component.
11395
11396 else
11397 return Possible_Bit_Aligned_Component (P);
11398 end if;
11399 end;
11400
11401 -- Case of selected component
11402
11403 when N_Selected_Component =>
11404 declare
11405 P : constant Node_Id := Prefix (N);
11406 Comp : constant Entity_Id := Entity (Selector_Name (N));
11407
11408 begin
11409 -- This is the crucial test: if the component itself causes
11410 -- trouble, then we can stop and return True.
11411
11412 if Component_May_Be_Bit_Aligned (Comp) then
11413 return True;
11414
11415 -- Otherwise, we need to test the prefix, to see if we are
11416 -- selecting from a possibly unaligned component.
11417
11418 else
11419 return Possible_Bit_Aligned_Component (P);
11420 end if;
11421 end;
11422
11423 -- For a slice, test the prefix, if that is possibly misaligned,
11424 -- then for sure the slice is.
11425
11426 when N_Slice =>
11427 return Possible_Bit_Aligned_Component (Prefix (N));
11428
11429 -- For an unchecked conversion, check whether the expression may
11430 -- be bit aligned.
11431
11432 when N_Unchecked_Type_Conversion =>
11433 return Possible_Bit_Aligned_Component (Expression (N));
11434
11435 -- If we have none of the above, it means that we have fallen off the
11436 -- top testing prefixes recursively, and we now have a stand alone
11437 -- object, where we don't have a problem, unless this is a renaming,
11438 -- in which case we need to look into the renamed object.
11439
11440 when others =>
11441 if Is_Entity_Name (N)
11442 and then Is_Object (Entity (N))
11443 and then Present (Renamed_Object (Entity (N)))
11444 then
11445 return
11446 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
11447 else
11448 return False;
11449 end if;
11450 end case;
11451 end Possible_Bit_Aligned_Component;
11452
11453 -----------------------------------------------
11454 -- Process_Statements_For_Controlled_Objects --
11455 -----------------------------------------------
11456
11457 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
11458 Loc : constant Source_Ptr := Sloc (N);
11459
11460 function Are_Wrapped (L : List_Id) return Boolean;
11461 -- Determine whether list L contains only one statement which is a block
11462
11463 function Wrap_Statements_In_Block
11464 (L : List_Id;
11465 Scop : Entity_Id := Current_Scope) return Node_Id;
11466 -- Given a list of statements L, wrap it in a block statement and return
11467 -- the generated node. Scop is either the current scope or the scope of
11468 -- the context (if applicable).
11469
11470 -----------------
11471 -- Are_Wrapped --
11472 -----------------
11473
11474 function Are_Wrapped (L : List_Id) return Boolean is
11475 Stmt : constant Node_Id := First (L);
11476 begin
11477 return
11478 Present (Stmt)
11479 and then No (Next (Stmt))
11480 and then Nkind (Stmt) = N_Block_Statement;
11481 end Are_Wrapped;
11482
11483 ------------------------------
11484 -- Wrap_Statements_In_Block --
11485 ------------------------------
11486
11487 function Wrap_Statements_In_Block
11488 (L : List_Id;
11489 Scop : Entity_Id := Current_Scope) return Node_Id
11490 is
11491 Block_Id : Entity_Id;
11492 Block_Nod : Node_Id;
11493 Iter_Loop : Entity_Id;
11494
11495 begin
11496 Block_Nod :=
11497 Make_Block_Statement (Loc,
11498 Declarations => No_List,
11499 Handled_Statement_Sequence =>
11500 Make_Handled_Sequence_Of_Statements (Loc,
11501 Statements => L));
11502
11503 -- Create a label for the block in case the block needs to manage the
11504 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
11505
11506 Add_Block_Identifier (Block_Nod, Block_Id);
11507
11508 -- When wrapping the statements of an iterator loop, check whether
11509 -- the loop requires secondary stack management and if so, propagate
11510 -- the appropriate flags to the block. This ensures that the cursor
11511 -- is properly cleaned up at each iteration of the loop.
11512
11513 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
11514
11515 if Present (Iter_Loop) then
11516 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
11517
11518 -- Secondary stack reclamation is suppressed when the associated
11519 -- iterator loop contains a return statement which uses the stack.
11520
11521 Set_Sec_Stack_Needed_For_Return
11522 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
11523 end if;
11524
11525 return Block_Nod;
11526 end Wrap_Statements_In_Block;
11527
11528 -- Local variables
11529
11530 Block : Node_Id;
11531
11532 -- Start of processing for Process_Statements_For_Controlled_Objects
11533
11534 begin
11535 -- Whenever a non-handled statement list is wrapped in a block, the
11536 -- block must be explicitly analyzed to redecorate all entities in the
11537 -- list and ensure that a finalizer is properly built.
11538
11539 case Nkind (N) is
11540 when N_Conditional_Entry_Call
11541 | N_Elsif_Part
11542 | N_If_Statement
11543 | N_Selective_Accept
11544 =>
11545 -- Check the "then statements" for elsif parts and if statements
11546
11547 if Nkind (N) in N_Elsif_Part | N_If_Statement
11548 and then not Is_Empty_List (Then_Statements (N))
11549 and then not Are_Wrapped (Then_Statements (N))
11550 and then Requires_Cleanup_Actions
11551 (L => Then_Statements (N),
11552 Lib_Level => False,
11553 Nested_Constructs => False)
11554 then
11555 Block := Wrap_Statements_In_Block (Then_Statements (N));
11556 Set_Then_Statements (N, New_List (Block));
11557
11558 Analyze (Block);
11559 end if;
11560
11561 -- Check the "else statements" for conditional entry calls, if
11562 -- statements and selective accepts.
11563
11564 if Nkind (N) in
11565 N_Conditional_Entry_Call | N_If_Statement | N_Selective_Accept
11566 and then not Is_Empty_List (Else_Statements (N))
11567 and then not Are_Wrapped (Else_Statements (N))
11568 and then Requires_Cleanup_Actions
11569 (L => Else_Statements (N),
11570 Lib_Level => False,
11571 Nested_Constructs => False)
11572 then
11573 Block := Wrap_Statements_In_Block (Else_Statements (N));
11574 Set_Else_Statements (N, New_List (Block));
11575
11576 Analyze (Block);
11577 end if;
11578
11579 when N_Abortable_Part
11580 | N_Accept_Alternative
11581 | N_Case_Statement_Alternative
11582 | N_Delay_Alternative
11583 | N_Entry_Call_Alternative
11584 | N_Exception_Handler
11585 | N_Loop_Statement
11586 | N_Triggering_Alternative
11587 =>
11588 if not Is_Empty_List (Statements (N))
11589 and then not Are_Wrapped (Statements (N))
11590 and then Requires_Cleanup_Actions
11591 (L => Statements (N),
11592 Lib_Level => False,
11593 Nested_Constructs => False)
11594 then
11595 if Nkind (N) = N_Loop_Statement
11596 and then Present (Identifier (N))
11597 then
11598 Block :=
11599 Wrap_Statements_In_Block
11600 (L => Statements (N),
11601 Scop => Entity (Identifier (N)));
11602 else
11603 Block := Wrap_Statements_In_Block (Statements (N));
11604 end if;
11605
11606 Set_Statements (N, New_List (Block));
11607 Analyze (Block);
11608 end if;
11609
11610 -- Could be e.g. a loop that was transformed into a block or null
11611 -- statement. Do nothing for terminate alternatives.
11612
11613 when N_Block_Statement
11614 | N_Null_Statement
11615 | N_Terminate_Alternative
11616 =>
11617 null;
11618
11619 when others =>
11620 raise Program_Error;
11621 end case;
11622 end Process_Statements_For_Controlled_Objects;
11623
11624 ------------------
11625 -- Power_Of_Two --
11626 ------------------
11627
11628 function Power_Of_Two (N : Node_Id) return Nat is
11629 Typ : constant Entity_Id := Etype (N);
11630 pragma Assert (Is_Integer_Type (Typ));
11631
11632 Siz : constant Nat := UI_To_Int (Esize (Typ));
11633 Val : Uint;
11634
11635 begin
11636 if not Compile_Time_Known_Value (N) then
11637 return 0;
11638
11639 else
11640 Val := Expr_Value (N);
11641 for J in 1 .. Siz - 1 loop
11642 if Val = Uint_2 ** J then
11643 return J;
11644 end if;
11645 end loop;
11646
11647 return 0;
11648 end if;
11649 end Power_Of_Two;
11650
11651 ----------------------
11652 -- Remove_Init_Call --
11653 ----------------------
11654
11655 function Remove_Init_Call
11656 (Var : Entity_Id;
11657 Rep_Clause : Node_Id) return Node_Id
11658 is
11659 Par : constant Node_Id := Parent (Var);
11660 Typ : constant Entity_Id := Etype (Var);
11661
11662 Init_Proc : Entity_Id;
11663 -- Initialization procedure for Typ
11664
11665 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11666 -- Look for init call for Var starting at From and scanning the
11667 -- enclosing list until Rep_Clause or the end of the list is reached.
11668
11669 ----------------------------
11670 -- Find_Init_Call_In_List --
11671 ----------------------------
11672
11673 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11674 Init_Call : Node_Id;
11675
11676 begin
11677 Init_Call := From;
11678 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11679 if Nkind (Init_Call) = N_Procedure_Call_Statement
11680 and then Is_Entity_Name (Name (Init_Call))
11681 and then Entity (Name (Init_Call)) = Init_Proc
11682 then
11683 return Init_Call;
11684 end if;
11685
11686 Next (Init_Call);
11687 end loop;
11688
11689 return Empty;
11690 end Find_Init_Call_In_List;
11691
11692 Init_Call : Node_Id;
11693
11694 -- Start of processing for Remove_Init_Call
11695
11696 begin
11697 if Present (Initialization_Statements (Var)) then
11698 Init_Call := Initialization_Statements (Var);
11699 Set_Initialization_Statements (Var, Empty);
11700
11701 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11702
11703 -- No init proc for the type, so obviously no call to be found
11704
11705 return Empty;
11706
11707 else
11708 -- We might be able to handle other cases below by just properly
11709 -- setting Initialization_Statements at the point where the init proc
11710 -- call is generated???
11711
11712 Init_Proc := Base_Init_Proc (Typ);
11713
11714 -- First scan the list containing the declaration of Var
11715
11716 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11717
11718 -- If not found, also look on Var's freeze actions list, if any,
11719 -- since the init call may have been moved there (case of an address
11720 -- clause applying to Var).
11721
11722 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11723 Init_Call :=
11724 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11725 end if;
11726
11727 -- If the initialization call has actuals that use the secondary
11728 -- stack, the call may have been wrapped into a temporary block, in
11729 -- which case the block itself has to be removed.
11730
11731 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11732 declare
11733 Blk : constant Node_Id := Next (Par);
11734 begin
11735 if Present
11736 (Find_Init_Call_In_List
11737 (First (Statements (Handled_Statement_Sequence (Blk)))))
11738 then
11739 Init_Call := Blk;
11740 end if;
11741 end;
11742 end if;
11743 end if;
11744
11745 if Present (Init_Call) then
11746 -- If restrictions have forbidden Aborts, the initialization call
11747 -- for objects that require deep initialization has not been wrapped
11748 -- into the following block (see Exp_Ch3, Default_Initialize_Object)
11749 -- so if present remove it as well, and include the IP call in it,
11750 -- in the rare case the caller may need to simply displace the
11751 -- initialization, as is done for a later address specification.
11752
11753 if Nkind (Next (Init_Call)) = N_Block_Statement
11754 and then Is_Initialization_Block (Next (Init_Call))
11755 then
11756 declare
11757 IP_Call : constant Node_Id := Init_Call;
11758 begin
11759 Init_Call := Next (IP_Call);
11760 Remove (IP_Call);
11761 Prepend (IP_Call,
11762 Statements (Handled_Statement_Sequence (Init_Call)));
11763 end;
11764 end if;
11765
11766 Remove (Init_Call);
11767 end if;
11768
11769 return Init_Call;
11770 end Remove_Init_Call;
11771
11772 -------------------------
11773 -- Remove_Side_Effects --
11774 -------------------------
11775
11776 procedure Remove_Side_Effects
11777 (Exp : Node_Id;
11778 Name_Req : Boolean := False;
11779 Renaming_Req : Boolean := False;
11780 Variable_Ref : Boolean := False;
11781 Related_Id : Entity_Id := Empty;
11782 Is_Low_Bound : Boolean := False;
11783 Is_High_Bound : Boolean := False;
11784 Discr_Number : Int := 0;
11785 Check_Side_Effects : Boolean := True)
11786 is
11787 function Build_Temporary
11788 (Loc : Source_Ptr;
11789 Id : Character;
11790 Related_Nod : Node_Id := Empty) return Entity_Id;
11791 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11792 -- is present (xxx is taken from the Chars field of Related_Nod),
11793 -- otherwise it generates an internal temporary. The created temporary
11794 -- entity is marked as internal.
11795
11796 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean;
11797 -- Computes whether a side effect is possible in SPARK, which should
11798 -- be handled by removing it from the expression for GNATprove. Note
11799 -- that other side effects related to volatile variables are handled
11800 -- separately.
11801
11802 ---------------------
11803 -- Build_Temporary --
11804 ---------------------
11805
11806 function Build_Temporary
11807 (Loc : Source_Ptr;
11808 Id : Character;
11809 Related_Nod : Node_Id := Empty) return Entity_Id
11810 is
11811 Temp_Id : Entity_Id;
11812 Temp_Nam : Name_Id;
11813 Should_Set_Related_Expression : Boolean := False;
11814
11815 begin
11816 -- The context requires an external symbol : expression is
11817 -- the bound of an array, or a discriminant value. We create
11818 -- a unique string using the related entity and an appropriate
11819 -- suffix, rather than a numeric serial number (used for internal
11820 -- entities) that may vary depending on compilation options, in
11821 -- particular on the Assertions_Enabled mode. This avoids spurious
11822 -- link errors.
11823
11824 if Present (Related_Id) then
11825 if Is_Low_Bound then
11826 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11827
11828 elsif Is_High_Bound then
11829 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11830
11831 else
11832 pragma Assert (Discr_Number > 0);
11833
11834 -- We don't have any intelligible way of printing T_DISCR in
11835 -- CodePeer. Thus, set a related expression in this case.
11836
11837 Should_Set_Related_Expression := True;
11838
11839 -- Use fully qualified name to avoid ambiguities.
11840
11841 Temp_Nam :=
11842 New_External_Name
11843 (Get_Qualified_Name (Related_Id), "_DISCR", Discr_Number);
11844 end if;
11845
11846 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11847
11848 if Should_Set_Related_Expression then
11849 Set_Related_Expression (Temp_Id, Related_Nod);
11850 end if;
11851
11852 -- Otherwise generate an internal temporary
11853
11854 else
11855 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
11856 end if;
11857
11858 Set_Is_Internal (Temp_Id);
11859
11860 return Temp_Id;
11861 end Build_Temporary;
11862
11863 -----------------------------------
11864 -- Possible_Side_Effect_In_SPARK --
11865 -----------------------------------
11866
11867 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean is
11868 begin
11869 -- Side-effect removal in SPARK should only occur when not inside a
11870 -- generic and not doing a preanalysis, inside an object renaming or
11871 -- a type declaration or a for-loop iteration scheme.
11872
11873 return not Inside_A_Generic
11874 and then Full_Analysis
11875 and then Nkind (Enclosing_Declaration (Exp)) in
11876 N_Component_Declaration
11877 | N_Full_Type_Declaration
11878 | N_Iterator_Specification
11879 | N_Loop_Parameter_Specification
11880 | N_Object_Renaming_Declaration
11881 | N_Subtype_Declaration;
11882 end Possible_Side_Effect_In_SPARK;
11883
11884 -- Local variables
11885
11886 Loc : constant Source_Ptr := Sloc (Exp);
11887 Exp_Type : constant Entity_Id := Etype (Exp);
11888 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11889 Def_Id : Entity_Id;
11890 E : Node_Id;
11891 New_Exp : Node_Id;
11892 Ptr_Typ_Decl : Node_Id;
11893 Ref_Type : Entity_Id;
11894 Res : Node_Id;
11895
11896 -- Start of processing for Remove_Side_Effects
11897
11898 begin
11899 -- Handle cases in which there is nothing to do. In GNATprove mode,
11900 -- removal of side effects is useful for the light expansion of
11901 -- renamings.
11902
11903 if not Expander_Active
11904 and then not
11905 (GNATprove_Mode and then Possible_Side_Effect_In_SPARK (Exp))
11906 then
11907 return;
11908
11909 -- Cannot generate temporaries if the invocation to remove side effects
11910 -- was issued too early and the type of the expression is not resolved
11911 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11912 -- Remove_Side_Effects).
11913
11914 elsif No (Exp_Type)
11915 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11916 then
11917 return;
11918
11919 -- Nothing to do if prior expansion determined that a function call does
11920 -- not require side effect removal.
11921
11922 elsif Nkind (Exp) = N_Function_Call
11923 and then No_Side_Effect_Removal (Exp)
11924 then
11925 return;
11926
11927 -- No action needed for side-effect free expressions
11928
11929 elsif Check_Side_Effects
11930 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11931 then
11932 return;
11933
11934 -- Generating C code we cannot remove side effect of function returning
11935 -- class-wide types since there is no secondary stack (required to use
11936 -- 'reference).
11937
11938 elsif Modify_Tree_For_C
11939 and then Nkind (Exp) = N_Function_Call
11940 and then Is_Class_Wide_Type (Etype (Exp))
11941 then
11942 return;
11943 end if;
11944
11945 -- The remaining processing is done with all checks suppressed
11946
11947 -- Note: from now on, don't use return statements, instead do a goto
11948 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11949
11950 Scope_Suppress.Suppress := (others => True);
11951
11952 -- If this is a side-effect free attribute reference whose expressions
11953 -- are also side-effect free and whose prefix is not a name, remove the
11954 -- side effects of the prefix. A copy of the prefix is required in this
11955 -- case and it is better not to make an additional one for the attribute
11956 -- itself, because the return type of many of them is universal integer,
11957 -- which is a very large type for a temporary.
11958 -- The prefix of an attribute reference Reduce may be syntactically an
11959 -- aggregate, but will be expanded into a loop, so no need to remove
11960 -- side-effects.
11961
11962 if Nkind (Exp) = N_Attribute_Reference
11963 and then Side_Effect_Free_Attribute (Attribute_Name (Exp))
11964 and then Side_Effect_Free (Expressions (Exp), Name_Req, Variable_Ref)
11965 and then (Attribute_Name (Exp) /= Name_Reduce
11966 or else Nkind (Prefix (Exp)) /= N_Aggregate)
11967 and then not Is_Name_Reference (Prefix (Exp))
11968 then
11969 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11970 goto Leave;
11971
11972 -- If this is an elementary or a small not-by-reference record type, and
11973 -- we need to capture the value, just make a constant; this is cheap and
11974 -- objects of both kinds of types can be bit aligned, so it might not be
11975 -- possible to generate a reference to them. Likewise if this is not a
11976 -- name reference, except for a type conversion, because we would enter
11977 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11978 -- type has predicates (and type conversions need a specific treatment
11979 -- anyway, see below). Also do it if we have a volatile reference and
11980 -- Name_Req is not set (see comments for Side_Effect_Free).
11981
11982 elsif (Is_Elementary_Type (Exp_Type)
11983 or else (Is_Record_Type (Exp_Type)
11984 and then Known_Static_RM_Size (Exp_Type)
11985 and then RM_Size (Exp_Type) <= System_Max_Integer_Size
11986 and then not Has_Discriminants (Exp_Type)
11987 and then not Is_By_Reference_Type (Exp_Type)))
11988 and then (Variable_Ref
11989 or else (not Is_Name_Reference (Exp)
11990 and then Nkind (Exp) /= N_Type_Conversion)
11991 or else (not Name_Req
11992 and then Is_Volatile_Reference (Exp)))
11993 then
11994 Def_Id := Build_Temporary (Loc, 'R', Exp);
11995 Set_Etype (Def_Id, Exp_Type);
11996 Res := New_Occurrence_Of (Def_Id, Loc);
11997
11998 -- If the expression is a packed reference, it must be reanalyzed and
11999 -- expanded, depending on context. This is the case for actuals where
12000 -- a constraint check may capture the actual before expansion of the
12001 -- call is complete.
12002
12003 if Nkind (Exp) = N_Indexed_Component
12004 and then Is_Packed (Etype (Prefix (Exp)))
12005 then
12006 Set_Analyzed (Exp, False);
12007 Set_Analyzed (Prefix (Exp), False);
12008 end if;
12009
12010 -- Generate:
12011 -- Rnn : Exp_Type renames Expr;
12012
12013 -- In GNATprove mode, we prefer to use renamings for intermediate
12014 -- variables to definition of constants, due to the implicit move
12015 -- operation that such a constant definition causes as part of the
12016 -- support in GNATprove for ownership pointers. Hence, we generate
12017 -- a renaming for a reference to an object of a nonscalar type.
12018
12019 if Renaming_Req
12020 or else (GNATprove_Mode
12021 and then Is_Object_Reference (Exp)
12022 and then not Is_Scalar_Type (Exp_Type))
12023 then
12024 E :=
12025 Make_Object_Renaming_Declaration (Loc,
12026 Defining_Identifier => Def_Id,
12027 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12028 Name => Relocate_Node (Exp));
12029
12030 -- Generate:
12031 -- Rnn : constant Exp_Type := Expr;
12032
12033 else
12034 E :=
12035 Make_Object_Declaration (Loc,
12036 Defining_Identifier => Def_Id,
12037 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12038 Constant_Present => True,
12039 Expression => Relocate_Node (Exp));
12040
12041 Set_Assignment_OK (E);
12042 end if;
12043
12044 Insert_Action (Exp, E);
12045
12046 -- If the expression has the form v.all then we can just capture the
12047 -- pointer, and then do an explicit dereference on the result, but
12048 -- this is not right if this is a volatile reference.
12049
12050 elsif Nkind (Exp) = N_Explicit_Dereference
12051 and then not Is_Volatile_Reference (Exp)
12052 then
12053 Def_Id := Build_Temporary (Loc, 'R', Exp);
12054 Res :=
12055 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
12056
12057 Insert_Action (Exp,
12058 Make_Object_Declaration (Loc,
12059 Defining_Identifier => Def_Id,
12060 Object_Definition =>
12061 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
12062 Constant_Present => True,
12063 Expression => Relocate_Node (Prefix (Exp))));
12064
12065 -- Similar processing for an unchecked conversion of an expression of
12066 -- the form v.all, where we want the same kind of treatment.
12067
12068 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12069 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
12070 then
12071 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12072 goto Leave;
12073
12074 -- If this is a type conversion, leave the type conversion and remove
12075 -- side effects in the expression, unless it is of universal integer,
12076 -- which is a very large type for a temporary. This is important in
12077 -- several circumstances: for change of representations and also when
12078 -- this is a view conversion to a smaller object, where gigi can end
12079 -- up creating its own temporary of the wrong size.
12080
12081 elsif Nkind (Exp) = N_Type_Conversion
12082 and then Etype (Expression (Exp)) /= Universal_Integer
12083 then
12084 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12085
12086 -- Generating C code the type conversion of an access to constrained
12087 -- array type into an access to unconstrained array type involves
12088 -- initializing a fat pointer and the expression must be free of
12089 -- side effects to safely compute its bounds.
12090
12091 if Modify_Tree_For_C
12092 and then Is_Access_Type (Etype (Exp))
12093 and then Is_Array_Type (Designated_Type (Etype (Exp)))
12094 and then not Is_Constrained (Designated_Type (Etype (Exp)))
12095 then
12096 Def_Id := Build_Temporary (Loc, 'R', Exp);
12097 Set_Etype (Def_Id, Exp_Type);
12098 Res := New_Occurrence_Of (Def_Id, Loc);
12099
12100 Insert_Action (Exp,
12101 Make_Object_Declaration (Loc,
12102 Defining_Identifier => Def_Id,
12103 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12104 Constant_Present => True,
12105 Expression => Relocate_Node (Exp)));
12106 else
12107 goto Leave;
12108 end if;
12109
12110 -- If this is an unchecked conversion that Gigi can't handle, make
12111 -- a copy or a use a renaming to capture the value.
12112
12113 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12114 and then not Safe_Unchecked_Type_Conversion (Exp)
12115 then
12116 if CW_Or_Needs_Finalization (Exp_Type) then
12117
12118 -- Use a renaming to capture the expression, rather than create
12119 -- a controlled temporary.
12120
12121 Def_Id := Build_Temporary (Loc, 'R', Exp);
12122 Res := New_Occurrence_Of (Def_Id, Loc);
12123
12124 Insert_Action (Exp,
12125 Make_Object_Renaming_Declaration (Loc,
12126 Defining_Identifier => Def_Id,
12127 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12128 Name => Relocate_Node (Exp)));
12129
12130 else
12131 Def_Id := Build_Temporary (Loc, 'R', Exp);
12132 Set_Etype (Def_Id, Exp_Type);
12133 Res := New_Occurrence_Of (Def_Id, Loc);
12134
12135 E :=
12136 Make_Object_Declaration (Loc,
12137 Defining_Identifier => Def_Id,
12138 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12139 Constant_Present => not Is_Variable (Exp),
12140 Expression => Relocate_Node (Exp));
12141
12142 Set_Assignment_OK (E);
12143 Insert_Action (Exp, E);
12144 end if;
12145
12146 -- If this is a packed array component or a selected component with a
12147 -- nonstandard representation, we cannot generate a reference because
12148 -- the component may be unaligned, so we must use a renaming and this
12149 -- renaming is handled by the front end, as the back end may balk at
12150 -- the nonstandard representation (see Evaluation_Required in Exp_Ch8).
12151
12152 elsif Nkind (Exp) in N_Indexed_Component | N_Selected_Component
12153 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
12154 then
12155 Def_Id := Build_Temporary (Loc, 'R', Exp);
12156 Res := New_Occurrence_Of (Def_Id, Loc);
12157
12158 Insert_Action (Exp,
12159 Make_Object_Renaming_Declaration (Loc,
12160 Defining_Identifier => Def_Id,
12161 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12162 Name => Relocate_Node (Exp)));
12163
12164 -- For an expression that denotes a name, we can use a renaming scheme.
12165 -- This is needed for correctness in the case of a volatile object of
12166 -- a nonvolatile type because the Make_Reference call of the "default"
12167 -- approach would generate an illegal access value (an access value
12168 -- cannot designate such an object - see Analyze_Reference).
12169
12170 elsif Is_Name_Reference (Exp)
12171
12172 -- We skip using this scheme if we have an object of a volatile
12173 -- type and we do not have Name_Req set true (see comments for
12174 -- Side_Effect_Free).
12175
12176 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
12177 then
12178 Def_Id := Build_Temporary (Loc, 'R', Exp);
12179 Res := New_Occurrence_Of (Def_Id, Loc);
12180
12181 Insert_Action (Exp,
12182 Make_Object_Renaming_Declaration (Loc,
12183 Defining_Identifier => Def_Id,
12184 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12185 Name => Relocate_Node (Exp)));
12186
12187 -- Avoid generating a variable-sized temporary, by generating the
12188 -- reference just for the function call. The transformation could be
12189 -- refined to apply only when the array component is constrained by a
12190 -- discriminant???
12191
12192 elsif Nkind (Exp) = N_Selected_Component
12193 and then Nkind (Prefix (Exp)) = N_Function_Call
12194 and then Is_Array_Type (Exp_Type)
12195 then
12196 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12197 goto Leave;
12198
12199 -- Otherwise we generate a reference to the expression
12200
12201 else
12202 -- When generating C code we cannot consider side effect free object
12203 -- declarations that have discriminants and are initialized by means
12204 -- of a function call since on this target there is no secondary
12205 -- stack to store the return value and the expander may generate an
12206 -- extra call to the function to compute the discriminant value. In
12207 -- addition, for targets that have secondary stack, the expansion of
12208 -- functions with side effects involves the generation of an access
12209 -- type to capture the return value stored in the secondary stack;
12210 -- by contrast when generating C code such expansion generates an
12211 -- internal object declaration (no access type involved) which must
12212 -- be identified here to avoid entering into a never-ending loop
12213 -- generating internal object declarations.
12214
12215 if Modify_Tree_For_C
12216 and then Nkind (Parent (Exp)) = N_Object_Declaration
12217 and then
12218 (Nkind (Exp) /= N_Function_Call
12219 or else not Has_Discriminants (Exp_Type)
12220 or else Is_Internal_Name
12221 (Chars (Defining_Identifier (Parent (Exp)))))
12222 then
12223 goto Leave;
12224 end if;
12225
12226 -- Special processing for function calls that return a limited type.
12227 -- We need to build a declaration that will enable build-in-place
12228 -- expansion of the call. This is not done if the context is already
12229 -- an object declaration, to prevent infinite recursion.
12230
12231 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
12232 -- to accommodate functions returning limited objects by reference.
12233
12234 if Ada_Version >= Ada_2005
12235 and then Nkind (Exp) = N_Function_Call
12236 and then Is_Limited_View (Etype (Exp))
12237 and then Nkind (Parent (Exp)) /= N_Object_Declaration
12238 then
12239 declare
12240 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
12241 Decl : Node_Id;
12242
12243 begin
12244 Decl :=
12245 Make_Object_Declaration (Loc,
12246 Defining_Identifier => Obj,
12247 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12248 Expression => Relocate_Node (Exp));
12249
12250 Insert_Action (Exp, Decl);
12251 Set_Etype (Obj, Exp_Type);
12252 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
12253 goto Leave;
12254 end;
12255 end if;
12256
12257 Def_Id := Build_Temporary (Loc, 'R', Exp);
12258
12259 -- The regular expansion of functions with side effects involves the
12260 -- generation of an access type to capture the return value found on
12261 -- the secondary stack. Since SPARK (and why) cannot process access
12262 -- types, use a different approach which ignores the secondary stack
12263 -- and "copies" the returned object.
12264 -- When generating C code, no need for a 'reference since the
12265 -- secondary stack is not supported.
12266
12267 if GNATprove_Mode or Modify_Tree_For_C then
12268 Res := New_Occurrence_Of (Def_Id, Loc);
12269 Ref_Type := Exp_Type;
12270
12271 -- Regular expansion utilizing an access type and 'reference
12272
12273 else
12274 Res :=
12275 Make_Explicit_Dereference (Loc,
12276 Prefix => New_Occurrence_Of (Def_Id, Loc));
12277
12278 -- Generate:
12279 -- type Ann is access all <Exp_Type>;
12280
12281 Ref_Type := Make_Temporary (Loc, 'A');
12282
12283 Ptr_Typ_Decl :=
12284 Make_Full_Type_Declaration (Loc,
12285 Defining_Identifier => Ref_Type,
12286 Type_Definition =>
12287 Make_Access_To_Object_Definition (Loc,
12288 All_Present => True,
12289 Subtype_Indication =>
12290 New_Occurrence_Of (Exp_Type, Loc)));
12291
12292 Insert_Action (Exp, Ptr_Typ_Decl);
12293 end if;
12294
12295 E := Exp;
12296 if Nkind (E) = N_Explicit_Dereference then
12297 New_Exp := Relocate_Node (Prefix (E));
12298
12299 else
12300 E := Relocate_Node (E);
12301
12302 -- Do not generate a 'reference in SPARK mode or C generation
12303 -- since the access type is not created in the first place.
12304
12305 if GNATprove_Mode or Modify_Tree_For_C then
12306 New_Exp := E;
12307
12308 -- Otherwise generate reference, marking the value as non-null
12309 -- since we know it cannot be null and we don't want a check.
12310
12311 else
12312 New_Exp := Make_Reference (Loc, E);
12313 Set_Is_Known_Non_Null (Def_Id);
12314 end if;
12315 end if;
12316
12317 if Is_Delayed_Aggregate (E) then
12318
12319 -- The expansion of nested aggregates is delayed until the
12320 -- enclosing aggregate is expanded. As aggregates are often
12321 -- qualified, the predicate applies to qualified expressions as
12322 -- well, indicating that the enclosing aggregate has not been
12323 -- expanded yet. At this point the aggregate is part of a
12324 -- stand-alone declaration, and must be fully expanded.
12325
12326 if Nkind (E) = N_Qualified_Expression then
12327 Set_Expansion_Delayed (Expression (E), False);
12328 Set_Analyzed (Expression (E), False);
12329 else
12330 Set_Expansion_Delayed (E, False);
12331 end if;
12332
12333 Set_Analyzed (E, False);
12334 end if;
12335
12336 -- Generating C code of object declarations that have discriminants
12337 -- and are initialized by means of a function call we propagate the
12338 -- discriminants of the parent type to the internally built object.
12339 -- This is needed to avoid generating an extra call to the called
12340 -- function.
12341
12342 -- For example, if we generate here the following declaration, it
12343 -- will be expanded later adding an extra call to evaluate the value
12344 -- of the discriminant (needed to compute the size of the object).
12345 --
12346 -- type Rec (D : Integer) is ...
12347 -- Obj : constant Rec := SomeFunc;
12348
12349 if Modify_Tree_For_C
12350 and then Nkind (Parent (Exp)) = N_Object_Declaration
12351 and then Has_Discriminants (Exp_Type)
12352 and then Nkind (Exp) = N_Function_Call
12353 then
12354 Insert_Action (Exp,
12355 Make_Object_Declaration (Loc,
12356 Defining_Identifier => Def_Id,
12357 Object_Definition => New_Copy_Tree
12358 (Object_Definition (Parent (Exp))),
12359 Constant_Present => True,
12360 Expression => New_Exp));
12361 else
12362 Insert_Action (Exp,
12363 Make_Object_Declaration (Loc,
12364 Defining_Identifier => Def_Id,
12365 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
12366 Constant_Present => True,
12367 Expression => New_Exp));
12368 end if;
12369 end if;
12370
12371 -- Preserve the Assignment_OK flag in all copies, since at least one
12372 -- copy may be used in a context where this flag must be set (otherwise
12373 -- why would the flag be set in the first place).
12374
12375 Set_Assignment_OK (Res, Assignment_OK (Exp));
12376
12377 -- Preserve the Do_Range_Check flag in all copies
12378
12379 Set_Do_Range_Check (Res, Do_Range_Check (Exp));
12380
12381 -- Finally rewrite the original expression and we are done
12382
12383 Rewrite (Exp, Res);
12384 Analyze_And_Resolve (Exp, Exp_Type);
12385
12386 <<Leave>>
12387 Scope_Suppress := Svg_Suppress;
12388 end Remove_Side_Effects;
12389
12390 ------------------------
12391 -- Replace_References --
12392 ------------------------
12393
12394 procedure Replace_References
12395 (Expr : Node_Id;
12396 Par_Typ : Entity_Id;
12397 Deriv_Typ : Entity_Id;
12398 Par_Obj : Entity_Id := Empty;
12399 Deriv_Obj : Entity_Id := Empty)
12400 is
12401 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
12402 -- Determine whether node Ref denotes some component of Deriv_Obj
12403
12404 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
12405 -- Substitute a reference to an entity with the corresponding value
12406 -- stored in table Type_Map.
12407
12408 function Type_Of_Formal
12409 (Call : Node_Id;
12410 Actual : Node_Id) return Entity_Id;
12411 -- Find the type of the formal parameter which corresponds to actual
12412 -- parameter Actual in subprogram call Call.
12413
12414 ----------------------
12415 -- Is_Deriv_Obj_Ref --
12416 ----------------------
12417
12418 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
12419 Par : constant Node_Id := Parent (Ref);
12420
12421 begin
12422 -- Detect the folowing selected component form:
12423
12424 -- Deriv_Obj.(something)
12425
12426 return
12427 Nkind (Par) = N_Selected_Component
12428 and then Is_Entity_Name (Prefix (Par))
12429 and then Entity (Prefix (Par)) = Deriv_Obj;
12430 end Is_Deriv_Obj_Ref;
12431
12432 -----------------
12433 -- Replace_Ref --
12434 -----------------
12435
12436 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
12437 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
12438 -- Reset the Controlling_Argument of all function calls that
12439 -- encapsulate node From_Arg.
12440
12441 ----------------------------------
12442 -- Remove_Controlling_Arguments --
12443 ----------------------------------
12444
12445 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
12446 Par : Node_Id;
12447
12448 begin
12449 Par := From_Arg;
12450 while Present (Par) loop
12451 if Nkind (Par) = N_Function_Call
12452 and then Present (Controlling_Argument (Par))
12453 then
12454 Set_Controlling_Argument (Par, Empty);
12455
12456 -- Prevent the search from going too far
12457
12458 elsif Is_Body_Or_Package_Declaration (Par) then
12459 exit;
12460 end if;
12461
12462 Par := Parent (Par);
12463 end loop;
12464 end Remove_Controlling_Arguments;
12465
12466 -- Local variables
12467
12468 Context : constant Node_Id :=
12469 (if No (Ref) then Empty else Parent (Ref));
12470
12471 Loc : constant Source_Ptr := Sloc (Ref);
12472 Ref_Id : Entity_Id;
12473 Result : Traverse_Result;
12474
12475 New_Ref : Node_Id;
12476 -- The new reference which is intended to substitute the old one
12477
12478 Old_Ref : Node_Id;
12479 -- The reference designated for replacement. In certain cases this
12480 -- may be a node other than Ref.
12481
12482 Val : Node_Or_Entity_Id;
12483 -- The corresponding value of Ref from the type map
12484
12485 -- Start of processing for Replace_Ref
12486
12487 begin
12488 -- Assume that the input reference is to be replaced and that the
12489 -- traversal should examine the children of the reference.
12490
12491 Old_Ref := Ref;
12492 Result := OK;
12493
12494 -- The input denotes a meaningful reference
12495
12496 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
12497 Ref_Id := Entity (Ref);
12498 Val := Type_Map.Get (Ref_Id);
12499
12500 -- The reference has a corresponding value in the type map, a
12501 -- substitution is possible.
12502
12503 if Present (Val) then
12504
12505 -- The reference denotes a discriminant
12506
12507 if Ekind (Ref_Id) = E_Discriminant then
12508 if Nkind (Val) in N_Entity then
12509
12510 -- The value denotes another discriminant. Replace as
12511 -- follows:
12512
12513 -- _object.Discr -> _object.Val
12514
12515 if Ekind (Val) = E_Discriminant then
12516 New_Ref := New_Occurrence_Of (Val, Loc);
12517
12518 -- Otherwise the value denotes the entity of a name which
12519 -- constraints the discriminant. Replace as follows:
12520
12521 -- _object.Discr -> Val
12522
12523 else
12524 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12525
12526 New_Ref := New_Occurrence_Of (Val, Loc);
12527 Old_Ref := Parent (Old_Ref);
12528 end if;
12529
12530 -- Otherwise the value denotes an arbitrary expression which
12531 -- constraints the discriminant. Replace as follows:
12532
12533 -- _object.Discr -> Val
12534
12535 else
12536 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12537
12538 New_Ref := New_Copy_Tree (Val);
12539 Old_Ref := Parent (Old_Ref);
12540 end if;
12541
12542 -- Otherwise the reference denotes a primitive. Replace as
12543 -- follows:
12544
12545 -- Primitive -> Val
12546
12547 else
12548 pragma Assert (Nkind (Val) in N_Entity);
12549 New_Ref := New_Occurrence_Of (Val, Loc);
12550 end if;
12551
12552 -- The reference mentions the _object parameter of the parent
12553 -- type's DIC or type invariant procedure. Replace as follows:
12554
12555 -- _object -> _object
12556
12557 elsif Present (Par_Obj)
12558 and then Present (Deriv_Obj)
12559 and then Ref_Id = Par_Obj
12560 then
12561 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
12562
12563 -- The type of the _object parameter is class-wide when the
12564 -- expression comes from an assertion pragma that applies to
12565 -- an abstract parent type or an interface. The class-wide type
12566 -- facilitates the preanalysis of the expression by treating
12567 -- calls to abstract primitives that mention the current
12568 -- instance of the type as dispatching. Once the calls are
12569 -- remapped to invoke overriding or inherited primitives, the
12570 -- calls no longer need to be dispatching. Examine all function
12571 -- calls that encapsulate the _object parameter and reset their
12572 -- Controlling_Argument attribute.
12573
12574 if Is_Class_Wide_Type (Etype (Par_Obj))
12575 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
12576 then
12577 Remove_Controlling_Arguments (Old_Ref);
12578 end if;
12579
12580 -- The reference to _object acts as an actual parameter in a
12581 -- subprogram call which may be invoking a primitive of the
12582 -- parent type:
12583
12584 -- Primitive (... _object ...);
12585
12586 -- The parent type primitive may not be overridden nor
12587 -- inherited when it is declared after the derived type
12588 -- definition:
12589
12590 -- type Parent is tagged private;
12591 -- type Child is new Parent with private;
12592 -- procedure Primitive (Obj : Parent);
12593
12594 -- In this scenario the _object parameter is converted to the
12595 -- parent type. Due to complications with partial/full views
12596 -- and view swaps, the parent type is taken from the formal
12597 -- parameter of the subprogram being called.
12598
12599 if Nkind (Context) in N_Subprogram_Call
12600 and then No (Type_Map.Get (Entity (Name (Context))))
12601 then
12602 declare
12603 -- We need to use the Original_Node of the callee, in
12604 -- case it was already modified. Note that we are using
12605 -- Traverse_Proc to walk the tree, and it is defined to
12606 -- walk subtrees in an arbitrary order.
12607
12608 Callee : constant Entity_Id :=
12609 Entity (Original_Node (Name (Context)));
12610 begin
12611 if No (Type_Map.Get (Callee)) then
12612 New_Ref :=
12613 Convert_To
12614 (Type_Of_Formal (Context, Old_Ref), New_Ref);
12615
12616 -- Do not process the generated type conversion
12617 -- because both the parent type and the derived type
12618 -- are in the Type_Map table. This will clobber the
12619 -- type conversion by resetting its subtype mark.
12620
12621 Result := Skip;
12622 end if;
12623 end;
12624 end if;
12625
12626 -- Otherwise there is nothing to replace
12627
12628 else
12629 New_Ref := Empty;
12630 end if;
12631
12632 if Present (New_Ref) then
12633 Rewrite (Old_Ref, New_Ref);
12634
12635 -- Update the return type when the context of the reference
12636 -- acts as the name of a function call. Note that the update
12637 -- should not be performed when the reference appears as an
12638 -- actual in the call.
12639
12640 if Nkind (Context) = N_Function_Call
12641 and then Name (Context) = Old_Ref
12642 then
12643 Set_Etype (Context, Etype (Val));
12644 end if;
12645 end if;
12646 end if;
12647
12648 -- Reanalyze the reference due to potential replacements
12649
12650 if Nkind (Old_Ref) in N_Has_Etype then
12651 Set_Analyzed (Old_Ref, False);
12652 end if;
12653
12654 return Result;
12655 end Replace_Ref;
12656
12657 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
12658
12659 --------------------
12660 -- Type_Of_Formal --
12661 --------------------
12662
12663 function Type_Of_Formal
12664 (Call : Node_Id;
12665 Actual : Node_Id) return Entity_Id
12666 is
12667 A : Node_Id;
12668 F : Entity_Id;
12669
12670 begin
12671 -- Examine the list of actual and formal parameters in parallel
12672
12673 A := First (Parameter_Associations (Call));
12674 F := First_Formal (Entity (Name (Call)));
12675 while Present (A) and then Present (F) loop
12676 if A = Actual then
12677 return Etype (F);
12678 end if;
12679
12680 Next (A);
12681 Next_Formal (F);
12682 end loop;
12683
12684 -- The actual parameter must always have a corresponding formal
12685
12686 pragma Assert (False);
12687
12688 return Empty;
12689 end Type_Of_Formal;
12690
12691 -- Start of processing for Replace_References
12692
12693 begin
12694 -- Map the attributes of the parent type to the proper corresponding
12695 -- attributes of the derived type.
12696
12697 Map_Types
12698 (Parent_Type => Par_Typ,
12699 Derived_Type => Deriv_Typ);
12700
12701 -- Inspect the input expression and perform substitutions where
12702 -- necessary.
12703
12704 Replace_Refs (Expr);
12705 end Replace_References;
12706
12707 -----------------------------
12708 -- Replace_Type_References --
12709 -----------------------------
12710
12711 procedure Replace_Type_References
12712 (Expr : Node_Id;
12713 Typ : Entity_Id;
12714 Obj_Id : Entity_Id)
12715 is
12716 procedure Replace_Type_Ref (N : Node_Id);
12717 -- Substitute a single reference of the current instance of type Typ
12718 -- with a reference to Obj_Id.
12719
12720 ----------------------
12721 -- Replace_Type_Ref --
12722 ----------------------
12723
12724 procedure Replace_Type_Ref (N : Node_Id) is
12725 begin
12726 -- Decorate the reference to Typ even though it may be rewritten
12727 -- further down. This is done so that routines which examine
12728 -- properties of the Original_Node have some semantic information.
12729
12730 if Nkind (N) = N_Identifier then
12731 Set_Entity (N, Typ);
12732 Set_Etype (N, Typ);
12733
12734 elsif Nkind (N) = N_Selected_Component then
12735 Analyze (Prefix (N));
12736 Set_Entity (Selector_Name (N), Typ);
12737 Set_Etype (Selector_Name (N), Typ);
12738 end if;
12739
12740 -- Perform the following substitution:
12741
12742 -- Typ --> _object
12743
12744 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
12745 Set_Comes_From_Source (N, True);
12746 end Replace_Type_Ref;
12747
12748 procedure Replace_Type_Refs is
12749 new Replace_Type_References_Generic (Replace_Type_Ref);
12750
12751 -- Start of processing for Replace_Type_References
12752
12753 begin
12754 Replace_Type_Refs (Expr, Typ);
12755 end Replace_Type_References;
12756
12757 ---------------------------
12758 -- Represented_As_Scalar --
12759 ---------------------------
12760
12761 function Represented_As_Scalar (T : Entity_Id) return Boolean is
12762 UT : constant Entity_Id := Underlying_Type (T);
12763 begin
12764 return Is_Scalar_Type (UT)
12765 or else (Is_Bit_Packed_Array (UT)
12766 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
12767 end Represented_As_Scalar;
12768
12769 ------------------------------
12770 -- Requires_Cleanup_Actions --
12771 ------------------------------
12772
12773 function Requires_Cleanup_Actions
12774 (N : Node_Id;
12775 Lib_Level : Boolean) return Boolean
12776 is
12777 At_Lib_Level : constant Boolean :=
12778 Lib_Level
12779 and then Nkind (N) in N_Package_Body | N_Package_Specification;
12780 -- N is at the library level if the top-most context is a package and
12781 -- the path taken to reach N does not include nonpackage constructs.
12782
12783 begin
12784 case Nkind (N) is
12785 when N_Accept_Statement
12786 | N_Block_Statement
12787 | N_Entry_Body
12788 | N_Package_Body
12789 | N_Subprogram_Body
12790 | N_Task_Body
12791 =>
12792 return
12793 Requires_Cleanup_Actions
12794 (L => Declarations (N),
12795 Lib_Level => At_Lib_Level,
12796 Nested_Constructs => True)
12797 or else
12798 (Present (Handled_Statement_Sequence (N))
12799 and then
12800 Requires_Cleanup_Actions
12801 (L =>
12802 Statements (Handled_Statement_Sequence (N)),
12803 Lib_Level => At_Lib_Level,
12804 Nested_Constructs => True));
12805
12806 -- Extended return statements are the same as the above, except that
12807 -- there is no Declarations field. We do not want to clean up the
12808 -- Return_Object_Declarations.
12809
12810 when N_Extended_Return_Statement =>
12811 return
12812 Present (Handled_Statement_Sequence (N))
12813 and then Requires_Cleanup_Actions
12814 (L =>
12815 Statements (Handled_Statement_Sequence (N)),
12816 Lib_Level => At_Lib_Level,
12817 Nested_Constructs => True);
12818
12819 when N_Package_Specification =>
12820 return
12821 Requires_Cleanup_Actions
12822 (L => Visible_Declarations (N),
12823 Lib_Level => At_Lib_Level,
12824 Nested_Constructs => True)
12825 or else
12826 Requires_Cleanup_Actions
12827 (L => Private_Declarations (N),
12828 Lib_Level => At_Lib_Level,
12829 Nested_Constructs => True);
12830
12831 when others =>
12832 raise Program_Error;
12833 end case;
12834 end Requires_Cleanup_Actions;
12835
12836 ------------------------------
12837 -- Requires_Cleanup_Actions --
12838 ------------------------------
12839
12840 function Requires_Cleanup_Actions
12841 (L : List_Id;
12842 Lib_Level : Boolean;
12843 Nested_Constructs : Boolean) return Boolean
12844 is
12845 Decl : Node_Id;
12846 Expr : Node_Id;
12847 Obj_Id : Entity_Id;
12848 Obj_Typ : Entity_Id;
12849 Pack_Id : Entity_Id;
12850 Typ : Entity_Id;
12851
12852 begin
12853 Decl := First (L);
12854 while Present (Decl) loop
12855
12856 -- Library-level tagged types
12857
12858 if Nkind (Decl) = N_Full_Type_Declaration then
12859 Typ := Defining_Identifier (Decl);
12860
12861 -- Ignored Ghost types do not need any cleanup actions because
12862 -- they will not appear in the final tree.
12863
12864 if Is_Ignored_Ghost_Entity (Typ) then
12865 null;
12866
12867 elsif Is_Tagged_Type (Typ)
12868 and then Is_Library_Level_Entity (Typ)
12869 and then Convention (Typ) = Convention_Ada
12870 and then Present (Access_Disp_Table (Typ))
12871 and then not Is_Abstract_Type (Typ)
12872 and then not No_Run_Time_Mode
12873 and then not Restriction_Active (No_Tagged_Type_Registration)
12874 and then RTE_Available (RE_Unregister_Tag)
12875 then
12876 return True;
12877 end if;
12878
12879 -- Regular object declarations
12880
12881 elsif Nkind (Decl) = N_Object_Declaration then
12882 Obj_Id := Defining_Identifier (Decl);
12883 Obj_Typ := Base_Type (Etype (Obj_Id));
12884 Expr := Expression (Decl);
12885
12886 -- Bypass any form of processing for objects which have their
12887 -- finalization disabled. This applies only to objects at the
12888 -- library level.
12889
12890 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12891 null;
12892
12893 -- Finalization of transient objects are treated separately in
12894 -- order to handle sensitive cases. These include:
12895
12896 -- * Aggregate expansion
12897 -- * If, case, and expression with actions expansion
12898 -- * Transient scopes
12899
12900 -- If one of those contexts has marked the transient object as
12901 -- ignored, do not generate finalization actions for it.
12902
12903 elsif Is_Finalized_Transient (Obj_Id)
12904 or else Is_Ignored_Transient (Obj_Id)
12905 then
12906 null;
12907
12908 -- Ignored Ghost objects do not need any cleanup actions because
12909 -- they will not appear in the final tree.
12910
12911 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12912 null;
12913
12914 -- The object is of the form:
12915 -- Obj : [constant] Typ [:= Expr];
12916 --
12917 -- Do not process tag-to-class-wide conversions because they do
12918 -- not yield an object. Do not process the incomplete view of a
12919 -- deferred constant. Note that an object initialized by means
12920 -- of a build-in-place function call may appear as a deferred
12921 -- constant after expansion activities. These kinds of objects
12922 -- must be finalized.
12923
12924 elsif not Is_Imported (Obj_Id)
12925 and then Needs_Finalization (Obj_Typ)
12926 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
12927 and then not (Ekind (Obj_Id) = E_Constant
12928 and then not Has_Completion (Obj_Id)
12929 and then No (BIP_Initialization_Call (Obj_Id)))
12930 then
12931 return True;
12932
12933 -- The object is of the form:
12934 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12935 --
12936 -- Obj : Access_Typ :=
12937 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12938
12939 elsif Is_Access_Type (Obj_Typ)
12940 and then Needs_Finalization
12941 (Available_View (Designated_Type (Obj_Typ)))
12942 and then Present (Expr)
12943 and then
12944 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12945 or else
12946 (Is_Non_BIP_Func_Call (Expr)
12947 and then not Is_Related_To_Func_Return (Obj_Id)))
12948 then
12949 return True;
12950
12951 -- Processing for "hook" objects generated for transient objects
12952 -- declared inside an Expression_With_Actions.
12953
12954 elsif Is_Access_Type (Obj_Typ)
12955 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12956 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12957 N_Object_Declaration
12958 then
12959 return True;
12960
12961 -- Processing for intermediate results of if expressions where
12962 -- one of the alternatives uses a controlled function call.
12963
12964 elsif Is_Access_Type (Obj_Typ)
12965 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12966 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12967 N_Defining_Identifier
12968 and then Present (Expr)
12969 and then Nkind (Expr) = N_Null
12970 then
12971 return True;
12972
12973 -- Simple protected objects which use type System.Tasking.
12974 -- Protected_Objects.Protection to manage their locks should be
12975 -- treated as controlled since they require manual cleanup.
12976
12977 elsif Ekind (Obj_Id) = E_Variable
12978 and then (Is_Simple_Protected_Type (Obj_Typ)
12979 or else Has_Simple_Protected_Object (Obj_Typ))
12980 then
12981 return True;
12982 end if;
12983
12984 -- Specific cases of object renamings
12985
12986 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12987 Obj_Id := Defining_Identifier (Decl);
12988 Obj_Typ := Base_Type (Etype (Obj_Id));
12989
12990 -- Bypass any form of processing for objects which have their
12991 -- finalization disabled. This applies only to objects at the
12992 -- library level.
12993
12994 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12995 null;
12996
12997 -- Ignored Ghost object renamings do not need any cleanup actions
12998 -- because they will not appear in the final tree.
12999
13000 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
13001 null;
13002
13003 -- Return object of a build-in-place function. This case is
13004 -- recognized and marked by the expansion of an extended return
13005 -- statement (see Expand_N_Extended_Return_Statement).
13006
13007 elsif Needs_Finalization (Obj_Typ)
13008 and then Is_Return_Object (Obj_Id)
13009 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
13010 then
13011 return True;
13012
13013 -- Detect a case where a source object has been initialized by
13014 -- a controlled function call or another object which was later
13015 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
13016
13017 -- Obj1 : CW_Type := Src_Obj;
13018 -- Obj2 : CW_Type := Function_Call (...);
13019
13020 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
13021 -- Tmp : ... := Function_Call (...)'reference;
13022 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
13023
13024 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
13025 return True;
13026 end if;
13027
13028 -- Inspect the freeze node of an access-to-controlled type and look
13029 -- for a delayed finalization master. This case arises when the
13030 -- freeze actions are inserted at a later time than the expansion of
13031 -- the context. Since Build_Finalizer is never called on a single
13032 -- construct twice, the master will be ultimately left out and never
13033 -- finalized. This is also needed for freeze actions of designated
13034 -- types themselves, since in some cases the finalization master is
13035 -- associated with a designated type's freeze node rather than that
13036 -- of the access type (see handling for freeze actions in
13037 -- Build_Finalization_Master).
13038
13039 elsif Nkind (Decl) = N_Freeze_Entity
13040 and then Present (Actions (Decl))
13041 then
13042 Typ := Entity (Decl);
13043
13044 -- Freeze nodes for ignored Ghost types do not need cleanup
13045 -- actions because they will never appear in the final tree.
13046
13047 if Is_Ignored_Ghost_Entity (Typ) then
13048 null;
13049
13050 elsif ((Is_Access_Object_Type (Typ)
13051 and then Needs_Finalization
13052 (Available_View (Designated_Type (Typ))))
13053 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
13054 and then Requires_Cleanup_Actions
13055 (Actions (Decl), Lib_Level, Nested_Constructs)
13056 then
13057 return True;
13058 end if;
13059
13060 -- Nested package declarations
13061
13062 elsif Nested_Constructs
13063 and then Nkind (Decl) = N_Package_Declaration
13064 then
13065 Pack_Id := Defining_Entity (Decl);
13066
13067 -- Do not inspect an ignored Ghost package because all code found
13068 -- within will not appear in the final tree.
13069
13070 if Is_Ignored_Ghost_Entity (Pack_Id) then
13071 null;
13072
13073 elsif Ekind (Pack_Id) /= E_Generic_Package
13074 and then Requires_Cleanup_Actions
13075 (Specification (Decl), Lib_Level)
13076 then
13077 return True;
13078 end if;
13079
13080 -- Nested package bodies
13081
13082 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
13083
13084 -- Do not inspect an ignored Ghost package body because all code
13085 -- found within will not appear in the final tree.
13086
13087 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
13088 null;
13089
13090 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13091 and then Requires_Cleanup_Actions (Decl, Lib_Level)
13092 then
13093 return True;
13094 end if;
13095
13096 elsif Nkind (Decl) = N_Block_Statement
13097 and then
13098
13099 -- Handle a rare case caused by a controlled transient object
13100 -- created as part of a record init proc. The variable is wrapped
13101 -- in a block, but the block is not associated with a transient
13102 -- scope.
13103
13104 (Inside_Init_Proc
13105
13106 -- Handle the case where the original context has been wrapped in
13107 -- a block to avoid interference between exception handlers and
13108 -- At_End handlers. Treat the block as transparent and process its
13109 -- contents.
13110
13111 or else Is_Finalization_Wrapper (Decl))
13112 then
13113 if Requires_Cleanup_Actions (Decl, Lib_Level) then
13114 return True;
13115 end if;
13116 end if;
13117
13118 Next (Decl);
13119 end loop;
13120
13121 return False;
13122 end Requires_Cleanup_Actions;
13123
13124 ------------------------------------
13125 -- Safe_Unchecked_Type_Conversion --
13126 ------------------------------------
13127
13128 -- Note: this function knows quite a bit about the exact requirements of
13129 -- Gigi with respect to unchecked type conversions, and its code must be
13130 -- coordinated with any changes in Gigi in this area.
13131
13132 -- The above requirements should be documented in Sinfo ???
13133
13134 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
13135 Otyp : Entity_Id;
13136 Ityp : Entity_Id;
13137 Oalign : Uint;
13138 Ialign : Uint;
13139 Pexp : constant Node_Id := Parent (Exp);
13140
13141 begin
13142 -- If the expression is the RHS of an assignment or object declaration
13143 -- we are always OK because there will always be a target.
13144
13145 -- Object renaming declarations, (generated for view conversions of
13146 -- actuals in inlined calls), like object declarations, provide an
13147 -- explicit type, and are safe as well.
13148
13149 if (Nkind (Pexp) = N_Assignment_Statement
13150 and then Expression (Pexp) = Exp)
13151 or else Nkind (Pexp)
13152 in N_Object_Declaration | N_Object_Renaming_Declaration
13153 then
13154 return True;
13155
13156 -- If the expression is the prefix of an N_Selected_Component we should
13157 -- also be OK because GCC knows to look inside the conversion except if
13158 -- the type is discriminated. We assume that we are OK anyway if the
13159 -- type is not set yet or if it is controlled since we can't afford to
13160 -- introduce a temporary in this case.
13161
13162 elsif Nkind (Pexp) = N_Selected_Component
13163 and then Prefix (Pexp) = Exp
13164 then
13165 return No (Etype (Pexp))
13166 or else not Is_Type (Etype (Pexp))
13167 or else not Has_Discriminants (Etype (Pexp))
13168 or else Is_Constrained (Etype (Pexp));
13169 end if;
13170
13171 -- Set the output type, this comes from Etype if it is set, otherwise we
13172 -- take it from the subtype mark, which we assume was already fully
13173 -- analyzed.
13174
13175 if Present (Etype (Exp)) then
13176 Otyp := Etype (Exp);
13177 else
13178 Otyp := Entity (Subtype_Mark (Exp));
13179 end if;
13180
13181 -- The input type always comes from the expression, and we assume this
13182 -- is indeed always analyzed, so we can simply get the Etype.
13183
13184 Ityp := Etype (Expression (Exp));
13185
13186 -- Initialize alignments to unknown so far
13187
13188 Oalign := No_Uint;
13189 Ialign := No_Uint;
13190
13191 -- Replace a concurrent type by its corresponding record type and each
13192 -- type by its underlying type and do the tests on those. The original
13193 -- type may be a private type whose completion is a concurrent type, so
13194 -- find the underlying type first.
13195
13196 if Present (Underlying_Type (Otyp)) then
13197 Otyp := Underlying_Type (Otyp);
13198 end if;
13199
13200 if Present (Underlying_Type (Ityp)) then
13201 Ityp := Underlying_Type (Ityp);
13202 end if;
13203
13204 if Is_Concurrent_Type (Otyp) then
13205 Otyp := Corresponding_Record_Type (Otyp);
13206 end if;
13207
13208 if Is_Concurrent_Type (Ityp) then
13209 Ityp := Corresponding_Record_Type (Ityp);
13210 end if;
13211
13212 -- If the base types are the same, we know there is no problem since
13213 -- this conversion will be a noop.
13214
13215 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
13216 return True;
13217
13218 -- Same if this is an upwards conversion of an untagged type, and there
13219 -- are no constraints involved (could be more general???)
13220
13221 elsif Etype (Ityp) = Otyp
13222 and then not Is_Tagged_Type (Ityp)
13223 and then not Has_Discriminants (Ityp)
13224 and then No (First_Rep_Item (Base_Type (Ityp)))
13225 then
13226 return True;
13227
13228 -- If the expression has an access type (object or subprogram) we assume
13229 -- that the conversion is safe, because the size of the target is safe,
13230 -- even if it is a record (which might be treated as having unknown size
13231 -- at this point).
13232
13233 elsif Is_Access_Type (Ityp) then
13234 return True;
13235
13236 -- If the size of output type is known at compile time, there is never
13237 -- a problem. Note that unconstrained records are considered to be of
13238 -- known size, but we can't consider them that way here, because we are
13239 -- talking about the actual size of the object.
13240
13241 -- We also make sure that in addition to the size being known, we do not
13242 -- have a case which might generate an embarrassingly large temp in
13243 -- stack checking mode.
13244
13245 elsif Size_Known_At_Compile_Time (Otyp)
13246 and then
13247 (not Stack_Checking_Enabled
13248 or else not May_Generate_Large_Temp (Otyp))
13249 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
13250 then
13251 return True;
13252
13253 -- If either type is tagged, then we know the alignment is OK so Gigi
13254 -- will be able to use pointer punning.
13255
13256 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
13257 return True;
13258
13259 -- If either type is a limited record type, we cannot do a copy, so say
13260 -- safe since there's nothing else we can do.
13261
13262 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
13263 return True;
13264
13265 -- Conversions to and from packed array types are always ignored and
13266 -- hence are safe.
13267
13268 elsif Is_Packed_Array_Impl_Type (Otyp)
13269 or else Is_Packed_Array_Impl_Type (Ityp)
13270 then
13271 return True;
13272 end if;
13273
13274 -- The only other cases known to be safe is if the input type's
13275 -- alignment is known to be at least the maximum alignment for the
13276 -- target or if both alignments are known and the output type's
13277 -- alignment is no stricter than the input's. We can use the component
13278 -- type alignment for an array if a type is an unpacked array type.
13279
13280 if Present (Alignment_Clause (Otyp)) then
13281 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
13282
13283 elsif Is_Array_Type (Otyp)
13284 and then Present (Alignment_Clause (Component_Type (Otyp)))
13285 then
13286 Oalign := Expr_Value (Expression (Alignment_Clause
13287 (Component_Type (Otyp))));
13288 end if;
13289
13290 if Present (Alignment_Clause (Ityp)) then
13291 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
13292
13293 elsif Is_Array_Type (Ityp)
13294 and then Present (Alignment_Clause (Component_Type (Ityp)))
13295 then
13296 Ialign := Expr_Value (Expression (Alignment_Clause
13297 (Component_Type (Ityp))));
13298 end if;
13299
13300 if Present (Ialign) and then Ialign > Maximum_Alignment then
13301 return True;
13302
13303 elsif Present (Ialign)
13304 and then Present (Oalign)
13305 and then Ialign <= Oalign
13306 then
13307 return True;
13308
13309 -- Otherwise, Gigi cannot handle this and we must make a temporary
13310
13311 else
13312 return False;
13313 end if;
13314 end Safe_Unchecked_Type_Conversion;
13315
13316 ---------------------------------
13317 -- Set_Current_Value_Condition --
13318 ---------------------------------
13319
13320 -- Note: the implementation of this procedure is very closely tied to the
13321 -- implementation of Get_Current_Value_Condition. Here we set required
13322 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
13323 -- them, so they must have a consistent view.
13324
13325 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
13326
13327 procedure Set_Entity_Current_Value (N : Node_Id);
13328 -- If N is an entity reference, where the entity is of an appropriate
13329 -- kind, then set the current value of this entity to Cnode, unless
13330 -- there is already a definite value set there.
13331
13332 procedure Set_Expression_Current_Value (N : Node_Id);
13333 -- If N is of an appropriate form, sets an appropriate entry in current
13334 -- value fields of relevant entities. Multiple entities can be affected
13335 -- in the case of an AND or AND THEN.
13336
13337 ------------------------------
13338 -- Set_Entity_Current_Value --
13339 ------------------------------
13340
13341 procedure Set_Entity_Current_Value (N : Node_Id) is
13342 begin
13343 if Is_Entity_Name (N) then
13344 declare
13345 Ent : constant Entity_Id := Entity (N);
13346
13347 begin
13348 -- Don't capture if not safe to do so
13349
13350 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
13351 return;
13352 end if;
13353
13354 -- Here we have a case where the Current_Value field may need
13355 -- to be set. We set it if it is not already set to a compile
13356 -- time expression value.
13357
13358 -- Note that this represents a decision that one condition
13359 -- blots out another previous one. That's certainly right if
13360 -- they occur at the same level. If the second one is nested,
13361 -- then the decision is neither right nor wrong (it would be
13362 -- equally OK to leave the outer one in place, or take the new
13363 -- inner one). Really we should record both, but our data
13364 -- structures are not that elaborate.
13365
13366 if Nkind (Current_Value (Ent)) not in N_Subexpr then
13367 Set_Current_Value (Ent, Cnode);
13368 end if;
13369 end;
13370 end if;
13371 end Set_Entity_Current_Value;
13372
13373 ----------------------------------
13374 -- Set_Expression_Current_Value --
13375 ----------------------------------
13376
13377 procedure Set_Expression_Current_Value (N : Node_Id) is
13378 Cond : Node_Id;
13379
13380 begin
13381 Cond := N;
13382
13383 -- Loop to deal with (ignore for now) any NOT operators present. The
13384 -- presence of NOT operators will be handled properly when we call
13385 -- Get_Current_Value_Condition.
13386
13387 while Nkind (Cond) = N_Op_Not loop
13388 Cond := Right_Opnd (Cond);
13389 end loop;
13390
13391 -- For an AND or AND THEN, recursively process operands
13392
13393 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
13394 Set_Expression_Current_Value (Left_Opnd (Cond));
13395 Set_Expression_Current_Value (Right_Opnd (Cond));
13396 return;
13397 end if;
13398
13399 -- Check possible relational operator
13400
13401 if Nkind (Cond) in N_Op_Compare then
13402 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
13403 Set_Entity_Current_Value (Left_Opnd (Cond));
13404 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
13405 Set_Entity_Current_Value (Right_Opnd (Cond));
13406 end if;
13407
13408 elsif Nkind (Cond) in N_Type_Conversion
13409 | N_Qualified_Expression
13410 | N_Expression_With_Actions
13411 then
13412 Set_Expression_Current_Value (Expression (Cond));
13413
13414 -- Check possible boolean variable reference
13415
13416 else
13417 Set_Entity_Current_Value (Cond);
13418 end if;
13419 end Set_Expression_Current_Value;
13420
13421 -- Start of processing for Set_Current_Value_Condition
13422
13423 begin
13424 Set_Expression_Current_Value (Condition (Cnode));
13425 end Set_Current_Value_Condition;
13426
13427 --------------------------
13428 -- Set_Elaboration_Flag --
13429 --------------------------
13430
13431 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
13432 Loc : constant Source_Ptr := Sloc (N);
13433 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
13434 Asn : Node_Id;
13435
13436 begin
13437 if Present (Ent) then
13438
13439 -- Nothing to do if at the compilation unit level, because in this
13440 -- case the flag is set by the binder generated elaboration routine.
13441
13442 if Nkind (Parent (N)) = N_Compilation_Unit then
13443 null;
13444
13445 -- Here we do need to generate an assignment statement
13446
13447 else
13448 Check_Restriction (No_Elaboration_Code, N);
13449
13450 Asn :=
13451 Make_Assignment_Statement (Loc,
13452 Name => New_Occurrence_Of (Ent, Loc),
13453 Expression => Make_Integer_Literal (Loc, Uint_1));
13454
13455 -- Mark the assignment statement as elaboration code. This allows
13456 -- the early call region mechanism (see Sem_Elab) to properly
13457 -- ignore such assignments even though they are nonpreelaborable
13458 -- code.
13459
13460 Set_Is_Elaboration_Code (Asn);
13461
13462 if Nkind (Parent (N)) = N_Subunit then
13463 Insert_After (Corresponding_Stub (Parent (N)), Asn);
13464 else
13465 Insert_After (N, Asn);
13466 end if;
13467
13468 Analyze (Asn);
13469
13470 -- Kill current value indication. This is necessary because the
13471 -- tests of this flag are inserted out of sequence and must not
13472 -- pick up bogus indications of the wrong constant value.
13473
13474 Set_Current_Value (Ent, Empty);
13475
13476 -- If the subprogram is in the current declarative part and
13477 -- 'access has been applied to it, generate an elaboration
13478 -- check at the beginning of the declarations of the body.
13479
13480 if Nkind (N) = N_Subprogram_Body
13481 and then Address_Taken (Spec_Id)
13482 and then
13483 Ekind (Scope (Spec_Id)) in E_Block | E_Procedure | E_Function
13484 then
13485 declare
13486 Loc : constant Source_Ptr := Sloc (N);
13487 Decls : constant List_Id := Declarations (N);
13488 Chk : Node_Id;
13489
13490 begin
13491 -- No need to generate this check if first entry in the
13492 -- declaration list is a raise of Program_Error now.
13493
13494 if Present (Decls)
13495 and then Nkind (First (Decls)) = N_Raise_Program_Error
13496 then
13497 return;
13498 end if;
13499
13500 -- Otherwise generate the check
13501
13502 Chk :=
13503 Make_Raise_Program_Error (Loc,
13504 Condition =>
13505 Make_Op_Eq (Loc,
13506 Left_Opnd => New_Occurrence_Of (Ent, Loc),
13507 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
13508 Reason => PE_Access_Before_Elaboration);
13509
13510 if No (Decls) then
13511 Set_Declarations (N, New_List (Chk));
13512 else
13513 Prepend (Chk, Decls);
13514 end if;
13515
13516 Analyze (Chk);
13517 end;
13518 end if;
13519 end if;
13520 end if;
13521 end Set_Elaboration_Flag;
13522
13523 ----------------------------
13524 -- Set_Renamed_Subprogram --
13525 ----------------------------
13526
13527 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
13528 begin
13529 -- If input node is an identifier, we can just reset it
13530
13531 if Nkind (N) = N_Identifier then
13532 Set_Chars (N, Chars (E));
13533 Set_Entity (N, E);
13534
13535 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
13536
13537 else
13538 declare
13539 CS : constant Boolean := Comes_From_Source (N);
13540 begin
13541 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
13542 Set_Entity (N, E);
13543 Set_Comes_From_Source (N, CS);
13544 Set_Analyzed (N, True);
13545 end;
13546 end if;
13547 end Set_Renamed_Subprogram;
13548
13549 ----------------------
13550 -- Side_Effect_Free --
13551 ----------------------
13552
13553 function Side_Effect_Free
13554 (N : Node_Id;
13555 Name_Req : Boolean := False;
13556 Variable_Ref : Boolean := False) return Boolean
13557 is
13558 Typ : constant Entity_Id := Etype (N);
13559 -- Result type of the expression
13560
13561 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
13562 -- The argument N is a construct where the Prefix is dereferenced if it
13563 -- is an access type and the result is a variable. The call returns True
13564 -- if the construct is side effect free (not considering side effects in
13565 -- other than the prefix which are to be tested by the caller).
13566
13567 function Within_In_Parameter (N : Node_Id) return Boolean;
13568 -- Determines if N is a subcomponent of a composite in-parameter. If so,
13569 -- N is not side-effect free when the actual is global and modifiable
13570 -- indirectly from within a subprogram, because it may be passed by
13571 -- reference. The front-end must be conservative here and assume that
13572 -- this may happen with any array or record type. On the other hand, we
13573 -- cannot create temporaries for all expressions for which this
13574 -- condition is true, for various reasons that might require clearing up
13575 -- ??? For example, discriminant references that appear out of place, or
13576 -- spurious type errors with class-wide expressions. As a result, we
13577 -- limit the transformation to loop bounds, which is so far the only
13578 -- case that requires it.
13579
13580 -----------------------------
13581 -- Safe_Prefixed_Reference --
13582 -----------------------------
13583
13584 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
13585 begin
13586 -- If prefix is not side effect free, definitely not safe
13587
13588 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
13589 return False;
13590
13591 -- If the prefix is of an access type that is not access-to-constant,
13592 -- then this construct is a variable reference, which means it is to
13593 -- be considered to have side effects if Variable_Ref is set True.
13594
13595 elsif Is_Access_Type (Etype (Prefix (N)))
13596 and then not Is_Access_Constant (Etype (Prefix (N)))
13597 and then Variable_Ref
13598 then
13599 -- Exception is a prefix that is the result of a previous removal
13600 -- of side effects.
13601
13602 return Is_Entity_Name (Prefix (N))
13603 and then not Comes_From_Source (Prefix (N))
13604 and then Ekind (Entity (Prefix (N))) = E_Constant
13605 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
13606
13607 -- If the prefix is an explicit dereference then this construct is a
13608 -- variable reference, which means it is to be considered to have
13609 -- side effects if Variable_Ref is True.
13610
13611 -- We do NOT exclude dereferences of access-to-constant types because
13612 -- we handle them as constant view of variables.
13613
13614 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
13615 and then Variable_Ref
13616 then
13617 return False;
13618
13619 -- Note: The following test is the simplest way of solving a complex
13620 -- problem uncovered by the following test (Side effect on loop bound
13621 -- that is a subcomponent of a global variable:
13622
13623 -- with Text_Io; use Text_Io;
13624 -- procedure Tloop is
13625 -- type X is
13626 -- record
13627 -- V : Natural := 4;
13628 -- S : String (1..5) := (others => 'a');
13629 -- end record;
13630 -- X1 : X;
13631
13632 -- procedure Modi;
13633
13634 -- generic
13635 -- with procedure Action;
13636 -- procedure Loop_G (Arg : X; Msg : String)
13637
13638 -- procedure Loop_G (Arg : X; Msg : String) is
13639 -- begin
13640 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
13641 -- & Natural'Image (Arg.V));
13642 -- for Index in 1 .. Arg.V loop
13643 -- Text_Io.Put_Line
13644 -- (Natural'Image (Index) & " " & Arg.S (Index));
13645 -- if Index > 2 then
13646 -- Modi;
13647 -- end if;
13648 -- end loop;
13649 -- Put_Line ("end loop_g " & Msg);
13650 -- end;
13651
13652 -- procedure Loop1 is new Loop_G (Modi);
13653 -- procedure Modi is
13654 -- begin
13655 -- X1.V := 1;
13656 -- Loop1 (X1, "from modi");
13657 -- end;
13658 --
13659 -- begin
13660 -- Loop1 (X1, "initial");
13661 -- end;
13662
13663 -- The output of the above program should be:
13664
13665 -- begin loop_g initial will loop till: 4
13666 -- 1 a
13667 -- 2 a
13668 -- 3 a
13669 -- begin loop_g from modi will loop till: 1
13670 -- 1 a
13671 -- end loop_g from modi
13672 -- 4 a
13673 -- begin loop_g from modi will loop till: 1
13674 -- 1 a
13675 -- end loop_g from modi
13676 -- end loop_g initial
13677
13678 -- If a loop bound is a subcomponent of a global variable, a
13679 -- modification of that variable within the loop may incorrectly
13680 -- affect the execution of the loop.
13681
13682 elsif Parent_Kind (Parent (N)) = N_Loop_Parameter_Specification
13683 and then Within_In_Parameter (Prefix (N))
13684 and then Variable_Ref
13685 then
13686 return False;
13687
13688 -- All other cases are side effect free
13689
13690 else
13691 return True;
13692 end if;
13693 end Safe_Prefixed_Reference;
13694
13695 -------------------------
13696 -- Within_In_Parameter --
13697 -------------------------
13698
13699 function Within_In_Parameter (N : Node_Id) return Boolean is
13700 begin
13701 if not Comes_From_Source (N) then
13702 return False;
13703
13704 elsif Is_Entity_Name (N) then
13705 return Ekind (Entity (N)) = E_In_Parameter;
13706
13707 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
13708 return Within_In_Parameter (Prefix (N));
13709
13710 else
13711 return False;
13712 end if;
13713 end Within_In_Parameter;
13714
13715 -- Start of processing for Side_Effect_Free
13716
13717 begin
13718 -- If volatile reference, always consider it to have side effects
13719
13720 if Is_Volatile_Reference (N) then
13721 return False;
13722 end if;
13723
13724 -- Note on checks that could raise Constraint_Error. Strictly, if we
13725 -- take advantage of 11.6, these checks do not count as side effects.
13726 -- However, we would prefer to consider that they are side effects,
13727 -- since the back end CSE does not work very well on expressions which
13728 -- can raise Constraint_Error. On the other hand if we don't consider
13729 -- them to be side effect free, then we get some awkward expansions
13730 -- in -gnato mode, resulting in code insertions at a point where we
13731 -- do not have a clear model for performing the insertions.
13732
13733 -- Special handling for entity names
13734
13735 if Is_Entity_Name (N) then
13736
13737 -- A type reference is always side effect free
13738
13739 if Is_Type (Entity (N)) then
13740 return True;
13741
13742 -- Variables are considered to be a side effect if Variable_Ref
13743 -- is set or if we have a volatile reference and Name_Req is off.
13744 -- If Name_Req is True then we can't help returning a name which
13745 -- effectively allows multiple references in any case.
13746
13747 elsif Is_Variable (N, Use_Original_Node => False) then
13748 return not Variable_Ref
13749 and then (not Is_Volatile_Reference (N) or else Name_Req);
13750
13751 -- Any other entity (e.g. a subtype name) is definitely side
13752 -- effect free.
13753
13754 else
13755 return True;
13756 end if;
13757
13758 -- A value known at compile time is always side effect free
13759
13760 elsif Compile_Time_Known_Value (N) then
13761 return True;
13762
13763 -- A variable renaming is not side-effect free, because the renaming
13764 -- will function like a macro in the front-end in some cases, and an
13765 -- assignment can modify the component designated by N, so we need to
13766 -- create a temporary for it.
13767
13768 -- The guard testing for Entity being present is needed at least in
13769 -- the case of rewritten predicate expressions, and may well also be
13770 -- appropriate elsewhere. Obviously we can't go testing the entity
13771 -- field if it does not exist, so it's reasonable to say that this is
13772 -- not the renaming case if it does not exist.
13773
13774 elsif Is_Entity_Name (Original_Node (N))
13775 and then Present (Entity (Original_Node (N)))
13776 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13777 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13778 then
13779 declare
13780 RO : constant Node_Id :=
13781 Renamed_Object (Entity (Original_Node (N)));
13782
13783 begin
13784 -- If the renamed object is an indexed component, or an
13785 -- explicit dereference, then the designated object could
13786 -- be modified by an assignment.
13787
13788 if Nkind (RO) in N_Indexed_Component | N_Explicit_Dereference then
13789 return False;
13790
13791 -- A selected component must have a safe prefix
13792
13793 elsif Nkind (RO) = N_Selected_Component then
13794 return Safe_Prefixed_Reference (RO);
13795
13796 -- In all other cases, designated object cannot be changed so
13797 -- we are side effect free.
13798
13799 else
13800 return True;
13801 end if;
13802 end;
13803
13804 -- Remove_Side_Effects generates an object renaming declaration to
13805 -- capture the expression of a class-wide expression. In VM targets
13806 -- the frontend performs no expansion for dispatching calls to
13807 -- class- wide types since they are handled by the VM. Hence, we must
13808 -- locate here if this node corresponds to a previous invocation of
13809 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13810
13811 elsif not Tagged_Type_Expansion
13812 and then not Comes_From_Source (N)
13813 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13814 and then Is_Class_Wide_Type (Typ)
13815 then
13816 return True;
13817
13818 -- Generating C the type conversion of an access to constrained array
13819 -- type into an access to unconstrained array type involves initializing
13820 -- a fat pointer and the expression cannot be assumed to be free of side
13821 -- effects since it must referenced several times to compute its bounds.
13822
13823 elsif Modify_Tree_For_C
13824 and then Nkind (N) = N_Type_Conversion
13825 and then Is_Access_Type (Typ)
13826 and then Is_Array_Type (Designated_Type (Typ))
13827 and then not Is_Constrained (Designated_Type (Typ))
13828 then
13829 return False;
13830 end if;
13831
13832 -- For other than entity names and compile time known values,
13833 -- check the node kind for special processing.
13834
13835 case Nkind (N) is
13836
13837 -- An attribute reference is side-effect free if its expressions
13838 -- are side-effect free and its prefix is side-effect free or is
13839 -- an entity reference.
13840
13841 when N_Attribute_Reference =>
13842 return Side_Effect_Free_Attribute (Attribute_Name (N))
13843 and then
13844 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13845 and then
13846 (Is_Entity_Name (Prefix (N))
13847 or else
13848 Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref));
13849
13850 -- A binary operator is side effect free if and both operands are
13851 -- side effect free. For this purpose binary operators include
13852 -- short circuit forms.
13853
13854 when N_Binary_Op
13855 | N_Short_Circuit
13856 =>
13857 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13858 and then
13859 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13860
13861 -- Membership tests may have either Right_Opnd or Alternatives set
13862
13863 when N_Membership_Test =>
13864 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13865 and then
13866 (if Present (Right_Opnd (N))
13867 then Side_Effect_Free
13868 (Right_Opnd (N), Name_Req, Variable_Ref)
13869 else Side_Effect_Free
13870 (Alternatives (N), Name_Req, Variable_Ref));
13871
13872 -- An explicit dereference is side effect free only if it is
13873 -- a side effect free prefixed reference.
13874
13875 when N_Explicit_Dereference =>
13876 return Safe_Prefixed_Reference (N);
13877
13878 -- An expression with action is side effect free if its expression
13879 -- is side effect free and it has no actions.
13880
13881 when N_Expression_With_Actions =>
13882 return
13883 Is_Empty_List (Actions (N))
13884 and then Side_Effect_Free
13885 (Expression (N), Name_Req, Variable_Ref);
13886
13887 -- A call to _rep_to_pos is side effect free, since we generate
13888 -- this pure function call ourselves. Moreover it is critically
13889 -- important to make this exception, since otherwise we can have
13890 -- discriminants in array components which don't look side effect
13891 -- free in the case of an array whose index type is an enumeration
13892 -- type with an enumeration rep clause.
13893
13894 -- All other function calls are not side effect free
13895
13896 when N_Function_Call =>
13897 return
13898 Nkind (Name (N)) = N_Identifier
13899 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
13900 and then Side_Effect_Free
13901 (First (Parameter_Associations (N)),
13902 Name_Req, Variable_Ref);
13903
13904 -- An IF expression is side effect free if it's of a scalar type, and
13905 -- all its components are all side effect free (conditions and then
13906 -- actions and else actions). We restrict to scalar types, since it
13907 -- is annoying to deal with things like (if A then B else C)'First
13908 -- where the type involved is a string type.
13909
13910 when N_If_Expression =>
13911 return
13912 Is_Scalar_Type (Typ)
13913 and then Side_Effect_Free
13914 (Expressions (N), Name_Req, Variable_Ref);
13915
13916 -- An indexed component is side effect free if it is a side
13917 -- effect free prefixed reference and all the indexing
13918 -- expressions are side effect free.
13919
13920 when N_Indexed_Component =>
13921 return
13922 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13923 and then Safe_Prefixed_Reference (N);
13924
13925 -- A type qualification, type conversion, or unchecked expression is
13926 -- side effect free if the expression is side effect free.
13927
13928 when N_Qualified_Expression
13929 | N_Type_Conversion
13930 | N_Unchecked_Expression
13931 =>
13932 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13933
13934 -- A selected component is side effect free only if it is a side
13935 -- effect free prefixed reference.
13936
13937 when N_Selected_Component =>
13938 return Safe_Prefixed_Reference (N);
13939
13940 -- A range is side effect free if the bounds are side effect free
13941
13942 when N_Range =>
13943 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13944 and then
13945 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13946
13947 -- A slice is side effect free if it is a side effect free
13948 -- prefixed reference and the bounds are side effect free.
13949
13950 when N_Slice =>
13951 return
13952 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13953 and then Safe_Prefixed_Reference (N);
13954
13955 -- A unary operator is side effect free if the operand
13956 -- is side effect free.
13957
13958 when N_Unary_Op =>
13959 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13960
13961 -- An unchecked type conversion is side effect free only if it
13962 -- is safe and its argument is side effect free.
13963
13964 when N_Unchecked_Type_Conversion =>
13965 return
13966 Safe_Unchecked_Type_Conversion (N)
13967 and then Side_Effect_Free
13968 (Expression (N), Name_Req, Variable_Ref);
13969
13970 -- A literal is side effect free
13971
13972 when N_Character_Literal
13973 | N_Integer_Literal
13974 | N_Real_Literal
13975 | N_String_Literal
13976 =>
13977 return True;
13978
13979 -- An aggregate is side effect free if all its values are compile
13980 -- time known.
13981
13982 when N_Aggregate =>
13983 return Compile_Time_Known_Aggregate (N);
13984
13985 -- We consider that anything else has side effects. This is a bit
13986 -- crude, but we are pretty close for most common cases, and we
13987 -- are certainly correct (i.e. we never return True when the
13988 -- answer should be False).
13989
13990 when others =>
13991 return False;
13992 end case;
13993 end Side_Effect_Free;
13994
13995 -- A list is side effect free if all elements of the list are side
13996 -- effect free.
13997
13998 function Side_Effect_Free
13999 (L : List_Id;
14000 Name_Req : Boolean := False;
14001 Variable_Ref : Boolean := False) return Boolean
14002 is
14003 N : Node_Id;
14004
14005 begin
14006 if L = No_List or else L = Error_List then
14007 return True;
14008
14009 else
14010 N := First (L);
14011 while Present (N) loop
14012 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
14013 return False;
14014 else
14015 Next (N);
14016 end if;
14017 end loop;
14018
14019 return True;
14020 end if;
14021 end Side_Effect_Free;
14022
14023 --------------------------------
14024 -- Side_Effect_Free_Attribute --
14025 --------------------------------
14026
14027 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean is
14028 begin
14029 case Name is
14030 when Name_Input =>
14031 return False;
14032
14033 when Name_Image
14034 | Name_Img
14035 | Name_Wide_Image
14036 | Name_Wide_Wide_Image
14037 =>
14038 -- CodePeer doesn't want to see replicated copies of 'Image calls
14039
14040 return not CodePeer_Mode;
14041
14042 when others =>
14043 return True;
14044 end case;
14045 end Side_Effect_Free_Attribute;
14046
14047 ----------------------------------
14048 -- Silly_Boolean_Array_Not_Test --
14049 ----------------------------------
14050
14051 -- This procedure implements an odd and silly test. We explicitly check
14052 -- for the case where the 'First of the component type is equal to the
14053 -- 'Last of this component type, and if this is the case, we make sure
14054 -- that constraint error is raised. The reason is that the NOT is bound
14055 -- to cause CE in this case, and we will not otherwise catch it.
14056
14057 -- No such check is required for AND and OR, since for both these cases
14058 -- False op False = False, and True op True = True. For the XOR case,
14059 -- see Silly_Boolean_Array_Xor_Test.
14060
14061 -- Believe it or not, this was reported as a bug. Note that nearly always,
14062 -- the test will evaluate statically to False, so the code will be
14063 -- statically removed, and no extra overhead caused.
14064
14065 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
14066 Loc : constant Source_Ptr := Sloc (N);
14067 CT : constant Entity_Id := Component_Type (T);
14068
14069 begin
14070 -- The check we install is
14071
14072 -- constraint_error when
14073 -- component_type'first = component_type'last
14074 -- and then array_type'Length /= 0)
14075
14076 -- We need the last guard because we don't want to raise CE for empty
14077 -- arrays since no out of range values result. (Empty arrays with a
14078 -- component type of True .. True -- very useful -- even the ACATS
14079 -- does not test that marginal case).
14080
14081 Insert_Action (N,
14082 Make_Raise_Constraint_Error (Loc,
14083 Condition =>
14084 Make_And_Then (Loc,
14085 Left_Opnd =>
14086 Make_Op_Eq (Loc,
14087 Left_Opnd =>
14088 Make_Attribute_Reference (Loc,
14089 Prefix => New_Occurrence_Of (CT, Loc),
14090 Attribute_Name => Name_First),
14091
14092 Right_Opnd =>
14093 Make_Attribute_Reference (Loc,
14094 Prefix => New_Occurrence_Of (CT, Loc),
14095 Attribute_Name => Name_Last)),
14096
14097 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
14098 Reason => CE_Range_Check_Failed));
14099 end Silly_Boolean_Array_Not_Test;
14100
14101 ----------------------------------
14102 -- Silly_Boolean_Array_Xor_Test --
14103 ----------------------------------
14104
14105 -- This procedure implements an odd and silly test. We explicitly check
14106 -- for the XOR case where the component type is True .. True, since this
14107 -- will raise constraint error. A special check is required since CE
14108 -- will not be generated otherwise (cf Expand_Packed_Not).
14109
14110 -- No such check is required for AND and OR, since for both these cases
14111 -- False op False = False, and True op True = True, and no check is
14112 -- required for the case of False .. False, since False xor False = False.
14113 -- See also Silly_Boolean_Array_Not_Test
14114
14115 procedure Silly_Boolean_Array_Xor_Test
14116 (N : Node_Id;
14117 R : Node_Id;
14118 T : Entity_Id)
14119 is
14120 Loc : constant Source_Ptr := Sloc (N);
14121 CT : constant Entity_Id := Component_Type (T);
14122
14123 begin
14124 -- The check we install is
14125
14126 -- constraint_error when
14127 -- Boolean (component_type'First)
14128 -- and then Boolean (component_type'Last)
14129 -- and then array_type'Length /= 0)
14130
14131 -- We need the last guard because we don't want to raise CE for empty
14132 -- arrays since no out of range values result (Empty arrays with a
14133 -- component type of True .. True -- very useful -- even the ACATS
14134 -- does not test that marginal case).
14135
14136 Insert_Action (N,
14137 Make_Raise_Constraint_Error (Loc,
14138 Condition =>
14139 Make_And_Then (Loc,
14140 Left_Opnd =>
14141 Make_And_Then (Loc,
14142 Left_Opnd =>
14143 Convert_To (Standard_Boolean,
14144 Make_Attribute_Reference (Loc,
14145 Prefix => New_Occurrence_Of (CT, Loc),
14146 Attribute_Name => Name_First)),
14147
14148 Right_Opnd =>
14149 Convert_To (Standard_Boolean,
14150 Make_Attribute_Reference (Loc,
14151 Prefix => New_Occurrence_Of (CT, Loc),
14152 Attribute_Name => Name_Last))),
14153
14154 Right_Opnd => Make_Non_Empty_Check (Loc, R)),
14155 Reason => CE_Range_Check_Failed));
14156 end Silly_Boolean_Array_Xor_Test;
14157
14158 ----------------------------
14159 -- Small_Integer_Type_For --
14160 ----------------------------
14161
14162 function Small_Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id
14163 is
14164 begin
14165 pragma Assert (S <= System_Max_Integer_Size);
14166
14167 if S <= Standard_Short_Short_Integer_Size then
14168 if Uns then
14169 return Standard_Short_Short_Unsigned;
14170 else
14171 return Standard_Short_Short_Integer;
14172 end if;
14173
14174 elsif S <= Standard_Short_Integer_Size then
14175 if Uns then
14176 return Standard_Short_Unsigned;
14177 else
14178 return Standard_Short_Integer;
14179 end if;
14180
14181 elsif S <= Standard_Integer_Size then
14182 if Uns then
14183 return Standard_Unsigned;
14184 else
14185 return Standard_Integer;
14186 end if;
14187
14188 elsif S <= Standard_Long_Integer_Size then
14189 if Uns then
14190 return Standard_Long_Unsigned;
14191 else
14192 return Standard_Long_Integer;
14193 end if;
14194
14195 elsif S <= Standard_Long_Long_Integer_Size then
14196 if Uns then
14197 return Standard_Long_Long_Unsigned;
14198 else
14199 return Standard_Long_Long_Integer;
14200 end if;
14201
14202 elsif S <= Standard_Long_Long_Long_Integer_Size then
14203 if Uns then
14204 return Standard_Long_Long_Long_Unsigned;
14205 else
14206 return Standard_Long_Long_Long_Integer;
14207 end if;
14208
14209 else
14210 raise Program_Error;
14211 end if;
14212 end Small_Integer_Type_For;
14213
14214 -------------------
14215 -- Type_Map_Hash --
14216 -------------------
14217
14218 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
14219 begin
14220 return Type_Map_Header (Id mod Type_Map_Size);
14221 end Type_Map_Hash;
14222
14223 ------------------------------------------
14224 -- Type_May_Have_Bit_Aligned_Components --
14225 ------------------------------------------
14226
14227 function Type_May_Have_Bit_Aligned_Components
14228 (Typ : Entity_Id) return Boolean
14229 is
14230 begin
14231 -- Array type, check component type
14232
14233 if Is_Array_Type (Typ) then
14234 return
14235 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
14236
14237 -- Record type, check components
14238
14239 elsif Is_Record_Type (Typ) then
14240 declare
14241 E : Entity_Id;
14242
14243 begin
14244 E := First_Component_Or_Discriminant (Typ);
14245 while Present (E) loop
14246 -- This is the crucial test: if the component itself causes
14247 -- trouble, then we can stop and return True.
14248
14249 if Component_May_Be_Bit_Aligned (E) then
14250 return True;
14251 end if;
14252
14253 -- Otherwise, we need to test its type, to see if it may
14254 -- itself contain a troublesome component.
14255
14256 if Type_May_Have_Bit_Aligned_Components (Etype (E)) then
14257 return True;
14258 end if;
14259
14260 Next_Component_Or_Discriminant (E);
14261 end loop;
14262
14263 return False;
14264 end;
14265
14266 -- Type other than array or record is always OK
14267
14268 else
14269 return False;
14270 end if;
14271 end Type_May_Have_Bit_Aligned_Components;
14272
14273 -------------------------------
14274 -- Update_Primitives_Mapping --
14275 -------------------------------
14276
14277 procedure Update_Primitives_Mapping
14278 (Inher_Id : Entity_Id;
14279 Subp_Id : Entity_Id)
14280 is
14281 Parent_Type : constant Entity_Id := Find_Dispatching_Type (Inher_Id);
14282 Derived_Type : constant Entity_Id := Find_Dispatching_Type (Subp_Id);
14283
14284 begin
14285 pragma Assert (Parent_Type /= Derived_Type);
14286 Map_Types (Parent_Type, Derived_Type);
14287 end Update_Primitives_Mapping;
14288
14289 ----------------------------------
14290 -- Within_Case_Or_If_Expression --
14291 ----------------------------------
14292
14293 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
14294 Par : Node_Id;
14295
14296 begin
14297 -- Locate an enclosing case or if expression. Note that these constructs
14298 -- can be expanded into Expression_With_Actions, hence the test of the
14299 -- original node.
14300
14301 Par := Parent (N);
14302 while Present (Par) loop
14303 if Nkind (Original_Node (Par)) in N_Case_Expression | N_If_Expression
14304 then
14305 return True;
14306
14307 -- Prevent the search from going too far
14308
14309 elsif Is_Body_Or_Package_Declaration (Par) then
14310 return False;
14311 end if;
14312
14313 Par := Parent (Par);
14314 end loop;
14315
14316 return False;
14317 end Within_Case_Or_If_Expression;
14318
14319 ------------------------------
14320 -- Predicate_Check_In_Scope --
14321 ------------------------------
14322
14323 function Predicate_Check_In_Scope (N : Node_Id) return Boolean is
14324 S : Entity_Id;
14325
14326 begin
14327 S := Current_Scope;
14328 while Present (S) and then not Is_Subprogram (S) loop
14329 S := Scope (S);
14330 end loop;
14331
14332 if Present (S) then
14333
14334 -- Predicate checks should only be enabled in init procs for
14335 -- expressions coming from source.
14336
14337 if Is_Init_Proc (S) then
14338 return Comes_From_Source (N);
14339
14340 elsif Get_TSS_Name (S) /= TSS_Null
14341 and then not Is_Predicate_Function (S)
14342 and then not Is_Predicate_Function_M (S)
14343 then
14344 return False;
14345 end if;
14346 end if;
14347
14348 return True;
14349 end Predicate_Check_In_Scope;
14350
14351 end Exp_Util;