OSDN Git Service

2010-06-22 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_util.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             E X P _ U T I L                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Debug;    use Debug;
29 with Einfo;    use Einfo;
30 with Elists;   use Elists;
31 with Errout;   use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Ch6;  use Exp_Ch6;
34 with Exp_Ch7;  use Exp_Ch7;
35 with Inline;   use Inline;
36 with Itypes;   use Itypes;
37 with Lib;      use Lib;
38 with Nlists;   use Nlists;
39 with Nmake;    use Nmake;
40 with Opt;      use Opt;
41 with Restrict; use Restrict;
42 with Rident;   use Rident;
43 with Sem;      use Sem;
44 with Sem_Aux;  use Sem_Aux;
45 with Sem_Ch8;  use Sem_Ch8;
46 with Sem_SCIL; use Sem_SCIL;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Res;  use Sem_Res;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Snames;   use Snames;
52 with Stand;    use Stand;
53 with Stringt;  use Stringt;
54 with Targparm; use Targparm;
55 with Tbuild;   use Tbuild;
56 with Ttypes;   use Ttypes;
57 with Uintp;    use Uintp;
58 with Urealp;   use Urealp;
59 with Validsw;  use Validsw;
60
61 package body Exp_Util is
62
63    -----------------------
64    -- Local Subprograms --
65    -----------------------
66
67    function Build_Task_Array_Image
68      (Loc    : Source_Ptr;
69       Id_Ref : Node_Id;
70       A_Type : Entity_Id;
71       Dyn    : Boolean := False) return Node_Id;
72    --  Build function to generate the image string for a task that is an
73    --  array component, concatenating the images of each index. To avoid
74    --  storage leaks, the string is built with successive slice assignments.
75    --  The flag Dyn indicates whether this is called for the initialization
76    --  procedure of an array of tasks, or for the name of a dynamically
77    --  created task that is assigned to an indexed component.
78
79    function Build_Task_Image_Function
80      (Loc   : Source_Ptr;
81       Decls : List_Id;
82       Stats : List_Id;
83       Res   : Entity_Id) return Node_Id;
84    --  Common processing for Task_Array_Image and Task_Record_Image.
85    --  Build function body that computes image.
86
87    procedure Build_Task_Image_Prefix
88       (Loc    : Source_Ptr;
89        Len    : out Entity_Id;
90        Res    : out Entity_Id;
91        Pos    : out Entity_Id;
92        Prefix : Entity_Id;
93        Sum    : Node_Id;
94        Decls  : List_Id;
95        Stats  : List_Id);
96    --  Common processing for Task_Array_Image and Task_Record_Image.
97    --  Create local variables and assign prefix of name to result string.
98
99    function Build_Task_Record_Image
100      (Loc    : Source_Ptr;
101       Id_Ref : Node_Id;
102       Dyn    : Boolean := False) return Node_Id;
103    --  Build function to generate the image string for a task that is a
104    --  record component. Concatenate name of variable with that of selector.
105    --  The flag Dyn indicates whether this is called for the initialization
106    --  procedure of record with task components, or for a dynamically
107    --  created task that is assigned to a selected component.
108
109    function Make_CW_Equivalent_Type
110      (T : Entity_Id;
111       E : Node_Id) return Entity_Id;
112    --  T is a class-wide type entity, E is the initial expression node that
113    --  constrains T in case such as: " X: T := E" or "new T'(E)"
114    --  This function returns the entity of the Equivalent type and inserts
115    --  on the fly the necessary declaration such as:
116    --
117    --    type anon is record
118    --       _parent : Root_Type (T); constrained with E discriminants (if any)
119    --       Extension : String (1 .. expr to match size of E);
120    --    end record;
121    --
122    --  This record is compatible with any object of the class of T thanks
123    --  to the first field and has the same size as E thanks to the second.
124
125    function Make_Literal_Range
126      (Loc         : Source_Ptr;
127       Literal_Typ : Entity_Id) return Node_Id;
128    --  Produce a Range node whose bounds are:
129    --    Low_Bound (Literal_Type) ..
130    --        Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
131    --  this is used for expanding declarations like X : String := "sdfgdfg";
132    --
133    --  If the index type of the target array is not integer, we generate:
134    --     Low_Bound (Literal_Type) ..
135    --        Literal_Type'Val
136    --          (Literal_Type'Pos (Low_Bound (Literal_Type))
137    --             + (Length (Literal_Typ) -1))
138
139    function Make_Non_Empty_Check
140      (Loc : Source_Ptr;
141       N   : Node_Id) return Node_Id;
142    --  Produce a boolean expression checking that the unidimensional array
143    --  node N is not empty.
144
145    function New_Class_Wide_Subtype
146      (CW_Typ : Entity_Id;
147       N      : Node_Id) return Entity_Id;
148    --  Create an implicit subtype of CW_Typ attached to node N
149
150    ----------------------
151    -- Adjust_Condition --
152    ----------------------
153
154    procedure Adjust_Condition (N : Node_Id) is
155    begin
156       if No (N) then
157          return;
158       end if;
159
160       declare
161          Loc : constant Source_Ptr := Sloc (N);
162          T   : constant Entity_Id  := Etype (N);
163          Ti  : Entity_Id;
164
165       begin
166          --  For now, we simply ignore a call where the argument has no
167          --  type (probably case of unanalyzed condition), or has a type
168          --  that is not Boolean. This is because this is a pretty marginal
169          --  piece of functionality, and violations of these rules are
170          --  likely to be truly marginal (how much code uses Fortran Logical
171          --  as the barrier to a protected entry?) and we do not want to
172          --  blow up existing programs. We can change this to an assertion
173          --  after 3.12a is released ???
174
175          if No (T) or else not Is_Boolean_Type (T) then
176             return;
177          end if;
178
179          --  Apply validity checking if needed
180
181          if Validity_Checks_On and Validity_Check_Tests then
182             Ensure_Valid (N);
183          end if;
184
185          --  Immediate return if standard boolean, the most common case,
186          --  where nothing needs to be done.
187
188          if Base_Type (T) = Standard_Boolean then
189             return;
190          end if;
191
192          --  Case of zero/non-zero semantics or non-standard enumeration
193          --  representation. In each case, we rewrite the node as:
194
195          --      ityp!(N) /= False'Enum_Rep
196
197          --  where ityp is an integer type with large enough size to hold
198          --  any value of type T.
199
200          if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
201             if Esize (T) <= Esize (Standard_Integer) then
202                Ti := Standard_Integer;
203             else
204                Ti := Standard_Long_Long_Integer;
205             end if;
206
207             Rewrite (N,
208               Make_Op_Ne (Loc,
209                 Left_Opnd  => Unchecked_Convert_To (Ti, N),
210                 Right_Opnd =>
211                   Make_Attribute_Reference (Loc,
212                     Attribute_Name => Name_Enum_Rep,
213                     Prefix         =>
214                       New_Occurrence_Of (First_Literal (T), Loc))));
215             Analyze_And_Resolve (N, Standard_Boolean);
216
217          else
218             Rewrite (N, Convert_To (Standard_Boolean, N));
219             Analyze_And_Resolve (N, Standard_Boolean);
220          end if;
221       end;
222    end Adjust_Condition;
223
224    ------------------------
225    -- Adjust_Result_Type --
226    ------------------------
227
228    procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
229    begin
230       --  Ignore call if current type is not Standard.Boolean
231
232       if Etype (N) /= Standard_Boolean then
233          return;
234       end if;
235
236       --  If result is already of correct type, nothing to do. Note that
237       --  this will get the most common case where everything has a type
238       --  of Standard.Boolean.
239
240       if Base_Type (T) = Standard_Boolean then
241          return;
242
243       else
244          declare
245             KP : constant Node_Kind := Nkind (Parent (N));
246
247          begin
248             --  If result is to be used as a Condition in the syntax, no need
249             --  to convert it back, since if it was changed to Standard.Boolean
250             --  using Adjust_Condition, that is just fine for this usage.
251
252             if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
253                return;
254
255             --  If result is an operand of another logical operation, no need
256             --  to reset its type, since Standard.Boolean is just fine, and
257             --  such operations always do Adjust_Condition on their operands.
258
259             elsif     KP in N_Op_Boolean
260               or else KP in N_Short_Circuit
261               or else KP = N_Op_Not
262             then
263                return;
264
265             --  Otherwise we perform a conversion from the current type,
266             --  which must be Standard.Boolean, to the desired type.
267
268             else
269                Set_Analyzed (N);
270                Rewrite (N, Convert_To (T, N));
271                Analyze_And_Resolve (N, T);
272             end if;
273          end;
274       end if;
275    end Adjust_Result_Type;
276
277    --------------------------
278    -- Append_Freeze_Action --
279    --------------------------
280
281    procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
282       Fnode : Node_Id;
283
284    begin
285       Ensure_Freeze_Node (T);
286       Fnode := Freeze_Node (T);
287
288       if No (Actions (Fnode)) then
289          Set_Actions (Fnode, New_List);
290       end if;
291
292       Append (N, Actions (Fnode));
293    end Append_Freeze_Action;
294
295    ---------------------------
296    -- Append_Freeze_Actions --
297    ---------------------------
298
299    procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
300       Fnode : constant Node_Id := Freeze_Node (T);
301
302    begin
303       if No (L) then
304          return;
305
306       else
307          if No (Actions (Fnode)) then
308             Set_Actions (Fnode, L);
309
310          else
311             Append_List (L, Actions (Fnode));
312          end if;
313
314       end if;
315    end Append_Freeze_Actions;
316
317    ------------------------
318    -- Build_Runtime_Call --
319    ------------------------
320
321    function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
322    begin
323       --  If entity is not available, we can skip making the call (this avoids
324       --  junk duplicated error messages in a number of cases).
325
326       if not RTE_Available (RE) then
327          return Make_Null_Statement (Loc);
328       else
329          return
330            Make_Procedure_Call_Statement (Loc,
331              Name => New_Reference_To (RTE (RE), Loc));
332       end if;
333    end Build_Runtime_Call;
334
335    ----------------------------
336    -- Build_Task_Array_Image --
337    ----------------------------
338
339    --  This function generates the body for a function that constructs the
340    --  image string for a task that is an array component. The function is
341    --  local to the init proc for the array type, and is called for each one
342    --  of the components. The constructed image has the form of an indexed
343    --  component, whose prefix is the outer variable of the array type.
344    --  The n-dimensional array type has known indices Index, Index2...
345    --  Id_Ref is an indexed component form created by the enclosing init proc.
346    --  Its successive indices are Val1, Val2, ... which are the loop variables
347    --  in the loops that call the individual task init proc on each component.
348
349    --  The generated function has the following structure:
350
351    --  function F return String is
352    --     Pref : string renames Task_Name;
353    --     T1   : String := Index1'Image (Val1);
354    --     ...
355    --     Tn   : String := indexn'image (Valn);
356    --     Len  : Integer := T1'Length + ... + Tn'Length + n + 1;
357    --     --  Len includes commas and the end parentheses.
358    --     Res  : String (1..Len);
359    --     Pos  : Integer := Pref'Length;
360    --
361    --  begin
362    --     Res (1 .. Pos) := Pref;
363    --     Pos := Pos + 1;
364    --     Res (Pos)    := '(';
365    --     Pos := Pos + 1;
366    --     Res (Pos .. Pos + T1'Length - 1) := T1;
367    --     Pos := Pos + T1'Length;
368    --     Res (Pos) := '.';
369    --     Pos := Pos + 1;
370    --     ...
371    --     Res (Pos .. Pos + Tn'Length - 1) := Tn;
372    --     Res (Len) := ')';
373    --
374    --     return Res;
375    --  end F;
376    --
377    --  Needless to say, multidimensional arrays of tasks are rare enough
378    --  that the bulkiness of this code is not really a concern.
379
380    function Build_Task_Array_Image
381      (Loc    : Source_Ptr;
382       Id_Ref : Node_Id;
383       A_Type : Entity_Id;
384       Dyn    : Boolean := False) return Node_Id
385    is
386       Dims : constant Nat := Number_Dimensions (A_Type);
387       --  Number of dimensions for array of tasks
388
389       Temps : array (1 .. Dims) of Entity_Id;
390       --  Array of temporaries to hold string for each index
391
392       Indx : Node_Id;
393       --  Index expression
394
395       Len : Entity_Id;
396       --  Total length of generated name
397
398       Pos : Entity_Id;
399       --  Running index for substring assignments
400
401       Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
402       --  Name of enclosing variable, prefix of resulting name
403
404       Res : Entity_Id;
405       --  String to hold result
406
407       Val : Node_Id;
408       --  Value of successive indices
409
410       Sum : Node_Id;
411       --  Expression to compute total size of string
412
413       T : Entity_Id;
414       --  Entity for name at one index position
415
416       Decls : constant List_Id := New_List;
417       Stats : constant List_Id := New_List;
418
419    begin
420       --  For a dynamic task, the name comes from the target variable.
421       --  For a static one it is a formal of the enclosing init proc.
422
423       if Dyn then
424          Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
425          Append_To (Decls,
426            Make_Object_Declaration (Loc,
427              Defining_Identifier => Pref,
428              Object_Definition => New_Occurrence_Of (Standard_String, Loc),
429              Expression =>
430                Make_String_Literal (Loc,
431                  Strval => String_From_Name_Buffer)));
432
433       else
434          Append_To (Decls,
435            Make_Object_Renaming_Declaration (Loc,
436              Defining_Identifier => Pref,
437              Subtype_Mark        => New_Occurrence_Of (Standard_String, Loc),
438              Name                => Make_Identifier (Loc, Name_uTask_Name)));
439       end if;
440
441       Indx := First_Index (A_Type);
442       Val  := First (Expressions (Id_Ref));
443
444       for J in 1 .. Dims loop
445          T := Make_Temporary (Loc, 'T');
446          Temps (J) := T;
447
448          Append_To (Decls,
449             Make_Object_Declaration (Loc,
450                Defining_Identifier => T,
451                Object_Definition => New_Occurrence_Of (Standard_String, Loc),
452                Expression =>
453                  Make_Attribute_Reference (Loc,
454                    Attribute_Name => Name_Image,
455                    Prefix         => New_Occurrence_Of (Etype (Indx), Loc),
456                    Expressions    => New_List (New_Copy_Tree (Val)))));
457
458          Next_Index (Indx);
459          Next (Val);
460       end loop;
461
462       Sum := Make_Integer_Literal (Loc, Dims + 1);
463
464       Sum :=
465         Make_Op_Add (Loc,
466           Left_Opnd => Sum,
467           Right_Opnd =>
468            Make_Attribute_Reference (Loc,
469              Attribute_Name => Name_Length,
470              Prefix =>
471                New_Occurrence_Of (Pref, Loc),
472              Expressions => New_List (Make_Integer_Literal (Loc, 1))));
473
474       for J in 1 .. Dims loop
475          Sum :=
476             Make_Op_Add (Loc,
477              Left_Opnd => Sum,
478              Right_Opnd =>
479               Make_Attribute_Reference (Loc,
480                 Attribute_Name => Name_Length,
481                 Prefix =>
482                   New_Occurrence_Of (Temps (J), Loc),
483                 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
484       end loop;
485
486       Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
487
488       Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
489
490       Append_To (Stats,
491          Make_Assignment_Statement (Loc,
492            Name => Make_Indexed_Component (Loc,
493               Prefix => New_Occurrence_Of (Res, Loc),
494               Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
495            Expression =>
496              Make_Character_Literal (Loc,
497                Chars => Name_Find,
498                Char_Literal_Value =>
499                  UI_From_Int (Character'Pos ('(')))));
500
501       Append_To (Stats,
502          Make_Assignment_Statement (Loc,
503             Name => New_Occurrence_Of (Pos, Loc),
504             Expression =>
505               Make_Op_Add (Loc,
506                 Left_Opnd => New_Occurrence_Of (Pos, Loc),
507                 Right_Opnd => Make_Integer_Literal (Loc, 1))));
508
509       for J in 1 .. Dims loop
510
511          Append_To (Stats,
512             Make_Assignment_Statement (Loc,
513               Name => Make_Slice (Loc,
514                  Prefix => New_Occurrence_Of (Res, Loc),
515                  Discrete_Range  =>
516                    Make_Range (Loc,
517                       Low_Bound => New_Occurrence_Of  (Pos, Loc),
518                       High_Bound => Make_Op_Subtract (Loc,
519                         Left_Opnd =>
520                           Make_Op_Add (Loc,
521                             Left_Opnd => New_Occurrence_Of (Pos, Loc),
522                             Right_Opnd =>
523                               Make_Attribute_Reference (Loc,
524                                 Attribute_Name => Name_Length,
525                                 Prefix =>
526                                   New_Occurrence_Of (Temps (J), Loc),
527                                 Expressions =>
528                                   New_List (Make_Integer_Literal (Loc, 1)))),
529                          Right_Opnd => Make_Integer_Literal (Loc, 1)))),
530
531               Expression => New_Occurrence_Of (Temps (J), Loc)));
532
533          if J < Dims then
534             Append_To (Stats,
535                Make_Assignment_Statement (Loc,
536                   Name => New_Occurrence_Of (Pos, Loc),
537                   Expression =>
538                     Make_Op_Add (Loc,
539                       Left_Opnd => New_Occurrence_Of (Pos, Loc),
540                       Right_Opnd =>
541                         Make_Attribute_Reference (Loc,
542                           Attribute_Name => Name_Length,
543                             Prefix => New_Occurrence_Of (Temps (J), Loc),
544                             Expressions =>
545                               New_List (Make_Integer_Literal (Loc, 1))))));
546
547             Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
548
549             Append_To (Stats,
550                Make_Assignment_Statement (Loc,
551                  Name => Make_Indexed_Component (Loc,
552                     Prefix => New_Occurrence_Of (Res, Loc),
553                     Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
554                  Expression =>
555                    Make_Character_Literal (Loc,
556                      Chars => Name_Find,
557                      Char_Literal_Value =>
558                        UI_From_Int (Character'Pos (',')))));
559
560             Append_To (Stats,
561               Make_Assignment_Statement (Loc,
562                 Name => New_Occurrence_Of (Pos, Loc),
563                   Expression =>
564                     Make_Op_Add (Loc,
565                       Left_Opnd => New_Occurrence_Of (Pos, Loc),
566                       Right_Opnd => Make_Integer_Literal (Loc, 1))));
567          end if;
568       end loop;
569
570       Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
571
572       Append_To (Stats,
573          Make_Assignment_Statement (Loc,
574            Name => Make_Indexed_Component (Loc,
575               Prefix => New_Occurrence_Of (Res, Loc),
576               Expressions => New_List (New_Occurrence_Of (Len, Loc))),
577            Expression =>
578              Make_Character_Literal (Loc,
579                Chars => Name_Find,
580                Char_Literal_Value =>
581                  UI_From_Int (Character'Pos (')')))));
582       return Build_Task_Image_Function (Loc, Decls, Stats, Res);
583    end Build_Task_Array_Image;
584
585    ----------------------------
586    -- Build_Task_Image_Decls --
587    ----------------------------
588
589    function Build_Task_Image_Decls
590      (Loc          : Source_Ptr;
591       Id_Ref       : Node_Id;
592       A_Type       : Entity_Id;
593       In_Init_Proc : Boolean := False) return List_Id
594    is
595       Decls  : constant List_Id   := New_List;
596       T_Id   : Entity_Id := Empty;
597       Decl   : Node_Id;
598       Expr   : Node_Id   := Empty;
599       Fun    : Node_Id   := Empty;
600       Is_Dyn : constant Boolean :=
601                  Nkind (Parent (Id_Ref)) = N_Assignment_Statement
602                    and then
603                  Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
604
605    begin
606       --  If Discard_Names or No_Implicit_Heap_Allocations are in effect,
607       --  generate a dummy declaration only.
608
609       if Restriction_Active (No_Implicit_Heap_Allocations)
610         or else Global_Discard_Names
611       then
612          T_Id := Make_Temporary (Loc, 'J');
613          Name_Len := 0;
614
615          return
616            New_List (
617              Make_Object_Declaration (Loc,
618                Defining_Identifier => T_Id,
619                Object_Definition => New_Occurrence_Of (Standard_String, Loc),
620                Expression =>
621                  Make_String_Literal (Loc,
622                    Strval => String_From_Name_Buffer)));
623
624       else
625          if Nkind (Id_Ref) = N_Identifier
626            or else Nkind (Id_Ref) = N_Defining_Identifier
627          then
628             --  For a simple variable, the image of the task is built from
629             --  the name of the variable. To avoid possible conflict with
630             --  the anonymous type created for a single protected object,
631             --  add a numeric suffix.
632
633             T_Id :=
634               Make_Defining_Identifier (Loc,
635                 New_External_Name (Chars (Id_Ref), 'T', 1));
636
637             Get_Name_String (Chars (Id_Ref));
638
639             Expr :=
640               Make_String_Literal (Loc,
641                 Strval => String_From_Name_Buffer);
642
643          elsif Nkind (Id_Ref) = N_Selected_Component then
644             T_Id :=
645               Make_Defining_Identifier (Loc,
646                 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
647             Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
648
649          elsif Nkind (Id_Ref) = N_Indexed_Component then
650             T_Id :=
651               Make_Defining_Identifier (Loc,
652                 New_External_Name (Chars (A_Type), 'N'));
653
654             Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
655          end if;
656       end if;
657
658       if Present (Fun) then
659          Append (Fun, Decls);
660          Expr := Make_Function_Call (Loc,
661            Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
662
663          if not In_Init_Proc and then VM_Target = No_VM then
664             Set_Uses_Sec_Stack (Defining_Entity (Fun));
665          end if;
666       end if;
667
668       Decl := Make_Object_Declaration (Loc,
669         Defining_Identifier => T_Id,
670         Object_Definition   => New_Occurrence_Of (Standard_String, Loc),
671         Constant_Present    => True,
672         Expression          => Expr);
673
674       Append (Decl, Decls);
675       return Decls;
676    end Build_Task_Image_Decls;
677
678    -------------------------------
679    -- Build_Task_Image_Function --
680    -------------------------------
681
682    function Build_Task_Image_Function
683      (Loc   : Source_Ptr;
684       Decls : List_Id;
685       Stats : List_Id;
686       Res   : Entity_Id) return Node_Id
687    is
688       Spec : Node_Id;
689
690    begin
691       Append_To (Stats,
692         Make_Simple_Return_Statement (Loc,
693           Expression => New_Occurrence_Of (Res, Loc)));
694
695       Spec := Make_Function_Specification (Loc,
696         Defining_Unit_Name => Make_Temporary (Loc, 'F'),
697         Result_Definition  => New_Occurrence_Of (Standard_String, Loc));
698
699       --  Calls to 'Image use the secondary stack, which must be cleaned
700       --  up after the task name is built.
701
702       return Make_Subprogram_Body (Loc,
703          Specification => Spec,
704          Declarations => Decls,
705          Handled_Statement_Sequence =>
706            Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
707    end Build_Task_Image_Function;
708
709    -----------------------------
710    -- Build_Task_Image_Prefix --
711    -----------------------------
712
713    procedure Build_Task_Image_Prefix
714       (Loc    : Source_Ptr;
715        Len    : out Entity_Id;
716        Res    : out Entity_Id;
717        Pos    : out Entity_Id;
718        Prefix : Entity_Id;
719        Sum    : Node_Id;
720        Decls  : List_Id;
721        Stats  : List_Id)
722    is
723    begin
724       Len := Make_Temporary (Loc, 'L', Sum);
725
726       Append_To (Decls,
727         Make_Object_Declaration (Loc,
728           Defining_Identifier => Len,
729           Object_Definition   => New_Occurrence_Of (Standard_Integer, Loc),
730           Expression          => Sum));
731
732       Res := Make_Temporary (Loc, 'R');
733
734       Append_To (Decls,
735          Make_Object_Declaration (Loc,
736             Defining_Identifier => Res,
737             Object_Definition =>
738                Make_Subtype_Indication (Loc,
739                   Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
740                Constraint =>
741                  Make_Index_Or_Discriminant_Constraint (Loc,
742                    Constraints =>
743                      New_List (
744                        Make_Range (Loc,
745                          Low_Bound => Make_Integer_Literal (Loc, 1),
746                          High_Bound => New_Occurrence_Of (Len, Loc)))))));
747
748       Pos := Make_Temporary (Loc, 'P');
749
750       Append_To (Decls,
751          Make_Object_Declaration (Loc,
752             Defining_Identifier => Pos,
753             Object_Definition   => New_Occurrence_Of (Standard_Integer, Loc)));
754
755       --  Pos := Prefix'Length;
756
757       Append_To (Stats,
758          Make_Assignment_Statement (Loc,
759             Name => New_Occurrence_Of (Pos, Loc),
760             Expression =>
761               Make_Attribute_Reference (Loc,
762                 Attribute_Name => Name_Length,
763                 Prefix         => New_Occurrence_Of (Prefix, Loc),
764                 Expressions    => New_List (Make_Integer_Literal (Loc, 1)))));
765
766       --  Res (1 .. Pos) := Prefix;
767
768       Append_To (Stats,
769         Make_Assignment_Statement (Loc,
770           Name =>
771             Make_Slice (Loc,
772               Prefix          => New_Occurrence_Of (Res, Loc),
773               Discrete_Range  =>
774                 Make_Range (Loc,
775                    Low_Bound  => Make_Integer_Literal (Loc, 1),
776                    High_Bound => New_Occurrence_Of (Pos, Loc))),
777
778           Expression => New_Occurrence_Of (Prefix, Loc)));
779
780       Append_To (Stats,
781          Make_Assignment_Statement (Loc,
782             Name       => New_Occurrence_Of (Pos, Loc),
783             Expression =>
784               Make_Op_Add (Loc,
785                 Left_Opnd  => New_Occurrence_Of (Pos, Loc),
786                 Right_Opnd => Make_Integer_Literal (Loc, 1))));
787    end Build_Task_Image_Prefix;
788
789    -----------------------------
790    -- Build_Task_Record_Image --
791    -----------------------------
792
793    function Build_Task_Record_Image
794      (Loc    : Source_Ptr;
795       Id_Ref : Node_Id;
796       Dyn    : Boolean := False) return Node_Id
797    is
798       Len : Entity_Id;
799       --  Total length of generated name
800
801       Pos : Entity_Id;
802       --  Index into result
803
804       Res : Entity_Id;
805       --  String to hold result
806
807       Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
808       --  Name of enclosing variable, prefix of resulting name
809
810       Sum : Node_Id;
811       --  Expression to compute total size of string
812
813       Sel : Entity_Id;
814       --  Entity for selector name
815
816       Decls : constant List_Id := New_List;
817       Stats : constant List_Id := New_List;
818
819    begin
820       --  For a dynamic task, the name comes from the target variable.
821       --  For a static one it is a formal of the enclosing init proc.
822
823       if Dyn then
824          Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
825          Append_To (Decls,
826            Make_Object_Declaration (Loc,
827              Defining_Identifier => Pref,
828              Object_Definition => New_Occurrence_Of (Standard_String, Loc),
829              Expression =>
830                Make_String_Literal (Loc,
831                  Strval => String_From_Name_Buffer)));
832
833       else
834          Append_To (Decls,
835            Make_Object_Renaming_Declaration (Loc,
836              Defining_Identifier => Pref,
837              Subtype_Mark        => New_Occurrence_Of (Standard_String, Loc),
838              Name                => Make_Identifier (Loc, Name_uTask_Name)));
839       end if;
840
841       Sel := Make_Temporary (Loc, 'S');
842
843       Get_Name_String (Chars (Selector_Name (Id_Ref)));
844
845       Append_To (Decls,
846          Make_Object_Declaration (Loc,
847            Defining_Identifier => Sel,
848            Object_Definition   => New_Occurrence_Of (Standard_String, Loc),
849            Expression          =>
850              Make_String_Literal (Loc,
851                Strval => String_From_Name_Buffer)));
852
853       Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
854
855       Sum :=
856         Make_Op_Add (Loc,
857           Left_Opnd => Sum,
858           Right_Opnd =>
859            Make_Attribute_Reference (Loc,
860              Attribute_Name => Name_Length,
861              Prefix =>
862                New_Occurrence_Of (Pref, Loc),
863              Expressions => New_List (Make_Integer_Literal (Loc, 1))));
864
865       Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
866
867       Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
868
869       --  Res (Pos) := '.';
870
871       Append_To (Stats,
872          Make_Assignment_Statement (Loc,
873            Name => Make_Indexed_Component (Loc,
874               Prefix => New_Occurrence_Of (Res, Loc),
875               Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
876            Expression =>
877              Make_Character_Literal (Loc,
878                Chars => Name_Find,
879                Char_Literal_Value =>
880                  UI_From_Int (Character'Pos ('.')))));
881
882       Append_To (Stats,
883         Make_Assignment_Statement (Loc,
884           Name => New_Occurrence_Of (Pos, Loc),
885           Expression =>
886             Make_Op_Add (Loc,
887               Left_Opnd => New_Occurrence_Of (Pos, Loc),
888               Right_Opnd => Make_Integer_Literal (Loc, 1))));
889
890       --  Res (Pos .. Len) := Selector;
891
892       Append_To (Stats,
893         Make_Assignment_Statement (Loc,
894           Name => Make_Slice (Loc,
895              Prefix => New_Occurrence_Of (Res, Loc),
896              Discrete_Range  =>
897                Make_Range (Loc,
898                  Low_Bound  => New_Occurrence_Of (Pos, Loc),
899                  High_Bound => New_Occurrence_Of (Len, Loc))),
900           Expression => New_Occurrence_Of (Sel, Loc)));
901
902       return Build_Task_Image_Function (Loc, Decls, Stats, Res);
903    end Build_Task_Record_Image;
904
905    ----------------------------------
906    -- Component_May_Be_Bit_Aligned --
907    ----------------------------------
908
909    function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
910       UT : constant Entity_Id := Underlying_Type (Etype (Comp));
911
912    begin
913       --  If no component clause, then everything is fine, since the back end
914       --  never bit-misaligns by default, even if there is a pragma Packed for
915       --  the record.
916
917       if No (Component_Clause (Comp)) then
918          return False;
919       end if;
920
921       --  It is only array and record types that cause trouble
922
923       if not Is_Record_Type (UT)
924         and then not Is_Array_Type (UT)
925       then
926          return False;
927
928       --  If we know that we have a small (64 bits or less) record or small
929       --  bit-packed array, then everything is fine, since the back end can
930       --  handle these cases correctly.
931
932       elsif Esize (Comp) <= 64
933         and then (Is_Record_Type (UT)
934                    or else Is_Bit_Packed_Array (UT))
935       then
936          return False;
937
938       --  Otherwise if the component is not byte aligned, we know we have the
939       --  nasty unaligned case.
940
941       elsif Normalized_First_Bit (Comp) /= Uint_0
942         or else Esize (Comp) mod System_Storage_Unit /= Uint_0
943       then
944          return True;
945
946       --  If we are large and byte aligned, then OK at this level
947
948       else
949          return False;
950       end if;
951    end Component_May_Be_Bit_Aligned;
952
953    -----------------------------------
954    -- Corresponding_Runtime_Package --
955    -----------------------------------
956
957    function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
958       Pkg_Id : RTU_Id := RTU_Null;
959
960    begin
961       pragma Assert (Is_Concurrent_Type (Typ));
962
963       if Ekind (Typ) in Protected_Kind then
964          if Has_Entries (Typ)
965            or else Has_Interrupt_Handler (Typ)
966            or else (Has_Attach_Handler (Typ)
967                       and then not Restricted_Profile)
968
969             --  A protected type without entries that covers an interface and
970             --  overrides the abstract routines with protected procedures is
971             --  considered equivalent to a protected type with entries in the
972             --  context of dispatching select statements. It is sufficient to
973             --  check for the presence of an interface list in the declaration
974             --  node to recognize this case.
975
976            or else Present (Interface_List (Parent (Typ)))
977          then
978             if Abort_Allowed
979               or else Restriction_Active (No_Entry_Queue) = False
980               or else Number_Entries (Typ) > 1
981               or else (Has_Attach_Handler (Typ)
982                          and then not Restricted_Profile)
983             then
984                Pkg_Id := System_Tasking_Protected_Objects_Entries;
985             else
986                Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
987             end if;
988
989          else
990             Pkg_Id := System_Tasking_Protected_Objects;
991          end if;
992       end if;
993
994       return Pkg_Id;
995    end Corresponding_Runtime_Package;
996
997    -------------------------------
998    -- Convert_To_Actual_Subtype --
999    -------------------------------
1000
1001    procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1002       Act_ST : Entity_Id;
1003
1004    begin
1005       Act_ST := Get_Actual_Subtype (Exp);
1006
1007       if Act_ST = Etype (Exp) then
1008          return;
1009
1010       else
1011          Rewrite (Exp,
1012            Convert_To (Act_ST, Relocate_Node (Exp)));
1013          Analyze_And_Resolve (Exp, Act_ST);
1014       end if;
1015    end Convert_To_Actual_Subtype;
1016
1017    -----------------------------------
1018    -- Current_Sem_Unit_Declarations --
1019    -----------------------------------
1020
1021    function Current_Sem_Unit_Declarations return List_Id is
1022       U     : Node_Id := Unit (Cunit (Current_Sem_Unit));
1023       Decls : List_Id;
1024
1025    begin
1026       --  If the current unit is a package body, locate the visible
1027       --  declarations of the package spec.
1028
1029       if Nkind (U) = N_Package_Body then
1030          U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1031       end if;
1032
1033       if Nkind (U) = N_Package_Declaration then
1034          U := Specification (U);
1035          Decls := Visible_Declarations (U);
1036
1037          if No (Decls) then
1038             Decls := New_List;
1039             Set_Visible_Declarations (U, Decls);
1040          end if;
1041
1042       else
1043          Decls := Declarations (U);
1044
1045          if No (Decls) then
1046             Decls := New_List;
1047             Set_Declarations (U, Decls);
1048          end if;
1049       end if;
1050
1051       return Decls;
1052    end Current_Sem_Unit_Declarations;
1053
1054    -----------------------
1055    -- Duplicate_Subexpr --
1056    -----------------------
1057
1058    function Duplicate_Subexpr
1059      (Exp      : Node_Id;
1060       Name_Req : Boolean := False) return Node_Id
1061    is
1062    begin
1063       Remove_Side_Effects (Exp, Name_Req);
1064       return New_Copy_Tree (Exp);
1065    end Duplicate_Subexpr;
1066
1067    ---------------------------------
1068    -- Duplicate_Subexpr_No_Checks --
1069    ---------------------------------
1070
1071    function Duplicate_Subexpr_No_Checks
1072      (Exp      : Node_Id;
1073       Name_Req : Boolean := False) return Node_Id
1074    is
1075       New_Exp : Node_Id;
1076
1077    begin
1078       Remove_Side_Effects (Exp, Name_Req);
1079       New_Exp := New_Copy_Tree (Exp);
1080       Remove_Checks (New_Exp);
1081       return New_Exp;
1082    end Duplicate_Subexpr_No_Checks;
1083
1084    -----------------------------------
1085    -- Duplicate_Subexpr_Move_Checks --
1086    -----------------------------------
1087
1088    function Duplicate_Subexpr_Move_Checks
1089      (Exp      : Node_Id;
1090       Name_Req : Boolean := False) return Node_Id
1091    is
1092       New_Exp : Node_Id;
1093
1094    begin
1095       Remove_Side_Effects (Exp, Name_Req);
1096       New_Exp := New_Copy_Tree (Exp);
1097       Remove_Checks (Exp);
1098       return New_Exp;
1099    end Duplicate_Subexpr_Move_Checks;
1100
1101    --------------------
1102    -- Ensure_Defined --
1103    --------------------
1104
1105    procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1106       IR : Node_Id;
1107
1108    begin
1109       --  An itype reference must only be created if this is a local
1110       --  itype, so that gigi can elaborate it on the proper objstack.
1111
1112       if Is_Itype (Typ)
1113         and then Scope (Typ) = Current_Scope
1114       then
1115          IR := Make_Itype_Reference (Sloc (N));
1116          Set_Itype (IR, Typ);
1117          Insert_Action (N, IR);
1118       end if;
1119    end Ensure_Defined;
1120
1121    --------------------
1122    -- Entry_Names_OK --
1123    --------------------
1124
1125    function Entry_Names_OK return Boolean is
1126    begin
1127       return
1128         not Restricted_Profile
1129           and then not Global_Discard_Names
1130           and then not Restriction_Active (No_Implicit_Heap_Allocations)
1131           and then not Restriction_Active (No_Local_Allocators);
1132    end Entry_Names_OK;
1133
1134    ---------------------
1135    -- Evolve_And_Then --
1136    ---------------------
1137
1138    procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1139    begin
1140       if No (Cond) then
1141          Cond := Cond1;
1142       else
1143          Cond :=
1144            Make_And_Then (Sloc (Cond1),
1145              Left_Opnd  => Cond,
1146              Right_Opnd => Cond1);
1147       end if;
1148    end Evolve_And_Then;
1149
1150    --------------------
1151    -- Evolve_Or_Else --
1152    --------------------
1153
1154    procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1155    begin
1156       if No (Cond) then
1157          Cond := Cond1;
1158       else
1159          Cond :=
1160            Make_Or_Else (Sloc (Cond1),
1161              Left_Opnd  => Cond,
1162              Right_Opnd => Cond1);
1163       end if;
1164    end Evolve_Or_Else;
1165
1166    ------------------------------
1167    -- Expand_Subtype_From_Expr --
1168    ------------------------------
1169
1170    --  This function is applicable for both static and dynamic allocation of
1171    --  objects which are constrained by an initial expression. Basically it
1172    --  transforms an unconstrained subtype indication into a constrained one.
1173    --  The expression may also be transformed in certain cases in order to
1174    --  avoid multiple evaluation. In the static allocation case, the general
1175    --  scheme is:
1176
1177    --     Val : T := Expr;
1178
1179    --        is transformed into
1180
1181    --     Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1182    --
1183    --  Here are the main cases :
1184    --
1185    --  <if Expr is a Slice>
1186    --    Val : T ([Index_Subtype (Expr)]) := Expr;
1187    --
1188    --  <elsif Expr is a String Literal>
1189    --    Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1190    --
1191    --  <elsif Expr is Constrained>
1192    --    subtype T is Type_Of_Expr
1193    --    Val : T := Expr;
1194    --
1195    --  <elsif Expr is an entity_name>
1196    --    Val : T (constraints taken from Expr) := Expr;
1197    --
1198    --  <else>
1199    --    type Axxx is access all T;
1200    --    Rval : Axxx := Expr'ref;
1201    --    Val  : T (constraints taken from Rval) := Rval.all;
1202
1203    --    ??? note: when the Expression is allocated in the secondary stack
1204    --              we could use it directly instead of copying it by declaring
1205    --              Val : T (...) renames Rval.all
1206
1207    procedure Expand_Subtype_From_Expr
1208      (N             : Node_Id;
1209       Unc_Type      : Entity_Id;
1210       Subtype_Indic : Node_Id;
1211       Exp           : Node_Id)
1212    is
1213       Loc     : constant Source_Ptr := Sloc (N);
1214       Exp_Typ : constant Entity_Id  := Etype (Exp);
1215       T       : Entity_Id;
1216
1217    begin
1218       --  In general we cannot build the subtype if expansion is disabled,
1219       --  because internal entities may not have been defined. However, to
1220       --  avoid some cascaded errors, we try to continue when the expression
1221       --  is an array (or string), because it is safe to compute the bounds.
1222       --  It is in fact required to do so even in a generic context, because
1223       --  there may be constants that depend on bounds of string literal.
1224
1225       if not Expander_Active
1226         and then (No (Etype (Exp))
1227                    or else Base_Type (Etype (Exp)) /= Standard_String)
1228       then
1229          return;
1230       end if;
1231
1232       if Nkind (Exp) = N_Slice then
1233          declare
1234             Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1235
1236          begin
1237             Rewrite (Subtype_Indic,
1238               Make_Subtype_Indication (Loc,
1239                 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1240                 Constraint =>
1241                   Make_Index_Or_Discriminant_Constraint (Loc,
1242                     Constraints => New_List
1243                       (New_Reference_To (Slice_Type, Loc)))));
1244
1245             --  This subtype indication may be used later for constraint checks
1246             --  we better make sure that if a variable was used as a bound of
1247             --  of the original slice, its value is frozen.
1248
1249             Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1250             Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1251          end;
1252
1253       elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1254          Rewrite (Subtype_Indic,
1255            Make_Subtype_Indication (Loc,
1256              Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1257              Constraint =>
1258                Make_Index_Or_Discriminant_Constraint (Loc,
1259                  Constraints => New_List (
1260                    Make_Literal_Range (Loc,
1261                      Literal_Typ => Exp_Typ)))));
1262
1263       elsif Is_Constrained (Exp_Typ)
1264         and then not Is_Class_Wide_Type (Unc_Type)
1265       then
1266          if Is_Itype (Exp_Typ) then
1267
1268             --  Within an initialization procedure, a selected component
1269             --  denotes a component of the enclosing record, and it appears
1270             --  as an actual in a call to its own initialization procedure.
1271             --  If this component depends on the outer discriminant, we must
1272             --  generate the proper actual subtype for it.
1273
1274             if Nkind (Exp) = N_Selected_Component
1275               and then Within_Init_Proc
1276             then
1277                declare
1278                   Decl : constant Node_Id :=
1279                            Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
1280                begin
1281                   if Present (Decl) then
1282                      Insert_Action (N, Decl);
1283                      T := Defining_Identifier (Decl);
1284                   else
1285                      T := Exp_Typ;
1286                   end if;
1287                end;
1288
1289             --  No need to generate a new one (new what???)
1290
1291             else
1292                T := Exp_Typ;
1293             end if;
1294
1295          else
1296             T := Make_Temporary (Loc, 'T');
1297
1298             Insert_Action (N,
1299               Make_Subtype_Declaration (Loc,
1300                 Defining_Identifier => T,
1301                 Subtype_Indication  => New_Reference_To (Exp_Typ, Loc)));
1302
1303             --  This type is marked as an itype even though it has an
1304             --  explicit declaration because otherwise it can be marked
1305             --  with Is_Generic_Actual_Type and generate spurious errors.
1306             --  (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1307
1308             Set_Is_Itype (T);
1309             Set_Associated_Node_For_Itype (T, Exp);
1310          end if;
1311
1312          Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1313
1314       --  Nothing needs to be done for private types with unknown discriminants
1315       --  if the underlying type is not an unconstrained composite type or it
1316       --  is an unchecked union.
1317
1318       elsif Is_Private_Type (Unc_Type)
1319         and then Has_Unknown_Discriminants (Unc_Type)
1320         and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1321                    or else Is_Constrained (Underlying_Type (Unc_Type))
1322                    or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
1323       then
1324          null;
1325
1326       --  Case of derived type with unknown discriminants where the parent type
1327       --  also has unknown discriminants.
1328
1329       elsif Is_Record_Type (Unc_Type)
1330         and then not Is_Class_Wide_Type (Unc_Type)
1331         and then Has_Unknown_Discriminants (Unc_Type)
1332         and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
1333       then
1334          --  Nothing to be done if no underlying record view available
1335
1336          if No (Underlying_Record_View (Unc_Type)) then
1337             null;
1338
1339          --  Otherwise use the Underlying_Record_View to create the proper
1340          --  constrained subtype for an object of a derived type with unknown
1341          --  discriminants.
1342
1343          else
1344             Remove_Side_Effects (Exp);
1345             Rewrite (Subtype_Indic,
1346               Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
1347          end if;
1348
1349       --  Renamings of class-wide interface types require no equivalent
1350       --  constrained type declarations because we only need to reference
1351       --  the tag component associated with the interface.
1352
1353       elsif Present (N)
1354         and then Nkind (N) = N_Object_Renaming_Declaration
1355         and then Is_Interface (Unc_Type)
1356       then
1357          pragma Assert (Is_Class_Wide_Type (Unc_Type));
1358          null;
1359
1360       --  In Ada95, nothing to be done if the type of the expression is
1361       --  limited, because in this case the expression cannot be copied,
1362       --  and its use can only be by reference.
1363
1364       --  In Ada2005, the context can be an object declaration whose expression
1365       --  is a function that returns in place. If the nominal subtype has
1366       --  unknown discriminants, the call still provides constraints on the
1367       --  object, and we have to create an actual subtype from it.
1368
1369       --  If the type is class-wide, the expression is dynamically tagged and
1370       --  we do not create an actual subtype either. Ditto for an interface.
1371
1372       elsif Is_Limited_Type (Exp_Typ)
1373         and then
1374          (Is_Class_Wide_Type (Exp_Typ)
1375            or else Is_Interface (Exp_Typ)
1376            or else not Has_Unknown_Discriminants (Exp_Typ)
1377            or else not Is_Composite_Type (Unc_Type))
1378       then
1379          null;
1380
1381       --  For limited objects initialized with build in place function calls,
1382       --  nothing to be done; otherwise we prematurely introduce an N_Reference
1383       --  node in the expression initializing the object, which breaks the
1384       --  circuitry that detects and adds the additional arguments to the
1385       --  called function.
1386
1387       elsif Is_Build_In_Place_Function_Call (Exp) then
1388          null;
1389
1390       else
1391          Remove_Side_Effects (Exp);
1392          Rewrite (Subtype_Indic,
1393            Make_Subtype_From_Expr (Exp, Unc_Type));
1394       end if;
1395    end Expand_Subtype_From_Expr;
1396
1397    --------------------
1398    -- Find_Init_Call --
1399    --------------------
1400
1401    function Find_Init_Call
1402      (Var        : Entity_Id;
1403       Rep_Clause : Node_Id) return Node_Id
1404    is
1405       Typ : constant Entity_Id := Etype (Var);
1406
1407       Init_Proc : Entity_Id;
1408       --  Initialization procedure for Typ
1409
1410       function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
1411       --  Look for init call for Var starting at From and scanning the
1412       --  enclosing list until Rep_Clause or the end of the list is reached.
1413
1414       ----------------------------
1415       -- Find_Init_Call_In_List --
1416       ----------------------------
1417
1418       function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
1419          Init_Call : Node_Id;
1420       begin
1421          Init_Call := From;
1422
1423          while Present (Init_Call) and then Init_Call /= Rep_Clause loop
1424             if Nkind (Init_Call) = N_Procedure_Call_Statement
1425                  and then Is_Entity_Name (Name (Init_Call))
1426                  and then Entity (Name (Init_Call)) = Init_Proc
1427             then
1428                return Init_Call;
1429             end if;
1430             Next (Init_Call);
1431          end loop;
1432
1433          return Empty;
1434       end Find_Init_Call_In_List;
1435
1436       Init_Call : Node_Id;
1437
1438    --  Start of processing for Find_Init_Call
1439
1440    begin
1441       if not Has_Non_Null_Base_Init_Proc (Typ) then
1442          --  No init proc for the type, so obviously no call to be found
1443
1444          return Empty;
1445       end if;
1446
1447       Init_Proc := Base_Init_Proc (Typ);
1448
1449       --  First scan the list containing the declaration of Var
1450
1451       Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
1452
1453       --  If not found, also look on Var's freeze actions list, if any, since
1454       --  the init call may have been moved there (case of an address clause
1455       --  applying to Var).
1456
1457       if No (Init_Call) and then Present (Freeze_Node (Var)) then
1458          Init_Call := Find_Init_Call_In_List
1459                         (First (Actions (Freeze_Node (Var))));
1460       end if;
1461
1462       return Init_Call;
1463    end Find_Init_Call;
1464
1465    ------------------------
1466    -- Find_Interface_ADT --
1467    ------------------------
1468
1469    function Find_Interface_ADT
1470      (T     : Entity_Id;
1471       Iface : Entity_Id) return Elmt_Id
1472    is
1473       ADT : Elmt_Id;
1474       Typ : Entity_Id := T;
1475
1476    begin
1477       pragma Assert (Is_Interface (Iface));
1478
1479       --  Handle private types
1480
1481       if Has_Private_Declaration (Typ)
1482         and then Present (Full_View (Typ))
1483       then
1484          Typ := Full_View (Typ);
1485       end if;
1486
1487       --  Handle access types
1488
1489       if Is_Access_Type (Typ) then
1490          Typ := Designated_Type (Typ);
1491       end if;
1492
1493       --  Handle task and protected types implementing interfaces
1494
1495       if Is_Concurrent_Type (Typ) then
1496          Typ := Corresponding_Record_Type (Typ);
1497       end if;
1498
1499       pragma Assert
1500         (not Is_Class_Wide_Type (Typ)
1501           and then Ekind (Typ) /= E_Incomplete_Type);
1502
1503       if Is_Ancestor (Iface, Typ) then
1504          return First_Elmt (Access_Disp_Table (Typ));
1505
1506       else
1507          ADT :=
1508            Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
1509          while Present (ADT)
1510            and then Present (Related_Type (Node (ADT)))
1511            and then Related_Type (Node (ADT)) /= Iface
1512            and then not Is_Ancestor (Iface, Related_Type (Node (ADT)))
1513          loop
1514             Next_Elmt (ADT);
1515          end loop;
1516
1517          pragma Assert (Present (Related_Type (Node (ADT))));
1518          return ADT;
1519       end if;
1520    end Find_Interface_ADT;
1521
1522    ------------------------
1523    -- Find_Interface_Tag --
1524    ------------------------
1525
1526    function Find_Interface_Tag
1527      (T     : Entity_Id;
1528       Iface : Entity_Id) return Entity_Id
1529    is
1530       AI_Tag : Entity_Id;
1531       Found  : Boolean   := False;
1532       Typ    : Entity_Id := T;
1533
1534       procedure Find_Tag (Typ : Entity_Id);
1535       --  Internal subprogram used to recursively climb to the ancestors
1536
1537       --------------
1538       -- Find_Tag --
1539       --------------
1540
1541       procedure Find_Tag (Typ : Entity_Id) is
1542          AI_Elmt : Elmt_Id;
1543          AI      : Node_Id;
1544
1545       begin
1546          --  This routine does not handle the case in which the interface is an
1547          --  ancestor of Typ. That case is handled by the enclosing subprogram.
1548
1549          pragma Assert (Typ /= Iface);
1550
1551          --  Climb to the root type handling private types
1552
1553          if Present (Full_View (Etype (Typ))) then
1554             if Full_View (Etype (Typ)) /= Typ then
1555                Find_Tag (Full_View (Etype (Typ)));
1556             end if;
1557
1558          elsif Etype (Typ) /= Typ then
1559             Find_Tag (Etype (Typ));
1560          end if;
1561
1562          --  Traverse the list of interfaces implemented by the type
1563
1564          if not Found
1565            and then Present (Interfaces (Typ))
1566            and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
1567          then
1568             --  Skip the tag associated with the primary table
1569
1570             pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1571             AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1572             pragma Assert (Present (AI_Tag));
1573
1574             AI_Elmt := First_Elmt (Interfaces (Typ));
1575             while Present (AI_Elmt) loop
1576                AI := Node (AI_Elmt);
1577
1578                if AI = Iface or else Is_Ancestor (Iface, AI) then
1579                   Found := True;
1580                   return;
1581                end if;
1582
1583                AI_Tag := Next_Tag_Component (AI_Tag);
1584                Next_Elmt (AI_Elmt);
1585             end loop;
1586          end if;
1587       end Find_Tag;
1588
1589    --  Start of processing for Find_Interface_Tag
1590
1591    begin
1592       pragma Assert (Is_Interface (Iface));
1593
1594       --  Handle access types
1595
1596       if Is_Access_Type (Typ) then
1597          Typ := Designated_Type (Typ);
1598       end if;
1599
1600       --  Handle class-wide types
1601
1602       if Is_Class_Wide_Type (Typ) then
1603          Typ := Root_Type (Typ);
1604       end if;
1605
1606       --  Handle private types
1607
1608       if Has_Private_Declaration (Typ)
1609         and then Present (Full_View (Typ))
1610       then
1611          Typ := Full_View (Typ);
1612       end if;
1613
1614       --  Handle entities from the limited view
1615
1616       if Ekind (Typ) = E_Incomplete_Type then
1617          pragma Assert (Present (Non_Limited_View (Typ)));
1618          Typ := Non_Limited_View (Typ);
1619       end if;
1620
1621       --  Handle task and protected types implementing interfaces
1622
1623       if Is_Concurrent_Type (Typ) then
1624          Typ := Corresponding_Record_Type (Typ);
1625       end if;
1626
1627       --  If the interface is an ancestor of the type, then it shared the
1628       --  primary dispatch table.
1629
1630       if Is_Ancestor (Iface, Typ) then
1631          pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1632          return First_Tag_Component (Typ);
1633
1634       --  Otherwise we need to search for its associated tag component
1635
1636       else
1637          Find_Tag (Typ);
1638          pragma Assert (Found);
1639          return AI_Tag;
1640       end if;
1641    end Find_Interface_Tag;
1642
1643    ------------------
1644    -- Find_Prim_Op --
1645    ------------------
1646
1647    function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1648       Prim : Elmt_Id;
1649       Typ  : Entity_Id := T;
1650       Op   : Entity_Id;
1651
1652    begin
1653       if Is_Class_Wide_Type (Typ) then
1654          Typ := Root_Type (Typ);
1655       end if;
1656
1657       Typ := Underlying_Type (Typ);
1658
1659       --  Loop through primitive operations
1660
1661       Prim := First_Elmt (Primitive_Operations (Typ));
1662       while Present (Prim) loop
1663          Op := Node (Prim);
1664
1665          --  We can retrieve primitive operations by name if it is an internal
1666          --  name. For equality we must check that both of its operands have
1667          --  the same type, to avoid confusion with user-defined equalities
1668          --  than may have a non-symmetric signature.
1669
1670          exit when Chars (Op) = Name
1671            and then
1672              (Name /= Name_Op_Eq
1673                 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
1674
1675          Next_Elmt (Prim);
1676
1677          --  Raise Program_Error if no primitive found
1678
1679          if No (Prim) then
1680             raise Program_Error;
1681          end if;
1682       end loop;
1683
1684       return Node (Prim);
1685    end Find_Prim_Op;
1686
1687    ------------------
1688    -- Find_Prim_Op --
1689    ------------------
1690
1691    function Find_Prim_Op
1692      (T    : Entity_Id;
1693       Name : TSS_Name_Type) return Entity_Id
1694    is
1695       Prim : Elmt_Id;
1696       Typ  : Entity_Id := T;
1697
1698    begin
1699       if Is_Class_Wide_Type (Typ) then
1700          Typ := Root_Type (Typ);
1701       end if;
1702
1703       Typ := Underlying_Type (Typ);
1704
1705       Prim := First_Elmt (Primitive_Operations (Typ));
1706       while not Is_TSS (Node (Prim), Name) loop
1707          Next_Elmt (Prim);
1708
1709          --  Raise program error if no primitive found
1710
1711          if No (Prim) then
1712             raise Program_Error;
1713          end if;
1714       end loop;
1715
1716       return Node (Prim);
1717    end Find_Prim_Op;
1718
1719    ----------------------------
1720    -- Find_Protection_Object --
1721    ----------------------------
1722
1723    function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
1724       S : Entity_Id;
1725
1726    begin
1727       S := Scop;
1728       while Present (S) loop
1729          if (Ekind (S) = E_Entry
1730                or else Ekind (S) = E_Entry_Family
1731                or else Ekind (S) = E_Function
1732                or else Ekind (S) = E_Procedure)
1733            and then Present (Protection_Object (S))
1734          then
1735             return Protection_Object (S);
1736          end if;
1737
1738          S := Scope (S);
1739       end loop;
1740
1741       --  If we do not find a Protection object in the scope chain, then
1742       --  something has gone wrong, most likely the object was never created.
1743
1744       raise Program_Error;
1745    end Find_Protection_Object;
1746
1747    ----------------------
1748    -- Force_Evaluation --
1749    ----------------------
1750
1751    procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1752    begin
1753       Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
1754    end Force_Evaluation;
1755
1756    ------------------------
1757    -- Generate_Poll_Call --
1758    ------------------------
1759
1760    procedure Generate_Poll_Call (N : Node_Id) is
1761    begin
1762       --  No poll call if polling not active
1763
1764       if not Polling_Required then
1765          return;
1766
1767       --  Otherwise generate require poll call
1768
1769       else
1770          Insert_Before_And_Analyze (N,
1771            Make_Procedure_Call_Statement (Sloc (N),
1772              Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1773       end if;
1774    end Generate_Poll_Call;
1775
1776    ---------------------------------
1777    -- Get_Current_Value_Condition --
1778    ---------------------------------
1779
1780    --  Note: the implementation of this procedure is very closely tied to the
1781    --  implementation of Set_Current_Value_Condition. In the Get procedure, we
1782    --  interpret Current_Value fields set by the Set procedure, so the two
1783    --  procedures need to be closely coordinated.
1784
1785    procedure Get_Current_Value_Condition
1786      (Var : Node_Id;
1787       Op  : out Node_Kind;
1788       Val : out Node_Id)
1789    is
1790       Loc : constant Source_Ptr := Sloc (Var);
1791       Ent : constant Entity_Id  := Entity (Var);
1792
1793       procedure Process_Current_Value_Condition
1794         (N : Node_Id;
1795          S : Boolean);
1796       --  N is an expression which holds either True (S = True) or False (S =
1797       --  False) in the condition. This procedure digs out the expression and
1798       --  if it refers to Ent, sets Op and Val appropriately.
1799
1800       -------------------------------------
1801       -- Process_Current_Value_Condition --
1802       -------------------------------------
1803
1804       procedure Process_Current_Value_Condition
1805         (N : Node_Id;
1806          S : Boolean)
1807       is
1808          Cond : Node_Id;
1809          Sens : Boolean;
1810
1811       begin
1812          Cond := N;
1813          Sens := S;
1814
1815          --  Deal with NOT operators, inverting sense
1816
1817          while Nkind (Cond) = N_Op_Not loop
1818             Cond := Right_Opnd (Cond);
1819             Sens := not Sens;
1820          end loop;
1821
1822          --  Deal with AND THEN and AND cases
1823
1824          if Nkind (Cond) = N_And_Then
1825            or else Nkind (Cond) = N_Op_And
1826          then
1827             --  Don't ever try to invert a condition that is of the form
1828             --  of an AND or AND THEN (since we are not doing sufficiently
1829             --  general processing to allow this).
1830
1831             if Sens = False then
1832                Op  := N_Empty;
1833                Val := Empty;
1834                return;
1835             end if;
1836
1837             --  Recursively process AND and AND THEN branches
1838
1839             Process_Current_Value_Condition (Left_Opnd (Cond), True);
1840
1841             if Op /= N_Empty then
1842                return;
1843             end if;
1844
1845             Process_Current_Value_Condition (Right_Opnd (Cond), True);
1846             return;
1847
1848          --  Case of relational operator
1849
1850          elsif Nkind (Cond) in N_Op_Compare then
1851             Op := Nkind (Cond);
1852
1853             --  Invert sense of test if inverted test
1854
1855             if Sens = False then
1856                case Op is
1857                   when N_Op_Eq => Op := N_Op_Ne;
1858                   when N_Op_Ne => Op := N_Op_Eq;
1859                   when N_Op_Lt => Op := N_Op_Ge;
1860                   when N_Op_Gt => Op := N_Op_Le;
1861                   when N_Op_Le => Op := N_Op_Gt;
1862                   when N_Op_Ge => Op := N_Op_Lt;
1863                   when others  => raise Program_Error;
1864                end case;
1865             end if;
1866
1867             --  Case of entity op value
1868
1869             if Is_Entity_Name (Left_Opnd (Cond))
1870               and then Ent = Entity (Left_Opnd (Cond))
1871               and then Compile_Time_Known_Value (Right_Opnd (Cond))
1872             then
1873                Val := Right_Opnd (Cond);
1874
1875             --  Case of value op entity
1876
1877             elsif Is_Entity_Name (Right_Opnd (Cond))
1878               and then Ent = Entity (Right_Opnd (Cond))
1879               and then Compile_Time_Known_Value (Left_Opnd (Cond))
1880             then
1881                Val := Left_Opnd (Cond);
1882
1883                --  We are effectively swapping operands
1884
1885                case Op is
1886                   when N_Op_Eq => null;
1887                   when N_Op_Ne => null;
1888                   when N_Op_Lt => Op := N_Op_Gt;
1889                   when N_Op_Gt => Op := N_Op_Lt;
1890                   when N_Op_Le => Op := N_Op_Ge;
1891                   when N_Op_Ge => Op := N_Op_Le;
1892                   when others  => raise Program_Error;
1893                end case;
1894
1895             else
1896                Op := N_Empty;
1897             end if;
1898
1899             return;
1900
1901             --  Case of Boolean variable reference, return as though the
1902             --  reference had said var = True.
1903
1904          else
1905             if Is_Entity_Name (Cond)
1906               and then Ent = Entity (Cond)
1907             then
1908                Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
1909
1910                if Sens = False then
1911                   Op := N_Op_Ne;
1912                else
1913                   Op := N_Op_Eq;
1914                end if;
1915             end if;
1916          end if;
1917       end Process_Current_Value_Condition;
1918
1919    --  Start of processing for Get_Current_Value_Condition
1920
1921    begin
1922       Op  := N_Empty;
1923       Val := Empty;
1924
1925       --  Immediate return, nothing doing, if this is not an object
1926
1927       if Ekind (Ent) not in Object_Kind then
1928          return;
1929       end if;
1930
1931       --  Otherwise examine current value
1932
1933       declare
1934          CV   : constant Node_Id := Current_Value (Ent);
1935          Sens : Boolean;
1936          Stm  : Node_Id;
1937
1938       begin
1939          --  If statement. Condition is known true in THEN section, known False
1940          --  in any ELSIF or ELSE part, and unknown outside the IF statement.
1941
1942          if Nkind (CV) = N_If_Statement then
1943
1944             --  Before start of IF statement
1945
1946             if Loc < Sloc (CV) then
1947                return;
1948
1949                --  After end of IF statement
1950
1951             elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
1952                return;
1953             end if;
1954
1955             --  At this stage we know that we are within the IF statement, but
1956             --  unfortunately, the tree does not record the SLOC of the ELSE so
1957             --  we cannot use a simple SLOC comparison to distinguish between
1958             --  the then/else statements, so we have to climb the tree.
1959
1960             declare
1961                N : Node_Id;
1962
1963             begin
1964                N := Parent (Var);
1965                while Parent (N) /= CV loop
1966                   N := Parent (N);
1967
1968                   --  If we fall off the top of the tree, then that's odd, but
1969                   --  perhaps it could occur in some error situation, and the
1970                   --  safest response is simply to assume that the outcome of
1971                   --  the condition is unknown. No point in bombing during an
1972                   --  attempt to optimize things.
1973
1974                   if No (N) then
1975                      return;
1976                   end if;
1977                end loop;
1978
1979                --  Now we have N pointing to a node whose parent is the IF
1980                --  statement in question, so now we can tell if we are within
1981                --  the THEN statements.
1982
1983                if Is_List_Member (N)
1984                  and then List_Containing (N) = Then_Statements (CV)
1985                then
1986                   Sens := True;
1987
1988                --  If the variable reference does not come from source, we
1989                --  cannot reliably tell whether it appears in the else part.
1990                --  In particular, if it appears in generated code for a node
1991                --  that requires finalization, it may be attached to a list
1992                --  that has not been yet inserted into the code. For now,
1993                --  treat it as unknown.
1994
1995                elsif not Comes_From_Source (N) then
1996                   return;
1997
1998                --  Otherwise we must be in ELSIF or ELSE part
1999
2000                else
2001                   Sens := False;
2002                end if;
2003             end;
2004
2005             --  ELSIF part. Condition is known true within the referenced
2006             --  ELSIF, known False in any subsequent ELSIF or ELSE part, and
2007             --  unknown before the ELSE part or after the IF statement.
2008
2009          elsif Nkind (CV) = N_Elsif_Part then
2010
2011             --  if the Elsif_Part had condition_actions, the elsif has been
2012             --  rewritten as a nested if, and the original elsif_part is
2013             --  detached from the tree, so there is no way to obtain useful
2014             --  information on the current value of the variable.
2015             --  Can this be improved ???
2016
2017             if No (Parent (CV)) then
2018                return;
2019             end if;
2020
2021             Stm := Parent (CV);
2022
2023             --  Before start of ELSIF part
2024
2025             if Loc < Sloc (CV) then
2026                return;
2027
2028                --  After end of IF statement
2029
2030             elsif Loc >= Sloc (Stm) +
2031               Text_Ptr (UI_To_Int (End_Span (Stm)))
2032             then
2033                return;
2034             end if;
2035
2036             --  Again we lack the SLOC of the ELSE, so we need to climb the
2037             --  tree to see if we are within the ELSIF part in question.
2038
2039             declare
2040                N : Node_Id;
2041
2042             begin
2043                N := Parent (Var);
2044                while Parent (N) /= Stm loop
2045                   N := Parent (N);
2046
2047                   --  If we fall off the top of the tree, then that's odd, but
2048                   --  perhaps it could occur in some error situation, and the
2049                   --  safest response is simply to assume that the outcome of
2050                   --  the condition is unknown. No point in bombing during an
2051                   --  attempt to optimize things.
2052
2053                   if No (N) then
2054                      return;
2055                   end if;
2056                end loop;
2057
2058                --  Now we have N pointing to a node whose parent is the IF
2059                --  statement in question, so see if is the ELSIF part we want.
2060                --  the THEN statements.
2061
2062                if N = CV then
2063                   Sens := True;
2064
2065                   --  Otherwise we must be in subsequent ELSIF or ELSE part
2066
2067                else
2068                   Sens := False;
2069                end if;
2070             end;
2071
2072          --  Iteration scheme of while loop. The condition is known to be
2073          --  true within the body of the loop.
2074
2075          elsif Nkind (CV) = N_Iteration_Scheme then
2076             declare
2077                Loop_Stmt : constant Node_Id := Parent (CV);
2078
2079             begin
2080                --  Before start of body of loop
2081
2082                if Loc < Sloc (Loop_Stmt) then
2083                   return;
2084
2085                --  After end of LOOP statement
2086
2087                elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2088                   return;
2089
2090                --  We are within the body of the loop
2091
2092                else
2093                   Sens := True;
2094                end if;
2095             end;
2096
2097          --  All other cases of Current_Value settings
2098
2099          else
2100             return;
2101          end if;
2102
2103          --  If we fall through here, then we have a reportable condition, Sens
2104          --  is True if the condition is true and False if it needs inverting.
2105
2106          Process_Current_Value_Condition (Condition (CV), Sens);
2107       end;
2108    end Get_Current_Value_Condition;
2109
2110    ---------------------------------
2111    -- Has_Controlled_Coextensions --
2112    ---------------------------------
2113
2114    function Has_Controlled_Coextensions (Typ : Entity_Id) return Boolean is
2115       D_Typ : Entity_Id;
2116       Discr : Entity_Id;
2117
2118    begin
2119       --  Only consider record types
2120
2121       if not Ekind_In (Typ, E_Record_Type, E_Record_Subtype) then
2122          return False;
2123       end if;
2124
2125       if Has_Discriminants (Typ) then
2126          Discr := First_Discriminant (Typ);
2127          while Present (Discr) loop
2128             D_Typ := Etype (Discr);
2129
2130             if Ekind (D_Typ) = E_Anonymous_Access_Type
2131               and then
2132                 (Is_Controlled (Designated_Type (D_Typ))
2133                    or else
2134                  Is_Concurrent_Type (Designated_Type (D_Typ)))
2135             then
2136                return True;
2137             end if;
2138
2139             Next_Discriminant (Discr);
2140          end loop;
2141       end if;
2142
2143       return False;
2144    end Has_Controlled_Coextensions;
2145
2146    ------------------------
2147    -- Has_Address_Clause --
2148    ------------------------
2149
2150    --  Should this function check the private part in a package ???
2151
2152    function Has_Following_Address_Clause (D : Node_Id) return Boolean is
2153       Id   : constant Entity_Id := Defining_Identifier (D);
2154       Decl : Node_Id;
2155
2156    begin
2157       Decl := Next (D);
2158       while Present (Decl) loop
2159          if Nkind (Decl) = N_At_Clause
2160            and then Chars (Identifier (Decl)) = Chars (Id)
2161          then
2162             return True;
2163
2164          elsif Nkind (Decl) = N_Attribute_Definition_Clause
2165            and then Chars (Decl) = Name_Address
2166            and then Chars (Name (Decl)) = Chars (Id)
2167          then
2168             return True;
2169          end if;
2170
2171          Next (Decl);
2172       end loop;
2173
2174       return False;
2175    end Has_Following_Address_Clause;
2176
2177    --------------------
2178    -- Homonym_Number --
2179    --------------------
2180
2181    function Homonym_Number (Subp : Entity_Id) return Nat is
2182       Count : Nat;
2183       Hom   : Entity_Id;
2184
2185    begin
2186       Count := 1;
2187       Hom := Homonym (Subp);
2188       while Present (Hom) loop
2189          if Scope (Hom) = Scope (Subp) then
2190             Count := Count + 1;
2191          end if;
2192
2193          Hom := Homonym (Hom);
2194       end loop;
2195
2196       return Count;
2197    end Homonym_Number;
2198
2199    ------------------------------
2200    -- In_Unconditional_Context --
2201    ------------------------------
2202
2203    function In_Unconditional_Context (Node : Node_Id) return Boolean is
2204       P : Node_Id;
2205
2206    begin
2207       P := Node;
2208       while Present (P) loop
2209          case Nkind (P) is
2210             when N_Subprogram_Body =>
2211                return True;
2212
2213             when N_If_Statement =>
2214                return False;
2215
2216             when N_Loop_Statement =>
2217                return False;
2218
2219             when N_Case_Statement =>
2220                return False;
2221
2222             when others =>
2223                P := Parent (P);
2224          end case;
2225       end loop;
2226
2227       return False;
2228    end In_Unconditional_Context;
2229
2230    -------------------
2231    -- Insert_Action --
2232    -------------------
2233
2234    procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
2235    begin
2236       if Present (Ins_Action) then
2237          Insert_Actions (Assoc_Node, New_List (Ins_Action));
2238       end if;
2239    end Insert_Action;
2240
2241    --  Version with check(s) suppressed
2242
2243    procedure Insert_Action
2244      (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
2245    is
2246    begin
2247       Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
2248    end Insert_Action;
2249
2250    --------------------
2251    -- Insert_Actions --
2252    --------------------
2253
2254    procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
2255       N : Node_Id;
2256       P : Node_Id;
2257
2258       Wrapped_Node : Node_Id := Empty;
2259
2260    begin
2261       if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
2262          return;
2263       end if;
2264
2265       --  Ignore insert of actions from inside default expression (or other
2266       --  similar "spec expression") in the special spec-expression analyze
2267       --  mode. Any insertions at this point have no relevance, since we are
2268       --  only doing the analyze to freeze the types of any static expressions.
2269       --  See section "Handling of Default Expressions" in the spec of package
2270       --  Sem for further details.
2271
2272       if In_Spec_Expression then
2273          return;
2274       end if;
2275
2276       --  If the action derives from stuff inside a record, then the actions
2277       --  are attached to the current scope, to be inserted and analyzed on
2278       --  exit from the scope. The reason for this is that we may also
2279       --  be generating freeze actions at the same time, and they must
2280       --  eventually be elaborated in the correct order.
2281
2282       if Is_Record_Type (Current_Scope)
2283         and then not Is_Frozen (Current_Scope)
2284       then
2285          if No (Scope_Stack.Table
2286            (Scope_Stack.Last).Pending_Freeze_Actions)
2287          then
2288             Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
2289               Ins_Actions;
2290          else
2291             Append_List
2292               (Ins_Actions,
2293                Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
2294          end if;
2295
2296          return;
2297       end if;
2298
2299       --  We now intend to climb up the tree to find the right point to
2300       --  insert the actions. We start at Assoc_Node, unless this node is
2301       --  a subexpression in which case we start with its parent. We do this
2302       --  for two reasons. First it speeds things up. Second, if Assoc_Node
2303       --  is itself one of the special nodes like N_And_Then, then we assume
2304       --  that an initial request to insert actions for such a node does not
2305       --  expect the actions to get deposited in the node for later handling
2306       --  when the node is expanded, since clearly the node is being dealt
2307       --  with by the caller. Note that in the subexpression case, N is
2308       --  always the child we came from.
2309
2310       --  N_Raise_xxx_Error is an annoying special case, it is a statement
2311       --  if it has type Standard_Void_Type, and a subexpression otherwise.
2312       --  otherwise. Procedure attribute references are also statements.
2313
2314       if Nkind (Assoc_Node) in N_Subexpr
2315         and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
2316                    or else Etype (Assoc_Node) /= Standard_Void_Type)
2317         and then (Nkind (Assoc_Node) /= N_Attribute_Reference
2318                    or else
2319                      not Is_Procedure_Attribute_Name
2320                            (Attribute_Name (Assoc_Node)))
2321       then
2322          P := Assoc_Node;             -- ??? does not agree with above!
2323          N := Parent (Assoc_Node);
2324
2325       --  Non-subexpression case. Note that N is initially Empty in this
2326       --  case (N is only guaranteed Non-Empty in the subexpr case).
2327
2328       else
2329          P := Assoc_Node;
2330          N := Empty;
2331       end if;
2332
2333       --  Capture root of the transient scope
2334
2335       if Scope_Is_Transient then
2336          Wrapped_Node := Node_To_Be_Wrapped;
2337       end if;
2338
2339       loop
2340          pragma Assert (Present (P));
2341
2342          case Nkind (P) is
2343
2344             --  Case of right operand of AND THEN or OR ELSE. Put the actions
2345             --  in the Actions field of the right operand. They will be moved
2346             --  out further when the AND THEN or OR ELSE operator is expanded.
2347             --  Nothing special needs to be done for the left operand since
2348             --  in that case the actions are executed unconditionally.
2349
2350             when N_Short_Circuit =>
2351                if N = Right_Opnd (P) then
2352
2353                   --  We are now going to either append the actions to the
2354                   --  actions field of the short-circuit operation. We will
2355                   --  also analyze the actions now.
2356
2357                   --  This analysis is really too early, the proper thing would
2358                   --  be to just park them there now, and only analyze them if
2359                   --  we find we really need them, and to it at the proper
2360                   --  final insertion point. However attempting to this proved
2361                   --  tricky, so for now we just kill current values before and
2362                   --  after the analyze call to make sure we avoid peculiar
2363                   --  optimizations from this out of order insertion.
2364
2365                   Kill_Current_Values;
2366
2367                   if Present (Actions (P)) then
2368                      Insert_List_After_And_Analyze
2369                        (Last (Actions (P)), Ins_Actions);
2370                   else
2371                      Set_Actions (P, Ins_Actions);
2372                      Analyze_List (Actions (P));
2373                   end if;
2374
2375                   Kill_Current_Values;
2376
2377                   return;
2378                end if;
2379
2380             --  Then or Else operand of conditional expression. Add actions to
2381             --  Then_Actions or Else_Actions field as appropriate. The actions
2382             --  will be moved further out when the conditional is expanded.
2383
2384             when N_Conditional_Expression =>
2385                declare
2386                   ThenX : constant Node_Id := Next (First (Expressions (P)));
2387                   ElseX : constant Node_Id := Next (ThenX);
2388
2389                begin
2390                   --  Actions belong to the then expression, temporarily
2391                   --  place them as Then_Actions of the conditional expr.
2392                   --  They will be moved to the proper place later when
2393                   --  the conditional expression is expanded.
2394
2395                   if N = ThenX then
2396                      if Present (Then_Actions (P)) then
2397                         Insert_List_After_And_Analyze
2398                           (Last (Then_Actions (P)), Ins_Actions);
2399                      else
2400                         Set_Then_Actions (P, Ins_Actions);
2401                         Analyze_List (Then_Actions (P));
2402                      end if;
2403
2404                      return;
2405
2406                   --  Actions belong to the else expression, temporarily
2407                   --  place them as Else_Actions of the conditional expr.
2408                   --  They will be moved to the proper place later when
2409                   --  the conditional expression is expanded.
2410
2411                   elsif N = ElseX then
2412                      if Present (Else_Actions (P)) then
2413                         Insert_List_After_And_Analyze
2414                           (Last (Else_Actions (P)), Ins_Actions);
2415                      else
2416                         Set_Else_Actions (P, Ins_Actions);
2417                         Analyze_List (Else_Actions (P));
2418                      end if;
2419
2420                      return;
2421
2422                   --  Actions belong to the condition. In this case they are
2423                   --  unconditionally executed, and so we can continue the
2424                   --  search for the proper insert point.
2425
2426                   else
2427                      null;
2428                   end if;
2429                end;
2430
2431             --  Alternative of case expression, we place the action in
2432             --  the Actions field of the case expression alternative, this
2433             --  will be handled when the case expression is expanded.
2434
2435             when N_Case_Expression_Alternative =>
2436                if Present (Actions (P)) then
2437                   Insert_List_After_And_Analyze
2438                     (Last (Actions (P)), Ins_Actions);
2439                else
2440                   Set_Actions (P, Ins_Actions);
2441                   Analyze_List (Then_Actions (P));
2442                end if;
2443
2444                return;
2445
2446             --  Case of appearing within an Expressions_With_Actions node. We
2447             --  prepend the actions to the list of actions already there.
2448
2449             when N_Expression_With_Actions =>
2450                Prepend_List (Ins_Actions, Actions (P));
2451                return;
2452
2453             --  Case of appearing in the condition of a while expression or
2454             --  elsif. We insert the actions into the Condition_Actions field.
2455             --  They will be moved further out when the while loop or elsif
2456             --  is analyzed.
2457
2458             when N_Iteration_Scheme |
2459                  N_Elsif_Part
2460             =>
2461                if N = Condition (P) then
2462                   if Present (Condition_Actions (P)) then
2463                      Insert_List_After_And_Analyze
2464                        (Last (Condition_Actions (P)), Ins_Actions);
2465                   else
2466                      Set_Condition_Actions (P, Ins_Actions);
2467
2468                      --  Set the parent of the insert actions explicitly.
2469                      --  This is not a syntactic field, but we need the
2470                      --  parent field set, in particular so that freeze
2471                      --  can understand that it is dealing with condition
2472                      --  actions, and properly insert the freezing actions.
2473
2474                      Set_Parent (Ins_Actions, P);
2475                      Analyze_List (Condition_Actions (P));
2476                   end if;
2477
2478                   return;
2479                end if;
2480
2481             --  Statements, declarations, pragmas, representation clauses
2482
2483             when
2484                --  Statements
2485
2486                N_Procedure_Call_Statement               |
2487                N_Statement_Other_Than_Procedure_Call    |
2488
2489                --  Pragmas
2490
2491                N_Pragma                                 |
2492
2493                --  Representation_Clause
2494
2495                N_At_Clause                              |
2496                N_Attribute_Definition_Clause            |
2497                N_Enumeration_Representation_Clause      |
2498                N_Record_Representation_Clause           |
2499
2500                --  Declarations
2501
2502                N_Abstract_Subprogram_Declaration        |
2503                N_Entry_Body                             |
2504                N_Exception_Declaration                  |
2505                N_Exception_Renaming_Declaration         |
2506                N_Formal_Abstract_Subprogram_Declaration |
2507                N_Formal_Concrete_Subprogram_Declaration |
2508                N_Formal_Object_Declaration              |
2509                N_Formal_Type_Declaration                |
2510                N_Full_Type_Declaration                  |
2511                N_Function_Instantiation                 |
2512                N_Generic_Function_Renaming_Declaration  |
2513                N_Generic_Package_Declaration            |
2514                N_Generic_Package_Renaming_Declaration   |
2515                N_Generic_Procedure_Renaming_Declaration |
2516                N_Generic_Subprogram_Declaration         |
2517                N_Implicit_Label_Declaration             |
2518                N_Incomplete_Type_Declaration            |
2519                N_Number_Declaration                     |
2520                N_Object_Declaration                     |
2521                N_Object_Renaming_Declaration            |
2522                N_Package_Body                           |
2523                N_Package_Body_Stub                      |
2524                N_Package_Declaration                    |
2525                N_Package_Instantiation                  |
2526                N_Package_Renaming_Declaration           |
2527                N_Private_Extension_Declaration          |
2528                N_Private_Type_Declaration               |
2529                N_Procedure_Instantiation                |
2530                N_Protected_Body                         |
2531                N_Protected_Body_Stub                    |
2532                N_Protected_Type_Declaration             |
2533                N_Single_Task_Declaration                |
2534                N_Subprogram_Body                        |
2535                N_Subprogram_Body_Stub                   |
2536                N_Subprogram_Declaration                 |
2537                N_Subprogram_Renaming_Declaration        |
2538                N_Subtype_Declaration                    |
2539                N_Task_Body                              |
2540                N_Task_Body_Stub                         |
2541                N_Task_Type_Declaration                  |
2542
2543                --  Freeze entity behaves like a declaration or statement
2544
2545                N_Freeze_Entity
2546             =>
2547                --  Do not insert here if the item is not a list member (this
2548                --  happens for example with a triggering statement, and the
2549                --  proper approach is to insert before the entire select).
2550
2551                if not Is_List_Member (P) then
2552                   null;
2553
2554                --  Do not insert if parent of P is an N_Component_Association
2555                --  node (i.e. we are in the context of an N_Aggregate or
2556                --  N_Extension_Aggregate node. In this case we want to insert
2557                --  before the entire aggregate.
2558
2559                elsif Nkind (Parent (P)) = N_Component_Association then
2560                   null;
2561
2562                --  Do not insert if the parent of P is either an N_Variant
2563                --  node or an N_Record_Definition node, meaning in either
2564                --  case that P is a member of a component list, and that
2565                --  therefore the actions should be inserted outside the
2566                --  complete record declaration.
2567
2568                elsif Nkind (Parent (P)) = N_Variant
2569                  or else Nkind (Parent (P)) = N_Record_Definition
2570                then
2571                   null;
2572
2573                --  Do not insert freeze nodes within the loop generated for
2574                --  an aggregate, because they may be elaborated too late for
2575                --  subsequent use in the back end: within a package spec the
2576                --  loop is part of the elaboration procedure and is only
2577                --  elaborated during the second pass.
2578                --  If the loop comes from source, or the entity is local to
2579                --  the loop itself it must remain within.
2580
2581                elsif Nkind (Parent (P)) = N_Loop_Statement
2582                  and then not Comes_From_Source (Parent (P))
2583                  and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2584                  and then
2585                    Scope (Entity (First (Ins_Actions))) /= Current_Scope
2586                then
2587                   null;
2588
2589                --  Otherwise we can go ahead and do the insertion
2590
2591                elsif P = Wrapped_Node then
2592                   Store_Before_Actions_In_Scope (Ins_Actions);
2593                   return;
2594
2595                else
2596                   Insert_List_Before_And_Analyze (P, Ins_Actions);
2597                   return;
2598                end if;
2599
2600             --  A special case, N_Raise_xxx_Error can act either as a
2601             --  statement or a subexpression. We tell the difference
2602             --  by looking at the Etype. It is set to Standard_Void_Type
2603             --  in the statement case.
2604
2605             when
2606                N_Raise_xxx_Error =>
2607                   if Etype (P) = Standard_Void_Type then
2608                      if  P = Wrapped_Node then
2609                         Store_Before_Actions_In_Scope (Ins_Actions);
2610                      else
2611                         Insert_List_Before_And_Analyze (P, Ins_Actions);
2612                      end if;
2613
2614                      return;
2615
2616                   --  In the subexpression case, keep climbing
2617
2618                   else
2619                      null;
2620                   end if;
2621
2622             --  If a component association appears within a loop created for
2623             --  an array aggregate, attach the actions to the association so
2624             --  they can be subsequently inserted within the loop. For other
2625             --  component associations insert outside of the aggregate. For
2626             --  an association that will generate a loop, its Loop_Actions
2627             --  attribute is already initialized (see exp_aggr.adb).
2628
2629             --  The list of loop_actions can in turn generate additional ones,
2630             --  that are inserted before the associated node. If the associated
2631             --  node is outside the aggregate, the new actions are collected
2632             --  at the end of the loop actions, to respect the order in which
2633             --  they are to be elaborated.
2634
2635             when
2636                N_Component_Association =>
2637                   if Nkind (Parent (P)) = N_Aggregate
2638                     and then Present (Loop_Actions (P))
2639                   then
2640                      if Is_Empty_List (Loop_Actions (P)) then
2641                         Set_Loop_Actions (P, Ins_Actions);
2642                         Analyze_List (Ins_Actions);
2643
2644                      else
2645                         declare
2646                            Decl : Node_Id;
2647
2648                         begin
2649                            --  Check whether these actions were generated
2650                            --  by a declaration that is part of the loop_
2651                            --  actions for the component_association.
2652
2653                            Decl := Assoc_Node;
2654                            while Present (Decl) loop
2655                               exit when Parent (Decl) = P
2656                                 and then Is_List_Member (Decl)
2657                                 and then
2658                                   List_Containing (Decl) = Loop_Actions (P);
2659                               Decl := Parent (Decl);
2660                            end loop;
2661
2662                            if Present (Decl) then
2663                               Insert_List_Before_And_Analyze
2664                                 (Decl, Ins_Actions);
2665                            else
2666                               Insert_List_After_And_Analyze
2667                                 (Last (Loop_Actions (P)), Ins_Actions);
2668                            end if;
2669                         end;
2670                      end if;
2671
2672                      return;
2673
2674                   else
2675                      null;
2676                   end if;
2677
2678             --  Another special case, an attribute denoting a procedure call
2679
2680             when
2681                N_Attribute_Reference =>
2682                   if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2683                      if P = Wrapped_Node then
2684                         Store_Before_Actions_In_Scope (Ins_Actions);
2685                      else
2686                         Insert_List_Before_And_Analyze (P, Ins_Actions);
2687                      end if;
2688
2689                      return;
2690
2691                   --  In the subexpression case, keep climbing
2692
2693                   else
2694                      null;
2695                   end if;
2696
2697             --  For all other node types, keep climbing tree
2698
2699             when
2700                N_Abortable_Part                         |
2701                N_Accept_Alternative                     |
2702                N_Access_Definition                      |
2703                N_Access_Function_Definition             |
2704                N_Access_Procedure_Definition            |
2705                N_Access_To_Object_Definition            |
2706                N_Aggregate                              |
2707                N_Allocator                              |
2708                N_Case_Expression                        |
2709                N_Case_Statement_Alternative             |
2710                N_Character_Literal                      |
2711                N_Compilation_Unit                       |
2712                N_Compilation_Unit_Aux                   |
2713                N_Component_Clause                       |
2714                N_Component_Declaration                  |
2715                N_Component_Definition                   |
2716                N_Component_List                         |
2717                N_Constrained_Array_Definition           |
2718                N_Decimal_Fixed_Point_Definition         |
2719                N_Defining_Character_Literal             |
2720                N_Defining_Identifier                    |
2721                N_Defining_Operator_Symbol               |
2722                N_Defining_Program_Unit_Name             |
2723                N_Delay_Alternative                      |
2724                N_Delta_Constraint                       |
2725                N_Derived_Type_Definition                |
2726                N_Designator                             |
2727                N_Digits_Constraint                      |
2728                N_Discriminant_Association               |
2729                N_Discriminant_Specification             |
2730                N_Empty                                  |
2731                N_Entry_Body_Formal_Part                 |
2732                N_Entry_Call_Alternative                 |
2733                N_Entry_Declaration                      |
2734                N_Entry_Index_Specification              |
2735                N_Enumeration_Type_Definition            |
2736                N_Error                                  |
2737                N_Exception_Handler                      |
2738                N_Expanded_Name                          |
2739                N_Explicit_Dereference                   |
2740                N_Extension_Aggregate                    |
2741                N_Floating_Point_Definition              |
2742                N_Formal_Decimal_Fixed_Point_Definition  |
2743                N_Formal_Derived_Type_Definition         |
2744                N_Formal_Discrete_Type_Definition        |
2745                N_Formal_Floating_Point_Definition       |
2746                N_Formal_Modular_Type_Definition         |
2747                N_Formal_Ordinary_Fixed_Point_Definition |
2748                N_Formal_Package_Declaration             |
2749                N_Formal_Private_Type_Definition         |
2750                N_Formal_Signed_Integer_Type_Definition  |
2751                N_Function_Call                          |
2752                N_Function_Specification                 |
2753                N_Generic_Association                    |
2754                N_Handled_Sequence_Of_Statements         |
2755                N_Identifier                             |
2756                N_In                                     |
2757                N_Index_Or_Discriminant_Constraint       |
2758                N_Indexed_Component                      |
2759                N_Integer_Literal                        |
2760                N_Itype_Reference                        |
2761                N_Label                                  |
2762                N_Loop_Parameter_Specification           |
2763                N_Mod_Clause                             |
2764                N_Modular_Type_Definition                |
2765                N_Not_In                                 |
2766                N_Null                                   |
2767                N_Op_Abs                                 |
2768                N_Op_Add                                 |
2769                N_Op_And                                 |
2770                N_Op_Concat                              |
2771                N_Op_Divide                              |
2772                N_Op_Eq                                  |
2773                N_Op_Expon                               |
2774                N_Op_Ge                                  |
2775                N_Op_Gt                                  |
2776                N_Op_Le                                  |
2777                N_Op_Lt                                  |
2778                N_Op_Minus                               |
2779                N_Op_Mod                                 |
2780                N_Op_Multiply                            |
2781                N_Op_Ne                                  |
2782                N_Op_Not                                 |
2783                N_Op_Or                                  |
2784                N_Op_Plus                                |
2785                N_Op_Rem                                 |
2786                N_Op_Rotate_Left                         |
2787                N_Op_Rotate_Right                        |
2788                N_Op_Shift_Left                          |
2789                N_Op_Shift_Right                         |
2790                N_Op_Shift_Right_Arithmetic              |
2791                N_Op_Subtract                            |
2792                N_Op_Xor                                 |
2793                N_Operator_Symbol                        |
2794                N_Ordinary_Fixed_Point_Definition        |
2795                N_Others_Choice                          |
2796                N_Package_Specification                  |
2797                N_Parameter_Association                  |
2798                N_Parameter_Specification                |
2799                N_Pop_Constraint_Error_Label             |
2800                N_Pop_Program_Error_Label                |
2801                N_Pop_Storage_Error_Label                |
2802                N_Pragma_Argument_Association            |
2803                N_Procedure_Specification                |
2804                N_Protected_Definition                   |
2805                N_Push_Constraint_Error_Label            |
2806                N_Push_Program_Error_Label               |
2807                N_Push_Storage_Error_Label               |
2808                N_Qualified_Expression                   |
2809                N_Range                                  |
2810                N_Range_Constraint                       |
2811                N_Real_Literal                           |
2812                N_Real_Range_Specification               |
2813                N_Record_Definition                      |
2814                N_Reference                              |
2815                N_SCIL_Dispatch_Table_Object_Init        |
2816                N_SCIL_Dispatch_Table_Tag_Init           |
2817                N_SCIL_Dispatching_Call                  |
2818                N_SCIL_Membership_Test                   |
2819                N_SCIL_Tag_Init                          |
2820                N_Selected_Component                     |
2821                N_Signed_Integer_Type_Definition         |
2822                N_Single_Protected_Declaration           |
2823                N_Slice                                  |
2824                N_String_Literal                         |
2825                N_Subprogram_Info                        |
2826                N_Subtype_Indication                     |
2827                N_Subunit                                |
2828                N_Task_Definition                        |
2829                N_Terminate_Alternative                  |
2830                N_Triggering_Alternative                 |
2831                N_Type_Conversion                        |
2832                N_Unchecked_Expression                   |
2833                N_Unchecked_Type_Conversion              |
2834                N_Unconstrained_Array_Definition         |
2835                N_Unused_At_End                          |
2836                N_Unused_At_Start                        |
2837                N_Use_Package_Clause                     |
2838                N_Use_Type_Clause                        |
2839                N_Variant                                |
2840                N_Variant_Part                           |
2841                N_Validate_Unchecked_Conversion          |
2842                N_With_Clause
2843             =>
2844                null;
2845
2846          end case;
2847
2848          --  Make sure that inserted actions stay in the transient scope
2849
2850          if P = Wrapped_Node then
2851             Store_Before_Actions_In_Scope (Ins_Actions);
2852             return;
2853          end if;
2854
2855          --  If we fall through above tests, keep climbing tree
2856
2857          N := P;
2858
2859          if Nkind (Parent (N)) = N_Subunit then
2860
2861             --  This is the proper body corresponding to a stub. Insertion
2862             --  must be done at the point of the stub, which is in the decla-
2863             --  rative part of the parent unit.
2864
2865             P := Corresponding_Stub (Parent (N));
2866
2867          else
2868             P := Parent (N);
2869          end if;
2870       end loop;
2871    end Insert_Actions;
2872
2873    --  Version with check(s) suppressed
2874
2875    procedure Insert_Actions
2876      (Assoc_Node  : Node_Id;
2877       Ins_Actions : List_Id;
2878       Suppress    : Check_Id)
2879    is
2880    begin
2881       if Suppress = All_Checks then
2882          declare
2883             Svg : constant Suppress_Array := Scope_Suppress;
2884          begin
2885             Scope_Suppress := (others => True);
2886             Insert_Actions (Assoc_Node, Ins_Actions);
2887             Scope_Suppress := Svg;
2888          end;
2889
2890       else
2891          declare
2892             Svg : constant Boolean := Scope_Suppress (Suppress);
2893          begin
2894             Scope_Suppress (Suppress) := True;
2895             Insert_Actions (Assoc_Node, Ins_Actions);
2896             Scope_Suppress (Suppress) := Svg;
2897          end;
2898       end if;
2899    end Insert_Actions;
2900
2901    --------------------------
2902    -- Insert_Actions_After --
2903    --------------------------
2904
2905    procedure Insert_Actions_After
2906      (Assoc_Node  : Node_Id;
2907       Ins_Actions : List_Id)
2908    is
2909    begin
2910       if Scope_Is_Transient
2911         and then Assoc_Node = Node_To_Be_Wrapped
2912       then
2913          Store_After_Actions_In_Scope (Ins_Actions);
2914       else
2915          Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2916       end if;
2917    end Insert_Actions_After;
2918
2919    ---------------------------------
2920    -- Insert_Library_Level_Action --
2921    ---------------------------------
2922
2923    procedure Insert_Library_Level_Action (N : Node_Id) is
2924       Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2925
2926    begin
2927       Push_Scope (Cunit_Entity (Main_Unit));
2928       --  ??? should this be Current_Sem_Unit instead of Main_Unit?
2929
2930       if No (Actions (Aux)) then
2931          Set_Actions (Aux, New_List (N));
2932       else
2933          Append (N, Actions (Aux));
2934       end if;
2935
2936       Analyze (N);
2937       Pop_Scope;
2938    end Insert_Library_Level_Action;
2939
2940    ----------------------------------
2941    -- Insert_Library_Level_Actions --
2942    ----------------------------------
2943
2944    procedure Insert_Library_Level_Actions (L : List_Id) is
2945       Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2946
2947    begin
2948       if Is_Non_Empty_List (L) then
2949          Push_Scope (Cunit_Entity (Main_Unit));
2950          --  ??? should this be Current_Sem_Unit instead of Main_Unit?
2951
2952          if No (Actions (Aux)) then
2953             Set_Actions (Aux, L);
2954             Analyze_List (L);
2955          else
2956             Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2957          end if;
2958
2959          Pop_Scope;
2960       end if;
2961    end Insert_Library_Level_Actions;
2962
2963    ----------------------
2964    -- Inside_Init_Proc --
2965    ----------------------
2966
2967    function Inside_Init_Proc return Boolean is
2968       S : Entity_Id;
2969
2970    begin
2971       S := Current_Scope;
2972       while Present (S)
2973         and then S /= Standard_Standard
2974       loop
2975          if Is_Init_Proc (S) then
2976             return True;
2977          else
2978             S := Scope (S);
2979          end if;
2980       end loop;
2981
2982       return False;
2983    end Inside_Init_Proc;
2984
2985    ----------------------------
2986    -- Is_All_Null_Statements --
2987    ----------------------------
2988
2989    function Is_All_Null_Statements (L : List_Id) return Boolean is
2990       Stm : Node_Id;
2991
2992    begin
2993       Stm := First (L);
2994       while Present (Stm) loop
2995          if Nkind (Stm) /= N_Null_Statement then
2996             return False;
2997          end if;
2998
2999          Next (Stm);
3000       end loop;
3001
3002       return True;
3003    end Is_All_Null_Statements;
3004
3005    ---------------------------------
3006    -- Is_Fully_Repped_Tagged_Type --
3007    ---------------------------------
3008
3009    function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
3010       U    : constant Entity_Id := Underlying_Type (T);
3011       Comp : Entity_Id;
3012
3013    begin
3014       if No (U) or else not Is_Tagged_Type (U) then
3015          return False;
3016       elsif Has_Discriminants (U) then
3017          return False;
3018       elsif not Has_Specified_Layout (U) then
3019          return False;
3020       end if;
3021
3022       --  Here we have a tagged type, see if it has any unlayed out fields
3023       --  other than a possible tag and parent fields. If so, we return False.
3024
3025       Comp := First_Component (U);
3026       while Present (Comp) loop
3027          if not Is_Tag (Comp)
3028            and then Chars (Comp) /= Name_uParent
3029            and then No (Component_Clause (Comp))
3030          then
3031             return False;
3032          else
3033             Next_Component (Comp);
3034          end if;
3035       end loop;
3036
3037       --  All components are layed out
3038
3039       return True;
3040    end Is_Fully_Repped_Tagged_Type;
3041
3042    ----------------------------------
3043    -- Is_Library_Level_Tagged_Type --
3044    ----------------------------------
3045
3046    function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
3047    begin
3048       return Is_Tagged_Type (Typ)
3049         and then Is_Library_Level_Entity (Typ);
3050    end Is_Library_Level_Tagged_Type;
3051
3052    ----------------------------------
3053    -- Is_Possibly_Unaligned_Object --
3054    ----------------------------------
3055
3056    function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
3057       T  : constant Entity_Id := Etype (N);
3058
3059    begin
3060       --  If renamed object, apply test to underlying object
3061
3062       if Is_Entity_Name (N)
3063         and then Is_Object (Entity (N))
3064         and then Present (Renamed_Object (Entity (N)))
3065       then
3066          return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
3067       end if;
3068
3069       --  Tagged and controlled types and aliased types are always aligned,
3070       --  as are concurrent types.
3071
3072       if Is_Aliased (T)
3073         or else Has_Controlled_Component (T)
3074         or else Is_Concurrent_Type (T)
3075         or else Is_Tagged_Type (T)
3076         or else Is_Controlled (T)
3077       then
3078          return False;
3079       end if;
3080
3081       --  If this is an element of a packed array, may be unaligned
3082
3083       if Is_Ref_To_Bit_Packed_Array (N) then
3084          return True;
3085       end if;
3086
3087       --  Case of component reference
3088
3089       if Nkind (N) = N_Selected_Component then
3090          declare
3091             P : constant Node_Id   := Prefix (N);
3092             C : constant Entity_Id := Entity (Selector_Name (N));
3093             M : Nat;
3094             S : Nat;
3095
3096          begin
3097             --  If component reference is for an array with non-static bounds,
3098             --  then it is always aligned: we can only process unaligned
3099             --  arrays with static bounds (more accurately bounds known at
3100             --  compile time).
3101
3102             if Is_Array_Type (T)
3103               and then not Compile_Time_Known_Bounds (T)
3104             then
3105                return False;
3106             end if;
3107
3108             --  If component is aliased, it is definitely properly aligned
3109
3110             if Is_Aliased (C) then
3111                return False;
3112             end if;
3113
3114             --  If component is for a type implemented as a scalar, and the
3115             --  record is packed, and the component is other than the first
3116             --  component of the record, then the component may be unaligned.
3117
3118             if Is_Packed (Etype (P))
3119               and then Represented_As_Scalar (Etype (C))
3120               and then First_Entity (Scope (C)) /= C
3121             then
3122                return True;
3123             end if;
3124
3125             --  Compute maximum possible alignment for T
3126
3127             --  If alignment is known, then that settles things
3128
3129             if Known_Alignment (T) then
3130                M := UI_To_Int (Alignment (T));
3131
3132             --  If alignment is not known, tentatively set max alignment
3133
3134             else
3135                M := Ttypes.Maximum_Alignment;
3136
3137                --  We can reduce this if the Esize is known since the default
3138                --  alignment will never be more than the smallest power of 2
3139                --  that does not exceed this Esize value.
3140
3141                if Known_Esize (T) then
3142                   S := UI_To_Int (Esize (T));
3143
3144                   while (M / 2) >= S loop
3145                      M := M / 2;
3146                   end loop;
3147                end if;
3148             end if;
3149
3150             --  If the component reference is for a record that has a specified
3151             --  alignment, and we either know it is too small, or cannot tell,
3152             --  then the component may be unaligned
3153
3154             if Known_Alignment (Etype (P))
3155               and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3156               and then M > Alignment (Etype (P))
3157             then
3158                return True;
3159             end if;
3160
3161             --  Case of component clause present which may specify an
3162             --  unaligned position.
3163
3164             if Present (Component_Clause (C)) then
3165
3166                --  Otherwise we can do a test to make sure that the actual
3167                --  start position in the record, and the length, are both
3168                --  consistent with the required alignment. If not, we know
3169                --  that we are unaligned.
3170
3171                declare
3172                   Align_In_Bits : constant Nat := M * System_Storage_Unit;
3173                begin
3174                   if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3175                     or else Esize (C) mod Align_In_Bits /= 0
3176                   then
3177                      return True;
3178                   end if;
3179                end;
3180             end if;
3181
3182             --  Otherwise, for a component reference, test prefix
3183
3184             return Is_Possibly_Unaligned_Object (P);
3185          end;
3186
3187       --  If not a component reference, must be aligned
3188
3189       else
3190          return False;
3191       end if;
3192    end Is_Possibly_Unaligned_Object;
3193
3194    ---------------------------------
3195    -- Is_Possibly_Unaligned_Slice --
3196    ---------------------------------
3197
3198    function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3199    begin
3200       --  Go to renamed object
3201
3202       if Is_Entity_Name (N)
3203         and then Is_Object (Entity (N))
3204         and then Present (Renamed_Object (Entity (N)))
3205       then
3206          return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3207       end if;
3208
3209       --  The reference must be a slice
3210
3211       if Nkind (N) /= N_Slice then
3212          return False;
3213       end if;
3214
3215       --  Always assume the worst for a nested record component with a
3216       --  component clause, which gigi/gcc does not appear to handle well.
3217       --  It is not clear why this special test is needed at all ???
3218
3219       if Nkind (Prefix (N)) = N_Selected_Component
3220         and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3221         and then
3222           Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3223       then
3224          return True;
3225       end if;
3226
3227       --  We only need to worry if the target has strict alignment
3228
3229       if not Target_Strict_Alignment then
3230          return False;
3231       end if;
3232
3233       --  If it is a slice, then look at the array type being sliced
3234
3235       declare
3236          Sarr : constant Node_Id := Prefix (N);
3237          --  Prefix of the slice, i.e. the array being sliced
3238
3239          Styp : constant Entity_Id := Etype (Prefix (N));
3240          --  Type of the array being sliced
3241
3242          Pref : Node_Id;
3243          Ptyp : Entity_Id;
3244
3245       begin
3246          --  The problems arise if the array object that is being sliced
3247          --  is a component of a record or array, and we cannot guarantee
3248          --  the alignment of the array within its containing object.
3249
3250          --  To investigate this, we look at successive prefixes to see
3251          --  if we have a worrisome indexed or selected component.
3252
3253          Pref := Sarr;
3254          loop
3255             --  Case of array is part of an indexed component reference
3256
3257             if Nkind (Pref) = N_Indexed_Component then
3258                Ptyp := Etype (Prefix (Pref));
3259
3260                --  The only problematic case is when the array is packed,
3261                --  in which case we really know nothing about the alignment
3262                --  of individual components.
3263
3264                if Is_Bit_Packed_Array (Ptyp) then
3265                   return True;
3266                end if;
3267
3268             --  Case of array is part of a selected component reference
3269
3270             elsif Nkind (Pref) = N_Selected_Component then
3271                Ptyp := Etype (Prefix (Pref));
3272
3273                --  We are definitely in trouble if the record in question
3274                --  has an alignment, and either we know this alignment is
3275                --  inconsistent with the alignment of the slice, or we
3276                --  don't know what the alignment of the slice should be.
3277
3278                if Known_Alignment (Ptyp)
3279                  and then (Unknown_Alignment (Styp)
3280                              or else Alignment (Styp) > Alignment (Ptyp))
3281                then
3282                   return True;
3283                end if;
3284
3285                --  We are in potential trouble if the record type is packed.
3286                --  We could special case when we know that the array is the
3287                --  first component, but that's not such a simple case ???
3288
3289                if Is_Packed (Ptyp) then
3290                   return True;
3291                end if;
3292
3293                --  We are in trouble if there is a component clause, and
3294                --  either we do not know the alignment of the slice, or
3295                --  the alignment of the slice is inconsistent with the
3296                --  bit position specified by the component clause.
3297
3298                declare
3299                   Field : constant Entity_Id := Entity (Selector_Name (Pref));
3300                begin
3301                   if Present (Component_Clause (Field))
3302                     and then
3303                       (Unknown_Alignment (Styp)
3304                         or else
3305                          (Component_Bit_Offset (Field) mod
3306                            (System_Storage_Unit * Alignment (Styp))) /= 0)
3307                   then
3308                      return True;
3309                   end if;
3310                end;
3311
3312             --  For cases other than selected or indexed components we
3313             --  know we are OK, since no issues arise over alignment.
3314
3315             else
3316                return False;
3317             end if;
3318
3319             --  We processed an indexed component or selected component
3320             --  reference that looked safe, so keep checking prefixes.
3321
3322             Pref := Prefix (Pref);
3323          end loop;
3324       end;
3325    end Is_Possibly_Unaligned_Slice;
3326
3327    --------------------------------
3328    -- Is_Ref_To_Bit_Packed_Array --
3329    --------------------------------
3330
3331    function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3332       Result : Boolean;
3333       Expr   : Node_Id;
3334
3335    begin
3336       if Is_Entity_Name (N)
3337         and then Is_Object (Entity (N))
3338         and then Present (Renamed_Object (Entity (N)))
3339       then
3340          return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3341       end if;
3342
3343       if Nkind (N) = N_Indexed_Component
3344            or else
3345          Nkind (N) = N_Selected_Component
3346       then
3347          if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3348             Result := True;
3349          else
3350             Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3351          end if;
3352
3353          if Result and then Nkind (N) = N_Indexed_Component then
3354             Expr := First (Expressions (N));
3355             while Present (Expr) loop
3356                Force_Evaluation (Expr);
3357                Next (Expr);
3358             end loop;
3359          end if;
3360
3361          return Result;
3362
3363       else
3364          return False;
3365       end if;
3366    end Is_Ref_To_Bit_Packed_Array;
3367
3368    --------------------------------
3369    -- Is_Ref_To_Bit_Packed_Slice --
3370    --------------------------------
3371
3372    function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3373    begin
3374       if Nkind (N) = N_Type_Conversion then
3375          return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3376
3377       elsif Is_Entity_Name (N)
3378         and then Is_Object (Entity (N))
3379         and then Present (Renamed_Object (Entity (N)))
3380       then
3381          return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3382
3383       elsif Nkind (N) = N_Slice
3384         and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3385       then
3386          return True;
3387
3388       elsif Nkind (N) = N_Indexed_Component
3389            or else
3390          Nkind (N) = N_Selected_Component
3391       then
3392          return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3393
3394       else
3395          return False;
3396       end if;
3397    end Is_Ref_To_Bit_Packed_Slice;
3398
3399    -----------------------
3400    -- Is_Renamed_Object --
3401    -----------------------
3402
3403    function Is_Renamed_Object (N : Node_Id) return Boolean is
3404       Pnod : constant Node_Id   := Parent (N);
3405       Kind : constant Node_Kind := Nkind (Pnod);
3406    begin
3407       if Kind = N_Object_Renaming_Declaration then
3408          return True;
3409       elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
3410          return Is_Renamed_Object (Pnod);
3411       else
3412          return False;
3413       end if;
3414    end Is_Renamed_Object;
3415
3416    ----------------------------
3417    -- Is_Untagged_Derivation --
3418    ----------------------------
3419
3420    function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3421    begin
3422       return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3423                or else
3424              (Is_Private_Type (T) and then Present (Full_View (T))
3425                and then not Is_Tagged_Type (Full_View (T))
3426                and then Is_Derived_Type (Full_View (T))
3427                and then Etype (Full_View (T)) /= T);
3428    end Is_Untagged_Derivation;
3429
3430    ---------------------------
3431    -- Is_Volatile_Reference --
3432    ---------------------------
3433
3434    function Is_Volatile_Reference (N : Node_Id) return Boolean is
3435    begin
3436       if Nkind (N) in N_Has_Etype
3437         and then Present (Etype (N))
3438         and then Treat_As_Volatile (Etype (N))
3439       then
3440          return True;
3441
3442       elsif Is_Entity_Name (N) then
3443          return Treat_As_Volatile (Entity (N));
3444
3445       elsif Nkind (N) = N_Slice then
3446          return Is_Volatile_Reference (Prefix (N));
3447
3448       elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
3449          if (Is_Entity_Name (Prefix (N))
3450                and then Has_Volatile_Components (Entity (Prefix (N))))
3451            or else (Present (Etype (Prefix (N)))
3452                       and then Has_Volatile_Components (Etype (Prefix (N))))
3453          then
3454             return True;
3455          else
3456             return Is_Volatile_Reference (Prefix (N));
3457          end if;
3458
3459       else
3460          return False;
3461       end if;
3462    end Is_Volatile_Reference;
3463
3464    --------------------
3465    -- Kill_Dead_Code --
3466    --------------------
3467
3468    procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3469       W : Boolean := Warn;
3470       --  Set False if warnings suppressed
3471
3472    begin
3473       if Present (N) then
3474          Remove_Warning_Messages (N);
3475
3476          --  Generate warning if appropriate
3477
3478          if W then
3479
3480             --  We suppress the warning if this code is under control of an
3481             --  if statement, whose condition is a simple identifier, and
3482             --  either we are in an instance, or warnings off is set for this
3483             --  identifier. The reason for killing it in the instance case is
3484             --  that it is common and reasonable for code to be deleted in
3485             --  instances for various reasons.
3486
3487             if Nkind (Parent (N)) = N_If_Statement then
3488                declare
3489                   C : constant Node_Id := Condition (Parent (N));
3490                begin
3491                   if Nkind (C) = N_Identifier
3492                     and then
3493                       (In_Instance
3494                         or else (Present (Entity (C))
3495                                    and then Has_Warnings_Off (Entity (C))))
3496                   then
3497                      W := False;
3498                   end if;
3499                end;
3500             end if;
3501
3502             --  Generate warning if not suppressed
3503
3504             if W then
3505                Error_Msg_F
3506                  ("?this code can never be executed and has been deleted!", N);
3507             end if;
3508          end if;
3509
3510          --  Recurse into block statements and bodies to process declarations
3511          --  and statements.
3512
3513          if Nkind (N) = N_Block_Statement
3514            or else Nkind (N) = N_Subprogram_Body
3515            or else Nkind (N) = N_Package_Body
3516          then
3517             Kill_Dead_Code (Declarations (N), False);
3518             Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
3519
3520             if Nkind (N) = N_Subprogram_Body then
3521                Set_Is_Eliminated (Defining_Entity (N));
3522             end if;
3523
3524          elsif Nkind (N) = N_Package_Declaration then
3525             Kill_Dead_Code (Visible_Declarations (Specification (N)));
3526             Kill_Dead_Code (Private_Declarations (Specification (N)));
3527
3528             --  ??? After this point, Delete_Tree has been called on all
3529             --  declarations in Specification (N), so references to
3530             --  entities therein look suspicious.
3531
3532             declare
3533                E : Entity_Id := First_Entity (Defining_Entity (N));
3534             begin
3535                while Present (E) loop
3536                   if Ekind (E) = E_Operator then
3537                      Set_Is_Eliminated (E);
3538                   end if;
3539
3540                   Next_Entity (E);
3541                end loop;
3542             end;
3543
3544          --  Recurse into composite statement to kill individual statements,
3545          --  in particular instantiations.
3546
3547          elsif Nkind (N) = N_If_Statement then
3548             Kill_Dead_Code (Then_Statements (N));
3549             Kill_Dead_Code (Elsif_Parts (N));
3550             Kill_Dead_Code (Else_Statements (N));
3551
3552          elsif Nkind (N) = N_Loop_Statement then
3553             Kill_Dead_Code (Statements (N));
3554
3555          elsif Nkind (N) = N_Case_Statement then
3556             declare
3557                Alt : Node_Id;
3558             begin
3559                Alt := First (Alternatives (N));
3560                while Present (Alt) loop
3561                   Kill_Dead_Code (Statements (Alt));
3562                   Next (Alt);
3563                end loop;
3564             end;
3565
3566          elsif Nkind (N) = N_Case_Statement_Alternative then
3567             Kill_Dead_Code (Statements (N));
3568
3569          --  Deal with dead instances caused by deleting instantiations
3570
3571          elsif Nkind (N) in N_Generic_Instantiation then
3572             Remove_Dead_Instance (N);
3573          end if;
3574       end if;
3575    end Kill_Dead_Code;
3576
3577    --  Case where argument is a list of nodes to be killed
3578
3579    procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3580       N : Node_Id;
3581       W : Boolean;
3582    begin
3583       W := Warn;
3584       if Is_Non_Empty_List (L) then
3585          N := First (L);
3586          while Present (N) loop
3587             Kill_Dead_Code (N, W);
3588             W := False;
3589             Next (N);
3590          end loop;
3591       end if;
3592    end Kill_Dead_Code;
3593
3594    ------------------------
3595    -- Known_Non_Negative --
3596    ------------------------
3597
3598    function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3599    begin
3600       if Is_OK_Static_Expression (Opnd)
3601         and then Expr_Value (Opnd) >= 0
3602       then
3603          return True;
3604
3605       else
3606          declare
3607             Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3608
3609          begin
3610             return
3611               Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3612          end;
3613       end if;
3614    end Known_Non_Negative;
3615
3616    --------------------
3617    -- Known_Non_Null --
3618    --------------------
3619
3620    function Known_Non_Null (N : Node_Id) return Boolean is
3621    begin
3622       --  Checks for case where N is an entity reference
3623
3624       if Is_Entity_Name (N) and then Present (Entity (N)) then
3625          declare
3626             E   : constant Entity_Id := Entity (N);
3627             Op  : Node_Kind;
3628             Val : Node_Id;
3629
3630          begin
3631             --  First check if we are in decisive conditional
3632
3633             Get_Current_Value_Condition (N, Op, Val);
3634
3635             if Known_Null (Val) then
3636                if Op = N_Op_Eq then
3637                   return False;
3638                elsif Op = N_Op_Ne then
3639                   return True;
3640                end if;
3641             end if;
3642
3643             --  If OK to do replacement, test Is_Known_Non_Null flag
3644
3645             if OK_To_Do_Constant_Replacement (E) then
3646                return Is_Known_Non_Null (E);
3647
3648             --  Otherwise if not safe to do replacement, then say so
3649
3650             else
3651                return False;
3652             end if;
3653          end;
3654
3655       --  True if access attribute
3656
3657       elsif Nkind (N) = N_Attribute_Reference
3658         and then (Attribute_Name (N) = Name_Access
3659                     or else
3660                   Attribute_Name (N) = Name_Unchecked_Access
3661                     or else
3662                   Attribute_Name (N) = Name_Unrestricted_Access)
3663       then
3664          return True;
3665
3666       --  True if allocator
3667
3668       elsif Nkind (N) = N_Allocator then
3669          return True;
3670
3671       --  For a conversion, true if expression is known non-null
3672
3673       elsif Nkind (N) = N_Type_Conversion then
3674          return Known_Non_Null (Expression (N));
3675
3676       --  Above are all cases where the value could be determined to be
3677       --  non-null. In all other cases, we don't know, so return False.
3678
3679       else
3680          return False;
3681       end if;
3682    end Known_Non_Null;
3683
3684    ----------------
3685    -- Known_Null --
3686    ----------------
3687
3688    function Known_Null (N : Node_Id) return Boolean is
3689    begin
3690       --  Checks for case where N is an entity reference
3691
3692       if Is_Entity_Name (N) and then Present (Entity (N)) then
3693          declare
3694             E   : constant Entity_Id := Entity (N);
3695             Op  : Node_Kind;
3696             Val : Node_Id;
3697
3698          begin
3699             --  Constant null value is for sure null
3700
3701             if Ekind (E) = E_Constant
3702               and then Known_Null (Constant_Value (E))
3703             then
3704                return True;
3705             end if;
3706
3707             --  First check if we are in decisive conditional
3708
3709             Get_Current_Value_Condition (N, Op, Val);
3710
3711             if Known_Null (Val) then
3712                if Op = N_Op_Eq then
3713                   return True;
3714                elsif Op = N_Op_Ne then
3715                   return False;
3716                end if;
3717             end if;
3718
3719             --  If OK to do replacement, test Is_Known_Null flag
3720
3721             if OK_To_Do_Constant_Replacement (E) then
3722                return Is_Known_Null (E);
3723
3724             --  Otherwise if not safe to do replacement, then say so
3725
3726             else
3727                return False;
3728             end if;
3729          end;
3730
3731       --  True if explicit reference to null
3732
3733       elsif Nkind (N) = N_Null then
3734          return True;
3735
3736       --  For a conversion, true if expression is known null
3737
3738       elsif Nkind (N) = N_Type_Conversion then
3739          return Known_Null (Expression (N));
3740
3741       --  Above are all cases where the value could be determined to be null.
3742       --  In all other cases, we don't know, so return False.
3743
3744       else
3745          return False;
3746       end if;
3747    end Known_Null;
3748
3749    -----------------------------
3750    -- Make_CW_Equivalent_Type --
3751    -----------------------------
3752
3753    --  Create a record type used as an equivalent of any member of the class
3754    --  which takes its size from exp.
3755
3756    --  Generate the following code:
3757
3758    --   type Equiv_T is record
3759    --     _parent :  T (List of discriminant constraints taken from Exp);
3760    --     Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3761    --   end Equiv_T;
3762    --
3763    --   ??? Note that this type does not guarantee same alignment as all
3764    --   derived types
3765
3766    function Make_CW_Equivalent_Type
3767      (T : Entity_Id;
3768       E : Node_Id) return Entity_Id
3769    is
3770       Loc         : constant Source_Ptr := Sloc (E);
3771       Root_Typ    : constant Entity_Id  := Root_Type (T);
3772       List_Def    : constant List_Id    := Empty_List;
3773       Comp_List   : constant List_Id    := New_List;
3774       Equiv_Type  : Entity_Id;
3775       Range_Type  : Entity_Id;
3776       Str_Type    : Entity_Id;
3777       Constr_Root : Entity_Id;
3778       Sizexpr     : Node_Id;
3779
3780    begin
3781       --  If the root type is already constrained, there are no discriminants
3782       --  in the expression.
3783
3784       if not Has_Discriminants (Root_Typ)
3785         or else Is_Constrained (Root_Typ)
3786       then
3787          Constr_Root := Root_Typ;
3788       else
3789          Constr_Root := Make_Temporary (Loc, 'R');
3790
3791          --  subtype cstr__n is T (List of discr constraints taken from Exp)
3792
3793          Append_To (List_Def,
3794            Make_Subtype_Declaration (Loc,
3795              Defining_Identifier => Constr_Root,
3796              Subtype_Indication  => Make_Subtype_From_Expr (E, Root_Typ)));
3797       end if;
3798
3799       --  Generate the range subtype declaration
3800
3801       Range_Type := Make_Temporary (Loc, 'G');
3802
3803       if not Is_Interface (Root_Typ) then
3804
3805          --  subtype rg__xx is
3806          --    Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
3807
3808          Sizexpr :=
3809            Make_Op_Subtract (Loc,
3810              Left_Opnd =>
3811                Make_Attribute_Reference (Loc,
3812                  Prefix =>
3813                    OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3814                  Attribute_Name => Name_Size),
3815              Right_Opnd =>
3816                Make_Attribute_Reference (Loc,
3817                  Prefix => New_Reference_To (Constr_Root, Loc),
3818                  Attribute_Name => Name_Object_Size));
3819       else
3820          --  subtype rg__xx is
3821          --    Storage_Offset range 1 .. Expr'size / Storage_Unit
3822
3823          Sizexpr :=
3824            Make_Attribute_Reference (Loc,
3825              Prefix =>
3826                OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3827              Attribute_Name => Name_Size);
3828       end if;
3829
3830       Set_Paren_Count (Sizexpr, 1);
3831
3832       Append_To (List_Def,
3833         Make_Subtype_Declaration (Loc,
3834           Defining_Identifier => Range_Type,
3835           Subtype_Indication =>
3836             Make_Subtype_Indication (Loc,
3837               Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3838               Constraint => Make_Range_Constraint (Loc,
3839                 Range_Expression =>
3840                   Make_Range (Loc,
3841                     Low_Bound => Make_Integer_Literal (Loc, 1),
3842                     High_Bound =>
3843                       Make_Op_Divide (Loc,
3844                         Left_Opnd => Sizexpr,
3845                         Right_Opnd => Make_Integer_Literal (Loc,
3846                             Intval => System_Storage_Unit)))))));
3847
3848       --  subtype str__nn is Storage_Array (rg__x);
3849
3850       Str_Type := Make_Temporary (Loc, 'S');
3851       Append_To (List_Def,
3852         Make_Subtype_Declaration (Loc,
3853           Defining_Identifier => Str_Type,
3854           Subtype_Indication =>
3855             Make_Subtype_Indication (Loc,
3856               Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3857               Constraint =>
3858                 Make_Index_Or_Discriminant_Constraint (Loc,
3859                   Constraints =>
3860                     New_List (New_Reference_To (Range_Type, Loc))))));
3861
3862       --  type Equiv_T is record
3863       --    [ _parent : Tnn; ]
3864       --    E : Str_Type;
3865       --  end Equiv_T;
3866
3867       Equiv_Type := Make_Temporary (Loc, 'T');
3868       Set_Ekind (Equiv_Type, E_Record_Type);
3869       Set_Parent_Subtype (Equiv_Type, Constr_Root);
3870
3871       --  Set Is_Class_Wide_Equivalent_Type very early to trigger the special
3872       --  treatment for this type. In particular, even though _parent's type
3873       --  is a controlled type or contains controlled components, we do not
3874       --  want to set Has_Controlled_Component on it to avoid making it gain
3875       --  an unwanted _controller component.
3876
3877       Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
3878
3879       if not Is_Interface (Root_Typ) then
3880          Append_To (Comp_List,
3881            Make_Component_Declaration (Loc,
3882              Defining_Identifier =>
3883                Make_Defining_Identifier (Loc, Name_uParent),
3884              Component_Definition =>
3885                Make_Component_Definition (Loc,
3886                  Aliased_Present    => False,
3887                  Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
3888       end if;
3889
3890       Append_To (Comp_List,
3891         Make_Component_Declaration (Loc,
3892           Defining_Identifier  => Make_Temporary (Loc, 'C'),
3893           Component_Definition =>
3894             Make_Component_Definition (Loc,
3895               Aliased_Present    => False,
3896               Subtype_Indication => New_Reference_To (Str_Type, Loc))));
3897
3898       Append_To (List_Def,
3899         Make_Full_Type_Declaration (Loc,
3900           Defining_Identifier => Equiv_Type,
3901           Type_Definition =>
3902             Make_Record_Definition (Loc,
3903               Component_List =>
3904                 Make_Component_List (Loc,
3905                   Component_Items => Comp_List,
3906                   Variant_Part    => Empty))));
3907
3908       --  Suppress all checks during the analysis of the expanded code
3909       --  to avoid the generation of spurious warnings under ZFP run-time.
3910
3911       Insert_Actions (E, List_Def, Suppress => All_Checks);
3912       return Equiv_Type;
3913    end Make_CW_Equivalent_Type;
3914
3915    ------------------------
3916    -- Make_Literal_Range --
3917    ------------------------
3918
3919    function Make_Literal_Range
3920      (Loc         : Source_Ptr;
3921       Literal_Typ : Entity_Id) return Node_Id
3922    is
3923       Lo          : constant Node_Id :=
3924                       New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
3925       Index       : constant Entity_Id := Etype (Lo);
3926
3927       Hi          : Node_Id;
3928       Length_Expr : constant Node_Id :=
3929                       Make_Op_Subtract (Loc,
3930                         Left_Opnd =>
3931                           Make_Integer_Literal (Loc,
3932                             Intval => String_Literal_Length (Literal_Typ)),
3933                         Right_Opnd =>
3934                           Make_Integer_Literal (Loc, 1));
3935
3936    begin
3937       Set_Analyzed (Lo, False);
3938
3939          if Is_Integer_Type (Index) then
3940             Hi :=
3941               Make_Op_Add (Loc,
3942                 Left_Opnd  => New_Copy_Tree (Lo),
3943                 Right_Opnd => Length_Expr);
3944          else
3945             Hi :=
3946               Make_Attribute_Reference (Loc,
3947                 Attribute_Name => Name_Val,
3948                 Prefix => New_Occurrence_Of (Index, Loc),
3949                 Expressions => New_List (
3950                  Make_Op_Add (Loc,
3951                    Left_Opnd =>
3952                      Make_Attribute_Reference (Loc,
3953                        Attribute_Name => Name_Pos,
3954                        Prefix => New_Occurrence_Of (Index, Loc),
3955                        Expressions => New_List (New_Copy_Tree (Lo))),
3956                   Right_Opnd => Length_Expr)));
3957          end if;
3958
3959          return
3960            Make_Range (Loc,
3961              Low_Bound  => Lo,
3962              High_Bound => Hi);
3963    end Make_Literal_Range;
3964
3965    --------------------------
3966    -- Make_Non_Empty_Check --
3967    --------------------------
3968
3969    function Make_Non_Empty_Check
3970      (Loc : Source_Ptr;
3971       N   : Node_Id) return Node_Id
3972    is
3973    begin
3974       return
3975         Make_Op_Ne (Loc,
3976           Left_Opnd =>
3977             Make_Attribute_Reference (Loc,
3978               Attribute_Name => Name_Length,
3979               Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
3980           Right_Opnd =>
3981             Make_Integer_Literal (Loc, 0));
3982    end Make_Non_Empty_Check;
3983
3984    ----------------------------
3985    -- Make_Subtype_From_Expr --
3986    ----------------------------
3987
3988    --  1. If Expr is an unconstrained array expression, creates
3989    --    Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
3990
3991    --  2. If Expr is a unconstrained discriminated type expression, creates
3992    --    Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
3993
3994    --  3. If Expr is class-wide, creates an implicit class wide subtype
3995
3996    function Make_Subtype_From_Expr
3997      (E       : Node_Id;
3998       Unc_Typ : Entity_Id) return Node_Id
3999    is
4000       Loc         : constant Source_Ptr := Sloc (E);
4001       List_Constr : constant List_Id    := New_List;
4002       D           : Entity_Id;
4003
4004       Full_Subtyp  : Entity_Id;
4005       Priv_Subtyp  : Entity_Id;
4006       Utyp         : Entity_Id;
4007       Full_Exp     : Node_Id;
4008
4009    begin
4010       if Is_Private_Type (Unc_Typ)
4011         and then Has_Unknown_Discriminants (Unc_Typ)
4012       then
4013          --  Prepare the subtype completion, Go to base type to
4014          --  find underlying type, because the type may be a generic
4015          --  actual or an explicit subtype.
4016
4017          Utyp        := Underlying_Type (Base_Type (Unc_Typ));
4018          Full_Subtyp := Make_Temporary (Loc, 'C');
4019          Full_Exp    :=
4020            Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
4021          Set_Parent (Full_Exp, Parent (E));
4022
4023          Priv_Subtyp := Make_Temporary (Loc, 'P');
4024
4025          Insert_Action (E,
4026            Make_Subtype_Declaration (Loc,
4027              Defining_Identifier => Full_Subtyp,
4028              Subtype_Indication  => Make_Subtype_From_Expr (Full_Exp, Utyp)));
4029
4030          --  Define the dummy private subtype
4031
4032          Set_Ekind          (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
4033          Set_Etype          (Priv_Subtyp, Base_Type (Unc_Typ));
4034          Set_Scope          (Priv_Subtyp, Full_Subtyp);
4035          Set_Is_Constrained (Priv_Subtyp);
4036          Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
4037          Set_Is_Itype       (Priv_Subtyp);
4038          Set_Associated_Node_For_Itype (Priv_Subtyp, E);
4039
4040          if Is_Tagged_Type  (Priv_Subtyp) then
4041             Set_Class_Wide_Type
4042               (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
4043             Set_Primitive_Operations (Priv_Subtyp,
4044               Primitive_Operations (Unc_Typ));
4045          end if;
4046
4047          Set_Full_View (Priv_Subtyp, Full_Subtyp);
4048
4049          return New_Reference_To (Priv_Subtyp, Loc);
4050
4051       elsif Is_Array_Type (Unc_Typ) then
4052          for J in 1 .. Number_Dimensions (Unc_Typ) loop
4053             Append_To (List_Constr,
4054               Make_Range (Loc,
4055                 Low_Bound =>
4056                   Make_Attribute_Reference (Loc,
4057                     Prefix => Duplicate_Subexpr_No_Checks (E),
4058                     Attribute_Name => Name_First,
4059                     Expressions => New_List (
4060                       Make_Integer_Literal (Loc, J))),
4061
4062                 High_Bound =>
4063                   Make_Attribute_Reference (Loc,
4064                     Prefix         => Duplicate_Subexpr_No_Checks (E),
4065                     Attribute_Name => Name_Last,
4066                     Expressions    => New_List (
4067                       Make_Integer_Literal (Loc, J)))));
4068          end loop;
4069
4070       elsif Is_Class_Wide_Type (Unc_Typ) then
4071          declare
4072             CW_Subtype : Entity_Id;
4073             EQ_Typ     : Entity_Id := Empty;
4074
4075          begin
4076             --  A class-wide equivalent type is not needed when VM_Target
4077             --  because the VM back-ends handle the class-wide object
4078             --  initialization itself (and doesn't need or want the
4079             --  additional intermediate type to handle the assignment).
4080
4081             if Expander_Active and then Tagged_Type_Expansion then
4082
4083                --  If this is the class_wide type of a completion that is
4084                --  a record subtype, set the type of the class_wide type
4085                --  to be the full base type, for use in the expanded code
4086                --  for the equivalent type. Should this be done earlier when
4087                --  the completion is analyzed ???
4088
4089                if Is_Private_Type (Etype (Unc_Typ))
4090                  and then
4091                    Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
4092                then
4093                   Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
4094                end if;
4095
4096                EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
4097             end if;
4098
4099             CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
4100             Set_Equivalent_Type (CW_Subtype, EQ_Typ);
4101             Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
4102
4103             return New_Occurrence_Of (CW_Subtype, Loc);
4104          end;
4105
4106       --  Indefinite record type with discriminants
4107
4108       else
4109          D := First_Discriminant (Unc_Typ);
4110          while Present (D) loop
4111             Append_To (List_Constr,
4112               Make_Selected_Component (Loc,
4113                 Prefix        => Duplicate_Subexpr_No_Checks (E),
4114                 Selector_Name => New_Reference_To (D, Loc)));
4115
4116             Next_Discriminant (D);
4117          end loop;
4118       end if;
4119
4120       return
4121         Make_Subtype_Indication (Loc,
4122           Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
4123           Constraint   =>
4124             Make_Index_Or_Discriminant_Constraint (Loc,
4125               Constraints => List_Constr));
4126    end Make_Subtype_From_Expr;
4127
4128    -----------------------------
4129    -- May_Generate_Large_Temp --
4130    -----------------------------
4131
4132    --  At the current time, the only types that we return False for (i.e.
4133    --  where we decide we know they cannot generate large temps) are ones
4134    --  where we know the size is 256 bits or less at compile time, and we
4135    --  are still not doing a thorough job on arrays and records ???
4136
4137    function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
4138    begin
4139       if not Size_Known_At_Compile_Time (Typ) then
4140          return False;
4141
4142       elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
4143          return False;
4144
4145       elsif Is_Array_Type (Typ)
4146         and then Present (Packed_Array_Type (Typ))
4147       then
4148          return May_Generate_Large_Temp (Packed_Array_Type (Typ));
4149
4150       --  We could do more here to find other small types ???
4151
4152       else
4153          return True;
4154       end if;
4155    end May_Generate_Large_Temp;
4156
4157    ----------------------------
4158    -- New_Class_Wide_Subtype --
4159    ----------------------------
4160
4161    function New_Class_Wide_Subtype
4162      (CW_Typ : Entity_Id;
4163       N      : Node_Id) return Entity_Id
4164    is
4165       Res       : constant Entity_Id := Create_Itype (E_Void, N);
4166       Res_Name  : constant Name_Id   := Chars (Res);
4167       Res_Scope : constant Entity_Id := Scope (Res);
4168
4169    begin
4170       Copy_Node (CW_Typ, Res);
4171       Set_Comes_From_Source (Res, False);
4172       Set_Sloc (Res, Sloc (N));
4173       Set_Is_Itype (Res);
4174       Set_Associated_Node_For_Itype (Res, N);
4175       Set_Is_Public (Res, False);   --  By default, may be changed below.
4176       Set_Public_Status (Res);
4177       Set_Chars (Res, Res_Name);
4178       Set_Scope (Res, Res_Scope);
4179       Set_Ekind (Res, E_Class_Wide_Subtype);
4180       Set_Next_Entity (Res, Empty);
4181       Set_Etype (Res, Base_Type (CW_Typ));
4182       Set_Is_Frozen (Res, False);
4183       Set_Freeze_Node (Res, Empty);
4184       return (Res);
4185    end New_Class_Wide_Subtype;
4186
4187    --------------------------------
4188    -- Non_Limited_Designated_Type --
4189    ---------------------------------
4190
4191    function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
4192       Desig : constant Entity_Id := Designated_Type (T);
4193    begin
4194       if Ekind (Desig) = E_Incomplete_Type
4195         and then Present (Non_Limited_View (Desig))
4196       then
4197          return Non_Limited_View (Desig);
4198       else
4199          return Desig;
4200       end if;
4201    end Non_Limited_Designated_Type;
4202
4203    -----------------------------------
4204    -- OK_To_Do_Constant_Replacement --
4205    -----------------------------------
4206
4207    function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
4208       ES : constant Entity_Id := Scope (E);
4209       CS : Entity_Id;
4210
4211    begin
4212       --  Do not replace statically allocated objects, because they may be
4213       --  modified outside the current scope.
4214
4215       if Is_Statically_Allocated (E) then
4216          return False;
4217
4218       --  Do not replace aliased or volatile objects, since we don't know what
4219       --  else might change the value.
4220
4221       elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
4222          return False;
4223
4224       --  Debug flag -gnatdM disconnects this optimization
4225
4226       elsif Debug_Flag_MM then
4227          return False;
4228
4229       --  Otherwise check scopes
4230
4231       else
4232          CS := Current_Scope;
4233
4234          loop
4235             --  If we are in right scope, replacement is safe
4236
4237             if CS = ES then
4238                return True;
4239
4240             --  Packages do not affect the determination of safety
4241
4242             elsif Ekind (CS) = E_Package then
4243                exit when CS = Standard_Standard;
4244                CS := Scope (CS);
4245
4246             --  Blocks do not affect the determination of safety
4247
4248             elsif Ekind (CS) = E_Block then
4249                CS := Scope (CS);
4250
4251             --  Loops do not affect the determination of safety. Note that we
4252             --  kill all current values on entry to a loop, so we are just
4253             --  talking about processing within a loop here.
4254
4255             elsif Ekind (CS) = E_Loop then
4256                CS := Scope (CS);
4257
4258             --  Otherwise, the reference is dubious, and we cannot be sure that
4259             --  it is safe to do the replacement.
4260
4261             else
4262                exit;
4263             end if;
4264          end loop;
4265
4266          return False;
4267       end if;
4268    end OK_To_Do_Constant_Replacement;
4269
4270    ------------------------------------
4271    -- Possible_Bit_Aligned_Component --
4272    ------------------------------------
4273
4274    function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4275    begin
4276       case Nkind (N) is
4277
4278          --  Case of indexed component
4279
4280          when N_Indexed_Component =>
4281             declare
4282                P    : constant Node_Id   := Prefix (N);
4283                Ptyp : constant Entity_Id := Etype (P);
4284
4285             begin
4286                --  If we know the component size and it is less than 64, then
4287                --  we are definitely OK. The back end always does assignment of
4288                --  misaligned small objects correctly.
4289
4290                if Known_Static_Component_Size (Ptyp)
4291                  and then Component_Size (Ptyp) <= 64
4292                then
4293                   return False;
4294
4295                --  Otherwise, we need to test the prefix, to see if we are
4296                --  indexing from a possibly unaligned component.
4297
4298                else
4299                   return Possible_Bit_Aligned_Component (P);
4300                end if;
4301             end;
4302
4303          --  Case of selected component
4304
4305          when N_Selected_Component =>
4306             declare
4307                P    : constant Node_Id   := Prefix (N);
4308                Comp : constant Entity_Id := Entity (Selector_Name (N));
4309
4310             begin
4311                --  If there is no component clause, then we are in the clear
4312                --  since the back end will never misalign a large component
4313                --  unless it is forced to do so. In the clear means we need
4314                --  only the recursive test on the prefix.
4315
4316                if Component_May_Be_Bit_Aligned (Comp) then
4317                   return True;
4318                else
4319                   return Possible_Bit_Aligned_Component (P);
4320                end if;
4321             end;
4322
4323          --  For a slice, test the prefix, if that is possibly misaligned,
4324          --  then for sure the slice is!
4325
4326          when N_Slice =>
4327             return Possible_Bit_Aligned_Component (Prefix (N));
4328
4329          --  If we have none of the above, it means that we have fallen off the
4330          --  top testing prefixes recursively, and we now have a stand alone
4331          --  object, where we don't have a problem.
4332
4333          when others =>
4334             return False;
4335
4336       end case;
4337    end Possible_Bit_Aligned_Component;
4338
4339    -------------------------
4340    -- Remove_Side_Effects --
4341    -------------------------
4342
4343    procedure Remove_Side_Effects
4344      (Exp          : Node_Id;
4345       Name_Req     : Boolean := False;
4346       Variable_Ref : Boolean := False)
4347    is
4348       Loc          : constant Source_Ptr     := Sloc (Exp);
4349       Exp_Type     : constant Entity_Id      := Etype (Exp);
4350       Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4351       Def_Id       : Entity_Id;
4352       Ref_Type     : Entity_Id;
4353       Res          : Node_Id;
4354       Ptr_Typ_Decl : Node_Id;
4355       New_Exp      : Node_Id;
4356       E            : Node_Id;
4357
4358       function Side_Effect_Free (N : Node_Id) return Boolean;
4359       --  Determines if the tree N represents an expression that is known not
4360       --  to have side effects, and for which no processing is required.
4361
4362       function Side_Effect_Free (L : List_Id) return Boolean;
4363       --  Determines if all elements of the list L are side effect free
4364
4365       function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4366       --  The argument N is a construct where the Prefix is dereferenced if it
4367       --  is an access type and the result is a variable. The call returns True
4368       --  if the construct is side effect free (not considering side effects in
4369       --  other than the prefix which are to be tested by the caller).
4370
4371       function Within_In_Parameter (N : Node_Id) return Boolean;
4372       --  Determines if N is a subcomponent of a composite in-parameter. If so,
4373       --  N is not side-effect free when the actual is global and modifiable
4374       --  indirectly from within a subprogram, because it may be passed by
4375       --  reference. The front-end must be conservative here and assume that
4376       --  this may happen with any array or record type. On the other hand, we
4377       --  cannot create temporaries for all expressions for which this
4378       --  condition is true, for various reasons that might require clearing up
4379       --  ??? For example, discriminant references that appear out of place, or
4380       --  spurious type errors with class-wide expressions. As a result, we
4381       --  limit the transformation to loop bounds, which is so far the only
4382       --  case that requires it.
4383
4384       -----------------------------
4385       -- Safe_Prefixed_Reference --
4386       -----------------------------
4387
4388       function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4389       begin
4390          --  If prefix is not side effect free, definitely not safe
4391
4392          if not Side_Effect_Free (Prefix (N)) then
4393             return False;
4394
4395          --  If the prefix is of an access type that is not access-to-constant,
4396          --  then this construct is a variable reference, which means it is to
4397          --  be considered to have side effects if Variable_Ref is set True
4398          --  Exception is an access to an entity that is a constant or an
4399          --  in-parameter which does not come from source, and is the result
4400          --  of a previous removal of side-effects.
4401
4402          elsif Is_Access_Type (Etype (Prefix (N)))
4403            and then not Is_Access_Constant (Etype (Prefix (N)))
4404            and then Variable_Ref
4405          then
4406             if not Is_Entity_Name (Prefix (N)) then
4407                return False;
4408             else
4409                return Ekind (Entity (Prefix (N))) = E_Constant
4410                  or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4411             end if;
4412
4413          --  The following test is the simplest way of solving a complex
4414          --  problem uncovered by BB08-010: Side effect on loop bound that
4415          --  is a subcomponent of a global variable:
4416          --    If a loop bound is a subcomponent of a global variable, a
4417          --    modification of that variable within the loop may incorrectly
4418          --    affect the execution of the loop.
4419
4420          elsif not
4421            (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4422               or else not Within_In_Parameter (Prefix (N)))
4423          then
4424             return False;
4425
4426          --  All other cases are side effect free
4427
4428          else
4429             return True;
4430          end if;
4431       end Safe_Prefixed_Reference;
4432
4433       ----------------------
4434       -- Side_Effect_Free --
4435       ----------------------
4436
4437       function Side_Effect_Free (N : Node_Id) return Boolean is
4438       begin
4439          --  Note on checks that could raise Constraint_Error. Strictly, if
4440          --  we take advantage of 11.6, these checks do not count as side
4441          --  effects. However, we would just as soon consider that they are
4442          --  side effects, since the backend CSE does not work very well on
4443          --  expressions which can raise Constraint_Error. On the other
4444          --  hand, if we do not consider them to be side effect free, then
4445          --  we get some awkward expansions in -gnato mode, resulting in
4446          --  code insertions at a point where we do not have a clear model
4447          --  for performing the insertions.
4448
4449          --  Special handling for entity names
4450
4451          if Is_Entity_Name (N) then
4452
4453             --  If the entity is a constant, it is definitely side effect
4454             --  free. Note that the test of Is_Variable (N) below might
4455             --  be expected to catch this case, but it does not, because
4456             --  this test goes to the original tree, and we may have
4457             --  already rewritten a variable node with a constant as
4458             --  a result of an earlier Force_Evaluation call.
4459
4460             if Ekind_In (Entity (N), E_Constant, E_In_Parameter) then
4461                return True;
4462
4463             --  Functions are not side effect free
4464
4465             elsif Ekind (Entity (N)) = E_Function then
4466                return False;
4467
4468             --  Variables are considered to be a side effect if Variable_Ref
4469             --  is set or if we have a volatile reference and Name_Req is off.
4470             --  If Name_Req is True then we can't help returning a name which
4471             --  effectively allows multiple references in any case.
4472
4473             elsif Is_Variable (N) then
4474                return not Variable_Ref
4475                  and then (not Is_Volatile_Reference (N) or else Name_Req);
4476
4477             --  Any other entity (e.g. a subtype name) is definitely side
4478             --  effect free.
4479
4480             else
4481                return True;
4482             end if;
4483
4484          --  A value known at compile time is always side effect free
4485
4486          elsif Compile_Time_Known_Value (N) then
4487             return True;
4488
4489          --  A variable renaming is not side-effect free, because the
4490          --  renaming will function like a macro in the front-end in
4491          --  some cases, and an assignment can modify the component
4492          --  designated by N, so we need to create a temporary for it.
4493
4494          elsif Is_Entity_Name (Original_Node (N))
4495            and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
4496            and then Ekind (Entity (Original_Node (N))) /= E_Constant
4497          then
4498             return False;
4499          end if;
4500
4501          --  For other than entity names and compile time known values,
4502          --  check the node kind for special processing.
4503
4504          case Nkind (N) is
4505
4506             --  An attribute reference is side effect free if its expressions
4507             --  are side effect free and its prefix is side effect free or
4508             --  is an entity reference.
4509
4510             --  Is this right? what about x'first where x is a variable???
4511
4512             when N_Attribute_Reference =>
4513                return Side_Effect_Free (Expressions (N))
4514                  and then Attribute_Name (N) /= Name_Input
4515                  and then (Is_Entity_Name (Prefix (N))
4516                             or else Side_Effect_Free (Prefix (N)));
4517
4518             --  A binary operator is side effect free if and both operands
4519             --  are side effect free. For this purpose binary operators
4520             --  include membership tests and short circuit forms
4521
4522             when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
4523                return Side_Effect_Free (Left_Opnd  (N))
4524                         and then
4525                       Side_Effect_Free (Right_Opnd (N));
4526
4527             --  An explicit dereference is side effect free only if it is
4528             --  a side effect free prefixed reference.
4529
4530             when N_Explicit_Dereference =>
4531                return Safe_Prefixed_Reference (N);
4532
4533             --  A call to _rep_to_pos is side effect free, since we generate
4534             --  this pure function call ourselves. Moreover it is critically
4535             --  important to make this exception, since otherwise we can
4536             --  have discriminants in array components which don't look
4537             --  side effect free in the case of an array whose index type
4538             --  is an enumeration type with an enumeration rep clause.
4539
4540             --  All other function calls are not side effect free
4541
4542             when N_Function_Call =>
4543                return Nkind (Name (N)) = N_Identifier
4544                  and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4545                  and then
4546                    Side_Effect_Free (First (Parameter_Associations (N)));
4547
4548             --  An indexed component is side effect free if it is a side
4549             --  effect free prefixed reference and all the indexing
4550             --  expressions are side effect free.
4551
4552             when N_Indexed_Component =>
4553                return Side_Effect_Free (Expressions (N))
4554                  and then Safe_Prefixed_Reference (N);
4555
4556             --  A type qualification is side effect free if the expression
4557             --  is side effect free.
4558
4559             when N_Qualified_Expression =>
4560                return Side_Effect_Free (Expression (N));
4561
4562             --  A selected component is side effect free only if it is a
4563             --  side effect free prefixed reference. If it designates a
4564             --  component with a rep. clause it must be treated has having
4565             --  a potential side effect, because it may be modified through
4566             --  a renaming, and a subsequent use of the renaming as a macro
4567             --  will yield the wrong value. This complex interaction between
4568             --  renaming and removing side effects is a reminder that the
4569             --  latter has become a headache to maintain, and that it should
4570             --  be removed in favor of the gcc mechanism to capture values ???
4571
4572             when N_Selected_Component =>
4573                if Nkind (Parent (N)) = N_Explicit_Dereference
4574                  and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
4575                then
4576                   return False;
4577                else
4578                   return Safe_Prefixed_Reference (N);
4579                end if;
4580
4581             --  A range is side effect free if the bounds are side effect free
4582
4583             when N_Range =>
4584                return Side_Effect_Free (Low_Bound (N))
4585                  and then Side_Effect_Free (High_Bound (N));
4586
4587             --  A slice is side effect free if it is a side effect free
4588             --  prefixed reference and the bounds are side effect free.
4589
4590             when N_Slice =>
4591                return Side_Effect_Free (Discrete_Range (N))
4592                  and then Safe_Prefixed_Reference (N);
4593
4594             --  A type conversion is side effect free if the expression to be
4595             --  converted is side effect free.
4596
4597             when N_Type_Conversion =>
4598                return Side_Effect_Free (Expression (N));
4599
4600             --  A unary operator is side effect free if the operand
4601             --  is side effect free.
4602
4603             when N_Unary_Op =>
4604                return Side_Effect_Free (Right_Opnd (N));
4605
4606             --  An unchecked type conversion is side effect free only if it
4607             --  is safe and its argument is side effect free.
4608
4609             when N_Unchecked_Type_Conversion =>
4610                return Safe_Unchecked_Type_Conversion (N)
4611                  and then Side_Effect_Free (Expression (N));
4612
4613             --  An unchecked expression is side effect free if its expression
4614             --  is side effect free.
4615
4616             when N_Unchecked_Expression =>
4617                return Side_Effect_Free (Expression (N));
4618
4619             --  A literal is side effect free
4620
4621             when N_Character_Literal    |
4622                  N_Integer_Literal      |
4623                  N_Real_Literal         |
4624                  N_String_Literal       =>
4625                return True;
4626
4627             --  We consider that anything else has side effects. This is a bit
4628             --  crude, but we are pretty close for most common cases, and we
4629             --  are certainly correct (i.e. we never return True when the
4630             --  answer should be False).
4631
4632             when others =>
4633                return False;
4634          end case;
4635       end Side_Effect_Free;
4636
4637       --  A list is side effect free if all elements of the list are
4638       --  side effect free.
4639
4640       function Side_Effect_Free (L : List_Id) return Boolean is
4641          N : Node_Id;
4642
4643       begin
4644          if L = No_List or else L = Error_List then
4645             return True;
4646
4647          else
4648             N := First (L);
4649             while Present (N) loop
4650                if not Side_Effect_Free (N) then
4651                   return False;
4652                else
4653                   Next (N);
4654                end if;
4655             end loop;
4656
4657             return True;
4658          end if;
4659       end Side_Effect_Free;
4660
4661       -------------------------
4662       -- Within_In_Parameter --
4663       -------------------------
4664
4665       function Within_In_Parameter (N : Node_Id) return Boolean is
4666       begin
4667          if not Comes_From_Source (N) then
4668             return False;
4669
4670          elsif Is_Entity_Name (N) then
4671             return Ekind (Entity (N)) = E_In_Parameter;
4672
4673          elsif Nkind (N) = N_Indexed_Component
4674            or else Nkind (N) = N_Selected_Component
4675          then
4676             return Within_In_Parameter (Prefix (N));
4677          else
4678
4679             return False;
4680          end if;
4681       end Within_In_Parameter;
4682
4683    --  Start of processing for Remove_Side_Effects
4684
4685    begin
4686       --  If we are side effect free already or expansion is disabled,
4687       --  there is nothing to do.
4688
4689       if Side_Effect_Free (Exp) or else not Expander_Active then
4690          return;
4691       end if;
4692
4693       --  All this must not have any checks
4694
4695       Scope_Suppress := (others => True);
4696
4697       --  If it is a scalar type and we need to capture the value, just make
4698       --  a copy. Likewise for a function call, an attribute reference, an
4699       --  allocator, or an operator. And if we have a volatile reference and
4700       --  Name_Req is not set (see comments above for Side_Effect_Free).
4701
4702       if Is_Elementary_Type (Exp_Type)
4703         and then (Variable_Ref
4704                    or else Nkind (Exp) = N_Function_Call
4705                    or else Nkind (Exp) = N_Attribute_Reference
4706                    or else Nkind (Exp) = N_Allocator
4707                    or else Nkind (Exp) in N_Op
4708                    or else (not Name_Req and then Is_Volatile_Reference (Exp)))
4709       then
4710          Def_Id := Make_Temporary (Loc, 'R', Exp);
4711          Set_Etype (Def_Id, Exp_Type);
4712          Res := New_Reference_To (Def_Id, Loc);
4713
4714          E :=
4715            Make_Object_Declaration (Loc,
4716              Defining_Identifier => Def_Id,
4717              Object_Definition   => New_Reference_To (Exp_Type, Loc),
4718              Constant_Present    => True,
4719              Expression          => Relocate_Node (Exp));
4720
4721          --  Check if the previous node relocation requires readjustment of
4722          --  some SCIL Dispatching node.
4723
4724          if Generate_SCIL
4725            and then Nkind (Exp) = N_Function_Call
4726          then
4727             Adjust_SCIL_Node (Exp, Expression (E));
4728          end if;
4729
4730          Set_Assignment_OK (E);
4731          Insert_Action (Exp, E);
4732
4733       --  If the expression has the form v.all then we can just capture
4734       --  the pointer, and then do an explicit dereference on the result.
4735
4736       elsif Nkind (Exp) = N_Explicit_Dereference then
4737          Def_Id := Make_Temporary (Loc, 'R', Exp);
4738          Res :=
4739            Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4740
4741          Insert_Action (Exp,
4742            Make_Object_Declaration (Loc,
4743              Defining_Identifier => Def_Id,
4744              Object_Definition   =>
4745                New_Reference_To (Etype (Prefix (Exp)), Loc),
4746              Constant_Present    => True,
4747              Expression          => Relocate_Node (Prefix (Exp))));
4748
4749       --  Similar processing for an unchecked conversion of an expression
4750       --  of the form v.all, where we want the same kind of treatment.
4751
4752       elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4753         and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4754       then
4755          Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4756          Scope_Suppress := Svg_Suppress;
4757          return;
4758
4759       --  If this is a type conversion, leave the type conversion and remove
4760       --  the side effects in the expression. This is important in several
4761       --  circumstances: for change of representations, and also when this is
4762       --  a view conversion to a smaller object, where gigi can end up creating
4763       --  its own temporary of the wrong size.
4764
4765       elsif Nkind (Exp) = N_Type_Conversion then
4766          Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4767          Scope_Suppress := Svg_Suppress;
4768          return;
4769
4770       --  If this is an unchecked conversion that Gigi can't handle, make
4771       --  a copy or a use a renaming to capture the value.
4772
4773       elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4774         and then not Safe_Unchecked_Type_Conversion (Exp)
4775       then
4776          if CW_Or_Has_Controlled_Part (Exp_Type) then
4777
4778             --  Use a renaming to capture the expression, rather than create
4779             --  a controlled temporary.
4780
4781             Def_Id := Make_Temporary (Loc, 'R', Exp);
4782             Res := New_Reference_To (Def_Id, Loc);
4783
4784             Insert_Action (Exp,
4785               Make_Object_Renaming_Declaration (Loc,
4786                 Defining_Identifier => Def_Id,
4787                 Subtype_Mark        => New_Reference_To (Exp_Type, Loc),
4788                 Name                => Relocate_Node (Exp)));
4789
4790          else
4791             Def_Id := Make_Temporary (Loc, 'R', Exp);
4792             Set_Etype (Def_Id, Exp_Type);
4793             Res := New_Reference_To (Def_Id, Loc);
4794
4795             E :=
4796               Make_Object_Declaration (Loc,
4797                 Defining_Identifier => Def_Id,
4798                 Object_Definition   => New_Reference_To (Exp_Type, Loc),
4799                 Constant_Present    => not Is_Variable (Exp),
4800                 Expression          => Relocate_Node (Exp));
4801
4802             Set_Assignment_OK (E);
4803             Insert_Action (Exp, E);
4804          end if;
4805
4806       --  For expressions that denote objects, we can use a renaming scheme.
4807       --  We skip using this if we have a volatile reference and we do not
4808       --  have Name_Req set true (see comments above for Side_Effect_Free).
4809
4810       elsif Is_Object_Reference (Exp)
4811         and then Nkind (Exp) /= N_Function_Call
4812         and then (Name_Req or else not Is_Volatile_Reference (Exp))
4813       then
4814          Def_Id := Make_Temporary (Loc, 'R', Exp);
4815
4816          if Nkind (Exp) = N_Selected_Component
4817            and then Nkind (Prefix (Exp)) = N_Function_Call
4818            and then Is_Array_Type (Exp_Type)
4819          then
4820             --  Avoid generating a variable-sized temporary, by generating
4821             --  the renaming declaration just for the function call. The
4822             --  transformation could be refined to apply only when the array
4823             --  component is constrained by a discriminant???
4824
4825             Res :=
4826               Make_Selected_Component (Loc,
4827                 Prefix => New_Occurrence_Of (Def_Id, Loc),
4828                 Selector_Name => Selector_Name (Exp));
4829
4830             Insert_Action (Exp,
4831               Make_Object_Renaming_Declaration (Loc,
4832                 Defining_Identifier => Def_Id,
4833                 Subtype_Mark        =>
4834                   New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
4835                 Name                => Relocate_Node (Prefix (Exp))));
4836
4837          else
4838             Res := New_Reference_To (Def_Id, Loc);
4839
4840             Insert_Action (Exp,
4841               Make_Object_Renaming_Declaration (Loc,
4842                 Defining_Identifier => Def_Id,
4843                 Subtype_Mark        => New_Reference_To (Exp_Type, Loc),
4844                 Name                => Relocate_Node (Exp)));
4845          end if;
4846
4847          --  If this is a packed reference, or a selected component with a
4848          --  non-standard representation, a reference to the temporary will
4849          --  be replaced by a copy of the original expression (see
4850          --  Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
4851          --  elaborated by gigi, and is of course not to be replaced in-line
4852          --  by the expression it renames, which would defeat the purpose of
4853          --  removing the side-effect.
4854
4855          if (Nkind (Exp) = N_Selected_Component
4856               or else Nkind (Exp) = N_Indexed_Component)
4857            and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
4858          then
4859             null;
4860          else
4861             Set_Is_Renaming_Of_Object (Def_Id, False);
4862          end if;
4863
4864       --  Otherwise we generate a reference to the value
4865
4866       else
4867          --  Special processing for function calls that return a limited type.
4868          --  We need to build a declaration that will enable build-in-place
4869          --  expansion of the call. This is not done if the context is already
4870          --  an object declaration, to prevent infinite recursion.
4871
4872          --  This is relevant only in Ada 2005 mode. In Ada 95 programs we have
4873          --  to accommodate functions returning limited objects by reference.
4874
4875          if Nkind (Exp) = N_Function_Call
4876            and then Is_Inherently_Limited_Type (Etype (Exp))
4877            and then Nkind (Parent (Exp)) /= N_Object_Declaration
4878            and then Ada_Version >= Ada_05
4879          then
4880             declare
4881                Obj  : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
4882                Decl : Node_Id;
4883
4884             begin
4885                Decl :=
4886                  Make_Object_Declaration (Loc,
4887                    Defining_Identifier => Obj,
4888                    Object_Definition   => New_Occurrence_Of (Exp_Type, Loc),
4889                    Expression          => Relocate_Node (Exp));
4890
4891                --  Check if the previous node relocation requires readjustment
4892                --  of some SCIL Dispatching node.
4893
4894                if Generate_SCIL
4895                  and then Nkind (Exp) = N_Function_Call
4896                then
4897                   Adjust_SCIL_Node (Exp, Expression (Decl));
4898                end if;
4899
4900                Insert_Action (Exp, Decl);
4901                Set_Etype (Obj, Exp_Type);
4902                Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
4903                return;
4904             end;
4905          end if;
4906
4907          Ref_Type := Make_Temporary (Loc, 'A');
4908
4909          Ptr_Typ_Decl :=
4910            Make_Full_Type_Declaration (Loc,
4911              Defining_Identifier => Ref_Type,
4912              Type_Definition =>
4913                Make_Access_To_Object_Definition (Loc,
4914                  All_Present => True,
4915                  Subtype_Indication =>
4916                    New_Reference_To (Exp_Type, Loc)));
4917
4918          E := Exp;
4919          Insert_Action (Exp, Ptr_Typ_Decl);
4920
4921          Def_Id := Make_Temporary (Loc, 'R', Exp);
4922          Set_Etype (Def_Id, Exp_Type);
4923
4924          Res :=
4925            Make_Explicit_Dereference (Loc,
4926              Prefix => New_Reference_To (Def_Id, Loc));
4927
4928          if Nkind (E) = N_Explicit_Dereference then
4929             New_Exp := Relocate_Node (Prefix (E));
4930          else
4931             E := Relocate_Node (E);
4932             New_Exp := Make_Reference (Loc, E);
4933          end if;
4934
4935          if Is_Delayed_Aggregate (E) then
4936
4937             --  The expansion of nested aggregates is delayed until the
4938             --  enclosing aggregate is expanded. As aggregates are often
4939             --  qualified, the predicate applies to qualified expressions
4940             --  as well, indicating that the enclosing aggregate has not
4941             --  been expanded yet. At this point the aggregate is part of
4942             --  a stand-alone declaration, and must be fully expanded.
4943
4944             if Nkind (E) = N_Qualified_Expression then
4945                Set_Expansion_Delayed (Expression (E), False);
4946                Set_Analyzed (Expression (E), False);
4947             else
4948                Set_Expansion_Delayed (E, False);
4949             end if;
4950
4951             Set_Analyzed (E, False);
4952          end if;
4953
4954          Insert_Action (Exp,
4955            Make_Object_Declaration (Loc,
4956              Defining_Identifier => Def_Id,
4957              Object_Definition   => New_Reference_To (Ref_Type, Loc),
4958              Expression          => New_Exp));
4959
4960          --  Check if the previous node relocation requires readjustment
4961          --  of some SCIL Dispatching node.
4962
4963          if Generate_SCIL
4964            and then Nkind (Exp) = N_Function_Call
4965          then
4966             Adjust_SCIL_Node (Exp, Prefix (New_Exp));
4967          end if;
4968       end if;
4969
4970       --  Preserve the Assignment_OK flag in all copies, since at least
4971       --  one copy may be used in a context where this flag must be set
4972       --  (otherwise why would the flag be set in the first place).
4973
4974       Set_Assignment_OK (Res, Assignment_OK (Exp));
4975
4976       --  Finally rewrite the original expression and we are done
4977
4978       Rewrite (Exp, Res);
4979       Analyze_And_Resolve (Exp, Exp_Type);
4980       Scope_Suppress := Svg_Suppress;
4981    end Remove_Side_Effects;
4982
4983    ---------------------------
4984    -- Represented_As_Scalar --
4985    ---------------------------
4986
4987    function Represented_As_Scalar (T : Entity_Id) return Boolean is
4988       UT : constant Entity_Id := Underlying_Type (T);
4989    begin
4990       return Is_Scalar_Type (UT)
4991         or else (Is_Bit_Packed_Array (UT)
4992                    and then Is_Scalar_Type (Packed_Array_Type (UT)));
4993    end Represented_As_Scalar;
4994
4995    ------------------------------------
4996    -- Safe_Unchecked_Type_Conversion --
4997    ------------------------------------
4998
4999    --  Note: this function knows quite a bit about the exact requirements
5000    --  of Gigi with respect to unchecked type conversions, and its code
5001    --  must be coordinated with any changes in Gigi in this area.
5002
5003    --  The above requirements should be documented in Sinfo ???
5004
5005    function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
5006       Otyp   : Entity_Id;
5007       Ityp   : Entity_Id;
5008       Oalign : Uint;
5009       Ialign : Uint;
5010       Pexp   : constant Node_Id := Parent (Exp);
5011
5012    begin
5013       --  If the expression is the RHS of an assignment or object declaration
5014       --   we are always OK because there will always be a target.
5015
5016       --  Object renaming declarations, (generated for view conversions of
5017       --  actuals in inlined calls), like object declarations, provide an
5018       --  explicit type, and are safe as well.
5019
5020       if (Nkind (Pexp) = N_Assignment_Statement
5021            and then Expression (Pexp) = Exp)
5022         or else Nkind (Pexp) = N_Object_Declaration
5023         or else Nkind (Pexp) = N_Object_Renaming_Declaration
5024       then
5025          return True;
5026
5027       --  If the expression is the prefix of an N_Selected_Component
5028       --  we should also be OK because GCC knows to look inside the
5029       --  conversion except if the type is discriminated. We assume
5030       --  that we are OK anyway if the type is not set yet or if it is
5031       --  controlled since we can't afford to introduce a temporary in
5032       --  this case.
5033
5034       elsif Nkind (Pexp) = N_Selected_Component
5035          and then Prefix (Pexp) = Exp
5036       then
5037          if No (Etype (Pexp)) then
5038             return True;
5039          else
5040             return
5041               not Has_Discriminants (Etype (Pexp))
5042                 or else Is_Constrained (Etype (Pexp));
5043          end if;
5044       end if;
5045
5046       --  Set the output type, this comes from Etype if it is set, otherwise
5047       --  we take it from the subtype mark, which we assume was already
5048       --  fully analyzed.
5049
5050       if Present (Etype (Exp)) then
5051          Otyp := Etype (Exp);
5052       else
5053          Otyp := Entity (Subtype_Mark (Exp));
5054       end if;
5055
5056       --  The input type always comes from the expression, and we assume
5057       --  this is indeed always analyzed, so we can simply get the Etype.
5058
5059       Ityp := Etype (Expression (Exp));
5060
5061       --  Initialize alignments to unknown so far
5062
5063       Oalign := No_Uint;
5064       Ialign := No_Uint;
5065
5066       --  Replace a concurrent type by its corresponding record type
5067       --  and each type by its underlying type and do the tests on those.
5068       --  The original type may be a private type whose completion is a
5069       --  concurrent type, so find the underlying type first.
5070
5071       if Present (Underlying_Type (Otyp)) then
5072          Otyp := Underlying_Type (Otyp);
5073       end if;
5074
5075       if Present (Underlying_Type (Ityp)) then
5076          Ityp := Underlying_Type (Ityp);
5077       end if;
5078
5079       if Is_Concurrent_Type (Otyp) then
5080          Otyp := Corresponding_Record_Type (Otyp);
5081       end if;
5082
5083       if Is_Concurrent_Type (Ityp) then
5084          Ityp := Corresponding_Record_Type (Ityp);
5085       end if;
5086
5087       --  If the base types are the same, we know there is no problem since
5088       --  this conversion will be a noop.
5089
5090       if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
5091          return True;
5092
5093       --  Same if this is an upwards conversion of an untagged type, and there
5094       --  are no constraints involved (could be more general???)
5095
5096       elsif Etype (Ityp) = Otyp
5097         and then not Is_Tagged_Type (Ityp)
5098         and then not Has_Discriminants (Ityp)
5099         and then No (First_Rep_Item (Base_Type (Ityp)))
5100       then
5101          return True;
5102
5103       --  If the expression has an access type (object or subprogram) we
5104       --  assume that the conversion is safe, because the size of the target
5105       --  is safe, even if it is a record (which might be treated as having
5106       --  unknown size at this point).
5107
5108       elsif Is_Access_Type (Ityp) then
5109          return True;
5110
5111       --  If the size of output type is known at compile time, there is
5112       --  never a problem.  Note that unconstrained records are considered
5113       --  to be of known size, but we can't consider them that way here,
5114       --  because we are talking about the actual size of the object.
5115
5116       --  We also make sure that in addition to the size being known, we do
5117       --  not have a case which might generate an embarrassingly large temp
5118       --  in stack checking mode.
5119
5120       elsif Size_Known_At_Compile_Time (Otyp)
5121         and then
5122           (not Stack_Checking_Enabled
5123              or else not May_Generate_Large_Temp (Otyp))
5124         and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
5125       then
5126          return True;
5127
5128       --  If either type is tagged, then we know the alignment is OK so
5129       --  Gigi will be able to use pointer punning.
5130
5131       elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
5132          return True;
5133
5134       --  If either type is a limited record type, we cannot do a copy, so
5135       --  say safe since there's nothing else we can do.
5136
5137       elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
5138          return True;
5139
5140       --  Conversions to and from packed array types are always ignored and
5141       --  hence are safe.
5142
5143       elsif Is_Packed_Array_Type (Otyp)
5144         or else Is_Packed_Array_Type (Ityp)
5145       then
5146          return True;
5147       end if;
5148
5149       --  The only other cases known to be safe is if the input type's
5150       --  alignment is known to be at least the maximum alignment for the
5151       --  target or if both alignments are known and the output type's
5152       --  alignment is no stricter than the input's.  We can use the alignment
5153       --  of the component type of an array if a type is an unpacked
5154       --  array type.
5155
5156       if Present (Alignment_Clause (Otyp)) then
5157          Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
5158
5159       elsif Is_Array_Type (Otyp)
5160         and then Present (Alignment_Clause (Component_Type (Otyp)))
5161       then
5162          Oalign := Expr_Value (Expression (Alignment_Clause
5163                                            (Component_Type (Otyp))));
5164       end if;
5165
5166       if Present (Alignment_Clause (Ityp)) then
5167          Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
5168
5169       elsif Is_Array_Type (Ityp)
5170         and then Present (Alignment_Clause (Component_Type (Ityp)))
5171       then
5172          Ialign := Expr_Value (Expression (Alignment_Clause
5173                                            (Component_Type (Ityp))));
5174       end if;
5175
5176       if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
5177          return True;
5178
5179       elsif Ialign /= No_Uint and then Oalign /= No_Uint
5180         and then Ialign <= Oalign
5181       then
5182          return True;
5183
5184       --   Otherwise, Gigi cannot handle this and we must make a temporary
5185
5186       else
5187          return False;
5188       end if;
5189    end Safe_Unchecked_Type_Conversion;
5190
5191    ---------------------------------
5192    -- Set_Current_Value_Condition --
5193    ---------------------------------
5194
5195    --  Note: the implementation of this procedure is very closely tied to the
5196    --  implementation of Get_Current_Value_Condition. Here we set required
5197    --  Current_Value fields, and in Get_Current_Value_Condition, we interpret
5198    --  them, so they must have a consistent view.
5199
5200    procedure Set_Current_Value_Condition (Cnode : Node_Id) is
5201
5202       procedure Set_Entity_Current_Value (N : Node_Id);
5203       --  If N is an entity reference, where the entity is of an appropriate
5204       --  kind, then set the current value of this entity to Cnode, unless
5205       --  there is already a definite value set there.
5206
5207       procedure Set_Expression_Current_Value (N : Node_Id);
5208       --  If N is of an appropriate form, sets an appropriate entry in current
5209       --  value fields of relevant entities. Multiple entities can be affected
5210       --  in the case of an AND or AND THEN.
5211
5212       ------------------------------
5213       -- Set_Entity_Current_Value --
5214       ------------------------------
5215
5216       procedure Set_Entity_Current_Value (N : Node_Id) is
5217       begin
5218          if Is_Entity_Name (N) then
5219             declare
5220                Ent : constant Entity_Id := Entity (N);
5221
5222             begin
5223                --  Don't capture if not safe to do so
5224
5225                if not Safe_To_Capture_Value (N, Ent, Cond => True) then
5226                   return;
5227                end if;
5228
5229                --  Here we have a case where the Current_Value field may
5230                --  need to be set. We set it if it is not already set to a
5231                --  compile time expression value.
5232
5233                --  Note that this represents a decision that one condition
5234                --  blots out another previous one. That's certainly right
5235                --  if they occur at the same level. If the second one is
5236                --  nested, then the decision is neither right nor wrong (it
5237                --  would be equally OK to leave the outer one in place, or
5238                --  take the new inner one. Really we should record both, but
5239                --  our data structures are not that elaborate.
5240
5241                if Nkind (Current_Value (Ent)) not in N_Subexpr then
5242                   Set_Current_Value (Ent, Cnode);
5243                end if;
5244             end;
5245          end if;
5246       end Set_Entity_Current_Value;
5247
5248       ----------------------------------
5249       -- Set_Expression_Current_Value --
5250       ----------------------------------
5251
5252       procedure Set_Expression_Current_Value (N : Node_Id) is
5253          Cond : Node_Id;
5254
5255       begin
5256          Cond := N;
5257
5258          --  Loop to deal with (ignore for now) any NOT operators present. The
5259          --  presence of NOT operators will be handled properly when we call
5260          --  Get_Current_Value_Condition.
5261
5262          while Nkind (Cond) = N_Op_Not loop
5263             Cond := Right_Opnd (Cond);
5264          end loop;
5265
5266          --  For an AND or AND THEN, recursively process operands
5267
5268          if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
5269             Set_Expression_Current_Value (Left_Opnd (Cond));
5270             Set_Expression_Current_Value (Right_Opnd (Cond));
5271             return;
5272          end if;
5273
5274          --  Check possible relational operator
5275
5276          if Nkind (Cond) in N_Op_Compare then
5277             if Compile_Time_Known_Value (Right_Opnd (Cond)) then
5278                Set_Entity_Current_Value (Left_Opnd (Cond));
5279             elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
5280                Set_Entity_Current_Value (Right_Opnd (Cond));
5281             end if;
5282
5283             --  Check possible boolean variable reference
5284
5285          else
5286             Set_Entity_Current_Value (Cond);
5287          end if;
5288       end Set_Expression_Current_Value;
5289
5290    --  Start of processing for Set_Current_Value_Condition
5291
5292    begin
5293       Set_Expression_Current_Value (Condition (Cnode));
5294    end Set_Current_Value_Condition;
5295
5296    --------------------------
5297    -- Set_Elaboration_Flag --
5298    --------------------------
5299
5300    procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
5301       Loc : constant Source_Ptr := Sloc (N);
5302       Ent : constant Entity_Id  := Elaboration_Entity (Spec_Id);
5303       Asn : Node_Id;
5304
5305    begin
5306       if Present (Ent) then
5307
5308          --  Nothing to do if at the compilation unit level, because in this
5309          --  case the flag is set by the binder generated elaboration routine.
5310
5311          if Nkind (Parent (N)) = N_Compilation_Unit then
5312             null;
5313
5314          --  Here we do need to generate an assignment statement
5315
5316          else
5317             Check_Restriction (No_Elaboration_Code, N);
5318             Asn :=
5319               Make_Assignment_Statement (Loc,
5320                 Name       => New_Occurrence_Of (Ent, Loc),
5321                 Expression => New_Occurrence_Of (Standard_True, Loc));
5322
5323             if Nkind (Parent (N)) = N_Subunit then
5324                Insert_After (Corresponding_Stub (Parent (N)), Asn);
5325             else
5326                Insert_After (N, Asn);
5327             end if;
5328
5329             Analyze (Asn);
5330
5331             --  Kill current value indication. This is necessary because the
5332             --  tests of this flag are inserted out of sequence and must not
5333             --  pick up bogus indications of the wrong constant value.
5334
5335             Set_Current_Value (Ent, Empty);
5336          end if;
5337       end if;
5338    end Set_Elaboration_Flag;
5339
5340    ----------------------------
5341    -- Set_Renamed_Subprogram --
5342    ----------------------------
5343
5344    procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
5345    begin
5346       --  If input node is an identifier, we can just reset it
5347
5348       if Nkind (N) = N_Identifier then
5349          Set_Chars  (N, Chars (E));
5350          Set_Entity (N, E);
5351
5352          --  Otherwise we have to do a rewrite, preserving Comes_From_Source
5353
5354       else
5355          declare
5356             CS : constant Boolean := Comes_From_Source (N);
5357          begin
5358             Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
5359             Set_Entity (N, E);
5360             Set_Comes_From_Source (N, CS);
5361             Set_Analyzed (N, True);
5362          end;
5363       end if;
5364    end Set_Renamed_Subprogram;
5365
5366    ----------------------------------
5367    -- Silly_Boolean_Array_Not_Test --
5368    ----------------------------------
5369
5370    --  This procedure implements an odd and silly test. We explicitly check
5371    --  for the case where the 'First of the component type is equal to the
5372    --  'Last of this component type, and if this is the case, we make sure
5373    --  that constraint error is raised. The reason is that the NOT is bound
5374    --  to cause CE in this case, and we will not otherwise catch it.
5375
5376    --  No such check is required for AND and OR, since for both these cases
5377    --  False op False = False, and True op True = True. For the XOR case,
5378    --  see Silly_Boolean_Array_Xor_Test.
5379
5380    --  Believe it or not, this was reported as a bug. Note that nearly
5381    --  always, the test will evaluate statically to False, so the code will
5382    --  be statically removed, and no extra overhead caused.
5383
5384    procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
5385       Loc : constant Source_Ptr := Sloc (N);
5386       CT  : constant Entity_Id  := Component_Type (T);
5387
5388    begin
5389       --  The check we install is
5390
5391       --    constraint_error when
5392       --      component_type'first = component_type'last
5393       --        and then array_type'Length /= 0)
5394
5395       --  We need the last guard because we don't want to raise CE for empty
5396       --  arrays since no out of range values result. (Empty arrays with a
5397       --  component type of True .. True -- very useful -- even the ACATS
5398       --  does not test that marginal case!)
5399
5400       Insert_Action (N,
5401         Make_Raise_Constraint_Error (Loc,
5402           Condition =>
5403             Make_And_Then (Loc,
5404               Left_Opnd =>
5405                 Make_Op_Eq (Loc,
5406                   Left_Opnd =>
5407                     Make_Attribute_Reference (Loc,
5408                       Prefix         => New_Occurrence_Of (CT, Loc),
5409                       Attribute_Name => Name_First),
5410
5411                   Right_Opnd =>
5412                     Make_Attribute_Reference (Loc,
5413                       Prefix         => New_Occurrence_Of (CT, Loc),
5414                       Attribute_Name => Name_Last)),
5415
5416               Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5417           Reason => CE_Range_Check_Failed));
5418    end Silly_Boolean_Array_Not_Test;
5419
5420    ----------------------------------
5421    -- Silly_Boolean_Array_Xor_Test --
5422    ----------------------------------
5423
5424    --  This procedure implements an odd and silly test. We explicitly check
5425    --  for the XOR case where the component type is True .. True, since this
5426    --  will raise constraint error. A special check is required since CE
5427    --  will not be generated otherwise (cf Expand_Packed_Not).
5428
5429    --  No such check is required for AND and OR, since for both these cases
5430    --  False op False = False, and True op True = True, and no check is
5431    --  required for the case of False .. False, since False xor False = False.
5432    --  See also Silly_Boolean_Array_Not_Test
5433
5434    procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
5435       Loc : constant Source_Ptr := Sloc (N);
5436       CT  : constant Entity_Id  := Component_Type (T);
5437
5438    begin
5439       --  The check we install is
5440
5441       --    constraint_error when
5442       --      Boolean (component_type'First)
5443       --        and then Boolean (component_type'Last)
5444       --        and then array_type'Length /= 0)
5445
5446       --  We need the last guard because we don't want to raise CE for empty
5447       --  arrays since no out of range values result (Empty arrays with a
5448       --  component type of True .. True -- very useful -- even the ACATS
5449       --  does not test that marginal case!).
5450
5451       Insert_Action (N,
5452         Make_Raise_Constraint_Error (Loc,
5453           Condition =>
5454             Make_And_Then (Loc,
5455               Left_Opnd =>
5456                 Make_And_Then (Loc,
5457                   Left_Opnd =>
5458                     Convert_To (Standard_Boolean,
5459                       Make_Attribute_Reference (Loc,
5460                         Prefix         => New_Occurrence_Of (CT, Loc),
5461                         Attribute_Name => Name_First)),
5462
5463                   Right_Opnd =>
5464                     Convert_To (Standard_Boolean,
5465                       Make_Attribute_Reference (Loc,
5466                         Prefix         => New_Occurrence_Of (CT, Loc),
5467                         Attribute_Name => Name_Last))),
5468
5469               Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5470           Reason => CE_Range_Check_Failed));
5471    end Silly_Boolean_Array_Xor_Test;
5472
5473    --------------------------
5474    -- Target_Has_Fixed_Ops --
5475    --------------------------
5476
5477    Integer_Sized_Small : Ureal;
5478    --  Set to 2.0 ** -(Integer'Size - 1) the first time that this
5479    --  function is called (we don't want to compute it more than once!)
5480
5481    Long_Integer_Sized_Small : Ureal;
5482    --  Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
5483    --  function is called (we don't want to compute it more than once)
5484
5485    First_Time_For_THFO : Boolean := True;
5486    --  Set to False after first call (if Fractional_Fixed_Ops_On_Target)
5487
5488    function Target_Has_Fixed_Ops
5489      (Left_Typ   : Entity_Id;
5490       Right_Typ  : Entity_Id;
5491       Result_Typ : Entity_Id) return Boolean
5492    is
5493       function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
5494       --  Return True if the given type is a fixed-point type with a small
5495       --  value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
5496       --  an absolute value less than 1.0. This is currently limited
5497       --  to fixed-point types that map to Integer or Long_Integer.
5498
5499       ------------------------
5500       -- Is_Fractional_Type --
5501       ------------------------
5502
5503       function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5504       begin
5505          if Esize (Typ) = Standard_Integer_Size then
5506             return Small_Value (Typ) = Integer_Sized_Small;
5507
5508          elsif Esize (Typ) = Standard_Long_Integer_Size then
5509             return Small_Value (Typ) = Long_Integer_Sized_Small;
5510
5511          else
5512             return False;
5513          end if;
5514       end Is_Fractional_Type;
5515
5516    --  Start of processing for Target_Has_Fixed_Ops
5517
5518    begin
5519       --  Return False if Fractional_Fixed_Ops_On_Target is false
5520
5521       if not Fractional_Fixed_Ops_On_Target then
5522          return False;
5523       end if;
5524
5525       --  Here the target has Fractional_Fixed_Ops, if first time, compute
5526       --  standard constants used by Is_Fractional_Type.
5527
5528       if First_Time_For_THFO then
5529          First_Time_For_THFO := False;
5530
5531          Integer_Sized_Small :=
5532            UR_From_Components
5533              (Num   => Uint_1,
5534               Den   => UI_From_Int (Standard_Integer_Size - 1),
5535               Rbase => 2);
5536
5537          Long_Integer_Sized_Small :=
5538            UR_From_Components
5539              (Num   => Uint_1,
5540               Den   => UI_From_Int (Standard_Long_Integer_Size - 1),
5541               Rbase => 2);
5542       end if;
5543
5544       --  Return True if target supports fixed-by-fixed multiply/divide
5545       --  for fractional fixed-point types (see Is_Fractional_Type) and
5546       --  the operand and result types are equivalent fractional types.
5547
5548       return Is_Fractional_Type (Base_Type (Left_Typ))
5549         and then Is_Fractional_Type (Base_Type (Right_Typ))
5550         and then Is_Fractional_Type (Base_Type (Result_Typ))
5551         and then Esize (Left_Typ) = Esize (Right_Typ)
5552         and then Esize (Left_Typ) = Esize (Result_Typ);
5553    end Target_Has_Fixed_Ops;
5554
5555    ------------------------------------------
5556    -- Type_May_Have_Bit_Aligned_Components --
5557    ------------------------------------------
5558
5559    function Type_May_Have_Bit_Aligned_Components
5560      (Typ : Entity_Id) return Boolean
5561    is
5562    begin
5563       --  Array type, check component type
5564
5565       if Is_Array_Type (Typ) then
5566          return
5567            Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5568
5569       --  Record type, check components
5570
5571       elsif Is_Record_Type (Typ) then
5572          declare
5573             E : Entity_Id;
5574
5575          begin
5576             E := First_Component_Or_Discriminant (Typ);
5577             while Present (E) loop
5578                if Component_May_Be_Bit_Aligned (E)
5579                  or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5580                then
5581                   return True;
5582                end if;
5583
5584                Next_Component_Or_Discriminant (E);
5585             end loop;
5586
5587             return False;
5588          end;
5589
5590       --  Type other than array or record is always OK
5591
5592       else
5593          return False;
5594       end if;
5595    end Type_May_Have_Bit_Aligned_Components;
5596
5597    ----------------------------
5598    -- Wrap_Cleanup_Procedure --
5599    ----------------------------
5600
5601    procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5602       Loc   : constant Source_Ptr := Sloc (N);
5603       Stseq : constant Node_Id    := Handled_Statement_Sequence (N);
5604       Stmts : constant List_Id    := Statements (Stseq);
5605
5606    begin
5607       if Abort_Allowed then
5608          Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5609          Append_To  (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5610       end if;
5611    end Wrap_Cleanup_Procedure;
5612
5613 end Exp_Util;