Debian Codegraph @

gcc/ada/exp_util.adb

raw
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-2025, 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 Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Aggr; use Exp_Aggr;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch11; use Exp_Ch11;
39 with Exp_Dbug; use Exp_Dbug;
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 Stand; use Stand;
65 with Stringt; use Stringt;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Validsw; use Validsw;
69 with Warnsw; use Warnsw;
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_Expression_Of_Func_Return (N : Node_Id) return Boolean;
171 -- Return True if N is the expression of a function return
172
173 function Is_Uninitialized_Aggregate
174 (Exp : Node_Id;
175 T : Entity_Id) return Boolean;
176 -- Determine whether an array aggregate used in an object declaration
177 -- is uninitialized, when the aggregate is declared with a box and
178 -- the component type has no default value. Such an aggregate can be
179 -- optimized away to prevent the copying of uninitialized data, and
180 -- the bounds of the aggregate can be propagated directly to the
181 -- object declaration.
182
183 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
184 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
185 -- an assertion expression that should be verified at run time.
186
187 function Make_Literal_Range
188 (Loc : Source_Ptr;
189 Literal_Typ : Entity_Id) return Node_Id;
190 -- Produce a Range node whose bounds are:
191 -- Low_Bound (Literal_Type) ..
192 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
193 -- this is used for expanding declarations like X : String := "sdfgdfg";
194 --
195 -- If the index type of the target array is not integer, we generate:
196 -- Low_Bound (Literal_Type) ..
197 -- Literal_Type'Val
198 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
199 -- + (Length (Literal_Typ) -1))
200
201 function Make_Non_Empty_Check
202 (Loc : Source_Ptr;
203 N : Node_Id) return Node_Id;
204 -- Produce a boolean expression checking that the unidimensional array
205 -- node N is not empty.
206
207 function New_Class_Wide_Subtype
208 (CW_Typ : Entity_Id;
209 N : Node_Id) return Entity_Id;
210 -- Create an implicit subtype of CW_Typ attached to node N
211
212 function Requires_Cleanup_Actions
213 (L : List_Id;
214 Lib_Level : Boolean;
215 Nested_Constructs : Boolean) return Boolean;
216 -- Given a list L, determine whether it contains one of the following:
217 --
218 -- 1) controlled objects
219 -- 2) library-level tagged types
220 --
221 -- Lib_Level is True when the list comes from a construct at the library
222 -- level, and False otherwise. Nested_Constructs is True when any nested
223 -- packages declared in L must be processed, and False otherwise.
224
225 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean;
226 -- Return True if the evaluation of the given attribute is considered
227 -- side-effect-free, independently of its prefix and expressions.
228
229 -------------------------------------
230 -- Activate_Atomic_Synchronization --
231 -------------------------------------
232
233 procedure Activate_Atomic_Synchronization (N : Node_Id) is
234 Msg_Node : Node_Id;
235
236 begin
237 case Nkind (Parent (N)) is
238
239 -- Check for cases of appearing in the prefix of a construct where we
240 -- don't need atomic synchronization for this kind of usage.
241
242 when
243 -- Nothing to do if we are the prefix of an attribute, since we
244 -- do not want an atomic sync operation for things like 'Size.
245
246 N_Attribute_Reference
247
248 -- The N_Reference node is like an attribute
249
250 | N_Reference
251
252 -- Nothing to do for a reference to a component (or components)
253 -- of a composite object. Only reads and updates of the object
254 -- as a whole require atomic synchronization (RM C.6 (15)).
255
256 | N_Indexed_Component
257 | N_Selected_Component
258 | N_Slice
259 =>
260 -- For all the above cases, nothing to do if we are the prefix
261
262 if Prefix (Parent (N)) = N then
263 return;
264 end if;
265
266 when others =>
267 null;
268 end case;
269
270 -- Nothing to do for the identifier in an object renaming declaration,
271 -- the renaming itself does not need atomic synchronization.
272
273 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
274 return;
275 end if;
276
277 -- Go ahead and set the flag
278
279 Set_Atomic_Sync_Required (N);
280
281 -- Generate info message if requested
282
283 if Warn_On_Atomic_Synchronization then
284 case Nkind (N) is
285 when N_Identifier =>
286 Msg_Node := N;
287
288 when N_Expanded_Name
289 | N_Selected_Component
290 =>
291 Msg_Node := Selector_Name (N);
292
293 when N_Explicit_Dereference
294 | N_Indexed_Component
295 =>
296 Msg_Node := Empty;
297
298 when others =>
299 pragma Assert (False);
300 return;
301 end case;
302
303 if Present (Msg_Node) then
304 Error_Msg_N
305 ("atomic synchronization set for &?.n?", Msg_Node);
306 else
307 Error_Msg_N
308 ("atomic synchronization set?.n?", N);
309 end if;
310 end if;
311 end Activate_Atomic_Synchronization;
312
313 ----------------------
314 -- Adjust_Condition --
315 ----------------------
316
317 procedure Adjust_Condition (N : Node_Id) is
318
319 function Is_Hardbool_Type (T : Entity_Id) return Boolean;
320 -- Return True iff T is a type annotated with the
321 -- Machine_Attribute pragma "hardbool".
322
323 ----------------------
324 -- Is_Hardbool_Type --
325 ----------------------
326
327 function Is_Hardbool_Type (T : Entity_Id) return Boolean is
328
329 function Find_Hardbool_Pragma
330 (Id : Entity_Id) return Node_Id;
331 -- Return a Rep_Item associated with entity Id that
332 -- corresponds to the Hardbool Machine_Attribute pragma, if
333 -- any, or Empty otherwise.
334
335 function Pragma_Arg_To_String (Item : Node_Id) return String is
336 (To_String (Strval (Expr_Value_S (Item))));
337 -- Return the pragma argument Item as a String
338
339 function Hardbool_Pragma_P (Item : Node_Id) return Boolean is
340 (Nkind (Item) = N_Pragma
341 and then
342 Pragma_Name (Item) = Name_Machine_Attribute
343 and then
344 Pragma_Arg_To_String
345 (Get_Pragma_Arg
346 (Next (First (Pragma_Argument_Associations (Item)))))
347 = "hardbool");
348 -- Return True iff representation Item is a "hardbool"
349 -- Machine_Attribute pragma.
350
351 --------------------------
352 -- Find_Hardbool_Pragma --
353 --------------------------
354
355 function Find_Hardbool_Pragma
356 (Id : Entity_Id) return Node_Id
357 is
358 Item : Node_Id;
359
360 begin
361 if not Has_Gigi_Rep_Item (Id) then
362 return Empty;
363 end if;
364
365 Item := First_Rep_Item (Id);
366 while Present (Item) loop
367 if Hardbool_Pragma_P (Item) then
368 return Item;
369 end if;
370 Item := Next_Rep_Item (Item);
371 end loop;
372
373 return Empty;
374 end Find_Hardbool_Pragma;
375
376 -- Start of processing for Is_Hardbool_Type
377
378 begin
379 return Present (Find_Hardbool_Pragma (T));
380 end Is_Hardbool_Type;
381
382 -- Start of processing for Adjust_Condition
383
384 begin
385 if No (N) then
386 return;
387 end if;
388
389 declare
390 Loc : constant Source_Ptr := Sloc (N);
391 T : constant Entity_Id := Etype (N);
392
393 begin
394 -- Defend against a call where the argument has no type, or has a
395 -- type that is not Boolean. This can occur because of prior errors.
396
397 if No (T) or else not Is_Boolean_Type (T) then
398 return;
399 end if;
400
401 -- Apply validity checking if needed
402
403 if Validity_Checks_On
404 and then
405 (Validity_Check_Tests or else Is_Hardbool_Type (T))
406
407 -- no check needed here if validity has already been checked
408 and then not
409 (Validity_Check_Operands and then
410 (Nkind (N) in N_Op or else Nkind (Parent (N)) in N_Op))
411 then
412 Ensure_Valid (N);
413 end if;
414
415 -- Immediate return if standard boolean, the most common case,
416 -- where nothing needs to be done.
417
418 if Base_Type (T) = Standard_Boolean then
419 return;
420 end if;
421
422 -- Case of zero/nonzero semantics or nonstandard enumeration
423 -- representation. In each case, we rewrite the node as:
424
425 -- ityp!(N) /= False'Enum_Rep
426
427 -- where ityp is an integer type with large enough size to hold any
428 -- value of type T.
429
430 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
431 Rewrite (N,
432 Make_Op_Ne (Loc,
433 Left_Opnd =>
434 Unchecked_Convert_To
435 (Integer_Type_For (Esize (T), Uns => False), N),
436 Right_Opnd =>
437 Make_Attribute_Reference (Loc,
438 Attribute_Name => Name_Enum_Rep,
439 Prefix =>
440 New_Occurrence_Of (First_Literal (T), Loc))));
441 Analyze_And_Resolve (N, Standard_Boolean);
442
443 else
444 Rewrite (N, Convert_To (Standard_Boolean, N));
445 Analyze_And_Resolve (N, Standard_Boolean);
446 end if;
447 end;
448 end Adjust_Condition;
449
450 ------------------------
451 -- Adjust_Result_Type --
452 ------------------------
453
454 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
455 begin
456 -- Ignore call if current type is not Standard.Boolean
457
458 if Etype (N) /= Standard_Boolean then
459 return;
460 end if;
461
462 -- If result is already of correct type, nothing to do. Note that
463 -- this will get the most common case where everything has a type
464 -- of Standard.Boolean.
465
466 if Base_Type (T) = Standard_Boolean then
467 return;
468
469 else
470 declare
471 KP : constant Node_Kind := Nkind (Parent (N));
472
473 begin
474 -- If result is to be used as a Condition in the syntax, no need
475 -- to convert it back, since if it was changed to Standard.Boolean
476 -- using Adjust_Condition, that is just fine for this usage.
477
478 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
479 return;
480
481 -- If result is an operand of another logical operation, no need
482 -- to reset its type, since Standard.Boolean is just fine, and
483 -- such operations always do Adjust_Condition on their operands.
484
485 elsif KP in N_Op_Boolean
486 or else KP in N_Short_Circuit
487 or else KP = N_Op_Not
488 or else (KP in N_Type_Conversion
489 | N_Unchecked_Type_Conversion
490 and then Is_Boolean_Type (Etype (Parent (N))))
491 then
492 return;
493
494 -- Otherwise we perform a conversion from the current type, which
495 -- must be Standard.Boolean, to the desired type. Use the base
496 -- type to prevent spurious constraint checks that are extraneous
497 -- to the transformation. The type and its base have the same
498 -- representation, standard or otherwise.
499
500 else
501 Set_Analyzed (N);
502 Rewrite (N, Convert_To (Base_Type (T), N));
503 Analyze_And_Resolve (N, Base_Type (T));
504 end if;
505 end;
506 end if;
507 end Adjust_Result_Type;
508
509 --------------------------
510 -- Append_Freeze_Action --
511 --------------------------
512
513 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
514 Fnode : Node_Id;
515
516 begin
517 Ensure_Freeze_Node (T);
518 Fnode := Freeze_Node (T);
519
520 if No (Actions (Fnode)) then
521 Set_Actions (Fnode, New_List (N));
522 else
523 Append (N, Actions (Fnode));
524 end if;
525 end Append_Freeze_Action;
526
527 ---------------------------
528 -- Append_Freeze_Actions --
529 ---------------------------
530
531 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
532 Fnode : Node_Id;
533
534 begin
535 if No (L) then
536 return;
537 end if;
538
539 Ensure_Freeze_Node (T);
540 Fnode := Freeze_Node (T);
541
542 if No (Actions (Fnode)) then
543 Set_Actions (Fnode, L);
544 else
545 Append_List (L, Actions (Fnode));
546 end if;
547 end Append_Freeze_Actions;
548
549 ----------------------------------------
550 -- Attribute_Constrained_Static_Value --
551 ----------------------------------------
552
553 function Attribute_Constrained_Static_Value (Pref : Node_Id) return Boolean
554 is
555 Ptyp : constant Entity_Id := Etype (Pref);
556 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
557
558 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
559 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
560 -- view of an aliased object whose subtype is constrained.
561
562 ---------------------------------
563 -- Is_Constrained_Aliased_View --
564 ---------------------------------
565
566 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
567 E : Entity_Id;
568
569 begin
570 if Is_Entity_Name (Obj) then
571 E := Entity (Obj);
572
573 if Present (Renamed_Object (E)) then
574 return Is_Constrained_Aliased_View (Renamed_Object (E));
575 else
576 return Is_Aliased (E) and then Is_Constrained (Etype (E));
577 end if;
578
579 else
580 return Is_Aliased_View (Obj)
581 and then
582 (Is_Constrained (Etype (Obj))
583 or else
584 (Nkind (Obj) = N_Explicit_Dereference
585 and then
586 not Object_Type_Has_Constrained_Partial_View
587 (Typ => Base_Type (Etype (Obj)),
588 Scop => Current_Scope)));
589 end if;
590 end Is_Constrained_Aliased_View;
591
592 -- Start of processing for Attribute_Constrained_Static_Value
593
594 begin
595 -- We are in a case where the attribute is known statically, and
596 -- implicit dereferences have been rewritten.
597
598 pragma Assert
599 (not (Present (Formal_Ent)
600 and then Ekind (Formal_Ent) /= E_Constant
601 and then Present (Extra_Constrained (Formal_Ent)))
602 and then
603 not (Is_Access_Type (Etype (Pref))
604 and then (not Is_Entity_Name (Pref)
605 or else Is_Object (Entity (Pref))))
606 and then
607 not (Nkind (Pref) = N_Identifier
608 and then Ekind (Entity (Pref)) = E_Variable
609 and then Present (Extra_Constrained (Entity (Pref)))));
610
611 if Is_Entity_Name (Pref) then
612 declare
613 Ent : constant Entity_Id := Entity (Pref);
614 Res : Boolean;
615
616 begin
617 -- (RM J.4) obsolescent cases
618
619 if Is_Type (Ent) then
620
621 -- Private type
622
623 if Is_Private_Type (Ent) then
624 Res := not Has_Discriminants (Ent)
625 or else Is_Constrained (Ent);
626
627 -- It not a private type, must be a generic actual type
628 -- that corresponded to a private type. We know that this
629 -- correspondence holds, since otherwise the reference
630 -- within the generic template would have been illegal.
631
632 else
633 if Is_Composite_Type (Underlying_Type (Ent)) then
634 Res := Is_Constrained (Ent);
635 else
636 Res := True;
637 end if;
638 end if;
639
640 else
641
642 -- If the prefix is not a variable or is aliased, then
643 -- definitely true; if it's a formal parameter without an
644 -- associated extra formal, then treat it as constrained.
645
646 -- Ada 2005 (AI-363): An aliased prefix must be known to be
647 -- constrained in order to set the attribute to True.
648
649 if not Is_Variable (Pref)
650 or else Present (Formal_Ent)
651 or else (Ada_Version < Ada_2005
652 and then Is_Aliased_View (Pref))
653 or else (Ada_Version >= Ada_2005
654 and then Is_Constrained_Aliased_View (Pref))
655 then
656 Res := True;
657
658 -- Variable case, look at type to see if it is constrained.
659 -- Note that the one case where this is not accurate (the
660 -- procedure formal case), has been handled above.
661
662 -- We use the Underlying_Type here (and below) in case the
663 -- type is private without discriminants, but the full type
664 -- has discriminants. This case is illegal, but we generate
665 -- it internally for passing to the Extra_Constrained
666 -- parameter.
667
668 else
669 -- In Ada 2012, test for case of a limited tagged type,
670 -- in which case the attribute is always required to
671 -- return True. The underlying type is tested, to make
672 -- sure we also return True for cases where there is an
673 -- unconstrained object with an untagged limited partial
674 -- view which has defaulted discriminants (such objects
675 -- always produce a False in earlier versions of
676 -- Ada). (Ada 2012: AI05-0214)
677
678 Res :=
679 Is_Constrained (Underlying_Type (Etype (Ent)))
680 or else
681 (Ada_Version >= Ada_2012
682 and then Is_Tagged_Type (Underlying_Type (Ptyp))
683 and then Is_Limited_Type (Ptyp));
684 end if;
685 end if;
686
687 return Res;
688 end;
689
690 -- Prefix is not an entity name. These are also cases where we can
691 -- always tell at compile time by looking at the form and type of the
692 -- prefix. If an explicit dereference of an object with constrained
693 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
694 -- underlying type is a limited tagged type, then Constrained is
695 -- required to always return True (Ada 2012: AI05-0214).
696
697 else
698 return not Is_Variable (Pref)
699 or else
700 (Nkind (Pref) = N_Explicit_Dereference
701 and then
702 not Object_Type_Has_Constrained_Partial_View
703 (Typ => Base_Type (Ptyp),
704 Scop => Current_Scope))
705 or else Is_Constrained (Underlying_Type (Ptyp))
706 or else (Ada_Version >= Ada_2012
707 and then Is_Tagged_Type (Underlying_Type (Ptyp))
708 and then Is_Limited_Type (Ptyp));
709 end if;
710 end Attribute_Constrained_Static_Value;
711
712 ------------------------------------
713 -- Build_Allocate_Deallocate_Proc --
714 ------------------------------------
715
716 procedure Build_Allocate_Deallocate_Proc
717 (N : Node_Id;
718 Mark : Node_Id := Empty)
719 is
720 Is_Allocate : constant Boolean := Nkind (N) /= N_Free_Statement;
721
722 function Find_Object (E : Node_Id) return Node_Id;
723 -- Given an arbitrary expression of an allocator, try to find an object
724 -- reference in it, otherwise return the original expression.
725
726 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
727 -- Determine whether subprogram Subp denotes a custom allocate or
728 -- deallocate.
729
730 -----------------
731 -- Find_Object --
732 -----------------
733
734 function Find_Object (E : Node_Id) return Node_Id is
735 Expr : Node_Id;
736
737 begin
738 pragma Assert (Is_Allocate);
739
740 Expr := E;
741 loop
742 if Nkind (Expr) = N_Explicit_Dereference then
743 Expr := Prefix (Expr);
744
745 elsif Nkind (Expr) = N_Qualified_Expression then
746 Expr := Expression (Expr);
747
748 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
749
750 -- When interface class-wide types are involved in allocation,
751 -- the expander introduces several levels of address arithmetic
752 -- to perform dispatch table displacement. In this scenario the
753 -- object appears as:
754
755 -- Tag_Ptr (Base_Address (<object>'Address))
756
757 -- Detect this case and utilize the whole expression as the
758 -- "object" since it now points to the proper dispatch table.
759
760 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
761 exit;
762
763 -- Continue to strip the object
764
765 else
766 Expr := Expression (Expr);
767 end if;
768
769 else
770 exit;
771 end if;
772 end loop;
773
774 return Expr;
775 end Find_Object;
776
777 ---------------------------------
778 -- Is_Allocate_Deallocate_Proc --
779 ---------------------------------
780
781 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
782 begin
783 -- Look for a subprogram body with only one statement which is a
784 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
785
786 if Ekind (Subp) = E_Procedure
787 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
788 then
789 declare
790 HSS : constant Node_Id :=
791 Handled_Statement_Sequence (Parent (Parent (Subp)));
792 Proc : Entity_Id;
793
794 begin
795 if Present (Statements (HSS))
796 and then Nkind (First (Statements (HSS))) =
797 N_Procedure_Call_Statement
798 then
799 Proc := Entity (Name (First (Statements (HSS))));
800
801 return
802 Is_RTE (Proc, RE_Allocate_Any_Controlled)
803 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
804 end if;
805 end;
806 end if;
807
808 return False;
809 end Is_Allocate_Deallocate_Proc;
810
811 -- Local variables
812
813 Desig_Typ : Entity_Id;
814 Expr : Node_Id;
815 Needs_Fin : Boolean;
816 Pool_Id : Entity_Id;
817 Proc_To_Call : Node_Id;
818 Ptr_Typ : Entity_Id;
819 Use_Secondary_Stack_Pool : Boolean;
820
821 -- Start of processing for Build_Allocate_Deallocate_Proc
822
823 begin
824 -- Obtain the attributes of the allocation
825
826 if Is_Allocate then
827 if Nkind (N) in N_Assignment_Statement | N_Object_Declaration then
828 Expr := Expression (N);
829 else
830 Expr := N;
831 end if;
832
833 -- Deal with type conversions created for interface types
834
835 if Nkind (Expr) = N_Unchecked_Type_Conversion then
836 Expr := Expression (Expr);
837 end if;
838
839 -- In certain cases, an allocator with a qualified expression may be
840 -- relocated and used as the initialization expression of a temporary
841 -- and the analysis of the declaration of this temporary may in turn
842 -- create another temporary:
843
844 -- before:
845 -- Obj : Ptr_Typ := new Desig_Typ'(...);
846
847 -- after:
848 -- Tmp2 : Ptr_Typ := new Desig_Typ'(...);
849 -- [constraint_error when Tmp2...]
850 -- Tmp1 : Ptr_Typ := Tmp2
851 -- Obj : Ptr_Typ := Tmp1;
852
853 -- Detect this case where we are invoked on Tmp1's declaration by
854 -- recognizing Tmp2 and then proceed to its declaration instead.
855
856 if Nkind (Expr) = N_Identifier
857 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
858 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
859 then
860 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), Mark);
861 return;
862 end if;
863
864 pragma Assert (Nkind (Expr) = N_Allocator);
865
866 Ptr_Typ := Base_Type (Etype (Expr));
867 Proc_To_Call := Procedure_To_Call (Expr);
868
869 -- Obtain the attributes of the deallocation
870
871 else
872 Expr := Expression (N);
873 Ptr_Typ := Base_Type (Etype (Expr));
874 Proc_To_Call := Procedure_To_Call (N);
875 end if;
876
877 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
878 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
879
880 -- Handle concurrent types
881
882 if Is_Concurrent_Type (Desig_Typ)
883 and then Present (Corresponding_Record_Type (Desig_Typ))
884 then
885 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
886 end if;
887
888 Use_Secondary_Stack_Pool :=
889 Is_RTE (Pool_Id, RE_SS_Pool)
890 or else (Nkind (Expr) = N_Allocator
891 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool));
892
893 -- Do not process allocations / deallocations without a pool
894
895 if No (Pool_Id) then
896 return;
897
898 -- Do not process allocations from the return stack
899
900 elsif Is_RTE (Pool_Id, RE_RS_Pool) then
901 return;
902
903 -- Do not process allocations on / deallocations from the secondary
904 -- stack, except for access types used to implement indirect temps.
905
906 elsif Use_Secondary_Stack_Pool
907 and then not Old_Attr_Util.Indirect_Temps
908 .Is_Access_Type_For_Indirect_Temp (Ptr_Typ)
909 then
910 return;
911
912 -- Optimize the case where we are using the default Global_Pool_Object,
913 -- and we don't need the heavy finalization machinery.
914
915 elsif Is_RTE (Pool_Id, RE_Global_Pool_Object)
916 and then not Needs_Finalization (Desig_Typ)
917 then
918 return;
919
920 -- Do not replicate the machinery if the allocator / free has already
921 -- been expanded and has a custom Allocate / Deallocate.
922
923 elsif Present (Proc_To_Call)
924 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
925 then
926 return;
927 end if;
928
929 -- Finalization actions are required when the object to be allocated or
930 -- deallocated needs these actions and the associated access type is not
931 -- subject to pragma No_Heap_Finalization.
932
933 Needs_Fin :=
934 Needs_Finalization (Desig_Typ)
935 and then not Has_Relaxed_Finalization (Desig_Typ)
936 and then not No_Heap_Finalization (Ptr_Typ);
937
938 -- The allocation/deallocation of a controlled object must be associated
939 -- with an attachment to/detachment from a finalization collection, but
940 -- the implementation cannot guarantee this property for every anonymous
941 -- access type, see Build_Anonymous_Collection.
942
943 if Needs_Fin and then No (Finalization_Collection (Ptr_Typ)) then
944 pragma Assert (Ekind (Ptr_Typ) = E_Anonymous_Access_Type);
945 Needs_Fin := False;
946 end if;
947
948 if Needs_Fin then
949
950 -- Do nothing if the access type may never allocate / deallocate
951 -- objects.
952
953 if No_Pool_Assigned (Ptr_Typ) then
954 return;
955 end if;
956
957 -- The only other kind of allocation / deallocation supported by this
958 -- routine is on / from a subpool.
959
960 elsif Nkind (Expr) = N_Allocator
961 and then No (Subpool_Handle_Name (Expr))
962 then
963 return;
964 end if;
965
966 declare
967 Loc : constant Source_Ptr := Sloc (N);
968 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
969 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
970 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
971 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
972
973 Actuals : List_Id;
974 Alloc_Expr : Node_Id;
975 Fin_Coll_Id : Entity_Id;
976 Proc_To_Call : Entity_Id;
977 Ptr_Coll_Id : Entity_Id;
978 Subpool : Node_Id;
979
980 begin
981 -- When we are building an allocator procedure, extract the qualified
982 -- expression from the allocator if there is one.
983
984 if Is_Allocate
985 and then Nkind (Expression (Expr)) = N_Qualified_Expression
986 then
987 Alloc_Expr := Expression (Expr);
988 else
989 Alloc_Expr := Empty;
990 end if;
991
992 -- Step 1: Construct all the actuals for the call to library routine
993 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
994
995 -- a) Storage pool
996
997 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
998
999 if Is_Allocate then
1000
1001 -- b) Subpool
1002
1003 Subpool := Subpool_Handle_Name (Expr);
1004
1005 -- If a subpool is present it can be an arbitrary name, so make
1006 -- the actual by copying the tree.
1007
1008 if Present (Subpool) then
1009 Append_To
1010 (Actuals, New_Copy_Tree (Subpool, New_Scope => Proc_Id));
1011 else
1012 Append_To (Actuals, Make_Null (Loc));
1013 end if;
1014
1015 -- c) Finalization collection
1016
1017 Fin_Coll_Id := Make_Temporary (Loc, 'C');
1018 Ptr_Coll_Id := Finalization_Collection (Ptr_Typ);
1019
1020 -- Create the temporary which represents the collection of
1021 -- the expression. Generate:
1022 --
1023 -- C : Finalization_Collection_Ptr :=
1024 -- Finalization_Collection (Ptr_Typ)'Access
1025 --
1026 -- Handle the case where a collection is actually a pointer
1027 -- to a collection. This arises in build-in-place functions.
1028
1029 Insert_Action (N,
1030 Make_Object_Declaration (Loc,
1031 Defining_Identifier => Fin_Coll_Id,
1032 Object_Definition =>
1033 New_Occurrence_Of
1034 (RTE (RE_Finalization_Collection_Ptr), Loc),
1035 Expression =>
1036 (if not Needs_Fin
1037 then Make_Null (Loc)
1038 elsif Is_Access_Type (Etype (Ptr_Coll_Id))
1039 then New_Occurrence_Of (Ptr_Coll_Id, Loc)
1040 else
1041 Make_Attribute_Reference (Loc,
1042 Prefix =>
1043 New_Occurrence_Of (Ptr_Coll_Id, Loc),
1044 Attribute_Name => Name_Unrestricted_Access))));
1045
1046 Append_To (Actuals, New_Occurrence_Of (Fin_Coll_Id, Loc));
1047 end if;
1048
1049 -- d) Address
1050 -- e) Storage_Size
1051 -- f) Alignment
1052
1053 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
1054 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
1055
1056 -- Class-wide allocations without expressions and non-class-wide
1057 -- allocations can be performed without getting the alignment from
1058 -- the type's Type Specific Record.
1059
1060 if (Is_Allocate and then No (Alloc_Expr))
1061 or else not Is_Class_Wide_Type (Desig_Typ)
1062 then
1063 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
1064
1065 -- For operations on class-wide types we obtain the value of
1066 -- alignment from the Type Specific Record of the relevant object.
1067 -- This is needed because the frontend expansion of class-wide types
1068 -- into equivalent types confuses the back end.
1069
1070 else
1071 -- Generate:
1072 -- Obj.all'Alignment
1073 -- or
1074 -- Alloc_Expr'Alignment
1075
1076 -- ... because 'Alignment applied to class-wide types is expanded
1077 -- into the code that reads the value of alignment from the TSD
1078 -- (see Expand_N_Attribute_Reference)
1079
1080 Append_To (Actuals,
1081 Unchecked_Convert_To (RTE (RE_Storage_Offset),
1082 Make_Attribute_Reference (Loc,
1083 Prefix =>
1084 (if Is_Allocate then
1085 Duplicate_Subexpr_No_Checks
1086 (Expression (Alloc_Expr), New_Scope => Proc_Id)
1087 else
1088 Make_Explicit_Dereference (Loc,
1089 Duplicate_Subexpr_No_Checks
1090 (Expr, New_Scope => Proc_Id))),
1091 Attribute_Name => Name_Alignment)));
1092 end if;
1093
1094 -- g) Is_Controlled
1095
1096 if Needs_Fin then
1097 Is_Controlled : declare
1098 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
1099
1100 Flag_Expr : Node_Id;
1101 Param : Node_Id;
1102 Temp : Node_Id;
1103
1104 begin
1105 if Is_Allocate then
1106 Temp := Find_Object (Expression (Expr));
1107 else
1108 Temp := Expr;
1109 end if;
1110
1111 -- Processing for allocations where the expression is a subtype
1112 -- indication.
1113
1114 if Is_Allocate
1115 and then Is_Entity_Name (Temp)
1116 and then Is_Type (Entity (Temp))
1117 then
1118 Flag_Expr :=
1119 New_Occurrence_Of
1120 (Boolean_Literals
1121 (Needs_Finalization (Entity (Temp))), Loc);
1122
1123 -- The allocation / deallocation of a class-wide object relies
1124 -- on a runtime check to determine whether the object is truly
1125 -- controlled or not. Depending on this check, the finalization
1126 -- machinery will request or reclaim extra storage reserved for
1127 -- a list header.
1128
1129 elsif Is_Class_Wide_Type (Desig_Typ) then
1130
1131 -- Detect a special case where interface class-wide types
1132 -- are involved as the object appears as:
1133
1134 -- Tag_Ptr (Base_Address (<object>'Address))
1135
1136 -- The expression already yields the proper tag, generate:
1137
1138 -- Temp.all
1139
1140 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
1141 Param :=
1142 Make_Explicit_Dereference (Loc,
1143 Prefix =>
1144 Duplicate_Subexpr_No_Checks
1145 (Temp, New_Scope => Proc_Id));
1146
1147 -- In the default case, obtain the tag of the object about
1148 -- to be allocated / deallocated. Generate:
1149
1150 -- Temp'Tag
1151
1152 -- If the object is an unchecked conversion (typically to
1153 -- an access to class-wide type), we must preserve the
1154 -- conversion to ensure that the object is seen as tagged
1155 -- in the code that follows.
1156
1157 else
1158 if Nkind (Parent (Temp)) = N_Unchecked_Type_Conversion
1159 then
1160 Temp := Parent (Temp);
1161 end if;
1162
1163 Param :=
1164 Make_Attribute_Reference (Loc,
1165 Prefix =>
1166 Duplicate_Subexpr_No_Checks
1167 (Temp, New_Scope => Proc_Id),
1168 Attribute_Name => Name_Tag);
1169 end if;
1170
1171 -- Generate:
1172 -- Needs_Finalization (<Param>)
1173
1174 Flag_Expr :=
1175 Make_Function_Call (Loc,
1176 Name =>
1177 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
1178 Parameter_Associations => New_List (Param));
1179
1180 -- Processing for generic actuals
1181
1182 elsif Is_Generic_Actual_Type (Desig_Typ) then
1183 Flag_Expr :=
1184 New_Occurrence_Of (Boolean_Literals
1185 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
1186
1187 -- The object does not require any specialized checks, it is
1188 -- known to be controlled.
1189
1190 else
1191 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
1192 end if;
1193
1194 -- Create the temporary which represents the finalization state
1195 -- of the expression. Generate:
1196 --
1197 -- F : constant Boolean := <Flag_Expr>;
1198
1199 Insert_Action (N,
1200 Make_Object_Declaration (Loc,
1201 Defining_Identifier => Flag_Id,
1202 Constant_Present => True,
1203 Object_Definition =>
1204 New_Occurrence_Of (Standard_Boolean, Loc),
1205 Expression => Flag_Expr));
1206
1207 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
1208
1209 -- Finalize_Address is not generated in CodePeer mode because
1210 -- the body contains address arithmetic. So we don't want to
1211 -- generate the attach or detach in this case.
1212
1213 if CodePeer_Mode then
1214 null;
1215
1216 -- Nothing to generate if the flag is statically false
1217
1218 elsif Is_Entity_Name (Flag_Expr)
1219 and then Entity (Flag_Expr) = Standard_False
1220 then
1221 null;
1222
1223 -- Generate:
1224 -- if F then
1225 -- Attach_Object_To_Collection
1226 -- (Temp.all'Address,
1227 -- Desig_Typ_FD'Access,
1228 -- Fin_Coll_Id.all);
1229 -- end if;
1230
1231 elsif Is_Allocate then
1232 declare
1233 Stmt : Node_Id;
1234 Temp : Entity_Id;
1235
1236 begin
1237 -- The original allocator must have been rewritten by
1238 -- the caller at this point and a temporary introduced.
1239
1240 case Nkind (N) is
1241 when N_Assignment_Statement =>
1242 Temp := New_Copy_Tree (Name (N));
1243
1244 when N_Object_Declaration =>
1245 Temp :=
1246 New_Occurrence_Of (Defining_Identifier (N), Loc);
1247
1248 when others =>
1249 raise Program_Error;
1250 end case;
1251
1252 Stmt :=
1253 Make_If_Statement (Loc,
1254 Condition =>
1255 New_Occurrence_Of (Flag_Id, Loc),
1256 Then_Statements => New_List (
1257 Make_Procedure_Call_Statement (Loc,
1258 Name =>
1259 New_Occurrence_Of
1260 (RTE (RE_Attach_Object_To_Collection), Loc),
1261 Parameter_Associations => New_List (
1262 Make_Address_For_Finalize (Loc,
1263 Make_Explicit_Dereference (Loc, Temp),
1264 Desig_Typ),
1265 Make_Attribute_Reference (Loc,
1266 Prefix =>
1267 New_Occurrence_Of
1268 (Finalize_Address (Desig_Typ), Loc),
1269 Attribute_Name => Name_Unrestricted_Access),
1270 Make_Explicit_Dereference (Loc,
1271 New_Occurrence_Of (Fin_Coll_Id, Loc))))));
1272
1273 -- If we have a mark past the initialization, then insert
1274 -- the statement there, otherwise insert after either the
1275 -- assignment or the last initialization statement of the
1276 -- declaration of the temporary.
1277
1278 if Present (Mark) then
1279 Insert_Action (Mark, Stmt, Suppress => All_Checks);
1280
1281 elsif Nkind (N) = N_Assignment_Statement then
1282 Insert_After_And_Analyze
1283 (N, Stmt, Suppress => All_Checks);
1284
1285 else
1286 Insert_After_And_Analyze
1287 (Find_Last_Init (N), Stmt, Suppress => All_Checks);
1288 end if;
1289 end;
1290
1291 -- Nothing to generate for the cleanup of an allocator
1292
1293 elsif For_Allocator (N) then
1294 null;
1295
1296 -- Generate:
1297 -- if F then
1298 -- Detach_Object_From_Collection (Temp.all'Address);
1299 -- end if;
1300
1301 else
1302 Insert_Action (N,
1303 Make_If_Statement (Loc,
1304 Condition => New_Occurrence_Of (Flag_Id, Loc),
1305 Then_Statements => New_List (
1306 Make_Procedure_Call_Statement (Loc,
1307 Name =>
1308 New_Occurrence_Of
1309 (RTE (RE_Detach_Object_From_Collection), Loc),
1310 Parameter_Associations => New_List (
1311 Make_Address_For_Finalize (Loc,
1312 Make_Explicit_Dereference (Loc,
1313 New_Occurrence_Of
1314 (Entity (Expression (N)), Loc)),
1315 Desig_Typ))))),
1316 Suppress => All_Checks);
1317 end if;
1318
1319 end Is_Controlled;
1320
1321 -- The object is not controlled
1322
1323 else
1324 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
1325 end if;
1326
1327 -- h) On_Subpool
1328
1329 if Is_Allocate then
1330 Append_To (Actuals,
1331 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
1332 end if;
1333
1334 -- Step 2: Build a wrapper Allocate / Deallocate which internally
1335 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
1336
1337 -- Select the proper routine to call
1338
1339 if Is_Allocate then
1340 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
1341 else
1342 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
1343 end if;
1344
1345 -- Create a custom Allocate/Deallocate routine which has identical
1346 -- profile to that of System.Storage_Pools, except for a secondary
1347 -- stack allocation where the profile must be identical to that of
1348 -- the System.Secondary_Stack.SS_Allocate procedure (deallocation
1349 -- is not supported for the secondary stack).
1350
1351 declare
1352 function Pool_Param return Node_Id is (
1353 Make_Parameter_Specification (Loc,
1354 Defining_Identifier => Make_Temporary (Loc, 'P'),
1355 Parameter_Type =>
1356 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)));
1357 -- P : Root_Storage_Pool
1358
1359 function Address_Param return Node_Id is (
1360 Make_Parameter_Specification (Loc,
1361 Defining_Identifier => Addr_Id,
1362 Out_Present => Is_Allocate,
1363 Parameter_Type =>
1364 New_Occurrence_Of (RTE (RE_Address), Loc)));
1365 -- A : [out] Address
1366
1367 function Size_Param return Node_Id is (
1368 Make_Parameter_Specification (Loc,
1369 Defining_Identifier => Size_Id,
1370 Parameter_Type =>
1371 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1372 -- S : Storage_Count
1373
1374 function Alignment_Param return Node_Id is (
1375 Make_Parameter_Specification (Loc,
1376 Defining_Identifier => Alig_Id,
1377 Parameter_Type =>
1378 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1379 -- L : Storage_Count
1380
1381 Formal_Params : constant List_Id :=
1382 (if Use_Secondary_Stack_Pool
1383 then New_List (Address_Param, Size_Param, Alignment_Param)
1384 else
1385 New_List
1386 (Pool_Param, Address_Param, Size_Param, Alignment_Param));
1387 -- The list of formal parameters of the routine
1388
1389 begin
1390 Insert_Action (N,
1391 Make_Subprogram_Body (Loc,
1392 Specification =>
1393 -- procedure Pnn
1394 Make_Procedure_Specification (Loc,
1395 Defining_Unit_Name => Proc_Id,
1396 Parameter_Specifications => Formal_Params),
1397
1398 Declarations => No_List,
1399
1400 Handled_Statement_Sequence =>
1401 Make_Handled_Sequence_Of_Statements (Loc,
1402 Statements => New_List (
1403 Make_Procedure_Call_Statement (Loc,
1404 Name =>
1405 New_Occurrence_Of (Proc_To_Call, Loc),
1406 Parameter_Associations => Actuals)))),
1407 Suppress => All_Checks);
1408 end;
1409
1410 -- The newly generated Allocate / Deallocate becomes the default
1411 -- procedure to call when the back end processes the allocation /
1412 -- deallocation.
1413
1414 if Is_Allocate then
1415 Set_Procedure_To_Call (Expr, Proc_Id);
1416 else
1417 Set_Procedure_To_Call (N, Proc_Id);
1418 end if;
1419 end;
1420 end Build_Allocate_Deallocate_Proc;
1421
1422 -------------------------------
1423 -- Build_Abort_Undefer_Block --
1424 -------------------------------
1425
1426 function Build_Abort_Undefer_Block
1427 (Loc : Source_Ptr;
1428 Stmts : List_Id;
1429 Context : Node_Id) return Node_Id
1430 is
1431 Exceptions_OK : constant Boolean :=
1432 not Restriction_Active (No_Exception_Propagation);
1433
1434 AUD : Entity_Id;
1435 Blk : Node_Id;
1436 Blk_Id : Entity_Id;
1437 HSS : Node_Id;
1438
1439 begin
1440 -- The block should be generated only when undeferring abort in the
1441 -- context of a potential exception.
1442
1443 pragma Assert (Abort_Allowed and Exceptions_OK);
1444
1445 -- Generate:
1446 -- begin
1447 -- <Stmts>
1448 -- at end
1449 -- Abort_Undefer_Direct;
1450 -- end;
1451
1452 AUD := RTE (RE_Abort_Undefer_Direct);
1453
1454 HSS :=
1455 Make_Handled_Sequence_Of_Statements (Loc,
1456 Statements => Stmts,
1457 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1458
1459 Blk :=
1460 Make_Block_Statement (Loc,
1461 Handled_Statement_Sequence => HSS);
1462 Set_Is_Abort_Block (Blk);
1463
1464 Add_Block_Identifier (Blk, Blk_Id);
1465 Expand_At_End_Handler (HSS, Blk_Id);
1466
1467 -- Present the Abort_Undefer_Direct function to the back end to inline
1468 -- the call to the routine.
1469
1470 Add_Inlined_Body (AUD, Context);
1471
1472 return Blk;
1473 end Build_Abort_Undefer_Block;
1474
1475 ---------------------------------
1476 -- Build_Class_Wide_Expression --
1477 ---------------------------------
1478
1479 procedure Build_Class_Wide_Expression
1480 (Pragma_Or_Expr : Node_Id;
1481 Subp : Entity_Id;
1482 Par_Subp : Entity_Id;
1483 Adjust_Sloc : Boolean)
1484 is
1485 function Replace_Entity (N : Node_Id) return Traverse_Result;
1486 -- Replace reference to formal of inherited operation or to primitive
1487 -- operation of root type, with corresponding entity for derived type,
1488 -- when constructing the class-wide condition of an overriding
1489 -- subprogram.
1490
1491 --------------------
1492 -- Replace_Entity --
1493 --------------------
1494
1495 function Replace_Entity (N : Node_Id) return Traverse_Result is
1496 New_E : Entity_Id;
1497
1498 begin
1499 if Adjust_Sloc then
1500 Adjust_Inherited_Pragma_Sloc (N);
1501 end if;
1502
1503 if Nkind (N) in N_Identifier | N_Expanded_Name | N_Operator_Symbol
1504 and then Present (Entity (N))
1505 and then
1506 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1507 and then
1508 (Nkind (Parent (N)) /= N_Attribute_Reference
1509 or else Attribute_Name (Parent (N)) /= Name_Class)
1510 then
1511 -- The replacement does not apply to dispatching calls within the
1512 -- condition, but only to calls whose static tag is that of the
1513 -- parent type.
1514
1515 if Is_Subprogram (Entity (N))
1516 and then Nkind (Parent (N)) = N_Function_Call
1517 and then Present (Controlling_Argument (Parent (N)))
1518 then
1519 return OK;
1520 end if;
1521
1522 -- Determine whether entity has a renaming
1523
1524 New_E := Type_Map.Get (Entity (N));
1525
1526 if Present (New_E) then
1527 declare
1528
1529 Ctrl_Type : constant Entity_Id
1530 := Find_Dispatching_Type (Par_Subp);
1531
1532 function Call_To_Parent_Dispatching_Op_Must_Be_Mapped
1533 (Call_Node : Node_Id) return Boolean;
1534 -- If Call_Node is a call to a primitive function F of the
1535 -- tagged type T associated with Par_Subp that either has
1536 -- any actuals that are controlling formals of Par_Subp,
1537 -- or else the call to F is an actual parameter of an
1538 -- enclosing call to a primitive of T that has any actuals
1539 -- that are controlling formals of Par_Subp (and recursively
1540 -- up the tree of enclosing function calls), returns True;
1541 -- otherwise returns False. Returning True implies that the
1542 -- call to F must be mapped to a call that instead targets
1543 -- the corresponding function F of the tagged type for which
1544 -- Subp is a primitive function.
1545
1546 --------------------------------------------------
1547 -- Call_To_Parent_Dispatching_Op_Must_Be_Mapped --
1548 --------------------------------------------------
1549
1550 function Call_To_Parent_Dispatching_Op_Must_Be_Mapped
1551 (Call_Node : Node_Id) return Boolean
1552 is
1553 pragma Assert (Nkind (Call_Node) = N_Function_Call);
1554
1555 Actual : Node_Id := First_Actual (Call_Node);
1556 Actual_Type : Entity_Id;
1557 Actual_Or_Prefix : Node_Id;
1558
1559 begin
1560 if Is_Entity_Name (Name (Call_Node))
1561 and then Is_Dispatching_Operation
1562 (Entity (Name (Call_Node)))
1563 and then
1564 Is_Ancestor
1565 (Ctrl_Type,
1566 Find_Dispatching_Type
1567 (Entity (Name (Call_Node))))
1568 then
1569 while Present (Actual) loop
1570
1571 -- Account for 'Old and explicit dereferences,
1572 -- picking up the prefix object in those cases.
1573
1574 if (Nkind (Actual) = N_Attribute_Reference
1575 and then Attribute_Name (Actual) = Name_Old)
1576 or else Nkind (Actual) = N_Explicit_Dereference
1577 then
1578 Actual_Or_Prefix := Prefix (Actual);
1579 else
1580 Actual_Or_Prefix := Actual;
1581 end if;
1582
1583 Actual_Type := Etype (Actual);
1584
1585 if Is_Anonymous_Access_Type (Actual_Type) then
1586 Actual_Type := Designated_Type (Actual_Type);
1587 end if;
1588
1589 if Nkind (Actual_Or_Prefix)
1590 in N_Identifier
1591 | N_Expanded_Name
1592 | N_Operator_Symbol
1593
1594 and then Is_Formal (Entity (Actual_Or_Prefix))
1595
1596 and then Covers (Ctrl_Type, Actual_Type)
1597 then
1598 -- At least one actual is a formal parameter of
1599 -- Par_Subp with type Ctrl_Type.
1600
1601 return True;
1602 end if;
1603
1604 Next_Actual (Actual);
1605 end loop;
1606
1607 if Nkind (Parent (Call_Node)) = N_Function_Call then
1608 return
1609 Call_To_Parent_Dispatching_Op_Must_Be_Mapped
1610 (Parent (Call_Node));
1611 end if;
1612
1613 return False;
1614
1615 else
1616 return False;
1617 end if;
1618 end Call_To_Parent_Dispatching_Op_Must_Be_Mapped;
1619
1620 begin
1621 -- If N's entity is in the map, then the entity is either
1622 -- a formal of the parent subprogram that should necessarily
1623 -- be mapped, or it's a function call's target entity that
1624 -- that should be mapped if the call involves any actuals
1625 -- that reference formals of the parent subprogram (or the
1626 -- function call is part of an enclosing call that similarly
1627 -- qualifies for mapping). Rewrite a node that references
1628 -- any such qualified entity to a new node referencing the
1629 -- corresponding entity associated with the derived type.
1630
1631 if not Is_Subprogram (Entity (N))
1632 or else Nkind (Parent (N)) /= N_Function_Call
1633 or else
1634 Call_To_Parent_Dispatching_Op_Must_Be_Mapped (Parent (N))
1635 then
1636 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1637 end if;
1638 end;
1639 end if;
1640
1641 -- Update type of function call node, which should be the same as
1642 -- the function's return type.
1643
1644 if Is_Subprogram (Entity (N))
1645 and then Nkind (Parent (N)) = N_Function_Call
1646 then
1647 Set_Etype (Parent (N), Etype (Entity (N)));
1648 end if;
1649
1650 -- The whole expression will be reanalyzed
1651
1652 elsif Nkind (N) in N_Has_Etype then
1653 Set_Analyzed (N, False);
1654 end if;
1655
1656 return OK;
1657 end Replace_Entity;
1658
1659 procedure Replace_Condition_Entities is
1660 new Traverse_Proc (Replace_Entity);
1661
1662 -- Local variables
1663
1664 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Subp);
1665 Subp_Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1666
1667 -- Start of processing for Build_Class_Wide_Expression
1668
1669 begin
1670 pragma Assert (Par_Typ /= Subp_Typ);
1671
1672 Update_Primitives_Mapping (Par_Subp, Subp);
1673 Map_Formals (Par_Subp, Subp);
1674 Replace_Condition_Entities (Pragma_Or_Expr);
1675 end Build_Class_Wide_Expression;
1676
1677 --------------------
1678 -- Build_DIC_Call --
1679 --------------------
1680
1681 function Build_DIC_Call
1682 (Loc : Source_Ptr;
1683 Obj_Name : Node_Id;
1684 Typ : Entity_Id) return Node_Id
1685 is
1686 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1687 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1688
1689 begin
1690 -- The DIC procedure has a null body if assertions are disabled or
1691 -- Assertion_Policy Ignore is in effect. In that case, it would be
1692 -- nice to generate a null statement instead of a call to the DIC
1693 -- procedure, but doing that seems to interfere with the determination
1694 -- of ECRs (early call regions) in SPARK. ???
1695
1696 return
1697 Make_Procedure_Call_Statement (Loc,
1698 Name => New_Occurrence_Of (Proc_Id, Loc),
1699 Parameter_Associations => New_List (
1700 Unchecked_Convert_To (Formal_Typ, Obj_Name)));
1701 end Build_DIC_Call;
1702
1703 ------------------------------
1704 -- Build_DIC_Procedure_Body --
1705 ------------------------------
1706
1707 -- WARNING: This routine manages Ghost regions. Return statements must be
1708 -- replaced by gotos which jump to the end of the routine and restore the
1709 -- Ghost mode.
1710
1711 procedure Build_DIC_Procedure_Body
1712 (Typ : Entity_Id;
1713 Partial_DIC : Boolean := False)
1714 is
1715 Pragmas_Seen : Elist_Id := No_Elist;
1716 -- This list contains all DIC pragmas processed so far. The list is used
1717 -- to avoid redundant Default_Initial_Condition checks.
1718
1719 procedure Add_DIC_Check
1720 (DIC_Prag : Node_Id;
1721 DIC_Expr : Node_Id;
1722 Stmts : in out List_Id);
1723 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1724 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1725 -- is added to list Stmts.
1726
1727 procedure Add_Inherited_DIC
1728 (DIC_Prag : Node_Id;
1729 Par_Typ : Entity_Id;
1730 Deriv_Typ : Entity_Id;
1731 Stmts : in out List_Id);
1732 -- Add a runtime check to verify the assertion expression of inherited
1733 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1734 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1735 -- pragma. All generated code is added to list Stmts.
1736
1737 procedure Add_Inherited_Tagged_DIC
1738 (DIC_Prag : Node_Id;
1739 Expr : Node_Id;
1740 Stmts : in out List_Id);
1741 -- Add a runtime check to verify assertion expression DIC_Expr of
1742 -- inherited pragma DIC_Prag. This routine applies class-wide pre-
1743 -- and postcondition-like runtime semantics to the check. Expr is
1744 -- the assertion expression after substitution has been performed
1745 -- (via Replace_References). All generated code is added to list Stmts.
1746
1747 procedure Add_Inherited_DICs
1748 (T : Entity_Id;
1749 Priv_Typ : Entity_Id;
1750 Full_Typ : Entity_Id;
1751 Obj_Id : Entity_Id;
1752 Checks : in out List_Id);
1753 -- Generate a DIC check for each inherited Default_Initial_Condition
1754 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
1755 -- the partial and full view of the parent type. Obj_Id denotes the
1756 -- entity of the _object formal parameter of the DIC procedure. All
1757 -- created checks are added to list Checks.
1758
1759 procedure Add_Own_DIC
1760 (DIC_Prag : Node_Id;
1761 DIC_Typ : Entity_Id;
1762 Obj_Id : Entity_Id;
1763 Stmts : in out List_Id);
1764 -- Add a runtime check to verify the assertion expression of pragma
1765 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. Obj_Id is the
1766 -- object to substitute in the assertion expression for any references
1767 -- to the current instance of the type All generated code is added to
1768 -- list Stmts.
1769
1770 procedure Add_Parent_DICs
1771 (T : Entity_Id;
1772 Obj_Id : Entity_Id;
1773 Checks : in out List_Id);
1774 -- Generate a Default_Initial_Condition check for each inherited DIC
1775 -- aspect coming from all parent types of type T. Obj_Id denotes the
1776 -- entity of the _object formal parameter of the DIC procedure. All
1777 -- created checks are added to list Checks.
1778
1779 -------------------
1780 -- Add_DIC_Check --
1781 -------------------
1782
1783 procedure Add_DIC_Check
1784 (DIC_Prag : Node_Id;
1785 DIC_Expr : Node_Id;
1786 Stmts : in out List_Id)
1787 is
1788 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1789 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1790
1791 begin
1792 -- The DIC pragma is ignored, nothing left to do
1793
1794 if Is_Ignored (DIC_Prag) then
1795 null;
1796
1797 -- Otherwise the DIC expression must be checked at run time.
1798 -- Generate:
1799
1800 -- pragma Check (<Nam>, <DIC_Expr>);
1801
1802 else
1803 Append_New_To (Stmts,
1804 Make_Pragma (Loc,
1805 Pragma_Identifier =>
1806 Make_Identifier (Loc, Name_Check),
1807
1808 Pragma_Argument_Associations => New_List (
1809 Make_Pragma_Argument_Association (Loc,
1810 Expression => Make_Identifier (Loc, Nam)),
1811
1812 Make_Pragma_Argument_Association (Loc,
1813 Expression => DIC_Expr))));
1814 end if;
1815
1816 -- Add the pragma to the list of processed pragmas
1817
1818 Append_New_Elmt (DIC_Prag, Pragmas_Seen);
1819 end Add_DIC_Check;
1820
1821 -----------------------
1822 -- Add_Inherited_DIC --
1823 -----------------------
1824
1825 procedure Add_Inherited_DIC
1826 (DIC_Prag : Node_Id;
1827 Par_Typ : Entity_Id;
1828 Deriv_Typ : Entity_Id;
1829 Stmts : in out List_Id)
1830 is
1831 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1832 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1833 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1834 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1835 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1836
1837 begin
1838 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1839
1840 -- Verify the inherited DIC assertion expression by calling the DIC
1841 -- procedure of the parent type.
1842
1843 -- Generate:
1844 -- <Par_Typ>DIC (Par_Typ (_object));
1845
1846 Append_New_To (Stmts,
1847 Make_Procedure_Call_Statement (Loc,
1848 Name => New_Occurrence_Of (Par_Proc, Loc),
1849 Parameter_Associations => New_List (
1850 Convert_To
1851 (Typ => Etype (Par_Obj),
1852 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1853 end Add_Inherited_DIC;
1854
1855 ------------------------------
1856 -- Add_Inherited_Tagged_DIC --
1857 ------------------------------
1858
1859 procedure Add_Inherited_Tagged_DIC
1860 (DIC_Prag : Node_Id;
1861 Expr : Node_Id;
1862 Stmts : in out List_Id)
1863 is
1864 begin
1865 -- Once the DIC assertion expression is fully processed, add a check
1866 -- to the statements of the DIC procedure.
1867
1868 Add_DIC_Check
1869 (DIC_Prag => DIC_Prag,
1870 DIC_Expr => Expr,
1871 Stmts => Stmts);
1872 end Add_Inherited_Tagged_DIC;
1873
1874 ------------------------
1875 -- Add_Inherited_DICs --
1876 ------------------------
1877
1878 procedure Add_Inherited_DICs
1879 (T : Entity_Id;
1880 Priv_Typ : Entity_Id;
1881 Full_Typ : Entity_Id;
1882 Obj_Id : Entity_Id;
1883 Checks : in out List_Id)
1884 is
1885 Deriv_Typ : Entity_Id;
1886 Expr : Node_Id;
1887 Prag : Node_Id;
1888 Prag_Expr : Node_Id;
1889 Prag_Expr_Arg : Node_Id;
1890 Prag_Typ : Node_Id;
1891 Prag_Typ_Arg : Node_Id;
1892
1893 Par_Proc : Entity_Id;
1894 -- The "partial" invariant procedure of Par_Typ
1895
1896 Par_Typ : Entity_Id;
1897 -- The suitable view of the parent type used in the substitution of
1898 -- type attributes.
1899
1900 begin
1901 if No (Priv_Typ) and then No (Full_Typ) then
1902 return;
1903 end if;
1904
1905 -- When the type inheriting the class-wide invariant is a concurrent
1906 -- type, use the corresponding record type because it contains all
1907 -- primitive operations of the concurrent type and allows for proper
1908 -- substitution.
1909
1910 if Is_Concurrent_Type (T) then
1911 Deriv_Typ := Corresponding_Record_Type (T);
1912 else
1913 Deriv_Typ := T;
1914 end if;
1915
1916 pragma Assert (Present (Deriv_Typ));
1917
1918 -- Determine which rep item chain to use. Precedence is given to that
1919 -- of the parent type's partial view since it usually carries all the
1920 -- class-wide invariants.
1921
1922 if Present (Priv_Typ) then
1923 Prag := First_Rep_Item (Priv_Typ);
1924 else
1925 Prag := First_Rep_Item (Full_Typ);
1926 end if;
1927
1928 while Present (Prag) loop
1929 if Nkind (Prag) = N_Pragma
1930 and then Pragma_Name (Prag) = Name_Default_Initial_Condition
1931 then
1932 -- Nothing to do if the pragma was already processed
1933
1934 if Contains (Pragmas_Seen, Prag) then
1935 return;
1936 end if;
1937
1938 -- Extract arguments of the Default_Initial_Condition pragma
1939
1940 Prag_Expr_Arg := First (Pragma_Argument_Associations (Prag));
1941 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
1942
1943 -- Pick up the implicit second argument of the pragma, which
1944 -- indicates the type that the pragma applies to.
1945
1946 Prag_Typ_Arg := Next (Prag_Expr_Arg);
1947 if Present (Prag_Typ_Arg) then
1948 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
1949 else
1950 Prag_Typ := Empty;
1951 end if;
1952
1953 -- The pragma applies to the partial view of the parent type
1954
1955 if Present (Priv_Typ)
1956 and then Present (Prag_Typ)
1957 and then Entity (Prag_Typ) = Priv_Typ
1958 then
1959 Par_Typ := Priv_Typ;
1960
1961 -- The pragma applies to the full view of the parent type
1962
1963 elsif Present (Full_Typ)
1964 and then Present (Prag_Typ)
1965 and then Entity (Prag_Typ) = Full_Typ
1966 then
1967 Par_Typ := Full_Typ;
1968
1969 -- Otherwise the pragma does not belong to the parent type and
1970 -- should not be considered.
1971
1972 else
1973 return;
1974 end if;
1975
1976 -- Substitute references in the DIC expression that are related
1977 -- to the partial type with corresponding references related to
1978 -- the derived type (call to Replace_References below).
1979
1980 Expr := New_Copy_Tree (Prag_Expr);
1981
1982 Par_Proc := Partial_DIC_Procedure (Par_Typ);
1983
1984 -- If there's not a partial DIC procedure (such as when a
1985 -- full type doesn't have its own DIC, but is inherited from
1986 -- a type with DIC), get the full DIC procedure.
1987
1988 if No (Par_Proc) then
1989 Par_Proc := DIC_Procedure (Par_Typ);
1990 end if;
1991
1992 Replace_References
1993 (Expr => Expr,
1994 Par_Typ => Par_Typ,
1995 Deriv_Typ => Deriv_Typ,
1996 Par_Obj => First_Formal (Par_Proc),
1997 Deriv_Obj => Obj_Id);
1998
1999 -- Why are there different actions depending on whether T is
2000 -- tagged? Can these be unified? ???
2001
2002 if Is_Tagged_Type (T) then
2003 Add_Inherited_Tagged_DIC
2004 (DIC_Prag => Prag,
2005 Expr => Expr,
2006 Stmts => Checks);
2007
2008 else
2009 Add_Inherited_DIC
2010 (DIC_Prag => Prag,
2011 Par_Typ => Par_Typ,
2012 Deriv_Typ => Deriv_Typ,
2013 Stmts => Checks);
2014 end if;
2015
2016 -- Leave as soon as we get a DIC pragma, since we'll visit
2017 -- the pragmas of the parents, so will get to any "inherited"
2018 -- pragmas that way.
2019
2020 return;
2021 end if;
2022
2023 Next_Rep_Item (Prag);
2024 end loop;
2025 end Add_Inherited_DICs;
2026
2027 -----------------
2028 -- Add_Own_DIC --
2029 -----------------
2030
2031 procedure Add_Own_DIC
2032 (DIC_Prag : Node_Id;
2033 DIC_Typ : Entity_Id;
2034 Obj_Id : Entity_Id;
2035 Stmts : in out List_Id)
2036 is
2037 DIC_Args : constant List_Id :=
2038 Pragma_Argument_Associations (DIC_Prag);
2039 DIC_Arg : constant Node_Id := First (DIC_Args);
2040 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
2041 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
2042
2043 -- Local variables
2044
2045 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
2046
2047 Expr : Node_Id;
2048
2049 -- Start of processing for Add_Own_DIC
2050
2051 begin
2052 pragma Assert (Present (DIC_Expr));
2053
2054 -- We need to preanalyze the expression itself inside a generic to
2055 -- be able to capture global references present in it.
2056
2057 if Inside_A_Generic then
2058 Expr := DIC_Expr;
2059 else
2060 Expr := New_Copy_Tree (DIC_Expr);
2061 end if;
2062
2063 -- Perform the following substitution:
2064
2065 -- * Replace the current instance of DIC_Typ with a reference to
2066 -- the _object formal parameter of the DIC procedure.
2067
2068 Replace_Type_References
2069 (Expr => Expr,
2070 Typ => DIC_Typ,
2071 Obj_Id => Obj_Id);
2072
2073 -- Preanalyze the DIC expression to detect errors and at the same
2074 -- time capture the visibility of the proper package part.
2075
2076 Set_Parent (Expr, Typ_Decl);
2077 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2078
2079 -- Save a copy of the expression with all replacements and analysis
2080 -- already taken place in case a derived type inherits the pragma.
2081 -- The copy will be used as the foundation of the derived type's own
2082 -- version of the DIC assertion expression.
2083
2084 if Is_Tagged_Type (DIC_Typ) then
2085 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
2086 end if;
2087
2088 -- If the pragma comes from an aspect specification, replace the
2089 -- saved expression because all type references must be substituted
2090 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
2091 -- routines.
2092
2093 if Present (DIC_Asp) then
2094 Set_Expression_Copy (DIC_Asp, New_Copy_Tree (Expr));
2095 end if;
2096
2097 -- Once the DIC assertion expression is fully processed, add a check
2098 -- to the statements of the DIC procedure (unless the type is an
2099 -- abstract type, in which case we don't want the possibility of
2100 -- generating a call to an abstract function of the type; such DIC
2101 -- procedures can never be called in any case, so not generating the
2102 -- check at all is OK).
2103
2104 if not Is_Abstract_Type (DIC_Typ) or else GNATprove_Mode then
2105 Add_DIC_Check
2106 (DIC_Prag => DIC_Prag,
2107 DIC_Expr => Expr,
2108 Stmts => Stmts);
2109 end if;
2110 end Add_Own_DIC;
2111
2112 ---------------------
2113 -- Add_Parent_DICs --
2114 ---------------------
2115
2116 procedure Add_Parent_DICs
2117 (T : Entity_Id;
2118 Obj_Id : Entity_Id;
2119 Checks : in out List_Id)
2120 is
2121 Dummy_1 : Entity_Id;
2122 Dummy_2 : Entity_Id;
2123
2124 Curr_Typ : Entity_Id;
2125 -- The entity of the current type being examined
2126
2127 Full_Typ : Entity_Id;
2128 -- The full view of Par_Typ
2129
2130 Par_Typ : Entity_Id;
2131 -- The entity of the parent type
2132
2133 Priv_Typ : Entity_Id;
2134 -- The partial view of Par_Typ
2135
2136 Op_Node : Elmt_Id;
2137 Par_Prim : Entity_Id;
2138 Prim : Entity_Id;
2139
2140 begin
2141 -- Map the overridden primitive to the overriding one; required by
2142 -- Replace_References (called by Add_Inherited_DICs) to handle calls
2143 -- to parent primitives.
2144
2145 Op_Node := First_Elmt (Primitive_Operations (T));
2146 while Present (Op_Node) loop
2147 Prim := Node (Op_Node);
2148
2149 if Present (Overridden_Operation (Prim))
2150 and then Comes_From_Source (Prim)
2151 then
2152 Par_Prim := Overridden_Operation (Prim);
2153
2154 -- Create a mapping of the form:
2155 -- parent type primitive -> derived type primitive
2156
2157 Type_Map.Set (Par_Prim, Prim);
2158 end if;
2159
2160 Next_Elmt (Op_Node);
2161 end loop;
2162
2163 -- Climb the parent type chain
2164
2165 Curr_Typ := T;
2166 loop
2167 -- Do not consider subtypes, as they inherit the DICs from their
2168 -- base types.
2169
2170 Par_Typ := Base_Type (Etype (Base_Type (Curr_Typ)));
2171
2172 -- Stop the climb once the root of the parent chain is
2173 -- reached.
2174
2175 exit when Curr_Typ = Par_Typ;
2176
2177 -- Process the DICs of the parent type
2178
2179 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2180
2181 -- Only try to inherit a DIC pragma from the parent type Par_Typ
2182 -- if it Has_Own_DIC pragma. The loop will proceed up the parent
2183 -- chain to find all types that have their own DIC.
2184
2185 if Has_Own_DIC (Par_Typ) then
2186 Add_Inherited_DICs
2187 (T => T,
2188 Priv_Typ => Priv_Typ,
2189 Full_Typ => Full_Typ,
2190 Obj_Id => Obj_Id,
2191 Checks => Checks);
2192 end if;
2193
2194 Curr_Typ := Par_Typ;
2195 end loop;
2196 end Add_Parent_DICs;
2197
2198 -- Local variables
2199
2200 Loc : constant Source_Ptr := Sloc (Typ);
2201
2202 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2203 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2204 -- Save the Ghost-related attributes to restore on exit
2205
2206 DIC_Prag : Node_Id;
2207 DIC_Typ : Entity_Id;
2208 Dummy_1 : Entity_Id;
2209 Dummy_2 : Entity_Id;
2210 Proc_Body : Node_Id;
2211 Proc_Body_Id : Entity_Id;
2212 Proc_Decl : Node_Id;
2213 Proc_Id : Entity_Id;
2214 Stmts : List_Id := No_List;
2215
2216 CRec_Typ : Entity_Id := Empty;
2217 -- The corresponding record type of Full_Typ
2218
2219 Full_Typ : Entity_Id := Empty;
2220 -- The full view of the working type
2221
2222 Obj_Id : Entity_Id := Empty;
2223 -- The _object formal parameter of the invariant procedure
2224
2225 Part_Proc : Entity_Id := Empty;
2226 -- The entity of the "partial" invariant procedure
2227
2228 Priv_Typ : Entity_Id := Empty;
2229 -- The partial view of the working type
2230
2231 Work_Typ : Entity_Id;
2232 -- The working type
2233
2234 -- Start of processing for Build_DIC_Procedure_Body
2235
2236 begin
2237 Work_Typ := Base_Type (Typ);
2238
2239 -- Do not process class-wide types as these are Itypes, but lack a first
2240 -- subtype (see below).
2241
2242 if Is_Class_Wide_Type (Work_Typ) then
2243 return;
2244
2245 -- Do not process the underlying full view of a private type. There is
2246 -- no way to get back to the partial view, plus the body will be built
2247 -- by the full view or the base type.
2248
2249 elsif Is_Underlying_Full_View (Work_Typ) then
2250 return;
2251
2252 -- Use the first subtype when dealing with implicit base types
2253
2254 elsif Is_Itype (Work_Typ) then
2255 Work_Typ := First_Subtype (Work_Typ);
2256
2257 -- The input denotes the corresponding record type of a protected or a
2258 -- task type. Work with the concurrent type because the corresponding
2259 -- record type may not be visible to clients of the type.
2260
2261 elsif Ekind (Work_Typ) = E_Record_Type
2262 and then Is_Concurrent_Record_Type (Work_Typ)
2263 then
2264 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2265 end if;
2266
2267 -- The working type may be subject to pragma Ghost. Set the mode now to
2268 -- ensure that the DIC procedure is properly marked as Ghost.
2269
2270 Set_Ghost_Mode (Work_Typ);
2271
2272 -- The working type must be either define a DIC pragma of its own or
2273 -- inherit one from a parent type.
2274
2275 pragma Assert (Has_DIC (Work_Typ));
2276
2277 -- Recover the type which defines the DIC pragma. This is either the
2278 -- working type itself or a parent type when the pragma is inherited.
2279
2280 DIC_Typ := Find_DIC_Type (Work_Typ);
2281 pragma Assert (Present (DIC_Typ));
2282
2283 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2284 pragma Assert (Present (DIC_Prag));
2285
2286 -- Nothing to do if pragma DIC appears without an argument or its sole
2287 -- argument is "null".
2288
2289 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2290 goto Leave;
2291 end if;
2292
2293 -- Obtain both views of the type
2294
2295 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy_1, CRec_Typ);
2296
2297 -- The caller requests a body for the partial DIC procedure
2298
2299 if Partial_DIC then
2300 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2301
2302 -- The "full" DIC procedure body was already created
2303
2304 -- Create a declaration for the "partial" DIC procedure if it
2305 -- is not available.
2306
2307 if No (Proc_Id) then
2308 Build_DIC_Procedure_Declaration
2309 (Typ => Work_Typ,
2310 Partial_DIC => True);
2311
2312 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2313 end if;
2314
2315 -- The caller requests a body for the "full" DIC procedure
2316
2317 else
2318 Proc_Id := DIC_Procedure (Work_Typ);
2319 Part_Proc := Partial_DIC_Procedure (Work_Typ);
2320
2321 -- Create a declaration for the "full" DIC procedure if it is
2322 -- not available.
2323
2324 if No (Proc_Id) then
2325 Build_DIC_Procedure_Declaration (Work_Typ);
2326 Proc_Id := DIC_Procedure (Work_Typ);
2327 end if;
2328 end if;
2329
2330 -- At this point there should be a DIC procedure declaration
2331
2332 pragma Assert (Present (Proc_Id));
2333 Proc_Decl := Unit_Declaration_Node (Proc_Id);
2334
2335 -- Nothing to do if the DIC procedure already has a body
2336
2337 if Present (Corresponding_Body (Proc_Decl)) then
2338 goto Leave;
2339 end if;
2340
2341 -- Emulate the environment of the DIC procedure by installing its scope
2342 -- and formal parameters.
2343
2344 Push_Scope (Proc_Id);
2345 Install_Formals (Proc_Id);
2346
2347 Obj_Id := First_Formal (Proc_Id);
2348 pragma Assert (Present (Obj_Id));
2349
2350 -- The "partial" DIC procedure verifies the DICs of the partial view
2351 -- only.
2352
2353 if Partial_DIC then
2354 pragma Assert (Present (Priv_Typ));
2355
2356 if Has_Own_DIC (Work_Typ) then -- If we're testing this then maybe
2357 Add_Own_DIC -- we shouldn't be calling Find_DIC_Typ above???
2358 (DIC_Prag => DIC_Prag,
2359 DIC_Typ => DIC_Typ, -- Should this just be Work_Typ???
2360 Obj_Id => Obj_Id,
2361 Stmts => Stmts);
2362 end if;
2363
2364 -- Otherwise, the "full" DIC procedure verifies the DICs inherited from
2365 -- parent types, as well as indirectly verifying the DICs of the partial
2366 -- view by calling the "partial" DIC procedure.
2367
2368 else
2369 -- Check the DIC of the partial view by calling the "partial" DIC
2370 -- procedure, unless the partial DIC body is empty. Generate:
2371
2372 -- <Work_Typ>Partial_DIC (_object);
2373
2374 if Present (Part_Proc) and then not Has_Null_Body (Part_Proc) then
2375 Append_New_To (Stmts,
2376 Make_Procedure_Call_Statement (Loc,
2377 Name => New_Occurrence_Of (Part_Proc, Loc),
2378 Parameter_Associations => New_List (
2379 New_Occurrence_Of (Obj_Id, Loc))));
2380 end if;
2381
2382 -- Process inherited Default_Initial_Conditions for all parent types
2383
2384 Add_Parent_DICs (Work_Typ, Obj_Id, Stmts);
2385 end if;
2386
2387 End_Scope;
2388
2389 -- Produce an empty completing body in the following cases:
2390 -- * Assertions are disabled
2391 -- * The DIC Assertion_Policy is Ignore
2392
2393 if No (Stmts) then
2394 Stmts := New_List (Make_Null_Statement (Loc));
2395 end if;
2396
2397 -- Generate:
2398 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
2399 -- begin
2400 -- <Stmts>
2401 -- end <Work_Typ>DIC;
2402
2403 Proc_Body :=
2404 Make_Subprogram_Body (Loc,
2405 Specification =>
2406 Copy_Subprogram_Spec (Parent (Proc_Id)),
2407 Declarations => Empty_List,
2408 Handled_Statement_Sequence =>
2409 Make_Handled_Sequence_Of_Statements (Loc,
2410 Statements => Stmts));
2411 Proc_Body_Id := Defining_Entity (Proc_Body);
2412
2413 -- Perform minor decoration in case the body is not analyzed
2414
2415 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
2416 Set_Etype (Proc_Body_Id, Standard_Void_Type);
2417 Set_Scope (Proc_Body_Id, Current_Scope);
2418 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
2419 Set_SPARK_Pragma_Inherited
2420 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
2421
2422 -- Link both spec and body to avoid generating duplicates
2423
2424 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
2425 Set_Corresponding_Spec (Proc_Body, Proc_Id);
2426
2427 -- The body should not be inserted into the tree when the context
2428 -- is a generic unit because it is not part of the template.
2429 -- Note that the body must still be generated in order to resolve the
2430 -- DIC assertion expression.
2431
2432 if Inside_A_Generic then
2433 null;
2434
2435 -- Semi-insert the body into the tree for GNATprove by setting its
2436 -- Parent field. This allows for proper upstream tree traversals.
2437
2438 elsif GNATprove_Mode then
2439 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
2440
2441 -- Otherwise the body is part of the freezing actions of the working
2442 -- type.
2443
2444 else
2445 Append_Freeze_Action (Work_Typ, Proc_Body);
2446 end if;
2447
2448 <<Leave>>
2449 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2450 end Build_DIC_Procedure_Body;
2451
2452 -------------------------------------
2453 -- Build_DIC_Procedure_Declaration --
2454 -------------------------------------
2455
2456 -- WARNING: This routine manages Ghost regions. Return statements must be
2457 -- replaced by gotos which jump to the end of the routine and restore the
2458 -- Ghost mode.
2459
2460 procedure Build_DIC_Procedure_Declaration
2461 (Typ : Entity_Id;
2462 Partial_DIC : Boolean := False)
2463 is
2464 Loc : constant Source_Ptr := Sloc (Typ);
2465
2466 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2467 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2468 -- Save the Ghost-related attributes to restore on exit
2469
2470 DIC_Prag : Node_Id;
2471 DIC_Typ : Entity_Id;
2472 Proc_Decl : Node_Id;
2473 Proc_Id : Entity_Id;
2474 Proc_Nam : Name_Id;
2475 Typ_Decl : Node_Id;
2476
2477 CRec_Typ : Entity_Id;
2478 -- The corresponding record type of Full_Typ
2479
2480 Full_Typ : Entity_Id;
2481 -- The full view of working type
2482
2483 Obj_Id : Entity_Id;
2484 -- The _object formal parameter of the DIC procedure
2485
2486 Priv_Typ : Entity_Id;
2487 -- The partial view of working type
2488
2489 UFull_Typ : Entity_Id;
2490 -- The underlying full view of Full_Typ
2491
2492 Work_Typ : Entity_Id;
2493 -- The working type
2494
2495 begin
2496 Work_Typ := Base_Type (Typ);
2497
2498 -- Do not process class-wide types as these are Itypes, but lack a first
2499 -- subtype (see below).
2500
2501 if Is_Class_Wide_Type (Work_Typ) then
2502 return;
2503
2504 -- Do not process the underlying full view of a private type. There is
2505 -- no way to get back to the partial view, plus the body will be built
2506 -- by the full view or the base type.
2507
2508 elsif Is_Underlying_Full_View (Work_Typ) then
2509 return;
2510
2511 -- Use the first subtype when dealing with various base types
2512
2513 elsif Is_Itype (Work_Typ) then
2514 Work_Typ := First_Subtype (Work_Typ);
2515
2516 -- The input denotes the corresponding record type of a protected or a
2517 -- task type. Work with the concurrent type because the corresponding
2518 -- record type may not be visible to clients of the type.
2519
2520 elsif Ekind (Work_Typ) = E_Record_Type
2521 and then Is_Concurrent_Record_Type (Work_Typ)
2522 then
2523 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2524 end if;
2525
2526 -- The working type may be subject to pragma Ghost. Set the mode now to
2527 -- ensure that the DIC procedure is properly marked as Ghost.
2528
2529 Set_Ghost_Mode (Work_Typ);
2530
2531 -- The type must be either subject to a DIC pragma or inherit one from a
2532 -- parent type.
2533
2534 pragma Assert (Has_DIC (Work_Typ));
2535
2536 -- Recover the type which defines the DIC pragma. This is either the
2537 -- working type itself or a parent type when the pragma is inherited.
2538
2539 DIC_Typ := Find_DIC_Type (Work_Typ);
2540 pragma Assert (Present (DIC_Typ));
2541
2542 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2543 pragma Assert (Present (DIC_Prag));
2544
2545 -- Nothing to do if pragma DIC appears without an argument or its sole
2546 -- argument is "null".
2547
2548 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2549 goto Leave;
2550 end if;
2551
2552 -- Nothing to do if the type already has a "partial" DIC procedure
2553
2554 if Partial_DIC then
2555 if Present (Partial_DIC_Procedure (Work_Typ)) then
2556 goto Leave;
2557 end if;
2558
2559 -- Nothing to do if the type already has a "full" DIC procedure
2560
2561 elsif Present (DIC_Procedure (Work_Typ)) then
2562 goto Leave;
2563 end if;
2564
2565 -- The caller requests the declaration of the "partial" DIC procedure
2566
2567 if Partial_DIC then
2568 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_DIC");
2569
2570 -- Otherwise the caller requests the declaration of the "full" DIC
2571 -- procedure.
2572
2573 else
2574 Proc_Nam := New_External_Name (Chars (Work_Typ), "DIC");
2575 end if;
2576
2577 Proc_Id :=
2578 Make_Defining_Identifier (Loc, Chars => Proc_Nam);
2579
2580 -- Perform minor decoration in case the declaration is not analyzed
2581
2582 Mutate_Ekind (Proc_Id, E_Procedure);
2583 Set_Etype (Proc_Id, Standard_Void_Type);
2584 Set_Is_DIC_Procedure (Proc_Id);
2585 Set_Scope (Proc_Id, Current_Scope);
2586 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
2587 Set_SPARK_Pragma_Inherited (Proc_Id);
2588
2589 Set_DIC_Procedure (Work_Typ, Proc_Id);
2590
2591 -- The DIC procedure requires debug info when the assertion expression
2592 -- is subject to Source Coverage Obligations.
2593
2594 if Generate_SCO then
2595 Set_Debug_Info_Needed (Proc_Id);
2596 end if;
2597
2598 -- Obtain all views of the input type
2599
2600 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
2601
2602 -- Associate the DIC procedure and various flags with all views
2603
2604 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
2605 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
2606 Propagate_DIC_Attributes (UFull_Typ, From_Typ => Work_Typ);
2607 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
2608
2609 -- The declaration of the DIC procedure must be inserted after the
2610 -- declaration of the partial view as this allows for proper external
2611 -- visibility.
2612
2613 if Present (Priv_Typ) then
2614 Typ_Decl := Declaration_Node (Priv_Typ);
2615
2616 -- Derived types with the full view as parent do not have a partial
2617 -- view. Insert the DIC procedure after the derived type.
2618
2619 else
2620 Typ_Decl := Declaration_Node (Full_Typ);
2621 end if;
2622
2623 -- The type should have a declarative node
2624
2625 pragma Assert (Present (Typ_Decl));
2626
2627 -- Create the formal parameter which emulates the variable-like behavior
2628 -- of the type's current instance.
2629
2630 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
2631
2632 -- Perform minor decoration in case the declaration is not analyzed
2633
2634 Mutate_Ekind (Obj_Id, E_In_Parameter);
2635 Set_Etype (Obj_Id, Work_Typ);
2636 Set_Scope (Obj_Id, Proc_Id);
2637
2638 Set_First_Entity (Proc_Id, Obj_Id);
2639 Set_Last_Entity (Proc_Id, Obj_Id);
2640
2641 -- Generate:
2642 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
2643
2644 Proc_Decl :=
2645 Make_Subprogram_Declaration (Loc,
2646 Specification =>
2647 Make_Procedure_Specification (Loc,
2648 Defining_Unit_Name => Proc_Id,
2649 Parameter_Specifications => New_List (
2650 Make_Parameter_Specification (Loc,
2651 Defining_Identifier => Obj_Id,
2652 Parameter_Type =>
2653 New_Occurrence_Of (Work_Typ, Loc)))));
2654
2655 -- The declaration should not be inserted into the tree when the context
2656 -- is a generic unit because it is not part of the template.
2657
2658 if Inside_A_Generic then
2659 null;
2660
2661 -- Semi-insert the declaration into the tree for GNATprove by setting
2662 -- its Parent field. This allows for proper upstream tree traversals.
2663
2664 elsif GNATprove_Mode then
2665 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2666
2667 -- Otherwise insert the declaration
2668
2669 else
2670 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2671 end if;
2672
2673 <<Leave>>
2674 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2675 end Build_DIC_Procedure_Declaration;
2676
2677 ------------------------------------
2678 -- Build_Invariant_Procedure_Body --
2679 ------------------------------------
2680
2681 -- WARNING: This routine manages Ghost regions. Return statements must be
2682 -- replaced by gotos which jump to the end of the routine and restore the
2683 -- Ghost mode.
2684
2685 procedure Build_Invariant_Procedure_Body
2686 (Typ : Entity_Id;
2687 Partial_Invariant : Boolean := False)
2688 is
2689 Loc : constant Source_Ptr := Sloc (Typ);
2690
2691 Pragmas_Seen : Elist_Id := No_Elist;
2692 -- This list contains all invariant pragmas processed so far. The list
2693 -- is used to avoid generating redundant invariant checks.
2694
2695 Produced_Check : Boolean := False;
2696 -- This flag tracks whether the type has produced at least one invariant
2697 -- check. The flag is used as a sanity check at the end of the routine.
2698
2699 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2700 -- intentionally unnested to avoid deep indentation of code.
2701
2702 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2703 -- they emit checks, loops (for arrays) and case statements (for record
2704 -- variant parts) only when there are invariants to verify. This keeps
2705 -- the body of the invariant procedure free of useless code.
2706
2707 procedure Add_Array_Component_Invariants
2708 (T : Entity_Id;
2709 Obj_Id : Entity_Id;
2710 Checks : in out List_Id);
2711 -- Generate an invariant check for each component of array type T.
2712 -- Obj_Id denotes the entity of the _object formal parameter of the
2713 -- invariant procedure. All created checks are added to list Checks.
2714
2715 procedure Add_Inherited_Invariants
2716 (T : Entity_Id;
2717 Priv_Typ : Entity_Id;
2718 Full_Typ : Entity_Id;
2719 Obj_Id : Entity_Id;
2720 Checks : in out List_Id);
2721 -- Generate an invariant check for each inherited class-wide invariant
2722 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2723 -- the partial and full view of the parent type. Obj_Id denotes the
2724 -- entity of the _object formal parameter of the invariant procedure.
2725 -- All created checks are added to list Checks.
2726
2727 procedure Add_Interface_Invariants
2728 (T : Entity_Id;
2729 Obj_Id : Entity_Id;
2730 Checks : in out List_Id);
2731 -- Generate an invariant check for each inherited class-wide invariant
2732 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2733 -- entity of the _object formal parameter of the invariant procedure.
2734 -- All created checks are added to list Checks.
2735
2736 procedure Add_Invariant_Check
2737 (Prag : Node_Id;
2738 Expr : Node_Id;
2739 Checks : in out List_Id;
2740 Inherited : Boolean := False);
2741 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2742 -- verify assertion expression Expr of pragma Prag. All generated code
2743 -- is added to list Checks. Flag Inherited should be set when the pragma
2744 -- is inherited from a parent or interface type.
2745
2746 procedure Add_Own_Invariants
2747 (T : Entity_Id;
2748 Obj_Id : Entity_Id;
2749 Checks : in out List_Id;
2750 Priv_Item : Node_Id := Empty);
2751 -- Generate an invariant check for each invariant found for type T.
2752 -- Obj_Id denotes the entity of the _object formal parameter of the
2753 -- invariant procedure. All created checks are added to list Checks.
2754 -- Priv_Item denotes the first rep item of the private type.
2755
2756 procedure Add_Parent_Invariants
2757 (T : Entity_Id;
2758 Obj_Id : Entity_Id;
2759 Checks : in out List_Id);
2760 -- Generate an invariant check for each inherited class-wide invariant
2761 -- coming from all parent types of type T. Obj_Id denotes the entity of
2762 -- the _object formal parameter of the invariant procedure. All created
2763 -- checks are added to list Checks.
2764
2765 procedure Add_Record_Component_Invariants
2766 (T : Entity_Id;
2767 Obj_Id : Entity_Id;
2768 Checks : in out List_Id);
2769 -- Generate an invariant check for each component of record type T.
2770 -- Obj_Id denotes the entity of the _object formal parameter of the
2771 -- invariant procedure. All created checks are added to list Checks.
2772
2773 ------------------------------------
2774 -- Add_Array_Component_Invariants --
2775 ------------------------------------
2776
2777 procedure Add_Array_Component_Invariants
2778 (T : Entity_Id;
2779 Obj_Id : Entity_Id;
2780 Checks : in out List_Id)
2781 is
2782 Comp_Typ : constant Entity_Id := Component_Type (T);
2783 Dims : constant Pos := Number_Dimensions (T);
2784
2785 procedure Process_Array_Component
2786 (Indices : List_Id;
2787 Comp_Checks : in out List_Id);
2788 -- Generate an invariant check for an array component identified by
2789 -- the indices in list Indices. All created checks are added to list
2790 -- Comp_Checks.
2791
2792 procedure Process_One_Dimension
2793 (Dim : Pos;
2794 Indices : List_Id;
2795 Dim_Checks : in out List_Id);
2796 -- Generate a loop over the Nth dimension Dim of an array type. List
2797 -- Indices contains all array indices for the dimension. All created
2798 -- checks are added to list Dim_Checks.
2799
2800 -----------------------------
2801 -- Process_Array_Component --
2802 -----------------------------
2803
2804 procedure Process_Array_Component
2805 (Indices : List_Id;
2806 Comp_Checks : in out List_Id)
2807 is
2808 Proc_Id : Entity_Id;
2809
2810 begin
2811 if Has_Invariants (Comp_Typ) then
2812
2813 -- In GNATprove mode, the component invariants are checked by
2814 -- other means. They should not be added to the array type
2815 -- invariant procedure, so that the procedure can be used to
2816 -- check the array type invariants if any.
2817
2818 if GNATprove_Mode then
2819 null;
2820
2821 else
2822 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2823
2824 -- The component type should have an invariant procedure
2825 -- if it has invariants of its own or inherits class-wide
2826 -- invariants from parent or interface types.
2827
2828 pragma Assert (Present (Proc_Id));
2829
2830 -- Generate:
2831 -- <Comp_Typ>Invariant (_object (<Indices>));
2832
2833 -- The invariant procedure has a null body if assertions are
2834 -- disabled or Assertion_Policy Ignore is in effect.
2835
2836 if not Has_Null_Body (Proc_Id) then
2837 Append_New_To (Comp_Checks,
2838 Make_Procedure_Call_Statement (Loc,
2839 Name =>
2840 New_Occurrence_Of (Proc_Id, Loc),
2841 Parameter_Associations => New_List (
2842 Make_Indexed_Component (Loc,
2843 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2844 Expressions => New_Copy_List (Indices)))));
2845 end if;
2846 end if;
2847
2848 Produced_Check := True;
2849 end if;
2850 end Process_Array_Component;
2851
2852 ---------------------------
2853 -- Process_One_Dimension --
2854 ---------------------------
2855
2856 procedure Process_One_Dimension
2857 (Dim : Pos;
2858 Indices : List_Id;
2859 Dim_Checks : in out List_Id)
2860 is
2861 Comp_Checks : List_Id := No_List;
2862 Index : Entity_Id;
2863
2864 begin
2865 -- Generate the invariant checks for the array component after all
2866 -- dimensions have produced their respective loops.
2867
2868 if Dim > Dims then
2869 Process_Array_Component
2870 (Indices => Indices,
2871 Comp_Checks => Dim_Checks);
2872
2873 -- Otherwise create a loop for the current dimension
2874
2875 else
2876 -- Create a new loop variable for each dimension
2877
2878 Index :=
2879 Make_Defining_Identifier (Loc,
2880 Chars => New_External_Name ('I', Dim));
2881 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2882
2883 Process_One_Dimension
2884 (Dim => Dim + 1,
2885 Indices => Indices,
2886 Dim_Checks => Comp_Checks);
2887
2888 -- Generate:
2889 -- for I<Dim> in _object'Range (<Dim>) loop
2890 -- <Comp_Checks>
2891 -- end loop;
2892
2893 -- Note that the invariant procedure may have a null body if
2894 -- assertions are disabled or Assertion_Policy Ignore is in
2895 -- effect.
2896
2897 if Present (Comp_Checks) then
2898 Append_New_To (Dim_Checks,
2899 Make_Implicit_Loop_Statement (T,
2900 Identifier => Empty,
2901 Iteration_Scheme =>
2902 Make_Iteration_Scheme (Loc,
2903 Loop_Parameter_Specification =>
2904 Make_Loop_Parameter_Specification (Loc,
2905 Defining_Identifier => Index,
2906 Discrete_Subtype_Definition =>
2907 Make_Attribute_Reference (Loc,
2908 Prefix =>
2909 New_Occurrence_Of (Obj_Id, Loc),
2910 Attribute_Name => Name_Range,
2911 Expressions => New_List (
2912 Make_Integer_Literal (Loc, Dim))))),
2913 Statements => Comp_Checks));
2914 end if;
2915 end if;
2916 end Process_One_Dimension;
2917
2918 -- Start of processing for Add_Array_Component_Invariants
2919
2920 begin
2921 Process_One_Dimension
2922 (Dim => 1,
2923 Indices => New_List,
2924 Dim_Checks => Checks);
2925 end Add_Array_Component_Invariants;
2926
2927 ------------------------------
2928 -- Add_Inherited_Invariants --
2929 ------------------------------
2930
2931 procedure Add_Inherited_Invariants
2932 (T : Entity_Id;
2933 Priv_Typ : Entity_Id;
2934 Full_Typ : Entity_Id;
2935 Obj_Id : Entity_Id;
2936 Checks : in out List_Id)
2937 is
2938 Deriv_Typ : Entity_Id;
2939 Expr : Node_Id;
2940 Prag : Node_Id;
2941 Prag_Expr : Node_Id;
2942 Prag_Expr_Arg : Node_Id;
2943 Prag_Typ : Node_Id;
2944 Prag_Typ_Arg : Node_Id;
2945
2946 Par_Proc : Entity_Id;
2947 -- The "partial" invariant procedure of Par_Typ
2948
2949 Par_Typ : Entity_Id;
2950 -- The suitable view of the parent type used in the substitution of
2951 -- type attributes.
2952
2953 begin
2954 if No (Priv_Typ) and then No (Full_Typ) then
2955 return;
2956 end if;
2957
2958 -- When the type inheriting the class-wide invariant is a concurrent
2959 -- type, use the corresponding record type because it contains all
2960 -- primitive operations of the concurrent type and allows for proper
2961 -- substitution.
2962
2963 if Is_Concurrent_Type (T) then
2964 Deriv_Typ := Corresponding_Record_Type (T);
2965 else
2966 Deriv_Typ := T;
2967 end if;
2968
2969 pragma Assert (Present (Deriv_Typ));
2970
2971 -- Determine which rep item chain to use. Precedence is given to that
2972 -- of the parent type's partial view since it usually carries all the
2973 -- class-wide invariants.
2974
2975 if Present (Priv_Typ) then
2976 Prag := First_Rep_Item (Priv_Typ);
2977 else
2978 Prag := First_Rep_Item (Full_Typ);
2979 end if;
2980
2981 while Present (Prag) loop
2982 if Nkind (Prag) = N_Pragma
2983 and then Pragma_Name (Prag) = Name_Invariant
2984 then
2985 -- Nothing to do if the pragma was already processed
2986
2987 if Contains (Pragmas_Seen, Prag) then
2988 return;
2989
2990 -- Nothing to do when the caller requests the processing of all
2991 -- inherited class-wide invariants, but the pragma does not
2992 -- fall in this category.
2993
2994 elsif not Class_Present (Prag) then
2995 return;
2996 end if;
2997
2998 -- Extract the arguments of the invariant pragma
2999
3000 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
3001 Prag_Expr_Arg := Next (Prag_Typ_Arg);
3002 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
3003 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
3004
3005 -- The pragma applies to the partial view of the parent type
3006
3007 if Present (Priv_Typ)
3008 and then Entity (Prag_Typ) = Priv_Typ
3009 then
3010 Par_Typ := Priv_Typ;
3011
3012 -- The pragma applies to the full view of the parent type
3013
3014 elsif Present (Full_Typ)
3015 and then Entity (Prag_Typ) = Full_Typ
3016 then
3017 Par_Typ := Full_Typ;
3018
3019 -- Otherwise the pragma does not belong to the parent type and
3020 -- should not be considered.
3021
3022 else
3023 return;
3024 end if;
3025
3026 -- Perform the following substitutions:
3027
3028 -- * Replace a reference to the _object parameter of the
3029 -- parent type's partial invariant procedure with a
3030 -- reference to the _object parameter of the derived
3031 -- type's full invariant procedure.
3032
3033 -- * Replace a reference to a discriminant of the parent type
3034 -- with a suitable value from the point of view of the
3035 -- derived type.
3036
3037 -- * Replace a call to an overridden parent primitive with a
3038 -- call to the overriding derived type primitive.
3039
3040 -- * Replace a call to an inherited parent primitive with a
3041 -- call to the internally-generated inherited derived type
3042 -- primitive.
3043
3044 Expr := New_Copy_Tree (Prag_Expr);
3045
3046 -- The parent type must have a "partial" invariant procedure
3047 -- because class-wide invariants are captured exclusively by
3048 -- it.
3049
3050 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
3051 pragma Assert (Present (Par_Proc));
3052
3053 Replace_References
3054 (Expr => Expr,
3055 Par_Typ => Par_Typ,
3056 Deriv_Typ => Deriv_Typ,
3057 Par_Obj => First_Formal (Par_Proc),
3058 Deriv_Obj => Obj_Id);
3059
3060 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
3061 end if;
3062
3063 Next_Rep_Item (Prag);
3064 end loop;
3065 end Add_Inherited_Invariants;
3066
3067 ------------------------------
3068 -- Add_Interface_Invariants --
3069 ------------------------------
3070
3071 procedure Add_Interface_Invariants
3072 (T : Entity_Id;
3073 Obj_Id : Entity_Id;
3074 Checks : in out List_Id)
3075 is
3076 Iface_Elmt : Elmt_Id;
3077 Ifaces : Elist_Id;
3078
3079 begin
3080 -- Generate an invariant check for each class-wide invariant coming
3081 -- from all interfaces implemented by type T.
3082
3083 if Is_Tagged_Type (T) then
3084 Collect_Interfaces (T, Ifaces);
3085
3086 -- Process the class-wide invariants of all implemented interfaces
3087
3088 Iface_Elmt := First_Elmt (Ifaces);
3089 while Present (Iface_Elmt) loop
3090
3091 -- The Full_Typ parameter is intentionally left Empty because
3092 -- interfaces are treated as the partial view of a private type
3093 -- in order to achieve uniformity with the general case.
3094
3095 Add_Inherited_Invariants
3096 (T => T,
3097 Priv_Typ => Node (Iface_Elmt),
3098 Full_Typ => Empty,
3099 Obj_Id => Obj_Id,
3100 Checks => Checks);
3101
3102 Next_Elmt (Iface_Elmt);
3103 end loop;
3104 end if;
3105 end Add_Interface_Invariants;
3106
3107 -------------------------
3108 -- Add_Invariant_Check --
3109 -------------------------
3110
3111 procedure Add_Invariant_Check
3112 (Prag : Node_Id;
3113 Expr : Node_Id;
3114 Checks : in out List_Id;
3115 Inherited : Boolean := False)
3116 is
3117 Args : constant List_Id := Pragma_Argument_Associations (Prag);
3118 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
3119 Ploc : constant Source_Ptr := Sloc (Prag);
3120 Str_Arg : constant Node_Id := Next (Next (First (Args)));
3121
3122 Assoc : List_Id;
3123 Str : String_Id;
3124
3125 begin
3126 -- The invariant is ignored, nothing left to do
3127
3128 if Is_Ignored (Prag) then
3129 null;
3130
3131 -- Otherwise the invariant is checked. Build a pragma Check to verify
3132 -- the expression at run time.
3133
3134 else
3135 Assoc := New_List (
3136 Make_Pragma_Argument_Association (Ploc,
3137 Expression => Make_Identifier (Ploc, Nam)),
3138 Make_Pragma_Argument_Association (Ploc,
3139 Expression => Expr));
3140
3141 -- Handle the String argument (if any)
3142
3143 if Present (Str_Arg) then
3144 Str := Strval (Get_Pragma_Arg (Str_Arg));
3145
3146 -- When inheriting an invariant, modify the message from
3147 -- "failed invariant" to "failed inherited invariant".
3148
3149 if Inherited then
3150 String_To_Name_Buffer (Str);
3151
3152 if Name_Buffer (1 .. 16) = "failed invariant" then
3153 Insert_Str_In_Name_Buffer ("inherited ", 8);
3154 Str := String_From_Name_Buffer;
3155 end if;
3156 end if;
3157
3158 Append_To (Assoc,
3159 Make_Pragma_Argument_Association (Ploc,
3160 Expression => Make_String_Literal (Ploc, Str)));
3161 end if;
3162
3163 -- Generate:
3164 -- pragma Check (<Nam>, <Expr>, <Str>);
3165
3166 Append_New_To (Checks,
3167 Make_Pragma (Ploc,
3168 Chars => Name_Check,
3169 Pragma_Argument_Associations => Assoc));
3170 end if;
3171
3172 -- Output an info message when inheriting an invariant and the
3173 -- listing option is enabled.
3174
3175 if Inherited and List_Inherited_Aspects then
3176 Error_Msg_Sloc := Sloc (Prag);
3177 Error_Msg_N
3178 ("info: & inherits `Invariant''Class` aspect from #?.l?", Typ);
3179 end if;
3180
3181 -- Add the pragma to the list of processed pragmas
3182
3183 Append_New_Elmt (Prag, Pragmas_Seen);
3184 Produced_Check := True;
3185 end Add_Invariant_Check;
3186
3187 ---------------------------
3188 -- Add_Parent_Invariants --
3189 ---------------------------
3190
3191 procedure Add_Parent_Invariants
3192 (T : Entity_Id;
3193 Obj_Id : Entity_Id;
3194 Checks : in out List_Id)
3195 is
3196 Dummy_1 : Entity_Id;
3197 Dummy_2 : Entity_Id;
3198
3199 Curr_Typ : Entity_Id;
3200 -- The entity of the current type being examined
3201
3202 Full_Typ : Entity_Id;
3203 -- The full view of Par_Typ
3204
3205 Par_Typ : Entity_Id;
3206 -- The entity of the parent type
3207
3208 Priv_Typ : Entity_Id;
3209 -- The partial view of Par_Typ
3210
3211 begin
3212 -- Do not process array types because they cannot have true parent
3213 -- types. This also prevents the generation of a duplicate invariant
3214 -- check when the input type is an array base type because its Etype
3215 -- denotes the first subtype, both of which share the same component
3216 -- type.
3217
3218 if Is_Array_Type (T) then
3219 return;
3220 end if;
3221
3222 -- Climb the parent type chain
3223
3224 Curr_Typ := T;
3225 loop
3226 -- Do not consider subtypes as they inherit the invariants
3227 -- from their base types.
3228
3229 Par_Typ := Base_Type (Etype (Curr_Typ));
3230
3231 -- Stop the climb once the root of the parent chain is
3232 -- reached.
3233
3234 exit when Curr_Typ = Par_Typ;
3235
3236 -- Process the class-wide invariants of the parent type
3237
3238 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
3239
3240 -- Process the elements of an array type
3241
3242 if Is_Array_Type (Full_Typ) then
3243 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
3244
3245 -- Process the components of a record type
3246
3247 elsif Ekind (Full_Typ) = E_Record_Type then
3248 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
3249 end if;
3250
3251 Add_Inherited_Invariants
3252 (T => T,
3253 Priv_Typ => Priv_Typ,
3254 Full_Typ => Full_Typ,
3255 Obj_Id => Obj_Id,
3256 Checks => Checks);
3257
3258 Curr_Typ := Par_Typ;
3259 end loop;
3260 end Add_Parent_Invariants;
3261
3262 ------------------------
3263 -- Add_Own_Invariants --
3264 ------------------------
3265
3266 procedure Add_Own_Invariants
3267 (T : Entity_Id;
3268 Obj_Id : Entity_Id;
3269 Checks : in out List_Id;
3270 Priv_Item : Node_Id := Empty)
3271 is
3272 Expr : Node_Id;
3273 Prag : Node_Id;
3274 Prag_Asp : Node_Id;
3275 Prag_Expr : Node_Id;
3276 Prag_Expr_Arg : Node_Id;
3277 Prag_Typ : Node_Id;
3278 Prag_Typ_Arg : Node_Id;
3279
3280 begin
3281 if No (T) then
3282 return;
3283 end if;
3284
3285 Prag := First_Rep_Item (T);
3286 while Present (Prag) loop
3287 if Nkind (Prag) = N_Pragma
3288 and then Pragma_Name (Prag) = Name_Invariant
3289 then
3290 -- Stop the traversal of the rep item chain once a specific
3291 -- item is encountered.
3292
3293 if Present (Priv_Item) and then Prag = Priv_Item then
3294 exit;
3295 end if;
3296
3297 -- Nothing to do if the pragma was already processed
3298
3299 if Contains (Pragmas_Seen, Prag) then
3300 return;
3301 end if;
3302
3303 -- Extract the arguments of the invariant pragma
3304
3305 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
3306 Prag_Expr_Arg := Next (Prag_Typ_Arg);
3307 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
3308 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
3309 Prag_Asp := Corresponding_Aspect (Prag);
3310
3311 -- Verify the pragma belongs to T, otherwise the pragma applies
3312 -- to a parent type in which case it will be processed later by
3313 -- Add_Parent_Invariants or Add_Interface_Invariants.
3314
3315 if Entity (Prag_Typ) /= T then
3316 return;
3317 end if;
3318
3319 -- We need to preanalyze the expression itself inside a generic
3320 -- to be able to capture global references present in it.
3321
3322 if Inside_A_Generic then
3323 Expr := Prag_Expr;
3324 else
3325 Expr := New_Copy_Tree (Prag_Expr);
3326 end if;
3327
3328 -- Substitute all references to type T with references to the
3329 -- _object formal parameter.
3330
3331 Replace_Type_References (Expr, T, Obj_Id);
3332
3333 -- Preanalyze the invariant expression to detect errors and at
3334 -- the same time capture the visibility of the proper package
3335 -- part.
3336
3337 Set_Parent (Expr, Parent (Prag_Expr));
3338 Preanalyze_Assert_Expression (Expr, Any_Boolean);
3339
3340 -- Save a copy of the expression when T is tagged to detect
3341 -- errors and capture the visibility of the proper package part
3342 -- for the generation of inherited type invariants.
3343
3344 if Is_Tagged_Type (T) then
3345 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
3346 end if;
3347
3348 -- If the pragma comes from an aspect specification, replace
3349 -- the saved expression because all type references must be
3350 -- substituted for the call to Preanalyze_Spec_Expression in
3351 -- Check_Aspect_At_xxx routines.
3352
3353 if Present (Prag_Asp) then
3354 Set_Expression_Copy (Prag_Asp, New_Copy_Tree (Expr));
3355 end if;
3356
3357 Add_Invariant_Check (Prag, Expr, Checks);
3358 end if;
3359
3360 Next_Rep_Item (Prag);
3361 end loop;
3362 end Add_Own_Invariants;
3363
3364 -------------------------------------
3365 -- Add_Record_Component_Invariants --
3366 -------------------------------------
3367
3368 procedure Add_Record_Component_Invariants
3369 (T : Entity_Id;
3370 Obj_Id : Entity_Id;
3371 Checks : in out List_Id)
3372 is
3373 procedure Process_Component_List
3374 (Comp_List : Node_Id;
3375 CL_Checks : in out List_Id);
3376 -- Generate invariant checks for all record components found in
3377 -- component list Comp_List, including variant parts. All created
3378 -- checks are added to list CL_Checks.
3379
3380 procedure Process_Record_Component
3381 (Comp_Id : Entity_Id;
3382 Comp_Checks : in out List_Id);
3383 -- Generate an invariant check for a record component identified by
3384 -- Comp_Id. All created checks are added to list Comp_Checks.
3385
3386 ----------------------------
3387 -- Process_Component_List --
3388 ----------------------------
3389
3390 procedure Process_Component_List
3391 (Comp_List : Node_Id;
3392 CL_Checks : in out List_Id)
3393 is
3394 Comp : Node_Id;
3395 Var : Node_Id;
3396 Var_Alts : List_Id := No_List;
3397 Var_Checks : List_Id := No_List;
3398 Var_Stmts : List_Id;
3399
3400 Produced_Variant_Check : Boolean := False;
3401 -- This flag tracks whether the component has produced at least
3402 -- one invariant check.
3403
3404 begin
3405 -- Traverse the component items
3406
3407 Comp := First (Component_Items (Comp_List));
3408 while Present (Comp) loop
3409 if Nkind (Comp) = N_Component_Declaration then
3410
3411 -- Generate the component invariant check
3412
3413 Process_Record_Component
3414 (Comp_Id => Defining_Entity (Comp),
3415 Comp_Checks => CL_Checks);
3416 end if;
3417
3418 Next (Comp);
3419 end loop;
3420
3421 -- Traverse the variant part
3422
3423 if Present (Variant_Part (Comp_List)) then
3424 Var := First (Variants (Variant_Part (Comp_List)));
3425 while Present (Var) loop
3426 Var_Checks := No_List;
3427
3428 -- Generate invariant checks for all components and variant
3429 -- parts that qualify.
3430
3431 Process_Component_List
3432 (Comp_List => Component_List (Var),
3433 CL_Checks => Var_Checks);
3434
3435 -- The components of the current variant produced at least
3436 -- one invariant check.
3437
3438 if Present (Var_Checks) then
3439 Var_Stmts := Var_Checks;
3440 Produced_Variant_Check := True;
3441
3442 -- Otherwise there are either no components with invariants,
3443 -- assertions are disabled, or Assertion_Policy Ignore is in
3444 -- effect.
3445
3446 else
3447 Var_Stmts := New_List (Make_Null_Statement (Loc));
3448 end if;
3449
3450 Append_New_To (Var_Alts,
3451 Make_Case_Statement_Alternative (Loc,
3452 Discrete_Choices =>
3453 New_Copy_List (Discrete_Choices (Var)),
3454 Statements => Var_Stmts));
3455
3456 Next (Var);
3457 end loop;
3458
3459 -- Create a case statement which verifies the invariant checks
3460 -- of a particular component list depending on the discriminant
3461 -- values only when there is at least one real invariant check.
3462
3463 if Produced_Variant_Check then
3464 Append_New_To (CL_Checks,
3465 Make_Case_Statement (Loc,
3466 Expression =>
3467 Make_Selected_Component (Loc,
3468 Prefix => New_Occurrence_Of (Obj_Id, Loc),
3469 Selector_Name =>
3470 New_Occurrence_Of
3471 (Entity (Name (Variant_Part (Comp_List))), Loc)),
3472 Alternatives => Var_Alts));
3473 end if;
3474 end if;
3475 end Process_Component_List;
3476
3477 ------------------------------
3478 -- Process_Record_Component --
3479 ------------------------------
3480
3481 procedure Process_Record_Component
3482 (Comp_Id : Entity_Id;
3483 Comp_Checks : in out List_Id)
3484 is
3485 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
3486 Proc_Id : Entity_Id;
3487
3488 Produced_Component_Check : Boolean := False;
3489 -- This flag tracks whether the component has produced at least
3490 -- one invariant check.
3491
3492 begin
3493 -- Nothing to do for internal component _parent. Note that it is
3494 -- not desirable to check whether the component comes from source
3495 -- because protected type components are relocated to an internal
3496 -- corresponding record, but still need processing.
3497
3498 if Chars (Comp_Id) = Name_uParent then
3499 return;
3500 end if;
3501
3502 -- Verify the invariant of the component. Note that an access
3503 -- type may have an invariant when it acts as the full view of a
3504 -- private type and the invariant appears on the partial view. In
3505 -- this case verify the access value itself.
3506
3507 if Has_Invariants (Comp_Typ) then
3508
3509 -- In GNATprove mode, the component invariants are checked by
3510 -- other means. They should not be added to the record type
3511 -- invariant procedure, so that the procedure can be used to
3512 -- check the record type invariants if any.
3513
3514 if GNATprove_Mode then
3515 null;
3516
3517 else
3518 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
3519
3520 -- The component type should have an invariant procedure
3521 -- if it has invariants of its own or inherits class-wide
3522 -- invariants from parent or interface types.
3523
3524 -- However, given that the invariant procedure is built by
3525 -- the expander, it is not available compiling generic units
3526 -- or when the sources have errors, since expansion is then
3527 -- disabled.
3528
3529 pragma Assert (Present (Proc_Id)
3530 or else not Expander_Active);
3531
3532 -- Generate:
3533 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
3534
3535 -- Note that the invariant procedure may have a null body if
3536 -- assertions are disabled or Assertion_Policy Ignore is in
3537 -- effect.
3538
3539 if Present (Proc_Id)
3540 and then not Has_Null_Body (Proc_Id)
3541 then
3542 Append_New_To (Comp_Checks,
3543 Make_Procedure_Call_Statement (Loc,
3544 Name =>
3545 New_Occurrence_Of (Proc_Id, Loc),
3546 Parameter_Associations => New_List (
3547 Make_Selected_Component (Loc,
3548 Prefix =>
3549 Unchecked_Convert_To
3550 (T, New_Occurrence_Of (Obj_Id, Loc)),
3551 Selector_Name =>
3552 New_Occurrence_Of (Comp_Id, Loc)))));
3553 end if;
3554 end if;
3555
3556 Produced_Check := True;
3557 Produced_Component_Check := True;
3558 end if;
3559
3560 if Produced_Component_Check and then Has_Unchecked_Union (T) then
3561 Error_Msg_NE
3562 ("invariants cannot be checked on components of "
3563 & "unchecked_union type &??", Comp_Id, T);
3564 end if;
3565 end Process_Record_Component;
3566
3567 -- Local variables
3568
3569 Comps : Node_Id;
3570 Def : Node_Id;
3571
3572 -- Start of processing for Add_Record_Component_Invariants
3573
3574 begin
3575 -- An untagged derived type inherits the components of its parent
3576 -- type. In order to avoid creating redundant invariant checks, do
3577 -- not process the components now. Instead wait until the ultimate
3578 -- parent of the untagged derivation chain is reached.
3579
3580 if not Is_Untagged_Derivation (T) then
3581 Def := Type_Definition (Parent (T));
3582
3583 if Nkind (Def) = N_Derived_Type_Definition then
3584 Def := Record_Extension_Part (Def);
3585 end if;
3586
3587 pragma Assert (Nkind (Def) = N_Record_Definition);
3588 Comps := Component_List (Def);
3589
3590 if Present (Comps) then
3591 Process_Component_List
3592 (Comp_List => Comps,
3593 CL_Checks => Checks);
3594 end if;
3595 end if;
3596 end Add_Record_Component_Invariants;
3597
3598 -- Local variables
3599
3600 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3601 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3602 -- Save the Ghost-related attributes to restore on exit
3603
3604 Dummy : Entity_Id;
3605 Priv_Item : Node_Id;
3606 Proc_Body : Node_Id;
3607 Proc_Body_Id : Entity_Id;
3608 Proc_Decl : Node_Id;
3609 Proc_Id : Entity_Id;
3610 Stmts : List_Id := No_List;
3611
3612 CRec_Typ : Entity_Id := Empty;
3613 -- The corresponding record type of Full_Typ
3614
3615 Full_Proc : Entity_Id := Empty;
3616 -- The entity of the "full" invariant procedure
3617
3618 Full_Typ : Entity_Id := Empty;
3619 -- The full view of the working type
3620
3621 Obj_Id : Entity_Id := Empty;
3622 -- The _object formal parameter of the invariant procedure
3623
3624 Part_Proc : Entity_Id := Empty;
3625 -- The entity of the "partial" invariant procedure
3626
3627 Priv_Typ : Entity_Id := Empty;
3628 -- The partial view of the working type
3629
3630 Work_Typ : Entity_Id := Empty;
3631 -- The working type
3632
3633 -- Start of processing for Build_Invariant_Procedure_Body
3634
3635 begin
3636 Work_Typ := Typ;
3637
3638 -- Do not process the underlying full view of a private type. There is
3639 -- no way to get back to the partial view, plus the body will be built
3640 -- by the full view or the base type.
3641
3642 if Is_Underlying_Full_View (Work_Typ) then
3643 return;
3644
3645 -- The input type denotes the implementation base type of a constrained
3646 -- array type. Work with the first subtype as all invariant pragmas are
3647 -- on its rep item chain.
3648
3649 elsif Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3650 Work_Typ := First_Subtype (Work_Typ);
3651
3652 -- The input type denotes the corresponding record type of a protected
3653 -- or task type. Work with the concurrent type because the corresponding
3654 -- record type may not be visible to clients of the type.
3655
3656 elsif Ekind (Work_Typ) = E_Record_Type
3657 and then Is_Concurrent_Record_Type (Work_Typ)
3658 then
3659 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3660 end if;
3661
3662 -- The working type may be subject to pragma Ghost. Set the mode now to
3663 -- ensure that the invariant procedure is properly marked as Ghost.
3664
3665 Set_Ghost_Mode (Work_Typ);
3666
3667 -- The type must either have invariants of its own, inherit class-wide
3668 -- invariants from parent types or interfaces, or be an array or record
3669 -- type whose components have invariants.
3670
3671 pragma Assert (Has_Invariants (Work_Typ));
3672
3673 -- Interfaces are treated as the partial view of a private type in order
3674 -- to achieve uniformity with the general case.
3675
3676 if Is_Interface (Work_Typ) then
3677 Priv_Typ := Work_Typ;
3678
3679 -- Otherwise obtain both views of the type
3680
3681 else
3682 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3683 end if;
3684
3685 -- The caller requests a body for the partial invariant procedure
3686
3687 if Partial_Invariant then
3688 Full_Proc := Invariant_Procedure (Work_Typ);
3689 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3690
3691 -- The "full" invariant procedure body was already created
3692
3693 if Present (Full_Proc)
3694 and then Present
3695 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3696 then
3697 -- This scenario happens only when the type is an untagged
3698 -- derivation from a private parent and the underlying full
3699 -- view was processed before the partial view.
3700
3701 pragma Assert
3702 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3703
3704 -- Nothing to do because the processing of the underlying full
3705 -- view already checked the invariants of the partial view.
3706
3707 goto Leave;
3708 end if;
3709
3710 -- Create a declaration for the "partial" invariant procedure if it
3711 -- is not available.
3712
3713 if No (Proc_Id) then
3714 Build_Invariant_Procedure_Declaration
3715 (Typ => Work_Typ,
3716 Partial_Invariant => True);
3717
3718 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3719 end if;
3720
3721 -- The caller requests a body for the "full" invariant procedure
3722
3723 else
3724 Proc_Id := Invariant_Procedure (Work_Typ);
3725 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3726
3727 -- Create a declaration for the "full" invariant procedure if it is
3728 -- not available.
3729
3730 if No (Proc_Id) then
3731 Build_Invariant_Procedure_Declaration (Work_Typ);
3732 Proc_Id := Invariant_Procedure (Work_Typ);
3733 end if;
3734 end if;
3735
3736 -- At this point there should be an invariant procedure declaration
3737
3738 pragma Assert (Present (Proc_Id));
3739 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3740
3741 -- Nothing to do if the invariant procedure already has a body
3742
3743 if Present (Corresponding_Body (Proc_Decl)) then
3744 goto Leave;
3745 end if;
3746
3747 -- Emulate the environment of the invariant procedure by installing its
3748 -- scope and formal parameters. Note that this is not needed, but having
3749 -- the scope installed helps with the detection of invariant-related
3750 -- errors.
3751
3752 Push_Scope (Proc_Id);
3753 Install_Formals (Proc_Id);
3754
3755 Obj_Id := First_Formal (Proc_Id);
3756 pragma Assert (Present (Obj_Id));
3757
3758 -- The "partial" invariant procedure verifies the invariants of the
3759 -- partial view only.
3760
3761 if Partial_Invariant then
3762 pragma Assert (Present (Priv_Typ));
3763
3764 Add_Own_Invariants
3765 (T => Priv_Typ,
3766 Obj_Id => Obj_Id,
3767 Checks => Stmts);
3768
3769 -- Otherwise the "full" invariant procedure verifies the invariants of
3770 -- the full view, all array or record components, as well as class-wide
3771 -- invariants inherited from parent types or interfaces. In addition, it
3772 -- indirectly verifies the invariants of the partial view by calling the
3773 -- "partial" invariant procedure.
3774
3775 else
3776 pragma Assert (Present (Full_Typ));
3777
3778 -- Check the invariants of the partial view by calling the "partial"
3779 -- invariant procedure. Generate:
3780
3781 -- <Work_Typ>Partial_Invariant (_object);
3782
3783 if Present (Part_Proc) then
3784 Append_New_To (Stmts,
3785 Make_Procedure_Call_Statement (Loc,
3786 Name => New_Occurrence_Of (Part_Proc, Loc),
3787 Parameter_Associations => New_List (
3788 New_Occurrence_Of (Obj_Id, Loc))));
3789
3790 Produced_Check := True;
3791 end if;
3792
3793 Priv_Item := Empty;
3794
3795 -- Derived subtypes do not have a partial view
3796
3797 if Present (Priv_Typ) then
3798
3799 -- The processing of the "full" invariant procedure intentionally
3800 -- skips the partial view because a) this may result in changes of
3801 -- visibility and b) lead to duplicate checks. However, when the
3802 -- full view is the underlying full view of an untagged derived
3803 -- type whose parent type is private, partial invariants appear on
3804 -- the rep item chain of the partial view only.
3805
3806 -- package Pack_1 is
3807 -- type Root ... is private;
3808 -- private
3809 -- <full view of Root>
3810 -- end Pack_1;
3811
3812 -- with Pack_1;
3813 -- package Pack_2 is
3814 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3815 -- <underlying full view of Child>
3816 -- end Pack_2;
3817
3818 -- As a result, the processing of the full view must also consider
3819 -- all invariants of the partial view.
3820
3821 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3822 null;
3823
3824 -- Otherwise the invariants of the partial view are ignored
3825
3826 else
3827 -- Note that the rep item chain is shared between the partial
3828 -- and full views of a type. To avoid processing the invariants
3829 -- of the partial view, signal the logic to stop when the first
3830 -- rep item of the partial view has been reached.
3831
3832 Priv_Item := First_Rep_Item (Priv_Typ);
3833
3834 -- Ignore the invariants of the partial view by eliminating the
3835 -- view.
3836
3837 Priv_Typ := Empty;
3838 end if;
3839 end if;
3840
3841 -- Process the invariants of the full view and in certain cases those
3842 -- of the partial view. This also handles any invariants on array or
3843 -- record components.
3844
3845 Add_Own_Invariants
3846 (T => Priv_Typ,
3847 Obj_Id => Obj_Id,
3848 Checks => Stmts,
3849 Priv_Item => Priv_Item);
3850
3851 Add_Own_Invariants
3852 (T => Full_Typ,
3853 Obj_Id => Obj_Id,
3854 Checks => Stmts,
3855 Priv_Item => Priv_Item);
3856
3857 -- Process the elements of an array type
3858
3859 if Is_Array_Type (Full_Typ) then
3860 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3861
3862 -- Process the components of a record type
3863
3864 elsif Ekind (Full_Typ) = E_Record_Type then
3865 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3866
3867 -- Process the components of a corresponding record
3868
3869 elsif Present (CRec_Typ) then
3870 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3871 end if;
3872
3873 -- Process the inherited class-wide invariants of all parent types.
3874 -- This also handles any invariants on record components.
3875
3876 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3877
3878 -- Process the inherited class-wide invariants of all implemented
3879 -- interface types.
3880
3881 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3882 end if;
3883
3884 End_Scope;
3885
3886 -- At this point there should be at least one invariant check. If this
3887 -- is not the case, then the invariant-related flags were not properly
3888 -- set, or there is a missing invariant procedure on one of the array
3889 -- or record components.
3890
3891 pragma Assert (Produced_Check);
3892
3893 -- Account for the case where assertions are disabled or all invariant
3894 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3895 -- empty body.
3896
3897 if No (Stmts) then
3898 Stmts := New_List (Make_Null_Statement (Loc));
3899 end if;
3900
3901 -- Generate:
3902 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3903 -- begin
3904 -- <Stmts>
3905 -- end <Work_Typ>[Partial_]Invariant;
3906
3907 Proc_Body :=
3908 Make_Subprogram_Body (Loc,
3909 Specification =>
3910 Copy_Subprogram_Spec (Parent (Proc_Id)),
3911 Declarations => Empty_List,
3912 Handled_Statement_Sequence =>
3913 Make_Handled_Sequence_Of_Statements (Loc,
3914 Statements => Stmts));
3915 Proc_Body_Id := Defining_Entity (Proc_Body);
3916
3917 -- Perform minor decoration in case the body is not analyzed
3918
3919 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
3920 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3921 Set_Scope (Proc_Body_Id, Current_Scope);
3922
3923 -- Link both spec and body to avoid generating duplicates
3924
3925 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3926 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3927
3928 -- The body should not be inserted into the tree when the context is
3929 -- a generic unit because it is not part of the template. Note
3930 -- that the body must still be generated in order to resolve the
3931 -- invariants.
3932
3933 if Inside_A_Generic then
3934 null;
3935
3936 -- Semi-insert the body into the tree for GNATprove by setting its
3937 -- Parent field. This allows for proper upstream tree traversals.
3938
3939 elsif GNATprove_Mode then
3940 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3941
3942 -- Otherwise the body is part of the freezing actions of the type
3943
3944 else
3945 Append_Freeze_Action (Work_Typ, Proc_Body);
3946 end if;
3947
3948 <<Leave>>
3949 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3950 end Build_Invariant_Procedure_Body;
3951
3952 -------------------------------------------
3953 -- Build_Invariant_Procedure_Declaration --
3954 -------------------------------------------
3955
3956 -- WARNING: This routine manages Ghost regions. Return statements must be
3957 -- replaced by gotos which jump to the end of the routine and restore the
3958 -- Ghost mode.
3959
3960 procedure Build_Invariant_Procedure_Declaration
3961 (Typ : Entity_Id;
3962 Partial_Invariant : Boolean := False)
3963 is
3964 Loc : constant Source_Ptr := Sloc (Typ);
3965
3966 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3967 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3968 -- Save the Ghost-related attributes to restore on exit
3969
3970 Proc_Decl : Node_Id;
3971 Proc_Id : Entity_Id;
3972 Proc_Nam : Name_Id;
3973 Typ_Decl : Node_Id;
3974
3975 CRec_Typ : Entity_Id;
3976 -- The corresponding record type of Full_Typ
3977
3978 Full_Typ : Entity_Id;
3979 -- The full view of working type
3980
3981 Obj_Id : Entity_Id;
3982 -- The _object formal parameter of the invariant procedure
3983
3984 Obj_Typ : Entity_Id;
3985 -- The type of the _object formal parameter
3986
3987 Priv_Typ : Entity_Id;
3988 -- The partial view of working type
3989
3990 UFull_Typ : Entity_Id;
3991 -- The underlying full view of Full_Typ
3992
3993 Work_Typ : Entity_Id;
3994 -- The working type
3995
3996 begin
3997 Work_Typ := Typ;
3998
3999 -- The input type denotes the implementation base type of a constrained
4000 -- array type. Work with the first subtype as all invariant pragmas are
4001 -- on its rep item chain.
4002
4003 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
4004 Work_Typ := First_Subtype (Work_Typ);
4005
4006 -- The input denotes the corresponding record type of a protected or a
4007 -- task type. Work with the concurrent type because the corresponding
4008 -- record type may not be visible to clients of the type.
4009
4010 elsif Ekind (Work_Typ) = E_Record_Type
4011 and then Is_Concurrent_Record_Type (Work_Typ)
4012 then
4013 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
4014 end if;
4015
4016 -- The working type may be subject to pragma Ghost. Set the mode now to
4017 -- ensure that the invariant procedure is properly marked as Ghost.
4018
4019 Set_Ghost_Mode (Work_Typ);
4020
4021 -- The type must either have invariants of its own, inherit class-wide
4022 -- invariants from parent or interface types, or be an array or record
4023 -- type whose components have invariants.
4024
4025 pragma Assert (Has_Invariants (Work_Typ));
4026
4027 -- Nothing to do if the type already has a "partial" invariant procedure
4028
4029 if Partial_Invariant then
4030 if Present (Partial_Invariant_Procedure (Work_Typ)) then
4031 goto Leave;
4032 end if;
4033
4034 -- Nothing to do if the type already has a "full" invariant procedure
4035
4036 elsif Present (Invariant_Procedure (Work_Typ)) then
4037 goto Leave;
4038 end if;
4039
4040 -- The caller requests the declaration of the "partial" invariant
4041 -- procedure.
4042
4043 if Partial_Invariant then
4044 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
4045
4046 -- Otherwise the caller requests the declaration of the "full" invariant
4047 -- procedure.
4048
4049 else
4050 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
4051 end if;
4052
4053 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
4054
4055 -- Perform minor decoration in case the declaration is not analyzed
4056
4057 Mutate_Ekind (Proc_Id, E_Procedure);
4058 Set_Etype (Proc_Id, Standard_Void_Type);
4059 Set_Scope (Proc_Id, Current_Scope);
4060
4061 if Partial_Invariant then
4062 Set_Is_Partial_Invariant_Procedure (Proc_Id);
4063 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
4064 else
4065 Set_Is_Invariant_Procedure (Proc_Id);
4066 Set_Invariant_Procedure (Work_Typ, Proc_Id);
4067 end if;
4068
4069 -- The invariant procedure requires debug info when the invariants are
4070 -- subject to Source Coverage Obligations.
4071
4072 if Generate_SCO then
4073 Set_Debug_Info_Needed (Proc_Id);
4074 end if;
4075
4076 -- Obtain all views of the input type
4077
4078 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
4079
4080 -- Associate the invariant procedure and various flags with all views
4081
4082 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
4083 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
4084 Propagate_Invariant_Attributes (UFull_Typ, From_Typ => Work_Typ);
4085 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
4086
4087 -- The declaration of the invariant procedure is inserted after the
4088 -- declaration of the partial view as this allows for proper external
4089 -- visibility.
4090
4091 if Present (Priv_Typ) then
4092 Typ_Decl := Declaration_Node (Priv_Typ);
4093
4094 -- Anonymous arrays in object declarations have no explicit declaration
4095 -- so use the related object declaration as the insertion point.
4096
4097 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
4098 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
4099
4100 -- Derived types with the full view as parent do not have a partial
4101 -- view. Insert the invariant procedure after the derived type.
4102
4103 else
4104 Typ_Decl := Declaration_Node (Full_Typ);
4105 end if;
4106
4107 -- The type should have a declarative node
4108
4109 pragma Assert (Present (Typ_Decl));
4110
4111 -- Create the formal parameter which emulates the variable-like behavior
4112 -- of the current type instance.
4113
4114 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
4115
4116 -- When generating an invariant procedure declaration for an abstract
4117 -- type (including interfaces), use the class-wide type as the _object
4118 -- type. This has several desirable effects:
4119
4120 -- * The invariant procedure does not become a primitive of the type.
4121 -- This eliminates the need to either special case the treatment of
4122 -- invariant procedures, or to make it a predefined primitive and
4123 -- force every derived type to potentially provide an empty body.
4124
4125 -- * The invariant procedure does not need to be declared as abstract.
4126 -- This allows for a proper body, which in turn avoids redundant
4127 -- processing of the same invariants for types with multiple views.
4128
4129 -- * The class-wide type allows for calls to abstract primitives
4130 -- within a nonabstract subprogram. The calls are treated as
4131 -- dispatching and require additional processing when they are
4132 -- remapped to call primitives of derived types. See routine
4133 -- Replace_References for details.
4134
4135 if Is_Abstract_Type (Work_Typ) then
4136 Obj_Typ := Class_Wide_Type (Work_Typ);
4137 else
4138 Obj_Typ := Work_Typ;
4139 end if;
4140
4141 -- Perform minor decoration in case the declaration is not analyzed
4142
4143 Mutate_Ekind (Obj_Id, E_In_Parameter);
4144 Set_Etype (Obj_Id, Obj_Typ);
4145 Set_Scope (Obj_Id, Proc_Id);
4146
4147 Set_First_Entity (Proc_Id, Obj_Id);
4148 Set_Last_Entity (Proc_Id, Obj_Id);
4149
4150 -- Generate:
4151 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
4152
4153 Proc_Decl :=
4154 Make_Subprogram_Declaration (Loc,
4155 Specification =>
4156 Make_Procedure_Specification (Loc,
4157 Defining_Unit_Name => Proc_Id,
4158 Parameter_Specifications => New_List (
4159 Make_Parameter_Specification (Loc,
4160 Defining_Identifier => Obj_Id,
4161 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
4162
4163 -- The declaration should not be inserted into the tree when the context
4164 -- is a generic unit because it is not part of the template.
4165
4166 if Inside_A_Generic then
4167 null;
4168
4169 -- Semi-insert the declaration into the tree for GNATprove by setting
4170 -- its Parent field. This allows for proper upstream tree traversals.
4171
4172 elsif GNATprove_Mode then
4173 Set_Parent (Proc_Decl, Parent (Typ_Decl));
4174
4175 -- Otherwise insert the declaration
4176
4177 else
4178 pragma Assert (Present (Typ_Decl));
4179 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
4180 end if;
4181
4182 <<Leave>>
4183 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4184 end Build_Invariant_Procedure_Declaration;
4185
4186 ------------------------
4187 -- Build_Runtime_Call --
4188 ------------------------
4189
4190 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
4191 begin
4192 -- If entity is not available, we can skip making the call (this avoids
4193 -- junk duplicated error messages in a number of cases).
4194
4195 if not RTE_Available (RE) then
4196 return Make_Null_Statement (Loc);
4197 else
4198 return
4199 Make_Procedure_Call_Statement (Loc,
4200 Name => New_Occurrence_Of (RTE (RE), Loc));
4201 end if;
4202 end Build_Runtime_Call;
4203
4204 ------------------------
4205 -- Build_SS_Mark_Call --
4206 ------------------------
4207
4208 function Build_SS_Mark_Call
4209 (Loc : Source_Ptr;
4210 Mark : Entity_Id) return Node_Id
4211 is
4212 begin
4213 -- Generate:
4214 -- Mark : constant Mark_Id := SS_Mark;
4215
4216 return
4217 Make_Object_Declaration (Loc,
4218 Defining_Identifier => Mark,
4219 Constant_Present => True,
4220 Object_Definition =>
4221 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
4222 Expression =>
4223 Make_Function_Call (Loc,
4224 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
4225 end Build_SS_Mark_Call;
4226
4227 ---------------------------
4228 -- Build_SS_Release_Call --
4229 ---------------------------
4230
4231 function Build_SS_Release_Call
4232 (Loc : Source_Ptr;
4233 Mark : Entity_Id) return Node_Id
4234 is
4235 begin
4236 -- Generate:
4237 -- SS_Release (Mark);
4238
4239 return
4240 Make_Procedure_Call_Statement (Loc,
4241 Name =>
4242 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
4243 Parameter_Associations => New_List (
4244 New_Occurrence_Of (Mark, Loc)));
4245 end Build_SS_Release_Call;
4246
4247 ----------------------------
4248 -- Build_Task_Array_Image --
4249 ----------------------------
4250
4251 -- This function generates the body for a function that constructs the
4252 -- image string for a task that is an array component. The function is
4253 -- local to the init proc for the array type, and is called for each one
4254 -- of the components. The constructed image has the form of an indexed
4255 -- component, whose prefix is the outer variable of the array type.
4256 -- The n-dimensional array type has known indexes Index, Index2...
4257
4258 -- Id_Ref is an indexed component form created by the enclosing init proc.
4259 -- Its successive indexes are Val1, Val2, ... which are the loop variables
4260 -- in the loops that call the individual task init proc on each component.
4261
4262 -- The generated function has the following structure:
4263
4264 -- function F return String is
4265 -- Pref : String renames Task_Name;
4266 -- T1 : constant String := Index1'Image (Val1);
4267 -- ...
4268 -- Tn : constant String := Indexn'Image (Valn);
4269 -- Len : constant Integer :=
4270 -- Pref'Length + T1'Length + ... + Tn'Length + n + 1;
4271 -- -- Len includes commas and the end parentheses
4272 --
4273 -- Res : String (1 .. Len);
4274 -- Pos : Integer := Pref'Length;
4275 --
4276 -- begin
4277 -- Res (1 .. Pos) := Pref;
4278 -- Pos := Pos + 1;
4279 -- Res (Pos) := '(';
4280 -- Pos := Pos + 1;
4281 -- Res (Pos .. Pos + T1'Length - 1) := T1;
4282 -- Pos := Pos + T1'Length;
4283 -- Res (Pos) := '.';
4284 -- Pos := Pos + 1;
4285 -- ...
4286 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
4287 -- Res (Len) := ')';
4288 --
4289 -- return Res;
4290 -- end F;
4291 --
4292 -- Needless to say, multidimensional arrays of tasks are rare enough that
4293 -- the bulkiness of this code is not really a concern.
4294
4295 function Build_Task_Array_Image
4296 (Loc : Source_Ptr;
4297 Id_Ref : Node_Id;
4298 A_Type : Entity_Id;
4299 Dyn : Boolean := False) return Node_Id
4300 is
4301 Dims : constant Nat := Number_Dimensions (A_Type);
4302 -- Number of dimensions for array of tasks
4303
4304 Temps : array (1 .. Dims) of Entity_Id;
4305 -- Array of temporaries to hold string for each index
4306
4307 Indx : Node_Id;
4308 -- Index expression
4309
4310 Len : Entity_Id;
4311 -- Total length of generated name
4312
4313 Pos : Entity_Id;
4314 -- Running index for substring assignments
4315
4316 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4317 -- Name of enclosing variable, prefix of resulting name
4318
4319 Res : Entity_Id;
4320 -- String to hold result
4321
4322 Val : Node_Id;
4323 -- Value of successive indexes
4324
4325 Sum : Node_Id;
4326 -- Expression to compute total size of string
4327
4328 T : Entity_Id;
4329 -- Entity for name at one index position
4330
4331 Decls : constant List_Id := New_List;
4332 Stats : constant List_Id := New_List;
4333
4334 begin
4335 -- For a dynamic task, the name comes from the target variable. For a
4336 -- static one it is a formal of the enclosing init proc.
4337
4338 if Dyn then
4339 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4340 Append_To (Decls,
4341 Make_Object_Declaration (Loc,
4342 Defining_Identifier => Pref,
4343 Constant_Present => True,
4344 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4345 Expression =>
4346 Make_String_Literal (Loc,
4347 Strval => String_From_Name_Buffer)));
4348
4349 else
4350 Append_To (Decls,
4351 Make_Object_Renaming_Declaration (Loc,
4352 Defining_Identifier => Pref,
4353 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4354 Name => Make_Identifier (Loc, Name_uTask_Name)));
4355 end if;
4356
4357 Indx := First_Index (A_Type);
4358 Val := First (Expressions (Id_Ref));
4359
4360 for J in 1 .. Dims loop
4361 T := Make_Temporary (Loc, 'T');
4362 Temps (J) := T;
4363
4364 Append_To (Decls,
4365 Make_Object_Declaration (Loc,
4366 Defining_Identifier => T,
4367 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4368 Constant_Present => True,
4369 Expression =>
4370 Make_Attribute_Reference (Loc,
4371 Attribute_Name => Name_Image,
4372 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
4373 Expressions => New_List (New_Copy_Tree (Val)))));
4374
4375 Next_Index (Indx);
4376 Next (Val);
4377 end loop;
4378
4379 Sum := Make_Integer_Literal (Loc, Dims + 1);
4380
4381 Sum :=
4382 Make_Op_Add (Loc,
4383 Left_Opnd => Sum,
4384 Right_Opnd =>
4385 Make_Attribute_Reference (Loc,
4386 Attribute_Name => Name_Length,
4387 Prefix => New_Occurrence_Of (Pref, Loc),
4388 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4389
4390 for J in 1 .. Dims loop
4391 Sum :=
4392 Make_Op_Add (Loc,
4393 Left_Opnd => Sum,
4394 Right_Opnd =>
4395 Make_Attribute_Reference (Loc,
4396 Attribute_Name => Name_Length,
4397 Prefix =>
4398 New_Occurrence_Of (Temps (J), Loc),
4399 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4400 end loop;
4401
4402 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4403
4404 Set_Character_Literal_Name (Get_Char_Code ('('));
4405
4406 Append_To (Stats,
4407 Make_Assignment_Statement (Loc,
4408 Name =>
4409 Make_Indexed_Component (Loc,
4410 Prefix => New_Occurrence_Of (Res, Loc),
4411 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4412 Expression =>
4413 Make_Character_Literal (Loc,
4414 Chars => Name_Find,
4415 Char_Literal_Value => UI_From_CC (Get_Char_Code ('(')))));
4416
4417 Append_To (Stats,
4418 Make_Assignment_Statement (Loc,
4419 Name => New_Occurrence_Of (Pos, Loc),
4420 Expression =>
4421 Make_Op_Add (Loc,
4422 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4423 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4424
4425 for J in 1 .. Dims loop
4426
4427 Append_To (Stats,
4428 Make_Assignment_Statement (Loc,
4429 Name =>
4430 Make_Slice (Loc,
4431 Prefix => New_Occurrence_Of (Res, Loc),
4432 Discrete_Range =>
4433 Make_Range (Loc,
4434 Low_Bound => New_Occurrence_Of (Pos, Loc),
4435 High_Bound =>
4436 Make_Op_Subtract (Loc,
4437 Left_Opnd =>
4438 Make_Op_Add (Loc,
4439 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4440 Right_Opnd =>
4441 Make_Attribute_Reference (Loc,
4442 Attribute_Name => Name_Length,
4443 Prefix =>
4444 New_Occurrence_Of (Temps (J), Loc),
4445 Expressions =>
4446 New_List (Make_Integer_Literal (Loc, 1)))),
4447 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
4448
4449 Expression => New_Occurrence_Of (Temps (J), Loc)));
4450
4451 if J < Dims then
4452 Append_To (Stats,
4453 Make_Assignment_Statement (Loc,
4454 Name => New_Occurrence_Of (Pos, Loc),
4455 Expression =>
4456 Make_Op_Add (Loc,
4457 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4458 Right_Opnd =>
4459 Make_Attribute_Reference (Loc,
4460 Attribute_Name => Name_Length,
4461 Prefix => New_Occurrence_Of (Temps (J), Loc),
4462 Expressions =>
4463 New_List (Make_Integer_Literal (Loc, 1))))));
4464
4465 Set_Character_Literal_Name (Get_Char_Code (','));
4466
4467 Append_To (Stats,
4468 Make_Assignment_Statement (Loc,
4469 Name => Make_Indexed_Component (Loc,
4470 Prefix => New_Occurrence_Of (Res, Loc),
4471 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4472 Expression =>
4473 Make_Character_Literal (Loc,
4474 Chars => Name_Find,
4475 Char_Literal_Value => UI_From_CC (Get_Char_Code (',')))));
4476
4477 Append_To (Stats,
4478 Make_Assignment_Statement (Loc,
4479 Name => New_Occurrence_Of (Pos, Loc),
4480 Expression =>
4481 Make_Op_Add (Loc,
4482 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4483 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4484 end if;
4485 end loop;
4486
4487 Set_Character_Literal_Name (Get_Char_Code (')'));
4488
4489 Append_To (Stats,
4490 Make_Assignment_Statement (Loc,
4491 Name =>
4492 Make_Indexed_Component (Loc,
4493 Prefix => New_Occurrence_Of (Res, Loc),
4494 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
4495 Expression =>
4496 Make_Character_Literal (Loc,
4497 Chars => Name_Find,
4498 Char_Literal_Value => UI_From_CC (Get_Char_Code (')')))));
4499 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4500 end Build_Task_Array_Image;
4501
4502 ----------------------------
4503 -- Build_Task_Image_Decls --
4504 ----------------------------
4505
4506 function Build_Task_Image_Decls
4507 (Loc : Source_Ptr;
4508 Id_Ref : Node_Id;
4509 A_Type : Entity_Id;
4510 In_Init_Proc : Boolean := False) return List_Id
4511 is
4512 Decls : constant List_Id := New_List;
4513 T_Id : Entity_Id := Empty;
4514 Decl : Node_Id;
4515 Expr : Node_Id := Empty;
4516 Fun : Node_Id := Empty;
4517 Is_Dyn : constant Boolean :=
4518 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
4519 and then
4520 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
4521 Id : constant Node_Id :=
4522 (if Nkind (Id_Ref) = N_Expanded_Name then Selector_Name (Id_Ref)
4523 else Id_Ref);
4524 -- Replace expanded_name X.Y with Y
4525
4526 Component_Suffix_Index : constant Int :=
4527 (if In_Init_Proc then -1 else 0);
4528 -- If an init proc calls Build_Task_Image_Decls twice for its
4529 -- _Parent component (to split early/late initialization), we don't
4530 -- want two decls with the same name. Hence, the -1 suffix.
4531
4532 begin
4533 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
4534 -- generate a dummy declaration only.
4535
4536 if Restriction_Active (No_Implicit_Heap_Allocations)
4537 or else Global_Discard_Names
4538 then
4539 T_Id := Make_Temporary (Loc, 'J');
4540 Name_Len := 0;
4541
4542 return
4543 New_List (
4544 Make_Object_Declaration (Loc,
4545 Defining_Identifier => T_Id,
4546 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4547 Expression =>
4548 Make_String_Literal (Loc,
4549 Strval => String_From_Name_Buffer)));
4550 end if;
4551
4552 case Nkind (Id) is
4553 when N_Identifier | N_Defining_Identifier =>
4554 -- For a simple variable, the image of the task is built from
4555 -- the name of the variable. To avoid possible conflict with the
4556 -- anonymous type created for a single protected object, add a
4557 -- numeric suffix.
4558
4559 T_Id :=
4560 Make_Defining_Identifier (Loc,
4561 New_External_Name (Chars (Id), 'T', 1));
4562
4563 Get_Name_String (Chars (Id));
4564
4565 Expr :=
4566 Make_String_Literal (Loc,
4567 Strval => String_From_Name_Buffer);
4568
4569 when N_Selected_Component =>
4570 T_Id :=
4571 Make_Defining_Identifier (Loc,
4572 New_External_Name (Chars (Selector_Name (Id)), 'T',
4573 Suffix_Index => Component_Suffix_Index));
4574 Fun := Build_Task_Record_Image (Loc, Id, Is_Dyn);
4575
4576 when N_Indexed_Component =>
4577 T_Id :=
4578 Make_Defining_Identifier (Loc,
4579 New_External_Name (Chars (A_Type), 'N'));
4580
4581 Fun := Build_Task_Array_Image (Loc, Id, A_Type, Is_Dyn);
4582
4583 when others =>
4584 raise Program_Error;
4585 end case;
4586
4587 if Present (Fun) then
4588 Append (Fun, Decls);
4589 Expr := Make_Function_Call (Loc,
4590 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
4591
4592 if not In_Init_Proc then
4593 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4594 end if;
4595 end if;
4596
4597 Decl := Make_Object_Declaration (Loc,
4598 Defining_Identifier => T_Id,
4599 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4600 Constant_Present => True,
4601 Expression => Expr);
4602
4603 Append (Decl, Decls);
4604 return Decls;
4605 end Build_Task_Image_Decls;
4606
4607 -------------------------------
4608 -- Build_Task_Image_Function --
4609 -------------------------------
4610
4611 function Build_Task_Image_Function
4612 (Loc : Source_Ptr;
4613 Decls : List_Id;
4614 Stats : List_Id;
4615 Res : Entity_Id) return Node_Id
4616 is
4617 Spec : Node_Id;
4618
4619 begin
4620 Append_To (Stats,
4621 Make_Simple_Return_Statement (Loc,
4622 Expression => New_Occurrence_Of (Res, Loc)));
4623
4624 Spec := Make_Function_Specification (Loc,
4625 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4626 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4627
4628 -- Calls to 'Image use the secondary stack, which must be cleaned up
4629 -- after the task name is built.
4630
4631 return Make_Subprogram_Body (Loc,
4632 Specification => Spec,
4633 Declarations => Decls,
4634 Handled_Statement_Sequence =>
4635 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4636 end Build_Task_Image_Function;
4637
4638 -----------------------------
4639 -- Build_Task_Image_Prefix --
4640 -----------------------------
4641
4642 procedure Build_Task_Image_Prefix
4643 (Loc : Source_Ptr;
4644 Len : out Entity_Id;
4645 Res : out Entity_Id;
4646 Pos : out Entity_Id;
4647 Prefix : Entity_Id;
4648 Sum : Node_Id;
4649 Decls : List_Id;
4650 Stats : List_Id)
4651 is
4652 begin
4653 Len := Make_Temporary (Loc, 'L', Sum);
4654
4655 Append_To (Decls,
4656 Make_Object_Declaration (Loc,
4657 Defining_Identifier => Len,
4658 Constant_Present => True,
4659 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4660 Expression => Sum));
4661
4662 Res := Make_Temporary (Loc, 'R');
4663
4664 Append_To (Decls,
4665 Make_Object_Declaration (Loc,
4666 Defining_Identifier => Res,
4667 Object_Definition =>
4668 Make_Subtype_Indication (Loc,
4669 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4670 Constraint =>
4671 Make_Index_Or_Discriminant_Constraint (Loc,
4672 Constraints =>
4673 New_List (
4674 Make_Range (Loc,
4675 Low_Bound => Make_Integer_Literal (Loc, 1),
4676 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4677
4678 -- Indicate that the result is an internal temporary, so it does not
4679 -- receive a bogus initialization when declaration is expanded. This
4680 -- is both efficient, and prevents anomalies in the handling of
4681 -- dynamic objects on the secondary stack.
4682
4683 Set_Is_Internal (Res);
4684 Pos := Make_Temporary (Loc, 'P');
4685
4686 Append_To (Decls,
4687 Make_Object_Declaration (Loc,
4688 Defining_Identifier => Pos,
4689 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4690
4691 -- Pos := Prefix'Length;
4692
4693 Append_To (Stats,
4694 Make_Assignment_Statement (Loc,
4695 Name => New_Occurrence_Of (Pos, Loc),
4696 Expression =>
4697 Make_Attribute_Reference (Loc,
4698 Attribute_Name => Name_Length,
4699 Prefix => New_Occurrence_Of (Prefix, Loc),
4700 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4701
4702 -- Res (1 .. Pos) := Prefix;
4703
4704 Append_To (Stats,
4705 Make_Assignment_Statement (Loc,
4706 Name =>
4707 Make_Slice (Loc,
4708 Prefix => New_Occurrence_Of (Res, Loc),
4709 Discrete_Range =>
4710 Make_Range (Loc,
4711 Low_Bound => Make_Integer_Literal (Loc, 1),
4712 High_Bound => New_Occurrence_Of (Pos, Loc))),
4713
4714 Expression => New_Occurrence_Of (Prefix, Loc)));
4715
4716 Append_To (Stats,
4717 Make_Assignment_Statement (Loc,
4718 Name => New_Occurrence_Of (Pos, Loc),
4719 Expression =>
4720 Make_Op_Add (Loc,
4721 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4722 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4723 end Build_Task_Image_Prefix;
4724
4725 -----------------------------
4726 -- Build_Task_Record_Image --
4727 -----------------------------
4728
4729 function Build_Task_Record_Image
4730 (Loc : Source_Ptr;
4731 Id_Ref : Node_Id;
4732 Dyn : Boolean := False) return Node_Id
4733 is
4734 Len : Entity_Id;
4735 -- Total length of generated name
4736
4737 Pos : Entity_Id;
4738 -- Index into result
4739
4740 Res : Entity_Id;
4741 -- String to hold result
4742
4743 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4744 -- Name of enclosing variable, prefix of resulting name
4745
4746 Sum : Node_Id;
4747 -- Expression to compute total size of string
4748
4749 Sel : Entity_Id;
4750 -- Entity for selector name
4751
4752 Decls : constant List_Id := New_List;
4753 Stats : constant List_Id := New_List;
4754
4755 begin
4756 -- For a dynamic task, the name comes from the target variable. For a
4757 -- static one it is a formal of the enclosing init proc.
4758
4759 if Dyn then
4760 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4761 Append_To (Decls,
4762 Make_Object_Declaration (Loc,
4763 Defining_Identifier => Pref,
4764 Constant_Present => True,
4765 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4766 Expression =>
4767 Make_String_Literal (Loc,
4768 Strval => String_From_Name_Buffer)));
4769
4770 else
4771 Append_To (Decls,
4772 Make_Object_Renaming_Declaration (Loc,
4773 Defining_Identifier => Pref,
4774 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4775 Name => Make_Identifier (Loc, Name_uTask_Name)));
4776 end if;
4777
4778 Sel := Make_Temporary (Loc, 'S');
4779
4780 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4781
4782 Append_To (Decls,
4783 Make_Object_Declaration (Loc,
4784 Defining_Identifier => Sel,
4785 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4786 Expression =>
4787 Make_String_Literal (Loc,
4788 Strval => String_From_Name_Buffer)));
4789
4790 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4791
4792 Sum :=
4793 Make_Op_Add (Loc,
4794 Left_Opnd => Sum,
4795 Right_Opnd =>
4796 Make_Attribute_Reference (Loc,
4797 Attribute_Name => Name_Length,
4798 Prefix =>
4799 New_Occurrence_Of (Pref, Loc),
4800 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4801
4802 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4803
4804 Set_Character_Literal_Name (Get_Char_Code ('.'));
4805
4806 -- Res (Pos) := '.';
4807
4808 Append_To (Stats,
4809 Make_Assignment_Statement (Loc,
4810 Name => Make_Indexed_Component (Loc,
4811 Prefix => New_Occurrence_Of (Res, Loc),
4812 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4813 Expression =>
4814 Make_Character_Literal (Loc,
4815 Chars => Name_Find,
4816 Char_Literal_Value =>
4817 UI_From_CC (Get_Char_Code ('.')))));
4818
4819 Append_To (Stats,
4820 Make_Assignment_Statement (Loc,
4821 Name => New_Occurrence_Of (Pos, Loc),
4822 Expression =>
4823 Make_Op_Add (Loc,
4824 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4825 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4826
4827 -- Res (Pos .. Len) := Selector;
4828
4829 Append_To (Stats,
4830 Make_Assignment_Statement (Loc,
4831 Name => Make_Slice (Loc,
4832 Prefix => New_Occurrence_Of (Res, Loc),
4833 Discrete_Range =>
4834 Make_Range (Loc,
4835 Low_Bound => New_Occurrence_Of (Pos, Loc),
4836 High_Bound => New_Occurrence_Of (Len, Loc))),
4837 Expression => New_Occurrence_Of (Sel, Loc)));
4838
4839 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4840 end Build_Task_Record_Image;
4841
4842 ----------------------------------------
4843 -- Build_Temporary_On_Secondary_Stack --
4844 ----------------------------------------
4845
4846 function Build_Temporary_On_Secondary_Stack
4847 (Loc : Source_Ptr;
4848 Typ : Entity_Id;
4849 Code : List_Id) return Entity_Id
4850 is
4851 Acc_Typ : Entity_Id;
4852 Alloc : Node_Id;
4853 Alloc_Obj : Entity_Id;
4854
4855 begin
4856 pragma Assert (RTE_Available (RE_SS_Pool)
4857 and then not Needs_Finalization (Typ));
4858
4859 Acc_Typ := Make_Temporary (Loc, 'A');
4860 Mutate_Ekind (Acc_Typ, E_Access_Type);
4861 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
4862
4863 Append_To (Code,
4864 Make_Full_Type_Declaration (Loc,
4865 Defining_Identifier => Acc_Typ,
4866 Type_Definition =>
4867 Make_Access_To_Object_Definition (Loc,
4868 All_Present => True,
4869 Subtype_Indication =>
4870 New_Occurrence_Of (Typ, Loc))));
4871
4872 Alloc :=
4873 Make_Allocator (Loc, Expression => New_Occurrence_Of (Typ, Loc));
4874 Set_No_Initialization (Alloc);
4875
4876 Alloc_Obj := Make_Temporary (Loc, 'R');
4877
4878 Append_To (Code,
4879 Make_Object_Declaration (Loc,
4880 Defining_Identifier => Alloc_Obj,
4881 Constant_Present => True,
4882 Object_Definition =>
4883 New_Occurrence_Of (Acc_Typ, Loc),
4884 Expression => Alloc));
4885
4886 Set_Uses_Sec_Stack (Current_Scope);
4887
4888 return Alloc_Obj;
4889 end Build_Temporary_On_Secondary_Stack;
4890
4891 -----------------------------
4892 -- Check_Float_Op_Overflow --
4893 -----------------------------
4894
4895 procedure Check_Float_Op_Overflow (N : Node_Id) is
4896 begin
4897 -- Return if no check needed
4898
4899 if not Is_Floating_Point_Type (Etype (N))
4900 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4901
4902 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4903 -- and do not expand the code for float overflow checking.
4904
4905 or else CodePeer_Mode
4906 then
4907 return;
4908 end if;
4909
4910 -- Otherwise we replace the expression by
4911
4912 -- do Tnn : constant ftype := expression;
4913 -- constraint_error when not Tnn'Valid;
4914 -- in Tnn;
4915
4916 declare
4917 Loc : constant Source_Ptr := Sloc (N);
4918 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4919 Typ : constant Entity_Id := Etype (N);
4920
4921 begin
4922 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4923 -- right here. We also set the node as analyzed to prevent infinite
4924 -- recursion from repeating the operation in the expansion.
4925
4926 Set_Do_Overflow_Check (N, False);
4927 Set_Analyzed (N, True);
4928
4929 -- Do the rewrite to include the check
4930
4931 Rewrite (N,
4932 Make_Expression_With_Actions (Loc,
4933 Actions => New_List (
4934 Make_Object_Declaration (Loc,
4935 Defining_Identifier => Tnn,
4936 Object_Definition => New_Occurrence_Of (Typ, Loc),
4937 Constant_Present => True,
4938 Expression => Relocate_Node (N)),
4939 Make_Raise_Constraint_Error (Loc,
4940 Condition =>
4941 Make_Op_Not (Loc,
4942 Right_Opnd =>
4943 Make_Attribute_Reference (Loc,
4944 Prefix => New_Occurrence_Of (Tnn, Loc),
4945 Attribute_Name => Name_Valid)),
4946 Reason => CE_Overflow_Check_Failed)),
4947 Expression => New_Occurrence_Of (Tnn, Loc)));
4948
4949 Analyze_And_Resolve (N, Typ);
4950 end;
4951 end Check_Float_Op_Overflow;
4952
4953 ----------------------------------
4954 -- Component_May_Be_Bit_Aligned --
4955 ----------------------------------
4956
4957 function Component_May_Be_Bit_Aligned
4958 (Comp : Entity_Id;
4959 For_Slice : Boolean := False) return Boolean
4960 is
4961 UT : Entity_Id;
4962
4963 begin
4964 -- If no component clause, then everything is fine, since the back end
4965 -- never misaligns from byte boundaries by default, even if there is a
4966 -- pragma Pack for the record.
4967
4968 if No (Comp) or else No (Component_Clause (Comp)) then
4969 return False;
4970 end if;
4971
4972 UT := Underlying_Type (Etype (Comp));
4973
4974 -- It is only array and record types that cause trouble
4975
4976 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4977 return False;
4978
4979 -- If we know that we have a small (at most the maximum integer size)
4980 -- bit-packed array or record without variant part, then everything is
4981 -- fine, since the back end can handle these cases correctly, except if
4982 -- a slice is involved.
4983
4984 elsif Known_Esize (Comp)
4985 and then Esize (Comp) <= System_Max_Integer_Size
4986 and then (Is_Bit_Packed_Array (UT)
4987 or else (Is_Record_Type (UT)
4988 and then not Has_Variant_Part (UT)))
4989 and then not For_Slice
4990 then
4991 return False;
4992
4993 elsif not Known_Normalized_First_Bit (Comp) then
4994 return True;
4995
4996 -- Otherwise if the component is not byte aligned, we know we have the
4997 -- nasty unaligned case.
4998
4999 elsif Normalized_First_Bit (Comp) /= Uint_0
5000 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
5001 then
5002 return True;
5003
5004 -- If we are large and byte aligned, then OK at this level
5005
5006 else
5007 return False;
5008 end if;
5009 end Component_May_Be_Bit_Aligned;
5010
5011 -------------------------------
5012 -- Convert_To_Actual_Subtype --
5013 -------------------------------
5014
5015 procedure Convert_To_Actual_Subtype (Exp : Node_Id) is
5016 Act_ST : Entity_Id;
5017
5018 begin
5019 Act_ST := Get_Actual_Subtype (Exp);
5020
5021 if Act_ST = Etype (Exp) then
5022 return;
5023 else
5024 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
5025 Analyze_And_Resolve (Exp, Act_ST);
5026 end if;
5027 end Convert_To_Actual_Subtype;
5028
5029 -----------------------------------
5030 -- Corresponding_Runtime_Package --
5031 -----------------------------------
5032
5033 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
5034 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
5035 -- Return True if protected type T has one entry and the maximum queue
5036 -- length is one.
5037
5038 --------------------------------
5039 -- Has_One_Entry_And_No_Queue --
5040 --------------------------------
5041
5042 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
5043 Item : Entity_Id;
5044 Is_First : Boolean := True;
5045
5046 begin
5047 Item := First_Entity (T);
5048 while Present (Item) loop
5049 if Is_Entry (Item) then
5050
5051 -- The protected type has more than one entry
5052
5053 if not Is_First then
5054 return False;
5055 end if;
5056
5057 -- The queue length is not one
5058
5059 if not Restriction_Active (No_Entry_Queue)
5060 and then Get_Max_Queue_Length (Item) /= Uint_1
5061 then
5062 return False;
5063 end if;
5064
5065 Is_First := False;
5066 end if;
5067
5068 Next_Entity (Item);
5069 end loop;
5070
5071 return True;
5072 end Has_One_Entry_And_No_Queue;
5073
5074 -- Local variables
5075
5076 Pkg_Id : RTU_Id := RTU_Null;
5077
5078 -- Start of processing for Corresponding_Runtime_Package
5079
5080 begin
5081 pragma Assert (Is_Concurrent_Type (Typ));
5082
5083 if Is_Protected_Type (Typ) then
5084 if Has_Entries (Typ)
5085
5086 -- A protected type without entries that covers an interface and
5087 -- overrides the abstract routines with protected procedures is
5088 -- considered equivalent to a protected type with entries in the
5089 -- context of dispatching select statements. It is sufficient to
5090 -- check for the presence of an interface list in the declaration
5091 -- node to recognize this case.
5092
5093 or else Present (Interface_List (Parent (Typ)))
5094
5095 -- Protected types with interrupt handlers (when not using a
5096 -- restricted profile) are also considered equivalent to
5097 -- protected types with entries. The types which are used
5098 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
5099 -- are derived from Protection_Entries.
5100
5101 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
5102 or else Has_Interrupt_Handler (Typ)
5103 then
5104 if Abort_Allowed
5105 or else Restriction_Active (No_Select_Statements) = False
5106 or else not Has_One_Entry_And_No_Queue (Typ)
5107 or else (Has_Attach_Handler (Typ)
5108 and then not Restricted_Profile)
5109 then
5110 Pkg_Id := System_Tasking_Protected_Objects_Entries;
5111 else
5112 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
5113 end if;
5114
5115 else
5116 Pkg_Id := System_Tasking_Protected_Objects;
5117 end if;
5118 end if;
5119
5120 return Pkg_Id;
5121 end Corresponding_Runtime_Package;
5122
5123 -----------------------------------
5124 -- Current_Sem_Unit_Declarations --
5125 -----------------------------------
5126
5127 function Current_Sem_Unit_Declarations return List_Id is
5128 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
5129 Decls : List_Id;
5130
5131 begin
5132 -- If the current unit is a package body, locate the visible
5133 -- declarations of the package spec.
5134
5135 if Nkind (U) = N_Package_Body then
5136 U := Unit (Spec_Lib_Unit (Cunit (Current_Sem_Unit)));
5137 end if;
5138
5139 if Nkind (U) = N_Package_Declaration then
5140 U := Specification (U);
5141 Decls := Visible_Declarations (U);
5142
5143 if No (Decls) then
5144 Decls := New_List;
5145 Set_Visible_Declarations (U, Decls);
5146 end if;
5147
5148 else
5149 Decls := Declarations (U);
5150
5151 if No (Decls) then
5152 Decls := New_List;
5153 Set_Declarations (U, Decls);
5154 end if;
5155 end if;
5156
5157 return Decls;
5158 end Current_Sem_Unit_Declarations;
5159
5160 -------------------------------------------
5161 -- Delay_Conditional_Expressions_Between --
5162 -------------------------------------------
5163
5164 procedure Delay_Conditional_Expressions_Between (From, To : Node_Id) is
5165 N : Node_Id := From;
5166
5167 begin
5168 while N /= To loop
5169 if Nkind (N) in N_Case_Expression | N_If_Expression then
5170 Set_Expansion_Delayed (N);
5171 end if;
5172
5173 N := Parent (N);
5174 end loop;
5175 end Delay_Conditional_Expressions_Between;
5176
5177 -----------------------
5178 -- Duplicate_Subexpr --
5179 -----------------------
5180
5181 function Duplicate_Subexpr
5182 (Exp : Node_Id;
5183 New_Scope : Entity_Id := Empty;
5184 Name_Req : Boolean := False;
5185 Renaming_Req : Boolean := False) return Node_Id
5186 is
5187 begin
5188 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5189 return New_Copy_Tree (Exp, New_Scope => New_Scope);
5190 end Duplicate_Subexpr;
5191
5192 ---------------------------------
5193 -- Duplicate_Subexpr_No_Checks --
5194 ---------------------------------
5195
5196 function Duplicate_Subexpr_No_Checks
5197 (Exp : Node_Id;
5198 New_Scope : Entity_Id := Empty;
5199 Name_Req : Boolean := False;
5200 Renaming_Req : Boolean := False) return Node_Id
5201 is
5202 New_Exp : Node_Id;
5203
5204 begin
5205 Remove_Side_Effects
5206 (Exp => Exp,
5207 Name_Req => Name_Req,
5208 Renaming_Req => Renaming_Req);
5209
5210 New_Exp := New_Copy_Tree (Exp, New_Scope => New_Scope);
5211 Remove_Checks (New_Exp);
5212 return New_Exp;
5213 end Duplicate_Subexpr_No_Checks;
5214
5215 -----------------------------------
5216 -- Duplicate_Subexpr_Move_Checks --
5217 -----------------------------------
5218
5219 function Duplicate_Subexpr_Move_Checks
5220 (Exp : Node_Id;
5221 New_Scope : Entity_Id := Empty;
5222 Name_Req : Boolean := False;
5223 Renaming_Req : Boolean := False) return Node_Id
5224 is
5225 New_Exp : Node_Id;
5226
5227 begin
5228 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5229 New_Exp := New_Copy_Tree (Exp, New_Scope => New_Scope);
5230 Remove_Checks (Exp);
5231 return New_Exp;
5232 end Duplicate_Subexpr_Move_Checks;
5233
5234 -------------------------
5235 -- Enclosing_Init_Proc --
5236 -------------------------
5237
5238 function Enclosing_Init_Proc return Entity_Id is
5239 S : Entity_Id;
5240
5241 begin
5242 S := Current_Scope;
5243 while Present (S) and then S /= Standard_Standard loop
5244 if Is_Init_Proc (S) then
5245 return S;
5246 else
5247 S := Scope (S);
5248 end if;
5249 end loop;
5250
5251 return Empty;
5252 end Enclosing_Init_Proc;
5253
5254 --------------------
5255 -- Ensure_Defined --
5256 --------------------
5257
5258 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
5259 IR : Node_Id;
5260
5261 begin
5262 -- An itype reference must only be created if this is a local itype, so
5263 -- that gigi can elaborate it on the proper objstack.
5264
5265 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
5266 IR := Make_Itype_Reference (Sloc (N));
5267 Set_Itype (IR, Typ);
5268 Insert_Action (N, IR);
5269 end if;
5270 end Ensure_Defined;
5271
5272 -------------------
5273 -- Evaluate_Name --
5274 -------------------
5275
5276 procedure Evaluate_Name (Nam : Node_Id) is
5277 begin
5278 case Nkind (Nam) is
5279 -- For an aggregate, force its evaluation
5280
5281 when N_Aggregate =>
5282 Force_Evaluation (Nam);
5283
5284 -- For an attribute reference or an indexed component, evaluate the
5285 -- prefix, which is itself a name, recursively, and then force the
5286 -- evaluation of all the subscripts (or attribute expressions).
5287
5288 when N_Attribute_Reference
5289 | N_Indexed_Component
5290 =>
5291 Evaluate_Name (Prefix (Nam));
5292
5293 declare
5294 E : Node_Id;
5295
5296 begin
5297 E := First (Expressions (Nam));
5298 while Present (E) loop
5299 Force_Evaluation (E);
5300
5301 if Is_Rewrite_Substitution (E) then
5302 Set_Do_Range_Check
5303 (E, Do_Range_Check (Original_Node (E)));
5304 end if;
5305
5306 Next (E);
5307 end loop;
5308 end;
5309
5310 -- For an explicit dereference, we simply force the evaluation of
5311 -- the name expression. The dereference provides a value that is the
5312 -- address for the renamed object, and it is precisely this value
5313 -- that we want to preserve.
5314
5315 when N_Explicit_Dereference =>
5316 Force_Evaluation (Prefix (Nam));
5317
5318 -- For a function call, we evaluate the call; same for an operator
5319
5320 when N_Function_Call
5321 | N_Op
5322 =>
5323 Force_Evaluation (Nam);
5324
5325 -- For a qualified expression, we evaluate the expression
5326
5327 when N_Qualified_Expression =>
5328 Evaluate_Name (Expression (Nam));
5329
5330 -- For a selected component, we simply evaluate the prefix
5331
5332 when N_Selected_Component =>
5333 Evaluate_Name (Prefix (Nam));
5334
5335 -- For a slice, we evaluate the prefix, as for the indexed component
5336 -- case and then, if there is a range present, either directly or as
5337 -- the constraint of a discrete subtype indication, we evaluate the
5338 -- two bounds of this range.
5339
5340 when N_Slice =>
5341 Evaluate_Name (Prefix (Nam));
5342 Evaluate_Slice_Bounds (Nam);
5343
5344 -- For a type conversion, the expression of the conversion must be
5345 -- the name of an object, and we simply need to evaluate this name.
5346
5347 when N_Type_Conversion =>
5348 Evaluate_Name (Expression (Nam));
5349
5350 -- The remaining cases are direct name and character literal. In all
5351 -- these cases, we do nothing, since we want to reevaluate each time
5352 -- the renamed object is used. ??? There are more remaining cases, at
5353 -- least in the GNATprove_Mode, where this routine is called in more
5354 -- contexts than in GNAT.
5355
5356 when others =>
5357 null;
5358 end case;
5359 end Evaluate_Name;
5360
5361 ---------------------------
5362 -- Evaluate_Slice_Bounds --
5363 ---------------------------
5364
5365 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
5366 DR : constant Node_Id := Discrete_Range (Slice);
5367 Constr : Node_Id;
5368 Rexpr : Node_Id;
5369
5370 begin
5371 if Nkind (DR) = N_Range then
5372 Force_Evaluation (Low_Bound (DR));
5373 Force_Evaluation (High_Bound (DR));
5374
5375 elsif Nkind (DR) = N_Subtype_Indication then
5376 Constr := Constraint (DR);
5377
5378 if Nkind (Constr) = N_Range_Constraint then
5379 Rexpr := Range_Expression (Constr);
5380
5381 Force_Evaluation (Low_Bound (Rexpr));
5382 Force_Evaluation (High_Bound (Rexpr));
5383 end if;
5384 end if;
5385 end Evaluate_Slice_Bounds;
5386
5387 ---------------------
5388 -- Evolve_And_Then --
5389 ---------------------
5390
5391 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
5392 begin
5393 if No (Cond) then
5394 Cond := Cond1;
5395 else
5396 Cond :=
5397 Make_And_Then (Sloc (Cond1),
5398 Left_Opnd => Cond,
5399 Right_Opnd => Cond1);
5400 end if;
5401 end Evolve_And_Then;
5402
5403 --------------------
5404 -- Evolve_Or_Else --
5405 --------------------
5406
5407 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
5408 begin
5409 if No (Cond) then
5410 Cond := Cond1;
5411 else
5412 Cond :=
5413 Make_Or_Else (Sloc (Cond1),
5414 Left_Opnd => Cond,
5415 Right_Opnd => Cond1);
5416 end if;
5417 end Evolve_Or_Else;
5418
5419 -------------------------------
5420 -- Expand_Sliding_Conversion --
5421 -------------------------------
5422
5423 procedure Expand_Sliding_Conversion (N : Node_Id; Arr_Typ : Entity_Id) is
5424
5425 pragma Assert (Is_Array_Type (Arr_Typ)
5426 and then not Is_Constrained (Arr_Typ)
5427 and then Is_Fixed_Lower_Bound_Array_Subtype (Arr_Typ));
5428
5429 Constraints : List_Id;
5430 Index : Node_Id := First_Index (Arr_Typ);
5431 Loc : constant Source_Ptr := Sloc (N);
5432 Subt_Decl : Node_Id;
5433 Subt : Entity_Id;
5434 Subt_Low : Node_Id;
5435 Subt_High : Node_Id;
5436
5437 Act_Subt : Entity_Id;
5438 Act_Index : Node_Id;
5439 Act_Low : Node_Id;
5440 Act_High : Node_Id;
5441 Adjust_Incr : Node_Id;
5442 Dimension : Int := 0;
5443 All_FLBs_Match : Boolean := True;
5444
5445 begin
5446 -- This procedure is called during semantic analysis, and we only expand
5447 -- a sliding conversion when Expander_Active, to avoid doing it during
5448 -- preanalysis (which can lead to problems with the target subtype not
5449 -- getting properly expanded during later full analysis). Also, sliding
5450 -- should never be needed for string literals, because their bounds are
5451 -- determined directly based on the fixed lower bound of Arr_Typ and
5452 -- their length.
5453
5454 if Expander_Active and then Nkind (N) /= N_String_Literal then
5455 Constraints := New_List;
5456
5457 Act_Subt := Get_Actual_Subtype (N);
5458 Act_Index := First_Index (Act_Subt);
5459
5460 -- Loop over the indexes of the fixed-lower-bound array type or
5461 -- subtype to build up an index constraint for constructing the
5462 -- subtype that will be the target of a conversion of the array
5463 -- object that may need a sliding conversion.
5464
5465 while Present (Index) loop
5466 pragma Assert (Present (Act_Index));
5467
5468 Dimension := Dimension + 1;
5469
5470 Get_Index_Bounds (Act_Index, Act_Low, Act_High);
5471
5472 -- If Index defines a normal unconstrained range (range <>),
5473 -- then we will simply use the bounds of the actual subtype's
5474 -- corresponding index range.
5475
5476 if not Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index)) then
5477 Subt_Low := Act_Low;
5478 Subt_High := Act_High;
5479
5480 -- Otherwise, a range will be created with a low bound given by
5481 -- the fixed lower bound of the array subtype's index, and with
5482 -- high bound given by (Actual'Length + fixed lower bound - 1).
5483
5484 else
5485 if Nkind (Index) = N_Subtype_Indication then
5486 Subt_Low :=
5487 New_Copy_Tree
5488 (Low_Bound (Range_Expression (Constraint (Index))));
5489 else
5490 pragma Assert (Nkind (Index) = N_Range);
5491
5492 Subt_Low := New_Copy_Tree (Low_Bound (Index));
5493 end if;
5494
5495 -- If either we have a nonstatic lower bound, or the target and
5496 -- source subtypes are statically known to have unequal lower
5497 -- bounds, then we will need to make a subtype conversion to
5498 -- slide the bounds. However, if all of the indexes' lower
5499 -- bounds are static and known to be equal (the common case),
5500 -- then no conversion will be needed, and we'll end up not
5501 -- creating the subtype or the conversion (though we still
5502 -- build up the index constraint, which will simply be unused).
5503
5504 if not (Compile_Time_Known_Value (Subt_Low)
5505 and then Compile_Time_Known_Value (Act_Low))
5506 or else Expr_Value (Subt_Low) /= Expr_Value (Act_Low)
5507 then
5508 All_FLBs_Match := False;
5509 end if;
5510
5511 -- Apply 'Pos to lower bound, which may be of an enumeration
5512 -- type, before subtracting.
5513
5514 Adjust_Incr :=
5515 Make_Op_Subtract (Loc,
5516 Make_Attribute_Reference (Loc,
5517 Prefix =>
5518 New_Occurrence_Of (Etype (Act_Index), Loc),
5519 Attribute_Name =>
5520 Name_Pos,
5521 Expressions =>
5522 New_List (New_Copy_Tree (Subt_Low))),
5523 Make_Integer_Literal (Loc, 1));
5524
5525 -- Apply 'Val to the result of adding the increment to the
5526 -- length, to handle indexes of enumeration types.
5527
5528 Subt_High :=
5529 Make_Attribute_Reference (Loc,
5530 Prefix =>
5531 New_Occurrence_Of (Etype (Act_Index), Loc),
5532 Attribute_Name =>
5533 Name_Val,
5534 Expressions =>
5535 New_List (Make_Op_Add (Loc,
5536 Make_Attribute_Reference (Loc,
5537 Prefix =>
5538 New_Occurrence_Of (Act_Subt, Loc),
5539 Attribute_Name =>
5540 Name_Length,
5541 Expressions =>
5542 New_List
5543 (Make_Integer_Literal
5544 (Loc, Dimension))),
5545 Adjust_Incr)));
5546 end if;
5547
5548 Append (Make_Range (Loc, Subt_Low, Subt_High), Constraints);
5549
5550 Next (Index);
5551 Next (Act_Index);
5552 end loop;
5553
5554 -- If for each index with a fixed lower bound (FLB), the lower bound
5555 -- of the corresponding index of the actual subtype is statically
5556 -- known be equal to the FLB, then a sliding conversion isn't needed
5557 -- at all, so just return without building a subtype or conversion.
5558
5559 if All_FLBs_Match then
5560 return;
5561 end if;
5562
5563 -- A sliding conversion is needed, so create the target subtype using
5564 -- the index constraint created above, and rewrite the expression
5565 -- as a conversion to that subtype.
5566
5567 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
5568 Set_Is_Internal (Subt);
5569
5570 Subt_Decl :=
5571 Make_Subtype_Declaration (Loc,
5572 Defining_Identifier => Subt,
5573 Subtype_Indication =>
5574 Make_Subtype_Indication (Loc,
5575 Subtype_Mark =>
5576 New_Occurrence_Of (Arr_Typ, Loc),
5577 Constraint =>
5578 Make_Index_Or_Discriminant_Constraint (Loc,
5579 Constraints => Constraints)));
5580
5581 Mark_Rewrite_Insertion (Subt_Decl);
5582
5583 -- The actual subtype is an Itype, so we analyze the declaration,
5584 -- but do not attach it to the tree.
5585
5586 Set_Parent (Subt_Decl, N);
5587 Set_Is_Itype (Subt);
5588 Analyze (Subt_Decl, Suppress => All_Checks);
5589 Set_Associated_Node_For_Itype (Subt, N);
5590 Set_Has_Delayed_Freeze (Subt, False);
5591
5592 -- We need to freeze the actual subtype immediately. This is needed
5593 -- because otherwise this Itype will not get frozen at all, and it is
5594 -- always safe to freeze on creation because any associated types
5595 -- must be frozen at this point.
5596
5597 Freeze_Itype (Subt, N);
5598
5599 Rewrite (N,
5600 Make_Type_Conversion (Loc,
5601 Subtype_Mark =>
5602 New_Occurrence_Of (Subt, Loc),
5603 Expression => Relocate_Node (N)));
5604 Analyze (N);
5605 end if;
5606 end Expand_Sliding_Conversion;
5607
5608 -----------------------------------------
5609 -- Expand_Static_Predicates_In_Choices --
5610 -----------------------------------------
5611
5612 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
5613 pragma Assert (Nkind (N) in N_Case_Statement_Alternative | N_Variant);
5614
5615 Choices : List_Id := Discrete_Choices (N);
5616
5617 Choice : Node_Id;
5618 Next_C : Node_Id;
5619 P : Node_Id;
5620 C : Node_Id;
5621
5622 begin
5623 -- If this is an "others" alternative, we need to process any static
5624 -- predicates in its Others_Discrete_Choices.
5625
5626 if Nkind (First (Choices)) = N_Others_Choice then
5627 Choices := Others_Discrete_Choices (First (Choices));
5628 end if;
5629
5630 Choice := First (Choices);
5631 while Present (Choice) loop
5632 Next_C := Next (Choice);
5633
5634 -- Check for name of subtype with static predicate
5635
5636 if Is_Entity_Name (Choice)
5637 and then Is_Type (Entity (Choice))
5638 and then Has_Predicates (Entity (Choice))
5639 then
5640 -- Loop through entries in predicate list, converting to choices
5641 -- and inserting in the list before the current choice. Note that
5642 -- if the list is empty, corresponding to a False predicate, then
5643 -- no choices are inserted.
5644
5645 P := First (Static_Discrete_Predicate (Entity (Choice)));
5646 while Present (P) loop
5647
5648 -- If low bound and high bounds are equal, copy simple choice
5649
5650 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
5651 C := New_Copy (Low_Bound (P));
5652
5653 -- Otherwise copy a range
5654
5655 else
5656 C := New_Copy (P);
5657 end if;
5658
5659 -- Change Sloc to referencing choice (rather than the Sloc of
5660 -- the predicate declaration element itself).
5661
5662 Set_Sloc (C, Sloc (Choice));
5663 Insert_Before (Choice, C);
5664 Next (P);
5665 end loop;
5666
5667 -- Delete the predicated entry
5668
5669 Remove (Choice);
5670 end if;
5671
5672 -- Move to next choice to check
5673
5674 Choice := Next_C;
5675 end loop;
5676
5677 Set_Has_SP_Choice (N, False);
5678 end Expand_Static_Predicates_In_Choices;
5679
5680 ------------------------------
5681 -- Expand_Subtype_From_Expr --
5682 ------------------------------
5683
5684 -- This function is applicable for both static and dynamic allocation of
5685 -- objects which are constrained by an initial expression. Basically it
5686 -- transforms an unconstrained subtype indication into a constrained one.
5687
5688 -- The expression may also be transformed in certain cases in order to
5689 -- avoid multiple evaluation. In the static allocation case, the general
5690 -- scheme is:
5691
5692 -- Val : T := Expr;
5693
5694 -- is transformed into
5695
5696 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5697 --
5698 -- Here are the main cases :
5699 --
5700 -- <if Expr is a Slice>
5701 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5702 --
5703 -- <elsif Expr is a String Literal>
5704 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5705 --
5706 -- <elsif Expr is Constrained>
5707 -- subtype T is Type_Of_Expr
5708 -- Val : T := Expr;
5709 --
5710 -- <elsif Expr is an entity_name>
5711 -- Val : T (constraints taken from Expr) := Expr;
5712 --
5713 -- <else>
5714 -- type Axxx is access all T;
5715 -- Rval : Axxx := Expr'ref;
5716 -- Val : T (constraints taken from Rval) := Rval.all;
5717
5718 -- ??? note: when the Expression is allocated in the secondary stack
5719 -- we could use it directly instead of copying it by declaring
5720 -- Val : T (...) renames Rval.all
5721
5722 procedure Expand_Subtype_From_Expr
5723 (N : Node_Id;
5724 Unc_Type : Entity_Id;
5725 Subtype_Indic : Node_Id;
5726 Exp : Node_Id;
5727 Related_Id : Entity_Id := Empty)
5728 is
5729 Loc : constant Source_Ptr := Sloc (N);
5730 Exp_Typ : constant Entity_Id := Etype (Exp);
5731 T : Entity_Id;
5732
5733 begin
5734 -- In general we cannot build the subtype if expansion is disabled,
5735 -- because internal entities may not have been defined. However, to
5736 -- avoid some cascaded errors, we try to continue when the expression is
5737 -- an array (or string), because it is safe to compute the bounds. It is
5738 -- in fact required to do so even in a generic context, because there
5739 -- may be constants that depend on the bounds of a string literal, both
5740 -- standard string types and more generally arrays of characters.
5741
5742 -- In GNATprove mode, these extra subtypes are not needed, unless Exp is
5743 -- a static expression. In that case, the subtype will be constrained
5744 -- while the original type might be unconstrained, so expanding the type
5745 -- is necessary both for passing legality checks in GNAT and for precise
5746 -- analysis in GNATprove.
5747
5748 if GNATprove_Mode and then not Is_Static_Expression (Exp) then
5749 return;
5750 end if;
5751
5752 if not Expander_Active
5753 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5754 then
5755 return;
5756 end if;
5757
5758 if Nkind (Exp) = N_Slice then
5759 declare
5760 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5761
5762 begin
5763 Rewrite (Subtype_Indic,
5764 Make_Subtype_Indication (Loc,
5765 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5766 Constraint =>
5767 Make_Index_Or_Discriminant_Constraint (Loc,
5768 Constraints => New_List
5769 (New_Occurrence_Of (Slice_Type, Loc)))));
5770
5771 -- This subtype indication may be used later for constraint checks
5772 -- we better make sure that if a variable was used as a bound of
5773 -- the original slice, its value is frozen.
5774
5775 Evaluate_Slice_Bounds (Exp);
5776 end;
5777
5778 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5779 Rewrite (Subtype_Indic,
5780 Make_Subtype_Indication (Loc,
5781 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5782 Constraint =>
5783 Make_Index_Or_Discriminant_Constraint (Loc,
5784 Constraints => New_List (
5785 Make_Literal_Range (Loc,
5786 Literal_Typ => Exp_Typ)))));
5787
5788 -- If the type of the expression is an internally generated type it
5789 -- may not be necessary to create a new subtype. However there are two
5790 -- exceptions: references to the current instances, and aliased array
5791 -- object declarations for which the back end has to create a template.
5792
5793 elsif Is_Constrained (Exp_Typ)
5794 and then not Is_Class_Wide_Type (Unc_Type)
5795 and then
5796 (Nkind (N) /= N_Object_Declaration
5797 or else not Is_Entity_Name (Expression (N))
5798 or else not Comes_From_Source (Entity (Expression (N)))
5799 or else not Is_Array_Type (Exp_Typ)
5800 or else not Aliased_Present (N))
5801 then
5802 if Is_Itype (Exp_Typ)
5803
5804 -- When this is for an object declaration, the caller may want to
5805 -- set Is_Constr_Subt_For_U_Nominal on the subtype, so we must make
5806 -- sure that either the subtype has been built for the expression,
5807 -- typically for an aggregate, or the flag is already set on it;
5808 -- otherwise it could end up being set on the nominal constrained
5809 -- subtype of an object and thus later cause the failure to detect
5810 -- non-statically-matching subtypes on 'Access of this object.
5811
5812 and then (Nkind (N) /= N_Object_Declaration
5813 or else Nkind (Original_Node (Exp)) = N_Aggregate
5814 or else Is_Constr_Subt_For_U_Nominal (Exp_Typ))
5815 then
5816 -- Within an initialization procedure, a selected component
5817 -- denotes a component of the enclosing record, and it appears as
5818 -- an actual in a call to its own initialization procedure. If
5819 -- this component depends on the outer discriminant, we must
5820 -- generate the proper actual subtype for it.
5821
5822 if Nkind (Exp) = N_Selected_Component
5823 and then Within_Init_Proc
5824 then
5825 declare
5826 Decl : constant Node_Id :=
5827 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5828 begin
5829 if Present (Decl) then
5830 Insert_Action (N, Decl);
5831 T := Defining_Identifier (Decl);
5832 else
5833 T := Exp_Typ;
5834 end if;
5835 end;
5836
5837 -- No need to generate a new subtype
5838
5839 else
5840 T := Exp_Typ;
5841 end if;
5842
5843 else
5844 T := Make_Temporary (Loc, 'T');
5845
5846 Insert_Action (N,
5847 Make_Subtype_Declaration (Loc,
5848 Defining_Identifier => T,
5849 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5850
5851 -- This type is marked as an itype even though it has an explicit
5852 -- declaration since otherwise Is_Generic_Actual_Type can get
5853 -- set, resulting in the generation of spurious errors. (See
5854 -- sem_ch8.Analyze_Package_Renaming and Sem_Type.Covers.)
5855
5856 Set_Is_Itype (T);
5857 Set_Associated_Node_For_Itype (T, Exp);
5858 end if;
5859
5860 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5861
5862 -- Nothing needs to be done for private types with unknown discriminants
5863 -- if the underlying type is not an unconstrained composite type or it
5864 -- is an unchecked union.
5865
5866 elsif Is_Private_Type (Unc_Type)
5867 and then Has_Unknown_Discriminants (Unc_Type)
5868 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5869 or else Is_Constrained (Underlying_Type (Unc_Type))
5870 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5871 then
5872 null;
5873
5874 -- Case of derived type with unknown discriminants where the parent type
5875 -- also has unknown discriminants.
5876
5877 elsif Is_Record_Type (Unc_Type)
5878 and then not Is_Class_Wide_Type (Unc_Type)
5879 and then Has_Unknown_Discriminants (Unc_Type)
5880 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5881 then
5882 -- Nothing to be done if no underlying record view available
5883
5884 -- If this is a limited type derived from a type with unknown
5885 -- discriminants, do not expand either, so that subsequent expansion
5886 -- of the call can add build-in-place parameters to call.
5887
5888 if No (Underlying_Record_View (Unc_Type))
5889 or else Is_Limited_Type (Unc_Type)
5890 then
5891 null;
5892
5893 -- Otherwise use the Underlying_Record_View to create the proper
5894 -- constrained subtype for an object of a derived type with unknown
5895 -- discriminants.
5896
5897 else
5898 Rewrite (Subtype_Indic,
5899 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5900 end if;
5901
5902 -- Renamings of class-wide interface types require no equivalent
5903 -- constrained type declarations because we only need to reference
5904 -- the tag component associated with the interface. The same is
5905 -- presumably true for class-wide types in general, so this test
5906 -- is broadened to include all class-wide renamings, which also
5907 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5908 -- (Is this really correct, or are there some cases of class-wide
5909 -- renamings that require action in this procedure???)
5910
5911 elsif Present (N)
5912 and then Nkind (N) = N_Object_Renaming_Declaration
5913 and then Is_Class_Wide_Type (Unc_Type)
5914 then
5915 null;
5916
5917 -- In Ada 95 nothing to be done if the type of the expression is limited
5918 -- because in this case the expression cannot be copied, and its use can
5919 -- only be by reference.
5920
5921 -- In Ada 2005 the context can be an object declaration whose expression
5922 -- is a function that returns in place. If the nominal subtype has
5923 -- unknown discriminants, the call still provides constraints on the
5924 -- object, and we have to create an actual subtype from it.
5925
5926 -- If the type is class-wide, the expression is dynamically tagged and
5927 -- we do not create an actual subtype either. Ditto for an interface.
5928 -- For now this applies only if the type is immutably limited, and the
5929 -- function being called is build-in-place. This will have to be revised
5930 -- when build-in-place functions are generalized to other types.
5931
5932 elsif Is_Inherently_Limited_Type (Exp_Typ)
5933 and then
5934 (Is_Class_Wide_Type (Exp_Typ)
5935 or else Is_Interface (Exp_Typ)
5936 or else not Has_Unknown_Discriminants (Exp_Typ)
5937 or else not Is_Composite_Type (Unc_Type))
5938 then
5939 null;
5940
5941 -- For limited objects initialized with build-in-place function calls,
5942 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5943 -- node in the expression initializing the object, which breaks the
5944 -- circuitry that detects and adds the additional arguments to the
5945 -- called function.
5946
5947 elsif Is_Build_In_Place_Function_Call (Exp) then
5948 null;
5949
5950 -- If the expression is an uninitialized aggregate, no need to build
5951 -- a subtype from the expression, because this may require the use of
5952 -- dynamic memory to create the object.
5953
5954 elsif Is_Uninitialized_Aggregate (Exp, Exp_Typ) then
5955 Rewrite (Subtype_Indic, New_Occurrence_Of (Etype (Exp), Sloc (N)));
5956 if Nkind (N) = N_Object_Declaration then
5957 Set_Expression (N, Empty);
5958 Set_No_Initialization (N);
5959 end if;
5960
5961 else
5962 Rewrite (Subtype_Indic,
5963 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5964 end if;
5965 end Expand_Subtype_From_Expr;
5966
5967 ---------------------------------------------
5968 -- Expression_Contains_Primitives_Calls_Of --
5969 ---------------------------------------------
5970
5971 function Expression_Contains_Primitives_Calls_Of
5972 (Expr : Node_Id;
5973 Typ : Entity_Id) return Boolean
5974 is
5975 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5976
5977 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5978 -- Search for nondispatching calls to primitive functions of type Typ
5979
5980 ----------------------------
5981 -- Search_Primitive_Calls --
5982 ----------------------------
5983
5984 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5985 Disp_Typ : Entity_Id;
5986 Subp : Entity_Id;
5987
5988 begin
5989 -- Detect a function call that could denote a nondispatching
5990 -- primitive of the input type.
5991
5992 if Nkind (N) = N_Function_Call
5993 and then Is_Entity_Name (Name (N))
5994 then
5995 Subp := Entity (Name (N));
5996
5997 -- Do not consider function calls with a controlling argument, as
5998 -- those are always dispatching calls.
5999
6000 if Is_Dispatching_Operation (Subp)
6001 and then No (Controlling_Argument (N))
6002 then
6003 Disp_Typ := Find_Dispatching_Type (Subp);
6004
6005 -- To qualify as a suitable primitive, the dispatching type of
6006 -- the function must be the input type.
6007
6008 if Present (Disp_Typ)
6009 and then Unique_Entity (Disp_Typ) = U_Typ
6010 then
6011 -- There is no need to continue the traversal, as one such
6012 -- call suffices.
6013
6014 return Abandon;
6015 end if;
6016 end if;
6017 end if;
6018
6019 return OK;
6020 end Search_Primitive_Calls;
6021
6022 function Search_Calls is new Traverse_Func (Search_Primitive_Calls);
6023
6024 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
6025
6026 begin
6027 return Search_Calls (Expr) = Abandon;
6028 end Expression_Contains_Primitives_Calls_Of;
6029
6030 ----------------------
6031 -- Finalize_Address --
6032 ----------------------
6033
6034 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
6035 Btyp : constant Entity_Id := Base_Type (Typ);
6036 Utyp : Entity_Id := Typ;
6037
6038 begin
6039 -- Handle protected class-wide or task class-wide types
6040
6041 if Is_Class_Wide_Type (Utyp) then
6042 if Is_Concurrent_Type (Root_Type (Utyp)) then
6043 Utyp := Root_Type (Utyp);
6044
6045 elsif Is_Private_Type (Root_Type (Utyp))
6046 and then Present (Full_View (Root_Type (Utyp)))
6047 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
6048 then
6049 Utyp := Full_View (Root_Type (Utyp));
6050 end if;
6051 end if;
6052
6053 -- Handle private types
6054
6055 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
6056 Utyp := Full_View (Utyp);
6057 end if;
6058
6059 -- Handle protected and task types
6060
6061 if Is_Concurrent_Type (Utyp)
6062 and then Present (Corresponding_Record_Type (Utyp))
6063 then
6064 Utyp := Corresponding_Record_Type (Utyp);
6065 end if;
6066
6067 Utyp := Underlying_Type (Base_Type (Utyp));
6068
6069 -- Deal with untagged derivation of private views. If the parent is
6070 -- now known to be protected, the finalization routine is the one
6071 -- defined on the corresponding record of the ancestor (corresponding
6072 -- records do not automatically inherit operations, but maybe they
6073 -- should???). This does not apply to array types, where every base
6074 -- type has a finalization routine that depends on the first subtype.
6075
6076 if Is_Untagged_Derivation (Btyp) and then not Is_Array_Type (Btyp) then
6077 if Is_Protected_Type (Btyp) then
6078 Utyp := Corresponding_Record_Type (Root_Type (Btyp));
6079
6080 elsif Is_Implicit_Full_View (Utyp) then
6081 if Is_Derived_Type (Btyp) then
6082 Utyp := Underlying_Type (Root_Type (Btyp));
6083 else
6084 Utyp := Underlying_Type (Root_Type (Full_View (Btyp)));
6085 end if;
6086
6087 if Is_Protected_Type (Utyp) then
6088 Utyp := Corresponding_Record_Type (Utyp);
6089 end if;
6090 end if;
6091 end if;
6092
6093 -- If the underlying_type is a subtype, we are dealing with the
6094 -- completion of a private type. We need to access the base type and
6095 -- generate a conversion to it.
6096
6097 if Utyp /= Base_Type (Utyp) then
6098 pragma Assert (Is_Private_Type (Typ));
6099
6100 Utyp := Base_Type (Utyp);
6101 end if;
6102
6103 -- When dealing with an internally built full view for a type with
6104 -- unknown discriminants, use the original record type.
6105
6106 if Is_Underlying_Record_View (Utyp) then
6107 Utyp := Etype (Utyp);
6108 end if;
6109
6110 return TSS (Utyp, TSS_Finalize_Address);
6111 end Finalize_Address;
6112
6113 -----------------------------
6114 -- Find_Controlled_Prim_Op --
6115 -----------------------------
6116
6117 function Find_Controlled_Prim_Op
6118 (T : Entity_Id; Name : Name_Id) return Entity_Id
6119 is
6120 Op_Name : constant Name_Id := Name_Of_Controlled_Prim_Op (T, Name);
6121
6122 begin
6123 if Op_Name = No_Name then
6124 return Empty;
6125 end if;
6126
6127 return Find_Optional_Prim_Op (T, Op_Name);
6128 end Find_Controlled_Prim_Op;
6129
6130 ------------------------
6131 -- Find_Interface_ADT --
6132 ------------------------
6133
6134 function Find_Interface_ADT
6135 (T : Entity_Id;
6136 Iface : Entity_Id) return Elmt_Id
6137 is
6138 ADT : Elmt_Id;
6139 Typ : Entity_Id := T;
6140
6141 begin
6142 pragma Assert (Is_Interface (Iface));
6143
6144 -- Handle private types
6145
6146 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6147 Typ := Full_View (Typ);
6148 end if;
6149
6150 -- Handle access types
6151
6152 if Is_Access_Type (Typ) then
6153 Typ := Designated_Type (Typ);
6154 end if;
6155
6156 -- Handle task and protected types implementing interfaces
6157
6158 if Is_Concurrent_Type (Typ) then
6159 Typ := Corresponding_Record_Type (Typ);
6160 end if;
6161
6162 pragma Assert
6163 (not Is_Class_Wide_Type (Typ)
6164 and then Ekind (Typ) /= E_Incomplete_Type);
6165
6166 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6167 return First_Elmt (Access_Disp_Table (Typ));
6168
6169 else
6170 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
6171 while Present (ADT)
6172 and then Present (Related_Type (Node (ADT)))
6173 and then Related_Type (Node (ADT)) /= Iface
6174 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
6175 Use_Full_View => True)
6176 loop
6177 Next_Elmt (ADT);
6178 end loop;
6179
6180 pragma Assert (Present (Related_Type (Node (ADT))));
6181 return ADT;
6182 end if;
6183 end Find_Interface_ADT;
6184
6185 ------------------------
6186 -- Find_Interface_Tag --
6187 ------------------------
6188
6189 function Find_Interface_Tag
6190 (T : Entity_Id;
6191 Iface : Entity_Id) return Entity_Id
6192 is
6193 AI_Tag : Entity_Id := Empty;
6194 Found : Boolean := False;
6195 Typ : Entity_Id := T;
6196
6197 procedure Find_Tag (Typ : Entity_Id);
6198 -- Internal subprogram used to recursively climb to the ancestors
6199
6200 --------------
6201 -- Find_Tag --
6202 --------------
6203
6204 procedure Find_Tag (Typ : Entity_Id) is
6205 AI_Elmt : Elmt_Id;
6206 AI : Node_Id;
6207
6208 begin
6209 -- This routine does not handle the case in which the interface is an
6210 -- ancestor of Typ. That case is handled by the enclosing subprogram.
6211
6212 pragma Assert (Typ /= Iface);
6213
6214 -- Climb to the root type handling private types
6215
6216 if Present (Full_View (Etype (Typ))) then
6217 if Full_View (Etype (Typ)) /= Typ then
6218 Find_Tag (Full_View (Etype (Typ)));
6219 end if;
6220
6221 elsif Etype (Typ) /= Typ then
6222 Find_Tag (Etype (Typ));
6223 end if;
6224
6225 -- Traverse the list of interfaces implemented by the type
6226
6227 if not Found
6228 and then Present (Interfaces (Typ))
6229 and then not Is_Empty_Elmt_List (Interfaces (Typ))
6230 then
6231 -- Skip the tag associated with the primary table
6232
6233 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
6234 pragma Assert (Present (AI_Tag));
6235
6236 AI_Elmt := First_Elmt (Interfaces (Typ));
6237 while Present (AI_Elmt) loop
6238 AI := Node (AI_Elmt);
6239
6240 if AI = Iface
6241 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
6242 then
6243 Found := True;
6244 return;
6245 end if;
6246
6247 AI_Tag := Next_Tag_Component (AI_Tag);
6248 Next_Elmt (AI_Elmt);
6249 end loop;
6250 end if;
6251 end Find_Tag;
6252
6253 -- Start of processing for Find_Interface_Tag
6254
6255 begin
6256 pragma Assert (Is_Interface (Iface));
6257
6258 -- Handle access types
6259
6260 if Is_Access_Type (Typ) then
6261 Typ := Designated_Type (Typ);
6262 end if;
6263
6264 -- Handle class-wide types
6265
6266 if Is_Class_Wide_Type (Typ) then
6267 Typ := Root_Type (Typ);
6268 end if;
6269
6270 -- Handle private types
6271
6272 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6273 Typ := Full_View (Typ);
6274 end if;
6275
6276 -- Handle entities from the limited view
6277
6278 if Ekind (Typ) = E_Incomplete_Type then
6279 pragma Assert (Present (Non_Limited_View (Typ)));
6280 Typ := Non_Limited_View (Typ);
6281 end if;
6282
6283 -- Handle task and protected types implementing interfaces
6284
6285 if Is_Concurrent_Type (Typ) then
6286 Typ := Corresponding_Record_Type (Typ);
6287 end if;
6288
6289 -- If the interface is an ancestor of the type, then it shared the
6290 -- primary dispatch table.
6291
6292 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6293 return First_Tag_Component (Typ);
6294
6295 -- Otherwise we need to search for its associated tag component
6296
6297 else
6298 Find_Tag (Typ);
6299 return AI_Tag;
6300 end if;
6301 end Find_Interface_Tag;
6302
6303 --------------------
6304 -- Find_Last_Init --
6305 --------------------
6306
6307 function Find_Last_Init (Decl : Node_Id) return Node_Id is
6308 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
6309
6310 Init_Typ : Entity_Id;
6311 -- The initialization type of the related object declaration. Note
6312 -- that this is not necessarily the same type as Obj_Typ because of
6313 -- possible type derivations.
6314
6315 Obj_Typ : Entity_Id;
6316 -- The (designated) type of the object declaration
6317
6318 function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id;
6319 -- Find the last initialization call within the statements of block Blk
6320
6321 function Is_Init_Call (N : Node_Id) return Boolean;
6322 -- Determine whether node N denotes one of the initialization procedures
6323 -- of types Init_Typ or Typ.
6324
6325 function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id;
6326 -- Obtain the next statement which follows list member Stmt while
6327 -- ignoring artifacts related to access-before-elaboration checks.
6328
6329 -----------------------------
6330 -- Find_Last_Init_In_Block --
6331 -----------------------------
6332
6333 function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id is
6334 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
6335
6336 Stmt : Node_Id;
6337
6338 begin
6339 -- Examine the individual statements of the block in reverse to
6340 -- locate the last initialization call.
6341
6342 if Present (HSS) and then Present (Statements (HSS)) then
6343 Stmt := Last (Statements (HSS));
6344
6345 while Present (Stmt) loop
6346 -- Peek inside nested blocks in case aborts are allowed
6347
6348 if Nkind (Stmt) = N_Block_Statement then
6349 return Find_Last_Init_In_Block (Stmt);
6350
6351 elsif Is_Init_Call (Stmt) then
6352 return Stmt;
6353 end if;
6354
6355 Prev (Stmt);
6356 end loop;
6357 end if;
6358
6359 return Empty;
6360 end Find_Last_Init_In_Block;
6361
6362 ------------------
6363 -- Is_Init_Call --
6364 ------------------
6365
6366 function Is_Init_Call (N : Node_Id) return Boolean is
6367 function Is_Init_Proc_Of
6368 (Subp : Entity_Id;
6369 Typ : Entity_Id) return Boolean;
6370 -- Determine whether subprogram Subp_Id is a valid init proc of
6371 -- type Typ.
6372
6373 ---------------------
6374 -- Is_Init_Proc_Of --
6375 ---------------------
6376
6377 function Is_Init_Proc_Of
6378 (Subp : Entity_Id;
6379 Typ : Entity_Id) return Boolean
6380 is
6381 Deep_Init : Entity_Id := Empty;
6382 Prim_Init : Entity_Id := Empty;
6383 Type_Init : Entity_Id := Empty;
6384
6385 begin
6386 -- Obtain all possible initialization routines of the
6387 -- related type and try to match the subprogram entity
6388 -- against one of them.
6389
6390 -- Deep_Initialize
6391
6392 Deep_Init := TSS (Typ, TSS_Deep_Initialize);
6393
6394 -- Primitive Initialize
6395
6396 if Is_Controlled (Typ) then
6397 Prim_Init := Find_Controlled_Prim_Op (Typ, Name_Initialize);
6398
6399 if Present (Prim_Init) then
6400 Prim_Init := Ultimate_Alias (Prim_Init);
6401 end if;
6402 end if;
6403
6404 -- Type initialization routine
6405
6406 if Has_Non_Null_Base_Init_Proc (Typ) then
6407 Type_Init := Base_Init_Proc (Typ);
6408 end if;
6409
6410 return
6411 (Present (Deep_Init) and then Subp = Deep_Init)
6412 or else
6413 (Present (Prim_Init) and then Subp = Prim_Init)
6414 or else
6415 (Present (Type_Init) and then Subp = Type_Init);
6416 end Is_Init_Proc_Of;
6417
6418 -- Local variables
6419
6420 Call_Id : Entity_Id;
6421
6422 -- Start of processing for Is_Init_Call
6423
6424 begin
6425 if Nkind (N) = N_Procedure_Call_Statement
6426 and then Is_Entity_Name (Name (N))
6427 then
6428 Call_Id := Entity (Name (N));
6429
6430 -- Consider both the type of the object declaration and its
6431 -- related initialization type.
6432
6433 return
6434 Is_Init_Proc_Of (Call_Id, Init_Typ)
6435 or else
6436 Is_Init_Proc_Of (Call_Id, Obj_Typ);
6437 end if;
6438
6439 return False;
6440 end Is_Init_Call;
6441
6442 -----------------------------
6443 -- Next_Suitable_Statement --
6444 -----------------------------
6445
6446 function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id is
6447 Result : Node_Id;
6448
6449 begin
6450 -- Skip call markers and Program_Error raises installed by the
6451 -- ABE mechanism.
6452
6453 Result := Next (Stmt);
6454 while Present (Result) loop
6455 exit when Nkind (Result) not in
6456 N_Call_Marker | N_Raise_Program_Error;
6457
6458 Next (Result);
6459 end loop;
6460
6461 return Result;
6462 end Next_Suitable_Statement;
6463
6464 -- Local variables
6465
6466 Call : Node_Id;
6467 Last_Init : Node_Id;
6468 Stmt : Node_Id;
6469 Stmt_2 : Node_Id;
6470
6471 Deep_Init_Found : Boolean := False;
6472 -- A flag set when a call to [Deep_]Initialize has been found
6473
6474 -- Start of processing for Find_Last_Init
6475
6476 begin
6477 Last_Init := Decl;
6478
6479 -- Objects that capture controlled function results do not require
6480 -- initialization.
6481
6482 if Nkind (Decl) = N_Object_Declaration
6483 and then Nkind (Expression (Decl)) = N_Reference
6484 then
6485 return Last_Init;
6486 end if;
6487
6488 Obj_Typ := Base_Type (Etype (Obj_Id));
6489
6490 if Is_Access_Type (Obj_Typ) then
6491 Obj_Typ := Base_Type (Available_View (Designated_Type (Obj_Typ)));
6492 end if;
6493
6494 -- Handle the initialization type of the object declaration
6495
6496 if Is_Class_Wide_Type (Obj_Typ)
6497 and then Nkind (Decl) = N_Object_Declaration
6498 and then Nkind (Expression (Decl)) = N_Allocator
6499 then
6500 Init_Typ := Base_Type (Etype (Expression (Expression (Decl))));
6501 else
6502 Init_Typ := Obj_Typ;
6503 end if;
6504
6505 loop
6506 if Is_Private_Type (Init_Typ)
6507 and then Present (Full_View (Init_Typ))
6508 then
6509 Init_Typ := Base_Type (Full_View (Init_Typ));
6510
6511 elsif Is_Concurrent_Type (Init_Typ)
6512 and then Present (Corresponding_Record_Type (Init_Typ))
6513 then
6514 Init_Typ := Corresponding_Record_Type (Init_Typ);
6515
6516 elsif Is_Untagged_Derivation (Init_Typ) then
6517 Init_Typ := Root_Type (Init_Typ);
6518
6519 else
6520 exit;
6521 end if;
6522 end loop;
6523
6524 if Present (Freeze_Node (Obj_Id)) then
6525 Stmt := First (Actions (Freeze_Node (Obj_Id)));
6526 else
6527 Stmt := Next_Suitable_Statement (Decl);
6528 end if;
6529
6530 -- For an object with suppressed initialization, we check whether
6531 -- there is in fact no initialization expression. If there is not,
6532 -- then this is an object declaration that has been turned into a
6533 -- different object declaration that calls the build-in-place
6534 -- function in a 'Reference attribute, as in "F(...)'Reference".
6535 -- We search for that later object declaration, so that the
6536 -- attachment will be inserted after the call. Otherwise, if the
6537 -- call raises an exception, we will finalize the (uninitialized)
6538 -- object, which is wrong.
6539
6540 if Nkind (Decl) = N_Object_Declaration
6541 and then No_Initialization (Decl)
6542 then
6543 if No (Expression (Last_Init)) then
6544 loop
6545 Next (Last_Init);
6546
6547 exit when No (Last_Init);
6548 exit when Nkind (Last_Init) = N_Object_Declaration
6549 and then Nkind (Expression (Last_Init)) = N_Reference
6550 and then Nkind (Prefix (Expression (Last_Init))) =
6551 N_Function_Call
6552 and then Is_Expanded_Build_In_Place_Call
6553 (Prefix (Expression (Last_Init)));
6554 end loop;
6555 end if;
6556
6557 return Last_Init;
6558
6559 -- If the initialization is in the declaration, we're done, so
6560 -- early return if we have no more statements or they have been
6561 -- rewritten, which means that they were in the source code.
6562
6563 elsif No (Stmt) or else Original_Node (Stmt) /= Stmt then
6564 return Last_Init;
6565
6566 -- In all other cases the initialization calls follow the related
6567 -- object. The general structure of object initialization built by
6568 -- routine Default_Initialize_Object is as follows:
6569
6570 -- [begin -- aborts allowed
6571 -- Abort_Defer;]
6572 -- Type_Init_Proc (Obj);
6573 -- [begin] -- exceptions allowed
6574 -- Deep_Initialize (Obj);
6575 -- [exception -- exceptions allowed
6576 -- when others =>
6577 -- Deep_Finalize (Obj, Self => False);
6578 -- raise;
6579 -- end;]
6580 -- [at end -- aborts allowed
6581 -- Abort_Undefer;
6582 -- end;]
6583
6584 -- When aborts are allowed, the initialization calls are housed
6585 -- within a block.
6586
6587 elsif Nkind (Stmt) = N_Block_Statement then
6588 Call := Find_Last_Init_In_Block (Stmt);
6589
6590 if Present (Call) then
6591 Last_Init := Call;
6592 end if;
6593
6594 -- Otherwise the initialization calls follow the related object
6595
6596 else
6597 Stmt_2 := Next_Suitable_Statement (Stmt);
6598
6599 -- Check for an optional call to Deep_Initialize which may
6600 -- appear within a block depending on whether the object has
6601 -- controlled components.
6602
6603 if Present (Stmt_2) then
6604 if Nkind (Stmt_2) = N_Block_Statement then
6605 Call := Find_Last_Init_In_Block (Stmt_2);
6606
6607 if Present (Call) then
6608 Deep_Init_Found := True;
6609 Last_Init := Call;
6610 end if;
6611
6612 elsif Is_Init_Call (Stmt_2) then
6613 Deep_Init_Found := True;
6614 Last_Init := Stmt_2;
6615 end if;
6616 end if;
6617
6618 -- If the object lacks a call to Deep_Initialize, then it must
6619 -- have a call to its related type init proc.
6620
6621 if not Deep_Init_Found and then Is_Init_Call (Stmt) then
6622 Last_Init := Stmt;
6623 end if;
6624 end if;
6625
6626 return Last_Init;
6627 end Find_Last_Init;
6628
6629 ---------------------------
6630 -- Find_Optional_Prim_Op --
6631 ---------------------------
6632
6633 function Find_Optional_Prim_Op
6634 (T : Entity_Id; Name : Name_Id) return Entity_Id
6635 is
6636 Prim : Elmt_Id;
6637 Typ : Entity_Id := T;
6638 Op : Entity_Id;
6639
6640 begin
6641 if Is_Class_Wide_Type (Typ) then
6642 Typ := Root_Type (Typ);
6643 end if;
6644
6645 Typ := Underlying_Type (Typ);
6646
6647 -- We cannot find the operation if there is no full view available
6648
6649 if No (Typ) then
6650 return Empty;
6651 end if;
6652
6653 -- Loop through primitive operations
6654
6655 Prim := First_Elmt (Primitive_Operations (Typ));
6656 while Present (Prim) loop
6657 Op := Node (Prim);
6658
6659 -- We can retrieve primitive operations by name if it is an internal
6660 -- name. For equality we must check that both of its operands have
6661 -- the same type, to avoid confusion with user-defined equalities
6662 -- than may have a asymmetric signature.
6663
6664 exit when Chars (Op) = Name
6665 and then
6666 (Name /= Name_Op_Eq
6667 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
6668
6669 Next_Elmt (Prim);
6670 end loop;
6671
6672 return Node (Prim); -- Empty if not found
6673 end Find_Optional_Prim_Op;
6674
6675 ---------------------------
6676 -- Find_Optional_Prim_Op --
6677 ---------------------------
6678
6679 function Find_Optional_Prim_Op
6680 (T : Entity_Id;
6681 Name : TSS_Name_Type) return Entity_Id
6682 is
6683 Inher_Op : Entity_Id := Empty;
6684 Own_Op : Entity_Id := Empty;
6685 Prim_Elmt : Elmt_Id;
6686 Prim_Id : Entity_Id;
6687 Typ : Entity_Id := T;
6688
6689 begin
6690 if Is_Class_Wide_Type (Typ) then
6691 Typ := Root_Type (Typ);
6692 end if;
6693
6694 Typ := Underlying_Type (Typ);
6695
6696 -- This search is based on the assertion that the dispatching version
6697 -- of the TSS routine always precedes the real primitive.
6698
6699 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6700 while Present (Prim_Elmt) loop
6701 Prim_Id := Node (Prim_Elmt);
6702
6703 if Is_TSS (Prim_Id, Name) then
6704 if Present (Alias (Prim_Id)) then
6705 Inher_Op := Prim_Id;
6706 else
6707 Own_Op := Prim_Id;
6708 end if;
6709 end if;
6710
6711 Next_Elmt (Prim_Elmt);
6712 end loop;
6713
6714 if Present (Own_Op) then
6715 return Own_Op;
6716 elsif Present (Inher_Op) then
6717 return Inher_Op;
6718 else
6719 return Empty;
6720 end if;
6721 end Find_Optional_Prim_Op;
6722
6723 ------------------
6724 -- Find_Prim_Op --
6725 ------------------
6726
6727 function Find_Prim_Op
6728 (T : Entity_Id; Name : Name_Id) return Entity_Id
6729 is
6730 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6731 begin
6732 if No (Result) then
6733 raise Program_Error;
6734 end if;
6735
6736 return Result;
6737 end Find_Prim_Op;
6738
6739 ------------------
6740 -- Find_Prim_Op --
6741 ------------------
6742
6743 function Find_Prim_Op
6744 (T : Entity_Id;
6745 Name : TSS_Name_Type) return Entity_Id
6746 is
6747 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6748 begin
6749 if No (Result) then
6750 raise Program_Error;
6751 end if;
6752
6753 return Result;
6754 end Find_Prim_Op;
6755
6756 ----------------------------
6757 -- Find_Protection_Object --
6758 ----------------------------
6759
6760 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
6761 S : Entity_Id;
6762
6763 begin
6764 S := Scop;
6765 while Present (S) loop
6766 if Ekind (S) in E_Entry | E_Entry_Family | E_Function | E_Procedure
6767 and then Present (Protection_Object (S))
6768 then
6769 return Protection_Object (S);
6770 end if;
6771
6772 S := Scope (S);
6773 end loop;
6774
6775 -- If we do not find a Protection object in the scope chain, then
6776 -- something has gone wrong, most likely the object was never created.
6777
6778 raise Program_Error;
6779 end Find_Protection_Object;
6780
6781 --------------------------
6782 -- Find_Protection_Type --
6783 --------------------------
6784
6785 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
6786 Comp : Entity_Id;
6787 Typ : Entity_Id := Conc_Typ;
6788
6789 begin
6790 if Is_Concurrent_Type (Typ) then
6791 Typ := Corresponding_Record_Type (Typ);
6792 end if;
6793
6794 -- Since restriction violations are not considered serious errors, the
6795 -- expander remains active, but may leave the corresponding record type
6796 -- malformed. In such cases, component _object is not available so do
6797 -- not look for it.
6798
6799 if not Analyzed (Typ) then
6800 return Empty;
6801 end if;
6802
6803 Comp := First_Component (Typ);
6804 while Present (Comp) loop
6805 if Chars (Comp) = Name_uObject then
6806 return Base_Type (Etype (Comp));
6807 end if;
6808
6809 Next_Component (Comp);
6810 end loop;
6811
6812 -- The corresponding record of a protected type should always have an
6813 -- _object field.
6814
6815 raise Program_Error;
6816 end Find_Protection_Type;
6817
6818 function Find_Storage_Op
6819 (Typ : Entity_Id;
6820 Nam : Name_Id) return Entity_Id
6821 is
6822 use Sem_Util.Storage_Model_Support;
6823
6824 begin
6825 if Has_Storage_Model_Type_Aspect (Typ) then
6826 return Get_Storage_Model_Type_Entity (Typ, Nam);
6827
6828 -- Otherwise we assume that Typ is a descendant of Root_Storage_Pool
6829
6830 else
6831 return Find_Prim_Op (Typ, Nam);
6832 end if;
6833 end Find_Storage_Op;
6834
6835 -----------------------
6836 -- Find_Hook_Context --
6837 -----------------------
6838
6839 function Find_Hook_Context (N : Node_Id) return Node_Id is
6840 Par : Node_Id;
6841 Top : Node_Id;
6842
6843 Wrapped_Node : Node_Id;
6844 -- Note: if we are in a transient scope, we want to reuse it as
6845 -- the context for actions insertion, if possible. But if N is itself
6846 -- part of the stored actions for the current transient scope,
6847 -- then we need to insert at the appropriate (inner) location in
6848 -- the not as an action on Node_To_Be_Wrapped.
6849
6850 In_Cond_Expr : constant Boolean := Within_Conditional_Expression (N);
6851
6852 begin
6853 -- When the node is inside a case/if expression, the lifetime of any
6854 -- temporary controlled object is extended. Find a suitable insertion
6855 -- node by locating the topmost case or if expressions.
6856
6857 if In_Cond_Expr then
6858 Par := N;
6859 Top := N;
6860 while Present (Par) loop
6861 if Nkind (Original_Node (Par)) in N_Case_Expression
6862 | N_If_Expression
6863 then
6864 Top := Par;
6865
6866 elsif Nkind (Par) in N_Case_Statement
6867 | N_If_Statement
6868 and then From_Conditional_Expression (Par)
6869 then
6870 Top := Par;
6871
6872 -- Prevent the search from going too far
6873
6874 elsif Is_Body_Or_Package_Declaration (Par) then
6875 exit;
6876 end if;
6877
6878 Par := Parent (Par);
6879 end loop;
6880
6881 -- The topmost case or if expression is now recovered, but it may
6882 -- still not be the correct place to add generated code. Climb to
6883 -- find a parent that is part of a declarative or statement list,
6884 -- and is not a list of actuals in a call.
6885
6886 Par := Top;
6887 while Present (Par) loop
6888 if Is_List_Member (Par)
6889 and then Nkind (Par) not in N_Component_Association
6890 | N_Discriminant_Association
6891 | N_Parameter_Association
6892 | N_Pragma_Argument_Association
6893 | N_Aggregate
6894 | N_Delta_Aggregate
6895 | N_Extension_Aggregate
6896 | N_Elsif_Part
6897 and then Nkind (Parent (Par)) not in N_Function_Call
6898 | N_Procedure_Call_Statement
6899 | N_Entry_Call_Statement
6900 | N_Aggregate
6901 | N_Delta_Aggregate
6902 | N_Extension_Aggregate
6903 then
6904 return Par;
6905
6906 -- Prevent the search from going too far
6907
6908 elsif Is_Body_Or_Package_Declaration (Par) then
6909 exit;
6910 end if;
6911
6912 Par := Parent (Par);
6913 end loop;
6914
6915 return Par;
6916
6917 else
6918 Par := N;
6919 while Present (Par) loop
6920
6921 -- Keep climbing past various operators
6922
6923 if Nkind (Parent (Par)) in N_Op
6924 or else Nkind (Parent (Par)) in N_And_Then | N_Or_Else
6925 then
6926 Par := Parent (Par);
6927 else
6928 exit;
6929 end if;
6930 end loop;
6931
6932 Top := Par;
6933
6934 -- The node may be located in a pragma in which case return the
6935 -- pragma itself:
6936
6937 -- pragma Precondition (... and then Ctrl_Func_Call ...);
6938
6939 -- Similar case occurs when the node is related to an object
6940 -- declaration or assignment:
6941
6942 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
6943
6944 -- Another case to consider is when the node is part of a return
6945 -- statement:
6946
6947 -- return ... and then Ctrl_Func_Call ...;
6948
6949 -- Another case is when the node acts as a formal in a procedure
6950 -- call statement:
6951
6952 -- Proc (... and then Ctrl_Func_Call ...);
6953
6954 if Scope_Is_Transient then
6955 Wrapped_Node := Node_To_Be_Wrapped;
6956 else
6957 Wrapped_Node := Empty;
6958 end if;
6959
6960 while Present (Par) loop
6961 if Par = Wrapped_Node
6962 or else Nkind (Par) in N_Assignment_Statement
6963 | N_Object_Declaration
6964 | N_Pragma
6965 | N_Procedure_Call_Statement
6966 | N_Simple_Return_Statement
6967 then
6968 return Par;
6969
6970 -- Prevent the search from going too far
6971
6972 elsif Is_Body_Or_Package_Declaration (Par) then
6973 exit;
6974 end if;
6975
6976 Par := Parent (Par);
6977 end loop;
6978
6979 -- Return the topmost short circuit operator
6980
6981 return Top;
6982 end if;
6983 end Find_Hook_Context;
6984
6985 ------------------------------
6986 -- Following_Address_Clause --
6987 ------------------------------
6988
6989 function Following_Address_Clause (D : Node_Id) return Node_Id is
6990 Id : constant Entity_Id := Defining_Identifier (D);
6991 Result : Node_Id;
6992 Par : Node_Id;
6993
6994 function Check_Decls (D : Node_Id) return Node_Id;
6995 -- This internal function differs from the main function in that it
6996 -- gets called to deal with a following package private part, and
6997 -- it checks declarations starting with D (the main function checks
6998 -- declarations following D). If D is Empty, then Empty is returned.
6999
7000 -----------------
7001 -- Check_Decls --
7002 -----------------
7003
7004 function Check_Decls (D : Node_Id) return Node_Id is
7005 Decl : Node_Id;
7006
7007 begin
7008 Decl := D;
7009 while Present (Decl) loop
7010 if Nkind (Decl) = N_At_Clause
7011 and then Chars (Identifier (Decl)) = Chars (Id)
7012 then
7013 return Decl;
7014
7015 elsif Nkind (Decl) = N_Attribute_Definition_Clause
7016 and then Chars (Decl) = Name_Address
7017 and then Chars (Name (Decl)) = Chars (Id)
7018 then
7019 return Decl;
7020 end if;
7021
7022 Next (Decl);
7023 end loop;
7024
7025 -- Otherwise not found, return Empty
7026
7027 return Empty;
7028 end Check_Decls;
7029
7030 -- Start of processing for Following_Address_Clause
7031
7032 begin
7033 -- If parser detected no address clause for the identifier in question,
7034 -- then the answer is a quick NO, without the need for a search.
7035
7036 if not Get_Name_Table_Boolean1 (Chars (Id)) then
7037 return Empty;
7038 end if;
7039
7040 -- Otherwise search current declarative unit
7041
7042 Result := Check_Decls (Next (D));
7043
7044 if Present (Result) then
7045 return Result;
7046 end if;
7047
7048 -- Check for possible package private part following
7049
7050 Par := Parent (D);
7051
7052 if Nkind (Par) = N_Package_Specification
7053 and then Visible_Declarations (Par) = List_Containing (D)
7054 and then Present (Private_Declarations (Par))
7055 then
7056 -- Private part present, check declarations there
7057
7058 return Check_Decls (First (Private_Declarations (Par)));
7059
7060 else
7061 -- No private part, clause not found, return Empty
7062
7063 return Empty;
7064 end if;
7065 end Following_Address_Clause;
7066
7067 ----------------------
7068 -- Force_Evaluation --
7069 ----------------------
7070
7071 procedure Force_Evaluation
7072 (Exp : Node_Id;
7073 Name_Req : Boolean := False;
7074 Related_Id : Entity_Id := Empty;
7075 Is_Low_Bound : Boolean := False;
7076 Is_High_Bound : Boolean := False;
7077 Discr_Number : Int := 0;
7078 Mode : Force_Evaluation_Mode := Relaxed)
7079 is
7080 begin
7081 Remove_Side_Effects
7082 (Exp => Exp,
7083 Name_Req => Name_Req,
7084 Variable_Ref => True,
7085 Renaming_Req => False,
7086 Related_Id => Related_Id,
7087 Is_Low_Bound => Is_Low_Bound,
7088 Is_High_Bound => Is_High_Bound,
7089 Discr_Number => Discr_Number,
7090 Check_Side_Effects =>
7091 Is_Static_Expression (Exp)
7092 or else Mode = Relaxed);
7093 end Force_Evaluation;
7094
7095 ---------------------------------
7096 -- Fully_Qualified_Name_String --
7097 ---------------------------------
7098
7099 function Fully_Qualified_Name_String
7100 (E : Entity_Id;
7101 Append_NUL : Boolean := True) return String_Id
7102 is
7103 procedure Internal_Full_Qualified_Name (E : Entity_Id);
7104 -- Compute recursively the qualified name without NUL at the end, adding
7105 -- it to the currently started string being generated
7106
7107 ----------------------------------
7108 -- Internal_Full_Qualified_Name --
7109 ----------------------------------
7110
7111 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
7112 Ent : Entity_Id;
7113
7114 begin
7115 -- Deal properly with child units
7116
7117 if Nkind (E) = N_Defining_Program_Unit_Name then
7118 Ent := Defining_Identifier (E);
7119 else
7120 Ent := E;
7121 end if;
7122
7123 -- Compute qualification recursively (only "Standard" has no scope)
7124
7125 if Present (Scope (Scope (Ent))) then
7126 Internal_Full_Qualified_Name (Scope (Ent));
7127 Store_String_Char (Get_Char_Code ('.'));
7128 end if;
7129
7130 -- Every entity should have a name except some expanded blocks
7131 -- don't bother about those.
7132
7133 if Chars (Ent) = No_Name then
7134 return;
7135 end if;
7136
7137 -- Generates the entity name in upper case
7138
7139 Get_Decoded_Name_String (Chars (Ent));
7140 Set_Casing (All_Upper_Case);
7141 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7142 return;
7143 end Internal_Full_Qualified_Name;
7144
7145 -- Start of processing for Full_Qualified_Name
7146
7147 begin
7148 Start_String;
7149 Internal_Full_Qualified_Name (E);
7150
7151 if Append_NUL then
7152 Store_String_Char (Get_Char_Code (ASCII.NUL));
7153 end if;
7154
7155 return End_String;
7156 end Fully_Qualified_Name_String;
7157
7158 ---------------------------------
7159 -- Get_Current_Value_Condition --
7160 ---------------------------------
7161
7162 -- Note: the implementation of this procedure is very closely tied to the
7163 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
7164 -- interpret Current_Value fields set by the Set procedure, so the two
7165 -- procedures need to be closely coordinated.
7166
7167 procedure Get_Current_Value_Condition
7168 (Var : Node_Id;
7169 Op : out Node_Kind;
7170 Val : out Node_Id)
7171 is
7172 Loc : constant Source_Ptr := Sloc (Var);
7173 Ent : constant Entity_Id := Entity (Var);
7174
7175 procedure Process_Current_Value_Condition (N : Node_Id; S : Boolean);
7176 -- N is an expression which holds either True (S = True) or False (S =
7177 -- False) in the condition. This procedure digs out the expression and
7178 -- if it refers to Ent, sets Op and Val appropriately.
7179
7180 -------------------------------------
7181 -- Process_Current_Value_Condition --
7182 -------------------------------------
7183
7184 procedure Process_Current_Value_Condition
7185 (N : Node_Id;
7186 S : Boolean)
7187 is
7188 Cond : Node_Id;
7189 Prev_Cond : Node_Id;
7190 Sens : Boolean;
7191
7192 begin
7193 Cond := N;
7194 Sens := S;
7195
7196 loop
7197 Prev_Cond := Cond;
7198
7199 -- Deal with NOT operators, inverting sense
7200
7201 while Nkind (Cond) = N_Op_Not loop
7202 Cond := Right_Opnd (Cond);
7203 Sens := not Sens;
7204 end loop;
7205
7206 -- Deal with conversions, qualifications, and expressions with
7207 -- actions.
7208
7209 while Nkind (Cond) in N_Type_Conversion
7210 | N_Qualified_Expression
7211 | N_Expression_With_Actions
7212 loop
7213 Cond := Expression (Cond);
7214 end loop;
7215
7216 exit when Cond = Prev_Cond;
7217 end loop;
7218
7219 -- Deal with AND THEN and AND cases
7220
7221 if Nkind (Cond) in N_And_Then | N_Op_And then
7222
7223 -- Don't ever try to invert a condition that is of the form of an
7224 -- AND or AND THEN (since we are not doing sufficiently general
7225 -- processing to allow this).
7226
7227 if Sens = False then
7228 Op := N_Empty;
7229 Val := Empty;
7230 return;
7231 end if;
7232
7233 -- Recursively process AND and AND THEN branches
7234
7235 Process_Current_Value_Condition (Left_Opnd (Cond), True);
7236 pragma Assert (Op'Valid);
7237
7238 if Op /= N_Empty then
7239 return;
7240 end if;
7241
7242 Process_Current_Value_Condition (Right_Opnd (Cond), True);
7243 return;
7244
7245 -- Case of relational operator
7246
7247 elsif Nkind (Cond) in N_Op_Compare then
7248 Op := Nkind (Cond);
7249
7250 -- Invert sense of test if inverted test
7251
7252 if Sens = False then
7253 case Op is
7254 when N_Op_Eq => Op := N_Op_Ne;
7255 when N_Op_Ne => Op := N_Op_Eq;
7256 when N_Op_Lt => Op := N_Op_Ge;
7257 when N_Op_Gt => Op := N_Op_Le;
7258 when N_Op_Le => Op := N_Op_Gt;
7259 when N_Op_Ge => Op := N_Op_Lt;
7260 when others => raise Program_Error;
7261 end case;
7262 end if;
7263
7264 -- Case of entity op value
7265
7266 if Is_Entity_Name (Left_Opnd (Cond))
7267 and then Ent = Entity (Left_Opnd (Cond))
7268 and then Compile_Time_Known_Value (Right_Opnd (Cond))
7269 then
7270 Val := Right_Opnd (Cond);
7271
7272 -- Case of value op entity
7273
7274 elsif Is_Entity_Name (Right_Opnd (Cond))
7275 and then Ent = Entity (Right_Opnd (Cond))
7276 and then Compile_Time_Known_Value (Left_Opnd (Cond))
7277 then
7278 Val := Left_Opnd (Cond);
7279
7280 -- We are effectively swapping operands
7281
7282 case Op is
7283 when N_Op_Eq => null;
7284 when N_Op_Ne => null;
7285 when N_Op_Lt => Op := N_Op_Gt;
7286 when N_Op_Gt => Op := N_Op_Lt;
7287 when N_Op_Le => Op := N_Op_Ge;
7288 when N_Op_Ge => Op := N_Op_Le;
7289 when others => raise Program_Error;
7290 end case;
7291
7292 else
7293 Op := N_Empty;
7294 end if;
7295
7296 return;
7297
7298 elsif Nkind (Cond) in N_Type_Conversion
7299 | N_Qualified_Expression
7300 | N_Expression_With_Actions
7301 then
7302 Cond := Expression (Cond);
7303
7304 -- Case of Boolean variable reference, return as though the
7305 -- reference had said var = True.
7306
7307 else
7308 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
7309 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
7310
7311 if Sens = False then
7312 Op := N_Op_Ne;
7313 else
7314 Op := N_Op_Eq;
7315 end if;
7316 end if;
7317 end if;
7318 end Process_Current_Value_Condition;
7319
7320 -- Start of processing for Get_Current_Value_Condition
7321
7322 begin
7323 Op := N_Empty;
7324 Val := Empty;
7325
7326 -- Immediate return, nothing doing, if this is not an object
7327
7328 if not Is_Object (Ent) then
7329 return;
7330 end if;
7331
7332 -- In GNATprove mode we don't want to use current value optimizer, in
7333 -- particular for loop invariant expressions and other assertions that
7334 -- act as cut points for proof. The optimizer often folds expressions
7335 -- into True/False where they trivially follow from the previous
7336 -- assignments, but this deprives proof from the information needed to
7337 -- discharge checks that are beyond the scope of the value optimizer.
7338
7339 if GNATprove_Mode then
7340 return;
7341 end if;
7342
7343 -- Otherwise examine current value
7344
7345 declare
7346 CV : constant Node_Id := Current_Value (Ent);
7347 Sens : Boolean;
7348 Stm : Node_Id;
7349
7350 begin
7351 -- If statement. Condition is known true in THEN section, known False
7352 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
7353
7354 if Nkind (CV) = N_If_Statement then
7355
7356 -- Before start of IF statement
7357
7358 if Loc < Sloc (CV) then
7359 return;
7360
7361 -- In condition of IF statement
7362
7363 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7364 return;
7365
7366 -- After end of IF statement
7367
7368 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
7369 return;
7370 end if;
7371
7372 -- At this stage we know that we are within the IF statement, but
7373 -- unfortunately, the tree does not record the SLOC of the ELSE so
7374 -- we cannot use a simple SLOC comparison to distinguish between
7375 -- the then/else statements, so we have to climb the tree.
7376
7377 declare
7378 N : Node_Id;
7379
7380 begin
7381 N := Parent (Var);
7382 while Parent (N) /= CV loop
7383 N := Parent (N);
7384
7385 -- If we fall off the top of the tree, then that's odd, but
7386 -- perhaps it could occur in some error situation, and the
7387 -- safest response is simply to assume that the outcome of
7388 -- the condition is unknown. No point in bombing during an
7389 -- attempt to optimize things.
7390
7391 if No (N) then
7392 return;
7393 end if;
7394 end loop;
7395
7396 -- Now we have N pointing to a node whose parent is the IF
7397 -- statement in question, so now we can tell if we are within
7398 -- the THEN statements.
7399
7400 if Is_List_Member (N)
7401 and then List_Containing (N) = Then_Statements (CV)
7402 then
7403 Sens := True;
7404
7405 -- If the variable reference does not come from source, we
7406 -- cannot reliably tell whether it appears in the else part.
7407 -- In particular, if it appears in generated code for a node
7408 -- that requires finalization, it may be attached to a list
7409 -- that has not been yet inserted into the code. For now,
7410 -- treat it as unknown.
7411
7412 elsif not Comes_From_Source (N) then
7413 return;
7414
7415 -- Otherwise we must be in ELSIF or ELSE part
7416
7417 else
7418 Sens := False;
7419 end if;
7420 end;
7421
7422 -- ELSIF part. Condition is known true within the referenced
7423 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
7424 -- and unknown before the ELSE part or after the IF statement.
7425
7426 elsif Nkind (CV) = N_Elsif_Part then
7427
7428 -- if the Elsif_Part had condition_actions, the elsif has been
7429 -- rewritten as a nested if, and the original elsif_part is
7430 -- detached from the tree, so there is no way to obtain useful
7431 -- information on the current value of the variable.
7432 -- Can this be improved ???
7433
7434 if No (Parent (CV)) then
7435 return;
7436 end if;
7437
7438 Stm := Parent (CV);
7439
7440 -- If the tree has been otherwise rewritten there is nothing
7441 -- else to be done either.
7442
7443 if Nkind (Stm) /= N_If_Statement then
7444 return;
7445 end if;
7446
7447 -- Before start of ELSIF part
7448
7449 if Loc < Sloc (CV) then
7450 return;
7451
7452 -- In condition of ELSIF part
7453
7454 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7455 return;
7456
7457 -- After end of IF statement
7458
7459 elsif Loc >= Sloc (Stm) +
7460 Text_Ptr (UI_To_Int (End_Span (Stm)))
7461 then
7462 return;
7463 end if;
7464
7465 -- Again we lack the SLOC of the ELSE, so we need to climb the
7466 -- tree to see if we are within the ELSIF part in question.
7467
7468 declare
7469 N : Node_Id;
7470
7471 begin
7472 N := Parent (Var);
7473 while Parent (N) /= Stm loop
7474 N := Parent (N);
7475
7476 -- If we fall off the top of the tree, then that's odd, but
7477 -- perhaps it could occur in some error situation, and the
7478 -- safest response is simply to assume that the outcome of
7479 -- the condition is unknown. No point in bombing during an
7480 -- attempt to optimize things.
7481
7482 if No (N) then
7483 return;
7484 end if;
7485 end loop;
7486
7487 -- Now we have N pointing to a node whose parent is the IF
7488 -- statement in question, so see if is the ELSIF part we want.
7489 -- the THEN statements.
7490
7491 if N = CV then
7492 Sens := True;
7493
7494 -- Otherwise we must be in subsequent ELSIF or ELSE part
7495
7496 else
7497 Sens := False;
7498 end if;
7499 end;
7500
7501 -- Iteration scheme of while loop. The condition is known to be
7502 -- true within the body of the loop.
7503
7504 elsif Nkind (CV) = N_Iteration_Scheme then
7505 declare
7506 Loop_Stmt : constant Node_Id := Parent (CV);
7507
7508 begin
7509 -- Before start of body of loop
7510
7511 if Loc < Sloc (Loop_Stmt) then
7512 return;
7513
7514 -- In condition of while loop
7515
7516 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7517 return;
7518
7519 -- After end of LOOP statement
7520
7521 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
7522 return;
7523
7524 -- We are within the body of the loop
7525
7526 else
7527 Sens := True;
7528 end if;
7529 end;
7530
7531 -- All other cases of Current_Value settings
7532
7533 else
7534 return;
7535 end if;
7536
7537 -- If we fall through here, then we have a reportable condition, Sens
7538 -- is True if the condition is true and False if it needs inverting.
7539
7540 Process_Current_Value_Condition (Condition (CV), Sens);
7541 end;
7542 end Get_Current_Value_Condition;
7543
7544 -----------------------
7545 -- Get_Index_Subtype --
7546 -----------------------
7547
7548 function Get_Index_Subtype (N : Node_Id) return Entity_Id is
7549 P_Type : Entity_Id := Etype (Prefix (N));
7550 Indx : Node_Id;
7551 J : Int;
7552
7553 begin
7554 if Is_Access_Type (P_Type) then
7555 P_Type := Designated_Type (P_Type);
7556 end if;
7557
7558 if No (Expressions (N)) then
7559 J := 1;
7560 else
7561 J := UI_To_Int (Expr_Value (First (Expressions (N))));
7562 end if;
7563
7564 Indx := First_Index (P_Type);
7565 while J > 1 loop
7566 Next_Index (Indx);
7567 J := J - 1;
7568 end loop;
7569
7570 return Etype (Indx);
7571 end Get_Index_Subtype;
7572
7573 -----------------------
7574 -- Get_Mapped_Entity --
7575 -----------------------
7576
7577 function Get_Mapped_Entity (E : Entity_Id) return Entity_Id is
7578 begin
7579 return Type_Map.Get (E);
7580 end Get_Mapped_Entity;
7581
7582 ---------------------
7583 -- Get_Stream_Size --
7584 ---------------------
7585
7586 function Get_Stream_Size (E : Entity_Id) return Uint is
7587 begin
7588 -- If we have a Stream_Size clause for this type use it
7589
7590 if Has_Stream_Size_Clause (E) then
7591 return Static_Integer (Expression (Stream_Size_Clause (E)));
7592
7593 -- Otherwise the Stream_Size is the size of the type
7594
7595 else
7596 return Esize (E);
7597 end if;
7598 end Get_Stream_Size;
7599
7600 ---------------------------
7601 -- Has_Access_Constraint --
7602 ---------------------------
7603
7604 function Has_Access_Constraint (E : Entity_Id) return Boolean is
7605 Disc : Entity_Id;
7606 T : constant Entity_Id := Etype (E);
7607
7608 begin
7609 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
7610 Disc := First_Discriminant (T);
7611 while Present (Disc) loop
7612 if Is_Access_Type (Etype (Disc)) then
7613 return True;
7614 end if;
7615
7616 Next_Discriminant (Disc);
7617 end loop;
7618
7619 return False;
7620 else
7621 return False;
7622 end if;
7623 end Has_Access_Constraint;
7624
7625 ---------------------
7626 -- Has_Tag_Of_Type --
7627 ---------------------
7628
7629 function Has_Tag_Of_Type (Exp : Node_Id) return Boolean is
7630 Typ : constant Entity_Id := Etype (Exp);
7631
7632 begin
7633 pragma Assert (Is_Tagged_Type (Typ));
7634
7635 -- The tag of an object of a class-wide type is that of its
7636 -- initialization expression.
7637
7638 if Is_Class_Wide_Type (Typ) then
7639 return False;
7640 end if;
7641
7642 -- The tag of a stand-alone object of a specific tagged type T
7643 -- identifies T.
7644
7645 if Is_Entity_Name (Exp)
7646 and then Ekind (Entity (Exp)) in E_Constant | E_Variable
7647 then
7648 return True;
7649
7650 else
7651 case Nkind (Exp) is
7652 -- The tag of a component or an aggregate of a specific tagged
7653 -- type T identifies T.
7654
7655 when N_Indexed_Component
7656 | N_Selected_Component
7657 | N_Aggregate
7658 | N_Extension_Aggregate
7659 =>
7660 return True;
7661
7662 -- The tag of the result returned by a function whose result
7663 -- type is a specific tagged type T identifies T.
7664
7665 when N_Function_Call =>
7666 return True;
7667
7668 when N_Explicit_Dereference =>
7669 return Is_Captured_Function_Call (Exp);
7670
7671 -- For a tagged type, the operand of a qualified expression
7672 -- shall resolve to be of the type of the expression.
7673
7674 when N_Qualified_Expression =>
7675 return Has_Tag_Of_Type (Expression (Exp));
7676
7677 when others =>
7678 return False;
7679 end case;
7680 end if;
7681 end Has_Tag_Of_Type;
7682
7683 --------------------
7684 -- Homonym_Number --
7685 --------------------
7686
7687 function Homonym_Number (Subp : Entity_Id) return Pos is
7688 Hom : Entity_Id := Homonym (Subp);
7689 Count : Pos := 1;
7690
7691 begin
7692 while Present (Hom) loop
7693 if Scope (Hom) = Scope (Subp) then
7694 Count := Count + 1;
7695 end if;
7696
7697 Hom := Homonym (Hom);
7698 end loop;
7699
7700 return Count;
7701 end Homonym_Number;
7702
7703 -----------------------------------
7704 -- In_Library_Level_Package_Body --
7705 -----------------------------------
7706
7707 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
7708 begin
7709 -- First determine whether the entity appears at the library level, then
7710 -- look at the containing unit.
7711
7712 if Is_Library_Level_Entity (Id) then
7713 declare
7714 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
7715
7716 begin
7717 return Nkind (Unit (Container)) = N_Package_Body;
7718 end;
7719 end if;
7720
7721 return False;
7722 end In_Library_Level_Package_Body;
7723
7724 ------------------------------
7725 -- In_Unconditional_Context --
7726 ------------------------------
7727
7728 function In_Unconditional_Context (Node : Node_Id) return Boolean is
7729 P : Node_Id;
7730
7731 begin
7732 P := Node;
7733 while Present (P) loop
7734 case Nkind (P) is
7735 when N_Subprogram_Body => return True;
7736 when N_If_Statement => return False;
7737 when N_Loop_Statement => return False;
7738 when N_Case_Statement => return False;
7739 when others => P := Parent (P);
7740 end case;
7741 end loop;
7742
7743 return False;
7744 end In_Unconditional_Context;
7745
7746 ----------------------------
7747 -- Init_Proc_Level_Formal --
7748 ----------------------------
7749
7750 function Init_Proc_Level_Formal (Proc : Entity_Id) return Entity_Id is
7751 Form : Entity_Id;
7752
7753 begin
7754 -- Go through the formals of the initialization procedure Proc to find
7755 -- the extra accessibility level parameter associated with the object
7756 -- being initialized.
7757
7758 Form := First_Formal (Proc);
7759 while Present (Form) loop
7760 if Chars (Form) = Name_uInit_Level then
7761 return Form;
7762 end if;
7763
7764 Next_Formal (Form);
7765 end loop;
7766
7767 -- No formal was found, return Empty
7768
7769 return Empty;
7770 end Init_Proc_Level_Formal;
7771
7772 -------------------
7773 -- Insert_Action --
7774 -------------------
7775
7776 procedure Insert_Action
7777 (Assoc_Node : Node_Id;
7778 Ins_Action : Node_Id;
7779 Spec_Expr_OK : Boolean := False)
7780 is
7781 begin
7782 if Present (Ins_Action) then
7783 Insert_Actions
7784 (Assoc_Node => Assoc_Node,
7785 Ins_Actions => New_List (Ins_Action),
7786 Spec_Expr_OK => Spec_Expr_OK);
7787 end if;
7788 end Insert_Action;
7789
7790 -- Version with check(s) suppressed
7791
7792 procedure Insert_Action
7793 (Assoc_Node : Node_Id;
7794 Ins_Action : Node_Id;
7795 Suppress : Check_Id;
7796 Spec_Expr_OK : Boolean := False)
7797 is
7798 begin
7799 Insert_Actions
7800 (Assoc_Node => Assoc_Node,
7801 Ins_Actions => New_List (Ins_Action),
7802 Suppress => Suppress,
7803 Spec_Expr_OK => Spec_Expr_OK);
7804 end Insert_Action;
7805
7806 -------------------------
7807 -- Insert_Action_After --
7808 -------------------------
7809
7810 procedure Insert_Action_After
7811 (Assoc_Node : Node_Id;
7812 Ins_Action : Node_Id)
7813 is
7814 begin
7815 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
7816 end Insert_Action_After;
7817
7818 --------------------
7819 -- Insert_Actions --
7820 --------------------
7821
7822 procedure Insert_Actions
7823 (Assoc_Node : Node_Id;
7824 Ins_Actions : List_Id;
7825 Spec_Expr_OK : Boolean := False)
7826 is
7827 N : Node_Id;
7828 P : Node_Id;
7829
7830 Wrapped_Node : Node_Id := Empty;
7831
7832 begin
7833 if Is_Empty_List (Ins_Actions) then
7834 return;
7835 end if;
7836
7837 -- Ignore insert of actions from inside default expression (or other
7838 -- similar "spec expression") in the special spec-expression analyze
7839 -- mode. Any insertions at this point have no relevance, since we are
7840 -- only doing the analyze to freeze the types of any static expressions.
7841 -- See section "Handling of Default and Per-Object Expressions" in the
7842 -- spec of package Sem for further details. However, if the user does
7843 -- nevertheless request the insert, then obey it.
7844
7845 -- Under strict preanalysis we cannot ignore insert of actions because
7846 -- we may be adding to the tree a subtype declaration that is required
7847 -- for proper preanalysis (see Sem_Ch3.Find_Type_Of_Object).
7848
7849 if In_Spec_Expression and then not Spec_Expr_OK then
7850 pragma Assert (not In_Strict_Preanalysis);
7851 return;
7852 end if;
7853
7854 -- If the action derives from stuff inside a record, then the actions
7855 -- are attached to the current scope, to be inserted and analyzed on
7856 -- exit from the scope. The reason for this is that we may also be
7857 -- generating freeze actions at the same time, and they must eventually
7858 -- be elaborated in the correct order.
7859
7860 if Is_Record_Type (Current_Scope)
7861 and then not Is_Frozen (Current_Scope)
7862 then
7863 if No (Scope_Stack.Table
7864 (Scope_Stack.Last).Pending_Freeze_Actions)
7865 then
7866 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
7867 Ins_Actions;
7868 else
7869 Append_List
7870 (Ins_Actions,
7871 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
7872 end if;
7873
7874 return;
7875 end if;
7876
7877 -- We now intend to climb up the tree to find the right point to
7878 -- insert the actions. We start at Assoc_Node, unless this node is a
7879 -- subexpression in which case we start with its parent. We do this for
7880 -- two reasons. First it speeds things up. Second, if Assoc_Node is
7881 -- itself one of the special nodes like N_And_Then, then we assume that
7882 -- an initial request to insert actions for such a node does not expect
7883 -- the actions to get deposited in the node for later handling when the
7884 -- node is expanded, since clearly the node is being dealt with by the
7885 -- caller. Note that in the subexpression case, N is always the child we
7886 -- came from.
7887
7888 -- N_Raise_xxx_Error is an annoying special case, it is a statement
7889 -- if it has type Standard_Void_Type, and a subexpression otherwise.
7890 -- Procedure calls, and similarly procedure attribute references, are
7891 -- also statements.
7892
7893 if Nkind (Assoc_Node) in N_Subexpr
7894 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
7895 or else Etype (Assoc_Node) /= Standard_Void_Type)
7896 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
7897 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
7898 or else not Is_Procedure_Attribute_Name
7899 (Attribute_Name (Assoc_Node)))
7900 then
7901 N := Assoc_Node;
7902 P := Parent (Assoc_Node);
7903
7904 -- Nonsubexpression case. Note that N is initially Empty in this case
7905 -- (N is only guaranteed non-Empty in the subexpr case).
7906
7907 else
7908 N := Empty;
7909 P := Assoc_Node;
7910 end if;
7911
7912 -- Capture root of the transient scope
7913
7914 if Scope_Is_Transient then
7915 Wrapped_Node := Node_To_Be_Wrapped;
7916 end if;
7917
7918 loop
7919 pragma Assert (Present (P));
7920
7921 -- Make sure that inserted actions stay in the transient scope
7922
7923 if Present (Wrapped_Node) and then N = Wrapped_Node then
7924 Store_Before_Actions_In_Scope (Ins_Actions);
7925 return;
7926 end if;
7927
7928 case Nkind (P) is
7929
7930 -- Case of right operand of AND THEN or OR ELSE. Put the actions
7931 -- in the Actions field of the right operand. They will be moved
7932 -- out further when the AND THEN or OR ELSE operator is expanded.
7933 -- Nothing special needs to be done for the left operand since
7934 -- in that case the actions are executed unconditionally.
7935
7936 when N_Short_Circuit =>
7937 if N = Right_Opnd (P) then
7938
7939 -- We are now going to either append the actions to the
7940 -- actions field of the short-circuit operation. We will
7941 -- also analyze the actions now.
7942
7943 -- This analysis is really too early, the proper thing would
7944 -- be to just park them there now, and only analyze them if
7945 -- we find we really need them, and to it at the proper
7946 -- final insertion point. However attempting to this proved
7947 -- tricky, so for now we just kill current values before and
7948 -- after the analyze call to make sure we avoid peculiar
7949 -- optimizations from this out of order insertion.
7950
7951 Kill_Current_Values;
7952
7953 -- If P has already been expanded, we can't park new actions
7954 -- on it, so we need to expand them immediately, introducing
7955 -- an Expression_With_Actions. N can't be an expression
7956 -- with actions, or else then the actions would have been
7957 -- inserted at an inner level.
7958
7959 if Analyzed (P) then
7960 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
7961 Rewrite (N,
7962 Make_Expression_With_Actions (Sloc (N),
7963 Actions => Ins_Actions,
7964 Expression => Relocate_Node (N)));
7965 Analyze_And_Resolve (N);
7966
7967 elsif Present (Actions (P)) then
7968 Insert_List_After_And_Analyze
7969 (Last (Actions (P)), Ins_Actions);
7970 else
7971 Set_Actions (P, Ins_Actions);
7972 Analyze_List (Actions (P));
7973 end if;
7974
7975 Kill_Current_Values;
7976
7977 return;
7978 end if;
7979
7980 -- Then or Else dependent expression of an if expression. Add
7981 -- actions to Then_Actions or Else_Actions field as appropriate.
7982 -- The actions will be moved further out when the if is expanded.
7983
7984 when N_If_Expression =>
7985 declare
7986 ThenX : constant Node_Id := Next (First (Expressions (P)));
7987 ElseX : constant Node_Id := Next (ThenX);
7988
7989 begin
7990 -- If the enclosing expression is already analyzed, as
7991 -- is the case for nested elaboration checks, insert the
7992 -- conditional further out.
7993
7994 if Analyzed (P) then
7995 null;
7996
7997 -- Actions belong to the then expression, temporarily place
7998 -- them as Then_Actions of the if expression. They will be
7999 -- moved to the proper place later when the if expression is
8000 -- expanded.
8001
8002 elsif N = ThenX then
8003 if Present (Then_Actions (P)) then
8004 Insert_List_After_And_Analyze
8005 (Last (Then_Actions (P)), Ins_Actions);
8006 else
8007 Set_Then_Actions (P, Ins_Actions);
8008 Analyze_List (Then_Actions (P));
8009 end if;
8010
8011 return;
8012
8013 -- Else_Actions is treated the same as Then_Actions above
8014
8015 elsif N = ElseX then
8016 if Present (Else_Actions (P)) then
8017 Insert_List_After_And_Analyze
8018 (Last (Else_Actions (P)), Ins_Actions);
8019 else
8020 Set_Else_Actions (P, Ins_Actions);
8021 Analyze_List (Else_Actions (P));
8022 end if;
8023
8024 return;
8025
8026 -- Actions belong to the condition. In this case they are
8027 -- unconditionally executed, and so we can continue the
8028 -- search for the proper insert point.
8029
8030 else
8031 null;
8032 end if;
8033 end;
8034
8035 -- Alternative of case expression, we place the action in the
8036 -- Actions field of the case expression alternative, this will
8037 -- be handled when the case expression is expanded.
8038
8039 when N_Case_Expression_Alternative =>
8040 if Present (Actions (P)) then
8041 Insert_List_After_And_Analyze
8042 (Last (Actions (P)), Ins_Actions);
8043 else
8044 Set_Actions (P, Ins_Actions);
8045 Analyze_List (Actions (P));
8046 end if;
8047
8048 return;
8049
8050 -- Case of appearing within an Expressions_With_Actions node. When
8051 -- the new actions come from the expression of the expression with
8052 -- actions, they must be added to the existing actions. The other
8053 -- alternative is when the new actions are related to one of the
8054 -- existing actions of the expression with actions, and should
8055 -- never reach here: if actions are inserted on a statement
8056 -- within the Actions of an expression with actions, or on some
8057 -- subexpression of such a statement, then the outermost proper
8058 -- insertion point is right before the statement, and we should
8059 -- never climb up as far as the N_Expression_With_Actions itself.
8060
8061 when N_Expression_With_Actions =>
8062 if Is_List_Member (N) and then List_Containing (N) = Actions (P)
8063 then
8064 raise Program_Error;
8065 end if;
8066
8067 if Is_Empty_List (Actions (P)) then
8068 Append_List_To (Actions (P), Ins_Actions);
8069 Analyze_List (Actions (P));
8070 else
8071 Insert_List_After_And_Analyze
8072 (Last (Actions (P)), Ins_Actions);
8073 end if;
8074
8075 return;
8076
8077 -- Case of appearing in the condition of a while expression or
8078 -- elsif. We insert the actions into the Condition_Actions field.
8079 -- They will be moved further out when the while loop or elsif
8080 -- is analyzed.
8081
8082 when N_Elsif_Part
8083 | N_Iteration_Scheme
8084 =>
8085 if Present (Condition (P)) and then N = Condition (P) then
8086 if Present (Condition_Actions (P)) then
8087 Insert_List_After_And_Analyze
8088 (Last (Condition_Actions (P)), Ins_Actions);
8089 else
8090 Set_Condition_Actions (P, Ins_Actions);
8091
8092 -- Set the parent of the insert actions explicitly. This
8093 -- is not a syntactic field, but we need the parent field
8094 -- set, in particular so that freeze can understand that
8095 -- it is dealing with condition actions, and properly
8096 -- insert the freezing actions.
8097
8098 Set_Parent (Ins_Actions, P);
8099 Analyze_List (Condition_Actions (P));
8100 end if;
8101
8102 return;
8103 end if;
8104
8105 -- Statements, declarations, pragmas, representation clauses
8106
8107 when
8108 -- Statements
8109
8110 N_Procedure_Call_Statement
8111 | N_Statement_Other_Than_Procedure_Call
8112
8113 -- Pragmas
8114
8115 | N_Pragma
8116
8117 -- Representation_Clause
8118
8119 | N_At_Clause
8120 | N_Attribute_Definition_Clause
8121 | N_Enumeration_Representation_Clause
8122 | N_Record_Representation_Clause
8123
8124 -- Declarations
8125
8126 | N_Abstract_Subprogram_Declaration
8127 | N_Entry_Body
8128 | N_Exception_Declaration
8129 | N_Exception_Renaming_Declaration
8130 | N_Expression_Function
8131 | N_Formal_Abstract_Subprogram_Declaration
8132 | N_Formal_Concrete_Subprogram_Declaration
8133 | N_Formal_Object_Declaration
8134 | N_Formal_Type_Declaration
8135 | N_Full_Type_Declaration
8136 | N_Function_Instantiation
8137 | N_Generic_Function_Renaming_Declaration
8138 | N_Generic_Package_Declaration
8139 | N_Generic_Package_Renaming_Declaration
8140 | N_Generic_Procedure_Renaming_Declaration
8141 | N_Generic_Subprogram_Declaration
8142 | N_Implicit_Label_Declaration
8143 | N_Incomplete_Type_Declaration
8144 | N_Number_Declaration
8145 | N_Object_Declaration
8146 | N_Object_Renaming_Declaration
8147 | N_Package_Body
8148 | N_Package_Body_Stub
8149 | N_Package_Declaration
8150 | N_Package_Instantiation
8151 | N_Package_Renaming_Declaration
8152 | N_Private_Extension_Declaration
8153 | N_Private_Type_Declaration
8154 | N_Procedure_Instantiation
8155 | N_Protected_Body
8156 | N_Protected_Body_Stub
8157 | N_Single_Task_Declaration
8158 | N_Subprogram_Body
8159 | N_Subprogram_Body_Stub
8160 | N_Subprogram_Declaration
8161 | N_Subprogram_Renaming_Declaration
8162 | N_Subtype_Declaration
8163 | N_Task_Body
8164 | N_Task_Body_Stub
8165
8166 -- Other things that can occur in stmt or decl lists
8167
8168 | N_Itype_Reference
8169
8170 -- Use clauses can appear in lists of declarations
8171
8172 | N_Use_Package_Clause
8173 | N_Use_Type_Clause
8174
8175 -- Freeze entity behaves like a declaration or statement
8176
8177 | N_Freeze_Entity
8178 | N_Freeze_Generic_Entity
8179 =>
8180 -- Do not insert here if the item is not a list member (this
8181 -- happens for example with a triggering statement, and the
8182 -- proper approach is to insert before the entire select).
8183
8184 if not Is_List_Member (P) then
8185 null;
8186
8187 -- Do not insert if parent of P is an N_Component_Association
8188 -- node (i.e. we are in the context of an N_Aggregate or
8189 -- N_Extension_Aggregate node. In this case we want to insert
8190 -- before the entire aggregate.
8191
8192 elsif Nkind (Parent (P)) = N_Component_Association then
8193 null;
8194
8195 -- Do not insert if the parent of P is either an N_Variant node
8196 -- or an N_Record_Definition node, meaning in either case that
8197 -- P is a member of a component list, and that therefore the
8198 -- actions should be inserted outside the complete record
8199 -- declaration.
8200
8201 elsif Nkind (Parent (P)) in N_Variant | N_Record_Definition then
8202 null;
8203
8204 -- Do not insert freeze nodes within a block or loop generated
8205 -- for an aggregate, because they may be elaborated too late
8206 -- for subsequent use in the back end: within a package spec,
8207 -- the block or loop is part of the elaboration procedure and
8208 -- is only elaborated during the second pass.
8209
8210 -- If the block or loop comes from source, or the entity is
8211 -- local to the block or loop itself, it must remain within.
8212
8213 elsif ((Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
8214 and then
8215 Nkind (Parent (Parent (P))) = N_Block_Statement
8216 and then not Comes_From_Source (Parent (Parent (P))))
8217 or else (Nkind (Parent (P)) = N_Loop_Statement
8218 and then not Comes_From_Source (Parent (P))))
8219 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
8220 and then not
8221 Within_Scope (Entity (First (Ins_Actions)), Current_Scope)
8222 then
8223 null;
8224
8225 -- Otherwise we can go ahead and do the insertion
8226
8227 elsif P = Wrapped_Node then
8228 Store_Before_Actions_In_Scope (Ins_Actions);
8229 return;
8230
8231 else
8232 Insert_List_Before_And_Analyze (P, Ins_Actions);
8233 return;
8234 end if;
8235
8236 -- The expansion of task and protected type declarations can
8237 -- create declarations for temporaries which, like other actions
8238 -- are inserted and analyzed before the current declaration.
8239 -- However, in some cases, the current scope is the synchronized
8240 -- type, and for unnesting it is critical that the proper scope
8241 -- for these generated entities be the enclosing one.
8242
8243 when N_Task_Type_Declaration
8244 | N_Protected_Type_Declaration =>
8245
8246 declare
8247 Skip_Scope : constant Boolean :=
8248 Ekind (Current_Scope) in Concurrent_Kind;
8249 begin
8250 if Skip_Scope then
8251 Push_Scope (Scope (Current_Scope));
8252 end if;
8253
8254 Insert_List_Before_And_Analyze (P, Ins_Actions);
8255
8256 if Skip_Scope then
8257 Pop_Scope;
8258 end if;
8259 end;
8260
8261 return;
8262
8263 -- A special case, N_Raise_xxx_Error can act either as a statement
8264 -- or a subexpression. We tell the difference by looking at the
8265 -- Etype. It is set to Standard_Void_Type in the statement case.
8266
8267 when N_Raise_xxx_Error =>
8268 if Etype (P) = Standard_Void_Type then
8269 if P = Wrapped_Node then
8270 Store_Before_Actions_In_Scope (Ins_Actions);
8271 else
8272 Insert_List_Before_And_Analyze (P, Ins_Actions);
8273 end if;
8274
8275 return;
8276
8277 -- In the subexpression case, keep climbing
8278
8279 else
8280 null;
8281 end if;
8282
8283 -- If a component association appears within a loop created for
8284 -- an array aggregate, attach the actions to the association so
8285 -- they can be subsequently inserted within the loop. For other
8286 -- component associations, insert outside of the aggregate. For
8287 -- an association that will generate a loop, its Loop_Actions
8288 -- field is already initialized (see exp_aggr.adb).
8289
8290 -- The list of Loop_Actions can in turn generate additional ones,
8291 -- that are inserted before the associated node. If the associated
8292 -- node is outside the aggregate, the new actions are collected
8293 -- at the end of the Loop_Actions, to respect the order in which
8294 -- they are to be elaborated.
8295
8296 when N_Component_Association
8297 | N_Iterated_Component_Association
8298 | N_Iterated_Element_Association
8299 =>
8300 if Nkind (Parent (P)) in N_Aggregate | N_Delta_Aggregate
8301 and then Present (Loop_Actions (P))
8302 then
8303 if Is_Empty_List (Loop_Actions (P)) then
8304 Set_Loop_Actions (P, Ins_Actions);
8305 Analyze_List (Ins_Actions);
8306
8307 else
8308 declare
8309 Decl : Node_Id;
8310
8311 begin
8312 -- Check whether these actions were generated by a
8313 -- declaration that is part of the Loop_Actions for
8314 -- the component_association.
8315
8316 Decl := Assoc_Node;
8317 while Present (Decl) loop
8318 exit when Parent (Decl) = P
8319 and then Is_List_Member (Decl)
8320 and then
8321 List_Containing (Decl) = Loop_Actions (P);
8322 Decl := Parent (Decl);
8323 end loop;
8324
8325 if Present (Decl) then
8326 Insert_List_Before_And_Analyze
8327 (Decl, Ins_Actions);
8328 else
8329 Insert_List_After_And_Analyze
8330 (Last (Loop_Actions (P)), Ins_Actions);
8331 end if;
8332 end;
8333 end if;
8334
8335 return;
8336 end if;
8337
8338 -- Special case: an attribute denoting a procedure call
8339
8340 when N_Attribute_Reference =>
8341 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
8342 if P = Wrapped_Node then
8343 Store_Before_Actions_In_Scope (Ins_Actions);
8344 else
8345 Insert_List_Before_And_Analyze (P, Ins_Actions);
8346 end if;
8347
8348 return;
8349
8350 -- In the subexpression case, keep climbing
8351
8352 else
8353 null;
8354 end if;
8355
8356 -- Special case: a marker
8357
8358 when N_Call_Marker
8359 | N_Variable_Reference_Marker
8360 =>
8361 if Is_List_Member (P) then
8362 Insert_List_Before_And_Analyze (P, Ins_Actions);
8363 return;
8364 end if;
8365
8366 -- A contract node should not belong to the tree
8367
8368 when N_Contract =>
8369 raise Program_Error;
8370
8371 -- For all other node types, keep climbing tree
8372
8373 when N_Abortable_Part
8374 | N_Accept_Alternative
8375 | N_Access_Definition
8376 | N_Access_Function_Definition
8377 | N_Access_Procedure_Definition
8378 | N_Access_To_Object_Definition
8379 | N_Aggregate
8380 | N_Allocator
8381 | N_Aspect_Specification
8382 | N_Case_Expression
8383 | N_Case_Statement_Alternative
8384 | N_Character_Literal
8385 | N_Compilation_Unit
8386 | N_Compilation_Unit_Aux
8387 | N_Component_Clause
8388 | N_Component_Declaration
8389 | N_Component_Definition
8390 | N_Component_List
8391 | N_Constrained_Array_Definition
8392 | N_Decimal_Fixed_Point_Definition
8393 | N_Defining_Character_Literal
8394 | N_Defining_Identifier
8395 | N_Defining_Operator_Symbol
8396 | N_Defining_Program_Unit_Name
8397 | N_Delay_Alternative
8398 | N_Delta_Aggregate
8399 | N_Delta_Constraint
8400 | N_Derived_Type_Definition
8401 | N_Designator
8402 | N_Digits_Constraint
8403 | N_Discriminant_Association
8404 | N_Discriminant_Specification
8405 | N_Empty
8406 | N_Entry_Body_Formal_Part
8407 | N_Entry_Call_Alternative
8408 | N_Entry_Declaration
8409 | N_Entry_Index_Specification
8410 | N_Enumeration_Type_Definition
8411 | N_Error
8412 | N_Exception_Handler
8413 | N_Expanded_Name
8414 | N_Explicit_Dereference
8415 | N_Extension_Aggregate
8416 | N_External_Initializer
8417 | N_Floating_Point_Definition
8418 | N_Formal_Decimal_Fixed_Point_Definition
8419 | N_Formal_Derived_Type_Definition
8420 | N_Formal_Discrete_Type_Definition
8421 | N_Formal_Floating_Point_Definition
8422 | N_Formal_Modular_Type_Definition
8423 | N_Formal_Ordinary_Fixed_Point_Definition
8424 | N_Formal_Package_Declaration
8425 | N_Formal_Private_Type_Definition
8426 | N_Formal_Incomplete_Type_Definition
8427 | N_Formal_Signed_Integer_Type_Definition
8428 | N_Function_Call
8429 | N_Function_Specification
8430 | N_Generic_Association
8431 | N_Handled_Sequence_Of_Statements
8432 | N_Identifier
8433 | N_In
8434 | N_Index_Or_Discriminant_Constraint
8435 | N_Indexed_Component
8436 | N_Integer_Literal
8437 | N_Iterator_Specification
8438 | N_Interpolated_String_Literal
8439 | N_Label
8440 | N_Loop_Parameter_Specification
8441 | N_Mod_Clause
8442 | N_Modular_Type_Definition
8443 | N_Not_In
8444 | N_Null
8445 | N_Op_Abs
8446 | N_Op_Add
8447 | N_Op_And
8448 | N_Op_Concat
8449 | N_Op_Divide
8450 | N_Op_Eq
8451 | N_Op_Expon
8452 | N_Op_Ge
8453 | N_Op_Gt
8454 | N_Op_Le
8455 | N_Op_Lt
8456 | N_Op_Minus
8457 | N_Op_Mod
8458 | N_Op_Multiply
8459 | N_Op_Ne
8460 | N_Op_Not
8461 | N_Op_Or
8462 | N_Op_Plus
8463 | N_Op_Rem
8464 | N_Op_Rotate_Left
8465 | N_Op_Rotate_Right
8466 | N_Op_Shift_Left
8467 | N_Op_Shift_Right
8468 | N_Op_Shift_Right_Arithmetic
8469 | N_Op_Subtract
8470 | N_Op_Xor
8471 | N_Operator_Symbol
8472 | N_Ordinary_Fixed_Point_Definition
8473 | N_Others_Choice
8474 | N_Package_Specification
8475 | N_Parameter_Association
8476 | N_Parameter_Specification
8477 | N_Pop_Constraint_Error_Label
8478 | N_Pop_Program_Error_Label
8479 | N_Pop_Storage_Error_Label
8480 | N_Pragma_Argument_Association
8481 | N_Procedure_Specification
8482 | N_Protected_Definition
8483 | N_Push_Constraint_Error_Label
8484 | N_Push_Program_Error_Label
8485 | N_Push_Storage_Error_Label
8486 | N_Qualified_Expression
8487 | N_Quantified_Expression
8488 | N_Raise_Expression
8489 | N_Range
8490 | N_Range_Constraint
8491 | N_Real_Literal
8492 | N_Real_Range_Specification
8493 | N_Record_Definition
8494 | N_Reference
8495 | N_SCIL_Dispatch_Table_Tag_Init
8496 | N_SCIL_Dispatching_Call
8497 | N_SCIL_Membership_Test
8498 | N_Selected_Component
8499 | N_Signed_Integer_Type_Definition
8500 | N_Single_Protected_Declaration
8501 | N_Slice
8502 | N_String_Literal
8503 | N_Subtype_Indication
8504 | N_Subunit
8505 | N_Target_Name
8506 | N_Task_Definition
8507 | N_Terminate_Alternative
8508 | N_Triggering_Alternative
8509 | N_Type_Conversion
8510 | N_Unchecked_Type_Conversion
8511 | N_Unconstrained_Array_Definition
8512 | N_Unused_At_End
8513 | N_Unused_At_Start
8514 | N_Variant
8515 | N_Variant_Part
8516 | N_Validate_Unchecked_Conversion
8517 | N_With_Clause
8518 =>
8519 null;
8520 end case;
8521
8522 -- If we fall through above tests, keep climbing tree
8523
8524 N := P;
8525
8526 if Nkind (Parent (N)) = N_Subunit then
8527
8528 -- This is the proper body corresponding to a stub. Insertion must
8529 -- be done at the point of the stub, which is in the declarative
8530 -- part of the parent unit.
8531
8532 P := Corresponding_Stub (Parent (N));
8533
8534 else
8535 P := Parent (N);
8536 end if;
8537 end loop;
8538 end Insert_Actions;
8539
8540 -- Version with check(s) suppressed
8541
8542 procedure Insert_Actions
8543 (Assoc_Node : Node_Id;
8544 Ins_Actions : List_Id;
8545 Suppress : Check_Id;
8546 Spec_Expr_OK : Boolean := False)
8547 is
8548 begin
8549 if Suppress = All_Checks then
8550 declare
8551 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
8552 begin
8553 Scope_Suppress.Suppress := (others => True);
8554 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8555 Scope_Suppress.Suppress := Sva;
8556 end;
8557
8558 else
8559 declare
8560 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
8561 begin
8562 Scope_Suppress.Suppress (Suppress) := True;
8563 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8564 Scope_Suppress.Suppress (Suppress) := Svg;
8565 end;
8566 end if;
8567 end Insert_Actions;
8568
8569 --------------------------
8570 -- Insert_Actions_After --
8571 --------------------------
8572
8573 procedure Insert_Actions_After
8574 (Assoc_Node : Node_Id;
8575 Ins_Actions : List_Id)
8576 is
8577 begin
8578 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
8579 Store_After_Actions_In_Scope (Ins_Actions);
8580 else
8581 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
8582 end if;
8583 end Insert_Actions_After;
8584
8585 ---------------------------------
8586 -- Insert_Library_Level_Action --
8587 ---------------------------------
8588
8589 procedure Insert_Library_Level_Action (N : Node_Id) is
8590 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8591
8592 begin
8593 Push_Scope (Cunit_Entity (Current_Sem_Unit));
8594 -- And not Main_Unit as previously. If the main unit is a body,
8595 -- the scope needed to analyze the actions is the entity of the
8596 -- corresponding declaration.
8597
8598 if No (Actions (Aux)) then
8599 Set_Actions (Aux, New_List (N));
8600 else
8601 Append (N, Actions (Aux));
8602 end if;
8603
8604 Analyze (N);
8605 Pop_Scope;
8606 end Insert_Library_Level_Action;
8607
8608 ----------------------------------
8609 -- Insert_Library_Level_Actions --
8610 ----------------------------------
8611
8612 procedure Insert_Library_Level_Actions (L : List_Id) is
8613 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8614
8615 begin
8616 if Is_Non_Empty_List (L) then
8617 Push_Scope (Cunit_Entity (Main_Unit));
8618 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
8619
8620 if No (Actions (Aux)) then
8621 Set_Actions (Aux, L);
8622 Analyze_List (L);
8623 else
8624 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
8625 end if;
8626
8627 Pop_Scope;
8628 end if;
8629 end Insert_Library_Level_Actions;
8630
8631 ----------------------
8632 -- Inside_Init_Proc --
8633 ----------------------
8634
8635 function Inside_Init_Proc return Boolean is
8636 begin
8637 return Present (Enclosing_Init_Proc);
8638 end Inside_Init_Proc;
8639
8640 ----------------------
8641 -- Integer_Type_For --
8642 ----------------------
8643
8644 function Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id is
8645 begin
8646 pragma Assert
8647 (Standard_Long_Integer_Size in
8648 Standard_Integer_Size | Standard_Long_Long_Integer_Size);
8649 -- So we don't need to check for Standard_Long_Integer_Size below
8650 pragma Assert (S <= System_Max_Integer_Size);
8651
8652 -- This is the canonical 32-bit type
8653
8654 if S <= Standard_Integer_Size then
8655 if Uns then
8656 return Standard_Unsigned;
8657 else
8658 return Standard_Integer;
8659 end if;
8660
8661 -- This is the canonical 64-bit type
8662
8663 elsif S <= Standard_Long_Long_Integer_Size then
8664 if Uns then
8665 return Standard_Long_Long_Unsigned;
8666 else
8667 return Standard_Long_Long_Integer;
8668 end if;
8669
8670 -- This is the canonical 128-bit type
8671
8672 elsif S <= Standard_Long_Long_Long_Integer_Size then
8673 if Uns then
8674 return Standard_Long_Long_Long_Unsigned;
8675 else
8676 return Standard_Long_Long_Long_Integer;
8677 end if;
8678
8679 else
8680 raise Program_Error;
8681 end if;
8682 end Integer_Type_For;
8683
8684 -------------------------------
8685 -- Is_Captured_Function_Call --
8686 -------------------------------
8687
8688 function Is_Captured_Function_Call (N : Node_Id) return Boolean is
8689 begin
8690 if Nkind (N) = N_Explicit_Dereference
8691 and then Is_Entity_Name (Prefix (N))
8692 and then Ekind (Entity (Prefix (N))) = E_Constant
8693 then
8694 declare
8695 Value : constant Node_Id := Constant_Value (Entity (Prefix (N)));
8696
8697 begin
8698 return Present (Value)
8699 and then Nkind (Value) = N_Reference
8700 and then Nkind (Prefix (Value)) = N_Function_Call;
8701 end;
8702
8703 else
8704 return False;
8705 end if;
8706 end Is_Captured_Function_Call;
8707
8708 -------------------------------------------------
8709 -- Is_Constr_Array_Subt_Of_Unc_With_Controlled --
8710 -------------------------------------------------
8711
8712 function Is_Constr_Array_Subt_Of_Unc_With_Controlled (Typ : Entity_Id)
8713 return Boolean
8714 is
8715 begin
8716 return Is_Array_Type (Typ)
8717 and then Is_Constrained (Typ)
8718 and then Has_Controlled_Component (Typ)
8719 and then not Is_Constrained (First_Subtype (Typ));
8720 end Is_Constr_Array_Subt_Of_Unc_With_Controlled;
8721
8722 ------------------------------------------
8723 -- Is_Conversion_Or_Reference_To_Formal --
8724 ------------------------------------------
8725
8726 function Is_Conversion_Or_Reference_To_Formal (N : Node_Id) return Boolean
8727 is
8728 begin
8729 return Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion
8730 or else (Nkind (N) = N_Explicit_Dereference
8731 and then Nkind (Prefix (N)) in N_Type_Conversion
8732 | N_Unchecked_Type_Conversion)
8733 or else (Is_Entity_Name (N)
8734 and then Present (Entity (N))
8735 and then Is_Formal (Entity (N)));
8736 end Is_Conversion_Or_Reference_To_Formal;
8737
8738 ---------------------------------------
8739 -- Is_Delayed_Conditional_Expression --
8740 ---------------------------------------
8741
8742 function Is_Delayed_Conditional_Expression (N : Node_Id) return Boolean is
8743 Unqual_N : constant Node_Id := Unqualify (N);
8744
8745 begin
8746 return Nkind (Unqual_N) in N_Case_Expression | N_If_Expression
8747 and then Expansion_Delayed (Unqual_N);
8748 end Is_Delayed_Conditional_Expression;
8749
8750 --------------------------------------------------
8751 -- Is_Expanded_Class_Wide_Interface_Object_Decl --
8752 --------------------------------------------------
8753
8754 function Is_Expanded_Class_Wide_Interface_Object_Decl
8755 (N : Node_Id) return Boolean is
8756 begin
8757 return not Comes_From_Source (N)
8758 and then Nkind (Original_Node (N)) = N_Object_Declaration
8759 and then Nkind (N) = N_Object_Renaming_Declaration
8760 and then Is_Class_Wide_Type (Etype (Defining_Identifier (N)))
8761 and then Is_Interface (Etype (Defining_Identifier (N)))
8762 and then Nkind (Name (N)) = N_Explicit_Dereference;
8763 end Is_Expanded_Class_Wide_Interface_Object_Decl;
8764
8765 ----------------------------------
8766 -- Is_Expression_Of_Func_Return --
8767 ----------------------------------
8768
8769 function Is_Expression_Of_Func_Return (N : Node_Id) return Boolean is
8770 Par : constant Node_Id := Parent (N);
8771
8772 begin
8773 return Nkind (Par) = N_Simple_Return_Statement
8774 or else (Nkind (Par) in N_Object_Declaration
8775 | N_Object_Renaming_Declaration
8776 and then Is_Return_Object (Defining_Entity (Par)));
8777 end Is_Expression_Of_Func_Return;
8778
8779 ---------------------------
8780 -- Is_Finalizable_Access --
8781 ---------------------------
8782
8783 function Is_Finalizable_Access (Decl : Node_Id) return Boolean is
8784 Obj : constant Entity_Id := Defining_Identifier (Decl);
8785 Typ : constant Entity_Id := Base_Type (Etype (Obj));
8786 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
8787 Expr : constant Node_Id := Expression (Decl);
8788
8789 Secondary_Stack_Val : constant Uint :=
8790 UI_From_Int (BIP_Allocation_Form'Pos (Secondary_Stack));
8791
8792 Actual : Node_Id;
8793 Call : Node_Id;
8794 Formal : Node_Id;
8795 Param : Node_Id;
8796
8797 begin
8798 -- The prerequisite is a reference to a controlled object
8799
8800 if No (Expr)
8801 or else Nkind (Expr) /= N_Reference
8802 or else not Needs_Finalization (Desig)
8803 then
8804 return False;
8805 end if;
8806
8807 Call := Unqual_Conv (Prefix (Expr));
8808
8809 -- For a BIP function call, the only case where the return object needs
8810 -- to be finalized through Obj is when it is allocated on the secondary
8811 -- stack; when it is allocated in the caller, it is finalized directly,
8812 -- and when it is allocated on the global heap or in a storage pool, it
8813 -- is finalized through another mechanism.
8814
8815 -- Obj : Access_Typ :=
8816 -- BIP_Function_Call (BIPalloc => Secondary_Stack, ...)'reference;
8817
8818 if Is_Build_In_Place_Function_Call (Call) then
8819
8820 -- Examine all parameter associations of the function call
8821
8822 Param := First (Parameter_Associations (Call));
8823 while Present (Param) loop
8824 if Nkind (Param) = N_Parameter_Association then
8825 Formal := Selector_Name (Param);
8826 Actual := Explicit_Actual_Parameter (Param);
8827
8828 -- A match for BIPalloc => Secondary_Stack has been found
8829
8830 if Is_Build_In_Place_Entity (Formal)
8831 and then BIP_Suffix_Kind (Formal) = BIP_Alloc_Form
8832 and then Nkind (Actual) = N_Integer_Literal
8833 and then Intval (Actual) = Secondary_Stack_Val
8834 then
8835 return True;
8836 end if;
8837 end if;
8838
8839 Next (Param);
8840 end loop;
8841
8842 -- For a non-BIP function call, the only case where the return object
8843 -- need not be finalized is when it itself is going to be returned.
8844
8845 -- Obj : Typ := Non_BIP_Function_Call'reference;
8846
8847 elsif Nkind (Call) = N_Function_Call
8848 and then not Is_Related_To_Func_Return (Obj)
8849 then
8850 return True;
8851 end if;
8852
8853 return False;
8854 end Is_Finalizable_Access;
8855
8856 ------------------------------
8857 -- Is_Finalizable_Transient --
8858 ------------------------------
8859
8860 function Is_Finalizable_Transient
8861 (Decl : Node_Id;
8862 N : Node_Id) return Boolean
8863 is
8864 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
8865 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8866
8867 function Is_Aliased
8868 (Trans_Id : Entity_Id;
8869 First_Stmt : Node_Id) return Boolean;
8870 -- Determine whether transient object Trans_Id has been renamed or
8871 -- aliased through 'reference in the statement list starting from
8872 -- First_Stmt.
8873
8874 function Is_Indexed_Container
8875 (Trans_Id : Entity_Id;
8876 First_Stmt : Node_Id) return Boolean;
8877 -- Determine whether transient object Trans_Id denotes a container which
8878 -- is in the process of being indexed in the statement list starting
8879 -- from First_Stmt.
8880
8881 function Is_Iterated_Container
8882 (Trans_Id : Entity_Id;
8883 First_Stmt : Node_Id) return Boolean;
8884 -- Determine whether transient object Trans_Id denotes a container which
8885 -- is in the process of being iterated in the statement list starting
8886 -- from First_Stmt.
8887
8888 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean;
8889 -- Return True if N is directly part of a build-in-place return
8890 -- statement.
8891
8892 ----------------
8893 -- Is_Aliased --
8894 ----------------
8895
8896 function Is_Aliased
8897 (Trans_Id : Entity_Id;
8898 First_Stmt : Node_Id) return Boolean
8899 is
8900 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
8901 -- Given an object renaming declaration, retrieve the entity within
8902 -- the renamed name, recursively if this entity is itself a renaming.
8903 -- Return Empty if the renamed name contains anything other than a
8904 -- variable or a constant.
8905
8906 -------------------------
8907 -- Find_Renamed_Object --
8908 -------------------------
8909
8910 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
8911 Ren_Obj : Node_Id := Empty;
8912
8913 function Find_Object (N : Node_Id) return Traverse_Result;
8914 -- Try to detect an object which is either a constant or a
8915 -- variable.
8916
8917 -----------------
8918 -- Find_Object --
8919 -----------------
8920
8921 function Find_Object (N : Node_Id) return Traverse_Result is
8922 begin
8923 -- Stop the search once a constant or a variable has been
8924 -- detected.
8925
8926 if Nkind (N) = N_Identifier
8927 and then Present (Entity (N))
8928 and then Ekind (Entity (N)) in E_Constant | E_Variable
8929 then
8930 Ren_Obj := Entity (N);
8931 return Abandon;
8932 end if;
8933
8934 return OK;
8935 end Find_Object;
8936
8937 procedure Search is new Traverse_Proc (Find_Object);
8938
8939 -- Local variables
8940
8941 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
8942
8943 -- Start of processing for Find_Renamed_Object
8944
8945 begin
8946 -- Actions related to dispatching calls may appear as renamings of
8947 -- tags. Do not process this type of renaming because it does not
8948 -- use the actual value of the object.
8949
8950 if not Is_RTE (Typ, RE_Tag_Ptr) then
8951 Search (Name (Ren_Decl));
8952 end if;
8953
8954 -- For renamings generated by Expand_N_Object_Declaration to deal
8955 -- with (class-wide) interface objects, there is an intermediate
8956 -- temporary of an anonymous access type used to hold the result
8957 -- of the displacement of the address of the renamed object.
8958
8959 if Present (Ren_Obj)
8960 and then Ekind (Ren_Obj) = E_Constant
8961 and then Is_Itype (Etype (Ren_Obj))
8962 and then Ekind (Etype (Ren_Obj)) = E_Anonymous_Access_Type
8963 and then
8964 Is_Class_Wide_Type (Directly_Designated_Type (Etype (Ren_Obj)))
8965 and then
8966 Is_Interface (Directly_Designated_Type (Etype (Ren_Obj)))
8967 then
8968 Search (Constant_Value (Ren_Obj));
8969 end if;
8970
8971 -- Recurse if Ren_Obj is itself a renaming
8972
8973 if Present (Ren_Obj)
8974 and then Ekind (Ren_Obj) in E_Constant | E_Variable
8975 and then Present (Renamed_Object (Ren_Obj))
8976 then
8977 return Find_Renamed_Object (Declaration_Node (Ren_Obj));
8978 else
8979 return Ren_Obj;
8980 end if;
8981 end Find_Renamed_Object;
8982
8983 -- Local variables
8984
8985 Expr : Node_Id;
8986 Ren_Obj : Entity_Id;
8987 Stmt : Node_Id;
8988
8989 -- Start of processing for Is_Aliased
8990
8991 begin
8992 -- Examine the statements following the controlled object and look
8993 -- for various forms of aliasing.
8994
8995 Stmt := First_Stmt;
8996 while Present (Stmt) loop
8997 -- (Transient) objects initialized by a reference to another named
8998 -- object are never finalized (see Is_Finalizable_Access), so we
8999 -- need not worry about finalizing (transient) referenced objects
9000 -- twice. Therefore, we only need to look at the nontransient
9001 -- object serviced by the transient scope, if it exists and is
9002 -- declared as a reference to another named object.
9003
9004 if Nkind (Stmt) = N_Object_Declaration
9005 and then Stmt = N
9006 then
9007 Expr := Expression (Stmt);
9008
9009 -- Aliasing of the form:
9010 -- Obj : ... := Trans_Id'reference;
9011
9012 if Present (Expr)
9013 and then Nkind (Expr) = N_Reference
9014 and then Is_Entity_Name (Prefix (Expr))
9015 and then Entity (Prefix (Expr)) = Trans_Id
9016 then
9017 return True;
9018 end if;
9019
9020 -- (Transient) renamings are never finalized so we need not worry
9021 -- about finalizing (transient) renamed objects twice. Therefore,
9022 -- we only need to look at the nontransient object serviced by the
9023 -- transient scope, if it exists and is declared as a renaming.
9024
9025 elsif Nkind (Stmt) = N_Object_Renaming_Declaration
9026 and then Stmt = N
9027 then
9028 Ren_Obj := Find_Renamed_Object (Stmt);
9029
9030 -- Aliasing of the form:
9031 -- Obj : ... renames ... Trans_Id ...;
9032
9033 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
9034 return True;
9035 end if;
9036 end if;
9037
9038 Next (Stmt);
9039 end loop;
9040
9041 return False;
9042 end Is_Aliased;
9043
9044 --------------------------
9045 -- Is_Indexed_Container --
9046 --------------------------
9047
9048 function Is_Indexed_Container
9049 (Trans_Id : Entity_Id;
9050 First_Stmt : Node_Id) return Boolean
9051 is
9052 Aspect : Node_Id;
9053 Call : Node_Id;
9054 Index : Entity_Id;
9055 Param : Node_Id;
9056 Stmt : Node_Id;
9057 Typ : Entity_Id;
9058
9059 begin
9060 -- It is not possible to iterate over containers in non-Ada 2012 code
9061
9062 if Ada_Version < Ada_2012 then
9063 return False;
9064 end if;
9065
9066 Typ := Etype (Trans_Id);
9067
9068 -- Handle access type created for the reference below
9069
9070 if Is_Access_Type (Typ) then
9071 Typ := Designated_Type (Typ);
9072 end if;
9073
9074 -- Look for aspect Constant_Indexing. It may be part of a type
9075 -- declaration for a container, or inherited from a base type
9076 -- or parent type.
9077
9078 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Constant_Indexing);
9079
9080 if Present (Aspect) then
9081 Index := Ultimate_Alias (Entity (Aspect));
9082
9083 -- Examine the statements following the container object and
9084 -- look for a call to the default indexing routine where the
9085 -- first parameter is the transient. Such a call appears as:
9086
9087 -- It : Access_To_Constant_Reference_Type :=
9088 -- Constant_Indexing (Trans_Id.all, ...)'reference;
9089
9090 Stmt := First_Stmt;
9091 while Present (Stmt) loop
9092
9093 -- Detect an object declaration which is initialized by a
9094 -- controlled function call.
9095
9096 if Nkind (Stmt) = N_Object_Declaration
9097 and then Present (Expression (Stmt))
9098 and then Nkind (Expression (Stmt)) = N_Reference
9099 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
9100 then
9101 Call := Prefix (Expression (Stmt));
9102
9103 -- The call must invoke the default indexing routine of
9104 -- the container and the transient object must appear as
9105 -- the first actual parameter. Skip any calls whose names
9106 -- are not entities.
9107
9108 if Is_Entity_Name (Name (Call))
9109 and then Entity (Name (Call)) = Index
9110 and then Present (Parameter_Associations (Call))
9111 then
9112 Param := First (Parameter_Associations (Call));
9113
9114 if Nkind (Param) = N_Explicit_Dereference
9115 and then Entity (Prefix (Param)) = Trans_Id
9116 then
9117 return True;
9118 end if;
9119 end if;
9120 end if;
9121
9122 Next (Stmt);
9123 end loop;
9124 end if;
9125
9126 return False;
9127 end Is_Indexed_Container;
9128
9129 ---------------------------
9130 -- Is_Iterated_Container --
9131 ---------------------------
9132
9133 function Is_Iterated_Container
9134 (Trans_Id : Entity_Id;
9135 First_Stmt : Node_Id) return Boolean
9136 is
9137 Aspect : Node_Id;
9138 Call : Node_Id;
9139 Iter : Entity_Id;
9140 Param : Node_Id;
9141 Stmt : Node_Id;
9142 Typ : Entity_Id;
9143
9144 begin
9145 -- It is not possible to iterate over containers in non-Ada 2012 code
9146
9147 if Ada_Version < Ada_2012 then
9148 return False;
9149 end if;
9150
9151 Typ := Etype (Trans_Id);
9152
9153 -- Handle access type created for the reference below
9154
9155 if Is_Access_Type (Typ) then
9156 Typ := Designated_Type (Typ);
9157 end if;
9158
9159 -- Look for aspect Default_Iterator. It may be part of a type
9160 -- declaration for a container, or inherited from a base type
9161 -- or parent type.
9162
9163 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
9164
9165 if Present (Aspect) then
9166 Iter := Ultimate_Alias (Entity (Aspect));
9167
9168 -- Examine the statements following the container object and
9169 -- look for a call to the default iterate routine where the
9170 -- first parameter is the transient. Such a call appears as:
9171
9172 -- It : Access_To_CW_Iterator :=
9173 -- Iterate (Trans_Id.all, ...)'reference;
9174
9175 Stmt := First_Stmt;
9176 while Present (Stmt) loop
9177
9178 -- Detect an object declaration which is initialized by a
9179 -- controlled function call.
9180
9181 if Nkind (Stmt) = N_Object_Declaration
9182 and then Present (Expression (Stmt))
9183 and then Nkind (Expression (Stmt)) = N_Reference
9184 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
9185 then
9186 Call := Prefix (Expression (Stmt));
9187
9188 -- The call must invoke the default iterate routine of
9189 -- the container and the transient object must appear as
9190 -- the first actual parameter. Skip any calls whose names
9191 -- are not entities.
9192
9193 if Is_Entity_Name (Name (Call))
9194 and then Entity (Name (Call)) = Iter
9195 and then Present (Parameter_Associations (Call))
9196 then
9197 Param := First (Parameter_Associations (Call));
9198
9199 if Nkind (Param) = N_Explicit_Dereference
9200 and then Entity (Prefix (Param)) = Trans_Id
9201 then
9202 return True;
9203 end if;
9204 end if;
9205 end if;
9206
9207 Next (Stmt);
9208 end loop;
9209 end if;
9210
9211 return False;
9212 end Is_Iterated_Container;
9213
9214 -------------------------------------
9215 -- Is_Part_Of_BIP_Return_Statement --
9216 -------------------------------------
9217
9218 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean is
9219 Subp : constant Entity_Id := Current_Subprogram;
9220 Context : Node_Id;
9221
9222 begin
9223 -- First check if N is part of a BIP function
9224
9225 if No (Subp) or else not Is_Build_In_Place_Function (Subp) then
9226 return False;
9227 end if;
9228
9229 -- Then check whether N is a complete part of a return statement
9230 -- Should we consider other node kinds to go up the tree???
9231
9232 Context := N;
9233 loop
9234 case Nkind (Context) is
9235 when N_Expression_With_Actions => Context := Parent (Context);
9236 when N_Simple_Return_Statement => return True;
9237 when others => return False;
9238 end case;
9239 end loop;
9240 end Is_Part_Of_BIP_Return_Statement;
9241
9242 -- Local variables
9243
9244 Desig : Entity_Id := Obj_Typ;
9245
9246 -- Start of processing for Is_Finalizable_Transient
9247
9248 begin
9249 -- If the node serviced by the transient context is a return statement,
9250 -- then the finalization needs to be deferred to the generic machinery.
9251
9252 if Nkind (N) = N_Simple_Return_Statement
9253 or else Is_Part_Of_BIP_Return_Statement (N)
9254 then
9255 return False;
9256 end if;
9257
9258 -- Handle access types
9259
9260 if Is_Access_Type (Desig) then
9261 Desig := Available_View (Designated_Type (Desig));
9262 end if;
9263
9264 return
9265 Ekind (Obj_Id) in E_Constant | E_Variable
9266 and then Needs_Finalization (Desig)
9267
9268 -- Do not consider a transient object that was already processed
9269
9270 and then not Is_Finalized_Transient (Obj_Id)
9271
9272 -- Do not consider iterators because those are treated as normal
9273 -- controlled objects and are processed by the usual finalization
9274 -- machinery. This avoids the double finalization of an iterator.
9275
9276 and then not Is_Iterator (Desig)
9277
9278 -- If the transient object is of an access type, check that it must
9279 -- be finalized.
9280
9281 and then (not Is_Access_Type (Obj_Typ)
9282 or else Is_Finalizable_Access (Decl))
9283
9284 -- Do not consider renamed transient objects because the act of
9285 -- renaming extends the object's lifetime.
9286
9287 and then not Is_Aliased (Obj_Id, Decl)
9288
9289 -- Do not consider containers in the context of iterator loops. Such
9290 -- transient objects must exist for as long as the loop is around,
9291 -- otherwise any operation carried out by the iterator will fail.
9292
9293 and then not Is_Iterated_Container (Obj_Id, Decl)
9294
9295 -- Likewise for indexed containers in the context of iterator loops
9296
9297 and then not Is_Indexed_Container (Obj_Id, Decl);
9298 end Is_Finalizable_Transient;
9299
9300 ---------------------------------
9301 -- Is_Fully_Repped_Tagged_Type --
9302 ---------------------------------
9303
9304 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
9305 U : constant Entity_Id := Underlying_Type (T);
9306 Comp : Entity_Id;
9307
9308 begin
9309 if No (U) or else not Is_Tagged_Type (U) then
9310 return False;
9311 elsif Has_Discriminants (U) then
9312 return False;
9313 elsif not Has_Specified_Layout (U) then
9314 return False;
9315 end if;
9316
9317 -- Here we have a tagged type, see if it has any component (other than
9318 -- tag and parent) with no component_clause. If so, we return False.
9319
9320 Comp := First_Component (U);
9321 while Present (Comp) loop
9322 if not Is_Tag (Comp)
9323 and then Chars (Comp) /= Name_uParent
9324 and then No (Component_Clause (Comp))
9325 then
9326 return False;
9327 else
9328 Next_Component (Comp);
9329 end if;
9330 end loop;
9331
9332 -- All components have clauses
9333
9334 return True;
9335 end Is_Fully_Repped_Tagged_Type;
9336
9337 ----------------------------------
9338 -- Is_Library_Level_Tagged_Type --
9339 ----------------------------------
9340
9341 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
9342 begin
9343 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
9344 end Is_Library_Level_Tagged_Type;
9345
9346 --------------------
9347 -- Is_LSP_Wrapper --
9348 --------------------
9349
9350 function Is_LSP_Wrapper (E : Entity_Id) return Boolean is
9351 begin
9352 return Is_Dispatch_Table_Wrapper (E)
9353 and then Present (LSP_Subprogram (E));
9354 end Is_LSP_Wrapper;
9355
9356 ----------------------------------
9357 -- Is_Possibly_Unaligned_Object --
9358 ----------------------------------
9359
9360 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
9361 T : constant Entity_Id := Etype (N);
9362
9363 begin
9364 -- If renamed object, apply test to underlying object
9365
9366 if Is_Entity_Name (N)
9367 and then Is_Object (Entity (N))
9368 and then Present (Renamed_Object (Entity (N)))
9369 then
9370 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
9371 end if;
9372
9373 -- Tagged and controlled types and aliased types are always aligned, as
9374 -- are concurrent types.
9375
9376 if Is_Aliased (T)
9377 or else Has_Controlled_Component (T)
9378 or else Is_Concurrent_Type (T)
9379 or else Is_Tagged_Type (T)
9380 or else Is_Controlled (T)
9381 then
9382 return False;
9383 end if;
9384
9385 -- If this is an element of a packed array, may be unaligned
9386
9387 if Is_Ref_To_Bit_Packed_Array (N) then
9388 return True;
9389 end if;
9390
9391 -- Case of indexed component reference: test whether prefix is unaligned
9392
9393 if Nkind (N) = N_Indexed_Component then
9394 return Is_Possibly_Unaligned_Object (Prefix (N));
9395
9396 -- Case of selected component reference
9397
9398 elsif Nkind (N) = N_Selected_Component then
9399 declare
9400 P : constant Node_Id := Prefix (N);
9401 C : constant Entity_Id := Entity (Selector_Name (N));
9402 M : Nat;
9403 S : Nat;
9404
9405 begin
9406 -- If component reference is for an array with nonstatic bounds,
9407 -- then it is always aligned: we can only process unaligned arrays
9408 -- with static bounds (more precisely compile time known bounds).
9409
9410 if Is_Array_Type (T)
9411 and then not Compile_Time_Known_Bounds (T)
9412 then
9413 return False;
9414 end if;
9415
9416 -- If component is aliased, it is definitely properly aligned
9417
9418 if Is_Aliased (C) then
9419 return False;
9420 end if;
9421
9422 -- If component is for a type implemented as a scalar, and the
9423 -- record is packed, and the component is other than the first
9424 -- component of the record, then the component may be unaligned.
9425
9426 if Is_Packed (Etype (P))
9427 and then Represented_As_Scalar (Etype (C))
9428 and then First_Entity (Scope (C)) /= C
9429 then
9430 return True;
9431 end if;
9432
9433 -- Compute maximum possible alignment for T
9434
9435 -- If alignment is known, then that settles things
9436
9437 if Known_Alignment (T) then
9438 M := UI_To_Int (Alignment (T));
9439
9440 -- If alignment is not known, tentatively set max alignment
9441
9442 else
9443 M := Ttypes.Maximum_Alignment;
9444
9445 -- We can reduce this if the Esize is known since the default
9446 -- alignment will never be more than the smallest power of 2
9447 -- that does not exceed this Esize value.
9448
9449 if Known_Esize (T) then
9450 S := UI_To_Int (Esize (T));
9451
9452 while (M / 2) >= S loop
9453 M := M / 2;
9454 end loop;
9455 end if;
9456 end if;
9457
9458 -- Case of component clause present which may specify an
9459 -- unaligned position.
9460
9461 if Present (Component_Clause (C)) then
9462
9463 -- Otherwise we can do a test to make sure that the actual
9464 -- start position in the record, and the length, are both
9465 -- consistent with the required alignment. If not, we know
9466 -- that we are unaligned.
9467
9468 declare
9469 Align_In_Bits : constant Nat := M * System_Storage_Unit;
9470 Comp : Entity_Id;
9471
9472 begin
9473 Comp := C;
9474
9475 -- For a component inherited in a record extension, the
9476 -- clause is inherited but position and size are not set.
9477
9478 if Is_Base_Type (Etype (P))
9479 and then Is_Tagged_Type (Etype (P))
9480 and then Present (Original_Record_Component (Comp))
9481 then
9482 Comp := Original_Record_Component (Comp);
9483 end if;
9484
9485 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
9486 or else Esize (Comp) mod Align_In_Bits /= 0
9487 then
9488 return True;
9489 end if;
9490 end;
9491 end if;
9492
9493 -- Otherwise, for a component reference, test prefix
9494
9495 return Is_Possibly_Unaligned_Object (P);
9496 end;
9497
9498 -- If not a component reference, must be aligned
9499
9500 else
9501 return False;
9502 end if;
9503 end Is_Possibly_Unaligned_Object;
9504
9505 ---------------------------------
9506 -- Is_Possibly_Unaligned_Slice --
9507 ---------------------------------
9508
9509 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
9510 begin
9511 -- Go to renamed object
9512
9513 if Is_Entity_Name (N)
9514 and then Is_Object (Entity (N))
9515 and then Present (Renamed_Object (Entity (N)))
9516 then
9517 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
9518 end if;
9519
9520 -- The reference must be a slice
9521
9522 if Nkind (N) /= N_Slice then
9523 return False;
9524 end if;
9525
9526 -- If it is a slice, then look at the array type being sliced
9527
9528 declare
9529 Sarr : constant Node_Id := Prefix (N);
9530 -- Prefix of the slice, i.e. the array being sliced
9531
9532 Styp : constant Entity_Id := Etype (Prefix (N));
9533 -- Type of the array being sliced
9534
9535 Pref : Node_Id;
9536 Ptyp : Entity_Id;
9537
9538 begin
9539 -- The problems arise if the array object that is being sliced
9540 -- is a component of a record or array, and we cannot guarantee
9541 -- the alignment of the array within its containing object.
9542
9543 -- To investigate this, we look at successive prefixes to see
9544 -- if we have a worrisome indexed or selected component.
9545
9546 Pref := Sarr;
9547 loop
9548 -- Case of array is part of an indexed component reference
9549
9550 if Nkind (Pref) = N_Indexed_Component then
9551 Ptyp := Etype (Prefix (Pref));
9552
9553 -- The only problematic case is when the array is packed, in
9554 -- which case we really know nothing about the alignment of
9555 -- individual components.
9556
9557 if Is_Bit_Packed_Array (Ptyp) then
9558 return True;
9559 end if;
9560
9561 -- Case of array is part of a selected component reference
9562
9563 elsif Nkind (Pref) = N_Selected_Component then
9564 Ptyp := Etype (Prefix (Pref));
9565
9566 -- We are definitely in trouble if the record in question
9567 -- has an alignment, and either we know this alignment is
9568 -- inconsistent with the alignment of the slice, or we don't
9569 -- know what the alignment of the slice should be. But this
9570 -- really matters only if the target has strict alignment.
9571
9572 if Target_Strict_Alignment
9573 and then Known_Alignment (Ptyp)
9574 and then (not Known_Alignment (Styp)
9575 or else Alignment (Styp) > Alignment (Ptyp))
9576 then
9577 return True;
9578 end if;
9579
9580 -- We are in potential trouble if the record type is packed.
9581 -- We could special case when we know that the array is the
9582 -- first component, but that's not such a simple case ???
9583
9584 if Is_Packed (Ptyp) then
9585 return True;
9586 end if;
9587
9588 -- We are in trouble if there is a component clause, and
9589 -- either we do not know the alignment of the slice, or
9590 -- the alignment of the slice is inconsistent with the
9591 -- bit position specified by the component clause.
9592
9593 declare
9594 Field : constant Entity_Id := Entity (Selector_Name (Pref));
9595 begin
9596 if Present (Component_Clause (Field))
9597 and then
9598 (not Known_Alignment (Styp)
9599 or else
9600 (Component_Bit_Offset (Field) mod
9601 (System_Storage_Unit * Alignment (Styp))) /= 0)
9602 then
9603 return True;
9604 end if;
9605 end;
9606
9607 -- For cases other than selected or indexed components we know we
9608 -- are OK, since no issues arise over alignment.
9609
9610 else
9611 return False;
9612 end if;
9613
9614 -- We processed an indexed component or selected component
9615 -- reference that looked safe, so keep checking prefixes.
9616
9617 Pref := Prefix (Pref);
9618 end loop;
9619 end;
9620 end Is_Possibly_Unaligned_Slice;
9621
9622 -------------------------------
9623 -- Is_Related_To_Func_Return --
9624 -------------------------------
9625
9626 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
9627 Expr : constant Node_Id := Related_Expression (Id);
9628
9629 begin
9630 -- In the case of a function with a class-wide result that returns
9631 -- a call to a function with a specific result, we introduce a
9632 -- type conversion for the return expression. We do not want that
9633 -- type conversion to influence the result of this function.
9634
9635 return Present (Expr)
9636 and then Nkind (Unqual_Conv (Expr)) = N_Explicit_Dereference
9637 and then Is_Expression_Of_Func_Return (Expr);
9638 end Is_Related_To_Func_Return;
9639
9640 --------------------------------
9641 -- Is_Ref_To_Bit_Packed_Array --
9642 --------------------------------
9643
9644 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
9645 Result : Boolean;
9646 Expr : Node_Id;
9647
9648 begin
9649 if Is_Entity_Name (N)
9650 and then Is_Object (Entity (N))
9651 and then Present (Renamed_Object (Entity (N)))
9652 then
9653 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
9654 end if;
9655
9656 if Nkind (N) in N_Indexed_Component | N_Selected_Component then
9657 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
9658 Result := True;
9659 else
9660 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
9661 end if;
9662
9663 if Result and then Nkind (N) = N_Indexed_Component then
9664 Expr := First (Expressions (N));
9665 while Present (Expr) loop
9666 Force_Evaluation (Expr);
9667 Next (Expr);
9668 end loop;
9669 end if;
9670
9671 return Result;
9672
9673 else
9674 return False;
9675 end if;
9676 end Is_Ref_To_Bit_Packed_Array;
9677
9678 --------------------------------
9679 -- Is_Ref_To_Bit_Packed_Slice --
9680 --------------------------------
9681
9682 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
9683 begin
9684 if Nkind (N) = N_Type_Conversion then
9685 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
9686
9687 elsif Is_Entity_Name (N)
9688 and then Is_Object (Entity (N))
9689 and then Present (Renamed_Object (Entity (N)))
9690 then
9691 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
9692
9693 elsif Nkind (N) = N_Slice
9694 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
9695 then
9696 return True;
9697
9698 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9699 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
9700
9701 else
9702 return False;
9703 end if;
9704 end Is_Ref_To_Bit_Packed_Slice;
9705
9706 -----------------------
9707 -- Is_Renamed_Object --
9708 -----------------------
9709
9710 function Is_Renamed_Object (N : Node_Id) return Boolean is
9711 Pnod : constant Node_Id := Parent (N);
9712 Kind : constant Node_Kind := Nkind (Pnod);
9713 begin
9714 if Kind = N_Object_Renaming_Declaration then
9715 return True;
9716 elsif Kind in N_Indexed_Component | N_Selected_Component then
9717 return Is_Renamed_Object (Pnod);
9718 else
9719 return False;
9720 end if;
9721 end Is_Renamed_Object;
9722
9723 ------------------------------
9724 -- Is_Secondary_Stack_Thunk --
9725 ------------------------------
9726
9727 function Is_Secondary_Stack_Thunk (Id : Entity_Id) return Boolean is
9728 begin
9729 return Ekind (Id) = E_Function
9730 and then Is_Thunk (Id)
9731 and then Has_Controlling_Result (Id);
9732 end Is_Secondary_Stack_Thunk;
9733
9734 ----------------------------
9735 -- Is_Statically_Disabled --
9736 ----------------------------
9737
9738 function Is_Statically_Disabled
9739 (N : Node_Id;
9740 Value : Boolean;
9741 Include_Valid : Boolean)
9742 return Boolean
9743 is
9744 function Is_Discrete_Literal (N : Node_Id) return Boolean;
9745 -- Returns whether N is an integer, character or enumeration literal
9746
9747 -------------------------
9748 -- Is_Discrete_Literal --
9749 -------------------------
9750
9751 function Is_Discrete_Literal (N : Node_Id) return Boolean is
9752 (Nkind (N) in N_Integer_Literal | N_Character_Literal
9753 or else (Nkind (N) in N_Identifier | N_Expanded_Name
9754 and then Ekind (Entity (N)) = E_Enumeration_Literal));
9755
9756 Expr_N : constant Node_Id :=
9757 (if Is_Static_Expression (N)
9758 and then Entity (N) in Standard_True | Standard_False
9759 and then Is_Rewrite_Substitution (N)
9760 then Original_Node (N)
9761 else N);
9762
9763 -- Start of processing for Is_Statically_Disabled
9764
9765 begin
9766 -- A "statically disabled" condition which evaluates to Value is either:
9767
9768 case Nkind (Expr_N) is
9769
9770 -- an AND or AND THEN operator when:
9771 -- - Value is True and both operands are statically disabled
9772 -- conditions evaluated to True.
9773 -- - Value is False and at least one operand is a statically disabled
9774 -- condition evaluated to False.
9775
9776 when N_Op_And | N_And_Then =>
9777 return
9778 (if Value then
9779 (Is_Statically_Disabled
9780 (Left_Opnd (Expr_N), Value, Include_Valid)
9781 and then Is_Statically_Disabled
9782 (Right_Opnd (Expr_N), Value, Include_Valid))
9783 else
9784 (Is_Statically_Disabled
9785 (Left_Opnd (Expr_N), Value, Include_Valid)
9786 or else Is_Statically_Disabled
9787 (Right_Opnd (Expr_N), Value, Include_Valid)));
9788
9789 -- an OR or OR ELSE operator when:
9790 -- - Value is True and at least one operand is a statically disabled
9791 -- condition evaluated to True.
9792 -- - Value is False and both operands are statically disabled
9793 -- conditions evaluated to False.
9794
9795 when N_Op_Or | N_Or_Else =>
9796 return
9797 (if Value then
9798 (Is_Statically_Disabled
9799 (Left_Opnd (Expr_N), Value, Include_Valid)
9800 or else Is_Statically_Disabled
9801 (Right_Opnd (Expr_N), Value, Include_Valid))
9802 else
9803 (Is_Statically_Disabled
9804 (Left_Opnd (Expr_N), Value, Include_Valid)
9805 and then Is_Statically_Disabled
9806 (Right_Opnd (Expr_N), Value, Include_Valid)));
9807
9808 -- a NOT operator when the right operand is a statically disabled
9809 -- condition evaluated to the negation of Value.
9810
9811 when N_Op_Not =>
9812 return Is_Statically_Disabled
9813 (Right_Opnd (Expr_N), not Value, Include_Valid);
9814
9815 -- a static constant when it is of a boolean type with aspect
9816 -- Warnings Off.
9817
9818 when N_Identifier | N_Expanded_Name =>
9819 return Is_Static_Expression (Expr_N)
9820 and then Value = Is_True (Expr_Value (Expr_N))
9821 and then Ekind (Entity (Expr_N)) = E_Constant
9822 and then Has_Warnings_Off (Entity (Expr_N));
9823
9824 -- a relational_operator where one operand is a static constant with
9825 -- aspect Warnings Off and the other operand is a literal of the
9826 -- corresponding type.
9827
9828 when N_Op_Compare =>
9829 declare
9830 Left : constant Node_Id := Left_Opnd (Expr_N);
9831 Right : constant Node_Id := Right_Opnd (Expr_N);
9832 begin
9833 return
9834 Is_Static_Expression (N)
9835 and then Value = Is_True (Expr_Value (N))
9836 and then
9837 ((Is_Discrete_Literal (Right)
9838 and then Nkind (Left) in N_Identifier
9839 | N_Expanded_Name
9840 and then Ekind (Entity (Left)) = E_Constant
9841 and then Has_Warnings_Off (Entity (Left)))
9842 or else
9843 (Is_Discrete_Literal (Left)
9844 and then Nkind (Right) in N_Identifier
9845 | N_Expanded_Name
9846 and then Ekind (Entity (Right)) = E_Constant
9847 and then Has_Warnings_Off (Entity (Right))));
9848 end;
9849
9850 -- a reference to 'Valid or 'Valid_Scalar if Include_Valid is True
9851
9852 when N_Attribute_Reference =>
9853 return Include_Valid
9854 and then Get_Attribute_Id (Attribute_Name (Expr_N)) in
9855 Attribute_Valid | Attribute_Valid_Scalars
9856 and then Value;
9857
9858 when others =>
9859 return False;
9860 end case;
9861 end Is_Statically_Disabled;
9862
9863 --------------------------------
9864 -- Is_Uninitialized_Aggregate --
9865 --------------------------------
9866
9867 function Is_Uninitialized_Aggregate
9868 (Exp : Node_Id;
9869 T : Entity_Id) return Boolean
9870 is
9871 Comp : Node_Id;
9872 Comp_Type : Entity_Id;
9873 Typ : Entity_Id;
9874
9875 begin
9876 if Nkind (Exp) /= N_Aggregate then
9877 return False;
9878 end if;
9879
9880 Preanalyze_And_Resolve (Exp, T);
9881 Typ := Etype (Exp);
9882
9883 if No (Typ)
9884 or else Ekind (Typ) /= E_Array_Subtype
9885 or else Present (Expressions (Exp))
9886 or else No (Component_Associations (Exp))
9887 then
9888 return False;
9889 else
9890 Comp_Type := Component_Type (Typ);
9891 Comp := First (Component_Associations (Exp));
9892
9893 if not Box_Present (Comp)
9894 or else Present (Next (Comp))
9895 then
9896 return False;
9897 end if;
9898
9899 return Is_Scalar_Type (Comp_Type)
9900 and then No (Default_Aspect_Component_Value (Typ));
9901 end if;
9902 end Is_Uninitialized_Aggregate;
9903
9904 ----------------------------
9905 -- Is_Untagged_Derivation --
9906 ----------------------------
9907
9908 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
9909 begin
9910 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
9911 or else
9912 (Is_Private_Type (T)
9913 and then Present (Full_View (T))
9914 and then not Is_Tagged_Type (Full_View (T))
9915 and then Is_Derived_Type (Full_View (T))
9916 and then Etype (Full_View (T)) /= T);
9917 end Is_Untagged_Derivation;
9918
9919 ------------------------------------
9920 -- Is_Untagged_Private_Derivation --
9921 ------------------------------------
9922
9923 function Is_Untagged_Private_Derivation
9924 (Priv_Typ : Entity_Id;
9925 Full_Typ : Entity_Id) return Boolean
9926 is
9927 begin
9928 return
9929 Present (Priv_Typ)
9930 and then Is_Untagged_Derivation (Priv_Typ)
9931 and then Is_Private_Type (Etype (Priv_Typ))
9932 and then Present (Full_Typ)
9933 and then Is_Itype (Full_Typ);
9934 end Is_Untagged_Private_Derivation;
9935
9936 ------------------------------
9937 -- Is_Verifiable_DIC_Pragma --
9938 ------------------------------
9939
9940 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
9941 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9942
9943 begin
9944 -- To qualify as verifiable, a DIC pragma must have a non-null argument
9945
9946 return
9947 Present (Args)
9948
9949 -- If there are args, but the first arg is Empty, then treat the
9950 -- pragma the same as having no args (there may be a second arg that
9951 -- is an implicitly added type arg, and Empty is a placeholder).
9952
9953 and then Present (Get_Pragma_Arg (First (Args)))
9954
9955 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
9956 end Is_Verifiable_DIC_Pragma;
9957
9958 ---------------------------
9959 -- Is_Volatile_Reference --
9960 ---------------------------
9961
9962 function Is_Volatile_Reference (N : Node_Id) return Boolean is
9963 begin
9964 -- Only source references are to be treated as volatile, internally
9965 -- generated stuff cannot have volatile external effects.
9966
9967 if not Comes_From_Source (N) then
9968 return False;
9969
9970 -- Never true for reference to a type
9971
9972 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9973 return False;
9974
9975 -- Never true for a compile time known constant
9976
9977 elsif Compile_Time_Known_Value (N) then
9978 return False;
9979
9980 -- True if object reference with volatile type
9981
9982 elsif Is_Volatile_Object_Ref (N) then
9983 return True;
9984
9985 -- True if reference to volatile entity
9986
9987 elsif Is_Entity_Name (N) then
9988 return Treat_As_Volatile (Entity (N));
9989
9990 -- True for slice of volatile array
9991
9992 elsif Nkind (N) = N_Slice then
9993 return Is_Volatile_Reference (Prefix (N));
9994
9995 -- True if volatile component
9996
9997 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9998 if (Is_Entity_Name (Prefix (N))
9999 and then Has_Volatile_Components (Entity (Prefix (N))))
10000 or else (Present (Etype (Prefix (N)))
10001 and then Has_Volatile_Components (Etype (Prefix (N))))
10002 then
10003 return True;
10004 else
10005 return Is_Volatile_Reference (Prefix (N));
10006 end if;
10007
10008 -- Otherwise false
10009
10010 else
10011 return False;
10012 end if;
10013 end Is_Volatile_Reference;
10014
10015 --------------------
10016 -- Kill_Dead_Code --
10017 --------------------
10018
10019 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
10020 W : Boolean := Warn;
10021 -- Set False if warnings suppressed
10022
10023 begin
10024 if Present (N) then
10025 Remove_Warning_Messages (N);
10026
10027 -- Update the internal structures of the ABE mechanism in case the
10028 -- dead node is an elaboration scenario.
10029
10030 Kill_Elaboration_Scenario (N);
10031
10032 -- Generate warning if appropriate
10033
10034 if W then
10035
10036 -- We suppress the warning if this code is under control of an
10037 -- if/case statement and either
10038 -- a) we are in an instance and the condition/selector
10039 -- has a statically known value; or
10040 -- b) the selector of a case statement is a simple identifier
10041 -- and warnings off is set for this identifier; or
10042 -- c) the condition of an if statement is a "statically
10043 -- disabled" condition which evaluates to False as described
10044 -- in section 7.3.2 of SPARK User's Guide.
10045 -- Dead code is common and reasonable in instances, so we don't
10046 -- want a warning in that case.
10047
10048 declare
10049 C : Node_Id := Empty;
10050 begin
10051 if Nkind (Parent (N)) = N_If_Statement then
10052 C := Condition (Parent (N));
10053
10054 if Is_Statically_Disabled
10055 (C, Value => False, Include_Valid => False)
10056 then
10057 W := False;
10058 end if;
10059
10060 elsif Nkind (Parent (N)) = N_Case_Statement_Alternative then
10061 C := Expression (Parent (Parent (N)));
10062
10063 if Nkind (C) = N_Identifier
10064 and then Present (Entity (C))
10065 and then Has_Warnings_Off (Entity (C))
10066 then
10067 W := False;
10068 end if;
10069 end if;
10070
10071 if Present (C)
10072 and then (In_Instance and Compile_Time_Known_Value (C))
10073 then
10074 W := False;
10075 end if;
10076 end;
10077
10078 -- Generate warning if not suppressed
10079
10080 if W then
10081 Error_Msg_F
10082 ("?t?this code can never be executed and has been deleted!",
10083 N);
10084 end if;
10085 end if;
10086
10087 -- Recurse into block statements and bodies to process declarations
10088 -- and statements.
10089
10090 if Nkind (N) = N_Block_Statement
10091 or else Nkind (N) = N_Subprogram_Body
10092 or else Nkind (N) = N_Package_Body
10093 then
10094 Kill_Dead_Code (Declarations (N), False);
10095 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
10096
10097 if Nkind (N) = N_Subprogram_Body then
10098 Set_Is_Eliminated (Defining_Entity (N));
10099 end if;
10100
10101 elsif Nkind (N) = N_Package_Declaration then
10102 Kill_Dead_Code (Visible_Declarations (Specification (N)));
10103 Kill_Dead_Code (Private_Declarations (Specification (N)));
10104
10105 -- ??? After this point, Delete_Tree has been called on all
10106 -- declarations in Specification (N), so references to entities
10107 -- therein look suspicious.
10108
10109 declare
10110 E : Entity_Id := First_Entity (Defining_Entity (N));
10111
10112 begin
10113 while Present (E) loop
10114 if Ekind (E) = E_Operator then
10115 Set_Is_Eliminated (E);
10116 end if;
10117
10118 Next_Entity (E);
10119 end loop;
10120 end;
10121
10122 -- Recurse into composite statement to kill individual statements in
10123 -- particular instantiations.
10124
10125 elsif Nkind (N) = N_If_Statement then
10126 Kill_Dead_Code (Then_Statements (N));
10127 Kill_Dead_Code (Elsif_Parts (N));
10128 Kill_Dead_Code (Else_Statements (N));
10129
10130 elsif Nkind (N) = N_Loop_Statement then
10131 Kill_Dead_Code (Statements (N));
10132
10133 elsif Nkind (N) = N_Case_Statement then
10134 declare
10135 Alt : Node_Id;
10136 begin
10137 Alt := First (Alternatives (N));
10138 while Present (Alt) loop
10139 Kill_Dead_Code (Statements (Alt));
10140 Next (Alt);
10141 end loop;
10142 end;
10143
10144 elsif Nkind (N) = N_Case_Statement_Alternative then
10145 Kill_Dead_Code (Statements (N));
10146
10147 -- Deal with dead instances caused by deleting instantiations
10148
10149 elsif Nkind (N) in N_Generic_Instantiation then
10150 Remove_Dead_Instance (N);
10151 end if;
10152 end if;
10153 end Kill_Dead_Code;
10154
10155 -- Case where argument is a list of nodes to be killed
10156
10157 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
10158 N : Node_Id;
10159 W : Boolean;
10160
10161 begin
10162 W := Warn;
10163
10164 N := First (L);
10165 while Present (N) loop
10166 Kill_Dead_Code (N, W);
10167 W := False;
10168 Next (N);
10169 end loop;
10170 end Kill_Dead_Code;
10171
10172 -----------------------------
10173 -- Make_CW_Equivalent_Type --
10174 -----------------------------
10175
10176 -- Create a record type used as an equivalent of any member of the class
10177 -- which takes its size from exp.
10178
10179 -- Generate the following code:
10180
10181 -- type Equiv_T is record
10182 -- _parent : T (List of discriminant constraints taken from Exp);
10183 -- Cnn : Storage_Array (1 .. (Exp'size - Typ'object_size)/Storage_Unit);
10184 -- end Equiv_T;
10185 --
10186 -- Note that this type does not guarantee same alignment as all derived
10187 -- types.
10188 --
10189 -- Note: for the freezing circuitry, this looks like a record extension,
10190 -- and so we need to make sure that the scalar storage order is the same
10191 -- as that of the parent type. (This does not change anything for the
10192 -- representation of the extension part.)
10193
10194 function Make_CW_Equivalent_Type
10195 (T : Entity_Id;
10196 E : Node_Id;
10197 List_Def : out List_Id) return Entity_Id
10198 is
10199 Loc : constant Source_Ptr := Sloc (E);
10200 Root_Typ : constant Entity_Id := Root_Type (T);
10201 Root_Utyp : constant Entity_Id := Underlying_Type (Root_Typ);
10202 Comp_List : constant List_Id := New_List;
10203
10204 Equiv_Type : Entity_Id;
10205 Range_Type : Entity_Id;
10206 Str_Type : Entity_Id;
10207 Constr_Root : Entity_Id;
10208 Size_Attr : Node_Id;
10209 Size_Expr : Node_Id;
10210
10211 begin
10212 List_Def := New_List;
10213
10214 -- If the root type is already constrained, there are no discriminants
10215 -- in the expression.
10216
10217 if not Has_Discriminants (Root_Typ)
10218 or else Is_Constrained (Root_Typ)
10219 then
10220 Constr_Root := Root_Typ;
10221
10222 -- At this point in the expansion, nonlimited view of the type
10223 -- must be available, otherwise the error will be reported later.
10224
10225 if From_Limited_With (Constr_Root)
10226 and then Present (Non_Limited_View (Constr_Root))
10227 then
10228 Constr_Root := Non_Limited_View (Constr_Root);
10229 end if;
10230
10231 else
10232 Constr_Root := Make_Temporary (Loc, 'R');
10233
10234 -- subtype cstr__n is T (List of discr constraints taken from Exp)
10235
10236 Append_To (List_Def,
10237 Make_Subtype_Declaration (Loc,
10238 Defining_Identifier => Constr_Root,
10239 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
10240 end if;
10241
10242 -- Generate the range subtype declaration
10243
10244 Range_Type := Make_Temporary (Loc, 'G');
10245
10246 -- If the expression is known to have the tag of its type, then we can
10247 -- use it directly for the prefix of the Size attribute; otherwise we
10248 -- need to convert it first to the class-wide type to force a call to
10249 -- the _Size primitive operation.
10250
10251 if No (E) then
10252 Size_Attr := Make_Integer_Literal (Loc, RM_Size (T));
10253
10254 elsif Has_Tag_Of_Type (E) then
10255 if not Has_Discriminants (Etype (E))
10256 or else Is_Constrained (Etype (E))
10257 then
10258 Size_Attr :=
10259 Make_Attribute_Reference (Loc,
10260 Prefix => New_Occurrence_Of (Etype (E), Loc),
10261 Attribute_Name => Name_Object_Size);
10262
10263 else
10264 Size_Attr :=
10265 Make_Attribute_Reference (Loc,
10266 Prefix => Duplicate_Subexpr_No_Checks (E),
10267 Attribute_Name => Name_Size);
10268 end if;
10269
10270 else
10271 Size_Attr :=
10272 Make_Attribute_Reference (Loc,
10273 Prefix => OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
10274 Attribute_Name => Name_Size);
10275 end if;
10276
10277 if not Is_Interface (Root_Typ) and then Present (E) then
10278
10279 -- subtype rg__xx is
10280 -- Storage_Offset range 1 .. (Exp'size - Typ'object_size)
10281 -- / Storage_Unit
10282
10283 Size_Expr :=
10284 Make_Op_Subtract (Loc,
10285 Left_Opnd => Size_Attr,
10286 Right_Opnd =>
10287 Make_Attribute_Reference (Loc,
10288 Prefix => New_Occurrence_Of (Constr_Root, Loc),
10289 Attribute_Name => Name_Object_Size));
10290 else
10291 -- subtype rg__xx is
10292 -- Storage_Offset range 1 .. (Exp'size - Ada.Tags.Tag'object_size)
10293 -- / Storage_Unit
10294
10295 Size_Expr :=
10296 Make_Op_Subtract (Loc,
10297 Left_Opnd => Size_Attr,
10298 Right_Opnd =>
10299 Make_Attribute_Reference (Loc,
10300 Prefix => New_Occurrence_Of (RTE (RE_Tag), Loc),
10301 Attribute_Name => Name_Object_Size));
10302 end if;
10303
10304 Set_Paren_Count (Size_Expr, 1);
10305
10306 Append_To (List_Def,
10307 Make_Subtype_Declaration (Loc,
10308 Defining_Identifier => Range_Type,
10309 Subtype_Indication =>
10310 Make_Subtype_Indication (Loc,
10311 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
10312 Constraint => Make_Range_Constraint (Loc,
10313 Range_Expression =>
10314 Make_Range (Loc,
10315 Low_Bound => Make_Integer_Literal (Loc, 1),
10316 High_Bound =>
10317 Make_Op_Divide (Loc,
10318 Left_Opnd => Size_Expr,
10319 Right_Opnd => Make_Integer_Literal (Loc,
10320 Intval => System_Storage_Unit)))))));
10321
10322 -- subtype str__nn is Storage_Array (rg__x);
10323
10324 Str_Type := Make_Temporary (Loc, 'S');
10325 Append_To (List_Def,
10326 Make_Subtype_Declaration (Loc,
10327 Defining_Identifier => Str_Type,
10328 Subtype_Indication =>
10329 Make_Subtype_Indication (Loc,
10330 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
10331 Constraint =>
10332 Make_Index_Or_Discriminant_Constraint (Loc,
10333 Constraints =>
10334 New_List (New_Occurrence_Of (Range_Type, Loc))))));
10335
10336 -- type Equiv_T is record
10337 -- _Parent : Snn; -- not interface
10338 -- _Tag : Ada.Tags.Tag -- interface
10339 -- Cnn : Str_Type;
10340 -- end Equiv_T;
10341
10342 Equiv_Type := Make_Temporary (Loc, 'T');
10343 Mutate_Ekind (Equiv_Type, E_Record_Type);
10344
10345 if not Is_Interface (Root_Typ) then
10346 Set_Parent_Subtype (Equiv_Type, Constr_Root);
10347 end if;
10348
10349 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
10350 -- treatment for this type. In particular, even though _parent's type
10351 -- is a controlled type or contains controlled components, we do not
10352 -- want to set Has_Controlled_Component on it to avoid making it gain
10353 -- an unwanted _controller component.
10354
10355 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
10356
10357 -- A class-wide equivalent type does not require initialization unless
10358 -- no expression is present - in which case initialization gets
10359 -- generated as part of the mutably tagged type machinery.
10360
10361 if Present (E) then
10362 Set_Suppress_Initialization (Equiv_Type);
10363 end if;
10364
10365 if not Is_Interface (Root_Typ) and Present (E) then
10366 Append_To (Comp_List,
10367 Make_Component_Declaration (Loc,
10368 Defining_Identifier =>
10369 Make_Defining_Identifier (Loc, Name_uParent),
10370 Component_Definition =>
10371 Make_Component_Definition (Loc,
10372 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
10373
10374 Set_Reverse_Storage_Order
10375 (Equiv_Type, Reverse_Storage_Order (Base_Type (Root_Utyp)));
10376 Set_Reverse_Bit_Order
10377 (Equiv_Type, Reverse_Bit_Order (Base_Type (Root_Utyp)));
10378
10379 else
10380 Append_To (Comp_List,
10381 Make_Component_Declaration (Loc,
10382 Defining_Identifier =>
10383 Make_Defining_Identifier (Loc, Name_uTag),
10384 Component_Definition =>
10385 Make_Component_Definition (Loc,
10386 Subtype_Indication =>
10387 New_Occurrence_Of (RTE (RE_Tag), Loc))));
10388
10389 Set_Is_Tag (Defining_Identifier (Last (Comp_List)));
10390 end if;
10391
10392 Append_To (Comp_List,
10393 Make_Component_Declaration (Loc,
10394 Defining_Identifier => Make_Temporary (Loc, 'C'),
10395 Component_Definition =>
10396 Make_Component_Definition (Loc,
10397 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
10398
10399 Append_To (List_Def,
10400 Make_Full_Type_Declaration (Loc,
10401 Defining_Identifier => Equiv_Type,
10402 Type_Definition =>
10403 Make_Record_Definition (Loc,
10404 Component_List =>
10405 Make_Component_List (Loc,
10406 Component_Items => Comp_List,
10407 Variant_Part => Empty))));
10408
10409 return Equiv_Type;
10410 end Make_CW_Equivalent_Type;
10411
10412 -------------------------
10413 -- Make_Invariant_Call --
10414 -------------------------
10415
10416 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
10417 Loc : constant Source_Ptr := Sloc (Expr);
10418 Typ : constant Entity_Id := Base_Type (Etype (Expr));
10419 pragma Assert (Has_Invariants (Typ));
10420 Proc_Id : constant Entity_Id := Invariant_Procedure (Typ);
10421 pragma Assert (Present (Proc_Id));
10422 Inv_Typ : constant Entity_Id :=
10423 Base_Type (Etype (First_Formal (Proc_Id)));
10424
10425 Arg : Node_Id;
10426
10427 begin
10428 -- The invariant procedure has a null body if assertions are disabled or
10429 -- Assertion_Policy Ignore is in effect. In that case, generate a null
10430 -- statement instead of a call to the invariant procedure.
10431
10432 if Has_Null_Body (Proc_Id) then
10433 return Make_Null_Statement (Loc);
10434
10435 else
10436 -- As done elsewhere, for example in Build_Initialization_Call, we
10437 -- may need to bridge the gap between views of the type.
10438
10439 if Inv_Typ /= Typ then
10440 Arg := OK_Convert_To (Inv_Typ, Expr);
10441 else
10442 Arg := Relocate_Node (Expr);
10443 end if;
10444
10445 return
10446 Make_Procedure_Call_Statement (Loc,
10447 Name => New_Occurrence_Of (Proc_Id, Loc),
10448 Parameter_Associations => New_List (Arg));
10449 end if;
10450 end Make_Invariant_Call;
10451
10452 ------------------------
10453 -- Make_Literal_Range --
10454 ------------------------
10455
10456 function Make_Literal_Range
10457 (Loc : Source_Ptr;
10458 Literal_Typ : Entity_Id) return Node_Id
10459 is
10460 Lo : constant Node_Id :=
10461 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
10462 Index : constant Entity_Id := Etype (Lo);
10463 Length_Expr : constant Node_Id :=
10464 Make_Op_Subtract (Loc,
10465 Left_Opnd =>
10466 Make_Integer_Literal (Loc,
10467 Intval => String_Literal_Length (Literal_Typ)),
10468 Right_Opnd => Make_Integer_Literal (Loc, 1));
10469
10470 Hi : Node_Id;
10471
10472 begin
10473 Set_Analyzed (Lo, False);
10474
10475 if Is_Integer_Type (Index) then
10476 Hi :=
10477 Make_Op_Add (Loc,
10478 Left_Opnd => New_Copy_Tree (Lo),
10479 Right_Opnd => Length_Expr);
10480 else
10481 Hi :=
10482 Make_Attribute_Reference (Loc,
10483 Attribute_Name => Name_Val,
10484 Prefix => New_Occurrence_Of (Index, Loc),
10485 Expressions => New_List (
10486 Make_Op_Add (Loc,
10487 Left_Opnd =>
10488 Make_Attribute_Reference (Loc,
10489 Attribute_Name => Name_Pos,
10490 Prefix => New_Occurrence_Of (Index, Loc),
10491 Expressions => New_List (New_Copy_Tree (Lo))),
10492 Right_Opnd => Length_Expr)));
10493 end if;
10494
10495 return
10496 Make_Range (Loc,
10497 Low_Bound => Lo,
10498 High_Bound => Hi);
10499 end Make_Literal_Range;
10500
10501 --------------------------
10502 -- Make_Non_Empty_Check --
10503 --------------------------
10504
10505 function Make_Non_Empty_Check
10506 (Loc : Source_Ptr;
10507 N : Node_Id) return Node_Id
10508 is
10509 begin
10510 return
10511 Make_Op_Ne (Loc,
10512 Left_Opnd =>
10513 Make_Attribute_Reference (Loc,
10514 Attribute_Name => Name_Length,
10515 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
10516 Right_Opnd =>
10517 Make_Integer_Literal (Loc, 0));
10518 end Make_Non_Empty_Check;
10519
10520 -------------------------
10521 -- Make_Predicate_Call --
10522 -------------------------
10523
10524 -- WARNING: This routine manages Ghost regions. Return statements must be
10525 -- replaced by gotos which jump to the end of the routine and restore the
10526 -- Ghost mode.
10527
10528 function Make_Predicate_Call
10529 (Typ : Entity_Id;
10530 Expr : Node_Id;
10531 Static_Mem : Boolean := False;
10532 Dynamic_Mem : Node_Id := Empty) return Node_Id
10533 is
10534 Loc : constant Source_Ptr := Sloc (Expr);
10535
10536 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10537 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10538 -- Save the Ghost-related attributes to restore on exit
10539
10540 Call : Node_Id;
10541 Func_Id : Entity_Id;
10542 Param_Assocs : List_Id;
10543 begin
10544 Func_Id := Predicate_Function (Typ);
10545 pragma Assert (Present (Func_Id));
10546
10547 -- The related type may be subject to pragma Ghost. Set the mode now to
10548 -- ensure that the call is properly marked as Ghost.
10549
10550 Set_Ghost_Mode (Typ);
10551
10552 -- Case of calling normal predicate function
10553
10554 -- If the type is tagged, the expression may be class-wide, in which
10555 -- case it has to be converted to its root type, given that the
10556 -- generated predicate function is not dispatching. The conversion is
10557 -- type-safe and does not need validation, which matters when private
10558 -- extensions are involved.
10559
10560 if Is_Tagged_Type (Typ) then
10561 Param_Assocs := New_List (OK_Convert_To (Typ, Relocate_Node (Expr)));
10562 else
10563 Param_Assocs := New_List (Relocate_Node (Expr));
10564 end if;
10565
10566 if Predicate_Function_Needs_Membership_Parameter (Typ) then
10567 -- Pass in parameter indicating whether this call is for a
10568 -- membership test.
10569 Append ((if Present (Dynamic_Mem)
10570 then Dynamic_Mem
10571 else New_Occurrence_Of
10572 (Boolean_Literals (Static_Mem), Loc)),
10573 Param_Assocs);
10574 end if;
10575
10576 Call :=
10577 Make_Function_Call (Loc,
10578 Name => New_Occurrence_Of (Func_Id, Loc),
10579 Parameter_Associations => Param_Assocs);
10580
10581 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10582
10583 return Call;
10584 end Make_Predicate_Call;
10585
10586 --------------------------
10587 -- Make_Predicate_Check --
10588 --------------------------
10589
10590 function Make_Predicate_Check
10591 (Typ : Entity_Id;
10592 Expr : Node_Id) return Node_Id
10593 is
10594 Loc : constant Source_Ptr := Sloc (Expr);
10595
10596 -- Local variables
10597
10598 Args : List_Id;
10599 Nam : Name_Id;
10600
10601 -- Start of processing for Make_Predicate_Check
10602
10603 begin
10604 -- If predicate checks are suppressed, then return a null statement. For
10605 -- this call, we check only the scope setting. If the caller wants to
10606 -- check a specific entity's setting, they must do it manually.
10607
10608 if Predicate_Checks_Suppressed (Empty) then
10609 return Make_Null_Statement (Loc);
10610 end if;
10611
10612 -- Do not generate a check within stream functions and the like.
10613
10614 if not Predicate_Check_In_Scope (Expr) then
10615 return Make_Null_Statement (Loc);
10616 end if;
10617
10618 -- Compute proper name to use, we need to get this right so that the
10619 -- right set of check policies apply to the Check pragma we are making.
10620 -- The presence or not of a Ghost_Predicate does not influence the
10621 -- choice of the applicable check policy.
10622
10623 if Has_Dynamic_Predicate_Aspect (Typ) then
10624 Nam := Name_Dynamic_Predicate;
10625 elsif Has_Static_Predicate_Aspect (Typ) then
10626 Nam := Name_Static_Predicate;
10627 else
10628 Nam := Name_Predicate;
10629 end if;
10630
10631 Args := New_List (
10632 Make_Pragma_Argument_Association (Loc,
10633 Expression => Make_Identifier (Loc, Nam)),
10634 Make_Pragma_Argument_Association (Loc,
10635 Expression => Make_Predicate_Call (Typ, Expr)));
10636
10637 -- If the subtype is subject to pragma Predicate_Failure, add the
10638 -- failure expression as an additional parameter.
10639
10640 return
10641 Make_Pragma (Loc,
10642 Chars => Name_Check,
10643 Pragma_Argument_Associations => Args);
10644 end Make_Predicate_Check;
10645
10646 ----------------------------
10647 -- Make_Subtype_From_Expr --
10648 ----------------------------
10649
10650 -- 1. If Expr is an unconstrained array expression, creates
10651 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
10652
10653 -- 2. If Expr is a unconstrained discriminated type expression, creates
10654 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
10655
10656 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
10657
10658 function Make_Subtype_From_Expr
10659 (E : Node_Id;
10660 Unc_Typ : Entity_Id;
10661 Related_Id : Entity_Id := Empty) return Node_Id
10662 is
10663 List_Constr : constant List_Id := New_List;
10664 Loc : constant Source_Ptr := Sloc (E);
10665 D : Entity_Id;
10666 Full_Exp : Node_Id;
10667 Full_Subtyp : Entity_Id;
10668 High_Bound : Entity_Id;
10669 Index_Typ : Entity_Id;
10670 Low_Bound : Entity_Id;
10671 Priv_Subtyp : Entity_Id;
10672 Utyp : Entity_Id;
10673
10674 begin
10675 if Is_Private_Type (Unc_Typ)
10676 and then Has_Unknown_Discriminants (Unc_Typ)
10677 then
10678 -- The caller requests a unique external name for both the private
10679 -- and the full subtype.
10680
10681 if Present (Related_Id) then
10682 Full_Subtyp :=
10683 Make_Defining_Identifier (Loc,
10684 Chars => New_External_Name (Chars (Related_Id), 'C'));
10685 Priv_Subtyp :=
10686 Make_Defining_Identifier (Loc,
10687 Chars => New_External_Name (Chars (Related_Id), 'P'));
10688
10689 else
10690 Full_Subtyp := Make_Temporary (Loc, 'C');
10691 Priv_Subtyp := Make_Temporary (Loc, 'P');
10692 end if;
10693
10694 -- Prepare the subtype completion. Use the base type to find the
10695 -- underlying type because the type may be a generic actual or an
10696 -- explicit subtype.
10697
10698 Utyp := Underlying_Type (Base_Type (Unc_Typ));
10699
10700 Full_Exp :=
10701 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
10702 Set_Parent (Full_Exp, Parent (E));
10703
10704 Insert_Action (E,
10705 Make_Subtype_Declaration (Loc,
10706 Defining_Identifier => Full_Subtyp,
10707 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
10708
10709 -- Define the dummy private subtype
10710
10711 Mutate_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
10712 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
10713 Set_Scope (Priv_Subtyp, Full_Subtyp);
10714 Set_Is_Constrained (Priv_Subtyp);
10715 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
10716 Set_Is_Itype (Priv_Subtyp);
10717 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
10718
10719 Set_Direct_Primitive_Operations
10720 (Priv_Subtyp, Direct_Primitive_Operations (Unc_Typ));
10721
10722 Set_Full_View (Priv_Subtyp, Full_Subtyp);
10723
10724 return New_Occurrence_Of (Priv_Subtyp, Loc);
10725
10726 elsif Is_Array_Type (Unc_Typ) then
10727 Index_Typ := First_Index (Unc_Typ);
10728 for J in 1 .. Number_Dimensions (Unc_Typ) loop
10729
10730 -- Capture the bounds of each index constraint in case the context
10731 -- is an object declaration of an unconstrained type initialized
10732 -- by a function call:
10733
10734 -- Obj : Unconstr_Typ := Func_Call;
10735
10736 -- This scenario requires secondary scope management and the index
10737 -- constraint cannot depend on the temporary used to capture the
10738 -- result of the function call.
10739
10740 -- SS_Mark;
10741 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
10742 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
10743 -- Obj : S := Temp.all;
10744 -- SS_Release; -- Temp is gone at this point, bounds of S are
10745 -- -- non existent.
10746
10747 -- Generate:
10748 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
10749
10750 Low_Bound := Make_Temporary (Loc, 'B');
10751 Insert_Action (E,
10752 Make_Object_Declaration (Loc,
10753 Defining_Identifier => Low_Bound,
10754 Object_Definition =>
10755 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10756 Constant_Present => True,
10757 Expression =>
10758 Make_Attribute_Reference (Loc,
10759 Prefix => Duplicate_Subexpr_No_Checks (E),
10760 Attribute_Name => Name_First,
10761 Expressions => New_List (
10762 Make_Integer_Literal (Loc, J)))));
10763
10764 -- Generate:
10765 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
10766
10767 High_Bound := Make_Temporary (Loc, 'B');
10768 Insert_Action (E,
10769 Make_Object_Declaration (Loc,
10770 Defining_Identifier => High_Bound,
10771 Object_Definition =>
10772 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10773 Constant_Present => True,
10774 Expression =>
10775 Make_Attribute_Reference (Loc,
10776 Prefix => Duplicate_Subexpr_No_Checks (E),
10777 Attribute_Name => Name_Last,
10778 Expressions => New_List (
10779 Make_Integer_Literal (Loc, J)))));
10780
10781 Append_To (List_Constr,
10782 Make_Range (Loc,
10783 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
10784 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
10785
10786 Next_Index (Index_Typ);
10787 end loop;
10788
10789 elsif Is_Class_Wide_Type (Unc_Typ) then
10790 declare
10791 CW_Subtype : constant Entity_Id :=
10792 New_Class_Wide_Subtype (Unc_Typ, E);
10793 Equiv_Def : List_Id;
10794
10795 begin
10796 -- A class-wide equivalent type is not needed on VM targets
10797 -- because the VM back-ends handle the class-wide object
10798 -- initialization itself (and doesn't need or want the
10799 -- additional intermediate type to handle the assignment).
10800
10801 if Expander_Active and then Tagged_Type_Expansion then
10802
10803 -- If this is the class-wide type of a completion that is a
10804 -- record subtype, set the type of the class-wide type to be
10805 -- the full base type, for use in the expanded code for the
10806 -- equivalent type. Should this be done earlier when the
10807 -- completion is analyzed ???
10808
10809 if Is_Private_Type (Etype (Unc_Typ))
10810 and then
10811 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
10812 then
10813 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
10814 end if;
10815
10816 Set_Equivalent_Type
10817 (CW_Subtype, Make_CW_Equivalent_Type (Unc_Typ, E, Equiv_Def));
10818
10819 -- Suppress all checks during the analysis of the expanded
10820 -- code to avoid the generation of spurious warnings under
10821 -- ZFP run-time.
10822
10823 Insert_Actions
10824 (E, Equiv_Def, Suppress => All_Checks);
10825 end if;
10826
10827 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
10828
10829 return New_Occurrence_Of (CW_Subtype, Loc);
10830 end;
10831
10832 -- Indefinite record type with discriminants
10833
10834 else
10835 D := First_Discriminant (Unc_Typ);
10836 while Present (D) loop
10837 Append_To (List_Constr,
10838 Make_Selected_Component (Loc,
10839 Prefix => Duplicate_Subexpr_No_Checks (E),
10840 Selector_Name => New_Occurrence_Of (D, Loc)));
10841
10842 Next_Discriminant (D);
10843 end loop;
10844 end if;
10845
10846 return
10847 Make_Subtype_Indication (Loc,
10848 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
10849 Constraint =>
10850 Make_Index_Or_Discriminant_Constraint (Loc,
10851 Constraints => List_Constr));
10852 end Make_Subtype_From_Expr;
10853
10854 -----------------------------------
10855 -- Make_Tag_Assignment_From_Type --
10856 -----------------------------------
10857
10858 function Make_Tag_Assignment_From_Type
10859 (Loc : Source_Ptr;
10860 Target : Node_Id;
10861 Typ : Entity_Id) return Node_Id
10862 is
10863 Nam : constant Node_Id :=
10864 Make_Selected_Component (Loc,
10865 Prefix => Target,
10866 Selector_Name =>
10867 New_Occurrence_Of (First_Tag_Component (Typ), Loc));
10868
10869 begin
10870 Set_Assignment_OK (Nam);
10871
10872 return
10873 Make_Assignment_Statement (Loc,
10874 Name => Nam,
10875 Expression =>
10876 Unchecked_Convert_To (RTE (RE_Tag),
10877 New_Occurrence_Of
10878 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)));
10879 end Make_Tag_Assignment_From_Type;
10880
10881 -----------------------------
10882 -- Make_Variant_Comparison --
10883 -----------------------------
10884
10885 function Make_Variant_Comparison
10886 (Loc : Source_Ptr;
10887 Typ : Entity_Id;
10888 Mode : Name_Id;
10889 Curr_Val : Node_Id;
10890 Old_Val : Node_Id) return Node_Id
10891 is
10892 function Big_Integer_Lt return Entity_Id;
10893 -- Returns the entity of the predefined "<" function from
10894 -- Ada.Numerics.Big_Numbers.Big_Integers.
10895
10896 --------------------
10897 -- Big_Integer_Lt --
10898 --------------------
10899
10900 function Big_Integer_Lt return Entity_Id is
10901 Big_Integers : constant Entity_Id :=
10902 RTU_Entity (Ada_Numerics_Big_Numbers_Big_Integers);
10903
10904 E : Entity_Id := First_Entity (Big_Integers);
10905
10906 begin
10907 while Present (E) loop
10908 if Chars (E) = Name_Op_Lt then
10909 return E;
10910 end if;
10911 Next_Entity (E);
10912 end loop;
10913
10914 raise Program_Error;
10915 end Big_Integer_Lt;
10916
10917 -- Start of processing for Make_Variant_Comparison
10918
10919 begin
10920 if Mode = Name_Increases then
10921 return Make_Op_Gt (Loc, Curr_Val, Old_Val);
10922
10923 else pragma Assert (Mode = Name_Decreases);
10924
10925 -- For discrete expressions use the "<" operator
10926
10927 if Is_Discrete_Type (Typ) then
10928 return Make_Op_Lt (Loc, Curr_Val, Old_Val);
10929
10930 -- For Big_Integer expressions use the "<" function, because the
10931 -- operator on private type might not be visible and won't be
10932 -- resolved.
10933
10934 else pragma Assert (Is_RTE (Base_Type (Typ), RE_Big_Integer)
10935 or else
10936 Is_RTE (Base_Type (Typ), RO_GH_Big_Integer)
10937 or else
10938 Is_RTE (Base_Type (Typ), RO_SP_Big_Integer));
10939 return
10940 Make_Function_Call (Loc,
10941 Name =>
10942 New_Occurrence_Of (Big_Integer_Lt, Loc),
10943 Parameter_Associations =>
10944 New_List (Curr_Val, Old_Val));
10945 end if;
10946 end if;
10947 end Make_Variant_Comparison;
10948
10949 -----------------
10950 -- Map_Formals --
10951 -----------------
10952
10953 procedure Map_Formals
10954 (Parent_Subp : Entity_Id;
10955 Derived_Subp : Entity_Id;
10956 Force_Update : Boolean := False)
10957 is
10958 Par_Formal : Entity_Id := First_Formal (Parent_Subp);
10959 Subp_Formal : Entity_Id := First_Formal (Derived_Subp);
10960
10961 begin
10962 if Force_Update then
10963 Type_Map.Set (Parent_Subp, Derived_Subp);
10964 end if;
10965
10966 -- At this stage either we are under regular processing and the caller
10967 -- has previously ensured that these primitives are already mapped (by
10968 -- means of calling previously to Update_Primitives_Mapping), or we are
10969 -- processing a late-overriding primitive and Force_Update updated above
10970 -- the mapping of these primitives.
10971
10972 while Present (Par_Formal) and then Present (Subp_Formal) loop
10973 Type_Map.Set (Par_Formal, Subp_Formal);
10974 Next_Formal (Par_Formal);
10975 Next_Formal (Subp_Formal);
10976 end loop;
10977 end Map_Formals;
10978
10979 ---------------
10980 -- Map_Types --
10981 ---------------
10982
10983 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
10984
10985 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
10986 -- avoid deep indentation of code.
10987
10988 -- NOTE: Routines which deal with discriminant mapping operate on the
10989 -- [underlying/record] full view of various types because those views
10990 -- contain all discriminants and stored constraints.
10991
10992 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
10993 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
10994 -- overriding chain starting from Prim whose dispatching type is parent
10995 -- type Par_Typ and add a mapping between the result and primitive Prim.
10996
10997 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
10998 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
10999 -- the inheritance or overriding chain of subprogram Subp. Return Empty
11000 -- if no such primitive is available.
11001
11002 function Build_Chain
11003 (Par_Typ : Entity_Id;
11004 Deriv_Typ : Entity_Id) return Elist_Id;
11005 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
11006 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
11007 -- list has the form:
11008 --
11009 -- head tail
11010 -- v v
11011 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
11012 --
11013 -- Note that Par_Typ is not part of the resulting derivation chain
11014
11015 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
11016 -- Return the view of type Typ which could potentially contains either
11017 -- the discriminants or stored constraints of the type.
11018
11019 function Find_Discriminant_Value
11020 (Discr : Entity_Id;
11021 Par_Typ : Entity_Id;
11022 Deriv_Typ : Entity_Id;
11023 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
11024 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
11025 -- in the derivation chain starting from parent type Par_Typ leading to
11026 -- derived type Deriv_Typ. The returned value is one of the following:
11027 --
11028 -- * An entity which is either a discriminant or a nondiscriminant
11029 -- name, and renames/constraints Discr.
11030 --
11031 -- * An expression which constraints Discr
11032 --
11033 -- Typ_Elmt is an element of the derivation chain created by routine
11034 -- Build_Chain and denotes the current ancestor being examined.
11035
11036 procedure Map_Discriminants
11037 (Par_Typ : Entity_Id;
11038 Deriv_Typ : Entity_Id);
11039 -- Map each discriminant of type Par_Typ to a meaningful constraint
11040 -- from the point of view of type Deriv_Typ.
11041
11042 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
11043 -- Map each primitive of type Par_Typ to a corresponding primitive of
11044 -- type Deriv_Typ.
11045
11046 -------------------
11047 -- Add_Primitive --
11048 -------------------
11049
11050 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
11051 Par_Prim : Entity_Id;
11052
11053 begin
11054 -- Inspect the inheritance chain through the Alias attribute and the
11055 -- overriding chain through the Overridden_Operation looking for an
11056 -- ancestor primitive with the appropriate dispatching type.
11057
11058 Par_Prim := Prim;
11059 while Present (Par_Prim) loop
11060 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
11061 Par_Prim := Ancestor_Primitive (Par_Prim);
11062 end loop;
11063
11064 -- Create a mapping of the form:
11065
11066 -- parent type primitive -> derived type primitive
11067
11068 if Present (Par_Prim) then
11069 Type_Map.Set (Par_Prim, Prim);
11070 end if;
11071 end Add_Primitive;
11072
11073 ------------------------
11074 -- Ancestor_Primitive --
11075 ------------------------
11076
11077 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
11078 Inher_Prim : constant Entity_Id := Alias (Subp);
11079 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
11080
11081 begin
11082 -- The current subprogram overrides an ancestor primitive
11083
11084 if Present (Over_Prim) then
11085 return Over_Prim;
11086
11087 -- The current subprogram is an internally generated alias of an
11088 -- inherited ancestor primitive.
11089
11090 elsif Present (Inher_Prim) then
11091 -- It is possible that an internally generated alias could be
11092 -- set to a subprogram which overrides the same aliased primitive,
11093 -- so return Empty in this case.
11094
11095 if Ancestor_Primitive (Inher_Prim) = Subp then
11096 return Empty;
11097 end if;
11098
11099 return Inher_Prim;
11100
11101 -- Otherwise the current subprogram is the root of the inheritance or
11102 -- overriding chain.
11103
11104 else
11105 return Empty;
11106 end if;
11107 end Ancestor_Primitive;
11108
11109 -----------------
11110 -- Build_Chain --
11111 -----------------
11112
11113 function Build_Chain
11114 (Par_Typ : Entity_Id;
11115 Deriv_Typ : Entity_Id) return Elist_Id
11116 is
11117 Anc_Typ : Entity_Id;
11118 Chain : Elist_Id;
11119 Curr_Typ : Entity_Id;
11120
11121 begin
11122 Chain := New_Elmt_List;
11123
11124 -- Add the derived type to the derivation chain
11125
11126 Prepend_Elmt (Deriv_Typ, Chain);
11127
11128 -- Examine all ancestors starting from the derived type climbing
11129 -- towards parent type Par_Typ.
11130
11131 Curr_Typ := Deriv_Typ;
11132 loop
11133 -- Handle the case where the current type is a record which
11134 -- derives from a subtype.
11135
11136 -- subtype Sub_Typ is Par_Typ ...
11137 -- type Deriv_Typ is Sub_Typ ...
11138
11139 if Ekind (Curr_Typ) = E_Record_Type
11140 and then Present (Parent_Subtype (Curr_Typ))
11141 then
11142 Anc_Typ := Parent_Subtype (Curr_Typ);
11143
11144 -- Handle the case where the current type is a record subtype of
11145 -- another subtype.
11146
11147 -- subtype Sub_Typ1 is Par_Typ ...
11148 -- subtype Sub_Typ2 is Sub_Typ1 ...
11149
11150 elsif Ekind (Curr_Typ) = E_Record_Subtype
11151 and then Present (Cloned_Subtype (Curr_Typ))
11152 then
11153 Anc_Typ := Cloned_Subtype (Curr_Typ);
11154
11155 -- Otherwise use the direct parent type
11156
11157 else
11158 Anc_Typ := Etype (Curr_Typ);
11159 end if;
11160
11161 -- Use the first subtype when dealing with itypes
11162
11163 if Is_Itype (Anc_Typ) then
11164 Anc_Typ := First_Subtype (Anc_Typ);
11165 end if;
11166
11167 -- Work with the view which contains the discriminants and stored
11168 -- constraints.
11169
11170 Anc_Typ := Discriminated_View (Anc_Typ);
11171
11172 -- Stop the climb when either the parent type has been reached or
11173 -- there are no more ancestors left to examine.
11174
11175 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
11176
11177 Prepend_Unique_Elmt (Anc_Typ, Chain);
11178 Curr_Typ := Anc_Typ;
11179 end loop;
11180
11181 return Chain;
11182 end Build_Chain;
11183
11184 ------------------------
11185 -- Discriminated_View --
11186 ------------------------
11187
11188 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
11189 T : Entity_Id;
11190
11191 begin
11192 T := Typ;
11193
11194 -- Use the [underlying] full view when dealing with private types
11195 -- because the view contains all inherited discriminants or stored
11196 -- constraints.
11197
11198 if Is_Private_Type (T) then
11199 if Present (Underlying_Full_View (T)) then
11200 T := Underlying_Full_View (T);
11201
11202 elsif Present (Full_View (T)) then
11203 T := Full_View (T);
11204 end if;
11205 end if;
11206
11207 -- Use the underlying record view when the type is an extenstion of
11208 -- a parent type with unknown discriminants because the view contains
11209 -- all inherited discriminants or stored constraints.
11210
11211 if Ekind (T) = E_Record_Type
11212 and then Present (Underlying_Record_View (T))
11213 then
11214 T := Underlying_Record_View (T);
11215 end if;
11216
11217 return T;
11218 end Discriminated_View;
11219
11220 -----------------------------
11221 -- Find_Discriminant_Value --
11222 -----------------------------
11223
11224 function Find_Discriminant_Value
11225 (Discr : Entity_Id;
11226 Par_Typ : Entity_Id;
11227 Deriv_Typ : Entity_Id;
11228 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
11229 is
11230 Discr_Pos : constant Uint := Discriminant_Number (Discr);
11231 Typ : constant Entity_Id := Node (Typ_Elmt);
11232
11233 function Find_Constraint_Value
11234 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
11235 -- Given constraint Constr, find what it denotes. This is either:
11236 --
11237 -- * An entity which is either a discriminant or a name
11238 --
11239 -- * An expression
11240
11241 ---------------------------
11242 -- Find_Constraint_Value --
11243 ---------------------------
11244
11245 function Find_Constraint_Value
11246 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
11247 is
11248 begin
11249 if Nkind (Constr) in N_Entity then
11250
11251 -- The constraint denotes a discriminant of the curren type
11252 -- which renames the ancestor discriminant:
11253
11254 -- vv
11255 -- type Typ (D1 : ...; DN : ...) is
11256 -- new Anc (Discr => D1) with ...
11257 -- ^^
11258
11259 if Ekind (Constr) = E_Discriminant then
11260
11261 -- The discriminant belongs to derived type Deriv_Typ. This
11262 -- is the final value for the ancestor discriminant as the
11263 -- derivations chain has been fully exhausted.
11264
11265 if Typ = Deriv_Typ then
11266 return Constr;
11267
11268 -- Otherwise the discriminant may be renamed or constrained
11269 -- at a lower level. Continue looking down the derivation
11270 -- chain.
11271
11272 else
11273 return
11274 Find_Discriminant_Value
11275 (Discr => Constr,
11276 Par_Typ => Par_Typ,
11277 Deriv_Typ => Deriv_Typ,
11278 Typ_Elmt => Next_Elmt (Typ_Elmt));
11279 end if;
11280
11281 -- Otherwise the constraint denotes a reference to some name
11282 -- which results in a Stored discriminant:
11283
11284 -- vvvv
11285 -- Name : ...;
11286 -- type Typ (D1 : ...; DN : ...) is
11287 -- new Anc (Discr => Name) with ...
11288 -- ^^^^
11289
11290 -- Return the name as this is the proper constraint of the
11291 -- discriminant.
11292
11293 else
11294 return Constr;
11295 end if;
11296
11297 -- The constraint denotes a reference to a name
11298
11299 elsif Is_Entity_Name (Constr) then
11300 return Find_Constraint_Value (Entity (Constr));
11301
11302 -- Otherwise the current constraint is an expression which yields
11303 -- a Stored discriminant:
11304
11305 -- type Typ (D1 : ...; DN : ...) is
11306 -- new Anc (Discr => <expression>) with ...
11307 -- ^^^^^^^^^^
11308
11309 -- Return the expression as this is the proper constraint of the
11310 -- discriminant.
11311
11312 else
11313 return Constr;
11314 end if;
11315 end Find_Constraint_Value;
11316
11317 -- Local variables
11318
11319 Constrs : constant Elist_Id := Stored_Constraint (Typ);
11320
11321 Constr_Elmt : Elmt_Id;
11322 Pos : Uint;
11323 Typ_Discr : Entity_Id;
11324
11325 -- Start of processing for Find_Discriminant_Value
11326
11327 begin
11328 -- The algorithm for finding the value of a discriminant works as
11329 -- follows. First, it recreates the derivation chain from Par_Typ
11330 -- to Deriv_Typ as a list:
11331
11332 -- Par_Typ (shown for completeness)
11333 -- v
11334 -- Ancestor_N <-- head of chain
11335 -- v
11336 -- Ancestor_1
11337 -- v
11338 -- Deriv_Typ <-- tail of chain
11339
11340 -- The algorithm then traces the fate of a parent discriminant down
11341 -- the derivation chain. At each derivation level, the discriminant
11342 -- may be either inherited or constrained.
11343
11344 -- 1) Discriminant is inherited: there are two cases, depending on
11345 -- which type is inheriting.
11346
11347 -- 1.1) Deriv_Typ is inheriting:
11348
11349 -- type Ancestor (D_1 : ...) is tagged ...
11350 -- type Deriv_Typ is new Ancestor ...
11351
11352 -- In this case the inherited discriminant is the final value of
11353 -- the parent discriminant because the end of the derivation chain
11354 -- has been reached.
11355
11356 -- 1.2) Some other type is inheriting:
11357
11358 -- type Ancestor_1 (D_1 : ...) is tagged ...
11359 -- type Ancestor_2 is new Ancestor_1 ...
11360
11361 -- In this case the algorithm continues to trace the fate of the
11362 -- inherited discriminant down the derivation chain because it may
11363 -- be further inherited or constrained.
11364
11365 -- 2) Discriminant is constrained: there are three cases, depending
11366 -- on what the constraint is.
11367
11368 -- 2.1) The constraint is another discriminant (aka renaming):
11369
11370 -- type Ancestor_1 (D_1 : ...) is tagged ...
11371 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
11372
11373 -- In this case the constraining discriminant becomes the one to
11374 -- track down the derivation chain. The algorithm already knows
11375 -- that D_2 constrains D_1, therefore if the algorithm finds the
11376 -- value of D_2, then this would also be the value for D_1.
11377
11378 -- 2.2) The constraint is a name (aka Stored):
11379
11380 -- Name : ...
11381 -- type Ancestor_1 (D_1 : ...) is tagged ...
11382 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
11383
11384 -- In this case the name is the final value of D_1 because the
11385 -- discriminant cannot be further constrained.
11386
11387 -- 2.3) The constraint is an expression (aka Stored):
11388
11389 -- type Ancestor_1 (D_1 : ...) is tagged ...
11390 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
11391
11392 -- Similar to 2.2, the expression is the final value of D_1
11393
11394 Pos := Uint_1;
11395
11396 -- When a derived type constrains its parent type, all constaints
11397 -- appear in the Stored_Constraint list. Examine the list looking
11398 -- for a positional match.
11399
11400 if Present (Constrs) then
11401 Constr_Elmt := First_Elmt (Constrs);
11402 while Present (Constr_Elmt) loop
11403
11404 -- The position of the current constraint matches that of the
11405 -- ancestor discriminant.
11406
11407 if Pos = Discr_Pos then
11408 return Find_Constraint_Value (Node (Constr_Elmt));
11409 end if;
11410
11411 Next_Elmt (Constr_Elmt);
11412 Pos := Pos + 1;
11413 end loop;
11414
11415 -- Otherwise the derived type does not constraint its parent type in
11416 -- which case it inherits the parent discriminants.
11417
11418 else
11419 Typ_Discr := First_Discriminant (Typ);
11420 while Present (Typ_Discr) loop
11421
11422 -- The position of the current discriminant matches that of the
11423 -- ancestor discriminant.
11424
11425 if Pos = Discr_Pos then
11426 return Find_Constraint_Value (Typ_Discr);
11427 end if;
11428
11429 Next_Discriminant (Typ_Discr);
11430 Pos := Pos + 1;
11431 end loop;
11432 end if;
11433
11434 -- A discriminant must always have a corresponding value. This is
11435 -- either another discriminant, a name, or an expression. If this
11436 -- point is reached, them most likely the derivation chain employs
11437 -- the wrong views of types.
11438
11439 pragma Assert (False);
11440
11441 return Empty;
11442 end Find_Discriminant_Value;
11443
11444 -----------------------
11445 -- Map_Discriminants --
11446 -----------------------
11447
11448 procedure Map_Discriminants
11449 (Par_Typ : Entity_Id;
11450 Deriv_Typ : Entity_Id)
11451 is
11452 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
11453
11454 Discr : Entity_Id;
11455 Discr_Val : Node_Or_Entity_Id;
11456
11457 begin
11458 -- Examine each discriminant of parent type Par_Typ and find a
11459 -- suitable value for it from the point of view of derived type
11460 -- Deriv_Typ.
11461
11462 if Has_Discriminants (Par_Typ) then
11463 Discr := First_Discriminant (Par_Typ);
11464 while Present (Discr) loop
11465 Discr_Val :=
11466 Find_Discriminant_Value
11467 (Discr => Discr,
11468 Par_Typ => Par_Typ,
11469 Deriv_Typ => Deriv_Typ,
11470 Typ_Elmt => First_Elmt (Deriv_Chain));
11471
11472 -- Create a mapping of the form:
11473
11474 -- parent type discriminant -> value
11475
11476 Type_Map.Set (Discr, Discr_Val);
11477
11478 Next_Discriminant (Discr);
11479 end loop;
11480 end if;
11481 end Map_Discriminants;
11482
11483 --------------------
11484 -- Map_Primitives --
11485 --------------------
11486
11487 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
11488 Deriv_Prim : Entity_Id;
11489 Par_Prim : Entity_Id;
11490 Par_Prims : Elist_Id;
11491 Prim_Elmt : Elmt_Id;
11492
11493 begin
11494 -- Inspect the primitives of the derived type and determine whether
11495 -- they relate to the primitives of the parent type. If there is a
11496 -- meaningful relation, create a mapping of the form:
11497
11498 -- parent type primitive -> derived type primitive
11499
11500 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
11501 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
11502 while Present (Prim_Elmt) loop
11503 Deriv_Prim := Node (Prim_Elmt);
11504
11505 if Is_Subprogram (Deriv_Prim)
11506 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
11507 then
11508 Add_Primitive (Deriv_Prim, Par_Typ);
11509 end if;
11510
11511 Next_Elmt (Prim_Elmt);
11512 end loop;
11513 end if;
11514
11515 -- If the parent operation is an interface operation, the overriding
11516 -- indicator is not present. Instead, we get from the interface
11517 -- operation the primitive of the current type that implements it.
11518
11519 if Is_Interface (Par_Typ) then
11520 Par_Prims := Collect_Primitive_Operations (Par_Typ);
11521
11522 if Present (Par_Prims) then
11523 Prim_Elmt := First_Elmt (Par_Prims);
11524
11525 while Present (Prim_Elmt) loop
11526 Par_Prim := Node (Prim_Elmt);
11527 Deriv_Prim :=
11528 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
11529
11530 if Present (Deriv_Prim) then
11531 Type_Map.Set (Par_Prim, Deriv_Prim);
11532 end if;
11533
11534 Next_Elmt (Prim_Elmt);
11535 end loop;
11536 end if;
11537 end if;
11538 end Map_Primitives;
11539
11540 -- Start of processing for Map_Types
11541
11542 begin
11543 -- Nothing to do if there are no types to work with
11544
11545 if No (Parent_Type) or else No (Derived_Type) then
11546 return;
11547
11548 -- Nothing to do if the mapping already exists
11549
11550 elsif Type_Map.Get (Parent_Type) = Derived_Type then
11551 return;
11552
11553 -- Nothing to do if both types are not tagged. Note that untagged types
11554 -- do not have primitive operations and their discriminants are already
11555 -- handled by gigi.
11556
11557 elsif not Is_Tagged_Type (Parent_Type)
11558 or else not Is_Tagged_Type (Derived_Type)
11559 then
11560 return;
11561 end if;
11562
11563 -- Create a mapping of the form
11564
11565 -- parent type -> derived type
11566
11567 -- to prevent any subsequent attempts to produce the same relations
11568
11569 Type_Map.Set (Parent_Type, Derived_Type);
11570
11571 -- Create mappings of the form
11572
11573 -- parent type discriminant -> derived type discriminant
11574 -- <or>
11575 -- parent type discriminant -> constraint
11576
11577 -- Note that mapping of discriminants breaks privacy because it needs to
11578 -- work with those views which contains the discriminants and any stored
11579 -- constraints.
11580
11581 Map_Discriminants
11582 (Par_Typ => Discriminated_View (Parent_Type),
11583 Deriv_Typ => Discriminated_View (Derived_Type));
11584
11585 -- Create mappings of the form
11586
11587 -- parent type primitive -> derived type primitive
11588
11589 Map_Primitives
11590 (Par_Typ => Parent_Type,
11591 Deriv_Typ => Derived_Type);
11592 end Map_Types;
11593
11594 ----------------------------
11595 -- Matching_Standard_Type --
11596 ----------------------------
11597
11598 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
11599 pragma Assert (Is_Scalar_Type (Typ));
11600 Siz : constant Uint := Esize (Typ);
11601
11602 begin
11603 -- Floating-point cases
11604
11605 if Is_Floating_Point_Type (Typ) then
11606 if Siz <= Esize (Standard_Short_Float) then
11607 return Standard_Short_Float;
11608 elsif Siz <= Esize (Standard_Float) then
11609 return Standard_Float;
11610 elsif Siz <= Esize (Standard_Long_Float) then
11611 return Standard_Long_Float;
11612 elsif Siz <= Esize (Standard_Long_Long_Float) then
11613 return Standard_Long_Long_Float;
11614 else
11615 raise Program_Error;
11616 end if;
11617
11618 -- Integer cases (includes fixed-point types)
11619
11620 -- Unsigned integer cases (includes normal enumeration types)
11621
11622 else
11623 return Small_Integer_Type_For (Siz, Is_Unsigned_Type (Typ));
11624 end if;
11625 end Matching_Standard_Type;
11626
11627 ---------------------------------------
11628 -- Move_To_Initialization_Statements --
11629 ---------------------------------------
11630
11631 procedure Move_To_Initialization_Statements (Decl, Stop : Node_Id) is
11632 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
11633 Stmts : constant List_Id := New_List;
11634 Stmt : constant Node_Id :=
11635 Make_Compound_Statement (Sloc (Decl), Actions => Stmts);
11636
11637 begin
11638 while Next (Decl) /= Stop loop
11639 Append_To (Stmts, Remove_Next (Decl));
11640 end loop;
11641
11642 pragma Assert (No (Initialization_Statements (Obj_Id)));
11643
11644 Insert_After (Decl, Stmt);
11645 Set_Initialization_Statements (Obj_Id, Stmt);
11646 end Move_To_Initialization_Statements;
11647
11648 --------------------------------
11649 -- Name_Of_Controlled_Prim_Op --
11650 --------------------------------
11651
11652 function Name_Of_Controlled_Prim_Op
11653 (Typ : Entity_Id;
11654 Nam : Name_Id) return Name_Id
11655 is
11656 begin
11657 pragma Assert (Is_Controlled (Typ));
11658
11659 -- The aspect Finalizable may change the name of the primitives when
11660 -- present, but it's a GNAT extension.
11661
11662 if Core_Extensions_Allowed then
11663 declare
11664 Rep : constant Node_Id :=
11665 Get_Rep_Item (Typ, Name_Finalizable, Check_Parents => True);
11666
11667 Assoc : Node_Id;
11668
11669 begin
11670 if Present (Rep) then
11671 Assoc := First (Component_Associations (Expression (Rep)));
11672 while Present (Assoc) loop
11673 if Chars (First (Choices (Assoc))) = Nam then
11674 return Chars (Expression (Assoc));
11675 end if;
11676
11677 Next (Assoc);
11678 end loop;
11679
11680 return No_Name;
11681 end if;
11682 end;
11683 end if;
11684
11685 return Nam;
11686 end Name_Of_Controlled_Prim_Op;
11687
11688 --------------------------------------------
11689 -- Needs_Conditional_Null_Excluding_Check --
11690 --------------------------------------------
11691
11692 function Needs_Conditional_Null_Excluding_Check
11693 (Typ : Entity_Id) return Boolean
11694 is
11695 begin
11696 return
11697 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
11698 end Needs_Conditional_Null_Excluding_Check;
11699
11700 ----------------------------
11701 -- Needs_Constant_Address --
11702 ----------------------------
11703
11704 function Needs_Constant_Address
11705 (Decl : Node_Id;
11706 Typ : Entity_Id) return Boolean
11707 is
11708 begin
11709 -- If we have no initialization of any kind, then we don't need to place
11710 -- any restrictions on the address clause, because the object will be
11711 -- elaborated after the address clause is evaluated. This happens if the
11712 -- declaration has no initial expression, or the type has no implicit
11713 -- initialization, or the object is imported.
11714
11715 -- The same holds for all initialized scalar types and all access types.
11716 -- Packed bit array types of size up to the maximum integer size are
11717 -- represented using a modular type with an initialization (to zero) and
11718 -- can be processed like other initialized scalar types.
11719
11720 -- If the type is controlled, code to attach the object to a
11721 -- finalization chain is generated at the point of declaration, and
11722 -- therefore the elaboration of the object cannot be delayed: the
11723 -- address expression must be a constant.
11724
11725 if No (Expression (Decl))
11726 and then not Needs_Finalization (Typ)
11727 and then
11728 (not Has_Non_Null_Base_Init_Proc (Typ)
11729 or else Is_Imported (Defining_Identifier (Decl)))
11730 then
11731 return False;
11732
11733 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
11734 or else Is_Access_Type (Typ)
11735 or else
11736 (Is_Bit_Packed_Array (Typ)
11737 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
11738 then
11739 return False;
11740
11741 else
11742 -- Otherwise, we require the address clause to be constant because
11743 -- the call to the initialization procedure (or the attach code) has
11744 -- to happen at the point of the declaration.
11745
11746 -- Actually the IP call has been moved to the freeze actions anyway,
11747 -- so maybe we can relax this restriction???
11748
11749 return True;
11750 end if;
11751 end Needs_Constant_Address;
11752
11753 -------------------------------------
11754 -- Needs_Initialization_Statements --
11755 -------------------------------------
11756
11757 function Needs_Initialization_Statements (Decl : Node_Id) return Boolean is
11758 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
11759
11760 begin
11761 -- See the documentation of Initialization_Statements in Einfo
11762
11763 return Comes_From_Source (Decl)
11764 and then (Has_Delayed_Aspects (Obj_Id)
11765 or else Present (Following_Address_Clause (Decl))
11766 or else Init_Or_Norm_Scalars);
11767 end Needs_Initialization_Statements;
11768
11769 ----------------------------
11770 -- New_Class_Wide_Subtype --
11771 ----------------------------
11772
11773 function New_Class_Wide_Subtype
11774 (CW_Typ : Entity_Id;
11775 N : Node_Id) return Entity_Id
11776 is
11777 Res : constant Entity_Id := Create_Itype (E_Void, N);
11778
11779 -- Capture relevant attributes of the class-wide subtype which must be
11780 -- restored after the copy.
11781
11782 Res_Chars : constant Name_Id := Chars (Res);
11783 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
11784 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
11785 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
11786 Res_Scope : constant Entity_Id := Scope (Res);
11787
11788 begin
11789 Copy_Node (CW_Typ, Res);
11790
11791 -- Restore the relevant attributes of the class-wide subtype
11792
11793 Set_Chars (Res, Res_Chars);
11794 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
11795 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
11796 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
11797 Set_Scope (Res, Res_Scope);
11798
11799 -- Decorate the class-wide subtype
11800
11801 Set_Associated_Node_For_Itype (Res, N);
11802 Set_Comes_From_Source (Res, False);
11803 Mutate_Ekind (Res, E_Class_Wide_Subtype);
11804 Set_Etype (Res, Base_Type (CW_Typ));
11805 Set_Freeze_Node (Res, Empty);
11806 Set_Is_Frozen (Res, False);
11807 Set_Is_Itype (Res);
11808 Set_Is_Public (Res, False);
11809 Set_Next_Entity (Res, Empty);
11810 Set_Prev_Entity (Res, Empty);
11811 Set_Sloc (Res, Sloc (N));
11812
11813 Set_Public_Status (Res);
11814
11815 return Res;
11816 end New_Class_Wide_Subtype;
11817
11818 -----------------------------------
11819 -- OK_To_Do_Constant_Replacement --
11820 -----------------------------------
11821
11822 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
11823 ES : constant Entity_Id := Scope (E);
11824 CS : Entity_Id;
11825
11826 begin
11827 -- Do not replace statically allocated objects, because they may be
11828 -- modified outside the current scope.
11829
11830 if Is_Statically_Allocated (E) then
11831 return False;
11832
11833 -- Do not replace aliased or volatile objects, since we don't know what
11834 -- else might change the value.
11835
11836 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
11837 return False;
11838
11839 -- Debug flag -gnatdM disconnects this optimization
11840
11841 elsif Debug_Flag_MM then
11842 return False;
11843
11844 -- Otherwise check scopes
11845
11846 else
11847 CS := Current_Scope;
11848
11849 loop
11850 -- If we are in right scope, replacement is safe
11851
11852 if CS = ES then
11853 return True;
11854
11855 -- Packages do not affect the determination of safety
11856
11857 elsif Ekind (CS) = E_Package then
11858 exit when CS = Standard_Standard;
11859 CS := Scope (CS);
11860
11861 -- Blocks do not affect the determination of safety
11862
11863 elsif Ekind (CS) = E_Block then
11864 CS := Scope (CS);
11865
11866 -- Loops do not affect the determination of safety. Note that we
11867 -- kill all current values on entry to a loop, so we are just
11868 -- talking about processing within a loop here.
11869
11870 elsif Ekind (CS) = E_Loop then
11871 CS := Scope (CS);
11872
11873 -- Otherwise, the reference is dubious, and we cannot be sure that
11874 -- it is safe to do the replacement.
11875
11876 else
11877 exit;
11878 end if;
11879 end loop;
11880
11881 return False;
11882 end if;
11883 end OK_To_Do_Constant_Replacement;
11884
11885 ------------------------------------
11886 -- Possible_Bit_Aligned_Component --
11887 ------------------------------------
11888
11889 function Possible_Bit_Aligned_Component
11890 (N : Node_Id;
11891 For_Slice : Boolean := False) return Boolean
11892 is
11893 begin
11894 -- Do not process an unanalyzed node because it is not yet decorated and
11895 -- most checks performed below will fail.
11896
11897 if not Analyzed (N) then
11898 return False;
11899 end if;
11900
11901 -- There are never alignment issues in CodePeer mode
11902
11903 if CodePeer_Mode then
11904 return False;
11905 end if;
11906
11907 case Nkind (N) is
11908
11909 -- Case of indexed component
11910
11911 when N_Indexed_Component =>
11912 declare
11913 P : constant Node_Id := Prefix (N);
11914 Ptyp : constant Entity_Id := Etype (P);
11915
11916 begin
11917 -- If we know the component size and it is not larger than the
11918 -- maximum integer size, then we are OK. The back end does the
11919 -- assignment of small misaligned objects correctly.
11920
11921 if Known_Static_Component_Size (Ptyp)
11922 and then Component_Size (Ptyp) <= System_Max_Integer_Size
11923 then
11924 return False;
11925
11926 -- Otherwise, we need to test the prefix, to see if we are
11927 -- indexing from a possibly unaligned component.
11928
11929 else
11930 return Possible_Bit_Aligned_Component (P, For_Slice);
11931 end if;
11932 end;
11933
11934 -- Case of selected component
11935
11936 when N_Selected_Component =>
11937 declare
11938 P : constant Node_Id := Prefix (N);
11939 Comp : constant Entity_Id := Entity (Selector_Name (N));
11940
11941 begin
11942 -- This is the crucial test: if the component itself causes
11943 -- trouble, then we can stop and return True.
11944
11945 if Component_May_Be_Bit_Aligned (Comp, For_Slice) then
11946 return True;
11947
11948 -- Otherwise, we need to test the prefix, to see if we are
11949 -- selecting from a possibly unaligned component.
11950
11951 else
11952 return Possible_Bit_Aligned_Component (P, For_Slice);
11953 end if;
11954 end;
11955
11956 -- For a slice, test the prefix, if that is possibly misaligned,
11957 -- then for sure the slice is.
11958
11959 when N_Slice =>
11960 return Possible_Bit_Aligned_Component (Prefix (N), True);
11961
11962 -- For an unchecked conversion, check whether the expression may
11963 -- be bit aligned.
11964
11965 when N_Unchecked_Type_Conversion =>
11966 return Possible_Bit_Aligned_Component (Expression (N), For_Slice);
11967
11968 -- If we have none of the above, it means that we have fallen off the
11969 -- top testing prefixes recursively, and we now have a stand alone
11970 -- object, where we don't have a problem, unless this is a renaming,
11971 -- in which case we need to look into the renamed object.
11972
11973 when others =>
11974 return Is_Entity_Name (N)
11975 and then Is_Object (Entity (N))
11976 and then Present (Renamed_Object (Entity (N)))
11977 and then Possible_Bit_Aligned_Component
11978 (Renamed_Object (Entity (N)), For_Slice);
11979 end case;
11980 end Possible_Bit_Aligned_Component;
11981
11982 -----------------------------------------------
11983 -- Process_Statements_For_Controlled_Objects --
11984 -----------------------------------------------
11985
11986 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
11987 Loc : constant Source_Ptr := Sloc (N);
11988
11989 function Are_Wrapped (L : List_Id) return Boolean;
11990 -- Determine whether list L contains only one statement which is a block
11991
11992 function Wrap_Statements_In_Block
11993 (L : List_Id;
11994 Scop : Entity_Id := Current_Scope) return Node_Id;
11995 -- Given a list of statements L, wrap it in a block statement and return
11996 -- the generated node. Scop is either the current scope or the scope of
11997 -- the context (if applicable).
11998
11999 -----------------
12000 -- Are_Wrapped --
12001 -----------------
12002
12003 function Are_Wrapped (L : List_Id) return Boolean is
12004 Stmt : constant Node_Id := First (L);
12005 begin
12006 return
12007 Present (Stmt)
12008 and then No (Next (Stmt))
12009 and then Nkind (Stmt) = N_Block_Statement;
12010 end Are_Wrapped;
12011
12012 ------------------------------
12013 -- Wrap_Statements_In_Block --
12014 ------------------------------
12015
12016 function Wrap_Statements_In_Block
12017 (L : List_Id;
12018 Scop : Entity_Id := Current_Scope) return Node_Id
12019 is
12020 Block_Id : Entity_Id;
12021 Block_Nod : Node_Id;
12022 Iter_Loop : Entity_Id;
12023
12024 begin
12025 Block_Nod :=
12026 Make_Block_Statement (Loc,
12027 Declarations => No_List,
12028 Handled_Statement_Sequence =>
12029 Make_Handled_Sequence_Of_Statements (Loc,
12030 Statements => L));
12031
12032 -- Create a label for the block in case the block needs to manage the
12033 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
12034
12035 Add_Block_Identifier (Block_Nod, Block_Id, Scop);
12036
12037 -- When wrapping the statements of an iterator loop, check whether
12038 -- the loop requires secondary stack management and if so, propagate
12039 -- the appropriate flags to the block. This ensures that the cursor
12040 -- is properly cleaned up at each iteration of the loop.
12041
12042 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
12043
12044 if Present (Iter_Loop) then
12045 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
12046
12047 -- Secondary stack reclamation is suppressed when the associated
12048 -- iterator loop contains a return statement which uses the stack.
12049
12050 Set_Sec_Stack_Needed_For_Return
12051 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
12052 end if;
12053
12054 return Block_Nod;
12055 end Wrap_Statements_In_Block;
12056
12057 -- Local variables
12058
12059 Block : Node_Id;
12060
12061 -- Start of processing for Process_Statements_For_Controlled_Objects
12062
12063 begin
12064 -- Whenever a non-handled statement list is wrapped in a block, the
12065 -- block must be explicitly analyzed to redecorate all entities in the
12066 -- list and ensure that a finalizer is properly built.
12067
12068 case Nkind (N) is
12069 when N_Conditional_Entry_Call
12070 | N_Elsif_Part
12071 | N_If_Statement
12072 | N_Selective_Accept
12073 =>
12074 -- Check the "then statements" for elsif parts and if statements
12075
12076 if Nkind (N) in N_Elsif_Part | N_If_Statement
12077 and then not Is_Empty_List (Then_Statements (N))
12078 and then not Are_Wrapped (Then_Statements (N))
12079 and then Requires_Cleanup_Actions
12080 (L => Then_Statements (N),
12081 Lib_Level => False,
12082 Nested_Constructs => False)
12083 then
12084 Block := Wrap_Statements_In_Block (Then_Statements (N));
12085 Set_Then_Statements (N, New_List (Block));
12086
12087 Analyze (Block);
12088 end if;
12089
12090 -- Check the "else statements" for conditional entry calls, if
12091 -- statements and selective accepts.
12092
12093 if Nkind (N) in
12094 N_Conditional_Entry_Call | N_If_Statement | N_Selective_Accept
12095 and then not Is_Empty_List (Else_Statements (N))
12096 and then not Are_Wrapped (Else_Statements (N))
12097 and then Requires_Cleanup_Actions
12098 (L => Else_Statements (N),
12099 Lib_Level => False,
12100 Nested_Constructs => False)
12101 then
12102 Block := Wrap_Statements_In_Block (Else_Statements (N));
12103 Set_Else_Statements (N, New_List (Block));
12104
12105 Analyze (Block);
12106 end if;
12107
12108 when N_Abortable_Part
12109 | N_Accept_Alternative
12110 | N_Case_Statement_Alternative
12111 | N_Delay_Alternative
12112 | N_Entry_Call_Alternative
12113 | N_Exception_Handler
12114 | N_Loop_Statement
12115 | N_Triggering_Alternative
12116 =>
12117 if not Is_Empty_List (Statements (N))
12118 and then not Are_Wrapped (Statements (N))
12119 and then Requires_Cleanup_Actions
12120 (L => Statements (N),
12121 Lib_Level => False,
12122 Nested_Constructs => False)
12123 then
12124 if Nkind (N) = N_Loop_Statement
12125 and then Present (Identifier (N))
12126 then
12127 Block :=
12128 Wrap_Statements_In_Block
12129 (L => Statements (N),
12130 Scop => Entity (Identifier (N)));
12131 else
12132 Block := Wrap_Statements_In_Block (Statements (N));
12133 end if;
12134
12135 Set_Statements (N, New_List (Block));
12136 Analyze (Block);
12137 end if;
12138
12139 -- Could be e.g. a loop that was transformed into a block or null
12140 -- statement. Do nothing for terminate alternatives.
12141
12142 when N_Block_Statement
12143 | N_Null_Statement
12144 | N_Terminate_Alternative
12145 =>
12146 null;
12147
12148 when others =>
12149 raise Program_Error;
12150 end case;
12151 end Process_Statements_For_Controlled_Objects;
12152
12153 ------------------
12154 -- Power_Of_Two --
12155 ------------------
12156
12157 function Power_Of_Two (N : Node_Id) return Nat is
12158 Typ : constant Entity_Id := Etype (N);
12159 pragma Assert (Is_Integer_Type (Typ));
12160
12161 Siz : constant Nat := UI_To_Int (Esize (Typ));
12162 Val : Uint;
12163
12164 begin
12165 if not Compile_Time_Known_Value (N) then
12166 return 0;
12167
12168 else
12169 Val := Expr_Value (N);
12170 for J in 1 .. Siz - 1 loop
12171 if Val = Uint_2 ** J then
12172 return J;
12173 end if;
12174 end loop;
12175
12176 return 0;
12177 end if;
12178 end Power_Of_Two;
12179
12180 ----------------------
12181 -- Remove_Init_Call --
12182 ----------------------
12183
12184 function Remove_Init_Call
12185 (Var : Entity_Id;
12186 Rep_Clause : Node_Id) return Node_Id
12187 is
12188 Par : constant Node_Id := Parent (Var);
12189 Typ : constant Entity_Id := Etype (Var);
12190
12191 Init_Proc : Entity_Id;
12192 -- Initialization procedure for Typ
12193
12194 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
12195 -- Look for init call for Var starting at From and scanning the
12196 -- enclosing list until Rep_Clause or the end of the list is reached.
12197
12198 ----------------------------
12199 -- Find_Init_Call_In_List --
12200 ----------------------------
12201
12202 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
12203 Init_Call : Node_Id;
12204
12205 begin
12206 Init_Call := From;
12207 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
12208 if Nkind (Init_Call) = N_Procedure_Call_Statement
12209 and then Is_Entity_Name (Name (Init_Call))
12210 and then Entity (Name (Init_Call)) = Init_Proc
12211 then
12212 return Init_Call;
12213 end if;
12214
12215 Next (Init_Call);
12216 end loop;
12217
12218 return Empty;
12219 end Find_Init_Call_In_List;
12220
12221 Init_Call : Node_Id;
12222
12223 -- Start of processing for Remove_Init_Call
12224
12225 begin
12226 if Present (Initialization_Statements (Var)) then
12227 Init_Call := Initialization_Statements (Var);
12228 Set_Initialization_Statements (Var, Empty);
12229
12230 -- Note that we rewrite Init_Call into a null statement, rather than
12231 -- just removing it, because Freeze_All may rely on this particular
12232 -- node still being present in the enclosing list to know where to
12233 -- stop freezing (see Explode_Initialization_Compound_Statement).
12234
12235 if Nkind (Init_Call) = N_Compound_Statement then
12236 declare
12237 Init_Actions : constant List_Id := Actions (Init_Call);
12238 Loc : constant Source_Ptr := Sloc (Init_Call);
12239
12240 begin
12241 Rewrite (Init_Call, Make_Null_Statement (Loc));
12242 return Make_Compound_Statement (Loc, Init_Actions);
12243 end;
12244 end if;
12245
12246 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
12247
12248 -- No init proc for the type, so obviously no call to be found
12249
12250 return Empty;
12251
12252 else
12253 -- We might be able to handle other cases below by just properly
12254 -- setting Initialization_Statements at the point where the init proc
12255 -- call is generated???
12256
12257 Init_Proc := Base_Init_Proc (Typ);
12258
12259 -- First scan the list containing the declaration of Var
12260
12261 Init_Call := Find_Init_Call_In_List (From => Next (Par));
12262
12263 -- If not found, also look on Var's freeze actions list, if any,
12264 -- since the init call may have been moved there (case of an address
12265 -- clause applying to Var).
12266
12267 if No (Init_Call) and then Present (Freeze_Node (Var)) then
12268 Init_Call :=
12269 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
12270 end if;
12271
12272 -- If the initialization call has actuals that use the secondary
12273 -- stack, the call may have been wrapped into a temporary block, in
12274 -- which case the block itself has to be removed.
12275
12276 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
12277 declare
12278 Blk : constant Node_Id := Next (Par);
12279 begin
12280 if Present
12281 (Find_Init_Call_In_List
12282 (First (Statements (Handled_Statement_Sequence (Blk)))))
12283 then
12284 Init_Call := Blk;
12285 end if;
12286 end;
12287 end if;
12288 end if;
12289
12290 if Present (Init_Call) then
12291 -- If restrictions have forbidden Aborts, the initialization call
12292 -- for objects that require deep initialization has not been wrapped
12293 -- into the following block (see Exp_Ch3, Default_Initialize_Object)
12294 -- so if present remove it as well, and include the IP call in it,
12295 -- in the rare case the caller may need to simply displace the
12296 -- initialization, as is done for a later address specification.
12297
12298 if Nkind (Next (Init_Call)) = N_Block_Statement
12299 and then Is_Initialization_Block (Next (Init_Call))
12300 then
12301 declare
12302 IP_Call : constant Node_Id := Init_Call;
12303 begin
12304 Init_Call := Next (IP_Call);
12305 Remove (IP_Call);
12306 Prepend (IP_Call,
12307 Statements (Handled_Statement_Sequence (Init_Call)));
12308 end;
12309 end if;
12310
12311 Remove (Init_Call);
12312 end if;
12313
12314 return Init_Call;
12315 end Remove_Init_Call;
12316
12317 -------------------------
12318 -- Remove_Side_Effects --
12319 -------------------------
12320
12321 procedure Remove_Side_Effects
12322 (Exp : Node_Id;
12323 Name_Req : Boolean := False;
12324 Renaming_Req : Boolean := False;
12325 Variable_Ref : Boolean := False;
12326 Related_Id : Entity_Id := Empty;
12327 Is_Low_Bound : Boolean := False;
12328 Is_High_Bound : Boolean := False;
12329 Discr_Number : Int := 0;
12330 Check_Side_Effects : Boolean := True)
12331 is
12332 function Build_Temporary
12333 (Loc : Source_Ptr;
12334 Id : Character;
12335 Related_Nod : Node_Id := Empty) return Entity_Id;
12336 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
12337 -- is present (xxx is taken from the Chars field of Related_Nod),
12338 -- otherwise it generates an internal temporary. The created temporary
12339 -- entity is marked as internal.
12340
12341 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean;
12342 -- Computes whether a side effect is possible in SPARK, which should
12343 -- be handled by removing it from the expression for GNATprove. Note
12344 -- that other side effects related to volatile variables are handled
12345 -- separately.
12346
12347 ---------------------
12348 -- Build_Temporary --
12349 ---------------------
12350
12351 function Build_Temporary
12352 (Loc : Source_Ptr;
12353 Id : Character;
12354 Related_Nod : Node_Id := Empty) return Entity_Id
12355 is
12356 Temp_Id : Entity_Id;
12357 Temp_Nam : Name_Id;
12358 Should_Set_Related_Expression : Boolean := False;
12359
12360 begin
12361 -- The context requires an external symbol : expression is
12362 -- the bound of an array, or a discriminant value. We create
12363 -- a unique string using the related entity and an appropriate
12364 -- suffix, rather than a numeric serial number (used for internal
12365 -- entities) that may vary depending on compilation options, in
12366 -- particular on the Assertions_Enabled mode. This avoids spurious
12367 -- link errors.
12368
12369 if Present (Related_Id) then
12370 if Is_Low_Bound then
12371 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
12372
12373 elsif Is_High_Bound then
12374 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
12375
12376 else
12377 pragma Assert (Discr_Number > 0);
12378
12379 -- We don't have any intelligible way of printing T_DISCR in
12380 -- CodePeer. Thus, set a related expression in this case.
12381
12382 Should_Set_Related_Expression := True;
12383
12384 -- Use fully qualified name to avoid ambiguities.
12385
12386 Temp_Nam :=
12387 New_External_Name
12388 (Get_Qualified_Name (Related_Id), "_DISCR", Discr_Number);
12389 end if;
12390
12391 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
12392
12393 if Should_Set_Related_Expression then
12394 Set_Related_Expression (Temp_Id, Related_Nod);
12395 end if;
12396
12397 -- Otherwise generate an internal temporary
12398
12399 else
12400 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
12401 end if;
12402
12403 Set_Is_Internal (Temp_Id);
12404
12405 return Temp_Id;
12406 end Build_Temporary;
12407
12408 -----------------------------------
12409 -- Possible_Side_Effect_In_SPARK --
12410 -----------------------------------
12411
12412 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean is
12413 begin
12414 -- Side-effect removal in SPARK should only occur when not inside a
12415 -- generic and not doing a preanalysis, inside an object renaming or
12416 -- a type declaration or a for-loop iteration scheme.
12417
12418 if not Inside_A_Generic
12419 and then Full_Analysis
12420 then
12421
12422 case Nkind (Enclosing_Declaration (Exp)) is
12423 when N_Component_Declaration
12424 | N_Full_Type_Declaration
12425 | N_Iterator_Specification
12426 | N_Loop_Parameter_Specification
12427 | N_Object_Renaming_Declaration
12428 =>
12429 return True;
12430
12431 -- If the expression belongs to an itype declaration, then
12432 -- check if side effects are allowed in the original
12433 -- associated node.
12434
12435 when N_Subtype_Declaration =>
12436 declare
12437 Subt : constant Entity_Id :=
12438 Defining_Identifier (Enclosing_Declaration (Exp));
12439 begin
12440 if Is_Itype (Subt) then
12441
12442 -- When this routine is called while the itype
12443 -- is being created, the entity might not yet be
12444 -- decorated with the associated node, but will
12445 -- have the related expression.
12446
12447 if Present (Associated_Node_For_Itype (Subt)) then
12448 return
12449 Possible_Side_Effect_In_SPARK
12450 (Associated_Node_For_Itype (Subt));
12451
12452 else
12453 return
12454 Possible_Side_Effect_In_SPARK
12455 (Related_Expression (Subt));
12456 end if;
12457 else
12458 return True;
12459 end if;
12460 end;
12461
12462 when others =>
12463 return False;
12464 end case;
12465 else
12466 return False;
12467 end if;
12468 end Possible_Side_Effect_In_SPARK;
12469
12470 -- Local variables
12471
12472 Loc : constant Source_Ptr := Sloc (Exp);
12473 Exp_Type : constant Entity_Id := Etype (Exp);
12474 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
12475 Def_Id : Entity_Id;
12476 E : Node_Id;
12477 New_Exp : Node_Id;
12478 Ptr_Typ_Decl : Node_Id;
12479 Ref_Type : Entity_Id;
12480 Res : Node_Id;
12481
12482 -- Start of processing for Remove_Side_Effects
12483
12484 begin
12485 -- Handle cases in which there is nothing to do. In GNATprove mode,
12486 -- removal of side effects is useful for the light expansion of
12487 -- renamings.
12488
12489 if not Expander_Active
12490 and then not
12491 (GNATprove_Mode and then Possible_Side_Effect_In_SPARK (Exp))
12492 then
12493 return;
12494
12495 -- Cannot generate temporaries if the invocation to remove side effects
12496 -- was issued too early and the type of the expression is not resolved
12497 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
12498 -- Remove_Side_Effects).
12499
12500 elsif No (Exp_Type)
12501 or else Ekind (Exp_Type) = E_Access_Attribute_Type
12502 then
12503 return;
12504
12505 -- No action needed for side-effect-free expressions
12506
12507 elsif Check_Side_Effects
12508 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
12509 then
12510 return;
12511 end if;
12512
12513 -- The remaining processing is done with all checks suppressed
12514
12515 -- Note: from now on, don't use return statements, instead do a goto
12516 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
12517
12518 Scope_Suppress.Suppress := (others => True);
12519
12520 -- If this is a side-effect-free attribute reference whose expressions
12521 -- are also side-effect-free and whose prefix is not a name, remove the
12522 -- side effects of the prefix. A copy of the prefix is required in this
12523 -- case and it is better not to make an additional one for the attribute
12524 -- itself, because the return type of many of them is universal integer,
12525 -- which is a very large type for a temporary.
12526 -- The prefix of an attribute reference Reduce may be syntactically an
12527 -- aggregate, but will be expanded into a loop, so no need to remove
12528 -- side effects.
12529
12530 if Nkind (Exp) = N_Attribute_Reference
12531 and then Side_Effect_Free_Attribute (Attribute_Name (Exp))
12532 and then Side_Effect_Free (Expressions (Exp), Name_Req, Variable_Ref)
12533 and then (Attribute_Name (Exp) /= Name_Reduce
12534 or else Nkind (Prefix (Exp)) /= N_Aggregate)
12535 and then not Is_Name_Reference (Prefix (Exp))
12536 then
12537 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12538 goto Leave;
12539
12540 -- If this is an elementary or a small not-by-reference record type, and
12541 -- we need to capture the value, just make a constant; this is cheap and
12542 -- objects of both kinds of types can be bit aligned, so it might not be
12543 -- possible to generate a reference to them. Likewise if this is not a
12544 -- name reference, except for a type conversion, because we would enter
12545 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
12546 -- type has predicates (and type conversions need a specific treatment
12547 -- anyway, see below). Also do it if we have a volatile reference and
12548 -- Name_Req is not set (see comments for Side_Effect_Free).
12549
12550 elsif (Is_Elementary_Type (Exp_Type)
12551 or else (Is_Record_Type (Exp_Type)
12552 and then Known_Static_RM_Size (Exp_Type)
12553 and then RM_Size (Exp_Type) <= System_Max_Integer_Size
12554 and then not Has_Discriminants (Exp_Type)
12555 and then not Is_By_Reference_Type (Exp_Type)))
12556 and then (Variable_Ref
12557 or else (not Is_Name_Reference (Exp)
12558 and then Nkind (Exp) /= N_Type_Conversion)
12559 or else (not Name_Req
12560 and then Is_Volatile_Reference (Exp)))
12561 then
12562 Def_Id := Build_Temporary (Loc, 'R', Exp);
12563 Set_Etype (Def_Id, Exp_Type);
12564 Res := New_Occurrence_Of (Def_Id, Loc);
12565
12566 -- If the expression is a packed reference, it must be reanalyzed and
12567 -- expanded, depending on context. This is the case for actuals where
12568 -- a constraint check may capture the actual before expansion of the
12569 -- call is complete.
12570
12571 if Nkind (Exp) = N_Indexed_Component
12572 and then Is_Packed (Etype (Prefix (Exp)))
12573 then
12574 Set_Analyzed (Exp, False);
12575 Set_Analyzed (Prefix (Exp), False);
12576 end if;
12577
12578 -- Generate:
12579 -- Rnn : Exp_Type renames Expr;
12580
12581 -- In GNATprove mode, we prefer to use renamings for intermediate
12582 -- variables to definition of constants, due to the implicit move
12583 -- operation that such a constant definition causes as part of the
12584 -- support in GNATprove for ownership pointers. Hence, we generate
12585 -- a renaming for a reference to an object of a nonscalar type.
12586
12587 if Renaming_Req
12588 or else (GNATprove_Mode
12589 and then Is_Object_Reference (Exp)
12590 and then not Is_Scalar_Type (Exp_Type))
12591 then
12592 E :=
12593 Make_Object_Renaming_Declaration (Loc,
12594 Defining_Identifier => Def_Id,
12595 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12596 Name => Relocate_Node (Exp));
12597
12598 -- Generate:
12599 -- Rnn : constant Exp_Type := Expr;
12600
12601 else
12602 E :=
12603 Make_Object_Declaration (Loc,
12604 Defining_Identifier => Def_Id,
12605 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12606 Constant_Present => True,
12607 Expression => Relocate_Node (Exp));
12608
12609 Set_Assignment_OK (E);
12610 end if;
12611
12612 Insert_Action (Exp, E);
12613
12614 -- If the expression has the form v.all then we can just capture the
12615 -- pointer, and then do an explicit dereference on the result, but
12616 -- this is not right if this is a volatile reference.
12617
12618 elsif Nkind (Exp) = N_Explicit_Dereference
12619 and then not Is_Volatile_Reference (Exp)
12620 then
12621 Def_Id := Build_Temporary (Loc, 'R', Exp);
12622 Res :=
12623 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
12624
12625 Insert_Action (Exp,
12626 Make_Object_Declaration (Loc,
12627 Defining_Identifier => Def_Id,
12628 Object_Definition =>
12629 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
12630 Constant_Present => True,
12631 Expression => Relocate_Node (Prefix (Exp))));
12632
12633 -- Similar processing for an unchecked conversion of an expression of
12634 -- the form v.all, where we want the same kind of treatment.
12635
12636 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12637 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
12638 then
12639 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12640 goto Leave;
12641
12642 -- If this is a type conversion, leave the type conversion and remove
12643 -- side effects in the expression, unless it is of universal integer,
12644 -- which is a very large type for a temporary. This is important in
12645 -- several circumstances: for change of representations and also when
12646 -- this is a view conversion to a smaller object, where gigi can end
12647 -- up creating its own temporary of the wrong size.
12648
12649 elsif Nkind (Exp) = N_Type_Conversion
12650 and then Etype (Expression (Exp)) /= Universal_Integer
12651 then
12652 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12653 goto Leave;
12654
12655 -- If this is an unchecked conversion that Gigi can't handle, make
12656 -- a copy or a use a renaming to capture the value.
12657
12658 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12659 and then not Safe_Unchecked_Type_Conversion (Exp)
12660 then
12661 if CW_Or_Needs_Finalization (Exp_Type) then
12662
12663 -- Use a renaming to capture the expression, rather than create
12664 -- a controlled temporary.
12665
12666 Def_Id := Build_Temporary (Loc, 'R', Exp);
12667 Res := New_Occurrence_Of (Def_Id, Loc);
12668
12669 Insert_Action (Exp,
12670 Make_Object_Renaming_Declaration (Loc,
12671 Defining_Identifier => Def_Id,
12672 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12673 Name => Relocate_Node (Exp)));
12674
12675 else
12676 Def_Id := Build_Temporary (Loc, 'R', Exp);
12677 Set_Etype (Def_Id, Exp_Type);
12678 Res := New_Occurrence_Of (Def_Id, Loc);
12679
12680 E :=
12681 Make_Object_Declaration (Loc,
12682 Defining_Identifier => Def_Id,
12683 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12684 Constant_Present => not Is_Variable (Exp),
12685 Expression => Relocate_Node (Exp));
12686
12687 Set_Assignment_OK (E);
12688 Insert_Action (Exp, E);
12689 end if;
12690
12691 -- If this is a packed array component or a selected component with a
12692 -- nonstandard representation, we cannot generate a reference because
12693 -- the component may be unaligned, so we must use a renaming and this
12694 -- renaming is handled by the front end, as the back end may balk at
12695 -- the nonstandard representation (see Evaluation_Required in Exp_Ch8).
12696
12697 elsif (Nkind (Exp) in N_Indexed_Component | N_Selected_Component
12698 and then Has_Non_Standard_Rep (Etype (Prefix (Exp))))
12699
12700 -- For an expression that denotes a name, we can use a renaming
12701 -- scheme. This is needed for correctness in the case of a volatile
12702 -- object of a nonvolatile type because the Make_Reference call of the
12703 -- "default" approach would generate an illegal access value (an
12704 -- access value cannot designate such an object - see
12705 -- Analyze_Reference).
12706
12707 or else (Is_Name_Reference (Exp)
12708
12709 -- We skip using this scheme if we have an object of a volatile
12710 -- type and we do not have Name_Req set true (see comments for
12711 -- Side_Effect_Free).
12712
12713 and then (Name_Req or else not Treat_As_Volatile (Exp_Type)))
12714 then
12715 Def_Id := Build_Temporary (Loc, 'R', Exp);
12716 Res := New_Occurrence_Of (Def_Id, Loc);
12717
12718 Insert_Action (Exp,
12719 Make_Object_Renaming_Declaration (Loc,
12720 Defining_Identifier => Def_Id,
12721 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12722 Name => Relocate_Node (Exp)));
12723
12724 -- Avoid generating a variable-sized temporary, by generating the
12725 -- reference just for the function call. The transformation could be
12726 -- refined to apply only when the array component is constrained by a
12727 -- discriminant???
12728
12729 elsif Nkind (Exp) = N_Selected_Component
12730 and then Nkind (Prefix (Exp)) = N_Function_Call
12731 and then Is_Array_Type (Exp_Type)
12732 then
12733 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12734 goto Leave;
12735
12736 -- Otherwise we generate a reference to the expression
12737
12738 else
12739 -- Special processing for function calls with a result type that is
12740 -- either BIP or a constrained array with controlled component and
12741 -- an unconstrained first subtype, when the context is neither an
12742 -- object declaration (to prevent infinite recursion) nor a function
12743 -- return (to propagate the anonymous return object).
12744
12745 -- We need to build an object declaration to trigger build-in-place
12746 -- expansion of the call in the former case, and addition of bounds
12747 -- to the object in the latter case.
12748
12749 if Nkind (Exp) = N_Function_Call
12750 and then (Is_Build_In_Place_Result_Type (Exp_Type)
12751 or else
12752 Is_Constr_Array_Subt_Of_Unc_With_Controlled (Exp_Type))
12753 and then Nkind (Parent (Exp)) /= N_Object_Declaration
12754 and then not Is_Expression_Of_Func_Return (Exp)
12755 then
12756 declare
12757 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
12758 Decl : Node_Id;
12759
12760 begin
12761 Decl :=
12762 Make_Object_Declaration (Loc,
12763 Defining_Identifier => Obj,
12764 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12765 Expression => Relocate_Node (Exp));
12766
12767 Insert_Action (Exp, Decl);
12768 Set_Etype (Obj, Exp_Type);
12769 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
12770 goto Leave;
12771 end;
12772 end if;
12773
12774 Def_Id := Build_Temporary (Loc, 'R', Exp);
12775
12776 -- The regular expansion of functions with side effects involves the
12777 -- generation of an access type to capture the return value found on
12778 -- the secondary stack. Since SPARK (and why) cannot process access
12779 -- types, use a different approach which ignores the secondary stack
12780 -- and "copies" the returned object.
12781
12782 if GNATprove_Mode then
12783 Res := New_Occurrence_Of (Def_Id, Loc);
12784 Ref_Type := Exp_Type;
12785
12786 -- Regular expansion utilizing an access type and 'reference
12787
12788 else
12789 Res :=
12790 Make_Explicit_Dereference (Loc,
12791 Prefix => New_Occurrence_Of (Def_Id, Loc));
12792
12793 -- Generate:
12794 -- type Ann is access all <Exp_Type>;
12795
12796 Ref_Type := Make_Temporary (Loc, 'A');
12797
12798 Ptr_Typ_Decl :=
12799 Make_Full_Type_Declaration (Loc,
12800 Defining_Identifier => Ref_Type,
12801 Type_Definition =>
12802 Make_Access_To_Object_Definition (Loc,
12803 All_Present => True,
12804 Subtype_Indication =>
12805 New_Occurrence_Of (Exp_Type, Loc)));
12806
12807 Insert_Action (Exp, Ptr_Typ_Decl);
12808 end if;
12809
12810 E := Exp;
12811 if Nkind (E) = N_Explicit_Dereference then
12812 New_Exp := Relocate_Node (Prefix (E));
12813
12814 else
12815 E := Relocate_Node (E);
12816
12817 -- Do not generate a 'reference in SPARK mode since the access
12818 -- type is not created in the first place.
12819
12820 if GNATprove_Mode then
12821 New_Exp := E;
12822
12823 -- Otherwise generate reference, marking the value as non-null
12824 -- since we know it cannot be null and we don't want a check.
12825
12826 else
12827 -- Make_Reference assumes that the referenced
12828 -- object satisfies the constraints of the designated
12829 -- subtype of the access type. Ensure that this assumption
12830 -- holds by introducing a qualified expression if needed.
12831
12832 if not Analyzed (Exp)
12833 and then Nkind (Exp) = N_Aggregate
12834 and then (Is_Array_Type (Exp_Type)
12835 or else Has_Discriminants (Exp_Type))
12836 and then Is_Constrained (Exp_Type)
12837 then
12838 -- Do not suppress checks associated with the qualified
12839 -- expression we are about to introduce (unless those
12840 -- checks were already suppressed when Remove_Side_Effects
12841 -- was called).
12842
12843 if Is_Array_Type (Exp_Type) then
12844 Scope_Suppress.Suppress (Length_Check) :=
12845 Svg_Suppress.Suppress (Length_Check);
12846 else
12847 Scope_Suppress.Suppress (Discriminant_Check) :=
12848 Svg_Suppress.Suppress (Discriminant_Check);
12849 end if;
12850
12851 E := Make_Qualified_Expression (Loc,
12852 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12853 Expression => E);
12854 end if;
12855
12856 New_Exp := Make_Reference (Loc, E);
12857 Set_Is_Known_Non_Null (Def_Id);
12858 end if;
12859 end if;
12860
12861 if Is_Delayed_Aggregate (E) then
12862
12863 -- The expansion of nested aggregates is delayed until the
12864 -- enclosing aggregate is expanded. As aggregates are often
12865 -- qualified, the predicate applies to qualified expressions as
12866 -- well, indicating that the enclosing aggregate has not been
12867 -- expanded yet. At this point the aggregate is part of a
12868 -- stand-alone declaration, and must be fully expanded.
12869
12870 if Nkind (E) = N_Qualified_Expression then
12871 Set_Expansion_Delayed (Expression (E), False);
12872 Set_Analyzed (Expression (E), False);
12873 else
12874 Set_Expansion_Delayed (E, False);
12875 end if;
12876
12877 Set_Analyzed (E, False);
12878 end if;
12879
12880 Insert_Action (Exp,
12881 Make_Object_Declaration (Loc,
12882 Defining_Identifier => Def_Id,
12883 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
12884 Constant_Present => True,
12885 Expression => New_Exp));
12886 end if;
12887
12888 -- Preserve the Assignment_OK flag in all copies, since at least one
12889 -- copy may be used in a context where this flag must be set (otherwise
12890 -- why would the flag be set in the first place).
12891
12892 Set_Assignment_OK (Res, Assignment_OK (Exp));
12893
12894 -- Preserve the Do_Range_Check flag in all copies
12895
12896 Set_Do_Range_Check (Res, Do_Range_Check (Exp));
12897
12898 -- Finally rewrite the original expression and we are done
12899
12900 Rewrite (Exp, Res);
12901 Analyze_And_Resolve (Exp, Exp_Type);
12902
12903 <<Leave>>
12904 Scope_Suppress := Svg_Suppress;
12905 end Remove_Side_Effects;
12906
12907 ------------------------
12908 -- Replace_References --
12909 ------------------------
12910
12911 procedure Replace_References
12912 (Expr : Node_Id;
12913 Par_Typ : Entity_Id;
12914 Deriv_Typ : Entity_Id;
12915 Par_Obj : Entity_Id := Empty;
12916 Deriv_Obj : Entity_Id := Empty)
12917 is
12918 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
12919 -- Determine whether node Ref denotes some component of Deriv_Obj
12920
12921 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
12922 -- Substitute a reference to an entity with the corresponding value
12923 -- stored in table Type_Map.
12924
12925 function Type_Of_Formal
12926 (Call : Node_Id;
12927 Actual : Node_Id) return Entity_Id;
12928 -- Find the type of the formal parameter which corresponds to actual
12929 -- parameter Actual in subprogram call Call.
12930
12931 ----------------------
12932 -- Is_Deriv_Obj_Ref --
12933 ----------------------
12934
12935 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
12936 Par : constant Node_Id := Parent (Ref);
12937
12938 begin
12939 -- Detect the folowing selected component form:
12940
12941 -- Deriv_Obj.(something)
12942
12943 return
12944 Nkind (Par) = N_Selected_Component
12945 and then Is_Entity_Name (Prefix (Par))
12946 and then Entity (Prefix (Par)) = Deriv_Obj;
12947 end Is_Deriv_Obj_Ref;
12948
12949 -----------------
12950 -- Replace_Ref --
12951 -----------------
12952
12953 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
12954 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
12955 -- Reset the Controlling_Argument of all function calls that
12956 -- encapsulate node From_Arg.
12957
12958 ----------------------------------
12959 -- Remove_Controlling_Arguments --
12960 ----------------------------------
12961
12962 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
12963 Par : Node_Id;
12964
12965 begin
12966 Par := From_Arg;
12967 while Present (Par) loop
12968 if Nkind (Par) = N_Function_Call
12969 and then Present (Controlling_Argument (Par))
12970 then
12971 Set_Controlling_Argument (Par, Empty);
12972
12973 -- Prevent the search from going too far
12974
12975 elsif Is_Body_Or_Package_Declaration (Par) then
12976 exit;
12977 end if;
12978
12979 Par := Parent (Par);
12980 end loop;
12981 end Remove_Controlling_Arguments;
12982
12983 -- Local variables
12984
12985 Context : constant Node_Id :=
12986 (if No (Ref) then Empty else Parent (Ref));
12987
12988 Loc : constant Source_Ptr := Sloc (Ref);
12989 Ref_Id : Entity_Id;
12990 Result : Traverse_Result;
12991
12992 New_Ref : Node_Id;
12993 -- The new reference which is intended to substitute the old one
12994
12995 Old_Ref : Node_Id;
12996 -- The reference designated for replacement. In certain cases this
12997 -- may be a node other than Ref.
12998
12999 Val : Node_Or_Entity_Id;
13000 -- The corresponding value of Ref from the type map
13001
13002 -- Start of processing for Replace_Ref
13003
13004 begin
13005 -- Assume that the input reference is to be replaced and that the
13006 -- traversal should examine the children of the reference.
13007
13008 Old_Ref := Ref;
13009 Result := OK;
13010
13011 -- The input denotes a meaningful reference
13012
13013 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
13014 Ref_Id := Entity (Ref);
13015 Val := Type_Map.Get (Ref_Id);
13016
13017 -- The reference has a corresponding value in the type map, a
13018 -- substitution is possible.
13019
13020 if Present (Val) then
13021
13022 -- The reference denotes a discriminant
13023
13024 if Ekind (Ref_Id) = E_Discriminant then
13025 if Nkind (Val) in N_Entity then
13026
13027 -- The value denotes another discriminant. Replace as
13028 -- follows:
13029
13030 -- _object.Discr -> _object.Val
13031
13032 if Ekind (Val) = E_Discriminant then
13033 New_Ref := New_Occurrence_Of (Val, Loc);
13034
13035 -- Otherwise the value denotes the entity of a name which
13036 -- constraints the discriminant. Replace as follows:
13037
13038 -- _object.Discr -> Val
13039
13040 else
13041 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
13042
13043 New_Ref := New_Occurrence_Of (Val, Loc);
13044 Old_Ref := Parent (Old_Ref);
13045 end if;
13046
13047 -- Otherwise the value denotes an arbitrary expression which
13048 -- constraints the discriminant. Replace as follows:
13049
13050 -- _object.Discr -> Val
13051
13052 else
13053 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
13054
13055 New_Ref := New_Copy_Tree (Val);
13056 Old_Ref := Parent (Old_Ref);
13057 end if;
13058
13059 -- Otherwise the reference denotes a primitive. Replace as
13060 -- follows:
13061
13062 -- Primitive -> Val
13063
13064 else
13065 pragma Assert (Nkind (Val) in N_Entity);
13066 New_Ref := New_Occurrence_Of (Val, Loc);
13067 end if;
13068
13069 -- The reference mentions the _object parameter of the parent
13070 -- type's DIC or type invariant procedure. Replace as follows:
13071
13072 -- _object -> _object
13073
13074 elsif Present (Par_Obj)
13075 and then Present (Deriv_Obj)
13076 and then Ref_Id = Par_Obj
13077 then
13078 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
13079
13080 -- The type of the _object parameter is class-wide when the
13081 -- expression comes from an assertion pragma that applies to
13082 -- an abstract parent type or an interface. The class-wide type
13083 -- facilitates the preanalysis of the expression by treating
13084 -- calls to abstract primitives that mention the current
13085 -- instance of the type as dispatching. Once the calls are
13086 -- remapped to invoke overriding or inherited primitives, the
13087 -- calls no longer need to be dispatching. Examine all function
13088 -- calls that encapsulate the _object parameter and reset their
13089 -- Controlling_Argument attribute.
13090
13091 if Is_Class_Wide_Type (Etype (Par_Obj))
13092 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
13093 then
13094 Remove_Controlling_Arguments (Old_Ref);
13095 end if;
13096
13097 -- The reference to _object acts as an actual parameter in a
13098 -- subprogram call which may be invoking a primitive of the
13099 -- parent type:
13100
13101 -- Primitive (... _object ...);
13102
13103 -- The parent type primitive may not be overridden nor
13104 -- inherited when it is declared after the derived type
13105 -- definition:
13106
13107 -- type Parent is tagged private;
13108 -- type Child is new Parent with private;
13109 -- procedure Primitive (Obj : Parent);
13110
13111 -- In this scenario the _object parameter is converted to the
13112 -- parent type. Due to complications with partial/full views
13113 -- and view swaps, the parent type is taken from the formal
13114 -- parameter of the subprogram being called.
13115
13116 if Nkind (Context) in N_Subprogram_Call
13117 and then No (Type_Map.Get (Entity (Name (Context))))
13118 then
13119 declare
13120 -- We need to use the Original_Node of the callee, in
13121 -- case it was already modified. Note that we are using
13122 -- Traverse_Proc to walk the tree, and it is defined to
13123 -- walk subtrees in an arbitrary order.
13124
13125 Callee : constant Entity_Id :=
13126 Entity (Original_Node (Name (Context)));
13127 begin
13128 if No (Type_Map.Get (Callee)) then
13129 New_Ref :=
13130 Convert_To
13131 (Type_Of_Formal (Context, Old_Ref), New_Ref);
13132
13133 -- Do not process the generated type conversion
13134 -- because both the parent type and the derived type
13135 -- are in the Type_Map table. This will clobber the
13136 -- type conversion by resetting its subtype mark.
13137
13138 Result := Skip;
13139 end if;
13140 end;
13141 end if;
13142
13143 -- Otherwise there is nothing to replace
13144
13145 else
13146 New_Ref := Empty;
13147 end if;
13148
13149 if Present (New_Ref) then
13150 Rewrite (Old_Ref, New_Ref);
13151
13152 -- Update the return type when the context of the reference
13153 -- acts as the name of a function call. Note that the update
13154 -- should not be performed when the reference appears as an
13155 -- actual in the call.
13156
13157 if Nkind (Context) = N_Function_Call
13158 and then Name (Context) = Old_Ref
13159 then
13160 Set_Etype (Context, Etype (Val));
13161 end if;
13162 end if;
13163 end if;
13164
13165 -- Reanalyze the reference due to potential replacements
13166
13167 if Nkind (Old_Ref) in N_Has_Etype then
13168 Set_Analyzed (Old_Ref, False);
13169 end if;
13170
13171 return Result;
13172 end Replace_Ref;
13173
13174 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
13175
13176 --------------------
13177 -- Type_Of_Formal --
13178 --------------------
13179
13180 function Type_Of_Formal
13181 (Call : Node_Id;
13182 Actual : Node_Id) return Entity_Id
13183 is
13184 A : Node_Id;
13185 F : Entity_Id;
13186
13187 begin
13188 -- Examine the list of actual and formal parameters in parallel
13189
13190 A := First_Actual (Call);
13191 F := First_Formal (Entity (Name (Call)));
13192 while Present (A) and then Present (F) loop
13193 if A = Actual then
13194 return Etype (F);
13195 end if;
13196
13197 Next_Actual (A);
13198 Next_Formal (F);
13199 end loop;
13200
13201 -- The actual parameter must always have a corresponding formal
13202
13203 pragma Assert (False);
13204
13205 return Empty;
13206 end Type_Of_Formal;
13207
13208 -- Start of processing for Replace_References
13209
13210 begin
13211 -- Map the attributes of the parent type to the proper corresponding
13212 -- attributes of the derived type.
13213
13214 Map_Types
13215 (Parent_Type => Par_Typ,
13216 Derived_Type => Deriv_Typ);
13217
13218 -- Inspect the input expression and perform substitutions where
13219 -- necessary.
13220
13221 Replace_Refs (Expr);
13222 end Replace_References;
13223
13224 -----------------------------
13225 -- Replace_Type_References --
13226 -----------------------------
13227
13228 procedure Replace_Type_References
13229 (Expr : Node_Id;
13230 Typ : Entity_Id;
13231 Obj_Id : Entity_Id)
13232 is
13233 procedure Replace_Type_Ref (N : Node_Id);
13234 -- Substitute a single reference of the current instance of type Typ
13235 -- with a reference to Obj_Id.
13236
13237 ----------------------
13238 -- Replace_Type_Ref --
13239 ----------------------
13240
13241 procedure Replace_Type_Ref (N : Node_Id) is
13242 begin
13243 -- Decorate the reference to Typ even though it may be rewritten
13244 -- further down. This is done so that routines which examine
13245 -- properties of the Original_Node have some semantic information.
13246
13247 if Nkind (N) = N_Identifier then
13248 Set_Entity (N, Typ);
13249 Set_Etype (N, Typ);
13250
13251 elsif Nkind (N) = N_Selected_Component then
13252 Analyze (Prefix (N));
13253 Set_Entity (Selector_Name (N), Typ);
13254 Set_Etype (Selector_Name (N), Typ);
13255 end if;
13256
13257 -- Perform the following substitution:
13258
13259 -- Typ --> _object
13260
13261 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
13262 Set_Comes_From_Source (N, True);
13263 end Replace_Type_Ref;
13264
13265 procedure Replace_Type_Refs is
13266 new Replace_Type_References_Generic (Replace_Type_Ref);
13267
13268 -- Start of processing for Replace_Type_References
13269
13270 begin
13271 Replace_Type_Refs (Expr, Typ);
13272 end Replace_Type_References;
13273
13274 ---------------------------
13275 -- Represented_As_Scalar --
13276 ---------------------------
13277
13278 function Represented_As_Scalar (T : Entity_Id) return Boolean is
13279 UT : constant Entity_Id := Underlying_Type (T);
13280 begin
13281 return Is_Scalar_Type (UT)
13282 or else (Is_Bit_Packed_Array (UT)
13283 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
13284 end Represented_As_Scalar;
13285
13286 ------------------------------
13287 -- Requires_Cleanup_Actions --
13288 ------------------------------
13289
13290 function Requires_Cleanup_Actions
13291 (N : Node_Id;
13292 Lib_Level : Boolean) return Boolean
13293 is
13294 At_Lib_Level : constant Boolean :=
13295 Lib_Level
13296 and then Nkind (N) in N_Package_Body | N_Package_Specification;
13297 -- N is at the library level if the top-most context is a package and
13298 -- the path taken to reach N does not include nonpackage constructs.
13299
13300 begin
13301 case Nkind (N) is
13302 when N_Accept_Statement
13303 | N_Block_Statement
13304 | N_Entry_Body
13305 | N_Package_Body
13306 | N_Subprogram_Body
13307 | N_Task_Body
13308 =>
13309 return
13310 Requires_Cleanup_Actions
13311 (L => Declarations (N),
13312 Lib_Level => At_Lib_Level,
13313 Nested_Constructs => True)
13314 or else
13315 (Present (Handled_Statement_Sequence (N))
13316 and then
13317 Requires_Cleanup_Actions
13318 (L =>
13319 Statements (Handled_Statement_Sequence (N)),
13320 Lib_Level => At_Lib_Level,
13321 Nested_Constructs => True));
13322
13323 -- Extended return statements are the same as the above, except that
13324 -- there is no Declarations field. We do not want to clean up the
13325 -- Return_Object_Declarations.
13326
13327 when N_Extended_Return_Statement =>
13328 return
13329 Present (Handled_Statement_Sequence (N))
13330 and then Requires_Cleanup_Actions
13331 (L =>
13332 Statements (Handled_Statement_Sequence (N)),
13333 Lib_Level => At_Lib_Level,
13334 Nested_Constructs => True);
13335
13336 when N_Package_Specification =>
13337 return
13338 Requires_Cleanup_Actions
13339 (L => Visible_Declarations (N),
13340 Lib_Level => At_Lib_Level,
13341 Nested_Constructs => True)
13342 or else
13343 Requires_Cleanup_Actions
13344 (L => Private_Declarations (N),
13345 Lib_Level => At_Lib_Level,
13346 Nested_Constructs => True);
13347
13348 when others =>
13349 raise Program_Error;
13350 end case;
13351 end Requires_Cleanup_Actions;
13352
13353 ------------------------------
13354 -- Requires_Cleanup_Actions --
13355 ------------------------------
13356
13357 function Requires_Cleanup_Actions
13358 (L : List_Id;
13359 Lib_Level : Boolean;
13360 Nested_Constructs : Boolean) return Boolean
13361 is
13362 Decl : Node_Id;
13363 Obj_Id : Entity_Id;
13364 Obj_Typ : Entity_Id;
13365 Pack_Id : Entity_Id;
13366 Typ : Entity_Id;
13367
13368 begin
13369 Decl := First (L);
13370 while Present (Decl) loop
13371
13372 -- Library-level tagged types
13373
13374 if Nkind (Decl) = N_Full_Type_Declaration then
13375 Typ := Defining_Identifier (Decl);
13376
13377 -- Ignored Ghost types do not need any cleanup actions because
13378 -- they will not appear in the final tree.
13379
13380 if Is_Ignored_Ghost_Entity (Typ) then
13381 null;
13382
13383 elsif Is_Tagged_Type (Typ)
13384 and then Is_Library_Level_Entity (Typ)
13385 and then Convention (Typ) = Convention_Ada
13386 and then Present (Access_Disp_Table (Typ))
13387 and then not Is_Abstract_Type (Typ)
13388 and then not No_Run_Time_Mode
13389 and then not Restriction_Active (No_Tagged_Type_Registration)
13390 and then RTE_Available (RE_Unregister_Tag)
13391 then
13392 return True;
13393 end if;
13394
13395 -- Regular object declarations
13396
13397 elsif Nkind (Decl) = N_Object_Declaration then
13398 Obj_Id := Defining_Identifier (Decl);
13399 Obj_Typ := Base_Type (Etype (Obj_Id));
13400
13401 -- Bypass any form of processing for objects which have their
13402 -- finalization disabled. This applies only to objects at the
13403 -- library level.
13404
13405 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
13406 null;
13407
13408 -- Finalization of transient objects is treated separately in
13409 -- order to handle sensitive cases. These include:
13410
13411 -- * Conditional expressions
13412 -- * Expressions with actions
13413 -- * Transient scopes
13414
13415 elsif Is_Finalized_Transient (Obj_Id) then
13416 null;
13417
13418 -- Finalization of specific objects is also treated separately
13419
13420 elsif Is_Ignored_For_Finalization (Obj_Id) then
13421 null;
13422
13423 -- Ignored Ghost objects do not need any cleanup actions because
13424 -- they will not appear in the final tree.
13425
13426 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
13427 null;
13428
13429 -- Conversely, if one of the above cases created a Master_Node,
13430 -- finalization actions are required for the associated object.
13431
13432 elsif Ekind (Obj_Id) = E_Variable
13433 and then Is_RTE (Obj_Typ, RE_Master_Node)
13434 then
13435 return True;
13436
13437 -- The object is of the form:
13438 -- Obj : [constant] Typ [:= Expr];
13439 --
13440 -- Do not process the incomplete view of a deferred constant.
13441 -- Note that an object initialized by means of a BIP function
13442 -- call may appear as a deferred constant after expansion
13443 -- activities. These kinds of objects must be finalized.
13444
13445 elsif not Is_Imported (Obj_Id)
13446 and then Needs_Finalization (Obj_Typ)
13447 and then not (Ekind (Obj_Id) = E_Constant
13448 and then not Has_Completion (Obj_Id)
13449 and then No (BIP_Initialization_Call (Obj_Id)))
13450 then
13451 return True;
13452
13453 -- The object is an access-to-controlled that must be finalized
13454
13455 elsif Is_Access_Type (Obj_Typ)
13456 and then Is_Finalizable_Access (Decl)
13457 then
13458 return True;
13459
13460 -- Simple protected objects which use the type System.Tasking.
13461 -- Protected_Objects.Protection to manage their locks should be
13462 -- treated as controlled since they require manual cleanup, but
13463 -- not for restricted run-time libraries (Ravenscar), see also
13464 -- Cleanup_Protected_Object in Exp_Ch7.
13465
13466 -- The only exception is illustrated in the following example:
13467
13468 -- package Pkg is
13469 -- type Ctrl is new Controlled ...
13470 -- procedure Finalize (Obj : in out Ctrl);
13471 -- Lib_Obj : Ctrl;
13472 -- end Pkg;
13473
13474 -- package body Pkg is
13475 -- protected Prot is
13476 -- procedure Do_Something (Obj : in out Ctrl);
13477 -- end Prot;
13478
13479 -- protected body Prot is
13480 -- procedure Do_Something (Obj : in out Ctrl) is ...
13481 -- end Prot;
13482
13483 -- procedure Finalize (Obj : in out Ctrl) is
13484 -- begin
13485 -- Prot.Do_Something (Obj);
13486 -- end Finalize;
13487 -- end Pkg;
13488
13489 -- Since for the most part entities in package bodies depend on
13490 -- those in package specs, Prot's lock should be cleaned up
13491 -- first. The subsequent cleanup of the spec finalizes Lib_Obj.
13492 -- This act however attempts to invoke Do_Something and fails
13493 -- because the lock has disappeared.
13494
13495 elsif Ekind (Obj_Id) = E_Variable
13496 and then not In_Library_Level_Package_Body (Obj_Id)
13497 and then Has_Simple_Protected_Object (Obj_Typ)
13498 and then not Restricted_Profile
13499 then
13500 return True;
13501 end if;
13502
13503 -- Inspect the freeze node of an access-to-controlled type and look
13504 -- for a delayed finalization collection. This case arises when the
13505 -- freeze actions are inserted at a later time than the expansion of
13506 -- the context. Since Build_Finalizer is never called on a single
13507 -- construct twice, the collection would be ultimately left out and
13508 -- never finalized. This is also needed for the freeze actions of
13509 -- designated types themselves, since in some cases the finalization
13510 -- collection is associated with a designated type's freeze node
13511 -- rather than that of the access type (see handling for freeze
13512 -- actions in Build_Finalization_Collection).
13513
13514 elsif Nkind (Decl) = N_Freeze_Entity
13515 and then Present (Actions (Decl))
13516 then
13517 Typ := Entity (Decl);
13518
13519 -- Freeze nodes for ignored Ghost types do not need cleanup
13520 -- actions because they will never appear in the final tree.
13521
13522 if Is_Ignored_Ghost_Entity (Typ) then
13523 null;
13524
13525 elsif ((Is_Access_Object_Type (Typ)
13526 and then Needs_Finalization
13527 (Available_View (Designated_Type (Typ))))
13528 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
13529 and then Requires_Cleanup_Actions
13530 (Actions (Decl), Lib_Level, Nested_Constructs)
13531 then
13532 return True;
13533 end if;
13534
13535 -- Nested package declarations
13536
13537 elsif Nested_Constructs
13538 and then Nkind (Decl) = N_Package_Declaration
13539 then
13540 Pack_Id := Defining_Entity (Decl);
13541
13542 -- Do not inspect an ignored Ghost package because all code found
13543 -- within will not appear in the final tree.
13544
13545 if Is_Ignored_Ghost_Entity (Pack_Id) then
13546 null;
13547
13548 elsif Ekind (Pack_Id) /= E_Generic_Package
13549 and then Requires_Cleanup_Actions
13550 (Specification (Decl), Lib_Level)
13551 then
13552 return True;
13553 end if;
13554
13555 -- Nested package bodies
13556
13557 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
13558
13559 -- Do not inspect an ignored Ghost package body because all code
13560 -- found within will not appear in the final tree.
13561
13562 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
13563 null;
13564
13565 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13566 and then Requires_Cleanup_Actions (Decl, Lib_Level)
13567 then
13568 return True;
13569 end if;
13570 end if;
13571
13572 Next (Decl);
13573 end loop;
13574
13575 return False;
13576 end Requires_Cleanup_Actions;
13577
13578 --------------------------------------------
13579 -- Rewrite_Object_Declaration_As_Renaming --
13580 --------------------------------------------
13581
13582 procedure Rewrite_Object_Declaration_As_Renaming (N, Nam : Node_Id) is
13583 Def_Id : constant Entity_Id := Defining_Identifier (N);
13584 Loc : constant Source_Ptr := Sloc (N);
13585
13586 begin
13587 Rewrite (N,
13588 Make_Object_Renaming_Declaration (Loc,
13589 Defining_Identifier => Def_Id,
13590 Subtype_Mark => New_Occurrence_Of (Etype (Def_Id), Loc),
13591 Name => Nam));
13592
13593 -- Keep original aspects
13594
13595 Move_Aspects (Original_Node (N), N);
13596
13597 -- We do not analyze this renaming declaration, because all its
13598 -- components have already been analyzed, and if we were to go
13599 -- ahead and analyze it, we would in effect be trying to generate
13600 -- another declaration of X, which won't do.
13601
13602 Set_Renamed_Object (Def_Id, Nam);
13603 Set_Analyzed (N);
13604
13605 -- We do need to deal with debug issues for this renaming
13606
13607 -- First, if entity comes from source, then mark it as needing
13608 -- debug information, even though it is defined by a generated
13609 -- renaming that does not come from source.
13610
13611 Set_Debug_Info_Defining_Id (N);
13612
13613 -- Now call the routine to generate debug info for the renaming
13614
13615 Insert_Action (N, Debug_Renaming_Declaration (N));
13616 end Rewrite_Object_Declaration_As_Renaming;
13617
13618 ------------------------------------
13619 -- Safe_Unchecked_Type_Conversion --
13620 ------------------------------------
13621
13622 -- Note: this function knows quite a bit about the exact requirements of
13623 -- Gigi with respect to unchecked type conversions, and its code must be
13624 -- coordinated with any changes in Gigi in this area.
13625
13626 -- The above requirements should be documented in Sinfo ???
13627
13628 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
13629 Pexp : constant Node_Id := Parent (Exp);
13630
13631 Otyp : Entity_Id;
13632 Ityp : Entity_Id;
13633 Oalign : Uint;
13634 Ialign : Uint;
13635
13636 begin
13637 -- If the expression is the RHS of an assignment or object declaration
13638 -- we are always OK because there will always be a target.
13639
13640 -- Object renaming declarations, (generated for view conversions of
13641 -- actuals in inlined calls), like object declarations, provide an
13642 -- explicit type, and are safe as well.
13643
13644 if (Nkind (Pexp) = N_Assignment_Statement
13645 and then Expression (Pexp) = Exp)
13646 or else Nkind (Pexp)
13647 in N_Object_Declaration | N_Object_Renaming_Declaration
13648 then
13649 return True;
13650
13651 -- If the expression is the prefix of an N_Selected_Component we should
13652 -- also be OK because GCC knows to look inside the conversion.
13653
13654 elsif Nkind (Pexp) = N_Selected_Component
13655 and then Prefix (Pexp) = Exp
13656 then
13657 return True;
13658 end if;
13659
13660 -- Set the output type, this comes from Etype if it is set, otherwise we
13661 -- take it from the subtype mark, which we assume was already fully
13662 -- analyzed.
13663
13664 if Present (Etype (Exp)) then
13665 Otyp := Etype (Exp);
13666 else
13667 Otyp := Entity (Subtype_Mark (Exp));
13668 end if;
13669
13670 -- The input type always comes from the expression, and we assume this
13671 -- is indeed always analyzed, so we can simply get the Etype.
13672
13673 Ityp := Etype (Expression (Exp));
13674
13675 -- Initialize alignments to unknown so far
13676
13677 Oalign := No_Uint;
13678 Ialign := No_Uint;
13679
13680 -- Replace a concurrent type by its corresponding record type and each
13681 -- type by its underlying type and do the tests on those. The original
13682 -- type may be a private type whose completion is a concurrent type, so
13683 -- find the underlying type first.
13684
13685 if Present (Underlying_Type (Otyp)) then
13686 Otyp := Underlying_Type (Otyp);
13687 end if;
13688
13689 if Present (Underlying_Type (Ityp)) then
13690 Ityp := Underlying_Type (Ityp);
13691 end if;
13692
13693 if Is_Concurrent_Type (Otyp) then
13694 Otyp := Corresponding_Record_Type (Otyp);
13695 end if;
13696
13697 if Is_Concurrent_Type (Ityp) then
13698 Ityp := Corresponding_Record_Type (Ityp);
13699 end if;
13700
13701 -- If the base types are the same, we know there is no problem since
13702 -- this conversion will be a noop.
13703
13704 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
13705 return True;
13706
13707 -- Same if this is an upwards conversion of an untagged type, and there
13708 -- are no constraints involved (could be more general???)
13709
13710 elsif Etype (Ityp) = Otyp
13711 and then not Is_Tagged_Type (Ityp)
13712 and then not Has_Discriminants (Ityp)
13713 and then No (First_Rep_Item (Base_Type (Ityp)))
13714 then
13715 return True;
13716
13717 -- If the expression has an access type (object or subprogram) we assume
13718 -- that the conversion is safe, because the size of the target is safe,
13719 -- even if it is a record (which might be treated as having unknown size
13720 -- at this point).
13721
13722 elsif Is_Access_Type (Ityp) then
13723 return True;
13724
13725 -- If the size of output type is known at compile time, there is never
13726 -- a problem. Note that unconstrained records are considered to be of
13727 -- known size, but we can't consider them that way here, because we are
13728 -- talking about the actual size of the object.
13729
13730 elsif Size_Known_At_Compile_Time (Otyp)
13731 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
13732 then
13733 return True;
13734
13735 -- If either type is tagged, then we know the alignment is OK so Gigi
13736 -- will be able to use pointer punning.
13737
13738 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
13739 return True;
13740
13741 -- If either type is a limited record type, we cannot do a copy, so say
13742 -- safe since there's nothing else we can do.
13743
13744 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
13745 return True;
13746
13747 -- Conversions to and from packed array types are always ignored and
13748 -- hence are safe.
13749
13750 elsif Is_Packed_Array_Impl_Type (Otyp)
13751 or else Is_Packed_Array_Impl_Type (Ityp)
13752 then
13753 return True;
13754 end if;
13755
13756 -- The only other cases known to be safe is if the input type's
13757 -- alignment is known to be at least the maximum alignment for the
13758 -- target or if both alignments are known and the output type's
13759 -- alignment is no stricter than the input's. We can use the component
13760 -- type alignment for an array if a type is an unpacked array type.
13761
13762 if Present (Alignment_Clause (Otyp)) then
13763 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
13764
13765 elsif Is_Array_Type (Otyp)
13766 and then Present (Alignment_Clause (Component_Type (Otyp)))
13767 then
13768 Oalign := Expr_Value (Expression (Alignment_Clause
13769 (Component_Type (Otyp))));
13770 end if;
13771
13772 if Present (Alignment_Clause (Ityp)) then
13773 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
13774
13775 elsif Is_Array_Type (Ityp)
13776 and then Present (Alignment_Clause (Component_Type (Ityp)))
13777 then
13778 Ialign := Expr_Value (Expression (Alignment_Clause
13779 (Component_Type (Ityp))));
13780 end if;
13781
13782 if Present (Ialign) and then Ialign > Maximum_Alignment then
13783 return True;
13784
13785 elsif Present (Ialign)
13786 and then Present (Oalign)
13787 and then Ialign <= Oalign
13788 then
13789 return True;
13790
13791 -- Otherwise, Gigi cannot handle this and we must make a temporary
13792
13793 else
13794 return False;
13795 end if;
13796 end Safe_Unchecked_Type_Conversion;
13797
13798 ---------------------------------
13799 -- Set_Current_Value_Condition --
13800 ---------------------------------
13801
13802 -- Note: the implementation of this procedure is very closely tied to the
13803 -- implementation of Get_Current_Value_Condition. Here we set required
13804 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
13805 -- them, so they must have a consistent view.
13806
13807 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
13808
13809 procedure Set_Entity_Current_Value (N : Node_Id);
13810 -- If N is an entity reference, where the entity is of an appropriate
13811 -- kind, then set the current value of this entity to Cnode, unless
13812 -- there is already a definite value set there.
13813
13814 procedure Set_Expression_Current_Value (N : Node_Id);
13815 -- If N is of an appropriate form, sets an appropriate entry in current
13816 -- value fields of relevant entities. Multiple entities can be affected
13817 -- in the case of an AND or AND THEN.
13818
13819 ------------------------------
13820 -- Set_Entity_Current_Value --
13821 ------------------------------
13822
13823 procedure Set_Entity_Current_Value (N : Node_Id) is
13824 begin
13825 if Is_Entity_Name (N) then
13826 declare
13827 Ent : constant Entity_Id := Entity (N);
13828
13829 begin
13830 -- Don't capture if not safe to do so
13831
13832 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
13833 return;
13834 end if;
13835
13836 -- Here we have a case where the Current_Value field may need
13837 -- to be set. We set it if it is not already set to a compile
13838 -- time expression value.
13839
13840 -- Note that this represents a decision that one condition
13841 -- blots out another previous one. That's certainly right if
13842 -- they occur at the same level. If the second one is nested,
13843 -- then the decision is neither right nor wrong (it would be
13844 -- equally OK to leave the outer one in place, or take the new
13845 -- inner one). Really we should record both, but our data
13846 -- structures are not that elaborate.
13847
13848 if Nkind (Current_Value (Ent)) not in N_Subexpr then
13849 Set_Current_Value (Ent, Cnode);
13850 end if;
13851 end;
13852 end if;
13853 end Set_Entity_Current_Value;
13854
13855 ----------------------------------
13856 -- Set_Expression_Current_Value --
13857 ----------------------------------
13858
13859 procedure Set_Expression_Current_Value (N : Node_Id) is
13860 Cond : Node_Id;
13861
13862 begin
13863 Cond := N;
13864
13865 -- Loop to deal with (ignore for now) any NOT operators present. The
13866 -- presence of NOT operators will be handled properly when we call
13867 -- Get_Current_Value_Condition.
13868
13869 while Nkind (Cond) = N_Op_Not loop
13870 Cond := Right_Opnd (Cond);
13871 end loop;
13872
13873 -- For an AND or AND THEN, recursively process operands
13874
13875 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
13876 Set_Expression_Current_Value (Left_Opnd (Cond));
13877 Set_Expression_Current_Value (Right_Opnd (Cond));
13878 return;
13879 end if;
13880
13881 -- Check possible relational operator
13882
13883 if Nkind (Cond) in N_Op_Compare then
13884 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
13885 Set_Entity_Current_Value (Left_Opnd (Cond));
13886 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
13887 Set_Entity_Current_Value (Right_Opnd (Cond));
13888 end if;
13889
13890 elsif Nkind (Cond) in N_Type_Conversion
13891 | N_Qualified_Expression
13892 | N_Expression_With_Actions
13893 then
13894 Set_Expression_Current_Value (Expression (Cond));
13895
13896 -- Check possible boolean variable reference
13897
13898 else
13899 Set_Entity_Current_Value (Cond);
13900 end if;
13901 end Set_Expression_Current_Value;
13902
13903 -- Start of processing for Set_Current_Value_Condition
13904
13905 begin
13906 Set_Expression_Current_Value (Condition (Cnode));
13907 end Set_Current_Value_Condition;
13908
13909 --------------------------
13910 -- Set_Elaboration_Flag --
13911 --------------------------
13912
13913 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
13914 Loc : constant Source_Ptr := Sloc (N);
13915 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
13916 Asn : Node_Id;
13917
13918 begin
13919 if Present (Ent) then
13920
13921 -- Nothing to do if at the compilation unit level, because in this
13922 -- case the flag is set by the binder generated elaboration routine.
13923
13924 if Nkind (Parent (N)) = N_Compilation_Unit then
13925 null;
13926
13927 -- Here we do need to generate an assignment statement
13928
13929 else
13930 Check_Restriction (No_Elaboration_Code, N);
13931
13932 Asn :=
13933 Make_Assignment_Statement (Loc,
13934 Name => New_Occurrence_Of (Ent, Loc),
13935 Expression => Make_Integer_Literal (Loc, Uint_1));
13936
13937 -- Mark the assignment statement as elaboration code. This allows
13938 -- the early call region mechanism (see Sem_Elab) to properly
13939 -- ignore such assignments even though they are nonpreelaborable
13940 -- code.
13941
13942 Set_Is_Elaboration_Code (Asn);
13943
13944 if Nkind (Parent (N)) = N_Subunit then
13945 Insert_After (Corresponding_Stub (Parent (N)), Asn);
13946 else
13947 Insert_After (N, Asn);
13948 end if;
13949
13950 Analyze (Asn);
13951
13952 -- Kill current value indication. This is necessary because the
13953 -- tests of this flag are inserted out of sequence and must not
13954 -- pick up bogus indications of the wrong constant value.
13955
13956 Set_Current_Value (Ent, Empty);
13957
13958 -- If the subprogram is in the current declarative part and
13959 -- 'access has been applied to it, generate an elaboration
13960 -- check at the beginning of the declarations of the body.
13961
13962 if Nkind (N) = N_Subprogram_Body
13963 and then Address_Taken (Spec_Id)
13964 and then
13965 Ekind (Scope (Spec_Id)) in E_Block | E_Procedure | E_Function
13966 then
13967 declare
13968 Loc : constant Source_Ptr := Sloc (N);
13969 Decls : constant List_Id := Declarations (N);
13970 Chk : Node_Id;
13971
13972 begin
13973 -- No need to generate this check if first entry in the
13974 -- declaration list is a raise of Program_Error now.
13975
13976 if Present (Decls)
13977 and then Nkind (First (Decls)) = N_Raise_Program_Error
13978 then
13979 return;
13980 end if;
13981
13982 -- Otherwise generate the check
13983
13984 Chk :=
13985 Make_Raise_Program_Error (Loc,
13986 Condition =>
13987 Make_Op_Eq (Loc,
13988 Left_Opnd => New_Occurrence_Of (Ent, Loc),
13989 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
13990 Reason => PE_Access_Before_Elaboration);
13991
13992 if No (Decls) then
13993 Set_Declarations (N, New_List (Chk));
13994 else
13995 Prepend (Chk, Decls);
13996 end if;
13997
13998 Analyze (Chk);
13999 end;
14000 end if;
14001 end if;
14002 end if;
14003 end Set_Elaboration_Flag;
14004
14005 ----------------------------
14006 -- Set_Renamed_Subprogram --
14007 ----------------------------
14008
14009 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
14010 begin
14011 -- If input node is an identifier, we can just reset it
14012
14013 if Nkind (N) = N_Identifier then
14014 Set_Chars (N, Chars (E));
14015 Set_Entity (N, E);
14016
14017 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
14018
14019 else
14020 declare
14021 CS : constant Boolean := Comes_From_Source (N);
14022 begin
14023 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
14024 Set_Entity (N, E);
14025 Set_Comes_From_Source (N, CS);
14026 Set_Analyzed (N, True);
14027 end;
14028 end if;
14029 end Set_Renamed_Subprogram;
14030
14031 ----------------------
14032 -- Side_Effect_Free --
14033 ----------------------
14034
14035 function Side_Effect_Free
14036 (N : Node_Id;
14037 Name_Req : Boolean := False;
14038 Variable_Ref : Boolean := False) return Boolean
14039 is
14040 Typ : constant Entity_Id := Etype (N);
14041 -- Result type of the expression
14042
14043 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
14044 -- The argument N is a construct where the Prefix is dereferenced if it
14045 -- is an access type and the result is a variable. The call returns True
14046 -- if the construct is side-effect-free (not considering side effects in
14047 -- other than the prefix which are to be tested by the caller).
14048
14049 function Within_In_Parameter (N : Node_Id) return Boolean;
14050 -- Determines if N is a subcomponent of a composite in-parameter. If so,
14051 -- N is not side-effect-free when the actual is global and modifiable
14052 -- indirectly from within a subprogram, because it may be passed by
14053 -- reference. The front-end must be conservative here and assume that
14054 -- this may happen with any array or record type. On the other hand, we
14055 -- cannot create temporaries for all expressions for which this
14056 -- condition is true, for various reasons that might require clearing up
14057 -- ??? For example, discriminant references that appear out of place, or
14058 -- spurious type errors with class-wide expressions. As a result, we
14059 -- limit the transformation to loop bounds, which is so far the only
14060 -- case that requires it.
14061
14062 -----------------------------
14063 -- Safe_Prefixed_Reference --
14064 -----------------------------
14065
14066 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
14067 begin
14068 -- If prefix is not side-effect-free, definitely not safe
14069
14070 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
14071 return False;
14072
14073 -- If the prefix is of an access type that is not access-to-constant,
14074 -- then this construct is a variable reference, which means it is to
14075 -- be considered to have side effects if Variable_Ref is set True.
14076
14077 elsif Is_Access_Type (Etype (Prefix (N)))
14078 and then not Is_Access_Constant (Etype (Prefix (N)))
14079 and then Variable_Ref
14080 then
14081 -- Exception is a prefix that is the result of a previous removal
14082 -- of side effects.
14083
14084 return Is_Entity_Name (Prefix (N))
14085 and then not Comes_From_Source (Prefix (N))
14086 and then Ekind (Entity (Prefix (N))) = E_Constant
14087 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
14088
14089 -- If the prefix is an explicit dereference then this construct is a
14090 -- variable reference, which means it is to be considered to have
14091 -- side effects if Variable_Ref is True.
14092
14093 -- We do NOT exclude dereferences of access-to-constant types because
14094 -- we handle them as constant view of variables.
14095
14096 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
14097 and then Variable_Ref
14098 then
14099 return False;
14100
14101 -- Note: The following test is the simplest way of solving a complex
14102 -- problem uncovered by the following test (Side effect on loop bound
14103 -- that is a subcomponent of a global variable:
14104
14105 -- with Text_Io; use Text_Io;
14106 -- procedure Tloop is
14107 -- type X is
14108 -- record
14109 -- V : Natural := 4;
14110 -- S : String (1..5) := (others => 'a');
14111 -- end record;
14112 -- X1 : X;
14113
14114 -- procedure Modi;
14115
14116 -- generic
14117 -- with procedure Action;
14118 -- procedure Loop_G (Arg : X; Msg : String)
14119
14120 -- procedure Loop_G (Arg : X; Msg : String) is
14121 -- begin
14122 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
14123 -- & Natural'Image (Arg.V));
14124 -- for Index in 1 .. Arg.V loop
14125 -- Text_Io.Put_Line
14126 -- (Natural'Image (Index) & " " & Arg.S (Index));
14127 -- if Index > 2 then
14128 -- Modi;
14129 -- end if;
14130 -- end loop;
14131 -- Put_Line ("end loop_g " & Msg);
14132 -- end;
14133
14134 -- procedure Loop1 is new Loop_G (Modi);
14135 -- procedure Modi is
14136 -- begin
14137 -- X1.V := 1;
14138 -- Loop1 (X1, "from modi");
14139 -- end;
14140 --
14141 -- begin
14142 -- Loop1 (X1, "initial");
14143 -- end;
14144
14145 -- The output of the above program should be:
14146
14147 -- begin loop_g initial will loop till: 4
14148 -- 1 a
14149 -- 2 a
14150 -- 3 a
14151 -- begin loop_g from modi will loop till: 1
14152 -- 1 a
14153 -- end loop_g from modi
14154 -- 4 a
14155 -- begin loop_g from modi will loop till: 1
14156 -- 1 a
14157 -- end loop_g from modi
14158 -- end loop_g initial
14159
14160 -- If a loop bound is a subcomponent of a global variable, a
14161 -- modification of that variable within the loop may incorrectly
14162 -- affect the execution of the loop.
14163
14164 elsif Parent_Kind (Parent (N)) = N_Loop_Parameter_Specification
14165 and then Within_In_Parameter (Prefix (N))
14166 and then Variable_Ref
14167 then
14168 return False;
14169
14170 -- All other cases are side-effect-free
14171
14172 else
14173 return True;
14174 end if;
14175 end Safe_Prefixed_Reference;
14176
14177 -------------------------
14178 -- Within_In_Parameter --
14179 -------------------------
14180
14181 function Within_In_Parameter (N : Node_Id) return Boolean is
14182 begin
14183 if not Comes_From_Source (N) then
14184 return False;
14185
14186 elsif Is_Entity_Name (N) then
14187 return Ekind (Entity (N)) = E_In_Parameter;
14188
14189 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
14190 return Within_In_Parameter (Prefix (N));
14191
14192 else
14193 return False;
14194 end if;
14195 end Within_In_Parameter;
14196
14197 -- Start of processing for Side_Effect_Free
14198
14199 begin
14200 -- If volatile reference, always consider it to have side effects
14201
14202 if Is_Volatile_Reference (N) then
14203 return False;
14204 end if;
14205
14206 -- Note on checks that could raise Constraint_Error. Strictly, if we
14207 -- take advantage of 11.6, these checks do not count as side effects.
14208 -- However, we would prefer to consider that they are side effects,
14209 -- since the back end CSE does not work very well on expressions which
14210 -- can raise Constraint_Error. On the other hand if we don't consider
14211 -- them to be side-effect-free, then we get some awkward expansions
14212 -- in -gnato mode, resulting in code insertions at a point where we
14213 -- do not have a clear model for performing the insertions.
14214
14215 -- Special handling for entity names
14216
14217 if Is_Entity_Name (N) then
14218
14219 -- A type reference is always side-effect-free
14220
14221 if Is_Type (Entity (N)) then
14222 return True;
14223
14224 -- Variables are considered to be a side effect if Variable_Ref
14225 -- is set or if we have a volatile reference and Name_Req is off.
14226 -- If Name_Req is True then we can't help returning a name which
14227 -- effectively allows multiple references in any case.
14228
14229 elsif Is_Variable (N, Use_Original_Node => False) then
14230 return not Variable_Ref
14231 and then (not Is_Volatile_Reference (N) or else Name_Req);
14232
14233 -- Any other entity (e.g. a subtype name) is definitely side
14234 -- effect free.
14235
14236 else
14237 return True;
14238 end if;
14239
14240 -- A value known at compile time is always side-effect-free
14241
14242 elsif Compile_Time_Known_Value (N) then
14243 return True;
14244
14245 -- A variable renaming is not side-effect-free, because the renaming
14246 -- will function like a macro in the front-end in some cases, and an
14247 -- assignment can modify the component designated by N, so we need to
14248 -- create a temporary for it.
14249
14250 -- The guard testing for Entity being present is needed at least in
14251 -- the case of rewritten predicate expressions, and may well also be
14252 -- appropriate elsewhere. Obviously we can't go testing the entity
14253 -- field if it does not exist, so it's reasonable to say that this is
14254 -- not the renaming case if it does not exist.
14255
14256 elsif Is_Entity_Name (Original_Node (N))
14257 and then Present (Entity (Original_Node (N)))
14258 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
14259 and then Ekind (Entity (Original_Node (N))) /= E_Constant
14260 then
14261 declare
14262 RO : constant Node_Id :=
14263 Renamed_Object (Entity (Original_Node (N)));
14264
14265 begin
14266 -- If the renamed object is an indexed component, or an
14267 -- explicit dereference, then the designated object could
14268 -- be modified by an assignment.
14269
14270 if Nkind (RO) in N_Indexed_Component | N_Explicit_Dereference then
14271 return False;
14272
14273 -- A selected component must have a safe prefix
14274
14275 elsif Nkind (RO) = N_Selected_Component then
14276 return Safe_Prefixed_Reference (RO);
14277
14278 -- In all other cases, designated object cannot be changed so
14279 -- we are side-effect-free.
14280
14281 else
14282 return True;
14283 end if;
14284 end;
14285
14286 -- Remove_Side_Effects generates an object renaming declaration to
14287 -- capture the expression of a class-wide expression. In VM targets
14288 -- the frontend performs no expansion for dispatching calls to
14289 -- class- wide types since they are handled by the VM. Hence, we must
14290 -- locate here if this node corresponds to a previous invocation of
14291 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
14292
14293 elsif not Tagged_Type_Expansion
14294 and then not Comes_From_Source (N)
14295 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
14296 and then Is_Class_Wide_Type (Typ)
14297 then
14298 return True;
14299 end if;
14300
14301 -- For other than entity names and compile time known values,
14302 -- check the node kind for special processing.
14303
14304 case Nkind (N) is
14305
14306 -- An attribute reference is side-effect-free if its expressions
14307 -- are side-effect-free and its prefix is side-effect-free or is
14308 -- an entity reference.
14309
14310 when N_Attribute_Reference =>
14311 return Side_Effect_Free_Attribute (Attribute_Name (N))
14312 and then
14313 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
14314 and then
14315 (Is_Entity_Name (Prefix (N))
14316 or else
14317 Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref));
14318
14319 -- A binary operator is side-effect-free if and both operands are
14320 -- side-effect-free. For this purpose binary operators include
14321 -- short circuit forms.
14322
14323 when N_Binary_Op
14324 | N_Short_Circuit
14325 =>
14326 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
14327 and then
14328 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
14329
14330 -- Membership tests may have either Right_Opnd or Alternatives set
14331
14332 when N_Membership_Test =>
14333 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
14334 and then
14335 (if Present (Right_Opnd (N))
14336 then Side_Effect_Free
14337 (Right_Opnd (N), Name_Req, Variable_Ref)
14338 else Side_Effect_Free
14339 (Alternatives (N), Name_Req, Variable_Ref));
14340
14341 -- An explicit dereference is side-effect-free only if it is
14342 -- a side-effect-free prefixed reference.
14343
14344 when N_Explicit_Dereference =>
14345 return Safe_Prefixed_Reference (N);
14346
14347 -- An expression with action is side-effect-free if its expression
14348 -- is side-effect-free and it has no actions.
14349
14350 when N_Expression_With_Actions =>
14351 return
14352 Is_Empty_List (Actions (N))
14353 and then Side_Effect_Free
14354 (Expression (N), Name_Req, Variable_Ref);
14355
14356 -- A call to _rep_to_pos is side-effect-free, since we generate
14357 -- this pure function call ourselves. Moreover it is critically
14358 -- important to make this exception, since otherwise we can have
14359 -- discriminants in array components which don't look side-effect
14360 -- free in the case of an array whose index type is an enumeration
14361 -- type with an enumeration rep clause.
14362
14363 -- All other function calls are not side-effect-free
14364
14365 when N_Function_Call =>
14366 return
14367 Nkind (Name (N)) = N_Identifier
14368 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
14369 and then Side_Effect_Free
14370 (First (Parameter_Associations (N)),
14371 Name_Req, Variable_Ref);
14372
14373 -- An IF expression is side-effect-free if it's of a scalar type, and
14374 -- all its components are all side-effect-free (conditions and then
14375 -- actions and else actions). We restrict to scalar types, since it
14376 -- is annoying to deal with things like (if A then B else C)'First
14377 -- where the type involved is a string type.
14378
14379 when N_If_Expression =>
14380 return
14381 Is_Scalar_Type (Typ)
14382 and then Side_Effect_Free
14383 (Expressions (N), Name_Req, Variable_Ref);
14384
14385 -- An indexed component is side-effect-free if it is a side
14386 -- effect free prefixed reference and all the indexing
14387 -- expressions are side-effect-free.
14388
14389 when N_Indexed_Component =>
14390 return
14391 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
14392 and then Safe_Prefixed_Reference (N);
14393
14394 -- A type qualification, type conversion, or unchecked expression is
14395 -- side-effect-free if the expression is side-effect-free.
14396
14397 when N_Qualified_Expression
14398 | N_Type_Conversion
14399 =>
14400 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
14401
14402 -- A selected component is side-effect-free only if it is a side
14403 -- effect free prefixed reference.
14404
14405 when N_Selected_Component =>
14406 return Safe_Prefixed_Reference (N);
14407
14408 -- A range is side-effect-free if the bounds are side-effect-free
14409
14410 when N_Range =>
14411 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
14412 and then
14413 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
14414
14415 -- A slice is side-effect-free if it is a side-effect-free
14416 -- prefixed reference and the bounds are side-effect-free.
14417
14418 when N_Slice =>
14419 return
14420 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
14421 and then Safe_Prefixed_Reference (N);
14422
14423 -- A unary operator is side-effect-free if the operand
14424 -- is side-effect-free.
14425
14426 when N_Unary_Op =>
14427 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
14428
14429 -- An unchecked type conversion is side-effect-free only if it
14430 -- is safe and its argument is side-effect-free.
14431
14432 when N_Unchecked_Type_Conversion =>
14433 return
14434 Safe_Unchecked_Type_Conversion (N)
14435 and then Side_Effect_Free
14436 (Expression (N), Name_Req, Variable_Ref);
14437
14438 -- A literal is side-effect-free
14439
14440 when N_Character_Literal
14441 | N_Integer_Literal
14442 | N_Real_Literal
14443 | N_String_Literal
14444 =>
14445 return True;
14446
14447 -- An aggregate is side-effect-free if all its values are compile
14448 -- time known.
14449
14450 when N_Aggregate =>
14451 return Compile_Time_Known_Aggregate (N);
14452
14453 -- A reference is side-effect-free
14454
14455 when N_Reference =>
14456 return True;
14457
14458 -- We consider that anything else has side effects. This is a bit
14459 -- crude, but we are pretty close for most common cases, and we
14460 -- are certainly correct (i.e. we never return True when the
14461 -- answer should be False).
14462
14463 when others =>
14464 return False;
14465 end case;
14466 end Side_Effect_Free;
14467
14468 -- A list is side-effect-free if all elements of the list are side
14469 -- effect free.
14470
14471 function Side_Effect_Free
14472 (L : List_Id;
14473 Name_Req : Boolean := False;
14474 Variable_Ref : Boolean := False) return Boolean
14475 is
14476 N : Node_Id;
14477
14478 begin
14479 if L = No_List or else L = Error_List then
14480 return True;
14481
14482 else
14483 N := First (L);
14484 while Present (N) loop
14485 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
14486 return False;
14487 else
14488 Next (N);
14489 end if;
14490 end loop;
14491
14492 return True;
14493 end if;
14494 end Side_Effect_Free;
14495
14496 --------------------------------
14497 -- Side_Effect_Free_Attribute --
14498 --------------------------------
14499
14500 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean is
14501 begin
14502 case Name is
14503 when Name_Input =>
14504 return False;
14505
14506 when Name_Image
14507 | Name_Img
14508 | Name_Wide_Image
14509 | Name_Wide_Wide_Image
14510 =>
14511 -- CodePeer doesn't want to see replicated copies of 'Image calls
14512
14513 return not CodePeer_Mode;
14514
14515 when others =>
14516 return True;
14517 end case;
14518 end Side_Effect_Free_Attribute;
14519
14520 ----------------------------------
14521 -- Silly_Boolean_Array_Not_Test --
14522 ----------------------------------
14523
14524 -- This procedure implements an odd and silly test. We explicitly check
14525 -- for the case where the 'First of the component type is equal to the
14526 -- 'Last of this component type, and if this is the case, we make sure
14527 -- that constraint error is raised. The reason is that the NOT is bound
14528 -- to cause CE in this case, and we will not otherwise catch it.
14529
14530 -- No such check is required for AND and OR, since for both these cases
14531 -- False op False = False, and True op True = True. For the XOR case,
14532 -- see Silly_Boolean_Array_Xor_Test.
14533
14534 -- Believe it or not, this was reported as a bug. Note that nearly always,
14535 -- the test will evaluate statically to False, so the code will be
14536 -- statically removed, and no extra overhead caused.
14537
14538 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
14539 Loc : constant Source_Ptr := Sloc (N);
14540 CT : constant Entity_Id := Component_Type (T);
14541
14542 begin
14543 -- The check we install is
14544
14545 -- constraint_error when
14546 -- component_type'first = component_type'last
14547 -- and then array_type'Length /= 0)
14548
14549 -- We need the last guard because we don't want to raise CE for empty
14550 -- arrays since no out of range values result. (Empty arrays with a
14551 -- component type of True .. True -- very useful -- even the ACATS
14552 -- does not test that marginal case).
14553
14554 Insert_Action (N,
14555 Make_Raise_Constraint_Error (Loc,
14556 Condition =>
14557 Make_And_Then (Loc,
14558 Left_Opnd =>
14559 Make_Op_Eq (Loc,
14560 Left_Opnd =>
14561 Make_Attribute_Reference (Loc,
14562 Prefix => New_Occurrence_Of (CT, Loc),
14563 Attribute_Name => Name_First),
14564
14565 Right_Opnd =>
14566 Make_Attribute_Reference (Loc,
14567 Prefix => New_Occurrence_Of (CT, Loc),
14568 Attribute_Name => Name_Last)),
14569
14570 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
14571 Reason => CE_Range_Check_Failed));
14572 end Silly_Boolean_Array_Not_Test;
14573
14574 ----------------------------------
14575 -- Silly_Boolean_Array_Xor_Test --
14576 ----------------------------------
14577
14578 -- This procedure implements an odd and silly test. We explicitly check
14579 -- for the XOR case where the component type is True .. True, since this
14580 -- will raise constraint error. A special check is required since CE
14581 -- will not be generated otherwise (cf Expand_Packed_Not).
14582
14583 -- No such check is required for AND and OR, since for both these cases
14584 -- False op False = False, and True op True = True, and no check is
14585 -- required for the case of False .. False, since False xor False = False.
14586 -- See also Silly_Boolean_Array_Not_Test
14587
14588 procedure Silly_Boolean_Array_Xor_Test
14589 (N : Node_Id;
14590 R : Node_Id;
14591 T : Entity_Id)
14592 is
14593 Loc : constant Source_Ptr := Sloc (N);
14594 CT : constant Entity_Id := Component_Type (T);
14595
14596 begin
14597 -- The check we install is
14598
14599 -- constraint_error when
14600 -- Boolean (component_type'First)
14601 -- and then Boolean (component_type'Last)
14602 -- and then array_type'Length /= 0)
14603
14604 -- We need the last guard because we don't want to raise CE for empty
14605 -- arrays since no out of range values result (Empty arrays with a
14606 -- component type of True .. True -- very useful -- even the ACATS
14607 -- does not test that marginal case).
14608
14609 Insert_Action (N,
14610 Make_Raise_Constraint_Error (Loc,
14611 Condition =>
14612 Make_And_Then (Loc,
14613 Left_Opnd =>
14614 Make_And_Then (Loc,
14615 Left_Opnd =>
14616 Convert_To (Standard_Boolean,
14617 Make_Attribute_Reference (Loc,
14618 Prefix => New_Occurrence_Of (CT, Loc),
14619 Attribute_Name => Name_First)),
14620
14621 Right_Opnd =>
14622 Convert_To (Standard_Boolean,
14623 Make_Attribute_Reference (Loc,
14624 Prefix => New_Occurrence_Of (CT, Loc),
14625 Attribute_Name => Name_Last))),
14626
14627 Right_Opnd => Make_Non_Empty_Check (Loc, R)),
14628 Reason => CE_Range_Check_Failed));
14629 end Silly_Boolean_Array_Xor_Test;
14630
14631 ----------------------------
14632 -- Small_Integer_Type_For --
14633 ----------------------------
14634
14635 function Small_Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id
14636 is
14637 begin
14638 -- The only difference between this and Integer_Type_For is that this
14639 -- can return small (8- or 16-bit) types.
14640
14641 if S <= Standard_Short_Short_Integer_Size then
14642 if Uns then
14643 return Standard_Short_Short_Unsigned;
14644 else
14645 return Standard_Short_Short_Integer;
14646 end if;
14647
14648 elsif S <= Standard_Short_Integer_Size then
14649 if Uns then
14650 return Standard_Short_Unsigned;
14651 else
14652 return Standard_Short_Integer;
14653 end if;
14654
14655 else
14656 return Integer_Type_For (S, Uns);
14657 end if;
14658 end Small_Integer_Type_For;
14659
14660 ------------------
14661 -- Thunk_Target --
14662 ------------------
14663
14664 function Thunk_Target (Thunk : Entity_Id) return Entity_Id is
14665 Target : Entity_Id := Thunk;
14666
14667 begin
14668 pragma Assert (Is_Thunk (Thunk));
14669
14670 while Is_Thunk (Target) loop
14671 Target := Thunk_Entity (Target);
14672 end loop;
14673
14674 return Target;
14675 end Thunk_Target;
14676
14677 -----------------------
14678 -- Try_Inline_Always --
14679 -----------------------
14680
14681 function Try_Inline_Always (Subp : Entity_Id) return Boolean is
14682 ((Is_Expression_Function (Subp) or else Is_Finalizer (Subp))
14683 and then not Debug_Flag_Dot_8);
14684 -- We want to inline expression functions and finalizers as much as
14685 -- practical unless -gnatd.8.
14686
14687 -------------------
14688 -- Type_Map_Hash --
14689 -------------------
14690
14691 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
14692 begin
14693 return Type_Map_Header (Id mod Type_Map_Size);
14694 end Type_Map_Hash;
14695
14696 ------------------------------------------
14697 -- Type_May_Have_Bit_Aligned_Components --
14698 ------------------------------------------
14699
14700 function Type_May_Have_Bit_Aligned_Components
14701 (Typ : Entity_Id) return Boolean
14702 is
14703 begin
14704 -- Array type, check component type
14705
14706 if Is_Array_Type (Typ) then
14707 return
14708 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
14709
14710 -- Record type, check components
14711
14712 elsif Is_Record_Type (Typ) then
14713 declare
14714 E : Entity_Id;
14715
14716 begin
14717 E := First_Component_Or_Discriminant (Typ);
14718 while Present (E) loop
14719 -- This is the crucial test: if the component itself causes
14720 -- trouble, then we can stop and return True.
14721
14722 if Component_May_Be_Bit_Aligned (E) then
14723 return True;
14724 end if;
14725
14726 -- Otherwise, we need to test its type, to see if it may
14727 -- itself contain a troublesome component.
14728
14729 if Type_May_Have_Bit_Aligned_Components (Etype (E)) then
14730 return True;
14731 end if;
14732
14733 Next_Component_Or_Discriminant (E);
14734 end loop;
14735
14736 return False;
14737 end;
14738
14739 -- Type other than array or record is always OK
14740
14741 else
14742 return False;
14743 end if;
14744 end Type_May_Have_Bit_Aligned_Components;
14745
14746 ----------------------------------------------
14747 -- Unanalyze_Delayed_Conditional_Expression --
14748 ----------------------------------------------
14749
14750 procedure Unanalyze_Delayed_Conditional_Expression (N : Node_Id) is
14751 Expr : Node_Id := N;
14752
14753 begin
14754 -- Is_Delayed_Conditional_Expression looks through qualified expressions
14755 -- surrounding conditional expressions, so we need to reset the Analyzed
14756 -- flag on them as well.
14757
14758 loop
14759 Set_Analyzed (Expr, False);
14760
14761 exit when Nkind (Expr) in N_Case_Expression | N_If_Expression;
14762
14763 pragma Assert (Nkind (Expr) = N_Qualified_Expression);
14764 Expr := Expression (Expr);
14765 end loop;
14766 end Unanalyze_Delayed_Conditional_Expression;
14767
14768 --------------------------
14769 -- Unconditional_Parent --
14770 --------------------------
14771
14772 function Unconditional_Parent (N : Node_Id) return Node_Id is
14773 Node : Node_Id := N;
14774 Parent_Node : Node_Id := Parent (Node);
14775
14776 begin
14777 loop
14778 case Nkind (Parent_Node) is
14779 when N_Case_Expression_Alternative =>
14780 null;
14781
14782 when N_Case_Expression =>
14783 exit when Node = Expression (Parent_Node);
14784
14785 when N_If_Expression =>
14786 exit when Node = First (Expressions (Parent_Node));
14787
14788 when N_Qualified_Expression =>
14789 null;
14790
14791 when others =>
14792 exit;
14793 end case;
14794
14795 Node := Parent_Node;
14796 Parent_Node := Parent (Node);
14797 end loop;
14798
14799 return Parent_Node;
14800 end Unconditional_Parent;
14801
14802 -------------------------------
14803 -- Update_Primitives_Mapping --
14804 -------------------------------
14805
14806 procedure Update_Primitives_Mapping
14807 (Inher_Id : Entity_Id;
14808 Subp_Id : Entity_Id)
14809 is
14810 Parent_Type : constant Entity_Id := Find_Dispatching_Type (Inher_Id);
14811 Derived_Type : constant Entity_Id := Find_Dispatching_Type (Subp_Id);
14812
14813 begin
14814 pragma Assert (Parent_Type /= Derived_Type);
14815 Map_Types (Parent_Type, Derived_Type);
14816 end Update_Primitives_Mapping;
14817
14818 -----------------------------------
14819 -- Within_Conditional_Expression --
14820 -----------------------------------
14821
14822 function Within_Conditional_Expression (N : Node_Id) return Boolean is
14823 Nod : Node_Id;
14824 Par : Node_Id;
14825
14826 begin
14827 -- Locate an enclosing case or if expression. Note that these constructs
14828 -- can be rewritten as Expression_With_Actions nodes, hence the test of
14829 -- the original node. Moreover, we need to take into account conditional
14830 -- statements synthesized out of these expressions.
14831
14832 Nod := N;
14833 Par := Parent (Nod);
14834
14835 while Present (Par) loop
14836 if Nkind (Original_Node (Par)) = N_Case_Expression
14837 and then Nod /= Expression (Original_Node (Par))
14838 then
14839 return True;
14840
14841 elsif Nkind (Original_Node (Par)) = N_If_Expression
14842 and then Nod /= First (Expressions (Original_Node (Par)))
14843 then
14844 return True;
14845
14846 elsif Nkind (Par) = N_Case_Statement
14847 and then From_Conditional_Expression (Par)
14848 and then Nod /= Expression (Par)
14849 then
14850 return True;
14851
14852 elsif Nkind (Par) = N_If_Statement
14853 and then From_Conditional_Expression (Par)
14854 and then Nod /= Condition (Par)
14855 then
14856 return True;
14857
14858 -- Stop at contexts where temporaries may be contained
14859
14860 elsif Nkind (Par) in N_Aggregate
14861 | N_Delta_Aggregate
14862 | N_Extension_Aggregate
14863 | N_Block_Statement
14864 | N_Loop_Statement
14865 then
14866 return False;
14867
14868 -- Prevent the search from going too far
14869
14870 elsif Is_Body_Or_Package_Declaration (Par) then
14871 return False;
14872 end if;
14873
14874 Nod := Par;
14875 Par := Parent (Nod);
14876 end loop;
14877
14878 return False;
14879 end Within_Conditional_Expression;
14880
14881 ------------------------------
14882 -- Predicate_Check_In_Scope --
14883 ------------------------------
14884
14885 function Predicate_Check_In_Scope (N : Node_Id) return Boolean is
14886 S : Entity_Id;
14887
14888 begin
14889 S := Current_Scope;
14890 while Present (S) and then not Is_Subprogram (S) loop
14891 S := Scope (S);
14892 end loop;
14893
14894 if Present (S) then
14895
14896 -- Predicate checks should only be enabled in init procs for
14897 -- expressions coming from source.
14898
14899 if Is_Init_Proc (S) then
14900 return Comes_From_Source (N);
14901
14902 elsif Get_TSS_Name (S) /= TSS_Null
14903 and then not Is_Predicate_Function (S)
14904 then
14905 return False;
14906 end if;
14907 end if;
14908
14909 return True;
14910 end Predicate_Check_In_Scope;
14911
14912 end Exp_Util;